forked from xuos/xiuos
it is OK
This commit is contained in:
commit
3653ac690d
|
@ -11,6 +11,8 @@ menu "Applications"
|
|||
default 85 if KTASK_PRIORITY_256
|
||||
endmenu
|
||||
|
||||
source "$APP_DIR/Applications/ota/Kconfig"
|
||||
|
||||
source "$APP_DIR/Applications/app_test/Kconfig"
|
||||
source "$APP_DIR/Applications/connection_app/Kconfig"
|
||||
source "$APP_DIR/Applications/control_app/Kconfig"
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
SRC_DIR := general_functions
|
||||
SRC_DIR := general_functions app_test
|
||||
|
||||
SRC_FILES := main.c framework_init.c
|
||||
SRC_FILES := main.c framework_init.c
|
||||
|
||||
ifeq ($(CONFIG_APPLICATION_OTA),y)
|
||||
SRC_DIR += ota
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_APPLICATION_SENSOR),y)
|
||||
SRC_DIR += sensor_app
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
/*
|
||||
* 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 sd_card_mount.c
|
||||
* @brief Mount SD card when opened SDIO
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.19
|
||||
*/
|
||||
|
||||
#include "user_api/switch_api/user_api.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(FS_VFS)
|
||||
#include <iot-vfs.h>
|
||||
|
||||
/**
|
||||
* @description: Mount SD card
|
||||
* @return 0
|
||||
*/
|
||||
int MountSDCard(void)
|
||||
{
|
||||
if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, FSTYPE_FATFS, "/") == 0)
|
||||
DBG("sd card mount to '/'");
|
||||
else
|
||||
SYS_WARN("sd card mount to '/' failed!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),MountSDCard, MountSDCard, MountSDCard );
|
|
@ -1,46 +0,0 @@
|
|||
/*
|
||||
* 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 spi_sd_card_mount.c
|
||||
* @brief Mount SD card when opened SPI SD card
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.01
|
||||
*/
|
||||
|
||||
#include "user_api/switch_api/user_api.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(FS_VFS)
|
||||
#include <iot-vfs.h>
|
||||
|
||||
/**
|
||||
* @description: Mount SD card
|
||||
* @return 0
|
||||
*/
|
||||
int MountSDCard(void)
|
||||
{
|
||||
struct Bus *spi_bus;
|
||||
spi_bus = BusFind(SPI_BUS_NAME_1);
|
||||
|
||||
if (NONE == SpiSdInit(spi_bus, SPI_1_DEVICE_NAME_0, SPI_1_DRV_NAME, SPI_SD_NAME)) {
|
||||
KPrintf("MountSDCard SpiSdInit error!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (EOK == MountFilesystem(SPI_BUS_NAME_1, SPI_SD_NAME, SPI_1_DRV_NAME, FSTYPE_FATFS, "/"))
|
||||
KPrintf("SPI SD card fatfs mounted\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
|
@ -13,13 +13,17 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <user_api.h>
|
||||
#include <transform.h>
|
||||
|
||||
extern int FrameworkInit();
|
||||
|
||||
extern void ApplicationOtaTaskInit(void);
|
||||
int main(void)
|
||||
{
|
||||
printf("Hello, world!\n");
|
||||
FrameworkInit();
|
||||
#ifdef APPLICATION_OTA
|
||||
ApplicationOtaTaskInit();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
// int cppmain(void);
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
menu "ota app "
|
||||
menuconfig APPLICATION_OTA
|
||||
bool "Using app bin ota"
|
||||
default n
|
||||
|
||||
|
||||
endmenu
|
|
@ -0,0 +1,3 @@
|
|||
SRC_FILES := ota.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -0,0 +1,19 @@
|
|||
# OTA README
|
||||
|
||||
xiuos当前的ota功能允许应用bin文件可以通过4G实现远程的bin文件更新(限制:1、bin文件存放在设备SD卡并且应用从SD卡启动;2、暂且支持4G实现;3、暂时只支持aiit终端;4、只支持xiuos内核)。
|
||||
|
||||
## 文件说明
|
||||
|
||||
| 名称 | 说明 |
|
||||
| -- | -- |
|
||||
| ota.c| xiuos设备OTA代码 |
|
||||
| ota_server.c | pc服务端的实例代码供参考 |
|
||||
|
||||
|
||||
## 使用说明
|
||||
xiuos的应用bin文件更新依赖上层的adapter框架,因此需要在menuconfig同时配置以下选项:
|
||||
1、ota开关APPLICATION_OTA打开;
|
||||
2、adapter的4G功能开关;
|
||||
3、拆分的应用启动SEPARATE_COMPILE开关从SD卡启动的配置开关APP_STARTUP_FROM_SDCARD。
|
||||
|
||||
|
|
@ -0,0 +1,411 @@
|
|||
|
||||
/*
|
||||
* 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: ota.c
|
||||
* @brief: a application ota task of system
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2021/11/3
|
||||
*
|
||||
*/
|
||||
#include <transform.h>
|
||||
#include <adapter.h>
|
||||
|
||||
extern int main(void);
|
||||
|
||||
struct ota_header_t
|
||||
{
|
||||
int16 frame_flag; ///< frame start flag 2 Bytes
|
||||
uint8 dev_type; ///< device type
|
||||
uint8 burn_mode; ///< data burn way
|
||||
uint32 total_len; ///< send data total length caculated from each frame_len
|
||||
uint32 dev_hid; ///< device hardware version
|
||||
uint32 dev_sid; ///< device software version
|
||||
char resv[8]; ///< reserve
|
||||
};
|
||||
|
||||
struct ota_frame_t
|
||||
{
|
||||
uint32 frame_id; ///< Current frame id
|
||||
uint32 frame_len; ///< Current frame data length
|
||||
char frame_data[64]; ///< Current frame data,max length 64
|
||||
uint32 crc; ///< Current frame data crc
|
||||
};
|
||||
|
||||
struct ota_data
|
||||
{
|
||||
struct ota_header_t header;
|
||||
struct ota_frame_t frame;
|
||||
char end[4];
|
||||
};
|
||||
|
||||
pthread_t ota_task;
|
||||
pthread_t restart_main;
|
||||
|
||||
/**
|
||||
* @description: CRC16 check
|
||||
* @param data data buffer
|
||||
* @param length data length
|
||||
* @return check code
|
||||
*/
|
||||
uint32_t OtaCrc16(uint8_t * data, uint32_t length)
|
||||
{
|
||||
int j;
|
||||
unsigned int reg_crc=0xFFFF;
|
||||
printf("crc data length[%d] Bytes,",length);
|
||||
|
||||
while (length--) {
|
||||
reg_crc ^= *data++;
|
||||
for (j=0;j<8;j++) {
|
||||
if(reg_crc & 0x01)
|
||||
reg_crc=reg_crc >>1 ^ 0xA001;
|
||||
else
|
||||
reg_crc=reg_crc >>1;
|
||||
}
|
||||
}
|
||||
printf(" crc = [0x%x]\n",reg_crc);
|
||||
return reg_crc;
|
||||
}
|
||||
|
||||
uint32_t FileCrc16(uint8_t * data, uint32_t length, unsigned int last_crc)
|
||||
{
|
||||
int j;
|
||||
//printf("crc data length[%d] Bytes,",length);
|
||||
|
||||
while (length--) {
|
||||
last_crc ^= *data++;
|
||||
for (j=0;j<8;j++) {
|
||||
if(last_crc & 0x01)
|
||||
last_crc = last_crc >>1 ^ 0xA001;
|
||||
else
|
||||
last_crc = last_crc >>1;
|
||||
}
|
||||
}
|
||||
|
||||
//printf(" crc = [0x%x]\n",last_crc);
|
||||
|
||||
return last_crc;
|
||||
}
|
||||
|
||||
|
||||
static int SaveAppBin(int fd, char* buf, int len)
|
||||
{
|
||||
int ret = 0;
|
||||
int fd_t = 0;
|
||||
fd_t = open( BOARD_APP_NAME, O_RDWR | O_APPEND);
|
||||
ret = write(fd, buf, len);
|
||||
if(ret < 0){
|
||||
printf("fd = %d write buf len[%d] failed.ret = %d\n",fd_t,len,ret);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("fd[%d] write buf length[%d] done.\n",fd_t,ret);
|
||||
}
|
||||
|
||||
close(fd_t);
|
||||
}
|
||||
|
||||
static int CrcFileCheck(uint32 crc_check, unsigned long total_len)
|
||||
{
|
||||
int ret = 0;
|
||||
int fd = 0;
|
||||
int len = 0;
|
||||
char *buf = NULL;
|
||||
unsigned int last_crc = 0xffff;
|
||||
unsigned long already_crc_length = 0;
|
||||
|
||||
fd = open( BOARD_APP_NAME, O_RDONLY );
|
||||
if(fd < 0){
|
||||
printf("open %s bin failed.\n",BOARD_APP_NAME);
|
||||
return -1;
|
||||
}
|
||||
|
||||
buf = PrivMalloc(128);
|
||||
if(NULL == buf)
|
||||
{
|
||||
printf("malloc failed.\n");
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* crc check every 1024 Bytes until crc all the total file */
|
||||
while(already_crc_length != total_len)
|
||||
{
|
||||
memset(buf , 0 , 128);
|
||||
len = read(fd, buf, 128);
|
||||
if(len < 0)
|
||||
{
|
||||
printf("file read failed.ret = %d\n",len);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
last_crc = FileCrc16(buf, len, last_crc);
|
||||
already_crc_length += len;
|
||||
printf("read len[%d] Bytes,already_crc_length[%d]\n",len,already_crc_length);
|
||||
}
|
||||
|
||||
|
||||
if (last_crc != crc_check)
|
||||
{
|
||||
printf("file crc error!!! last crc[%x] != check[%x]\n",last_crc,crc_check);
|
||||
ret =-1;
|
||||
}
|
||||
|
||||
PrivFree(buf);
|
||||
close(fd);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void RestartApplication(void)
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 10;
|
||||
attr.stacksize = 2048;
|
||||
|
||||
while(1)
|
||||
{
|
||||
unsigned long pid = PrivUserTaskSearch();
|
||||
if ((pid > 0) && (pid != pthread_self()))
|
||||
{
|
||||
printf("kill usertask pid[%d]\n",pid);
|
||||
PrivTaskDelete(pid, 0);
|
||||
PrivTaskDelay(1000); /* NOTE:this delay will make a schedule and recycle all user task */
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
printf("restart main.\n");
|
||||
PrivTaskCreate(&restart_main, &attr, (void *)main, NULL);
|
||||
}
|
||||
static int OtaDataRecv(struct Adapter* adapter)
|
||||
{
|
||||
struct ota_data recv_msg;
|
||||
char reply[16] = {0};
|
||||
int ret = 0;
|
||||
int try_times = 10;
|
||||
int fd = 0;
|
||||
int frame_cnt = 0;
|
||||
|
||||
fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
|
||||
if(fd < 0)
|
||||
{
|
||||
printf("open %s failed\n",BOARD_APP_NAME);
|
||||
return -1;
|
||||
}
|
||||
close(fd);
|
||||
|
||||
while(1) {
|
||||
memset(&recv_msg, 0, sizeof(struct ota_data));
|
||||
printf("recv msg...\n");
|
||||
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
|
||||
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
|
||||
{
|
||||
if(0 == strncmp("aiit_ota_end",recv_msg.frame.frame_data, strlen("aiit_ota_end")))
|
||||
{
|
||||
printf("total [%d]frames [%d]Bytes crc[%x],receive successful,\n",frame_cnt,recv_msg.header.total_len,recv_msg.frame.crc);
|
||||
if(0 != CrcFileCheck(recv_msg.frame.crc, recv_msg.header.total_len))
|
||||
{
|
||||
printf("crc check %s bin failed.please try again.\n", BOARD_APP_NAME);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
PrivTaskDelay(500);
|
||||
printf("tolal file crc done.send ok\n");
|
||||
memset(reply, 0, 16);
|
||||
memcpy(reply, "ok", strlen("ok"));
|
||||
|
||||
AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
frame_cnt = recv_msg.frame.frame_id;
|
||||
|
||||
if(0 == strncmp("wait_ok_timeout",recv_msg.frame.frame_data, strlen("wait_ok_timeout")))
|
||||
{
|
||||
printf("go to send ok again.\n");
|
||||
goto send_ok_again;
|
||||
|
||||
}
|
||||
|
||||
if (recv_msg.frame.crc == OtaCrc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len))
|
||||
{
|
||||
printf("save current [%d] frame,length[%d] Bytes.\n",frame_cnt,recv_msg.frame.frame_len);
|
||||
for(int i = 0; i < recv_msg.frame.frame_len;i++ ){
|
||||
printf(" %x ",*((char *)&recv_msg.frame.frame_data + i));
|
||||
}
|
||||
printf("\n");
|
||||
SaveAppBin(fd, recv_msg.frame.frame_data, recv_msg.frame.frame_len);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("current [%d] frame crc check failed,try again!\n",frame_cnt);
|
||||
goto try_again;
|
||||
}
|
||||
|
||||
send_ok_again:
|
||||
memset(reply, 0, 16);
|
||||
memcpy(reply, "ok", strlen("ok"));
|
||||
// PrivTaskDelay(100);
|
||||
|
||||
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||
if(ret < 0){
|
||||
printf("send ok failed.\n");
|
||||
goto send_ok_again;
|
||||
}
|
||||
printf("send reply[%s] done.\n",reply);
|
||||
try_times = 10;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
try_again:
|
||||
if(try_times == 0)
|
||||
{
|
||||
printf("oops!!! current [%d] frame try 10 times failed,break out!\n",frame_cnt);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
memset(reply, 0, 16);
|
||||
memcpy(reply, "retry", strlen("retry"));
|
||||
printf("[%d] frame receive failed. retry\n",frame_cnt);
|
||||
AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||
try_times--;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
|
||||
if(0 == ret) {
|
||||
printf("ota file done,start application.\n");
|
||||
RestartApplication();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void *OtaKTaskEntry(void *parameter)
|
||||
{
|
||||
struct ota_data recv_msg;
|
||||
char reply[16] = {0};
|
||||
int baud_rate = BAUD_RATE_115200;
|
||||
int len = 0;
|
||||
int ret = 0;
|
||||
|
||||
struct Adapter* adapter = AdapterDeviceFindByName("4G");
|
||||
uint8 server_addr[64] = "115.238.53.61";
|
||||
uint8 server_port[64] = "9898";
|
||||
|
||||
adapter->socket.socket_id = 0;
|
||||
|
||||
AdapterDeviceOpen(adapter);
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
|
||||
|
||||
/* using nbiot as connection way*/
|
||||
|
||||
// struct Adapter* adapter = AdapterDeviceFindByName("nbiot");
|
||||
|
||||
// while(1)
|
||||
// {
|
||||
// int connect_times = 5;
|
||||
// ret = AdapterDeviceOpen(adapter);
|
||||
// if(ret < 0)
|
||||
// {
|
||||
// printf("open adapter failed\n");
|
||||
// continue;
|
||||
// }
|
||||
|
||||
// connect_again:
|
||||
// connect_times--;
|
||||
// ret = AdapterDeviceConnect(adapter, 1, "115.238.53.61","9898",1);
|
||||
// if(ret < 0)
|
||||
// {
|
||||
// if(connect_times > 0){
|
||||
// goto connect_again;
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// AdapterDeviceClose(adapter);
|
||||
// continue;
|
||||
// }
|
||||
// }
|
||||
// break;
|
||||
// }
|
||||
PrivTaskDelay(5000);
|
||||
while(1)
|
||||
{
|
||||
memset(&recv_msg, 0, sizeof(struct ota_data));
|
||||
/* step1: Confirm the start signal of transmission*/
|
||||
printf("waiting for start msg...\n");
|
||||
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
|
||||
for(int i = 0; i < sizeof(struct ota_data);i++ ){
|
||||
printf(" %x ",*((char *)&recv_msg + i));
|
||||
}
|
||||
printf("\n");
|
||||
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
|
||||
{
|
||||
if (0 == strncmp("aiit_ota_start",recv_msg.frame.frame_data, strlen("aiit_ota_start")))
|
||||
{
|
||||
memset(reply, 0, 16);
|
||||
memcpy(reply, "ready", strlen("ready"));
|
||||
// PrivTaskDelay(3000);
|
||||
printf("receive start signal,send [ready] signal to server\n");
|
||||
send_ready_again:
|
||||
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||
if(ret < 0)
|
||||
{
|
||||
goto send_ready_again;
|
||||
}
|
||||
PrivTaskDelay(3000);
|
||||
printf("start receive ota file.\n");
|
||||
/* step2: start receive source bin file of application*/
|
||||
ret = OtaDataRecv(adapter);
|
||||
if (0 != ret)
|
||||
{
|
||||
memset(reply, 0, 16);
|
||||
memcpy(reply, "ota_restart", strlen("ota_restart"));
|
||||
AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
memset(reply, 0, 16);
|
||||
memcpy(reply, "notready", strlen("notready"));
|
||||
printf("ota status:not ready\n");
|
||||
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||
}
|
||||
PrivTaskDelay(3000); /* check ota signal every 5s */
|
||||
}
|
||||
AdapterDeviceClose(adapter);
|
||||
|
||||
}
|
||||
|
||||
void ApplicationOtaTaskInit(void)
|
||||
{
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 20;
|
||||
attr.stacksize = 4096;
|
||||
|
||||
PrivTaskCreate(&ota_task, &attr, OtaKTaskEntry, NULL);
|
||||
|
||||
}
|
|
@ -0,0 +1,364 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <errno.h>
|
||||
#include <sys/time.h>
|
||||
#include <assert.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
typedef int BOOL;
|
||||
#define true 1
|
||||
#define false 0
|
||||
|
||||
int serverfd;//服务器socket
|
||||
int clientfd[100000];//客户端的socketfd,100个元素,clientfd[0]~clientfd[99]
|
||||
int size = 99999;//用来控制进入聊天室的人数为50以内
|
||||
int PORT = 9898;//端口号
|
||||
typedef struct sockaddr meng;
|
||||
|
||||
struct ota_header_t
|
||||
{
|
||||
int16_t frame_flag; ///< frame start flag 2 Bytes
|
||||
uint8_t dev_type; ///< device type
|
||||
uint8_t burn_mode; ///< data burn way
|
||||
uint32_t total_len; ///< send data total length caculated from each frame_len
|
||||
uint32_t dev_hid; ///< device hardware version
|
||||
uint32_t dev_sid; ///< device software version
|
||||
char resv[8]; ///< reserve
|
||||
};
|
||||
|
||||
struct ota_frame_t
|
||||
{
|
||||
uint32_t frame_id; ///< Current frame id
|
||||
uint32_t frame_len; ///< Current frame data length
|
||||
char frame_data[64]; ///< Current frame data,max length 224
|
||||
uint32_t crc; ///< Current frame data crc
|
||||
};
|
||||
|
||||
struct ota_data
|
||||
{
|
||||
struct ota_header_t header;
|
||||
struct ota_frame_t frame;
|
||||
char end[4];
|
||||
};
|
||||
|
||||
pthread_t ota_ktask;
|
||||
|
||||
/**
|
||||
* @description: CRC16 check
|
||||
* @param data data buffer
|
||||
* @param length data length
|
||||
* @return check code
|
||||
*/
|
||||
uint32_t OtaCrc16(uint8_t * data, uint32_t length)
|
||||
{
|
||||
int j;
|
||||
unsigned int reg_crc=0xFFFF;
|
||||
|
||||
printf("crc data length[%d] Bytes,",length);
|
||||
|
||||
while (length--) {
|
||||
reg_crc ^= *data++;
|
||||
for (j=0;j<8;j++) {
|
||||
if(reg_crc & 0x01)
|
||||
reg_crc=reg_crc >>1 ^ 0xA001;
|
||||
else
|
||||
reg_crc=reg_crc >>1;
|
||||
}
|
||||
}
|
||||
printf(" crc = [0x%x]\n",reg_crc);
|
||||
return reg_crc;
|
||||
}
|
||||
|
||||
void init(void)
|
||||
{
|
||||
serverfd = socket(PF_INET,SOCK_STREAM,0);
|
||||
|
||||
if (serverfd == -1)
|
||||
{
|
||||
perror("创建socket失败");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
//为套接字设置ip协议 设置端口号 并自动获取本机ip转化为网络ip
|
||||
|
||||
struct sockaddr_in addr;//存储套接字的信息
|
||||
addr.sin_family = AF_INET;//地址族
|
||||
addr.sin_port = htons(PORT);//设置server端端口号,你可以随便设置,当sin_port = 0时,系统随机选择一个未被使用的端口号
|
||||
addr.sin_addr.s_addr = htons(INADDR_ANY);//当sin_addr = INADDR_ANY时,表示从本机的任一网卡接收数据
|
||||
|
||||
//绑定套接字
|
||||
// int on = 1;
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 5;
|
||||
timeout.tv_usec = 0;
|
||||
if(setsockopt(serverfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
|
||||
{
|
||||
perror("端口设置失败");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (bind(serverfd,(meng*)&addr,sizeof(addr)) == -1)
|
||||
{
|
||||
perror("绑定失败");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
if (listen(serverfd,100) == -1)
|
||||
{//监听最大连接数
|
||||
perror("设置监听失败");
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
int OtaFileSend(int fd)
|
||||
{
|
||||
unsigned char buf[32] = { 0 };
|
||||
struct ota_data data;
|
||||
FILE *file_fd;
|
||||
char ch;
|
||||
int length = 0;
|
||||
int try_times = 10;
|
||||
int recv_end_times = 3;
|
||||
int ret = 0;
|
||||
int frame_cnt = 0;
|
||||
int file_length = 0;
|
||||
char * file_buf = NULL;
|
||||
|
||||
file_fd = fopen("/home/aep04/wwg/XiUOS_aiit-arm32-board_app.bin", "r");
|
||||
if (NULL == file_fd){
|
||||
printf("open file failed.\n");
|
||||
return -1;
|
||||
}
|
||||
fseek(file_fd, 0, SEEK_SET);
|
||||
printf("start send file.\n");
|
||||
while(!feof(file_fd))
|
||||
{
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
data.header.frame_flag = 0x5A5A;
|
||||
length = fread( data.frame.frame_data, 1, 64, file_fd );
|
||||
if(length > 0)
|
||||
{
|
||||
printf("read %d Bytes\n",length);
|
||||
data.frame.frame_id = frame_cnt;
|
||||
data.frame.frame_len = length;
|
||||
data.frame.crc = OtaCrc16(data.frame.frame_data, length);
|
||||
file_length += length;
|
||||
}
|
||||
|
||||
send_again:
|
||||
usleep(50000);
|
||||
printf("ota send current[%d] frame.\n",frame_cnt);
|
||||
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
|
||||
if(length < 0){
|
||||
printf("send [%d] frame faile.go to send again\n",frame_cnt);
|
||||
goto send_again;
|
||||
}
|
||||
|
||||
recv_again:
|
||||
memset(buf, 0, 32);
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length < 0 ){
|
||||
printf("[%d] frame waiting for ok timeout,receive again.\n",frame_cnt);
|
||||
goto recv_again;
|
||||
}
|
||||
|
||||
printf("receive buf[%s] length = %d\n",buf, length);
|
||||
if(0 == strncmp(buf, "ok", length))
|
||||
{
|
||||
try_times = 10;
|
||||
printf("[%d]frame data send done.\n",frame_cnt);
|
||||
frame_cnt++;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(try_times > 0)
|
||||
{
|
||||
try_times--;
|
||||
goto send_again;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("send frame[%d] 10 times failed.\n",frame_cnt);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* finally,crc check total bin file.*/
|
||||
if (ret == 0)
|
||||
{
|
||||
sleep(1);
|
||||
printf("total send file length[%d] Bytes [%d] frames.\n",file_length,frame_cnt);
|
||||
printf("now crc check total bin file.\n");
|
||||
file_buf = malloc(file_length);
|
||||
memset(file_buf, 0, file_length);
|
||||
memset(&data, 0, sizeof(data));
|
||||
|
||||
data.header.frame_flag = 0x5A5A;
|
||||
|
||||
file_fd = fopen("/home/aep04/wwg/XiUOS_aiit-arm32-board_app.bin", "r");
|
||||
if (NULL == file_fd){
|
||||
printf("open file failed.\n");
|
||||
return -1;
|
||||
}
|
||||
fseek(file_fd, 0, SEEK_SET);
|
||||
length = fread(file_buf,1, file_length, file_fd);
|
||||
printf("read file length = %d\n",length);
|
||||
if(length > 0) {
|
||||
data.frame.frame_id = frame_cnt;
|
||||
data.header.total_len = file_length;
|
||||
data.frame.frame_len = strlen("aiit_ota_end");
|
||||
data.frame.crc = OtaCrc16(file_buf, length);
|
||||
memcpy(data.frame.frame_data,"aiit_ota_end",strlen("aiit_ota_end"));
|
||||
}
|
||||
|
||||
send_end_signal:
|
||||
printf("send aiit_ota_end signal.\n");
|
||||
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
|
||||
if(length < 0){
|
||||
printf("send end signal faile,send end signal again\n");
|
||||
goto send_end_signal;
|
||||
}
|
||||
|
||||
recv_end_signal:
|
||||
memset(buf, 0, 32);
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if(length < 0 )
|
||||
{
|
||||
recv_end_times--;
|
||||
printf("end signal waiting for ok timeout,receive again.\n");
|
||||
if(recv_end_times > 0)
|
||||
{
|
||||
goto recv_end_signal;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
|
||||
if(0 != strncmp(buf, "ok", length))
|
||||
{
|
||||
printf("error end !!!\n");
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
free(file_buf);
|
||||
}
|
||||
|
||||
fclose(file_fd);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void* server_thread(void* p)
|
||||
{
|
||||
int fd = *(int*)p;
|
||||
unsigned char buf[32] = { 0 };
|
||||
struct ota_data data;
|
||||
int ret = 0;
|
||||
int length = 0;
|
||||
|
||||
printf("pthread = %d\n",fd);
|
||||
sleep(8);
|
||||
while(1)
|
||||
{
|
||||
memset(&data, 0x0 , sizeof(struct ota_data));
|
||||
data.header.frame_flag = 0x5A5A;
|
||||
memcpy(data.frame.frame_data,"aiit_ota_start",strlen("aiit_ota_start"));
|
||||
data.frame.frame_len = strlen("aiit_ota_start");
|
||||
|
||||
printf("send start signal.\n");
|
||||
ret = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
|
||||
if (ret > 0){
|
||||
printf("send %s[%d] Bytes\n",data.frame.frame_data,ret);
|
||||
}
|
||||
// sleep(1);
|
||||
memset(buf, 0, 32);
|
||||
length = recv(fd, buf, sizeof(buf), 0);
|
||||
if (length <= 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("recv buf %s length %d\n",buf,length);
|
||||
if(0 == strncmp(buf, "ready", length))
|
||||
{
|
||||
ret = OtaFileSend(fd);
|
||||
if (ret == 0) {
|
||||
printf("ota file send successful.\n");
|
||||
break;
|
||||
} else { /* ota failed then restart the ota process */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("exit fd = %d\n",fd);
|
||||
close(fd);
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
void server(void)
|
||||
{
|
||||
printf("ota Server startup\n");
|
||||
while(1)
|
||||
{
|
||||
struct sockaddr_in fromaddr;
|
||||
socklen_t len = sizeof(fromaddr);
|
||||
int fd = accept(serverfd,(meng*)&fromaddr,&len);
|
||||
|
||||
//调用accept进入堵塞状态,等待客户端的连接
|
||||
|
||||
if (fd == -1)
|
||||
{
|
||||
// printf("The client connection is wrong...\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (i = 0;i < size;i++)
|
||||
{
|
||||
if (clientfd[i] == 0)
|
||||
{
|
||||
//记录客户端的socket
|
||||
clientfd[i] = fd;
|
||||
|
||||
//有客户端连接之后,启动线程给此客户服务
|
||||
pthread_t tid;
|
||||
pthread_create(&tid,0,server_thread,&fd);
|
||||
break;
|
||||
}
|
||||
|
||||
if (size == i)
|
||||
{
|
||||
//发送给客户端说聊天室满了
|
||||
char* str = "Devices full";
|
||||
printf("%s", str);
|
||||
send(fd,str,strlen(str),0);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
init();
|
||||
server();
|
||||
}
|
||||
|
|
@ -112,4 +112,4 @@ int Adapter4GTest(void)
|
|||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, Adapter4GTest, Adapter4GTest, show adapter 4G information);
|
||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, Adapter4GTest, Adapter4GTest, show adapter 4G information);
|
||||
|
|
|
@ -268,7 +268,7 @@ static int Ec200tSend(struct Adapter *adapter, const void *buf, size_t len)
|
|||
static int Ec200tRecv(struct Adapter *adapter, void *buf, size_t len)
|
||||
{
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 40000);
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 6);
|
||||
} else {
|
||||
printf("Ec200tRecv can not find agent\n");
|
||||
}
|
||||
|
|
|
@ -135,6 +135,7 @@ int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const ch
|
|||
|
||||
abstime.tv_sec = timeout_s;
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
agent->receive_mode = AT_MODE;
|
||||
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
|
@ -147,31 +148,33 @@ int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const ch
|
|||
uint32 cmd_size = 0;
|
||||
uint32 result = 0;
|
||||
const char *cmd = NULL;
|
||||
PrivMutexObtain(&agent->lock);
|
||||
|
||||
agent->reply = reply;
|
||||
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
if(agent->reply != NULL) {
|
||||
PrivMutexObtain(&agent->lock);
|
||||
reply->reply_len = 0;
|
||||
va_start(params, cmd_expr);
|
||||
ATSprintf(agent->fd, cmd_expr, params);
|
||||
va_end(params);
|
||||
if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != 0) {
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != EOK) {
|
||||
printf("take sem %d timeout\n",agent->rsp_sem);
|
||||
result = -ETIMEOUT;
|
||||
goto __out;
|
||||
}
|
||||
} else {
|
||||
PrivMutexObtain(&agent->lock);
|
||||
va_start(params, cmd_expr);
|
||||
ATSprintf(agent->fd, cmd_expr, params);
|
||||
va_end(params);
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
goto __out;
|
||||
}
|
||||
|
||||
__out:
|
||||
agent->reply = NULL;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
|
||||
// agent->reply = NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -183,15 +186,19 @@ int AtCmdConfigAndCheck(ATAgentType agent, char *cmd, char *check)
|
|||
return -1;
|
||||
}
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
ATReplyType reply = CreateATReply(256);
|
||||
if (NULL == reply) {
|
||||
printf("%s %d at_create_resp failed!\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd);
|
||||
PrivTaskDelay(3000);
|
||||
ret = ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd);
|
||||
if(ret < 0){
|
||||
printf("%s %d ATOrderSend failed.\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
// PrivTaskDelay(3000);
|
||||
|
||||
result = GetReplyText(reply);
|
||||
if (!result) {
|
||||
|
@ -254,15 +261,23 @@ 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);
|
||||
return -1;
|
||||
}
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
memset(send_buf, 0, 128);
|
||||
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
|
||||
memcpy(send_buf, data, len);
|
||||
memcpy(send_buf + len, "!@", 2);
|
||||
// memcpy(send_buf + len, "!@", 2);
|
||||
|
||||
PrivWrite(agent->fd, send_buf, len + 2);
|
||||
|
||||
return 0;
|
||||
PrivWrite(agent->fd, send_buf, len);
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
printf("entm send %s length %d\n",send_buf, len);
|
||||
return EOK;
|
||||
}
|
||||
|
||||
int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
|
||||
|
@ -270,32 +285,32 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
|
|||
struct timespec abstime;
|
||||
|
||||
abstime.tv_sec = timeout_s;
|
||||
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
|
||||
PrivTaskDelay(1000);
|
||||
|
||||
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) {
|
||||
return -1;
|
||||
if(buffer_len > ENTM_RECV_MAX){
|
||||
printf("read length more then max length[%d] Bytes",ENTM_RECV_MAX);
|
||||
return -1;
|
||||
}
|
||||
PrivMutexObtain(&agent->lock);
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
agent->read_len = buffer_len;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
// PrivTaskDelay(1000);
|
||||
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) {
|
||||
printf("wait sem[%d] timeout\n",agent->entm_rx_notice);
|
||||
return -ERROR;
|
||||
}
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
|
||||
if (buffer_len < agent->entm_recv_len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("EntmRecv once len %u.\n", agent->entm_recv_len);
|
||||
printf("EntmRecv once len %d.\n", agent->entm_recv_len);
|
||||
|
||||
agent->entm_recv_buf[agent->entm_recv_len - 1] = '\0';
|
||||
memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len - 2);
|
||||
memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len);
|
||||
|
||||
memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX);
|
||||
agent->entm_recv_len = 0;
|
||||
|
||||
agent->read_len = 0;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
return 0;
|
||||
return buffer_len;
|
||||
}
|
||||
|
||||
static int GetCompleteATReply(ATAgentType agent)
|
||||
|
@ -304,33 +319,50 @@ static int GetCompleteATReply(ATAgentType agent)
|
|||
char ch = 0, last_ch = 0;
|
||||
bool is_full = false;
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
agent->maintain_len = 0;
|
||||
|
||||
while (1) {
|
||||
memset(agent->entm_recv_buf, 0x00, 256);
|
||||
agent->entm_recv_len = 0;
|
||||
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
while (1)
|
||||
{
|
||||
PrivRead(agent->fd, &ch, 1);
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf(" %c (0x%x)\n", ch, ch);
|
||||
#endif
|
||||
if (agent->receive_mode == ENTM_MODE){
|
||||
if (agent->entm_recv_len < ENTM_RECV_MAX) {
|
||||
PrivMutexObtain(&agent->lock);
|
||||
|
||||
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++;
|
||||
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
|
||||
if (last_ch == '!' && ch == '@'){
|
||||
if(agent->entm_recv_len < agent->read_len){
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("ENTM_MODE recv %d Bytes done.\n",agent->entm_recv_len);
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
PrivSemaphoreAbandon(&agent->entm_rx_notice);
|
||||
}
|
||||
|
||||
last_ch = ch;
|
||||
} else {
|
||||
printf("entm_recv_buf is_full ...\n");
|
||||
}
|
||||
} else if (agent->receive_mode == AT_MODE) {
|
||||
if (read_len < agent->maintain_max){
|
||||
}
|
||||
else if (agent->receive_mode == AT_MODE)
|
||||
{
|
||||
if (read_len < agent->maintain_max)
|
||||
{
|
||||
agent->maintain_buffer[read_len] = ch;
|
||||
read_len++;
|
||||
agent->maintain_len = read_len;
|
||||
|
@ -347,13 +379,19 @@ static int GetCompleteATReply(ATAgentType agent)
|
|||
printf("read line failed. The line data length is out of buffer size(%d)!", agent->maintain_max);
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
agent->maintain_len = 0;
|
||||
return -1;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
return -ERROR;
|
||||
}
|
||||
|
||||
printf("GetCompleteATReply done\n");
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
break;
|
||||
}
|
||||
last_ch = ch;
|
||||
}
|
||||
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
}
|
||||
|
||||
return read_len;
|
||||
|
@ -371,21 +409,31 @@ ATAgentType GetATAgent(const char *agent_name)
|
|||
return result;
|
||||
}
|
||||
|
||||
static int DeleteATAgent(ATAgentType agent)
|
||||
|
||||
int DeleteATAgent(ATAgentType agent)
|
||||
{
|
||||
printf("delete agent->at_handler = %d\n",agent->at_handler);
|
||||
if(agent->at_handler > 0){
|
||||
PrivTaskDelete(agent->at_handler, 0);
|
||||
}
|
||||
|
||||
if (agent->fd > 0) {
|
||||
printf("close agent fd = %d\n",agent->fd);
|
||||
PrivClose(agent->fd);
|
||||
}
|
||||
|
||||
if (agent->lock) {
|
||||
printf("delete agent lock = %d\n",agent->lock);
|
||||
PrivMutexDelete(&agent->lock);
|
||||
}
|
||||
|
||||
if (agent->entm_rx_notice) {
|
||||
printf("delete agent entm_rx_notice = %d\n",agent->entm_rx_notice);
|
||||
PrivSemaphoreDelete(&agent->entm_rx_notice);
|
||||
}
|
||||
|
||||
if (agent->fd > 0) {
|
||||
PrivClose(agent->fd);
|
||||
}
|
||||
|
||||
if (agent->rsp_sem) {
|
||||
printf("delete agent rsp_sem = %d\n",agent->rsp_sem);
|
||||
PrivSemaphoreDelete(&agent->rsp_sem);
|
||||
}
|
||||
|
||||
|
@ -394,6 +442,7 @@ static int DeleteATAgent(ATAgentType agent)
|
|||
}
|
||||
|
||||
memset(agent, 0x00, sizeof(struct ATAgent));
|
||||
printf("delete ATagent\n");
|
||||
}
|
||||
|
||||
static void *ATAgentReceiveProcess(void *param)
|
||||
|
@ -402,23 +451,25 @@ static void *ATAgentReceiveProcess(void *param)
|
|||
const struct at_urc *urc;
|
||||
|
||||
while (1) {
|
||||
if (GetCompleteATReply(agent) > 0) {
|
||||
if (GetCompleteATReply(agent) > 0)
|
||||
{
|
||||
PrivMutexObtain(&agent->lock);
|
||||
if (agent->reply != NULL){
|
||||
ATReplyType reply = agent->reply;
|
||||
|
||||
agent->maintain_buffer[agent->maintain_len] = '\0';
|
||||
|
||||
if (agent->maintain_len < reply->reply_max_len) {
|
||||
if (agent->maintain_len <= reply->reply_max_len) {
|
||||
memset(reply->reply_buffer, 0 , reply->reply_max_len);
|
||||
memcpy(reply->reply_buffer, agent->maintain_buffer, agent->maintain_len);
|
||||
|
||||
reply->reply_len = agent->maintain_len;
|
||||
} else {
|
||||
printf("out of memory (%d)!", reply->reply_max_len);
|
||||
printf("out of memory (%d)!\n", reply->reply_max_len);
|
||||
}
|
||||
|
||||
agent->reply = NULL;
|
||||
// agent->reply = NULL;
|
||||
PrivSemaphoreAbandon(&agent->rsp_sem);
|
||||
}
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -442,26 +493,26 @@ static int ATAgentInit(ATAgentType agent)
|
|||
printf("ATAgentInit create entm sem error\n");
|
||||
goto __out;
|
||||
}
|
||||
|
||||
printf("create entm_rx_notice_sem %d\n ",agent->entm_rx_notice);
|
||||
result = PrivSemaphoreCreate(&agent->rsp_sem, 0, 0);
|
||||
if (result < 0) {
|
||||
printf("ATAgentInit create rsp sem error\n");
|
||||
goto __out;
|
||||
}
|
||||
|
||||
printf("create rsp_sem %d\n ",agent->rsp_sem);
|
||||
if(PrivMutexCreate(&agent->lock, 0) < 0) {
|
||||
printf("AdapterFrameworkInit mutex create failed.\n");
|
||||
goto __out;
|
||||
}
|
||||
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 18;
|
||||
attr.stacksize = 2048;
|
||||
attr.stacksize = 4096;
|
||||
|
||||
PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, agent);
|
||||
|
||||
printf("create agent->at_handler = %d\n",agent->at_handler);
|
||||
return result;
|
||||
|
||||
__out:
|
||||
|
@ -517,6 +568,7 @@ ATReplyType CreateATReply(uint32 reply_max_len)
|
|||
printf("no more memory\n");
|
||||
return NULL;
|
||||
}
|
||||
memset(reply, 0, sizeof(struct ATReply));
|
||||
|
||||
reply->reply_max_len = reply_max_len;
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define REPLY_TIME_OUT 3
|
||||
#define REPLY_TIME_OUT 10
|
||||
|
||||
enum ReceiveMode
|
||||
{
|
||||
|
@ -47,6 +47,7 @@ struct ATAgent
|
|||
{
|
||||
char agent_name[64];
|
||||
int fd;
|
||||
int read_len; ///< control data length of wanting reading
|
||||
|
||||
char *maintain_buffer;
|
||||
uint32 maintain_len;
|
||||
|
@ -83,6 +84,7 @@ void SwapStr(char *str, int begin, int end);
|
|||
char* IpTstr(unsigned int ipint);
|
||||
ATAgentType GetATAgent(const char *agent_name);
|
||||
int InitATAgent(const char *agent_name, int fd, uint32 maintain_max);
|
||||
int DeleteATAgent(ATAgentType agent);
|
||||
int ParseATReply(char* str, const char *format, ...);
|
||||
void DeleteATReply(ATReplyType reply);
|
||||
int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const char *cmd_expr, ...);
|
||||
|
|
|
@ -17,8 +17,9 @@
|
|||
* @author AIIT XUOS Lab
|
||||
* @date 2021.06.25
|
||||
*/
|
||||
#include <transform.h>
|
||||
#include <adapter.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef ADAPTER_BC28
|
||||
extern AdapterProductInfoType BC28Attach(struct Adapter *adapter);
|
||||
|
@ -50,7 +51,7 @@ int AdapterNbiotInit(void)
|
|||
{
|
||||
int ret = 0;
|
||||
|
||||
struct Adapter *adapter = malloc(sizeof(struct Adapter));
|
||||
struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter));
|
||||
if (!adapter) {
|
||||
printf("malloc adapter failed.\n");
|
||||
return -1;
|
||||
|
@ -59,14 +60,14 @@ int AdapterNbiotInit(void)
|
|||
ret = AdapterNbiotRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("register nbiot adapter error\n");
|
||||
free(adapter);
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
#ifdef ADAPTER_BC28
|
||||
AdapterProductInfoType product_info = BC28Attach(adapter);
|
||||
if (!product_info) {
|
||||
printf("bc28 attach error\n");
|
||||
free(adapter);
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -96,7 +97,7 @@ int opennb(void)
|
|||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, opennb, opennb, show adapter nb information);
|
||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, opennb, opennb, show adapter nb information);
|
||||
int closenb(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
@ -114,7 +115,7 @@ int closenb(void)
|
|||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, closenb, closenb, show adapter nb information);
|
||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, closenb, closenb, show adapter nb information);
|
||||
|
||||
int connectnb(int argc, char *argv[])
|
||||
{
|
||||
|
@ -134,7 +135,7 @@ int closenb(void)
|
|||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, connectnb, connectnb, show adapter nb information);
|
||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, connectnb, connectnb, show adapter nb information);
|
||||
|
||||
int sendnb(int argc, char *argv[])
|
||||
{
|
||||
|
@ -155,7 +156,7 @@ int closenb(void)
|
|||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, sendnb, sendnb, show adapter nb information);
|
||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, sendnb, sendnb, show adapter nb information);
|
||||
|
||||
int recvnb(void)
|
||||
{
|
||||
|
@ -168,5 +169,5 @@ int closenb(void)
|
|||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, recvnb, recvnb, show adapter nb information);
|
||||
// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, recvnb, recvnb, show adapter nb information);
|
||||
|
||||
|
|
|
@ -21,6 +21,7 @@
|
|||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
#include "../adapter_nbiot.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SOCKET_PROTOCOL_TCP (6)
|
||||
#define SOCKET_PROTOCOL_UDP (17)
|
||||
|
@ -29,7 +30,7 @@
|
|||
#define NET_TYPE_AF_INET6 (1)
|
||||
|
||||
#define SOCKET_INVALID_ID (-1)
|
||||
|
||||
static int nbiot_lock;
|
||||
|
||||
static int BC28UartOpen(struct Adapter *adapter)
|
||||
{
|
||||
|
@ -112,16 +113,52 @@ int NBIoTStatusCheck(struct Adapter *adapter )
|
|||
char at_cmd[64] = {0};
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); /* set receive end flag as 'OK'*/
|
||||
|
||||
memcpy(at_cmd, "AT+CSQ", 6);
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+NCONFIG=AUTOCONNECT,TRUE", 27);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
if(result < 0) {
|
||||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
}
|
||||
ATOrderSend(adapter->agent, 0, NULL, at_cmd);
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
|
||||
// memset(at_cmd, 0 ,64);
|
||||
// memcpy(at_cmd, "AT+NRB", 6);
|
||||
// strcat(at_cmd, "\n");
|
||||
// printf("cmd : %s\n", at_cmd);
|
||||
// result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
// if(result < 0) {
|
||||
// printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
// result = -1;
|
||||
// goto out;
|
||||
// }
|
||||
// BC28PowerSet(); /* reset bc28 module by set reset pin */
|
||||
// PrivTaskDelay(6000);
|
||||
|
||||
// memset(at_cmd, 0 ,64);
|
||||
// memcpy(at_cmd, "AT+NBAND=5", 10);
|
||||
// strcat(at_cmd, "\n");
|
||||
// printf("cmd : %s\n", at_cmd);
|
||||
// result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
// if(result < 0) {
|
||||
// printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
// result = -1;
|
||||
// goto out;
|
||||
// }
|
||||
|
||||
|
||||
// memset(at_cmd, 0 ,64);
|
||||
// memcpy(at_cmd, "AT+CFUN=1", 10);
|
||||
// strcat(at_cmd, "\n");
|
||||
// printf("cmd : %s\n", at_cmd);
|
||||
// result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
// if(result < 0) {
|
||||
// printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
// result = -1;
|
||||
// goto out;
|
||||
// }
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); /* set receive end flag as 'OK'*/
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+CFUN?", 8);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
|
@ -129,8 +166,10 @@ int NBIoTStatusCheck(struct Adapter *adapter )
|
|||
if(result < 0) {
|
||||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+CIMI", 7);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
|
@ -138,17 +177,32 @@ int NBIoTStatusCheck(struct Adapter *adapter )
|
|||
if(result < 0) {
|
||||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+CEREG?", 9);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
result = AtCmdConfigAndCheck(adapter->agent, at_cmd, ",1");
|
||||
if(result < 0) {
|
||||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
}
|
||||
goto out;
|
||||
}
|
||||
|
||||
// memset(at_cmd, 0 ,64);
|
||||
// memcpy(at_cmd, "AT+CGATT=1", 10);
|
||||
// strcat(at_cmd, "\n");
|
||||
// printf("cmd : %s\n", at_cmd);
|
||||
// result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
// if(result < 0) {
|
||||
// printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
// result = -1;
|
||||
// goto out;
|
||||
// }
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+CGATT?", 9);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
|
@ -156,8 +210,21 @@ int NBIoTStatusCheck(struct Adapter *adapter )
|
|||
if(result < 0) {
|
||||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+CSQ", 6);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
if(result < 0) {
|
||||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+CGPADDR", 10);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
|
@ -166,7 +233,19 @@ int NBIoTStatusCheck(struct Adapter *adapter )
|
|||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+QREGSWT=2", 12);
|
||||
strcat(at_cmd, "\n");
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
if(result < 0) {
|
||||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -231,7 +310,7 @@ int NBIoTSocketCreate(struct Adapter *adapter, struct Socket *socket )
|
|||
result = -1;
|
||||
goto out;
|
||||
}
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+NSOCR=", 9);
|
||||
strcat(at_cmd, str_type);
|
||||
strcat(at_cmd, ",");
|
||||
|
@ -258,6 +337,96 @@ out:
|
|||
return result;
|
||||
}
|
||||
|
||||
static int BC28ParseData(void *dst, ATReplyType reply)
|
||||
{
|
||||
int nbdata_len = 0;
|
||||
int check_cnt = 0;
|
||||
int left = 0;
|
||||
int right = 0;
|
||||
int tag = 0;
|
||||
char *nbdata = NULL;
|
||||
char *result = NULL;
|
||||
|
||||
if (NULL == reply) {
|
||||
printf("at create failed ! \n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = GetReplyText(reply);
|
||||
if (!result) {
|
||||
printf("%s %n get reply failed.\n",__func__,__LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*the data style of BC28: eg.'<socket id>,<ip addr>,<port>,<data length>,<data>,<data length>,' ,its assic code*/
|
||||
|
||||
/*step1: get the <data> section between the fourth and fifth ','*/
|
||||
for(int i = 0; i < reply->reply_len; i++)
|
||||
{
|
||||
if(',' == *(result + i))
|
||||
{
|
||||
check_cnt++;
|
||||
if(4 == check_cnt) /*the fourth ','*/
|
||||
{
|
||||
left = i;
|
||||
}
|
||||
|
||||
if(5 == check_cnt) /*the fifth ',' */
|
||||
{
|
||||
right = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(left > 0 && right > 0)
|
||||
{
|
||||
/*step2: transform assic code as hexadecimal*/
|
||||
for(int j = (left + 1);j <= (right - 1);j++)
|
||||
{
|
||||
if(*(result + j) >= 0x30 && *(result + j) <= 0x39){
|
||||
*(result + j) = (*(result + j) - 0x30); /* transform [0-9]*/
|
||||
}
|
||||
else if(*(result + j) >= 0x41 && *(result + j) <= 0x46)
|
||||
{
|
||||
*(result + j) = (*(result + j) - 0x37); /* transform [A-F],NOTE!!! A-F can not be regarded as a char,only can be reagrded as a number*/
|
||||
}
|
||||
|
||||
printf("0x%x ",*(result + j));
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
nbdata_len = (right - left - 1) / 2; /*caculate the data length */
|
||||
nbdata = PrivMalloc(nbdata_len);
|
||||
if (!nbdata) {
|
||||
printf("%s %n malloc failed.\n",__func__,__LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
tag = left;
|
||||
printf("left = %d right = %d nbdata_len = %d\n",left ,right ,nbdata_len);
|
||||
|
||||
/*step3: transform hexadecimal as actual data */
|
||||
for(int k = 0; k < nbdata_len; k++ )
|
||||
{
|
||||
*(nbdata + k) = (*(result + tag + 1) * 16 + *(result + tag + 2));
|
||||
tag = tag + 2; /* transform with two data*/
|
||||
printf("0x%x ",*(nbdata + k));
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
memcpy(dst, nbdata, nbdata_len);
|
||||
|
||||
PrivFree(nbdata);
|
||||
|
||||
return nbdata_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: NBIoT device close a socket connection
|
||||
* @param adapter - NBIoT adapter AT
|
||||
|
@ -277,9 +446,11 @@ int NBIoTSocketDelete(struct Adapter *adapter )
|
|||
}
|
||||
|
||||
char str_fd[2] = {0};
|
||||
char at_cmd[16] = {0};
|
||||
char at_cmd[32] = {0};
|
||||
printf("NBIOT close socket id = %d\n",adapter->socket.socket_id);
|
||||
itoa(adapter->socket.socket_id, str_fd, 10);
|
||||
|
||||
memset(at_cmd, 0 ,32);
|
||||
memcpy(at_cmd, "AT+NSOCL=", 9);
|
||||
strcat(at_cmd, str_fd);
|
||||
strcat(at_cmd, "\n");
|
||||
|
@ -302,35 +473,56 @@ static int BC28Open(struct Adapter *adapter)
|
|||
if (NULL == adapter) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(PrivMutexCreate(&nbiot_lock, 0) < 0) {
|
||||
printf("nbiot_lock mutex create failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*step1: open BC8 serial port*/
|
||||
ret = BC28UartOpen(adapter);
|
||||
if (ret < 0) {
|
||||
printf("bc18 setup failed.\n");
|
||||
PrivMutexDelete(&nbiot_lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*step2: init AT agent*/
|
||||
if (!adapter->agent) {
|
||||
char *agent_name = "niot_device";
|
||||
if (EOK != InitATAgent(agent_name, adapter->fd, 512)) {
|
||||
PrivClose(adapter->fd);
|
||||
PrivMutexDelete(&nbiot_lock);
|
||||
printf("at agent init failed !\n");
|
||||
return -1;
|
||||
}
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
adapter->agent = at_agent;
|
||||
}
|
||||
|
||||
// BC28PowerSet(); /* reset bc28 module by set reset pin */
|
||||
PrivTaskDelay(6000);
|
||||
|
||||
ret = NBIoTStatusCheck(adapter); /* ask module status*/
|
||||
if(ret < 0){
|
||||
DeleteATAgent(adapter->agent);
|
||||
adapter->agent = NULL;
|
||||
PrivMutexDelete(&nbiot_lock);
|
||||
printf("NBIot status check failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
create_socket.type = SOCKET_TYPE_STREAM;
|
||||
create_socket.listen_port = 0;
|
||||
create_socket.socket_id = 1;
|
||||
create_socket.af_type = NET_TYPE_AF_INET;
|
||||
|
||||
BC28PowerSet(); /* reset bc28 module by set reset pin */
|
||||
PrivTaskDelay(6000);
|
||||
|
||||
NBIoTStatusCheck(adapter); /* ask module status*/
|
||||
|
||||
/*step3: create a tcp socket default */
|
||||
ret = NBIoTSocketCreate(adapter, &create_socket);
|
||||
if(ret < 0){
|
||||
if(ret < 0) {
|
||||
DeleteATAgent(adapter->agent);
|
||||
adapter->agent = NULL;
|
||||
PrivMutexDelete(&nbiot_lock);
|
||||
printf("NBIot create tcp socket failed.\n");
|
||||
return -1;
|
||||
}
|
||||
|
@ -341,14 +533,25 @@ static int BC28Open(struct Adapter *adapter)
|
|||
|
||||
static int BC28Close(struct Adapter *adapter)
|
||||
{
|
||||
NBIoTSocketDelete(adapter);
|
||||
PrivClose(adapter->fd);
|
||||
|
||||
if(&adapter->socket){
|
||||
NBIoTSocketDelete(adapter);
|
||||
}
|
||||
|
||||
if(adapter->agent)
|
||||
{
|
||||
DeleteATAgent(adapter->agent);
|
||||
adapter->agent = NULL;
|
||||
}
|
||||
|
||||
PrivMutexDelete(&nbiot_lock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int BC28Ioctl(struct Adapter *adapter, int cmd, void *args)
|
||||
{
|
||||
int ret = 0;
|
||||
PrivMutexObtain(&nbiot_lock);
|
||||
switch (cmd)
|
||||
{
|
||||
case CONFIG_NBIOT_RESET: /* reset nbiot */
|
||||
|
@ -368,7 +571,7 @@ static int BC28Ioctl(struct Adapter *adapter, int cmd, void *args)
|
|||
ret = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
PrivMutexAbandon(&nbiot_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -391,8 +594,10 @@ static int BC28Connect(struct Adapter *adapter, enum NetRoleType net_role, const
|
|||
char at_cmd[64] = {0};
|
||||
char str_fd[2] = {0};
|
||||
|
||||
PrivMutexObtain(&nbiot_lock);
|
||||
itoa(adapter->socket.socket_id, str_fd, 10);
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+NSOCO=", 9);
|
||||
strcat(at_cmd, str_fd);
|
||||
strcat(at_cmd, ",");
|
||||
|
@ -408,7 +613,7 @@ static int BC28Connect(struct Adapter *adapter, enum NetRoleType net_role, const
|
|||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
}
|
||||
|
||||
PrivMutexAbandon(&nbiot_lock);
|
||||
__exit:
|
||||
return result;
|
||||
}
|
||||
|
@ -418,8 +623,29 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len)
|
|||
uint32_t result = 0;
|
||||
char at_cmd[64] = {0};
|
||||
char str_fd[2] = {0};
|
||||
char assic_str[2] = {0};
|
||||
int assic_val = 0;
|
||||
int length = 0;
|
||||
|
||||
PrivMutexObtain(&nbiot_lock);
|
||||
|
||||
char *nbdata = PrivMalloc(2 * len);
|
||||
memset(nbdata, 0 ,2 * len);
|
||||
|
||||
for(int i = 0; i < len; i++)
|
||||
{
|
||||
assic_val = 0;
|
||||
for( ; assic_val <= 0x7f; assic_val++)
|
||||
{
|
||||
if(*(char*)(buf + i) == assic_val)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
itoa(assic_val, assic_str, 16);
|
||||
memcpy(nbdata + length, assic_str, 2);
|
||||
length = length + 2;
|
||||
}
|
||||
if (adapter->socket.type == SOCKET_TYPE_STREAM ) {
|
||||
|
||||
char size[2] = {0};
|
||||
|
@ -427,12 +653,13 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len)
|
|||
itoa(adapter->socket.socket_id, str_fd, 10);
|
||||
size[0] = len + '0';
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+NSOSD=", 9);
|
||||
strcat(at_cmd, str_fd);
|
||||
strcat(at_cmd, ",");
|
||||
strcat(at_cmd, size);
|
||||
strcat(at_cmd, ",");
|
||||
strcat(at_cmd, buf);
|
||||
strncat(at_cmd, nbdata,length);
|
||||
strcat(at_cmd, "\n");
|
||||
|
||||
} else if(adapter->socket.type == SOCKET_TYPE_DGRAM ) {
|
||||
|
@ -443,6 +670,7 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len)
|
|||
|
||||
itoa(adapter->socket.listen_port, listen_port, 10);
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+NSOST=", 9);
|
||||
strcat(at_cmd, str_fd);
|
||||
strcat(at_cmd, ",");
|
||||
|
@ -450,11 +678,11 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len)
|
|||
strcat(at_cmd, ",");
|
||||
strcat(at_cmd, listen_port);
|
||||
strcat(at_cmd, ",");
|
||||
strcat(at_cmd, buf);
|
||||
strncat(at_cmd, nbdata,length);
|
||||
strcat(at_cmd, "\n");
|
||||
|
||||
}
|
||||
|
||||
PrivFree(nbdata);
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK");
|
||||
|
@ -462,26 +690,32 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len)
|
|||
printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd);
|
||||
result = -1;
|
||||
}
|
||||
|
||||
PrivMutexAbandon(&nbiot_lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static int BC28Recv(struct Adapter *adapter, void *buf, size_t len)
|
||||
{
|
||||
char at_cmd[64] = {0};
|
||||
char str_fd[2] = {0};
|
||||
char size[2] = {0};
|
||||
int ret = 0;
|
||||
char *result = NULL;
|
||||
|
||||
PrivMutexObtain(&nbiot_lock);
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
ATReplyType reply = CreateATReply(512);
|
||||
if (NULL == reply) {
|
||||
printf("at create failed ! \n");
|
||||
PrivMutexAbandon(&nbiot_lock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
itoa(adapter->socket.socket_id, str_fd, 10);
|
||||
itoa(len, size, 10);
|
||||
|
||||
memset(at_cmd, 0 ,64);
|
||||
memcpy(at_cmd, "AT+NSORF=", 9);
|
||||
strcat(at_cmd, str_fd);
|
||||
strcat(at_cmd, ",");
|
||||
|
@ -489,20 +723,31 @@ static int BC28Recv(struct Adapter *adapter, void *buf, size_t len)
|
|||
strcat(at_cmd, "\n");
|
||||
|
||||
printf("cmd : %s\n", at_cmd);
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
|
||||
PrivTaskDelay(300);
|
||||
|
||||
result = GetReplyText(reply);
|
||||
if (!result) {
|
||||
printf("%s %n get reply failed.\n",__func__,__LINE__);
|
||||
ret = ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd);
|
||||
if(ret < 0)
|
||||
{
|
||||
printf("NBiot receive timeout\n");
|
||||
DeleteATReply(reply);
|
||||
return -1;
|
||||
}
|
||||
memcpy(buf, result, reply->reply_len);
|
||||
|
||||
if (reply) {
|
||||
if(reply)
|
||||
{
|
||||
ret = BC28ParseData(buf, reply);
|
||||
if (ret < 0)
|
||||
{
|
||||
PrivMutexAbandon(&nbiot_lock);
|
||||
if (reply)
|
||||
{
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
PrivMutexAbandon(&nbiot_lock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int BC28Disconnect(struct Adapter *adapter)
|
||||
|
@ -534,7 +779,7 @@ static const struct IpProtocolDone BC28_done =
|
|||
|
||||
AdapterProductInfoType BC28Attach(struct Adapter *adapter)
|
||||
{
|
||||
struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo));
|
||||
struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("BC28Attach malloc product_info error\n");
|
||||
return NULL;
|
||||
|
|
|
@ -79,6 +79,13 @@ int PrivTaskStartup(pthread_t *thread)
|
|||
{
|
||||
return 0;
|
||||
}
|
||||
#ifdef SEPARATE_COMPILE
|
||||
/* private API of xiuos to search the first user task in manage list */
|
||||
int PrivUserTaskSearch(void)
|
||||
{
|
||||
return UserTaskSearch();
|
||||
}
|
||||
#endif
|
||||
|
||||
int PrivTaskDelete(pthread_t thread, int sig)
|
||||
{
|
||||
|
@ -96,7 +103,7 @@ int PrivTaskDelay(int32_t ms)
|
|||
}
|
||||
|
||||
/*********************fs**************************/
|
||||
|
||||
#ifdef FS_VFS
|
||||
/************************Driver Posix Transform***********************/
|
||||
int PrivOpen(const char *path, int flags)
|
||||
{
|
||||
|
@ -150,7 +157,7 @@ int PrivIoctl(int fd, int cmd, void *args)
|
|||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
/********************memory api************/
|
||||
void *PrivMalloc(size_t size)
|
||||
{
|
||||
|
|
|
@ -101,7 +101,7 @@ struct PinParam
|
|||
long pin;//< pin number
|
||||
int mode;//< pin mode: input/output
|
||||
struct PinDevIrq irq_set;//< pin irq set
|
||||
uint64 arg;
|
||||
uint64_t arg;
|
||||
};
|
||||
|
||||
struct PinStat
|
||||
|
@ -128,7 +128,7 @@ struct SerialDataCfg
|
|||
uint8_t serial_invert_mode;
|
||||
uint16_t serial_buffer_size;
|
||||
|
||||
uint8 ext_uart_no;
|
||||
uint8_t ext_uart_no;
|
||||
enum ExtSerialPortConfigure port_configure;
|
||||
};
|
||||
|
||||
|
@ -171,6 +171,7 @@ int PrivTaskStartup(pthread_t *thread);
|
|||
int PrivTaskDelete(pthread_t thread, int sig);
|
||||
void PrivTaskQuit(void *value_ptr);
|
||||
int PrivTaskDelay(int32_t ms);
|
||||
int PrivUserTaskSearch(void);
|
||||
|
||||
/*********************driver*************************/
|
||||
|
||||
|
|
|
@ -49,6 +49,8 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
|
|||
return -1 ;
|
||||
|
||||
ret = UserTaskStartup(pid);
|
||||
*thread = pid;
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
@ -99,6 +101,7 @@ int pthread_setcanceltype(int type, int *oldtype)
|
|||
int pthread_kill(pthread_t thread, int sig)
|
||||
{
|
||||
/* This api should not be used, and will not be supported */
|
||||
UserTaskDelete(thread);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
|
|
@ -22,16 +22,16 @@
|
|||
#ifndef XS_USER_API_H
|
||||
#define XS_USER_API_H
|
||||
|
||||
#include <xiuos.h>
|
||||
#include <xsconfig.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <libc.h>
|
||||
#include "../../../../../../Ubiquitous/XiUOS/arch/kswitch.h"
|
||||
|
||||
|
||||
#ifdef SEPARATE_COMPILE
|
||||
|
||||
#include "../../../../../../Ubiquitous/XiUOS/arch/kswitch.h"
|
||||
|
||||
#define TASK_INFO 1
|
||||
#define MEM_INFO 2
|
||||
#define SEM_INFO 3
|
||||
|
@ -58,22 +58,23 @@ typedef void DIR;
|
|||
|
||||
int32_t UserTaskCreate(UtaskType utask);
|
||||
|
||||
x_err_t UserTaskStartup(int32_t id);
|
||||
x_err_t UserTaskDelete(int32_t id);
|
||||
long UserTaskStartup(int32_t id);
|
||||
long UserTaskDelete(int32_t id);
|
||||
long UserTaskSearch(void);
|
||||
void UserTaskQuit(void);
|
||||
x_err_t UserTaskDelay(int32_t ms);
|
||||
x_err_t UserGetTaskName(int32_t id ,char *name);
|
||||
long UserTaskDelay(int32_t ms);
|
||||
long UserGetTaskName(int32_t id ,char *name);
|
||||
int32_t UserGetTaskID(void);
|
||||
uint8_t UserGetTaskStat(int32_t id);
|
||||
|
||||
#ifdef ARCH_SMP
|
||||
x_err_t UserTaskCoreCombine(int32_t id,uint8_t core_id);
|
||||
x_err_t UserTaskCoreUnCombine(int32_t id);
|
||||
long UserTaskCoreCombine(int32_t id,uint8_t core_id);
|
||||
long UserTaskCoreUnCombine(int32_t id);
|
||||
uint8_t UserGetTaskCombinedCore(int32_t id);
|
||||
uint8_t UserGetTaskRunningCore(int32_t id);
|
||||
#endif
|
||||
|
||||
x_err_t UserGetTaskErrorstatus(int32_t id);
|
||||
long UserGetTaskErrorstatus(int32_t id);
|
||||
uint8_t UserGetTaskPriority(int32_t id);
|
||||
|
||||
|
||||
|
@ -93,10 +94,10 @@ int32_t UserMutexAbandon(int32_t mutex);
|
|||
#ifdef KERNEL_SEMAPHORE
|
||||
typedef int32 sem_t;
|
||||
sem_t UserSemaphoreCreate(uint16_t val);
|
||||
x_err_t UserSemaphoreDelete(sem_t sem);
|
||||
x_err_t UserSemaphoreObtain(sem_t sem, int32_t wait_time);
|
||||
x_err_t UserSemaphoreAbandon(sem_t sem);
|
||||
x_err_t UserSemaphoreSetValue(sem_t sem, uint16_t val);
|
||||
long UserSemaphoreDelete(sem_t sem);
|
||||
long UserSemaphoreObtain(sem_t sem, int32_t wait_time);
|
||||
long UserSemaphoreAbandon(sem_t sem);
|
||||
long UserSemaphoreSetValue(sem_t sem, uint16_t val);
|
||||
#endif
|
||||
|
||||
|
||||
|
@ -104,22 +105,22 @@ x_err_t UserSemaphoreSetValue(sem_t sem, uint16_t val);
|
|||
typedef int32 EventIdType;
|
||||
EventIdType UserEventCreate(uint8_t flag);
|
||||
void UserEventDelete(EventIdType event);
|
||||
x_err_t UserEventTrigger(EventIdType event, uint32_t set);
|
||||
x_err_t UserEventProcess(EventIdType event, uint32_t set, uint8_t option,
|
||||
long UserEventTrigger(EventIdType event, uint32_t set);
|
||||
long UserEventProcess(EventIdType event, uint32_t set, uint8_t option,
|
||||
int32_t wait_time, uint32_t *Recved);
|
||||
x_err_t UserEventReinit(EventIdType event);
|
||||
long UserEventReinit(EventIdType event);
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef KERNEL_MESSAGEQUEUE
|
||||
int32_t UserMsgQueueCreate(size_t msg_size, size_t max_msgs);
|
||||
x_err_t UserMsgQueueDelete(int32_t mq );
|
||||
x_err_t UserMsgQueueSendwait(int32_t mq, const void *buffer,
|
||||
long UserMsgQueueDelete(int32_t mq );
|
||||
long UserMsgQueueSendwait(int32_t mq, const void *buffer,
|
||||
size_t size, int32_t wait_time);
|
||||
x_err_t UserMsgQueueSend(int32_t mq, const void *buffer, size_t size);
|
||||
x_err_t UserMsgQueueUrgentSend(int32_t mq, const void *buffer, size_t size);
|
||||
x_err_t UserMsgQueueRecv(int32_t mq, void *buffer, size_t size,int32_t wait_time);
|
||||
x_err_t UserMsgQueueReinit(int32_t mq);
|
||||
long UserMsgQueueSend(int32_t mq, const void *buffer, size_t size);
|
||||
long UserMsgQueueUrgentSend(int32_t mq, const void *buffer, size_t size);
|
||||
long UserMsgQueueRecv(int32_t mq, void *buffer, size_t size,int32_t wait_time);
|
||||
long UserMsgQueueReinit(int32_t mq);
|
||||
#endif
|
||||
|
||||
int open(const char *path, int flags, ...);
|
||||
|
@ -154,14 +155,18 @@ struct statfs {
|
|||
};
|
||||
|
||||
int statfs(const char *path, struct statfs *buf);
|
||||
#endif
|
||||
|
||||
/* NOTE!!!: when cutting out file system, the 'printf' function can not output angthing */
|
||||
int Userprintf(const char *fmt, ...);
|
||||
#define printf Userprintf
|
||||
|
||||
#define printf Userprintf
|
||||
#endif
|
||||
|
||||
|
||||
#else
|
||||
|
||||
#include <xiuos.h>
|
||||
|
||||
#ifdef FS_VFS
|
||||
#include <iot-vfs_posix.h>
|
||||
#endif
|
||||
|
@ -181,8 +186,9 @@ int32_t UserTaskCreate(UtaskType utask);
|
|||
#define UserTaskDelete KTaskDelete
|
||||
#define UserTaskQuit KTaskQuit
|
||||
#define UserTaskDelay MdelayKTask
|
||||
#define UserTaskSearch UTaskSearch
|
||||
|
||||
x_err_t UserGetTaskName(int32_t id ,char *name);
|
||||
long UserGetTaskName(int32_t id ,char *name);
|
||||
int32_t UserGetTaskID(void);
|
||||
uint8_t UserGetTaskStat(int32_t id);
|
||||
|
||||
|
@ -194,7 +200,7 @@ uint8_t UserGetTaskCombinedCore(int32_t id);
|
|||
uint8_t UserGetTaskRunningCore(int32_t id);
|
||||
#endif
|
||||
|
||||
x_err_t UserGetTaskErrorstatus(int32_t id);
|
||||
long UserGetTaskErrorstatus(int32_t id);
|
||||
uint8_t UserGetTaskPriority(int32_t id);
|
||||
|
||||
#define UserMalloc x_malloc
|
||||
|
|
|
@ -60,6 +60,17 @@ x_err_t UserTaskDelete(int32_t id){
|
|||
return (x_err_t)KSwitch1(KS_USER_TASK_DELETE,(uintptr_t)id);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will find the first user task in task manage list.
|
||||
*
|
||||
* @param
|
||||
*
|
||||
* @return task id on success; EINVALED on failure
|
||||
*/
|
||||
x_err_t UserTaskSearch(void){
|
||||
return KSwitch0(KS_USER_TASK_SEARCH);
|
||||
}
|
||||
|
||||
void UserTaskQuit(void){
|
||||
KSwitch0(KS_USER_TASK_EXECEXIT);
|
||||
return ;
|
||||
|
|
|
@ -51,6 +51,12 @@ menu "aiit-arm32-board feature"
|
|||
endif
|
||||
endmenu
|
||||
|
||||
menu "config board peripheral"
|
||||
config MOUNT_SDCARD
|
||||
bool "mount cd card"
|
||||
default n
|
||||
select BSP_USING_SDIO
|
||||
endmenu
|
||||
endmenu
|
||||
endmenu
|
||||
|
||||
|
|
|
@ -150,6 +150,24 @@ struct InitSequenceDesc _board_init[] =
|
|||
{ " NONE ",NONE },
|
||||
};
|
||||
|
||||
#if defined(FS_VFS) && defined(MOUNT_SDCARD)
|
||||
#include <iot-vfs.h>
|
||||
|
||||
/**
|
||||
* @description: Mount SD card
|
||||
* @return 0
|
||||
*/
|
||||
int MountSDCard(void)
|
||||
{
|
||||
if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, FSTYPE_FATFS, "/") == 0)
|
||||
DBG("sd card mount to '/'");
|
||||
else
|
||||
SYS_WARN("sd card mount to '/' failed!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void InitBoardHardware()
|
||||
{
|
||||
int i = 0;
|
||||
|
|
|
@ -53,6 +53,13 @@ menu "aiit-riscv64-board feature"
|
|||
endif
|
||||
endmenu
|
||||
|
||||
menu "config board peripheral"
|
||||
config MOUNT_SDCARD
|
||||
bool "mount cd card"
|
||||
default n
|
||||
select BSP_USING_SDIO
|
||||
endmenu
|
||||
|
||||
endmenu
|
||||
|
||||
config __STACKSIZE__
|
||||
|
|
|
@ -64,6 +64,28 @@ extern int HwRtcInit(void);
|
|||
extern int HwTouchBusInit(void);
|
||||
extern int HwCh376Init(void);
|
||||
|
||||
#if defined(FS_VFS) && defined(MOUNT_SDCARD)
|
||||
#include <iot-vfs.h>
|
||||
|
||||
/**
|
||||
* @description: Mount SD card
|
||||
* @return 0
|
||||
*/
|
||||
/**
|
||||
* @description: Mount SD card
|
||||
* @return 0
|
||||
*/
|
||||
int MountSDCard(void)
|
||||
{
|
||||
if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, FSTYPE_CH376, "/") == 0)
|
||||
DBG("sd card mount to '/'");
|
||||
else
|
||||
SYS_WARN("sd card mount to '/' failed!");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void init_bss(void)
|
||||
{
|
||||
unsigned int *dst;
|
||||
|
|
|
@ -58,6 +58,15 @@ menu "kd233 feature"
|
|||
|
||||
endmenu
|
||||
|
||||
menu "config board peripheral"
|
||||
config MOUNT_SDCARD
|
||||
bool "mount cd card"
|
||||
default n
|
||||
select BSP_USING_SPI
|
||||
select BSP_USING_SPI1
|
||||
select RESOURCES_SPI_SD
|
||||
endmenu
|
||||
|
||||
endmenu
|
||||
|
||||
config __STACKSIZE__
|
||||
|
|
|
@ -62,6 +62,32 @@ extern int HwWdtInit(void);
|
|||
extern int HwLcdInit(void);
|
||||
extern int HwTimerInit(void);
|
||||
|
||||
#if defined(FS_VFS) && defined (MOUNT_SDCARD)
|
||||
#include <iot-vfs.h>
|
||||
#include <sd_spi.h>
|
||||
extern SpiSdDeviceType SpiSdInit(struct Bus *bus, const char *dev_name, const char *drv_name, const char *sd_name);
|
||||
|
||||
/**
|
||||
* @description: Mount SD card
|
||||
* @return 0
|
||||
*/
|
||||
int MountSDCard(void)
|
||||
{
|
||||
struct Bus *spi_bus;
|
||||
spi_bus = BusFind(SPI_BUS_NAME_1);
|
||||
|
||||
if (NONE == SpiSdInit(spi_bus, SPI_1_DEVICE_NAME_0, SPI_1_DRV_NAME, SPI_SD_NAME)) {
|
||||
KPrintf("MountSDCard SpiSdInit error!\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (EOK == MountFilesystem(SPI_BUS_NAME_1, SPI_SD_NAME, SPI_1_DRV_NAME, FSTYPE_FATFS, "/"))
|
||||
KPrintf("SPI SD card fatfs mounted\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void InitBss(void)
|
||||
{
|
||||
unsigned int *dst;
|
||||
|
|
|
@ -45,7 +45,6 @@ extern int RtcNtpSyncInit(void);
|
|||
extern int MountSDCard(void);
|
||||
extern int DfsMountTable(void);
|
||||
extern int userShellInit(void);
|
||||
extern int Stm32SdcardMount(void);
|
||||
extern int STM32USBHostRegister(void);
|
||||
extern int WorkSysWorkQueueInit(void);
|
||||
|
||||
|
|
|
@ -159,6 +159,7 @@ int32 KTaskCreate(const char *name,
|
|||
void KTaskQuit(void);
|
||||
|
||||
#ifdef SEPARATE_COMPILE
|
||||
int UTaskSearch(void);
|
||||
int32 UTaskCreate(const char *name,
|
||||
void (*entry)(void *parameter),
|
||||
void *parameter,
|
||||
|
|
|
@ -33,6 +33,7 @@ enum KernelServiceEnum
|
|||
KS_USER_TASK_CREATE ,
|
||||
KS_USER_TASK_STARTUP ,
|
||||
KS_USER_TASK_DELETE,
|
||||
KS_USER_TASK_SEARCH,
|
||||
KS_USER_TASK_EXECEXIT,
|
||||
KS_USER_TASK_CORE_COMBINE,
|
||||
KS_USER_TASK_CORE_UNCOMBINE,
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
#include <xs_sem.h>
|
||||
#include <xs_memory.h>
|
||||
#include <xs_mutex.h>
|
||||
#include <xs_event.h>
|
||||
#include <xs_msg.h>
|
||||
|
||||
#ifdef FS_VFS
|
||||
#include <iot-vfs_posix.h>
|
||||
|
@ -118,6 +120,11 @@ uintptr_t KsTaskDelete(uint32_t knum,uintptr_t *param, uint8_t num)
|
|||
return (uintptr_t)ret;
|
||||
}
|
||||
|
||||
uintptr_t KsUserTaskSerach(uint32_t knum,uintptr_t *param, uint8_t num )
|
||||
{
|
||||
return (uintptr_t)UTaskSearch();
|
||||
}
|
||||
|
||||
extern void KTaskQuit(void);
|
||||
uintptr_t KsTaskQuit(uint32_t knum,uintptr_t *param, uint8_t num )
|
||||
{
|
||||
|
@ -565,6 +572,7 @@ struct KernelService g_service_table[256] __attribute__ ((section (".g_service_
|
|||
[KS_USER_TASK_CREATE] = { KsTaskCreate, 5 },
|
||||
[KS_USER_TASK_STARTUP] = { KsStartupTask, 1 },
|
||||
[KS_USER_TASK_DELETE] = { KsTaskDelete, 1 },
|
||||
[KS_USER_TASK_SEARCH] = { KsUserTaskSerach, 0 },
|
||||
[KS_USER_TASK_EXECEXIT] = { KsTaskQuit, 0 },
|
||||
[KS_USER_TASK_CORE_COMBINE] = { KsTaskCoreCombine, 2 },
|
||||
[KS_USER_TASK_CORE_UNCOMBINE] = { KsTaskCoreUnCombine, 1 },
|
||||
|
|
|
@ -350,11 +350,12 @@ static void* BigMemMalloc(struct DynamicBuddyMemory *dynamic_buddy, x_size_t siz
|
|||
|
||||
/* best-fit method */
|
||||
for (node = dynamic_buddy->mm_freenode_list[ndx].next;
|
||||
node && (node->size < allocsize);
|
||||
(ndx < MEM_LINKNRS ) && (node->size < allocsize);
|
||||
node = node->next) {
|
||||
ndx++;
|
||||
};
|
||||
/* get the best-fit freeNode */
|
||||
if (node && (node->size > allocsize)) {
|
||||
if (node && (node->size >= allocsize)) {
|
||||
struct DynamicFreeNode *remainder;
|
||||
struct DynamicFreeNode *next;
|
||||
uint32 remaining;
|
||||
|
@ -1197,25 +1198,47 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHE
|
|||
*/
|
||||
void ShowBuddy(void)
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
int lock = 0;
|
||||
struct DynamicFreeNode *debug = NONE;
|
||||
|
||||
lock = CriticalAreaLock();
|
||||
KPrintf("\n\033[41;1mlist memory information\033[0m\n", __func__);
|
||||
for (debug = ByteManager.dynamic_buddy_manager.mm_freenode_list[0].next;
|
||||
debug;debug = debug->next){
|
||||
KPrintf("%s,current is %x,next is %x, size %u, flag %x\n",__func__, debug, debug->next,debug->size,debug->flag);
|
||||
};
|
||||
for(int level = 0; level < MEM_LINKNRS; level++) {
|
||||
KPrintf("\n %s level [%d],memory size[2^%d] \n",__func__, level,level +6);
|
||||
for (debug = &ByteManager.dynamic_buddy_manager.mm_freenode_list[level]; ; ) {
|
||||
if(debug->size > 0)
|
||||
KPrintf(" [current node %x,next node %x, size %u, flag %x]\n",debug, debug->next,debug->size,debug->flag);
|
||||
else
|
||||
KPrintf(" [listhead node %x,next node %x]\n",debug, debug->next);
|
||||
if(debug->next)
|
||||
debug = debug->next;
|
||||
|
||||
if(debug->size == 0)
|
||||
break;
|
||||
};
|
||||
}
|
||||
|
||||
KPrintf("\nlist memory information\n\n");
|
||||
#ifdef MEM_EXTERN_SRAM
|
||||
for(i = 0; i < EXTSRAM_MAX_NUM; i++) {
|
||||
if(NONE != ExtByteManager[i].done){
|
||||
KPrintf("\nlist extern sram[%d] memory information\n\n",i);
|
||||
for (debug = ExtByteManager[i].dynamic_buddy_manager.mm_freenode_list[0].next;
|
||||
debug;debug = debug->next){
|
||||
KPrintf("%s,current is %x,next is %x, size %u, flag %x\n",__func__, debug, debug->next,debug->size,debug->flag);
|
||||
};
|
||||
for(int lev = 0; lev < MEM_LINKNRS; lev++) {
|
||||
KPrintf("\n %s level [%d],memory size[2^%d] \n",__func__, lev,lev +6);
|
||||
for (debug = & ExtByteManager[i].dynamic_buddy_manager.mm_freenode_list[lev]; ; ) {
|
||||
if(debug->size > 0)
|
||||
KPrintf(" [current node %x,next node %x, size %u, flag %x]\n",debug, debug->next,debug->size,debug->flag);
|
||||
else
|
||||
KPrintf(" [listhead node %x,next node %x]\n",debug, debug->next);
|
||||
if(debug->next)
|
||||
debug = debug->next;
|
||||
|
||||
if(debug->size == 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -104,11 +104,11 @@ struct InitSequenceDesc components_init[] =
|
|||
};
|
||||
struct InitSequenceDesc env_init[] =
|
||||
{
|
||||
#ifdef ARCH_RISCV
|
||||
#if defined (RESOURCES_SPI_SD)|| defined(RESOURCES_SDIO )
|
||||
|
||||
#ifdef MOUNT_SDCARD
|
||||
{ "MountSDCard", MountSDCard },
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef FS_VFS_MNTTABLE
|
||||
{ "DfsMountTable", DfsMountTable },
|
||||
#endif
|
||||
|
@ -119,9 +119,6 @@ struct InitSequenceDesc env_init[] =
|
|||
};
|
||||
struct InitSequenceDesc communication_init[] =
|
||||
{
|
||||
// #ifdef BSP_USING_SDIO
|
||||
// { "Stm32SdcardMount",Stm32SdcardMount },
|
||||
// #endif
|
||||
#ifdef BSP_USING_USBH
|
||||
{ "STM32USBHostRegister", STM32USBHostRegister },
|
||||
{ "hw usb", Stm32HwUsbInit },
|
||||
|
|
|
@ -1052,6 +1052,36 @@ x_err_t KTaskWakeup(int32 id)
|
|||
|
||||
|
||||
#ifdef SEPARATE_COMPILE
|
||||
/**
|
||||
* find the first user task in manage list.
|
||||
*
|
||||
* @param
|
||||
*
|
||||
* @note in interrupt status,this function is not permitted to call.
|
||||
*/
|
||||
int UTaskSearch(void)
|
||||
{
|
||||
x_base lock = 0;
|
||||
KTaskDescriptorType temp_task = NONE;
|
||||
struct SysDoubleLinklistNode *node = NONE;
|
||||
|
||||
lock = CriticalAreaLock();
|
||||
|
||||
DOUBLE_LINKLIST_FOR_EACH(node,&xiaoshan_task_head)
|
||||
{
|
||||
temp_task = SYS_DOUBLE_LINKLIST_ENTRY(node, struct TaskDescriptor, link);
|
||||
if (1 == temp_task->task_dync_sched_member.isolation_flag)
|
||||
{
|
||||
CriticalAreaUnLock(lock);
|
||||
return temp_task->id.id;
|
||||
}
|
||||
}
|
||||
|
||||
CriticalAreaUnLock(lock);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* This function init a user task in dynamic way .
|
||||
|
|
Loading…
Reference in New Issue