• 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: zdiag ind producer
15  * This file should be changed only infrequently and with great care.
16  */
17 #include "diag_ind_src.h"
18 #include "securec.h"
19 #include "zdiag_mem.h"
20 #include "zdiag_pkt_router.h"
21 #include "zdiag_debug.h"
22 #include "soc_diag_cmd_id.h"
23 #include "diag_adapt_layer.h"
24 #include "diag_dfx.h"
25 #include "diag_filter.h"
26 #include "dfx_adapt_layer.h"
27 #include "diag_pkt.h"
28 #include "errcode.h"
29 #include "log_file.h"
30 #include "diag_msg.h"
31 #include "uapi_crc.h"
32 
33 #ifndef DIAG_CMD_MSG_RPT_SYS
34 #define DIAG_CMD_MSG_RPT_SYS DIAG_CMD_MSG_RPT_SYS_FULLY_LOG
35 #endif
36 #define DIAG_LOG_HEADER_SIZE DIAG_FULL_LOG_HEADER_SIZE
37 
zdiag_msg_permit(uint32_t module_id,uint8_t level)38 STATIC bool zdiag_msg_permit(uint32_t module_id, uint8_t level)
39 {
40     if ((zdiag_is_enable() == false) && (uapi_zdiag_offline_log_is_enable() != true)) {
41         return false;
42     }
43 
44     return zdiag_log_enable(level, module_id);
45 }
46 
uapi_diag_report_packet(uint16_t cmd_id,diag_option_t * option,const uint8_t * packet,uint16_t packet_size,bool sync)47 errcode_t uapi_diag_report_packet(uint16_t cmd_id, diag_option_t *option, const uint8_t *packet, uint16_t packet_size,
48     bool sync)
49 {
50     errcode_t ret = ERRCODE_SUCC;
51     diag_pkt_handle_t pkt;
52     diag_pkt_process_param_t process_param;
53     uint16_t crc16 = 0;
54     diag_addr dst = 0;
55     if (option != NULL) {
56         dst = option->peer_addr;
57     }
58 
59     dfx_log_debug("uapi_diag_report_packet in\r\n");
60     if (zdiag_is_enable() == false) {
61         dfx_log_err("diag_unconnect\r\n");
62         return ERRCODE_FAIL;
63     }
64 
65     uint8_t *buf = dfx_malloc(0, DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE);
66     msp_mux_packet_head_stru_t *mux = (msp_mux_packet_head_stru_t *)buf;
67     msp_diag_head_ind_stru_t *ind = (msp_diag_head_ind_stru_t *)(buf + DIAG_MUX_HEADER_SIZE);
68 
69     diag_pkt_handle_init(&pkt, 2); /* data_cnt 为2 */
70     diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_0, (uint8_t *)mux, DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE,
71         DIAG_PKT_DATA_STACK);
72     diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_1, (uint8_t *)packet, packet_size, DIAG_PKT_DATA_STACK);
73 
74     diag_mk_ind_header(ind, cmd_id, packet_size);
75     crc16 = uapi_crc16(crc16, (uint8_t *)ind, DIAG_IND_HEADER_SIZE);
76     crc16 = uapi_crc16(crc16, packet, packet_size);
77     diag_mk_mux_header_1(mux, MUX_PKT_IND, cmd_id, DIAG_IND_HEADER_SIZE + packet_size);
78     diag_mk_mux_header_2(mux, dst, crc16);
79     if (sync) {
80         process_param.cur_proc = DIAG_PKT_PROC_USR_SYNC_CMD_IND;
81     } else {
82         process_param.cur_proc = DIAG_PKT_PROC_USR_ASYNC_CMD_IND;
83     }
84     diag_pkt_router(&pkt, &process_param);
85     dfx_free(0, buf);
86     return ret;
87 }
88 
zdiag_report_packets(uint16_t cmd_id,diag_option_t * option,diag_report_packet * report_packet,uint8_t critical)89 STATIC errcode_t zdiag_report_packets(uint16_t cmd_id, diag_option_t *option, diag_report_packet *report_packet,
90     uint8_t critical)
91 {
92     errcode_t ret = ERRCODE_SUCC;
93     uint8_t **packet = report_packet->packet;
94     uint16_t *packet_size = report_packet->packet_size;
95     uint8_t pkt_cnt = report_packet->pkt_cnt;
96     diag_pkt_handle_t pkt;
97     diag_pkt_process_param_t process_param;
98     uint8_t buf[DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE];
99     msp_mux_packet_head_stru_t *mux = (msp_mux_packet_head_stru_t *)buf;
100     msp_diag_head_ind_stru_t *ind = (msp_diag_head_ind_stru_t *)(buf + DIAG_MUX_HEADER_SIZE);
101     uint16_t crc16 = 0;
102     uint16_t usr_data_size = 0;
103     diag_addr dst = 0;
104     if (option != NULL) {
105         dst = option->peer_addr;
106     }
107 
108     dfx_log_debug("%s in\r\n", __func__);
109     if (zdiag_is_enable() == false) {
110         dfx_log_err("diag_unconnect\r\n");
111         return ERRCODE_FAIL;
112     }
113 
114     if (pkt_cnt > DIAG_PKT_DATA_ID_USR_MAX - 1) {
115         dfx_log_err("[ERROR][%s][%d]\r\n", __func__, __LINE__);
116         return ERRCODE_FAIL;
117     }
118 
119     diag_pkt_handle_init(&pkt, pkt_cnt + 1);
120     if (critical != 0) {
121         diag_pkt_set_critical(&pkt);
122     }
123     diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_0, (uint8_t *)mux, DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE,
124         DIAG_PKT_DATA_STACK);
125     for (int i = 0; i < pkt_cnt; i++) {
126         diag_pkt_handle_set_data(&pkt, (uint8_t)(i + 1), (uint8_t *)packet[i], packet_size[i], DIAG_PKT_DATA_STACK);
127         usr_data_size += packet_size[i];
128     }
129 
130     diag_mk_ind_header(ind, cmd_id, usr_data_size);
131     crc16 = uapi_crc16(crc16, (uint8_t *)ind, DIAG_IND_HEADER_SIZE);
132     for (int i = 0; i < pkt_cnt; i++) {
133         crc16 = uapi_crc16(crc16, packet[i], packet_size[i]);
134     }
135     diag_mk_mux_header_1(mux, MUX_PKT_IND, cmd_id, DIAG_IND_HEADER_SIZE + usr_data_size);
136     diag_mk_mux_header_2(mux, dst, crc16);
137 
138     process_param.cur_proc = DIAG_PKT_PROC_USR_SYNC_CMD_IND;
139     diag_pkt_router(&pkt, &process_param);
140     return ret;
141 }
142 
uapi_diag_report_packets_critical(uint16_t cmd_id,diag_option_t * option,uint8_t ** packet,uint16_t * packet_size,uint8_t pkt_cnt)143 errcode_t uapi_diag_report_packets_critical(uint16_t cmd_id, diag_option_t *option, uint8_t **packet,
144     uint16_t *packet_size, uint8_t pkt_cnt)
145 {
146     diag_report_packet pkt;
147     pkt.packet = packet;
148     pkt.packet_size = packet_size;
149     pkt.pkt_cnt = pkt_cnt;
150     return zdiag_report_packets(cmd_id, option, &pkt, 1);
151 }
152 
uapi_diag_report_packets_normal(uint16_t cmd_id,diag_option_t * option,uint8_t ** packet,uint16_t * packet_size,uint8_t pkt_cnt)153 errcode_t uapi_diag_report_packets_normal(uint16_t cmd_id, diag_option_t *option, uint8_t **packet,
154     uint16_t *packet_size, uint8_t pkt_cnt)
155 {
156     diag_report_packet pkt;
157     pkt.packet = packet;
158     pkt.packet_size = packet_size;
159     pkt.pkt_cnt = pkt_cnt;
160     return zdiag_report_packets(cmd_id, option, &pkt, 0);
161 }
162 
uapi_diag_report_ack(msp_diag_ack_param_t * ack,diag_option_t * option)163 errcode_t uapi_diag_report_ack(msp_diag_ack_param_t *ack, diag_option_t *option)
164 {
165     errcode_t ret;
166     diag_pkt_handle_t pkt;
167     diag_pkt_process_param_t process_param;
168     uint8_t buf[DIAG_MUX_HEADER_SIZE + DIAG_CNF_HEADER_SIZE];
169     msp_mux_packet_head_stru_t *mux = (msp_mux_packet_head_stru_t *)buf;
170     msp_diag_head_cnf_stru_t *cnf = (msp_diag_head_cnf_stru_t *)(buf + DIAG_MUX_HEADER_SIZE);
171     uint16_t crc16 = 0;
172     diag_addr dst = 0;
173     if (option != NULL) {
174         dst = option->peer_addr;
175     }
176 
177     diag_pkt_handle_init(&pkt, 2); /* data_cnt 为2 */
178     diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_0, (uint8_t *)mux, DIAG_MUX_HEADER_SIZE + DIAG_CNF_HEADER_SIZE,
179         DIAG_PKT_DATA_STACK);
180     diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_1, (uint8_t *)ack->param, ack->param_size,
181         DIAG_PKT_DATA_STACK);
182 
183     diag_mk_cnf_header(cnf, ack);
184     crc16 = uapi_crc16(crc16, (uint8_t *)cnf, DIAG_CNF_HEADER_SIZE);
185     crc16 = uapi_crc16(crc16, ack->param, ack->param_size);
186     diag_mk_mux_header_1(mux, MUX_PKT_ACK, ack->cmd_id, DIAG_CNF_HEADER_SIZE + ack->param_size);
187     diag_mk_mux_header_2(mux, dst, crc16);
188 
189     process_param.cur_proc = DIAG_PKT_PROC_USR_SYNC_CMD_IND;
190     ret = diag_pkt_router(&pkt, &process_param);
191     return ret;
192 }
193 
uapi_zdiag_report_sys_msg_instance(uint32_t module_id,uint32_t msg_id,const uint8_t * packet,uint16_t packet_size,uint8_t level)194 errcode_t uapi_zdiag_report_sys_msg_instance(uint32_t module_id, uint32_t msg_id, const uint8_t *packet,
195     uint16_t packet_size, uint8_t level)
196 {
197     errcode_t ret;
198     diag_pkt_handle_t pkt;
199     diag_pkt_process_param_t process_param;
200     uint8_t buf[DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE + DIAG_LOG_HEADER_SIZE];
201     msp_mux_packet_head_stru_t *mux = (msp_mux_packet_head_stru_t *)buf;
202     msp_diag_head_ind_stru_t *ind = (msp_diag_head_ind_stru_t *)(buf + DIAG_MUX_HEADER_SIZE);
203     diag_cmd_log_layer_ind_stru_t *log =
204         (diag_cmd_log_layer_ind_stru_t *)(buf + DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE);
205     uint16_t crc16 = 0;
206 
207     if (zdiag_msg_permit(module_id, level) == false) {
208         return ERRCODE_FAIL;
209     }
210 
211     diag_pkt_handle_init(&pkt, 1); /* data_cnt 为1 */
212     diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_0, (uint8_t *)mux,
213         DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE + DIAG_LOG_HEADER_SIZE, DIAG_PKT_DATA_STACK);
214     if (packet_size != 0) {
215         diag_pkt_cnt_increase(&pkt, 1);
216         diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_1, (uint8_t *)packet, packet_size, DIAG_PKT_DATA_STACK);
217     }
218 
219     diag_mk_ind_header(ind, DIAG_CMD_MSG_RPT_SYS, DIAG_LOG_HEADER_SIZE + packet_size);
220     zdiag_mk_log_pkt(log, module_id, msg_id);
221     crc16 = uapi_crc16(crc16, (uint8_t *)ind, DIAG_IND_HEADER_SIZE);
222     crc16 = uapi_crc16(crc16, (uint8_t *)log, DIAG_LOG_HEADER_SIZE);
223     if (packet_size != 0) {
224         crc16 = uapi_crc16(crc16, packet, packet_size);
225     }
226 
227     diag_mk_mux_header_1(mux, MUX_PKT_IND, DIAG_CMD_MSG_RPT_SYS,
228         DIAG_IND_HEADER_SIZE + DIAG_LOG_HEADER_SIZE + packet_size);
229     diag_mk_mux_header_2(mux, 0, crc16);
230 
231     process_param.cur_proc = DIAG_PKT_PROC_USR_SYNC_CMD_IND;
232 
233 #if CONFIG_DFX_SUPPORT_OFFLINE_LOG_FILE == DFX_YES
234     if (uapi_zdiag_offline_log_is_enable()) {
235         uint32_t data_len = DIAG_LOG_HEADER_SIZE + (uint32_t)packet_size;
236         uint8_t *data = (uint8_t *)dfx_malloc(0, data_len);
237         if (data == NULL) {
238             return ERRCODE_MALLOC;
239         }
240         memcpy_s(data, data_len, log, DIAG_LOG_HEADER_SIZE);
241         memcpy_s(data + DIAG_LOG_HEADER_SIZE, data_len - DIAG_LOG_HEADER_SIZE, packet, (uint32_t)packet_size);
242         ret = uapi_logfile_write(STORE_DIAG, 0, data, data_len);
243         if (ret != ERRCODE_SUCC && ret != ERRCODE_DFX_LOGFILE_SUSPENDED) {
244             printf("offline log wrtie failed, ret = 0x%x\r\n", ret);
245         }
246         dfx_free(0, data);
247     } else {
248         ret = diag_pkt_router(&pkt, &process_param);
249     }
250 #else
251     ret = diag_pkt_router(&pkt, &process_param);
252 #endif
253     return ret;
254 }
255 
uapi_zdiag_report_sys_msg_instance_sn(uint32_t module_id,uint32_t msg_id,diag_report_sys_msg_packet * report_sys_msg_packet,uint8_t level,uint32_t sn)256 errcode_t uapi_zdiag_report_sys_msg_instance_sn(uint32_t module_id, uint32_t msg_id,
257     diag_report_sys_msg_packet *report_sys_msg_packet, uint8_t level, uint32_t sn)
258 {
259     uint8_t *packet = report_sys_msg_packet->packet;
260     uint16_t packet_size = report_sys_msg_packet->packet_size;
261     errcode_t ret;
262     diag_pkt_handle_t pkt;
263     diag_pkt_process_param_t process_param;
264     uint8_t buf[DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE + DIAG_LOG_HEADER_SIZE];
265     msp_mux_packet_head_stru_t *mux = (msp_mux_packet_head_stru_t *)buf;
266     msp_diag_head_ind_stru_t *ind = (msp_diag_head_ind_stru_t *)(buf + DIAG_MUX_HEADER_SIZE);
267     diag_cmd_log_layer_ind_stru_t *log =
268         (diag_cmd_log_layer_ind_stru_t *)(buf + DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE);
269     uint16_t crc16 = 0;
270 
271     if (zdiag_msg_permit(module_id, level) == false) {
272         return ERRCODE_FAIL;
273     }
274 
275     diag_pkt_handle_init(&pkt, 1); /* data_cnt 为1 */
276     diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_0, (uint8_t *)mux,
277         DIAG_MUX_HEADER_SIZE + DIAG_IND_HEADER_SIZE + DIAG_LOG_HEADER_SIZE, DIAG_PKT_DATA_STACK);
278     if (packet_size != 0) {
279         diag_pkt_cnt_increase(&pkt, 1);
280         diag_pkt_handle_set_data(&pkt, DIAG_PKT_DATA_ID_DATA_1, (uint8_t *)packet, packet_size, DIAG_PKT_DATA_STACK);
281     }
282 
283     diag_mk_ind_header(ind, DIAG_CMD_MSG_RPT_SYS, DIAG_LOG_HEADER_SIZE + packet_size);
284     zdiag_mk_log_pkt_sn(log, module_id, msg_id, sn);
285     crc16 = uapi_crc16(crc16, (uint8_t *)ind, DIAG_IND_HEADER_SIZE);
286     crc16 = uapi_crc16(crc16, (uint8_t *)log, DIAG_LOG_HEADER_SIZE);
287     if (packet_size != 0) {
288         crc16 = uapi_crc16(crc16, packet, packet_size);
289     }
290 
291     diag_mk_mux_header_1(mux, MUX_PKT_IND, DIAG_CMD_MSG_RPT_SYS,
292         DIAG_IND_HEADER_SIZE + DIAG_LOG_HEADER_SIZE + packet_size);
293     diag_mk_mux_header_2(mux, 0, crc16);
294 
295     process_param.cur_proc = DIAG_PKT_PROC_USR_SYNC_CMD_IND;
296     ret = diag_pkt_router(&pkt, &process_param);
297     return ret;
298 }
299 
uapi_zdiag_report_packet(uint16_t cmd_id,diag_option_t * option,const uint8_t * packet,uint16_t packet_size,bool sync)300 errcode_t uapi_zdiag_report_packet(uint16_t cmd_id, diag_option_t *option, const uint8_t *packet,
301                                    uint16_t packet_size, bool sync)
302 {
303     return uapi_diag_report_packet(cmd_id, option, packet, packet_size, sync);
304 }