1 /*
2 * Copyright (c) 2020 Huawei Device Co., Ltd.
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 "hiview_util.h"
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <time.h>
26
27 #include "cmsis_os.h"
28
29 #if defined(CHIP_VER_Hi3861) || \
30 defined(CHIP_VER_Hi3861L) || \
31 defined(CHIP_VER_Hi3881)
32 #include "los_hwi.h"
33 #else
34 #include "../../../kernel/liteos_m/arch/include/los_interrupt.h"
35 #endif
36
37 #define HIVIEW_WAIT_FOREVER osWaitForever
38 #define HIVIEW_MS_PER_SECOND 1000
39 #define HIVIEW_NS_PER_MILLISECOND 1000000
40 #define BUFFER_SIZE 128
41
42 static uint64 HIVIEW_GetCurrentTimeDef(void);
43 static void HIVIEW_UartPrintDef(const char *str);
44 static int (*hiview_open)(const char *, int, ...) = open;
45 static int (*hiview_close)(int) = close;
46 static ssize_t (*hiview_read)(int, void *, size_t) = read;
47 static ssize_t (*hiview_write)(int, const void *, size_t) = write;
48 static off_t (*hiview_lseek)(int, off_t, int) = lseek;
49 static int (*hiview_fsync)(int) = fsync;
50 static int (*hiview_unlink)(const char *) = unlink;
51 static int (*hiview_rename)(const char *, const char *) = NULL; // not all platform support rename
52 static uint64 (*hiview_get_time)(void) = HIVIEW_GetCurrentTimeDef;
53 static void (*hiview_uart_print)(const char *) = HIVIEW_UartPrintDef;
54
HIVIEW_MemAlloc(uint8 modId,uint32 size)55 void *HIVIEW_MemAlloc(uint8 modId, uint32 size)
56 {
57 (void)modId;
58 return malloc(size);
59 }
60
HIVIEW_MemFree(uint8 modId,void * pMem)61 void HIVIEW_MemFree(uint8 modId, void *pMem)
62 {
63 (void)modId;
64 free(pMem);
65 }
66
HIVIEW_GetCurrentTimeDef(void)67 static uint64 HIVIEW_GetCurrentTimeDef(void)
68 {
69 struct timespec current = {0};
70 int ret = clock_gettime(CLOCK_REALTIME, ¤t);
71 if (ret != 0) {
72 return 0;
73 }
74 return (uint64)current.tv_sec * HIVIEW_MS_PER_SECOND + (uint64)current.tv_nsec / HIVIEW_NS_PER_MILLISECOND;
75 }
76
HIVIEW_GetCurrentTime(void)77 uint64 HIVIEW_GetCurrentTime(void)
78 {
79 return hiview_get_time();
80 }
81
HIVIEW_RtcGetCurrentTime(uint64 * val,HIVIEW_RtcTime * time)82 int32 HIVIEW_RtcGetCurrentTime(uint64 *val, HIVIEW_RtcTime *time)
83 {
84 (void)val;
85 (void)time;
86 return OHOS_SUCCESS;
87 }
88
HIVIEW_MutexInit()89 HiviewMutexId_t HIVIEW_MutexInit()
90 {
91 return (HiviewMutexId_t)osMutexNew(NULL);
92 }
93
HIVIEW_MutexLock(HiviewMutexId_t mutex)94 int32 HIVIEW_MutexLock(HiviewMutexId_t mutex)
95 {
96 if (mutex == NULL) {
97 return -1;
98 }
99 return (int32)osMutexAcquire((osMutexId_t)mutex, HIVIEW_WAIT_FOREVER);
100 }
101
HIVIEW_MutexLockOrWait(HiviewMutexId_t mutex,uint32 timeout)102 int32 HIVIEW_MutexLockOrWait(HiviewMutexId_t mutex, uint32 timeout)
103 {
104 if (mutex == NULL) {
105 return -1;
106 }
107 return (int32)osMutexAcquire((osMutexId_t)mutex, timeout);
108 }
109
HIVIEW_MutexUnlock(HiviewMutexId_t mutex)110 int32 HIVIEW_MutexUnlock(HiviewMutexId_t mutex)
111 {
112 if (mutex == NULL) {
113 return -1;
114 }
115 return (int32)osMutexRelease((osMutexId_t)mutex);
116 }
117
HIVIEW_IntLock()118 uint32 HIVIEW_IntLock()
119 {
120 return LOS_IntLock();
121 }
122
HIVIEW_IntRestore(uint32 intSave)123 void HIVIEW_IntRestore(uint32 intSave)
124 {
125 LOS_IntRestore(intSave);
126 }
127
HIVIEW_GetTaskId()128 uint32 HIVIEW_GetTaskId()
129 {
130 return (uint32)osThreadGetId();
131 }
132
HIVIEW_UartPrintDef(const char * str)133 static void HIVIEW_UartPrintDef(const char *str)
134 {
135 printf("%s\n", str);
136 }
137
HIVIEW_UartPrint(const char * str)138 void HIVIEW_UartPrint(const char *str)
139 {
140 hiview_uart_print(str);
141 }
142
HIVIEW_Sleep(uint32 ms)143 void HIVIEW_Sleep(uint32 ms)
144 {
145 osDelay(ms / HIVIEW_MS_PER_SECOND);
146 }
147
HIVIEW_InitHook(HIVIEW_Hooks * hooks)148 void HIVIEW_InitHook(HIVIEW_Hooks *hooks)
149 {
150 if (hooks == NULL) {
151 // reset
152 hiview_open = open;
153 hiview_close = close;
154 hiview_read = read;
155 hiview_write = write;
156 hiview_lseek = lseek;
157 hiview_fsync = fsync;
158 hiview_unlink = unlink;
159 hiview_rename = NULL;
160 hiview_get_time = HIVIEW_GetCurrentTimeDef;
161 hiview_uart_print = HIVIEW_UartPrintDef;
162 return;
163 }
164 hiview_open = (hooks->open_fn) == NULL ? open : hooks->open_fn;
165 hiview_close = (hooks->close_fn) == NULL ? close : hooks->close_fn;
166 hiview_read = (hooks->read_fn) == NULL ? read : hooks->read_fn;
167 hiview_write = (hooks->write_fn) == NULL ? write : hooks->write_fn;
168 hiview_lseek = (hooks->lseek_fn) == NULL ? lseek : hooks->lseek_fn;
169 hiview_fsync = (hooks->fsync_fn) == NULL ? fsync : hooks->fsync_fn;
170 hiview_unlink = (hooks->unlink_fn) == NULL ? unlink : hooks->unlink_fn;
171 hiview_rename = hooks->rename_fn;
172 hiview_get_time = (hooks->hiview_get_time_fn) == NULL ? HIVIEW_GetCurrentTimeDef : hooks->hiview_get_time_fn;
173 hiview_uart_print = (hooks->hiview_uart_print_fn) == NULL ? HIVIEW_UartPrintDef : hooks->hiview_uart_print_fn;
174 }
175
HIVIEW_FileOpen(const char * path)176 int32 HIVIEW_FileOpen(const char *path)
177 {
178 int32 handle = hiview_open(path, O_RDWR | O_CREAT, 0);
179 if (handle < 0) {
180 printf("HIVIEW_FileOpen %s fail, errno:%d\n", path, errno);
181 }
182 return handle;
183 }
184
HIVIEW_FileClose(int32 handle)185 int32 HIVIEW_FileClose(int32 handle)
186 {
187 if (handle < 0) {
188 return -1;
189 }
190 return hiview_close(handle);
191 }
192
HIVIEW_FileRead(int32 handle,uint8 * buf,uint32 len)193 int32 HIVIEW_FileRead(int32 handle, uint8 *buf, uint32 len)
194 {
195 if (handle < 0) {
196 return -1;
197 }
198 return hiview_read(handle, (char *)buf, len);
199 }
200
HIVIEW_FileWrite(int32 handle,const uint8 * buf,uint32 len)201 int32 HIVIEW_FileWrite(int32 handle, const uint8 *buf, uint32 len)
202 {
203 if (handle < 0) {
204 return -1;
205 }
206 return hiview_write(handle, (const char *)buf, len);
207 }
208
HIVIEW_FileSeek(int32 handle,int32 offset,int32 whence)209 int32 HIVIEW_FileSeek(int32 handle, int32 offset, int32 whence)
210 {
211 if (handle < 0) {
212 return -1;
213 }
214 return hiview_lseek(handle, (off_t)offset, whence);
215 }
216
HIVIEW_FileSize(int32 handle)217 int32 HIVIEW_FileSize(int32 handle)
218 {
219 if (handle < 0) {
220 return -1;
221 }
222 return hiview_lseek(handle, 0, SEEK_END);
223 }
224
HIVIEW_FileSync(int32 handle)225 int32 HIVIEW_FileSync(int32 handle)
226 {
227 if (handle < 0) {
228 return -1;
229 }
230 return hiview_fsync(handle);
231 }
232
HIVIEW_FileUnlink(const char * path)233 int32 HIVIEW_FileUnlink(const char *path)
234 {
235 return hiview_unlink(path);
236 }
237
HIVIEW_FileCopy(const char * src,const char * dest)238 int32 HIVIEW_FileCopy(const char *src, const char *dest)
239 {
240 if (src == NULL || dest == NULL) {
241 HIVIEW_UartPrint("HIVIEW_FileCopy input param is NULL");
242 return -1;
243 }
244 int32 fdSrc = hiview_open(src, O_RDONLY, 0);
245 if (fdSrc < 0) {
246 HIVIEW_UartPrint("HIVIEW_FileCopy open src file fail");
247 return fdSrc;
248 }
249 int32 fdDest = hiview_open(dest, O_RDWR | O_CREAT | O_TRUNC, 0);
250 if (fdDest < 0) {
251 HIVIEW_UartPrint("HIVIEW_FileCopy open dest file fail");
252 HIVIEW_FileClose(fdSrc);
253 return fdDest;
254 }
255 boolean copyFailed = TRUE;
256 uint8 *dataBuf = (uint8 *)HIVIEW_MemAlloc(MEM_POOL_HIVIEW_ID, BUFFER_SIZE);
257 if (dataBuf == NULL) {
258 HIVIEW_UartPrint("HIVIEW_FileCopy malloc error");
259 goto MALLOC_ERROR;
260 }
261 int32 nLen = HIVIEW_FileRead(fdSrc, dataBuf, BUFFER_SIZE);
262 while (nLen > 0) {
263 if (HIVIEW_FileWrite(fdDest, dataBuf, nLen) != nLen) {
264 goto EXIT;
265 }
266 nLen = HIVIEW_FileRead(fdSrc, dataBuf, BUFFER_SIZE);
267 }
268 copyFailed = (nLen < 0);
269
270 EXIT:
271 free(dataBuf);
272 MALLOC_ERROR:
273 HIVIEW_FileClose(fdSrc);
274 HIVIEW_FileClose(fdDest);
275 if (copyFailed) {
276 HIVIEW_UartPrint("HIVIEW_FileCopy copy failed");
277 HIVIEW_FileUnlink(dest);
278 return -1;
279 }
280
281 return 0;
282 }
283
HIVIEW_FileMove(const char * src,const char * dest)284 int32 HIVIEW_FileMove(const char *src, const char *dest)
285 {
286 if (hiview_rename != NULL) {
287 return hiview_rename(src, dest);
288 }
289 int32 ret = HIVIEW_FileCopy(src, dest);
290 if (HIVIEW_FileUnlink(src) != 0 || ret != 0) {
291 return -1;
292 }
293 return 0;
294 }
295
Change32Endian(uint32 num)296 uint32 Change32Endian(uint32 num)
297 {
298 unsigned char *buffer = (unsigned char *)#
299 uint32 newEndian = (buffer[3] & 0xFF); // 3: forth char
300 newEndian |= ((buffer[2] << 8) & 0xFF00); // 2: third char, 8: 1 byte length
301 newEndian |= ((buffer[1] << 16) & 0xFF0000); // 16: 2 byte length
302 newEndian |= ((buffer[0] << 24) & 0xFF000000); // 24: 3 byte length
303 return newEndian;
304 }
305
Change16Endian(uint16 num)306 uint16 Change16Endian(uint16 num)
307 {
308 unsigned char* buffer = (unsigned char*)#
309 uint16 newEndian = (buffer[1] & 0xFF);
310 newEndian |= ((buffer[0] << 8) & 0xFF00); // 8: 1 byte length
311 return newEndian;
312 }
313