Skip to content

Commit

Permalink
change externalDataCommand to return LDL_STATUS_SIZE when message too…
Browse files Browse the repository at this point in the history
… long
  • Loading branch information
cjhdev committed Feb 28, 2021
1 parent 3978dd7 commit d07c65c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 120 deletions.
213 changes: 106 additions & 107 deletions src/ldl_mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -1068,9 +1068,6 @@ static void processWait(struct ldl_mac *self, enum ldl_mac_sme event, uint32_t l
#endif
break;

case LDL_SME_BAND:
break;

default:
break;
}
Expand Down Expand Up @@ -1650,6 +1647,7 @@ static enum ldl_mac_status externalDataCommand(struct ldl_mac *self, bool confir
struct ldl_frame_data f;
struct ldl_stream s;
uint8_t macs[30U]; // large enough for all possible MAC commands
size_t desired_len = len + (size_t)LDL_Frame_dataOverhead();

if(self->ctx.joined){

Expand All @@ -1666,21 +1664,21 @@ static enum ldl_mac_status externalDataCommand(struct ldl_mac *self, bool confir
#else
self->tx.rate = LDL_Region_applyUplinkDwell(self->ctx.region, uplinkDwell(self->ctx.tx_param_setup), self->ctx.rate);
#endif

if(selectChannel(self, self->ctx.rate, 0U, &self->tx)){

LDL_Region_convertRate(self->ctx.region, self->ctx.rate, &sf, &bw, &maxPayload);

(void)memset(&self->opts, 0, sizeof(self->opts));
if(desired_len <= (size_t)maxPayload){

if(opts != NULL){
(void)memset(&self->opts, 0, sizeof(self->opts));

(void)memcpy(&self->opts, opts, sizeof(self->opts));
}
if(opts != NULL){

self->opts.nbTrans = self->opts.nbTrans & 0xfU;
(void)memcpy(&self->opts, opts, sizeof(self->opts));
}

self->opts.nbTrans = self->opts.nbTrans & 0xfU;

{
self->trials = 0U;

(void)memset(&f, 0, sizeof(f));
Expand All @@ -1698,145 +1696,142 @@ static enum ldl_mac_status externalDataCommand(struct ldl_mac *self, bool confir
* responding to a confirmed downlink */
f.ack = self->pendingACK;


/* 1.1 has to awkwardly re-calculate the MIC when a frame is retried on a
* different channel and the counter is a parameter */
self->tx.counter = self->ctx.up;

self->ctx.up++;

/* serialise pending MAC commands */
{
LDL_Stream_init(&s, macs, U8(sizeof(macs)));

LDL_DEBUG("preparing data frame")
LDL_Stream_init(&s, macs, U8(sizeof(macs)));

/* sticky commands */
LDL_DEBUG("preparing data frame")

/* sticky commands */
#if defined(LDL_ENABLE_L2_1_1)
if(commandIsPending(self, LDL_CMD_REKEY)){
if(commandIsPending(self, LDL_CMD_REKEY)){

struct ldl_rekey_ind ind = {
.version = self->ctx.version
};
struct ldl_rekey_ind ind = {
.version = self->ctx.version
};

LDL_MAC_putRekeyInd(&s, &ind);
LDL_MAC_putRekeyInd(&s, &ind);

LDL_DEBUG("adding rekey_ind: version=%u", self->ctx.version)
}
LDL_DEBUG("adding rekey_ind: version=%u", self->ctx.version)
}
#endif
if(commandIsPending(self, LDL_CMD_RX_PARAM_SETUP)){
if(commandIsPending(self, LDL_CMD_RX_PARAM_SETUP)){

LDL_MAC_putRXParamSetupAns(&s, &self->ctx.rx_param_setup_ans);
LDL_MAC_putRXParamSetupAns(&s, &self->ctx.rx_param_setup_ans);

LDL_DEBUG("adding rx_param_setup_ans: rx1DROffsetOK=%s rx2DataRate=%s rx2Freq=%s",
self->ctx.rx_param_setup_ans.rx1DROffsetOK ? "true" : "false",
self->ctx.rx_param_setup_ans.rx2DataRateOK ? "true" : "false",
self->ctx.rx_param_setup_ans.channelOK ? "true" : "false"
)
}
LDL_DEBUG("adding rx_param_setup_ans: rx1DROffsetOK=%s rx2DataRate=%s rx2Freq=%s",
self->ctx.rx_param_setup_ans.rx1DROffsetOK ? "true" : "false",
self->ctx.rx_param_setup_ans.rx2DataRateOK ? "true" : "false",
self->ctx.rx_param_setup_ans.channelOK ? "true" : "false"
)
}

if(commandIsPending(self, LDL_CMD_DL_CHANNEL)){
if(commandIsPending(self, LDL_CMD_DL_CHANNEL)){

LDL_MAC_putDLChannelAns(&s, &self->ctx.dl_channel_ans);
LDL_MAC_putDLChannelAns(&s, &self->ctx.dl_channel_ans);

LDL_DEBUG("adding dl_channel_ans: uplinkFreqOK=%s channelFreqOK=%s",
self->ctx.dl_channel_ans.uplinkFreqOK ? "true" : "false",
self->ctx.dl_channel_ans.channelFreqOK ? "true" : "false"
)
}
LDL_DEBUG("adding dl_channel_ans: uplinkFreqOK=%s channelFreqOK=%s",
self->ctx.dl_channel_ans.uplinkFreqOK ? "true" : "false",
self->ctx.dl_channel_ans.channelFreqOK ? "true" : "false"
)
}

if(commandIsPending(self, LDL_CMD_RX_TIMING_SETUP)){
if(commandIsPending(self, LDL_CMD_RX_TIMING_SETUP)){

LDL_MAC_putRXTimingSetupAns(&s);
LDL_DEBUG("adding rx_timing_setup_ans")
}
LDL_MAC_putRXTimingSetupAns(&s);
LDL_DEBUG("adding rx_timing_setup_ans")
}

/* single shot commands */
/* single shot commands */

if(commandIsPending(self, LDL_CMD_LINK_ADR)){
if(commandIsPending(self, LDL_CMD_LINK_ADR)){

LDL_MAC_putLinkADRAns(&s, &self->ctx.link_adr_ans);
clearPendingCommand(self, LDL_CMD_LINK_ADR);
LDL_MAC_putLinkADRAns(&s, &self->ctx.link_adr_ans);
clearPendingCommand(self, LDL_CMD_LINK_ADR);

LDL_DEBUG("adding link_adr_ans: powerOK=%s dataRateOK=%s channelMaskOK=%s",
self->ctx.link_adr_ans.dataRateOK ? "true" : "false",
self->ctx.link_adr_ans.powerOK ? "true" : "false",
self->ctx.link_adr_ans.channelMaskOK ? "true" : "false"
)
}
LDL_DEBUG("adding link_adr_ans: powerOK=%s dataRateOK=%s channelMaskOK=%s",
self->ctx.link_adr_ans.dataRateOK ? "true" : "false",
self->ctx.link_adr_ans.powerOK ? "true" : "false",
self->ctx.link_adr_ans.channelMaskOK ? "true" : "false"
)
}

if(commandIsPending(self, LDL_CMD_DEV_STATUS)){
if(commandIsPending(self, LDL_CMD_DEV_STATUS)){

LDL_MAC_putDevStatusAns(&s, &self->ctx.dev_status_ans);
clearPendingCommand(self, LDL_CMD_DEV_STATUS);
LDL_MAC_putDevStatusAns(&s, &self->ctx.dev_status_ans);
clearPendingCommand(self, LDL_CMD_DEV_STATUS);

LDL_DEBUG("adding dev_status_ans: battery=%u margin=%i",
self->ctx.dev_status_ans.battery,
self->ctx.dev_status_ans.margin
)
}
LDL_DEBUG("adding dev_status_ans: battery=%u margin=%i",
self->ctx.dev_status_ans.battery,
self->ctx.dev_status_ans.margin
)
}

if(commandIsPending(self, LDL_CMD_NEW_CHANNEL)){
if(commandIsPending(self, LDL_CMD_NEW_CHANNEL)){

LDL_MAC_putNewChannelAns(&s, &self->ctx.new_channel_ans);
clearPendingCommand(self, LDL_CMD_NEW_CHANNEL);
LDL_MAC_putNewChannelAns(&s, &self->ctx.new_channel_ans);
clearPendingCommand(self, LDL_CMD_NEW_CHANNEL);

LDL_DEBUG("adding new_channel_ans: dataRateRangeOK=%s channelFreqOK=%s",
self->ctx.new_channel_ans.dataRateRangeOK ? "true" : "false",
self->ctx.new_channel_ans.channelFreqOK ? "true" : "false"
)
}
LDL_DEBUG("adding new_channel_ans: dataRateRangeOK=%s channelFreqOK=%s",
self->ctx.new_channel_ans.dataRateRangeOK ? "true" : "false",
self->ctx.new_channel_ans.channelFreqOK ? "true" : "false"
)
}
#if defined(LDL_ENABLE_L2_1_1)
if(commandIsPending(self, LDL_CMD_REJOIN_PARAM_SETUP)){
if(commandIsPending(self, LDL_CMD_REJOIN_PARAM_SETUP)){

LDL_MAC_putRejoinParamSetupAns(&s, &self->ctx.rejoin_param_setup_ans);
clearPendingCommand(self, LDL_CMD_REJOIN_PARAM_SETUP);
LDL_MAC_putRejoinParamSetupAns(&s, &self->ctx.rejoin_param_setup_ans);
clearPendingCommand(self, LDL_CMD_REJOIN_PARAM_SETUP);

LDL_DEBUG("adding rejoin_param_setup_ans: timeOK=%s",
self->ctx.rejoin_param_setup_ans.timeOK ? "true" : "false"
)
}
LDL_DEBUG("adding rejoin_param_setup_ans: timeOK=%s",
self->ctx.rejoin_param_setup_ans.timeOK ? "true" : "false"
)
}

if(commandIsPending(self, LDL_CMD_ADR_PARAM_SETUP)){
if(commandIsPending(self, LDL_CMD_ADR_PARAM_SETUP)){

LDL_MAC_putADRParamSetupAns(&s);
clearPendingCommand(self, LDL_CMD_ADR_PARAM_SETUP);
LDL_MAC_putADRParamSetupAns(&s);
clearPendingCommand(self, LDL_CMD_ADR_PARAM_SETUP);

LDL_DEBUG("adding adr_param_setup_ans")
}
LDL_DEBUG("adding adr_param_setup_ans")
}
#endif
#ifndef LDL_DISABLE_TX_PARAM_SETUP
if(commandIsPending(self, LDL_CMD_TX_PARAM_SETUP)){
if(commandIsPending(self, LDL_CMD_TX_PARAM_SETUP)){

LDL_MAC_putTXParamSetupAns(&s);
clearPendingCommand(self, LDL_CMD_TX_PARAM_SETUP);
LDL_MAC_putTXParamSetupAns(&s);
clearPendingCommand(self, LDL_CMD_TX_PARAM_SETUP);

LDL_DEBUG("adding tx_param_setup_ans")
}
LDL_DEBUG("adding tx_param_setup_ans")
}
#endif
if(commandIsPending(self, LDL_CMD_DUTY_CYCLE)){
if(commandIsPending(self, LDL_CMD_DUTY_CYCLE)){

LDL_MAC_putDutyCycleAns(&s);
clearPendingCommand(self, LDL_CMD_DUTY_CYCLE);
LDL_MAC_putDutyCycleAns(&s);
clearPendingCommand(self, LDL_CMD_DUTY_CYCLE);

LDL_DEBUG("adding duty_cycle_ans")
}
LDL_DEBUG("adding duty_cycle_ans")
}
#ifndef LDL_DISABLE_LINK_CHECK
if(self->opts.check){
if(self->opts.check){

LDL_MAC_putLinkCheckReq(&s);
LDL_DEBUG("adding link_check_req")
}
LDL_MAC_putLinkCheckReq(&s);
LDL_DEBUG("adding link_check_req")
}
#endif
#ifndef LDL_DISABLE_DEVICE_TIME
if(self->opts.getTime){
if(self->opts.getTime){

LDL_MAC_putDeviceTimeReq(&s);
LDL_DEBUG("adding device_time_req")
}
#endif
LDL_MAC_putDeviceTimeReq(&s);
LDL_DEBUG("adding device_time_req")
}

#endif
/* create port 0 message and ignore application */
if(LDL_Stream_tell(&s) > 15U){

Expand All @@ -1853,7 +1848,7 @@ static enum ldl_mac_status externalDataCommand(struct ldl_mac *self, bool confir
retval = LDL_STATUS_MACPRIORITY;
}
/* ignore application */
else if((LDL_Frame_dataOverhead() + len + LDL_Stream_tell(&s)) > maxPayload){
else if((desired_len + (size_t)LDL_Stream_tell(&s)) > (size_t)maxPayload){

LDL_DEBUG("mac data has been prioritised")

Expand Down Expand Up @@ -1882,17 +1877,21 @@ static enum ldl_mac_status externalDataCommand(struct ldl_mac *self, bool confir
self->bufferLen = LDL_OPS_prepareData(self, &f, self->buffer, U8(sizeof(self->buffer)));

LDL_OPS_micDataFrame(self, self->buffer, self->bufferLen);
}

if(self->state == LDL_STATE_IDLE){
if(self->state == LDL_STATE_IDLE){

self->state = LDL_STATE_WAIT_TX;
LDL_MAC_timerSet(self, LDL_TIMER_WAITA, 0U);
self->state = LDL_STATE_WAIT_TX;
LDL_MAC_timerSet(self, LDL_TIMER_WAITA, 0U);
}

LDL_DEBUG("initiate data: ticks=%" PRIu32,
self->ticks(self->app)
)
}
else{

LDL_DEBUG("initiate data: ticks=%" PRIu32,
self->ticks(self->app)
)
retval = LDL_STATUS_SIZE;
}
}
else{

Expand Down
17 changes: 8 additions & 9 deletions src/ldl_region.c
Original file line number Diff line number Diff line change
Expand Up @@ -978,13 +978,14 @@ uint8_t LDL_Region_getJoinRate(enum ldl_region region, uint32_t trial)

uint8_t LDL_Region_getJoinIndex(enum ldl_region region, uint32_t trial, uint32_t random)
{
uint8_t retval = 0;
uint8_t retval;

(void)trial;
(void)random;

switch(region){
default:
retval = 0;
break;
#ifdef LDL_ENABLE_US_902_928
case LDL_US_902_928:
Expand Down Expand Up @@ -1070,7 +1071,7 @@ const char *LDL_Region_enumToString(enum ldl_region region)

bool LDL_Region_txParamSetupImplemented(enum ldl_region region)
{
bool retval = false;
bool retval;

switch(region){
#ifdef LDL_ENABLE_AU_915_928
Expand All @@ -1079,6 +1080,7 @@ bool LDL_Region_txParamSetupImplemented(enum ldl_region region)
break;
#endif
default:
retval = false;
break;
}

Expand All @@ -1087,23 +1089,20 @@ bool LDL_Region_txParamSetupImplemented(enum ldl_region region)

uint8_t LDL_Region_applyUplinkDwell(enum ldl_region region, bool dwell, uint8_t rate)
{
uint8_t retval = rate;
uint8_t retval;

(void)dwell;

switch(region){
#ifdef LDL_ENABLE_AU_915_928
case LDL_AU_915_928:
if(dwell){

if(retval < 2U){

retval = 2U;
}
}
retval = (dwell && (rate < 2U)) ? 2U : rate;
break;
#endif
default:

retval = rate;
break;
}

Expand Down
Loading

0 comments on commit d07c65c

Please sign in to comment.