@@ -1033,6 +1033,7 @@ lorawan_status_t LoRaMac::schedule_tx()
1033
1033
{
1034
1034
channel_selection_params_t next_channel;
1035
1035
lorawan_time_t backoff_time = 0 ;
1036
+ uint8_t fopts_len = 0 ;
1036
1037
1037
1038
if (_params.sys_params .max_duty_cycle == 255 ) {
1038
1039
return LORAWAN_STATUS_DEVICE_OFF;
@@ -1090,9 +1091,25 @@ lorawan_status_t LoRaMac::schedule_tx()
1090
1091
_params.rx_window2_delay = _params.sys_params .join_accept_delay2
1091
1092
+ _params.rx_window2_config .window_offset ;
1092
1093
} else {
1093
- if (validate_payload_length (_params.tx_buffer_len ,
1094
+
1095
+ // if the outgoing message is a proprietary message, it doesn't include any
1096
+ // standard message formatting except port and MHDR.
1097
+ if (_ongoing_tx_msg.type == MCPS_PROPRIETARY) {
1098
+ fopts_len = 0 ;
1099
+ } else {
1100
+ fopts_len = _mac_commands.get_mac_cmd_length () + _mac_commands.get_repeat_commands_length ();
1101
+ }
1102
+
1103
+ // A check was performed for validity of FRMPayload in ::prepare_ongoing_tx() API.
1104
+ // However, owing to the asynch nature of the send() API, we should check the
1105
+ // validity again, as datarate may have changed since we last attempted to transmit.
1106
+ if (validate_payload_length (_ongoing_tx_msg.f_buffer_size ,
1094
1107
_params.sys_params .channel_data_rate ,
1095
- _mac_commands.get_mac_cmd_length ()) == false ) {
1108
+ fopts_len) == false ) {
1109
+ tr_error (" Allowed FRMPayload = %d, FRMPayload = %d, MAC commands pending = %d" ,
1110
+ _lora_phy->get_max_payload (_params.sys_params .channel_data_rate ,
1111
+ _params.is_repeater_supported ),
1112
+ _ongoing_tx_msg.f_buffer_size , fopts_len);
1096
1113
return LORAWAN_STATUS_LENGTH_ERROR;
1097
1114
}
1098
1115
_params.rx_window1_delay = _params.sys_params .recv_delay1
@@ -1215,27 +1232,9 @@ int16_t LoRaMac::prepare_ongoing_tx(const uint8_t port,
1215
1232
uint8_t num_retries)
1216
1233
{
1217
1234
_ongoing_tx_msg.port = port;
1218
-
1219
- uint8_t max_possible_size = get_max_possible_tx_size (length);
1220
-
1221
- if (max_possible_size > MBED_CONF_LORA_TX_MAX_SIZE) {
1222
- max_possible_size = MBED_CONF_LORA_TX_MAX_SIZE;
1223
- }
1224
-
1225
- if (max_possible_size < length) {
1226
- tr_info (" Cannot transmit %d bytes. Possible TX Size is %d bytes" ,
1227
- length, max_possible_size);
1228
-
1229
- _ongoing_tx_msg.pending_size = length - max_possible_size;
1230
- _ongoing_tx_msg.f_buffer_size = max_possible_size;
1231
- memcpy (_ongoing_tx_msg.f_buffer , data, _ongoing_tx_msg.f_buffer_size );
1232
- } else {
1233
- _ongoing_tx_msg.f_buffer_size = length;
1234
- _ongoing_tx_msg.pending_size = 0 ;
1235
- if (length > 0 ) {
1236
- memcpy (_ongoing_tx_msg.f_buffer , data, length);
1237
- }
1238
- }
1235
+ uint8_t max_possible_size = 0 ;
1236
+ uint8_t fopts_len = _mac_commands.get_mac_cmd_length ()
1237
+ + _mac_commands.get_repeat_commands_length ();
1239
1238
1240
1239
// Handles unconfirmed messages
1241
1240
if (flags & MSG_UNCONFIRMED_FLAG) {
@@ -1256,6 +1255,30 @@ int16_t LoRaMac::prepare_ongoing_tx(const uint8_t port,
1256
1255
_ongoing_tx_msg.type = MCPS_PROPRIETARY;
1257
1256
_ongoing_tx_msg.fport = port;
1258
1257
_ongoing_tx_msg.nb_trials = 1 ;
1258
+ // a proprietary frame only includes an MHDR field which contains MTYPE field.
1259
+ // Everything else is at the discretion of the implementer
1260
+ fopts_len = 0 ;
1261
+ }
1262
+
1263
+ max_possible_size = get_max_possible_tx_size (fopts_len);
1264
+
1265
+ if (max_possible_size > MBED_CONF_LORA_TX_MAX_SIZE) {
1266
+ max_possible_size = MBED_CONF_LORA_TX_MAX_SIZE;
1267
+ }
1268
+
1269
+ if (max_possible_size < length) {
1270
+ tr_info (" Cannot transmit %d bytes. Possible TX Size is %d bytes" ,
1271
+ length, max_possible_size);
1272
+
1273
+ _ongoing_tx_msg.pending_size = length - max_possible_size;
1274
+ _ongoing_tx_msg.f_buffer_size = max_possible_size;
1275
+ memcpy (_ongoing_tx_msg.f_buffer , data, _ongoing_tx_msg.f_buffer_size );
1276
+ } else {
1277
+ _ongoing_tx_msg.f_buffer_size = length;
1278
+ _ongoing_tx_msg.pending_size = 0 ;
1279
+ if (length > 0 ) {
1280
+ memcpy (_ongoing_tx_msg.f_buffer , data, length);
1281
+ }
1259
1282
}
1260
1283
1261
1284
tr_info (" RTS = %u bytes, PEND = %u, Port: %u" ,
@@ -1566,8 +1589,10 @@ lorawan_status_t LoRaMac::prepare_frame(loramac_mhdr_t *machdr,
1566
1589
1567
1590
_mac_commands.parse_mac_commands_to_repeat ();
1568
1591
1592
+ // We always add Port Field. Spec leaves it optional.
1593
+ _params.tx_buffer [pkt_header_len++] = frame_port;
1594
+
1569
1595
if ((payload != NULL ) && (_params.tx_buffer_len > 0 )) {
1570
- _params.tx_buffer [pkt_header_len++] = frame_port;
1571
1596
1572
1597
uint8_t *key = _params.keys .app_skey ;
1573
1598
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