Forráskód Böngészése

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

alienwalker 1 éve
szülő
commit
52f0d5c69c

+ 183 - 162
components/camera/luat_camera.h

@@ -1,162 +1,183 @@
-/******************************************************************************
- *  CAMERA设备操作抽象层
- *  @author Dozingfiretruck
- *  @since 0.0.1
- *****************************************************************************/
-#ifndef Luat_CAMERA_H
-#define Luat_CAMERA_H
-
-#include "luat_base.h"
-#ifdef __LUATOS__
-#include "luat_lcd.h"
-#endif
-/**
- * @defgroup CAMERA  CAMERA设备(CAMERA)
- * @{
- */
-enum
-{
-	LUAT_CAMERA_FRAME_START = 0,
-	LUAT_CAMERA_FRAME_END,
-	LUAT_CAMERA_FRAME_RX_DONE,
-	LUAT_CAMERA_FRAME_ERROR,
-
-	LUAT_CAMERA_MODE_AUTO = 0,
-	LUAT_CAMERA_MODE_SCAN,
-};
-
-typedef struct luat_camera_conf
-{
-    uint8_t id;
-    uint8_t zbar_scan;
-    uint8_t draw_lcd;
-    uint8_t i2c_id;
-    uint8_t i2c_addr;
-    uint8_t pwm_id;
-    size_t pwm_period;
-    uint8_t pwm_pulse;
-    uint16_t sensor_width;
-    uint16_t sensor_height;
-    uint8_t color_bit;
-    uint8_t id_reg;
-	uint8_t id_value;
-    size_t init_cmd_size;
-    uint8_t *init_cmd;
-#ifdef __LUATOS__
-    luat_lcd_conf_t* lcd_conf;
-#else
-    void *lcd_conf;
-#endif
-} luat_camera_conf_t;
-
-typedef struct
-{
-	size_t  camera_speed;			//提供给camera时钟频率
-	uint16_t sensor_width;			//camera的最大宽度
-    uint16_t sensor_height;			//camera的最大高度
-    uint8_t only_y;
-	uint8_t rowScaleRatio;
-	uint8_t colScaleRatio;
-	uint8_t scaleBytes;
-	uint8_t spi_mode;
-	uint8_t is_msb;	//0 or 1;
-	uint8_t is_two_line_rx; //0 or 1;
-	uint8_t seq_type;	//0 or 1
-	uint8_t plat_param[4];
-#ifdef __LUATOS__
-    luat_lcd_conf_t* lcd_conf;
-#else
-    void *lcd_conf;
-#endif
-} luat_spi_camera_t;
-#ifdef __LUATOS__
-int l_camera_handler(lua_State *L, void* ptr);
-#endif
-/**
- * @brief 配置spi camera并且初始化camera
- * @param id camera接收数据总线ID,ec618上有2条,0和1
- * @param conf camera相关配置
- * @param callback camera接收中断回调,注意这是在中断里的回调
- * @param param 中断回调时用户的参数
- * @return >=0成功,其他失败
- */
-int luat_camera_setup(int id, luat_spi_camera_t *conf, void* callback, void *param);
-
-int luat_camera_set_image_w_h(int id, uint16_t w, uint16_t h);
-
-/**
- * @brief 配置camera并且初始化camera,spi camera不要使用这个
- * @param conf camera相关配置
- * @return 0成功,其他失败
- */
-int luat_camera_init(luat_camera_conf_t *conf);
-
-/**
- * @brief 关闭camera并且释放资源
- * @param id camera接收数据总线ID
- * @return 0成功,其他失败
- */
-int luat_camera_close(int id);
-
-/**
- * @brief 摄像头启动开始接收数据,csdk专用
- * @param id camera接收数据总线ID
- * @param buf 用户区地址,如果为NULL,则表示不存放到用户区
- * @return 0成功,其他失败
- */
-int luat_camera_start_with_buffer(int id, void *buf);
-/**
- * @brief 摄像头切换接收数据缓冲区,csdk专用
- * @param id camera接收数据总线ID
- * @param buf 用户区地址,如果为NULL,则表示不存放到用户区
- * @return 0成功,其他失败
- */
-void luat_camera_continue_with_buffer(int id, void *buf);
-/**
- * @brief 暂停接收camera数据
- * @param id camera接收数据总线ID
- * @param is_pause 非0暂停,0恢复
- * @return 0成功,其他失败
- */
-int luat_camera_pause(int id, uint8_t is_pause);
-/*
- * @brief 扫码库初始化
- * @param type 扫码库型号,目前只支持0
- * @param stack 扫码库任务的堆栈地址
- * @param stack_length 扫码库任务的堆栈深度,type=0时需要至少220KB
- * @param priority 扫码库任务优先级
- * @return 0成功,其他失败
- */
-int luat_camera_image_decode_init(uint8_t type, void *stack, uint32_t stack_length, uint32_t priority);
-
-
-int luat_camera_image_decode_once(uint8_t *data, uint16_t image_w, uint16_t image_h, uint32_t timeout, void *callback, void *param);
-
-
-void luat_camera_image_decode_deinit(void);
-
-int luat_camera_image_decode_get_result(uint8_t *buf);
-
-/**********以下是luatos使用,csdk不要使用***********/
-/**
- * @brief 开始接收camera数据
- * @param id camera接收数据总线ID
- * @return 0成功,其他失败
- */
-int luat_camera_start(int id);
-/**
- * @brief 停止接收camera数据
- * @param id camera接收数据总线ID
- * @return 0成功,其他失败
- */
-int luat_camera_stop(int id);
-
-int luat_camera_preview(int id, uint8_t on_off);
-
-int luat_camera_work_mode(int id, int mode);
-
-int luat_camera_capture(int id, uint8_t quality, const char *path);
-
-int luat_camera_capture_in_ram(int id, uint8_t quality, void *buffer);
-/** @}*/
-#endif
+/******************************************************************************
+ *  CAMERA设备操作抽象层
+ *  @author Dozingfiretruck
+ *  @since 0.0.1
+ *****************************************************************************/
+#ifndef Luat_CAMERA_H
+#define Luat_CAMERA_H
+
+#include "luat_base.h"
+#ifdef __LUATOS__
+#include "luat_lcd.h"
+#endif
+/**
+ * @defgroup CAMERA  CAMERA设备(CAMERA)
+ * @{
+ */
+enum
+{
+	LUAT_CAMERA_FRAME_START = 0,
+	LUAT_CAMERA_FRAME_END,
+	LUAT_CAMERA_FRAME_RX_DONE,
+	LUAT_CAMERA_FRAME_ERROR,
+
+	LUAT_CAMERA_MODE_AUTO = 0,
+	LUAT_CAMERA_MODE_SCAN,
+};
+
+typedef struct luat_camera_conf
+{
+    uint8_t id;
+    uint8_t zbar_scan;
+    uint8_t draw_lcd;
+    uint8_t i2c_id;
+    uint8_t i2c_addr;
+    uint8_t pwm_id;
+    size_t pwm_period;
+    uint8_t pwm_pulse;
+    uint16_t sensor_width;
+    uint16_t sensor_height;
+    uint8_t color_bit;
+    uint8_t id_reg;
+	uint8_t id_value;
+    size_t init_cmd_size;
+    uint8_t *init_cmd;
+#ifdef __LUATOS__
+    luat_lcd_conf_t* lcd_conf;
+#else
+    void *lcd_conf;
+#endif
+} luat_camera_conf_t;
+
+typedef struct
+{
+	size_t  camera_speed;			//提供给camera时钟频率
+	uint16_t sensor_width;			//camera的最大宽度
+    uint16_t sensor_height;			//camera的最大高度
+    uint8_t only_y;
+	uint8_t rowScaleRatio;
+	uint8_t colScaleRatio;
+	uint8_t scaleBytes;
+	uint8_t spi_mode;
+	uint8_t is_msb;	//0 or 1;
+	uint8_t is_two_line_rx; //0 or 1;
+	uint8_t seq_type;	//0 or 1
+	uint8_t plat_param[4];
+#ifdef __LUATOS__
+    luat_lcd_conf_t* lcd_conf;
+#else
+    void *lcd_conf;
+#endif
+} luat_spi_camera_t;
+#ifdef __LUATOS__
+int l_camera_handler(lua_State *L, void* ptr);
+#endif
+/**
+ * @brief 配置spi camera并且初始化camera
+ * @param id camera接收数据总线ID,ec618上有2条,0和1
+ * @param conf camera相关配置
+ * @param callback camera接收中断回调,注意这是在中断里的回调
+ * @param param 中断回调时用户的参数
+ * @return >=0成功,其他失败
+ */
+int luat_camera_setup(int id, luat_spi_camera_t *conf, void* callback, void *param);
+
+/**
+ * @brief 配置图像大小
+ * @param id camera接收数据总线ID
+ * @param w 图像宽
+ * @param h 图像高
+ * @return >=0成功,其他失败
+ */
+int luat_camera_set_image_w_h(int id, uint16_t w, uint16_t h);
+
+/**
+ * @brief 配置camera并且初始化camera,spi camera不要使用这个
+ * @param conf camera相关配置
+ * @return 0成功,其他失败
+ */
+int luat_camera_init(luat_camera_conf_t *conf);
+
+/**
+ * @brief 关闭camera并且释放资源
+ * @param id camera接收数据总线ID
+ * @return 0成功,其他失败
+ */
+int luat_camera_close(int id);
+
+/**
+ * @brief 摄像头启动开始接收数据,csdk专用
+ * @param id camera接收数据总线ID
+ * @param buf 用户区地址,如果为NULL,则表示不存放到用户区
+ * @return 0成功,其他失败
+ */
+int luat_camera_start_with_buffer(int id, void *buf);
+/**
+ * @brief 摄像头切换接收数据缓冲区,csdk专用
+ * @param id camera接收数据总线ID
+ * @param buf 用户区地址,如果为NULL,则表示不存放到用户区
+ * @return 0成功,其他失败
+ */
+void luat_camera_continue_with_buffer(int id, void *buf);
+/**
+ * @brief 暂停接收camera数据
+ * @param id camera接收数据总线ID
+ * @param is_pause 非0暂停,0恢复
+ * @return 0成功,其他失败
+ */
+int luat_camera_pause(int id, uint8_t is_pause);
+/*
+ * @brief 扫码库初始化
+ * @param type 扫码库型号,目前只支持0
+ * @param stack 扫码库任务的堆栈地址
+ * @param stack_length 扫码库任务的堆栈深度,type=0时需要至少220KB
+ * @param priority 扫码库任务优先级
+ * @return 0成功,其他失败
+ */
+int luat_camera_image_decode_init(uint8_t type, void *stack, uint32_t stack_length, uint32_t priority);
+/*
+ * @brief 扫码库进行一次解码
+ * @param data 缓冲区
+ * @param image_w 图像宽
+ * @param image_h 图像高
+ * @param timeout 超时
+ * @param callback 回调函数
+ * @param param 回调参数
+ * @return 0成功,其他失败
+ */
+int luat_camera_image_decode_once(uint8_t *data, uint16_t image_w, uint16_t image_h, uint32_t timeout, void *callback, void *param);
+
+/*
+ * @brief 扫码库反初始化
+ */
+void luat_camera_image_decode_deinit(void);
+/*
+ * @brief 获取解码结果
+ * @param buf 缓冲区
+ * @return 1成功,其他失败
+ */
+int luat_camera_image_decode_get_result(uint8_t *buf);
+
+/**********以下是luatos使用,csdk不要使用***********/
+/**
+ * @brief 开始接收camera数据
+ * @param id camera接收数据总线ID
+ * @return 0成功,其他失败
+ */
+int luat_camera_start(int id);
+/**
+ * @brief 停止接收camera数据
+ * @param id camera接收数据总线ID
+ * @return 0成功,其他失败
+ */
+int luat_camera_stop(int id);
+
+int luat_camera_preview(int id, uint8_t on_off);
+
+int luat_camera_work_mode(int id, int mode);
+
+int luat_camera_capture(int id, uint8_t quality, const char *path);
+
+int luat_camera_capture_in_ram(int id, uint8_t quality, void *buffer);
+/** @}*/
+#endif

+ 99 - 82
components/fskv/luat_fskv.h

@@ -1,82 +1,99 @@
-/*
- * Copyright (c) 2022 OpenLuat & AirM2M
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#ifndef LUAT_FSKV_H
-#define LUAT_FSKV_H
-
-#include "lfs.h"
-
-
-
-/**
- * @defgroup luatos_fskv 持久化数据存储接口
- * @{
- */
-
-/**
- * @brief 初始化kv数据存储
- * 
- * @return int == 0 正常 != 0失败
- */
-int luat_fskv_init(void);
-
-/**
- * @brief 删除指定的key
- * @param key[IN] 待删除的key值
- * @return int == 0 正常 != 0失败
- */
-int luat_fskv_del(const char* key);
-
-/**
- * @brief 写入指定key的数据
- * @param key[IN] 待写入的key值,不能为NULL,必须是\0结尾,最大长度64字节
- * @param data[IN] 待写入的数据, 不需要\0结尾
- * @param len[IN] 待写入的数据长度, 不含\0,当前支持最大长度255字节
- * @return 实际写入的长度
- */
-int luat_fskv_set(const char* key, void* data, size_t len);
-
-int luat_fskv_size(const char* key, char buff[4]);
-
-/**
- * @brief 读取指定key的数据
- * @param key[IN] 待读取的key值,不能为NULL,必须是\0结尾
- * @param data[IN] 待读取的数据, 可写入空间必须大于等于len值
- * @param len[IN] 待读取的数据长度最大长度, 不含\0
- * @return int > 0 实际读取的长度, <=0 失败
- */
-int luat_fskv_get(const char* key, void* data, size_t len);
-
-/**
- * @brief 清空所有数据
- * @return int == 0 正常 != 0失败
- */
-int luat_fskv_clear(void);
-
-int luat_fskv_stat(size_t *using_sz, size_t *max_sz, size_t *kv_count);
-
-int luat_fskv_next(char* buff, size_t offset);
-
-/**
- * @}
- */
-
-#endif
+/*
+ * Copyright (c) 2022 OpenLuat & AirM2M
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef LUAT_FSKV_H
+#define LUAT_FSKV_H
+
+#include "lfs.h"
+
+
+
+/**
+ * @defgroup luatos_fskv 持久化数据存储接口
+ * @{
+ */
+
+/**
+ * @brief 初始化kv数据存储
+ * 
+ * @return int == 0 正常 != 0失败
+ */
+int luat_fskv_init(void);
+
+/**
+ * @brief 删除指定的key
+ * @param key[IN] 待删除的key值
+ * @return int == 0 正常 != 0失败
+ */
+int luat_fskv_del(const char* key);
+
+/**
+ * @brief 写入指定key的数据
+ * @param key[IN] 待写入的key值,不能为NULL,必须是\0结尾,最大长度64字节
+ * @param data[IN] 待写入的数据, 不需要\0结尾
+ * @param len[IN] 待写入的数据长度, 不含\0,当前支持最大长度255字节
+ * @return 实际写入的长度
+ */
+int luat_fskv_set(const char* key, void* data, size_t len);
+
+/**
+ * @brief 读取key大小
+ * @param key key值,不能为NULL,必须是\0结尾,最大长度64字节
+ * @param buff 缓冲区
+ * @return  长度,<=0 失败
+ */
+int luat_fskv_size(const char* key, char buff[4]);
+
+/**
+ * @brief 读取指定key的数据
+ * @param key[IN] 待读取的key值,不能为NULL,必须是\0结尾
+ * @param data[IN] 待读取的数据, 可写入空间必须大于等于len值
+ * @param len[IN] 待读取的数据长度最大长度, 不含\0
+ * @return int > 0 实际读取的长度, <=0 失败
+ */
+int luat_fskv_get(const char* key, void* data, size_t len);
+
+/**
+ * @brief 清空所有数据
+ * @return int == 0 正常 != 0失败
+ */
+int luat_fskv_clear(void);
+/**
+ * @brief 获取kv数据库状态
+ * @param using_sz 已使用的空间,单位字节
+ * @param max_sz 总可用空间, 单位字节
+ * @param kv_count 总kv键值对数量, 单位个
+ * @return 0 成功,其他失败
+ */
+int luat_fskv_stat(size_t *using_sz, size_t *max_sz, size_t *kv_count);
+/**
+ * @brief 读取下一个偏移的数据
+ * @param buff 读取数据
+ * @param offset 偏移
+ * @return 0 成功,其他失败
+ */
+int luat_fskv_next(char* buff, size_t offset);
+
+/**
+ * @}
+ */
+
+#endif

+ 1 - 1
components/little_flash/inc/little_flash_table.h

@@ -18,7 +18,7 @@ extern "C" {
 #define LITTLE_FLASH_CHIP_TABLE                                                                              \
 {                                                                                                            \
     {"W25N01GVZEIG", LF_MF_ID_WINBOND, 0xAA21, LF_DRIVER_NAND_FLASH, 128L*1024L*1024L, 0xD8, 64L*2048L},     \
-    {"W25Q128FVSG", LF_MF_ID_WINBOND, 0x4018, LF_DRIVER_NOR_FLASH, 16L*1024L*1024L, 0x20, 4096L},            \
+    {"W25Q128FVSG", LF_MF_ID_WINBOND, 0x4018, LF_DRIVER_NOR_FLASH, 16L*1024L*1024L, 0x20, 4096L}             \
 }
 
 #ifdef __cplusplus

+ 5 - 1
components/mobile/luat_mobile.h

@@ -691,8 +691,12 @@ int luat_mobile_event_deregister_handler(void);
  * @return int =0成功,其他失败
  */
 int luat_mobile_sms_sdk_event_register_handler(luat_mobile_sms_event_callback_t callback_fun);
+
 /**
- * @brief 和luat_mobile_sms_sdk_event_register_handler一样,只是为了兼容老的BSP
+ * @brief 注册底层短信消息回调函数,后续改为统一消息处理(和luat_mobile_sms_sdk_event_register_handler一样,只是为了兼容老的BSP)
+ *
+ * @param callback_fun 短信消息回调函数,如果为NULL,则是注销
+ * @return int =0成功,其他失败
  */
 int luat_mobile_sms_event_register_handler(luat_mobile_sms_event_callback_t callback_fun);
 /* ------------------------------------------------- mobile status end ------------------------------------------------ */

+ 24 - 1
components/network/libemqtt/luat_mqtt.h

@@ -15,7 +15,7 @@
 
 #ifndef MQTT_RECV_BUF_LEN_MAX
 #if defined(CHIP_EC618) || defined(CHIP_EC718)|| defined(CHIP_EC716)
-#define MQTT_RECV_BUF_LEN_MAX (32*1024) ///< MQTT 接收BUFF大小
+#define MQTT_RECV_BUF_LEN_MAX (80*1024) ///< MQTT 接收BUFF大小
 #else
 #define MQTT_RECV_BUF_LEN_MAX 4096 ///< MQTT 接收BUFF大小
 #endif
@@ -93,10 +93,33 @@ typedef void (*luat_mqtt_cb_t)(luat_mqtt_ctrl_t *luat_mqtt_ctrl, uint16_t event)
 int luat_mqtt_connect(luat_mqtt_ctrl_t *mqtt_ctrl);
 // static int luat_mqtt_msg_cb(luat_mqtt_ctrl_t *mqtt_ctrl);
 // int l_mqtt_callback(lua_State *L, void* ptr);
+/**
+ *@brief MQTT内部回调(用户无需关心)
+ *@param ctrl luatos_mqtt对象实例
+ *@param arg1 参数1
+ *@param arg2 参数2
+ *@return 成功为0,其他值失败
+ */
 int l_luat_mqtt_msg_cb(luat_mqtt_ctrl_t * ctrl, int arg1, int arg2);
+/**
+ *@brief MQTT报文解析内部回调(用户无需关心)
+ *@param data 数据
+ *@param param 参数
+ *@return 成功为0,其他值失败
+ */
 int32_t luat_mqtt_callback(void *data, void *param);
+/**
+ *@brief MQTT定时器内部回调(用户无需关心)
+ */
 LUAT_RT_RET_TYPE luat_mqtt_timer_callback(LUAT_RT_CB_PARAM);
 // int luat_mqtt_read_packet(luat_mqtt_ctrl_t *mqtt_ctrl);
+/**
+ *@brief MQTT报文发送(用户无需关心)
+ *@param socket_info socket
+ *@param buf 数据
+ *@param count 数据大小
+ *@return 成功为0,其他值失败
+ */
 int luat_mqtt_send_packet(void* socket_info, const void* buf, unsigned int count);
 
 /**

+ 270 - 259
components/network/libhttp/luat_http.h

@@ -1,259 +1,270 @@
-#ifndef LUAT_HTTP_H
-#define LUAT_HTTP_H
-
-#ifdef __LUATOS__
-#include "luat_zbuff.h"
-
-
-#if defined(AIR101) || defined(AIR103)
-// #define HTTP_REQ_HEADER_MAX_SIZE 	(2048)
-#define HTTP_RESP_BUFF_SIZE 		(2048)
-#else
-// #define HTTP_REQ_HEADER_MAX_SIZE 	(8192)
-#define HTTP_RESP_BUFF_SIZE 		(8192)
-#endif
-#else
-#include "http_parser.h"
-#define HTTP_HEADER_BASE_SIZE 	(1024)
-#define HTTP_RESP_BUFF_SIZE 	(4096)
-
-#endif
-/**
- * @defgroup luatos_HTTP  HTTP(S)相关接口
- * @{
- */
-enum
-{
-	HTTP_STATE_IDLE,
-	HTTP_STATE_CONNECT,
-	HTTP_STATE_SEND_HEAD,
-	HTTP_STATE_SEND_BODY_START,
-	HTTP_STATE_SEND_BODY,
-	HTTP_STATE_GET_HEAD,
-	HTTP_STATE_GET_BODY,
-	HTTP_STATE_DONE,
-	HTTP_STATE_WAIT_CLOSE,
-};
-
-#ifndef __LUATOS__
-
-/*
- * http运行过程的回调函数
- * status >=0 表示运行状态,看HTTP_STATE_XXX <0说明出错停止了,==0表示结束了
- * data 在获取响应阶段,会回调head数据,如果为NULL,则head接收完成了。如果设置了data_mode,body数据也直接回调。
- * data_len 数据长度,head接收时,每行会多加一个\0,方便字符串处理
- * user_param 用户自己的参数
- */
-typedef void (*luat_http_cb)(int status, void *data, uint32_t data_len, void *user_param);
-
-#define HTTP_GET_DATA 		(2)
-#define HTTP_POST_DATA 		(1)
-#endif
-
-#define HTTP_OK 			(0)
-#define HTTP_ERROR_STATE 	(-1)
-#define HTTP_ERROR_HEADER 	(-2)
-#define HTTP_ERROR_BODY 	(-3)
-#define HTTP_ERROR_CONNECT 	(-4)
-#define HTTP_ERROR_CLOSE 	(-5)
-#define HTTP_ERROR_RX 		(-6)
-#define HTTP_ERROR_DOWNLOAD (-7)
-#define HTTP_ERROR_TIMEOUT  (-8)
-#define HTTP_ERROR_FOTA  	(-9)
-
-#define HTTP_CALLBACK 		(1)
-
-#define HTTP_RE_REQUEST_MAX (3)
-
-#define HTTP_TIMEOUT 		(10*60*1000) // 10分钟
-
-typedef struct{
-	network_ctrl_t *netc;		// http netc
-	http_parser  parser;	    //解析相关
-	char *host; 			/**< http host,需要释放 */
-	char* request_line;	/**< 需要释放,http请求的首行数据*/
-	uint16_t remote_port; 		/**< 远程端口号 */
-	uint8_t is_tls;             // 是否SSL
-	uint8_t custom_host;        /**< 是否自定义Host了*/
-	uint8_t is_post;
-	void* timeout_timer;			/**< timeout_timer 定时器*/
-	uint32_t timeout;
-	uint32_t tx_offset;
-#ifdef __LUATOS__
-	luat_ip_addr_t ip_addr;		// http ip
-	// const char *url;			// url
-	// const char *uri;			// uri
-	// char method[12];			// method
-
-	// 发送相关
-	// uint8_t request_message[HTTP_REQUEST_BUF_LEN_MAX];
-	char *req_header;
-	char *req_body;				//发送body
-	size_t req_body_len;		//发送body长度
-	luat_zbuff_t *zbuff_body;
-	char *req_auth;
-#ifdef LUAT_USE_FOTA
-	//OTA相关
-	uint8_t isfota;				//是否为ota下载
-	uint32_t address;			
-	uint32_t length;		
-	luat_spi_device_t* spi_device;
-#endif
-	int http_cb;				// http lua回调函数
-	int http_cb_userdata;				// http lua回调函数用户传参
-	//下载相关
-	uint8_t is_download;		//是否下载
-	char *dst;			//下载路径
-
-	// http_parser_settings parser_settings;
-	char* headers;
-	uint32_t headers_len;		//headers缓存长度
-	char* body;
-	uint32_t body_len;			//body缓存长度
-	// uint8_t is_chunk;		//是否chunk编码
-	uint8_t re_request_count;
-	// 响应相关
-	int32_t resp_content_len;	//content 长度
-	FILE* fd;					//下载 FILE
-	uint64_t idp;
-
-	uint8_t headers_complete;
-	uint8_t close_state;
-	char resp_buff[HTTP_RESP_BUFF_SIZE];
-	size_t resp_buff_offset;
-	size_t resp_headers_done;
-#else
-	Buffer_Struct request_head_buffer;	/**<存放用户自定义的请求head数据*/
-	luat_http_cb http_cb;				/**< http 回调函数 */
-	void *http_cb_userdata;				/**< http 回调函数用户传参*/
-	Buffer_Struct response_head_buffer;	/**<接收到的head数据缓存,回调给客户后就销毁了*/
-	int error_code;
-	Buffer_Struct response_cache;
-	uint32_t total_len;
-	uint32_t done_len;
-	uint32_t offset;
-	uint8_t retry_cnt_max;		/**<最大重试次数*/
-	uint8_t retry_cnt;
-	uint8_t state;
-	uint8_t data_mode;
-	uint8_t is_pause;
-	uint8_t debug_onoff;
-	uint8_t new_data;
-#endif
-}luat_http_ctrl_t;
-#ifdef __LUATOS__
-int luat_http_client_init(luat_http_ctrl_t* http, int ipv6);
-int luat_http_client_start(luat_http_ctrl_t* http);
-#else
-
-/**
- * @brief 创建一个http客户端
- *
- * @param cb http运行过程回调函数
- * @param user_param 回调时用户自己的参数
- * @param adapter_index 网卡适配器,不清楚的写-1,系统自动分配
- * @return 成功返回客户端地址,失败返回NULL
- */
-luat_http_ctrl_t* luat_http_client_create(luat_http_cb cb, void *user_param, int adapter_index);
-/**
- * @brief http客户端的通用配置,创建客户端时已经有默认配置,可以不配置
- *
- * @param http_ctrl 客户端
- * @param timeout 单次数据传输超时时间,单位ms
- * @param debug_onoff 是否开启调试打印,开启后会占用一点系统资源
- * @param retry_cnt 因传输异常而重传的最大次数
- * @return 成功返回0,其他值失败
- */
-int luat_http_client_base_config(luat_http_ctrl_t* http_ctrl, uint32_t timeout, uint8_t debug_onoff, uint8_t retry_cnt);
-/**
- * @brief 客户端SSL配置,只有访问https才需要配置
- *
- * @param http_ctrl 客户端
- * @param mode <0 关闭SSL功能,并忽略后续参数; 0忽略证书验证过程,大部分https应用就可以这个配置,后续证书配置可以都写NULL和0; 2强制证书验证,后续证书相关参数必须写对
- * @param server_cert 服务器证书字符串,结尾必须有0,如果不忽略证书验证,这个必须有
- * @param server_cert_len 服务器证书数据长度,长度包含结尾的0,也就是strlen(server_cert) + 1
- * @param client_cert 客户端证书字符串,结尾必须有0,双向认证才有,一般金融行业可能会用
- * @param client_cert_len 客户端证书数据长度,长度包含结尾的0
- * @param client_cert_key 客户端证书私钥字符串,结尾必须有0,双向认证才有,一般金融行业可能会用
- * @param client_cert_key_len 客户端证书私钥数据长度,长度包含结尾的0
- * @param client_cert_key_password 客户端证书私钥密码字符串,结尾必须有0,双向认证才有,如果私钥没有密码保护,则不需要
- * @param client_cert_key_password_len 客户端证书私钥密码数据长度,长度包含结尾的0
- * @return 成功返回0,其他值失败
- */
-int luat_http_client_ssl_config(luat_http_ctrl_t* http_ctrl, int mode, const char *server_cert, uint32_t server_cert_len,
-		const char *client_cert, uint32_t client_cert_len,
-		const char *client_cert_key, uint32_t client_cert_key_len,
-		const char *client_cert_key_password, uint32_t client_cert_key_password_len);
-
-/**
- * @brief 清空用户设置的POST数据和request head参数
- *
- * @param http_ctrl 客户端
- * @return 成功返回0,其他值失败
- */
-
-int luat_http_client_clear(luat_http_ctrl_t *http_ctrl);
-
-/**
- * @brief 设置一条用户的request head参数,Content-Length一般不需要,在设置POST的body时自动生成
- *
- * @param http_ctrl 客户端
- * @param name head参数的name
- * @param value head参数的value
- * @return 成功返回0,其他值失败
- */
-int luat_http_client_set_user_head(luat_http_ctrl_t *http_ctrl, const char *name, const char *value);
-
-
-/**
- * @brief 启动一个http请求
- *
- * @param http_ctrl 客户端
- * @param url http请求完整的url,如果有转义字符需要提前转义好
- * @param type 请求类型,0 get 1 post 2 put 3 delete
- * @param ipv6 是否存在IPV6的服务器
- * @param data_mode 大数据模式,接收数据超过1KB的时候,必须开启。开启后请求头里自动加入"Accept: application/octet-stream\r\n"
- * @return 成功返回0,其他值失败
- */
-int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t type, uint8_t ipv6, uint8_t continue_mode);
-/**
- * @brief 停止当前的http请求,调用后不再有http回调了
- *
- * @param http_ctrl 客户端
- * @return 成功返回0,其他值失败
- */
-
-int luat_http_client_close(luat_http_ctrl_t *http_ctrl);
-/**
- * @brief 完全释放掉当前的http客户端
- *
- * @param p_http_ctrl 客户端指针的地址
- * @return 成功返回0,其他值失败
- */
-int luat_http_client_destroy(luat_http_ctrl_t **p_http_ctrl);
-
-/**
- * @brief POST请求时发送body数据,如果数据量比较大,可以在HTTP_STATE_SEND_BODY回调里分次发送
- *
- * @param http_ctrl 客户端
- * @param data body数据
- * @param len body数据长度
- * @return 成功返回0,其他值失败
- */
-int luat_http_client_post_body(luat_http_ctrl_t *http_ctrl, void *data, uint32_t len);
-
-int luat_http_client_get_status_code(luat_http_ctrl_t *http_ctrl);
-
-int luat_http_client_pause(luat_http_ctrl_t *http_ctrl, uint8_t is_pause);
-/**
- * @brief GET请求时要求服务器从offset位置开始传输数据,谨慎使用
- *
- * @param http_ctrl 客户端
- * @param offset 偏移位置
- * @return 成功返回0,其他值失败
- */
-int luat_http_client_set_get_offset(luat_http_ctrl_t *http_ctrl, uint32_t offset);
-// int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t is_post, uint8_t ipv6, uint8_t data_mode);
-/** @}*/
-#endif
-#endif
+#ifndef LUAT_HTTP_H
+#define LUAT_HTTP_H
+
+#ifdef __LUATOS__
+#include "luat_zbuff.h"
+
+
+#if defined(AIR101) || defined(AIR103)
+// #define HTTP_REQ_HEADER_MAX_SIZE 	(2048)
+#define HTTP_RESP_BUFF_SIZE 		(2048)
+#else
+// #define HTTP_REQ_HEADER_MAX_SIZE 	(8192)
+#define HTTP_RESP_BUFF_SIZE 		(8192)
+#endif
+#else
+#include "http_parser.h"
+#define HTTP_HEADER_BASE_SIZE 	(1024)
+#define HTTP_RESP_BUFF_SIZE 	(4096)
+
+#endif
+/**
+ * @defgroup luatos_HTTP  HTTP(S)相关接口
+ * @{
+ */
+enum
+{
+	HTTP_STATE_IDLE,
+	HTTP_STATE_CONNECT,
+	HTTP_STATE_SEND_HEAD,
+	HTTP_STATE_SEND_BODY_START,
+	HTTP_STATE_SEND_BODY,
+	HTTP_STATE_GET_HEAD,
+	HTTP_STATE_GET_BODY,
+	HTTP_STATE_DONE,
+	HTTP_STATE_WAIT_CLOSE,
+};
+
+#ifndef __LUATOS__
+
+/*
+ * http运行过程的回调函数
+ * status >=0 表示运行状态,看HTTP_STATE_XXX <0说明出错停止了,==0表示结束了
+ * data 在获取响应阶段,会回调head数据,如果为NULL,则head接收完成了。如果设置了data_mode,body数据也直接回调。
+ * data_len 数据长度,head接收时,每行会多加一个\0,方便字符串处理
+ * user_param 用户自己的参数
+ */
+typedef void (*luat_http_cb)(int status, void *data, uint32_t data_len, void *user_param);
+
+#define HTTP_GET_DATA 		(2)
+#define HTTP_POST_DATA 		(1)
+#endif
+
+#define HTTP_OK 			(0)
+#define HTTP_ERROR_STATE 	(-1)
+#define HTTP_ERROR_HEADER 	(-2)
+#define HTTP_ERROR_BODY 	(-3)
+#define HTTP_ERROR_CONNECT 	(-4)
+#define HTTP_ERROR_CLOSE 	(-5)
+#define HTTP_ERROR_RX 		(-6)
+#define HTTP_ERROR_DOWNLOAD (-7)
+#define HTTP_ERROR_TIMEOUT  (-8)
+#define HTTP_ERROR_FOTA  	(-9)
+
+#define HTTP_CALLBACK 		(1)
+
+#define HTTP_RE_REQUEST_MAX (3)
+
+#define HTTP_TIMEOUT 		(10*60*1000) // 10分钟
+
+typedef struct{
+	network_ctrl_t *netc;		// http netc
+	http_parser  parser;	    //解析相关
+	char *host; 			/**< http host,需要释放 */
+	char* request_line;	/**< 需要释放,http请求的首行数据*/
+	uint16_t remote_port; 		/**< 远程端口号 */
+	uint8_t is_tls;             // 是否SSL
+	uint8_t custom_host;        /**< 是否自定义Host了*/
+	uint8_t is_post;
+	void* timeout_timer;			/**< timeout_timer 定时器*/
+	uint32_t timeout;
+	uint32_t tx_offset;
+#ifdef __LUATOS__
+	luat_ip_addr_t ip_addr;		// http ip
+	// const char *url;			// url
+	// const char *uri;			// uri
+	// char method[12];			// method
+
+	// 发送相关
+	// uint8_t request_message[HTTP_REQUEST_BUF_LEN_MAX];
+	char *req_header;
+	char *req_body;				//发送body
+	size_t req_body_len;		//发送body长度
+	luat_zbuff_t *zbuff_body;
+	char *req_auth;
+#ifdef LUAT_USE_FOTA
+	//OTA相关
+	uint8_t isfota;				//是否为ota下载
+	uint32_t address;			
+	uint32_t length;		
+	luat_spi_device_t* spi_device;
+#endif
+	int http_cb;				// http lua回调函数
+	int http_cb_userdata;				// http lua回调函数用户传参
+	//下载相关
+	uint8_t is_download;		//是否下载
+	char *dst;			//下载路径
+
+	// http_parser_settings parser_settings;
+	char* headers;
+	uint32_t headers_len;		//headers缓存长度
+	char* body;
+	uint32_t body_len;			//body缓存长度
+	// uint8_t is_chunk;		//是否chunk编码
+	uint8_t re_request_count;
+	// 响应相关
+	int32_t resp_content_len;	//content 长度
+	FILE* fd;					//下载 FILE
+	uint64_t idp;
+
+	uint8_t headers_complete;
+	uint8_t close_state;
+	char resp_buff[HTTP_RESP_BUFF_SIZE];
+	size_t resp_buff_offset;
+	size_t resp_headers_done;
+#else
+	Buffer_Struct request_head_buffer;	/**<存放用户自定义的请求head数据*/
+	luat_http_cb http_cb;				/**< http 回调函数 */
+	void *http_cb_userdata;				/**< http 回调函数用户传参*/
+	Buffer_Struct response_head_buffer;	/**<接收到的head数据缓存,回调给客户后就销毁了*/
+	int error_code;
+	Buffer_Struct response_cache;
+	uint32_t total_len;
+	uint32_t done_len;
+	uint32_t offset;
+	uint8_t retry_cnt_max;		/**<最大重试次数*/
+	uint8_t retry_cnt;
+	uint8_t state;
+	uint8_t data_mode;
+	uint8_t is_pause;
+	uint8_t debug_onoff;
+	uint8_t new_data;
+#endif
+}luat_http_ctrl_t;
+#ifdef __LUATOS__
+int luat_http_client_init(luat_http_ctrl_t* http, int ipv6);
+int luat_http_client_start(luat_http_ctrl_t* http);
+#else
+
+/**
+ * @brief 创建一个http客户端
+ *
+ * @param cb http运行过程回调函数
+ * @param user_param 回调时用户自己的参数
+ * @param adapter_index 网卡适配器,不清楚的写-1,系统自动分配
+ * @return 成功返回客户端地址,失败返回NULL
+ */
+luat_http_ctrl_t* luat_http_client_create(luat_http_cb cb, void *user_param, int adapter_index);
+/**
+ * @brief http客户端的通用配置,创建客户端时已经有默认配置,可以不配置
+ *
+ * @param http_ctrl 客户端
+ * @param timeout 单次数据传输超时时间,单位ms
+ * @param debug_onoff 是否开启调试打印,开启后会占用一点系统资源
+ * @param retry_cnt 因传输异常而重传的最大次数
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_base_config(luat_http_ctrl_t* http_ctrl, uint32_t timeout, uint8_t debug_onoff, uint8_t retry_cnt);
+/**
+ * @brief 客户端SSL配置,只有访问https才需要配置
+ *
+ * @param http_ctrl 客户端
+ * @param mode <0 关闭SSL功能,并忽略后续参数; 0忽略证书验证过程,大部分https应用就可以这个配置,后续证书配置可以都写NULL和0; 2强制证书验证,后续证书相关参数必须写对
+ * @param server_cert 服务器证书字符串,结尾必须有0,如果不忽略证书验证,这个必须有
+ * @param server_cert_len 服务器证书数据长度,长度包含结尾的0,也就是strlen(server_cert) + 1
+ * @param client_cert 客户端证书字符串,结尾必须有0,双向认证才有,一般金融行业可能会用
+ * @param client_cert_len 客户端证书数据长度,长度包含结尾的0
+ * @param client_cert_key 客户端证书私钥字符串,结尾必须有0,双向认证才有,一般金融行业可能会用
+ * @param client_cert_key_len 客户端证书私钥数据长度,长度包含结尾的0
+ * @param client_cert_key_password 客户端证书私钥密码字符串,结尾必须有0,双向认证才有,如果私钥没有密码保护,则不需要
+ * @param client_cert_key_password_len 客户端证书私钥密码数据长度,长度包含结尾的0
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_ssl_config(luat_http_ctrl_t* http_ctrl, int mode, const char *server_cert, uint32_t server_cert_len,
+		const char *client_cert, uint32_t client_cert_len,
+		const char *client_cert_key, uint32_t client_cert_key_len,
+		const char *client_cert_key_password, uint32_t client_cert_key_password_len);
+
+/**
+ * @brief 清空用户设置的POST数据和request head参数
+ *
+ * @param http_ctrl 客户端
+ * @return 成功返回0,其他值失败
+ */
+
+int luat_http_client_clear(luat_http_ctrl_t *http_ctrl);
+
+/**
+ * @brief 设置一条用户的request head参数,Content-Length一般不需要,在设置POST的body时自动生成
+ *
+ * @param http_ctrl 客户端
+ * @param name head参数的name
+ * @param value head参数的value
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_set_user_head(luat_http_ctrl_t *http_ctrl, const char *name, const char *value);
+
+
+/**
+ * @brief 启动一个http请求
+ *
+ * @param http_ctrl 客户端
+ * @param url http请求完整的url,如果有转义字符需要提前转义好
+ * @param type 请求类型,0 get 1 post 2 put 3 delete
+ * @param ipv6 是否存在IPV6的服务器
+ * @param data_mode 大数据模式,接收数据超过1KB的时候,必须开启。开启后请求头里自动加入"Accept: application/octet-stream\r\n"
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t type, uint8_t ipv6, uint8_t continue_mode);
+/**
+ * @brief 停止当前的http请求,调用后不再有http回调了
+ *
+ * @param http_ctrl 客户端
+ * @return 成功返回0,其他值失败
+ */
+
+int luat_http_client_close(luat_http_ctrl_t *http_ctrl);
+/**
+ * @brief 完全释放掉当前的http客户端
+ *
+ * @param p_http_ctrl 客户端指针的地址
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_destroy(luat_http_ctrl_t **p_http_ctrl);
+
+/**
+ * @brief POST请求时发送body数据,如果数据量比较大,可以在HTTP_STATE_SEND_BODY回调里分次发送
+ *
+ * @param http_ctrl 客户端
+ * @param data body数据
+ * @param len body数据长度
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_post_body(luat_http_ctrl_t *http_ctrl, void *data, uint32_t len);
+/**
+ * @brief http获取状态码
+ *
+ * @param http_ctrl 客户端
+ * @return 状态码
+ */
+int luat_http_client_get_status_code(luat_http_ctrl_t *http_ctrl);
+/**
+ * @brief http客户端设置暂停
+ *
+ * @param http_ctrl 客户端
+ * @param is_pause 是否暂停
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_pause(luat_http_ctrl_t *http_ctrl, uint8_t is_pause);
+/**
+ * @brief GET请求时要求服务器从offset位置开始传输数据,谨慎使用
+ *
+ * @param http_ctrl 客户端
+ * @param offset 偏移位置
+ * @return 成功返回0,其他值失败
+ */
+int luat_http_client_set_get_offset(luat_http_ctrl_t *http_ctrl, uint32_t offset);
+// int luat_http_client_start(luat_http_ctrl_t *http_ctrl, const char *url, uint8_t is_post, uint8_t ipv6, uint8_t data_mode);
+/** @}*/
+#endif
+#endif

+ 0 - 98
demo/aliyun2/main.lua

@@ -1,98 +0,0 @@
---[[
-本demo是演示aliyun2模块的使用,使用该模块可以连接阿里云物联网平台
-注意与aliyun库的差异
-
-TODO 本demo当前还不是很完整, 后续继续改造
-]]
-PROJECT = "aliyundemo"
-VERSION = "1.0.0"
-
-local sys = require "sys"
-local aliyun2 = require "aliyun2"
-
-if fskv then
-    fskv.init()
-end
-
-local rtos_bsp = rtos.bsp()
-function pinx() -- 根据不同开发板,给LED赋值不同的gpio引脚编号
-    if rtos_bsp == "AIR101" then -- Air101开发板LED引脚编号
-        return pin.PB08, pin.PB09, pin.PB10
-    elseif rtos_bsp == "AIR103" then -- Air103开发板LED引脚编号
-        return pin.PB26, pin.PB25, pin.PB24
-    elseif rtos_bsp == "AIR601" then -- Air103开发板LED引脚编号
-        return pin.PA7, 255, 255
-    elseif rtos_bsp == "AIR105" then -- Air105开发板LED引脚编号
-        return pin.PD14, pin.PD15, pin.PC3
-    elseif rtos_bsp == "ESP32C3" then -- ESP32C3开发板的引脚
-        return 12, 13, 255 -- 开发板上就2个灯
-    elseif rtos_bsp == "ESP32S3" then -- ESP32C3开发板的引脚
-        return 10, 11, 255 -- 开发板上就2个灯
-    elseif rtos_bsp == "EC618" then -- Air780E开发板引脚
-        return 27, 255, 255 -- AIR780E开发板上就一个灯
-    elseif rtos_bsp == "EC718P" then -- Air780E开发板引脚
-        return 27, 255, 255 -- AIR780EP开发板上就一个灯
-    elseif rtos_bsp == "UIS8850BM" then -- Air780UM开发板引脚
-        return 36, 255, 255 -- Air780UM开发板上就一个灯
-    else
-        log.info("main", "define led pin in main.lua")
-        return 0, 0, 0
-    end
-end
-
-
---LED引脚判断赋值结束
-
-local P1,P2,P3=pinx()--赋值开发板LED引脚编号
-local LEDA= gpio.setup(P1, 0, gpio.PULLUP)
--- local LEDB= gpio.setup(P2, 0, gpio.PULLUP)
--- local LEDC= gpio.setup(P3, 0, gpio.PULLUP)
-
-sys.taskInit(function()
-    sys.waitUntil("IP_READY")
-    local ali = aliyun2.create({
-        productKey = "a1QPg8JEj02",
-        deviceName = "869300038718048",
-        -- deviceSecret = "8837e03c52650fde37614fd078cac6e9",
-        productSecret = "tHqlKgc2Evq5V2m1",
-        regnwl = true
-    })
-    if ali and aliyun2.start(ali) then
-        while 1 do
-            local result, event, data, payload = sys.waitUntil(ali.topic, 30000)
-            if result then
-                log.info("aliyun", "event", event, data)
-                if event == "ota" then
-                    log.info("aliyun", "ota", data.url)
-                elseif event == "recv" then
-                    if data == ali.sub_topics.down_raw then
-                        log.info("aliyun", "下行的透传信息", data)
-                    elseif data == ali.sub_topics.property_set then
-                        log.info("aliyun2", "属性设置")
-                        local jdata = json.decode(payload)
-                        if jdata and jdata.params then
-                            if jdata.params.LEDSwitch then
-                                if jdata.params.LEDSwitch == 0 then
-                                    log.info("aliyun2", "关闭LEDA")
-                                    if LEDA then
-                                        LEDA(0)
-                                    end
-                                else
-                                    log.info("aliyun2", "打开LEDA")
-                                    if LEDA then
-                                        LEDA(1)
-                                    end
-                                end
-                            end
-                        end
-                    end
-                end
-            end
-        end
-    end
-end)
-
--- 用户代码已结束---------------------------------------------
--- 结尾总是这一句
-sys.run()
--- sys.run()之后后面不要加任何语句!!!!!

+ 387 - 387
luat/include/luat_audio.h

@@ -1,387 +1,387 @@
-/*
- * Copyright (c) 2022 OpenLuat & AirM2M
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of
- * this software and associated documentation files (the "Software"), to deal in
- * the Software without restriction, including without limitation the rights to
- * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
- * the Software, and to permit persons to whom the Software is furnished to do so,
- * subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
- * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
- * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#ifndef __LUAT_AUDIO_H__
-#define __LUAT_AUDIO_H__
-#include "luat_base.h"
-
-#include "luat_rtos.h"
-#include"luat_audio_codec.h"
-#include"luat_multimedia_codec.h"
-
-#ifndef __BSP_COMMON_H__
-#include "c_common.h"
-#endif
-
-typedef struct luat_audio_conf {
-    luat_multimedia_codec_t multimedia_codec;
-	uint64_t last_wakeup_time_ms;
-	luat_audio_codec_conf_t codec_conf;
-	void *hardware_data;
-	luat_rtos_timer_t pa_delay_timer;
-    uint32_t after_sleep_ready_time;                                    //
-    uint16_t pa_delay_time;
-	uint16_t power_off_delay_time;                                      // 电源关闭后延时时间
-	uint16_t soft_vol;
-    uint16_t speech_downlink_type;
-    uint16_t speech_uplink_type;
-    uint16_t i2s_rx_cb_save;
-	uint16_t last_vol;
-	uint16_t last_mic_vol;
-    uint8_t bus_type;
-    uint8_t raw_mode;
-    uint8_t debug_on_off;
-    uint8_t sleep_mode;
-    uint8_t wakeup_ready;
-    uint8_t pa_on_enable;
-    uint8_t record_mode;
-    uint8_t pa_pin;                                                     // pa pin
-	uint8_t pa_on_level;                                                // pa 使能电平
-	uint8_t power_pin;													// 电源控制
-	uint8_t power_on_level;                                             // 电源使能电平
-	uint8_t pa_is_control_enable;
-} luat_audio_conf_t;
-
-typedef enum{
-    LUAT_AUDIO_PM_RESUME = 0,       /* 工作模式 */
-    LUAT_AUDIO_PM_STANDBY,          /* 待机模式 */
-    LUAT_AUDIO_PM_SHUTDOWN,         /* 关断模式 */
-	LUAT_AUDIO_PM_POWER_OFF,        /* 完全断电模式 */
-}luat_audio_pm_mode_t;
-
-typedef enum{
-	LUAT_AUDIO_BUS_DAC=0,
-	LUAT_AUDIO_BUS_I2S,
-	LUAT_AUDIO_BUS_SOFT_DAC
-}luat_audio_bus_type_t;
-
-#ifdef LUAT_USE_RECORD
-#include "luat_i2s.h"
-typedef struct{
-//	luat_rtos_task_handle task_handle;
-	FILE* fd;
-    uint8_t multimedia_id;
-	uint8_t quailty;
-	uint8_t type;
-	uint8_t is_run;
-	uint32_t record_time;
-	uint32_t record_time_tmp;
-	void* encoder_handler;
-	luat_zbuff_t * record_buffer[2];
-	int record_buffer_index;
-	int zbuff_ref[2];
-    uint32_t bak_sample_rate;                                   // i2s采样率
-    uint32_t bak_cb_rx_len;                                     // 接收触发回调数据长度
-    int (*bak_luat_i2s_event_callback)(uint8_t id ,luat_i2s_event_t event, uint8_t *rx_data, uint32_t rx_len, void *param); //  i2s回调函数
-    uint8_t bak_is_full_duplex;		                            // 是否全双工
-}luat_record_ctrl_t;
-
-#endif
-
-/**
- * @brief 设置音频硬件输出类型,后续初始化都会根据类型做不同处理,所以要首先使用此函数设置类型!!!
- *
- * @param bus_type 见MULTIMEDIA_AUDIO_BUS,目前只有0=DAC 1=I2S 2=SOFT_DAC
- */
-int luat_audio_set_bus_type(uint8_t multimedia_id,uint8_t bus_type);
-
-//此函数可获取multimedia_id对应的audio结构体,用于动态修改,如果有无法直接设置的函数可自行通过此方法修改结构体,一般不需要使用此函数
-luat_audio_conf_t *luat_audio_get_config(uint8_t multimedia_id);
-
-/**
- * @brief audio和codec绑定
- *
- * @param multimedia_id 多媒体通道,目前只有0
- * @param codec_conf codec信息
- * @return int =0成功,其他失败
- */
-int luat_audio_setup_codec(uint8_t multimedia_id, const luat_audio_codec_conf_t *codec_conf);
-
-/**
- * @brief 初始化audio
- *
- * @param multimedia_id 多媒体通道,目前只有0
- * @param init_vol 默认硬件音量
- * @param init_mic_vol 默认MIC音量
- * @return int =0成功,其他失败
- */
-int luat_audio_init(uint8_t multimedia_id, uint16_t init_vol, uint16_t init_mic_vol);
-
-/**
- * @brief audio休眠控制,注意,pm各个模式下功耗由具体audio硬件决定
- * 
- * @param multimedia_id 多媒体通道
- * @param mode 
- * @return int =0成功,其他失败
- */
-int luat_audio_pm_request(uint8_t multimedia_id,luat_audio_pm_mode_t mode);
-
-/**
- * @brief 播放空白音,一般不需要主动调用
- * 
- * @param multimedia_id 多媒体通道
- * @param on_off 1打开空白,0关闭
- * @return int =0成功,其他失败
- */
-int luat_audio_play_blank(uint8_t multimedia_id, uint8_t on_off);
-
-#ifdef __LUATOS__
-/**
- * @brief 播放指定数量的文件或者ROM数组(文件数据直接写成数组形式)
- *
- * @param multimedia_id 多媒体通道,目前只有0
- * @param info 文件信息,文件路径信息
- * @param files_num 文件数量
- * @return int =0成功,其他失败
- */
-int luat_audio_play_multi_files(uint8_t multimedia_id, uData_t *info, uint32_t files_num, uint8_t error_stop);
-#endif
-/**
- * @brief 播放指定的文件或
- *
- * @param multimedia_id 多媒体通道,目前只有0
- * @param path 文件路径
- * @return int =0成功,其他失败
- */
-int luat_audio_play_file(uint8_t multimedia_id, const char *path);
-/**
- * @brief 是否播放完全部数据
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @return uint8_t =1是,=0没有
- */
-uint8_t luat_audio_is_finish(uint8_t multimedia_id);
-
-/**
- * @brief 强制停止播放文件,但是不会停止已经输出到底层驱动的数据播放
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @return int =0成功,其他失败
- */
-int luat_audio_play_stop(uint8_t multimedia_id);
-
-/**
- * @brief 获取上一次播放结果,在MULTIMEDIA_CB_AUDIO_DONE回调时调用最佳
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @return int =0完整的播放完成,<0被用户停止了,>0 TTS失败,或者第几个音频文件解码失败(用户在play_info未设置了解码失败后继续,文件位置+1)
- */
-int luat_audio_play_get_last_error(uint8_t multimedia_id);
-
-
-/**
- * @brief 立刻初始化播放未编码的原始音频数据流
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @param audio_format 音频数据格式,目前只支持PCM,即需要手动解码
- * @param num_channels 声道数,目前只能1或2
- * @param sample_rate 采样率,注意只有8K,16K,32K,48K,96K,22.05K,44.1K这些能被支持
- * @param bits_per_sample 量化bit,只能是16
- * @param is_signed 量化数据是否带符号,只能是1
- * @return int =0成功,其他失败
- */
-int luat_audio_start_raw(uint8_t multimedia_id, uint8_t audio_format, uint8_t num_channels, uint32_t sample_rate, uint8_t bits_per_sample, uint8_t is_signed);
-/**
- * @brief 向底层驱动传入一段原始音频数据
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @param data 原始音频数据
- * @param len 原始音频数据长度
- * @return int =0成功,其他失败
- */
-int luat_audio_write_raw(uint8_t multimedia_id, uint8_t *data, uint32_t len);
-/**
- * @brief 强制停止所有播放,同时底层驱动也会停止输出,不要用于播放文件的结束
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @return int =0成功,其他失败
- */
-int luat_audio_stop_raw(uint8_t multimedia_id);
-/**
- * @brief 暂停/恢复播放
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @param is_pause 0恢复,其他暂停
- * @return int =0成功,其他失败
- */
-int luat_audio_pause_raw(uint8_t multimedia_id, uint8_t is_pause);
-
-/**
- * @brief 编码并播放一段文字
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @param text 文字数据
- * @param text_bytes 文字数据长度
- * @return int =0成功,其他失败
- */
-int luat_audio_play_tts_text(uint8_t multimedia_id, void *text, uint32_t text_bytes);
-/**
- * @brief 在收到MULTIMEDIA_CB_TTS_INIT回调时,可以设置TTS参数,等同于ivTTS_SetParam
- *
- * @param multimedia_id multimedia_id 多媒体通道,目前只有0
- * @param param_id 见ivTTS_PARAM_XXX
- * @param param_value param_id对应的value
- * @return int =0成功,其他失败
- */
-int luat_audio_play_tts_set_param(uint8_t multimedia_id, uint32_t param_id, uint32_t param_value);
-
-/**
- * @brief pa引脚配置
- * 
- * @param multimedia_id multimedia_id 多媒体通道
- * @param pin pa pin
- * @param level pa使能电平
- * @param dummy_time_len 
- * @param pa_delay_time 
- */
-void luat_audio_config_pa(uint8_t multimedia_id, uint32_t pin, int level, uint32_t dummy_time_len, uint32_t pa_delay_time);
-
-/**
- * @brief power引脚配置
- * 
- * @param multimedia_id 多媒体通道
- * @param pin power pin
- * @param level 使能电平
- * @param dac_off_delay_time 
- */
-void luat_audio_config_dac(uint8_t multimedia_id, int pin, int level, uint32_t dac_off_delay_time);
-
-/**
- * @brief 音量控制
- * 
- * @param multimedia_id 多媒体通道
- * @param vol 音量,0-1000 0-100为硬件 100-1000为软件缩放,实际根据不同bsp不同硬件底层实现
- * @return uint16_t 音量,0-1000
- */
-uint16_t luat_audio_vol(uint8_t multimedia_id, uint16_t vol);
-
-/**
- * @brief mic音量控制
- * 
- * @param multimedia_id 多媒体通道
- * @param vol 音量,0-100
- * @return uint8_t 音量 0-100
- */
-uint8_t luat_audio_mic_vol(uint8_t multimedia_id, uint16_t vol);
-
-/**
- * @brief 静音
- * 
- * @param multimedia_id 多媒体通道
- * @param on 1 静音,0 取消静音
- * @return uint8_t 1 静音,0 取消静音
- */
-uint8_t luat_audio_mute(uint8_t multimedia_id, uint8_t on);
-
-/**
- * @brief 调试开关
- * 
- * @param multimedia_id 多媒体通道
- * @param onoff 0关闭,1打开
- */
-void luat_audio_play_debug_onoff(uint8_t multimedia_id, uint8_t onoff);
-
-/**
- * @brief 检测音频是否准备就绪
- * 
- * @param multimedia_id 多媒体通道
- * @return int -1未就绪,0就绪
- */
-int luat_audio_check_ready(uint8_t multimedia_id);
-
-/**
- * @brief 录音并播放
- * 
- * @param multimedia_id 多媒体通道
- * @param sample_rate 采样率
- * @param play_buffer buffer
- * @param one_trunk_len 一次传输长度
- * @param total_trunk_cnt 传输次数
- * @return int 成功返回0,失败返回-1
- */
-int luat_audio_record_and_play(uint8_t multimedia_id, uint32_t sample_rate, const uint8_t *play_buffer, uint32_t one_trunk_len, uint32_t total_trunk_cnt);
-
-/**
- * @brief 录音停止
- * 
- * @param multimedia_id 多媒体通道
- * @return int 成功返回0,失败返回-1
- */
-int luat_audio_record_stop(uint8_t multimedia_id);
-
-/**
- * @brief 开始通话输出
- * 
- * @param multimedia_id 多媒体通道
- * @param is_downlink 通话是否连接
- * @param type 类型
- * @param downlink_buffer buffer
- * @param buffer_len buffer 长度
- * @param channel_num 通道
- * @return int 成功返回0,失败返回-1
- */
-int luat_audio_speech(uint8_t multimedia_id, uint8_t is_downlink, uint8_t type, const uint8_t *downlink_buffer, uint32_t buffer_len, uint8_t channel_num);
-
-/**
- * @brief 通话输出停止
- * 
- * @param multimedia_id 多媒体通道
- * @return int 成功返回0,失败返回-1
- */
-int luat_audio_speech_stop(uint8_t multimedia_id);
-
-/**
- * @brief pa控制函数,一般不需要使用,底层会自动调用
- * 
- * @param multimedia_id multimedia_id 多媒体通道
- * @param on 1开,0关
- * @param delay 延迟时间,非阻塞,不延迟写0
- */
-void luat_audio_pa(uint8_t multimedia_id,uint8_t on, uint32_t delay);
-
-/**
- * @brief power控制函数,一般不需要使用,底层会自动调用
- * 
- * @param multimedia_id 多媒体通道
- * @param on 1开,0关
- */
-void luat_audio_power(uint8_t multimedia_id,uint8_t on);
-/**
- * @brief power保持控制,部分平台休眠时部分GPIO断电,因此需要控制是否进入休眠来
- *
- * @param on_off 1保持,0不保持
- */
-void luat_audio_power_keep_ctrl_by_bsp(uint8_t on_off);
-
-/**
- * @brief 把api投放到audio task运行
- *
- * @param api 需要运行的api
- * @param data api输入数据
- * @param len api输入数据长度
- */
-void luat_audio_run_callback_in_task(void *api, uint8_t *data, uint32_t len);
-
-void *luat_audio_inter_amr_coder_init(uint8_t is_wb, uint8_t quality);
-int luat_audio_inter_amr_coder_encode(void *handle, const uint16_t *pcm_buf, uint8_t *amr_buf, uint8_t *amr_len);
-int luat_audio_inter_amr_coder_decode(void *handle, uint16_t *pcm_buf, const uint8_t *amr_buf, uint8_t *amr_len);
-void luat_audio_inter_amr_coder_deinit(void *handle);
-#endif
+/*
+ * Copyright (c) 2022 OpenLuat & AirM2M
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __LUAT_AUDIO_H__
+#define __LUAT_AUDIO_H__
+#include "luat_base.h"
+
+#include "luat_rtos.h"
+#include"luat_audio_codec.h"
+#include"luat_multimedia_codec.h"
+
+#ifndef __BSP_COMMON_H__
+#include "c_common.h"
+#endif
+
+typedef struct luat_audio_conf {
+    luat_multimedia_codec_t multimedia_codec;
+	uint64_t last_wakeup_time_ms;
+	luat_audio_codec_conf_t codec_conf;
+	void *hardware_data;
+	luat_rtos_timer_t pa_delay_timer;
+    uint32_t after_sleep_ready_time;                                    //
+    uint16_t pa_delay_time;
+	uint16_t power_off_delay_time;                                      // 电源关闭后延时时间
+	uint16_t soft_vol;
+    uint16_t speech_downlink_type;
+    uint16_t speech_uplink_type;
+    uint16_t i2s_rx_cb_save;
+	uint16_t last_vol;
+	uint16_t last_mic_vol;
+    uint8_t bus_type;
+    uint8_t raw_mode;
+    uint8_t debug_on_off;
+    uint8_t sleep_mode;
+    uint8_t wakeup_ready;
+    uint8_t pa_on_enable;
+    uint8_t record_mode;
+    uint8_t pa_pin;                                                     // pa pin
+	uint8_t pa_on_level;                                                // pa 使能电平
+	uint8_t power_pin;													// 电源控制
+	uint8_t power_on_level;                                             // 电源使能电平
+	uint8_t pa_is_control_enable;
+} luat_audio_conf_t;
+
+typedef enum{
+    LUAT_AUDIO_PM_RESUME = 0,       /* 工作模式 */
+    LUAT_AUDIO_PM_STANDBY,          /* 待机模式 */
+    LUAT_AUDIO_PM_SHUTDOWN,         /* 关断模式 */
+	LUAT_AUDIO_PM_POWER_OFF,        /* 完全断电模式 */
+}luat_audio_pm_mode_t;
+
+typedef enum{
+	LUAT_AUDIO_BUS_DAC=0,
+	LUAT_AUDIO_BUS_I2S,
+	LUAT_AUDIO_BUS_SOFT_DAC
+}luat_audio_bus_type_t;
+
+#ifdef LUAT_USE_RECORD
+#include "luat_i2s.h"
+typedef struct{
+//	luat_rtos_task_handle task_handle;
+	FILE* fd;
+    uint8_t multimedia_id;
+	uint8_t quailty;
+	uint8_t type;
+	uint8_t is_run;
+	uint32_t record_time;
+	uint32_t record_time_tmp;
+	void* encoder_handler;
+	luat_zbuff_t * record_buffer[2];
+	int record_buffer_index;
+	int zbuff_ref[2];
+    uint32_t bak_sample_rate;                                   // i2s采样率
+    uint32_t bak_cb_rx_len;                                     // 接收触发回调数据长度
+    int (*bak_luat_i2s_event_callback)(uint8_t id ,luat_i2s_event_t event, uint8_t *rx_data, uint32_t rx_len, void *param); //  i2s回调函数
+    uint8_t bak_is_full_duplex;		                            // 是否全双工
+}luat_record_ctrl_t;
+
+#endif
+
+/**
+ * @brief 设置音频硬件输出类型,后续初始化都会根据类型做不同处理,所以要首先使用此函数设置类型!!!
+ *
+ * @param bus_type 见MULTIMEDIA_AUDIO_BUS,目前只有0=DAC 1=I2S 2=SOFT_DAC
+ */
+int luat_audio_set_bus_type(uint8_t multimedia_id,uint8_t bus_type);
+
+//此函数可获取multimedia_id对应的audio结构体,用于动态修改,如果有无法直接设置的函数可自行通过此方法修改结构体,一般不需要使用此函数
+luat_audio_conf_t *luat_audio_get_config(uint8_t multimedia_id);
+
+/**
+ * @brief audio和codec绑定
+ *
+ * @param multimedia_id 多媒体通道,目前只有0
+ * @param codec_conf codec信息
+ * @return int =0成功,其他失败
+ */
+int luat_audio_setup_codec(uint8_t multimedia_id, const luat_audio_codec_conf_t *codec_conf);
+
+/**
+ * @brief 初始化audio
+ *
+ * @param multimedia_id 多媒体通道,目前只有0
+ * @param init_vol 默认硬件音量
+ * @param init_mic_vol 默认MIC音量
+ * @return int =0成功,其他失败
+ */
+int luat_audio_init(uint8_t multimedia_id, uint16_t init_vol, uint16_t init_mic_vol);
+
+/**
+ * @brief audio休眠控制,注意,pm各个模式下功耗由具体audio硬件决定
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param mode 
+ * @return int =0成功,其他失败
+ */
+int luat_audio_pm_request(uint8_t multimedia_id,luat_audio_pm_mode_t mode);
+
+/**
+ * @brief 播放空白音,一般不需要主动调用
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param on_off 1打开空白,0关闭
+ * @return int =0成功,其他失败
+ */
+int luat_audio_play_blank(uint8_t multimedia_id, uint8_t on_off);
+
+#ifdef __LUATOS__
+/**
+ * @brief 播放指定数量的文件或者ROM数组(文件数据直接写成数组形式)
+ *
+ * @param multimedia_id 多媒体通道,目前只有0
+ * @param info 文件信息,文件路径信息
+ * @param files_num 文件数量
+ * @return int =0成功,其他失败
+ */
+int luat_audio_play_multi_files(uint8_t multimedia_id, uData_t *info, uint32_t files_num, uint8_t error_stop);
+#endif
+/**
+ * @brief 播放指定的文件或
+ *
+ * @param multimedia_id 多媒体通道,目前只有0
+ * @param path 文件路径
+ * @return int =0成功,其他失败
+ */
+int luat_audio_play_file(uint8_t multimedia_id, const char *path);
+/**
+ * @brief 是否播放完全部数据
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @return uint8_t =1是,=0没有
+ */
+uint8_t luat_audio_is_finish(uint8_t multimedia_id);
+
+/**
+ * @brief 强制停止播放文件,但是不会停止已经输出到底层驱动的数据播放
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @return int =0成功,其他失败
+ */
+int luat_audio_play_stop(uint8_t multimedia_id);
+
+/**
+ * @brief 获取上一次播放结果,在MULTIMEDIA_CB_AUDIO_DONE回调时调用最佳
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @return int =0完整的播放完成,<0被用户停止了,>0 TTS失败,或者第几个音频文件解码失败(用户在play_info未设置了解码失败后继续,文件位置+1)
+ */
+int luat_audio_play_get_last_error(uint8_t multimedia_id);
+
+
+/**
+ * @brief 立刻初始化播放未编码的原始音频数据流
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @param audio_format 音频数据格式,目前只支持PCM,即需要手动解码
+ * @param num_channels 声道数,目前只能1或2
+ * @param sample_rate 采样率,注意只有8K,16K,32K,48K,96K,22.05K,44.1K这些能被支持
+ * @param bits_per_sample 量化bit,只能是16
+ * @param is_signed 量化数据是否带符号,只能是1
+ * @return int =0成功,其他失败
+ */
+int luat_audio_start_raw(uint8_t multimedia_id, uint8_t audio_format, uint8_t num_channels, uint32_t sample_rate, uint8_t bits_per_sample, uint8_t is_signed);
+/**
+ * @brief 向底层驱动传入一段原始音频数据
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @param data 原始音频数据
+ * @param len 原始音频数据长度
+ * @return int =0成功,其他失败
+ */
+int luat_audio_write_raw(uint8_t multimedia_id, uint8_t *data, uint32_t len);
+/**
+ * @brief 强制停止所有播放,同时底层驱动也会停止输出,不要用于播放文件的结束
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @return int =0成功,其他失败
+ */
+int luat_audio_stop_raw(uint8_t multimedia_id);
+/**
+ * @brief 暂停/恢复播放
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @param is_pause 0恢复,其他暂停
+ * @return int =0成功,其他失败
+ */
+int luat_audio_pause_raw(uint8_t multimedia_id, uint8_t is_pause);
+
+/**
+ * @brief 编码并播放一段文字
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @param text 文字数据
+ * @param text_bytes 文字数据长度
+ * @return int =0成功,其他失败
+ */
+int luat_audio_play_tts_text(uint8_t multimedia_id, void *text, uint32_t text_bytes);
+/**
+ * @brief 在收到MULTIMEDIA_CB_TTS_INIT回调时,可以设置TTS参数,等同于ivTTS_SetParam
+ *
+ * @param multimedia_id multimedia_id 多媒体通道,目前只有0
+ * @param param_id 见ivTTS_PARAM_XXX
+ * @param param_value param_id对应的value
+ * @return int =0成功,其他失败
+ */
+int luat_audio_play_tts_set_param(uint8_t multimedia_id, uint32_t param_id, uint32_t param_value);
+
+/**
+ * @brief pa引脚配置
+ * 
+ * @param multimedia_id multimedia_id 多媒体通道
+ * @param pin pa pin
+ * @param level pa使能电平
+ * @param dummy_time_len 
+ * @param pa_delay_time 
+ */
+void luat_audio_config_pa(uint8_t multimedia_id, uint32_t pin, int level, uint32_t dummy_time_len, uint32_t pa_delay_time);
+
+/**
+ * @brief power引脚配置
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param pin power pin
+ * @param level 使能电平
+ * @param dac_off_delay_time 
+ */
+void luat_audio_config_dac(uint8_t multimedia_id, int pin, int level, uint32_t dac_off_delay_time);
+
+/**
+ * @brief 音量控制
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param vol 音量,0-1000 0-100为硬件 100-1000为软件缩放,实际根据不同bsp不同硬件底层实现
+ * @return uint16_t 音量,0-1000
+ */
+uint16_t luat_audio_vol(uint8_t multimedia_id, uint16_t vol);
+
+/**
+ * @brief mic音量控制
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param vol 音量,0-100
+ * @return uint8_t 音量 0-100
+ */
+uint8_t luat_audio_mic_vol(uint8_t multimedia_id, uint16_t vol);
+
+/**
+ * @brief 静音
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param on 1 静音,0 取消静音
+ * @return uint8_t 1 静音,0 取消静音
+ */
+uint8_t luat_audio_mute(uint8_t multimedia_id, uint8_t on);
+
+/**
+ * @brief 音频调试开关
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param onoff 0关闭,1打开
+ */
+void luat_audio_play_debug_onoff(uint8_t multimedia_id, uint8_t onoff);
+
+/**
+ * @brief 检测音频是否准备就绪
+ * 
+ * @param multimedia_id 多媒体通道
+ * @return int -1未就绪,0就绪
+ */
+int luat_audio_check_ready(uint8_t multimedia_id);
+
+/**
+ * @brief 录音并播放
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param sample_rate 采样率
+ * @param play_buffer buffer
+ * @param one_trunk_len 一次传输长度
+ * @param total_trunk_cnt 传输次数
+ * @return int 成功返回0,失败返回-1
+ */
+int luat_audio_record_and_play(uint8_t multimedia_id, uint32_t sample_rate, const uint8_t *play_buffer, uint32_t one_trunk_len, uint32_t total_trunk_cnt);
+
+/**
+ * @brief 录音停止
+ * 
+ * @param multimedia_id 多媒体通道
+ * @return int 成功返回0,失败返回-1
+ */
+int luat_audio_record_stop(uint8_t multimedia_id);
+
+/**
+ * @brief 开始通话输出
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param is_downlink 通话是否连接
+ * @param type 类型
+ * @param downlink_buffer buffer
+ * @param buffer_len buffer 长度
+ * @param channel_num 通道
+ * @return int 成功返回0,失败返回-1
+ */
+int luat_audio_speech(uint8_t multimedia_id, uint8_t is_downlink, uint8_t type, const uint8_t *downlink_buffer, uint32_t buffer_len, uint8_t channel_num);
+
+/**
+ * @brief 通话输出停止
+ * 
+ * @param multimedia_id 多媒体通道
+ * @return int 成功返回0,失败返回-1
+ */
+int luat_audio_speech_stop(uint8_t multimedia_id);
+
+/**
+ * @brief pa控制函数,一般不需要使用,底层会自动调用
+ * 
+ * @param multimedia_id multimedia_id 多媒体通道
+ * @param on 1开,0关
+ * @param delay 延迟时间,非阻塞,不延迟写0
+ */
+void luat_audio_pa(uint8_t multimedia_id,uint8_t on, uint32_t delay);
+
+/**
+ * @brief power控制函数,一般不需要使用,底层会自动调用
+ * 
+ * @param multimedia_id 多媒体通道
+ * @param on 1开,0关
+ */
+void luat_audio_power(uint8_t multimedia_id,uint8_t on);
+/**
+ * @brief power保持控制,部分平台休眠时部分GPIO断电,因此需要控制是否进入休眠来
+ *
+ * @param on_off 1保持,0不保持
+ */
+void luat_audio_power_keep_ctrl_by_bsp(uint8_t on_off);
+
+/**
+ * @brief 把api投放到audio task运行
+ *
+ * @param api 需要运行的api
+ * @param data api输入数据
+ * @param len api输入数据长度
+ */
+void luat_audio_run_callback_in_task(void *api, uint8_t *data, uint32_t len);
+
+void *luat_audio_inter_amr_coder_init(uint8_t is_wb, uint8_t quality);
+int luat_audio_inter_amr_coder_encode(void *handle, const uint16_t *pcm_buf, uint8_t *amr_buf, uint8_t *amr_len);
+int luat_audio_inter_amr_coder_decode(void *handle, uint16_t *pcm_buf, const uint8_t *amr_buf, uint8_t *amr_len);
+void luat_audio_inter_amr_coder_deinit(void *handle);
+#endif

+ 141 - 134
luat/include/luat_crypto.h

@@ -1,134 +1,141 @@
-#ifndef LUAT_CRYPTO_H
-#define LUAT_CRYPTO_H
-#include "luat_base.h"
-
-#define LUAT_CRYPTO_AES_ECB 1
-#define LUAT_CRYPTO_AES_CBC 2
-#define LUAT_CRYPTO_AES_CTR 3
-#define LUAT_CRYPTO_AES_CFB 4
-#define LUAT_CRYPTO_AES_OFB 5
-
-#define LUAT_CRYPTO_AES_PAD_ZERO 1
-#define LUAT_CRYPTO_AES_PAD_5 2
-#define LUAT_CRYPTO_AES_PAD_7 3
-
-typedef struct
-{
-    size_t result_size;
-    size_t key_len;
-	void* ctx;
-}luat_crypt_stream_t;
-/**
- * @defgroup luatos_crypto crypto数据加密
- * @{
- */
-int luat_crypto_trng(char* buff, size_t len);
-/// @brief 计算md5值
-/// @param str 需要计算的字符串
-/// @param str_size  需要计算的字符串的长度
-/// @param out_ptr 输出
-/// @return 成功0,失败-1
-int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr);
-/// @brief 计算hmac_md5值
-/// @param str 需要计算的字符串
-/// @param str_size 字符串长度
-/// @param mac 密钥
-/// @param mac_size 密钥的长度
-/// @param out_ptr 输出
-/// @return 成功0,失败-1
-int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
-
-/// @brief 计算sha1值
-/// @param str  需要计算的字符串
-/// @param str_size 需要计算的字符串的长度
-/// @param out_ptr 输出
-/// @return  成功0,失败-1
-int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr);
-/// @brief 计算hmac_sha1值
-/// @param str 需要计算的字符串
-/// @param str_size 字符串长度
-/// @param mac 密钥
-/// @param mac_size 密钥的长度
-/// @param out_ptr 输出
-/// @return 成功0,失败-1
-int luat_crypto_hmac_sha1_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
-/// @brief 计算sha256值
-/// @param str  需要计算的字符串
-/// @param str_size 需要计算的字符串的长度
-/// @param out_ptr 输出
-/// @return  成功0,失败-1
-int luat_crypto_sha256_simple(const char* str, size_t str_size, void* out_ptr);
-/// @brief 计算hmac_sha256值
-/// @param str 需要计算的字符串
-/// @param str_size 字符串长度
-/// @param mac 密钥
-/// @param mac_size 密钥的长度
-/// @param out_ptr 输出
-/// @return 成功0,失败-1
-int luat_crypto_hmac_sha256_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
-/// @brief 计算sha512值
-/// @param str  需要计算的字符串
-/// @param str_size 需要计算的字符串的长度
-/// @param out_ptr 输出
-/// @return  成功0,失败-1
-int luat_crypto_sha512_simple(const char* str, size_t str_size, void* out_ptr) ;
-/// @brief 计算hmac_sha512值
-/// @param str 需要计算的字符串
-/// @param str_size 字符串长度
-/// @param mac 密钥
-/// @param mac_size 密钥的长度
-/// @param out_ptr 输出
-/// @return 成功0,失败-1
-int luat_crypto_hmac_sha512_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
-/**
- * @brief BASE64加密
- * @param dst buffer
- * @param dlen buffer长度
- * @param olen 写入的字节数
- * @param src 加密密钥
- * @param slen 加密密钥长度
- * @return 0成功
- */
-int luat_crypto_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen ) ;
-/**
- * @brief BASE64解密
- * @param dst buffer
- * @param dlen buffer长度
- * @param olen 写入的字节数
- * @param src 密钥
- * @param slen 密钥长度
- * @return 0成功
- */
-int luat_crypto_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen ) ;
-/**@}*/ 
-int luat_crypto_cipher_list(const char** list, size_t* len);
-int luat_crypto_cipher_suites(const char** list, size_t* len);
-
-int luat_crypto_md(const char* md, const char* str, size_t str_size, void* out_ptr, const char* key, size_t key_len);
-int luat_crypto_md_file(const char* md, void* out_ptr, const char* key, size_t key_len, const char* path);
-
-int luat_crypto_md_init(const char* md, const char* key, luat_crypt_stream_t *stream);
-int luat_crypto_md_update(const char* str, size_t str_size, luat_crypt_stream_t *stream);
-int luat_crypto_md_finish(void* out_ptr, luat_crypt_stream_t *stream);
-
-typedef struct luat_crypto_cipher_ctx
-{
-    const char* cipher;
-    const char* pad;
-    const char* str;
-    const char* key;
-    const char* iv;
-    
-    size_t cipher_size;
-    size_t pad_size;
-    size_t str_size;
-    size_t key_size;
-    size_t iv_size;
-    char* outbuff;
-    size_t outlen;
-    uint8_t flags;
-}luat_crypto_cipher_ctx_t;
-
-int luat_crypto_cipher_xxx(luat_crypto_cipher_ctx_t* cctx);
-
-#endif
+#ifndef LUAT_CRYPTO_H
+#define LUAT_CRYPTO_H
+#include "luat_base.h"
+
+#define LUAT_CRYPTO_AES_ECB 1
+#define LUAT_CRYPTO_AES_CBC 2
+#define LUAT_CRYPTO_AES_CTR 3
+#define LUAT_CRYPTO_AES_CFB 4
+#define LUAT_CRYPTO_AES_OFB 5
+
+#define LUAT_CRYPTO_AES_PAD_ZERO 1
+#define LUAT_CRYPTO_AES_PAD_5 2
+#define LUAT_CRYPTO_AES_PAD_7 3
+
+typedef struct
+{
+    size_t result_size;
+    size_t key_len;
+	void* ctx;
+}luat_crypt_stream_t;
+/**
+ * @defgroup luatos_crypto crypto数据加密
+ * @{
+ */
+/**
+ * @brief 生成随机数
+ * 
+ * @param buff 随机数保存内存
+ * @param len 长度
+ * @return int
+ */
+int luat_crypto_trng(char* buff, size_t len);
+/// @brief 计算md5值
+/// @param str 需要计算的字符串
+/// @param str_size  需要计算的字符串的长度
+/// @param out_ptr 输出
+/// @return 成功0,失败-1
+int luat_crypto_md5_simple(const char* str, size_t str_size, void* out_ptr);
+/// @brief 计算hmac_md5值
+/// @param str 需要计算的字符串
+/// @param str_size 字符串长度
+/// @param mac 密钥
+/// @param mac_size 密钥的长度
+/// @param out_ptr 输出
+/// @return 成功0,失败-1
+int luat_crypto_hmac_md5_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
+
+/// @brief 计算sha1值
+/// @param str  需要计算的字符串
+/// @param str_size 需要计算的字符串的长度
+/// @param out_ptr 输出
+/// @return  成功0,失败-1
+int luat_crypto_sha1_simple(const char* str, size_t str_size, void* out_ptr);
+/// @brief 计算hmac_sha1值
+/// @param str 需要计算的字符串
+/// @param str_size 字符串长度
+/// @param mac 密钥
+/// @param mac_size 密钥的长度
+/// @param out_ptr 输出
+/// @return 成功0,失败-1
+int luat_crypto_hmac_sha1_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr);
+/// @brief 计算sha256值
+/// @param str  需要计算的字符串
+/// @param str_size 需要计算的字符串的长度
+/// @param out_ptr 输出
+/// @return  成功0,失败-1
+int luat_crypto_sha256_simple(const char* str, size_t str_size, void* out_ptr);
+/// @brief 计算hmac_sha256值
+/// @param str 需要计算的字符串
+/// @param str_size 字符串长度
+/// @param mac 密钥
+/// @param mac_size 密钥的长度
+/// @param out_ptr 输出
+/// @return 成功0,失败-1
+int luat_crypto_hmac_sha256_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
+/// @brief 计算sha512值
+/// @param str  需要计算的字符串
+/// @param str_size 需要计算的字符串的长度
+/// @param out_ptr 输出
+/// @return  成功0,失败-1
+int luat_crypto_sha512_simple(const char* str, size_t str_size, void* out_ptr) ;
+/// @brief 计算hmac_sha512值
+/// @param str 需要计算的字符串
+/// @param str_size 字符串长度
+/// @param mac 密钥
+/// @param mac_size 密钥的长度
+/// @param out_ptr 输出
+/// @return 成功0,失败-1
+int luat_crypto_hmac_sha512_simple(const char* str, size_t str_size, const char* mac, size_t mac_size, void* out_ptr) ;
+/**
+ * @brief BASE64加密
+ * @param dst buffer
+ * @param dlen buffer长度
+ * @param olen 写入的字节数
+ * @param src 加密密钥
+ * @param slen 加密密钥长度
+ * @return 0成功
+ */
+int luat_crypto_base64_encode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen ) ;
+/**
+ * @brief BASE64解密
+ * @param dst buffer
+ * @param dlen buffer长度
+ * @param olen 写入的字节数
+ * @param src 密钥
+ * @param slen 密钥长度
+ * @return 0成功
+ */
+int luat_crypto_base64_decode( unsigned char *dst, size_t dlen, size_t *olen, const unsigned char *src, size_t slen ) ;
+/**@}*/ 
+int luat_crypto_cipher_list(const char** list, size_t* len);
+int luat_crypto_cipher_suites(const char** list, size_t* len);
+
+int luat_crypto_md(const char* md, const char* str, size_t str_size, void* out_ptr, const char* key, size_t key_len);
+int luat_crypto_md_file(const char* md, void* out_ptr, const char* key, size_t key_len, const char* path);
+
+int luat_crypto_md_init(const char* md, const char* key, luat_crypt_stream_t *stream);
+int luat_crypto_md_update(const char* str, size_t str_size, luat_crypt_stream_t *stream);
+int luat_crypto_md_finish(void* out_ptr, luat_crypt_stream_t *stream);
+
+typedef struct luat_crypto_cipher_ctx
+{
+    const char* cipher;
+    const char* pad;
+    const char* str;
+    const char* key;
+    const char* iv;
+    
+    size_t cipher_size;
+    size_t pad_size;
+    size_t str_size;
+    size_t key_size;
+    size_t iv_size;
+    char* outbuff;
+    size_t outlen;
+    uint8_t flags;
+}luat_crypto_cipher_ctx_t;
+
+int luat_crypto_cipher_xxx(luat_crypto_cipher_ctx_t* cctx);
+
+#endif

+ 47 - 41
luat/include/luat_fota.h

@@ -1,41 +1,47 @@
-#ifndef LUAT_FOTA_H
-#define LUAT_FOTA_H
-
-#include "luat_base.h"
-#include "luat_spi.h"
-/**
- * @defgroup luatos_fota 远程升级接口
- * @{
- */
-
-/**
- * @brief 用于初始化fota,创建写入升级包数据的上下文结构体
- * 
- * @param start_address,开始地址 718/716系列填0
- * @param  len 长度 718/716系列填0
- * @param  spi_device 长度 718/716系列填NULL
- * @param  path 长度 718/716系列填NULL
- * @param  pathlen 长度 718/716系列填0
- * @return  
- */
-
-int luat_fota_init(uint32_t start_address, uint32_t len, luat_spi_device_t* spi_device, const char *path, uint32_t pathlen);
-
-/// @brief 用于向本地 Flash 中写入升级包数据
-/// @param data 升级包数据
-/// @param len 升级包数据长度
-/// @return int =0成功,其他失败
-int luat_fota_write(uint8_t *data, uint32_t len);
-
-/// @brief 用于结束升级包下载
-/// @return int =0成功,其他失败
-int luat_fota_done(void);
-
-int luat_fota_end(uint8_t is_ok);
-
-/// @brief 等待fota 准备,目前没有什么作用
-/// @param  
-/// @return uint8_t =1 准备好
-uint8_t luat_fota_wait_ready(void);
-/** @}*/
-#endif
+#ifndef LUAT_FOTA_H
+#define LUAT_FOTA_H
+
+#include "luat_base.h"
+#include "luat_spi.h"
+/**
+ * @defgroup luatos_fota 远程升级接口
+ * @{
+ */
+
+/**
+ * @brief 用于初始化fota,创建写入升级包数据的上下文结构体
+ * 
+ * @param start_address,开始地址 718/716系列填0
+ * @param  len 长度 718/716系列填0
+ * @param  spi_device 长度 718/716系列填NULL
+ * @param  path 长度 718/716系列填NULL
+ * @param  pathlen 长度 718/716系列填0
+ * @return  
+ */
+
+int luat_fota_init(uint32_t start_address, uint32_t len, luat_spi_device_t* spi_device, const char *path, uint32_t pathlen);
+
+/// @brief 用于向本地 Flash 中写入升级包数据
+/// @param data 升级包数据
+/// @param len 升级包数据长度
+/// @return int =0成功,其他失败
+int luat_fota_write(uint8_t *data, uint32_t len);
+
+/// @brief 用于结束升级包下载
+/// @return int =0成功,其他失败
+int luat_fota_done(void);
+
+/**
+ * @brief 结束fota流程
+ * 
+ * @param is_ok 是否结束流程
+ * @return int =0成功,其他失败
+ */
+int luat_fota_end(uint8_t is_ok);
+
+/// @brief 等待fota 准备,目前没有什么作用
+/// @param  
+/// @return uint8_t =1 准备好
+uint8_t luat_fota_wait_ready(void);
+/** @}*/
+#endif

+ 32 - 1
luat/include/luat_fs.h

@@ -36,9 +36,20 @@ typedef struct luat_fs_info
  * @return int =0成功,其他失败
  */
 int luat_fs_init(void);
-
+/**
+ * @brief 文件系统格式化
+ * @return int =0成功,其他失败
+ */
 int luat_fs_mkfs(luat_fs_conf_t *conf);
+/**
+ * @brief 文件系统挂载
+ * @return int =0成功,其他失败
+ */
 int luat_fs_mount(luat_fs_conf_t *conf);
+/**
+ * @brief 文件系统取消挂载
+ * @return int =0成功,其他失败
+ */
 int luat_fs_umount(luat_fs_conf_t *conf);
 
 /**
@@ -150,7 +161,20 @@ size_t luat_fs_fsize(const char *filename);
  * @return int =0不存在,否则存在
  */
 int luat_fs_fexist(const char *filename);
+/**
+ * @brief 行读取
+ * @param buf[OUT] 读取缓冲区
+ * @param bufsize[IN] 缓冲区大小
+ * @param stream[IN] 文件句柄
+ * @return int >=0实际写入的数量,<0出错
+ */
 int luat_fs_readline(char * buf, int bufsize, FILE * stream);
+
+/**
+ * @brief 获取文件映射地址
+ * @param stream[IN] 文件句柄
+ * @return void* 文件地址
+ */
 void* luat_fs_mmap(FILE * stream);
 
 // TODO 文件夹相关的API
@@ -188,6 +212,13 @@ int luat_fs_rmdir(char const* _DirName);
 
 int luat_fs_lsdir(char const* _DirName, luat_fs_dirent_t* ents, size_t offset, size_t len);
 
+
+/**
+ * @brief 文件截断
+ * @param filename[IN] 文件名
+ * @param len[IN] 长度
+ * @return int =>0读取到文件个数,否则失败
+ */
 int luat_fs_truncate(const char* filename, size_t len);
 /**
  * @brief 文件夹是否存在

+ 137 - 103
luat/include/luat_i2c.h

@@ -1,103 +1,137 @@
-#ifndef LUAT_I2C_H
-#define LUAT_I2C_H
-
-#include "luat_base.h"
-/**
- * @defgroup luatos_device_i2c I2C接口
- * @{
-*/
-
-/**
- * @brief 检查i2c是否存在
- * 
- * @param id i2c_id
- * @return 1存在 0不存在
- */
-int luat_i2c_exist(int id);
-
-/**
- * @brief 初始化i2c
- * 
- * @param id i2c_id
- * @param speed i2c 速度
- * @return 0成功 其他失败
- */
-int luat_i2c_setup(int id, int speed);
-
-/**
- * @brief 关闭 i2c
- * 
- * @param id i2c_id
- * @return 0成功 其他失败
- */
-
-int luat_i2c_close(int id);
-/**
- * @brief I2C 发送数据
- * 
- * @param id i2c_id
- * @param addr 7位设备地址
- * @param buff 数据buff
- * @param len 数据长度
- * @param stop 是否发送停止位
- * @return 0成功 其他失败
- */
-
-int luat_i2c_send(int id, int addr, void* buff, size_t len, uint8_t stop);
-
-/**
- * @brief I2C 接受数据
- * 
- * @param id i2c_id
- * @param addr 7位设备地址
- * @param buff 数据buff
- * @param len 数据长度
- * @return 0成功 其他失败
- */
-
-int luat_i2c_recv(int id, int addr, void* buff, size_t len);
-
-int luat_i2c_write_reg(int id, int addr, int reg, uint16_t value, uint8_t stop);
-int luat_i2c_read_reg(int id, int addr, int reg, uint16_t* value);
-/**
- * @brief I2C 收发数据
- * 
- * @param id i2c_id
- * @param addr 7位设备地址
- * @param reg 读的寄存器
- * @param reg_len 寄存器长度
- * @param buff 如果reg,reg_len 不为NULL,buff 为读的数据保存缓冲区,反之为写的缓冲区
- * @param len 数据长度
- * @return 0成功 其他失败
- */
-int luat_i2c_transfer(int id, int addr, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len);
-int luat_i2c_no_block_transfer(int id, int addr, uint8_t is_read, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len, uint16_t Toms, void *CB, void *pParam);
-
-/**
- * @brief 设置i2c复用
- * @attention 
- * @attention 
- * @param id i2c_id
- * @return -1 失败 其他正常
- */
-int luat_i2c_set_iomux(int id, uint8_t value);
-
-int luat_i2c_set_polling_mode(int id, uint8_t on_off);
-/** @}*/
-
-#define LUAT_EI2C_TYPE "EI2C*"
-
-typedef struct luat_ei2c {
-    int8_t sda;
-    int8_t scl;
-    int16_t udelay;
-} luat_ei2c_t;//软件i2c
-
-void i2c_soft_start(luat_ei2c_t *ei2c);
-char i2c_soft_recv(luat_ei2c_t *ei2c, unsigned char addr, char *buff, size_t len);
-char i2c_soft_send(luat_ei2c_t *ei2c, unsigned char addr, char *data, size_t len, uint8_t stop);
-
-
-#define toei2c(L) ((luat_ei2c_t *)luaL_checkudata(L, 1, LUAT_EI2C_TYPE))
-
-#endif
+#ifndef LUAT_I2C_H
+#define LUAT_I2C_H
+
+#include "luat_base.h"
+/**
+ * @defgroup luatos_device_i2c I2C接口
+ * @{
+*/
+
+/**
+ * @brief 检查i2c是否存在
+ * 
+ * @param id i2c_id
+ * @return 1存在 0不存在
+ */
+int luat_i2c_exist(int id);
+
+/**
+ * @brief 初始化i2c
+ * 
+ * @param id i2c_id
+ * @param speed i2c 速度
+ * @return 0成功 其他失败
+ */
+int luat_i2c_setup(int id, int speed);
+
+/**
+ * @brief 关闭 i2c
+ * 
+ * @param id i2c_id
+ * @return 0成功 其他失败
+ */
+
+int luat_i2c_close(int id);
+/**
+ * @brief I2C 发送数据
+ * 
+ * @param id i2c_id
+ * @param addr 7位设备地址
+ * @param buff 数据buff
+ * @param len 数据长度
+ * @param stop 是否发送停止位
+ * @return 0成功 其他失败
+ */
+
+int luat_i2c_send(int id, int addr, void* buff, size_t len, uint8_t stop);
+
+/**
+ * @brief I2C 接受数据
+ * 
+ * @param id i2c_id
+ * @param addr 7位设备地址
+ * @param buff 数据buff
+ * @param len 数据长度
+ * @return 0成功 其他失败
+ */
+int luat_i2c_recv(int id, int addr, void* buff, size_t len);
+
+/**
+ * @brief I2C 写寄存器
+ * 
+ * @param id i2c_id
+ * @param addr 7位设备地址
+ * @param reg 寄存器地址
+ * @param value 数据
+ * @param stop 是否发送停止位
+ * @return 0成功 其他失败
+ */
+int luat_i2c_write_reg(int id, int addr, int reg, uint16_t value, uint8_t stop);
+/**
+ * @brief I2C 读寄存器
+ * 
+ * @param id i2c_id
+ * @param addr 7位设备地址
+ * @param reg 寄存器地址
+ * @param value 数据
+ * @return 0成功 其他失败
+ */
+int luat_i2c_read_reg(int id, int addr, int reg, uint16_t* value);
+/**
+ * @brief I2C 收发数据
+ * 
+ * @param id i2c_id
+ * @param addr 7位设备地址
+ * @param reg 读的寄存器
+ * @param reg_len 寄存器长度
+ * @param buff 如果reg,reg_len 不为NULL,buff 为读的数据保存缓冲区,反之为写的缓冲区
+ * @param len 数据长度
+ * @return 0成功 其他失败
+ */
+int luat_i2c_transfer(int id, int addr, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len);
+
+/**
+ * @brief I2C 收发数据(异步)
+ * 
+ * @param id i2c_id
+ * @param addr 7位设备地址
+ * @param is_read 是否为读写
+ * @param reg 读的寄存器
+ * @param reg_len 寄存器长度
+ * @param buff 如果reg,reg_len 不为NULL,buff 为读的数据保存缓冲区,反之为写的缓冲区
+ * @param len 数据长度
+ * @param Toms 数据长度
+ * @param CB 回调函数
+ * @param pParam 回调函数参数
+ * @return 0成功 其他失败
+ */
+int luat_i2c_no_block_transfer(int id, int addr, uint8_t is_read, uint8_t *reg, size_t reg_len, uint8_t *buff, size_t len, uint16_t Toms, void *CB, void *pParam);
+
+/**
+ * @brief 设置i2c复用
+ * @attention 
+ * @attention 
+ * @param id i2c_id
+ * @return -1 失败 其他正常
+ */
+int luat_i2c_set_iomux(int id, uint8_t value);
+
+int luat_i2c_set_polling_mode(int id, uint8_t on_off);
+/** @}*/
+
+#define LUAT_EI2C_TYPE "EI2C*"
+
+typedef struct luat_ei2c {
+    int8_t sda;
+    int8_t scl;
+    int16_t udelay;
+} luat_ei2c_t;//软件i2c
+
+void i2c_soft_start(luat_ei2c_t *ei2c);
+char i2c_soft_recv(luat_ei2c_t *ei2c, unsigned char addr, char *buff, size_t len);
+char i2c_soft_send(luat_ei2c_t *ei2c, unsigned char addr, char *data, size_t len, uint8_t stop);
+
+
+#define toei2c(L) ((luat_ei2c_t *)luaL_checkudata(L, 1, LUAT_EI2C_TYPE))
+
+#endif

+ 2 - 2
luat/include/luat_pm.h

@@ -174,8 +174,8 @@ int luat_pm_iovolt_ctrl(int id, int val);
 
 /**
  * @brief 配置唤醒引脚,只针对esp系列
- * @param pin
- * @param val
+ * @param pin 引脚
+ * @param val 电平
  * @return
  */
 int luat_pm_wakeup_pin(int pin, int val);

+ 6 - 0
luat/include/luat_rtos.h

@@ -485,6 +485,12 @@ uint32_t luat_rtos_entry_critical(void);
  */
 void luat_rtos_exit_critical(uint32_t critical);
 
+/**
+ * @brief 获取是否为中断
+ * 
+ * @param critical 进入临界保护时返回的参数
+ * @return uint32_t 0不是中断,1是中断
+ */
 uint32_t luat_rtos_get_ipsr(void);
 /*------------------------------------------------ critical   end----------------------------------------------- */
 /** @}*/

+ 198 - 120
luat/include/luat_spi.h

@@ -1,120 +1,198 @@
-
-#ifndef LUAT_SPI_H
-#define LUAT_SPI_H
-#include "luat_base.h"
-/**
- * @defgroup luatos_device_spi SPI接口
- * @{
-*/
-typedef struct luat_spi
-{
-    int  id;            /**< spi id        可选  1,0*/
-    int  CPHA;          /**< CPHA          可选  1,0*/  
-    int  CPOL;          /**< CPOL          可选  1,0*/  
-    int  dataw;         /**< 数据宽度        8:8bit */
-    int  bit_dict;      /**< 高低位顺序     可选  1:MSB,   0:LSB     */  
-    int  master;        /**< 设置主从模式   可选  1:主机,  0:从机     */  
-    int  mode;          /**< 设置全\半双工  可选  1:全双工,0:半双工    */  
-    int bandrate;       /**< 频率           最小100000, 最大25600000*/  
-    int cs;             /**< cs控制引脚     SPI0的片选为GPIO8, 当配置为8时,表示启用SPI0自带片选;其他配置时,需要自行编码控制片选*/  
-} luat_spi_t;
-
-typedef struct luat_spi_device
-{
-    uint8_t  bus_id;
-    luat_spi_t spi_config;
-    void* user_data;
-} luat_spi_device_t;
-
-typedef struct luat_fatfs_spi
-{
-	uint8_t type;
-	uint8_t spi_id;
-	uint8_t spi_cs;
-	uint8_t nop;
-	uint32_t fast_speed;
-    uint8_t transfer_buf[7];
-	luat_spi_device_t * spi_device;
-}luat_fatfs_spi_t;
-
-/**
-    spiId,--串口id
-    cs,
-    0,--CPHA
-    0,--CPOL
-    8,--数据宽度
-    20000000,--最大频率20M
-    spi.MSB,--高低位顺序    可选,默认高位在前
-    spi.master,--主模式     可选,默认主
-    spi.full,--全双工       可选,默认全双工
-*/
-/**
- * @brief 初始化配置SPI各项参数,并打开SPI
- * 
- * @param spi spi结构体
- * @return int 成功返回0
- */
-int luat_spi_setup(luat_spi_t* spi);
-//收发SPI数据尝试启动DMA模式
-int luat_spi_config_dma(int spi_id, uint32_t tx_channel, uint32_t rx_channel);
-/**
- * @brief 关闭SPI
- * 
- * @param spi_id spi id
- * @return int 成功返回0
- */
-int luat_spi_close(int spi_id);
-/**
- * @brief 收发SPI数据
- * 
- * @param spi_id spi id
- * @param send_buf 发送数据
- * @param send_length 发送数据长度
- * @param recv_buf 接收数据
- * @param recv_length 接收数据长度
- * @return int 返回接收字节数
- */
-int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length);
-/**
- * @brief 收SPI数据
- * 
- * @param spi_id spi id
- * @param recv_buf 接收数据
- * @param length 数据长度
- * @return int 返回接收字节数
- */
-int luat_spi_recv(int spi_id, char* recv_buf, size_t length);
-/**
- * @brief 发SPI数据
- * 
- * @param spi_id spi id
- * @param send_buf 发送数据
- * @param length 数据长度
- * @return int 返回发送字节数
- */
-int luat_spi_send(int spi_id, const char* send_buf, size_t length);
-
-int luat_spi_change_speed(int spi_id, uint32_t speed);
-//非阻塞SPI收发数据
-int luat_spi_no_block_transfer(int spi_id, uint8_t *tx_buff, uint8_t *rx_buff, size_t len, void *CB, void *pParam);
-
-// 初始化总线
-int luat_spi_bus_setup(luat_spi_device_t* spi_dev);
-// 初始化设备
-int luat_spi_device_setup(luat_spi_device_t* spi_dev);
-// 配置设备
-int luat_spi_device_config(luat_spi_device_t* spi_dev);
-//关闭SPI设备,成功返回0
-int luat_spi_device_close(luat_spi_device_t* spi_dev);
-//收发SPI数据,返回接收字节数
-int luat_spi_device_transfer(luat_spi_device_t* spi_dev, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length);
-//收SPI数据,返回接收字节数
-int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t length);
-//发SPI数据,返回发送字节数
-int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length);
-
-int luat_spi_get_mode(int spi_id);
-
-int luat_spi_set_mode(int spi_id, uint8_t mode);
-/**@}*/
-#endif
+
+#ifndef LUAT_SPI_H
+#define LUAT_SPI_H
+#include "luat_base.h"
+/**
+ * @defgroup luatos_device_spi SPI接口
+ * @{
+*/
+typedef struct luat_spi
+{
+    int  id;            /**< spi id        可选  1,0*/
+    int  CPHA;          /**< CPHA          可选  1,0*/  
+    int  CPOL;          /**< CPOL          可选  1,0*/  
+    int  dataw;         /**< 数据宽度        8:8bit */
+    int  bit_dict;      /**< 高低位顺序     可选  1:MSB,   0:LSB     */  
+    int  master;        /**< 设置主从模式   可选  1:主机,  0:从机     */  
+    int  mode;          /**< 设置全\半双工  可选  1:全双工,0:半双工    */  
+    int bandrate;       /**< 频率           最小100000, 最大25600000*/  
+    int cs;             /**< cs控制引脚     SPI0的片选为GPIO8, 当配置为8时,表示启用SPI0自带片选;其他配置时,需要自行编码控制片选*/  
+} luat_spi_t;
+
+typedef struct luat_spi_device
+{
+    uint8_t  bus_id;
+    luat_spi_t spi_config;
+    void* user_data;
+} luat_spi_device_t;
+
+typedef struct luat_fatfs_spi
+{
+	uint8_t type;
+	uint8_t spi_id;
+	uint8_t spi_cs;
+	uint8_t nop;
+	uint32_t fast_speed;
+    uint8_t transfer_buf[7];
+	luat_spi_device_t * spi_device;
+}luat_fatfs_spi_t;
+
+/**
+    spiId,--串口id
+    cs,
+    0,--CPHA
+    0,--CPOL
+    8,--数据宽度
+    20000000,--最大频率20M
+    spi.MSB,--高低位顺序    可选,默认高位在前
+    spi.master,--主模式     可选,默认主
+    spi.full,--全双工       可选,默认全双工
+*/
+/**
+ * @brief 初始化配置SPI各项参数,并打开SPI
+ * 
+ * @param spi spi结构体
+ * @return int 成功返回0
+ */
+int luat_spi_setup(luat_spi_t* spi);
+/**
+ * @brief SPI收发数据尝试启动DMA模式
+ * 
+ * @param spi_id spi id
+ * @param tx_channel 发送通道
+ * @param rx_channel 接收通道
+ * @return int
+ */
+int luat_spi_config_dma(int spi_id, uint32_t tx_channel, uint32_t rx_channel);
+/**
+ * @brief 关闭SPI
+ * 
+ * @param spi_id spi id
+ * @return int 成功返回0
+ */
+int luat_spi_close(int spi_id);
+/**
+ * @brief 收发SPI数据
+ * 
+ * @param spi_id spi id
+ * @param send_buf 发送数据
+ * @param send_length 发送数据长度
+ * @param recv_buf 接收数据
+ * @param recv_length 接收数据长度
+ * @return int 返回接收字节数
+ */
+int luat_spi_transfer(int spi_id, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length);
+/**
+ * @brief 收SPI数据
+ * 
+ * @param spi_id spi id
+ * @param recv_buf 接收数据
+ * @param length 数据长度
+ * @return int 返回接收字节数
+ */
+int luat_spi_recv(int spi_id, char* recv_buf, size_t length);
+/**
+ * @brief 发SPI数据
+ * 
+ * @param spi_id spi id
+ * @param send_buf 发送数据
+ * @param length 数据长度
+ * @return int 返回发送字节数
+ */
+int luat_spi_send(int spi_id, const char* send_buf, size_t length);
+/**
+ * @brief SPI速率修改
+ * 
+ * @param spi_id spi id
+ * @param speed 速率
+ * @return int 返回发送字节数
+ */
+int luat_spi_change_speed(int spi_id, uint32_t speed);
+/**
+ * @brief SPI收发数据(异步)
+ * 
+ * @param spi_id spi id
+ * @param tx_buff 发送数据
+ * @param rx_buff 接收数据
+ * @param len 数据长度
+ * @param CB 回调函数
+ * @param pParam 回调参数
+ * @return int 返回发送字节数
+ */
+int luat_spi_no_block_transfer(int spi_id, uint8_t *tx_buff, uint8_t *rx_buff, size_t len, void *CB, void *pParam);
+/**
+ * @brief SPI模式获取
+ * 
+ * @param spi_id spi id
+ * @return int 模式
+ */
+int luat_spi_get_mode(int spi_id);
+/**
+ * @brief SPI模式修改
+ * 
+ * @param spi_id spi id
+ * @param mode 模式
+ * @return int 返回发送字节数
+ */
+int luat_spi_set_mode(int spi_id, uint8_t mode);
+
+/**
+ * @brief spi总线初始化
+ * 
+ * @param spi_dev luat_spi_device_t 结构体
+ * @return int 
+ */
+int luat_spi_bus_setup(luat_spi_device_t* spi_dev);
+/**
+ * @brief spi设备初始化
+ * 
+ * @param spi_dev luat_spi_device_t 结构体
+ * @return int 
+ */
+int luat_spi_device_setup(luat_spi_device_t* spi_dev);
+/**
+ * @brief spi设备配置
+ * 
+ * @param spi_dev luat_spi_device_t 结构体
+ * @return int 
+ */
+int luat_spi_device_config(luat_spi_device_t* spi_dev);
+/**
+ * @brief spi设备关闭
+ * 
+ * @param spi_dev luat_spi_device_t 结构体
+ * @return int 
+ */
+int luat_spi_device_close(luat_spi_device_t* spi_dev);
+/**
+ * @brief spi设备收发数据,返回接收字节数
+ * 
+ * @param spi_dev luat_spi_device_t 结构体
+ * @param send_buf 发送数据
+ * @param send_length 发送数据长度
+ * @param recv_buf 接收数据
+ * @param recv_length 接收数据长度
+ * @return int 
+ */
+int luat_spi_device_transfer(luat_spi_device_t* spi_dev, const char* send_buf, size_t send_length, char* recv_buf, size_t recv_length);
+/**
+ * @brief spi设备接收数据,返回接收字节数
+ * 
+ * @param spi_dev luat_spi_device_t 结构体
+ * @param recv_buf 接收数据
+ * @param length 数据长度
+ * @return int 返回接收字节数
+ */
+int luat_spi_device_recv(luat_spi_device_t* spi_dev, char* recv_buf, size_t length);
+/**
+ * @brief spi设备发送数据,返回接收字节数
+ * 
+ * @param spi_dev luat_spi_device_t 结构体
+ * @param send_buf 发送数据
+ * @param length 数据长度
+ * @return int 返回发送字节数
+ */
+int luat_spi_device_send(luat_spi_device_t* spi_dev, const char* send_buf, size_t length);
+
+/**@}*/
+#endif

+ 0 - 385
script/libs/aliyun2.lua

@@ -1,385 +0,0 @@
---[[
-@module aliyun2
-@summary 阿里云物联网平台(开发中)
-@version 1.0
-@date    2024.05.18
-@author  wendal
-@demo    aliyun2
-@usage
--- 请查阅demo
-
--- 本库基于阿里云物联网重新设计, 与aliyun.lua库不兼容
--- 本库尚属开发测试阶段, API随时可能变化, 也可能不变^_^
-]]
-_G.sys = require("sys")
-
-local aliyun2 = {}
-local g_id = 1
-
---[[
-初始化一个aliyun示例
-@api aliyun.create(opts)
-@table 参数表
-@return aliyun实例
-@usage
--- 初始化一个aliyun示例
-local ali = aliyun.create(opts)
-if ali and aliyun2.start(ali) then
-    while 1 do
-        local result, tip, params = sys.waitUntil(ali.topic, 30000)
-        if result then
-            log.info("aliyun", "event", tip, params)
-        end
-    end
-else
-    log.error("aliyun", "初始化失败")
-end
-]]
-function aliyun2.create(opts)
-    if not opts then
-        log.error("aliyun2", "配置参数表不能是空")
-        return
-    end
-    -- 检查最基本的参数
-    if not opts.productKey or not opts.deviceName then
-        log.error("aliyun2", "缺失配置参数productKey")
-        return
-    end
-    if not opts.deviceName then
-        if mobile then
-            opts.deviceName = mobile.imei()
-        elseif wlan and wlan.getMac then
-            opts.deviceName = wlan.getMac()
-        else
-            opts.deviceName = mcu.unique_id():toHex()
-        end
-        log.info("aliyun2", "deviceName未指定,自动分配", opts.deviceName)
-    end
-    if opts.productSecret and #opts.productSecret == 0 then
-        opts.productSecret = nil
-    end
-    if opts.deviceSecret and #opts.deviceSecret == 0 then
-        opts.deviceSecret = nil
-    end
-    if not opts.store_key then
-        opts.store_key = opts.productKey .. "_" .. opts.deviceName
-    end
-    if opts.productSecret and not opts.deviceSecret then
-        -- 从本地存储读取设备密钥
-        local payload = nil
-        if fskv then
-            payload = fskv.get(opts.store_key)
-        end
-        if not payload or #payload < 16 then
-            payload = io.readFile(opts.store_key)
-        end
-        if payload then
-            local jdata = json.decode(payload)
-            if jdata then
-                opts.deviceSecret = jdata["deviceSecret"]
-                opts.deviceToken = jdata["deviceToken"]
-                opts.clientId = jdata["clientId"]
-            end
-        end
-    end
-
-    if not opts.productSecret and not opts.deviceSecret and not opts.deviceToken then
-        log.error("aliyun2", "请指定productSecret或deviceSecret")
-        return
-    end
-    local ctx = opts
-    -- 计算mqtt的host和port
-    if not ctx.mqtt_host then
-        -- ctx.mqtt_host = "iot-as-mqtt." .. ctx.productKey .. ".aliyuncs.com"
-        if ctx.instanceId then
-            ctx.mqtt_host = ctx.instanceId .. ".mqtt.iothub.aliyuncs.com"
-        else
-            ctx.mqtt_host = ctx.productKey .. ".iot-as-mqtt." .. (opts.regionId or "cn-shanghai") ..".aliyuncs.com"
-        end
-    end
-    if not ctx.mqtt_port then
-        ctx.mqtt_port = 1883
-    end
-    ctx.device_retry = 0
-    ctx.topic = "aliyun2_" .. g_id
-    g_id = g_id + 1
-
-    -- 生成mqtt的topic
-    if ctx.auto_topic == nil or ctx.auto_topic then
-        if not ctx.sub_topics then
-            ctx.sub_topics = {}
-        end
-        if not ctx.pub_topics then
-            ctx.pub_topics = {}
-        end
-        local topics = ctx.sub_topics
-        -- 订阅必要的topic
-        local dn = ctx.productKey .. "/" .. ctx.deviceName
-        -- 首先是OTA的topic
-        topics.ota = "/ota/device/upgrade/" .. dn
-        -- 配置更新信息
-        topics.config_push = "/sys/" .. dn .. "/thing/config/push"
-        -- 广播信息
-        topics.broadcast = "/broadcast/" .. ctx.productKey .. "/#"
-        -- NTP信息
-        topics.ntp = "/ext/ntp/" .. dn .. "/response"
-
-        -- 物模型, 透传信息
-        topics.raw_reply = "/sys/" .. dn .. "/thing/model/up_raw_reply"
-        topics.raw_down = "/sys/" .. dn .. "/thing/model/down_raw"
-        -- 非透传
-        topics.property_set = "/sys/".. dn .. "/thing/service/property/set"
-
-        -- 上行常用的topic
-        topics = ctx.pub_topics
-        topics.inform = "/ota/device/inform/".. dn
-        topics.ntp = "/ext/ntp/".. dn .. "/request"
-        topics.raw_up = "/sys/".. dn .. "/thing/model/up_raw"
-    end
-
-    if ctx.instanceId then
-        log.info("aliyun2", "instanceId", ctx.instanceId)
-    end
-    log.info("aliyun2", "deviceName", ctx.deviceName)
-    log.info("aliyun2", "mqtt_host", ctx.mqtt_host)
-    log.info("aliyun2", "mqtt_port", ctx.mqtt_port)
-    return ctx
-end
-
-local function aliyun_do_reg(ctx)
-    local mqttc = mqtt.create(ctx.adapter, ctx.mqtt_host, 443, true)
-    if not mqttc then
-        log.error("aliyun2", "创建mqtt实例失败")
-        return
-    end
-    -- 计算自动注册所需要的密钥
-    local tm = tostring(os.time())
-    local client_id = string.format("%s|securemode=%s,authType=%s,random=%s,signmethod=hmacsha1%s|", ctx.deviceName, ctx.regnwl and -2 or 2, ctx.regnwl and "regnwl" or "register", tm, (ctx.instanceId and ",instanceId=" .. ctx.instanceId or ""))
-    log.info("aliyun2", "开始注册流程", client_id)
-    local user_name = ctx.deviceName .. "&"..ctx.productKey
-    local content = "deviceName"..ctx.deviceName.."productKey"..ctx.productKey.."random" .. tm
-    local password = crypto.hmac_sha1(content, ctx.productSecret)
-    log.info("aliyun2", "尝试注册", client_id, user_name, password)
-    mqttc:auth(client_id, user_name, password)
-    mqttc:keepalive(240) -- 实际上会忽略该属性
-    -- mqttc:debug(true)
-    mqttc:autoreconn(false, 3000) -- 不需要自动重连
-    local regok = false -- 记录注册成功与否
-    mqttc:on(function(mqtt_client, event, data, payload)
-        log.info("aliyun2", "event", event, data, payload)
-        if event == "recv" then
-            log.info("aliyun", "downlink", "topic", data, "payload", payload)
-            if payload then
-                local jdata,res = json.decode(payload) -- TODO 搞个alijson库进行封装
-                if jdata and (jdata["deviceSecret"] or jdata["deviceToken"]) then
-                    log.info("aliyun2", "获取到设备密钥")
-                    regok = true
-                    ctx.deviceSecret = jdata["deviceSecret"]
-                    ctx.deviceToken = jdata["deviceToken"]
-                    ctx.clientId = jdata["clientId"]
-                    sys.publish(tm, "reg", "ok")
-                    if fskv then
-                        log.info("aliyun2", "密钥信息存入fskv", ctx.store_key)
-                        fskv.set(ctx.store_key, payload)
-                    end
-                    log.info("aliyun2", "密钥信息存入文件系统", ctx.store_key)
-                    io.writeFile(ctx.store_key, payload)
-                else
-                    sys.publish(tm, "reg", "fail")
-                    return
-                end
-            end
-        elseif event == "close" then
-            sys.publish(tm, "close")
-        end
-    end)
-    mqttc:connect()
-    sys.waitUntil(tm, 5000)
-    sys.wait(100)
-    mqttc:close()
-    if regok then
-        log.info("aliyun2", "自动注册成功,密钥信息已获取")
-    else
-        log.info("alyun2", "自动注册失败,延迟30秒后重试")
-        sys.wait(30000)
-    end
-end
-
-local function aliyun_task_once(ctx)
-    -- 几个条件: 有设备密钥, 有产品密钥, 登陆失败的次数
-    -- 情况1: 只有设备密钥, 没有产品密钥, 那就固定是一机一密
-    -- 情况2: 只有产品密钥, 没有设备密钥, 那就是一机一密
-    -- 情况3: 有产品密钥, 有设备密钥, 登录失败次数少于设定值(默认3次),那继续用一机一密去尝试登陆
-    -- 情况4: 有产品密钥, 有设备密钥, 登录失败次数大于设定值(默认3次), 使用一型一密去尝试注册一次
-
-    if ctx.productSecret then
-        if ctx.deviceSecret or ctx.deviceToken then
-            if ctx.device_retry < 3 then
-                -- 失败次数还不多,先尝试用一机一密
-            else
-                log.info("aliyun2", "设备密钥已存在,但已经连续失败3次,尝试重新注册")
-                aliyun_do_reg(ctx)
-                ctx.device_retry = 0
-            end
-        else
-            aliyun_do_reg(ctx)
-        end
-    end
-
-    if not ctx.deviceSecret and not ctx.deviceToken then
-        log.info("aliyun2", "未能获取到设备密钥,等待重试")
-        return
-    end
-
-    local mqttc = mqtt.create(ctx.adapter, ctx.mqtt_host, ctx.mqtt_port, ctx.mqtt_isssl, ctx.ca_file)
-    if not mqttc then
-        log.error("aliyun2", "创建mqtt实例失败")
-        return
-    end
-    if ctx.deviceSecret then
-        local client_id,user_name, password = iotauth.aliyun(ctx.productKey, ctx.deviceName, ctx.deviceSecret)
-        log.info("aliyun2", "密钥模式", client_id,user_name, password)
-        mqttc:auth(client_id, user_name, password)
-    else
-        local client_id = ctx.clientId .. "|securemode=-2,authType=connwl|"
-        local user_name = ctx.deviceName.."&"..ctx.productKey
-        log.info("aliyun2", "token模式", client_id, user_name, ctx.deviceToken)
-        mqttc:auth(client_id, user_name, ctx.deviceToken)
-    end
-    mqttc:keepalive(ctx.mqtt_keepalive or 240)
-    -- mqttc:debug(true)
-    mqttc:autoreconn(false, 3000) -- 不需要自动重连
-    mqttc:on(function(mqtt_client, event, data, payload)
-        log.info("aliyun2", "event", event, data, payload)
-        if event == "conack" then
-            log.info("aliyun2", "连接成功,鉴权完成")
-            ctx.device_retry = 0
-
-            -- 需要订阅的topic
-            if ctx.sub_topics then
-                for k, v in pairs(ctx.sub_topics) do
-                    log.info("aliyun2", "订阅topic", v, "别名", k)
-                    mqttc:subscribe(v)
-                end
-            end
-
-            -- 上报一些基础信息
-            -- 版本信息
-            if ctx.pub_topics and ctx.pub_topics.inform then
-                local info = ctx.inform_data or {version=_G.VERSION, module=rtos.bsp():upper()}
-                info = json.encode({id="123", params=info})
-                log.info("aliyun2", "上报版本信息", ctx.pub_topics.inform, info)
-                mqttc:publish(ctx.pub_topics.inform, info, 1)
-            end
-            
-            sys.publish(ctx.topic, "conack")
-        elseif event == "recv" then
-            -- 收到消息
-            log.info("aliyun2", "收到下行信息", "topic", data, "payload前128字节", payload and payload:sub(1, 128))
-            sys.publish(ctx.topic, "recv", data, payload)
-            -- TODO 支持FOTA/OTA
-            if ctx.sub_topics and ctx.sub_topics.ota == data then
-                log.info("aliyun2", "收到ota信息", payload)
-                local jdata = json.decode(payload)
-                if jdata and jdata.data and jdata.data.url then
-                    log.info("aliyun2", "获取到OTA所需要的URL", jdata.data.url)
-                    sys.publish(ctx.topic, "ota", jdata.data)
-                end
-            end
-        end
-    end)
-    mqttc:connect()
-    sys.waitUntil(ctx.topic, 5000)
-    if mqttc:ready() then
-        ctx.mqttc = mqttc
-        ctx.device_retry = 0
-        while mqttc:ready() and ctx.running do
-            sys.waitUntil(ctx.topic, 5000)
-        end
-    else
-        ctx.device_retry = ctx.device_retry + 1
-    end
-    mqttc:close()
-    log.info("aliyun2", "单次任务结束")
-    ctx.mqttc = nil
-end
-
-local function aliyun_task (ctx)
-    -- 准备好参数
-
-    -- 开始主循环
-    while ctx.running do
-        aliyun_task_once(ctx)
-        sys.wait(2000)
-    end
-    log.info("aliyun2", "阿里云任务结束")
-end
-
---[[
-启动aliyun实例
-@api aliyun2.start(ali)
-@return 成功返回true,失败返回false
-]]
-function aliyun2.start(ali)
-    ali.running = true
-    ali.c_task = sys.taskInit(aliyun_task, ali)
-    return ali.c_task ~= nil
-end
-
---[[
-关闭aliyun实例
-@api aliyun2.stop(ali)
-@return boolean 成功返回true,失败返回false
-]]
-function aliyun2.stop(ali)
-    if ali.c_task then
-        ali.running = nil
-        sys.publish(ali.topic, "stop")
-    end
-end
-
---[[
-是否已经连接上
-@api aliyun2.ready(ali)
-@return boolean 已经成功返回true,否则一概返回false
-]]
-function aliyun2.ready(ali)
-    if ali and ali.mqttc and ali.mqttc:ready() then
-        return true
-    end
-end
-
---[[
-订阅自定义的topic
-@api aliyun2.subscribe(ali, topic)
-@return 成功返回true,失败返回false或者nil
-]]
-function aliyun2.subscribe(ali, topic, qos)
-    if not aliyun2.ready(ali) then
-        log.warn("aliyun2", "还没连上服务器,不能订阅", topic)
-        return
-    end
-    ali.mqttc:subscribe(topic, qos or 1)
-    return true
-end
-
---[[
-上报消息,上行数据
-@api aliyun2.publish(ali, topic, payload, qos, retain)
-@return 成功返回true,失败返回false或者nil
-]]
-function aliyun2.publish(ali, topic, payload, qos, retain)
-    if not aliyun2.ready(ali) then
-        log.warn("aliyun2", "还没连上服务器,不能上报", topic)
-        return
-    end
-    ali.mqttc:public(topic, payload, qos or 1, retain or 0)
-    return true
-end
-
--- TODO
--- 1. alijson库
--- 2. 对物模型做一些封装
--- 3. 对透传进行一些封装
-
-return aliyun2