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 /*chipsea_ohos proguard begin*/
16 #include "cs_proguard.h"
17 /*chipsea_ohos proguard end*/
18 #include "al_rtos.h"
19 #include "ble_task_msg.h"
20 #if PLF_CONSOLE
21 #include "console.h"
22 #endif
23 #include "ble_task.h"
24 #include "cs_ble_adp_api.h"
25 #include "bt_cs8800_drvif.h"
26 #include "app_ble_only.h"
27
28
29 APP_BLE_HANDLER ble_handler_register_list[BLE_HANDLER_REG_MAX] = {0};
30 #define APP_BLE_ONLY_MSG_MAX (5)
31 static rtos_queue app_ble_only_msg_queue;
32 extern uint8_t ble_addr[6];
33
app_ble_handler_register(enum APP_BLE_HANDLER_REG_T user,APP_BLE_HANDLER handler)34 int app_ble_handler_register(enum APP_BLE_HANDLER_REG_T user, APP_BLE_HANDLER handler)
35 {
36 ble_handler_register_list[user]= handler;
37 return 0;
38 }
39
ble_handler_reg_list_poll(void)40 void ble_handler_reg_list_poll(void)
41 {
42 uint8_t i;
43 for (i=0; i<BLE_HANDLER_REG_MAX; i++){
44 if (ble_handler_register_list[i]){
45 ble_handler_register_list[i]();
46 }
47 }
48 }
49
app_ble_only_msg_process(APP_BLE_ONLY_MSG * msg)50 int app_ble_only_msg_process(APP_BLE_ONLY_MSG* msg)
51 {
52 int status = -1;
53 TRACE("APP:ble_only_msg id:%d \n",msg->msg_id);
54 switch (msg->msg_id) {
55 case BLE_DEINIT_DONE:
56 status = ble_task_delete();
57 break;
58 case BLE_SET_LP_LEVEL:
59 TRACE("APP: ble lp level = %x\n",msg->param);
60 cs_adp_set_lp_level_ble_only((uint8_t)msg->param);
61 break;
62 case DUT_TEST:
63 TRACE("APP: enter dut test\n");
64 bt_drv_enable_dut();
65 break;
66 case DUT_NO_SCAN:
67 case DUT_ISCAN:
68 case DUT_PSCAN:
69 case DUT_BOTH_SCAN:
70 {
71 bt_drv_scan_en(msg->msg_id - DUT_NO_SCAN);
72 }
73 break;
74 case BLE_TIMER_NOTIFY:
75 cs_adp_rw_timer_notify();
76 break;
77 case BLE_SCAN_START:
78 app_ble_start_scan();
79 break;
80 case BLE_SCAN_STOP:
81 app_ble_stop_scan();
82 break;
83 case BLE_ADV_DATA_UPDATE:
84 {
85 uint8_t buf[32];
86
87 buf[0] = APP_UPDATE_DATA_LEN + 1;
88 buf[1] = GAP_AD_TYPE_COMPLETE_NAME;
89 memcpy(buf + 2, APP_UPDATE_DATA, APP_UPDATE_DATA_LEN);
90 app_ble_set_adv_data(buf, APP_UPDATE_DATA_LEN + 2);
91 app_ble_update_adv_data();
92 }
93 break;
94 case BLE_ADV_PARAM_UPDATE:
95 {
96 if (app_ble_update_adv_params(APP_ADV_INT_MIN*2, APP_ADV_INT_MIN *2)) {
97 TRACE("ERROR\r\n");
98 return -1;
99 }
100 }
101 break;
102 case BLE_ADV_STOP:
103 app_ble_stop_adv();
104 break;
105 case BLE_ADV_START:
106 app_ble_start_adv();
107 break;
108 case BLE_DISCONNECT:
109 app_ble_disconnect();
110 break;
111 case BLE_CON_PARAM_UPDATE:
112 app_ble_update_con_params(16, 16, 25, 400);
113 break;
114 case BLE_DELETE_BOND:
115 {
116 app_env.del_bond = true;
117 app_sec_remove_bond();
118 ble_addr[0] = co_rand_byte();
119 flash_btdm_le_rand_addr_write(&ble_addr[0]);
120 app_ble_update_addr(&ble_addr[0]);
121 if (app_env.adv_state == APP_ADV_STATE_STARTED) {
122 app_ble_stop_adv();
123 appm_delete_advertising();
124 }
125 if (app_env.conhdl || app_env.conidx) {
126 app_ble_disconnect();
127 }
128 //app_ble_start_adv();
129 }
130 break;
131 default:
132 break;
133 }
134 TRACE("APP: msg id:%d :status:%d exit\n", msg->msg_id, status);
135 return status;
136 }
137
app_ble_only_msg_send(APP_BLE_ONLY_MSG * msg)138 int app_ble_only_msg_send(APP_BLE_ONLY_MSG* msg)
139 {
140 if(rtos_queue_write(app_ble_only_msg_queue,(void *)msg,(TickType_t)1,false)){
141 ASSERT_ERR(0);
142 }
143 ble_task_queue_notify(false);
144 return 0;
145 }
146
app_ble_only_msg_send_isr(APP_BLE_ONLY_MSG * msg)147 int app_ble_only_msg_send_isr(APP_BLE_ONLY_MSG* msg)
148 {
149 if(rtos_queue_write(app_ble_only_msg_queue,(void *)msg,(TickType_t)1,true)){
150 ASSERT_ERR(0);
151 }
152 ble_task_queue_notify(true);
153 return 0;
154 }
155
156
app_ble_only_msg_get(APP_BLE_ONLY_MSG * QueueMsg)157 int app_ble_only_msg_get(APP_BLE_ONLY_MSG* QueueMsg)
158 {
159 if(rtos_queue_read(app_ble_only_msg_queue,(void *)QueueMsg,0,0)) {
160 return -1;
161 }
162 return 0;
163 }
164
app_ble_only_msg_poll(void)165 static void app_ble_only_msg_poll(void)
166 {
167 APP_BLE_ONLY_MSG QueueMsg;
168 if (!app_ble_only_msg_get(&QueueMsg)){
169 app_ble_only_msg_process(&QueueMsg);
170 }
171 }
172
app_ble_only_msg_init(void)173 int app_ble_only_msg_init(void)
174 {
175 if (rtos_queue_create(sizeof(APP_BLE_ONLY_MSG),APP_BLE_ONLY_MSG_MAX,&app_ble_only_msg_queue)){
176 TRACE("APP: Failed to Create app_ble_only_msg_queue\n");
177 return -1;
178 }
179 app_ble_handler_register(BLE_HANDLER_REG_1, app_ble_only_msg_poll);
180
181 return 0;
182 }
183
app_ble_only_msg_deinit(void)184 int app_ble_only_msg_deinit(void)
185 {
186 if(app_ble_only_msg_queue){
187 rtos_queue_delete(app_ble_only_msg_queue);
188 app_ble_only_msg_queue = NULL;
189 }
190 app_ble_handler_register(BLE_HANDLER_REG_1, NULL);
191 return 0;
192 }
193
app_ble_deint_done_msg_send(void)194 int app_ble_deint_done_msg_send(void)
195 {
196 APP_BLE_ONLY_MSG msg;
197 msg.msg_id = BLE_DEINIT_DONE;
198 app_ble_only_msg_send(&msg);
199 return 0;
200 }
201
app_ble_lp_level_msg_send(uint8_t level)202 int app_ble_lp_level_msg_send(uint8_t level)
203 {
204 APP_BLE_ONLY_MSG msg;
205 msg.msg_id = BLE_SET_LP_LEVEL;
206 msg.param = (uint32_t)level;
207 app_ble_only_msg_send(&msg);
208 return 0;
209 }
210
app_ble_enter_dut_msg_send(void)211 int app_ble_enter_dut_msg_send(void)
212 {
213 APP_BLE_ONLY_MSG msg;
214 msg.msg_id = DUT_TEST;
215 app_ble_only_msg_send(&msg);
216 return 0;
217 }
218
app_ble_adv_data_update_msg_send(void)219 int app_ble_adv_data_update_msg_send(void)
220 {
221 APP_BLE_ONLY_MSG msg;
222 msg.msg_id = BLE_ADV_DATA_UPDATE;
223 app_ble_only_msg_send(&msg);
224 return 0;
225 }
226
app_ble_adv_param_update_msg_send(void)227 int app_ble_adv_param_update_msg_send(void)
228 {
229 APP_BLE_ONLY_MSG msg;
230 msg.msg_id = BLE_ADV_PARAM_UPDATE;
231 app_ble_only_msg_send(&msg);
232 return 0;
233 }
234
app_ble_adv_stop_msg_send(void)235 int app_ble_adv_stop_msg_send(void)
236 {
237 APP_BLE_ONLY_MSG msg;
238 msg.msg_id = BLE_ADV_STOP;
239 app_ble_only_msg_send(&msg);
240 return 0;
241 }
242
app_ble_adv_start_msg_send(void)243 int app_ble_adv_start_msg_send(void)
244 {
245 APP_BLE_ONLY_MSG msg;
246 msg.msg_id = BLE_ADV_START;
247 app_ble_only_msg_send(&msg);
248 return 0;
249 }
250
app_ble_disconnect_msg_send(void)251 int app_ble_disconnect_msg_send(void)
252 {
253 APP_BLE_ONLY_MSG msg;
254 msg.msg_id = BLE_DISCONNECT;
255 app_ble_only_msg_send(&msg);
256 return 0;
257 }
258
app_ble_con_param_msg_send(void)259 int app_ble_con_param_msg_send(void)
260 {
261 APP_BLE_ONLY_MSG msg;
262 msg.msg_id = BLE_CON_PARAM_UPDATE;
263 app_ble_only_msg_send(&msg);
264 return 0;
265 }
266
app_ble_del_bond_msg_send(void)267 int app_ble_del_bond_msg_send(void)
268 {
269 APP_BLE_ONLY_MSG msg;
270 msg.msg_id = BLE_DELETE_BOND;
271 app_ble_only_msg_send(&msg);
272 return 0;
273 }
274
app_ble_scan_msg_start(void)275 int app_ble_scan_msg_start(void)
276 {
277 APP_BLE_ONLY_MSG msg;
278 msg.msg_id = BLE_SCAN_START;
279 app_ble_only_msg_send(&msg);
280 return 0;
281 }
282
app_ble_scan_msg_stop(void)283 int app_ble_scan_msg_stop(void)
284 {
285 APP_BLE_ONLY_MSG msg;
286 msg.msg_id = BLE_SCAN_STOP;
287 app_ble_only_msg_send(&msg);
288 return 0;
289 }
290
app_ble_wr_scan_en(unsigned int scan_en)291 int app_ble_wr_scan_en(unsigned int scan_en)
292 {
293 APP_BLE_ONLY_MSG msg;
294 msg.msg_id = DUT_NO_SCAN + scan_en;
295 app_ble_only_msg_send(&msg);
296 return 0;
297 }
298
app_rw_timer_notify(void)299 int app_rw_timer_notify(void)
300 {
301 APP_BLE_ONLY_MSG msg;
302 msg.msg_id = BLE_TIMER_NOTIFY;
303 app_ble_only_msg_send(&msg);
304 return 0;
305 }
306
307 #if PLF_CONSOLE
app_ble_onoff_control(int argc,char * argv[])308 static int app_ble_onoff_control(int argc, char *argv[])
309 {
310 unsigned int on_off;
311
312 if (argc < 2) {
313 return ERR_WRONG_ARGS;
314 }
315
316 on_off = console_cmd_strtoul(argv[1], NULL, 10);
317 if (on_off > 1) {
318 return ERR_WRONG_ARGS;
319 }
320
321 if(on_off){
322 ble_task_init();
323 }else{
324 ble_task_deinit();
325 }
326
327 return ERR_NONE;
328 }
329
do_show_heap(int argc,char * argv[])330 int do_show_heap(int argc, char *argv[])
331 {
332 int total, used, free, max_used;
333
334 rtos_heap_info(&total, &free, &max_used);
335 used = total - free;
336 max_used = total - max_used;
337 TRACE("RTOS HEAP: free=%d used=%d max_used=%d/%d\n",
338 free, used, max_used, total);
339
340 return ERR_NONE;
341 }
342
343
344
app_ble_console_command_add(void)345 void app_ble_console_command_add(void)
346 {
347 console_cmd_add("ble_on", "ble_on <on/off> :task on/off", 2, app_ble_onoff_control);
348 console_cmd_add("HEAP", "HEAP :Show remain heap", 1, do_show_heap);
349 }
350 #endif
351