@@ -42,6 +42,9 @@ extern uint8_t macid[6];
42
42
// The 'id' we make using that mac-id
43
43
static char jade_id [16 ];
44
44
45
+ #define JADE_MSG_REPLY_LEN 256
46
+ #define JADE_MSG_REPLY_OVERHEAD 64
47
+
45
48
#ifdef CONFIG_HEAP_TRACING
46
49
47
50
#include <esp_heap_trace.h>
@@ -603,16 +606,26 @@ void jade_process_reject_message_ex(const cbor_msg_t ctx, int code, const char*
603
606
void jade_process_reject_message (jade_process_t * process , int code , const char * message )
604
607
{
605
608
if (HAS_CURRENT_MESSAGE (process )) {
606
- uint8_t buf [256 ];
609
+ uint8_t buf [JADE_MSG_REPLY_LEN ];
607
610
jade_process_reject_message_ex (process -> ctx , code , message , NULL , 0 , buf , sizeof (buf ));
608
611
} else {
609
612
JADE_LOGW ("Ignoring attempt to reject 'no-message'" );
610
613
}
611
614
}
612
615
613
- void jade_process_reply_to_message_bytes (
614
- cbor_msg_t ctx , const uint8_t * data , const size_t datalen , uint8_t * buffer , const size_t buflen )
616
+ void jade_process_reply_to_message_bytes (const cbor_msg_t ctx , const uint8_t * data , const size_t datalen )
615
617
{
618
+ // Avoid allocating for small replies
619
+ uint8_t buf [JADE_MSG_REPLY_LEN ];
620
+ uint8_t * buffer = buf ;
621
+ size_t buflen = sizeof (buf );
622
+
623
+ if (datalen > JADE_MSG_REPLY_LEN - JADE_MSG_REPLY_OVERHEAD ) {
624
+ buflen = datalen + JADE_MSG_REPLY_OVERHEAD ;
625
+ JADE_ASSERT (buflen > sizeof (buf ));
626
+ buffer = JADE_MALLOC (buflen );
627
+ }
628
+
616
629
CborEncoder root_encoder ;
617
630
cbor_encoder_init (& root_encoder , buffer , buflen , 0 );
618
631
@@ -631,9 +644,14 @@ void jade_process_reply_to_message_bytes(
631
644
cberr = cbor_encoder_close_container (& root_encoder , & root_map_encoder );
632
645
JADE_ASSERT (cberr == CborNoError );
633
646
jade_process_push_out_message (buffer , cbor_encoder_get_buffer_size (& root_encoder , buffer ), ctx .source );
647
+
648
+ if (buffer != buf ) {
649
+ // Allocated buffer
650
+ free (buffer );
651
+ }
634
652
}
635
653
636
- void jade_process_reply_to_message_bytes_sequence (cbor_msg_t ctx , const size_t seqnum , const size_t seqlen ,
654
+ void jade_process_reply_to_message_bytes_sequence (const cbor_msg_t ctx , const size_t seqnum , const size_t seqlen ,
637
655
const uint8_t * data , const size_t datalen , uint8_t * buffer , const size_t buflen )
638
656
{
639
657
CborEncoder root_encoder ;
0 commit comments