Skip to content

Commit

Permalink
Merge branch 'master' of https://gitee.com/openLuat/LuatOS
Browse files Browse the repository at this point in the history
  • Loading branch information
allewalker committed Sep 24, 2024
2 parents 36ff920 + 155a308 commit f7f193a
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 43 deletions.
107 changes: 105 additions & 2 deletions components/iotauth/luat_iotauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,109 @@ static void str_tohex(const char* str, size_t str_len, char* hex,uint8_t upperca
}
}

static const unsigned char base64_dec_map[128] =
{
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 127, 127, 127, 127, 127, 127, 127,
127, 127, 127, 62, 127, 127, 127, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 127, 127,
127, 64, 127, 127, 127, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 127, 127, 127, 127, 127, 127, 26, 27, 28,
29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 127, 127, 127, 127, 127
};

static int str_base64_decode( unsigned char *dst, size_t dlen, size_t *olen,
const unsigned char *src, size_t slen )
{
size_t i, n;
uint32_t j, x;
unsigned char *p;

/* First pass: check for validity and get output length */
for( i = n = j = 0; i < slen; i++ )
{
/* Skip spaces before checking for EOL */
x = 0;
while( i < slen && src[i] == ' ' )
{
++i;
++x;
}

/* Spaces at end of buffer are OK */
if( i == slen )
break;

if( ( slen - i ) >= 2 &&
src[i] == '\r' && src[i + 1] == '\n' )
continue;

if( src[i] == '\n' )
continue;

/* Space inside a line is an error */
if( x != 0 )
return( -2 );

if( src[i] == '=' && ++j > 2 )
return( -2 );

if( src[i] > 127 || base64_dec_map[src[i]] == 127 )
return( -2 );

if( base64_dec_map[src[i]] < 64 && j != 0 )
return( -2 );

n++;
}

if( n == 0 )
{
*olen = 0;
return( 0 );
}

/* The following expression is to calculate the following formula without
* risk of integer overflow in n:
* n = ( ( n * 6 ) + 7 ) >> 3;
*/
n = ( 6 * ( n >> 3 ) ) + ( ( 6 * ( n & 0x7 ) + 7 ) >> 3 );
n -= j;

if( dst == NULL || dlen < n )
{
*olen = n;
return( -1 );
}

for( j = 3, n = x = 0, p = dst; i > 0; i--, src++ )
{
if( *src == '\r' || *src == '\n' || *src == ' ' )
continue;

j -= ( base64_dec_map[*src] == 64 );
x = ( x << 6 ) | ( base64_dec_map[*src] & 0x3F );

if( ++n == 4 )
{
n = 0;
if( j > 0 ) *p++ = (unsigned char)( x >> 16 );
if( j > 1 ) *p++ = (unsigned char)( x >> 8 );
if( j > 2 ) *p++ = (unsigned char)( x );
}
}

*olen = p - dst;

return( 0 );
}

int luat_aliyun_token(iotauth_ctx_t* ctx,const char* product_key,const char* device_name,const char* device_secret,long long cur_timestamp,const char* method,uint8_t is_tls){
char deviceId[64] = {0};
char macSrc[200] = {0};
Expand Down Expand Up @@ -168,7 +271,7 @@ int luat_onenet_token(iotauth_ctx_t* ctx,const iotauth_onenet_t* onenet) {
sprintf_(sign.res,"products/%s/devices/%s", onenet->product_id, onenet->device_name);
}

luat_str_base64_decode((unsigned char *)plaintext, sizeof(plaintext), &declen, (const unsigned char * )onenet->device_secret, strlen((char*)onenet->device_secret));
str_base64_decode((unsigned char *)plaintext, sizeof(plaintext), &declen, (const unsigned char * )onenet->device_secret, strlen((char*)onenet->device_secret));
sprintf_(StringForSignature, "%s\n%s\n%s\n%s", sign.et, sign.method, sign.res, sign.version);
if (!strcmp("md5", onenet->method)||!strcmp("MD5", onenet->method)) {
luat_crypto_hmac_md5_simple(StringForSignature, strlen(StringForSignature), plaintext, declen, hmac);
Expand Down Expand Up @@ -224,7 +327,7 @@ int luat_qcloud_token(iotauth_ctx_t* ctx,const char* product_id,const char* devi
char username_sign[41] = {0};
char psk_base64decode[DECODE_PSK_LENGTH] = {0};
size_t psk_base64decode_len = 0;
luat_str_base64_decode((unsigned char *)psk_base64decode, DECODE_PSK_LENGTH, &psk_base64decode_len,(unsigned char *)device_secret, strlen(device_secret));
str_base64_decode((unsigned char *)psk_base64decode, DECODE_PSK_LENGTH, &psk_base64decode_len,(unsigned char *)device_secret, strlen(device_secret));
get_next_conn_id(conn_id);
snprintf_(ctx->client_id, CLIENT_ID_LEN,"%s%s", product_id,device_name);
snprintf_(ctx->user_name, USER_NAME_LEN,"%s%s;%s;%s;%lld", product_id, device_name, sdk_appid,conn_id, cur_timestamp);
Expand Down
35 changes: 25 additions & 10 deletions components/little_flash/src/little_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ static lf_err_t little_flash_write_enabled(const little_flash_t *lf, uint8_t ena
lf->spi.transfer(lf,enable?(uint8_t[]){LF_CMD_WRITE_ENABLE}:(uint8_t[]){LF_CMD_WRITE_DISABLE}, 1,LF_NULL,0);

result = little_flash_wait_busy(lf,1);
if (result) return result;
if (result) {
LF_ERROR("Error: Write enabled timeout.");
return result;
}

if (lf->chip_info.type==LF_DRIVER_NOR_FLASH){
result |= little_flash_read_status(lf, 0, &status);
Expand All @@ -132,7 +135,7 @@ lf_err_t little_flash_init(void){
}

#ifdef LF_USE_SFDP
lf_err_t little_flash_sfdp_read(const little_flash_t *lf, uint32_t offset, uint8_t *data, size_t size){
static inline lf_err_t little_flash_sfdp_read(const little_flash_t *lf, uint32_t offset, uint8_t *data, size_t size){
lf_err_t result = LF_ERR_OK;
uint8_t cmd_data[]={LF_CMD_SFDP_REGISTER,(uint8_t)(offset>>16),(uint8_t)(offset>>8),(uint8_t)(offset),0XFF};
result = lf->spi.transfer(lf,cmd_data, sizeof(cmd_data), data, size);
Expand Down Expand Up @@ -207,7 +210,7 @@ lf_err_t little_flash_sfdp_probe(little_flash_t *lf){
// [9] = 0x09 0x00 0x00 0x00

memcpy(&sfdp.pt, parameter_table, sfdp.parameter_length*4);
LF_DEBUG("sfdp.pt Flash_Memory_Density 0x%08X",sfdp.pt.Flash_Memory_Density);
// LF_DEBUG("sfdp.pt Flash_Memory_Density 0x%08X",sfdp.pt.Flash_Memory_Density);

if (sfdp.pt.Flash_Memory_Density & 0x80000000){
lf->chip_info.capacity = sfdp.pt.Flash_Memory_Density;
Expand Down Expand Up @@ -238,13 +241,15 @@ lf_err_t little_flash_sfdp_probe(little_flash_t *lf){
lf->chip_info.addr_bytes |= LF_ADDR_BYTES_4;
}

// LF_DEBUG("capacity: %d bytes",lf->chip_info.capacity);
// LF_DEBUG("erase_size: %d bytes",lf->chip_info.erase_size);
// LF_DEBUG("prog_size: %d bytes",lf->chip_info.prog_size);

// LF_DEBUG("capacity %d",lf->chip_info.capacity);
// LF_DEBUG("erase_cmd 0x%02X",lf->chip_info.erase_cmd);
// LF_DEBUG("erase_size %d",lf->chip_info.erase_size);
// LF_DEBUG("prog_size %d",lf->chip_info.prog_size);
// LF_DEBUG("addr_bytes 0x%02X",lf->chip_info.addr_bytes);

LF_DEBUG("Found a flash chip. Size is %d bytes.",lf->chip_info.capacity);

return result;
}
#endif /* LF_USE_SFDP */
Expand Down Expand Up @@ -305,8 +310,11 @@ lf_err_t little_flash_deinit(void){
static lf_err_t little_flash_cheak_erase(const little_flash_t *lf){
lf_err_t result = LF_ERR_OK;
uint8_t status;
result |= little_flash_wait_busy(lf,2000);
if (result) return result;
result |= little_flash_wait_busy(lf,4000);
if (result) {
LF_ERROR("Error: Cheak erase timeout.");
return result;
}
if(lf->chip_info.type==LF_DRIVER_NAND_FLASH){
result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
if (result || (status&0x04)){
Expand All @@ -320,7 +328,10 @@ static lf_err_t little_flash_cheak_write(const little_flash_t *lf){
lf_err_t result = LF_ERR_OK;
uint8_t status;
result |= little_flash_wait_busy(lf,700);
if (result) return result;
if (result) {
LF_ERROR("Error: Cheak write timeout.");
return result;
}
if(lf->chip_info.type==LF_DRIVER_NAND_FLASH){
result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
if (result||(status&0x08)){
Expand All @@ -334,7 +345,10 @@ static lf_err_t little_flash_cheak_read(const little_flash_t *lf){
lf_err_t result = LF_ERR_OK;
uint8_t status;
result |= little_flash_wait_busy(lf,60);
if (result) return result;
if (result) {
LF_ERROR("Error: Cheak read timeout.");
return result;
}
if(lf->chip_info.type==LF_DRIVER_NAND_FLASH){
result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
// 以下也是要根据不同型号移植的
Expand Down Expand Up @@ -387,6 +401,7 @@ lf_err_t little_flash_chip_erase(const little_flash_t *lf){
}
return LF_ERR_OK;
error:
LF_ERROR("Error: Chip erase failed.");
little_flash_write_enabled(lf, LF_DISABLE);
if (lf->unlock) {
lf->unlock(lf);
Expand Down
2 changes: 2 additions & 0 deletions demo/gpio/gpio/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ function pinx() -- 根据不同开发板,给LED赋值不同的gpio引脚编号
return 10, 11, 255 -- 开发板上就2个灯
elseif rtos_bsp == "EC618" then -- Air780E开发板引脚
return 27, 255, 255 -- AIR780E开发板上就一个灯
elseif string.find(rtos_bsp,"EC716E") then -- Air780EQ开发板引脚
return 14, 255, 255 -- AIR780EQ开发板上就一个灯
elseif string.find(rtos_bsp,"EC718") then -- Air780E开发板引脚
return 27, 255, 255 -- AIR780EP开发板上就一个灯
elseif rtos_bsp == "UIS8850BM" then -- Air780UM开发板引脚
Expand Down
43 changes: 27 additions & 16 deletions demo/iotcloud/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,28 @@ sys.taskInit(function()
-- 腾讯云
-- 动态注册
-- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx" ,product_secret = "xxx"})

-- 密钥校验
-- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx",device_name = "xxx",key = "xxx=="})
-- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx",device_name = "123456789",device_secret = "xxx=="})
-- 证书校验
-- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx",device_name = "xxx"},{tls={client_cert=io.readFile("/luadb/client_cert.crt")}})

-- iotcloudc = iotcloud.new(iotcloud.TENCENT,{produt_id = "xxx",device_name = "123456789"},{tls={client_cert=io.readFile("/luadb/client_cert.crt")}})

-- 阿里云
-- 动态注册(免预注册)
-- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{produt_id = "xxx",product_secret = "xxx"})
-- 密钥校验 (预注册)
-- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{produt_id = "xxx",device_name = "xxx",key = "xxx"})


-- onenet
-- 动态注册(免预注册)(一型一密)(仅企业版支持)
-- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{instance_id = "xxx",produt_id = "xxx",product_secret = "xxx"}) -- 企业版公共实例
-- 动态注册(预注册)(一型一密)
-- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{produt_id = "xxx",device_name = "xxx",product_secret = "xxx"}) -- 旧版公共实例
-- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{instance_id = "xxx",produt_id = "xxx",device_name = "xxx",product_secret = "xxx"}) -- 新版公共实例
-- 密钥校验 (预注册)(一机一密)
-- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{produt_id = "xxx",device_name = "xxx",key = "xxx"}) -- 旧版公共实例
-- iotcloudc = iotcloud.new(iotcloud.ALIYUN,{instance_id = "xxx",produt_id = "xxx",device_name = "xxx",key = "xxx"})-- 新版公共实例

-- ONENET云
-- 动态注册
-- iotcloudc = iotcloud.new(iotcloud.ONENET,{produt_id = "xxx",userid = "xxx",userkey = "xxx"})
-- 一型一密
-- iotcloudc = iotcloud.new(iotcloud.ONENET,{produt_id = "xxx",product_secret = "xxx"})
-- 一机一密
-- iotcloudc = iotcloud.new(iotcloud.ONENET,{produt_id = "xxx",device_name = "xxx",key = "xxx"})
-- iotcloudc = iotcloud.new(iotcloud.ONENET,{produt_id = "xxx",device_name = "xxx",device_secret = "xxx"})

-- 华为云
-- 动态注册(免预注册)
Expand All @@ -96,6 +97,15 @@ sys.taskInit(function()
-- 密钥校验 (预注册)
-- iotcloudc = iotcloud.new(iotcloud.HUAWEI,{produt_id = "xxx",endpoint = "xxx",device_name = "xxx",device_secret = "xxx"})

-- -- 涂鸦云
-- iotcloudc = iotcloud.new(iotcloud.TUYA,{device_name = "xxx",device_secret = "xxx"})

-- 百度云
-- iotcloudc = iotcloud.new(iotcloud.BAIDU,{produt_id = "xxx",device_name = "xxx",device_secret = "xxx"})

-- Tlink云
-- iotcloudc = iotcloud.new(iotcloud.TLINK,{produt_id = "xxx",product_secret = "xxx",device_name = "xxx"})
-- iotcloudc = iotcloud.new(iotcloud.TLINK,{produt_id = "xxx",product_secret = "xxx",device_name = "xxx"},{tls={client_cert=io.readFile("/luadb/client_cert.crt")}})



Expand All @@ -108,18 +118,19 @@ sys.taskInit(function()
end)

sys.subscribe("iotcloud", function(cloudc,event,data,payload)
-- 注意,此处不是协程内,复杂操作发消息给协程内进行处理
if event == iotcloud.CONNECT then -- 云平台联上了
print("iotcloud","CONNECT", "云平台连接成功")
-- iotcloud:subscribe("test") -- 定阅主题
-- cloudc:subscribe("test") -- 可以自由定阅主题等
elseif event == iotcloud.RECEIVE then
print("iotcloud","topic", data, "payload", payload)
-- 用户处理代码
print("iotcloud","topic", data, "payload", payload)
-- 用户处理代码
elseif event == iotcloud.OTA then
if data then
rtos.reboot()
end
elseif event == iotcloud.DISCONNECT then -- 云平台断开了
-- 用户处理代码
-- 用户处理代码
end
end)

Expand Down
2 changes: 1 addition & 1 deletion lua/src/liolib.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@summary io操作(扩展)
@version 1.0
@date 2020.07.03
@tag LUAT_CONF_BSP
@tag LUAT_USE_GPIO
@demo fs
@usage
-- io模块是lua原生模块,LuatOS增加了一些API
Expand Down
2 changes: 1 addition & 1 deletion lua/src/loslib.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@summary os操作
@version 1.0
@date 2020.07.03
@tag LUAT_CONF_BSP
@tag LUAT_USE_GPIO
@demo os_date_time
@usage
-- os模块是lua原生模块, 这份文档是为了方便阐述实际使用中的常见问题
Expand Down
2 changes: 1 addition & 1 deletion lua/src/lstrlib_exts.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
@module string
@summary 字符串操作函数
@tag LUAT_CONF_BSP
@tag LUAT_USE_GPIO
@demo string
*/
#include "luat_base.h"
Expand Down
2 changes: 1 addition & 1 deletion luat/modules/luat_lib_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@summary 日志库
@version 1.0
@date 2020.03.30
@tag LUAT_CONF_BSP
@tag LUAT_USE_GPIO
*/
#include "luat_base.h"
#include "luat_sys.h"
Expand Down
5 changes: 4 additions & 1 deletion luat/modules/luat_lib_otp.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
@version 1.0
@date 2021.12.08
@tag LUAT_USE_OTP
@demo otp
@usage
----------------------------
--- 本库已经废弃, 不要使用 ---
----------------------------
*/
#include "luat_base.h"
#include "luat_otp.h"
Expand Down
2 changes: 1 addition & 1 deletion luat/modules/luat_lib_rtos.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@summary RTOS底层操作库
@version 1.0
@date 2020.03.30
@tag LUAT_CONF_BSP
@tag LUAT_USE_GPIO
*/
#include "luat_base.h"
#include "luat_sys.h"
Expand Down
2 changes: 1 addition & 1 deletion luat/modules/luat_lib_sys_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@version 1.0
@date 2019.11.23
@video https://www.bilibili.com/video/BV1194y1o7q2
@tag LUAT_CONF_BSP
@tag LUAT_USE_GPIO
*/

/*
Expand Down
2 changes: 1 addition & 1 deletion luat/modules/luat_lib_sysplus_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@summary sys库的强力补充
@version 1.0
@date 2022.11.23
@tag LUAT_CONF_BSP
@tag LUAT_USE_GPIO
@usage
-- 本库是sys库的补充, 添加了如下内容:
-- 1. cwait机制
Expand Down
Loading

0 comments on commit f7f193a

Please sign in to comment.