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: diag ind process
15 * This file should be changed only infrequently and with great care.
16 */
17
18 #include "zdiag_ind_dst.h"
19 #include "diag.h"
20 #include "diag_adapt_layer.h"
21 #include "errcode.h"
22
23 typedef struct {
24 const diag_cmd_reg_obj_t *user_cmd_list[CONFIG_DIAG_IND_TBL_NUM];
25 uint16_t aus_usert_cmd_num[CONFIG_DIAG_IND_TBL_NUM]; /* cmd obj num */
26 } diag_ind_ctrl_t;
27
28 STATIC diag_ind_ctrl_t g_diag_ind_ctrl;
29
diag_get_ind_ctrl(void)30 STATIC inline diag_ind_ctrl_t *diag_get_ind_ctrl(void)
31 {
32 return &g_diag_ind_ctrl;
33 }
34
diag_find_usr_ind_proc_func(uint32_t cmd_id)35 STATIC diag_cmd_f diag_find_usr_ind_proc_func(uint32_t cmd_id)
36 {
37 diag_ind_ctrl_t *cmd_ctrl = diag_get_ind_ctrl();
38 uint32_t i;
39 uint16_t k;
40 for (i = 0; i < CONFIG_DIAG_IND_TBL_NUM; i++) {
41 for (k = 0; k < cmd_ctrl->aus_usert_cmd_num[i]; k++) {
42 const diag_cmd_reg_obj_t *cmd_tbl = cmd_ctrl->user_cmd_list[i];
43 const diag_cmd_reg_obj_t *cmd_list = &cmd_tbl[k];
44
45 if ((cmd_id >= cmd_list->min_id) && (cmd_id <= cmd_list->max_id)) {
46 diag_cmd_f cmd;
47 cmd = (diag_cmd_f)cmd_list->fn_input_cmd;
48 return cmd;
49 }
50 }
51 }
52 return NULL;
53 }
54
diag_pkt_router_run_ind(diag_pkt_handle_t * pkt,const diag_option_t * option)55 errcode_t diag_pkt_router_run_ind(diag_pkt_handle_t *pkt, const diag_option_t *option)
56 {
57 msp_diag_head_ind_stru_t *ind_head = diag_receive_pkt_handle_get_ind(pkt);
58 diag_option_t new_option;
59 uint8_t *usr_data = diag_receive_pkt_handle_get_ind_data(pkt);
60 diag_cmd_f cmd_f = diag_find_usr_ind_proc_func(ind_head->cmd_id);
61 if (cmd_f) {
62 new_option = *option;
63 cmd_f(ind_head->cmd_id, usr_data, ind_head->param_size, &new_option);
64 return ERRCODE_SUCC;
65 }
66
67 return ERRCODE_FAIL;
68 }
69
diag_ind_tbl_check(const diag_cmd_reg_obj_t * cmd_tbl,uint16_t cmd_num)70 STATIC errcode_t diag_ind_tbl_check(const diag_cmd_reg_obj_t *cmd_tbl, uint16_t cmd_num)
71 {
72 unused(cmd_tbl);
73 unused(cmd_num);
74
75 return ERRCODE_SUCC;
76 }
77
uapi_diag_register_ind(const diag_cmd_reg_obj_t * cmd_tbl,uint16_t cmd_num)78 errcode_t uapi_diag_register_ind(const diag_cmd_reg_obj_t *cmd_tbl, uint16_t cmd_num)
79 {
80 errcode_t ret;
81 uint32_t lock_stat;
82 int i;
83 diag_ind_ctrl_t *cmd_ctrl = diag_get_ind_ctrl();
84
85 lock_stat = dfx_int_lock();
86 ret = diag_ind_tbl_check(cmd_tbl, cmd_num);
87 if (ret != ERRCODE_SUCC) {
88 goto end;
89 }
90
91 for (i = 0; i < CONFIG_DIAG_IND_TBL_NUM; i++) {
92 if ((cmd_ctrl->user_cmd_list[i] == NULL) || (cmd_ctrl->aus_usert_cmd_num[i] == 0)) {
93 cmd_ctrl->user_cmd_list[i] = cmd_tbl;
94 cmd_ctrl->aus_usert_cmd_num[i] = cmd_num;
95 ret = ERRCODE_SUCC;
96 goto end;
97 }
98 }
99 ret = ERRCODE_FAIL;
100 end:
101 dfx_int_restore(lock_stat);
102 return ret;
103 }
104