diff --git a/lora/lora.c b/lora/lora.c index 307a3b6..f9a8193 100644 --- a/lora/lora.c +++ b/lora/lora.c @@ -400,8 +400,9 @@ LORA_STATUS lora_receive_ready() { // ============================================================================= // lora_receive: receive a buffer from lora fifo with continuous mode // ============================================================================= -LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ +LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t buffer_len, uint8_t* num_bytes_received) { uint8_t timeout_flag; + // LORA_STATUS rx_done = lora_receive_ready(); // We check if we've received a packet. // If we haven't received a packet, the output will be similar to lora_receive_ready in that case. @@ -423,6 +424,11 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ uint8_t num_bytes; LORA_STATUS fifo2_status = lora_read_register(LORA_REG_FIFO_RX_NUM_BYTES, &num_bytes); + if (num_bytes > buffer_len) + { + return LORA_BUFFER_UNDERSIZED; + } + #if defined( TESTRECEIVER ) char buf[255]; int len = sprintf( buf, "Numbytes: %d\r\n", num_bytes ); @@ -450,7 +456,7 @@ LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr){ pld_xtr_status = lora_read_register(LORA_REG_FIFO_RW, &packet); // Access LoRA FIFO data buffer pointer buffer_ptr[i] = packet; } - *buffer_len_ptr = num_bytes; + *num_bytes_received = num_bytes; if (pld_xtr_status == LORA_OK ) { return LORA_OK; } else { diff --git a/lora/lora.h b/lora/lora.h index cdabf3e..2e81f14 100644 --- a/lora/lora.h +++ b/lora/lora.h @@ -39,6 +39,7 @@ typedef enum LORA_STATUS { LORA_TRANSMIT_FAIL, LORA_RECEIVE_FAIL, LORA_TIMEOUT_FAIL, + LORA_BUFFER_UNDERSIZED, LORA_READY, LORA_WAITING } LORA_STATUS; @@ -165,6 +166,6 @@ LORA_STATUS lora_transmit(uint8_t* buffer_ptr, uint8_t buffer_len); LORA_STATUS lora_receive_ready(); -LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t* buffer_len_ptr); +LORA_STATUS lora_receive(uint8_t* buffer_ptr, uint8_t buffer_len, uint8_t* num_bytes_received); #endif \ No newline at end of file