@@ -42,6 +42,9 @@ extern uint8_t macid[6];
4242// The 'id' we make using that mac-id
4343static char jade_id [16 ];
4444
45+ #define JADE_MSG_REPLY_LEN 256
46+ #define JADE_MSG_REPLY_OVERHEAD 64
47+
4548#ifdef CONFIG_HEAP_TRACING
4649
4750#include <esp_heap_trace.h>
@@ -603,16 +606,26 @@ void jade_process_reject_message_ex(const cbor_msg_t ctx, int code, const char*
603606void jade_process_reject_message (jade_process_t * process , int code , const char * message )
604607{
605608 if (HAS_CURRENT_MESSAGE (process )) {
606- uint8_t buf [256 ];
609+ uint8_t buf [JADE_MSG_REPLY_LEN ];
607610 jade_process_reject_message_ex (process -> ctx , code , message , NULL , 0 , buf , sizeof (buf ));
608611 } else {
609612 JADE_LOGW ("Ignoring attempt to reject 'no-message'" );
610613 }
611614}
612615
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 )
615617{
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+
616629 CborEncoder root_encoder ;
617630 cbor_encoder_init (& root_encoder , buffer , buflen , 0 );
618631
@@ -631,9 +644,14 @@ void jade_process_reply_to_message_bytes(
631644 cberr = cbor_encoder_close_container (& root_encoder , & root_map_encoder );
632645 JADE_ASSERT (cberr == CborNoError );
633646 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+ }
634652}
635653
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 ,
637655 const uint8_t * data , const size_t datalen , uint8_t * buffer , const size_t buflen )
638656{
639657 CborEncoder root_encoder ;
0 commit comments