Skip to content

Commit 57d91e0

Browse files
feat: updated according sourcery
1 parent 7fdccc0 commit 57d91e0

7 files changed

Lines changed: 274 additions & 269 deletions

File tree

src/ble/setup.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#include <memory.h>
22
#include <stdlib.h>
3-
4-
#include "setup.h"
5-
#include "CH58xBLE_LIB.h"
3+
#include <stdint.h>
4+
#include <string.h>
65
#include "CH58x_common.h"
76

7+
#include "config.h"
8+
#include "debug.h"
9+
#include "legacyctrl.h"
10+
#include "ngctrl.h"
11+
812
#ifndef BLE_BUFF_LEN
913
// MTU = 64 but clients should request new MTU, otherwise default will be 23
1014
#define BLE_BUFF_LEN (64 + 4)
@@ -40,12 +44,6 @@
4044
static __attribute__((aligned(4), section(".noinit")))
4145
uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
4246

43-
if (badge_cfg.ble_always_on) {
44-
ble_enable_advertise();
45-
} else {
46-
ble_disable_advertise();
47-
}
48-
4947
static void lsi_calib(void)
5048
{
5149
Calibration_LSI(Level_128);
@@ -87,3 +85,21 @@ void ble_hardwareInit(void)
8785

8886
BLE_LibInit(&cfg);
8987
}
88+
89+
void ble_setup(void)
90+
{
91+
ble_hardwareInit();
92+
tmos_clockInit();
93+
peripheral_init();
94+
95+
if (badge_cfg.ble_always_on) {
96+
ble_enable_advertise();
97+
} else {
98+
ble_disable_advertise();
99+
}
100+
101+
devInfo_registerService();
102+
legacy_registerService();
103+
batt_registerService();
104+
ng_registerService();
105+
}

src/config.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define __CONFIG_H__
33

44
#include <stdint.h>
5-
5+
#include <stdbool.h>
66
#include "xbm.h"
77
#include "leddrv.h"
88

@@ -40,6 +40,8 @@ int cfg_readflash(uint16_t flash_offs, badge_cfg_t *cfg);
4040
int cfg_writeflash_def(badge_cfg_t *cfg);
4141
int cfg_readflash_def(badge_cfg_t *cfg);
4242
void cfg_fallback();
43-
bool ble_always_on;
4443

45-
#endif /* __CONFIG_H__ */
44+
// Declare as extern to avoid multiple definition errors
45+
extern bool ble_always_on;
46+
47+
#endif /* __CONFIG_H__ */

src/legacyctrl.c

Lines changed: 110 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,127 @@
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.

src/ngctrl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include "ngctrl.h"
32

43
#include "CH58xBLE_LIB.h"
@@ -9,6 +8,7 @@
98
#include "debug.h"
109
#include "config.h"
1110
#include "leddrv.h"
11+
#include "ble/setup.h"
1212
#include <stdbool.h>
1313
// TODO: Some of configs can be added, just listing:
1414
// - Remote brighness adjusting

0 commit comments

Comments
 (0)