1 /***************************************************************************
2 *
3 *Copyright 2015-2019 BES.
4 *All rights reserved. All unpublished rights reserved.
5 *
6 *No part of this work may be used or reproduced in any form or by any
7 *means, or stored in a database or retrieval system, without prior written
8 *permission of BES.
9 *
10 *Use of this work is governed by a license granted by BES.
11 *This work contains confidential and proprietary information of
12 *BES. which is protected by copyright, trade secret,
13 *trademark and other intellectual property rights.
14 *
15 ****************************************************************************/
16 
17 #ifndef __APP_BLE_MODE_SWITCH_H__
18 #define __APP_BLE_MODE_SWITCH_H__
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /*****************************header include********************************/
25 #include "bluetooth.h"
26 
27 /******************************macro defination*****************************/
28 #ifndef BLE_CONNECTION_MAX
29 #define BLE_CONNECTION_MAX (1)
30 #endif
31 
32 // 3 bytes reserved for FLAG field
33 #define BLE_ADV_DATA_MAX_LEN (28)
34 #define BLE_SCAN_RSP_DATA_MAX_LEN (31)
35 
36 // the default interval is 160ms, note that for Bisto user case, to
37 // let GVA iOS version pop-out notification smoothly, the maximum interval should be this value
38 #define BLE_ADVERTISING_INTERVAL (160)
39 #define BLE_FAST_ADVERTISING_INTERVAL (48)
40 
41 #define BLE_ADV_SVC_FLAG  0x16
42 #define BLE_ADV_MANU_FLAG 0xFF
43 
44 // Maximal length of the Device Name value
45 #define APP_DEVICE_NAME_MAX_LEN      (24)
46 
47 /******************************type defination******************************/
48 /**
49  * @brief The state type of the ble
50  *
51  */
52 enum BLE_STATE_E {
53     STATE_IDLE       = 0,
54     ADVERTISING      = 1,
55     STARTING_ADV     = 2,
56     STOPPING_ADV     = 3,
57     SCANNING         = 4,
58     STARTING_SCAN    = 5,
59     STOPPING_SCAN    = 6,
60     CONNECTING       = 7,
61     STARTING_CONNECT = 8,
62     STOPPING_CONNECT = 9,
63 };
64 
65 /**
66  * @brief The operation type of the ble
67  *
68  */
69 enum BLE_OP_E {
70     OP_IDLE       = 0,
71     START_ADV     = 1,
72     START_SCAN    = 2,
73     START_CONNECT = 3,
74     STOP_ADV      = 4,
75     STOP_SCAN     = 5,
76     STOP_CONNECT  = 6,
77 };
78 
79 enum BLE_ADV_USER_E {
80     USER_STUB            = 0,
81     USER_GFPS            = 1,
82     USER_GSOUND          = 2,
83     USER_AI              = 3,
84     USER_INTERCONNECTION = 4,
85     USER_TILE            = 5,
86     USER_OTA             = 6,
87     USER_BT_ADP            = 7,
88     BLE_ADV_USER_NUM,
89 };
90 
91 typedef void (*BLE_DATA_FILL_FUNC_T)(void *advParam);
92 
93 enum BLE_CONNECT_STATE {
94     BLE_DISCONNECTED  = 0,
95     BLE_DISCONNECTING = 1,
96     BLE_CONNECTED     = 2,
97 };
98 
99 typedef struct {
100     uint8_t advType;
101     uint16_t advInterval;
102     uint8_t advDataLen;
103     uint8_t advData[BLE_ADV_DATA_MAX_LEN];
104     uint8_t scanRspDataLen;
105     uint8_t scanRspData[BLE_SCAN_RSP_DATA_MAX_LEN];
106 } BLE_ADV_PARAM_T;
107 
108 typedef struct {
109     uint8_t scanType;
110     uint16_t scanWindow;
111     uint16_t scanInterval;
112 } BLE_SCAN_PARAM_T;
113 
114 typedef struct {
115     uint8_t state;
116     uint8_t addr[BTIF_BD_ADDR_SIZE];
117 } BLE_CONNECT_PARAM_T;
118 
119 typedef struct
120 {
121     bool advSwitch;
122     uint8_t state;
123     uint8_t op;
124     uint8_t connectNum;
125     uint8_t bleAddrToConnect[BTIF_BD_ADDR_SIZE];
126     uint32_t adv_user_register; //one bit represent one user
127     uint32_t adv_user_enable;   //one bit represent one user
128 
129     /// Device Name length
130     uint8_t dev_name_len;
131     /// Device Name
132     uint8_t dev_name[APP_DEVICE_NAME_MAX_LEN];
133 
134     BLE_DATA_FILL_FUNC_T bleDataFillFunc[BLE_ADV_USER_NUM];
135 
136     // param used for BLE adv
137     BLE_ADV_PARAM_T advInfo;
138 
139     // prarm used for BLE scan
140     BLE_SCAN_PARAM_T scanInfo;
141 
142     // param used for BLE connect
143     BLE_CONNECT_PARAM_T connectInfo[BLE_CONNECTION_MAX];
144 } __attribute__((__packed__)) BLE_MODE_ENV_T;
145 
146 /****************************function declearation**************************/
147 /*---------------------------------------------------------------------------
148  *            app_ble_mode_tws_sync_init
149  *---------------------------------------------------------------------------
150  *
151  *Synopsis:
152  *    tws related environment initialization for ble module
153  *
154  * Parameters:
155  *    void
156  *
157  * Return:
158  *    void
159  */
160 void app_ble_mode_tws_sync_init(void);
161 
162 /*---------------------------------------------------------------------------
163  *            app_ble_mode_init
164  *---------------------------------------------------------------------------
165  *
166  *Synopsis:
167  *    init the bleModeEnv
168  *
169  * Parameters:
170  *    void
171  *
172  * Return:
173  *    void
174  */
175 void app_ble_mode_init(void);
176 
177 /*---------------------------------------------------------------------------
178  *            app_ble_sync_ble_info
179  *---------------------------------------------------------------------------
180  *
181  *Synopsis:
182  *    for tws sync ble info
183  *
184  * Parameters:
185  *    void
186  *
187  * Return:
188  *    void
189  */
190 void app_ble_sync_ble_info(void);
191 
192 /*---------------------------------------------------------------------------
193  *            app_ble_register_data_fill_handler
194  *---------------------------------------------------------------------------
195  *
196  *Synopsis:
197  *    register a BLE advertisement and scan response data fill handler for a
198  *    specific user(see @BLE_ADV_USER_E), so that the adv/scan response data
199  *    could present in BLE adv/scan response data
200  *
201  * Parameters:
202  *    user - see the defination in BLE_ADV_USER_E
203  *    func - adv/scan response data fill handler for specific user
204  *
205  * Return:
206  *    void
207  */
208 void app_ble_register_data_fill_handle(enum BLE_ADV_USER_E user, BLE_DATA_FILL_FUNC_T func, bool enable);
209 void app_ble_unregister_data_fill_handle(enum BLE_ADV_USER_E user);
210 void app_ble_data_fill_enable(enum BLE_ADV_USER_E user, bool enable);
211 bool app_ble_get_data_fill_enable(enum BLE_ADV_USER_E user);
212 bool app_ble_start_adv(uint8_t advType, uint16_t advInterval);
213 /*---------------------------------------------------------------------------
214  *            app_ble_start_connectable_adv
215  *---------------------------------------------------------------------------
216  *
217  *Synopsis:
218  *    start connetable BLE advertise
219  *
220  * Parameters:
221  *    advertisement interval in ms
222  *
223  * Return:
224  *    None
225  */
226 void app_ble_start_connectable_adv(uint16_t advInterval);
227 
228 void app_ble_force_switch_adv(bool onOff);
229 
230 void app_ble_start_scan(uint16_t scanWindow, uint16_t scanWnterval);
231 
232 void app_ble_start_connect(uint8_t *bdAddrToConnect);
233 
234 void app_ble_stop_activities(void);
235 
236 bool app_ble_is_in_connected_state(void);
237 
238 bool app_ble_is_in_advertising_state(void);
239 
240 void app_ble_stub_user_init(void);
241 
242 #ifdef __cplusplus
243 }
244 #endif
245 
246 #endif /* #ifndef __APP_BLE_MODE_SWITCH_H__ */
247