@@ -1034,6 +1034,7 @@ lorawan_status_t LoRaMac::schedule_tx()
1034
1034
{
1035
1035
channel_selection_params_t next_channel;
1036
1036
lorawan_time_t backoff_time = 0 ;
1037
+ uint8_t fopts_len = 0 ;
1037
1038
1038
1039
if (_params.sys_params .max_duty_cycle == 255 ) {
1039
1040
return LORAWAN_STATUS_DEVICE_OFF;
@@ -1091,9 +1092,25 @@ lorawan_status_t LoRaMac::schedule_tx()
1091
1092
_params.rx_window2_delay = _params.sys_params .join_accept_delay2
1092
1093
+ _params.rx_window2_config .window_offset ;
1093
1094
} else {
1094
- if (validate_payload_length (_params.tx_buffer_len ,
1095
+
1096
+ // if the outgoing message is a proprietary message, it doesn't include any
1097
+ // standard message formatting except port and MHDR.
1098
+ if (_ongoing_tx_msg.type == MCPS_PROPRIETARY) {
1099
+ fopts_len = 0 ;
1100
+ } else {
1101
+ fopts_len = _mac_commands.get_mac_cmd_length () + _mac_commands.get_repeat_commands_length ();
1102
+ }
1103
+
1104
+ // A check was performed for validity of FRMPayload in ::prepare_ongoing_tx() API.
1105
+ // However, owing to the asynch nature of the send() API, we should check the
1106
+ // validity again, as datarate may have changed since we last attempted to transmit.
1107
+ if (validate_payload_length (_ongoing_tx_msg.f_buffer_size ,
1095
1108
_params.sys_params .channel_data_rate ,
1096
- _mac_commands.get_mac_cmd_length ()) == false ) {
1109
+ fopts_len) == false ) {
1110
+ tr_error (" Allowed FRMPayload = %d, FRMPayload = %d, MAC commands pending = %d" ,
1111
+ _lora_phy->get_max_payload (_params.sys_params .channel_data_rate ,
1112
+ _params.is_repeater_supported ),
1113
+ _ongoing_tx_msg.f_buffer_size , fopts_len);
1097
1114
return LORAWAN_STATUS_LENGTH_ERROR;
1098
1115
}
1099
1116
_params.rx_window1_delay = _params.sys_params .recv_delay1
@@ -1216,27 +1233,9 @@ int16_t LoRaMac::prepare_ongoing_tx(const uint8_t port,
1216
1233
uint8_t num_retries)
1217
1234
{
1218
1235
_ongoing_tx_msg.port = port;
1219
-
1220
- uint8_t max_possible_size = get_max_possible_tx_size (length);
1221
-
1222
- if (max_possible_size > MBED_CONF_LORA_TX_MAX_SIZE) {
1223
- max_possible_size = MBED_CONF_LORA_TX_MAX_SIZE;
1224
- }
1225
-
1226
- if (max_possible_size < length) {
1227
- tr_info (" Cannot transmit %d bytes. Possible TX Size is %d bytes" ,
1228
- length, max_possible_size);
1229
-
1230
- _ongoing_tx_msg.pending_size = length - max_possible_size;
1231
- _ongoing_tx_msg.f_buffer_size = max_possible_size;
1232
- memcpy (_ongoing_tx_msg.f_buffer , data, _ongoing_tx_msg.f_buffer_size );
1233
- } else {
1234
- _ongoing_tx_msg.f_buffer_size = length;
1235
- _ongoing_tx_msg.pending_size = 0 ;
1236
- if (length > 0 ) {
1237
- memcpy (_ongoing_tx_msg.f_buffer , data, length);
1238
- }
1239
- }
1236
+ uint8_t max_possible_size = 0 ;
1237
+ uint8_t fopts_len = _mac_commands.get_mac_cmd_length ()
1238
+ + _mac_commands.get_repeat_commands_length ();
1240
1239
1241
1240
// Handles unconfirmed messages
1242
1241
if (flags & MSG_UNCONFIRMED_FLAG) {
@@ -1257,6 +1256,30 @@ int16_t LoRaMac::prepare_ongoing_tx(const uint8_t port,
1257
1256
_ongoing_tx_msg.type = MCPS_PROPRIETARY;
1258
1257
_ongoing_tx_msg.fport = port;
1259
1258
_ongoing_tx_msg.nb_trials = 1 ;
1259
+ // a proprietary frame only includes an MHDR field which contains MTYPE field.
1260
+ // Everything else is at the discretion of the implementer
1261
+ fopts_len = 0 ;
1262
+ }
1263
+
1264
+ max_possible_size = get_max_possible_tx_size (fopts_len);
1265
+
1266
+ if (max_possible_size > MBED_CONF_LORA_TX_MAX_SIZE) {
1267
+ max_possible_size = MBED_CONF_LORA_TX_MAX_SIZE;
1268
+ }
1269
+
1270
+ if (max_possible_size < length) {
1271
+ tr_info (" Cannot transmit %d bytes. Possible TX Size is %d bytes" ,
1272
+ length, max_possible_size);
1273
+
1274
+ _ongoing_tx_msg.pending_size = length - max_possible_size;
1275
+ _ongoing_tx_msg.f_buffer_size = max_possible_size;
1276
+ memcpy (_ongoing_tx_msg.f_buffer , data, _ongoing_tx_msg.f_buffer_size );
1277
+ } else {
1278
+ _ongoing_tx_msg.f_buffer_size = length;
1279
+ _ongoing_tx_msg.pending_size = 0 ;
1280
+ if (length > 0 ) {
1281
+ memcpy (_ongoing_tx_msg.f_buffer , data, length);
1282
+ }
1260
1283
}
1261
1284
1262
1285
tr_info (" RTS = %u bytes, PEND = %u, Port: %u" ,
@@ -1567,8 +1590,10 @@ lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr,
1567
1590
1568
1591
_mac_commands.parse_mac_commands_to_repeat ();
1569
1592
1593
+ // We always add Port Field. Spec leaves it optional.
1594
+ _params.tx_buffer [pkt_header_len++] = frame_port;
1595
+
1570
1596
if ((payload != NULL ) && (_params.tx_buffer_len > 0 )) {
1571
- _params.tx_buffer [pkt_header_len++] = frame_port;
1572
1597
1573
1598
uint8_t *key = _params.keys .app_skey ;
1574
1599
uint32_t key_length = sizeof (_params.keys .app_skey ) * 8 ;
@@ -1759,35 +1784,30 @@ void LoRaMac::disconnect()
1759
1784
reset_mcps_indication ();
1760
1785
}
1761
1786
1762
- uint8_t LoRaMac::get_max_possible_tx_size (uint8_t size )
1787
+ uint8_t LoRaMac::get_max_possible_tx_size (uint8_t fopts_len )
1763
1788
{
1764
1789
uint8_t max_possible_payload_size = 0 ;
1765
- uint8_t current_payload_size = 0 ;
1766
- uint8_t fopt_len = _mac_commands.get_mac_cmd_length ()
1767
- + _mac_commands.get_repeat_commands_length ();
1790
+ uint8_t allowed_frm_payload_size = 0 ;
1768
1791
1769
1792
if (_params.sys_params .adr_on ) {
1770
1793
_lora_phy.get_next_ADR (false , _params.sys_params .channel_data_rate ,
1771
1794
_params.sys_params .channel_tx_power ,
1772
1795
_params.adr_ack_counter );
1773
1796
}
1774
1797
1775
- current_payload_size = _lora_phy.get_max_payload (_params.sys_params .channel_data_rate , _params.is_repeater_supported );
1798
+ allowed_frm_payload_size = _lora_phy->get_max_payload (_params.sys_params .channel_data_rate ,
1799
+ _params.is_repeater_supported );
1776
1800
1777
- if (current_payload_size >= fopt_len ) {
1778
- max_possible_payload_size = current_payload_size - fopt_len ;
1801
+ if (allowed_frm_payload_size >= fopts_len ) {
1802
+ max_possible_payload_size = allowed_frm_payload_size - fopts_len ;
1779
1803
} else {
1780
- max_possible_payload_size = current_payload_size ;
1781
- fopt_len = 0 ;
1804
+ max_possible_payload_size = allowed_frm_payload_size ;
1805
+ fopts_len = 0 ;
1782
1806
_mac_commands.clear_command_buffer ();
1783
1807
_mac_commands.clear_repeat_buffer ();
1784
1808
}
1785
1809
1786
- if (validate_payload_length (size, _params.sys_params .channel_data_rate ,
1787
- fopt_len) == false ) {
1788
- return max_possible_payload_size;
1789
- }
1790
- return current_payload_size;
1810
+ return max_possible_payload_size;
1791
1811
}
1792
1812
1793
1813
bool LoRaMac::nwk_joined ()
0 commit comments