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 */
15
16 #include "diag_stat.h"
17 #include "diag_common.h"
18 #include "diag_adapt_layer.h"
19 #include "diag.h"
20 #include "errcode.h"
21
22 zdiag_stat_ctrl_t g_diag_stat_ctrl;
23
diag_get_stat_ctrl(void)24 STATIC zdiag_stat_ctrl_t *diag_get_stat_ctrl(void)
25 {
26 return &g_diag_stat_ctrl;
27 }
28
uapi_diag_register_stat_obj(const diag_sys_stat_obj_t * stat_obj_tbl,uint16_t obj_num)29 errcode_t uapi_diag_register_stat_obj(const diag_sys_stat_obj_t *stat_obj_tbl, uint16_t obj_num)
30 {
31 errcode_t ret = ERRCODE_FAIL;
32 uint32_t lock_stat;
33 zdiag_stat_ctrl_t *stat_ctrl;
34 uint16_t i;
35
36 stat_ctrl = diag_get_stat_ctrl();
37 lock_stat = dfx_int_lock();
38
39 for (i = 0; i < CONFIG_STAT_CMD_LIST_NUM; i++) {
40 if ((stat_ctrl->stat_cmd_list[i] == NULL) || (stat_ctrl->aus_stat_cmd_num[i] == 0)) {
41 stat_ctrl->stat_cmd_list[i] = stat_obj_tbl;
42 stat_ctrl->aus_stat_cmd_num[i] = obj_num;
43 ret = ERRCODE_SUCC;
44 goto end;
45 }
46 }
47 ret = ERRCODE_FAIL;
48 end:
49 dfx_int_restore(lock_stat);
50 return ret;
51 }
52
zdiag_report_stat_obj(zdiag_report_stat_obj_stru_t pkt)53 errcode_t zdiag_report_stat_obj(zdiag_report_stat_obj_stru_t pkt)
54 {
55 errcode_t ret = ERRCODE_FAIL;
56 uint16_t m;
57 void *obj = NULL;
58
59 for (m = 0; m < pkt.obj_cnt; m++) {
60 obj = (void *)((uint8_t *)pkt.object + m * pkt.obj_size);
61 ret = uapi_diag_report_packet(pkt.obj_id, pkt.option, (const uint8_t *)obj, pkt.obj_size, pkt.sync);
62 if (ret != ERRCODE_SUCC) {
63 break;
64 }
65 }
66
67 return ret;
68 }
69
zdiag_query_stat_obj(uint32_t id,uint32_t * obj,uint16_t * obj_len,uint16_t * obj_cnt)70 errcode_t zdiag_query_stat_obj(uint32_t id, uint32_t *obj, uint16_t *obj_len, uint16_t *obj_cnt)
71 {
72 errcode_t ret = ERRCODE_FAIL;
73 uint32_t n;
74 uint32_t k;
75 zdiag_stat_ctrl_t *ctx = diag_get_stat_ctrl();
76
77 for (n = 0; n < CONFIG_STAT_CMD_LIST_NUM; n++) {
78 if (ctx->stat_cmd_list[n] == NULL || ctx->aus_stat_cmd_num[n] == 0) {
79 dfx_log_err("stat_cmd_list is null or cmd_num is 0\r\n");
80 return ERRCODE_FAIL;
81 }
82
83 for (k = 0; k < ctx->aus_stat_cmd_num[n]; k++) {
84 const diag_sys_stat_obj_t *tbl = ctx->stat_cmd_list[n];
85 const diag_sys_stat_obj_t *node = &tbl[k];
86 if ((uint16_t)node->id != id) {
87 continue;
88 }
89 dfx_log_err("node->id=0x%x\r\n", node->id);
90 if (obj != NULL && obj_len != NULL && obj_cnt != NULL) {
91 *obj = (uint32_t)(uintptr_t)node->stat_packet; /* asume the address is 4bytes. */
92 *obj_len = (uint16_t)node->stat_packet_size;
93 *obj_cnt = node->array_cnt;
94 }
95
96 return ERRCODE_SUCC;
97 }
98 }
99
100 return ret;
101 }
102