Skip to content

Commit d07c65c

Browse files
committed
change externalDataCommand to return LDL_STATUS_SIZE when message too long
1 parent 3978dd7 commit d07c65c

File tree

4 files changed

+118
-120
lines changed

4 files changed

+118
-120
lines changed

src/ldl_mac.c

Lines changed: 106 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,9 +1068,6 @@ static void processWait(struct ldl_mac *self, enum ldl_mac_sme event, uint32_t l
10681068
#endif
10691069
break;
10701070

1071-
case LDL_SME_BAND:
1072-
break;
1073-
10741071
default:
10751072
break;
10761073
}
@@ -1650,6 +1647,7 @@ static enum ldl_mac_status externalDataCommand(struct ldl_mac *self, bool confir
16501647
struct ldl_frame_data f;
16511648
struct ldl_stream s;
16521649
uint8_t macs[30U]; // large enough for all possible MAC commands
1650+
size_t desired_len = len + (size_t)LDL_Frame_dataOverhead();
16531651

16541652
if(self->ctx.joined){
16551653

@@ -1666,21 +1664,21 @@ static enum ldl_mac_status externalDataCommand(struct ldl_mac *self, bool confir
16661664
#else
16671665
self->tx.rate = LDL_Region_applyUplinkDwell(self->ctx.region, uplinkDwell(self->ctx.tx_param_setup), self->ctx.rate);
16681666
#endif
1669-
16701667
if(selectChannel(self, self->ctx.rate, 0U, &self->tx)){
16711668

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

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

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

1678-
(void)memcpy(&self->opts, opts, sizeof(self->opts));
1679-
}
1675+
if(opts != NULL){
16801676

1681-
self->opts.nbTrans = self->opts.nbTrans & 0xfU;
1677+
(void)memcpy(&self->opts, opts, sizeof(self->opts));
1678+
}
1679+
1680+
self->opts.nbTrans = self->opts.nbTrans & 0xfU;
16821681

1683-
{
16841682
self->trials = 0U;
16851683

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

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

17061703
self->ctx.up++;
17071704

17081705
/* serialise pending MAC commands */
1709-
{
1710-
LDL_Stream_init(&s, macs, U8(sizeof(macs)));
17111706

1712-
LDL_DEBUG("preparing data frame")
1707+
LDL_Stream_init(&s, macs, U8(sizeof(macs)));
17131708

1714-
/* sticky commands */
1709+
LDL_DEBUG("preparing data frame")
1710+
1711+
/* sticky commands */
17151712
#if defined(LDL_ENABLE_L2_1_1)
1716-
if(commandIsPending(self, LDL_CMD_REKEY)){
1713+
if(commandIsPending(self, LDL_CMD_REKEY)){
17171714

1718-
struct ldl_rekey_ind ind = {
1719-
.version = self->ctx.version
1720-
};
1715+
struct ldl_rekey_ind ind = {
1716+
.version = self->ctx.version
1717+
};
17211718

1722-
LDL_MAC_putRekeyInd(&s, &ind);
1719+
LDL_MAC_putRekeyInd(&s, &ind);
17231720

1724-
LDL_DEBUG("adding rekey_ind: version=%u", self->ctx.version)
1725-
}
1721+
LDL_DEBUG("adding rekey_ind: version=%u", self->ctx.version)
1722+
}
17261723
#endif
1727-
if(commandIsPending(self, LDL_CMD_RX_PARAM_SETUP)){
1724+
if(commandIsPending(self, LDL_CMD_RX_PARAM_SETUP)){
17281725

1729-
LDL_MAC_putRXParamSetupAns(&s, &self->ctx.rx_param_setup_ans);
1726+
LDL_MAC_putRXParamSetupAns(&s, &self->ctx.rx_param_setup_ans);
17301727

1731-
LDL_DEBUG("adding rx_param_setup_ans: rx1DROffsetOK=%s rx2DataRate=%s rx2Freq=%s",
1732-
self->ctx.rx_param_setup_ans.rx1DROffsetOK ? "true" : "false",
1733-
self->ctx.rx_param_setup_ans.rx2DataRateOK ? "true" : "false",
1734-
self->ctx.rx_param_setup_ans.channelOK ? "true" : "false"
1735-
)
1736-
}
1728+
LDL_DEBUG("adding rx_param_setup_ans: rx1DROffsetOK=%s rx2DataRate=%s rx2Freq=%s",
1729+
self->ctx.rx_param_setup_ans.rx1DROffsetOK ? "true" : "false",
1730+
self->ctx.rx_param_setup_ans.rx2DataRateOK ? "true" : "false",
1731+
self->ctx.rx_param_setup_ans.channelOK ? "true" : "false"
1732+
)
1733+
}
17371734

1738-
if(commandIsPending(self, LDL_CMD_DL_CHANNEL)){
1735+
if(commandIsPending(self, LDL_CMD_DL_CHANNEL)){
17391736

1740-
LDL_MAC_putDLChannelAns(&s, &self->ctx.dl_channel_ans);
1737+
LDL_MAC_putDLChannelAns(&s, &self->ctx.dl_channel_ans);
17411738

1742-
LDL_DEBUG("adding dl_channel_ans: uplinkFreqOK=%s channelFreqOK=%s",
1743-
self->ctx.dl_channel_ans.uplinkFreqOK ? "true" : "false",
1744-
self->ctx.dl_channel_ans.channelFreqOK ? "true" : "false"
1745-
)
1746-
}
1739+
LDL_DEBUG("adding dl_channel_ans: uplinkFreqOK=%s channelFreqOK=%s",
1740+
self->ctx.dl_channel_ans.uplinkFreqOK ? "true" : "false",
1741+
self->ctx.dl_channel_ans.channelFreqOK ? "true" : "false"
1742+
)
1743+
}
17471744

1748-
if(commandIsPending(self, LDL_CMD_RX_TIMING_SETUP)){
1745+
if(commandIsPending(self, LDL_CMD_RX_TIMING_SETUP)){
17491746

1750-
LDL_MAC_putRXTimingSetupAns(&s);
1751-
LDL_DEBUG("adding rx_timing_setup_ans")
1752-
}
1747+
LDL_MAC_putRXTimingSetupAns(&s);
1748+
LDL_DEBUG("adding rx_timing_setup_ans")
1749+
}
17531750

1754-
/* single shot commands */
1751+
/* single shot commands */
17551752

1756-
if(commandIsPending(self, LDL_CMD_LINK_ADR)){
1753+
if(commandIsPending(self, LDL_CMD_LINK_ADR)){
17571754

1758-
LDL_MAC_putLinkADRAns(&s, &self->ctx.link_adr_ans);
1759-
clearPendingCommand(self, LDL_CMD_LINK_ADR);
1755+
LDL_MAC_putLinkADRAns(&s, &self->ctx.link_adr_ans);
1756+
clearPendingCommand(self, LDL_CMD_LINK_ADR);
17601757

1761-
LDL_DEBUG("adding link_adr_ans: powerOK=%s dataRateOK=%s channelMaskOK=%s",
1762-
self->ctx.link_adr_ans.dataRateOK ? "true" : "false",
1763-
self->ctx.link_adr_ans.powerOK ? "true" : "false",
1764-
self->ctx.link_adr_ans.channelMaskOK ? "true" : "false"
1765-
)
1766-
}
1758+
LDL_DEBUG("adding link_adr_ans: powerOK=%s dataRateOK=%s channelMaskOK=%s",
1759+
self->ctx.link_adr_ans.dataRateOK ? "true" : "false",
1760+
self->ctx.link_adr_ans.powerOK ? "true" : "false",
1761+
self->ctx.link_adr_ans.channelMaskOK ? "true" : "false"
1762+
)
1763+
}
17671764

1768-
if(commandIsPending(self, LDL_CMD_DEV_STATUS)){
1765+
if(commandIsPending(self, LDL_CMD_DEV_STATUS)){
17691766

1770-
LDL_MAC_putDevStatusAns(&s, &self->ctx.dev_status_ans);
1771-
clearPendingCommand(self, LDL_CMD_DEV_STATUS);
1767+
LDL_MAC_putDevStatusAns(&s, &self->ctx.dev_status_ans);
1768+
clearPendingCommand(self, LDL_CMD_DEV_STATUS);
17721769

1773-
LDL_DEBUG("adding dev_status_ans: battery=%u margin=%i",
1774-
self->ctx.dev_status_ans.battery,
1775-
self->ctx.dev_status_ans.margin
1776-
)
1777-
}
1770+
LDL_DEBUG("adding dev_status_ans: battery=%u margin=%i",
1771+
self->ctx.dev_status_ans.battery,
1772+
self->ctx.dev_status_ans.margin
1773+
)
1774+
}
17781775

1779-
if(commandIsPending(self, LDL_CMD_NEW_CHANNEL)){
1776+
if(commandIsPending(self, LDL_CMD_NEW_CHANNEL)){
17801777

1781-
LDL_MAC_putNewChannelAns(&s, &self->ctx.new_channel_ans);
1782-
clearPendingCommand(self, LDL_CMD_NEW_CHANNEL);
1778+
LDL_MAC_putNewChannelAns(&s, &self->ctx.new_channel_ans);
1779+
clearPendingCommand(self, LDL_CMD_NEW_CHANNEL);
17831780

1784-
LDL_DEBUG("adding new_channel_ans: dataRateRangeOK=%s channelFreqOK=%s",
1785-
self->ctx.new_channel_ans.dataRateRangeOK ? "true" : "false",
1786-
self->ctx.new_channel_ans.channelFreqOK ? "true" : "false"
1787-
)
1788-
}
1781+
LDL_DEBUG("adding new_channel_ans: dataRateRangeOK=%s channelFreqOK=%s",
1782+
self->ctx.new_channel_ans.dataRateRangeOK ? "true" : "false",
1783+
self->ctx.new_channel_ans.channelFreqOK ? "true" : "false"
1784+
)
1785+
}
17891786
#if defined(LDL_ENABLE_L2_1_1)
1790-
if(commandIsPending(self, LDL_CMD_REJOIN_PARAM_SETUP)){
1787+
if(commandIsPending(self, LDL_CMD_REJOIN_PARAM_SETUP)){
17911788

1792-
LDL_MAC_putRejoinParamSetupAns(&s, &self->ctx.rejoin_param_setup_ans);
1793-
clearPendingCommand(self, LDL_CMD_REJOIN_PARAM_SETUP);
1789+
LDL_MAC_putRejoinParamSetupAns(&s, &self->ctx.rejoin_param_setup_ans);
1790+
clearPendingCommand(self, LDL_CMD_REJOIN_PARAM_SETUP);
17941791

1795-
LDL_DEBUG("adding rejoin_param_setup_ans: timeOK=%s",
1796-
self->ctx.rejoin_param_setup_ans.timeOK ? "true" : "false"
1797-
)
1798-
}
1792+
LDL_DEBUG("adding rejoin_param_setup_ans: timeOK=%s",
1793+
self->ctx.rejoin_param_setup_ans.timeOK ? "true" : "false"
1794+
)
1795+
}
17991796

1800-
if(commandIsPending(self, LDL_CMD_ADR_PARAM_SETUP)){
1797+
if(commandIsPending(self, LDL_CMD_ADR_PARAM_SETUP)){
18011798

1802-
LDL_MAC_putADRParamSetupAns(&s);
1803-
clearPendingCommand(self, LDL_CMD_ADR_PARAM_SETUP);
1799+
LDL_MAC_putADRParamSetupAns(&s);
1800+
clearPendingCommand(self, LDL_CMD_ADR_PARAM_SETUP);
18041801

1805-
LDL_DEBUG("adding adr_param_setup_ans")
1806-
}
1802+
LDL_DEBUG("adding adr_param_setup_ans")
1803+
}
18071804
#endif
18081805
#ifndef LDL_DISABLE_TX_PARAM_SETUP
1809-
if(commandIsPending(self, LDL_CMD_TX_PARAM_SETUP)){
1806+
if(commandIsPending(self, LDL_CMD_TX_PARAM_SETUP)){
18101807

1811-
LDL_MAC_putTXParamSetupAns(&s);
1812-
clearPendingCommand(self, LDL_CMD_TX_PARAM_SETUP);
1808+
LDL_MAC_putTXParamSetupAns(&s);
1809+
clearPendingCommand(self, LDL_CMD_TX_PARAM_SETUP);
18131810

1814-
LDL_DEBUG("adding tx_param_setup_ans")
1815-
}
1811+
LDL_DEBUG("adding tx_param_setup_ans")
1812+
}
18161813
#endif
1817-
if(commandIsPending(self, LDL_CMD_DUTY_CYCLE)){
1814+
if(commandIsPending(self, LDL_CMD_DUTY_CYCLE)){
18181815

1819-
LDL_MAC_putDutyCycleAns(&s);
1820-
clearPendingCommand(self, LDL_CMD_DUTY_CYCLE);
1816+
LDL_MAC_putDutyCycleAns(&s);
1817+
clearPendingCommand(self, LDL_CMD_DUTY_CYCLE);
18211818

1822-
LDL_DEBUG("adding duty_cycle_ans")
1823-
}
1819+
LDL_DEBUG("adding duty_cycle_ans")
1820+
}
18241821
#ifndef LDL_DISABLE_LINK_CHECK
1825-
if(self->opts.check){
1822+
if(self->opts.check){
18261823

1827-
LDL_MAC_putLinkCheckReq(&s);
1828-
LDL_DEBUG("adding link_check_req")
1829-
}
1824+
LDL_MAC_putLinkCheckReq(&s);
1825+
LDL_DEBUG("adding link_check_req")
1826+
}
18301827
#endif
18311828
#ifndef LDL_DISABLE_DEVICE_TIME
1832-
if(self->opts.getTime){
1829+
if(self->opts.getTime){
18331830

1834-
LDL_MAC_putDeviceTimeReq(&s);
1835-
LDL_DEBUG("adding device_time_req")
1836-
}
1837-
#endif
1831+
LDL_MAC_putDeviceTimeReq(&s);
1832+
LDL_DEBUG("adding device_time_req")
18381833
}
1839-
1834+
#endif
18401835
/* create port 0 message and ignore application */
18411836
if(LDL_Stream_tell(&s) > 15U){
18421837

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

18581853
LDL_DEBUG("mac data has been prioritised")
18591854

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

18841879
LDL_OPS_micDataFrame(self, self->buffer, self->bufferLen);
1885-
}
18861880

1887-
if(self->state == LDL_STATE_IDLE){
1881+
if(self->state == LDL_STATE_IDLE){
18881882

1889-
self->state = LDL_STATE_WAIT_TX;
1890-
LDL_MAC_timerSet(self, LDL_TIMER_WAITA, 0U);
1883+
self->state = LDL_STATE_WAIT_TX;
1884+
LDL_MAC_timerSet(self, LDL_TIMER_WAITA, 0U);
1885+
}
1886+
1887+
LDL_DEBUG("initiate data: ticks=%" PRIu32,
1888+
self->ticks(self->app)
1889+
)
18911890
}
1891+
else{
18921892

1893-
LDL_DEBUG("initiate data: ticks=%" PRIu32,
1894-
self->ticks(self->app)
1895-
)
1893+
retval = LDL_STATUS_SIZE;
1894+
}
18961895
}
18971896
else{
18981897

src/ldl_region.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -978,13 +978,14 @@ uint8_t LDL_Region_getJoinRate(enum ldl_region region, uint32_t trial)
978978

979979
uint8_t LDL_Region_getJoinIndex(enum ldl_region region, uint32_t trial, uint32_t random)
980980
{
981-
uint8_t retval = 0;
981+
uint8_t retval;
982982

983983
(void)trial;
984984
(void)random;
985985

986986
switch(region){
987987
default:
988+
retval = 0;
988989
break;
989990
#ifdef LDL_ENABLE_US_902_928
990991
case LDL_US_902_928:
@@ -1070,7 +1071,7 @@ const char *LDL_Region_enumToString(enum ldl_region region)
10701071

10711072
bool LDL_Region_txParamSetupImplemented(enum ldl_region region)
10721073
{
1073-
bool retval = false;
1074+
bool retval;
10741075

10751076
switch(region){
10761077
#ifdef LDL_ENABLE_AU_915_928
@@ -1079,6 +1080,7 @@ bool LDL_Region_txParamSetupImplemented(enum ldl_region region)
10791080
break;
10801081
#endif
10811082
default:
1083+
retval = false;
10821084
break;
10831085
}
10841086

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

10881090
uint8_t LDL_Region_applyUplinkDwell(enum ldl_region region, bool dwell, uint8_t rate)
10891091
{
1090-
uint8_t retval = rate;
1092+
uint8_t retval;
10911093

10921094
(void)dwell;
10931095

10941096
switch(region){
10951097
#ifdef LDL_ENABLE_AU_915_928
10961098
case LDL_AU_915_928:
1097-
if(dwell){
1098-
1099-
if(retval < 2U){
11001099

1101-
retval = 2U;
1102-
}
1103-
}
1100+
retval = (dwell && (rate < 2U)) ? 2U : rate;
11041101
break;
11051102
#endif
11061103
default:
1104+
1105+
retval = rate;
11071106
break;
11081107
}
11091108

0 commit comments

Comments
 (0)