• 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: diag channel
15  * This file should be changed only infrequently and with great care.
16  */
17 #include "diag_channel_item.h"
18 #include "securec.h"
19 #include "zdiag_mem.h"
20 #include "diag_config.h"
21 #include "diag_adapt_layer.h"
22 #include "errcode.h"
23 
24 typedef struct {
25     diag_channel_item_t item[DIAG_SUPPORT_CHANNEL_CNT];
26 } diag_channel_ctrl_t;
27 
28 diag_channel_ctrl_t g_diag_channel_ctrl = {
29     .item[0].init = 0,
30 };
31 
diag_get_channel_ctrl(void)32 STATIC diag_channel_ctrl_t *diag_get_channel_ctrl(void)
33 {
34     return &g_diag_channel_ctrl;
35 }
36 
diag_chan_idx_2_item(diag_channel_id_t id)37 diag_channel_item_t *diag_chan_idx_2_item(diag_channel_id_t id)
38 {
39     diag_channel_ctrl_t *chan_ctrl = diag_get_channel_ctrl();
40     return &chan_ctrl->item[id];
41 }
42 
soc_diag_channel_set_notify_hook(diag_channel_id_t id,diag_channel_notify_hook hook)43 errcode_t soc_diag_channel_set_notify_hook(diag_channel_id_t id, diag_channel_notify_hook hook)
44 {
45     diag_channel_item_t *item = diag_chan_idx_2_item(id);
46     if (item == NULL) {
47         return ERRCODE_FAIL;
48     }
49 
50     item->notify_hook = hook;
51     return ERRCODE_SUCC;
52 }
53 
soc_diag_channel_set_tx_hook(diag_channel_id_t id,diag_channel_tx_hook hook)54 errcode_t soc_diag_channel_set_tx_hook(diag_channel_id_t id, diag_channel_tx_hook hook)
55 {
56     diag_channel_item_t *item = diag_chan_idx_2_item(id);
57     if (item == NULL) {
58         return ERRCODE_FAIL;
59     }
60 
61     item->tx_hook = hook;
62     return ERRCODE_SUCC;
63 }
64 
soc_diag_channel_set_connect_hso_addr(diag_channel_id_t id,uint8_t hso_addr)65 errcode_t soc_diag_channel_set_connect_hso_addr(diag_channel_id_t id, uint8_t hso_addr)
66 {
67     diag_channel_item_t *item = diag_chan_idx_2_item(id);
68     if (item == NULL) {
69         return ERRCODE_FAIL;
70     }
71     item->hso_addr = hso_addr;
72     return ERRCODE_SUCC;
73 }
74 
zdiag_dst_2_chan(uint8_t addr)75 diag_channel_item_t *zdiag_dst_2_chan(uint8_t addr)
76 {
77     diag_channel_id_t channel_id = diag_adapt_dst_2_channel_id(addr);
78     diag_channel_item_t *item = diag_chan_idx_2_item(channel_id);
79     return item;
80 }
81 
soc_diag_channel_init(diag_channel_id_t id,uint32_t attribute)82 errcode_t soc_diag_channel_init(diag_channel_id_t id, uint32_t attribute)
83 {
84     diag_channel_item_t *item = diag_chan_idx_2_item(id);
85     if (item == NULL || item->init == true) {
86         return ERRCODE_FAIL;
87     }
88 
89     if ((attribute & SOC_DIAG_CHANNEL_ATTR_NEED_RX_BUF) != 0) {
90         item->rx_buf_len = CONFIG_DIAG_RX_BUF_SIZE;
91         item->rx_buf_pos = 0;
92         item->rx_buf_is_using = false;
93         item->rx_buf = dfx_malloc(0, item->rx_buf_len);
94         if (item->rx_buf == NULL) {
95             return ERRCODE_FAIL;
96         }
97     }
98     return ERRCODE_SUCC;
99 }
100