Merge branch 'prepare_for_master' of https://www.gitlink.org.cn/xuos/xiuos into 2023_open
This commit is contained in:
@@ -46,7 +46,12 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_USER_TEST_I2C),y)
|
||||
SRC_FILES += test_i2c.c
|
||||
ifeq ($(CONFIG_BOARD_EDU_RISCV64_EVB),y)
|
||||
SRC_FILES += test_i2c_riscv.c
|
||||
endif
|
||||
ifeq ($(CONFIG_BOARD_EDU_ARM32_EVB),y)
|
||||
SRC_FILES += test_i2c_arm.c
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_USER_TEST_UART),y)
|
||||
@@ -66,7 +71,12 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_USER_TEST_RS485),y)
|
||||
SRC_FILES += test_rs485.c
|
||||
ifeq ($(CONFIG_BOARD_EDU_RISCV64_EVB),y)
|
||||
SRC_FILES += test_rs485_riscv.c
|
||||
endif
|
||||
ifeq ($(CONFIG_BOARD_EDU_ARM32_EVB),y)
|
||||
SRC_FILES += test_rs485_arm.c
|
||||
endif
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_USER_TEST_HWTIMER),y)
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
#include <transform.h>
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
|
||||
#define BSP_LED_PIN 134
|
||||
#define BSP_LED_PIN 29
|
||||
#define NULL_PARAMETER 0
|
||||
|
||||
static uint16_t pin_fd=0;
|
||||
@@ -37,7 +37,7 @@ void LedFlip(void *parameter)
|
||||
|
||||
void TestHwTimer(void)
|
||||
{
|
||||
x_ticks_t period = 100000;
|
||||
x_ticks_t period = 1;
|
||||
|
||||
pin_fd = PrivOpen(HWTIMER_PIN_DEV_DRIVER, O_RDWR);
|
||||
if(pin_fd<0) {
|
||||
|
||||
81
APP_Framework/Applications/app_test/test_i2c_riscv.c
Normal file
81
APP_Framework/Applications/app_test/test_i2c_riscv.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: test_i2c.c
|
||||
* @brief: a application of i2c function
|
||||
* @version: 1.1
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2022/12/17
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <transform.h>
|
||||
#include <sleep.h>
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
|
||||
#define I2C_SLAVE_ADDRESS (0x44U)
|
||||
|
||||
void TestI2C(void)
|
||||
{
|
||||
// config IIC pin(SCL:34.SDA:35) in menuconfig
|
||||
int iic_fd = PrivOpen(I2C_DEV_DRIVER, O_RDWR);
|
||||
if (iic_fd < 0)
|
||||
{
|
||||
printf("open iic_fd fd error:%d\n", iic_fd);
|
||||
return;
|
||||
}
|
||||
printf("IIC open successful!\n");
|
||||
|
||||
// init iic
|
||||
uint16 iic_address = I2C_SLAVE_ADDRESS;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = I2C_TYPE;
|
||||
ioctl_cfg.args = (void *)&iic_address;
|
||||
|
||||
if (0 != PrivIoctl(iic_fd, OPE_INT, &ioctl_cfg))
|
||||
{
|
||||
printf("ioctl iic fd error %d\n", iic_fd);
|
||||
PrivClose(iic_fd);
|
||||
return;
|
||||
}
|
||||
printf("IIC configure successful!\n");
|
||||
|
||||
// I2C read and write
|
||||
uint8_t data[32];
|
||||
|
||||
while (1)
|
||||
{
|
||||
PrivWrite(iic_fd, NONE, 0);
|
||||
msleep(40);
|
||||
PrivRead(iic_fd, data, 4);
|
||||
|
||||
float result = ((data[2] << 8 | data[3]) >> 2) * 165.0 /( (1 << 14) - 1) - 40.0;
|
||||
int temperature = result*10;
|
||||
printf("Temperature : %d.%d ℃\n", temperature/10, temperature%10);
|
||||
|
||||
result = ((data[0] << 8 | data[1] ) & 0x3fff) * 100.0 / ( (1 << 14) - 1);
|
||||
int humidity = result*10;
|
||||
printf("Humidity : %d.%d %%RH\n", humidity/10, humidity%10);
|
||||
|
||||
printf("HS300X origin data1:0x%2x%2x%2x%2x\n", data[0],data[1],data[2],data[3]);
|
||||
|
||||
msleep(1000);
|
||||
}
|
||||
|
||||
PrivClose(iic_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(TestI2C, a iic test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
#endif
|
||||
108
APP_Framework/Applications/app_test/test_rs485_riscv.c
Normal file
108
APP_Framework/Applications/app_test/test_rs485_riscv.c
Normal file
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: test_rs485.c
|
||||
* @brief: a application of rs485 function
|
||||
* @version: 1.1
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2022/12/17
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <transform.h>
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
|
||||
#define BSP_485_DIR_PIN 24
|
||||
|
||||
void Test485(void)
|
||||
{
|
||||
int pin_fd = PrivOpen(RS485_PIN_DEV_DRIVER, O_RDWR);
|
||||
if (pin_fd < 0)
|
||||
{
|
||||
printf("open pin fd error:%d\n", pin_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
int uart_fd = PrivOpen(RS485_UART_DEV_DRIVER, O_RDWR);
|
||||
if (uart_fd < 0)
|
||||
{
|
||||
printf("open pin fd error:%d\n", uart_fd);
|
||||
return;
|
||||
}
|
||||
printf("uart and pin fopen success\n");
|
||||
|
||||
//config led pin in board
|
||||
struct PinParam pin_parameter;
|
||||
memset(&pin_parameter, 0, sizeof(struct PinParam));
|
||||
pin_parameter.cmd = GPIO_CONFIG_MODE;
|
||||
pin_parameter.pin = BSP_485_DIR_PIN;
|
||||
pin_parameter.mode = GPIO_CFG_OUTPUT;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = PIN_TYPE;
|
||||
ioctl_cfg.args = (void *)&pin_parameter;
|
||||
|
||||
if (0 != PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg)) {
|
||||
printf("ioctl pin fd error %d\n", pin_fd);
|
||||
PrivClose(pin_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
struct SerialDataCfg uart_cfg;
|
||||
memset(&uart_cfg, 0, sizeof(struct SerialDataCfg));
|
||||
|
||||
uart_cfg.serial_baud_rate = BAUD_RATE_115200;
|
||||
uart_cfg.serial_data_bits = DATA_BITS_8;
|
||||
uart_cfg.serial_stop_bits = STOP_BITS_1;
|
||||
uart_cfg.serial_parity_mode = PARITY_NONE;
|
||||
uart_cfg.serial_bit_order = BIT_ORDER_LSB;
|
||||
uart_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
uart_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
uart_cfg.serial_timeout = 1000;
|
||||
uart_cfg.is_ext_uart = 0;
|
||||
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = (void *)&uart_cfg;
|
||||
|
||||
if (0 != PrivIoctl(uart_fd, OPE_INT, &ioctl_cfg))
|
||||
{
|
||||
printf("ioctl uart fd error %d\n", uart_fd);
|
||||
PrivClose(uart_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
struct PinStat pin_dir;
|
||||
pin_dir.pin = BSP_485_DIR_PIN;
|
||||
while (1)
|
||||
{
|
||||
pin_dir.val = GPIO_HIGH;
|
||||
PrivWrite(pin_fd,&pin_dir,0);
|
||||
PrivWrite(uart_fd,"Hello world!\n",sizeof("Hello world!\n"));
|
||||
printf("Send: Hello world!\n");
|
||||
PrivTaskDelay(1000);
|
||||
|
||||
pin_dir.val = GPIO_LOW;
|
||||
PrivWrite(pin_fd,&pin_dir,0);
|
||||
char recv_buff[100];
|
||||
memset(recv_buff,0,sizeof(recv_buff));
|
||||
PrivRead(uart_fd,recv_buff,20);
|
||||
printf("Recv: %s\n",recv_buff);
|
||||
PrivTaskDelay(1000);
|
||||
}
|
||||
PrivClose(pin_fd);
|
||||
PrivClose(uart_fd);
|
||||
return;
|
||||
}
|
||||
|
||||
PRIV_SHELL_CMD_FUNCTION(Test485, a RS485 test sample, PRIV_SHELL_CMD_MAIN_ATTR);
|
||||
#endif
|
||||
@@ -324,7 +324,7 @@ void TestSocket(int argc, char* argv[])
|
||||
return;
|
||||
} else {
|
||||
memset(iperf_param.host, 0, sizeof(iperf_param.host));
|
||||
strncpy(iperf_param.host, ip_ptr, strlen(ip_ptr));
|
||||
strncpy(iperf_param.host, ip_ptr, sizeof(iperf_param.host));
|
||||
}
|
||||
iperf_mode->mode = IPERF_MODE_CLIENT;
|
||||
}
|
||||
@@ -335,10 +335,22 @@ void TestSocket(int argc, char* argv[])
|
||||
if (mode == IPERF_MODE_SERVER) {
|
||||
printf("[%s] Running iperf server at port %d.\n", __func__, iperf_param.port);
|
||||
|
||||
PrivTaskCreate(&thd, NULL, TestIperfServer, (void*)&iperf_param);
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
char task_name[] = "test_iperf_server";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name;
|
||||
args.arg = (void *)&iperf_param;
|
||||
PrivTaskCreate(&thd, NULL, TestIperfServer, (void*)&args);
|
||||
#endif
|
||||
} else if (mode == IPERF_MODE_CLIENT) {
|
||||
printf("[%s] Running iperf client to server at %s:%d.\n", __func__, iperf_param.host, iperf_param.port);
|
||||
PrivTaskCreate(&thd, NULL, TestIperfClient, (void*)&iperf_param);
|
||||
#ifdef ADD_XIZI_FEATURES
|
||||
char task_name[] = "test_iperf_client";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name;
|
||||
args.arg = (void *)&iperf_param;
|
||||
PrivTaskCreate(&thd, NULL, TestIperfClient, (void*)&args);
|
||||
#endif
|
||||
}
|
||||
|
||||
PrivTaskStartup(&thd);
|
||||
|
||||
@@ -272,7 +272,7 @@ CircularAreaAppType CircularAreaAppInit(uint32_t circular_area_length)
|
||||
circular_area->p_tail = circular_area->data_buffer + circular_area_length;
|
||||
circular_area->area_length = circular_area_length;
|
||||
|
||||
printf("CircularAreaAppInit done p_head %8p p_tail %8p length %u\n",
|
||||
printf("CircularAreaAppInit done p_head %8p p_tail %8p length %lu\n",
|
||||
circular_area->p_head, circular_area->p_tail, circular_area->area_length);
|
||||
|
||||
circular_area->CircularAreaAppOperations = &CircularAreaAppOperations;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <cstdio>
|
||||
#include <transform.h>
|
||||
// #include <transform.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "tensorflow/lite/micro/all_ops_resolver.h"
|
||||
|
||||
@@ -26,3 +26,4 @@ void mnist_app(void);
|
||||
int tfmnist(void) {
|
||||
mnist_app();
|
||||
}
|
||||
PRIV_SHELL_CMD_FUNCTION(tfmnist, a tenorflow_lite_for_microcontroller sample, PRIV_SHELL_CMD_FUNC_ATTR);
|
||||
|
||||
@@ -24,23 +24,10 @@
|
||||
#include "lv_demo_calendar.h"
|
||||
#include <transform.h>
|
||||
|
||||
// extern void lv_example_chart_2(void);
|
||||
// extern void lv_example_img_1(void);
|
||||
// extern void lv_example_img_2(void);
|
||||
// extern void lv_example_img_3(void);
|
||||
// extern void lv_example_img_4(void);
|
||||
// extern void lv_example_line_1(void);
|
||||
// extern void lv_example_aoteman(void);
|
||||
extern void lv_example_show(void);
|
||||
void* lvgl_thread(void *parameter)
|
||||
{
|
||||
/* display demo; you may replace with your LVGL application at here */
|
||||
lv_demo_calendar();
|
||||
// lv_example_img_1();
|
||||
// lv_example_chart_2();
|
||||
// lv_example_table_1();
|
||||
// lv_example_line_1();
|
||||
// lv_example_aoteman();
|
||||
/* handle the tasks of LVGL */
|
||||
lv_example_show();
|
||||
while(1)
|
||||
{
|
||||
lv_task_handler();
|
||||
@@ -48,12 +35,12 @@ void* lvgl_thread(void *parameter)
|
||||
}
|
||||
}
|
||||
|
||||
pthread_t lvgl_task;
|
||||
static pthread_t lvgl_task;
|
||||
static int lvgl_demo_init(void)
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 25;
|
||||
attr.stacksize = 4096;
|
||||
attr.stacksize = 8192;
|
||||
|
||||
PrivTaskCreate(&lvgl_task, &attr, lvgl_thread, NULL);
|
||||
|
||||
|
||||
@@ -52,7 +52,7 @@ int lv_port_init(void)
|
||||
#endif
|
||||
|
||||
#ifndef PKG_USING_LVGL_INDEV_DEVICE
|
||||
lv_port_indev_init();
|
||||
// lv_port_indev_init();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -17,12 +17,21 @@
|
||||
|
||||
extern int FrameworkInit();
|
||||
extern void ApplicationOtaTaskInit(void);
|
||||
|
||||
#ifdef OTA_BY_PLATFORM
|
||||
extern int OtaTask(void);
|
||||
#endif
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Hello, world! \n");
|
||||
FrameworkInit();
|
||||
printf("Hello, world! \n");
|
||||
FrameworkInit();
|
||||
#ifdef APPLICATION_OTA
|
||||
ApplicationOtaTaskInit();
|
||||
ApplicationOtaTaskInit();
|
||||
#endif
|
||||
|
||||
#ifdef OTA_BY_PLATFORM
|
||||
OtaTask();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -28,10 +28,14 @@ void TvocTb600bTvoc10(void)
|
||||
{
|
||||
struct SensorQuantity *tvoc = SensorQuantityFind(SENSOR_QUANTITY_TB600B_TVOC, SENSOR_QUANTITY_TVOC);
|
||||
SensorQuantityOpen(tvoc);
|
||||
int32_t result = 0;
|
||||
|
||||
result = SensorQuantityReadValue(tvoc);
|
||||
for(int i = 0; i < 10;i++)
|
||||
{
|
||||
PrivTaskDelay(1000);
|
||||
SensorQuantityReadValue(tvoc);
|
||||
}
|
||||
|
||||
printf("tvoc concentration is : %dppb\n", result);
|
||||
SensorQuantityClose(tvoc);
|
||||
}
|
||||
}
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),tvoc, TvocTb600bTvoc10, read data from tvoc sensor);
|
||||
@@ -39,7 +39,7 @@ void* ttf_thread(void *parameter)
|
||||
}
|
||||
}
|
||||
|
||||
pthread_t lvgl_task;
|
||||
static pthread_t lvgl_task;
|
||||
static int ttf_demo_init(void)
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
|
||||
@@ -23,7 +23,7 @@ if ADD_XIZI_FEATURES
|
||||
|
||||
config ADAPTER_EC200T_DRIVER
|
||||
string "EC200T device uart driver path"
|
||||
default "/dev/usart2_dev2"
|
||||
default "/dev/uart8_dev8"
|
||||
depends on !ADAPTER_EC200T_DRIVER_EXTUART
|
||||
|
||||
if ADAPTER_EC200T_DRIVER_EXTUART
|
||||
|
||||
@@ -163,8 +163,12 @@ static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args)
|
||||
serial_cfg.serial_parity_mode = PARITY_NONE;
|
||||
serial_cfg.serial_bit_order = STOP_BITS_1;
|
||||
serial_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
#ifdef TOOL_USING_OTA
|
||||
serial_cfg.serial_timeout = OTA_RX_TIMEOUT;
|
||||
#else
|
||||
//serial receive timeout 10s
|
||||
serial_cfg.serial_timeout = 10000;
|
||||
serial_cfg.serial_timeout = 100000;
|
||||
#endif
|
||||
serial_cfg.is_ext_uart = 0;
|
||||
#ifdef ADAPTER_EC200T_DRIVER_EXT_PORT
|
||||
serial_cfg.is_ext_uart = 1;
|
||||
|
||||
@@ -6,7 +6,7 @@ menuconfig SUPPORT_CONNECTION_FRAMEWORK
|
||||
if SUPPORT_CONNECTION_FRAMEWORK
|
||||
config CONNECTION_FRAMEWORK_DEBUG
|
||||
bool "Using connection framework debug log function"
|
||||
default y
|
||||
default n
|
||||
|
||||
menuconfig CONNECTION_INDUSTRIAL_NETWORK
|
||||
bool "Using industrial network"
|
||||
|
||||
@@ -124,7 +124,9 @@ int ParseATReply(char *str, const char *format, ...)
|
||||
void ATSprintf(int fd, const char *format, va_list params)
|
||||
{
|
||||
last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, params);
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("AT send %s len %u\n",send_buf, last_cmd_len);
|
||||
#endif
|
||||
PrivWrite(fd, send_buf, last_cmd_len);
|
||||
}
|
||||
|
||||
@@ -264,29 +266,34 @@ int AtSetReplyCharNum(ATAgentType agent, unsigned int num)
|
||||
|
||||
int EntmSend(ATAgentType agent, const char *data, int len)
|
||||
{
|
||||
char send_buf[128];
|
||||
if(len > 128){
|
||||
printf("send length %d more then max 128 Bytes.\n",len);
|
||||
if(len > 256){
|
||||
printf("send length %d more then max 256 Bytes.\n",len);
|
||||
return -1;
|
||||
}
|
||||
char *send_buff = (char *)PrivMalloc(256);
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
memset(send_buf, 0, 128);
|
||||
memset(send_buff, 0, 256);
|
||||
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
|
||||
memcpy(send_buf, data, len);
|
||||
// memcpy(send_buf + len, "!@", 2);
|
||||
memcpy(send_buff, data, len);
|
||||
|
||||
PrivWrite(agent->fd, send_buf, len);
|
||||
PrivWrite(agent->fd, send_buff, len);
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
printf("entm send %s length %d\n",send_buf, len);
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("entm send length %d\n", len);
|
||||
#endif
|
||||
|
||||
PrivFree(send_buff);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
|
||||
{
|
||||
struct timespec abstime;
|
||||
uint32 real_recv_len = 0;
|
||||
|
||||
abstime.tv_sec = timeout_s;
|
||||
if(buffer_len > ENTM_RECV_MAX){
|
||||
@@ -299,21 +306,25 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
//PrivTaskDelay(1000);
|
||||
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) {
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("wait sem[%d] timeout\n",agent->entm_rx_notice);
|
||||
#endif
|
||||
agent->entm_recv_len = 0;
|
||||
return -1;
|
||||
}
|
||||
PrivMutexObtain(&agent->lock);
|
||||
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("EntmRecv once len %d.\n", agent->entm_recv_len);
|
||||
|
||||
#endif
|
||||
memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len);
|
||||
|
||||
memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX);
|
||||
|
||||
real_recv_len = agent->entm_recv_len;
|
||||
agent->entm_recv_len = 0;
|
||||
agent->read_len = 0;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
return buffer_len;
|
||||
return real_recv_len;
|
||||
}
|
||||
|
||||
static int GetCompleteATReply(ATAgentType agent)
|
||||
@@ -321,21 +332,22 @@ static int GetCompleteATReply(ATAgentType agent)
|
||||
uint32_t read_len = 0;
|
||||
char ch = 0, last_ch = 0;
|
||||
bool is_full = false;
|
||||
int res;
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
agent->maintain_len = 0;
|
||||
|
||||
memset(agent->entm_recv_buf, 0x00, 256);
|
||||
memset(agent->entm_recv_buf, 0x00, ENTM_RECV_MAX);
|
||||
agent->entm_recv_len = 0;
|
||||
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
while (1) {
|
||||
PrivRead(agent->fd, &ch, 1);
|
||||
res = PrivRead(agent->fd, &ch, 1);
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
if(ch != 0) {
|
||||
if((res == 1) && (ch != 0)) {
|
||||
printf(" %c (0x%x)\n", ch, ch);
|
||||
}
|
||||
#endif
|
||||
@@ -343,14 +355,28 @@ static int GetCompleteATReply(ATAgentType agent)
|
||||
PrivMutexObtain(&agent->lock);
|
||||
if (agent->receive_mode == ENTM_MODE) {
|
||||
if (agent->entm_recv_len < ENTM_RECV_MAX) {
|
||||
agent->entm_recv_buf[agent->entm_recv_len] = ch;
|
||||
agent->entm_recv_len++;
|
||||
|
||||
if(agent->entm_recv_len < agent->read_len) {
|
||||
#ifdef TOOL_USING_MQTT
|
||||
if((res == 1) && (agent->entm_recv_len < agent->read_len))
|
||||
{
|
||||
agent->entm_recv_buf[agent->entm_recv_len] = ch;
|
||||
agent->entm_recv_len++;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
continue;
|
||||
} else {
|
||||
}
|
||||
#else
|
||||
agent->entm_recv_buf[agent->entm_recv_len] = ch;
|
||||
agent->entm_recv_len++;
|
||||
if(agent->entm_recv_len < agent->read_len)
|
||||
{
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf("ENTM_MODE recv %d Bytes done.\n",agent->entm_recv_len);
|
||||
#endif
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
PrivSemaphoreAbandon(&agent->entm_rx_notice);
|
||||
}
|
||||
@@ -523,13 +549,19 @@ static int ATAgentInit(ATAgentType agent)
|
||||
attr.priority = 18;
|
||||
attr.stacksize = 8192;
|
||||
|
||||
PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, agent);
|
||||
#else
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 25;
|
||||
attr.stacksize = 4096;
|
||||
#endif
|
||||
|
||||
PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, agent);
|
||||
char task_name[] = "at_agent";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name;
|
||||
args.arg = (void *)agent;
|
||||
|
||||
PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, (void *)&args);
|
||||
#endif
|
||||
|
||||
return result;
|
||||
|
||||
|
||||
@@ -28,6 +28,12 @@
|
||||
|
||||
#define REPLY_TIME_OUT 10
|
||||
|
||||
#ifdef TOOL_USING_OTA
|
||||
#define ENTM_RECV_MAX OTA_RX_BUFFERSIZE
|
||||
#else
|
||||
#define ENTM_RECV_MAX 256
|
||||
#endif
|
||||
|
||||
enum ReceiveMode
|
||||
{
|
||||
DEFAULT_MODE = 0,
|
||||
@@ -70,7 +76,6 @@ struct ATAgent
|
||||
#endif
|
||||
pthread_t at_handler;
|
||||
|
||||
#define ENTM_RECV_MAX 256
|
||||
char entm_recv_buf[ENTM_RECV_MAX];
|
||||
uint32 entm_recv_len;
|
||||
enum ReceiveMode receive_mode;
|
||||
|
||||
@@ -129,6 +129,9 @@ uint8_t lora_recv_data[ADAPTER_LORA_TRANSFER_DATA_LENGTH];
|
||||
struct LoraDataFormat client_recv_data_format[ADAPTER_LORA_CLIENT_NUM];
|
||||
|
||||
static sem_t gateway_recv_data_sem;
|
||||
static sem_t gateway_send_cmd_sem;
|
||||
static sem_t client_recv_cmd_sem;
|
||||
static sem_t client_send_data_sem;
|
||||
struct LoraDataFormat gateway_recv_data_format;
|
||||
|
||||
static int recv_error_cnt = 0;
|
||||
@@ -315,6 +318,8 @@ static int LoraGatewaySendCmd(struct Adapter *adapter, uint8_t client_id, uint16
|
||||
return -1;
|
||||
}
|
||||
|
||||
PrivSemaphoreAbandon(&gateway_send_cmd_sem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -410,6 +415,7 @@ static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int lengt
|
||||
return -1;
|
||||
}
|
||||
|
||||
PrivSemaphoreAbandon(&client_send_data_sem);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -462,7 +468,7 @@ static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int le
|
||||
struct timespec abstime;
|
||||
abstime.tv_sec = DEFAULT_SEM_TIMEOUT;
|
||||
|
||||
ret = PrivSemaphoreObtainWait(&adapter->sem, &abstime);
|
||||
ret = PrivSemaphoreObtainWait(&client_recv_cmd_sem, NULL);
|
||||
if (0 == ret) {
|
||||
//only handle this client_id information from gateway
|
||||
if ((client_recv_data_format[client_id - 1].client_id == adapter->net_role_id) &&
|
||||
@@ -653,9 +659,15 @@ static int LoraReceiveDataCheck(struct Adapter *adapter, uint8_t *recv_data, uin
|
||||
static void *LoraReceiveTask(void *parameter)
|
||||
{
|
||||
int ret = 0;
|
||||
struct timespec abstime;
|
||||
abstime.tv_sec = DEFAULT_SEM_TIMEOUT;
|
||||
|
||||
struct Adapter *lora_adapter = (struct Adapter *)parameter;
|
||||
|
||||
while (1) {
|
||||
#ifdef AS_LORA_GATEWAY_ROLE
|
||||
PrivSemaphoreObtainWait(&gateway_send_cmd_sem, NULL);
|
||||
#endif
|
||||
memset(lora_recv_data, 0, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
|
||||
|
||||
ret = AdapterDeviceRecv(lora_adapter, lora_recv_data, ADAPTER_LORA_TRANSFER_DATA_LENGTH);
|
||||
@@ -675,8 +687,10 @@ static void *LoraReceiveTask(void *parameter)
|
||||
if (ret < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PrivSemaphoreAbandon(&lora_adapter->sem);
|
||||
#ifdef AS_LORA_CLIENT_ROLE
|
||||
PrivSemaphoreAbandon(&client_recv_cmd_sem);
|
||||
PrivSemaphoreObtainWait(&client_send_data_sem, &abstime);
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -702,7 +716,7 @@ void LoraGatewayProcess(struct Adapter *lora_adapter, struct LoraGatewayParam *g
|
||||
printf("LoraGatewaySendCmd client ID %d error\n", gateway->client_id[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
ret = PrivSemaphoreObtainWait(&gateway_recv_data_sem, &abstime);
|
||||
if (0 == ret) {
|
||||
printf("LoraGatewayProcess receive client %d data done\n", gateway->client_id[i]);
|
||||
@@ -834,8 +848,10 @@ static int AdapterLoraRegister(struct Adapter *adapter)
|
||||
ret = AdapterDeviceRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("Adapter4G register error\n");
|
||||
#ifdef AS_LORA_GATEWAY_ROLE
|
||||
if (lora_gateway)
|
||||
PrivFree(lora_gateway);
|
||||
#endif
|
||||
if (lora_client)
|
||||
PrivFree(lora_client);
|
||||
|
||||
@@ -904,9 +920,13 @@ int AdapterLoraInit(void)
|
||||
adapter->done = product_info->model_done;
|
||||
#endif
|
||||
|
||||
PrivSemaphoreCreate(&adapter->sem, 0, 0);
|
||||
|
||||
#ifdef AS_LORA_GATEWAY_ROLE
|
||||
PrivSemaphoreCreate(&gateway_recv_data_sem, 0, 0);
|
||||
PrivSemaphoreCreate(&gateway_send_cmd_sem, 0, 0);
|
||||
#else//AS_LORA_CLIENT_ROLE
|
||||
PrivSemaphoreCreate(&client_recv_cmd_sem, 0, 0);
|
||||
PrivSemaphoreCreate(&client_send_data_sem, 0, 0);
|
||||
#endif
|
||||
|
||||
PrivMutexCreate(&adapter->lock, 0);
|
||||
|
||||
@@ -934,22 +954,32 @@ int AdapterLoraTest(void)
|
||||
pthread_attr_t lora_gateway_attr = PTHREAD_ATTR_INITIALIZER;
|
||||
lora_gateway_attr.priority = 20;
|
||||
lora_gateway_attr.stacksize = 2048;
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_gateway_attr, &LoraReceiveTask, (void *)adapter);
|
||||
#else
|
||||
pthread_attr_t lora_gateway_attr;
|
||||
lora_gateway_attr.schedparam.sched_priority = 20;
|
||||
lora_gateway_attr.stacksize = 2048;
|
||||
|
||||
char task_name_1[] = "adapter_lora_recv";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name_1;
|
||||
args.arg = (void *)adapter;
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_gateway_attr, &LoraReceiveTask, (void *)&args);
|
||||
#endif
|
||||
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_gateway_attr, &LoraReceiveTask, (void *)adapter);
|
||||
PrivTaskStartup(&lora_recv_data_task);
|
||||
|
||||
#ifdef ADD_NUTTX_FEATURES
|
||||
lora_gateway_attr.priority = 19;
|
||||
lora_gateway_attr.priority = 20;
|
||||
PrivTaskCreate(&lora_gateway_task, &lora_gateway_attr, &LoraGatewayTask, (void *)adapter);
|
||||
#else
|
||||
lora_gateway_attr.schedparam.sched_priority = 19;
|
||||
lora_gateway_attr.schedparam.sched_priority = 20;
|
||||
char task_name_2[] = "adapter_lora_gateway";
|
||||
args.pthread_name = task_name_2;
|
||||
args.arg = (void *)adapter;
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_gateway_attr, &LoraReceiveTask, (void *)&args);
|
||||
#endif
|
||||
|
||||
PrivTaskCreate(&lora_gateway_task, &lora_gateway_attr, &LoraGatewayTask, (void *)adapter);
|
||||
PrivTaskStartup(&lora_gateway_task);
|
||||
|
||||
#else //AS_LORA_CLIENT_ROLE
|
||||
@@ -957,22 +987,34 @@ int AdapterLoraTest(void)
|
||||
pthread_attr_t lora_client_attr = PTHREAD_ATTR_INITIALIZER;
|
||||
lora_client_attr.priority = 20;
|
||||
lora_client_attr.stacksize = 2048;
|
||||
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_client_attr, &LoraReceiveTask, (void *)adapter);
|
||||
#else
|
||||
pthread_attr_t lora_client_attr;
|
||||
lora_client_attr.schedparam.sched_priority = 20;
|
||||
lora_client_attr.stacksize = 2048;
|
||||
|
||||
char task_name_1[] = "adapter_lora_recv";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name_1;
|
||||
args.arg = (void *)adapter;
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_client_attr, &LoraReceiveTask, (void *)&args);
|
||||
#endif
|
||||
PrivTaskCreate(&lora_recv_data_task, &lora_client_attr, &LoraReceiveTask, (void *)adapter);
|
||||
|
||||
PrivTaskStartup(&lora_recv_data_task);
|
||||
|
||||
#ifdef ADD_NUTTX_FEATURES
|
||||
lora_client_attr.priority = 19;
|
||||
lora_client_attr.priority = 20;
|
||||
PrivTaskCreate(&lora_client_data_task, &lora_client_attr, &LoraClientDataTask, (void *)adapter);
|
||||
#else
|
||||
lora_client_attr.schedparam.sched_priority = 19;
|
||||
lora_client_attr.schedparam.sched_priority = 20;
|
||||
char task_name_2[] = "adapter_lora_client";
|
||||
args.pthread_name = task_name_2;
|
||||
args.arg = (void *)adapter;
|
||||
PrivTaskCreate(&lora_client_data_task, &lora_client_attr, &LoraClientDataTask, (void *)&args);
|
||||
#endif
|
||||
|
||||
//create lora client task
|
||||
PrivTaskCreate(&lora_client_data_task, &lora_client_attr, &LoraClientDataTask, (void *)adapter);
|
||||
PrivTaskStartup(&lora_client_data_task);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -67,7 +67,7 @@ typedef enum
|
||||
|
||||
struct ControlDevice
|
||||
{
|
||||
char *dev_name;
|
||||
char dev_name[20];
|
||||
int status;
|
||||
|
||||
//to do
|
||||
|
||||
@@ -322,7 +322,12 @@ int ControlProtocolOpenDef(struct ControlProtocol *control_protocol)
|
||||
attr.schedparam.sched_priority = 19;
|
||||
attr.stacksize = 2048;
|
||||
|
||||
PrivTaskCreate(&recv_plc_data_task, &attr, &ReceivePlcDataTask, control_protocol);
|
||||
char task_name[] = "control_recv_data";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name;
|
||||
args.arg = (void *)control_protocol;
|
||||
|
||||
PrivTaskCreate(&recv_plc_data_task, &attr, &ReceivePlcDataTask, (void *)&args);
|
||||
PrivTaskStartup(&recv_plc_data_task);
|
||||
}
|
||||
|
||||
|
||||
@@ -76,7 +76,7 @@ void Uart485Init(uint32_t baud_rate, uint8_t data_bits, uint8_t stop_bits, uint8
|
||||
ioctl_cfg.args = &pin_param;
|
||||
PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
|
||||
|
||||
uart_fd = open(CONTROL_FRAMEWORK_UART_DEV, O_RDWR);
|
||||
uart_fd = PrivOpen(CONTROL_FRAMEWORK_UART_DEV, O_RDWR);
|
||||
if (uart_fd < 0) {
|
||||
printf("open fd error %d\n", uart_fd);
|
||||
return;
|
||||
@@ -190,3 +190,33 @@ int SerialRead(uint8_t *read_data, int length)
|
||||
return data_size;
|
||||
#endif
|
||||
}
|
||||
|
||||
int ControlFileDataStore(uint8 *data, int data_length)
|
||||
{
|
||||
int data_file_fd = -1;
|
||||
struct stat data_file_status;
|
||||
int i = 0;
|
||||
|
||||
//Step1 : open data file from SD card or other store device
|
||||
data_file_fd = PrivOpen(FILE_NAME, O_RDONLY);
|
||||
if (data_file_fd < 0) {
|
||||
printf("Open data file %s failed\n", FILE_NAME);
|
||||
PrivClose(data_file_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (0 != fstat(data_file_fd, &data_file_status)) {
|
||||
printf("Get data file information failed!\n");
|
||||
PrivClose(data_file_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
lseek(data_file_fd, data_file_status.st_size, SEEK_SET);
|
||||
|
||||
//Step2 : write data to file in SD card or other store device
|
||||
FatfsPrintf(GetFileDescriptor(data_file_fd), data, data_length);
|
||||
|
||||
//Step3 : close data file from SD card or other store device
|
||||
PrivClose(data_file_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -48,6 +48,9 @@ extern "C" {
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define FILE_NAME "plc_data.csv"
|
||||
extern void FatfsPrintf(struct FileDescriptor *fdp, const void *src, size_t len);
|
||||
|
||||
/*Control Framework Socket Init*/
|
||||
void SocketInit(char *ip, char *mask, char *gw);
|
||||
|
||||
@@ -60,6 +63,9 @@ void SerialWrite(uint8_t *write_data, int length);
|
||||
/*Control Framework Serial Read*/
|
||||
int SerialRead(uint8_t *read_data, int length);
|
||||
|
||||
/*Control Framework Store data in SD Card*/
|
||||
int ControlFileDataStore(uint8 *data, int data_length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -26,6 +26,7 @@ extern int AdapterEthercatInit(void);
|
||||
extern int AdapterZigbeeInit(void);
|
||||
extern int AdapterLoraInit(void);
|
||||
|
||||
extern int Bmp180AltitudeInit(void);
|
||||
extern int D124VoiceInit(void);
|
||||
extern int Hs300xTemperatureInit(void);
|
||||
extern int Hs300xHumidityInit(void);
|
||||
@@ -83,6 +84,10 @@ static struct InitDesc framework[] =
|
||||
|
||||
static struct InitDesc sensor_desc[] =
|
||||
{
|
||||
#ifdef SENSOR_DEVICE_BMP180
|
||||
{ "bmp180_altitude", Bmp180AltitudeInit},
|
||||
#endif
|
||||
|
||||
#ifdef SENSOR_DEVICE_D124
|
||||
{ "d124_voice", D124VoiceInit },
|
||||
#endif
|
||||
|
||||
@@ -9,7 +9,7 @@ config KPU_DEV_DRIVER
|
||||
|
||||
config CAMERA_DEV_DRIVER
|
||||
string "Set camera dev path for kpu"
|
||||
default "/dev/ov2640"
|
||||
default "/dev/camera_dev"
|
||||
|
||||
config KPU_LCD_DEV_DRIVER
|
||||
string "Set lcd dev path for kpu"
|
||||
|
||||
@@ -69,12 +69,28 @@ static int SensorDeviceRead(struct SensorDevice *sdev, size_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Write sensor device
|
||||
* @param sdev - sensor device pointer
|
||||
* @param buf - the buffer of write data
|
||||
* @param len - the length of the write data
|
||||
* @return success: 0 , failure: -1
|
||||
*/
|
||||
static int SensorDeviceWrite(struct SensorDevice *sdev, const void *buf, size_t len)
|
||||
{
|
||||
//Read i2c device data from i2c device address
|
||||
if (PrivWrite(sdev->fd, buf, len) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct SensorDone done =
|
||||
{
|
||||
SensorDeviceOpen,
|
||||
NULL,
|
||||
SensorDeviceRead,
|
||||
NULL,
|
||||
SensorDeviceWrite,
|
||||
NULL,
|
||||
};
|
||||
|
||||
@@ -109,7 +125,8 @@ static int32_t ReadHumidity(struct SensorQuantity *quant)
|
||||
float result = 0.0;
|
||||
if (quant->sdev->done->read != NULL) {
|
||||
if (quant->sdev->status == SENSOR_DEVICE_PASSIVE) {
|
||||
quant->sdev->done->read(quant->sdev, 4);
|
||||
quant->sdev->done->write(quant->sdev, NONE, 0);
|
||||
PrivTaskDelay(50);
|
||||
quant->sdev->done->read(quant->sdev, 4); /* It takes two reads to get the data right */
|
||||
result = ((quant->sdev->buffer[0] << 8 | quant->sdev->buffer[1] ) & 0x3fff) * 100.0 / ( (1 << 14) - 1);
|
||||
|
||||
|
||||
@@ -80,7 +80,12 @@ static int SensorDeviceOpen(struct SensorDevice *sdev)
|
||||
|
||||
result = PrivIoctl(sdev->fd, OPE_INT, &cfg);
|
||||
|
||||
PrivTaskCreate(&active_task_id, NULL, &ReadTask, sdev);
|
||||
char task_name[] = "ps5308_recv_data";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name;
|
||||
args.arg = (void *)sdev;
|
||||
|
||||
PrivTaskCreate(&active_task_id, NULL, &ReadTask, (void *)&args);
|
||||
PrivTaskStartup(&active_task_id);
|
||||
|
||||
return result;
|
||||
|
||||
@@ -68,12 +68,28 @@ static int SensorDeviceRead(struct SensorDevice *sdev, size_t len)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Write sensor device
|
||||
* @param sdev - sensor device pointer
|
||||
* @param buf - the buffer of write data
|
||||
* @param len - the length of the write data
|
||||
* @return success: 0 , failure: -1
|
||||
*/
|
||||
static int SensorDeviceWrite(struct SensorDevice *sdev, const void *buf, size_t len)
|
||||
{
|
||||
//Read i2c device data from i2c device address
|
||||
if (PrivWrite(sdev->fd, buf, len) < 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct SensorDone done =
|
||||
{
|
||||
SensorDeviceOpen,
|
||||
NULL,
|
||||
SensorDeviceRead,
|
||||
NULL,
|
||||
SensorDeviceWrite,
|
||||
NULL,
|
||||
};
|
||||
|
||||
@@ -108,7 +124,7 @@ static int32_t ReadTemperature(struct SensorQuantity *quant)
|
||||
float result;
|
||||
if (quant->sdev->done->read != NULL) {
|
||||
if (quant->sdev->status == SENSOR_DEVICE_PASSIVE) {
|
||||
quant->sdev->done->read(quant->sdev, 4);
|
||||
quant->sdev->done->write(quant->sdev, NONE, 0);
|
||||
PrivTaskDelay(50);
|
||||
quant->sdev->done->read(quant->sdev, 4); /* It takes two reads to get the data right */
|
||||
result = ((quant->sdev->buffer[2] << 8 | quant->sdev->buffer[3]) >> 2) * 165.0 /( (1 << 14) - 1) - 40.0;
|
||||
|
||||
@@ -116,7 +116,12 @@ static int SensorDeviceOpen(struct SensorDevice *sdev)
|
||||
attr.schedparam.sched_priority = 20;
|
||||
attr.stacksize = 2048;
|
||||
|
||||
PrivTaskCreate(&active_task_id, &attr, &ReadTask, sdev);
|
||||
char task_name[] = "d124_recv_data";
|
||||
pthread_args_t args;
|
||||
args.pthread_name = task_name;
|
||||
args.arg = (void *)sdev;
|
||||
|
||||
PrivTaskCreate(&active_task_id, &attr, &ReadTask, (void *)&args);
|
||||
PrivTaskStartup(&active_task_id);
|
||||
|
||||
return result;
|
||||
|
||||
@@ -206,7 +206,7 @@ static int PrivLcdIoctl(int fd, int cmd, void *args)
|
||||
|
||||
int PrivIoctl(int fd, int cmd, void *args)
|
||||
{
|
||||
int ret;
|
||||
int ret = -ERROR;
|
||||
struct PrivIoctlCfg *ioctl_cfg = (struct PrivIoctlCfg *)args;
|
||||
switch (ioctl_cfg->ioctl_driver_type)
|
||||
{
|
||||
|
||||
@@ -43,6 +43,9 @@ extern "C" {
|
||||
|
||||
#define NAME_NUM_MAX 32
|
||||
|
||||
#define LINKLIST_FLAG_FIFO 0x00
|
||||
#define LINKLIST_FLAG_PRIO 0x01
|
||||
|
||||
#ifndef EVENT_AND
|
||||
#define EVENT_AND (1 << 0)
|
||||
#endif
|
||||
@@ -169,6 +172,8 @@ struct SerialDataCfg
|
||||
uint16_t serial_buffer_size;
|
||||
int32 serial_timeout;
|
||||
|
||||
int (*dev_recv_callback) (void *dev, size_t length);
|
||||
|
||||
uint8_t is_ext_uart;
|
||||
uint8_t ext_uart_no;
|
||||
enum ExtSerialPortConfigure port_configure;
|
||||
|
||||
@@ -72,6 +72,11 @@ extern "C" {
|
||||
|
||||
typedef int pid_t;
|
||||
// typedef int pthread_mutex_t ;
|
||||
typedef struct pthread_args
|
||||
{
|
||||
void *arg;
|
||||
const char *pthread_name;
|
||||
}pthread_args_t;
|
||||
|
||||
/* scheduling algorithms */
|
||||
#define SCHED_OTHER 0
|
||||
|
||||
@@ -29,28 +29,40 @@
|
||||
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
||||
void *(*start_routine)(void *), void *arg)
|
||||
{
|
||||
int ret ;
|
||||
int pid ;
|
||||
int ret;
|
||||
int pid;
|
||||
char task_name[32] = {0};
|
||||
static int utask_id = 0;
|
||||
UtaskType task ;
|
||||
UtaskType task;
|
||||
pthread_args_t *pthread_args = (pthread_args_t *)arg;
|
||||
|
||||
if (NULL == attr) {
|
||||
task.prio = KTASK_PRIORITY_MAX / 2;
|
||||
task.stack_size = 4096 ;
|
||||
task.stack_size = 4096;
|
||||
} else {
|
||||
task.prio = attr->schedparam.sched_priority ;
|
||||
task.stack_size = attr->stacksize ;
|
||||
task.prio = attr->schedparam.sched_priority;
|
||||
task.stack_size = attr->stacksize;
|
||||
}
|
||||
|
||||
task.func_entry = start_routine;
|
||||
|
||||
if (NULL == pthread_args) {
|
||||
task.func_param = NULL;
|
||||
snprintf(task_name, sizeof(task_name) - 1, "utask%02d", utask_id++);
|
||||
} else {
|
||||
task.func_param = pthread_args->arg;
|
||||
if (NULL == pthread_args->pthread_name) {
|
||||
snprintf(task_name, sizeof(task_name) - 1, "utask%02d", utask_id++);
|
||||
} else {
|
||||
snprintf(task_name, sizeof(task_name) - 1, pthread_args->pthread_name);
|
||||
}
|
||||
}
|
||||
|
||||
task.func_entry = start_routine ;
|
||||
task.func_param = arg;
|
||||
snprintf(task_name, sizeof(task_name) - 1, "utask%02d",utask_id++);
|
||||
memcpy(task.name , task_name, sizeof(task_name) - 1);
|
||||
|
||||
pid = UserTaskCreate(task);
|
||||
if (pid < 0)
|
||||
return -1 ;
|
||||
return -1;
|
||||
|
||||
ret = UserTaskStartup(pid);
|
||||
*thread = (pthread_t)(long)pid;
|
||||
|
||||
@@ -25,8 +25,11 @@
|
||||
#include "include/semaphore.h"
|
||||
#include "include/pthread.h"
|
||||
|
||||
static sem_t timer_sem;
|
||||
static pthread_t timer_task;
|
||||
#define TIMER_NUM 20
|
||||
|
||||
static sem_t timer_sem[TIMER_NUM];
|
||||
static pthread_t timer_task[TIMER_NUM];
|
||||
static char timer_idx[TIMER_NUM];
|
||||
|
||||
struct timer_func {
|
||||
union sigval value;
|
||||
@@ -34,16 +37,17 @@ struct timer_func {
|
||||
void (* user_timer_function)(union sigval val);
|
||||
};
|
||||
|
||||
struct timer_func g_timer_func;
|
||||
struct timer_func g_timer_func[TIMER_NUM];
|
||||
|
||||
static void *timer_callback(void *args)
|
||||
{
|
||||
struct sigevent *evp = (struct sigevent *)args;
|
||||
clockid_t clockid = *((clockid_t *)args);
|
||||
|
||||
while (1) {
|
||||
if (g_timer_func.user_timer_function != NULL) {
|
||||
if (0 == sem_timedwait(&timer_sem, NULL)) {
|
||||
g_timer_func.user_timer_function(g_timer_func.value);
|
||||
if (g_timer_func[clockid].user_timer_function != NULL) {
|
||||
if (0 == sem_timedwait(&(timer_sem[clockid]), NULL)) {
|
||||
g_timer_func[clockid].value.sival_ptr = &clockid;
|
||||
g_timer_func[clockid].user_timer_function(g_timer_func[clockid].value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,32 +70,39 @@ int timer_create(clockid_t clockid, struct sigevent * evp, timer_t * timerid)
|
||||
}
|
||||
|
||||
memset(timer_name, 0, sizeof(timer_name));
|
||||
snprintf(timer_name, sizeof(timer_name), "timer_%d", clockid);
|
||||
snprintf(timer_name, sizeof(timer_name), "timer_%ld", clockid);
|
||||
|
||||
sem_init(&timer_sem, 0, 0);
|
||||
sem_init(&(timer_sem[clockid]), 0, 0);
|
||||
|
||||
g_timer_func.value = evp->sigev_value;
|
||||
g_timer_func.user_timer_function = evp->sigev_notify_function;
|
||||
g_timer_func.timer_flags = *(int *)(evp->sigev_notify_attributes);
|
||||
g_timer_func[clockid].value = evp->sigev_value;
|
||||
g_timer_func[clockid].user_timer_function = evp->sigev_notify_function;
|
||||
g_timer_func[clockid].timer_flags = *(int *)(evp->sigev_notify_attributes);
|
||||
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 22;
|
||||
attr.stacksize = 2048;
|
||||
|
||||
pthread_create(&timer_task, &attr, &timer_callback, (void *)evp);
|
||||
|
||||
timer_id = UserTimerCreate(timer_name, NULL, (void *)&timer_sem, 1000, g_timer_func.timer_flags);
|
||||
pthread_args_t args;
|
||||
args.pthread_name = timer_name;
|
||||
args.arg = &clockid;
|
||||
|
||||
pthread_create(&(timer_task[clockid]), &attr, &timer_callback, (void *)&args);
|
||||
|
||||
timer_id = UserTimerCreate(timer_name, NULL, (void *)&(timer_sem[clockid]), clockid, g_timer_func[clockid].timer_flags);
|
||||
*timerid = timer_id;
|
||||
return timer_id;
|
||||
}
|
||||
|
||||
int timer_delete(timer_t timerid)
|
||||
{
|
||||
pthread_kill(timer_task, 0);
|
||||
sem_t sem;
|
||||
int timer_id = timerid;
|
||||
pthread_kill(timer_task[timer_id], 0);
|
||||
|
||||
UserTimerQuitRun(timerid);
|
||||
|
||||
sem_destroy(&timer_sem);
|
||||
sem = timer_sem[timer_id];
|
||||
sem_destroy(&sem);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -14,4 +14,5 @@ menu "app lib"
|
||||
source "$APP_DIR/lib/lvgl/Kconfig"
|
||||
source "$APP_DIR/lib/embedded_database/Kconfig"
|
||||
source "$APP_DIR/lib/lorawan/Kconfig"
|
||||
source "$APP_DIR/lib/mqtt/Kconfig"
|
||||
endmenu
|
||||
|
||||
@@ -18,4 +18,8 @@ ifeq ($(CONFIG_LIB_USING_LORAWAN),y)
|
||||
SRC_DIR += lorawan
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_TOOL_USING_MQTT),y)
|
||||
SRC_DIR += mqtt
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
||||
@@ -20,10 +20,14 @@ xiuos/APP_Framework/lib/lorawan
|
||||
|
||||
```
|
||||
# 下载代码
|
||||
# 进入APP_Framework/lib/lorawan目录下载更新子模块
|
||||
# 进入APP_Framework/lib/lorawan目录下载更新子模块,首先执行以下命令:
|
||||
git submodule init
|
||||
# 若需要使用lora_radio_driver子模块,执行以下命令:
|
||||
git submodule update APP_Framework/lib/lorawan/lora_radio_driver
|
||||
# 若需要使用lorawan_devicenode子模块,执行以下命令:
|
||||
git submodule update APP_Framework/lib/lorawan/lorawan_devicenode
|
||||
# 若需要使用lorawan_gateway_single_channel子模块,执行以下命令:
|
||||
git submodule update APP_Framework/lib/lorawan/lorawan_gateway_single_channel
|
||||
|
||||
# 进入 APP_Framework/lib/lorawan/Kconfig 配置,增加子模块source路径,从而编译时可找到相应lib的配置
|
||||
menuconfig LIB_USING_LORAWAN_GATEWAY_SC
|
||||
|
||||
Submodule APP_Framework/lib/lorawan/lora_radio_driver updated: a94c007cb4...2d8abd3eff
Submodule APP_Framework/lib/lorawan/lorawan_devicenode updated: 254754bc7d...197c320b99
Submodule APP_Framework/lib/lorawan/lorawan_gateway_single_channel updated: ac1c6516ec...6ec2a3d264
@@ -866,5 +866,7 @@ menu "lib using LVGL"
|
||||
|
||||
endmenu
|
||||
endif
|
||||
|
||||
source "$APP_DIR/lib/lvgl/examples/Kconfig"
|
||||
|
||||
endmenu
|
||||
|
||||
8
APP_Framework/lib/lvgl/examples/Kconfig
Normal file
8
APP_Framework/lib/lvgl/examples/Kconfig
Normal file
@@ -0,0 +1,8 @@
|
||||
menu "lvgl image display parameter settings"
|
||||
menuconfig LVGL_WIDTH
|
||||
int "Set the width of the image."
|
||||
default 320
|
||||
menuconfig LVGL_HEIGHT
|
||||
int "set the height of the image."
|
||||
default 320
|
||||
endmenu
|
||||
1327
APP_Framework/lib/lvgl/examples/assets/xiuos.c
Normal file
1327
APP_Framework/lib/lvgl/examples/assets/xiuos.c
Normal file
File diff suppressed because one or more lines are too long
BIN
APP_Framework/lib/lvgl/examples/assets/xiuos.png
Normal file
BIN
APP_Framework/lib/lvgl/examples/assets/xiuos.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 26 KiB |
@@ -1,14 +1,15 @@
|
||||
#include "../../lv_examples.h"
|
||||
#include <transform.h>
|
||||
#if LV_USE_IMG && LV_BUILD_EXAMPLES
|
||||
|
||||
|
||||
void lv_example_aoteman(void)
|
||||
void lv_example_show(void)
|
||||
{
|
||||
LV_IMG_DECLARE(aoteman);
|
||||
LV_IMG_DECLARE(xiuos);
|
||||
lv_obj_t * img1 = lv_img_create(lv_scr_act());
|
||||
lv_img_set_src(img1, &aoteman);
|
||||
lv_img_set_src(img1, &xiuos);
|
||||
lv_obj_align(img1, LV_ALIGN_CENTER, 0, -20);
|
||||
lv_obj_set_size(img1, 320, 240);
|
||||
lv_obj_set_size(img1, LVGL_WIDTH, LVGL_HEIGHT);
|
||||
|
||||
lv_obj_t * img2 = lv_img_create(lv_scr_act());
|
||||
lv_img_set_src(img2, LV_SYMBOL_OK "Accept");
|
||||
33
APP_Framework/lib/mqtt/Kconfig
Normal file
33
APP_Framework/lib/mqtt/Kconfig
Normal file
@@ -0,0 +1,33 @@
|
||||
menu "lib using MQTT"
|
||||
|
||||
menuconfig TOOL_USING_MQTT
|
||||
bool "Enable support MQTT function"
|
||||
default n
|
||||
select SUPPORT_CONNECTION_FRAMEWORK
|
||||
select CONNECTION_ADAPTER_4G
|
||||
|
||||
if TOOL_USING_MQTT
|
||||
menu "MQTT connection parameter configuration."
|
||||
config PLATFORM_PRODUCTKEY
|
||||
string "Product Key, used to identify a product."
|
||||
default "iv74vebCdJC"
|
||||
|
||||
config CLIENT_DEVICENAME
|
||||
string "Device name, used to identify a client device."
|
||||
default "D001"
|
||||
|
||||
config CLIENT_DEVICESECRET
|
||||
string "Device secret, used for device authentication and data encryption."
|
||||
default "d2e613c4f714b6b0774bd7b68eeceae3"
|
||||
|
||||
config PLATFORM_SERVERIP
|
||||
string "mqtt platform server ip."
|
||||
default "101.133.196.127"
|
||||
|
||||
config PLATFORM_SERVERPORT
|
||||
string "mqtt platform server port."
|
||||
default "1883"
|
||||
endmenu
|
||||
endif
|
||||
|
||||
endmenu
|
||||
3
APP_Framework/lib/mqtt/Makefile
Normal file
3
APP_Framework/lib/mqtt/Makefile
Normal file
@@ -0,0 +1,3 @@
|
||||
SRC_FILES := platform_mqtt.c utils_hmacsha1.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
438
APP_Framework/lib/mqtt/platform_mqtt.c
Normal file
438
APP_Framework/lib/mqtt/platform_mqtt.c
Normal file
@@ -0,0 +1,438 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: platform_mqtt.c
|
||||
* @brief: platform_mqtt.c file
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/7/27
|
||||
*
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <adapter.h>
|
||||
#include <transform.h>
|
||||
#include "platform_mqtt.h"
|
||||
|
||||
MQTT_TCB Platform_mqtt; //创建一个用于连接云平台mqtt的结构体
|
||||
static struct Adapter *adapter;
|
||||
static const uint8_t parket_connetAck[] = {0x20,0x02,0x00,0x00}; //连接成功服务器回应报文
|
||||
static const uint8_t parket_disconnet[] = {0xE0,0x00}; //客户端主动断开连接发送报文
|
||||
static const uint8_t parket_heart[] = {0xC0,0x00}; //客户端发送保活心跳包
|
||||
static const uint8_t parket_subAck[] = {0x90,0x03,0x00,0x0A,0x01}; //订阅成功服务器回应报文
|
||||
static const uint8_t parket_unsubAck[] = {0xB0,0x02,0x00,0x0A}; //取消订阅成功服务器回应报文
|
||||
static uint8_t mqtt_rxbuf[16];
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: AdapterNetActive
|
||||
* 功能描述: 使能设备的网络模块连接,连接TCP服务器并进入透传模式,当前使用4G方式
|
||||
* 形 参: 无
|
||||
* 返 回 值: 0表示成功,其他值表示失败
|
||||
*******************************************************************************/
|
||||
int AdapterNetActive(void)
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t baud_rate = BAUD_RATE_115200;
|
||||
adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME);
|
||||
adapter->socket.socket_id = 0;
|
||||
|
||||
ret = AdapterDeviceOpen(adapter);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = AdapterDeviceConnect(adapter, CLIENT, PLATFORM_SERVERIP, PLATFORM_SERVERPORT, IPV4);
|
||||
if (ret < 0)
|
||||
{
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
if (ret < 0)
|
||||
{
|
||||
AdapterDeviceClose(adapter);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_Send
|
||||
* 功能描述: MQTT client数据发送函数
|
||||
* 形 参: buf:要发送的数据,buflen:要发送的数据长度
|
||||
* 返 回 值: 发送成功为0,发送失败为-1
|
||||
*******************************************************************************/
|
||||
int MQTT_Send(const uint8_t* buf, int buflen)
|
||||
{
|
||||
return AdapterDeviceSend(adapter, buf, buflen) ;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_Recv
|
||||
* 功能描述: MQTT client数据接收函数
|
||||
* 形 参: buf:数据缓冲区,buflen:期望接收的数据长度
|
||||
* 返 回 值: 实际接收到的数据长度,接收失败为-1
|
||||
*******************************************************************************/
|
||||
int MQTT_Recv(uint8_t* buf, int buflen)
|
||||
{
|
||||
return AdapterDeviceRecv(adapter, buf, buflen) ;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_Connect
|
||||
* 功能描述: 登录MQTT服务器
|
||||
* 形 参: 无
|
||||
* 返 回 值: 0表示成功,1表示失败
|
||||
*******************************************************************************/
|
||||
int MQTT_Connect(void)
|
||||
{
|
||||
uint8_t TryConnect_time = 10; //尝试登录次数
|
||||
uint8_t passwdtemp[PASSWARD_SIZE];
|
||||
|
||||
memset(&Platform_mqtt,0,sizeof(Platform_mqtt));
|
||||
sprintf(Platform_mqtt.ClientID,"%s|securemode=3,signmethod=hmacsha1|",CLIENT_DEVICENAME); //构建客户端ID并存入缓冲区
|
||||
sprintf(Platform_mqtt.Username,"%s&%s",CLIENT_DEVICENAME,PLATFORM_PRODUCTKEY); //构建用户名并存入缓冲区
|
||||
memset(passwdtemp,0,sizeof(passwdtemp));
|
||||
sprintf(passwdtemp,"clientId%sdeviceName%sproductKey%s",CLIENT_DEVICENAME,CLIENT_DEVICENAME,PLATFORM_PRODUCTKEY); //构建加密时的明文
|
||||
utils_hmac_sha1(passwdtemp,strlen(passwdtemp),Platform_mqtt.Passward,(char *)CLIENT_DEVICESECRET,strlen(CLIENT_DEVICESECRET)); //以DeviceSecret为秘钥对temp中的明文进行hmacsha1加密即为密码
|
||||
|
||||
Platform_mqtt.MessageID = 0; //报文标识符清零,CONNECT报文虽然不需要添加报文标识符,但是CONNECT报文是第一个发送的报文,在此清零报文标识符为后续报文做准备
|
||||
Platform_mqtt.Fixed_len = 1; //CONNECT报文固定报头长度暂定为1
|
||||
Platform_mqtt.Variable_len = 10; //CONNECT报文可变报头长度为10
|
||||
Platform_mqtt.Payload_len = (2+strlen(Platform_mqtt.ClientID)) + (2+strlen(Platform_mqtt.Username)) + (2+strlen(Platform_mqtt.Passward)); //CONNECT报文中负载长度
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Variable_len + Platform_mqtt.Payload_len; //剩余长度=可变报头长度+负载长度
|
||||
memset(Platform_mqtt.Pack_buff,0,sizeof(Platform_mqtt.Pack_buff));
|
||||
|
||||
Platform_mqtt.Pack_buff[0] = 0x10; //CONNECT报文 固定报头第1个字节0x10
|
||||
do{
|
||||
if((Platform_mqtt.Remaining_len/128) == 0)
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = Platform_mqtt.Remaining_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = (Platform_mqtt.Remaining_len%128)|0x80;
|
||||
}
|
||||
Platform_mqtt.Fixed_len++;
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Remaining_len/128;
|
||||
}while(Platform_mqtt.Remaining_len);
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+0] = 0x00; //CONNECT报文,可变报头第1个字节:固定0x00
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+1] = 0x04; //CONNECT报文,可变报头第2个字节:固定0x04
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2] = 0x4D; //CONNECT报文,可变报头第3个字节:固定0x4D,大写字母M
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+3] = 0x51; //CONNECT报文,可变报头第4个字节:固定0x51,大写字母Q
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+4] = 0x54; //CONNECT报文,可变报头第5个字节:固定0x54,大写字母T
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+5] = 0x54; //CONNECT报文,可变报头第6个字节:固定0x54,大写字母T
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+6] = 0x04; //CONNECT报文,可变报头第7个字节:固定0x04
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+7] = 0xC2; //CONNECT报文,可变报头第8个字节:使能用户名和密码校验,不使用遗嘱功能,不保留会话功能
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+8] = KEEPALIVE_TIME/256; //CONNECT报文,可变报头第9个字节:保活时间高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+9] = KEEPALIVE_TIME%256; //CONNECT报文,可变报头第10个字节:保活时间低字节,单位s
|
||||
|
||||
/* CLIENT_ID */
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+10] = strlen(Platform_mqtt.ClientID)/256; //客户端ID长度高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+11] = strlen(Platform_mqtt.ClientID)%256; //客户端ID长度低字节
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+12],Platform_mqtt.ClientID,strlen(Platform_mqtt.ClientID)); //复制过来客户端ID字串
|
||||
/* USER_NAME */
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+12+strlen(Platform_mqtt.ClientID)] = strlen(Platform_mqtt.Username)/256; //用户名长度高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+13+strlen(Platform_mqtt.ClientID)] = strlen(Platform_mqtt.Username)%256; //用户名长度低字节
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+14+strlen(Platform_mqtt.ClientID)],Platform_mqtt.Username,strlen(Platform_mqtt.Username)); //复制过来用户名字串
|
||||
/* PASSWARD */
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+14+strlen(Platform_mqtt.ClientID)+strlen(Platform_mqtt.Username)] = strlen(Platform_mqtt.Passward)/256; //密码长度高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+15+strlen(Platform_mqtt.ClientID)+strlen(Platform_mqtt.Username)] = strlen(Platform_mqtt.Passward)%256; //密码长度低字节
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+16+strlen(Platform_mqtt.ClientID)+strlen(Platform_mqtt.Username)],Platform_mqtt.Passward,strlen(Platform_mqtt.Passward)); //复制过来密码字串
|
||||
|
||||
while(TryConnect_time > 0)
|
||||
{
|
||||
memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf));
|
||||
MQTT_Send(Platform_mqtt.Pack_buff,Platform_mqtt.Fixed_len + Platform_mqtt.Variable_len + Platform_mqtt.Payload_len);
|
||||
MdelayKTask(50);
|
||||
MQTT_Recv(mqtt_rxbuf, 4);
|
||||
if(mqtt_rxbuf[0] == parket_connetAck[0] && mqtt_rxbuf[1] == parket_connetAck[1]) //连接成功
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
TryConnect_time--;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_Disconnect
|
||||
* 功能描述: 断开与MQTT服务器的连接
|
||||
* 形 参: 无
|
||||
* 返 回 值: 无
|
||||
*******************************************************************************/
|
||||
void MQTT_Disconnect(void)
|
||||
{
|
||||
while(MQTT_Send(parket_disconnet,sizeof(parket_disconnet)) < 0);
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_SubscribeTopic
|
||||
* 功能描述: MQTT订阅单个主题
|
||||
* 形 参: topic_name:要订阅的主题
|
||||
* 返 回 值: 0表示订阅成功,1表示订阅失败
|
||||
*******************************************************************************/
|
||||
int MQTT_SubscribeTopic(uint8_t *topic_name)
|
||||
{
|
||||
uint8_t TrySub_time = 10; //尝试订阅次数
|
||||
|
||||
Platform_mqtt.Fixed_len = 1; //SUBSCRIBE报文,固定报头长度暂定为1
|
||||
Platform_mqtt.Variable_len = 2;//SUBSCRIBE报文,可变报头长度=2,2为字节报文标识符
|
||||
Platform_mqtt.Payload_len = 0; //SUBSCRIBE报文,负载数据长度暂定为0
|
||||
|
||||
Platform_mqtt.Payload_len = strlen(topic_name) + 2 + 1; //每个需要订阅的topic除了本身的字符串长度,还包含表示topic字符串长度的2字节,以及订阅等级1字节
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Variable_len + Platform_mqtt.Payload_len; //计算剩余长度=可变报头长度+负载长度
|
||||
memset(Platform_mqtt.Pack_buff,0,sizeof(Platform_mqtt.Pack_buff));
|
||||
|
||||
Platform_mqtt.Pack_buff[0]=0x82; //SUBSCRIBE报文,固定报头第1个字节0x82
|
||||
do{
|
||||
if((Platform_mqtt.Remaining_len/128) == 0)
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = Platform_mqtt.Remaining_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = (Platform_mqtt.Remaining_len%128)|0x80;
|
||||
}
|
||||
Platform_mqtt.Fixed_len++;
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Remaining_len/128;
|
||||
}while(Platform_mqtt.Remaining_len);
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+0] = Platform_mqtt.MessageID/256; //报文标识符高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+1] = Platform_mqtt.MessageID%256; //报文标识符低字节
|
||||
Platform_mqtt.MessageID++; //每用一次MessageID加1
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2] = strlen(topic_name)/256; //主题长度高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+3] = strlen(topic_name)%256; //主题长度低字节
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+4],topic_name,strlen(topic_name)); //复制主题字串
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+4+strlen(topic_name)] = 0; //QOS等级设置为0
|
||||
|
||||
while(TrySub_time > 0)
|
||||
{
|
||||
memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf));
|
||||
MQTT_Send(Platform_mqtt.Pack_buff,Platform_mqtt.Fixed_len + Platform_mqtt.Variable_len + Platform_mqtt.Payload_len);
|
||||
MdelayKTask(50);
|
||||
MQTT_Recv(mqtt_rxbuf, 5);
|
||||
if(mqtt_rxbuf[0] == parket_subAck[0] && mqtt_rxbuf[1] == parket_subAck[1]) //订阅成功
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
TrySub_time--;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_UnSubscribeTopic
|
||||
* 功能描述: MQTT取消订阅单个主题
|
||||
* 形 参: topic_name:要取消订阅的主题
|
||||
* 返 回 值: 0表示订阅成功,1表示订阅失败
|
||||
*******************************************************************************/
|
||||
int MQTT_UnSubscribeTopic(uint8_t *topic_name)
|
||||
{
|
||||
uint8_t TryUnSub_time = 10; //尝试取消订阅次数
|
||||
|
||||
Platform_mqtt.Fixed_len = 1; //UNSUBSCRIBE报文,固定报头长度暂定为1
|
||||
Platform_mqtt.Variable_len = 2; //UNSUBSCRIBE报文,可变报头长度=2,2为字节报文标识符
|
||||
Platform_mqtt.Payload_len = strlen(topic_name) + 2; //每个需要取消的订阅topic除了本身的字符串长度,还包含表示topic字符串长度的2字节
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Variable_len + Platform_mqtt.Payload_len; //计算剩余长度=可变报头长度+负载长度
|
||||
memset(Platform_mqtt.Pack_buff,0,sizeof(Platform_mqtt.Pack_buff));
|
||||
|
||||
Platform_mqtt.Pack_buff[0]=0xA0; //UNSUBSCRIBE报文,固定报头第1个字节0xA0
|
||||
do{
|
||||
if((Platform_mqtt.Remaining_len/128) == 0)
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = Platform_mqtt.Remaining_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = (Platform_mqtt.Remaining_len%128)|0x80;
|
||||
}
|
||||
Platform_mqtt.Fixed_len++;
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Remaining_len/128;
|
||||
}while(Platform_mqtt.Remaining_len);
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+0] = Platform_mqtt.MessageID/256; //报文标识符高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+1] = Platform_mqtt.MessageID%256; //报文标识符低字节
|
||||
Platform_mqtt.MessageID++; //每用一次MessageID加1
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2] = strlen(topic_name)/256; //主题长度高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+3] = strlen(topic_name)%256; //主题长度低字节
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+4],topic_name,strlen(topic_name)); //复制主题字串
|
||||
|
||||
while(TryUnSub_time > 0)
|
||||
{
|
||||
memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf));
|
||||
MQTT_Send(Platform_mqtt.Pack_buff,Platform_mqtt.Fixed_len + Platform_mqtt.Variable_len + Platform_mqtt.Payload_len);
|
||||
MdelayKTask(50);
|
||||
MQTT_Recv(mqtt_rxbuf, 4);
|
||||
if(mqtt_rxbuf[0] == parket_unsubAck[0] && mqtt_rxbuf[1] == parket_unsubAck[1]) //取消订阅成功
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
TryUnSub_time--;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_PublishDataQs0
|
||||
* 功能描述: 向服务器发送等级0的Publish报文
|
||||
* 形 参: topic_name:主题名称
|
||||
data:数据缓存
|
||||
data_len:数据长度
|
||||
* 返 回 值: 发布Qs=0的消息服务器不返回确认消息
|
||||
*******************************************************************************/
|
||||
void MQTT_PublishDataQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len)
|
||||
{
|
||||
Platform_mqtt.Fixed_len = 1; //PUBLISH等级0报文固定报头长度暂定为1
|
||||
Platform_mqtt.Variable_len = 2 + strlen(topic_name); //PUBLISH等级0报文,可变报头长度=2字节topic长度标识字节+topic字符串的长度
|
||||
Platform_mqtt.Payload_len = data_len; //PUBLISH等级0报文,负载数据长度=data_len
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Variable_len + Platform_mqtt.Payload_len; //计算剩余长度=可变报头长度+负载长度
|
||||
memset(Platform_mqtt.Pack_buff,0,sizeof(Platform_mqtt.Pack_buff));
|
||||
|
||||
Platform_mqtt.Pack_buff[0]=0x30; //PUBLISH等级0报文固定报头第1个字节0x30
|
||||
do{
|
||||
if((Platform_mqtt.Remaining_len/128) == 0)
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = Platform_mqtt.Remaining_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = (Platform_mqtt.Remaining_len%128)|0x80;
|
||||
}
|
||||
Platform_mqtt.Fixed_len++;
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Remaining_len/128;
|
||||
}while(Platform_mqtt.Remaining_len);
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+0]=strlen(topic_name)/256; //主题长度高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+1]=strlen(topic_name)%256; //主题长度低字节
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2],topic_name,strlen(topic_name)); //复制主题字串
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2+strlen(topic_name)],data,data_len); //复制data数据
|
||||
|
||||
MQTT_Send(Platform_mqtt.Pack_buff, Platform_mqtt.Fixed_len + Platform_mqtt.Variable_len + Platform_mqtt.Payload_len);
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_PublishDataQs1
|
||||
* 功能描述: 向服务器发送等级1的Publish报文
|
||||
* 形 参: topic_name:主题名称
|
||||
data:数据缓存
|
||||
data_len:数据长度
|
||||
* 返 回 值: 无
|
||||
*******************************************************************************/
|
||||
void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len)
|
||||
{
|
||||
Platform_mqtt.Fixed_len = 1; //PUBLISH等级1报文固定报头长度暂定为1
|
||||
Platform_mqtt.Variable_len = 2 + 2 + strlen(topic_name); //PUBLISH等级1报文,可变报头长度=2字节消息标识符+2字节topic长度标识字节+topic字符串的长度
|
||||
Platform_mqtt.Payload_len = data_len; //PUBLISH等级1报文,负载数据长度=data_len
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Variable_len + Platform_mqtt.Payload_len; //计算剩余长度=可变报头长度+负载长度
|
||||
|
||||
Platform_mqtt.Pack_buff[0] = 0x32; //等级1的Publish报文固定报头第1个字节,0x32
|
||||
do{
|
||||
if(Platform_mqtt.Remaining_len/128 == 0)
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = Platform_mqtt.Remaining_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len] = (Platform_mqtt.Remaining_len%128)|0x80;
|
||||
}
|
||||
Platform_mqtt.Fixed_len++;
|
||||
Platform_mqtt.Remaining_len = Platform_mqtt.Remaining_len/128;
|
||||
}while(Platform_mqtt.Remaining_len);
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+0] = strlen(topic_name)/256; //主题长度高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+1] = strlen(topic_name)%256; //主题长度低字节
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2],topic_name,strlen(topic_name)); //复制主题字串
|
||||
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2+strlen(topic_name)] = Platform_mqtt.MessageID/256; //报文标识符高字节
|
||||
Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+3+strlen(topic_name)] = Platform_mqtt.MessageID%256; //报文标识符低字节
|
||||
Platform_mqtt.MessageID++; //每用一次MessageID加1
|
||||
|
||||
memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+4+strlen(topic_name)],data,strlen(data)); //复制data数据
|
||||
|
||||
MQTT_Send(Platform_mqtt.Pack_buff,Platform_mqtt.Fixed_len + Platform_mqtt.Variable_len + Platform_mqtt.Payload_len);
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_SendHeart
|
||||
* 功能描述: 发送心跳保活包
|
||||
* 形 参: 无
|
||||
* 返 回 值: 0表示发送成功,其他值表示发送失败
|
||||
*******************************************************************************/
|
||||
int MQTT_SendHeart(void)
|
||||
{
|
||||
uint8_t TrySentHeart_time = 10; //尝试发送心跳保活次数
|
||||
while(TrySentHeart_time > 0)
|
||||
{
|
||||
memset(mqtt_rxbuf,0,sizeof(mqtt_rxbuf));
|
||||
MQTT_Send(parket_heart,sizeof(parket_heart));
|
||||
MdelayKTask(50);
|
||||
MQTT_Recv(mqtt_rxbuf, 2);
|
||||
if(mqtt_rxbuf[0] == 0xD0 && mqtt_rxbuf[1] == 0x00)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
TrySentHeart_time--;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
* 函 数 名: MQTT_DealPublishData
|
||||
* 功能描述: 处理服务器发来的等级0的推送数据,附带topic信息
|
||||
* 形 参: redata:接收的数据,data_len:要处理的数据长度
|
||||
* 返 回 值: 无
|
||||
*******************************************************************************/
|
||||
void MQTT_DealPublishData(uint8_t *data, uint16_t data_len)
|
||||
{
|
||||
uint8_t i;
|
||||
uint16_t cmdpos,cmdlen;
|
||||
|
||||
for(i = 1;i < 5;i++)
|
||||
{
|
||||
if((data[i] & 0x80) == 0)
|
||||
break;
|
||||
}
|
||||
|
||||
cmdpos = 1+i+2;
|
||||
cmdlen = data_len-(1+i+2);
|
||||
|
||||
if(data_len <= CMD_SIZE)
|
||||
{
|
||||
memset(Platform_mqtt.cmdbuff, 0, CMD_SIZE);
|
||||
memcpy(Platform_mqtt.cmdbuff, &data[cmdpos], cmdlen);
|
||||
}
|
||||
}
|
||||
62
APP_Framework/lib/mqtt/platform_mqtt.h
Normal file
62
APP_Framework/lib/mqtt/platform_mqtt.h
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: platform_mqtt.h
|
||||
* @brief: platform_mqtt.h file
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/7/27
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _PLATFORM_MQTT_H_
|
||||
#define _PLATFORM_MQTT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "utils_hmacsha1.h"
|
||||
|
||||
#define KEEPALIVE_TIME 300 //保活时间(单位s),300s
|
||||
#define HEART_TIME 120000 //空闲时发送心跳包的时间间隔(单位ms),120s
|
||||
#define PACK_SIZE 512 //存放报文数据缓冲区大小
|
||||
#define CMD_SIZE 3072 //保存推送的PUBLISH报文中的数据缓冲区大小
|
||||
#define CLIENTID_SIZE 64 //存放客户端ID的缓冲区大小
|
||||
#define USERNAME_SIZE 64 //存放用户名的缓冲区大小
|
||||
#define PASSWARD_SIZE 64 //存放密码的缓冲区大小
|
||||
|
||||
typedef struct{
|
||||
uint8_t ClientID[CLIENTID_SIZE]; //存放客户端ID的缓冲区
|
||||
uint8_t Username[USERNAME_SIZE]; //存放用户名的缓冲区
|
||||
uint8_t Passward[PASSWARD_SIZE]; //存放密码的缓冲区
|
||||
uint8_t Pack_buff[PACK_SIZE]; //存放发送报文数据缓冲区
|
||||
uint16_t MessageID; //记录报文标识符
|
||||
uint16_t Fixed_len; //固定报头长度
|
||||
uint16_t Variable_len; //可变报头长度
|
||||
uint16_t Payload_len; //有效负荷长度
|
||||
uint16_t Remaining_len; //保存报文剩余长度字节
|
||||
uint8_t cmdbuff[CMD_SIZE]; //保存推送的PUBLISH报文中的数据缓冲区
|
||||
}MQTT_TCB;
|
||||
|
||||
extern MQTT_TCB Platform_mqtt; //外部变量声明
|
||||
|
||||
int AdapterNetActive(void);
|
||||
int MQTT_Send(const uint8_t* buf, int buflen);
|
||||
int MQTT_Recv(uint8_t* buf, int buflen);
|
||||
int MQTT_Connect(void);
|
||||
void MQTT_Disconnect(void);
|
||||
int MQTT_SubscribeTopic(uint8_t *topic_name);
|
||||
int MQTT_UnSubscribeTopic(uint8_t *topic_name);
|
||||
void MQTT_PublishDataQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len);
|
||||
void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len);
|
||||
int MQTT_SendHeart(void);
|
||||
void MQTT_DealPublishData(uint8_t *data, uint16_t data_len);
|
||||
#endif
|
||||
399
APP_Framework/lib/mqtt/utils_hmacsha1.c
Normal file
399
APP_Framework/lib/mqtt/utils_hmacsha1.c
Normal file
@@ -0,0 +1,399 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: utils_hmacsha1.c
|
||||
* @brief: utils_hmacsha1.c file
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/7/27
|
||||
*
|
||||
*/
|
||||
|
||||
#include "utils_hmacsha1.h"
|
||||
|
||||
#define KEY_IOPAD_SIZE 64
|
||||
#define SHA1_DIGEST_SIZE 20
|
||||
|
||||
static void utils_sha1_zeroize(void *v, size_t n);
|
||||
static void utils_sha1_init(iot_sha1_context *ctx);
|
||||
static void utils_sha1_free(iot_sha1_context *ctx);
|
||||
static void utils_sha1_clone(iot_sha1_context *dst, const iot_sha1_context *src);
|
||||
static void utils_sha1_starts(iot_sha1_context *ctx);
|
||||
static void utils_sha1_process(iot_sha1_context *ctx, const unsigned char data[64]);
|
||||
static void utils_sha1_update(iot_sha1_context *ctx, const unsigned char *input, size_t ilen);
|
||||
static void utils_sha1_finish(iot_sha1_context *ctx, unsigned char output[20]);
|
||||
static void utils_sha1(const unsigned char *input, size_t ilen, unsigned char output[20]);
|
||||
static int8_t utils_hb2hex(uint8_t hb);
|
||||
|
||||
const char * base64char = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
/* Implementation that should never be optimized out by the compiler */
|
||||
static void utils_sha1_zeroize(void *v, size_t n)
|
||||
{
|
||||
volatile unsigned char *p = v;
|
||||
while(n--) {
|
||||
*p++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* 32-bit integer manipulation macros (big endian) */
|
||||
#ifndef IOT_SHA1_GET_UINT32_BE
|
||||
#define IOT_SHA1_GET_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(n) = ( (uint32_t) (b)[(i) ] << 24 ) \
|
||||
| ( (uint32_t) (b)[(i) + 1] << 16 ) \
|
||||
| ( (uint32_t) (b)[(i) + 2] << 8 ) \
|
||||
| ( (uint32_t) (b)[(i) + 3] ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef IOT_SHA1_PUT_UINT32_BE
|
||||
#define IOT_SHA1_PUT_UINT32_BE(n,b,i) \
|
||||
{ \
|
||||
(b)[(i) ] = (unsigned char) ( (n) >> 24 ); \
|
||||
(b)[(i) + 1] = (unsigned char) ( (n) >> 16 ); \
|
||||
(b)[(i) + 2] = (unsigned char) ( (n) >> 8 ); \
|
||||
(b)[(i) + 3] = (unsigned char) ( (n) ); \
|
||||
}
|
||||
#endif
|
||||
|
||||
void utils_sha1_init(iot_sha1_context *ctx)
|
||||
{
|
||||
memset(ctx, 0, sizeof(iot_sha1_context));
|
||||
}
|
||||
|
||||
void utils_sha1_free(iot_sha1_context *ctx)
|
||||
{
|
||||
if(ctx == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
utils_sha1_zeroize(ctx, sizeof(iot_sha1_context));
|
||||
}
|
||||
|
||||
void utils_sha1_clone(iot_sha1_context *dst,
|
||||
const iot_sha1_context *src)
|
||||
{
|
||||
*dst = *src;
|
||||
}
|
||||
|
||||
/* SHA-1 context setup */
|
||||
void utils_sha1_starts(iot_sha1_context *ctx)
|
||||
{
|
||||
ctx->total[0] = 0;
|
||||
ctx->total[1] = 0;
|
||||
|
||||
ctx->state[0] = 0x67452301;
|
||||
ctx->state[1] = 0xEFCDAB89;
|
||||
ctx->state[2] = 0x98BADCFE;
|
||||
ctx->state[3] = 0x10325476;
|
||||
ctx->state[4] = 0xC3D2E1F0;
|
||||
}
|
||||
|
||||
void utils_sha1_process(iot_sha1_context *ctx, const unsigned char data[64])
|
||||
{
|
||||
uint32_t temp, W[16], A, B, C, D, E;
|
||||
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 0], data, 0);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 1], data, 4);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 2], data, 8);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 3], data, 12);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 4], data, 16);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 5], data, 20);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 6], data, 24);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 7], data, 28);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 8], data, 32);
|
||||
IOT_SHA1_GET_UINT32_BE(W[ 9], data, 36);
|
||||
IOT_SHA1_GET_UINT32_BE(W[10], data, 40);
|
||||
IOT_SHA1_GET_UINT32_BE(W[11], data, 44);
|
||||
IOT_SHA1_GET_UINT32_BE(W[12], data, 48);
|
||||
IOT_SHA1_GET_UINT32_BE(W[13], data, 52);
|
||||
IOT_SHA1_GET_UINT32_BE(W[14], data, 56);
|
||||
IOT_SHA1_GET_UINT32_BE(W[15], data, 60);
|
||||
|
||||
#define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
|
||||
|
||||
#define R(t) \
|
||||
( \
|
||||
temp = W[( t - 3 ) & 0x0F] ^ W[( t - 8 ) & 0x0F] ^ \
|
||||
W[( t - 14 ) & 0x0F] ^ W[ t & 0x0F], \
|
||||
( W[t & 0x0F] = S(temp,1) ) \
|
||||
)
|
||||
|
||||
#define P(a,b,c,d,e,x) \
|
||||
{ \
|
||||
e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
|
||||
}
|
||||
|
||||
A = ctx->state[0];
|
||||
B = ctx->state[1];
|
||||
C = ctx->state[2];
|
||||
D = ctx->state[3];
|
||||
E = ctx->state[4];
|
||||
|
||||
#define F(x,y,z) (z ^ (x & (y ^ z)))
|
||||
#define K 0x5A827999
|
||||
|
||||
P(A, B, C, D, E, W[0]);
|
||||
P(E, A, B, C, D, W[1]);
|
||||
P(D, E, A, B, C, W[2]);
|
||||
P(C, D, E, A, B, W[3]);
|
||||
P(B, C, D, E, A, W[4]);
|
||||
P(A, B, C, D, E, W[5]);
|
||||
P(E, A, B, C, D, W[6]);
|
||||
P(D, E, A, B, C, W[7]);
|
||||
P(C, D, E, A, B, W[8]);
|
||||
P(B, C, D, E, A, W[9]);
|
||||
P(A, B, C, D, E, W[10]);
|
||||
P(E, A, B, C, D, W[11]);
|
||||
P(D, E, A, B, C, W[12]);
|
||||
P(C, D, E, A, B, W[13]);
|
||||
P(B, C, D, E, A, W[14]);
|
||||
P(A, B, C, D, E, W[15]);
|
||||
P(E, A, B, C, D, R(16));
|
||||
P(D, E, A, B, C, R(17));
|
||||
P(C, D, E, A, B, R(18));
|
||||
P(B, C, D, E, A, R(19));
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
#define K 0x6ED9EBA1
|
||||
|
||||
P(A, B, C, D, E, R(20));
|
||||
P(E, A, B, C, D, R(21));
|
||||
P(D, E, A, B, C, R(22));
|
||||
P(C, D, E, A, B, R(23));
|
||||
P(B, C, D, E, A, R(24));
|
||||
P(A, B, C, D, E, R(25));
|
||||
P(E, A, B, C, D, R(26));
|
||||
P(D, E, A, B, C, R(27));
|
||||
P(C, D, E, A, B, R(28));
|
||||
P(B, C, D, E, A, R(29));
|
||||
P(A, B, C, D, E, R(30));
|
||||
P(E, A, B, C, D, R(31));
|
||||
P(D, E, A, B, C, R(32));
|
||||
P(C, D, E, A, B, R(33));
|
||||
P(B, C, D, E, A, R(34));
|
||||
P(A, B, C, D, E, R(35));
|
||||
P(E, A, B, C, D, R(36));
|
||||
P(D, E, A, B, C, R(37));
|
||||
P(C, D, E, A, B, R(38));
|
||||
P(B, C, D, E, A, R(39));
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) ((x & y) | (z & (x | y)))
|
||||
#define K 0x8F1BBCDC
|
||||
|
||||
P(A, B, C, D, E, R(40));
|
||||
P(E, A, B, C, D, R(41));
|
||||
P(D, E, A, B, C, R(42));
|
||||
P(C, D, E, A, B, R(43));
|
||||
P(B, C, D, E, A, R(44));
|
||||
P(A, B, C, D, E, R(45));
|
||||
P(E, A, B, C, D, R(46));
|
||||
P(D, E, A, B, C, R(47));
|
||||
P(C, D, E, A, B, R(48));
|
||||
P(B, C, D, E, A, R(49));
|
||||
P(A, B, C, D, E, R(50));
|
||||
P(E, A, B, C, D, R(51));
|
||||
P(D, E, A, B, C, R(52));
|
||||
P(C, D, E, A, B, R(53));
|
||||
P(B, C, D, E, A, R(54));
|
||||
P(A, B, C, D, E, R(55));
|
||||
P(E, A, B, C, D, R(56));
|
||||
P(D, E, A, B, C, R(57));
|
||||
P(C, D, E, A, B, R(58));
|
||||
P(B, C, D, E, A, R(59));
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
#define F(x,y,z) (x ^ y ^ z)
|
||||
#define K 0xCA62C1D6
|
||||
|
||||
P(A, B, C, D, E, R(60));
|
||||
P(E, A, B, C, D, R(61));
|
||||
P(D, E, A, B, C, R(62));
|
||||
P(C, D, E, A, B, R(63));
|
||||
P(B, C, D, E, A, R(64));
|
||||
P(A, B, C, D, E, R(65));
|
||||
P(E, A, B, C, D, R(66));
|
||||
P(D, E, A, B, C, R(67));
|
||||
P(C, D, E, A, B, R(68));
|
||||
P(B, C, D, E, A, R(69));
|
||||
P(A, B, C, D, E, R(70));
|
||||
P(E, A, B, C, D, R(71));
|
||||
P(D, E, A, B, C, R(72));
|
||||
P(C, D, E, A, B, R(73));
|
||||
P(B, C, D, E, A, R(74));
|
||||
P(A, B, C, D, E, R(75));
|
||||
P(E, A, B, C, D, R(76));
|
||||
P(D, E, A, B, C, R(77));
|
||||
P(C, D, E, A, B, R(78));
|
||||
P(B, C, D, E, A, R(79));
|
||||
|
||||
#undef K
|
||||
#undef F
|
||||
|
||||
ctx->state[0] += A;
|
||||
ctx->state[1] += B;
|
||||
ctx->state[2] += C;
|
||||
ctx->state[3] += D;
|
||||
ctx->state[4] += E;
|
||||
}
|
||||
|
||||
/* SHA-1 process buffer */
|
||||
void utils_sha1_update(iot_sha1_context *ctx, const unsigned char *input, size_t ilen)
|
||||
{
|
||||
size_t fill;
|
||||
uint32_t left;
|
||||
|
||||
if(ilen == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
left = ctx->total[0] & 0x3F;
|
||||
fill = 64 - left;
|
||||
|
||||
ctx->total[0] += (uint32_t) ilen;
|
||||
ctx->total[0] &= 0xFFFFFFFF;
|
||||
|
||||
if(ctx->total[0] < (uint32_t) ilen) {
|
||||
ctx->total[1]++;
|
||||
}
|
||||
|
||||
if(left && ilen >= fill) {
|
||||
memcpy((void *)(ctx->buffer + left), input, fill);
|
||||
utils_sha1_process(ctx, ctx->buffer);
|
||||
input += fill;
|
||||
ilen -= fill;
|
||||
left = 0;
|
||||
}
|
||||
|
||||
while(ilen >= 64) {
|
||||
utils_sha1_process(ctx, input);
|
||||
input += 64;
|
||||
ilen -= 64;
|
||||
}
|
||||
|
||||
if(ilen > 0) {
|
||||
memcpy((void *)(ctx->buffer + left), input, ilen);
|
||||
}
|
||||
}
|
||||
|
||||
static const unsigned char iot_sha1_padding[64] = {
|
||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
/* SHA-1 final digest */
|
||||
void utils_sha1_finish(iot_sha1_context *ctx, unsigned char output[20])
|
||||
{
|
||||
uint32_t last, padn;
|
||||
uint32_t high, low;
|
||||
unsigned char msglen[8];
|
||||
|
||||
high = (ctx->total[0] >> 29)
|
||||
| (ctx->total[1] << 3);
|
||||
low = (ctx->total[0] << 3);
|
||||
|
||||
IOT_SHA1_PUT_UINT32_BE(high, msglen, 0);
|
||||
IOT_SHA1_PUT_UINT32_BE(low, msglen, 4);
|
||||
|
||||
last = ctx->total[0] & 0x3F;
|
||||
padn = (last < 56) ? (56 - last) : (120 - last);
|
||||
|
||||
utils_sha1_update(ctx, iot_sha1_padding, padn);
|
||||
utils_sha1_update(ctx, msglen, 8);
|
||||
|
||||
IOT_SHA1_PUT_UINT32_BE(ctx->state[0], output, 0);
|
||||
IOT_SHA1_PUT_UINT32_BE(ctx->state[1], output, 4);
|
||||
IOT_SHA1_PUT_UINT32_BE(ctx->state[2], output, 8);
|
||||
IOT_SHA1_PUT_UINT32_BE(ctx->state[3], output, 12);
|
||||
IOT_SHA1_PUT_UINT32_BE(ctx->state[4], output, 16);
|
||||
}
|
||||
|
||||
|
||||
/* output = SHA-1(input buffer) */
|
||||
void utils_sha1(const unsigned char *input, size_t ilen, unsigned char output[20])
|
||||
{
|
||||
iot_sha1_context ctx;
|
||||
|
||||
utils_sha1_init(&ctx);
|
||||
utils_sha1_starts(&ctx);
|
||||
utils_sha1_update(&ctx, input, ilen);
|
||||
utils_sha1_finish(&ctx, output);
|
||||
utils_sha1_free(&ctx);
|
||||
}
|
||||
|
||||
|
||||
inline int8_t utils_hb2hex(uint8_t hb)
|
||||
{
|
||||
hb = hb & 0xF;
|
||||
return (int8_t)(hb < 10 ? '0' + hb : hb - 10 + 'a');
|
||||
}
|
||||
|
||||
|
||||
void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len)
|
||||
{
|
||||
iot_sha1_context context;
|
||||
unsigned char k_ipad[KEY_IOPAD_SIZE]; /* inner padding - key XORd with ipad */
|
||||
unsigned char k_opad[KEY_IOPAD_SIZE]; /* outer padding - key XORd with opad */
|
||||
unsigned char out[SHA1_DIGEST_SIZE];
|
||||
int i;
|
||||
|
||||
if((NULL == msg) || (NULL == digest) || (NULL == key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if(key_len > KEY_IOPAD_SIZE) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* start out by storing key in pads */
|
||||
memset(k_ipad, 0, sizeof(k_ipad));
|
||||
memset(k_opad, 0, sizeof(k_opad));
|
||||
memcpy(k_ipad, key, key_len);
|
||||
memcpy(k_opad, key, key_len);
|
||||
|
||||
/* XOR key with ipad and opad values */
|
||||
for(i = 0; i < KEY_IOPAD_SIZE; i++) {
|
||||
k_ipad[i] ^= 0x36;
|
||||
k_opad[i] ^= 0x5c;
|
||||
}
|
||||
|
||||
/* perform inner SHA */
|
||||
utils_sha1_init(&context); /* init context for 1st pass */
|
||||
utils_sha1_starts(&context); /* setup context for 1st pass */
|
||||
utils_sha1_update(&context, k_ipad, KEY_IOPAD_SIZE); /* start with inner pad */
|
||||
utils_sha1_update(&context, (unsigned char *) msg, msg_len); /* then text of datagram */
|
||||
utils_sha1_finish(&context, out); /* finish up 1st pass */
|
||||
|
||||
/* perform outer SHA */
|
||||
utils_sha1_init(&context); /* init context for 2nd pass */
|
||||
utils_sha1_starts(&context); /* setup context for 2nd pass */
|
||||
utils_sha1_update(&context, k_opad, KEY_IOPAD_SIZE); /* start with outer pad */
|
||||
utils_sha1_update(&context, out, SHA1_DIGEST_SIZE); /* then results of 1st hash */
|
||||
utils_sha1_finish(&context, out); /* finish up 2nd pass */
|
||||
|
||||
for(i = 0; i < SHA1_DIGEST_SIZE; ++i) {
|
||||
digest[i * 2] = utils_hb2hex(out[i] >> 4);
|
||||
digest[i * 2 + 1] = utils_hb2hex(out[i]);
|
||||
}
|
||||
}
|
||||
41
APP_Framework/lib/mqtt/utils_hmacsha1.h
Normal file
41
APP_Framework/lib/mqtt/utils_hmacsha1.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: utils_hmacsha1.h
|
||||
* @brief: utils_hmacsha1.h file
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/7/27
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef UTILS_HMACSHA1_H_
|
||||
#define UTILS_HMACSHA1_H_
|
||||
|
||||
#include "stdio.h"
|
||||
#include "stdint.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
/* SHA-1 context structure */
|
||||
typedef struct {
|
||||
uint32_t total[2]; /* number of bytes processed */
|
||||
uint32_t state[5]; /* intermediate digest state */
|
||||
unsigned char buffer[64]; /* data block being processed */
|
||||
} iot_sha1_context;
|
||||
|
||||
|
||||
void utils_hmac_sha1(const char *msg, int msg_len, char *digest, const char *key, int key_len);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user