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 for sample data.
15 */
16
17 #include "securec.h"
18 #include "diag.h"
19 #include "soc_osal.h"
20 #include "soc_diag_util.h"
21 #include "soc_diag_cmd_id.h"
22 #include "diag_sample_data_st.h"
23 #include "dfx_adapt_layer.h"
24 #include "diag_sample_data.h"
25
26 #undef THIS_FILE_ID
27 #define THIS_FILE_ID DIAG_FILE_ID_DIAG_SAMPLE_DATA_C
28
29 #undef THIS_MOD_ID
30 #define THIS_MOD_ID DIAG_MOD_ID_OAM_HOST
31
32 #define DIAG_SAMPLE_TRANSMIT_FINISH 11
33
34 diag_sample_func_cb g_diag_sample_cb[ZDIAG_SAMPLE_CB_END] = {NULL};
35 diag_sample_record g_sample_running = {0};
36
diag_cmd_wlan_module_sample_data(uint16_t cmd_id,void * cmd_param,uint16_t cmd_param_size,diag_option_t * option)37 errcode_t diag_cmd_wlan_module_sample_data(uint16_t cmd_id, void *cmd_param, uint16_t cmd_param_size,
38 diag_option_t *option)
39 {
40 diag_btsoc_sample_cmd *cmd = (diag_btsoc_sample_cmd *)cmd_param;
41 uint32_t idx = cmd_id - DIAG_CMD_ID_BGLE_SAMPLE;
42 diag_sample_data_ind_t ack = {ERRCODE_FAIL, 0, 0};
43 diag_sample_func_cb func_cb = (diag_sample_func_cb)g_diag_sample_cb[idx];
44
45 if (func_cb == NULL) {
46 osal_printk("diag_cmd_wlan_module_sample_data:sample func_cb is NULL[%d].", idx);
47 goto report_ack;
48 }
49
50 osal_printk("flag:%d, transmit_id:%d, sample_size:%d, sample_type:%d,msg_cnt:%d.\n",
51 cmd->flag, cmd->transmit_id, cmd->sample_size, cmd->sample_type, g_sample_running.msg_cnt);
52
53 if (g_sample_running.running == false && cmd->flag == ZDIAG_SAMPLE_START) { /* start */
54 g_sample_running.running = true;
55 ack.ret = func_cb(cmd_param, cmd_param_size);
56 } else if (g_sample_running.running == true && cmd->flag == ZDIAG_SAMPLE_STOP && /* stop */
57 g_sample_running.msg_cnt == 0) {
58 g_sample_running.running = false;
59 ack.ret = func_cb(cmd_param, cmd_param_size);
60 } else {
61 osal_printk("diag_cmd_wlan_module_sample_data:sample_running:%d, sample_start:%d, msg_cnt:%d.\n",
62 g_sample_running.running, cmd->flag, g_sample_running.msg_cnt);
63 goto report_ack;
64 }
65
66 g_sample_running.cmd_id = cmd_id;
67 g_sample_running.transmit_id = cmd->transmit_id;
68 g_sample_running.offset = 0;
69 g_sample_running.msg_cnt = 0;
70
71 report_ack:
72 ack.flag = cmd->flag;
73 ack.transmit_id = cmd->transmit_id;
74 uapi_diag_report_packet(cmd_id, option, (uint8_t *)&ack, sizeof(diag_sample_data_ind_t), true);
75 return ERRCODE_SUCC;
76 }
77
diag_cmd_report_data(uint8_t * buf,uint32_t len)78 STATIC errcode_t diag_cmd_report_data(uint8_t *buf, uint32_t len)
79 {
80 diag_sample_data_reply_pkt_t *reply = NULL;
81 uint32_t reply_size = (uint32_t)sizeof(diag_sample_data_reply_pkt_t) + len;
82 reply = (diag_sample_data_reply_pkt_t *)dfx_malloc(0, reply_size);
83 if (reply == NULL || buf == NULL) {
84 return ERRCODE_FAIL;
85 }
86 g_sample_running.offset += len;
87 reply->transmit_id = g_sample_running.transmit_id;
88 reply->offset = g_sample_running.offset;
89 reply->size = len;
90 reply->ret = ERRCODE_SUCC;
91 if (memcpy_s(reply->data, len, buf, len) != EOK) {
92 reply->ret = ERRCODE_FAIL;
93 osal_printk("diag_cmd_report_data:memcpy_s error, len:%d.\n", len);
94 }
95 uapi_diag_report_packet(DIAG_CMD_ID_SAMPLE_DATA, NULL, (uint8_t *)reply, (uint16_t)reply_size, true);
96 dfx_free(0, reply);
97 return ERRCODE_SUCC;
98 }
99
diag_cmd_report_finish(void)100 STATIC errcode_t diag_cmd_report_finish(void)
101 {
102 diag_sample_notify finish = {0};
103 g_sample_running.running = false;
104 finish.transmit_id = g_sample_running.transmit_id;
105 finish.state_code = DIAG_SAMPLE_TRANSMIT_FINISH;
106 osal_printk("diag_cmd_report_finish:transmit_id:%d, report_msg:%d.\n",
107 finish.transmit_id, g_sample_running.msg_cnt);
108 uapi_diag_report_packet(DIAG_CMD_ID_SAMPLE_FINISH, NULL, (uint8_t *)&finish, sizeof(diag_sample_notify), true);
109 return ERRCODE_SUCC;
110 }
111
diag_report_wlan_sample_data(const uint8_t * buf,uint32_t len,uint32_t msg_id)112 errcode_t diag_report_wlan_sample_data(const uint8_t *buf, uint32_t len, uint32_t msg_id)
113 {
114 if (len == 0) {
115 return diag_cmd_report_finish();
116 }
117 g_sample_running.msg_cnt++;
118 return uapi_diag_report_sys_msg(THIS_MOD_ID, msg_id, buf, (uint16_t)len, DIAG_LOG_LEVEL_WARN);
119 }
120
diag_cmd_report_sample_data(uint8_t * buf,uint32_t len)121 errcode_t diag_cmd_report_sample_data(uint8_t *buf, uint32_t len)
122 {
123 if (len == 0) {
124 return diag_cmd_report_finish();
125 }
126 g_sample_running.msg_cnt++;
127 return diag_cmd_report_data(buf, len);
128 }
129
diag_sample_data_register(diag_sample_cb_enum idx,diag_sample_func_cb func)130 void diag_sample_data_register(diag_sample_cb_enum idx, diag_sample_func_cb func)
131 {
132 if (idx >= ZDIAG_SAMPLE_CB_END) {
133 return;
134 }
135 g_diag_sample_cb[idx] = func;
136 }
137
138