43
43
* CONSTANTS
44
44
******************************************************************************/
45
45
46
- static size_t const CBOR_NOTE_MSG_MAX_SIZE = 255 ;
46
+ static size_t const LORA_CBOR_MSG_MAX_SIZE = 242 ;
47
+ static size_t const LORA_PAYLOAD_MAX_SIZE = 236 ;
47
48
static size_t const DEFAULT_READ_INTERVAL_MS = 1000 ; // 1 second
48
49
static size_t const FAILSAFE_READ_INTERVAL_MS = 15000 ; // 15 seconds
49
50
@@ -73,7 +74,7 @@ ArduinoIoTCloudNotecard::ArduinoIoTCloudNotecard()
73
74
,_thing(&_message_stream)
74
75
,_device(&_message_stream)
75
76
,_notecard_last_poll_ms{static_cast <uint32_t >(-DEFAULT_READ_INTERVAL_MS)}
76
- ,_notecard_poll_interval_ms {DEFAULT_READ_INTERVAL_MS}
77
+ ,_notecard_polling_interval_ms {DEFAULT_READ_INTERVAL_MS}
77
78
,_interrupt_pin{-1 }
78
79
,_data_available{false }
79
80
#if OTA_ENABLED
@@ -278,7 +279,7 @@ bool ArduinoIoTCloudNotecard::available(void)
278
279
const bool interrupts_enabled = (_interrupt_pin >= 0 );
279
280
const uint32_t now_ms = ::millis ();
280
281
281
- bool check_data = ((now_ms - _notecard_last_poll_ms) > _notecard_poll_interval_ms );
282
+ bool check_data = ((now_ms - _notecard_last_poll_ms) > _notecard_polling_interval_ms );
282
283
if (interrupts_enabled) {
283
284
check_data = (_data_available || ((now_ms - _notecard_last_poll_ms) > FAILSAFE_READ_INTERVAL_MS));
284
285
}
@@ -354,8 +355,8 @@ void ArduinoIoTCloudNotecard::pollNotecard(void)
354
355
{
355
356
/* Decode available data. */
356
357
if (available ()) {
357
- size_t note_len = CBOR_NOTE_MSG_MAX_SIZE ;
358
- uint8_t note_buf[CBOR_NOTE_MSG_MAX_SIZE ];
358
+ size_t note_len = LORA_CBOR_MSG_MAX_SIZE ;
359
+ uint8_t note_buf[LORA_CBOR_MSG_MAX_SIZE ];
359
360
fetchIncomingBytes (note_buf, note_len);
360
361
processMessage (note_buf, note_len);
361
362
}
@@ -471,7 +472,7 @@ void ArduinoIoTCloudNotecard::sendMessage(Message * msg)
471
472
{
472
473
switch (msg->id ) {
473
474
case PropertiesUpdateCmdId:
474
- return sendThingPropertyContainerToCloud ();
475
+ sendThingPropertyContainerToCloud ();
475
476
break ;
476
477
477
478
default :
@@ -482,17 +483,23 @@ void ArduinoIoTCloudNotecard::sendMessage(Message * msg)
482
483
483
484
void ArduinoIoTCloudNotecard::sendCommandMsgToCloud (Message * msg_)
484
485
{
485
- size_t bytes_encoded = CBOR_NOTE_MSG_MAX_SIZE ;
486
- uint8_t data[CBOR_NOTE_MSG_MAX_SIZE ];
486
+ size_t bytes_encoded = LORA_CBOR_MSG_MAX_SIZE ;
487
+ uint8_t data[LORA_CBOR_MSG_MAX_SIZE ];
487
488
CBORMessageEncoder encoder;
488
489
NotecardConnectionHandler *notecard_connection = reinterpret_cast <NotecardConnectionHandler *>(_connection);
489
490
490
491
if (encoder.encode (msg_, data, bytes_encoded) == Encoder::Status::Complete) {
491
- if (bytes_encoded > 0 ) {
492
+ if (LORA_PAYLOAD_MAX_SIZE < bytes_encoded) {
493
+ DEBUG_WARNING (" Encoded %d bytes for Command Message. Exceeds maximum payload size of %d bytes, and cannot be sent to cloud." , bytes_encoded, LORA_PAYLOAD_MAX_SIZE);
494
+ } else if (bytes_encoded > 0 ) {
495
+ DEBUG_DEBUG (" Encoded %d bytes for Command Message" , bytes_encoded);
492
496
notecard_connection->setTopicType (NotecardConnectionHandler::TopicType::Command);
493
- notecard_connection->write (data, bytes_encoded);
497
+ if (notecard_connection->write (data, bytes_encoded)) {
498
+ DEBUG_ERROR (" Failed to send Command Message to cloud" );
499
+ }
500
+ } else {
501
+ DEBUG_DEBUG (" No bytes encoded for Command Message" );
494
502
}
495
- DEBUG_DEBUG (" Encoded %d bytes for Command Message" , bytes_encoded);
496
503
} else {
497
504
DEBUG_ERROR (" Failed to encode Command Message" );
498
505
}
@@ -501,15 +508,19 @@ void ArduinoIoTCloudNotecard::sendCommandMsgToCloud(Message * msg_)
501
508
void ArduinoIoTCloudNotecard::sendThingPropertyContainerToCloud ()
502
509
{
503
510
int bytes_encoded = 0 ;
504
- uint8_t data[CBOR_NOTE_MSG_MAX_SIZE ];
511
+ uint8_t data[LORA_CBOR_MSG_MAX_SIZE ];
505
512
NotecardConnectionHandler *notecard_connection = reinterpret_cast <NotecardConnectionHandler *>(_connection);
506
513
507
514
// Check if any property needs encoding and send them to the cloud
508
515
if (CBOREncoder::encode (_thing.getPropertyContainer (), data, sizeof (data), bytes_encoded, _thing.getPropertyContainerIndex (), USE_LIGHT_PAYLOADS) == CborNoError) {
509
- if (bytes_encoded > 0 ) {
510
- notecard_connection-> setTopicType (NotecardConnectionHandler::TopicType:: Thing);
511
- notecard_connection-> write (data, bytes_encoded);
516
+ if (LORA_PAYLOAD_MAX_SIZE < bytes_encoded ) {
517
+ DEBUG_ERROR ( " Encoded %d bytes for Thing properties. Exceeds maximum encoded payload size of %d bytes, and cannot sync with cloud. " , bytes_encoded, LORA_PAYLOAD_MAX_SIZE );
518
+ } else if ( bytes_encoded > 0 ) {
512
519
DEBUG_DEBUG (" Encoded %d bytes for Thing properties" , bytes_encoded);
520
+ notecard_connection->setTopicType (NotecardConnectionHandler::TopicType::Thing);
521
+ if (notecard_connection->write (data, bytes_encoded)) {
522
+ DEBUG_ERROR (" Failed to sync Thing properties with cloud" );
523
+ }
513
524
}
514
525
} else {
515
526
DEBUG_ERROR (" Failed to encode Thing properties" );
0 commit comments