1 /*
2  * Copyright (C) 2015-2019 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _MESH_HAL_BLE_H_
6 #define _MESH_HAL_BLE_H_
7 
8 #include <stdint.h>
9 
10 #include <bluetooth/bluetooth.h>
11 #include <bluetooth/conn.h>
12 #include <bluetooth/gatt.h>
13 #include <net/buf.h>
14 
15 #include <api/mesh.h>
16 
17 /** @typedef bt_mesh_le_scan_cb_t
18  *  @brief Callback type for reporting LE scan results.
19  *
20  *  A function of this type is given to the bt_mesh_le_scan_start() function
21  *  and will be called for any discovered LE device.
22  *
23  *  @param addr Advertiser LE address and type.
24  *  @param rssi Strength of advertiser signal.
25  *  @param adv_type Type of advertising response from advertiser.
26  *  @param data Buffer containing advertiser data.
27  */
28 typedef void bt_mesh_le_scan_cb_t(const bt_addr_le_t *addr, int8_t rssi,
29                                   uint8_t adv_type, struct net_buf_simple *buf);
30 
31 /** @brief Start advertising
32  *
33  *  Set advertisement data, scan response data, advertisement parameters
34  *  and start advertising.
35  *
36  *  @param param Advertising parameters.
37  *  @param ad Data to be used in advertisement packets.
38  *  @param ad_len Number of elements in ad
39  *  @param sd Data to be used in scan response packets.
40  *  @param sd_len Number of elements in sd
41  *
42  *  @return Zero on success or (negative) error code otherwise.
43  */
44 int bt_mesh_adv_start(const struct bt_le_adv_param *param,
45                       const struct bt_data *ad, size_t ad_len,
46                       const struct bt_data *sd, size_t sd_len);
47 
48 /** @brief Stop advertising
49  *
50  *  Stops ongoing advertising.
51  *
52  *  @return Zero on success or (negative) error code otherwise.
53  */
54 int bt_mesh_adv_stop(void);
55 
56 /** @brief Start (LE) scanning
57  *
58  *  Start LE scanning with given parameters and provide results through
59  *  the specified callback.
60  *
61  *  @param param Scan parameters.
62  *  @param cb Callback to notify scan results.
63  *
64  *  @return Zero on success or error code otherwise, positive in case
65  *  of protocol error or negative (POSIX) in case of stack internal error
66  */
67 int bt_mesh_scan_start(const struct bt_le_scan_param *param, bt_mesh_le_scan_cb_t cb);
68 
69 /** @brief Stop (LE) scanning.
70  *
71  *  Stops ongoing LE scanning.
72  *
73  *  @return Zero on success or error code otherwise, positive in case
74  *  of protocol error or negative (POSIX) in case of stack internal error
75  */
76 int bt_mesh_scan_stop(void);
77 
78 /** @brief Register connection callbacks.
79  *
80  *  Register callbacks to monitor the state of connections.
81  *
82  *  @param cb Callback struct.
83  */
84 void bt_mesh_conn_cb_register(struct bt_conn_cb *cb);
85 
86 /** @brief Increment a connection's reference count.
87  *
88  *  Increment the reference count of a connection object.
89  *
90  *  @param conn Connection object.
91  *
92  *  @return Connection object with incremented reference count.
93  */
94 struct bt_conn *bt_mesh_conn_ref(struct bt_conn *conn);
95 
96 /** @brief Decrement a connection's reference count.
97  *
98  *  Decrement the reference count of a connection object.
99  *
100  *  @param conn Connection object.
101  */
102 void bt_mesh_conn_unref(struct bt_conn *conn);
103 
104 /** @brief Disconnect from a remote device or cancel pending connection.
105  *
106  *  Disconnect an active connection with the specified reason code or cancel
107  *  pending outgoing connection.
108  *
109  *  @param conn Connection to disconnect.
110  *  @param reason Reason code for the disconnection.
111  *
112  *  @return Zero on success or (negative) error code on failure.
113  */
114 int bt_mesh_conn_disconnect(struct bt_conn *conn, uint8_t reason);
115 
116 /** @brief Register GATT service.
117  *
118  *  Register GATT service. Applications can make use of
119  *  macros such as BT_GATT_PRIMARY_SERVICE, BT_GATT_CHARACTERISTIC,
120  *  BT_GATT_DESCRIPTOR, etc.
121  *
122  *  @param svc Service containing the available attributes
123  *
124  *  @return 0 in case of success or negative value in case of error.
125  */
126 int bt_mesh_gatt_service_register(struct bt_gatt_service *svc);
127 
128 /** @brief Unregister GATT service.
129  *
130  *  @param svc Service to be unregistered.
131  *
132  *  @return 0 in case of success or negative value in case of error.
133  */
134 int bt_mesh_gatt_service_unregister(struct bt_gatt_service *svc);
135 
136 /** @brief Notify attribute value change.
137  *
138  *  Send notification of attribute value change, if connection is NULL notify
139  *  all peer that have notification enabled via CCC otherwise do a direct
140  *  notification only the given connection.
141  *
142  *  The attribute object must be the so called Characteristic Value Descriptor,
143  *  its usually declared with BT_GATT_DESCRIPTOR after BT_GATT_CHARACTERISTIC
144  *  and before BT_GATT_CCC.
145  *
146  *  @param conn Connection object.
147  *  @param attr Characteristic Value Descriptor attribute.
148  *  @param data Pointer to Attribute data.
149  *  @param len Attribute value length.
150  */
151 int bt_mesh_gatt_notify(struct bt_conn *conn, const struct bt_gatt_attr *attr,
152                         const void *data, uint16_t len);
153 
154 /** @brief Generic Read Attribute value helper.
155  *
156  *  Read attribute value storing the result into buffer.
157  *
158  *  @param conn Connection object.
159  *  @param attr Attribute to read.
160  *  @param buf Buffer to store the value.
161  *  @param buf_len Buffer length.
162  *  @param offset Start offset.
163  *  @param value Attribute value.
164  *  @param value_len Length of the attribute value.
165  *
166  *  @return int number of bytes read in case of success or negative values in
167  *  case of error.
168  */
169 int bt_mesh_gatt_attr_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
170                                void *buf, uint16_t buf_len, uint16_t offset,
171                                const void *value, uint16_t value_len);
172 
173 /** @brief Get ATT MTU for a connection
174  *
175  *  Get negotiated ATT connection MTU, note that this does not equal the largest
176  *  amount of attribute data that can be transferred within a single packet.
177  *
178  *  @param conn Connection object.
179  *
180  *  @return MTU in bytes
181  */
182 uint16_t bt_mesh_gatt_get_mtu(struct bt_conn *conn);
183 
184 /** @brief Read Service Attribute helper.
185  *
186  *  Read service attribute value (Spec V4.2, Vol 3, part G,
187  *  table 3.1) storing the result into buffer after
188  *  encoding it.
189  *  NOTE: Only use this with attributes which user_data is a bt_mesh_uuid.
190  *
191  *  @param conn Connection object.
192  *  @param attr Attribute to read.
193  *  @param buf Buffer to store the value read.
194  *  @param len Buffer length.
195  *  @param offset Start offset.
196  *
197  *  @return int number of bytes read in case of success or negative values in
198  *  case of error.
199  */
200 int bt_mesh_gatt_attr_read_service(struct bt_conn *conn,
201                                    const struct bt_gatt_attr *attr,
202                                    void *buf, uint16_t len, uint16_t offset);
203 
204 /** @brief Read Characteristic Attribute helper.
205  *
206  *  Read characteristic attribute value (Spec V4.2, Vol 3, part G,
207  *  table 3.3) storing the result into buffer after
208  *  encoding it.
209  *  NOTE: Only use this with attributes which user_data is a bt_mesh_gatt_chrc.
210  *
211  *  @param conn Connection object.
212  *  @param attr Attribute to read.
213  *  @param buf Buffer to store the value read.
214  *  @param len Buffer length.
215  *  @param offset Start offset.
216  *
217  *  @return number of bytes read in case of success or negative values in
218  *  case of error.
219  */
220 int bt_mesh_gatt_attr_read_chrc(struct bt_conn *conn,
221                                const struct bt_gatt_attr *attr, void *buf,
222                                uint16_t len, uint16_t offset);
223 
224 #ifdef CONFIG_BT_MESH_PROVISIONER
225 typedef ssize_t (*gatt_recv_cb)(struct bt_conn *conn,
226                             const struct bt_gatt_attr *attr, const void *buf,
227                             u16_t len, u16_t offset, u8_t flags);
228 bool bt_prov_check_gattc_id(int id, const bt_addr_le_t *addr);
229 int bt_gattc_conn_create(int id, u16_t srvc_uuid);
230 u16_t bt_mesh_get_srvc_uuid(struct bt_conn *conn);
231 void bt_mesh_gatt_conn_open(struct bt_conn *conn, void (*open_complete)(struct bt_conn *conn));
232 u16_t bt_gatt_get_data_in_handle(struct bt_conn *conn);
233 u16_t bt_gatt_prov_get_mtu(struct bt_conn *conn);
234 struct bt_conn * bt_mesh_get_curr_conn(void);
235 void set_my_addr(u8_t index, const u8_t *addr, u8_t type);
236 void bt_mesh_gatt_recv_callback(gatt_recv_cb recv_callback);
237 int bt_prov_get_gattc_id(u8_t *addr);
238 
239 #endif
240 
241 #endif /* _MESH_HAL_BLE_H_ */
242