Skip to content

Commit 7fdccc0

Browse files
feat: enable always on mode
1 parent ac64530 commit 7fdccc0

11 files changed

Lines changed: 644 additions & 579 deletions

File tree

src/ble/profile/legacy.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ static bStatus_t write_handler(uint16 connHandle, gattAttribute_t *pAttr,
2525

2626
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
2727
if(uuid == RxCharUUID) {
28-
if (legacy_ble_rx(pValue, len)) {
29-
return ATT_ERR_UNLIKELY;
30-
}
28+
legacy_ble_rx(pValue, len);
3129
return SUCCESS;
3230
}
3331
return ATT_ERR_ATTR_NOT_FOUND;
3432
}
3533

34+
3635
static gattServiceCBs_t service_handlers = {
3736
NULL,
3837
write_handler,

src/ble/profile/ng.c

Lines changed: 107 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "../../power.h"
66
#include "../../ngctrl.h"
77
#include "../../debug.h"
8+
#include "../../config.h"
9+
#include "../setup.h"
810

911
static const uint16_t ServiceUUID = 0xF055;
1012
static const gattAttrType_t service = {2, (uint8_t *)&ServiceUUID};
@@ -20,141 +22,145 @@ static uint8_t RxCharProps = GATT_PROP_WRITE;
2022
static gattCharCfg_t TxCCCD[1];
2123

2224
static gattAttribute_t attr_table[] = {
23-
ATTR_DECLAR(primaryServiceUUID, 2, GATT_PERMIT_READ, &service),
25+
ATTR_DECLAR(primaryServiceUUID, 2, GATT_PERMIT_READ, &service),
2426

25-
CHAR_DECLAR(&RxCharProps),
26-
CHAR_VAL_DECLAR(&RxCharUUID, 2, GATT_PERMIT_WRITE, RxCharVal),
27+
CHAR_DECLAR(&RxCharProps),
28+
CHAR_VAL_DECLAR(&RxCharUUID, 2, GATT_PERMIT_WRITE, RxCharVal),
2729

28-
CHAR_DECLAR(&TxCharProps),
29-
CHAR_VAL_DECLAR(&TxCharUUID, 2, GATT_PERMIT_READ, TxCharVal),
30-
ATTR_DECLAR(clientCharCfgUUID, 2, GATT_PERMIT_READ | GATT_PERMIT_WRITE, TxCCCD),
30+
CHAR_DECLAR(&TxCharProps),
31+
CHAR_VAL_DECLAR(&TxCharUUID, 2, GATT_PERMIT_READ, TxCharVal),
32+
ATTR_DECLAR(clientCharCfgUUID, 2, GATT_PERMIT_READ | GATT_PERMIT_WRITE, TxCCCD),
3133
};
3234

3335
#define notiAttr attr_table[4]
3436

3537

3638
static bStatus_t write_handler(uint16 connHandle, gattAttribute_t *pAttr,
37-
uint8 *pValue, uint16 len, uint16 offset, uint8 method)
39+
uint8 *pValue, uint16 len, uint16 offset, uint8 method)
3840
{
39-
PRINT("ble: write_handler(): connHandle: %04X\n", connHandle);
40-
41-
if (!gattPermitWrite(pAttr->permissions)) {
42-
return ATT_ERR_WRITE_NOT_PERMITTED;
43-
}
44-
45-
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
46-
if(uuid == GATT_CLIENT_CHAR_CFG_UUID) {
47-
bStatus_t ret = GATTServApp_ProcessCCCWriteReq(connHandle, pAttr, pValue, len,
48-
offset, GATT_CLIENT_CFG_NOTIFY);
49-
PRINT("ble: CCCD changed: %02X\n", TxCCCD->value);
50-
return ret;
51-
}
52-
53-
if (uuid == RxCharUUID) {
54-
return ng_parse(pValue, len);
55-
}
56-
57-
return ATT_ERR_ATTR_NOT_FOUND;
41+
PRINT("ble: write_handler(): connHandle: %04X\n", connHandle);
42+
43+
if (!gattPermitWrite(pAttr->permissions)) {
44+
return ATT_ERR_WRITE_NOT_PERMITTED;
45+
}
46+
47+
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
48+
if(uuid == GATT_CLIENT_CHAR_CFG_UUID) {
49+
bStatus_t ret = GATTServApp_ProcessCCCWriteReq(connHandle, pAttr, pValue, len,
50+
offset, GATT_CLIENT_CFG_NOTIFY);
51+
PRINT("ble: CCCD changed: %02X\n", TxCCCD->value);
52+
return ret;
53+
}
54+
55+
if (uuid == RxCharUUID) {
56+
return ng_parse(pValue, len);
57+
}
58+
59+
return ATT_ERR_ATTR_NOT_FOUND;
5860
}
5961

6062
static bStatus_t read_handler(uint16_t connHandle, gattAttribute_t *pAttr,
61-
uint8_t *pValue, uint16_t *pLen, uint16_t offset,
62-
uint16_t maxLen, uint8_t method)
63+
uint8_t *pValue, uint16_t *pLen, uint16_t offset,
64+
uint16_t maxLen, uint8_t method)
6365
{
64-
if (!gattPermitRead(pAttr->permissions)) {
65-
return ATT_ERR_READ_NOT_PERMITTED;
66-
}
67-
68-
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
69-
if(uuid == GATT_CLIENT_CHAR_CFG_UUID) {
70-
*pLen = 2;
71-
tmos_memcpy(pValue, pAttr->pValue, *pLen);
72-
return SUCCESS;
73-
}
74-
75-
if (uuid == TxCharUUID) {
76-
*pLen = MIN(TxLen-offset, maxLen);
77-
tmos_memcpy(pValue, &pAttr->pValue[offset], *pLen);
78-
return SUCCESS;
79-
}
80-
81-
return ATT_ERR_ATTR_NOT_FOUND;
66+
if (!gattPermitRead(pAttr->permissions)) {
67+
return ATT_ERR_READ_NOT_PERMITTED;
68+
}
69+
70+
uint16_t uuid = BUILD_UINT16(pAttr->type.uuid[0], pAttr->type.uuid[1]);
71+
if(uuid == GATT_CLIENT_CHAR_CFG_UUID) {
72+
*pLen = 2;
73+
tmos_memcpy(pValue, pAttr->pValue, *pLen);
74+
return SUCCESS;
75+
}
76+
77+
if (uuid == TxCharUUID) {
78+
*pLen = MIN(TxLen-offset, maxLen);
79+
tmos_memcpy(pValue, &pAttr->pValue[offset], *pLen);
80+
return SUCCESS;
81+
}
82+
83+
return ATT_ERR_ATTR_NOT_FOUND;
8284
}
8385

8486
static gattServiceCBs_t service_handlers = {
85-
read_handler,
86-
write_handler,
87-
NULL
87+
read_handler,
88+
write_handler,
89+
NULL
8890
};
8991

9092
static void connStatus_handler(uint16 connHandle, uint8 changeType)
9193
{
92-
if(connHandle == LOOPBACK_CONNHANDLE)
93-
return;
94-
95-
// Reset ClientCharConfig if connection has dropped
96-
if((changeType == LINKDB_STATUS_UPDATE_REMOVED)
97-
|| ((changeType == LINKDB_STATUS_UPDATE_STATEFLAGS)
98-
&& (!linkDB_Up(connHandle)))) {
99-
GATTServApp_InitCharCfg(connHandle, TxCCCD);
100-
}
94+
if(connHandle == LOOPBACK_CONNHANDLE)
95+
return;
96+
97+
// Reset ClientCharConfig if connection has dropped
98+
if((changeType == LINKDB_STATUS_UPDATE_REMOVED)
99+
|| ((changeType == LINKDB_STATUS_UPDATE_STATEFLAGS)
100+
&& (!linkDB_Up(connHandle)))) {
101+
GATTServApp_InitCharCfg(connHandle, TxCCCD);
102+
}
103+
if (badge_cfg.ble_always_on) {
104+
PRINT("BLE disconnected, restarting advertising due to Always-On\n");
105+
ble_enable_advertise();
106+
}
101107
}
102108

103109
int ng_registerService()
104110
{
105-
uint8 status = SUCCESS;
106-
GATTServApp_InitCharCfg(INVALID_CONNHANDLE, TxCCCD);
107-
linkDB_Register(connStatus_handler);
108-
109-
status = GATTServApp_RegisterService(attr_table,
110-
GATT_NUM_ATTRS(attr_table),
111-
GATT_MAX_ENCRYPT_KEY_SIZE,
112-
&service_handlers);
113-
return (status);
111+
uint8 status = SUCCESS;
112+
GATTServApp_InitCharCfg(INVALID_CONNHANDLE, TxCCCD);
113+
linkDB_Register(connStatus_handler);
114+
115+
status = GATTServApp_RegisterService(attr_table,
116+
GATT_NUM_ATTRS(attr_table),
117+
GATT_MAX_ENCRYPT_KEY_SIZE,
118+
&service_handlers);
119+
return (status);
114120
}
115121

116122
static uint8 isNotifyEnabled(uint16 connHandle)
117123
{
118-
uint16_t val = GATTServApp_ReadCharCfg(connHandle, TxCCCD);
119-
return val & GATT_CLIENT_CFG_NOTIFY;
124+
uint16_t val = GATTServApp_ReadCharCfg(connHandle, TxCCCD);
125+
return val & GATT_CLIENT_CFG_NOTIFY;
120126
}
121127
/**
122128
* @brief Send notify to client. Currently support one client connection
123129
* only.
124130
*
125-
* @param val Value to be sent
126-
* @param len length of val. This should not be larger than MTU.
127-
* @return bStatus_t
131+
* @param val Value to be sent
132+
* @param len length of val. This should not be larger than MTU.
133+
* @return bStatus_t
128134
*/
129135
bStatus_t ng_notify(uint8_t *val, uint8_t len)
130136
{
131-
if(!isNotifyEnabled(TxCCCD->connHandle)) {
132-
PRINT("ble: ng_notify() notify is not enabled\n");
133-
return bleIncorrectMode;
134-
}
135-
if(len > ATT_GetMTU(TxCCCD->connHandle)) {
136-
return bleInvalidRange;
137-
}
138-
139-
attHandleValueNoti_t noti = {
140-
.handle = notiAttr.handle,
141-
.len = len
142-
};
143-
noti.pValue = GATT_bm_alloc(TxCCCD->connHandle, ATT_HANDLE_VALUE_NOTI,
144-
len, NULL, 0);
145-
if (noti.pValue == NULL) {
146-
return bleMemAllocError;
147-
}
148-
149-
tmos_memcpy(noti.pValue, val, len);
150-
151-
bStatus_t ret = GATT_Notification(TxCCCD->connHandle, &noti, FALSE);
152-
GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI);
153-
if (ret != SUCCESS) {
154-
PRINT("ble: noti sending failed\n");
155-
return ret;
156-
}
157-
158-
PRINT("ble: noti sent\n");
159-
return SUCCESS;
137+
if(!isNotifyEnabled(TxCCCD->connHandle)) {
138+
PRINT("ble: ng_notify() notify is not enabled\n");
139+
return bleIncorrectMode;
140+
}
141+
if(len > ATT_GetMTU(TxCCCD->connHandle)) {
142+
return bleInvalidRange;
143+
}
144+
145+
attHandleValueNoti_t noti = {
146+
.handle = notiAttr.handle,
147+
.len = len
148+
};
149+
noti.pValue = GATT_bm_alloc(TxCCCD->connHandle, ATT_HANDLE_VALUE_NOTI,
150+
len, NULL, 0);
151+
if (noti.pValue == NULL) {
152+
return bleMemAllocError;
153+
}
154+
155+
tmos_memcpy(noti.pValue, val, len);
156+
157+
bStatus_t ret = GATT_Notification(TxCCCD->connHandle, &noti, FALSE);
158+
GATT_bm_free((gattMsg_t *)&noti, ATT_HANDLE_VALUE_NOTI);
159+
if (ret != SUCCESS) {
160+
PRINT("ble: noti sending failed\n");
161+
return ret;
162+
}
163+
164+
PRINT("ble: noti sent\n");
165+
return SUCCESS;
160166
}

src/ble/setup.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
static __attribute__((aligned(4), section(".noinit")))
4141
uint32_t MEM_BUF[BLE_MEMHEAP_SIZE / 4];
4242

43+
if (badge_cfg.ble_always_on) {
44+
ble_enable_advertise();
45+
} else {
46+
ble_disable_advertise();
47+
}
48+
4349
static void lsi_calib(void)
4450
{
4551
Calibration_LSI(Level_128);

src/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ 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;
4344

4445
#endif /* __CONFIG_H__ */

src/legacyctrl.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
#include "debug.h"
55
#include "legacyctrl.h"
66

7-
int legacy_ble_rx(uint8_t *val, uint16_t len)
7+
void legacy_ble_rx(uint8_t *val, uint16_t len)
88
{
99
_TRACE();
1010
static uint16_t c, data_len, n;
1111
static uint8_t *data;
1212

1313
if (len != LEGACY_TRANSFER_WIDTH) {
1414
PRINT("Transfer width is not matched\n");
15-
return -1;
1615
}
1716

1817
PRINT("val[%d]: ", len);
@@ -24,13 +23,11 @@ int legacy_ble_rx(uint8_t *val, uint16_t len)
2423
if (c == 0) {
2524
if (memcmp(val, "wang", 5)) {
2625
PRINT("Not a header\n");
27-
return -2;
2826
} else {
2927
free(data);
3028
data = malloc(sizeof(data_legacy_t));
3129
if (!data) {
3230
PRINT("insufficient memory\n");
33-
return -3;
3431
}
3532
}
3633
} else { // Re attempt after a failed transfer
@@ -40,7 +37,6 @@ int legacy_ble_rx(uint8_t *val, uint16_t len)
4037
data = malloc(sizeof(data_legacy_t));
4138
if (!data) {
4239
PRINT("insufficient memory\n");
43-
return -3;
4440
}
4541
}
4642
}
@@ -56,7 +52,6 @@ int legacy_ble_rx(uint8_t *val, uint16_t len)
5652
data = realloc(data, data_len);
5753
if (!data) {
5854
PRINT("insufficient memory\n");
59-
return -3;
6055
}
6156
}
6257

@@ -70,10 +65,9 @@ int legacy_ble_rx(uint8_t *val, uint16_t len)
7065
}
7166

7267
c++;
73-
return 0;
7468
}
7569

76-
int legacy_usb_rx(uint8_t *buf, uint16_t len)
70+
void legacy_usb_rx(uint8_t *buf, uint16_t len)
7771
{
7872
static uint16_t rx_len, data_len;
7973
static uint8_t *data;
@@ -83,8 +77,10 @@ int legacy_usb_rx(uint8_t *buf, uint16_t len)
8377
buf[4], buf[5], buf[6], buf[7]);
8478

8579
if (rx_len == 0) {
86-
if (memcmp(buf, "wang", 5))
87-
return -1;
80+
if (memcmp(buf, "wang", 5)) {
81+
PRINT("Invalid header\n");
82+
return; // exit early from void function
83+
}
8884

8985
int init_len = len > LEGACY_HEADER_SIZE ? len : sizeof(data_legacy_t);
9086
init_len += MAX_PACKET_SIZE;
@@ -106,5 +102,4 @@ int legacy_usb_rx(uint8_t *buf, uint16_t len)
106102
free(data);
107103
handle_after_rx();
108104
}
109-
return 0;
110105
}

src/legacyctrl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <stdint.h>
55

66
void handle_after_rx();
7-
int legacy_ble_rx(uint8_t *val, uint16_t len);
8-
int legacy_usb_rx(uint8_t *buf, uint16_t len);
7+
void legacy_ble_rx(uint8_t *val, uint16_t len);
8+
void legacy_usb_rx(uint8_t *buf, uint16_t len);
99

1010
#endif /* __LEGACYCTRL_H__ */

0 commit comments

Comments
 (0)