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