1 /*
2  * This file is part of the MicroPython project, http://micropython.org/
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 Glenn Ruben Bakke
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this software and associated documentation files (the "Software"), to deal
10  * in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12  * copies of the Software, and to permit persons to whom the Software is
13  * furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24  * THE SOFTWARE.
25  */
26 
27 #ifndef BLUETOOTH_LE_DRIVER_H__
28 #define BLUETOOTH_LE_DRIVER_H__
29 
30 #if BLUETOOTH_SD
31 
32 #include <stdint.h>
33 #include <stdbool.h>
34 
35 #include "modubluepy.h"
36 
37 typedef struct {
38     uint8_t   addr[6];
39     uint8_t   addr_type;
40 } ble_drv_addr_t;
41 
42 typedef struct {
43     uint8_t * p_peer_addr;
44     uint8_t   addr_type;
45     bool      is_scan_resp;
46     int8_t    rssi;
47     uint8_t   data_len;
48     uint8_t * p_data;
49     uint8_t   adv_type;
50 } ble_drv_adv_data_t;
51 
52 typedef struct {
53     uint16_t uuid;
54     uint8_t  uuid_type;
55     uint16_t start_handle;
56     uint16_t end_handle;
57 } ble_drv_service_data_t;
58 
59 typedef struct {
60     uint16_t uuid;
61     uint8_t  uuid_type;
62     uint8_t  props;
63     uint16_t decl_handle;
64     uint16_t value_handle;
65 } ble_drv_char_data_t;
66 
67 typedef void (*ble_drv_gap_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data);
68 typedef void (*ble_drv_gatts_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data);
69 typedef void (*ble_drv_gattc_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t attr_handle, uint16_t length, uint8_t * data);
70 typedef void (*ble_drv_adv_evt_callback_t)(mp_obj_t self, uint16_t event_id, ble_drv_adv_data_t * data);
71 typedef void (*ble_drv_disc_add_service_callback_t)(mp_obj_t self, ble_drv_service_data_t * p_service_data);
72 typedef void (*ble_drv_disc_add_char_callback_t)(mp_obj_t self, ble_drv_char_data_t * p_desc_data);
73 typedef void (*ble_drv_gattc_char_data_callback_t)(mp_obj_t self, uint16_t length, uint8_t * p_data);
74 
75 uint32_t ble_drv_stack_enable(void);
76 
77 void ble_drv_stack_disable(void);
78 
79 uint8_t ble_drv_stack_enabled(void);
80 
81 void ble_drv_address_get(ble_drv_addr_t * p_addr);
82 
83 bool ble_drv_uuid_add_vs(uint8_t * p_uuid, uint8_t * idx);
84 
85 bool ble_drv_service_add(ubluepy_service_obj_t * p_service_obj);
86 
87 bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj);
88 
89 bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params);
90 
91 void ble_drv_advertise_stop(void);
92 
93 void ble_drv_gap_event_handler_set(mp_obj_t obs, ble_drv_gap_evt_callback_t evt_handler);
94 
95 void ble_drv_gatts_event_handler_set(mp_obj_t obj, ble_drv_gatts_evt_callback_t evt_handler);
96 
97 void ble_drv_gattc_event_handler_set(mp_obj_t obj, ble_drv_gattc_evt_callback_t evt_handler);
98 
99 void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
100 
101 void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, ble_drv_gattc_char_data_callback_t cb);
102 
103 void ble_drv_attr_s_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
104 
105 void ble_drv_attr_s_notify(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);
106 
107 void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data, bool w_response);
108 
109 void ble_drv_scan_start(bool cont);
110 
111 void ble_drv_scan_stop(void);
112 
113 void ble_drv_adv_report_handler_set(mp_obj_t obj, ble_drv_adv_evt_callback_t evt_handler);
114 
115 void ble_drv_connect(uint8_t * p_addr, uint8_t addr_type);
116 
117 bool ble_drv_discover_services(mp_obj_t obj, uint16_t conn_handle, uint16_t start_handle, ble_drv_disc_add_service_callback_t cb);
118 
119 bool ble_drv_discover_characteristic(mp_obj_t obj,
120                                      uint16_t conn_handle,
121                                      uint16_t start_handle,
122                                      uint16_t end_handle,
123                                      ble_drv_disc_add_char_callback_t cb);
124 
125 void ble_drv_discover_descriptors(void);
126 
127 #endif // BLUETOOTH_SD
128 
129 #endif // BLUETOOTH_LE_DRIVER_H__
130