Mbed OS Reference
Loading...
Searching...
No Matches
ESP32.h
1/* ESP32Interface Example
2 * Copyright (c) 2015 ARM Limited
3 * Copyright (c) 2017 Renesas Electronics Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef ESP32_H
19#define ESP32_H
20
21#if DEVICE_SERIAL && defined(MBED_CONF_EVENTS_PRESENT) && defined(MBED_CONF_NSAPI_PRESENT) && defined(MBED_CONF_RTOS_PRESENT)
22
23#include "drivers/DigitalOut.h"
24#include "netsocket/nsapi_types.h"
25#include "netsocket/WiFiAccessPoint.h"
26#include "platform/ATCmdParser.h"
27#include "rtos/ThisThread.h"
28#include "drivers/BufferedSerial.h"
29
30#ifndef ESP32_CONNECT_TIMEOUT
31#define ESP32_CONNECT_TIMEOUT 15000
32#endif
33#ifndef ESP32_SEND_TIMEOUT
34#define ESP32_SEND_TIMEOUT 2000
35#endif
36#ifndef ESP32_RECV_TIMEOUT
37#define ESP32_RECV_TIMEOUT 2000
38#endif
39#ifndef ESP32_MISC_TIMEOUT
40#define ESP32_MISC_TIMEOUT 2000
41#endif
42
43/** ESP32Interface class.
44 This is an interface to a ESP32 radio.
45 */
46class ESP32 {
47public:
48 /**
49 * Static method to create or retrieve the single ESP32 instance
50 */
51 static ESP32 *getESP32Inst(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
52 PinName rts, PinName cts, int baudrate);
53 static ESP32 *getESP32Inst(bool debug = false);
54
55 ESP32(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
56 PinName rts, PinName cts, int baudrate);
57
58 /**
59 * Checks Version Information
60 *
61 * @param ver_info buffer address for storing version information
62 * @param buf_size buffer size
63 * @return String of Version Information
64 */
65 bool get_version_info(char *ver_info, int buf_size);
66
67 /**
68 * Sets the Wi-Fi Mode
69 *
70 * @param mode mode of WIFI 1-client, 2-host, 3-both
71 * @return true only if ESP32 was setup correctly
72 */
73 bool set_mode(int mode);
74
75 /**
76 * Enable/Disable DHCP
77 *
78 * @param enabled DHCP enabled when true
79 * @param mode mode of DHCP 0-softAP, 1-station, 2-both
80 * @return true only if ESP32 enables/disables DHCP successfully
81 */
82 bool dhcp(bool enabled, int mode);
83
84 /**
85 * Connect ESP32 to AP
86 *
87 * @param ap the name of the AP
88 * @param passPhrase the password of AP
89 * @return true only if ESP32 is connected successfully
90 */
91 bool connect(const char *ap, const char *passPhrase);
92
93 /**
94 * Disconnect ESP32 from AP
95 *
96 * @return true only if ESP32 is disconnected successfully
97 */
98 bool disconnect(void);
99
100 /**
101 * Get the IP address of ESP32
102 *
103 * @return null-teriminated IP address or null if no IP address is assigned
104 */
105 const char *getIPAddress(void);
106 const char *getIPAddress_ap(void);
107
108 /**
109 * Get the MAC address of ESP32
110 *
111 * @return null-terminated MAC address or null if no MAC address is assigned
112 */
113 const char *getMACAddress(void);
114 const char *getMACAddress_ap(void);
115
116 /** Get the local gateway
117 *
118 * @return Null-terminated representation of the local gateway
119 * or null if no network mask has been recieved
120 */
121 const char *getGateway();
122 const char *getGateway_ap();
123
124 /** Get the local network mask
125 *
126 * @return Null-terminated representation of the local network mask
127 * or null if no network mask has been recieved
128 */
129 const char *getNetmask();
130 const char *getNetmask_ap();
131
132 /* Return RSSI for active connection
133 *
134 * @return Measured RSSI
135 */
136 int8_t getRSSI();
137
138 /**
139 * Check if ESP32 is conenected
140 *
141 * @return true only if the chip has an IP address
142 */
143 bool isConnected(void);
144
145 /** Scan for available networks
146 *
147 * @param ap Pointer to allocated array to store discovered AP
148 * @param limit Size of allocated @a res array, or 0 to only count available AP
149 * @return Number of entries in @a res, or if @a count was 0 number of available networks, negative on error
150 * see @a nsapi_error
151 */
152 int scan(WiFiAccessPoint *res, unsigned limit);
153
154 /**
155 * Open a socketed connection
156 *
157 * @param type the type of socket to open "UDP" or "TCP"
158 * @param id id to give the new socket, valid 0-4
159 * @param port port to open connection with
160 * @param addr the IP address of the destination
161 * @param addr the IP address of the destination
162 * @param opt type=" UDP" : UDP socket's local port, zero means any
163 * type=" TCP" : TCP connection's keep alive time, zero means disabled
164 * @return true only if socket opened successfully
165 */
166 bool open(const char *type, int id, const char *addr, int port, int opt = 0);
167
168 /**
169 * Sends data to an open socket
170 *
171 * @param id id of socket to send to
172 * @param data data to be sent
173 * @param amount amount of data to be sent - max 1024
174 * @return true only if data sent successfully
175 */
176 bool send(int id, const void *data, uint32_t amount);
177
178 /**
179 * Receives data from an open socket
180 *
181 * @param id id to receive from
182 * @param data placeholder for returned information
183 * @param amount number of bytes to be received
184 * @return the number of bytes received
185 */
186 int32_t recv(int id, void *data, uint32_t amount, uint32_t timeout = ESP32_RECV_TIMEOUT);
187
188 /**
189 * Closes a socket
190 *
191 * @param id id of socket to close, valid only 0-4
192 * @param wait_close
193 * @return true only if socket is closed successfully
194 */
195 bool close(int id, bool wait_close = false);
196
197 /**
198 * Allows timeout to be changed between commands
199 *
200 * @param timeout_ms timeout of the connection
201 */
202 void setTimeout(uint32_t timeout_ms = ESP32_MISC_TIMEOUT);
203
204 /**
205 * Checks if data is available
206 */
207 bool readable();
208
209 /**
210 * Checks if data can be written
211 */
212 bool writeable();
213
214 void socket_attach(int id, void (*callback)(void *), void *data);
215 int get_free_id();
216
217 bool config_soft_ap(const char *ap, const char *passPhrase, uint8_t chl, uint8_t ecn);
218
219 bool restart();
220 bool get_ssid(char *ap);
221 bool cre_server(int port);
222 bool del_server();
223 bool accept(int *p_id);
224
225 bool set_network(const char *ip_address, const char *netmask, const char *gateway);
226 bool set_network_ap(const char *ip_address, const char *netmask, const char *gateway);
227
228 /**
229 * Attach a function to call whenever network state has changed
230 *
231 * @param func A pointer to a void function, or 0 to set as none
232 */
233 void attach_wifi_status(mbed::Callback<void(int8_t)> status_cb);
234
235 /** Get the connection status
236 *
237 * @return The connection status according to ConnectionStatusType
238 */
239 int8_t get_wifi_status() const;
240
241 /**
242 * Flush the serial port input buffers.
243 *
244 * If you do HW reset for ESP module, you should
245 * flush the input buffers from existing responses
246 * from the device.
247 */
248 void flush();
249
250 static const int8_t WIFIMODE_STATION = 1;
251 static const int8_t WIFIMODE_SOFTAP = 2;
252 static const int8_t WIFIMODE_STATION_SOFTAP = 3;
253 static const int8_t SOCKET_COUNT = 5;
254
255 static const int8_t STATUS_DISCONNECTED = 0;
256 static const int8_t STATUS_CONNECTED = 1;
257 static const int8_t STATUS_GOT_IP = 2;
258
259private:
260 mbed::DigitalOut *_p_wifi_en;
261 mbed::DigitalOut *_p_wifi_io0;
262 bool _init_end_common;
263 bool _init_end_wifi;
264 mbed::BufferedSerial _serial;
265 mbed::ATCmdParser _parser;
266 struct packet {
267 struct packet *next;
268 int id;
269 uint32_t len;
270 uint32_t index;
271 // data follows
272 } *_packets, * *_packets_end;
273 int _wifi_mode;
274 int _baudrate;
275 PinName _rts;
276 PinName _cts;
277 int _flow_control;
278 uint32_t last_timeout_ms;
279
280 std::vector<int> _accept_id;
281 uint32_t _id_bits;
282 bool _server_act;
283 rtos::Mutex _smutex; // Protect serial port access
284 static ESP32 *instESP32;
285 int8_t _wifi_status;
286 mbed::Callback<void(int8_t)> _wifi_status_cb;
287 uint32_t _at_version;
288
289 bool _ids[SOCKET_COUNT];
290 struct {
291 void (*callback)(void *);
292 void *data;
293 int Notified;
294 } _cbs[SOCKET_COUNT];
295
296 void _startup_common();
297 bool _startup_wifi();
298 bool reset(void);
299 void debugOn(bool debug);
300 void socket_handler(bool connect, int id);
301 void _connect_handler_0();
302 void _connect_handler_1();
303 void _connect_handler_2();
304 void _connect_handler_3();
305 void _connect_handler_4();
306 void _closed_handler_0();
307 void _closed_handler_1();
308 void _closed_handler_2();
309 void _closed_handler_3();
310 void _closed_handler_4();
311 void _connection_status_handler();
312 void _packet_handler();
313 void _clear_socket_packets(int id);
314 void event();
315 bool recv_ap(nsapi_wifi_ap_t *ap);
316
317 char _ip_buffer[16];
318 char _gateway_buffer[16];
319 char _netmask_buffer[16];
320 char _mac_buffer[18];
321
322 char _ip_buffer_ap[16];
323 char _gateway_buffer_ap[16];
324 char _netmask_buffer_ap[16];
325 char _mac_buffer_ap[18];
326
327#if defined(TARGET_ESP32AT_BLE)
328public:
329 typedef struct {
330 int srv_index; /**< service's index starting from 1 */
331 int char_index; /**< characteristic's index starting from 1 */
332 int desc_index; /**< descriptor's index */
333 void *data; /**< data buffer address */
334 uint32_t len; /**< data len */
335 } ble_packet_t;
336
337 typedef union {
338 const uint8_t *addr; /**< buffer address */
339 uint32_t data; /**< data */
340 } union_data_t;
341
342 typedef struct {
343 union_data_t uuid; /**< UUID value */
344 uint8_t uuid_type; /**< UUID type 0: addr 1:data */
345 uint16_t uuid_size; /**< UUID size */
346 uint16_t val_max_len; /**< max allow value length (the max length when you dynamic set value) */
347 union_data_t value; /**< initial value */
348 uint8_t value_type; /**< initial value type 0: addr 1:data */
349 uint16_t value_size; /**< initial value size */
350 uint8_t permissions; /**< permission (refer to BLE Spec for definition) */
351 } gatt_service_t;
352
353 typedef struct {
354 uint16_t adv_int_min; /**< minimum value of advertising interval; range: 0x0020 ~ 0x4000 */
355 uint16_t adv_int_max; /**< maximum value of advertising interval; range: 0x0020 ~ 0x4000 */
356 uint8_t adv_type; /**< 0:FADV_TYPE_IND, 2:FADV_TYPE_SCAN_IND, 3:FADV_TYPE_NONCONN_IND */
357 uint8_t own_addr_type; /**< own BLE address type; 0:FBLE_ADDR_TYPE_PUBLIC, 1:FBLE_ADDR_TYPE_RANDOM */
358 uint8_t channel_map; /**< channel of advertising; ADV_CHNL_~ */
359 uint8_t adv_filter_policy; /**< filter policy of advertising; ADV_FILTER_ALLOW_SCAN_~ */
360 uint8_t peer_addr_type; /**< remote BLE address type; 0:FPUBLIC, 1:FRANDOM */
361 uint8_t peer_addr[6]; /**< remote BLE address */
362 } advertising_param_t;
363
364 typedef struct {
365 uint8_t addr[6]; /**< BLE address */
366 int8_t rssi; /**< signal strength */
367 uint8_t adv_data[31]; /**< advertising data */
368 uint8_t adv_data_len; /**< advertising data length */
369 uint8_t scan_rsp_data[31]; /**< scan response data */
370 uint8_t scan_rsp_data_len; /**< scan response data length */
371 uint8_t addr_type; /**< the address type of broadcasters */
372 } ble_scan_t;
373
374 typedef struct {
375 int srv_index; /**< service's index starting from 1 */
376 uint16_t srv_uuid; /**< service's UUID */
377 int srv_type; /**< service's type */
378 } ble_primary_service_t;
379
380 typedef struct {
381 int srv_index; /**< service's index starting from 1 */
382 uint16_t srv_uuid; /**< service's UUID */
383 int srv_type; /**< service's type */
384 } ble_characteristics_t;
385
386 typedef struct {
387 int char_index; /**< Characteristic's index starting from 1 */
388 uint16_t char_uuid; /**< Characteristic's UUID */
389 uint8_t char_prop; /**< Characteristic's properties */
390 } ble_discovers_char_t;
391
392 typedef struct {
393 int char_index; /**< Characteristic's index starting from 1 */
394 int desc_index; /**< Descriptor's index */
395 uint16_t desc_uuid; /**< Descriptor's UUID */
396 } ble_discovers_desc_t;
397
398 // advertising_param_t:adv_type
399#define ADV_TYPE_IND 0
400#define ADV_TYPE_SCAN_IND 2
401#define ADV_TYPE_NONCONN_IND 3
402
403 // advertising_param_t:own_addr_type and peer_addr_type
404#define BLE_ADDR_TYPE_PUBLIC 0
405#define BLE_ADDR_TYPE_RANDOM 1
406
407 // advertising_param_t:channel_map
408#define ADV_CHNL_37 0x01
409#define ADV_CHNL_38 0x02
410#define ADV_CHNL_39 0x04
411#define ADV_CHNL_ALL 0x07
412
413 // advertising_param_t:adv_filter_policy
414#define ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY 0
415#define ADV_FILTER_ALLOW_SCAN_WLST_CON_ANY 1
416#define ADV_FILTER_ALLOW_SCAN_ANY_CON_WLST 2
417#define ADV_FILTER_ALLOW_SCAN_WLST_CON_WLST 3
418
419 // ble_set_role: role
420#define INIT_CLIENT_ROLE 1
421#define INIT_SERVER_ROLE 2
422
423 /** Sets BLE Role
424 *
425 * @param role INIT_CLIENT_ROLE: client role, INIT_SERVER_ROLE: server role
426 * @return true: success, false: failure
427 */
428 bool ble_set_role(int role);
429
430 /** Gets BLE Role
431 *
432 * @param role INIT_CLIENT_ROLE: client role, INIT_SERVER_ROLE: server role
433 * @return true: success, false: failure
434 */
435 bool ble_get_role(int *role);
436
437 /** Sets BLE Device's Name
438 *
439 * @param name The BLE device name
440 * @return true: success, false: failure
441 */
442 bool ble_set_device_name(const char *name);
443
444 /** Gets BLE Device's Name
445 *
446 * @param name The BLE device name
447 * @return true: success, false: failure
448 */
449 bool ble_get_device_name(char *name);
450
451 /** GATTS Creates and Starts Services
452 *
453 * @return true: success, false: failure
454 */
455 bool ble_start_services();
456
457 /** Sets BLE Scan Response
458 *
459 * @param data Scan response data
460 * @param len Data len
461 * @return true: success, false: failure
462 */
463 bool ble_set_scan_response(const uint8_t *data, int len);
464
465 /** Starts Advertising
466 *
467 * @return true: success, false: failure
468 */
469 bool ble_start_advertising();
470
471 /** Stops Advertising
472 *
473 * @return true: success, false: failure
474 */
475 bool ble_stop_advertising();
476
477 /** Sets BLE Device's Address
478 *
479 * @param addr_type 0: public address, 1: random address
480 * @param addr Random address data. Valid only when addr_type is 1.
481 * @return true: success, false: failure
482 */
483 bool ble_set_addr(int addr_type, const uint8_t *random_addr = NULL);
484
485 /** Gets BLE Device's Address
486 *
487 * @param public_addr BLE public address
488 * @return true: success, false: failure
489 */
490 bool ble_get_addr(uint8_t *public_addr);
491
492 /** Sets Parameters of Advertising
493 *
494 * @param param Parameters. See advertising_param_t.
495 * @return true: success, false: failure
496 */
497 bool ble_set_advertising_param(const advertising_param_t *param);
498
499 /** Sets Advertising Data
500 *
501 * @param data Advertising data
502 * @param len Data len
503 * @return true: success, false: failure
504 */
505 bool ble_set_advertising_data(const uint8_t *data, int len);
506
507 /** GATT Sets Service
508 *
509 * @param service_list GATT service list. see gatt_service_t.
510 * @param num Number of GATT service list
511 * @return true: success, false: failure
512 */
513 bool ble_set_service(const gatt_service_t *service_list, int num);
514
515 /** GATTS Sets Characteristic
516 *
517 * @param srv_index Service's index starting from 1
518 * @param char_index Characteristic's index starting from 1
519 * @param data Data buffer address
520 * @param len Data len
521 * @return true: success, false: failure
522 */
523 bool ble_set_characteristic(int srv_index, int char_index, const uint8_t *data, int len);
524
525 /** GATTS Notifies of Characteristics
526 *
527 * @param srv_index Service's index starting from 1
528 * @param char_index Characteristic's index starting from 1
529 * @param data Data buffer address
530 * @param len Data len
531 * @return true: success, false: failure
532 */
533 bool ble_notifies_characteristic(int srv_index, int char_index, const uint8_t *data, int len);
534
535 /** Sets Parameters of BLE Scanning
536 *
537 * @param scan_type 0: passive scan 1: active scan
538 * @param own_addr_type 0: public address 1: random address 2: RPA public address 3: RPA random address
539 * @param filter_policy 0: BLE_SCAN_FILTER_ALLOW_ALL
540 * 1: BLE_SCAN_FILTER_ALLOW_ONLY_WLST
541 * 2: BLE_SCAN_FILTER_ALLOW_UND_RPA_DIR
542 * 3: BLE_SCAN_FILTER_ALLOW_WLIST_PRA_DIR
543 * @param scan_interval scan interval
544 * @param scan_window scan window
545 * @return rue: success, false: failure
546 */
547 bool ble_set_scan_param(int scan_type, int own_addr_type, int filter_policy, int scan_interval, int scan_window);
548
549 /** Enables BLE Scanning
550 *
551 * @param interval 0: scanning is continuous
552 * !0: scanning should last for <interval> seconds and then stop automatically
553 * @return true: success, false: failure
554 */
555 bool ble_start_scan(int interval = 0);
556
557 /** Disables BLE scan
558 *
559 * @return true: success, false: failure
560 */
561 bool ble_stop_scan();
562
563 /** Establishes BLE connection
564 *
565 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
566 * but multiple BLE connections will be supported in the future.
567 * @param remote_addr Remote BLE address
568 * @return true: success, false: failure
569 */
570 bool ble_connect(int conn_index, const uint8_t *remote_addr);
571
572 /** Ends BLE connection
573 *
574 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
575 * but multiple BLE connections will be supported in the future.
576 * @return true: success, false: failure
577 */
578 bool ble_disconnect(int conn_index);
579
580 /** GATTC Discovers Primary Services
581 *
582 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
583 * but multiple BLE connections will be supported in the future.
584 * @param service Service info
585 * @param num Number of service info
586 * @return true: success, false: failure
587 */
588 bool ble_discovery_service(int conn_index, ble_primary_service_t *service, int *num);
589
590 /** GATTC Discovers Characteristics
591 *
592 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
593 * but multiple BLE connections will be supported in the future.
594 * @param srv_index Service's index. It can be fetched with "ble_discovery_service()"
595 * @param discovers_char Characteristic info
596 * @param char_num Number of characteristic info
597 * @param discovers_desc Descriptor info
598 * @param desc_num Number of descriptor info
599 * @return true: success, false: failure
600 */
601 bool ble_discovery_characteristics(
602 int conn_index, int srv_index,
603 ble_discovers_char_t *discovers_char, int *char_num,
604 ble_discovers_desc_t *discovers_desc, int *desc_num
605 );
606
607 /** GATTC Reads a Characteristic
608 *
609 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
610 * but multiple BLE connections will be supported in the future.
611 * @param srv_index Service's index. It can be fetched with "ble_discovery_service()"
612 * @param char_index Characteristic's index. It can be fetched with "ble_discovery_characteristics()"
613 * @param data Read data buffer
614 * @param amount Amount of bytes to be received
615 * @return Data size of received
616 */
617 int32_t ble_read_characteristic(int conn_index, int srv_index, int char_index, uint8_t *data, int amount);
618
619 /** GATTC Reads a Descriptor
620 *
621 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
622 * but multiple BLE connections will be supported in the future.
623 * @param srv_index Service's index. It can be fetched with "ble_discovery_service()"
624 * @param char_index Characteristic's index. It can be fetched with "ble_discovery_characteristics()"
625 * @param desc_index Descriptor's index. It can be fetched with "ble_discovery_characteristics()"
626 * @param data Read data buffer
627 * @param amount Amount of bytes to be received
628 * @return true: success, false: failure
629 */
630 int32_t ble_read_descriptor(int conn_index, int srv_index, int char_index, int desc_index, uint8_t *data, int amount);
631
632 /** GATTC Writes Characteristic
633 *
634 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
635 * but multiple BLE connections will be supported in the future.
636 * @param srv_index Service's index. It can be fetched with "ble_discovery_service()"
637 * @param char_index Characteristic's index. It can be fetched with "ble_discovery_characteristics()"
638 * @param data Write data buffer
639 * @param amount Amount of data to be written
640 * @return true: success, false: failure
641 */
642 bool ble_write_characteristic(int conn_index, int srv_index, int char_index, const uint8_t *data, int amount);
643
644 /** GATTC Writes Descriptor
645 *
646 * @param conn_index Index of BLE connection; only 0 is supported for the single connection right now,
647 * but multiple BLE connections will be supported in the future.
648 * @param srv_index Service's index. It can be fetched with "ble_discovery_service()"
649 * @param char_index Characteristic's index. It can be fetched with "ble_discovery_characteristics()"
650 * @param desc_index Descriptor's index. It can be fetched with "ble_discovery_characteristics()"
651 * @param data Write data buffer
652 * @param amount Amount of data to be written
653 * @return true: success, false: failure
654 */
655 bool ble_write_descriptor(int conn_index, int srv_index, int char_index, int desc_index, const uint8_t *data, int amount);
656
657 /** For executing OOB processing on background
658 *
659 * @param timeout AT parser receive timeout
660 * @param all if TRUE, process all OOBs instead of only one
661 */
662 void ble_process_oob(uint32_t timeout, bool all);
663
664 /** Register a callback on state change.
665 *
666 * The specified callback will be called on state changes.
667 *
668 * The callback may be called in an interrupt context and should not
669 * perform expensive operations.
670 *
671 * Note! This is not intended as an attach-like asynchronous api, but rather
672 * as a building block for constructing such functionality.
673 *
674 * The exact timing of when the registered function
675 * is called is not guaranteed and susceptible to change. It should be used
676 * as a cue to make ble_process_oobl calls to find the current state.
677 *
678 * @param cb_func function to call on state change
679 */
680 void ble_attach_sigio(mbed::Callback<void()> cb_func);
681
682 /**
683 * Attach a function to call whenever the BLE connection establishes
684 *
685 * @param cb_func Pointer to the function to be calledt
686 * cb_func argument 0: disconnect, 1: connect
687 */
688 void ble_attach_conn(mbed::Callback<void(int, uint8_t *)> cb_func);
689
690 /**
691 * Attach a function to call whenever the BLE connection ends
692 *
693 * @param cb_func Pointer to the function to be calledt
694 * cb_func argument 0: disconnect, 1: connect
695 */
696 void ble_attach_disconn(mbed::Callback<void(int)> cb_func);
697
698 /**
699 * Attach a function to call whenever characteristic data is written
700 *
701 * @param cb_func Pointer to the function to be called
702 */
703 void ble_attach_write(mbed::Callback<void(ble_packet_t *)> cb_func);
704
705 /**
706 * Attach a function to call whenever scan data is received
707 *
708 * @param cb_func Pointer to the function to be called
709 */
710 void ble_attach_scan(mbed::Callback<void(ble_scan_t *)> cb_func);
711
712private:
713#define PRIMARY_SERVICE_BUF_NUM 16
714#define DISCOVERS_CHAR_BUF_NUM 16
715#define DISCOVERS_DESC_BUF_NUM 16
716
717 struct {
718 mbed::Callback<void()> callback;
719 int Notified;
720 } _cbs_ble;
721
722 mbed::Callback<void(int, uint8_t *)> _ble_conn_cb;
723 mbed::Callback<void(int)> _ble_disconn_cb;
724 mbed::Callback<void(ble_packet_t *)> _ble_write_cb;
725 mbed::Callback<void(ble_scan_t *)> _ble_scan_cb;
726 int _ble_role;
727 bool _init_end_ble;
728 int _primary_service_idx;
729 int _discovers_char_idx;
730 int _discovers_desc_idx;
731 ble_primary_service_t _primary_service[PRIMARY_SERVICE_BUF_NUM];
732 ble_discovers_char_t _discovers_char[DISCOVERS_CHAR_BUF_NUM];
733 ble_discovers_desc_t _discovers_desc[DISCOVERS_DESC_BUF_NUM];
734
735 bool _startup_ble();
736 void _ble_conn();
737 void _ble_disconn();
738 void _ble_write();
739 void _ble_scan();
740 void _ble_primsrv();
741 void _ble_discovers_char();
742 char _int2char(int data);
743 int _char2int(char c);
744 int _set_char(char *buf1, const uint8_t *buf2, int size);
745#endif /* TARGET_ESP32AT_BLE */
746
747};
748#endif
749#endif /* ESP32_H */
WiFiAccessPoint class.
Parser class for parsing AT commands.
Definition ATCmdParser.h:56
Class providing buffered UART communication functionality using separate circular buffer for send and...
Callback class based on template specialization.
Definition Callback.h:53
A digital output, used for setting the state of a pin.
Definition DigitalOut.h:55
The Mutex class is used to synchronize the execution of threads.
Definition Mutex.h:70
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=nullptr) noexcept
Create a callback class with type inferred from the arguments.
Definition Callback.h:678
nsapi_wifi_ap structure