16f2c2defSopenharmony_ci/*
26f2c2defSopenharmony_ci * Copyright (c) 2020 Huawei Device Co., Ltd.
36f2c2defSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
46f2c2defSopenharmony_ci * you may not use this file except in compliance with the License.
56f2c2defSopenharmony_ci * You may obtain a copy of the License at
66f2c2defSopenharmony_ci *
76f2c2defSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
86f2c2defSopenharmony_ci *
96f2c2defSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
106f2c2defSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
116f2c2defSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
126f2c2defSopenharmony_ci * See the License for the specific language governing permissions and
136f2c2defSopenharmony_ci * limitations under the License.
146f2c2defSopenharmony_ci */
156f2c2defSopenharmony_ci
166f2c2defSopenharmony_ci#include "hiview_util.h"
176f2c2defSopenharmony_ci
186f2c2defSopenharmony_ci#include <errno.h>
196f2c2defSopenharmony_ci#include <fcntl.h>
206f2c2defSopenharmony_ci#include <string.h>
216f2c2defSopenharmony_ci#include <stdio.h>
226f2c2defSopenharmony_ci#include <stdlib.h>
236f2c2defSopenharmony_ci#include <sys/stat.h>
246f2c2defSopenharmony_ci#include <sys/types.h>
256f2c2defSopenharmony_ci#include <time.h>
266f2c2defSopenharmony_ci
276f2c2defSopenharmony_ci#include "cmsis_os.h"
286f2c2defSopenharmony_ci
296f2c2defSopenharmony_ci#if defined(CHIP_VER_Hi3861) || \
306f2c2defSopenharmony_ci    defined(CHIP_VER_Hi3861L) || \
316f2c2defSopenharmony_ci    defined(CHIP_VER_Hi3881)
326f2c2defSopenharmony_ci#include "los_hwi.h"
336f2c2defSopenharmony_ci#else
346f2c2defSopenharmony_ci#include "../../../kernel/liteos_m/arch/include/los_interrupt.h"
356f2c2defSopenharmony_ci#endif
366f2c2defSopenharmony_ci
376f2c2defSopenharmony_ci#define HIVIEW_WAIT_FOREVER           osWaitForever
386f2c2defSopenharmony_ci#define HIVIEW_MS_PER_SECOND          1000
396f2c2defSopenharmony_ci#define HIVIEW_NS_PER_MILLISECOND     1000000
406f2c2defSopenharmony_ci#define BUFFER_SIZE                   128
416f2c2defSopenharmony_ci
426f2c2defSopenharmony_cistatic int (*hiview_open)(const char *, int, ...)  = open;
436f2c2defSopenharmony_cistatic int (*hiview_close)(int) = close;
446f2c2defSopenharmony_cistatic ssize_t (*hiview_read)(int, void *, size_t) = read;
456f2c2defSopenharmony_cistatic ssize_t (*hiview_write)(int, const void *, size_t) = write;
466f2c2defSopenharmony_cistatic off_t (*hiview_lseek)(int, off_t, int) = lseek;
476f2c2defSopenharmony_cistatic int (*hiview_fsync)(int) = fsync;
486f2c2defSopenharmony_cistatic int (*hiview_unlink)(const char *) = unlink;
496f2c2defSopenharmony_cistatic int (*hiview_rename)(const char *, const char *) = NULL; // not all platform support rename
506f2c2defSopenharmony_ci
516f2c2defSopenharmony_civoid *HIVIEW_MemAlloc(uint8 modId, uint32 size)
526f2c2defSopenharmony_ci{
536f2c2defSopenharmony_ci    (void)modId;
546f2c2defSopenharmony_ci    return malloc(size);
556f2c2defSopenharmony_ci}
566f2c2defSopenharmony_ci
576f2c2defSopenharmony_civoid HIVIEW_MemFree(uint8 modId, void *pMem)
586f2c2defSopenharmony_ci{
596f2c2defSopenharmony_ci    (void)modId;
606f2c2defSopenharmony_ci    free(pMem);
616f2c2defSopenharmony_ci}
626f2c2defSopenharmony_ci
636f2c2defSopenharmony_ciuint64 HIVIEW_GetCurrentTime()
646f2c2defSopenharmony_ci{
656f2c2defSopenharmony_ci    struct timespec current = {0};
666f2c2defSopenharmony_ci    int ret = clock_gettime(CLOCK_REALTIME, &current);
676f2c2defSopenharmony_ci    if (ret != 0) {
686f2c2defSopenharmony_ci        return 0;
696f2c2defSopenharmony_ci    }
706f2c2defSopenharmony_ci    return (uint64)current.tv_sec * HIVIEW_MS_PER_SECOND + current.tv_nsec / HIVIEW_NS_PER_MILLISECOND;
716f2c2defSopenharmony_ci}
726f2c2defSopenharmony_ci
736f2c2defSopenharmony_ciint32 HIVIEW_RtcGetCurrentTime(uint64 *val, HIVIEW_RtcTime *time)
746f2c2defSopenharmony_ci{
756f2c2defSopenharmony_ci    (void)val;
766f2c2defSopenharmony_ci    (void)time;
776f2c2defSopenharmony_ci    return OHOS_SUCCESS;
786f2c2defSopenharmony_ci}
796f2c2defSopenharmony_ci
806f2c2defSopenharmony_ciHiviewMutexId_t HIVIEW_MutexInit()
816f2c2defSopenharmony_ci{
826f2c2defSopenharmony_ci    return (HiviewMutexId_t)osMutexNew(NULL);
836f2c2defSopenharmony_ci}
846f2c2defSopenharmony_ci
856f2c2defSopenharmony_ciint32 HIVIEW_MutexLock(HiviewMutexId_t mutex)
866f2c2defSopenharmony_ci{
876f2c2defSopenharmony_ci    if (mutex == NULL) {
886f2c2defSopenharmony_ci        return -1;
896f2c2defSopenharmony_ci    }
906f2c2defSopenharmony_ci    return (int32)osMutexAcquire((osMutexId_t)mutex, HIVIEW_WAIT_FOREVER);
916f2c2defSopenharmony_ci}
926f2c2defSopenharmony_ci
936f2c2defSopenharmony_ciint32 HIVIEW_MutexLockOrWait(HiviewMutexId_t mutex, uint32 timeout)
946f2c2defSopenharmony_ci{
956f2c2defSopenharmony_ci    if (mutex == NULL) {
966f2c2defSopenharmony_ci        return -1;
976f2c2defSopenharmony_ci    }
986f2c2defSopenharmony_ci    return (int32)osMutexAcquire((osMutexId_t)mutex, timeout);
996f2c2defSopenharmony_ci}
1006f2c2defSopenharmony_ci
1016f2c2defSopenharmony_ciint32 HIVIEW_MutexUnlock(HiviewMutexId_t mutex)
1026f2c2defSopenharmony_ci{
1036f2c2defSopenharmony_ci    if (mutex == NULL) {
1046f2c2defSopenharmony_ci        return -1;
1056f2c2defSopenharmony_ci    }
1066f2c2defSopenharmony_ci    return (int32)osMutexRelease((osMutexId_t)mutex);
1076f2c2defSopenharmony_ci}
1086f2c2defSopenharmony_ci
1096f2c2defSopenharmony_ciuint32 HIVIEW_IntLock()
1106f2c2defSopenharmony_ci{
1116f2c2defSopenharmony_ci    return LOS_IntLock();
1126f2c2defSopenharmony_ci}
1136f2c2defSopenharmony_ci
1146f2c2defSopenharmony_civoid HIVIEW_IntRestore(uint32 intSave)
1156f2c2defSopenharmony_ci{
1166f2c2defSopenharmony_ci    LOS_IntRestore(intSave);
1176f2c2defSopenharmony_ci}
1186f2c2defSopenharmony_ci
1196f2c2defSopenharmony_ciuint32 HIVIEW_GetTaskId()
1206f2c2defSopenharmony_ci{
1216f2c2defSopenharmony_ci    return (uint32)osThreadGetId();
1226f2c2defSopenharmony_ci}
1236f2c2defSopenharmony_ci
1246f2c2defSopenharmony_civoid HIVIEW_UartPrint(const char *str)
1256f2c2defSopenharmony_ci{
1266f2c2defSopenharmony_ci    printf("%s\n", str);
1276f2c2defSopenharmony_ci}
1286f2c2defSopenharmony_ci
1296f2c2defSopenharmony_civoid HIVIEW_Sleep(uint32 ms)
1306f2c2defSopenharmony_ci{
1316f2c2defSopenharmony_ci    osDelay(ms / HIVIEW_MS_PER_SECOND);
1326f2c2defSopenharmony_ci}
1336f2c2defSopenharmony_ci
1346f2c2defSopenharmony_civoid HIVIEW_InitHook(HIVIEW_Hooks *hooks)
1356f2c2defSopenharmony_ci{
1366f2c2defSopenharmony_ci    if (hooks == NULL) {
1376f2c2defSopenharmony_ci        // reset
1386f2c2defSopenharmony_ci        hiview_open  = open;
1396f2c2defSopenharmony_ci        hiview_close = close;
1406f2c2defSopenharmony_ci        hiview_read = read;
1416f2c2defSopenharmony_ci        hiview_write = write;
1426f2c2defSopenharmony_ci        hiview_lseek = lseek;
1436f2c2defSopenharmony_ci        hiview_fsync = fsync;
1446f2c2defSopenharmony_ci        hiview_unlink = unlink;
1456f2c2defSopenharmony_ci        hiview_rename = NULL;
1466f2c2defSopenharmony_ci        return;
1476f2c2defSopenharmony_ci    }
1486f2c2defSopenharmony_ci    hiview_open  = (hooks->open_fn) == NULL ? open : hooks->open_fn;
1496f2c2defSopenharmony_ci    hiview_close = (hooks->close_fn) == NULL ? close : hooks->close_fn;
1506f2c2defSopenharmony_ci    hiview_read = (hooks->read_fn) == NULL ? read : hooks->read_fn;
1516f2c2defSopenharmony_ci    hiview_write = (hooks->write_fn) == NULL ? write : hooks->write_fn;
1526f2c2defSopenharmony_ci    hiview_lseek = (hooks->lseek_fn) == NULL ? lseek : hooks->lseek_fn;
1536f2c2defSopenharmony_ci    hiview_fsync = (hooks->fsync_fn) == NULL ? fsync : hooks->fsync_fn;
1546f2c2defSopenharmony_ci    hiview_unlink = (hooks->unlink_fn) == NULL ? unlink : hooks->unlink_fn;
1556f2c2defSopenharmony_ci    hiview_rename = (hooks->rename_fn) == NULL ? rename : hooks->rename_fn;
1566f2c2defSopenharmony_ci}
1576f2c2defSopenharmony_ci
1586f2c2defSopenharmony_ciint32 HIVIEW_FileOpen(const char *path)
1596f2c2defSopenharmony_ci{
1606f2c2defSopenharmony_ci    int32 handle = hiview_open(path, O_RDWR | O_CREAT, 0);
1616f2c2defSopenharmony_ci    if (handle < 0) {
1626f2c2defSopenharmony_ci        printf("HIVIEW_FileOpen %s fail, errno:%d\n", path, errno);
1636f2c2defSopenharmony_ci    }
1646f2c2defSopenharmony_ci    return handle;
1656f2c2defSopenharmony_ci}
1666f2c2defSopenharmony_ci
1676f2c2defSopenharmony_ciint32 HIVIEW_FileClose(int32 handle)
1686f2c2defSopenharmony_ci{
1696f2c2defSopenharmony_ci    if (handle < 0) {
1706f2c2defSopenharmony_ci        return -1;
1716f2c2defSopenharmony_ci    }
1726f2c2defSopenharmony_ci    return hiview_close(handle);
1736f2c2defSopenharmony_ci}
1746f2c2defSopenharmony_ci
1756f2c2defSopenharmony_ciint32 HIVIEW_FileRead(int32 handle, uint8 *buf, uint32 len)
1766f2c2defSopenharmony_ci{
1776f2c2defSopenharmony_ci    if (handle < 0) {
1786f2c2defSopenharmony_ci        return -1;
1796f2c2defSopenharmony_ci    }
1806f2c2defSopenharmony_ci    return hiview_read(handle, (char *)buf, len);
1816f2c2defSopenharmony_ci}
1826f2c2defSopenharmony_ci
1836f2c2defSopenharmony_ciint32 HIVIEW_FileWrite(int32 handle, const uint8 *buf, uint32 len)
1846f2c2defSopenharmony_ci{
1856f2c2defSopenharmony_ci    if (handle < 0) {
1866f2c2defSopenharmony_ci        return -1;
1876f2c2defSopenharmony_ci    }
1886f2c2defSopenharmony_ci    return hiview_write(handle, (const char *)buf, len);
1896f2c2defSopenharmony_ci}
1906f2c2defSopenharmony_ci
1916f2c2defSopenharmony_ciint32 HIVIEW_FileSeek(int32 handle, int32 offset, int32 whence)
1926f2c2defSopenharmony_ci{
1936f2c2defSopenharmony_ci    if (handle < 0) {
1946f2c2defSopenharmony_ci        return -1;
1956f2c2defSopenharmony_ci    }
1966f2c2defSopenharmony_ci    return hiview_lseek(handle, (off_t)offset, whence);
1976f2c2defSopenharmony_ci}
1986f2c2defSopenharmony_ci
1996f2c2defSopenharmony_ciint32 HIVIEW_FileSize(int32 handle)
2006f2c2defSopenharmony_ci{
2016f2c2defSopenharmony_ci    if (handle < 0) {
2026f2c2defSopenharmony_ci        return -1;
2036f2c2defSopenharmony_ci    }
2046f2c2defSopenharmony_ci    return hiview_lseek(handle, 0, SEEK_END);
2056f2c2defSopenharmony_ci}
2066f2c2defSopenharmony_ci
2076f2c2defSopenharmony_ciint32 HIVIEW_FileSync(int32 handle)
2086f2c2defSopenharmony_ci{
2096f2c2defSopenharmony_ci    if (handle < 0) {
2106f2c2defSopenharmony_ci        return -1;
2116f2c2defSopenharmony_ci    }
2126f2c2defSopenharmony_ci    return hiview_fsync(handle);
2136f2c2defSopenharmony_ci}
2146f2c2defSopenharmony_ci
2156f2c2defSopenharmony_ciint32 HIVIEW_FileUnlink(const char *path)
2166f2c2defSopenharmony_ci{
2176f2c2defSopenharmony_ci    return hiview_unlink(path);
2186f2c2defSopenharmony_ci}
2196f2c2defSopenharmony_ci
2206f2c2defSopenharmony_ciint32 HIVIEW_FileCopy(const char *src, const char *dest)
2216f2c2defSopenharmony_ci{
2226f2c2defSopenharmony_ci    if (src == NULL || dest == NULL) {
2236f2c2defSopenharmony_ci        HIVIEW_UartPrint("HIVIEW_FileCopy input param is NULL");
2246f2c2defSopenharmony_ci        return -1;
2256f2c2defSopenharmony_ci    }
2266f2c2defSopenharmony_ci    int32 fdSrc = hiview_open(src, O_RDONLY, 0);
2276f2c2defSopenharmony_ci    if (fdSrc < 0) {
2286f2c2defSopenharmony_ci        HIVIEW_UartPrint("HIVIEW_FileCopy open src file fail");
2296f2c2defSopenharmony_ci        return fdSrc;
2306f2c2defSopenharmony_ci    }
2316f2c2defSopenharmony_ci    int32 fdDest = hiview_open(dest, O_RDWR | O_CREAT | O_TRUNC, 0);
2326f2c2defSopenharmony_ci    if (fdDest < 0) {
2336f2c2defSopenharmony_ci        HIVIEW_UartPrint("HIVIEW_FileCopy open dest file fail");
2346f2c2defSopenharmony_ci        HIVIEW_FileClose(fdSrc);
2356f2c2defSopenharmony_ci        return fdDest;
2366f2c2defSopenharmony_ci    }
2376f2c2defSopenharmony_ci    boolean copyFailed = TRUE;
2386f2c2defSopenharmony_ci    uint8 *dataBuf = (uint8 *)HIVIEW_MemAlloc(MEM_POOL_HIVIEW_ID, BUFFER_SIZE);
2396f2c2defSopenharmony_ci    if (dataBuf == NULL) {
2406f2c2defSopenharmony_ci        HIVIEW_UartPrint("HIVIEW_FileCopy malloc error");
2416f2c2defSopenharmony_ci        goto MALLOC_ERROR;
2426f2c2defSopenharmony_ci    }
2436f2c2defSopenharmony_ci    int32 nLen = HIVIEW_FileRead(fdSrc, dataBuf, BUFFER_SIZE);
2446f2c2defSopenharmony_ci    while (nLen > 0) {
2456f2c2defSopenharmony_ci        if (HIVIEW_FileWrite(fdDest, dataBuf, nLen) != nLen) {
2466f2c2defSopenharmony_ci            goto EXIT;
2476f2c2defSopenharmony_ci        }
2486f2c2defSopenharmony_ci        nLen = HIVIEW_FileRead(fdSrc, dataBuf, BUFFER_SIZE);
2496f2c2defSopenharmony_ci    }
2506f2c2defSopenharmony_ci    copyFailed = (nLen < 0);
2516f2c2defSopenharmony_ci
2526f2c2defSopenharmony_ciEXIT:
2536f2c2defSopenharmony_ci    free(dataBuf);
2546f2c2defSopenharmony_ciMALLOC_ERROR:
2556f2c2defSopenharmony_ci    HIVIEW_FileClose(fdSrc);
2566f2c2defSopenharmony_ci    HIVIEW_FileClose(fdDest);
2576f2c2defSopenharmony_ci    if (copyFailed) {
2586f2c2defSopenharmony_ci        HIVIEW_UartPrint("HIVIEW_FileCopy copy failed");
2596f2c2defSopenharmony_ci        HIVIEW_FileUnlink(dest);
2606f2c2defSopenharmony_ci        return -1;
2616f2c2defSopenharmony_ci    }
2626f2c2defSopenharmony_ci
2636f2c2defSopenharmony_ci    return 0;
2646f2c2defSopenharmony_ci}
2656f2c2defSopenharmony_ci
2666f2c2defSopenharmony_ciint32 HIVIEW_FileMove(const char *src, const char *dest)
2676f2c2defSopenharmony_ci{
2686f2c2defSopenharmony_ci    if (hiview_rename != NULL) {
2696f2c2defSopenharmony_ci        return hiview_rename(src, dest);
2706f2c2defSopenharmony_ci    }
2716f2c2defSopenharmony_ci    int32 ret = HIVIEW_FileCopy(src, dest);
2726f2c2defSopenharmony_ci    if (HIVIEW_FileUnlink(src) != 0 || ret != 0) {
2736f2c2defSopenharmony_ci        return -1;
2746f2c2defSopenharmony_ci    }
2756f2c2defSopenharmony_ci    return 0;
2766f2c2defSopenharmony_ci}
2776f2c2defSopenharmony_ci
2786f2c2defSopenharmony_civoid HIVIEW_WatchDogSystemReset()
2796f2c2defSopenharmony_ci{
2806f2c2defSopenharmony_ci    /* reset MCU Core */
2816f2c2defSopenharmony_ci    HAL_NVIC_SystemReset();
2826f2c2defSopenharmony_ci}
2836f2c2defSopenharmony_ci
2846f2c2defSopenharmony_ciuint8 HIVIEW_WdgResetFlag()
2856f2c2defSopenharmony_ci{
2866f2c2defSopenharmony_ci    /* Depend:HAL_WdgGetResetFlag */
2876f2c2defSopenharmony_ci    return 1;
2886f2c2defSopenharmony_ci}
2896f2c2defSopenharmony_ci
2906f2c2defSopenharmony_ciuint32 Change32Endian(uint32 num)
2916f2c2defSopenharmony_ci{
2926f2c2defSopenharmony_ci    unsigned char *buffer = (unsigned char *)&num;
2936f2c2defSopenharmony_ci    uint32 newEndian = (buffer[3] & 0xFF); // 3: forth char
2946f2c2defSopenharmony_ci    newEndian |= ((buffer[2] << 8) & 0xFF00); // 2: third char, 8: 1 byte length
2956f2c2defSopenharmony_ci    newEndian |= ((buffer[1] << 16) & 0xFF0000); // 16: 2 byte length
2966f2c2defSopenharmony_ci    newEndian |= ((buffer[0] << 24) & 0xFF000000); // 24: 3 byte length
2976f2c2defSopenharmony_ci    return newEndian;
2986f2c2defSopenharmony_ci}
2996f2c2defSopenharmony_ci
3006f2c2defSopenharmony_ciuint16 Change16Endian(uint16 num)
3016f2c2defSopenharmony_ci{
3026f2c2defSopenharmony_ci    unsigned char* buffer = (unsigned char*)&num;
3036f2c2defSopenharmony_ci    uint16 newEndian = (buffer[1] & 0xFF);
3046f2c2defSopenharmony_ci    newEndian |= ((buffer[0] << 8) & 0xFF00); // 8: 1 byte length
3056f2c2defSopenharmony_ci    return newEndian;
3066f2c2defSopenharmony_ci}
307