• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Chipsea Technologies (Shenzhen) Corp., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef _APP_PRESETN_H
16 #define _APP_PRESETN_H
17 
18 #include "ble_ip_config.h"     // SW configuration
19 
20 #if (BLE_APP_PRESENT)
21 
22 #include <stdint.h>          // Standard Integer Definition
23 #include "bt_common.h"           // Common BT Definitions
24 #include "arch.h"            // Platform Definitions
25 #include "hal_gapc.h"            // GAPC Definitions
26 
27 #if (NVDS_SUPPORT)
28 #include "nvds.h"
29 #endif // (NVDS_SUPPORT)
30 #include "hal_gapm_task.h"
31 #include "flash_api.h"
32 
33 /*
34  * DEFINES
35  ****************************************************************************************
36  */
37 /// Maximal length of the Device Name value
38 #define APP_DEVICE_NAME_MAX_LEN      (32)
39 
40 /*
41  * MACROS
42  ****************************************************************************************
43  */
44 
45 #define APP_HANDLERS(subtask)    {&subtask##_msg_handler_list[0], ARRAY_LEN(subtask##_msg_handler_list)}
46 
47 /*
48  * ENUMERATIONS
49  ****************************************************************************************
50  */
51 
52 #if (NVDS_SUPPORT)
53 /// List of Application NVDS TAG identifiers
54 enum app_nvds_tag
55 {
56     /// BD Address
57     NVDS_TAG_BD_ADDRESS                 = 0x01,
58     NVDS_LEN_BD_ADDRESS                 = 6,
59 
60     /// Device Name
61     NVDS_TAG_DEVICE_NAME                = 0x02,
62     NVDS_LEN_DEVICE_NAME                = 62,
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     /// Audio mode 0 task
98     NVDS_TAG_AM0_FIRST                  = 0x90,
99     NVDS_TAG_AM0_LAST                   = 0x9F,
100 
101     /// Local device Identity resolving key
102     NVDS_TAG_LOC_IRK                    = 0xA0,
103     NVDS_LEN_LOC_IRK                    = KEY_LEN,
104 
105     /// Peer device Resolving identity key (+identity address)
106     NVDS_TAG_PEER_IRK                   = 0xA1,
107     NVDS_LEN_PEER_IRK                   = sizeof(struct gapc_irk),
108 };
109 #endif // (NVDS_SUPPORT)
110 
111 /// Advertising state machine
112 enum app_adv_state
113 {
114     /// Advertising activity does not exists
115     APP_ADV_STATE_IDLE = 0,
116     /// Creating advertising activity
117     APP_ADV_STATE_CREATING,
118     /// Setting advertising data
119     APP_ADV_STATE_SETTING_ADV_DATA,
120     /// Setting scan response data
121     APP_ADV_STATE_SETTING_SCAN_RSP_DATA,
122 
123     /// Advertising activity created
124     APP_ADV_STATE_CREATED,
125     /// Starting advertising activity
126     APP_ADV_STATE_STARTING,
127     /// Advertising activity started
128     APP_ADV_STATE_STARTED,
129     /// Stopping advertising activity
130     APP_ADV_STATE_STOPPING,
131     /// Upddate advertising data
132     APP_ADV_STATE_UPDATE_ADV_DATA,
133 };
134 
135 /// Scan state machine
136 enum app_scan_state
137 {
138     ///
139     APP_SCAN_STATE_IDLE = 0,
140     ///
141     APP_SCAN_STATE_CREATING,
142     ///
143     APP_SCAN_STATE_CREATED,
144     ///
145     APP_SCAN_STATE_STARTING,
146     ///
147     APP_SCAN_STATE_STARTED,
148     ///
149     APP_SCAN_STATE_STOPPING,
150 };
151 
152 enum app_deinit_flag
153 {
154     APP_DEINIT_FLAG_ADV = BIT0,
155     APP_DEINIT_FLAG_SCAN = BIT1,
156     APP_DEINIT_FLAG_CON = BIT2,
157 };
158 /*
159  * TYPE DEFINITIONS
160  ****************************************************************************************
161  */
162 
163 /// Structure containing information about the handlers for an application subtask
164 struct app_subtask_handlers
165 {
166     /// Pointer to the message handler table
167     const struct ke_msg_handler *p_msg_handler_tab;
168     /// Number of messages handled
169     uint16_t msg_cnt;
170 };
171 
172 /// Application environment structure
173 struct app_env_tag
174 {
175     /// Connection handle
176     uint16_t conhdl;
177     /// Connection Index
178     uint8_t  conidx;
179 
180     /// Advertising activity index
181     uint8_t adv_actv_idx;
182     /// Previous advertising state (@see enum app_adv_state)
183     uint8_t adv_state_prv;
184 
185     /// Current advertising state (@see enum app_adv_state)
186     uint8_t adv_state;
187     /// Next expected operation completed event
188     uint8_t adv_op;
189 
190     /// Last initialized profile
191     uint8_t next_svc;
192 
193     /// Bonding status
194     bool bonded;
195 
196     /// Device Name length
197     uint8_t dev_name_len;
198     /// Device Name
199     uint8_t dev_name[APP_DEVICE_NAME_MAX_LEN];
200 
201     /// Appearance
202     uint16_t appearance;
203 
204     /// Local device IRK
205     uint8_t loc_irk[KEY_LEN];
206 
207     /// Secure Connections on current link
208     bool sec_con_enabled;
209 
210     /// Counter used to generate IRK
211     uint8_t rand_cnt;
212 
213     /// Delete bonding
214     bool del_bond;
215 
216     /// Scan activity index
217     uint8_t scan_actv_idx;
218     /// Current scan state (@see enum app_scan_state)
219     uint8_t scan_state;
220     /// Next expected operation completed event
221     uint8_t scan_op;
222 };
223 
224 typedef struct app_env_tag AonBleAppEnv;
225 
226 struct ble_app_user_info
227 {
228     /// Device Name length
229     uint16_t dev_name_len;
230     /// Device Name
231     uint8_t dev_name[32];
232     /// Advertising Data length
233     uint16_t adv_data_len;
234     /// Advertising data
235     uint8_t adv_data[ADV_DATA_LEN];
236     /// Advertising parameters
237     bool user_adv_param;
238     /// Own address type (@see enum gapm_own_addr)
239     uint8_t own_addr_type;
240     /// BD Address of device
241     bd_addr_t own_addr;
242     /// Advertising duration (in unit of 10ms). 0 means that advertising continues
243     /// until the host disable it
244     uint16_t adv_duration;
245     /// For prop parameter, @see enum gapm_leg_adv_prop, @see enum gapm_ext_adv_prop and @see enum gapm_per_adv_prop for help
246     struct gapm_adv_create_param adv_param;
247     struct gapm_scan_param scan_param;
248     uint16_t scan_duration;
249     /// app_user_callback
250     struct app_callbacks *user_app_callback;
251 };
252 
253 typedef struct ble_app_user_info AonBleAppUserInfo;
254 
255 /// APP callbacks
256 struct app_callbacks
257 {
258     /// Callback app init
259     void (*app_on_init)(void);
260 
261     /// Callback app init complete
262     void (*app_on_init_complete)(void);
263 
264     /// Callback app add svc
265     bool (*app_on_add_svc)(void);
266 
267     /// Callback app enable prf
268     void (*app_on_enable_prf)(uint8_t);
269 
270     /// Callback connect
271     void (*app_on_connection)(const uint8_t, struct gapc_connection_req_ind const *);
272 
273     /// Callback disconnect
274     void (*app_on_disconnect)(struct gapc_disconnect_ind const *);
275 
276     /// Callback con param update
277     void (*app_on_update_params_request)(struct gapc_param_update_req_ind const *);
278 
279     /// Callback adv operation status
280     void (*app_on_adv_status)(struct gapm_cmp_evt const *);
281 
282 };
283 
284 typedef struct _BleAppOp {
285     list_node_t   node;
286     uint8_t       opType;
287     uint8_t       adv_state;
288     uint8_t       adv_state_prv;
289 } BleAppOp;
290 
291 /*
292  * GLOBAL VARIABLE DECLARATION
293  ****************************************************************************************
294  */
295 
296 /// Application environment
297 extern struct app_env_tag app_env;
298 extern struct ble_app_user_info ble_user_info;
299 
300 /*
301  * FUNCTION DECLARATIONS
302  ****************************************************************************************
303  */
304 
305 /**
306  ****************************************************************************************
307  * @brief Initialize the BLE demo application.
308  ****************************************************************************************
309  */
310 void appm_init(void);
311 
312 /**
313  ****************************************************************************************
314  * @brief Add a required service in the database
315  ****************************************************************************************
316  */
317 bool appm_add_svc(void);
318 
319 
320 /**
321  ****************************************************************************************
322  * @brief
323  ****************************************************************************************
324  */
325 void appm_adv_fsm_next(void);
326 
327 /**
328  ****************************************************************************************
329  * @brief
330  ****************************************************************************************
331  */
332 void appm_scan_fsm_next(void);
333 
334 /**
335  ****************************************************************************************
336  * @brief Send to request to update the connection parameters
337  ****************************************************************************************
338  */
339 void appm_update_param(struct gapc_conn_param *conn_param);
340 
341 /**
342  ****************************************************************************************
343  * @brief Send a disconnection request
344  ****************************************************************************************
345  */
346 void appm_disconnect(void);
347 
348 /**
349  ****************************************************************************************
350  * @brief Retrieve device name
351  *
352  * @param[out] device name
353  *
354  * @return name length
355  ****************************************************************************************
356  */
357 uint8_t appm_get_dev_name(uint8_t* name);
358 
359 /**
360  ****************************************************************************************
361  * @brief Start/stop advertising
362  *
363  * @param[in] start     True if advertising has to be started, else false
364  ****************************************************************************************
365  */
366 void appm_update_adv_state(bool start);
367 
368 /**
369  ****************************************************************************************
370  * @brief delete advertising
371  *
372  * @param[in] none
373  ****************************************************************************************
374  */
375 
376 void appm_delete_advertising(void);
377 /**
378  ****************************************************************************************
379  * @brief Return if the device is currently bonded
380  ****************************************************************************************
381  */
382 bool app_sec_get_bond_status(void);
383 
384 uint8_t appm_set_dev_name(char* name, uint8_t len);
385 void appm_set_dev_appearance(uint16_t appearance);
386 void appm_set_dev_info(struct gapc_set_dev_info_req_ind const *param, uint8_t* status);
387 
388 void ble_enable(void);
389 void ble_disable(void);
390 void ble_disabled(void);
391 bool ble_activity_enabled(void);
392 void app_ble_env_init();
393 void app_ble_addr_init(uint8_t *addr, uint8_t own_addr_type);
394 void app_ble_set_dev_name(char* name, uint8_t len);
395 void app_ble_set_adv_data(uint8_t *buf, uint16_t buf_len);
396 uint8_t app_ble_set_adv_params(struct gapm_adv_create_param* params, uint16_t adv_duration);
397 uint8_t app_ble_update_adv_data();
398 uint8_t app_ble_update_adv_params(uint32_t adv_intv_min, uint32_t adv_intv_max);
399 uint8_t app_ble_update_con_params(uint16_t conn_intv_min, uint16_t conn_intv_max,uint16_t conn_latency, uint16_t time_out);
400 uint8_t app_ble_start_scan();
401 uint8_t app_ble_stop_scan();
402 uint8_t app_ble_start_adv();
403 uint8_t app_ble_stop_adv();
404 uint8_t app_ble_disconnect();
405 void app_ble_register_callbak(struct app_callbacks *ble_app_callbak);
406 void app_ble_update_addr(uint8_t *addr);
407 void app_ble_oplist_init(void);
408 void app_ble_check_oplist(void);
409 void app_ble_oplist_insert(uint8_t optype ,uint8_t adv_state ,uint8_t adv_state_prv ,void *cmdptr);
410 bool app_env_check(void);
411 
412 /// @} APP
413 
414 #endif //(BLE_APP_PRESENT)
415 
416 #endif // _APP_PRESETN_H
417