• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: dfx system init
15  * This file should be changed only infrequently and with great care.
16  */
17 
18 #include "dfx_adapt_layer.h"
19 #include "diag.h"
20 #include "soc_diag_cmd_id.h"
21 #include "diag_cmd_connect.h"
22 #include "diag_cmd_filter.h"
23 #include "diag_cmd_password.h"
24 #include "diag_cmd_beat_heart.h"
25 #include "diag_cmd_get_mem_info.h"
26 #include "diag_cmd_get_task_info.h"
27 #include "diag_cmd_mem_read_write.h"
28 #include "diag_mocked_shell.h"
29 #include "diag_bt_sample_data.h"
30 #include "osal_task.h"
31 #include "osal_msgqueue.h"
32 #include "diag_ind_src.h"
33 #include "diag_filter.h"
34 #include "diag_msg.h"
35 #ifdef SUPPORT_DIAG_V2_PROTOCOL
36 #include "diag_service.h"
37 #include "diag_cmd_dispatch.h"
38 #endif /* SUPPORT_DIAG_V2_PROTOCOL */
39 #include "diag_rom_api.h"
40 #include "diag_channel.h"
41 #include "diag_adapt_layer.h"
42 #include "diag_adapt_sdt.h"
43 #include "soc_log_uart_instance.h"
44 #include "sample_data_adapt.h"
45 #include "dfx_channel.h"
46 #if (CONFIG_DFX_SUPPORT_TRANSMIT_FILE == DFX_YES)
47 #include "transmit.h"
48 #endif
49 #include "diag_adapt_psd.h"
50 #include "diag_sample_data.h"
51 #include "log_common.h"
52 #include "diag_dfx_cmd_init.h"
53 #ifdef CONFIG_MIDDLEWARE_NV_SUPPORT_HSO_ACCESS
54 #include "nv_adapt_zdiag.h"
55 #endif
56 #ifdef CONFIG_AT_SUPPORT_ZDIAG
57 #include "at_zdiag.h"
58 #endif
59 
60 static diag_cmd_reg_obj_t g_diag_default_cmd_tbl[] = {
61     { DIAG_CMD_CONNECT_RANDOM, DIAG_CMD_PWD_CHANGE, diag_cmd_password },
62     { DIAG_CMD_HOST_CONNECT, DIAG_CMD_HOST_DISCONNECT, diag_cmd_hso_connect_disconnect },
63 #ifndef SUPPORT_DIAG_V2_PROTOCOL
64     { DIAG_CMD_MSG_RPT_AIR, DIAG_CMD_MSG_RPT_USR, diag_cmd_filter_set },
65     { DIAG_CMD_MSG_CFG_SET_AIR, DIAG_CMD_MSG_CFG_SET_LEVEL, diag_cmd_filter_set },
66 #else
67     { DIAG_CMD_MSG_CFG_SET_SYS, DIAG_CMD_MSG_CFG_SET_LEVEL, diag_cmd_filter_set },
68 #endif
69 #if (CONFIG_DFX_SUPPORT_DIAG_BEAT_HEART == DFX_YES)
70     { DIAG_CMD_HEART_BEAT, DIAG_CMD_HEART_BEAT, diag_cmd_beat_heart },
71 #endif
72     { DIAG_CMD_GET_TASK_INFO, DIAG_CMD_GET_TASK_INFO, diag_cmd_get_task_info},
73     { DIAG_CMD_GET_MEM_INFO, DIAG_CMD_GET_MEM_INFO, diag_cmd_get_mem_info},
74 #if (CONFIG_DFX_MEMORY_OPERATE == DFX_YES)
75     { DIAG_CMD_MEM_MEM32, DIAG_CMD_MEM_W4, diag_cmd_mem_operate },
76 #endif
77 #if (CONFIG_DFX_BT_SAMPLE_DATA == DFX_YES)
78     { DIAG_CMD_ID_SAMPLE, DIAG_CMD_ID_SAMPLE, diag_cmd_sample_data},
79 #endif
80     { DIAG_CMD_ID_PSD_ENABLE, DIAG_CMD_ID_PSD_ENABLE, diag_cmd_psd_enable},
81     { DIAG_CMD_ID_BGLE_SAMPLE, DIAG_CMD_ID_WLAN_PHY_SAMPLE, diag_cmd_wlan_module_sample_data},
82 #ifdef CONFIG_MIDDLEWARE_NV_SUPPORT_HSO_ACCESS
83     {DIAG_CMD_NV_QRY, DIAG_CMD_NV_QRY, zdiag_adapt_nv_read},
84     {DIAG_CMD_NV_WR, DIAG_CMD_NV_WR, zdiag_adapt_nv_write},
85 #endif
86 #ifdef CONFIG_AT_SUPPORT_ZDIAG
87     {DIAG_CMD_SIMULATE_AT, DIAG_CMD_SIMULATE_AT, zdiag_at_proc},
88 #endif
89 };
90 
91 #define DFX_MSG_STACK_SIZE          0x800
92 #define TASK_PRIORITY_DFX_MSG       (osPriority_t)(5)
93 unsigned long g_dfx_osal_queue_id;
dfx_get_osal_queue_id(void)94 unsigned long dfx_get_osal_queue_id(void)
95 {
96     return g_dfx_osal_queue_id;
97 }
register_default_diag_cmd(void)98 static errcode_t register_default_diag_cmd(void)
99 {
100     return uapi_diag_register_cmd(g_diag_default_cmd_tbl,
101         sizeof(g_diag_default_cmd_tbl) / sizeof(g_diag_default_cmd_tbl[0]));
102 }
103 
msg_process_proc(uint32_t msg_id,uint8_t * data,uint32_t size)104 int32_t msg_process_proc(uint32_t msg_id, uint8_t *data, uint32_t size)
105 {
106     switch (msg_id) {
107         case DFX_MSG_ID_DIAG_PKT:
108             diag_msg_proc((uint16_t)msg_id, data, size);
109             break;
110         case DFX_MSG_ID_SDT_MSG:
111             zdiag_adapt_sdt_msg_dispatch(msg_id, data, size);
112             break;
113         case DFX_MSG_ID_BEAT_HEART:
114 #if (CONFIG_DFX_SUPPORT_DIAG_BEAT_HEART == DFX_YES)
115             diag_beat_heart_process();
116 #endif
117             break;
118 #if defined(CONFIG_DFX_SUPPORT_TRANSMIT_FILE) && (CONFIG_DFX_SUPPORT_TRANSMIT_FILE == DFX_YES)
119         case DFX_MSG_ID_TRANSMIT_FILE:
120             transmit_msg_proc(msg_id, data, size);
121             break;
122 #endif
123         default:
124             break;
125     }
126     return ERRCODE_SUCC;
127 }
128 
cmd_shell_proc(uint8_t * data,uint32_t data_len)129 static void cmd_shell_proc(uint8_t *data, uint32_t data_len)
130 {
131     diag_debug_cmd_proc(data, data_len);
132     dfx_log_debug("cmd shell: %s", data);
133 }
134 
dfx_set_log_leve(bool enable)135 static void dfx_set_log_leve(bool enable)
136 {
137     /* 断开hso, 不上报日志 */
138     if (enable == false) {
139         log_set_local_log_level(LOG_LEVEL_NONE);
140     }
141 }
142 
msg_process_thread(void * data)143 static int msg_process_thread(void *data)
144 {
145     uint8_t msg_data[DFX_MSG_MAX_SIZE + DFX_MSG_ID_LEN];
146     uint32_t msg_data_size = (uint32_t)sizeof(msg_data);
147     uint32_t msg_id;
148     int32_t ret;
149 
150     unused(data);
151 
152     while (true) {
153         (void)memset_s(msg_data, sizeof(msg_data), 0, sizeof(msg_data));
154 
155         ret = osal_msg_queue_read_copy(g_dfx_osal_queue_id, msg_data, &msg_data_size, OSAL_MSGQ_WAIT_FOREVER);
156         if (ret != ERRCODE_SUCC) {
157             PRINT("osal_msg_queue_read_copy, err ret[%d]", ret);
158             break;
159         }
160 
161         msg_id = *((uint32_t*)&msg_data[0]);
162 
163         msg_process_proc(msg_id, &msg_data[DFX_MSG_ID_LEN], DFX_MSG_MAX_SIZE);
164     }
165 
166     return ERRCODE_SUCC;
167 }
168 
thread_msg_event_init(void)169 static errcode_t thread_msg_event_init(void)
170 {
171     errcode_t ret;
172     ret = (uint32_t)osal_msg_queue_create("dfx_msg", DFX_QUEUE_MAX_SIZE, &g_dfx_osal_queue_id,
173         0, DFX_MSG_MAX_SIZE + DFX_MSG_ID_LEN);
174     if (ret != ERRCODE_SUCC) {
175         return ERRCODE_FAIL;
176     }
177 
178     osal_task *task = osal_kthread_create(msg_process_thread, NULL, "dfx_msg", DFX_MSG_STACK_SIZE);
179     if (task == NULL) {
180         return ERRCODE_FAIL;
181     }
182     osal_kthread_lock();
183     if (osal_kthread_set_priority(task, TASK_PRIORITY_DFX_MSG) != OSAL_SUCCESS) {
184         print_str("osal_kthread_set_priority excute failed!!! \r\n");
185     }
186     osal_kthread_unlock();
187     return ERRCODE_SUCC;
188 }
189 
dfx_system_init(void)190 errcode_t dfx_system_init(void)
191 {
192     errcode_t ret;
193     diag_rom_api_t rom_api;
194 
195 #ifdef SUPPORT_DIAG_V2_PROTOCOL
196     uapi_diag_service_init();
197 #endif
198     ret = register_default_diag_cmd();
199     if (ret != ERRCODE_SUCC) {
200         return ret;
201     }
202 
203     zdiag_filter_init();
204     zdiag_filter_register_notify_hook(dfx_set_log_leve);
205     dfx_set_log_leve(false);
206 
207 #ifndef FORBID_AUTO_LOG_REPORT
208     diag_auto_log_report_enable();
209 #else
210 #if CONFIG_DFX_SUPPORT_DIAG_BEAT_HEART == DFX_YES
211     ret = diag_beat_heart_init();
212     if (ret != ERRCODE_SUCC) {
213         return ret;
214     }
215 #endif
216 #endif
217 
218     ret = thread_msg_event_init();
219     if (ret != ERRCODE_SUCC) {
220         return ret;
221     }
222 
223     ret = diag_register_channel();
224     if (ret != ERRCODE_SUCC) {
225         return ret;
226     }
227 
228 #if (CONFIG_DFX_SUPPORT_DIAG_VRTTUAL_SHELL == DFX_YES)
229     zdiag_mocked_shell_init();
230     zdiag_mocked_shell_register_cmd_data_proc(cmd_shell_proc);
231 #endif /* CONFIG_DFX_SUPPORT_DIAG_VRTTUAL_SHELL */
232 
233     rom_api.report_sys_msg = uapi_zdiag_report_sys_msg_instance;
234     diag_rom_api_register(&rom_api);
235 
236 #if CONFIG_DFX_SUPPORT_TRANSMIT_FILE == DFX_YES
237     uapi_transmit_init();
238 #endif
239 
240     ret = diag_dfx_cmd_init();
241     if (ret != ERRCODE_SUCC) {
242         return ret;
243     }
244 
245     return ERRCODE_SUCC;
246 }
247