1 /**
2  ****************************************************************************************
3  *
4  * @file app.h
5  *
6  * @brief Application entry point
7  *
8  * Copyright (C) RivieraWaves 2009-2015
9  *
10  *
11  ****************************************************************************************
12  */
13 
14 #ifndef APP_H_
15 #define APP_H_
16 
17 /**
18  ****************************************************************************************
19  * @addtogroup APP
20  * @ingroup RICOW
21  *
22  * @brief Application entry point.
23  *
24  * @{
25  ****************************************************************************************
26  */
27 
28 /*
29  * INCLUDE FILES
30  ****************************************************************************************
31  */
32 
33 #include "rwip_config.h"     // SW configuration
34 
35 #if (BLE_APP_PRESENT)
36 
37 #include <stdint.h>          // Standard Integer Definition
38 #include <co_bt.h>           // Common BT Definitions
39 #include "ble_arch.h"
40 #include "gapc.h"            // GAPC Definitions
41 #include "gap.h"
42 
43 #if (NVDS_SUPPORT)
44 #include "nvds.h"
45 #endif // (NVDS_SUPPORT)
46 
47 /*
48  * DEFINES
49  ****************************************************************************************
50  */
51 /// Maximal length of the Device Name value
52 #define APP_DEVICE_NAME_MAX_LEN      (16)
53 
54 /*
55  * TYPE DEFINITIONS
56  ****************************************************************************************
57  */
58 
59 #if (NVDS_SUPPORT)
60 #ifdef default
61 /// List of Application NVDS TAG identifiers
62 enum app_nvds_tag
63 {
64     /// BLE Application Advertising data
65     NVDS_TAG_APP_BLE_ADV_DATA           = 0x0B,
66     NVDS_LEN_APP_BLE_ADV_DATA           = 32,
67 
68     /// BLE Application Scan response data
69     NVDS_TAG_APP_BLE_SCAN_RESP_DATA     = 0x0C,
70     NVDS_LEN_APP_BLE_SCAN_RESP_DATA     = 32,
71 
72     /// Mouse Sample Rate
73     NVDS_TAG_MOUSE_SAMPLE_RATE          = 0x38,
74     NVDS_LEN_MOUSE_SAMPLE_RATE          = 1,
75     /// Peripheral Bonded
76     NVDS_TAG_PERIPH_BONDED              = 0x39,
77     NVDS_LEN_PERIPH_BONDED              = 1,
78     /// Mouse NTF Cfg
79     NVDS_TAG_MOUSE_NTF_CFG              = 0x3A,
80     NVDS_LEN_MOUSE_NTF_CFG              = 2,
81     /// Mouse Timeout value
82     NVDS_TAG_MOUSE_TIMEOUT              = 0x3B,
83     NVDS_LEN_MOUSE_TIMEOUT              = 2,
84     /// Peer Device BD Address
85     NVDS_TAG_PEER_BD_ADDRESS            = 0x3C,
86     NVDS_LEN_PEER_BD_ADDRESS            = 7,
87     /// Mouse Energy Safe
88     NVDS_TAG_MOUSE_ENERGY_SAFE          = 0x3D,
89     NVDS_LEN_MOUSE_SAFE_ENERGY          = 2,
90     /// EDIV (2bytes), RAND NB (8bytes),  LTK (16 bytes), Key Size (1 byte)
91     NVDS_TAG_LTK                        = 0x3E,
92     NVDS_LEN_LTK                        = 28,
93     /// PAIRING
94     NVDS_TAG_PAIRING                    = 0x3F,
95     NVDS_LEN_PAIRING                    = 54,
96 };
97 #else
98 enum app_nvds_tag
99 {
100     /// BLE Application Advertising data
101     NVDS_TAG_APP_BLE_ADV_DATA           = 0x0B,
102     NVDS_LEN_APP_BLE_ADV_DATA           = 32,
103 
104     /// BLE Application Scan response data
105     NVDS_TAG_APP_BLE_SCAN_RESP_DATA     = 0x0C,
106     NVDS_LEN_APP_BLE_SCAN_RESP_DATA     = 32,
107 
108     /// Peripheral Bonded
109     NVDS_TAG_PERIPH_BONDED              = 0x39,
110     NVDS_LEN_PERIPH_BONDED              = 1,
111 
112     /// Peer Device BD Address
113     NVDS_TAG_PEER_BD_ADDRESS            = 0x3A,
114     NVDS_LEN_PEER_BD_ADDRESS            = 7,
115 
116     NVDS_TAG_PEER_BD_ADDRESS1            = 0x3B,
117     NVDS_LEN_PEER_BD_ADDRESS1            = 7,
118 
119     NVDS_TAG_PEER_BD_ADDRESS2            = 0x3C,
120     NVDS_LEN_PEER_BD_ADDRESS2            = 7,
121 
122     NVDS_TAG_PEER_BD_ADDRESS3            = 0x3D,
123     NVDS_LEN_PEER_BD_ADDRESS3            = 7,
124 
125     NVDS_TAG_PEER_BD_ADDRESS4            = 0x3E,
126     NVDS_LEN_PEER_BD_ADDRESS4            = 7,
127 
128     NVDS_TAG_DEVICE_NUM                  = 0x3F,
129     NVDS_LEN_DEVICE_NUM                  = 1,
130 
131     /// EDIV (2bytes), RAND NB (8bytes),  LTK (16 bytes), Key Size (1 byte)
132     NVDS_TAG_LTK                        = 0x40,
133     NVDS_LEN_LTK                        = 28,
134 
135     NVDS_TAG_LTK1                        = 0x41,
136     NVDS_LEN_LTK1                        = 28,
137 
138     NVDS_TAG_LTK2                        = 0x42,
139     NVDS_LEN_LTK2                        = 28,
140 
141     NVDS_TAG_LTK3                        = 0x43,
142     NVDS_LEN_LTK3                        = 28,
143 
144     NVDS_TAG_LTK4                        = 0x44,
145     NVDS_LEN_LTK4                        = 28,
146 
147 };
148 
149 #endif
150 enum app_loc_nvds_tag
151 {
152     /// Audio mode 0 task
153     NVDS_TAG_AM0_FIRST                  = NVDS_TAG_APP_SPECIFIC_FIRST,
154 
155     NVDS_TAG_AM0_LAST                   = NVDS_TAG_APP_SPECIFIC_FIRST+16,
156 
157     /// Local device Identity resolving key
158     NVDS_TAG_LOC_IRK,
159     /// Peer device Resolving identity key (+identity address)
160     NVDS_TAG_PEER_IRK,
161     /// size of local identity resolving key
162     NVDS_LEN_LOC_IRK                    = KEY_LEN,
163     /// size of Peer device identity resolving key (+identity address)
164     NVDS_LEN_PEER_IRK                   = sizeof(struct gapc_irk),
165 };
166 #endif // (NVDS_SUPPORT)
167 
168 /************************************************************************************************/
169 //add by bottle.mao
170 
171 #define BD_ADDR_LEN 6
172 
173 typedef uint8_t bd_addr[BD_ADDR_LEN];
174 
175 
176 
177 
178 
179 
180 //add by bottle.mao
181 struct devicc_adv_parameter
182 {
183     //advert_data
184     uint16_t adv_int_min;
185 
186     uint16_t adv_int_max;
187 
188     uint8_t adv_type;
189 
190     uint8_t addr_type_own;
191 
192     uint8_t channel_map;
193 
194     uint8_t adv_filter_policy;
195 
196     uint8_t discover_mode;
197 };
198 
199 
200 
201 
202 
203 
204 #define RX_DAT_BUF_SIZE 320
205 
206 struct RX_BUF_S
207 {
208     uint16_t RxCurrentLen;
209     uint16_t RxTotalLen;
210         // received data value
211 #ifndef N32WB452_BT_API
212     uint8_t RxBuf[RX_DAT_BUF_SIZE];
213 #endif
214         // received data length
215 };
216 
217 /************************************************************************************************/
218 
219 /// Application environment structure
220 struct app_env_tag
221 {
222 
223 
224     /// send data timesout
225     uint32_t timesout;
226 
227     /// Connection handle
228     uint16_t conhdl;
229       /// Connection interval
230     uint16_t con_interval;
231     uint16_t con_intv_min;
232     uint16_t con_intv_max;
233     uint16_t con_latency;
234     uint16_t con_time_out;
235     /// Advertising parameter
236     struct devicc_adv_parameter adv_para;
237 
238     /// MTU
239     uint16_t max_mtu;
240     /// Received data information
241     struct RX_BUF_S rx_data;
242 
243 
244     /// Connection Index
245     uint8_t  conidx;
246 
247     /// Last initialized profile
248     uint8_t next_svc;
249 
250     /// Bonding status
251     bool bonded;
252 
253     /// Device Name length
254     uint8_t dev_name_len;
255     /// Device Name
256     uint8_t dev_name[APP_DEVICE_NAME_MAX_LEN];
257 
258 
259     /// Counter used to generate IRK
260     uint8_t rand_cnt;
261     /// Local device IRK
262     uint8_t loc_irk[KEY_LEN];
263 
264 /*******************************************************************************/
265    /// Connection security enable
266     bool security_enable;
267 
268 /// receivce data flag
269     uint8_t rec_flag;
270 
271 //*******************************************************************************/
272 /// Pairing mode
273     uint8_t pairing_mode;
274 /// sec iocap
275     uint8_t iocap;
276 /// Pin code required
277     uint32_t pin_code;
278 
279 
280 /// RSSI value
281     int8_t rssi;
282 
283 /// White List size
284     uint8_t wlst_size;
285 
286 /// current connect device number
287     uint8_t con_device_num;
288 
289 /// local device bda
290     struct bd_addr bdaddr;
291 /// addr type
292     uint8_t bdaddr_type;
293 
294 /// adv data buf
295     uint8_t adv_data_buf[31];
296 /// adv data buf len
297     uint8_t adv_data_len;
298 
299 ///  scan response data buf
300     uint8_t scan_rsp_data_buf[31];
301 
302 ///  scan response data buf len
303     uint8_t scan_rsp_data_len;
304 
305 /// battery level
306     uint8_t  batt_lvl;
307 
308 
309 
310 /*******************************************************************************/
311 };
312 
313 
314 
315 /// Application environment
316 extern struct app_env_tag app_env;
317 
318 
319 
320 /**
321  ****************************************************************************************
322  * @brief Initialize the BLE demo application.
323  ****************************************************************************************
324  */
325 void appm_init(void);
326 
327 /**
328  ****************************************************************************************
329  * @brief Add a required service in the database
330  ****************************************************************************************
331  */
332 bool appm_add_svc(void);
333 
334 /**
335  ****************************************************************************************
336  * @brief Put the device in general discoverable and connectable mode
337  ****************************************************************************************
338  */
339 void appm_start_advertising(void);
340 
341 /**
342  ****************************************************************************************
343  * @brief Put the device in non discoverable and non connectable mode
344  ****************************************************************************************
345  */
346 void appm_stop_advertising(void);
347 
348 /**
349  ****************************************************************************************
350  * @brief Send to request to update the connection parameters
351  ****************************************************************************************
352  */
353 void appm_update_param(struct gapc_conn_param *conn_param);
354 
355 void appm_set_mtu(void);
356 
357 /**
358  ****************************************************************************************
359  * @brief Send a disconnection request
360  ****************************************************************************************
361  */
362 void appm_disconnect(void);
363 
364 /**
365  ****************************************************************************************
366  * @brief Retrieve device name
367  *
368  * @param[out] device name
369  *
370  * @return name length
371  ****************************************************************************************
372  */
373 uint8_t appm_get_dev_name(uint8_t* name);
374 
375 
376 /**
377  ****************************************************************************************
378  * @brief Return if the device is currently bonded
379  ****************************************************************************************
380  */
381 bool app_sec_get_bond_status(void);
382 
383 
384 #endif
385 
386 #endif
387