diff --git a/SmartL_E802/adapter/hals/utils/file/BUILD.gn b/SmartL_E802/adapter/hals/utils/file/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..432b1286f127eac3f4e762e5f942b6dc7519e95b --- /dev/null +++ b/SmartL_E802/adapter/hals/utils/file/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("hal_file_static") { + sources = [ "src/hal_file.c" ] + + include_dirs = [ "//utils/native/lite/hals/file" ] +} diff --git a/esp32/liteos_m/board/hals/utils/file/hal_file.c b/SmartL_E802/adapter/hals/utils/file/src/hal_file.c similarity index 84% rename from esp32/liteos_m/board/hals/utils/file/hal_file.c rename to SmartL_E802/adapter/hals/utils/file/src/hal_file.c index 92fee041189c021c8ce2fbc31d21517f5690b3c9..4be1e1627cfe49d3958a00775ade4a9ae098dfa6 100644 --- a/esp32/liteos_m/board/hals/utils/file/hal_file.c +++ b/SmartL_E802/adapter/hals/utils/file/src/hal_file.c @@ -29,59 +29,52 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include -#include "errno.h" -#include "fcntl.h" +#include "hal_file.h" +#include "securec.h" #include "sys/stat.h" -#include "hal_littlefs.h" -#include "hal_vfs.h" -#include "lfs.h" -#include "lfs_rambd.h" #include "fs_config.h" int HalFileOpen(const char* path, int oflag, int mode) { char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - return EspOpen(tmpPath, oflag, mode); + return open(tmpPath, oflag, mode); } int HalFileClose(int fd) { - return EspClose(fd); + return close(fd); } int HalFileRead(int fd, char *buf, unsigned int len) { - return EspRead(fd, buf, len); + return read(fd, buf, len); } int HalFileWrite(int fd, const char *buf, unsigned int len) { - return EspWrite(fd, buf, len); + return write(fd, buf, len); } int HalFileDelete(const char *path) { char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - return EspUnlink(tmpPath); + return unlink(path); } int HalFileStat(const char *path, unsigned int *fileSize) { char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; - struct stat halStat = {0}; + struct stat halStat ; int ret = 0; (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - ret = EspStat(tmpPath, &halStat); + ret = stat(tmpPath, &halStat); *fileSize = halStat.st_size; return ret; } int HalFileSeek(int fd, int offset, unsigned int whence) { - return EspLseek(fd, (off_t)offset, whence); + return lseek(fd, (off_t)offset, whence); } diff --git a/SmartL_E802/liteos_m/board/BUILD.gn b/SmartL_E802/liteos_m/board/BUILD.gn index 4ea8beb5dbddd5985c6ffefd61b751c5a4a477a8..a322b552b61e18b26627eec5e86526f1f0c584cd 100644 --- a/SmartL_E802/liteos_m/board/BUILD.gn +++ b/SmartL_E802/liteos_m/board/BUILD.gn @@ -16,8 +16,6 @@ import("//kernel/liteos_m/liteos.gni") kernel_module("bsp_config") { sources = [ "fs/fs_init.c", - "fs/hal_littlefs.c", - "fs/hal_vfs.c", "fs/littlefs_hal.c", "hals/csky_driver/src/ck_usart.c", "hals/csky_driver/src/devices.c", @@ -26,7 +24,6 @@ kernel_module("bsp_config") { "hals/csky_driver/src/isr.c", "hals/csky_driver/src/pinmux.c", "hals/csky_driver/src/sys_freq.c", - "hals/utils/file/hal_file.c", "libc/dprintf.c", "main.c", "startup.S", @@ -35,9 +32,6 @@ kernel_module("bsp_config") { # kernel's testsuites not enabled, use ower's sources += [ "test/task_demo.c" ] } - if (defined(LOSCFG_FS_FAT)) { - sources += [ "fs/ff_gen_drv.c" ] - } include_dirs = [ "//utils/native/lite/include", "test", @@ -52,10 +46,7 @@ config("public") { "-nostdlib", ] - libs = [ - "gcc", - "c", - ] + libs = [ "gcc" ] include_dirs = [ ".", diff --git a/SmartL_E802/liteos_m/board/fs/ff_gen_drv.c b/SmartL_E802/liteos_m/board/fs/ff_gen_drv.c deleted file mode 100644 index 9a23bb932fd128b150a8f0c3d335a3aaed4a1f7a..0000000000000000000000000000000000000000 --- a/SmartL_E802/liteos_m/board/fs/ff_gen_drv.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "ff_gen_drv.h" -#include "fs_config.h" -#include "fatfs.h" - -DiskDrvTypeDef g_diskDrv = { { 0 }, { 0 }, { 0 }, { 0 } }; diff --git a/SmartL_E802/liteos_m/board/fs/ff_gen_drv.h b/SmartL_E802/liteos_m/board/fs/ff_gen_drv.h deleted file mode 100644 index a52829c57356cfdf31c7b5f7f5c659889c6a60c0..0000000000000000000000000000000000000000 --- a/SmartL_E802/liteos_m/board/fs/ff_gen_drv.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _FF_GEN_DRV_H -#define _FF_GEN_DRV_H - -#include "stdint.h" -#include "diskio.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -typedef struct { - DSTATUS (*disk_initialize)(BYTE); - DSTATUS (*disk_status)(BYTE); - DSTATUS (*disk_read)(BYTE, BYTE *, DWORD, UINT); - DSTATUS (*disk_write)(BYTE, const BYTE *, DWORD, UINT); - DSTATUS (*disk_ioctl)(BYTE, BYTE, void *); -} DiskioDrvTypeDef; - -typedef struct { - uint8_t initialized[FF_VOLUMES]; - const DiskioDrvTypeDef *drv[FF_VOLUMES]; - uint8_t lun[FF_VOLUMES]; - volatile uint8_t nbr; -} DiskDrvTypeDef; - -extern DiskDrvTypeDef g_diskDrv; -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#endif /* _FF_GEN_DRV_H */ diff --git a/SmartL_E802/liteos_m/board/fs/fs_init.c b/SmartL_E802/liteos_m/board/fs/fs_init.c index af86951a7dacdba2ad515cd06c428fea35bee1c3..33c8875ba94e6963984cc2dc79f9308350bb8508 100644 --- a/SmartL_E802/liteos_m/board/fs/fs_init.c +++ b/SmartL_E802/liteos_m/board/fs/fs_init.c @@ -30,29 +30,40 @@ */ #include "fs_init.h" +#include "lfs.h" #include "los_task.h" #include "securec.h" -#include "hal_littlefs.h" +#include "utils_file.h" -#define FS_INIT_TASK_SIZE 0x1000 -#define FS_INIT_TASK_PRIORITY 2 +#define FS_INIT_TASK_SIZE 0x1000 +#define FS_INIT_TASK_PRIORITY 2 +#define LITTLEFS_ROOTDIR_MODE 775 static uint32_t g_taskId; static void FileSystemEntry(void) { - uint32_t ret; + int ret = 0; struct lfs_config *littlefsConfig = LittlefsConfigGet(); - ret = LittlefsInit(0, littlefsConfig); + LittlefsDriverInit(0); + SetDefaultMountPath(0, "/littlefs"); + ret = mount(NULL, "/littlefs", "littlefs", 0, littlefsConfig); if (ret != LOS_OK) { - printf("Littlefs init failed.\n"); + printf("Littlefs init failed 0x%x.\n", ret); + return; + } + + ret = mkdir("/littlefs", LITTLEFS_ROOTDIR_MODE); + if (ret != LOS_OK) { + printf("Mkdir failed 0x%x.\n", ret); + return; } - printf("Littlefs inited.\n"); + printf("Littlefs init successed!\n"); } void FileSystemInit(void) { - uint32_t ret; + int ret = 0; TSK_INIT_PARAM_S taskInitParam; ret = memset_s(&taskInitParam, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S)); diff --git a/SmartL_E802/liteos_m/board/fs/fs_init.h b/SmartL_E802/liteos_m/board/fs/fs_init.h index ee3773560d814536559e49341d4c398bcaa62764..a4e38809afaccc14fbb9356892f6326cfbe9e0d7 100644 --- a/SmartL_E802/liteos_m/board/fs/fs_init.h +++ b/SmartL_E802/liteos_m/board/fs/fs_init.h @@ -38,10 +38,6 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -struct spiffs_drv_t* SpiffsConfigGet(void); -struct diskio_drv* FatfsConfigGet(void); -struct lfs_config* LittlefsConfigGet(void); - void FileSystemInit(void); #ifdef __cplusplus diff --git a/SmartL_E802/liteos_m/board/fs/hal_littlefs.c b/SmartL_E802/liteos_m/board/fs/hal_littlefs.c deleted file mode 100644 index c6254ef9ef1fefb9ef891fe6bb765cc9ca0a44f2..0000000000000000000000000000000000000000 --- a/SmartL_E802/liteos_m/board/fs/hal_littlefs.c +++ /dev/null @@ -1,453 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "hal_littlefs.h" -#include -#include -#include -#include "lfs.h" -#include "errno.h" -#include "fcntl.h" -#include "sys/stat.h" -#include "hal_vfs.h" - -typedef struct { - lfs_t *lfs_fs; - lfs_file_t lfs_file[LOS_MAX_FILES]; -} LFS_P; - -LFS_P g_lfs_p; - -static lfs_t *littlefs_ptr; - -static int RetToErrno(int result) -{ - int err = 0; - switch (result) { - case LFS_ERR_OK: - return 0; - case LFS_ERR_IO: - err = EIO; - break; - case LFS_ERR_NOENT: - err = ENOENT; - break; - case LFS_ERR_EXIST: - err = EEXIST; - break; - case LFS_ERR_NOTDIR: - err = ENOTDIR; - break; - case LFS_ERR_ISDIR: - err = EISDIR; - break; - case LFS_ERR_NOTEMPTY: - err = ENOTEMPTY; - break; - case LFS_ERR_BADF: - err = EBADF; - break; - case LFS_ERR_INVAL: - err = EINVAL; - break; - case LFS_ERR_NOSPC: - err = ENOSPC; - break; - case LFS_ERR_NOMEM: - err = ENOMEM; - break; - case LFS_ERR_CORRUPT: - err = ELIBBAD; - break; - default: - err = EIO; - break; - } - VFS_ERRNO_SET(err); - - return -err; -} - -static int LittlefsFlagsGet(int oflags) -{ - int flags = 0; - switch (oflags & O_ACCMODE) { - case O_RDONLY: - flags |= LFS_O_RDONLY; - break; - case O_WRONLY: - flags |= LFS_O_WRONLY; - break; - case O_RDWR: - flags |= LFS_O_RDWR; - break; - default: - break; - } - - if (oflags & O_CREAT) { - flags |= LFS_O_CREAT; - } - - if ((oflags & O_EXCL)) { - flags |= LFS_O_EXCL; - } - - if (oflags & O_TRUNC) { - flags |= LFS_O_TRUNC; - } - - if (oflags & O_APPEND) { - flags |= LFS_O_APPEND; - } - - return flags; -} - -static int LittlefsOperationOpen(struct file *file, const char *pathInMp, int flags) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - if (f == lfs->mlist) { - lfs->mlist = NULL; - } - - int ret = lfs_file_open(lfs, f, pathInMp, LittlefsFlagsGet(flags)); - if (ret == LFS_ERR_OK) { - file->f_data = (void *)&f; - } - - return RetToErrno(ret); -} - -static int LittlefsOperationClose(struct file *file) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - int ret = lfs_file_close(lfs, f); - - return RetToErrno(ret); -} - -static ssize_t LittlefsOperationRead(struct file *file, char *buff, size_t bytes) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - lfs_ssize_t ret; - ret = lfs_file_read(lfs, f, (void *)buff, (lfs_size_t)bytes); - if (ret < 0) { - printf("Failed to read, read size=%d\n", (int)ret); - return RetToErrno((int)ret); - } - return (ssize_t)ret; -} - -static ssize_t LittlefsOperationWrite(struct file *file, const char *buff, size_t bytes) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - - lfs_ssize_t ret; - if ((buff == NULL) || (bytes == 0) || (lfs == NULL) || (f == NULL)) { - return -EINVAL; - } - - ret = lfs_file_write(lfs, f, (const void *)buff, (lfs_size_t)bytes); - if (ret < 0) { - return RetToErrno((int)ret); - } - - file->f_offset = f->pos; - return (ssize_t)ret; -} - -static off_t LittlefsOperationLseek(struct file *file, off_t off, int whence) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_soff_t ret; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - if (f == NULL) { - return -EINVAL; - } - if ((off > 0 && f->ctz.size < off) || (off < 0 && f->ctz.size < -off)) { - return -EINVAL; - } - ret = lfs_file_seek(lfs, f, (lfs_soff_t)off, whence); - if (ret < 0) { - return RetToErrno((int)ret); - } - return (off_t)ret; -} - -static off64_t LittlefsOperationLseek64(struct file *file, off64_t off, int whence) -{ - return (off64_t)LittlefsOperationLseek(file, (off_t)off, whence); -} - -int LittlefsOperationStat(struct mount_point *mp, const char *pathInMp, struct stat *stat) -{ - struct lfs_info info; - (void)memset_s(stat, sizeof(struct stat), 0, sizeof(struct stat)); - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret; - ret = lfs_stat(lfs, pathInMp, &info); - if (ret == LFS_ERR_OK) { - stat->st_size = info.size; - stat->st_mode = ((info.type == LFS_TYPE_DIR) ? S_IFDIR : S_IFREG); - } - return RetToErrno(ret); -} - -static int LittlefsOperationUlink(struct mount_point *mp, const char *pathInMp) -{ - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret = lfs_remove(lfs, pathInMp); - return RetToErrno(ret); -} - -static int LittlefsOperationRename(struct mount_point *mp, const char *pathInMpOld, const char *pathInMpNew) -{ - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret = lfs_rename(lfs, pathInMpOld, pathInMpNew); - return RetToErrno(ret); -} - -static int LittlefsOperationSync(struct file *file) -{ - int ret; - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - if ((lfs == NULL) || (f == NULL)) { - return -EINVAL; - } - - ret = lfs_file_sync(lfs, f); - return RetToErrno(ret); -} - -static int LittlefsOperationOpendir(struct dir *dir, const char *path) -{ - int ret; - LFS_P *p = NULL; - lfs_dir_t *lfs_dir = NULL; - lfs_t *lfs = NULL; - - if (dir == NULL) { - printf("Dir is null, open failed.\n"); - return -ENOMEM; - } - - lfs_dir = (lfs_dir_t *)malloc(sizeof(lfs_dir_t)); - (void)memset_s(lfs_dir, sizeof(lfs_dir_t), 0, sizeof(lfs_dir_t)); - p = (LFS_P *)dir->d_mp->m_data; - lfs = p->lfs_fs; - - ret = lfs_dir_open(lfs, lfs_dir, path); - if (ret != LFS_ERR_OK) { - (void)free(lfs_dir); - return RetToErrno(ret); - } - dir->d_data = (void *)lfs_dir; - dir->d_offset = 0; - return LFS_ERR_OK; -} - -static int LittlefsOperationReaddir(struct dir *dir, struct dirent *dent) -{ - LFS_P *p = (LFS_P *)dir->d_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_dir_t *lfs_dir = (lfs_dir_t *)dir->d_data; - struct lfs_info info; - int ret; - (void)memset_s(&info, sizeof(struct lfs_info), 0, sizeof(struct lfs_info)); - if (lfs_dir == NULL) { - return -EINVAL; - } - do { - ret = lfs_dir_read(lfs, lfs_dir, &info); - } while ((ret >= 0) && ((strcmp(info.name, ".") == 0) || (strcmp(info.name, "..") == 0))); - if (ret < 0) { - return ENOENT; - } else { - (void)strncpy_s((char *)dent->name, LOS_MAX_DIR_NAME_LEN, (const char *)info.name, LOS_MAX_FILE_NAME_LEN - 1); - dent->name[LOS_MAX_FILE_NAME_LEN - 1] = '\0'; - dent->size = info.size; - if (info.type == LFS_TYPE_DIR) { - dent->type = VFS_TYPE_DIR; - } else { - dent->type = VFS_TYPE_FILE; - } - } - return LOS_OK; -} - -static int LittlefsOperationClosedir(struct dir *dir) -{ - int ret; - LFS_P *p = (LFS_P *)dir->d_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_dir_t *lfs_dir = (lfs_dir_t *)dir->d_data; - if (lfs_dir == NULL) { - return -EINVAL; - } - ret = lfs_dir_close(lfs, lfs_dir); - if (ret == LFS_ERR_OK) { - (void)free(lfs_dir); - } - return RetToErrno(ret); -} - -static int LittlefsOperationMkdir(struct mount_point *mp, const char *path) -{ - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret; - - ret = lfs_mkdir(lfs, path); - if (ret == LFS_ERR_NOENT) { - int err; - VFS_ERRNO_SET(ENOENT); - err = ENOENT; - return err; - } - return RetToErrno(ret); -} - -static struct file_ops g_littlefsOps = { - LittlefsOperationOpen, - LittlefsOperationClose, - LittlefsOperationRead, - LittlefsOperationWrite, - LittlefsOperationLseek, - LittlefsOperationLseek64, - LittlefsOperationStat, - LittlefsOperationUlink, - LittlefsOperationRename, - NULL, /* ioctl not supported for now */ - LittlefsOperationSync, - LittlefsOperationOpendir, - LittlefsOperationReaddir, - LittlefsOperationClosedir, - LittlefsOperationMkdir - }; - -static struct file_system g_littlefsFs = { "littlefs", &g_littlefsOps, NULL, 0 }; - -int LittlefsMount(const char *path, const struct lfs_config *lfsConfig) -{ - int ret = -1; - int err; - lfs_t *fs = NULL; - - fs = (lfs_t *)malloc(sizeof(lfs_t)); - - g_lfs_p.lfs_fs = fs; - - if (fs == NULL) { - printf("Malloc memory failed.\n"); - goto err_free; - } - - (void)memset_s(fs, sizeof(lfs_t), 0, sizeof(lfs_t)); - - err = lfs_mount(fs, lfsConfig); - if (err == LFS_ERR_CORRUPT) { - (void)lfs_format(fs, lfsConfig); - err = lfs_mount(fs, lfsConfig); - } - if (err != LFS_ERR_OK) { - printf("Format fail.\n"); - goto err_unmount; - } - - ret = LOS_FsMount("littlefs", path, &g_lfs_p); - if (ret == LFS_ERR_OK) { - printf("Littlefs mount at %s done.\n", path); - littlefs_ptr = fs; - return LFS_ERR_OK; - } - printf("Failed to mount.\n"); -err_unmount: - lfs_unmount(fs); -err_free: - if (fs != NULL) { - (void)free(fs); - } - return ret; -} - -int LittlefsUnmout(const char *path) -{ - int ret = LFS_ERR_OK; - if (littlefs_ptr != NULL) { - ret = lfs_unmount(littlefs_ptr); - free(littlefs_ptr); - littlefs_ptr = NULL; - } - (void)LOS_FsUnmount(path); - return ret; -} - -int LittlefsInit(int needErase, const struct lfs_config *lfsConfig) -{ - int ret; - static int littlefsInited = FALSE; - if (littlefsInited) { - return LOS_OK; - } - if (LOS_VfsInit() != LOS_OK) { - return LOS_NOK; - } - if (LOS_FsRegister(&g_littlefsFs) != LOS_OK) { - printf("Failed to register fs.\n"); - return LOS_NOK; - } - printf("Register littlefs done.\n"); - - LittlefsDriverInit(needErase); - - ret = LittlefsMount("/littlefs/", lfsConfig); - if (ret == LFS_ERR_OK) { - littlefsInited = TRUE; - return LOS_OK; - } - - return ret; -} diff --git a/SmartL_E802/liteos_m/board/fs/hal_littlefs.h b/SmartL_E802/liteos_m/board/fs/hal_littlefs.h deleted file mode 100644 index e2efe04345cc41221b25e1b01450e624f64bbdea..0000000000000000000000000000000000000000 --- a/SmartL_E802/liteos_m/board/fs/hal_littlefs.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _HAL_LITTLEFS_H -#define _HAL_LITTLEFS_H - -#include -#include "lfs.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -void LittlefsDriverInit(int needErase); -int LittlefsInit(int needErase, const struct lfs_config *lfsConfig); -int LittlefsMount(const char *path, const struct lfs_config *lfsConfig); -int LittlefsUnmount(const char *path); - -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#endif /* _HAL_LITTLEFS_H */ diff --git a/SmartL_E802/liteos_m/board/fs/hal_vfs.c b/SmartL_E802/liteos_m/board/fs/hal_vfs.c deleted file mode 100644 index ef901fd992966143303cb1393809132d7cca5128..0000000000000000000000000000000000000000 --- a/SmartL_E802/liteos_m/board/fs/hal_vfs.c +++ /dev/null @@ -1,1482 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "hal_vfs.h" -#include -#include -#include -#include -#include "errno.h" -#include "fcntl.h" -#include "los_mux.h" -#include "los_task.h" -#include "los_debug.h" -#include "limits.h" -#include "fs_config.h" - -#ifdef LOSCFG_COMPONENTS_NET_LWIP -#include "lwipopts.h" -#include "lwip/sockets.h" -#endif - -#ifndef CONFIG_NFILE_DESCRIPTORS -#define CONFIG_NFILE_DESCRIPTORS 256 -#endif - -#define LOS_FCNTL (O_NONBLOCK | O_NDELAY | O_APPEND | O_SYNC | FASYNC) -#define IOV_MAX_CNT 4 -#define BASE_PATH 10 - -struct file g_files[LOS_MAX_FILES]; -uint32_t g_fsMutex; -uint32_t g_openfiles; -struct mount_point *g_mountPoints = NULL; -struct file_system *g_fileSystems = NULL; - -int FileToFd(struct file *file) -{ - if (file == NULL) { - return -1; - } - return file - g_files; -} - -static struct file *FdToFile(int fd) -{ - if (fd > LOS_MAX_FD) { - return NULL; - } - return &g_files[fd]; -} - -static struct file *LOS_FileGet(void) -{ - int i; - /* protected by g_fsMutex */ - for (i = LOS_FD_OFFSET - 1; i < LOS_MAX_FILES; i++) { - if (g_files[i].f_status == FILE_STATUS_NOT_USED) { - g_files[i].f_status = FILE_STATUS_INITING; - return &g_files[i]; - } - } - - return NULL; -} - -static struct file *LOS_FileGetNew(int fd) -{ - if (fd > LOS_MAX_FD) { - return NULL; - } - if (g_files[fd].f_status == FILE_STATUS_NOT_USED) { - g_files[fd].f_status = FILE_STATUS_INITING; - return &g_files[fd]; - } - - return NULL; -} - -static void LOS_FilePut(struct file *file) -{ - if (file == NULL) { - return; - } - - file->f_flags = 0; - file->f_fops = NULL; - file->f_data = NULL; - file->f_mp = NULL; - file->f_offset = 0; - file->f_owner = (uint32_t)-1; - file->full_path = NULL; - file->f_status = FILE_STATUS_NOT_USED; -} - -static struct mount_point *LOS_MpFind(const char *path, const char **pathInMp) -{ - struct mount_point *mp = g_mountPoints; - struct mount_point *bestMp = NULL; - int bestMatches = 0; - if (path == NULL) { - return NULL; - } - if (pathInMp != NULL) { - *pathInMp = NULL; - } - while ((mp != NULL) && (mp->m_path != NULL)) { - const char *m_path = mp->m_path; - const char *i_path = path; - const char *t = NULL; - int matches = 0; - do { - while ((*m_path == '/') && (*(m_path + 1) != '/')) { - m_path++; - } - while ((*i_path == '/') && (*(i_path + 1) != '/')) { - i_path++; - } - - t = strchr(m_path, '/'); - if (t == NULL) { - t = strchr(m_path, '\0'); - } - if ((t == m_path) || (t == NULL)) { - break; - } - if (strncmp(m_path, i_path, (size_t)(t - m_path)) != 0) { - goto next; /* this mount point do not match, check next */ - } - - i_path += (t - m_path); - if ((*i_path != '\0') && (*i_path != '/')) { - goto next; - } - - matches += (t - m_path); - m_path += (t - m_path); - } while (*m_path != '\0'); - - if (matches > bestMatches) { - bestMatches = matches; - bestMp = mp; - - while ((*i_path == '/') && (*(i_path + 1) != '/')) { - i_path++; - } - - if (pathInMp != NULL) { - *pathInMp = i_path; - } - } - next: - mp = mp->m_next; - } - return bestMp; -} - -static int LOS_FileOpened(const char *path) -{ - for (int i = LOS_FD_OFFSET; i < g_openfiles; i++) { - if (g_files[i].f_status == FILE_STATUS_READY) { - if (strcmp(g_files[i].full_path, path) == 0) { - return 1; - } - } - } - return 0; -} - -static int LOS_Open(const char *path, int flags) -{ - struct file *file = NULL; - int fd = -1; - const char *pathInMp = NULL; - char *fullPath = NULL; - struct mount_point *mp = NULL; - - if ((path == NULL) || (path[strlen(path) - 1] == '/') || - strlen(path) > LITTLEFS_MAX_LFN_LEN - BASE_PATH) { - return fd; - } - if (LOS_FileOpened(path)) { - return fd; - } - /* prevent fs/mp being removed while opening */ - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - return fd; - } - file = LOS_FileGet(); - mp = LOS_MpFind(path, &pathInMp); - - (void)LOS_MuxPost(g_fsMutex); - - if ((file == NULL) || (mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') || (mp->m_fs->fs_fops == NULL) || - (mp->m_fs->fs_fops->open == NULL)) { - return fd; - } - - if ((LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK)) { - LOS_FilePut(file); - return fd; - } - - file->f_flags = (uint32_t)flags; - file->f_offset = 0; - file->f_data = NULL; - file->f_fops = mp->m_fs->fs_fops; - file->f_mp = mp; - file->f_owner = LOS_CurTaskIDGet(); - file->full_path = path; - - if (file->f_fops->open(file, pathInMp, flags) == 0) { - mp->m_refs++; - g_openfiles++; - fd = FileToFd(file); - file->f_status = FILE_STATUS_READY; /* file now ready to use */ - fullPath = (char *)malloc(strlen(path) + 1); - (void)memset_s(fullPath, strlen(path) + 1, 0, strlen(path) + 1); - (void)memcpy_s(fullPath, strlen(path), path, strlen(path)); - g_files[fd].full_path = fullPath; - } else { - LOS_FilePut(file); - } - (void)LOS_MuxPost(mp->m_mutex); - - return fd; -} - -/* attach to a file and then set new status */ - -static struct file *LOS_AttachFile(int fd, uint32_t status) -{ - struct file *file = NULL; - - if ((fd < 0) || (fd >= LOS_MAX_FD)) { - VFS_ERRNO_SET(EBADF); - return file; - } - - file = FdToFile(fd); - /* - * Prevent file closed after the checking of: - * - * if (file->f_status == FILE_STATUS_READY) - * - * Because our g_files are not privated to one task, it may be operated - * by every task. - * So we should take the mutex of current mount point before operating it, - * but for now we don't know if this file is valid (FILE_STATUS_READY), if - * this file is not valid, the f_mp may be incorrect. so - * we must check the status first, but this file may be closed/removed - * after the checking if the consequence is not correct. - * - * Consider the following code: - * - * LOS_AttachFileReady (...) - * { - * if (file->f_status == FILE_STATUS_READY) - * { - * while (LOS_MuxPend (file->f_mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK); - * - * return file; - * } - * } - * - * It is not safe: - * - * If current task is interrupted by an IRQ just after the checking and then - * a new task is swapped in and the new task just closed this file. - * - * So is acquire first and then check if it is valid: if not, just - * return NULL (which means fail); If yes, the mutex for current mount point - * is acquired. And the close operation will also set task to - * FILE_STATUS_CLOSING to prevent other tasks operate on this file (and also - * prevent other tasks pend on the mutex of this mount point for this file). - * At last is released. And return the file handle (struct file *). - * - * As this logic used in almost all the operation routines, this routine is - * made to reduce the redundant code. - */ - if ((file == NULL) || (file->f_mp == NULL)) { - return NULL; - } - while (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - }; - if (file->f_mp == NULL) { - return file; - } - if (file->f_status == FILE_STATUS_READY) { - while (LOS_MuxPend(file->f_mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK) { - }; - if (status != FILE_STATUS_READY) { - file->f_status = status; - } - } else { - VFS_ERRNO_SET(EBADF); - file = NULL; - } - - (void)LOS_MuxPost(g_fsMutex); - - return file; -} - -static struct file *LOS_AttachFileReady(int fd) -{ - return LOS_AttachFile(fd, FILE_STATUS_READY); -} - -static struct file *LOS_AttachFileWithStatus(int fd, int status) -{ - return LOS_AttachFile(fd, (uint32_t)status); -} - -static void LOS_DetachFile(const struct file *file) -{ - if ((file == NULL) || (file->f_mp == NULL)) { - return; - } - (void)LOS_MuxPost(file->f_mp->m_mutex); -} - -static int LOS_Close(int fd) -{ - struct file *file; - int ret = -1; - - file = LOS_AttachFileWithStatus(fd, FILE_STATUS_CLOSING); - if (file == NULL) { - return ret; - } - - if (file->full_path != NULL) { - (void)free((void *)file->full_path); - } - if ((file->f_fops != NULL) && (file->f_fops->close != NULL)) { - ret = file->f_fops->close(file); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - if ((ret == 0) && (file->f_mp != NULL)) { - file->f_mp->m_refs--; - g_openfiles--; - } - - LOS_DetachFile(file); - - LOS_FilePut(file); - - return ret; -} - -static ssize_t LOS_Read(int fd, char *buff, size_t bytes) -{ - struct file *file = NULL; - ssize_t ret = (ssize_t)-1; - - if ((buff == NULL) || (bytes == 0)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_flags & O_ACCMODE) == O_WRONLY) { - VFS_ERRNO_SET(EACCES); - } else if ((file->f_fops != NULL) && (file->f_fops->read != NULL)) { - ret = file->f_fops->read(file, buff, bytes); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - /* else ret will be -1 */ - - LOS_DetachFile(file); - - return ret; -} - -static ssize_t LOS_Write(int fd, const void *buff, size_t bytes) -{ - struct file *file = NULL; - ssize_t ret = -1; - - if ((buff == NULL) || (bytes == 0)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_flags & O_ACCMODE) == O_RDONLY) { - VFS_ERRNO_SET(EACCES); - } else if ((file->f_fops != NULL) && (file->f_fops->write != NULL)) { - ret = file->f_fops->write(file, buff, bytes); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - /* else ret will be -1 */ - LOS_DetachFile(file); - - return ret; -} - -static off_t LOS_Lseek(int fd, off_t off, int whence) -{ - struct file *file; - off_t ret = -1; - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_fops == NULL) || (file->f_fops->lseek == NULL)) { - ret = file->f_offset; - } else { - ret = file->f_fops->lseek(file, off, whence); - } - - LOS_DetachFile(file); - - return ret; -} - -static off64_t LOS_Lseek64(int fd, off64_t off, int whence) -{ - struct file *file; - off64_t ret = -1; - - file = LOS_AttachFileReady(fd); - if ((file == NULL) || (file->f_fops == NULL)) { - return ret; - } - if (file->f_fops->lseek64 == NULL) { - ret = file->f_offset64; - } else { - ret = file->f_fops->lseek64(file, off, whence); - } - - LOS_DetachFile(file); - - return ret; -} - -static int LOS_Stat(const char *path, struct stat *stat) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - int ret = -1; - - if ((path == NULL) || (stat == NULL)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return ret; - } - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (mp->m_fs->fs_fops->stat != NULL) { - ret = mp->m_fs->fs_fops->stat(mp, pathInMp, stat); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - (void)LOS_MuxPost(g_fsMutex); - - return ret; -} - -static int LOS_Unlink(const char *path) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - int ret = -1; - - if (path == NULL) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); /* prevent the file open/rename */ - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') || (mp->m_fs->fs_fops->unlink == NULL)) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - ret = mp->m_fs->fs_fops->unlink(mp, pathInMp); - - if(ret != 0) { - return -1; - } - (void)LOS_MuxPost(g_fsMutex); - return ret; -} - -static int LOS_Rename(const char *old, const char *new) -{ - struct mount_point *mpOld = NULL; - struct mount_point *mpNew = NULL; - const char *pathInMpOld = NULL; - const char *pathInMpNew = NULL; - int ret = -1; - - if ((old == NULL) || (new == NULL)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); /* prevent file open/unlink */ - - mpOld = LOS_MpFind(old, &pathInMpOld); - - if (pathInMpOld == NULL) { - VFS_ERRNO_SET(EINVAL); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if ((mpOld == NULL) || (*pathInMpOld == '\0') || (mpOld->m_fs->fs_fops->unlink == NULL)) { - VFS_ERRNO_SET(EINVAL); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - mpNew = LOS_MpFind(new, &pathInMpNew); - if ((mpNew == NULL) || (pathInMpNew == NULL) || (*pathInMpNew == '\0') || (mpNew->m_fs->fs_fops->unlink == NULL)) { - VFS_ERRNO_SET(EINVAL); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (mpOld != mpNew) { - VFS_ERRNO_SET(EXDEV); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (mpOld->m_fs->fs_fops->rename != NULL) { - ret = mpOld->m_fs->fs_fops->rename(mpOld, pathInMpOld, pathInMpNew); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - (void)LOS_MuxPost(g_fsMutex); - return ret; -} - -static int LOS_Ioctl(int fd, int func, ...) -{ - va_list ap; - unsigned long arg; - struct file *file = NULL; - int ret = -1; - - (void)va_start(ap, func); - arg = va_arg(ap, unsigned long); - (void)va_end(ap); - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_fops != NULL) && (file->f_fops->ioctl != NULL)) { - ret = file->f_fops->ioctl(file, func, arg); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - LOS_DetachFile(file); - - return ret; -} - -static int LOS_Sync(int fd) -{ - struct file *file; - int ret = -1; - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_fops != NULL) && (file->f_fops->sync != NULL)) { - ret = file->f_fops->sync(file); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - LOS_DetachFile(file); - - return ret; -} - -static struct dir *LOS_Opendir(const char *path) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - struct dir *dir = NULL; - uint32_t ret; - - if (path == NULL) { - VFS_ERRNO_SET(EINVAL); - return NULL; - } - - dir = (struct dir *)malloc(sizeof(struct dir)); - if (dir == NULL) { - printf("fail to malloc memory in VFS, is needed," - "make sure it is added\n"); - VFS_ERRNO_SET(ENOMEM); - return NULL; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - (void)free(dir); - return NULL; - } - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL)) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - (void)free(dir); - return NULL; - } - - ret = LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER); - - (void)LOS_MuxPost(g_fsMutex); - - if ((ret != LOS_OK) || (mp->m_fs->fs_fops->opendir == NULL)) { - VFS_ERRNO_SET(ENOTSUP); - (void)LOS_MuxPost(mp->m_mutex); - (void)free(dir); - return NULL; - } - - dir->d_mp = mp; - dir->d_offset = 0; - - ret = (uint32_t)mp->m_fs->fs_fops->opendir(dir, pathInMp); - if (ret == 0) { - mp->m_refs++; - } else { - (void)free(dir); - dir = NULL; - } - - (void)LOS_MuxPost(mp->m_mutex); - - return dir; -} - -static struct dirent *LOS_Readdir(struct dir *dir) -{ - struct mount_point *mp = NULL; - struct dirent *ret = NULL; - - if ((dir == NULL) || (dir->d_mp == NULL)) { - VFS_ERRNO_SET(EINVAL); - return NULL; - } - - mp = dir->d_mp; - - if (LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return NULL; - } - - if ((dir->d_mp->m_fs != NULL) && (dir->d_mp->m_fs->fs_fops != NULL) && - (dir->d_mp->m_fs->fs_fops->readdir != NULL)) { - if (dir->d_mp->m_fs->fs_fops->readdir(dir, &dir->d_dent) == 0) { - ret = &dir->d_dent; - } else { - VFS_ERRNO_SET(EBADF); - } - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - (void)LOS_MuxPost(mp->m_mutex); - - return ret; -} - -static int LOS_Closedir(struct dir *dir) -{ - struct mount_point *mp = NULL; - int ret = -1; - - if ((dir == NULL) || (dir->d_mp == NULL)) { - VFS_ERRNO_SET(EBADF); - return ret; - } - - mp = dir->d_mp; - - if (LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return ret; - } - - if ((dir->d_mp->m_fs != NULL) && (dir->d_mp->m_fs->fs_fops != NULL) && - (dir->d_mp->m_fs->fs_fops->closedir != NULL)) { - ret = dir->d_mp->m_fs->fs_fops->closedir(dir); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - (void)LOS_MuxPost(mp->m_mutex); - if (ret == 0) { - mp->m_refs--; - } else { - VFS_ERRNO_SET(EBADF); - } - (void)free(dir); - dir = NULL; - return ret; -} - -static int LOS_Mkdir(const char *path, int mode) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - int ret = -1; - - (void)mode; - - if (path == NULL) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return ret; - } - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - ret = (int)LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER); - - (void)LOS_MuxPost(g_fsMutex); - - if (ret != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return -1; - } - - if (mp->m_fs->fs_fops->mkdir != NULL) { - ret = mp->m_fs->fs_fops->mkdir(mp, pathInMp); - } else { - VFS_ERRNO_SET(ENOTSUP); - ret = -1; - } - - (void)LOS_MuxPost(mp->m_mutex); - - return ret; -} - -static int LOS_Dup(int fd) -{ - int ret; - struct file *file1 = NULL; - struct file *file2 = NULL; - const char *mpath = NULL; - struct mount_point *mp = NULL; - - if ((fd < 0) || (fd > LOS_MAX_FD)) { - VFS_ERRNO_SET(EBADF); - return LOS_NOK; - } - - file1 = LOS_AttachFileReady(fd); - if (file1 == NULL) { - return LOS_NOK; - } - - file2 = LOS_FileGet(); - if (file2 == NULL) { - LOS_DetachFile(file1); - VFS_ERRNO_SET(ENFILE); - return LOS_NOK; - } - - file2->f_flags = file1->f_flags; - file2->f_status = file1->f_status; - file2->f_offset = file1->f_offset; - file2->f_owner = file1->f_owner; - file2->f_data = file1->f_data; - file2->f_fops = file1->f_fops; - file2->f_mp = file1->f_mp; - - if ((file1->f_mp == NULL) || (file1->f_mp->m_path == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - mp = LOS_MpFind(file1->f_mp->m_path, &mpath); - if ((mp == NULL) || (file1->f_fops == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - ret = file1->f_fops->open(file2, mpath, file1->f_flags); - LOS_DetachFile(file1); - return ret; -} - -static int LOS_Dup2(int oldFd, int newFd) -{ - struct file *file1 = NULL; - struct file *file2 = NULL; - const char *mpath = NULL; - struct mount_point *mp = NULL; - - file1 = LOS_AttachFileReady(oldFd); - if (file1 == NULL) { - return LOS_NOK; - } - - file2 = LOS_AttachFileReady(newFd); - if (file2 != NULL) { - LOS_DetachFile(file1); - LOS_DetachFile(file2); - (void)LOS_Close(newFd); - return LOS_NOK; - } - - file2 = LOS_FileGetNew(newFd); - if (file2 == NULL) { - VFS_ERRNO_SET(ENFILE); - printf("files no free!\n"); - LOS_DetachFile(file1); - return LOS_NOK; - } - if (oldFd == newFd) { - LOS_DetachFile(file1); - return oldFd; - } - - file2->f_flags = file1->f_flags; - file2->f_status = file1->f_status; - file2->f_offset = file1->f_offset; - file2->f_owner = file1->f_owner; - file2->f_data = file1->f_data; - file2->f_fops = file1->f_fops; - file2->f_mp = file1->f_mp; - - if ((file1->f_mp == NULL) || (file1->f_mp->m_path == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - mp = LOS_MpFind(file1->f_mp->m_path, &mpath); - if ((mp == NULL) || (file1->f_fops == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - file1->f_fops->open(file2, mpath, file1->f_flags); - LOS_DetachFile(file1); - - return newFd; -} - -static int LOS_Vfcntl(struct file *filep, int cmd, va_list ap) -{ - int ret; - int fd; - uint32_t flags; - - if ((filep == NULL) || (filep->f_fops == NULL)) { - return -EBADF; - } - - if (cmd == F_DUPFD) { - fd = FileToFd(filep); - ret = LOS_Dup(fd); - } else if (cmd == F_GETFL) { - ret = (int)(filep->f_flags); - } else if (cmd == F_SETFL) { - flags = (uint32_t)va_arg(ap, int); - flags &= LOS_FCNTL; - filep->f_flags &= ~LOS_FCNTL; - filep->f_flags |= flags; - ret = LOS_OK; - } else { - ret = -ENOSYS; - } - return ret; -} - -static int LOS_FsNameCheck(const char *name) -{ - char ch; - int len = 0; - - do { - ch = *(name++); - - if (ch == '\0') { - break; - } - - if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || - (ch == '_') || (ch == '-')) { - len++; - if (len == LOS_FS_MAX_NAME_LEN) { - return LOS_NOK; - } - continue; - } - } while (1); - - return len == 0 ? LOS_NOK : LOS_OK; -} - -static struct file_system *LOS_FsFind(const char *name) -{ - struct file_system *fs; - - for (fs = g_fileSystems; fs != NULL; fs = fs->fs_next) { - if (strncmp(fs->fs_name, name, LOS_FS_MAX_NAME_LEN) == 0) { - break; - } - } - - return fs; -} - -int LOS_FsRegister(struct file_system *fs) -{ - if ((fs == NULL) || (fs->fs_fops == NULL) || (fs->fs_fops->open == NULL)) { - return LOS_NOK; - } - - if (LOS_FsNameCheck(fs->fs_name) != LOS_OK) { - return LOS_NOK; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - return LOS_NOK; - } - - if (LOS_FsFind(fs->fs_name) != NULL) { - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - fs->fs_next = g_fileSystems; - g_fileSystems = fs; - - (void)LOS_MuxPost(g_fsMutex); - - return LOS_OK; -} - -int LOS_FsUnregister(struct file_system *fs) -{ - struct file_system *prev = NULL; - int ret = LOS_OK; - - if (fs == NULL) { - return LOS_NOK; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - return LOS_NOK; - } - - if (fs->fs_refs > 0) { - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (g_fileSystems == fs) { - g_fileSystems = fs->fs_next; - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - prev = g_fileSystems; - - while (prev != NULL) { - if (prev->fs_next == fs) { - break; - } - - prev = prev->fs_next; - } - - if (prev == NULL) { - ret = LOS_NOK; - } else { - prev->fs_next = fs->fs_next; - } - - (void)LOS_MuxPost(g_fsMutex); - return ret; -} - -int LOS_FsMount(const char *fsname, const char *path, void *data) -{ - struct file_system *fs = NULL; - struct mount_point *mp = NULL; - const char *tmp = NULL; - int ret; - if ((fsname == NULL) || (path == NULL) || (path[0] != '/')) { - return LOS_NOK; - } - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); - - fs = LOS_FsFind(fsname); - if (fs == NULL) { - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp = LOS_MpFind(path, &tmp); - if ((mp != NULL) && (tmp != NULL) && (*tmp == '\0')) { - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp = malloc(sizeof(struct mount_point)); - if (mp == NULL) { - printf("Fail to malloc memory in vfs\n"); - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - ret = memset_s(mp, sizeof(struct mount_point), 0, sizeof(struct mount_point)); - if (ret != LOS_OK) { - (void)free(mp); - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp->m_fs = fs; - mp->m_path = path; - mp->m_data = data; - mp->m_refs = 0; - - if (LOS_MuxCreate(&mp->m_mutex) != LOS_OK) { - (void)free(mp); - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp->m_next = g_mountPoints; - g_mountPoints = mp; - - fs->fs_refs++; - (void)LOS_MuxPost(g_fsMutex); - - return LOS_OK; -} - -int LOS_FsUnmount(const char *path) -{ - struct mount_point *mp = NULL; - struct mount_point *prev = NULL; - const char *tmp = NULL; - int ret = LOS_NOK; - - if (path == NULL) { - return ret; - } - - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); - - mp = LOS_MpFind(path, &tmp); - if ((mp == NULL) || (tmp == NULL) || (*tmp != '\0') || (mp->m_refs != 0)) { - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (g_mountPoints == mp) { - g_mountPoints = mp->m_next; - } else { - for (prev = g_mountPoints; prev != NULL; prev = prev->m_next) { - if (prev->m_next != mp) { - continue; - } - - prev->m_next = mp->m_next; - break; - } - } - - (void)LOS_MuxDelete(mp->m_mutex); - mp->m_fs->fs_refs--; - (void)free(mp); - (void)LOS_MuxPost(g_fsMutex); - return LOS_OK; -} - -int LOS_VfsInit(void) -{ - if (LOS_MuxCreate(&g_fsMutex) == LOS_OK) { - return LOS_OK; - } - - printf("Vfs init fail!\n"); - - return LOS_NOK; -} - - -static int MapToPosixRet(int ret) -{ - return ((ret) < 0 ? -1 : (ret)); -} - -int EspOpen(const char *path, int flags, ...) -{ - int ret = LOS_Open(path, flags); - return MapToPosixRet(ret); -} - -int EspClose(int fd) -{ - int ret; - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Close(fd); - } else { - ret = -1; - } - return MapToPosixRet(ret); -} - -ssize_t EspRead(int fd, void *buff, size_t bytes) -{ - ssize_t ret; - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Read(fd, buff, bytes); - } else { - ret = -1; - } - return MapToPosixRet(ret); -} - -ssize_t EspWrite(int fd, const void *buff, size_t bytes) -{ - ssize_t ret; - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Write(fd, buff, bytes); - } else { - ret = -1; - } - return MapToPosixRet(ret); -} - -off_t EspLseek(int fd, off_t off, int whence) -{ - off_t ret = LOS_Lseek(fd, off, whence); - return MapToPosixRet(ret); -} - -off64_t EspLseek64(int fd, off64_t off, int whence) -{ - off64_t ret = LOS_Lseek64(fd, off, whence); - return MapToPosixRet(ret); -} - -int EspStat(const char *path, struct stat *stat) -{ - int ret = LOS_Stat(path, stat); - return MapToPosixRet(ret); -} - -int EspUnlink(const char *path) -{ - int ret = LOS_Unlink(path); - return MapToPosixRet(ret); -} - -int EspRename(const char *oldpath, const char *newpath) -{ - int ret = LOS_Rename(oldpath, newpath); - return MapToPosixRet(ret); -} - -int EspFsync(int fd) -{ - int ret = LOS_Sync(fd); - return MapToPosixRet(ret); -} - -struct dir *EspOpendir(const char *path) -{ - return LOS_Opendir(path); -} - -struct dirent *EspReaddir(struct dir *dir) -{ - return LOS_Readdir(dir); -} - -int EspClosedir(struct dir *dir) -{ - int ret = LOS_Closedir(dir); - return MapToPosixRet(ret); -} - -int EspMkdir(const char *path, mode_t mode) -{ - int ret = LOS_Mkdir(path, (int)mode); - return MapToPosixRet(ret); -} - -int EspRmdir(const char *path) -{ - int ret = LOS_Unlink(path); - return MapToPosixRet(ret); -} - -int EspDup(int fd) -{ - int ret = LOS_Dup(fd); - return MapToPosixRet(ret); -} - -int EspDup2(int oldFd, int newFd) -{ - int ret = LOS_Dup2(oldFd, newFd); - return MapToPosixRet(ret); -} - -int EspLstat(const char *path, struct stat *buffer) -{ - return stat(path, buffer); -} - -int EspFstat(int fd, struct stat *buf) -{ - struct file *filep; - int ret; - filep = LOS_AttachFileReady(fd); - if ((filep == NULL) || (filep->f_mp == NULL) || filep->full_path == NULL) { - return VFS_ERROR; - } - ret = stat(filep->full_path, buf); - LOS_DetachFile(filep); - return ret; -} - -int EspFcntl(int fd, int cmd, ...) -{ - struct file *filep = NULL; - va_list ap; - int ret; - (void)va_start(ap, cmd); - - if (fd < CONFIG_NFILE_DESCRIPTORS) { - filep = LOS_AttachFileReady(fd); - ret = LOS_Vfcntl(filep, cmd, ap); - LOS_DetachFile(filep); - } else { - ret = -EBADF; - } - - (void)va_end(ap); - - if (ret < 0) { - ret = VFS_ERROR; - } - return ret; -} - -int EspIoctl(int fd, int func, ...) -{ - int ret; - va_list ap; - (void)va_start(ap, func); - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Ioctl(fd, func, ap); - } else { - ret = -EBADF; - } - - (void)va_end(ap); - return ret; -} - -ssize_t EspReadv(int fd, const struct iovec *iov, int iovcnt) -{ - int i; - int ret; - char *buf = NULL; - char *curBuf = NULL; - char *readBuf = NULL; - size_t bufLen = 0; - size_t bytesToRead; - ssize_t totalBytesRead; - size_t totalLen; - - if ((iov == NULL) || (iovcnt <= 0) || (iovcnt > IOV_MAX_CNT)) { - return VFS_ERROR; - } - - for (i = 0; i < iovcnt; ++i) { - if ((SSIZE_MAX - bufLen) < iov[i].iov_len) { - return VFS_ERROR; - } - bufLen += iov[i].iov_len; - } - if (bufLen == 0) { - return VFS_ERROR; - } - totalLen = bufLen * sizeof(char); - buf = (char *)malloc(totalLen); - if (buf == NULL) { - return VFS_ERROR; - } - - totalBytesRead = read(fd, buf, bufLen); - if ((size_t)totalBytesRead < totalLen) { - totalLen = (size_t)totalBytesRead; - } - curBuf = buf; - for (i = 0; i < iovcnt; ++i) { - readBuf = (char *)iov[i].iov_base; - bytesToRead = iov[i].iov_len; - - size_t lenToRead = totalLen < bytesToRead ? totalLen : bytesToRead; - ret = memcpy_s(readBuf, bytesToRead, curBuf, lenToRead); - if (ret != LOS_OK) { - (void)free(buf); - return VFS_ERROR; - } - if (totalLen < (size_t)bytesToRead) { - break; - } - curBuf += bytesToRead; - totalLen -= bytesToRead; - } - (void)free(buf); - return totalBytesRead; -} - -ssize_t EspWritev(int fd, const struct iovec *iov, int iovcnt) -{ - int i; - int ret; - char *buf = NULL; - char *curBuf = NULL; - char *writeBuf = NULL; - size_t bufLen = 0; - size_t bytesToWrite; - ssize_t totalBytesWritten; - size_t totalLen; - - if ((iov == NULL) || (iovcnt <= 0) || (iovcnt > IOV_MAX_CNT)) { - return VFS_ERROR; - } - - for (i = 0; i < iovcnt; ++i) { - if ((SSIZE_MAX - bufLen) < iov[i].iov_len) { - return VFS_ERROR; - } - bufLen += iov[i].iov_len; - } - if (bufLen == 0) { - return VFS_ERROR; - } - totalLen = bufLen * sizeof(char); - buf = (char *)malloc(totalLen); - if (buf == NULL) { - return VFS_ERROR; - } - curBuf = buf; - for (i = 0; i < iovcnt; ++i) { - writeBuf = (char *)iov[i].iov_base; - bytesToWrite = iov[i].iov_len; - if (((ssize_t)totalLen <= 0) || ((ssize_t)bytesToWrite <= 0)) { - continue; - } - ret = memcpy_s(curBuf, totalLen, writeBuf, bytesToWrite); - if (ret != LOS_OK) { - (void)free(buf); - return VFS_ERROR; - } - curBuf += bytesToWrite; - totalLen -= bytesToWrite; - } - - totalBytesWritten = write(fd, buf, bufLen); - (void)free(buf); - - return totalBytesWritten; -} - -int EspIsatty(int fd) -{ - (void)fd; - return 0; -} - -int EspAccess(const char *path, int amode) -{ - int result; - mode_t mode; - struct stat buf; - - result = stat(path, &buf); - if (result != ENOERR) { - return -1; - } - - mode = buf.st_mode; - if ((unsigned int)amode & R_OK) { - if ((mode & (S_IROTH | S_IRGRP | S_IRUSR)) == 0) { - return -1; - } - } - if ((unsigned int)amode & W_OK) { - if ((mode & (S_IWOTH | S_IWGRP | S_IWUSR)) == 0) { - return -1; - } - } - if ((unsigned int)amode & X_OK) { - if ((mode & (S_IXOTH | S_IXGRP | S_IXUSR)) == 0) { - return -1; - } - } - return 0; -} diff --git a/SmartL_E802/liteos_m/board/fs/hal_vfs.h b/SmartL_E802/liteos_m/board/fs/hal_vfs.h deleted file mode 100644 index a6966eae0088da1810dc8f9c1c8836c1250916b8..0000000000000000000000000000000000000000 --- a/SmartL_E802/liteos_m/board/fs/hal_vfs.h +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _HAL_VFS_H -#define _HAL_VFS_H - -#define _GNU_SOURCE 1 -#include -#include -#include "sys/stat.h" -#include "sys/types.h" -#include "los_config.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#define LOS_MAX_DIR_NAME_LEN 255 -#define LOS_MAX_FILE_NAME_LEN 32 -#define LOS_FS_MAX_NAME_LEN LOS_MAX_FILE_NAME_LEN -#define LOS_MAX_FILES 35 -#define LOS_FD_OFFSET 3 - -#define LOS_MAX_FD (LOS_MAX_FILES) - -#define STDOUT 1 - -struct file; -struct mount_point; -struct dir; -struct dirent; - -#define VFS_ERROR (-1) -#define OK 0 - -#if defined(__GNUC__) || defined(__CC_ARM) -#define VFS_ERRNO_SET(err) (errno = (err)) -#else -#define VFS_ERRNO_SET(err) -#endif - -struct file_ops { - int (*open)(struct file *, const char *, int); - int (*close)(struct file *); - ssize_t (*read)(struct file *, char *, size_t); - ssize_t (*write)(struct file *, const char *, size_t); - off_t (*lseek)(struct file *, off_t, int); - off64_t (*lseek64)(struct file *, off64_t, int); - int (*stat)(struct mount_point *, const char *, struct stat *); - int (*unlink)(struct mount_point *, const char *); - int (*rename)(struct mount_point *, const char *, const char *); - int (*ioctl)(struct file *, int, unsigned long); - int (*sync)(struct file *); - int (*opendir)(struct dir *, const char *); - int (*readdir)(struct dir *, struct dirent *); - int (*closedir)(struct dir *); - int (*mkdir)(struct mount_point *, const char *); -}; - -struct file_system { - const char fs_name[LOS_FS_MAX_NAME_LEN]; - struct file_ops *fs_fops; - struct file_system *fs_next; - volatile uint32_t fs_refs; -}; - -struct mount_point { - struct file_system *m_fs; - struct mount_point *m_next; - const char *m_path; - volatile uint32_t m_refs; - UINT32 m_mutex; - void *m_data; /* used by fs private data for this mount point (like /sdb1, /sdb2), */ -}; - -#define FILE_STATUS_NOT_USED 0 -#define FILE_STATUS_INITING 1 -#define FILE_STATUS_READY 2 -#define FILE_STATUS_CLOSING 3 - -#define VFS_TYPE_FILE 0 -#define VFS_TYPE_DIR 1 - -struct file { - struct file_ops *f_fops; - UINT32 f_flags; - UINT32 f_status; - off_t f_offset; - off64_t f_offset64; - struct mount_point *f_mp; /* can get private mount data here */ - UINT32 f_owner; /* the task that openned this file */ - void *f_data; - const char *full_path; -}; - -struct dirent { - char name[LOS_MAX_DIR_NAME_LEN + 1]; - UINT32 type; - UINT32 size; -}; - -struct dir { - struct mount_point *d_mp; /* can get private mount data here */ - struct dirent d_dent; - off_t d_offset; - void *d_data; -}; - -struct dir *opendir(const char *path); -struct dirent *readdir(struct dir *dir); -int closedir(struct dir *dir); - -int LOS_VfsInit(void); -int LOS_FsRegister(struct file_system *fs); -int LOS_FsUnregister(struct file_system *fs); -int LOS_FsMount(const char *fsname, const char *path, void *data); -int LOS_FsUnmount(const char *path); -int FileToFd(struct file *file); - -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#endif /* _HAL_VFS_H */ diff --git a/SmartL_E802/liteos_m/board/libc/dprintf.c b/SmartL_E802/liteos_m/board/libc/dprintf.c index 6ca0b60b62f37b3078a1393f6309f692f89c2bf7..e870f93276790388819212be9d50d028cabdd03a 100644 --- a/SmartL_E802/liteos_m/board/libc/dprintf.c +++ b/SmartL_E802/liteos_m/board/libc/dprintf.c @@ -30,6 +30,7 @@ */ #include "dprintf.h" +#include #include "stdio.h" #include "pin.h" #include "drv_gpio.h" @@ -37,9 +38,11 @@ #include #include "los_compiler.h" -#define UART_QUEUE_SIZE 64 -#define UART_QUEUE_BUF_MAX_LEN 1 -#define UART_BAUDRATE 115200 +#define PLACEHOLDER_OFFSET 2 +#define HEXADECIMAL_NUM 16 +#define DECIMALISM_NUM 10 +#define BOUNDARY_NUM 9 +#define UART_BAUDRATE 115200 usart_handle_t g_consoleHandle; @@ -53,38 +56,128 @@ void uart_early_init(void) USART_PARITY_NONE, USART_STOP_BITS_1, USART_DATA_BITS_8); } -int fputc(int ch, FILE *stream) +STATIC VOID UartPutc(CHAR c) { - (void)stream; - if (g_consoleHandle == NULL) { - return -1; - } - if (ch == '\n') { - csi_usart_putchar(g_consoleHandle, '\r'); - } - csi_usart_putchar(g_consoleHandle, ch); - return 0; + (VOID)csi_usart_putchar(g_consoleHandle, c); } -int fgetc(FILE *stream) +STATIC VOID MatchedOut(CHAR data, va_list *ap) { - uint8_t ch; - (void)stream; - if (g_consoleHandle == NULL) { - return -1; - } - csi_usart_getchar(g_consoleHandle, &ch); - return ch; -} + CHAR c = 0; + UINT32 v, n; + UINT32 val[100]; + CHAR *s = NULL; + INT32 i = 0; + INT32 j = 0; -int os_critical_enter(unsigned int *lock) -{ - (void)lock; - return 0; + switch (data) { + case 'c': + c = va_arg(*ap, UINT32); + UartPutc(c); + break; + case 's': + s = va_arg(*ap, CHAR*); + while (*s) { + UartPutc(*s++); + } + break; + case 'd': + v = va_arg(*ap, UINT32); + i = 0; + while (v) { + n = v % DECIMALISM_NUM; + val[i] = n; + v = v / DECIMALISM_NUM; + i++; + } + if (i == 0) { + UartPutc('0'); + } + for (j = i - 1; j >= 0; j--) { + UartPutc('0' + val[j]); + } + break; + case 'x': + v = va_arg(*ap, UINT32); + i = 0; + if (v == 0) { + val[i] = 0; + i++; + } + while (v) { + n = v % HEXADECIMAL_NUM; + val[i] = n; + v = v / HEXADECIMAL_NUM; + i++; + } + if (i == 0) { + UartPutc('0'); + } + for (j = i - 1; j >= 0; j--) { + if (val[j] > BOUNDARY_NUM) { + UartPutc('a' + val[j] - DECIMALISM_NUM); + } else { + UartPutc('0' + val[j]); + } + } + break; + case 'p': + v = va_arg(*ap, UINT32); + i = 0; + if (v == 0) { + val[i] = 0; + i++; + } + while (v) { + n = v % HEXADECIMAL_NUM; + val[i] = n; + v = v / HEXADECIMAL_NUM; + i++; + } + UartPutc('0'); + UartPutc('x'); + if (i == 0) { + UartPutc('0'); + } + for (j = i - 1; j >= 0; j--) { + if (val[j] > BOUNDARY_NUM) { + UartPutc('a' + val[j] - DECIMALISM_NUM); + } else { + UartPutc('0' + val[j]); + } + } + break; + default: + break; + } } -int os_critical_exit(unsigned int *lock) +INT32 printf(const CHAR *fmt, ...) { - (void)lock; + va_list ap; + (VOID)va_start(ap, fmt); + while (*fmt != '\0') { + switch (*fmt) { + case '%': + MatchedOut(fmt[1], &ap); + fmt += PLACEHOLDER_OFFSET; + break; + case '\n': + UartPutc('\r'); + UartPutc('\n'); + fmt++; + break; + case '\r': + UartPutc('\r'); + UartPutc('\n'); + fmt++; + break; + default: + UartPutc(*fmt); + fmt++; + break; + } + } + (VOID)va_end(ap); return 0; } diff --git a/SmartL_E802/liteos_m/board/main.c b/SmartL_E802/liteos_m/board/main.c index a2d7d24fde59a057735eb47dada1961828ad3479..247c54738d9be7dfe8fdf98004be08fc5967a36c 100644 --- a/SmartL_E802/liteos_m/board/main.c +++ b/SmartL_E802/liteos_m/board/main.c @@ -31,8 +31,9 @@ #include "stdio.h" #include "soc.h" -#include "task_demo.h" +#include "los_task.h" #include "dprintf.h" +#include "task_demo.h" int g_system_clock = IHS_VALUE; #ifndef CONFIG_SYSTICK_HZ @@ -46,9 +47,22 @@ void SystemInit(void) LITE_OS_SEC_TEXT_INIT int main(void) { + UINT32 ret; + SystemInit(); uart_early_init(); - RunTaskSample(); + ret = LOS_KernelInit(); + if (ret != LOS_OK) { + printf("Liteos kernel init failed! ERROR: 0x%x\n", ret); + } + FileSystemInit(); + + ret = LosAppInit(); + if (ret != LOS_OK) { + printf("LosAppInit failed! ERROR: 0x%x\n", ret); + } + + LOS_Start(); while (1) { __asm volatile("wait"); diff --git a/SmartL_E802/liteos_m/board/target_config.h b/SmartL_E802/liteos_m/board/target_config.h index 9e1e165dd1173b8d9e7eac61d66c22c333229c69..af9cb3d1baab2b5ebfb42bf884725fac2919ca42 100644 --- a/SmartL_E802/liteos_m/board/target_config.h +++ b/SmartL_E802/liteos_m/board/target_config.h @@ -61,21 +61,22 @@ extern "C" { Task module configuration =============================================================================*/ #define LOSCFG_BASE_CORE_TSK_LIMIT 16 -#define LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE (0x600U) -#define LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE (0x800U) -#define LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE (0x400U) +#define LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE (0x1000U) +#define LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE (0x2000U) +#define LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE (0x1000U) #define LOSCFG_BASE_CORE_TIMESLICE 1 -#define LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT 1000 +#define LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT 20000 +#define LOSCFG_BASE_CORE_TSK_DEFAULT_PRIO 15 /*============================================================================= Semaphore module configuration =============================================================================*/ #define LOSCFG_BASE_IPC_SEM 1 -#define LOSCFG_BASE_IPC_SEM_LIMIT 20 +#define LOSCFG_BASE_IPC_SEM_LIMIT 48 /*============================================================================= Mutex module configuration =============================================================================*/ #define LOSCFG_BASE_IPC_MUX 1 -#define LOSCFG_BASE_IPC_MUX_LIMIT 20 +#define LOSCFG_BASE_IPC_MUX_LIMIT 24 /*============================================================================= Queue module configuration =============================================================================*/ @@ -85,8 +86,8 @@ extern "C" { Software timer module configuration =============================================================================*/ #define LOSCFG_BASE_CORE_SWTMR 1 -#define LOSCFG_BASE_CORE_SWTMR_ALIGN 0 -#define LOSCFG_BASE_CORE_SWTMR_LIMIT 16 +#define LOSCFG_BASE_CORE_SWTMR_ALIGN 1 +#define LOSCFG_BASE_CORE_SWTMR_LIMIT 48 /*============================================================================= Exception module configuration =============================================================================*/ diff --git a/SmartL_E802/liteos_m/board/test/task_demo.c b/SmartL_E802/liteos_m/board/test/task_demo.c index 7ee9af0f0b36dc56cac78e7f0d3c02b765ba63e6..f07ec891f60a017d57b9f0919f9d04a11fb6585c 100644 --- a/SmartL_E802/liteos_m/board/test/task_demo.c +++ b/SmartL_E802/liteos_m/board/test/task_demo.c @@ -55,7 +55,7 @@ VOID TaskSampleEntry1(VOID) } } -VOID TaskSample(VOID) +UINT32 LosAppInit(VOID) { UINT32 ret; UINT32 taskID1; @@ -66,7 +66,7 @@ VOID TaskSample(VOID) stTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; stTask.pcName = "TaskSampleEntry1"; stTask.usTaskPrio = TASK1_PRIORITY; - ret = LOS_TaskCreate(&taskID2, &stTask); + ret = LOS_TaskCreate(&taskID1, &stTask); if (ret != LOS_OK) { printf("Task1 create failed\n"); } @@ -74,20 +74,11 @@ VOID TaskSample(VOID) stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskSampleEntry2; stTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; stTask.pcName = "TaskSampleEntry2"; - stTask.usTaskPrio = TASK1_PRIORITY; + stTask.usTaskPrio = TASK2_PRIORITY; ret = LOS_TaskCreate(&taskID2, &stTask); if (ret != LOS_OK) { printf("Task2 create failed\n"); } -} -VOID RunTaskSample(VOID) -{ - UINT32 ret; - ret = LOS_KernelInit(); - FileSystemInit(); - if (ret == LOS_OK) { - TaskSample(); - (VOID)LOS_Start(); - } + return ret; } diff --git a/SmartL_E802/liteos_m/board/test/task_demo.h b/SmartL_E802/liteos_m/board/test/task_demo.h index 7dbb6822488591ea3f1594db63463b3bece8f6ae..850525d768192ff1002d5cf2082cda2a358b8f00 100644 --- a/SmartL_E802/liteos_m/board/test/task_demo.h +++ b/SmartL_E802/liteos_m/board/test/task_demo.h @@ -42,7 +42,7 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -VOID RunTaskSample(VOID); +UINT32 LosAppInit(VOID); #ifdef __cplusplus #if __cplusplus diff --git a/esp32/adapter/hals/utils/file/BUILD.gn b/esp32/adapter/hals/utils/file/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..432b1286f127eac3f4e762e5f942b6dc7519e95b --- /dev/null +++ b/esp32/adapter/hals/utils/file/BUILD.gn @@ -0,0 +1,18 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_library("hal_file_static") { + sources = [ "src/hal_file.c" ] + + include_dirs = [ "//utils/native/lite/hals/file" ] +} diff --git a/SmartL_E802/liteos_m/board/hals/utils/file/hal_file.c b/esp32/adapter/hals/utils/file/src/hal_file.c similarity index 84% rename from SmartL_E802/liteos_m/board/hals/utils/file/hal_file.c rename to esp32/adapter/hals/utils/file/src/hal_file.c index 92fee041189c021c8ce2fbc31d21517f5690b3c9..4be1e1627cfe49d3958a00775ade4a9ae098dfa6 100644 --- a/SmartL_E802/liteos_m/board/hals/utils/file/hal_file.c +++ b/esp32/adapter/hals/utils/file/src/hal_file.c @@ -29,59 +29,52 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include -#include "errno.h" -#include "fcntl.h" +#include "hal_file.h" +#include "securec.h" #include "sys/stat.h" -#include "hal_littlefs.h" -#include "hal_vfs.h" -#include "lfs.h" -#include "lfs_rambd.h" #include "fs_config.h" int HalFileOpen(const char* path, int oflag, int mode) { char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - return EspOpen(tmpPath, oflag, mode); + return open(tmpPath, oflag, mode); } int HalFileClose(int fd) { - return EspClose(fd); + return close(fd); } int HalFileRead(int fd, char *buf, unsigned int len) { - return EspRead(fd, buf, len); + return read(fd, buf, len); } int HalFileWrite(int fd, const char *buf, unsigned int len) { - return EspWrite(fd, buf, len); + return write(fd, buf, len); } int HalFileDelete(const char *path) { char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - return EspUnlink(tmpPath); + return unlink(path); } int HalFileStat(const char *path, unsigned int *fileSize) { char tmpPath[LITTLEFS_MAX_LFN_LEN] = {0}; - struct stat halStat = {0}; + struct stat halStat ; int ret = 0; (void)snprintf_s(tmpPath, LITTLEFS_MAX_LFN_LEN, LITTLEFS_MAX_LFN_LEN, "/littlefs/%s", path); - ret = EspStat(tmpPath, &halStat); + ret = stat(tmpPath, &halStat); *fileSize = halStat.st_size; return ret; } int HalFileSeek(int fd, int offset, unsigned int whence) { - return EspLseek(fd, (off_t)offset, whence); + return lseek(fd, (off_t)offset, whence); } diff --git a/esp32/liteos_m/board/BUILD.gn b/esp32/liteos_m/board/BUILD.gn index f3f8d3ea73861ec4ae35daf403e9d22a7099e3fd..5e652b80c4d03f1cd40961d0b192aa5635dcda0e 100644 --- a/esp32/liteos_m/board/BUILD.gn +++ b/esp32/liteos_m/board/BUILD.gn @@ -11,14 +11,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -static_library("board") { +import("//kernel/liteos_m/liteos.gni") + +kernel_module("bsp_config") { sources = [ "fs/fs_init.c", - "fs/hal_littlefs.c", - "fs/hal_vfs.c", "fs/littlefs_hal.c", "hals/driver/hal_watchdog.c", - "hals/utils/file/hal_file.c", "libc/dprintf.c", "libc/swap.c", "main.c", @@ -27,13 +26,11 @@ static_library("board") { # kernel's testsuites not enabled, use ower's sources += [ "test/task_demo.c" ] } - if (defined(LOSCFG_FS_FAT)) { - sources += [ "fs/ff_gen_drv.c" ] - } include_dirs = [ "include", "hals/driver", "test", + "fs", "//utils/native/lite/include", ] } diff --git a/esp32/liteos_m/board/fs/ff_gen_drv.c b/esp32/liteos_m/board/fs/ff_gen_drv.c deleted file mode 100644 index 9a23bb932fd128b150a8f0c3d335a3aaed4a1f7a..0000000000000000000000000000000000000000 --- a/esp32/liteos_m/board/fs/ff_gen_drv.c +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "ff_gen_drv.h" -#include "fs_config.h" -#include "fatfs.h" - -DiskDrvTypeDef g_diskDrv = { { 0 }, { 0 }, { 0 }, { 0 } }; diff --git a/esp32/liteos_m/board/fs/ff_gen_drv.h b/esp32/liteos_m/board/fs/ff_gen_drv.h deleted file mode 100644 index a52829c57356cfdf31c7b5f7f5c659889c6a60c0..0000000000000000000000000000000000000000 --- a/esp32/liteos_m/board/fs/ff_gen_drv.h +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _FF_GEN_DRV_H -#define _FF_GEN_DRV_H - -#include "stdint.h" -#include "diskio.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -typedef struct { - DSTATUS (*disk_initialize)(BYTE); - DSTATUS (*disk_status)(BYTE); - DSTATUS (*disk_read)(BYTE, BYTE *, DWORD, UINT); - DSTATUS (*disk_write)(BYTE, const BYTE *, DWORD, UINT); - DSTATUS (*disk_ioctl)(BYTE, BYTE, void *); -} DiskioDrvTypeDef; - -typedef struct { - uint8_t initialized[FF_VOLUMES]; - const DiskioDrvTypeDef *drv[FF_VOLUMES]; - uint8_t lun[FF_VOLUMES]; - volatile uint8_t nbr; -} DiskDrvTypeDef; - -extern DiskDrvTypeDef g_diskDrv; -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#endif /* _FF_GEN_DRV_H */ diff --git a/esp32/liteos_m/board/fs/fs_init.c b/esp32/liteos_m/board/fs/fs_init.c index af86951a7dacdba2ad515cd06c428fea35bee1c3..33c8875ba94e6963984cc2dc79f9308350bb8508 100644 --- a/esp32/liteos_m/board/fs/fs_init.c +++ b/esp32/liteos_m/board/fs/fs_init.c @@ -30,29 +30,40 @@ */ #include "fs_init.h" +#include "lfs.h" #include "los_task.h" #include "securec.h" -#include "hal_littlefs.h" +#include "utils_file.h" -#define FS_INIT_TASK_SIZE 0x1000 -#define FS_INIT_TASK_PRIORITY 2 +#define FS_INIT_TASK_SIZE 0x1000 +#define FS_INIT_TASK_PRIORITY 2 +#define LITTLEFS_ROOTDIR_MODE 775 static uint32_t g_taskId; static void FileSystemEntry(void) { - uint32_t ret; + int ret = 0; struct lfs_config *littlefsConfig = LittlefsConfigGet(); - ret = LittlefsInit(0, littlefsConfig); + LittlefsDriverInit(0); + SetDefaultMountPath(0, "/littlefs"); + ret = mount(NULL, "/littlefs", "littlefs", 0, littlefsConfig); if (ret != LOS_OK) { - printf("Littlefs init failed.\n"); + printf("Littlefs init failed 0x%x.\n", ret); + return; + } + + ret = mkdir("/littlefs", LITTLEFS_ROOTDIR_MODE); + if (ret != LOS_OK) { + printf("Mkdir failed 0x%x.\n", ret); + return; } - printf("Littlefs inited.\n"); + printf("Littlefs init successed!\n"); } void FileSystemInit(void) { - uint32_t ret; + int ret = 0; TSK_INIT_PARAM_S taskInitParam; ret = memset_s(&taskInitParam, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S)); diff --git a/esp32/liteos_m/board/fs/fs_init.h b/esp32/liteos_m/board/fs/fs_init.h index ee3773560d814536559e49341d4c398bcaa62764..a4e38809afaccc14fbb9356892f6326cfbe9e0d7 100644 --- a/esp32/liteos_m/board/fs/fs_init.h +++ b/esp32/liteos_m/board/fs/fs_init.h @@ -38,10 +38,6 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -struct spiffs_drv_t* SpiffsConfigGet(void); -struct diskio_drv* FatfsConfigGet(void); -struct lfs_config* LittlefsConfigGet(void); - void FileSystemInit(void); #ifdef __cplusplus diff --git a/esp32/liteos_m/board/fs/hal_littlefs.c b/esp32/liteos_m/board/fs/hal_littlefs.c deleted file mode 100644 index c6254ef9ef1fefb9ef891fe6bb765cc9ca0a44f2..0000000000000000000000000000000000000000 --- a/esp32/liteos_m/board/fs/hal_littlefs.c +++ /dev/null @@ -1,453 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "hal_littlefs.h" -#include -#include -#include -#include "lfs.h" -#include "errno.h" -#include "fcntl.h" -#include "sys/stat.h" -#include "hal_vfs.h" - -typedef struct { - lfs_t *lfs_fs; - lfs_file_t lfs_file[LOS_MAX_FILES]; -} LFS_P; - -LFS_P g_lfs_p; - -static lfs_t *littlefs_ptr; - -static int RetToErrno(int result) -{ - int err = 0; - switch (result) { - case LFS_ERR_OK: - return 0; - case LFS_ERR_IO: - err = EIO; - break; - case LFS_ERR_NOENT: - err = ENOENT; - break; - case LFS_ERR_EXIST: - err = EEXIST; - break; - case LFS_ERR_NOTDIR: - err = ENOTDIR; - break; - case LFS_ERR_ISDIR: - err = EISDIR; - break; - case LFS_ERR_NOTEMPTY: - err = ENOTEMPTY; - break; - case LFS_ERR_BADF: - err = EBADF; - break; - case LFS_ERR_INVAL: - err = EINVAL; - break; - case LFS_ERR_NOSPC: - err = ENOSPC; - break; - case LFS_ERR_NOMEM: - err = ENOMEM; - break; - case LFS_ERR_CORRUPT: - err = ELIBBAD; - break; - default: - err = EIO; - break; - } - VFS_ERRNO_SET(err); - - return -err; -} - -static int LittlefsFlagsGet(int oflags) -{ - int flags = 0; - switch (oflags & O_ACCMODE) { - case O_RDONLY: - flags |= LFS_O_RDONLY; - break; - case O_WRONLY: - flags |= LFS_O_WRONLY; - break; - case O_RDWR: - flags |= LFS_O_RDWR; - break; - default: - break; - } - - if (oflags & O_CREAT) { - flags |= LFS_O_CREAT; - } - - if ((oflags & O_EXCL)) { - flags |= LFS_O_EXCL; - } - - if (oflags & O_TRUNC) { - flags |= LFS_O_TRUNC; - } - - if (oflags & O_APPEND) { - flags |= LFS_O_APPEND; - } - - return flags; -} - -static int LittlefsOperationOpen(struct file *file, const char *pathInMp, int flags) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - if (f == lfs->mlist) { - lfs->mlist = NULL; - } - - int ret = lfs_file_open(lfs, f, pathInMp, LittlefsFlagsGet(flags)); - if (ret == LFS_ERR_OK) { - file->f_data = (void *)&f; - } - - return RetToErrno(ret); -} - -static int LittlefsOperationClose(struct file *file) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - int ret = lfs_file_close(lfs, f); - - return RetToErrno(ret); -} - -static ssize_t LittlefsOperationRead(struct file *file, char *buff, size_t bytes) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - lfs_ssize_t ret; - ret = lfs_file_read(lfs, f, (void *)buff, (lfs_size_t)bytes); - if (ret < 0) { - printf("Failed to read, read size=%d\n", (int)ret); - return RetToErrno((int)ret); - } - return (ssize_t)ret; -} - -static ssize_t LittlefsOperationWrite(struct file *file, const char *buff, size_t bytes) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - - lfs_ssize_t ret; - if ((buff == NULL) || (bytes == 0) || (lfs == NULL) || (f == NULL)) { - return -EINVAL; - } - - ret = lfs_file_write(lfs, f, (const void *)buff, (lfs_size_t)bytes); - if (ret < 0) { - return RetToErrno((int)ret); - } - - file->f_offset = f->pos; - return (ssize_t)ret; -} - -static off_t LittlefsOperationLseek(struct file *file, off_t off, int whence) -{ - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_soff_t ret; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - if (f == NULL) { - return -EINVAL; - } - if ((off > 0 && f->ctz.size < off) || (off < 0 && f->ctz.size < -off)) { - return -EINVAL; - } - ret = lfs_file_seek(lfs, f, (lfs_soff_t)off, whence); - if (ret < 0) { - return RetToErrno((int)ret); - } - return (off_t)ret; -} - -static off64_t LittlefsOperationLseek64(struct file *file, off64_t off, int whence) -{ - return (off64_t)LittlefsOperationLseek(file, (off_t)off, whence); -} - -int LittlefsOperationStat(struct mount_point *mp, const char *pathInMp, struct stat *stat) -{ - struct lfs_info info; - (void)memset_s(stat, sizeof(struct stat), 0, sizeof(struct stat)); - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret; - ret = lfs_stat(lfs, pathInMp, &info); - if (ret == LFS_ERR_OK) { - stat->st_size = info.size; - stat->st_mode = ((info.type == LFS_TYPE_DIR) ? S_IFDIR : S_IFREG); - } - return RetToErrno(ret); -} - -static int LittlefsOperationUlink(struct mount_point *mp, const char *pathInMp) -{ - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret = lfs_remove(lfs, pathInMp); - return RetToErrno(ret); -} - -static int LittlefsOperationRename(struct mount_point *mp, const char *pathInMpOld, const char *pathInMpNew) -{ - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret = lfs_rename(lfs, pathInMpOld, pathInMpNew); - return RetToErrno(ret); -} - -static int LittlefsOperationSync(struct file *file) -{ - int ret; - LFS_P *p = (LFS_P *)file->f_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_file_t *f = &p->lfs_file[FileToFd(file)]; - if ((lfs == NULL) || (f == NULL)) { - return -EINVAL; - } - - ret = lfs_file_sync(lfs, f); - return RetToErrno(ret); -} - -static int LittlefsOperationOpendir(struct dir *dir, const char *path) -{ - int ret; - LFS_P *p = NULL; - lfs_dir_t *lfs_dir = NULL; - lfs_t *lfs = NULL; - - if (dir == NULL) { - printf("Dir is null, open failed.\n"); - return -ENOMEM; - } - - lfs_dir = (lfs_dir_t *)malloc(sizeof(lfs_dir_t)); - (void)memset_s(lfs_dir, sizeof(lfs_dir_t), 0, sizeof(lfs_dir_t)); - p = (LFS_P *)dir->d_mp->m_data; - lfs = p->lfs_fs; - - ret = lfs_dir_open(lfs, lfs_dir, path); - if (ret != LFS_ERR_OK) { - (void)free(lfs_dir); - return RetToErrno(ret); - } - dir->d_data = (void *)lfs_dir; - dir->d_offset = 0; - return LFS_ERR_OK; -} - -static int LittlefsOperationReaddir(struct dir *dir, struct dirent *dent) -{ - LFS_P *p = (LFS_P *)dir->d_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_dir_t *lfs_dir = (lfs_dir_t *)dir->d_data; - struct lfs_info info; - int ret; - (void)memset_s(&info, sizeof(struct lfs_info), 0, sizeof(struct lfs_info)); - if (lfs_dir == NULL) { - return -EINVAL; - } - do { - ret = lfs_dir_read(lfs, lfs_dir, &info); - } while ((ret >= 0) && ((strcmp(info.name, ".") == 0) || (strcmp(info.name, "..") == 0))); - if (ret < 0) { - return ENOENT; - } else { - (void)strncpy_s((char *)dent->name, LOS_MAX_DIR_NAME_LEN, (const char *)info.name, LOS_MAX_FILE_NAME_LEN - 1); - dent->name[LOS_MAX_FILE_NAME_LEN - 1] = '\0'; - dent->size = info.size; - if (info.type == LFS_TYPE_DIR) { - dent->type = VFS_TYPE_DIR; - } else { - dent->type = VFS_TYPE_FILE; - } - } - return LOS_OK; -} - -static int LittlefsOperationClosedir(struct dir *dir) -{ - int ret; - LFS_P *p = (LFS_P *)dir->d_mp->m_data; - lfs_t *lfs = p->lfs_fs; - lfs_dir_t *lfs_dir = (lfs_dir_t *)dir->d_data; - if (lfs_dir == NULL) { - return -EINVAL; - } - ret = lfs_dir_close(lfs, lfs_dir); - if (ret == LFS_ERR_OK) { - (void)free(lfs_dir); - } - return RetToErrno(ret); -} - -static int LittlefsOperationMkdir(struct mount_point *mp, const char *path) -{ - LFS_P *p = (LFS_P *)mp->m_data; - lfs_t *lfs = p->lfs_fs; - int ret; - - ret = lfs_mkdir(lfs, path); - if (ret == LFS_ERR_NOENT) { - int err; - VFS_ERRNO_SET(ENOENT); - err = ENOENT; - return err; - } - return RetToErrno(ret); -} - -static struct file_ops g_littlefsOps = { - LittlefsOperationOpen, - LittlefsOperationClose, - LittlefsOperationRead, - LittlefsOperationWrite, - LittlefsOperationLseek, - LittlefsOperationLseek64, - LittlefsOperationStat, - LittlefsOperationUlink, - LittlefsOperationRename, - NULL, /* ioctl not supported for now */ - LittlefsOperationSync, - LittlefsOperationOpendir, - LittlefsOperationReaddir, - LittlefsOperationClosedir, - LittlefsOperationMkdir - }; - -static struct file_system g_littlefsFs = { "littlefs", &g_littlefsOps, NULL, 0 }; - -int LittlefsMount(const char *path, const struct lfs_config *lfsConfig) -{ - int ret = -1; - int err; - lfs_t *fs = NULL; - - fs = (lfs_t *)malloc(sizeof(lfs_t)); - - g_lfs_p.lfs_fs = fs; - - if (fs == NULL) { - printf("Malloc memory failed.\n"); - goto err_free; - } - - (void)memset_s(fs, sizeof(lfs_t), 0, sizeof(lfs_t)); - - err = lfs_mount(fs, lfsConfig); - if (err == LFS_ERR_CORRUPT) { - (void)lfs_format(fs, lfsConfig); - err = lfs_mount(fs, lfsConfig); - } - if (err != LFS_ERR_OK) { - printf("Format fail.\n"); - goto err_unmount; - } - - ret = LOS_FsMount("littlefs", path, &g_lfs_p); - if (ret == LFS_ERR_OK) { - printf("Littlefs mount at %s done.\n", path); - littlefs_ptr = fs; - return LFS_ERR_OK; - } - printf("Failed to mount.\n"); -err_unmount: - lfs_unmount(fs); -err_free: - if (fs != NULL) { - (void)free(fs); - } - return ret; -} - -int LittlefsUnmout(const char *path) -{ - int ret = LFS_ERR_OK; - if (littlefs_ptr != NULL) { - ret = lfs_unmount(littlefs_ptr); - free(littlefs_ptr); - littlefs_ptr = NULL; - } - (void)LOS_FsUnmount(path); - return ret; -} - -int LittlefsInit(int needErase, const struct lfs_config *lfsConfig) -{ - int ret; - static int littlefsInited = FALSE; - if (littlefsInited) { - return LOS_OK; - } - if (LOS_VfsInit() != LOS_OK) { - return LOS_NOK; - } - if (LOS_FsRegister(&g_littlefsFs) != LOS_OK) { - printf("Failed to register fs.\n"); - return LOS_NOK; - } - printf("Register littlefs done.\n"); - - LittlefsDriverInit(needErase); - - ret = LittlefsMount("/littlefs/", lfsConfig); - if (ret == LFS_ERR_OK) { - littlefsInited = TRUE; - return LOS_OK; - } - - return ret; -} diff --git a/esp32/liteos_m/board/fs/hal_littlefs.h b/esp32/liteos_m/board/fs/hal_littlefs.h deleted file mode 100644 index e2efe04345cc41221b25e1b01450e624f64bbdea..0000000000000000000000000000000000000000 --- a/esp32/liteos_m/board/fs/hal_littlefs.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _HAL_LITTLEFS_H -#define _HAL_LITTLEFS_H - -#include -#include "lfs.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -void LittlefsDriverInit(int needErase); -int LittlefsInit(int needErase, const struct lfs_config *lfsConfig); -int LittlefsMount(const char *path, const struct lfs_config *lfsConfig); -int LittlefsUnmount(const char *path); - -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#endif /* _HAL_LITTLEFS_H */ diff --git a/esp32/liteos_m/board/fs/hal_vfs.c b/esp32/liteos_m/board/fs/hal_vfs.c deleted file mode 100644 index ef901fd992966143303cb1393809132d7cca5128..0000000000000000000000000000000000000000 --- a/esp32/liteos_m/board/fs/hal_vfs.c +++ /dev/null @@ -1,1482 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "hal_vfs.h" -#include -#include -#include -#include -#include "errno.h" -#include "fcntl.h" -#include "los_mux.h" -#include "los_task.h" -#include "los_debug.h" -#include "limits.h" -#include "fs_config.h" - -#ifdef LOSCFG_COMPONENTS_NET_LWIP -#include "lwipopts.h" -#include "lwip/sockets.h" -#endif - -#ifndef CONFIG_NFILE_DESCRIPTORS -#define CONFIG_NFILE_DESCRIPTORS 256 -#endif - -#define LOS_FCNTL (O_NONBLOCK | O_NDELAY | O_APPEND | O_SYNC | FASYNC) -#define IOV_MAX_CNT 4 -#define BASE_PATH 10 - -struct file g_files[LOS_MAX_FILES]; -uint32_t g_fsMutex; -uint32_t g_openfiles; -struct mount_point *g_mountPoints = NULL; -struct file_system *g_fileSystems = NULL; - -int FileToFd(struct file *file) -{ - if (file == NULL) { - return -1; - } - return file - g_files; -} - -static struct file *FdToFile(int fd) -{ - if (fd > LOS_MAX_FD) { - return NULL; - } - return &g_files[fd]; -} - -static struct file *LOS_FileGet(void) -{ - int i; - /* protected by g_fsMutex */ - for (i = LOS_FD_OFFSET - 1; i < LOS_MAX_FILES; i++) { - if (g_files[i].f_status == FILE_STATUS_NOT_USED) { - g_files[i].f_status = FILE_STATUS_INITING; - return &g_files[i]; - } - } - - return NULL; -} - -static struct file *LOS_FileGetNew(int fd) -{ - if (fd > LOS_MAX_FD) { - return NULL; - } - if (g_files[fd].f_status == FILE_STATUS_NOT_USED) { - g_files[fd].f_status = FILE_STATUS_INITING; - return &g_files[fd]; - } - - return NULL; -} - -static void LOS_FilePut(struct file *file) -{ - if (file == NULL) { - return; - } - - file->f_flags = 0; - file->f_fops = NULL; - file->f_data = NULL; - file->f_mp = NULL; - file->f_offset = 0; - file->f_owner = (uint32_t)-1; - file->full_path = NULL; - file->f_status = FILE_STATUS_NOT_USED; -} - -static struct mount_point *LOS_MpFind(const char *path, const char **pathInMp) -{ - struct mount_point *mp = g_mountPoints; - struct mount_point *bestMp = NULL; - int bestMatches = 0; - if (path == NULL) { - return NULL; - } - if (pathInMp != NULL) { - *pathInMp = NULL; - } - while ((mp != NULL) && (mp->m_path != NULL)) { - const char *m_path = mp->m_path; - const char *i_path = path; - const char *t = NULL; - int matches = 0; - do { - while ((*m_path == '/') && (*(m_path + 1) != '/')) { - m_path++; - } - while ((*i_path == '/') && (*(i_path + 1) != '/')) { - i_path++; - } - - t = strchr(m_path, '/'); - if (t == NULL) { - t = strchr(m_path, '\0'); - } - if ((t == m_path) || (t == NULL)) { - break; - } - if (strncmp(m_path, i_path, (size_t)(t - m_path)) != 0) { - goto next; /* this mount point do not match, check next */ - } - - i_path += (t - m_path); - if ((*i_path != '\0') && (*i_path != '/')) { - goto next; - } - - matches += (t - m_path); - m_path += (t - m_path); - } while (*m_path != '\0'); - - if (matches > bestMatches) { - bestMatches = matches; - bestMp = mp; - - while ((*i_path == '/') && (*(i_path + 1) != '/')) { - i_path++; - } - - if (pathInMp != NULL) { - *pathInMp = i_path; - } - } - next: - mp = mp->m_next; - } - return bestMp; -} - -static int LOS_FileOpened(const char *path) -{ - for (int i = LOS_FD_OFFSET; i < g_openfiles; i++) { - if (g_files[i].f_status == FILE_STATUS_READY) { - if (strcmp(g_files[i].full_path, path) == 0) { - return 1; - } - } - } - return 0; -} - -static int LOS_Open(const char *path, int flags) -{ - struct file *file = NULL; - int fd = -1; - const char *pathInMp = NULL; - char *fullPath = NULL; - struct mount_point *mp = NULL; - - if ((path == NULL) || (path[strlen(path) - 1] == '/') || - strlen(path) > LITTLEFS_MAX_LFN_LEN - BASE_PATH) { - return fd; - } - if (LOS_FileOpened(path)) { - return fd; - } - /* prevent fs/mp being removed while opening */ - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - return fd; - } - file = LOS_FileGet(); - mp = LOS_MpFind(path, &pathInMp); - - (void)LOS_MuxPost(g_fsMutex); - - if ((file == NULL) || (mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') || (mp->m_fs->fs_fops == NULL) || - (mp->m_fs->fs_fops->open == NULL)) { - return fd; - } - - if ((LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK)) { - LOS_FilePut(file); - return fd; - } - - file->f_flags = (uint32_t)flags; - file->f_offset = 0; - file->f_data = NULL; - file->f_fops = mp->m_fs->fs_fops; - file->f_mp = mp; - file->f_owner = LOS_CurTaskIDGet(); - file->full_path = path; - - if (file->f_fops->open(file, pathInMp, flags) == 0) { - mp->m_refs++; - g_openfiles++; - fd = FileToFd(file); - file->f_status = FILE_STATUS_READY; /* file now ready to use */ - fullPath = (char *)malloc(strlen(path) + 1); - (void)memset_s(fullPath, strlen(path) + 1, 0, strlen(path) + 1); - (void)memcpy_s(fullPath, strlen(path), path, strlen(path)); - g_files[fd].full_path = fullPath; - } else { - LOS_FilePut(file); - } - (void)LOS_MuxPost(mp->m_mutex); - - return fd; -} - -/* attach to a file and then set new status */ - -static struct file *LOS_AttachFile(int fd, uint32_t status) -{ - struct file *file = NULL; - - if ((fd < 0) || (fd >= LOS_MAX_FD)) { - VFS_ERRNO_SET(EBADF); - return file; - } - - file = FdToFile(fd); - /* - * Prevent file closed after the checking of: - * - * if (file->f_status == FILE_STATUS_READY) - * - * Because our g_files are not privated to one task, it may be operated - * by every task. - * So we should take the mutex of current mount point before operating it, - * but for now we don't know if this file is valid (FILE_STATUS_READY), if - * this file is not valid, the f_mp may be incorrect. so - * we must check the status first, but this file may be closed/removed - * after the checking if the consequence is not correct. - * - * Consider the following code: - * - * LOS_AttachFileReady (...) - * { - * if (file->f_status == FILE_STATUS_READY) - * { - * while (LOS_MuxPend (file->f_mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK); - * - * return file; - * } - * } - * - * It is not safe: - * - * If current task is interrupted by an IRQ just after the checking and then - * a new task is swapped in and the new task just closed this file. - * - * So is acquire first and then check if it is valid: if not, just - * return NULL (which means fail); If yes, the mutex for current mount point - * is acquired. And the close operation will also set task to - * FILE_STATUS_CLOSING to prevent other tasks operate on this file (and also - * prevent other tasks pend on the mutex of this mount point for this file). - * At last is released. And return the file handle (struct file *). - * - * As this logic used in almost all the operation routines, this routine is - * made to reduce the redundant code. - */ - if ((file == NULL) || (file->f_mp == NULL)) { - return NULL; - } - while (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - }; - if (file->f_mp == NULL) { - return file; - } - if (file->f_status == FILE_STATUS_READY) { - while (LOS_MuxPend(file->f_mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK) { - }; - if (status != FILE_STATUS_READY) { - file->f_status = status; - } - } else { - VFS_ERRNO_SET(EBADF); - file = NULL; - } - - (void)LOS_MuxPost(g_fsMutex); - - return file; -} - -static struct file *LOS_AttachFileReady(int fd) -{ - return LOS_AttachFile(fd, FILE_STATUS_READY); -} - -static struct file *LOS_AttachFileWithStatus(int fd, int status) -{ - return LOS_AttachFile(fd, (uint32_t)status); -} - -static void LOS_DetachFile(const struct file *file) -{ - if ((file == NULL) || (file->f_mp == NULL)) { - return; - } - (void)LOS_MuxPost(file->f_mp->m_mutex); -} - -static int LOS_Close(int fd) -{ - struct file *file; - int ret = -1; - - file = LOS_AttachFileWithStatus(fd, FILE_STATUS_CLOSING); - if (file == NULL) { - return ret; - } - - if (file->full_path != NULL) { - (void)free((void *)file->full_path); - } - if ((file->f_fops != NULL) && (file->f_fops->close != NULL)) { - ret = file->f_fops->close(file); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - if ((ret == 0) && (file->f_mp != NULL)) { - file->f_mp->m_refs--; - g_openfiles--; - } - - LOS_DetachFile(file); - - LOS_FilePut(file); - - return ret; -} - -static ssize_t LOS_Read(int fd, char *buff, size_t bytes) -{ - struct file *file = NULL; - ssize_t ret = (ssize_t)-1; - - if ((buff == NULL) || (bytes == 0)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_flags & O_ACCMODE) == O_WRONLY) { - VFS_ERRNO_SET(EACCES); - } else if ((file->f_fops != NULL) && (file->f_fops->read != NULL)) { - ret = file->f_fops->read(file, buff, bytes); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - /* else ret will be -1 */ - - LOS_DetachFile(file); - - return ret; -} - -static ssize_t LOS_Write(int fd, const void *buff, size_t bytes) -{ - struct file *file = NULL; - ssize_t ret = -1; - - if ((buff == NULL) || (bytes == 0)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_flags & O_ACCMODE) == O_RDONLY) { - VFS_ERRNO_SET(EACCES); - } else if ((file->f_fops != NULL) && (file->f_fops->write != NULL)) { - ret = file->f_fops->write(file, buff, bytes); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - /* else ret will be -1 */ - LOS_DetachFile(file); - - return ret; -} - -static off_t LOS_Lseek(int fd, off_t off, int whence) -{ - struct file *file; - off_t ret = -1; - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_fops == NULL) || (file->f_fops->lseek == NULL)) { - ret = file->f_offset; - } else { - ret = file->f_fops->lseek(file, off, whence); - } - - LOS_DetachFile(file); - - return ret; -} - -static off64_t LOS_Lseek64(int fd, off64_t off, int whence) -{ - struct file *file; - off64_t ret = -1; - - file = LOS_AttachFileReady(fd); - if ((file == NULL) || (file->f_fops == NULL)) { - return ret; - } - if (file->f_fops->lseek64 == NULL) { - ret = file->f_offset64; - } else { - ret = file->f_fops->lseek64(file, off, whence); - } - - LOS_DetachFile(file); - - return ret; -} - -static int LOS_Stat(const char *path, struct stat *stat) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - int ret = -1; - - if ((path == NULL) || (stat == NULL)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return ret; - } - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (mp->m_fs->fs_fops->stat != NULL) { - ret = mp->m_fs->fs_fops->stat(mp, pathInMp, stat); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - (void)LOS_MuxPost(g_fsMutex); - - return ret; -} - -static int LOS_Unlink(const char *path) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - int ret = -1; - - if (path == NULL) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); /* prevent the file open/rename */ - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') || (mp->m_fs->fs_fops->unlink == NULL)) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - ret = mp->m_fs->fs_fops->unlink(mp, pathInMp); - - if(ret != 0) { - return -1; - } - (void)LOS_MuxPost(g_fsMutex); - return ret; -} - -static int LOS_Rename(const char *old, const char *new) -{ - struct mount_point *mpOld = NULL; - struct mount_point *mpNew = NULL; - const char *pathInMpOld = NULL; - const char *pathInMpNew = NULL; - int ret = -1; - - if ((old == NULL) || (new == NULL)) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); /* prevent file open/unlink */ - - mpOld = LOS_MpFind(old, &pathInMpOld); - - if (pathInMpOld == NULL) { - VFS_ERRNO_SET(EINVAL); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if ((mpOld == NULL) || (*pathInMpOld == '\0') || (mpOld->m_fs->fs_fops->unlink == NULL)) { - VFS_ERRNO_SET(EINVAL); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - mpNew = LOS_MpFind(new, &pathInMpNew); - if ((mpNew == NULL) || (pathInMpNew == NULL) || (*pathInMpNew == '\0') || (mpNew->m_fs->fs_fops->unlink == NULL)) { - VFS_ERRNO_SET(EINVAL); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (mpOld != mpNew) { - VFS_ERRNO_SET(EXDEV); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (mpOld->m_fs->fs_fops->rename != NULL) { - ret = mpOld->m_fs->fs_fops->rename(mpOld, pathInMpOld, pathInMpNew); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - (void)LOS_MuxPost(g_fsMutex); - return ret; -} - -static int LOS_Ioctl(int fd, int func, ...) -{ - va_list ap; - unsigned long arg; - struct file *file = NULL; - int ret = -1; - - (void)va_start(ap, func); - arg = va_arg(ap, unsigned long); - (void)va_end(ap); - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_fops != NULL) && (file->f_fops->ioctl != NULL)) { - ret = file->f_fops->ioctl(file, func, arg); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - LOS_DetachFile(file); - - return ret; -} - -static int LOS_Sync(int fd) -{ - struct file *file; - int ret = -1; - - file = LOS_AttachFileReady(fd); - if (file == NULL) { - return ret; - } - - if ((file->f_fops != NULL) && (file->f_fops->sync != NULL)) { - ret = file->f_fops->sync(file); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - LOS_DetachFile(file); - - return ret; -} - -static struct dir *LOS_Opendir(const char *path) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - struct dir *dir = NULL; - uint32_t ret; - - if (path == NULL) { - VFS_ERRNO_SET(EINVAL); - return NULL; - } - - dir = (struct dir *)malloc(sizeof(struct dir)); - if (dir == NULL) { - printf("fail to malloc memory in VFS, is needed," - "make sure it is added\n"); - VFS_ERRNO_SET(ENOMEM); - return NULL; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - (void)free(dir); - return NULL; - } - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL)) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - (void)free(dir); - return NULL; - } - - ret = LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER); - - (void)LOS_MuxPost(g_fsMutex); - - if ((ret != LOS_OK) || (mp->m_fs->fs_fops->opendir == NULL)) { - VFS_ERRNO_SET(ENOTSUP); - (void)LOS_MuxPost(mp->m_mutex); - (void)free(dir); - return NULL; - } - - dir->d_mp = mp; - dir->d_offset = 0; - - ret = (uint32_t)mp->m_fs->fs_fops->opendir(dir, pathInMp); - if (ret == 0) { - mp->m_refs++; - } else { - (void)free(dir); - dir = NULL; - } - - (void)LOS_MuxPost(mp->m_mutex); - - return dir; -} - -static struct dirent *LOS_Readdir(struct dir *dir) -{ - struct mount_point *mp = NULL; - struct dirent *ret = NULL; - - if ((dir == NULL) || (dir->d_mp == NULL)) { - VFS_ERRNO_SET(EINVAL); - return NULL; - } - - mp = dir->d_mp; - - if (LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return NULL; - } - - if ((dir->d_mp->m_fs != NULL) && (dir->d_mp->m_fs->fs_fops != NULL) && - (dir->d_mp->m_fs->fs_fops->readdir != NULL)) { - if (dir->d_mp->m_fs->fs_fops->readdir(dir, &dir->d_dent) == 0) { - ret = &dir->d_dent; - } else { - VFS_ERRNO_SET(EBADF); - } - } else { - VFS_ERRNO_SET(ENOTSUP); - } - - (void)LOS_MuxPost(mp->m_mutex); - - return ret; -} - -static int LOS_Closedir(struct dir *dir) -{ - struct mount_point *mp = NULL; - int ret = -1; - - if ((dir == NULL) || (dir->d_mp == NULL)) { - VFS_ERRNO_SET(EBADF); - return ret; - } - - mp = dir->d_mp; - - if (LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return ret; - } - - if ((dir->d_mp->m_fs != NULL) && (dir->d_mp->m_fs->fs_fops != NULL) && - (dir->d_mp->m_fs->fs_fops->closedir != NULL)) { - ret = dir->d_mp->m_fs->fs_fops->closedir(dir); - } else { - VFS_ERRNO_SET(ENOTSUP); - } - (void)LOS_MuxPost(mp->m_mutex); - if (ret == 0) { - mp->m_refs--; - } else { - VFS_ERRNO_SET(EBADF); - } - (void)free(dir); - dir = NULL; - return ret; -} - -static int LOS_Mkdir(const char *path, int mode) -{ - struct mount_point *mp = NULL; - const char *pathInMp = NULL; - int ret = -1; - - (void)mode; - - if (path == NULL) { - VFS_ERRNO_SET(EINVAL); - return ret; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return ret; - } - - mp = LOS_MpFind(path, &pathInMp); - if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) { - VFS_ERRNO_SET(ENOENT); - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - ret = (int)LOS_MuxPend(mp->m_mutex, LOS_WAIT_FOREVER); - - (void)LOS_MuxPost(g_fsMutex); - - if (ret != LOS_OK) { - VFS_ERRNO_SET(EAGAIN); - return -1; - } - - if (mp->m_fs->fs_fops->mkdir != NULL) { - ret = mp->m_fs->fs_fops->mkdir(mp, pathInMp); - } else { - VFS_ERRNO_SET(ENOTSUP); - ret = -1; - } - - (void)LOS_MuxPost(mp->m_mutex); - - return ret; -} - -static int LOS_Dup(int fd) -{ - int ret; - struct file *file1 = NULL; - struct file *file2 = NULL; - const char *mpath = NULL; - struct mount_point *mp = NULL; - - if ((fd < 0) || (fd > LOS_MAX_FD)) { - VFS_ERRNO_SET(EBADF); - return LOS_NOK; - } - - file1 = LOS_AttachFileReady(fd); - if (file1 == NULL) { - return LOS_NOK; - } - - file2 = LOS_FileGet(); - if (file2 == NULL) { - LOS_DetachFile(file1); - VFS_ERRNO_SET(ENFILE); - return LOS_NOK; - } - - file2->f_flags = file1->f_flags; - file2->f_status = file1->f_status; - file2->f_offset = file1->f_offset; - file2->f_owner = file1->f_owner; - file2->f_data = file1->f_data; - file2->f_fops = file1->f_fops; - file2->f_mp = file1->f_mp; - - if ((file1->f_mp == NULL) || (file1->f_mp->m_path == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - mp = LOS_MpFind(file1->f_mp->m_path, &mpath); - if ((mp == NULL) || (file1->f_fops == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - ret = file1->f_fops->open(file2, mpath, file1->f_flags); - LOS_DetachFile(file1); - return ret; -} - -static int LOS_Dup2(int oldFd, int newFd) -{ - struct file *file1 = NULL; - struct file *file2 = NULL; - const char *mpath = NULL; - struct mount_point *mp = NULL; - - file1 = LOS_AttachFileReady(oldFd); - if (file1 == NULL) { - return LOS_NOK; - } - - file2 = LOS_AttachFileReady(newFd); - if (file2 != NULL) { - LOS_DetachFile(file1); - LOS_DetachFile(file2); - (void)LOS_Close(newFd); - return LOS_NOK; - } - - file2 = LOS_FileGetNew(newFd); - if (file2 == NULL) { - VFS_ERRNO_SET(ENFILE); - printf("files no free!\n"); - LOS_DetachFile(file1); - return LOS_NOK; - } - if (oldFd == newFd) { - LOS_DetachFile(file1); - return oldFd; - } - - file2->f_flags = file1->f_flags; - file2->f_status = file1->f_status; - file2->f_offset = file1->f_offset; - file2->f_owner = file1->f_owner; - file2->f_data = file1->f_data; - file2->f_fops = file1->f_fops; - file2->f_mp = file1->f_mp; - - if ((file1->f_mp == NULL) || (file1->f_mp->m_path == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - mp = LOS_MpFind(file1->f_mp->m_path, &mpath); - if ((mp == NULL) || (file1->f_fops == NULL)) { - LOS_DetachFile(file1); - return LOS_NOK; - } - file1->f_fops->open(file2, mpath, file1->f_flags); - LOS_DetachFile(file1); - - return newFd; -} - -static int LOS_Vfcntl(struct file *filep, int cmd, va_list ap) -{ - int ret; - int fd; - uint32_t flags; - - if ((filep == NULL) || (filep->f_fops == NULL)) { - return -EBADF; - } - - if (cmd == F_DUPFD) { - fd = FileToFd(filep); - ret = LOS_Dup(fd); - } else if (cmd == F_GETFL) { - ret = (int)(filep->f_flags); - } else if (cmd == F_SETFL) { - flags = (uint32_t)va_arg(ap, int); - flags &= LOS_FCNTL; - filep->f_flags &= ~LOS_FCNTL; - filep->f_flags |= flags; - ret = LOS_OK; - } else { - ret = -ENOSYS; - } - return ret; -} - -static int LOS_FsNameCheck(const char *name) -{ - char ch; - int len = 0; - - do { - ch = *(name++); - - if (ch == '\0') { - break; - } - - if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || - (ch == '_') || (ch == '-')) { - len++; - if (len == LOS_FS_MAX_NAME_LEN) { - return LOS_NOK; - } - continue; - } - } while (1); - - return len == 0 ? LOS_NOK : LOS_OK; -} - -static struct file_system *LOS_FsFind(const char *name) -{ - struct file_system *fs; - - for (fs = g_fileSystems; fs != NULL; fs = fs->fs_next) { - if (strncmp(fs->fs_name, name, LOS_FS_MAX_NAME_LEN) == 0) { - break; - } - } - - return fs; -} - -int LOS_FsRegister(struct file_system *fs) -{ - if ((fs == NULL) || (fs->fs_fops == NULL) || (fs->fs_fops->open == NULL)) { - return LOS_NOK; - } - - if (LOS_FsNameCheck(fs->fs_name) != LOS_OK) { - return LOS_NOK; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - return LOS_NOK; - } - - if (LOS_FsFind(fs->fs_name) != NULL) { - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - fs->fs_next = g_fileSystems; - g_fileSystems = fs; - - (void)LOS_MuxPost(g_fsMutex); - - return LOS_OK; -} - -int LOS_FsUnregister(struct file_system *fs) -{ - struct file_system *prev = NULL; - int ret = LOS_OK; - - if (fs == NULL) { - return LOS_NOK; - } - - if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { - return LOS_NOK; - } - - if (fs->fs_refs > 0) { - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (g_fileSystems == fs) { - g_fileSystems = fs->fs_next; - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - prev = g_fileSystems; - - while (prev != NULL) { - if (prev->fs_next == fs) { - break; - } - - prev = prev->fs_next; - } - - if (prev == NULL) { - ret = LOS_NOK; - } else { - prev->fs_next = fs->fs_next; - } - - (void)LOS_MuxPost(g_fsMutex); - return ret; -} - -int LOS_FsMount(const char *fsname, const char *path, void *data) -{ - struct file_system *fs = NULL; - struct mount_point *mp = NULL; - const char *tmp = NULL; - int ret; - if ((fsname == NULL) || (path == NULL) || (path[0] != '/')) { - return LOS_NOK; - } - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); - - fs = LOS_FsFind(fsname); - if (fs == NULL) { - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp = LOS_MpFind(path, &tmp); - if ((mp != NULL) && (tmp != NULL) && (*tmp == '\0')) { - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp = malloc(sizeof(struct mount_point)); - if (mp == NULL) { - printf("Fail to malloc memory in vfs\n"); - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - ret = memset_s(mp, sizeof(struct mount_point), 0, sizeof(struct mount_point)); - if (ret != LOS_OK) { - (void)free(mp); - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp->m_fs = fs; - mp->m_path = path; - mp->m_data = data; - mp->m_refs = 0; - - if (LOS_MuxCreate(&mp->m_mutex) != LOS_OK) { - (void)free(mp); - (void)LOS_MuxPost(g_fsMutex); - return LOS_NOK; - } - - mp->m_next = g_mountPoints; - g_mountPoints = mp; - - fs->fs_refs++; - (void)LOS_MuxPost(g_fsMutex); - - return LOS_OK; -} - -int LOS_FsUnmount(const char *path) -{ - struct mount_point *mp = NULL; - struct mount_point *prev = NULL; - const char *tmp = NULL; - int ret = LOS_NOK; - - if (path == NULL) { - return ret; - } - - (void)LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER); - - mp = LOS_MpFind(path, &tmp); - if ((mp == NULL) || (tmp == NULL) || (*tmp != '\0') || (mp->m_refs != 0)) { - (void)LOS_MuxPost(g_fsMutex); - return ret; - } - - if (g_mountPoints == mp) { - g_mountPoints = mp->m_next; - } else { - for (prev = g_mountPoints; prev != NULL; prev = prev->m_next) { - if (prev->m_next != mp) { - continue; - } - - prev->m_next = mp->m_next; - break; - } - } - - (void)LOS_MuxDelete(mp->m_mutex); - mp->m_fs->fs_refs--; - (void)free(mp); - (void)LOS_MuxPost(g_fsMutex); - return LOS_OK; -} - -int LOS_VfsInit(void) -{ - if (LOS_MuxCreate(&g_fsMutex) == LOS_OK) { - return LOS_OK; - } - - printf("Vfs init fail!\n"); - - return LOS_NOK; -} - - -static int MapToPosixRet(int ret) -{ - return ((ret) < 0 ? -1 : (ret)); -} - -int EspOpen(const char *path, int flags, ...) -{ - int ret = LOS_Open(path, flags); - return MapToPosixRet(ret); -} - -int EspClose(int fd) -{ - int ret; - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Close(fd); - } else { - ret = -1; - } - return MapToPosixRet(ret); -} - -ssize_t EspRead(int fd, void *buff, size_t bytes) -{ - ssize_t ret; - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Read(fd, buff, bytes); - } else { - ret = -1; - } - return MapToPosixRet(ret); -} - -ssize_t EspWrite(int fd, const void *buff, size_t bytes) -{ - ssize_t ret; - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Write(fd, buff, bytes); - } else { - ret = -1; - } - return MapToPosixRet(ret); -} - -off_t EspLseek(int fd, off_t off, int whence) -{ - off_t ret = LOS_Lseek(fd, off, whence); - return MapToPosixRet(ret); -} - -off64_t EspLseek64(int fd, off64_t off, int whence) -{ - off64_t ret = LOS_Lseek64(fd, off, whence); - return MapToPosixRet(ret); -} - -int EspStat(const char *path, struct stat *stat) -{ - int ret = LOS_Stat(path, stat); - return MapToPosixRet(ret); -} - -int EspUnlink(const char *path) -{ - int ret = LOS_Unlink(path); - return MapToPosixRet(ret); -} - -int EspRename(const char *oldpath, const char *newpath) -{ - int ret = LOS_Rename(oldpath, newpath); - return MapToPosixRet(ret); -} - -int EspFsync(int fd) -{ - int ret = LOS_Sync(fd); - return MapToPosixRet(ret); -} - -struct dir *EspOpendir(const char *path) -{ - return LOS_Opendir(path); -} - -struct dirent *EspReaddir(struct dir *dir) -{ - return LOS_Readdir(dir); -} - -int EspClosedir(struct dir *dir) -{ - int ret = LOS_Closedir(dir); - return MapToPosixRet(ret); -} - -int EspMkdir(const char *path, mode_t mode) -{ - int ret = LOS_Mkdir(path, (int)mode); - return MapToPosixRet(ret); -} - -int EspRmdir(const char *path) -{ - int ret = LOS_Unlink(path); - return MapToPosixRet(ret); -} - -int EspDup(int fd) -{ - int ret = LOS_Dup(fd); - return MapToPosixRet(ret); -} - -int EspDup2(int oldFd, int newFd) -{ - int ret = LOS_Dup2(oldFd, newFd); - return MapToPosixRet(ret); -} - -int EspLstat(const char *path, struct stat *buffer) -{ - return stat(path, buffer); -} - -int EspFstat(int fd, struct stat *buf) -{ - struct file *filep; - int ret; - filep = LOS_AttachFileReady(fd); - if ((filep == NULL) || (filep->f_mp == NULL) || filep->full_path == NULL) { - return VFS_ERROR; - } - ret = stat(filep->full_path, buf); - LOS_DetachFile(filep); - return ret; -} - -int EspFcntl(int fd, int cmd, ...) -{ - struct file *filep = NULL; - va_list ap; - int ret; - (void)va_start(ap, cmd); - - if (fd < CONFIG_NFILE_DESCRIPTORS) { - filep = LOS_AttachFileReady(fd); - ret = LOS_Vfcntl(filep, cmd, ap); - LOS_DetachFile(filep); - } else { - ret = -EBADF; - } - - (void)va_end(ap); - - if (ret < 0) { - ret = VFS_ERROR; - } - return ret; -} - -int EspIoctl(int fd, int func, ...) -{ - int ret; - va_list ap; - (void)va_start(ap, func); - if (fd < CONFIG_NFILE_DESCRIPTORS) { - ret = LOS_Ioctl(fd, func, ap); - } else { - ret = -EBADF; - } - - (void)va_end(ap); - return ret; -} - -ssize_t EspReadv(int fd, const struct iovec *iov, int iovcnt) -{ - int i; - int ret; - char *buf = NULL; - char *curBuf = NULL; - char *readBuf = NULL; - size_t bufLen = 0; - size_t bytesToRead; - ssize_t totalBytesRead; - size_t totalLen; - - if ((iov == NULL) || (iovcnt <= 0) || (iovcnt > IOV_MAX_CNT)) { - return VFS_ERROR; - } - - for (i = 0; i < iovcnt; ++i) { - if ((SSIZE_MAX - bufLen) < iov[i].iov_len) { - return VFS_ERROR; - } - bufLen += iov[i].iov_len; - } - if (bufLen == 0) { - return VFS_ERROR; - } - totalLen = bufLen * sizeof(char); - buf = (char *)malloc(totalLen); - if (buf == NULL) { - return VFS_ERROR; - } - - totalBytesRead = read(fd, buf, bufLen); - if ((size_t)totalBytesRead < totalLen) { - totalLen = (size_t)totalBytesRead; - } - curBuf = buf; - for (i = 0; i < iovcnt; ++i) { - readBuf = (char *)iov[i].iov_base; - bytesToRead = iov[i].iov_len; - - size_t lenToRead = totalLen < bytesToRead ? totalLen : bytesToRead; - ret = memcpy_s(readBuf, bytesToRead, curBuf, lenToRead); - if (ret != LOS_OK) { - (void)free(buf); - return VFS_ERROR; - } - if (totalLen < (size_t)bytesToRead) { - break; - } - curBuf += bytesToRead; - totalLen -= bytesToRead; - } - (void)free(buf); - return totalBytesRead; -} - -ssize_t EspWritev(int fd, const struct iovec *iov, int iovcnt) -{ - int i; - int ret; - char *buf = NULL; - char *curBuf = NULL; - char *writeBuf = NULL; - size_t bufLen = 0; - size_t bytesToWrite; - ssize_t totalBytesWritten; - size_t totalLen; - - if ((iov == NULL) || (iovcnt <= 0) || (iovcnt > IOV_MAX_CNT)) { - return VFS_ERROR; - } - - for (i = 0; i < iovcnt; ++i) { - if ((SSIZE_MAX - bufLen) < iov[i].iov_len) { - return VFS_ERROR; - } - bufLen += iov[i].iov_len; - } - if (bufLen == 0) { - return VFS_ERROR; - } - totalLen = bufLen * sizeof(char); - buf = (char *)malloc(totalLen); - if (buf == NULL) { - return VFS_ERROR; - } - curBuf = buf; - for (i = 0; i < iovcnt; ++i) { - writeBuf = (char *)iov[i].iov_base; - bytesToWrite = iov[i].iov_len; - if (((ssize_t)totalLen <= 0) || ((ssize_t)bytesToWrite <= 0)) { - continue; - } - ret = memcpy_s(curBuf, totalLen, writeBuf, bytesToWrite); - if (ret != LOS_OK) { - (void)free(buf); - return VFS_ERROR; - } - curBuf += bytesToWrite; - totalLen -= bytesToWrite; - } - - totalBytesWritten = write(fd, buf, bufLen); - (void)free(buf); - - return totalBytesWritten; -} - -int EspIsatty(int fd) -{ - (void)fd; - return 0; -} - -int EspAccess(const char *path, int amode) -{ - int result; - mode_t mode; - struct stat buf; - - result = stat(path, &buf); - if (result != ENOERR) { - return -1; - } - - mode = buf.st_mode; - if ((unsigned int)amode & R_OK) { - if ((mode & (S_IROTH | S_IRGRP | S_IRUSR)) == 0) { - return -1; - } - } - if ((unsigned int)amode & W_OK) { - if ((mode & (S_IWOTH | S_IWGRP | S_IWUSR)) == 0) { - return -1; - } - } - if ((unsigned int)amode & X_OK) { - if ((mode & (S_IXOTH | S_IXGRP | S_IXUSR)) == 0) { - return -1; - } - } - return 0; -} diff --git a/esp32/liteos_m/board/fs/hal_vfs.h b/esp32/liteos_m/board/fs/hal_vfs.h deleted file mode 100644 index a6966eae0088da1810dc8f9c1c8836c1250916b8..0000000000000000000000000000000000000000 --- a/esp32/liteos_m/board/fs/hal_vfs.h +++ /dev/null @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. - * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, this list of - * conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, this list - * of conditions and the following disclaimer in the documentation and/or other materials - * provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors may be used - * to endorse or promote products derived from this software without specific prior written - * permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, - * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _HAL_VFS_H -#define _HAL_VFS_H - -#define _GNU_SOURCE 1 -#include -#include -#include "sys/stat.h" -#include "sys/types.h" -#include "los_config.h" - -#ifdef __cplusplus -#if __cplusplus -extern "C" { -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#define LOS_MAX_DIR_NAME_LEN 255 -#define LOS_MAX_FILE_NAME_LEN 32 -#define LOS_FS_MAX_NAME_LEN LOS_MAX_FILE_NAME_LEN -#define LOS_MAX_FILES 35 -#define LOS_FD_OFFSET 3 - -#define LOS_MAX_FD (LOS_MAX_FILES) - -#define STDOUT 1 - -struct file; -struct mount_point; -struct dir; -struct dirent; - -#define VFS_ERROR (-1) -#define OK 0 - -#if defined(__GNUC__) || defined(__CC_ARM) -#define VFS_ERRNO_SET(err) (errno = (err)) -#else -#define VFS_ERRNO_SET(err) -#endif - -struct file_ops { - int (*open)(struct file *, const char *, int); - int (*close)(struct file *); - ssize_t (*read)(struct file *, char *, size_t); - ssize_t (*write)(struct file *, const char *, size_t); - off_t (*lseek)(struct file *, off_t, int); - off64_t (*lseek64)(struct file *, off64_t, int); - int (*stat)(struct mount_point *, const char *, struct stat *); - int (*unlink)(struct mount_point *, const char *); - int (*rename)(struct mount_point *, const char *, const char *); - int (*ioctl)(struct file *, int, unsigned long); - int (*sync)(struct file *); - int (*opendir)(struct dir *, const char *); - int (*readdir)(struct dir *, struct dirent *); - int (*closedir)(struct dir *); - int (*mkdir)(struct mount_point *, const char *); -}; - -struct file_system { - const char fs_name[LOS_FS_MAX_NAME_LEN]; - struct file_ops *fs_fops; - struct file_system *fs_next; - volatile uint32_t fs_refs; -}; - -struct mount_point { - struct file_system *m_fs; - struct mount_point *m_next; - const char *m_path; - volatile uint32_t m_refs; - UINT32 m_mutex; - void *m_data; /* used by fs private data for this mount point (like /sdb1, /sdb2), */ -}; - -#define FILE_STATUS_NOT_USED 0 -#define FILE_STATUS_INITING 1 -#define FILE_STATUS_READY 2 -#define FILE_STATUS_CLOSING 3 - -#define VFS_TYPE_FILE 0 -#define VFS_TYPE_DIR 1 - -struct file { - struct file_ops *f_fops; - UINT32 f_flags; - UINT32 f_status; - off_t f_offset; - off64_t f_offset64; - struct mount_point *f_mp; /* can get private mount data here */ - UINT32 f_owner; /* the task that openned this file */ - void *f_data; - const char *full_path; -}; - -struct dirent { - char name[LOS_MAX_DIR_NAME_LEN + 1]; - UINT32 type; - UINT32 size; -}; - -struct dir { - struct mount_point *d_mp; /* can get private mount data here */ - struct dirent d_dent; - off_t d_offset; - void *d_data; -}; - -struct dir *opendir(const char *path); -struct dirent *readdir(struct dir *dir); -int closedir(struct dir *dir); - -int LOS_VfsInit(void); -int LOS_FsRegister(struct file_system *fs); -int LOS_FsUnregister(struct file_system *fs); -int LOS_FsMount(const char *fsname, const char *path, void *data); -int LOS_FsUnmount(const char *path); -int FileToFd(struct file *file); - -#ifdef __cplusplus -#if __cplusplus -} -#endif /* __cplusplus */ -#endif /* __cplusplus */ - -#endif /* _HAL_VFS_H */ diff --git a/esp32/liteos_m/board/fs/littlefs_hal.c b/esp32/liteos_m/board/fs/littlefs_hal.c index 5b518dde16ddad469b2aff72d0999cd1b6970b00..78a58a8285b72ac74dff6fb2672b0ce150dafc79 100644 --- a/esp32/liteos_m/board/fs/littlefs_hal.c +++ b/esp32/liteos_m/board/fs/littlefs_hal.c @@ -29,15 +29,11 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include #include #include "los_memory.h" #include "lfs_rambd.h" #include "lfs.h" -#define LITTLEFS_PHYS_ADDR 0x00 -#define LITTLEFS_PHYS_SIZE (64 * 1024) - #define READ_SIZE 16 #define PROG_SIZE 16 #define BLOCK_SIZE 256 diff --git a/esp32/liteos_m/board/libc/dprintf.c b/esp32/liteos_m/board/libc/dprintf.c index cc56a799490acbf178c4729eca0269b2206c8b3b..4e0d1570a2e46ddff3c98da66515181ab8041ba3 100644 --- a/esp32/liteos_m/board/libc/dprintf.c +++ b/esp32/liteos_m/board/libc/dprintf.c @@ -38,12 +38,12 @@ #define DECIMALISM_NUM 10 #define BOUNDARY_NUM 9 -VOID UartPutc(CHAR c) +STATIC VOID UartPutc(CHAR c) { (VOID)uart_tx_one_char(c); } -VOID MatchedOut(CHAR data, va_list *ap) +STATIC VOID MatchedOut(CHAR data, va_list *ap) { CHAR c = 0; UINT32 v, n; diff --git a/esp32/liteos_m/board/main.c b/esp32/liteos_m/board/main.c index cc64377b0e8d0082b4b31568115bb7e131e67775..07f7e4e6ae1fadf362088afce0f9ff5d3aa56c2c 100644 --- a/esp32/liteos_m/board/main.c +++ b/esp32/liteos_m/board/main.c @@ -44,12 +44,25 @@ VOID BssClean(VOID) INT32 main(VOID) { + UINT32 ret; + BssClean(); WdtDisable(); __asm__ __volatile__("mov sp, %0" : : "r"(&__init_stack_e)); - RunTaskSample(); + ret = LOS_KernelInit(); + if (ret != LOS_OK) { + printf("Liteos kernel init failed! ERROR: 0x%x\n", ret); + } + FileSystemInit(); + + ret = LosAppInit(); + if (ret != LOS_OK) { + printf("LosAppInit failed! ERROR: 0x%x\n", ret); + } + + LOS_Start(); return 0; } diff --git a/esp32/liteos_m/board/test/task_demo.c b/esp32/liteos_m/board/test/task_demo.c index c6a850c5f3c91be0ab72b13977d8a24ed6890824..f07ec891f60a017d57b9f0919f9d04a11fb6585c 100644 --- a/esp32/liteos_m/board/test/task_demo.c +++ b/esp32/liteos_m/board/test/task_demo.c @@ -36,6 +36,9 @@ #include "los_task.h" #include "utils_file.h" +#define TASK1_PRIORITY 6 +#define TASK2_PRIORITY 7 + VOID TaskSampleEntry2(VOID) { while (1) { @@ -52,7 +55,7 @@ VOID TaskSampleEntry1(VOID) } } -VOID TaskSample(VOID) +UINT32 LosAppInit(VOID) { UINT32 ret; UINT32 taskID1; @@ -62,8 +65,8 @@ VOID TaskSample(VOID) stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskSampleEntry1; stTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; stTask.pcName = "TaskSampleEntry1"; - stTask.usTaskPrio = 2; /* Os task priority is 2 */ - ret = LOS_TaskCreate(&taskID2, &stTask); + stTask.usTaskPrio = TASK1_PRIORITY; + ret = LOS_TaskCreate(&taskID1, &stTask); if (ret != LOS_OK) { printf("Task1 create failed\n"); } @@ -71,20 +74,11 @@ VOID TaskSample(VOID) stTask.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskSampleEntry2; stTask.uwStackSize = LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE; stTask.pcName = "TaskSampleEntry2"; - stTask.usTaskPrio = 2; /* Os task priority is 2 */ + stTask.usTaskPrio = TASK2_PRIORITY; ret = LOS_TaskCreate(&taskID2, &stTask); if (ret != LOS_OK) { printf("Task2 create failed\n"); } -} -VOID RunTaskSample(VOID) -{ - UINT32 ret; - ret = LOS_KernelInit(); - FileSystemInit(); - if (ret == LOS_OK) { - TaskSample(); - (VOID)LOS_Start(); - } + return ret; } diff --git a/esp32/liteos_m/board/test/task_demo.h b/esp32/liteos_m/board/test/task_demo.h index 7dbb6822488591ea3f1594db63463b3bece8f6ae..850525d768192ff1002d5cf2082cda2a358b8f00 100644 --- a/esp32/liteos_m/board/test/task_demo.h +++ b/esp32/liteos_m/board/test/task_demo.h @@ -42,7 +42,7 @@ extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ -VOID RunTaskSample(VOID); +UINT32 LosAppInit(VOID); #ifdef __cplusplus #if __cplusplus diff --git a/esp32/liteos_m/config.gni b/esp32/liteos_m/config.gni index 8da4c51d7c8711c7bc4d19f29de9c746255705a5..47595c0e89745f26d938f9e58c73d1d6b768fd47 100644 --- a/esp32/liteos_m/config.gni +++ b/esp32/liteos_m/config.gni @@ -67,6 +67,7 @@ board_cflags = [ "-Wno-parentheses", "-Wno-pointer-sign", "-D__FPU_PRESENT", + "-D__NEED_uintptr_t", ] board_cxx_flags = board_cflags