• 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 /*chipsea_ohos proguard begin*/
16 #include "cs_proguard.h"
17 /*chipsea_ohos proguard end*/
18 #include "al_rtos.h"
19 #include "app_ble_only.h"
20 #include "ble_task_msg.h"
21 #include "csble.h"
22 #include "bt_cs8800_drvif.h"
23 #include "sysctrl_api.h"
24 #include "ble_task.h"
25 #include "bt_types_def.h"
26 #include "cs_ble_adp_api.h"
27 #include "system.h"
28 
29 /// Handle of the BLE task
30 static rtos_task_handle ble_task_handle = NULL;
31 /// Indicates whether BLE task is suspended or not
32 static bool ble_task_suspended;
33 static rtos_queue app_ble_task_queue;
34 static cs_host_cfg_t cfg = {
35     .support_sniff = true,
36 #if APP_SUPPORT_MULTIPLE_PHONE == 0
37     .multiple_phone = false,
38 #else
39     .multiple_phone = true,
40 #endif
41 #if (AON_SUPPORT)
42     .support_aon = true,
43 #ifdef APP_SUPPORT_DEEPSLEEP
44     .support_sleep = true,
45 #else
46     .support_sleep = false,
47 #endif
48 #else
49     .support_aon = false,
50     .support_sleep = false,
51 #endif
52 #ifdef CFG_BTDM_RAM_VER
53     .rom_version = 0,
54 #else
55     .rom_version = CFG_ROM_VER,
56 #endif
57 };
58 
59 #define CS_BLE_STACK_SIZE TASK_STACK_SIZE_BLE_TASK_ONLY
60 
61 
62 
63 
ble_task_suspend(void)64 uint32_t ble_task_suspend(void)
65 {
66      //TRACE("ble_task_suspend \n");
67 #if 1
68     uint32_t retvalue = 0;
69     GLOBAL_INT_DISABLE();
70     ble_task_suspended = true;
71     GLOBAL_INT_RESTORE();
72 
73     // wait for notification
74     retvalue = rtos_task_wait_notification(-1);
75     return retvalue;
76 #endif
77 }
78 
79 
80 
ble_task_resume(bool isr,uint32_t value)81 void ble_task_resume(bool isr,uint32_t value)
82 {
83     //TRACE("ble_task_resume \n");
84     //if (!ble_task_suspended) {
85         //return;
86     //}
87     ble_task_suspended = false;
88     //dbg("ble_task_handle = 0x%x\n",ble_task_handle);
89 
90     // Notify the bt task
91     rtos_task_notify(ble_task_handle, value, isr);
92 }
93 
94 /**
95  * @brief Main loop for the BLE Task
96  */
RTOS_TASK_FCT(ble_task)97 static RTOS_TASK_FCT(ble_task)
98 {
99     BOOL ret = TRUE;
100 #if APP_SUPPORT_AES == 1
101     uint32_t pwrmd = pwrctrl_pwrmd_cpusys_sw_record_getf();
102     if(pwrmd < CPU_SYS_POWER_DOWN) {
103         // clk en
104         cpusysctrl_hclkme_set(CSC_HCLKME_DMA_EN_BIT);
105         bt_drv_generate_secret_key();
106         bt_drv_start_generate_key();
107         app_ble_handler_register(BLE_HANDLER_REG_0, bt_drv_pool_key_complete);
108     }
109 #endif
110     app_ble_init();
111     #if (CFG_ROM_VER == 255)
112     uint8_t chip_id = ChipIdGet(0);
113     if (chip_id == 0x03) {
114         cfg.rom_version = 2;
115     } else if (chip_id == 0x07) {
116         cfg.rom_version = 3;
117     }
118     #endif
119     cs_stack_init_ble_only(cfg);
120     ret = cs_adp_stack_config_ble_only();
121     if(ret == TRUE){
122         GLOBAL_INT_DISABLE();
123         cs_bt_start();
124         GLOBAL_INT_RESTORE();
125         ble_task_set_default_priority();
126     }
127     ble_task_queue_notify(false);
128     for (;;)
129     {
130         uint32_t msg;
131         // Suspend the BLE task while waiting for new events
132         rtos_queue_read(app_ble_task_queue,(void *)&msg, -1, 0);
133         // schedule all pending events
134         cs_stack_loop_ble_only();
135         ble_handler_reg_list_poll();
136         cs_ble_schedule();
137     }
138 }
139 
140 
ble_task_queue_notify(bool isr)141 void ble_task_queue_notify(bool isr)
142 {
143     bool ret = 0;
144     uint32_t msg = 0xff;
145     if(rtos_queue_cnt(app_ble_task_queue) < 5){
146         ret = rtos_queue_write(app_ble_task_queue,&msg , 0, isr);
147         if(isr){
148             ret += 0;
149             //TRACE(" notify %d \n",ret);
150         }
151     }
152 }
153 
154 
ble_task_init(void)155 int ble_task_init(void)
156 {
157     uint32_t protect;
158     app_ble_only_msg_init();
159 
160     if (rtos_queue_create(sizeof(uint32_t),5,&app_ble_task_queue)){
161         TRACE("BT_TASK: Failed to Create app_bt_queue\n");
162         return -1;
163     }
164     protect = rtos_protect();
165     ble_task_suspended = false;
166     // Create the BLE task
167     if (rtos_task_create(ble_task, "BLE_TASK", BLE_TASK,
168                          CS_BLE_STACK_SIZE, NULL, TASK_PRIORITY_MAX, &ble_task_handle)) {
169         rtos_queue_delete(app_ble_task_queue);
170         return -1;
171     }
172     //TRACE("ble_task_handle = 0x%x\n",ble_task_handle);
173     rtos_unprotect(protect);
174     return 0;
175 }
176 
177 
ble_task_deinit(void)178 int ble_task_deinit(void)
179 {
180     cs_adp_ble_deinit();
181     return 0;
182 }
183 
ble_task_delete(void)184 int ble_task_delete(void)
185 {
186     cs_adp_free_stack_memory_ble_only();
187     //TRACE("ble_task_deinit ble_task_handle = 0x%x\n",ble_task_handle);
188     app_ble_only_msg_deinit();
189     if(app_ble_task_queue){
190         rtos_queue_delete(app_ble_task_queue);
191         app_ble_task_queue = NULL;
192     }
193     if(ble_task_handle){
194         rtos_task_delete(ble_task_handle);
195         ble_task_handle = NULL;
196     }
197     return 0;
198 }
199 
ble_task_set_max_priority(void)200 void ble_task_set_max_priority(void)
201 {
202     rtos_task_set_priority(ble_task_handle, TASK_PRIORITY_MAX);
203 }
204 
ble_task_set_default_priority(void)205 void ble_task_set_default_priority(void)
206 {
207     rtos_task_set_priority(ble_task_handle, TASK_PRIORITY_BT_TASK);
208 }
209 
210