55#include "../../power.h"
66#include "../../ngctrl.h"
77#include "../../debug.h"
8+ #include "../../config.h"
9+ #include "../setup.h"
810
911static const uint16_t ServiceUUID = 0xF055 ;
1012static const gattAttrType_t service = {2 , (uint8_t * )& ServiceUUID };
@@ -20,141 +22,145 @@ static uint8_t RxCharProps = GATT_PROP_WRITE;
2022static gattCharCfg_t TxCCCD [1 ];
2123
2224static 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
3638static 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
6062static 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
8486static gattServiceCBs_t service_handlers = {
85- read_handler ,
86- write_handler ,
87- NULL
87+ read_handler ,
88+ write_handler ,
89+ NULL
8890};
8991
9092static 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
103109int 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
116122static 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 */
129135bStatus_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}
0 commit comments