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