1
2 #include <aos/kernel.h>
3 //#include <aos/osal_debug.h>
4 #include <aos/ble.h>
5 #include <bt_errno.h>
6 #include "yoc/uart_server.h"
7
8
9
10 enum {
11 YOC_UART_IDX_SVC,
12
13 YOC_UART_IDX_RX_CHAR,
14 YOC_UART_IDX_RX_VAL,
15 YOC_UART_IDX_RX_DES,
16
17 YOC_UART_IDX_TX_CHAR,
18 YOC_UART_IDX_TX_VAL,
19 YOC_UART_IDX_TX_CCC,
20 YOC_UART_IDX_TX_DES,
21
22 YOC_UART_IDX_MAX,
23 };
24
25
26 //static struct bt_gatt_ccc_cfg_t ccc_data[2] = {};
27 static gatt_service g_uart_profile;
28
29 extern uint8_t llState;
30
31 static ble_uart_server_t *g_uart_dev = NULL;
32
33 static char rx_char_des[] = "YoC UART RX";
34 static char tx_char_des[] = "YoC UART TX";
35
36 gatt_attr_t uart_attrs[YOC_UART_IDX_MAX] = {
37 [YOC_UART_IDX_SVC] = GATT_PRIMARY_SERVICE_DEFINE(YOC_UART_SERVICE_UUID),
38
39 [YOC_UART_IDX_RX_CHAR] = GATT_CHAR_DEFINE(YOC_UART_RX_UUID, GATT_CHRC_PROP_WRITE),
40 [YOC_UART_IDX_RX_VAL] = GATT_CHAR_VAL_DEFINE(YOC_UART_RX_UUID, GATT_PERM_READ | GATT_PERM_WRITE),
41 [YOC_UART_IDX_RX_DES] = GATT_CHAR_CUD_DEFINE(rx_char_des, GATT_PERM_READ),
42
43 [YOC_UART_IDX_TX_CHAR] = GATT_CHAR_DEFINE(YOC_UART_TX_UUID, GATT_CHRC_PROP_NOTIFY | GATT_CHRC_PROP_READ),
44 [YOC_UART_IDX_TX_VAL] = GATT_CHAR_VAL_DEFINE(YOC_UART_TX_UUID, GATT_PERM_READ),
45 [YOC_UART_IDX_TX_CCC] = GATT_CHAR_CCC_DEFINE(),
46 [YOC_UART_IDX_TX_DES] = GATT_CHAR_CUD_DEFINE(tx_char_des, GATT_PERM_READ),
47 };
48
49
conn_change(ble_event_en event,void * event_data)50 static void conn_change(ble_event_en event, void *event_data)
51 {
52 evt_data_gap_conn_change_t *e = (evt_data_gap_conn_change_t *)event_data;
53
54 ble_uart_server_t *node = g_uart_dev;
55
56 if (!node) {
57 return;
58 }
59
60 if (e->connected == CONNECTED) {
61 node->conn_handle = e->conn_handle;
62 } else {
63 node->conn_handle = -1;
64 }
65
66 node->uart_event_callback(event, event_data);
67
68 }
69
70
mtu_exchange(ble_event_en event,void * event_data)71 static void mtu_exchange(ble_event_en event, void *event_data)
72 {
73 evt_data_gatt_mtu_exchange_t *e = (evt_data_gatt_mtu_exchange_t *)event_data;
74
75 ble_uart_server_t *node = g_uart_dev;
76
77 if (!node) {
78 return;
79 }
80
81 if (e->err == 0) {
82 if (node->conn_handle == e->conn_handle) {
83 node->mtu_exchanged = 1;
84 node->mtu = ble_stack_gatt_mtu_get(node->conn_handle);
85 }
86
87 conn_param_t param = {
88 0x08, //interval min 20ms
89 0x08, //interval max 20ms
90 0,
91 100, //supervision timeout 1s
92 };
93 ble_stack_connect_param_update(e->conn_handle, ¶m);
94
95 node->uart_event_callback(event, event_data);
96 }
97 }
98
event_char_ccc_change(ble_event_en event,void * event_data)99 static void event_char_ccc_change(ble_event_en event, void *event_data)
100 {
101 evt_data_gatt_char_ccc_change_t *e = (evt_data_gatt_char_ccc_change_t *)event_data;
102
103 ble_uart_server_t *uart = g_uart_dev;
104
105 if (!uart) {
106 return;
107 }
108
109 uart->server_data.tx_ccc_value = e->ccc_value;
110
111 if ((uart->update_param_flag || uart->conn_update_def_on) && uart->conn_param != NULL) {
112 ble_stack_connect_param_update(uart->conn_handle, (conn_param_t *)uart->conn_param);
113 }
114 }
115
event_char_write(ble_event_en event,void * event_data)116 static void event_char_write(ble_event_en event, void *event_data)
117 {
118
119 evt_data_gatt_char_write_t *e = (evt_data_gatt_char_write_t *)event_data;
120 ble_uart_server_t *uart = g_uart_dev;
121
122 if (!uart) {
123 return;
124 }
125
126 e->len = e->len < RX_MAX_LEN ? e->len : RX_MAX_LEN;
127 uart->uart_recv(e->data, e->len);
128 e->len = 0;
129 return;
130 }
131
uart_server_disconn(uart_handle_t handle)132 int uart_server_disconn(uart_handle_t handle)
133 {
134 ble_uart_server_t *uart = (ble_uart_server_t *)handle;
135
136 if (!uart) {
137 return -1;
138 }
139
140 if (ble_stack_disconnect(uart->conn_handle)) {
141 return -1;
142 }
143
144 return 0;
145 }
146
147
uart_event_callback(ble_event_en event,void * event_data)148 static int uart_event_callback(ble_event_en event, void *event_data)
149 {
150
151 switch (event) {
152 case EVENT_GAP_CONN_CHANGE:
153 conn_change(event, event_data);
154 break;
155
156 case EVENT_GATT_MTU_EXCHANGE:
157 mtu_exchange(event, event_data);
158 break;
159
160 case EVENT_GATT_CHAR_CCC_CHANGE:
161 event_char_ccc_change(event, event_data);
162 break;
163
164 case EVENT_GATT_CHAR_WRITE:
165 event_char_write(event, event_data);
166 break;
167
168 default:
169 break;
170 }
171
172 return 0;
173 }
174
175 static ble_event_cb_t ble_cb = {
176 .callback = uart_event_callback,
177 };
178
179
uart_server_send(uart_handle_t handle,const char * data,int length,bt_uart_send_cb * cb)180 int uart_server_send(uart_handle_t handle, const char *data, int length, bt_uart_send_cb *cb)
181 {
182
183 uint32_t count = length;
184 uint16_t wait_timer = 0;
185 int ret = 0;
186 ble_uart_server_t *uart = (ble_uart_server_t *)handle;
187
188 if (!data || !length || !uart || uart->conn_handle < 0 || uart->server_data.tx_ccc_value != CCC_VALUE_NOTIFY) {
189 return -1;
190 }
191
192 if (cb != NULL && cb->start != NULL) {
193 cb->start(0, NULL);
194 }
195
196 while (count) {
197 uint16_t send_count = (uart->mtu - 3) < count ? (uart->mtu - 3) : count;
198 ret = ble_stack_gatt_notificate(uart->conn_handle, uart->uart_svc_handle + YOC_UART_IDX_TX_VAL, (uint8_t *)data, send_count);
199
200 if (ret == -ENOMEM) {
201 wait_timer++;
202
203 if (wait_timer >= 100) {
204 cb->end(-1, NULL);
205 return -1;
206 }
207
208 aos_msleep(1);
209
210 continue;
211 }
212
213 if (ret) {
214 if (cb != NULL && cb->end != NULL) {
215 cb->end(ret, NULL);
216 }
217
218 return ret;
219 }
220
221 data += send_count;
222 count -= send_count;
223 }
224
225 if (cb != NULL && cb->end != NULL) {
226 cb->end(0, NULL);
227 }
228
229 return 0;
230
231 }
232
233
uart_server_init(ble_uart_server_t * service)234 uart_handle_t uart_server_init(ble_uart_server_t *service)
235 {
236 int ret = 0;
237 int g_yoc_uart_handle = -1;
238
239 if (service == NULL) {
240 return NULL;
241 }
242
243 ret = ble_stack_event_register(&ble_cb);
244
245 if (ret) {
246 return NULL;
247 }
248
249 g_yoc_uart_handle = ble_stack_gatt_registe_service(&g_uart_profile, uart_attrs, BLE_ARRAY_NUM(uart_attrs));
250
251 if (g_yoc_uart_handle < 0) {
252 return NULL;
253 }
254
255 service->uart_svc_handle = g_yoc_uart_handle;
256 service->conn_handle = -1;
257
258 g_uart_dev = service;
259 return service;
260 }
261
262
uart_server_adv_control(uint8_t adv_on,adv_param_t * adv_param)263 int uart_server_adv_control(uint8_t adv_on, adv_param_t *adv_param)
264 {
265 int ret = 0;
266
267 ble_stack_adv_stop();
268
269 if (adv_on) {
270 if (adv_param == NULL) {
271 return -1;
272 } else {
273 ret = ble_stack_adv_start(adv_param);
274
275 if (ret) {
276 return ret;
277 }
278 }
279 }
280
281 return 0;
282 }
283
284
285
uart_server_conn_param_update(uart_handle_t handle,conn_param_t * param)286 int uart_server_conn_param_update(uart_handle_t handle, conn_param_t *param)
287 {
288 ble_uart_server_t *uart = (ble_uart_server_t *)handle;
289
290 if (!uart) {
291 return -1;
292 }
293
294 uart->conn_param->interval_min = param->interval_min;
295 uart->conn_param->interval_max = param->interval_max;
296 uart->conn_param->latency = param->latency;
297 uart->conn_param->timeout = param->timeout;
298
299 if (uart->conn_handle < 0) {
300 uart->update_param_flag = 1;
301
302 } else {
303 return ble_stack_connect_param_update(uart->conn_handle, param);
304 }
305
306 return 0;
307 }
308
309
310
311