33#include "leddrv.h"
44#include "debug.h"
55#include "legacyctrl.h"
6+ #include <stdlib.h>
7+ #include <string.h>
68
7- void legacy_ble_rx (uint8_t * val , uint16_t len )
8- {
9- _TRACE ();
10- static uint16_t c , data_len , n ;
11- static uint8_t * data ;
9+ typedef enum {
10+ LEGACY_BLE_RX_OK = 0 ,
11+ LEGACY_BLE_RX_ERR_WIDTH ,
12+ LEGACY_BLE_RX_ERR_HEADER ,
13+ LEGACY_BLE_RX_ERR_MEM ,
14+ LEGACY_BLE_RX_ERR_COPY ,
15+ LEGACY_BLE_RX_ERR_UNKNOWN
16+ // Add more error codes as needed
17+ } legacy_ble_rx_error_t ;
1218
13- if (len != LEGACY_TRANSFER_WIDTH ) {
14- PRINT ("Transfer width is not matched\n" );
15- }
19+ static volatile legacy_ble_rx_error_t legacy_ble_rx_error = LEGACY_BLE_RX_OK ;
1620
17- PRINT ("val[%d]: " , len );
18- for (int i = 0 ; i < len ; i ++ ) {
19- PRINT ("%02X " , val [i ]);
20- }
21- PRINT ("\n" );
21+ // Retrieve and clear the last error state
22+ legacy_ble_rx_error_t legacy_ble_rx_get_last_error (void ) {
23+ legacy_ble_rx_error_t err = legacy_ble_rx_error ;
24+ legacy_ble_rx_error = LEGACY_BLE_RX_OK ;
25+ return err ;
26+ }
2227
23- if (c == 0 ) {
24- if (memcmp (val , "wang" , 5 )) {
25- PRINT ("Not a header\n" );
26- } else {
27- free (data );
28- data = malloc (sizeof (data_legacy_t ));
29- if (!data ) {
30- PRINT ("insufficient memory\n" );
31- }
32- }
33- } else { // Re attempt after a failed transfer
34- if (!memcmp (val , "wang" , 5 )) {
35- free (data );
36- c = 0 ;
37- data = malloc (sizeof (data_legacy_t ));
38- if (!data ) {
39- PRINT ("insufficient memory\n" );
40- }
41- }
42- }
28+ void legacy_ble_rx (uint8_t * val , uint16_t len )
29+ {
30+ _TRACE ();
31+ static uint16_t c = 0 , data_len = 0 , n = 0 ;
32+ static uint8_t * data = NULL ;
4333
44- PRINT ( "Copying BLE value\n" );
45- memcpy ( data + c * len , val , len ) ;
34+ // Always set error to OK at function start
35+ legacy_ble_rx_error = LEGACY_BLE_RX_OK ;
4636
47- if (c == 1 ) {
48- data_legacy_t * d = (data_legacy_t * )data ;
49- n = bigendian16_sum (d -> sizes , 8 );
50- data_len = LEGACY_HEADER_SIZE + LED_ROWS * n ;
51- PRINT ("Data len: %d\n" , data_len );
52- data = realloc (data , data_len );
53- if (!data ) {
54- PRINT ("insufficient memory\n" );
55- }
56- }
37+ if (len != LEGACY_TRANSFER_WIDTH ) {
38+ PRINT ("Transfer width is not matched\n" );
39+ legacy_ble_rx_error = LEGACY_BLE_RX_ERR_WIDTH ;
40+ return ;
41+ }
5742
58- if (c > 2 && ((c + 1 ) * LEGACY_TRANSFER_WIDTH ) >= data_len ) {
59- PRINT ("All bitmaps data received successfully\nWriting to flash.. " );
60- data_flatSave (data , data_len );
61- free (data );
62- data = NULL ;
63- handle_after_rx ();
64- PRINT ("Done\n" );
65- }
43+ PRINT ("val[%d]: " , len );
44+ for (int i = 0 ; i < len ; i ++ ) {
45+ PRINT ("%02X " , val [i ]);
46+ }
47+ PRINT ("\n" );
6648
67- c ++ ;
68- }
49+ if (c == 0 ) {
50+ if (memcmp (val , "wang" , 5 )) {
51+ PRINT ("Not a header\n" );
52+ legacy_ble_rx_error = LEGACY_BLE_RX_ERR_HEADER ;
53+ return ;
54+ } else {
55+ free (data );
56+ data = malloc (sizeof (data_legacy_t ));
57+ if (!data ) {
58+ PRINT ("insufficient memory\n" );
59+ legacy_ble_rx_error = LEGACY_BLE_RX_ERR_MEM ;
60+ return ;
61+ }
62+ }
63+ } else { // Re attempt after a failed transfer
64+ if (!memcmp (val , "wang" , 5 )) {
65+ free (data );
66+ c = 0 ;
67+ data = malloc (sizeof (data_legacy_t ));
68+ if (!data ) {
69+ PRINT ("insufficient memory\n" );
70+ legacy_ble_rx_error = LEGACY_BLE_RX_ERR_MEM ;
71+ return ;
72+ }
73+ }
74+ }
6975
70- void legacy_usb_rx (uint8_t * buf , uint16_t len )
71- {
72- static uint16_t rx_len , data_len ;
73- static uint8_t * data ;
74-
75- PRINT ("dump first 8 bytes: %02x %02x %02x %02x %02x %02x %02x %02x\n" ,
76- buf [0 ], buf [1 ], buf [2 ], buf [3 ],
77- buf [4 ], buf [5 ], buf [6 ], buf [7 ]);
76+ PRINT ("Copying BLE value\n" );
77+ // Defensive: check pointer before memcpy
78+ if (data == NULL ) {
79+ legacy_ble_rx_error = LEGACY_BLE_RX_ERR_MEM ;
80+ return ;
81+ }
82+ memcpy (data + c * len , val , len );
7883
79- if (rx_len == 0 ) {
80- if (memcmp (buf , "wang" , 5 )) {
81- PRINT ("Invalid header\n" );
82- return ; // exit early from void function
83- }
84+ if (c == 1 ) {
85+ data_legacy_t * d = (data_legacy_t * )data ;
86+ n = bigendian16_sum (d -> sizes , 8 );
87+ data_len = LEGACY_HEADER_SIZE + LED_ROWS * n ;
88+ data = realloc (data , data_len );
89+ if (!data ) {
90+ PRINT ("insufficient memory\n" );
91+ legacy_ble_rx_error = LEGACY_BLE_RX_ERR_MEM ;
92+ return ;
93+ }
94+ }
8495
85- int init_len = len > LEGACY_HEADER_SIZE ? len : sizeof (data_legacy_t );
86- init_len += MAX_PACKET_SIZE ;
87- data = malloc (init_len );
88- }
96+ if (c > 2 && ((c + 1 ) * LEGACY_TRANSFER_WIDTH ) >= data_len ) {
97+ PRINT ("All bitmaps data received successfully\nWriting to flash.. " );
98+ if (data == NULL ) {
99+ legacy_ble_rx_error = LEGACY_BLE_RX_ERR_MEM ;
100+ return ;
101+ }
102+ data_flatSave (data , data_len );
103+ free (data );
104+ data = NULL ;
105+ handle_after_rx ();
106+ PRINT ("Done\n" );
107+ c = 0 ;
108+ data_len = 0 ;
109+ n = 0 ;
110+ legacy_ble_rx_error = LEGACY_BLE_RX_OK ;
111+ return ;
112+ }
89113
90- memcpy (data + rx_len , buf , len );
91- rx_len += len ;
114+ c ++ ;
115+ legacy_ble_rx_error = LEGACY_BLE_RX_OK ;
116+ }
92117
93- if (!data_len ) {
94- data_legacy_t * d = (data_legacy_t * )data ;
95- uint16_t n = bigendian16_sum (d -> sizes , 8 );
96- data_len = LEGACY_HEADER_SIZE + LED_ROWS * n ;
97- data = realloc (data , data_len );
98- }
118+ // Example of a caller checking for error after calling legacy_ble_rx
119+ void example_caller (uint8_t * val , uint16_t len ) {
120+ legacy_ble_rx (val , len );
121+ legacy_ble_rx_error_t err = legacy_ble_rx_get_last_error ();
122+ if (err != LEGACY_BLE_RX_OK ) {
123+ PRINT ("legacy_ble_rx failed with error code: %d\n" , err );
124+ // Handle error accordingly
125+ }
126+ }
99127
100- if ((rx_len > LEGACY_HEADER_SIZE ) && rx_len >= data_len ) {
101- data_flatSave (data , data_len );
102- free (data );
103- handle_after_rx ();
104- }
105- }
128+ // For thread safety in a multithreaded context,
129+ // use mutexes or atomic operations around legacy_ble_rx_error access.
0 commit comments