Skip to content
This repository has been archived by the owner on May 2, 2024. It is now read-only.

Commit

Permalink
update LMIC to 4.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rgot-org committed Apr 1, 2023
1 parent f6c0dca commit 3457328
Show file tree
Hide file tree
Showing 36 changed files with 1,231 additions and 785 deletions.
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=TTN_esp32
version=0.1.3
version=0.1.4
author=Francois Riotte
maintainer=Francois Riotte <esc@rgot.org>
sentence=ESP 32 port of the Arduino TheThingsNetwork library.
Expand Down
4 changes: 2 additions & 2 deletions src/lmic/aes/other.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
// This should be defined elsewhere
void lmic_aes_encrypt(u1_t *data, u1_t *key);

// global area for passing parameters (aux, key) and for storing round keys
// global area for passing parameters (aux, key)
u4_t AESAUX[16/sizeof(u4_t)];
u4_t AESKEY[11*16/sizeof(u4_t)];
u4_t AESKEY[16/sizeof(u4_t)];

// Shift the given buffer left one bit
static void shift_left(xref2u1_t buf, u1_t len) {
Expand Down
8 changes: 7 additions & 1 deletion src/lmic/arduino_lmic_hal_boards.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Copyright & License:
#include "lmic/arduino_lmic_hal_configuration.h"

namespace TTN_esp32_LMIC {

const HalPinmap_t *GetPinmap_FeatherM0LoRa();
const HalPinmap_t *GetPinmap_Feather32U4LoRa();

Expand All @@ -34,13 +35,18 @@ const HalPinmap_t *GetPinmap_Catena4617();
const HalPinmap_t *GetPinmap_Catena4618();
const HalPinmap_t *GetPinmap_Catena4630();
const HalPinmap_t *GetPinmap_Catena4801();
const HalPinmap_t *GetPinmap_ttgo_lora32_v1 ();
const HalPinmap_t *GetPinmap_Catena4802();
const HalPinmap_t* GetPinmap_ttgo_lora32_v1();
const HalPinmap_t *GetPinmap_ttgo_lora32_v21();
const HalPinmap_t* GetPinmap_heltec_lora32();
const HalPinmap_t* GetPinmap_Disco_L072cz_Lrwan1();
const HalPinmap_t *GetPinmap_heltec_wifi_lora_32();
const HalPinmap_t *GetPinmap_heltec_wifi_lora_32_V2();
const HalPinmap_t *GetPinmap_heltec_wireless_stick();
const HalPinmap_t *GetPinmap_M5stack_Lora();
const HalPinmap_t *GetPinmap_ttgo_TBEAM();


const HalPinmap_t *GetPinmap_ThisBoard();

}; /* namespace Arduino_LIMC */
Expand Down
3 changes: 3 additions & 0 deletions src/lmic/arduino_lmic_hal_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class HalConfiguration_t
uint32_t frequency
)
{
LMIC_API_PARAMETER(policy);
LMIC_API_PARAMETER(requestedPower);
LMIC_API_PARAMETER(frequency);
// default: use PA_BOOST exclusively
return TxPowerPolicy_t::PA_BOOST;
}
Expand Down
16 changes: 12 additions & 4 deletions src/lmic/lmic/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
// enable support for MCMD_DeviceTimeReq and MCMD_DeviceTimeAns
// this is always defined, and non-zero to enable it.
#if !defined(LMIC_ENABLE_DeviceTimeReq)
# define LMIC_ENABLE_DeviceTimeReq 0
# define LMIC_ENABLE_DeviceTimeReq 1
#endif

// LMIC_ENABLE_user_events
Expand All @@ -187,9 +187,17 @@
#endif

// LMIC_ENABLE_long_messages
// LMIC certification requires that this be enabled.
#if !defined(LMIC_ENABLE_long_messages)
# define LMIC_ENABLE_long_messages 1 /* PARAM */
// LMIC certification requires full-length 255 frames, but to save RAM,
// a shorter maximum can be set. This controls both RX and TX buffers,
// so reducing this by 1 saves 2 bytes of RAM.
#if defined(LMIC_ENABLE_long_messages) && defined(LMIC_MAX_FRAME_LENGTH)
#error "Use only one of LMIC_ENABLE_long_messages or LMIC_MAX_FRAME_LENGTH"
#elif defined(LMIC_ENABLE_long_messages) && LMIC_ENABLE_long_messages == 0
# define LMIC_MAX_FRAME_LENGTH 64
#elif !defined(LMIC_MAX_FRAME_LENGTH)
# define LMIC_MAX_FRAME_LENGTH 255
#elif LMIC_MAX_FRAME_LENGTH > 255
#error "LMIC_MAX_FRAME_LENGTH cannot be larger than 255"
#endif

// LMIC_ENABLE_event_logging
Expand Down
11 changes: 11 additions & 0 deletions src/lmic/lmic/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ uint8_t hal_getTxPowerPolicy(
u4_t freq
);

void hal_pollPendingIRQs_helper();
void hal_processPendingIRQs(void);

/// \brief check for any pending interrupts: stub if interrupts are enabled.
static inline void hal_pollPendingIRQs(void)
{
#if !defined(LMIC_USE_INTERRUPTS)
hal_pollPendingIRQs_helper();
#endif /* !defined(LMIC_USE_INTERRUPTS) */
}

#ifdef __cplusplus
} // extern "C"
#endif
Expand Down
125 changes: 96 additions & 29 deletions src/lmic/lmic/lmic.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void os_wmsbf4 (xref2u1_t buf, u4_t v) {

#if !defined(os_getBattLevel)
u1_t os_getBattLevel (void) {
return MCMD_DEVS_BATT_NOINFO;
return LMIC.client.devStatusAns_battery;
}
#endif

Expand Down Expand Up @@ -295,7 +295,7 @@ ostime_t calcAirTime (rps_t rps, u1_t plen) {
//

static void setRxsyms (ostime_t rxsyms) {
if (rxsyms >= (1u << 10u)) {
if (rxsyms >= (((ostime_t)1) << 10u)) {
LMIC.rxsyms = (1u << 10u) - 1;
} else if (rxsyms < 0) {
LMIC.rxsyms = 0;
Expand Down Expand Up @@ -721,11 +721,17 @@ static CONST_TABLE(u1_t, macCmdSize)[] = {
};

static u1_t getMacCmdSize(u1_t macCmd) {
if (macCmd < 2)
return 0;
if ((macCmd - 2) >= LENOF_TABLE(macCmdSize))
return 0;
return TABLE_GET_U1(macCmdSize, macCmd - 2);
if (macCmd >= 2) {
const unsigned macCmdMinus2 = macCmd - 2u;
if (macCmdMinus2 < LENOF_TABLE(macCmdSize)) {
// macCmd in table, fetch it's size.
return TABLE_GET_U1(macCmdSize, macCmdMinus2);
}
}
// macCmd too small or too large: return zero. Zero is
// never a legal command size, so it signals an error
// to the caller.
return 0;
}

static bit_t
Expand Down Expand Up @@ -758,6 +764,9 @@ applyAdrRequests(
p4 = opts[oidx+4]; // ChMaskCtl, NbTrans
u1_t chpage = p4 & MCMD_LinkADRReq_Redundancy_ChMaskCntl_MASK; // channel page

// notice that we ignore map_ok except on the last setting.
// so LMICbandplan_mapChannels should report failure status, but do
// the work; if it fails, we'll back it out.
map_ok = LMICbandplan_mapChannels(chpage, chmap);
LMICOS_logEventUint32("applyAdrRequests: mapChannels", ((u4_t)chpage << 16)|(chmap << 0));
}
Expand Down Expand Up @@ -883,10 +892,14 @@ scan_mac_cmds(
uint8_t cmd;

LMIC.pendMacLen = 0;
if (port == 0)
if (port == 0) {
// port zero: mac data is in the normal payload, and there can't be
// piggyback mac data.
LMIC.pendMacPiggyback = 0;
else
} else {
// port is either -1 (no port) or non-zero (piggyback): treat as piggyback.
LMIC.pendMacPiggyback = 1;
}

while( oidx < olen ) {
bit_t response_fit;
Expand Down Expand Up @@ -1466,7 +1479,12 @@ ostime_t LMICcore_adjustForDrift (ostime_t delay, ostime_t hsym, rxsyms_t rxsyms
// a compile-time configuration. (In other words, assume that millis()
// clock is accurate to 0.1%.) You should never use clockerror to
// compensate for system-late problems.
u2_t const maxError = LMIC_kMaxClockError_ppm * MAX_CLOCK_ERROR / (1000 * 1000);
// note about compiler: The initializer for maxError is coded for
// maximum portability. On 16-bit systems, some compilers complain
// if we write x / (1000 * 1000). x / 1000 / 1000 uses constants,
// is generally acceptable so it can be optimized in compiler's own
// way.
u2_t const maxError = LMIC_kMaxClockError_ppm * MAX_CLOCK_ERROR / 1000 / 1000;
if (! LMIC_ENABLE_arbitrary_clock_error && clockerr > maxError)
{
clockerr = maxError;
Expand Down Expand Up @@ -1606,21 +1624,9 @@ static bit_t processJoinAccept (void) {
// initDefaultChannels(0) for EU-like, nothing otherwise
LMICbandplan_joinAcceptChannelClear();

if (!LMICbandplan_hasJoinCFlist() && dlen > LEN_JA) {
// if no JoinCFList, we're supposed to continue
// the join per 2.2.5 of LoRaWAN regional 2.2.4
// https://github.com/mcci-catena/arduino-lmic/issues/19
} else if ( LMICbandplan_hasJoinCFlist() && dlen > LEN_JA ) {
dlen = OFF_CFLIST;
for( u1_t chidx=3; chidx<8; chidx++, dlen+=3 ) {
u4_t freq = LMICbandplan_convFreq(&LMIC.frame[dlen]);
if( freq ) {
LMIC_setupChannel(chidx, freq, 0, -1);
#if LMIC_DEBUG_LEVEL > 1
LMIC_DEBUG_PRINTF("%"LMIC_PRId_ostime_t": Setup channel, idx=%d, freq=%"PRIu32"\n", os_getTime(), chidx, freq);
#endif
}
}
// process the CFList if present
if (dlen == LEN_JAEXT) {
LMICbandplan_processJoinAcceptCFList();
}

// already incremented when JOIN REQ got sent off
Expand Down Expand Up @@ -1855,7 +1861,8 @@ static bit_t buildDataFrame (void) {
// highest importance are the ones in the pendMac buffer.
int end = OFF_DAT_OPTS;

if (LMIC.pendTxPort != 0 && LMIC.pendMacPiggyback && LMIC.pendMacLen != 0) {
// Send piggyback data if: !txdata or txport != 0
if ((! txdata || LMIC.pendTxPort != 0) && LMIC.pendMacPiggyback && LMIC.pendMacLen != 0) {
os_copyMem(LMIC.frame + end, LMIC.pendMacData, LMIC.pendMacLen);
end += LMIC.pendMacLen;
}
Expand Down Expand Up @@ -2780,6 +2787,11 @@ void LMIC_reset (void) {
LMIC.adrEnabled = FCT_ADREN;
resetJoinParams();
LMIC.rxDelay = DELAY_DNW1;
// LMIC.pendMacLen = 0;
// LMIC.pendMacPiggyback = 0;
// LMIC.dn2Ans = 0;
// LMIC.macDlChannelAns = 0;
// LMIC.macRxTimingSetupAns = 0;
#if !defined(DISABLE_PING)
LMIC.ping.freq = FREQ_PING; // defaults for ping
LMIC.ping.dr = DR_PING; // ditto
Expand All @@ -2806,6 +2818,7 @@ void LMIC_reset (void) {

void LMIC_init (void) {
LMIC.opmode = OP_SHUTDOWN;
LMIC.client.devStatusAns_battery = MCMD_DEVS_BATT_NOINFO;
LMICbandplan_init();
}

Expand Down Expand Up @@ -2866,7 +2879,11 @@ dr_t LMIC_feasibleDataRateForFrame(dr_t dr, u1_t payloadSize) {
}

static bit_t isTxPathBusy(void) {
return (LMIC.opmode & (OP_TXDATA|OP_JOINING)) != 0;
return (LMIC.opmode & (OP_POLL | OP_TXDATA | OP_JOINING | OP_TXRXPEND)) != 0;
}

bit_t LMIC_queryTxReady (void) {
return ! isTxPathBusy();
}

static bit_t adjustDrForFrameIfNotBusy(u1_t len) {
Expand All @@ -2886,6 +2903,10 @@ void LMIC_setTxData (void) {
}

void LMIC_setTxData_strict (void) {
if (isTxPathBusy()) {
return;
}

LMICOS_logEventUint32(__func__, ((u4_t)LMIC.pendTxPort << 24u) | ((u4_t)LMIC.pendTxConf << 16u) | (LMIC.pendTxLen << 0u));
LMIC.opmode |= OP_TXDATA;
if( (LMIC.opmode & OP_JOINING) == 0 ) {
Expand All @@ -2904,7 +2925,7 @@ lmic_tx_error_t LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t conf

// send a message w/o callback; do not adjust data rate
lmic_tx_error_t LMIC_setTxData2_strict (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed) {
if ( LMIC.opmode & OP_TXDATA ) {
if (isTxPathBusy()) {
// already have a message queued
return LMIC_ERROR_TX_BUSY;
}
Expand All @@ -2924,7 +2945,7 @@ lmic_tx_error_t LMIC_setTxData2_strict (u1_t port, xref2u1_t data, u1_t dlen, u1
return LMIC_ERROR_TX_FAILED;
}
}
return 0;
return LMIC_ERROR_SUCCESS;
}

// send a message with callback; try to adjust data rate
Expand Down Expand Up @@ -3080,6 +3101,52 @@ int LMIC_getNetworkTimeReference(lmic_time_reference_t *pReference) {
pReference->tNetwork = LMIC.netDeviceTime;
return 1;
}
#else
LMIC_API_PARAMETER(pReference);
#endif // LMIC_ENABLE_DeviceTimeReq
return 0;
}

///
/// \brief set battery level to be returned by `DevStatusAns`.
///
/// \param uBattLevel is the 8-bit value to be returned. Per LoRaWAN 1.0.3 line 769,
/// this is \c MCMD_DEVS_EXT_POWER (0) if on external power,
/// \c MCMD_DEVS_NOINFO (255) if not able to measure battery level,
/// or a value in [ \c MCMD_DEVS_BATT_MIN, \c MCMD_DEVS_BATT_MAX ], numerically
/// [1, 254], to represent the charge state of the battery. Note that
/// this is not millivolts.
///
/// \returns
/// This function returns the previous value of the battery level.
///
/// \details
/// The LMIC maintains an idea of the current battery state, initially set to
/// \c MCMD_DEVS_NOINFO after the call to LMIC_init(). The appplication then calls
/// this function from time to time to update the battery level.
///
/// It is possible (in non-Arduino environments) to supply a local implementation
/// of os_getBatteryLevel(). In that case, it's up to the implementation to decide
/// whether to use the value supplied by this API.
///
/// This implementation was chosen to minimize the risk of a battery measurement
/// introducting breaking delays into the LMIC.
///
u1_t LMIC_setBatteryLevel(u1_t uBattLevel) {
const u1_t result = LMIC.client.devStatusAns_battery;

LMIC.client.devStatusAns_battery = uBattLevel;
return result;
}

///
/// \brief get battery level that is to be returned by `DevStatusAns`.
///
/// \returns
/// This function returns the saved value of the battery level.
///
/// \see LMIC_setBatteryLevel()
///
u1_t LMIC_getBatteryLevel(void) {
return LMIC.client.devStatusAns_battery;
}
Loading

0 comments on commit 3457328

Please sign in to comment.