/******************************************************************************* * Copyright (c) 2014-2015 IBM Corporation. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Zurich Research Lab - initial API, implementation and documentation *******************************************************************************/ #ifndef _lorabase_h_ #define _lorabase_h_ #ifdef __cplusplus extern "C"{ #endif // ================================================================================ // BEG: Keep in sync with lorabase.hpp // enum _cr_t { CR_4_5=0, CR_4_6, CR_4_7, CR_4_8 }; enum _sf_t { FSK=0, SF7, SF8, SF9, SF10, SF11, SF12, SFrfu }; enum _bw_t { BW125=0, BW250, BW500, BWrfu }; typedef u1_t cr_t; typedef u1_t sf_t; typedef u1_t bw_t; typedef u1_t dr_t; // Radio parameter set (encodes SF/BW/CR/IH/NOCRC) typedef u2_t rps_t; TYPEDEF_xref2rps_t; enum { ILLEGAL_RPS = 0xFF }; enum { DR_PAGE_EU868 = 0x00 }; enum { DR_PAGE_US915 = 0x10 }; // Global maximum frame length enum { STD_PREAMBLE_LEN = 8 }; enum { MAX_LEN_FRAME = 64 }; enum { LEN_DEVNONCE = 2 }; enum { LEN_ARTNONCE = 3 }; enum { LEN_NETID = 3 }; enum { DELAY_JACC1 = 5 }; // in secs enum { DELAY_DNW1 = 1 }; // in secs down window #1 enum { DELAY_EXTDNW2 = 1 }; // in secs enum { DELAY_JACC2 = DELAY_JACC1+(int)DELAY_EXTDNW2 }; // in secs enum { DELAY_DNW2 = DELAY_DNW1 +(int)DELAY_EXTDNW2 }; // in secs down window #1 enum { BCN_INTV_exp = 7 }; enum { BCN_INTV_sec = 1<> 3) & 0x3); } inline rps_t setBw (rps_t params, bw_t cr) { return (rps_t)((params & ~0x18) | (cr<<3)); } inline cr_t getCr (rps_t params) { return (cr_t)((params >> 5) & 0x3); } inline rps_t setCr (rps_t params, cr_t cr) { return (rps_t)((params & ~0x60) | (cr<<5)); } inline int getNocrc(rps_t params) { return ((params >> 7) & 0x1); } inline rps_t setNocrc(rps_t params, int nocrc) { return (rps_t)((params & ~0x80) | (nocrc<<7)); } inline int getIh (rps_t params) { return ((params >> 8) & 0xFF); } inline rps_t setIh (rps_t params, int ih) { return (rps_t)((params & ~0xFF00) | (ih<<8)); } inline rps_t makeRps (sf_t sf, bw_t bw, cr_t cr, int ih, int nocrc) { return sf | (bw<<3) | (cr<<5) | (nocrc?(1<<7):0) | ((ih&0xFF)<<8); } #define MAKERPS(sf,bw,cr,ih,nocrc) ((rps_t)((sf) | ((bw)<<3) | ((cr)<<5) | ((nocrc)?(1<<7):0) | ((ih&0xFF)<<8))) // Two frames with params r1/r2 would interfere on air: same SFx + BWx inline int sameSfBw(rps_t r1, rps_t r2) { return ((r1^r2)&0x1F) == 0; } extern CONST_TABLE(u1_t, _DR2RPS_CRC)[]; inline rps_t updr2rps (dr_t dr) { return (rps_t)TABLE_GET_U1(_DR2RPS_CRC, dr+1); } inline rps_t dndr2rps (dr_t dr) { return setNocrc(updr2rps(dr),1); } inline int isFasterDR (dr_t dr1, dr_t dr2) { return dr1 > dr2; } inline int isSlowerDR (dr_t dr1, dr_t dr2) { return dr1 < dr2; } inline dr_t incDR (dr_t dr) { return TABLE_GET_U1(_DR2RPS_CRC, dr+2)==ILLEGAL_RPS ? dr : (dr_t)(dr+1); } // increase data rate inline dr_t decDR (dr_t dr) { return TABLE_GET_U1(_DR2RPS_CRC, dr )==ILLEGAL_RPS ? dr : (dr_t)(dr-1); } // decrease data rate inline dr_t assertDR (dr_t dr) { return TABLE_GET_U1(_DR2RPS_CRC, dr+1)==ILLEGAL_RPS ? DR_DFLTMIN : dr; } // force into a valid DR inline bit_t validDR (dr_t dr) { return TABLE_GET_U1(_DR2RPS_CRC, dr+1)!=ILLEGAL_RPS; } // in range inline dr_t lowerDR (dr_t dr, u1_t n) { while(n--){dr=decDR(dr);} return dr; } // decrease data rate by n steps // // BEG: Keep in sync with lorabase.hpp // ================================================================================ // Convert between dBm values and power codes (MCMD_LADR_XdBm) s1_t pow2dBm (u1_t mcmd_ladr_p1); // Calculate airtime ostime_t calcAirTime (rps_t rps, u1_t plen); // Sensitivity at given SF/BW int getSensitivity (rps_t rps); #ifdef __cplusplus } // extern "C" #endif #endif // _lorabase_h_