• 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 usr adapt
15  * This file should be changed only infrequently and with great care.
16  */
17 
18 #ifndef DIAG_ADAPT_LAYER_H
19 #define DIAG_ADAPT_LAYER_H
20 
21 #include "time.h"
22 #include "diag_config.h"
23 #include "diag_common.h"
24 #include "diag_channel.h"
25 #include "dfx_adapt_layer.h"
26 
27 #define DIAG_HSO_MAX_MUX_PKT_SIZE 512
28 #define DIAG_HSO_VER_DEFAULT 0
29 #define DIAG_HSO_THIS_VER 0x11
30 
31 #define USER_CMD_LIST_NUM 10 /* Maximum number of non-system command lists */
32 
diag_adapt_get_msg_time(void)33 static inline uint32_t diag_adapt_get_msg_time(void)
34 {
35     return (uint32_t)uapi_systick_get_ms();
36 }
37 
diag_adapt_get_msg_time_ms(void)38 static inline uint64_t diag_adapt_get_msg_time_ms(void)
39 {
40     return uapi_systick_get_ms();
41 }
42 
diag_adapt_get_msg_local_time(void)43 static inline uint32_t diag_adapt_get_msg_local_time(void)
44 {
45     uint64_t time_ms = uapi_systick_get_ms();
46     time_t time_s = (time_t)(time_ms / 1000); /* 1 second = 1000 milliseconds */
47 
48     struct tm tm = { 0 };
49     if (localtime_r(&time_s, &tm) == NULL) {
50         return 0;
51     }
52 
53     uint32_t local_time = ((uint32_t)tm.tm_mon + 1) << 28; /* 28~31 bit: month */
54     /* 1 day = 24 hours, 1 hour = 60 minutes, 1 minute = 60 seconds, 1 second = 100 * 10 milliseconds */
55     local_time += (uint32_t)(((tm.tm_mday * 24 + tm.tm_hour) * 60 + tm.tm_min) * 60 + tm.tm_sec) * 100;
56     local_time += (uint32_t)((time_ms / 10) % 100); /* 1 second = 100 * 10 milliseconds */
57     return local_time;
58 }
59 
diag_adapt_sync_tx_sem_wait(void)60 static inline errcode_t diag_adapt_sync_tx_sem_wait(void)
61 {
62     return ERRCODE_FAIL;
63 }
64 
diag_adapt_sync_tx_sem_signal(void)65 static inline void diag_adapt_sync_tx_sem_signal(void)
66 {
67     return;
68 }
69 
70 #ifdef SUPPORT_DIAG_V2_PROTOCOL
diag_adapt_get_default_dst(void)71 static inline uint8_t diag_adapt_get_default_dst(void)
72 {
73     return DIAG_FRAME_FID_UART;
74 }
75 
diag_adapt_addr_2_channel_id(diag_frame_fid_t addr)76 static inline diag_channel_id_t diag_adapt_addr_2_channel_id(diag_frame_fid_t addr)
77 {
78     if (addr == DIAG_FRAME_FID_UART) {
79         return DIAG_CHANNEL_ID_0;
80     } else if (addr == DIAG_FRAME_FID_BLE_GATT) {
81         return DIAG_CHANNEL_ID_1;
82     }
83     return DIAG_CHANNEL_ID_INVALID;
84 }
85 
diag_adapt_channel_id_2_addr(diag_channel_id_t id)86 static inline diag_frame_fid_t diag_adapt_channel_id_2_addr(diag_channel_id_t id)
87 {
88     if (id == DIAG_CHANNEL_ID_0) {
89         return DIAG_FRAME_FID_UART;
90     } else if (id == DIAG_CHANNEL_ID_1) {
91         return DIAG_FRAME_FID_BLE_GATT;
92     }
93     return DIAG_FRAME_FID_MAX;
94 }
95 
diag_adapt_get_local_addr(void)96 static inline diag_frame_fid_t diag_adapt_get_local_addr(void)
97 {
98     return DIAG_FRAME_FID_LOCAL;
99 }
100 
101 #else /* SUPPORT_DIAG_V2_PROTOCOL */
102 
103 #define DIAG_LOCAL_ADDR             100
104 #define DIAG_UART_CONNECT_HSO_ADDR  200
105 
diag_adapt_get_default_dst(void)106 static inline uint8_t diag_adapt_get_default_dst(void)
107 {
108     return DIAG_UART_CONNECT_HSO_ADDR;
109 }
110 
diag_adapt_dst_2_channel_id(diag_addr addr)111 static inline diag_channel_id_t diag_adapt_dst_2_channel_id(diag_addr addr)
112 {
113     if (addr == DIAG_UART_CONNECT_HSO_ADDR) {
114         return DIAG_CHANNEL_ID_0;
115     }
116     return DIAG_CHANNEL_ID_INVALID;
117 }
118 
diag_adapt_get_local_addr(void)119 static inline diag_addr diag_adapt_get_local_addr(void)
120 {
121     return DIAG_LOCAL_ADDR;
122 }
123 
diag_adapt_addr_2_attribute(diag_addr addr)124 static inline diag_addr_attribute_t diag_adapt_addr_2_attribute(diag_addr addr)
125 {
126     if (addr == DIAG_UART_CONNECT_HSO_ADDR) {
127         return DIAG_ADDR_ATTRIBUTE_VALID | DIAG_ADDR_ATTRIBUTE_HSO_CONNECT;
128     } else if (addr == DIAG_LOCAL_ADDR) {
129         return DIAG_ADDR_ATTRIBUTE_VALID;
130     } else {
131         return 0;
132     }
133 }
134 #endif /* SUPPORT_DIAG_V2_PROTOCOL */
135 
diag_adapt_is_in_unblocking_context(void)136 static inline bool diag_adapt_is_in_unblocking_context(void)
137 {
138     return false;
139 }
140 
141 #endif /* DIAG_ADAPT_LAYER_H */
142