Преглед изворни кода

Merge branch 'master' of https://gitee.com/openLuat/LuatOS

alienwalker пре 7 месеци
родитељ
комит
081200109c

+ 7 - 0
components/serialization/protobuf/luat_lib_protobuf.c

@@ -2233,6 +2233,13 @@ LUALIB_API int luaopen_protobuf(lua_State *L) {
     return 1;
 }
 
+void luat_heap_free(void* ptr);
+void luat_heap_free_pb(void* ptr) {
+    if (ptr) {
+        luat_heap_free(ptr);
+    }
+}
+
 PB_NS_END
 
 /* cc: flags+='-O3 -ggdb -pedantic -std=c90 -Wall -Wextra --coverage'

+ 2 - 2
components/serialization/protobuf/pb.h

@@ -43,10 +43,10 @@
 #include <limits.h>
 
 void* luat_heap_malloc(size_t len);
-void  luat_heap_free(void* ptr);
+void  luat_heap_free_pb(void* ptr);
 
 #define os_malloc luat_heap_malloc
-#define os_free luat_heap_free
+#define os_free luat_heap_free_pb
 
 PB_NS_BEGIN
 

+ 464 - 45
luat/vfs/luat_fs_mem.c

@@ -5,6 +5,8 @@
 #define LUAT_LOG_TAG "fs"
 #include "luat_log.h"
 
+#if 0
+
 #define BLOCK_SIZE 4096
 
 typedef struct ram_file_block
@@ -138,29 +140,10 @@ FILE* luat_vfs_ram_fopen(void* userdata, const char *filename, const char *mode)
 }
 
 int luat_vfs_ram_getc(void* userdata, FILE* stream) {
-    (void)userdata;
-    //LLOGD("getc %p %p", userdata, stream);
-    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
-    //LLOGD("getc %p %p %d %d", userdata, stream, fd->offset, fd->size);
-    if (fd->fid < 0 || fd->fid >= RAM_FILE_MAX) {
-        return -1;
-    }
-    if (files[fd->fid] == NULL) {
-        return -1;
-    }
-    ram_file_block_t* block = files[fd->fid]->head;
-    size_t offset = fd->offset;
-    while (block) {
-        if (offset < BLOCK_SIZE) {
-            uint8_t c = block->data[offset];
-            if (c == '\0') {
-                return -1;
-            }
-            fd->offset++;
+    uint8_t c = 0;
+    size_t len = luat_vfs_ram_fread(userdata, &c, 1, 1, stream);
+    if (len == 1) {
             return c;
-        }
-        offset -= BLOCK_SIZE;
-        block = block->next;
     }
     return -1;
 }
@@ -213,17 +196,35 @@ size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb,
     (void)userdata;
     luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
     size_t read_size = size * nmemb;
+
+    // 如果偏移量已经超出文件大小
     if (fd->offset >= files[fd->fid]->size) {
         return 0;
     }
-    if (fd->offset + read_size >= files[fd->fid]->size) {
+
+    // 如果读取的大小超出文件剩余大小,调整读取大小
+    if (fd->offset + read_size > files[fd->fid]->size) {
         read_size = files[fd->fid]->size - fd->offset;
     }
+
+    // 找到offset对应的起始block
     ram_file_block_t* block = files[fd->fid]->head;
     size_t offset = fd->offset;
+    while (block != NULL && offset >= BLOCK_SIZE) {
+        offset -= BLOCK_SIZE;
+        block = block->next;
+    }
+
+    // 如果没有找到对应的block
+    if (block == NULL) {
+        LLOGW("no block for offset %d", fd->offset);
+        return 0;
+    }
+
+    // 开始读取
     uint8_t* dst = (uint8_t*)ptr;
     size_t bytes_read = 0; // 用于记录实际读取的字节数
-    while (block && read_size > 0) {
+    while (block != NULL && read_size > 0) {
         size_t copy_size = BLOCK_SIZE - (offset % BLOCK_SIZE);
         if (copy_size > read_size) {
             copy_size = read_size;
@@ -237,6 +238,7 @@ size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb,
             block = block->next;
         }
     }
+
     fd->offset += bytes_read; // 更新文件偏移量
     return bytes_read; // 返回实际读取的字节数
 }
@@ -249,30 +251,56 @@ size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t
         LLOGW("readonly fd %d!! path %s", fd->fid, files[fd->fid]->name);
         return 0;
     }
+
+    // 计算最终需要的总大小
+    size_t total_size = fd->offset + write_size;
+    size_t current_size = files[fd->fid]->size; // 当前文件大小
+
+    // 先补齐block
     ram_file_block_t* block = files[fd->fid]->head;
+    size_t block_offset = 0; // 当前block的偏移量
+
+    // 遍历现有的block,直到block_offset达到total_size
+    while (block != NULL && block_offset < total_size) {
+        block_offset += BLOCK_SIZE;
+        block = block->next;
+    }
+
+    // 如果block_offset小于total_size,继续分配新的block
+    while (block_offset < total_size) {
+        block = luat_heap_malloc(sizeof(ram_file_block_t));
+        if (block == NULL) {
+            LLOGW("out of memory when malloc ram_file_block_t");
+            return 0;
+        }
+        memset(block, 0, sizeof(ram_file_block_t));
+        block->next = NULL;
+
+        // 将新分配的block链接到链表末尾
+        if (files[fd->fid]->head == NULL) {
+            files[fd->fid]->head = block;
+        } else {
+            ram_file_block_t* last = files[fd->fid]->head;
+            while (last->next) {
+                last = last->next;
+            }
+            last->next = block;
+        }
+
+        block_offset += BLOCK_SIZE;
+    }
+
+    // 现在偏移到offset对应的block
+    block = files[fd->fid]->head;
     size_t offset = fd->offset;
+    while (block != NULL && offset >= BLOCK_SIZE) {
+        offset -= BLOCK_SIZE;
+        block = block->next;
+    }
+
+    // 开始写
     const uint8_t* src = (const uint8_t*)ptr;
     while (write_size > 0) {
-        if (block == NULL || offset % BLOCK_SIZE == 0) {
-            if (block == NULL) {
-                block = luat_heap_malloc(sizeof(ram_file_block_t));
-                if (block == NULL) {
-                    LLOGW("out of memory when malloc ram_file_block_t");
-                    return 0;
-                }
-                memset(block, 0, sizeof(ram_file_block_t));
-                block->next = NULL;
-                if (files[fd->fid]->head == NULL) {
-                    files[fd->fid]->head = block;
-                } else {
-                    ram_file_block_t* last = files[fd->fid]->head;
-                    while (last->next) {
-                        last = last->next;
-                    }
-                    last->next = block;
-                }
-            }
-        }
         size_t copy_size = BLOCK_SIZE - (offset % BLOCK_SIZE);
         if (copy_size > write_size) {
             copy_size = write_size;
@@ -285,8 +313,10 @@ size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t
             block = block->next;
         }
     }
+
     fd->offset += (size_t)(src - (const uint8_t*)ptr);
-    files[fd->fid]->size = offset > files[fd->fid]->size ? offset : files[fd->fid]->size;
+    // 更新文件大小
+    files[fd->fid]->size = total_size;
     // 打印一下写入的数据
     // LLOGD("write data %s", (char*)ptr);
     return (size_t)(src - (const uint8_t*)ptr);
@@ -458,6 +488,395 @@ int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
     return 0;
 }
 
+#else
+
+typedef struct ram_file
+{
+    size_t size;     // 当前文件大小, 也是指针对应内存块的大小
+    // size_t ptr_size; // 数值指针的大小
+    char name[32];   // 文件名称
+    char ptr[4];
+}ram_file_t;
+
+typedef struct luat_ram_fd
+{
+    int fid;
+    uint32_t  offset;
+    uint8_t readonly;
+}luat_raw_fd_t;
+
+#define RAM_FILE_MAX (64)
+static ram_file_t* files[RAM_FILE_MAX];
+
+
+FILE* luat_vfs_ram_fopen(void* userdata, const char *filename, const char *mode) {
+    (void)userdata;
+    // LLOGD("ram fs open %s %s", filename, mode);
+    if (filename == NULL || mode == NULL || strlen(filename) > 31)
+        return NULL;
+    // 读文件
+    if (!strcmp("r", mode) || !strcmp("rb", mode)) {
+        for (size_t i = 0; i < RAM_FILE_MAX; i++)
+        {
+            if (files[i]== NULL)
+                continue;
+            if (!strcmp(files[i]->name, filename)) {
+                luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
+                if (fd == NULL) {
+                    LLOGE("out of memory when malloc luat_raw_fd_t");
+                    return NULL;
+                }
+                fd->fid = i;
+                fd->offset = 0;
+                fd->readonly = 1;
+                return (FILE*)fd;
+            }
+        }
+        return NULL;
+    }
+    // 写文件
+    else if (!strcmp("w", mode) || !strcmp("wb", mode) || !strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode) || !strcmp("r+", mode) || !strcmp("rb+", mode) || !strcmp("r+b", mode)) {
+        // 先看看是否存在, 如果存在就重用老的
+        for (size_t i = 0; i < RAM_FILE_MAX; i++)
+        {
+            if (files[i]== NULL)
+                continue;
+            if (!strcmp(files[i]->name, filename)) {
+                luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
+                if (fd == NULL) {
+                    LLOGE("out of memory when malloc luat_raw_fd_t");
+                    return NULL;
+                }
+                fd->fid = i;
+                fd->readonly = 0;
+                fd->offset = 0;
+                if (!strcmp("w+", mode) || !strcmp("wb+", mode) || !strcmp("w+b", mode)) {
+                    // 截断模式
+                    char* tmp = luat_heap_realloc(files[i], sizeof(ram_file_t));
+                    if (tmp) {
+                        files[i] = (ram_file_t*)tmp;
+                    }
+                    else {
+                        LLOGE("realloc ram_file_t failed");
+                        luat_heap_free(fd);
+                        return NULL;
+                    }
+                    files[i]->size = 0;
+                }
+                return (FILE*)fd;
+            }
+        }
+        for (size_t i = 0; i < RAM_FILE_MAX; i++)
+        {
+            if (files[i] != NULL)
+                continue;
+            ram_file_t *file = luat_heap_malloc(sizeof(ram_file_t));
+            if (file == NULL) {
+                LLOGE("out of memory when malloc ram_file_t");
+                return NULL;
+            }
+            memset(file, 0, sizeof(ram_file_t));
+            strcpy(file->name, filename);
+            files[i] = file;
+            luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
+            if (fd == NULL) {
+                LLOGE("out of memory when malloc luat_raw_fd_t");
+                return NULL;
+            }
+            fd->fid = i;
+            fd->offset = 0;
+            fd->readonly = 0;
+            return (FILE*)fd;
+        }
+    }
+    // 追加模式
+    else if (!strcmp("a", mode) || !strcmp("ab", mode) || !strcmp("a+", mode) || !strcmp("ab+", mode) || !strcmp("a+b", mode) ) {
+        // 先看看是否存在, 如果存在就重用老的
+        for (size_t i = 0; i < RAM_FILE_MAX; i++)
+        {
+            if (files[i] == NULL)
+                continue;
+            if (!strcmp(files[i]->name, filename)) {
+                luat_raw_fd_t* fd = luat_heap_malloc(sizeof(luat_raw_fd_t));
+                if (fd == NULL) {
+                    LLOGE("out of memory when malloc luat_raw_fd_t");
+                    return NULL;
+                }
+                fd->fid = i;
+                fd->offset = files[i]->size;
+                fd->readonly = 0;
+                return (FILE*)fd;
+            }
+        }
+    }
+    else {
+        LLOGE("unkown open mode %s", mode);
+        return NULL;
+    }
+    LLOGE("too many ram files >= %d", RAM_FILE_MAX);
+    return NULL;
+}
+
+int luat_vfs_ram_getc(void* userdata, FILE* stream) {
+    (void)userdata;
+    //LLOGD("getc %p %p", userdata, stream);
+    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
+    //LLOGD("getc %p %p %d %d", userdata, stream, fd->offset, fd->size);
+    if (fd->fid < 0 || fd->fid >= RAM_FILE_MAX) {
+        return -1;
+    }
+    if (files[fd->fid] == NULL) {
+        return -1;
+    }
+    if (fd->offset < files[fd->fid]->size) {
+        uint8_t c = (uint8_t)files[fd->fid]->ptr[fd->offset];
+        fd->offset ++;
+        //LLOGD("getc %02X", c);
+        return c;
+    }
+    return -1;
+}
+
+int luat_vfs_ram_fseek(void* userdata, FILE* stream, long int offset, int origin) {
+    (void)userdata;
+    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
+    if (origin == SEEK_CUR) {
+        fd->offset += offset;
+        return 0;
+    }
+    else if (origin == SEEK_SET) {
+        fd->offset = offset;
+        return 0;
+    }
+    else {
+        fd->offset = files[fd->fid]->size - offset;
+        return 0;
+    }
+}
+
+int luat_vfs_ram_ftell(void* userdata, FILE* stream) {
+    (void)userdata;
+    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
+    // LLOGD("tell %p %p offset %d", userdata, stream, fd->offset);
+    return fd->offset;
+}
+
+int luat_vfs_ram_fclose(void* userdata, FILE* stream) {
+    (void)userdata;
+    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
+    //LLOGD("fclose %p %p %d %d", userdata, stream, fd->size, fd->offset);
+    luat_heap_free(fd);
+    return 0;
+}
+int luat_vfs_ram_feof(void* userdata, FILE* stream) {
+    (void)userdata;
+    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
+    //LLOGD("feof %p %p %d %d", userdata, stream, fd->size, fd->offset);
+    return fd->offset >= files[fd->fid]->size ? 1 : 0;
+}
+int luat_vfs_ram_ferror(void* userdata, FILE *stream) {
+    (void)userdata;
+    (void)stream;
+    return 0;
+}
+size_t luat_vfs_ram_fread(void* userdata, void *ptr, size_t size, size_t nmemb, FILE *stream) {
+    (void)userdata;
+    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
+    //LLOGD("fread %p %p %d %d", userdata, stream, fd->size, fd->offset);
+    //LLOGD("fread2 %p %p %d %d", userdata, stream, size * nmemb, fd->offset);
+    size_t read_size = size*nmemb;
+    if (fd->offset >= files[fd->fid]->size) {
+        return 0;
+    }
+    if (fd->offset + read_size >= files[fd->fid]->size) {
+        read_size = files[fd->fid]->size - fd->offset;
+    }
+    memcpy(ptr, files[fd->fid]->ptr + fd->offset, read_size);
+    fd->offset += read_size;
+    return read_size;
+}
+size_t luat_vfs_ram_fwrite(void* userdata, const void *ptr, size_t size, size_t nmemb, FILE *stream) {
+    (void)userdata;
+    luat_raw_fd_t* fd = (luat_raw_fd_t*)stream;
+    size_t write_size = size*nmemb;
+    if (write_size > 128 * 1024) {
+        LLOGW("ramfs large write !! %ld", write_size);
+    }
+    if (fd->readonly) {
+        LLOGW("readonly fd %d!! path %s", fd->fid, files[fd->fid]->name);
+        return 0;
+    }
+    
+    if (fd->offset + write_size > files[fd->fid]->size) {
+        char* ptr = luat_heap_realloc(files[fd->fid], fd->offset + write_size + sizeof(ram_file_t));
+        if (ptr == NULL) {
+            LLOGW("/ram out of sys memory!! %ld", write_size);
+            return 0;
+        }
+        files[fd->fid] = (ram_file_t*)ptr;
+        files[fd->fid]->size = fd->offset + write_size;
+    }
+    memcpy(files[fd->fid]->ptr + fd->offset, ptr, write_size);
+    fd->offset += write_size;
+    return write_size;
+}
+int luat_vfs_ram_remove(void* userdata, const char *filename) {
+    (void)userdata;
+    for (size_t i = 0; i < RAM_FILE_MAX; i++)
+    {
+        if (files[i] == NULL)
+            continue;
+        if (!strcmp(filename, files[i]->name)) {
+            luat_heap_free(files[i]);
+            files[i] = NULL;
+        }
+    }
+    return 0;
+}
+int luat_vfs_ram_rename(void* userdata, const char *old_filename, const char *new_filename) {
+    (void)userdata;
+    if (old_filename == NULL || new_filename == NULL)
+        return -1;
+    if (strlen(old_filename) > 31 || strlen(new_filename) > 31)
+        return -2;
+    for (size_t i = 0; i < RAM_FILE_MAX; i++)
+    {
+        if (files[i] == NULL)
+            continue;
+        if (!strcmp(old_filename, files[i]->name)) {
+            strcpy(files[i]->name, new_filename);
+            return 0;
+        }
+    }
+    return 0;
+}
+int luat_vfs_ram_fexist(void* userdata, const char *filename) {
+    (void)userdata;
+    for (size_t i = 0; i < RAM_FILE_MAX; i++)
+    {
+        if (files[i] == NULL)
+            continue;
+        if (!strcmp(filename, files[i]->name)) {
+            return 1;
+        }
+    }
+    return 0;
+}
+
+size_t luat_vfs_ram_fsize(void* userdata, const char *filename) {
+    (void)userdata;
+    for (size_t i = 0; i < RAM_FILE_MAX; i++)
+    {
+        if (files[i] == NULL)
+            continue;
+        if (!strcmp(filename, files[i]->name)) {
+            return files[i]->size;
+        }
+    }
+    return 0;
+}
+
+void* luat_vfs_ram_mmap(void* userdata, FILE *stream) {
+    (void)userdata;
+    luat_raw_fd_t *fd = (luat_raw_fd_t*)(stream);
+    //LLOGD("fsize %p %p %d %d", userdata, fd);
+    return files[fd->fid]->ptr;
+}
+
+int luat_vfs_ram_mkfs(void* userdata, luat_fs_conf_t *conf) {
+    (void)userdata;
+    (void)conf;
+    return -1;
+}
+
+int luat_vfs_ram_mount(void** userdata, luat_fs_conf_t *conf) {
+    (void)userdata;
+    (void)conf;
+    return 0;
+}
+
+int luat_vfs_ram_umount(void* userdata, luat_fs_conf_t *conf) {
+    (void)userdata;
+    (void)conf;
+    return 0;
+}
+
+int luat_vfs_ram_mkdir(void* userdata, char const* _DirName) {
+    (void)userdata;
+    (void)_DirName;
+    return -1;
+}
+
+int luat_vfs_ram_rmdir(void* userdata, char const* _DirName) {
+    (void)userdata;
+    (void)_DirName;
+    return -1;
+}
+
+int luat_vfs_ram_lsdir(void* userdata, char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len) {
+    (void)userdata;
+    (void)_DirName;
+    size_t count = 0;
+    for (size_t i = 0; i < RAM_FILE_MAX; i++)
+    {
+        if (count >= len)
+            break;
+        if (files[i] == NULL)
+            continue;
+        if (offset > 0) {
+            offset --;
+            continue;
+        }
+        ents[count].d_type = 0;
+        strcpy(ents[count].d_name, files[i]->name);
+        count ++;
+    }
+    return count;
+}
+
+int luat_vfs_ram_info(void* userdata, const char* path, luat_fs_info_t *conf) {
+    (void)userdata;
+    (void)path;
+    memcpy(conf->filesystem, "ram", strlen("ram")+1);
+    size_t ftotal = 0;
+    for (size_t i = 0; i < RAM_FILE_MAX; i++)
+    {
+        if (files[i] == NULL)
+            continue;
+        ftotal += files[i]->size;
+    }
+    size_t total; size_t used; size_t max_used;
+    luat_meminfo_sys(&total, &used, &max_used);
+    
+    conf->type = 0;
+    conf->total_block = 64;
+    conf->block_used = (ftotal + 1023) / 1024;
+    conf->block_size = 1024;
+    return 0;
+}
+
+int luat_vfs_ram_truncate(void* fsdata, char const* path, size_t nsize) {
+    for (size_t i = 0; i < RAM_FILE_MAX; i++)
+    {
+        if (files[i] == NULL)
+            continue;
+        if (!strcmp(files[i]->name, path)) {
+            if (files[i]->size > nsize) {
+                files[i]->size = nsize;
+                char* ptr = luat_heap_realloc(files[i], nsize + sizeof(ram_file_t));
+                if (ptr) {
+                    files[i] = (ram_file_t*)ptr;
+                }
+            }
+            return 0;
+        }
+    }
+    return 0;
+}
+
+#endif
+
+
 #define T(name) .name = luat_vfs_ram_##name
 const struct luat_vfs_filesystem vfs_fs_ram = {
     .name = "ram",

+ 4 - 0
module/Air8000/demo/ble/ibeacon/main.lua

@@ -39,6 +39,10 @@ if wdt then
     sys.timerLoopStart(wdt.feed, 3000)
 end
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
+
 -- 如果内核固件支持errDump功能,此处进行配置,【强烈建议打开此处的注释】
 -- 因为此功能模块可以记录并且上传脚本在运行过程中出现的语法错误或者其他自定义的错误信息,可以初步分析一些设备运行异常的问题
 -- 以下代码是最基本的用法,更复杂的用法可以详细阅读API说明文档

+ 4 - 0
module/Air8000/demo/ble/master/main.lua

@@ -36,6 +36,10 @@ if wdt then
     sys.timerLoopStart(wdt.feed, 3000)
 end
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
+
 -- 如果内核固件支持errDump功能,此处进行配置,【强烈建议打开此处的注释】
 -- 因为此功能模块可以记录并且上传脚本在运行过程中出现的语法错误或者其他自定义的错误信息,可以初步分析一些设备运行异常的问题
 -- 以下代码是最基本的用法,更复杂的用法可以详细阅读API说明文档

+ 4 - 0
module/Air8000/demo/ble/peripheral/main.lua

@@ -35,6 +35,10 @@ if wdt then
     sys.timerLoopStart(wdt.feed, 3000)
 end
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
+
 -- 如果内核固件支持errDump功能,此处进行配置,【强烈建议打开此处的注释】
 -- 因为此功能模块可以记录并且上传脚本在运行过程中出现的语法错误或者其他自定义的错误信息,可以初步分析一些设备运行异常的问题
 -- 以下代码是最基本的用法,更复杂的用法可以详细阅读API说明文档

+ 4 - 0
module/Air8000/demo/ble/scan/main.lua

@@ -35,6 +35,10 @@ if wdt then
     sys.timerLoopStart(wdt.feed, 3000)
 end
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
+
 -- 如果内核固件支持errDump功能,此处进行配置,【强烈建议打开此处的注释】
 -- 因为此功能模块可以记录并且上传脚本在运行过程中出现的语法错误或者其他自定义的错误信息,可以初步分析一些设备运行异常的问题
 -- 以下代码是最基本的用法,更复杂的用法可以详细阅读API说明文档

+ 3 - 0
module/Air8000/demo/wlan/AP/main.lua

@@ -7,6 +7,9 @@ dnsproxy = require("dnsproxy")
 dhcpsrv = require("dhcpsrv")
 httpplus = require("httpplus")
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
 
 function test_ap()
     log.info("执行AP创建操作")

+ 4 - 0
module/Air8000/demo/wlan/Power_Save/main.lua

@@ -7,6 +7,10 @@ dnsproxy = require("dnsproxy")
 dhcpsrv = require("dhcpsrv")
 -- httpplus = require("httpplus")
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
+
 -- 通过boot按键方便刷Air8000S
 function PWR8000S(val)
     gpio.set(23, val)

+ 3 - 1
module/Air8000/demo/wlan/STA/main.lua

@@ -7,7 +7,9 @@ dnsproxy = require("dnsproxy")
 dhcpsrv = require("dhcpsrv")
 httpplus = require("httpplus")
 
-
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
 
 -- wifi的STA相关事件
 sys.subscribe("WLAN_STA_INC", function(evt, data)

+ 4 - 0
module/Air8000/demo/wlan/wifi_configuration_network_by_ap/main.lua

@@ -9,6 +9,10 @@ dnsproxy = require("dnsproxy")
 dhcpsrv = require("dhcpsrv")
 httpplus = require("httpplus")
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
+
 -- 初始化LED灯, 这里演示控制Air8000核心板蓝灯,其他开发板请查看硬件原理图自行修改(如果使用整机开发板可以用GPIO146)
 local LEDA = gpio.setup(20, 0, gpio.PULLUP)
 

+ 4 - 0
module/Air8000/demo/wlan/wifi_scan/main.lua

@@ -6,6 +6,10 @@ VERSION = "1.0.5"
 _G.sys = require("sys")
 require "sysplus"
 
+-- 如果需要升级WIFI固件,请打开下面两行注释
+-- local fota_wifi = require("fota_wifi")
+-- sys.taskInit(fota_wifi.request)
+
 function test_scan()
     while 1 do
         log.info("执行wifi扫描")

+ 11 - 11
module/Air8000/project/School_To_Home/code/2712A.lua → module/Air8000/project/School_To_Home/code/2712.lua

@@ -6,30 +6,30 @@ local gpio_pin = pcb.chargeCmdPin()
 gpio.setup(gpio_pin, 1, gpio.PULLUP)
 sys.taskInit(function()
     sys.wait(1000)
-    local result, data = sensor.yhm27xx(gpio_pin, 0x04, 0x08)
+    local result, data = yhm27xx.cmd(gpio_pin, 0x04, 0x08)
     sys.wait(200)
     log.info("yhm27xxx", result, data)
     if result == true and data ~= nil then
         log.info("yhm27xxx", "yhm27xx存在--")
         sys.wait(200)
-        result = sensor.yhm27xx(gpio_pin, 0x04, 0x01, 0x02)
+        result = yhm27xx.cmd(gpio_pin, 0x04, 0x01, 0x02)
         if result == true then
-            result, data = sensor.yhm27xx(gpio_pin, 0x04, 0x01)
-            if data ~= 2 then
-                log.info("yhm27xxx", "写入失败", data)
+            result, data = yhm27xx.cmd(gpio_pin, 0x04, 0x01, 0x02)
+            if result then
+                log.info("yhm27xxx", "写入I_CTRL成功")
             else
-                log.info("yhm27xxx", "测试成功", data)
+                log.info("yhm27xxx", "测试I_CTRL失败")
             end
         else
             log.info("yhm27xxx", "读取失败", result)
         end
-        result = sensor.yhm27xx(gpio_pin, 0x04, 0x00, 0x00)
+        local result = yhm27xx.cmd(gpio_pin, 0x04, 0x00, 0x00)
         if result == true then
-            result, data = sensor.yhm27xx(gpio_pin, 0x04, 0x00)
-            if data ~= 0 then
-                log.info("yhm27xxx", "写入V_CTRL失败, " .. data )
+            result, data = yhm27xx.cmd(gpio_pin, 0x04, 0x00, 0x00)
+            if result then
+                log.info("yhm27xxx", "写入V_CTRL成功")
             else
-                log.info("yhm27xxx", "测试V_CTRL成功, " .. data, result)
+                log.info("yhm27xxx", "测试V_CTRL失败")
             end
         else
             log.info("yhm27xxx", "读取失败", result)

+ 2 - 2
module/Air8000/project/School_To_Home/code/bootup.lua

@@ -42,8 +42,8 @@ _G.mreport = require "mreport"
 -- config
 require "cfg"
 
---2712A
--- require "2712A"
+--2712
+require "2712"
 
 -- netWork
 _G.netWork = require "netWork"

+ 3 - 1
module/Air8000/project/School_To_Home/code/common.lua

@@ -8,7 +8,9 @@ end
 
 local common = {}
 
-local mode = "CAPTURE"
+-- local mode = "CAPTURE"
+local mode = "STATIC_GNSS"
+
 local lastGPSLocation
 
 -- 0200 状态位定义

+ 16 - 16
module/Air8000/project/School_To_Home/code/ledTask.lua

@@ -1,21 +1,21 @@
 -- 蓝灯
 local netLed = gpio.setup(1, 0)
-sys.taskInit(function()
-    while true do
-        while not srvs.isConnected() do
-            manage.wake("led")
-            netLed(1)
-            sys.wait(500)
-            netLed(0)
-            manage.sleep("led")
-            sys.wait(500)
-        end
-        netLed(0)
-        while srvs.isConnected() do
-            sys.wait(10000)
-        end
-    end
-end)
+-- sys.taskInit(function()
+--     while true do
+--         while not srvs.isConnected() do
+--             manage.wake("led")
+--             netLed(1)
+--             sys.wait(500)
+--             netLed(0)
+--             manage.sleep("led")
+--             sys.wait(500)
+--         end
+--         netLed(0)
+--         while srvs.isConnected() do
+--             sys.wait(10000)
+--         end
+--     end
+-- end)
 
 -- 黄灯  在gnss.lua里面,由当前Air201上CC0257B的1PPS引脚控制,可以给GNSS芯片发指令改变1pps输出周期
 

+ 43 - 0
module/Air8000/project/School_To_Home/code/main.lua

@@ -13,6 +13,9 @@ log.info("main", PROJECT, VERSION, PRODUCT_VER)
 _G.sys = require "sys"
 _G.sysplus = require "sysplus"
 
+gpio.setup(22, 1, gpio.PULLUP)
+
+
 _G.pcb = require "pcb"
 
 -- 开机防抖
@@ -28,6 +31,46 @@ pcb.gnssPower(false)
 gpio.setup(24, 1, gpio.PULLUP)          -- i2c工作的电压域
 gpio.setup(pcb.es8311PowerPin(), 1)
 
+
+--手动关闭部分外设电源
+pcb.gnssPower(0)--gpio25,26
+local result = pm.power(pm.GPS,false)--关闭gps电源--GPIO24,25
+log.info("main", "gps power off",result)
+
+local result = gpio.setup(pcb.es8311PowerPin(), 0)--关闭es8311电源
+log.info("main", "es8311 power off",result)
+
+local result = gpio.setup(pcb.chargeCmdPin(), 0)   --关闭充电ic的CMD脚--gpio152
+log.info("main", "chargeCmd power off",result)
+
+sys.taskInit(function()
+    
+    sys.wait(2000)
+    mobile.flymode(0,true)
+    log.info("main", "fly mode")
+    sys.wait(2000) 
+
+    -- local result = gpio.setup(24, 0)
+    -- log.info("main", "wakeup2 power off",result)
+    
+    airlink.pause(1)
+    sys.wait(2000)
+    log.info("main", "airlink pause")
+    -- sys.wait(3000)
+    local result = airlink.power(false)
+    log.info("关闭wifi",result)
+    -- gpio.setup(23, 0)
+
+    log.info("main", "PSM+")
+    log.info("pm check", pm.check())
+
+    pm.power(pm.WORK_MODE, 3)--PSM+模式
+    log.info("check pm state  ", pm.check())
+    sys.wait(3000)
+
+end)
+
+
 -- 默认进LIGHT模式
 -- pm.request(pm.LIGHT)
 

+ 1 - 1
module/Air8000/project/School_To_Home/code/pcb.lua

@@ -53,7 +53,7 @@ function pcb.chargeCmdPin()
     if hversion == "1.0.2" then
         return 20
     else
-        return 27
+        return 152
     end
 end