Compare commits
102 Commits
Author | SHA1 | Date |
---|---|---|
chunyexixiaoyu | b6bd1de959 | |
chunyexixiaoyu | be4da6f37d | |
chunyexixiaoyu | 12c9220487 | |
yangtuo250 | 62df72af5b | |
yangtuo250 | c782dd26c4 | |
yangtuo250 | efb28105e8 | |
yangtuo250 | 4640055ab6 | |
yangtuo250 | 203184e18c | |
yangtuo250 | c07c918150 | |
yangtuo250 | d368db9e76 | |
chunyexixiaoyu | 235dcb761c | |
chunyexixiaoyu | de27c59e51 | |
chunyexixiaoyu | d71c5fcd2f | |
chunyexixiaoyu | 6816dc023a | |
chunyexixiaoyu | d99d4e8f52 | |
chunyexixiaoyu | 60ce72b37a | |
xuedongliang | 20bc1e6e8b | |
bj | 668796ee18 | |
xuedongliang | 2356e42955 | |
xuedongliang | 3653ac690d | |
chunyexixiaoyu | c30ba15c6f | |
chunyexixiaoyu | 0379084f40 | |
chunyexixiaoyu | e70244d75e | |
Wang_Weigen | 4fc79c211a | |
Wang_Weigen | ac69e135ca | |
WentaoWong | 49caa6e533 | |
WentaoWong | 96ca3b18f1 | |
WentaoWong | 86cb28ee4f | |
WentaoWong | fe06b461ee | |
Wang_Weigen | 1c60efe8a8 | |
Wang_Weigen | 32cb324324 | |
Wang_Weigen | 592ca413e8 | |
Wang_Weigen | f24189d7e1 | |
Wang_Weigen | 9d970e7832 | |
Wang_Weigen | 5a8b6d79aa | |
chunyexixiaoyu | e0c78f200b | |
xuedongliang | 86b1d9c77d | |
chunyexixiaoyu | e33b0d0264 | |
chunyexixiaoyu | 2b69d31744 | |
chunyexixiaoyu | a9ac7efed2 | |
Liu_Weichao | 5fe8fb59b2 | |
Liu_Weichao | 7a172fd136 | |
xuedongliang | ca74d5cb93 | |
Wang_Weigen | 251846604a | |
Wang_Weigen | 2930757728 | |
Wang_Weigen | 25cd73821f | |
Wang_Weigen | eb14677832 | |
Liu_Weichao | c631063b7a | |
Wang_Weigen | f3d527cf70 | |
Wang_Weigen | b3c770dd26 | |
Wang_Weigen | 8609045f2a | |
Wang_Weigen | 9959ec6470 | |
Wang_Weigen | 8b2eee0aa0 | |
Wang_Weigen | 224201ba55 | |
xuedongliang | dfdc29b1d8 | |
xuedongliang | 410178e426 | |
bj | 471e2d7920 | |
chunyexixiaoyu | e69139c6b9 | |
chunyexixiaoyu | 4c0b05b752 | |
Wang_Weigen | 281a3b672a | |
Liu_Weichao | 7b82f2a60b | |
bj | 2f86d928c6 | |
Wang_Weigen | abe138ab27 | |
Wang_Weigen | 9bf26cdbf9 | |
Wang_Weigen | 267528408e | |
xuedongliang | 3f13410d96 | |
xuedongliang | ac588f2fcf | |
xuedongliang | 7d5149af24 | |
wang lingyu | 48a19fe349 | |
wang lingyu | 25221ac786 | |
Liu_Weichao | 06c9c6ecc4 | |
Liu_Weichao | 8f6cac9797 | |
Liu_Weichao | 8aac82b1b4 | |
chunyexixiaoyu | bea5eda2e0 | |
chunyexixiaoyu | 43f19c4b55 | |
chunyexixiaoyu | a184611226 | |
chunyexixiaoyu | 0eee31179c | |
chunyexixiaoyu | ecfbdb207f | |
Liu_Weichao | ab578dfe0e | |
Wang_Weigen | a4f24a29fc | |
xuedongliang | 2aa5c0f0ba | |
Liu_Weichao | a5e97b1431 | |
Liu_Weichao | 92347f7e29 | |
xuedongliang | b07ad19dc3 | |
Wang_Weigen | ebbb30c763 | |
xuedongliang | 58b3e0b6a6 | |
xuedongliang | a85e9fdb6d | |
Wang_Weigen | 626cbb084d | |
Wang_Weigen | b0f717bee4 | |
chunyexixiaoyu | 51d3a8ab41 | |
chunyexixiaoyu | d7e29f19cc | |
chunyexixiaoyu | d7cb0876f6 | |
Liu_Weichao | dd5a68916e | |
chunyexixiaoyu | d008cbf09f | |
chunyexixiaoyu | 2e209ef174 | |
chunyexixiaoyu | 3fdc18e837 | |
Wang_Weigen | 6be676af78 | |
Wang_Weigen | d14ec3cb12 | |
xuedongliang | 4a07f534d8 | |
xuedongliang | 869e69097f | |
Wang_Weigen | d77c1828b2 | |
Liu_Weichao | bb93aefe1d |
|
@ -1,6 +1,6 @@
|
|||
[submodule "Ubiquitous/RT_Thread/rt-thread"]
|
||||
path = Ubiquitous/RT_Thread/rt-thread
|
||||
url = https://git.trustie.net/chunyexixiaoyu/rt-thread.git
|
||||
url = https://code.gitlink.org.cn/chunyexixiaoyu/rt-thread.git
|
||||
[submodule "Ubiquitous/RT_Thread/bsp/k210/kendryte-sdk/kendryte-sdk-source"]
|
||||
path = Ubiquitous/RT_Thread/bsp/k210/kendryte-sdk/kendryte-sdk-source
|
||||
url = https://git.trustie.net/chunyexixiaoyu/kendryte-sdk-source.git
|
||||
path = Ubiquitous/RT_Thread/aiit_board/k210/kendryte-sdk/kendryte-sdk-source
|
||||
url = https://code.gitlink.org.cn/chunyexixiaoyu/kendryte-sdk-source.git
|
||||
|
|
|
@ -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,9 +1,15 @@
|
|||
import os
|
||||
Import('RTT_ROOT')
|
||||
Import('rtconfig')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
|
||||
SOURCES = ['framework_init.c']
|
||||
path = [cwd]
|
||||
objs = []
|
||||
group = DefineGroup('sensor', SOURCES, depend = [], CPPPATH = [cwd])
|
||||
objs = objs + group
|
||||
list = os.listdir(cwd)
|
||||
|
||||
for d in list:
|
||||
|
|
|
@ -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
|
|
@ -11,13 +11,18 @@
|
|||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <user_api.h>
|
||||
#include <transform.h>
|
||||
|
||||
extern int SensorFrameworkInit(void);
|
||||
extern int AdapterFrameworkInit(void);
|
||||
|
||||
extern int Adapter4GInit(void);
|
||||
extern int AdapterNbiotInit(void);
|
||||
extern int AdapterBlueToothInit(void);
|
||||
extern int AdapterWifiInit(void);
|
||||
extern int AdapterEthernetInit(void);
|
||||
extern int AdapterZigbeeInit(void);
|
||||
extern int AdapterLoraInit(void);
|
||||
|
||||
extern int D124VoiceInit(void);
|
||||
extern int Hs300xTemperatureInit(void);
|
||||
|
@ -89,11 +94,26 @@ static struct InitDesc sensor_desc[] =
|
|||
|
||||
static struct InitDesc connection_desc[] =
|
||||
{
|
||||
#ifdef ADAPTER_4G
|
||||
{ "4G adpter", Adapter4GInit},
|
||||
#ifdef CONNECTION_ADAPTER_4G
|
||||
{ "4G adapter", Adapter4GInit},
|
||||
#endif
|
||||
#ifdef ADAPTER_WIFI
|
||||
{ "Wifi adpter", AdapterWifiInit},
|
||||
#ifdef CONNECTION_ADAPTER_NB
|
||||
{ "NB adpter", AdapterNbiotInit},
|
||||
#endif
|
||||
#ifdef CONNECTION_ADAPTER_ZIGBEE
|
||||
{ "zigbee adapter", AdapterZigbeeInit},
|
||||
#endif
|
||||
#ifdef CONNECTION_ADAPTER_BLUETOOTH
|
||||
{ "bluetooth adapter", AdapterBlueToothInit},
|
||||
#endif
|
||||
#ifdef CONNECTION_ADAPTER_WIFI
|
||||
{ "wifi adapter", AdapterWifiInit},
|
||||
#endif
|
||||
#ifdef CONNECTION_ADAPTER_ETHERNET
|
||||
{ "ethernet adapter", AdapterEthernetInit},
|
||||
#endif
|
||||
#ifdef CONNECTION_ADAPTER_LORA
|
||||
{ "lora adapter", AdapterLoraInit},
|
||||
#endif
|
||||
{ "NULL", NULL },
|
||||
};
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
import os
|
||||
from building import *
|
||||
Import('RTT_ROOT')
|
||||
Import('rtconfig')
|
||||
cwd = GetCurrentDir()
|
||||
DEPENDS = [""]
|
||||
|
||||
SOURCES = ['double_list.c'] + ['single_list.c']
|
||||
path = [cwd]
|
||||
objs = DefineGroup('list', src = SOURCES, depend = DEPENDS,CPPPATH = path)
|
||||
Return("objs")
|
|
@ -23,7 +23,7 @@
|
|||
#define __LIST_H__
|
||||
|
||||
#include "libc.h"
|
||||
|
||||
#include<stddef.h>
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
|
|
@ -5,9 +5,11 @@ menu "knowing app"
|
|||
|
||||
if APPLICATION_KNOWING
|
||||
source "$APP_DIR/Applications/knowing_app/mnist/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/face_detect/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/instrusion_detect/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/helmet_detect/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/k210_detect_entry/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/iris_ml_demo/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/k210_fft_test/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/image_processing/Kconfig"
|
||||
source "$APP_DIR/Applications/knowing_app/cmsis_5_demo/Kconfig"
|
||||
|
||||
endif
|
||||
endmenu
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
menuconfig USING_CMSIS_5_DEMOAPP
|
||||
bool "CMSIS-5 demo app"
|
||||
depends on USING_USING_CMSIS_5_NN
|
||||
default n
|
||||
|
||||
if USING_CMSIS_5_DEMOAPP
|
||||
|
||||
config USING_CMSIS_5_NN_DEMOAPP
|
||||
bool "Using CMSIS-5 NN demo app"
|
||||
select USING_IMAGE_PROCESSING
|
||||
select IMAGE_PROCESSING_USING_TJPGD
|
||||
default n
|
||||
|
||||
endif
|
||||
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
# CMSIS-NN cifar10 example
|
||||
|
||||
The model of this example is from [[ARM-software](https://github.com/ARM-software)/**[ML-examples](https://github.com/ARM-software/ML-examples)**] and can be deployed on Arm Cortex-M CPUs using [CMSIS-NN](https://github.com/ARM-software/CMSIS_5).
|
||||
|
||||
## Requirements:
|
||||
- CMSIS-NN in Framework/knowing/cmsis_5
|
||||
- TJpgDec in Framework/knowing/image_processing
|
||||
- Enough stack size (recommend 10240) for finsh thread which can be changed in "RT-Thread Components->Command shell->finsh shell" by menuconfig.
|
||||
|
||||
## To run this demo:
|
||||
- Place the photo where you want
|
||||
- Run demo by type the command
|
||||
```
|
||||
cmsisnn_demo /path/to/photo
|
|
@ -0,0 +1,18 @@
|
|||
from building import *
|
||||
import os
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
|
||||
src = Split('''
|
||||
model/m4/nn.c
|
||||
demo/cmsisnn_demo.c
|
||||
''')
|
||||
|
||||
path = [
|
||||
cwd + '/model/m4',
|
||||
cwd + '/demo'
|
||||
]
|
||||
|
||||
group = DefineGroup('CMSISNN-cifar10', src, depend = ['USING_CMSIS_5_DEMOAPP'], CPPPATH = path)
|
||||
|
||||
Return('group')
|
Binary file not shown.
After Width: | Height: | Size: 1000 B |
|
@ -0,0 +1,157 @@
|
|||
#include <transform.h>
|
||||
#include <tjpgd.h>
|
||||
#include "../model/m4/nn.h"
|
||||
|
||||
#define WORK_POOL_SIZE (4 * 1024 + 32)
|
||||
|
||||
const char *cifar10_label[] = {"Plane", "Car", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Ship", "Truck"};
|
||||
|
||||
int get_top_prediction(q7_t *predictions)
|
||||
{
|
||||
int max_ind = 0;
|
||||
int max_val = -128;
|
||||
for (int i = 0; i < 10; i++)
|
||||
{
|
||||
if (max_val < predictions[i])
|
||||
{
|
||||
max_val = predictions[i];
|
||||
max_ind = i;
|
||||
}
|
||||
}
|
||||
return max_ind;
|
||||
}
|
||||
|
||||
int cmsisnn_inference(uint8_t *input_data)
|
||||
{
|
||||
q7_t output_data[10];
|
||||
run_nn((q7_t *)input_data, output_data);
|
||||
arm_softmax_q7(output_data, IP1_OUT_DIM, output_data);
|
||||
int top_ind = get_top_prediction(output_data);
|
||||
printf("\rPrediction: %s \r\n", cifar10_label[top_ind]);
|
||||
return top_ind;
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
FILE *fp;
|
||||
uint8_t *fbuf;
|
||||
uint16_t wfbuf;
|
||||
} IODEV;
|
||||
|
||||
unsigned int in_func_cmsisnn(JDEC *jd, uint8_t *buff, unsigned int nbyte)
|
||||
{
|
||||
IODEV *dev = (IODEV *)jd->device;
|
||||
|
||||
if (buff)
|
||||
{
|
||||
return (uint16_t)fread(buff, 1, nbyte, dev->fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte;
|
||||
}
|
||||
}
|
||||
|
||||
int out_func_cmsisnn(JDEC *jd, void *bitmap, JRECT *rect)
|
||||
{
|
||||
IODEV *dev = (IODEV *)jd->device;
|
||||
uint8_t *src, *dst;
|
||||
uint16_t y, bws, bwd;
|
||||
|
||||
if (rect->left == 0)
|
||||
{
|
||||
printf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height);
|
||||
}
|
||||
|
||||
src = (uint8_t *)bitmap;
|
||||
dst = dev->fbuf + 3 * (rect->top * dev->wfbuf + rect->left);
|
||||
bws = 3 * (rect->right - rect->left + 1);
|
||||
bwd = 3 * dev->wfbuf;
|
||||
for (y = rect->top; y <= rect->bottom; y++)
|
||||
{
|
||||
memcpy(dst, src, bws);
|
||||
src += bws;
|
||||
dst += bwd;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int cmsisnn_demo(int argc, char *argv[])
|
||||
{
|
||||
void *work;
|
||||
JDEC jdec;
|
||||
JRESULT res;
|
||||
IODEV devid;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("Jpeg_Dec illegal arguments ...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
devid.fp = fopen(argv[1], "r+");
|
||||
if (!devid.fp)
|
||||
{
|
||||
printf("Jpeg_Dec open the file failed...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
work = malloc(WORK_POOL_SIZE);
|
||||
if (work == NULL)
|
||||
{
|
||||
printf("Jpeg_Dec work malloc failed...\n");
|
||||
res = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
res = jd_prepare(&jdec, in_func_cmsisnn, work, WORK_POOL_SIZE, &devid);
|
||||
if (res == JDR_OK)
|
||||
{
|
||||
printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool);
|
||||
|
||||
devid.fbuf = malloc(3 * jdec.width * jdec.height);
|
||||
if (devid.fbuf == NULL)
|
||||
{
|
||||
printf("Jpeg_Dec devid.fbuf malloc failed, need to use %d Bytes ...\n", 3 * jdec.width * jdec.height);
|
||||
res = -1;
|
||||
goto __exit;
|
||||
}
|
||||
devid.wfbuf = jdec.width;
|
||||
|
||||
res = jd_decomp(&jdec, out_func_cmsisnn, 0);
|
||||
if (res == JDR_OK)
|
||||
{
|
||||
printf("\rDecompress success \n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to decompress: rc=%d\n", res);
|
||||
}
|
||||
|
||||
cmsisnn_inference(devid.fbuf);
|
||||
|
||||
if (devid.fbuf != NULL)
|
||||
{
|
||||
free(devid.fbuf);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to prepare: rc=%d\n", res);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (work != NULL)
|
||||
{
|
||||
free(work);
|
||||
}
|
||||
|
||||
fclose(devid.fp);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(cmsisnn_demo, cifar10 demo and filename should be followed);
|
||||
#endif
|
|
@ -0,0 +1,46 @@
|
|||
#include "nn.h"
|
||||
|
||||
// Timer t;
|
||||
|
||||
static uint8_t mean[DATA_OUT_CH*DATA_OUT_DIM*DATA_OUT_DIM] = MEAN_DATA;
|
||||
|
||||
static q7_t conv1_wt[CONV1_IN_CH*CONV1_KER_DIM*CONV1_KER_DIM*CONV1_OUT_CH] = CONV1_WT;
|
||||
static q7_t conv1_bias[CONV1_OUT_CH] = CONV1_BIAS;
|
||||
|
||||
static q7_t conv2_wt[CONV2_IN_CH*CONV2_KER_DIM*CONV2_KER_DIM*CONV2_OUT_CH] = CONV2_WT;
|
||||
static q7_t conv2_bias[CONV2_OUT_CH] = CONV2_BIAS;
|
||||
|
||||
static q7_t conv3_wt[CONV3_IN_CH*CONV3_KER_DIM*CONV3_KER_DIM*CONV3_OUT_CH] = CONV3_WT;
|
||||
static q7_t conv3_bias[CONV3_OUT_CH] = CONV3_BIAS;
|
||||
|
||||
static q7_t ip1_wt[IP1_IN_DIM*IP1_OUT_DIM] = IP1_WT;
|
||||
static q7_t ip1_bias[IP1_OUT_DIM] = IP1_BIAS;
|
||||
|
||||
//Add input_data and output_data in top main.cpp file
|
||||
//uint8_t input_data[DATA_OUT_CH*DATA_OUT_DIM*DATA_OUT_DIM];
|
||||
//q7_t output_data[IP1_OUT_DIM];
|
||||
|
||||
q7_t col_buffer[3200];
|
||||
q7_t scratch_buffer[40960];
|
||||
|
||||
void mean_subtract(q7_t* image_data) {
|
||||
for(int i=0; i<DATA_OUT_CH*DATA_OUT_DIM*DATA_OUT_DIM; i++) {
|
||||
image_data[i] = (q7_t)__SSAT( ((int)(image_data[i] - mean[i]) >> DATA_RSHIFT), 8);
|
||||
}
|
||||
}
|
||||
|
||||
void run_nn(q7_t* input_data, q7_t* output_data) {
|
||||
q7_t* buffer1 = scratch_buffer;
|
||||
q7_t* buffer2 = buffer1 + 32768;
|
||||
mean_subtract(input_data);
|
||||
arm_convolve_HWC_q7_RGB(input_data, CONV1_IN_DIM, CONV1_IN_CH, conv1_wt, CONV1_OUT_CH, CONV1_KER_DIM, CONV1_PAD, CONV1_STRIDE, conv1_bias, CONV1_BIAS_LSHIFT, CONV1_OUT_RSHIFT, buffer1, CONV1_OUT_DIM, (q15_t*)col_buffer, NULL);
|
||||
arm_maxpool_q7_HWC(buffer1, POOL1_IN_DIM, POOL1_IN_CH, POOL1_KER_DIM, POOL1_PAD, POOL1_STRIDE, POOL1_OUT_DIM, col_buffer, buffer2);
|
||||
arm_relu_q7(buffer2, RELU1_OUT_DIM*RELU1_OUT_DIM*RELU1_OUT_CH);
|
||||
arm_convolve_HWC_q7_fast(buffer2, CONV2_IN_DIM, CONV2_IN_CH, conv2_wt, CONV2_OUT_CH, CONV2_KER_DIM, CONV2_PAD, CONV2_STRIDE, conv2_bias, CONV2_BIAS_LSHIFT, CONV2_OUT_RSHIFT, buffer1, CONV2_OUT_DIM, (q15_t*)col_buffer, NULL);
|
||||
arm_relu_q7(buffer1, RELU2_OUT_DIM*RELU2_OUT_DIM*RELU2_OUT_CH);
|
||||
arm_avepool_q7_HWC(buffer1, POOL2_IN_DIM, POOL2_IN_CH, POOL2_KER_DIM, POOL2_PAD, POOL2_STRIDE, POOL2_OUT_DIM, col_buffer, buffer2);
|
||||
arm_convolve_HWC_q7_fast(buffer2, CONV3_IN_DIM, CONV3_IN_CH, conv3_wt, CONV3_OUT_CH, CONV3_KER_DIM, CONV3_PAD, CONV3_STRIDE, conv3_bias, CONV3_BIAS_LSHIFT, CONV3_OUT_RSHIFT, buffer1, CONV3_OUT_DIM, (q15_t*)col_buffer, NULL);
|
||||
arm_relu_q7(buffer1, RELU3_OUT_DIM*RELU3_OUT_DIM*RELU3_OUT_CH);
|
||||
arm_avepool_q7_HWC(buffer1, POOL3_IN_DIM, POOL3_IN_CH, POOL3_KER_DIM, POOL3_PAD, POOL3_STRIDE, POOL3_OUT_DIM, col_buffer, buffer2);
|
||||
arm_fully_connected_q7_opt(buffer2, ip1_wt, IP1_IN_DIM, IP1_OUT_DIM, IP1_BIAS_LSHIFT, IP1_OUT_RSHIFT, ip1_bias, output_data, (q15_t*)col_buffer);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __NN_H__
|
||||
#define __NN_H__
|
||||
|
||||
// #include "mbed.h"
|
||||
#include "arm_math.h"
|
||||
#include "parameter.h"
|
||||
#include "weights.h"
|
||||
#include "arm_nnfunctions.h"
|
||||
|
||||
void run_nn(q7_t* input_data, q7_t* output_data);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,69 @@
|
|||
#define DATA_OUT_CH 3
|
||||
#define DATA_OUT_DIM 32
|
||||
|
||||
#define CONV1_IN_DIM 32
|
||||
#define CONV1_IN_CH 3
|
||||
#define CONV1_KER_DIM 5
|
||||
#define CONV1_PAD 2
|
||||
#define CONV1_STRIDE 1
|
||||
#define CONV1_OUT_CH 32
|
||||
#define CONV1_OUT_DIM 32
|
||||
|
||||
#define POOL1_IN_DIM 32
|
||||
#define POOL1_IN_CH 32
|
||||
#define POOL1_KER_DIM 3
|
||||
#define POOL1_STRIDE 2
|
||||
#define POOL1_PAD 0
|
||||
#define POOL1_OUT_DIM 16
|
||||
|
||||
#define RELU1_OUT_CH 32
|
||||
#define RELU1_OUT_DIM 16
|
||||
|
||||
#define CONV2_IN_DIM 16
|
||||
#define CONV2_IN_CH 32
|
||||
#define CONV2_KER_DIM 5
|
||||
#define CONV2_PAD 2
|
||||
#define CONV2_STRIDE 1
|
||||
#define CONV2_OUT_CH 16
|
||||
#define CONV2_OUT_DIM 16
|
||||
|
||||
#define RELU2_OUT_CH 16
|
||||
#define RELU2_OUT_DIM 16
|
||||
|
||||
#define POOL2_IN_DIM 16
|
||||
#define POOL2_IN_CH 16
|
||||
#define POOL2_KER_DIM 3
|
||||
#define POOL2_STRIDE 2
|
||||
#define POOL2_PAD 0
|
||||
#define POOL2_OUT_DIM 8
|
||||
|
||||
#define CONV3_IN_DIM 8
|
||||
#define CONV3_IN_CH 16
|
||||
#define CONV3_KER_DIM 5
|
||||
#define CONV3_PAD 2
|
||||
#define CONV3_STRIDE 1
|
||||
#define CONV3_OUT_CH 32
|
||||
#define CONV3_OUT_DIM 8
|
||||
|
||||
#define RELU3_OUT_CH 32
|
||||
#define RELU3_OUT_DIM 8
|
||||
|
||||
#define POOL3_IN_DIM 8
|
||||
#define POOL3_IN_CH 32
|
||||
#define POOL3_KER_DIM 3
|
||||
#define POOL3_STRIDE 2
|
||||
#define POOL3_PAD 0
|
||||
#define POOL3_OUT_DIM 4
|
||||
|
||||
#define IP1_IN_DIM 512
|
||||
#define IP1_OUT_DIM 10
|
||||
|
||||
#define DATA_RSHIFT 0
|
||||
#define CONV1_BIAS_LSHIFT 0
|
||||
#define CONV1_OUT_RSHIFT 11
|
||||
#define CONV2_BIAS_LSHIFT 0
|
||||
#define CONV2_OUT_RSHIFT 8
|
||||
#define CONV3_BIAS_LSHIFT 0
|
||||
#define CONV3_OUT_RSHIFT 8
|
||||
#define IP1_BIAS_LSHIFT 5
|
||||
#define IP1_OUT_RSHIFT 7
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,46 @@
|
|||
#include "nn.h"
|
||||
|
||||
// Timer t;
|
||||
|
||||
static uint8_t mean[DATA_OUT_CH*DATA_OUT_DIM*DATA_OUT_DIM] = MEAN_DATA;
|
||||
|
||||
static q7_t conv1_wt[CONV1_IN_CH*CONV1_KER_DIM*CONV1_KER_DIM*CONV1_OUT_CH] = CONV1_WT;
|
||||
static q7_t conv1_bias[CONV1_OUT_CH] = CONV1_BIAS;
|
||||
|
||||
static q7_t conv2_wt[CONV2_IN_CH*CONV2_KER_DIM*CONV2_KER_DIM*CONV2_OUT_CH] = CONV2_WT;
|
||||
static q7_t conv2_bias[CONV2_OUT_CH] = CONV2_BIAS;
|
||||
|
||||
static q7_t conv3_wt[CONV3_IN_CH*CONV3_KER_DIM*CONV3_KER_DIM*CONV3_OUT_CH] = CONV3_WT;
|
||||
static q7_t conv3_bias[CONV3_OUT_CH] = CONV3_BIAS;
|
||||
|
||||
static q7_t ip1_wt[IP1_IN_DIM*IP1_OUT_DIM] = IP1_WT;
|
||||
static q7_t ip1_bias[IP1_OUT_DIM] = IP1_BIAS;
|
||||
|
||||
//Add input_data and output_data in top main.cpp file
|
||||
//uint8_t input_data[DATA_OUT_CH*DATA_OUT_DIM*DATA_OUT_DIM];
|
||||
//q7_t output_data[IP1_OUT_DIM];
|
||||
|
||||
q7_t col_buffer[6400];
|
||||
q7_t scratch_buffer[40960];
|
||||
|
||||
void mean_subtract(q7_t* image_data) {
|
||||
for(int i=0; i<DATA_OUT_CH*DATA_OUT_DIM*DATA_OUT_DIM; i++) {
|
||||
image_data[i] = (q7_t)__SSAT( ((int)(image_data[i] - mean[i]) >> DATA_RSHIFT), 8);
|
||||
}
|
||||
}
|
||||
|
||||
void run_nn(q7_t* input_data, q7_t* output_data) {
|
||||
q7_t* buffer1 = scratch_buffer;
|
||||
q7_t* buffer2 = buffer1 + 32768;
|
||||
mean_subtract(input_data);
|
||||
arm_convolve_HWC_q7_RGB(input_data, CONV1_IN_DIM, CONV1_IN_CH, conv1_wt, CONV1_OUT_CH, CONV1_KER_DIM, CONV1_PAD, CONV1_STRIDE, conv1_bias, CONV1_BIAS_LSHIFT, CONV1_OUT_RSHIFT, buffer1, CONV1_OUT_DIM, (q15_t*)col_buffer, NULL);
|
||||
arm_maxpool_q7_HWC(buffer1, POOL1_IN_DIM, POOL1_IN_CH, POOL1_KER_DIM, POOL1_PAD, POOL1_STRIDE, POOL1_OUT_DIM, col_buffer, buffer2);
|
||||
arm_relu_q7(buffer2, RELU1_OUT_DIM*RELU1_OUT_DIM*RELU1_OUT_CH);
|
||||
arm_convolve_HWC_q7_fast(buffer2, CONV2_IN_DIM, CONV2_IN_CH, conv2_wt, CONV2_OUT_CH, CONV2_KER_DIM, CONV2_PAD, CONV2_STRIDE, conv2_bias, CONV2_BIAS_LSHIFT, CONV2_OUT_RSHIFT, buffer1, CONV2_OUT_DIM, (q15_t*)col_buffer, NULL);
|
||||
arm_relu_q7(buffer1, RELU2_OUT_DIM*RELU2_OUT_DIM*RELU2_OUT_CH);
|
||||
arm_avepool_q7_HWC(buffer1, POOL2_IN_DIM, POOL2_IN_CH, POOL2_KER_DIM, POOL2_PAD, POOL2_STRIDE, POOL2_OUT_DIM, col_buffer, buffer2);
|
||||
arm_convolve_HWC_q7_fast(buffer2, CONV3_IN_DIM, CONV3_IN_CH, conv3_wt, CONV3_OUT_CH, CONV3_KER_DIM, CONV3_PAD, CONV3_STRIDE, conv3_bias, CONV3_BIAS_LSHIFT, CONV3_OUT_RSHIFT, buffer1, CONV3_OUT_DIM, (q15_t*)col_buffer, NULL);
|
||||
arm_relu_q7(buffer1, RELU3_OUT_DIM*RELU3_OUT_DIM*RELU3_OUT_CH);
|
||||
arm_avepool_q7_HWC(buffer1, POOL3_IN_DIM, POOL3_IN_CH, POOL3_KER_DIM, POOL3_PAD, POOL3_STRIDE, POOL3_OUT_DIM, col_buffer, buffer2);
|
||||
arm_fully_connected_q7_opt(buffer2, ip1_wt, IP1_IN_DIM, IP1_OUT_DIM, IP1_BIAS_LSHIFT, IP1_OUT_RSHIFT, ip1_bias, output_data, (q15_t*)col_buffer);
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __NN_H__
|
||||
#define __NN_H__
|
||||
|
||||
// #include "mbed.h"
|
||||
#include "arm_math.h"
|
||||
#include "parameter.h"
|
||||
#include "weights.h"
|
||||
#include "arm_nnfunctions.h"
|
||||
|
||||
void run_nn(q7_t* input_data, q7_t* output_data);
|
||||
|
||||
#endif
|
|
@ -0,0 +1,69 @@
|
|||
#define DATA_OUT_CH 3
|
||||
#define DATA_OUT_DIM 32
|
||||
|
||||
#define CONV1_IN_DIM 32
|
||||
#define CONV1_IN_CH 3
|
||||
#define CONV1_KER_DIM 5
|
||||
#define CONV1_PAD 2
|
||||
#define CONV1_STRIDE 1
|
||||
#define CONV1_OUT_CH 32
|
||||
#define CONV1_OUT_DIM 32
|
||||
|
||||
#define POOL1_IN_DIM 32
|
||||
#define POOL1_IN_CH 32
|
||||
#define POOL1_KER_DIM 3
|
||||
#define POOL1_STRIDE 2
|
||||
#define POOL1_PAD 0
|
||||
#define POOL1_OUT_DIM 16
|
||||
|
||||
#define RELU1_OUT_CH 32
|
||||
#define RELU1_OUT_DIM 16
|
||||
|
||||
#define CONV2_IN_DIM 16
|
||||
#define CONV2_IN_CH 32
|
||||
#define CONV2_KER_DIM 5
|
||||
#define CONV2_PAD 2
|
||||
#define CONV2_STRIDE 1
|
||||
#define CONV2_OUT_CH 32
|
||||
#define CONV2_OUT_DIM 16
|
||||
|
||||
#define RELU2_OUT_CH 32
|
||||
#define RELU2_OUT_DIM 16
|
||||
|
||||
#define POOL2_IN_DIM 16
|
||||
#define POOL2_IN_CH 32
|
||||
#define POOL2_KER_DIM 3
|
||||
#define POOL2_STRIDE 2
|
||||
#define POOL2_PAD 0
|
||||
#define POOL2_OUT_DIM 8
|
||||
|
||||
#define CONV3_IN_DIM 8
|
||||
#define CONV3_IN_CH 32
|
||||
#define CONV3_KER_DIM 5
|
||||
#define CONV3_PAD 2
|
||||
#define CONV3_STRIDE 1
|
||||
#define CONV3_OUT_CH 64
|
||||
#define CONV3_OUT_DIM 8
|
||||
|
||||
#define RELU3_OUT_CH 64
|
||||
#define RELU3_OUT_DIM 8
|
||||
|
||||
#define POOL3_IN_DIM 8
|
||||
#define POOL3_IN_CH 64
|
||||
#define POOL3_KER_DIM 3
|
||||
#define POOL3_STRIDE 2
|
||||
#define POOL3_PAD 0
|
||||
#define POOL3_OUT_DIM 4
|
||||
|
||||
#define IP1_IN_DIM 1024
|
||||
#define IP1_OUT_DIM 10
|
||||
|
||||
#define DATA_RSHIFT 0
|
||||
#define CONV1_BIAS_LSHIFT 0
|
||||
#define CONV1_OUT_RSHIFT 9
|
||||
#define CONV2_BIAS_LSHIFT 0
|
||||
#define CONV2_OUT_RSHIFT 9
|
||||
#define CONV3_BIAS_LSHIFT 0
|
||||
#define CONV3_OUT_RSHIFT 9
|
||||
#define IP1_BIAS_LSHIFT 3
|
||||
#define IP1_OUT_RSHIFT 5
|
File diff suppressed because one or more lines are too long
|
@ -1,8 +0,0 @@
|
|||
config FACE_DETECT
|
||||
bool "enable apps/face detect"
|
||||
depends on BOARD_K210_EVB
|
||||
depends on DRV_USING_OV2640
|
||||
depends on USING_KPU_POSTPROCESSING
|
||||
depends on USING_YOLOV2
|
||||
select LIB_USING_CJSON
|
||||
default n
|
|
@ -1,373 +0,0 @@
|
|||
#include <transform.h>
|
||||
#ifdef LIB_USING_CJSON
|
||||
#include <cJSON.h>
|
||||
#endif
|
||||
#include "region_layer.h"
|
||||
#define ANCHOR_NUM 5
|
||||
#define STACK_SIZE (128 * 1024)
|
||||
#define JSON_FILE_PATH "/kmodel/detect.json"
|
||||
#define JSON_BUFFER_SIZE (4 * 1024)
|
||||
|
||||
// params from json
|
||||
static float anchor[ANCHOR_NUM * 2] = {};
|
||||
static int net_output_shape[3] = {};
|
||||
static int net_input_size[2] = {};
|
||||
static int sensor_output_size[2] = {};
|
||||
static char kmodel_path[127] = "";
|
||||
static int kmodel_size = 0;
|
||||
static float obj_thresh[20] = {};
|
||||
static float nms_thresh = 0.0;
|
||||
static char labels[20][32] = {};
|
||||
static int class_num = 0;
|
||||
|
||||
#define THREAD_PRIORITY_FACE_D (11)
|
||||
static pthread_t facetid = 0;
|
||||
static void *thread_face_detcet_entry(void *parameter);
|
||||
static int g_fd = 0;
|
||||
static int kmodel_fd = 0;
|
||||
static int if_exit = 0;
|
||||
static unsigned char *showbuffer = NULL;
|
||||
static unsigned char *kpurgbbuffer = NULL;
|
||||
|
||||
static _ioctl_shoot_para shoot_para_t = {0};
|
||||
unsigned char *model_data = NULL; // kpu data load memory
|
||||
unsigned char *model_data_align = NULL;
|
||||
|
||||
kpu_model_context_t face_detect_task;
|
||||
static region_layer_t face_detect_rl;
|
||||
static obj_info_t face_detect_info;
|
||||
volatile uint32_t g_ai_done_flag;
|
||||
|
||||
static void ai_done(void *ctx) { g_ai_done_flag = 1; }
|
||||
|
||||
static void param_parse()
|
||||
{
|
||||
int fin;
|
||||
char buffer[JSON_BUFFER_SIZE] = "";
|
||||
// char *buffer;
|
||||
// if (NULL != (buffer = (char*)malloc(JSON_BUFFER_SIZE * sizeof(char)))) {
|
||||
// memset(buffer, 0, JSON_BUFFER_SIZE * sizeof(char));
|
||||
// } else {
|
||||
// printf("Json buffer malloc failed!");
|
||||
// exit(-1);
|
||||
// }
|
||||
int array_size;
|
||||
cJSON *json_obj;
|
||||
cJSON *json_item;
|
||||
cJSON *json_array_item;
|
||||
|
||||
fin = open(JSON_FILE_PATH, O_RDONLY);
|
||||
if (!fin) {
|
||||
printf("Error open file %s", JSON_FILE_PATH);
|
||||
exit(-1);
|
||||
}
|
||||
read(fin, buffer, sizeof(buffer));
|
||||
close(fin);
|
||||
|
||||
// read json string
|
||||
json_obj = cJSON_Parse(buffer);
|
||||
// free(buffer);
|
||||
char *json_print_str = cJSON_Print(json_obj);
|
||||
printf("Json file content: \n%s\n", json_print_str);
|
||||
cJSON_free(json_print_str);
|
||||
// get anchors
|
||||
json_item = cJSON_GetObjectItem(json_obj, "anchors");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (ANCHOR_NUM * 2 != array_size) {
|
||||
printf("Expect anchor size: %d, got %d in json file", ANCHOR_NUM * 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d anchors from json file\n", ANCHOR_NUM);
|
||||
}
|
||||
for (int i = 0; i < ANCHOR_NUM * 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
anchor[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, anchor[i]);
|
||||
}
|
||||
// net_input_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_input_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect net_input_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_input_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
net_input_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, net_input_size[i]);
|
||||
}
|
||||
// net_output_shape
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_output_shape");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (3 != array_size) {
|
||||
printf("Expect net_output_shape: %d, got %d in json file", 3, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_output_shape from json file\n", 3);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
net_output_shape[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, net_output_shape[i]);
|
||||
}
|
||||
// sensor_output_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "sensor_output_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect sensor_output_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d sensor_output_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
sensor_output_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, sensor_output_size[i]);
|
||||
}
|
||||
// kmodel_path
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_path");
|
||||
memcpy(kmodel_path, json_item->valuestring, strlen(json_item->valuestring));
|
||||
printf("Got kmodel_path: %s\n", kmodel_path);
|
||||
// kmodel_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_size");
|
||||
kmodel_size = json_item->valueint;
|
||||
printf("Got kmodel_size: %d\n", kmodel_size);
|
||||
// labels
|
||||
json_item = cJSON_GetObjectItem(json_obj, "labels");
|
||||
class_num = cJSON_GetArraySize(json_item);
|
||||
if (0 >= class_num) {
|
||||
printf("No labels!");
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d labels\n", class_num);
|
||||
}
|
||||
for (int i = 0; i < class_num; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
memcpy(labels[i], json_array_item->valuestring, strlen(json_array_item->valuestring));
|
||||
printf("%d: %s\n", i, labels[i]);
|
||||
}
|
||||
// obj_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "obj_thresh");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (class_num != array_size) {
|
||||
printf("label number and thresh number mismatch! label number : %d, obj thresh number %d", class_num, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d obj_thresh\n", array_size);
|
||||
}
|
||||
for (int i = 0; i < array_size; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
obj_thresh[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, obj_thresh[i]);
|
||||
}
|
||||
// nms_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "nms_thresh");
|
||||
nms_thresh = json_item->valuedouble;
|
||||
printf("Got nms_thresh: %f\n", nms_thresh);
|
||||
|
||||
cJSON_Delete(json_obj);
|
||||
return;
|
||||
}
|
||||
|
||||
void face_detect()
|
||||
{
|
||||
int ret = 0;
|
||||
int result = 0;
|
||||
int size = 0;
|
||||
param_parse();
|
||||
g_fd = open("/dev/ov2640", O_RDONLY);
|
||||
if (g_fd < 0) {
|
||||
printf("open ov2640 fail !!");
|
||||
return;
|
||||
}
|
||||
_ioctl_set_dvp_reso set_dvp_reso = {sensor_output_size[1], sensor_output_size[0]};
|
||||
ioctl(g_fd, IOCTRL_CAMERA_SET_DVP_RESO, &set_dvp_reso);
|
||||
showbuffer = (unsigned char *)malloc(sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
if (NULL == showbuffer) {
|
||||
close(g_fd);
|
||||
printf("showbuffer apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
kpurgbbuffer = (unsigned char *)malloc(net_input_size[0] * net_input_size[1] * 3);
|
||||
if (NULL == kpurgbbuffer) {
|
||||
close(g_fd);
|
||||
free(showbuffer);
|
||||
printf("kpurgbbuffer apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
model_data = (unsigned char *)malloc(kmodel_size + 255);
|
||||
if (NULL == model_data) {
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
close(g_fd);
|
||||
printf("model_data apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
memset(model_data, 0, kmodel_size + 255);
|
||||
memset(showbuffer, 0, sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
memset(kpurgbbuffer, 0, net_input_size[0] * net_input_size[1] * 3);
|
||||
shoot_para_t.pdata = (unsigned int *)(showbuffer);
|
||||
shoot_para_t.length = (size_t)(sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
/*
|
||||
load memory
|
||||
*/
|
||||
kmodel_fd = open(kmodel_path, O_RDONLY);
|
||||
if (kmodel_fd < 0) {
|
||||
printf("open kmodel fail");
|
||||
close(g_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
} else {
|
||||
size = read(kmodel_fd, model_data, kmodel_size);
|
||||
if (size != kmodel_size) {
|
||||
printf("read kmodel error size %d\n", size);
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
|
||||
} else {
|
||||
printf("read kmodel success \n");
|
||||
}
|
||||
}
|
||||
unsigned char *model_data_align = (unsigned char *)(((unsigned int)model_data + 255) & (~255));
|
||||
dvp_set_ai_addr((uint32_t)kpurgbbuffer, (uint32_t)(kpurgbbuffer + net_input_size[0] * net_input_size[1]),
|
||||
(uint32_t)(kpurgbbuffer + net_input_size[0] * net_input_size[1] * 2));
|
||||
if (kpu_load_kmodel(&face_detect_task, model_data_align) != 0) {
|
||||
printf("\nmodel init error\n");
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
}
|
||||
face_detect_rl.anchor_number = ANCHOR_NUM;
|
||||
face_detect_rl.anchor = anchor;
|
||||
face_detect_rl.threshold = malloc(class_num * sizeof(float));
|
||||
for (int idx = 0; idx < class_num; idx++) {
|
||||
face_detect_rl.threshold[idx] = obj_thresh[idx];
|
||||
}
|
||||
face_detect_rl.nms_value = nms_thresh;
|
||||
result = region_layer_init(&face_detect_rl, net_output_shape[0], net_output_shape[1], net_output_shape[2],
|
||||
net_input_size[1], net_input_size[0]);
|
||||
printf("region_layer_init result %d \n\r", result);
|
||||
size_t stack_size = STACK_SIZE;
|
||||
pthread_attr_t attr; /* 线程属性 */
|
||||
struct sched_param prio; /* 线程优先级 */
|
||||
prio.sched_priority = 8; /* 优先级设置为 8 */
|
||||
pthread_attr_init(&attr); /* 先使用默认值初始化属性 */
|
||||
pthread_attr_setschedparam(&attr, &prio); /* 修改属性对应的优先级 */
|
||||
pthread_attr_setstacksize(&attr, stack_size);
|
||||
|
||||
/* 创建线程 1, 属性为 attr,入口函数是 thread_entry,入口函数参数是 1 */
|
||||
result = pthread_create(&facetid, &attr, thread_face_detcet_entry, NULL);
|
||||
if (0 == result) {
|
||||
printf("thread_face_detcet_entry successfully!\n");
|
||||
} else {
|
||||
printf("thread_face_detcet_entry failed! error code is %d\n", result);
|
||||
close(g_fd);
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(face_detect, face detect task);
|
||||
#endif
|
||||
|
||||
static void *thread_face_detcet_entry(void *parameter)
|
||||
{
|
||||
extern void lcd_draw_picture(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint32_t * ptr);
|
||||
printf("thread_face_detcet_entry start!\n");
|
||||
int ret = 0;
|
||||
// sysctl_enable_irq();
|
||||
while (1) {
|
||||
// memset(showbuffer,0,320*240*2);
|
||||
g_ai_done_flag = 0;
|
||||
ret = ioctl(g_fd, IOCTRL_CAMERA_START_SHOT, &shoot_para_t);
|
||||
if (RT_ERROR == ret) {
|
||||
printf("ov2640 can't wait event flag");
|
||||
rt_free(showbuffer);
|
||||
close(g_fd);
|
||||
pthread_exit(NULL);
|
||||
return NULL;
|
||||
}
|
||||
kpu_run_kmodel(&face_detect_task, kpurgbbuffer, DMAC_CHANNEL5, ai_done, NULL);
|
||||
while (!g_ai_done_flag)
|
||||
;
|
||||
float *output;
|
||||
size_t output_size;
|
||||
kpu_get_output(&face_detect_task, 0, (uint8_t **)&output, &output_size);
|
||||
face_detect_rl.input = output;
|
||||
region_layer_run(&face_detect_rl, &face_detect_info);
|
||||
/* display result */
|
||||
#ifdef BSP_USING_LCD
|
||||
for (int face_cnt = 0; face_cnt < face_detect_info.obj_number; face_cnt++) {
|
||||
draw_edge((uint32_t *)showbuffer, &face_detect_info, face_cnt, 0xF800, (uint16_t)sensor_output_size[1],
|
||||
(uint16_t)sensor_output_size[0]);
|
||||
printf("%d: (%d, %d, %d, %d) cls: %s conf: %f\t", face_cnt, face_detect_info.obj[face_cnt].x1,
|
||||
face_detect_info.obj[face_cnt].y1, face_detect_info.obj[face_cnt].x2, face_detect_info.obj[face_cnt].y2,
|
||||
labels[face_detect_info.obj[face_cnt].class_id], face_detect_info.obj[face_cnt].prob);
|
||||
}
|
||||
if (0 != face_detect_info.obj_number) printf("\n");
|
||||
lcd_draw_picture(0, 0, (uint16_t)sensor_output_size[1], (uint16_t)sensor_output_size[0], (unsigned int *)showbuffer);
|
||||
#endif
|
||||
usleep(1);
|
||||
if (1 == if_exit) {
|
||||
if_exit = 0;
|
||||
printf("thread_face_detcet_entry exit");
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void face_detect_delete()
|
||||
{
|
||||
if (showbuffer != NULL) {
|
||||
int ret = 0;
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
printf("face detect task cancel!!! ret %d ", ret);
|
||||
if_exit = 1;
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(face_detect_delete, face detect task delete);
|
||||
#endif
|
||||
|
||||
void kmodel_load(unsigned char *model_data)
|
||||
{
|
||||
int kmodel_fd = 0;
|
||||
int size = 0;
|
||||
kmodel_fd = open(kmodel_path, O_RDONLY);
|
||||
|
||||
model_data = (unsigned char *)malloc(kmodel_size + 255);
|
||||
if (NULL == model_data) {
|
||||
printf("model_data apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
memset(model_data, 0, kmodel_size + 255);
|
||||
|
||||
if (kmodel_fd >= 0) {
|
||||
size = read(kmodel_fd, model_data, kmodel_size);
|
||||
if (size != kmodel_size) {
|
||||
printf("read kmodel error size %d\n", size);
|
||||
|
||||
} else {
|
||||
printf("read kmodel success");
|
||||
}
|
||||
} else {
|
||||
free(model_data);
|
||||
printf("open kmodel fail");
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(kmodel_load, kmodel load memory);
|
||||
#endif
|
|
@ -1,8 +0,0 @@
|
|||
config HELMET_DETECT
|
||||
bool "enable apps/helmet detect"
|
||||
depends on BOARD_K210_EVB
|
||||
depends on DRV_USING_OV2640
|
||||
depends on USING_KPU_POSTPROCESSING
|
||||
depends on USING_YOLOV2
|
||||
select LIB_USING_CJSON
|
||||
default n
|
|
@ -1,167 +0,0 @@
|
|||
# Helmet detection demo
|
||||
|
||||
### A helmet and head without helmet object detection task demo. Running MobileNet-yolo on K210-based edge devices.
|
||||
|
||||
---
|
||||
|
||||
## Training
|
||||
|
||||
### Enviroment preparation
|
||||
|
||||
Model generated by [aXeleRate](https://forgeplus.trustie.net/projects/yangtuo250/aXeleRate) and converted to kmodel by [nncase](https://github.com/kendryte/nncase/tree/v0.1.0-rc5).
|
||||
|
||||
```shell
|
||||
# master branch for MobileNetv1-yolov2 and unstable branch to test MobileNetv1(v2)-yolov2(v3)
|
||||
git clone https://git.trustie.net/yangtuo250/aXeleRate.git (-b unstable)
|
||||
cd aXeleRate
|
||||
pip install -r requirments.txt && pip install -e .
|
||||
```
|
||||
|
||||
### training config setting
|
||||
|
||||
Example [config](https://forgeplus.trustie.net/projects/yangtuo250/aXeleRate/tree/master/configs/detector.json), some hyper-parameters:
|
||||
|
||||
- architecture: backbone, MobileNet7_5 for default, MobileNet1_0(α = 1.0) and above cannot run on K210 because of OOM on feature map in master branch. For unstable branch MobileNetV2_1_0 is OK.
|
||||
|
||||
- input_size: fixed model input size, single integer for height equals to width, otherwise a list([height, width]).
|
||||
- anchors: yolov2 anchor(for master) or anchor scaled to 1.0(for unstable), can be generate by [darknet](https://github.com/AlexeyAB/darknet).
|
||||
- labels: labels of all classes.
|
||||
- train(valid)_image(annot)_folder: path of images and annoations for training and validation.
|
||||
- saved_folder: path for trainig result storage(models, checkpoints, logs ...).
|
||||
|
||||
Mine config for unstable:
|
||||
```json
|
||||
{
|
||||
"model": {
|
||||
"type": "Detector",
|
||||
"architecture": "MobileNetV2_1_0",
|
||||
"input_size": [
|
||||
224,
|
||||
320
|
||||
],
|
||||
"anchors": [
|
||||
[
|
||||
[
|
||||
0.1043,
|
||||
0.1560
|
||||
],
|
||||
[
|
||||
0.0839,
|
||||
0.3036
|
||||
],
|
||||
[
|
||||
0.1109,
|
||||
0.3923
|
||||
],
|
||||
[
|
||||
0.1378,
|
||||
0.5244
|
||||
],
|
||||
[
|
||||
0.2049,
|
||||
0.6673
|
||||
]
|
||||
]
|
||||
],
|
||||
"labels": [
|
||||
"human"
|
||||
],
|
||||
"obj_thresh": 0.5,
|
||||
"iou_thresh": 0.45,
|
||||
"coord_scale": 1.0,
|
||||
"class_scale": 0.0,
|
||||
"object_scale": 5.0,
|
||||
"no_object_scale": 3.0
|
||||
},
|
||||
"weights": {
|
||||
"full": "",
|
||||
"backend": ""
|
||||
},
|
||||
"train": {
|
||||
"actual_epoch": 2000,
|
||||
"train_image_folder": "mydata/human/Images/train",
|
||||
"train_annot_folder": "mydata/human/Annotations/train",
|
||||
"train_times": 2,
|
||||
"valid_image_folder": "mydata/human/Images/val",
|
||||
"valid_annot_folder": "mydata/human/Annotations/val",
|
||||
"valid_times": 1,
|
||||
"valid_metric": "precision",
|
||||
"batch_size": 32,
|
||||
"learning_rate": 2e-5,
|
||||
"saved_folder": "mydata/human/results",
|
||||
"first_trainable_layer": "",
|
||||
"augmentation": true,
|
||||
"is_only_detect": false,
|
||||
"validation_freq": 5,
|
||||
"quantize": false,
|
||||
"class_weights": [1.0]
|
||||
},
|
||||
"converter": {
|
||||
"type": [
|
||||
"k210"
|
||||
]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
*(For more detailed config usage, please refer to original aXeleRate repo.)*
|
||||
|
||||
### data preparation
|
||||
|
||||
Please refer to [VOC format](https://towardsdatascience.com/coco-data-format-for-object-detection-a4c5eaf518c5), path as config above.
|
||||
|
||||
### train it!
|
||||
|
||||
```shell
|
||||
python -m aXeleRate.train -c PATH_TO_YOUR_CONFIG
|
||||
```
|
||||
|
||||
### model convert
|
||||
|
||||
Please refer to [nncase repo](https://github.com/kendryte/nncase/tree/v0.1.0-rc5).
|
||||
|
||||
---
|
||||
|
||||
## Deployment
|
||||
|
||||
### compile and burn
|
||||
|
||||
Use `(scons --)menuconfig` in bsp folder *(Ubiquitous/RT_Thread/bsp/k210)*, open:
|
||||
|
||||
- More Drivers --> ov2640 driver
|
||||
- Board Drivers Config --> Enable LCD on SPI0
|
||||
- Board Drivers Config --> Enable SDCARD (spi1(ss0))
|
||||
- Board Drivers Config --> Enable DVP(camera)
|
||||
- RT-Thread Components --> POSIX layer and C standard library --> Enable pthreads APIs
|
||||
- APP_Framework --> Framework --> support knowing framework --> kpu model postprocessing --> yolov2 region layer
|
||||
- APP_Framework --> Applications --> knowing app --> enable apps/helmet detect
|
||||
|
||||
`scons -j(n)` to compile and burn in by *kflash*.
|
||||
|
||||
### json config and kmodel
|
||||
|
||||
Copy json config for deployment o SD card */kmodel*. Example config file is *helmet.json* in this directory. Something to be modified:
|
||||
|
||||
- net_input_size: same as *input_size* in training config file, but array only.
|
||||
- net_output_shape: final feature map size, can be found in **nncase** output.
|
||||
- sensor_output_size: image height and width from camera.
|
||||
- kmodel_size: kmodel size shown in file system.
|
||||
- anchors: same as *anchor* in training config file(multi-dimention anchors flatten to 1 dim).
|
||||
- labels: same as *label* in training config file.
|
||||
- obj_thresh: array, object threshold of each label.
|
||||
- nms_thresh: NMS threshold of boxes.
|
||||
|
||||
Copy final kmodel to SD card */kmodel* either.
|
||||
|
||||
---
|
||||
|
||||
## Run
|
||||
|
||||
In serial terminal, `helmet_detect` to start a detection thread, `helmet_detect_delete` to stop it. Detection results can be found in output.
|
||||
|
||||
---
|
||||
|
||||
## TODO
|
||||
|
||||
- [ ] Fix LCD real-time result display.
|
||||
- [ ] Test more object detection backbone and algorithm(like yolox).
|
|
@ -1,380 +0,0 @@
|
|||
#include <transform.h>
|
||||
#ifdef LIB_USING_CJSON
|
||||
#include <cJSON.h>
|
||||
#endif
|
||||
#include "region_layer.h"
|
||||
#define ANCHOR_NUM 5
|
||||
#define STACK_SIZE (128 * 1024)
|
||||
#define JSON_FILE_PATH "/kmodel/helmet.json"
|
||||
#define JSON_BUFFER_SIZE (4 * 1024)
|
||||
|
||||
// params from json
|
||||
static float anchor[ANCHOR_NUM * 2] = {};
|
||||
static int net_output_shape[3] = {};
|
||||
static int net_input_size[2] = {};
|
||||
static int sensor_output_size[2] = {};
|
||||
static char kmodel_path[127] = "";
|
||||
static int kmodel_size = 0;
|
||||
static float obj_thresh[20] = {};
|
||||
static float nms_thresh = 0.0;
|
||||
static char labels[20][32] = {};
|
||||
static int class_num = 0;
|
||||
|
||||
#define THREAD_PRIORITY_HELMET_D (11)
|
||||
static pthread_t helmettid = 0;
|
||||
static void *thread_helmet_detect_entry(void *parameter);
|
||||
static int g_fd = 0;
|
||||
static int kmodel_fd = 0;
|
||||
static int if_exit = 0;
|
||||
static unsigned char *showbuffer = NULL;
|
||||
static unsigned char *kpurgbbuffer = NULL;
|
||||
|
||||
static _ioctl_shoot_para shoot_para_t = {0};
|
||||
unsigned char *model_data = NULL; // kpu data load memory
|
||||
unsigned char *model_data_align = NULL;
|
||||
|
||||
kpu_model_context_t helmet_detect_task;
|
||||
static region_layer_t helmet_detect_rl;
|
||||
static obj_info_t helmet_detect_info;
|
||||
volatile uint32_t g_ai_done_flag;
|
||||
|
||||
static void ai_done(void *ctx) { g_ai_done_flag = 1; }
|
||||
|
||||
static void param_parse()
|
||||
{
|
||||
int fin;
|
||||
char buffer[JSON_BUFFER_SIZE] = "";
|
||||
// char *buffer;
|
||||
// if (NULL != (buffer = (char*)malloc(JSON_BUFFER_SIZE * sizeof(char)))) {
|
||||
// memset(buffer, 0, JSON_BUFFER_SIZE * sizeof(char));
|
||||
// } else {
|
||||
// printf("Json buffer malloc failed!");
|
||||
// exit(-1);
|
||||
// }
|
||||
int array_size;
|
||||
cJSON *json_obj;
|
||||
cJSON *json_item;
|
||||
cJSON *json_array_item;
|
||||
|
||||
fin = open(JSON_FILE_PATH, O_RDONLY);
|
||||
if (!fin) {
|
||||
printf("Error open file %s", JSON_FILE_PATH);
|
||||
exit(-1);
|
||||
}
|
||||
read(fin, buffer, sizeof(buffer));
|
||||
close(fin);
|
||||
|
||||
// read json string
|
||||
json_obj = cJSON_Parse(buffer);
|
||||
// free(buffer);
|
||||
char *json_print_str = cJSON_Print(json_obj);
|
||||
printf("Json file content: \n%s\n", json_print_str);
|
||||
cJSON_free(json_print_str);
|
||||
// get anchors
|
||||
json_item = cJSON_GetObjectItem(json_obj, "anchors");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (ANCHOR_NUM * 2 != array_size) {
|
||||
printf("Expect anchor size: %d, got %d in json file", ANCHOR_NUM * 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d anchors from json file\n", ANCHOR_NUM);
|
||||
}
|
||||
for (int i = 0; i < ANCHOR_NUM * 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
anchor[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, anchor[i]);
|
||||
}
|
||||
// net_input_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_input_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect net_input_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_input_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
net_input_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, net_input_size[i]);
|
||||
}
|
||||
// net_output_shape
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_output_shape");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (3 != array_size) {
|
||||
printf("Expect net_output_shape: %d, got %d in json file", 3, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_output_shape from json file\n", 3);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
net_output_shape[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, net_output_shape[i]);
|
||||
}
|
||||
// sensor_output_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "sensor_output_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect sensor_output_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d sensor_output_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
sensor_output_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, sensor_output_size[i]);
|
||||
}
|
||||
// kmodel_path
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_path");
|
||||
memcpy(kmodel_path, json_item->valuestring, strlen(json_item->valuestring));
|
||||
printf("Got kmodel_path: %s\n", kmodel_path);
|
||||
// kmodel_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_size");
|
||||
kmodel_size = json_item->valueint;
|
||||
printf("Got kmodel_size: %d\n", kmodel_size);
|
||||
// labels
|
||||
json_item = cJSON_GetObjectItem(json_obj, "labels");
|
||||
class_num = cJSON_GetArraySize(json_item);
|
||||
if (0 >= class_num) {
|
||||
printf("No labels!");
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d labels\n", class_num);
|
||||
}
|
||||
for (int i = 0; i < class_num; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
memcpy(labels[i], json_array_item->valuestring, strlen(json_array_item->valuestring));
|
||||
printf("%d: %s\n", i, labels[i]);
|
||||
}
|
||||
// obj_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "obj_thresh");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (class_num != array_size) {
|
||||
printf("label number and thresh number mismatch! label number : %d, obj thresh number %d", class_num, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d obj_thresh\n", array_size);
|
||||
}
|
||||
for (int i = 0; i < array_size; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
obj_thresh[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, obj_thresh[i]);
|
||||
}
|
||||
// nms_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "nms_thresh");
|
||||
nms_thresh = json_item->valuedouble;
|
||||
printf("Got nms_thresh: %f\n", nms_thresh);
|
||||
|
||||
cJSON_Delete(json_obj);
|
||||
return;
|
||||
}
|
||||
|
||||
void helmet_detect()
|
||||
{
|
||||
int ret = 0;
|
||||
int result = 0;
|
||||
int size = 0;
|
||||
param_parse();
|
||||
g_fd = open("/dev/ov2640", O_RDONLY);
|
||||
if (g_fd < 0) {
|
||||
printf("open ov2640 fail !!");
|
||||
return;
|
||||
}
|
||||
_ioctl_set_dvp_reso set_dvp_reso = {sensor_output_size[1], sensor_output_size[0]};
|
||||
ioctl(g_fd, IOCTRL_CAMERA_SET_DVP_RESO, &set_dvp_reso);
|
||||
showbuffer = (unsigned char *)malloc(sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
if (NULL == showbuffer) {
|
||||
close(g_fd);
|
||||
printf("showbuffer apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
kpurgbbuffer = (unsigned char *)malloc(net_input_size[0] * net_input_size[1] * 3);
|
||||
if (NULL == kpurgbbuffer) {
|
||||
close(g_fd);
|
||||
free(showbuffer);
|
||||
printf("kpurgbbuffer apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
model_data = (unsigned char *)malloc(kmodel_size + 255);
|
||||
if (NULL == model_data) {
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
close(g_fd);
|
||||
printf("model_data apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
memset(model_data, 0, kmodel_size + 255);
|
||||
memset(showbuffer, 0, sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
memset(kpurgbbuffer, 127, net_input_size[0] * net_input_size[1] * 3);
|
||||
shoot_para_t.pdata = (unsigned int *)(showbuffer);
|
||||
shoot_para_t.length = (size_t)(sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
/*
|
||||
load memory
|
||||
*/
|
||||
kmodel_fd = open(kmodel_path, O_RDONLY);
|
||||
if (kmodel_fd < 0) {
|
||||
printf("open kmodel fail");
|
||||
close(g_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
} else {
|
||||
size = read(kmodel_fd, model_data, kmodel_size);
|
||||
if (size != kmodel_size) {
|
||||
printf("read kmodel error size %d\n", size);
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
|
||||
} else {
|
||||
printf("read kmodel success \n");
|
||||
}
|
||||
}
|
||||
unsigned char *model_data_align = (unsigned char *)(((unsigned int)model_data + 255) & (~255));
|
||||
dvp_set_ai_addr((uint32_t)(kpurgbbuffer + net_input_size[1] * (net_input_size[0] - sensor_output_size[0])),
|
||||
(uint32_t)(kpurgbbuffer + net_input_size[1] * (net_input_size[0] - sensor_output_size[0]) +
|
||||
net_input_size[0] * net_input_size[1]),
|
||||
(uint32_t)(kpurgbbuffer + net_input_size[0] * net_input_size[1] * 2 +
|
||||
net_input_size[1] * (net_input_size[0] - sensor_output_size[0])));
|
||||
if (kpu_load_kmodel(&helmet_detect_task, model_data_align) != 0) {
|
||||
printf("\nmodel init error\n");
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
}
|
||||
helmet_detect_rl.anchor_number = ANCHOR_NUM;
|
||||
helmet_detect_rl.anchor = anchor;
|
||||
helmet_detect_rl.threshold = malloc(class_num * sizeof(float));
|
||||
for (int idx = 0; idx < class_num; idx++) {
|
||||
helmet_detect_rl.threshold[idx] = obj_thresh[idx];
|
||||
}
|
||||
helmet_detect_rl.nms_value = nms_thresh;
|
||||
result = region_layer_init(&helmet_detect_rl, net_output_shape[0], net_output_shape[1], net_output_shape[2],
|
||||
net_input_size[1], net_input_size[0]);
|
||||
printf("region_layer_init result %d \n\r", result);
|
||||
size_t stack_size = STACK_SIZE;
|
||||
pthread_attr_t attr; /* 线程属性 */
|
||||
struct sched_param prio; /* 线程优先级 */
|
||||
prio.sched_priority = 8; /* 优先级设置为 8 */
|
||||
pthread_attr_init(&attr); /* 先使用默认值初始化属性 */
|
||||
pthread_attr_setschedparam(&attr, &prio); /* 修改属性对应的优先级 */
|
||||
pthread_attr_setstacksize(&attr, stack_size);
|
||||
|
||||
/* 创建线程 1, 属性为 attr,入口函数是 thread_entry,入口函数参数是 1 */
|
||||
result = pthread_create(&helmettid, &attr, thread_helmet_detect_entry, NULL);
|
||||
if (0 == result) {
|
||||
printf("thread_helmet_detect_entry successfully!\n");
|
||||
} else {
|
||||
printf("thread_helmet_detect_entry failed! error code is %d\n", result);
|
||||
close(g_fd);
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(helmet_detect, helmet detect task);
|
||||
#endif
|
||||
|
||||
static void *thread_helmet_detect_entry(void *parameter)
|
||||
{
|
||||
extern void lcd_draw_picture(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint32_t * ptr);
|
||||
printf("thread_helmet_detect_entry start!\n");
|
||||
int ret = 0;
|
||||
// sysctl_enable_irq();
|
||||
while (1) {
|
||||
// memset(showbuffer,0,320*240*2);
|
||||
g_ai_done_flag = 0;
|
||||
ret = ioctl(g_fd, IOCTRL_CAMERA_START_SHOT, &shoot_para_t);
|
||||
if (RT_ERROR == ret) {
|
||||
printf("ov2640 can't wait event flag");
|
||||
rt_free(showbuffer);
|
||||
close(g_fd);
|
||||
pthread_exit(NULL);
|
||||
return NULL;
|
||||
}
|
||||
kpu_run_kmodel(&helmet_detect_task, kpurgbbuffer, DMAC_CHANNEL5, ai_done, NULL);
|
||||
while (!g_ai_done_flag)
|
||||
;
|
||||
float *output;
|
||||
size_t output_size;
|
||||
kpu_get_output(&helmet_detect_task, 0, (uint8_t **)&output, &output_size);
|
||||
helmet_detect_rl.input = output;
|
||||
region_layer_run(&helmet_detect_rl, &helmet_detect_info);
|
||||
/* display result */
|
||||
#ifdef BSP_USING_LCD
|
||||
for (int helmet_cnt = 0; helmet_cnt < helmet_detect_info.obj_number; helmet_cnt++) {
|
||||
// draw_edge((uint32_t *)showbuffer, &helmet_detect_info, helmet_cnt, 0xF800,
|
||||
// (uint16_t)sensor_output_size[1],
|
||||
// (uint16_t)sensor_output_size[0]);
|
||||
printf("%d: (%d, %d, %d, %d) cls: %s conf: %f\t", helmet_cnt, helmet_detect_info.obj[helmet_cnt].x1,
|
||||
helmet_detect_info.obj[helmet_cnt].y1, helmet_detect_info.obj[helmet_cnt].x2,
|
||||
helmet_detect_info.obj[helmet_cnt].y2, labels[helmet_detect_info.obj[helmet_cnt].class_id],
|
||||
helmet_detect_info.obj[helmet_cnt].prob);
|
||||
}
|
||||
if (0 != helmet_detect_info.obj_number) {
|
||||
printf("\n");
|
||||
}
|
||||
lcd_draw_picture(0, 0, (uint16_t)sensor_output_size[1], (uint16_t)sensor_output_size[0], (unsigned int *)showbuffer);
|
||||
#endif
|
||||
usleep(1);
|
||||
if (1 == if_exit) {
|
||||
if_exit = 0;
|
||||
printf("thread_helmet_detect_entry exit");
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void helmet_detect_delete()
|
||||
{
|
||||
if (showbuffer != NULL) {
|
||||
int ret = 0;
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
printf("helmet detect task cancel!!! ret %d ", ret);
|
||||
if_exit = 1;
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(helmet_detect_delete, helmet detect task delete);
|
||||
#endif
|
||||
|
||||
void kmodel_load(unsigned char *model_data)
|
||||
{
|
||||
int kmodel_fd = 0;
|
||||
int size = 0;
|
||||
kmodel_fd = open(kmodel_path, O_RDONLY);
|
||||
|
||||
model_data = (unsigned char *)malloc(kmodel_size + 255);
|
||||
if (NULL == model_data) {
|
||||
printf("model_data apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
memset(model_data, 0, kmodel_size + 255);
|
||||
|
||||
if (kmodel_fd >= 0) {
|
||||
size = read(kmodel_fd, model_data, kmodel_size);
|
||||
if (size != kmodel_size) {
|
||||
printf("read kmodel error size %d\n", size);
|
||||
|
||||
} else {
|
||||
printf("read kmodel success");
|
||||
}
|
||||
} else {
|
||||
free(model_data);
|
||||
printf("open kmodel fail");
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(kmodel_load, kmodel load memory);
|
||||
#endif
|
|
@ -0,0 +1,6 @@
|
|||
menuconfig USING_IMAGE_PROCESSING_APP
|
||||
bool "image processing app "
|
||||
default n
|
||||
if USING_IMAGE_PROCESSING_APP
|
||||
source "$APP_DIR/Applications/knowing_app/image_processing/TJpgDec_APP/Kconfig"
|
||||
endif
|
|
@ -0,0 +1,14 @@
|
|||
import os
|
||||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
objs = []
|
||||
list = os.listdir(cwd)
|
||||
|
||||
for d in list:
|
||||
path = os.path.join(cwd, d)
|
||||
if os.path.isfile(os.path.join(path, 'SConscript')):
|
||||
objs = objs + SConscript(os.path.join(path, 'SConscript'))
|
||||
|
||||
Return('objs')
|
|
@ -0,0 +1,4 @@
|
|||
config IMAGE_PROCESSING_TJPGDEC_APP
|
||||
bool "image processing apps/TJpgDec(example)"
|
||||
select IMAGE_PROCESSING_USING_TJPGD
|
||||
default n
|
|
@ -1,9 +1,9 @@
|
|||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c') + Glob('*.cpp')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('Applications', src, depend = ['FACE_DETECT'], LOCAL_CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Glob('*.c') + Glob('*.cpp')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('TJpgDec(example)', src, depend = ['IMAGE_PROCESSING_TJPGDEC_APP'], LOCAL_CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
|
@ -0,0 +1,151 @@
|
|||
#include <transform.h>
|
||||
#include <tjpgd.h>
|
||||
#define WORK_POOL_SIZE (4*1024+32)//This value depends on the resolution of the image
|
||||
|
||||
/* User defined device identifier */
|
||||
typedef struct {
|
||||
FILE *fp; /* File pointer for input function */
|
||||
uint8_t *fbuf; /* Pointer to the frame buffer for output function */
|
||||
uint16_t wfbuf; /* Width of the frame buffer [pix] */
|
||||
} IODEV;
|
||||
|
||||
|
||||
/*------------------------------*/
|
||||
/* User defined input funciton */
|
||||
/*------------------------------*/
|
||||
|
||||
unsigned int in_func (JDEC* jd, uint8_t* buff, unsigned int nbyte)
|
||||
{
|
||||
IODEV *dev = (IODEV*)jd->device; /* Device identifier for the session (5th argument of jd_prepare function) */
|
||||
|
||||
|
||||
if (buff) {
|
||||
/* Read bytes from input stream */
|
||||
return (uint16_t)fread(buff, 1, nbyte, dev->fp);
|
||||
} else {
|
||||
/* Remove bytes from input stream */
|
||||
return fseek(dev->fp, nbyte, SEEK_CUR) ? 0 : nbyte;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------*/
|
||||
/* User defined output funciton */
|
||||
/*------------------------------*/
|
||||
|
||||
int out_func (JDEC* jd, void* bitmap, JRECT* rect)
|
||||
{
|
||||
IODEV *dev = (IODEV*)jd->device;
|
||||
uint8_t *src, *dst;
|
||||
uint16_t y, bws, bwd;
|
||||
|
||||
|
||||
/* Put progress indicator */
|
||||
if (rect->left == 0) {
|
||||
printf("\r%lu%%", (rect->top << jd->scale) * 100UL / jd->height);
|
||||
}
|
||||
|
||||
/* Copy the decompressed RGB rectanglar to the frame buffer (assuming RGB888 cfg) */
|
||||
src = (uint8_t*)bitmap;
|
||||
dst = dev->fbuf + 3 * (rect->top * dev->wfbuf + rect->left); /* Left-top of destination rectangular */
|
||||
bws = 3 * (rect->right - rect->left + 1); /* Width of source rectangular [byte] */
|
||||
bwd = 3 * dev->wfbuf; /* Width of frame buffer [byte] */
|
||||
for (y = rect->top; y <= rect->bottom; y++) {
|
||||
memcpy(dst, src, bws); /* Copy a line */
|
||||
src += bws; dst += bwd; /* Next line */
|
||||
}
|
||||
|
||||
return 1; /* Continue to decompress */
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------*/
|
||||
/* Program Jpeg_Dec */
|
||||
/*------------------------------*/
|
||||
|
||||
int Jpeg_Dec (int argc, char* argv[])
|
||||
{
|
||||
void *work; /* Pointer to the decompressor work area */
|
||||
JDEC jdec; /* Decompression object */
|
||||
JRESULT res; /* Result code of TJpgDec API */
|
||||
IODEV devid; /* User defined device identifier */
|
||||
|
||||
/* Open a JPEG file */
|
||||
if (argc < 2)
|
||||
{
|
||||
printf("Jpeg_Dec illegal arguments ...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
devid.fp = fopen(argv[1], "r+");
|
||||
if (!devid.fp)
|
||||
{
|
||||
printf("Jpeg_Dec open the file failed...\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Allocate a work area for TJpgDec */
|
||||
work = malloc(WORK_POOL_SIZE);
|
||||
if(work == NULL)
|
||||
{
|
||||
printf("Jpeg_Dec work malloc failed...\n");
|
||||
res = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Prepare to decompress */
|
||||
res = jd_prepare(&jdec, in_func, work, WORK_POOL_SIZE, &devid);
|
||||
if (res == JDR_OK)
|
||||
{
|
||||
/* Ready to dcompress. Image info is available here. */
|
||||
printf("Image dimensions: %u by %u. %u bytes used.\n", jdec.width, jdec.height, 3100 - jdec.sz_pool);
|
||||
|
||||
devid.fbuf = malloc(3 * jdec.width * jdec.height); /* Frame buffer for output image (assuming RGB888 cfg) */
|
||||
if(devid.fbuf == RT_NULL)
|
||||
{
|
||||
printf("Jpeg_Dec devid.fbuf malloc failed, need to use %d Bytes ...\n", 3 * jdec.width * jdec.height);
|
||||
res = -1;
|
||||
goto __exit;
|
||||
}
|
||||
devid.wfbuf = jdec.width;
|
||||
|
||||
res = jd_decomp(&jdec, out_func, 0); /* Start to decompress with 1/1 scaling */
|
||||
if (res == JDR_OK) {
|
||||
/* Decompression succeeded. You have the decompressed image in the frame buffer here. */
|
||||
printf("\rOK \n");
|
||||
// for(int j = 0; j<3 * jdec.width * jdec.height;j++)
|
||||
// {
|
||||
// printf("%d,",*(devid.fbuf+j));
|
||||
// }
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to decompress: rc=%d\n", res);
|
||||
}
|
||||
|
||||
if(devid.fbuf != NULL)
|
||||
{
|
||||
free(devid.fbuf); /* Discard frame buffer */
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Failed to prepare: rc=%d\n", res);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if(work != NULL)
|
||||
{
|
||||
free(work); /* Discard work area */
|
||||
}
|
||||
|
||||
fclose(devid.fp); /* Close the JPEG file */
|
||||
|
||||
return res;
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(Jpeg_Dec, Jpeg Decode Test);
|
||||
#endif
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
config INSTRUSION_DETECT
|
||||
bool "enable apps/instrusion detect"
|
||||
depends on BOARD_K210_EVB
|
||||
depends on DRV_USING_OV2640
|
||||
depends on USING_KPU_POSTPROCESSING
|
||||
depends on USING_YOLOV2
|
||||
select LIB_USING_CJSON
|
||||
default n
|
|
@ -1,5 +0,0 @@
|
|||
# Instrusion detect demo
|
||||
|
||||
### A human object detection task demo. Running MobileNet-yolo on K210-based edge devices.
|
||||
|
||||
***Training, deployment and running, please see helmet_detect***
|
|
@ -1,381 +0,0 @@
|
|||
#include <transform.h>
|
||||
#ifdef LIB_USING_CJSON
|
||||
#include <cJSON.h>
|
||||
#endif
|
||||
#include "region_layer.h"
|
||||
#define ANCHOR_NUM 5
|
||||
#define STACK_SIZE (128 * 1024)
|
||||
#define JSON_FILE_PATH "/kmodel/human.json"
|
||||
#define JSON_BUFFER_SIZE (4 * 1024)
|
||||
|
||||
// params from json
|
||||
static float anchor[ANCHOR_NUM * 2] = {};
|
||||
static int net_output_shape[3] = {};
|
||||
static int net_input_size[2] = {};
|
||||
static int sensor_output_size[2] = {};
|
||||
static char kmodel_path[127] = "";
|
||||
static int kmodel_size = 0;
|
||||
static float obj_thresh[20] = {};
|
||||
static float nms_thresh = 0.0;
|
||||
static char labels[20][32] = {};
|
||||
static int class_num = 0;
|
||||
|
||||
#define THREAD_PRIORITY_HUMAN_D (11)
|
||||
static pthread_t instrusiontid = 0;
|
||||
static void *thread_instrusion_detect_entry(void *parameter);
|
||||
static int g_fd = 0;
|
||||
static int kmodel_fd = 0;
|
||||
static int if_exit = 0;
|
||||
static unsigned char *showbuffer = NULL;
|
||||
static unsigned char *kpurgbbuffer = NULL;
|
||||
|
||||
static _ioctl_shoot_para shoot_para_t = {0};
|
||||
unsigned char *model_data = NULL; // kpu data load memory
|
||||
unsigned char *model_data_align = NULL;
|
||||
|
||||
kpu_model_context_t instrusion_detect_task;
|
||||
static region_layer_t instrusion_detect_rl;
|
||||
static obj_info_t instrusion_detect_info;
|
||||
volatile uint32_t g_ai_done_flag;
|
||||
|
||||
static void ai_done(void *ctx) { g_ai_done_flag = 1; }
|
||||
|
||||
static void param_parse()
|
||||
{
|
||||
int fin;
|
||||
char buffer[JSON_BUFFER_SIZE] = "";
|
||||
// char *buffer;
|
||||
// if (NULL != (buffer = (char*)malloc(JSON_BUFFER_SIZE * sizeof(char)))) {
|
||||
// memset(buffer, 0, JSON_BUFFER_SIZE * sizeof(char));
|
||||
// } else {
|
||||
// printf("Json buffer malloc failed!");
|
||||
// exit(-1);
|
||||
// }
|
||||
int array_size;
|
||||
cJSON *json_obj;
|
||||
cJSON *json_item;
|
||||
cJSON *json_array_item;
|
||||
|
||||
fin = open(JSON_FILE_PATH, O_RDONLY);
|
||||
if (!fin) {
|
||||
printf("Error open file %s", JSON_FILE_PATH);
|
||||
exit(-1);
|
||||
}
|
||||
read(fin, buffer, sizeof(buffer));
|
||||
close(fin);
|
||||
|
||||
// read json string
|
||||
json_obj = cJSON_Parse(buffer);
|
||||
// free(buffer);
|
||||
char *json_print_str = cJSON_Print(json_obj);
|
||||
printf("Json file content: \n%s\n", json_print_str);
|
||||
cJSON_free(json_print_str);
|
||||
// get anchors
|
||||
json_item = cJSON_GetObjectItem(json_obj, "anchors");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (ANCHOR_NUM * 2 != array_size) {
|
||||
printf("Expect anchor size: %d, got %d in json file", ANCHOR_NUM * 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d anchors from json file\n", ANCHOR_NUM);
|
||||
}
|
||||
for (int i = 0; i < ANCHOR_NUM * 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
anchor[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, anchor[i]);
|
||||
}
|
||||
// net_input_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_input_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect net_input_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_input_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
net_input_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, net_input_size[i]);
|
||||
}
|
||||
// net_output_shape
|
||||
json_item = cJSON_GetObjectItem(json_obj, "net_output_shape");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (3 != array_size) {
|
||||
printf("Expect net_output_shape: %d, got %d in json file", 3, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d net_output_shape from json file\n", 3);
|
||||
}
|
||||
for (int i = 0; i < 3; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
net_output_shape[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, net_output_shape[i]);
|
||||
}
|
||||
// sensor_output_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "sensor_output_size");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (2 != array_size) {
|
||||
printf("Expect sensor_output_size: %d, got %d in json file", 2, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d sensor_output_size from json file\n", 2);
|
||||
}
|
||||
for (int i = 0; i < 2; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
sensor_output_size[i] = json_array_item->valueint;
|
||||
printf("%d: %d\n", i, sensor_output_size[i]);
|
||||
}
|
||||
// kmodel_path
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_path");
|
||||
memcpy(kmodel_path, json_item->valuestring, strlen(json_item->valuestring));
|
||||
printf("Got kmodel_path: %s\n", kmodel_path);
|
||||
// kmodel_size
|
||||
json_item = cJSON_GetObjectItem(json_obj, "kmodel_size");
|
||||
kmodel_size = json_item->valueint;
|
||||
printf("Got kmodel_size: %d\n", kmodel_size);
|
||||
// labels
|
||||
json_item = cJSON_GetObjectItem(json_obj, "labels");
|
||||
class_num = cJSON_GetArraySize(json_item);
|
||||
if (0 >= class_num) {
|
||||
printf("No labels!");
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d labels\n", class_num);
|
||||
}
|
||||
for (int i = 0; i < class_num; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
memcpy(labels[i], json_array_item->valuestring, strlen(json_array_item->valuestring));
|
||||
printf("%d: %s\n", i, labels[i]);
|
||||
}
|
||||
// obj_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "obj_thresh");
|
||||
array_size = cJSON_GetArraySize(json_item);
|
||||
if (class_num != array_size) {
|
||||
printf("label number and thresh number mismatch! label number : %d, obj thresh number %d", class_num, array_size);
|
||||
exit(-1);
|
||||
} else {
|
||||
printf("Got %d obj_thresh\n", array_size);
|
||||
}
|
||||
for (int i = 0; i < array_size; i++) {
|
||||
json_array_item = cJSON_GetArrayItem(json_item, i);
|
||||
obj_thresh[i] = json_array_item->valuedouble;
|
||||
printf("%d: %f\n", i, obj_thresh[i]);
|
||||
}
|
||||
// nms_thresh
|
||||
json_item = cJSON_GetObjectItem(json_obj, "nms_thresh");
|
||||
nms_thresh = json_item->valuedouble;
|
||||
printf("Got nms_thresh: %f\n", nms_thresh);
|
||||
|
||||
cJSON_Delete(json_obj);
|
||||
return;
|
||||
}
|
||||
|
||||
void instrusion_detect()
|
||||
{
|
||||
int ret = 0;
|
||||
int result = 0;
|
||||
int size = 0;
|
||||
param_parse();
|
||||
g_fd = open("/dev/ov2640", O_RDONLY);
|
||||
if (g_fd < 0) {
|
||||
printf("open ov2640 fail !!");
|
||||
return;
|
||||
}
|
||||
_ioctl_set_dvp_reso set_dvp_reso = {sensor_output_size[1], sensor_output_size[0]};
|
||||
ioctl(g_fd, IOCTRL_CAMERA_SET_DVP_RESO, &set_dvp_reso);
|
||||
showbuffer = (unsigned char *)malloc(sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
if (NULL == showbuffer) {
|
||||
close(g_fd);
|
||||
printf("showbuffer apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
kpurgbbuffer = (unsigned char *)malloc(net_input_size[0] * net_input_size[1] * 3);
|
||||
if (NULL == kpurgbbuffer) {
|
||||
close(g_fd);
|
||||
free(showbuffer);
|
||||
printf("kpurgbbuffer apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
model_data = (unsigned char *)malloc(kmodel_size + 255);
|
||||
if (NULL == model_data) {
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
close(g_fd);
|
||||
printf("model_data apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
memset(model_data, 0, kmodel_size + 255);
|
||||
memset(showbuffer, 0, sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
memset(kpurgbbuffer, 127, net_input_size[0] * net_input_size[1] * 3);
|
||||
shoot_para_t.pdata = (unsigned int *)(showbuffer);
|
||||
shoot_para_t.length = (size_t)(sensor_output_size[0] * sensor_output_size[1] * 2);
|
||||
/*
|
||||
load memory
|
||||
*/
|
||||
kmodel_fd = open(kmodel_path, O_RDONLY);
|
||||
if (kmodel_fd < 0) {
|
||||
printf("open kmodel fail");
|
||||
close(g_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
} else {
|
||||
size = read(kmodel_fd, model_data, kmodel_size);
|
||||
if (size != kmodel_size) {
|
||||
printf("read kmodel error size %d\n", size);
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
|
||||
} else {
|
||||
printf("read kmodel success \n");
|
||||
}
|
||||
}
|
||||
unsigned char *model_data_align = (unsigned char *)(((unsigned int)model_data + 255) & (~255));
|
||||
dvp_set_ai_addr((uint32_t)(kpurgbbuffer + net_input_size[1] * (net_input_size[0] - sensor_output_size[0])),
|
||||
(uint32_t)(kpurgbbuffer + net_input_size[1] * (net_input_size[0] - sensor_output_size[0]) +
|
||||
net_input_size[0] * net_input_size[1]),
|
||||
(uint32_t)(kpurgbbuffer + net_input_size[0] * net_input_size[1] * 2 +
|
||||
net_input_size[1] * (net_input_size[0] - sensor_output_size[0])));
|
||||
if (kpu_load_kmodel(&instrusion_detect_task, model_data_align) != 0) {
|
||||
printf("\nmodel init error\n");
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
return;
|
||||
}
|
||||
instrusion_detect_rl.anchor_number = ANCHOR_NUM;
|
||||
instrusion_detect_rl.anchor = anchor;
|
||||
instrusion_detect_rl.threshold = malloc(class_num * sizeof(float));
|
||||
for (int idx = 0; idx < class_num; idx++) {
|
||||
instrusion_detect_rl.threshold[idx] = obj_thresh[idx];
|
||||
}
|
||||
instrusion_detect_rl.nms_value = nms_thresh;
|
||||
result = region_layer_init(&instrusion_detect_rl, net_output_shape[0], net_output_shape[1], net_output_shape[2],
|
||||
net_input_size[1], net_input_size[0]);
|
||||
printf("region_layer_init result %d \n\r", result);
|
||||
size_t stack_size = STACK_SIZE;
|
||||
pthread_attr_t attr; /* 线程属性 */
|
||||
struct sched_param prio; /* 线程优先级 */
|
||||
prio.sched_priority = 8; /* 优先级设置为 8 */
|
||||
pthread_attr_init(&attr); /* 先使用默认值初始化属性 */
|
||||
pthread_attr_setschedparam(&attr, &prio); /* 修改属性对应的优先级 */
|
||||
pthread_attr_setstacksize(&attr, stack_size);
|
||||
|
||||
/* 创建线程 1, 属性为 attr,入口函数是 thread_entry,入口函数参数是 1 */
|
||||
result = pthread_create(&instrusiontid, &attr, thread_instrusion_detect_entry, NULL);
|
||||
if (0 == result) {
|
||||
printf("thread_instrusion_detect_entry successfully!\n");
|
||||
} else {
|
||||
printf("thread_instrusion_detect_entry failed! error code is %d\n", result);
|
||||
close(g_fd);
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(instrusion_detect, instrusion detect task);
|
||||
#endif
|
||||
|
||||
static void *thread_instrusion_detect_entry(void *parameter)
|
||||
{
|
||||
extern void lcd_draw_picture(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint32_t * ptr);
|
||||
printf("thread_instrusion_detect_entry start!\n");
|
||||
int ret = 0;
|
||||
// sysctl_enable_irq();
|
||||
while (1) {
|
||||
// memset(showbuffer,0,320*240*2);
|
||||
g_ai_done_flag = 0;
|
||||
ret = ioctl(g_fd, IOCTRL_CAMERA_START_SHOT, &shoot_para_t);
|
||||
if (RT_ERROR == ret) {
|
||||
printf("ov2640 can't wait event flag");
|
||||
rt_free(showbuffer);
|
||||
close(g_fd);
|
||||
pthread_exit(NULL);
|
||||
return NULL;
|
||||
}
|
||||
kpu_run_kmodel(&instrusion_detect_task, kpurgbbuffer, DMAC_CHANNEL5, ai_done, NULL);
|
||||
while (!g_ai_done_flag)
|
||||
;
|
||||
float *output;
|
||||
size_t output_size;
|
||||
kpu_get_output(&instrusion_detect_task, 0, (uint8_t **)&output, &output_size);
|
||||
instrusion_detect_rl.input = output;
|
||||
region_layer_run(&instrusion_detect_rl, &instrusion_detect_info);
|
||||
/* display result */
|
||||
|
||||
for (int instrusion_cnt = 0; instrusion_cnt < instrusion_detect_info.obj_number; instrusion_cnt++) {
|
||||
// draw_edge((uint32_t *)showbuffer, &instrusion_detect_info, instrusion_cnt, 0xF800,
|
||||
// (uint16_t)sensor_output_size[1],
|
||||
// (uint16_t)sensor_output_size[0]);
|
||||
printf("%d: (%d, %d, %d, %d) cls: %s conf: %f\t", instrusion_cnt, instrusion_detect_info.obj[instrusion_cnt].x1,
|
||||
instrusion_detect_info.obj[instrusion_cnt].y1, instrusion_detect_info.obj[instrusion_cnt].x2,
|
||||
instrusion_detect_info.obj[instrusion_cnt].y2, labels[instrusion_detect_info.obj[instrusion_cnt].class_id],
|
||||
instrusion_detect_info.obj[instrusion_cnt].prob);
|
||||
}
|
||||
if (0 != instrusion_detect_info.obj_number) {
|
||||
printf("\n");
|
||||
}
|
||||
#ifdef BSP_USING_LCD
|
||||
lcd_draw_picture(0, 0, (uint16_t)sensor_output_size[1], (uint16_t)sensor_output_size[0], (unsigned int *)showbuffer);
|
||||
#endif
|
||||
usleep(1);
|
||||
if (1 == if_exit) {
|
||||
if_exit = 0;
|
||||
printf("thread_instrusion_detect_entry exit");
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void instrusion_detect_delete()
|
||||
{
|
||||
if (showbuffer != NULL) {
|
||||
int ret = 0;
|
||||
close(g_fd);
|
||||
close(kmodel_fd);
|
||||
free(showbuffer);
|
||||
free(kpurgbbuffer);
|
||||
free(model_data);
|
||||
printf("instrusion detect task cancel!!! ret %d ", ret);
|
||||
if_exit = 1;
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(instrusion_detect_delete, instrusion detect task delete);
|
||||
#endif
|
||||
|
||||
void kmodel_load(unsigned char *model_data)
|
||||
{
|
||||
int kmodel_fd = 0;
|
||||
int size = 0;
|
||||
kmodel_fd = open(kmodel_path, O_RDONLY);
|
||||
|
||||
model_data = (unsigned char *)malloc(kmodel_size + 255);
|
||||
if (NULL == model_data) {
|
||||
printf("model_data apply memory fail !!");
|
||||
return;
|
||||
}
|
||||
memset(model_data, 0, kmodel_size + 255);
|
||||
|
||||
if (kmodel_fd >= 0) {
|
||||
size = read(kmodel_fd, model_data, kmodel_size);
|
||||
if (size != kmodel_size) {
|
||||
printf("read kmodel error size %d\n", size);
|
||||
|
||||
} else {
|
||||
printf("read kmodel success");
|
||||
}
|
||||
} else {
|
||||
free(model_data);
|
||||
printf("open kmodel fail");
|
||||
}
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(kmodel_load, kmodel load memory);
|
||||
#endif
|
|
@ -19,7 +19,8 @@ void simple_CSV_read()
|
|||
fin = open(CSV_PATH, O_RDONLY);
|
||||
if (!fin) {
|
||||
printf("Error open file %s", CSV_PATH);
|
||||
exit(-1);
|
||||
// exit(-1);
|
||||
return;
|
||||
}
|
||||
read(fin, buffer, sizeof(buffer));
|
||||
close(fin);
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
config K210_DETECT_ENTRY
|
||||
bool "enable apps/k210 yolov2 detect entry"
|
||||
depends on BOARD_K210_EVB
|
||||
depends on DRV_USING_OV2640
|
||||
depends on USING_KPU_PROCESSING
|
||||
depends on USING_YOLOV2
|
||||
depends on USING_YOLOV2_JSONPARSER
|
||||
depends on USING_K210_YOLOV2_DETECT
|
||||
select LIB_USING_CJSON
|
||||
default n
|
|
@ -4,6 +4,6 @@ cwd = GetCurrentDir()
|
|||
src = Glob('*.c') + Glob('*.cpp')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('Applications', src, depend = ['INSTRUSION_DETECT'], LOCAL_CPPPATH = CPPPATH)
|
||||
group = DefineGroup('Applications', src, depend = ['USING_K210_YOLOV2_DETECT'], LOCAL_CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
|
@ -24,7 +24,6 @@
|
|||
6.718375,
|
||||
9.01025
|
||||
],
|
||||
"kmodel_path": "/kmodel/detect.kmodel",
|
||||
"kmodel_size": 388776,
|
||||
"obj_thresh": [
|
||||
0.7
|
|
@ -24,11 +24,10 @@
|
|||
2.1128,
|
||||
3.184
|
||||
],
|
||||
"kmodel_path": "/kmodel/helmet.kmodel",
|
||||
"kmodel_size": 2714044,
|
||||
"obj_thresh": [
|
||||
0.7,
|
||||
0.9
|
||||
0.99
|
||||
],
|
||||
"labels": [
|
||||
"head",
|
|
@ -24,10 +24,9 @@
|
|||
2.049,
|
||||
4.6711
|
||||
],
|
||||
"kmodel_path": "/kmodel/human.kmodel",
|
||||
"kmodel_size": 2713236,
|
||||
"obj_thresh": [
|
||||
0.55
|
||||
0.7
|
||||
],
|
||||
"labels": [
|
||||
"human"
|
|
@ -0,0 +1,16 @@
|
|||
#include "k210_yolov2_detect.h"
|
||||
static void detect_app(int argc, char *argv[])
|
||||
{
|
||||
if (2 != argc) {
|
||||
printf("Usage: detect_app <ABSOLUTE_CONFIG_JSON_PATH>");
|
||||
} else {
|
||||
k210_detect(argv[1]);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
// clang-format off
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(detect_app, k210 detect app usage: detect_app <ABSOLUTE_CONFIG_JSON_PATH>);
|
||||
#endif
|
||||
// clang-format on
|
|
@ -0,0 +1,3 @@
|
|||
config K210_FFT_TEST
|
||||
bool "enable apps/k210 fft test"
|
||||
default n
|
|
@ -4,6 +4,6 @@ cwd = GetCurrentDir()
|
|||
src = Glob('*.c') + Glob('*.cpp')
|
||||
CPPPATH = [cwd]
|
||||
|
||||
group = DefineGroup('Applications', src, depend = ['HELMET_DETECT'], LOCAL_CPPPATH = CPPPATH)
|
||||
group = DefineGroup('Applications', src, depend = ['K210_FFT_TEST'], LOCAL_CPPPATH = CPPPATH)
|
||||
|
||||
Return('group')
|
|
@ -0,0 +1,101 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include "fft_soft.h"
|
||||
|
||||
#define PI 3.14159265358979323846
|
||||
complex add(complex a, complex b)
|
||||
{
|
||||
complex ret = {a.real + b.real, a.imag + b.imag};
|
||||
return ret;
|
||||
}
|
||||
|
||||
complex sub(complex a, complex b)
|
||||
{
|
||||
complex ret = {a.real - b.real, a.imag - b.imag};
|
||||
return ret;
|
||||
}
|
||||
|
||||
complex mul(complex a, complex b)
|
||||
{
|
||||
complex ret = {a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real};
|
||||
return ret;
|
||||
}
|
||||
|
||||
void bitrev(complex *data, int n)
|
||||
{
|
||||
int j = 0;
|
||||
int m = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
if (j > i)
|
||||
SWAP(data[i], data[j]);
|
||||
m = n / 2;
|
||||
while (j >= m && m != 0)
|
||||
{
|
||||
j -= m;
|
||||
m >>= 1;
|
||||
}
|
||||
j += m;
|
||||
}
|
||||
}
|
||||
|
||||
void fft_soft(complex *data, int n)
|
||||
{
|
||||
int M = 0;
|
||||
for (int i = n; i > 1; i = i >> 1, M++);
|
||||
|
||||
bitrev(data, n);
|
||||
|
||||
for (int m = 0; m < M; m++)
|
||||
{
|
||||
int K = n >> (m + 1);
|
||||
for (int k = 0; k < K; k++)
|
||||
{
|
||||
int J = 2 << m;
|
||||
int base = k * J;
|
||||
for (int j = 0; j < J / 2; j++)
|
||||
{
|
||||
int t = base + j;
|
||||
complex w = {cos(-2 * PI * j * K / n), sin(-2 * PI * j * K / n)};
|
||||
complex wn = mul(data[t + J / 2], w);
|
||||
complex temp = data[t];
|
||||
data[t] = add(data[t], wn);
|
||||
data[t + J / 2] = sub(temp, wn);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ifft_soft(complex *data, int n)
|
||||
{
|
||||
int M = 0;
|
||||
for (int i = n; i > 1; i = i >> 1, M++);
|
||||
|
||||
bitrev(data, n);
|
||||
|
||||
for (int m = 0; m < M; m++)
|
||||
{
|
||||
int K = n >> (m + 1);
|
||||
for (int k = 0; k < K; k++)
|
||||
{
|
||||
int J = 2 << m;
|
||||
int base = k * J;
|
||||
for (int j = 0; j < J / 2; j++)
|
||||
{
|
||||
int t = base + j;
|
||||
complex w = {cos(2 * PI * j * K / n), sin(2 * PI * j * K / n)};
|
||||
complex wn = mul(data[t + J / 2], w);
|
||||
complex temp = data[t];
|
||||
data[t] = add(data[t], wn);
|
||||
data[t + J / 2] = sub(temp, wn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
data[i].real /= n;
|
||||
data[i].imag /= n;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef _FFT_SOFT_H
|
||||
#define _FFT_SOFT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define SWAP(a, b) do {complex t = (a); (a) = (b); (b) = t;} while(0)
|
||||
|
||||
typedef struct{double real, imag;} complex;
|
||||
|
||||
void fft_soft(complex *data, int n);
|
||||
void ifft_soft(complex *data, int n);
|
||||
void show(complex *data, int n);
|
||||
|
||||
#endif /* _FFT_SOFT_H */
|
|
@ -0,0 +1,159 @@
|
|||
/* Copyright 2018 Canaan Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include <transform.h>
|
||||
#include <math.h>
|
||||
#include "encoding.h"
|
||||
#include "dmac.h"
|
||||
#include "fft.h"
|
||||
#include "encoding.h"
|
||||
#include "sysctl.h"
|
||||
#include "fft_soft.h"
|
||||
|
||||
#define FFT_N 512U
|
||||
#define FFT_FORWARD_SHIFT 0x0U
|
||||
#define FFT_BACKWARD_SHIFT 0x1ffU
|
||||
#define PI 3.14159265358979323846
|
||||
|
||||
typedef enum _complex_mode
|
||||
{
|
||||
FFT_HARD = 0,
|
||||
FFT_SOFT = 1,
|
||||
FFT_COMPLEX_MAX,
|
||||
} complex_mode_t;
|
||||
|
||||
int16_t real[FFT_N];
|
||||
int16_t imag[FFT_N];
|
||||
float hard_power[FFT_N];
|
||||
float soft_power[FFT_N];
|
||||
float hard_angel[FFT_N];
|
||||
float soft_angel[FFT_N];
|
||||
uint64_t fft_out_data[FFT_N / 2];
|
||||
uint64_t buffer_input[FFT_N];
|
||||
uint64_t buffer_output[FFT_N];
|
||||
uint64_t cycle[FFT_COMPLEX_MAX][FFT_DIR_MAX];
|
||||
|
||||
uint16_t get_bit1_num(uint32_t data)
|
||||
{
|
||||
uint16_t num;
|
||||
for (num = 0; data; num++)
|
||||
data &= data - 1;
|
||||
return num;
|
||||
}
|
||||
|
||||
void k210_fft_test(void)
|
||||
{
|
||||
int32_t i;
|
||||
float tempf1[3];
|
||||
fft_data_t *output_data;
|
||||
fft_data_t *input_data;
|
||||
uint16_t bit1_num = get_bit1_num(FFT_FORWARD_SHIFT);
|
||||
complex_hard_t data_hard[FFT_N] = {0};
|
||||
complex data_soft[FFT_N] = {0};
|
||||
for (i = 0; i < FFT_N; i++)
|
||||
{
|
||||
tempf1[0] = 0.3 * cosf(2 * PI * i / FFT_N + PI / 3) * 256;
|
||||
tempf1[1] = 0.1 * cosf(16 * 2 * PI * i / FFT_N - PI / 9) * 256;
|
||||
tempf1[2] = 0.5 * cosf((19 * 2 * PI * i / FFT_N) + PI / 6) * 256;
|
||||
data_hard[i].real = (int16_t)(tempf1[0] + tempf1[1] + tempf1[2] + 10);
|
||||
data_hard[i].imag = (int16_t)0;
|
||||
data_soft[i].real = data_hard[i].real;
|
||||
data_soft[i].imag = data_hard[i].imag;
|
||||
}
|
||||
for (int i = 0; i < FFT_N / 2; ++i)
|
||||
{
|
||||
input_data = (fft_data_t *)&buffer_input[i];
|
||||
input_data->R1 = data_hard[2 * i].real;
|
||||
input_data->I1 = data_hard[2 * i].imag;
|
||||
input_data->R2 = data_hard[2 * i + 1].real;
|
||||
input_data->I2 = data_hard[2 * i + 1].imag;
|
||||
}
|
||||
cycle[FFT_HARD][FFT_DIR_FORWARD] = read_cycle();
|
||||
fft_complex_uint16_dma(DMAC_CHANNEL0, DMAC_CHANNEL1, FFT_FORWARD_SHIFT, FFT_DIR_FORWARD, buffer_input, FFT_N, buffer_output);
|
||||
cycle[FFT_HARD][FFT_DIR_FORWARD] = read_cycle() - cycle[FFT_HARD][FFT_DIR_FORWARD];
|
||||
cycle[FFT_SOFT][FFT_DIR_FORWARD] = read_cycle();
|
||||
fft_soft(data_soft, FFT_N);
|
||||
cycle[FFT_SOFT][FFT_DIR_FORWARD] = read_cycle() - cycle[FFT_SOFT][FFT_DIR_FORWARD];
|
||||
for (i = 0; i < FFT_N / 2; i++)
|
||||
{
|
||||
output_data = (fft_data_t*)&buffer_output[i];
|
||||
data_hard[2 * i].imag = output_data->I1 ;
|
||||
data_hard[2 * i].real = output_data->R1 ;
|
||||
data_hard[2 * i + 1].imag = output_data->I2 ;
|
||||
data_hard[2 * i + 1].real = output_data->R2 ;
|
||||
}
|
||||
|
||||
for (i = 0; i < FFT_N; i++)
|
||||
{
|
||||
hard_power[i] = sqrt(data_hard[i].real * data_hard[i].real + data_hard[i].imag * data_hard[i].imag) * 2;
|
||||
soft_power[i] = sqrt(data_soft[i].real * data_soft[i].real + data_soft[i].imag * data_soft[i].imag) * 2;
|
||||
}
|
||||
|
||||
printf("\n[hard fft real][soft fft real][hard fft imag][soft fft imag]\n");
|
||||
for (i = 0; i < FFT_N / 2; i++)
|
||||
printf("%3d:%7d %7d %7d %7d\n",
|
||||
i, data_hard[i].real, (int32_t)data_soft[i].real, data_hard[i].imag, (int32_t)data_soft[i].imag);
|
||||
|
||||
printf("\nhard power soft power:\n");
|
||||
printf("%3d : %f %f\n", 0, hard_power[0] / 2 / FFT_N * (1 << bit1_num), soft_power[0] / 2 / FFT_N);
|
||||
for (i = 1; i < FFT_N / 2; i++)
|
||||
printf("%3d : %f %f\n", i, hard_power[i] / FFT_N * (1 << bit1_num), soft_power[i] / FFT_N);
|
||||
|
||||
printf("\nhard phase soft phase:\n");
|
||||
for (i = 0; i < FFT_N / 2; i++)
|
||||
{
|
||||
hard_angel[i] = atan2(data_hard[i].imag, data_hard[i].real);
|
||||
soft_angel[i] = atan2(data_soft[i].imag, data_soft[i].real);
|
||||
printf("%3d : %f %f\n", i, hard_angel[i] * 180 / PI, soft_angel[i] * 180 / PI);
|
||||
}
|
||||
|
||||
for (int i = 0; i < FFT_N / 2; ++i)
|
||||
{
|
||||
input_data = (fft_data_t *)&buffer_input[i];
|
||||
input_data->R1 = data_hard[2 * i].real;
|
||||
input_data->I1 = data_hard[2 * i].imag;
|
||||
input_data->R2 = data_hard[2 * i + 1].real;
|
||||
input_data->I2 = data_hard[2 * i + 1].imag;
|
||||
}
|
||||
cycle[FFT_HARD][FFT_DIR_BACKWARD] = read_cycle();
|
||||
fft_complex_uint16_dma(DMAC_CHANNEL0, DMAC_CHANNEL1, FFT_BACKWARD_SHIFT, FFT_DIR_BACKWARD, buffer_input, FFT_N, buffer_output);
|
||||
cycle[FFT_HARD][FFT_DIR_BACKWARD] = read_cycle() - cycle[FFT_HARD][FFT_DIR_BACKWARD];
|
||||
cycle[FFT_SOFT][FFT_DIR_BACKWARD] = read_cycle();
|
||||
ifft_soft(data_soft, FFT_N);
|
||||
cycle[FFT_SOFT][FFT_DIR_BACKWARD] = read_cycle() - cycle[FFT_SOFT][FFT_DIR_BACKWARD];
|
||||
for (i = 0; i < FFT_N / 2; i++)
|
||||
{
|
||||
output_data = (fft_data_t*)&buffer_output[i];
|
||||
data_hard[2 * i].imag = output_data->I1 ;
|
||||
data_hard[2 * i].real = output_data->R1 ;
|
||||
data_hard[2 * i + 1].imag = output_data->I2 ;
|
||||
data_hard[2 * i + 1].real = output_data->R2 ;
|
||||
}
|
||||
printf("\n[hard ifft real][soft ifft real][hard ifft imag][soft ifft imag]\n");
|
||||
for (i = 0; i < FFT_N / 2; i++)
|
||||
printf("%3d:%7d %7d %7d %7d\n",
|
||||
i, data_hard[i].real, (int32_t)data_soft[i].real, data_hard[i].imag, (int32_t)data_soft[i].imag);
|
||||
|
||||
printf("[hard fft test] [%d bytes] forward time = %ld us, backward time = %ld us\n",
|
||||
FFT_N,
|
||||
cycle[FFT_HARD][FFT_DIR_FORWARD]/(sysctl_clock_get_freq(SYSCTL_CLOCK_CPU)/1000000),
|
||||
cycle[FFT_HARD][FFT_DIR_BACKWARD]/(sysctl_clock_get_freq(SYSCTL_CLOCK_CPU)/1000000));
|
||||
printf("[soft fft test] [%d bytes] forward time = %ld us, backward time = %ld us\n",
|
||||
FFT_N,
|
||||
cycle[FFT_SOFT][FFT_DIR_FORWARD]/(sysctl_clock_get_freq(SYSCTL_CLOCK_CPU)/1000000),
|
||||
cycle[FFT_SOFT][FFT_DIR_BACKWARD]/(sysctl_clock_get_freq(SYSCTL_CLOCK_CPU)/1000000));
|
||||
}
|
||||
#ifdef __RT_THREAD_H__
|
||||
MSH_CMD_EXPORT(k210_fft_test,k210 fft test );
|
||||
#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
|
2
APP_Framework/Framework/connection/wifi/HFA21/Makefile → APP_Framework/Applications/ota/Makefile
Executable file → Normal file
2
APP_Framework/Framework/connection/wifi/HFA21/Makefile → APP_Framework/Applications/ota/Makefile
Executable file → Normal file
|
@ -1,3 +1,3 @@
|
|||
SRC_FILES := hfa21.c
|
||||
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();
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
import os
|
||||
from building import *
|
||||
Import('RTT_ROOT')
|
||||
Import('rtconfig')
|
||||
cwd = GetCurrentDir()
|
||||
DEPENDS = ["SUPPORT_SENSOR_FRAMEWORK"]
|
||||
SOURCES = []
|
||||
if GetDepend(['APPLICATION_SENSOR_CO2_ZG09']):
|
||||
SOURCES = ['co2_zg09.c'] + SOURCES
|
||||
if GetDepend(['APPLICATION_SENSOR_PM1_0_PS5308']):
|
||||
SOURCES = ['pm1_0_ps5308.c.c'] + SOURCES
|
||||
if GetDepend(['APPLICATION_SENSOR_VOICE_D124']):
|
||||
SOURCES = ['voice_d124.c'] + SOURCES
|
||||
if GetDepend(['APPLICATION_SENSOR_HUMIDITY_HS300X']):
|
||||
SOURCES = ['humidity_hs300x.c'] + SOURCES
|
||||
if GetDepend(['APPLICATION_SENSOR_TEMPERATURE_HS300X']):
|
||||
SOURCES = ['temperature_hs300x.c'] + SOURCES
|
||||
path = [cwd]
|
||||
objs = DefineGroup('sensor_app', src = SOURCES, depend = DEPENDS,CPPPATH = path)
|
||||
Return("objs")
|
|
@ -29,7 +29,7 @@ void Pm10Ps5308(void)
|
|||
{
|
||||
struct SensorQuantity *pm1_0 = SensorQuantityFind(SENSOR_QUANTITY_PS5308_PM1_0, SENSOR_QUANTITY_PM);
|
||||
SensorQuantityOpen(pm1_0);
|
||||
UserTaskDelay(2000);
|
||||
PrivTaskDelay(2000);
|
||||
printf("PM1.0 : %d ug/m³\n", SensorQuantityRead(pm1_0));
|
||||
SensorQuantityClose(pm1_0);
|
||||
}
|
|
@ -18,7 +18,7 @@
|
|||
* @date 2021.04.23
|
||||
*/
|
||||
|
||||
#include <user_api.h>
|
||||
#include <transform.h>
|
||||
#include <sensor.h>
|
||||
|
||||
/**
|
||||
|
@ -29,7 +29,7 @@ void VoiceD124(void)
|
|||
{
|
||||
struct SensorQuantity *voice = SensorQuantityFind(SENSOR_QUANTITY_D124_VOICE, SENSOR_QUANTITY_VOICE);
|
||||
SensorQuantityOpen(voice);
|
||||
UserTaskDelay(2000);
|
||||
PrivTaskDelay(2000);
|
||||
uint16 result = SensorQuantityRead(voice);
|
||||
printf("voice : %d.%d dB\n", result/(10*voice->value.decimal_places), result%(10*voice->value.decimal_places));
|
||||
SensorQuantityClose(voice);
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
config ADAPTER_4G
|
||||
bool "Using 4G adapter function"
|
||||
default y
|
||||
|
||||
if ADAPTER_4G
|
||||
config ADAPTER_EC200T
|
||||
bool "Using 4G adapter device EC200T"
|
||||
default y
|
||||
|
||||
if ADAPTER_EC200T
|
||||
source "$APP_DIR/Framework/connection/4G/EC200T/Kconfig"
|
||||
endif
|
||||
|
||||
endif
|
|
@ -1,7 +0,0 @@
|
|||
SRC_FILES := adapter_4G.c
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_EC200T),y)
|
||||
SRC_DIR += EC200T
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -0,0 +1,8 @@
|
|||
config ADAPTER_EC200T
|
||||
bool "Using 4G adapter device EC200T"
|
||||
default y
|
||||
|
||||
if ADAPTER_EC200T
|
||||
source "$APP_DIR/Framework/connection/4g/ec200t/Kconfig"
|
||||
endif
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
SRC_FILES := adapter_4g.c
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_EC200T),y)
|
||||
SRC_DIR += ec200t
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -49,16 +49,18 @@ int Adapter4GInit(void)
|
|||
{
|
||||
int ret = 0;
|
||||
|
||||
struct Adapter *adapter = malloc(sizeof(struct Adapter));
|
||||
struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter));
|
||||
if (!adapter) {
|
||||
free(adapter);
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(adapter, 0, sizeof(struct Adapter));
|
||||
|
||||
ret = Adapter4GRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("Adapter4GInit register 4G adapter error\n");
|
||||
free(adapter);
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -66,7 +68,7 @@ int Adapter4GInit(void)
|
|||
AdapterProductInfoType product_info = Ec200tAttach(adapter);
|
||||
if (!product_info) {
|
||||
printf("Adapter4GInit ec200t attach error\n");
|
||||
free(adapter);
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -82,16 +84,18 @@ int Adapter4GInit(void)
|
|||
/******************4G TEST*********************/
|
||||
int Adapter4GTest(void)
|
||||
{
|
||||
const char *send_msg = "SendHeart";
|
||||
char recv_msg[128];
|
||||
const char *send_msg = "Adapter_4G Test";
|
||||
char recv_msg[256] = {0};
|
||||
int baud_rate = BAUD_RATE_115200;
|
||||
|
||||
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME);
|
||||
|
||||
#ifdef ADAPTER_EC200T
|
||||
//Using DSD server to test 4G Socket connection
|
||||
uint8 server_addr[64] = "115.238.53.61";
|
||||
uint8 server_port[64] = "33333";
|
||||
//Using Hang Xiao server to test 4G Socket connection
|
||||
uint8 server_addr[64] = "101.68.82.219";
|
||||
uint8 server_port[64] = "9898";
|
||||
|
||||
adapter->socket.socket_id = 0;
|
||||
|
||||
AdapterDeviceOpen(adapter);
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
|
@ -100,11 +104,12 @@ int Adapter4GTest(void)
|
|||
|
||||
while (1) {
|
||||
AdapterDeviceSend(adapter, send_msg, strlen(send_msg));
|
||||
AdapterDeviceRecv(adapter, recv_msg, 128);
|
||||
AdapterDeviceRecv(adapter, recv_msg, 256);
|
||||
printf("4G recv msg %s\n", recv_msg);
|
||||
memset(recv_msg, 0, 256);
|
||||
}
|
||||
#endif
|
||||
|
||||
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);
|
|
@ -17,7 +17,7 @@ if ADD_XIUOS_FETURES
|
|||
|
||||
config ADAPTER_EC200T_DRIVER
|
||||
string "EC200T device uart driver path"
|
||||
default "/dev/uart2_dev2"
|
||||
default "/dev/usart2_dev2"
|
||||
depends on !ADAPTER_EC200T_DRIVER_EXTUART
|
||||
|
||||
if ADAPTER_EC200T_DRIVER_EXTUART
|
|
@ -21,6 +21,21 @@
|
|||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
#define EC200T_AT_MODE_CMD "+++"
|
||||
#define EC200T_GET_CCID_CMD "AT+CCID\r\n"
|
||||
#define EC200T_GET_CPIN_CMD "AT+CPIN?\r\n"
|
||||
#define EC200T_GET_CREG_CMD "AT+CREG?\r\n"
|
||||
#define EC200T_OPEN_SOCKET_CMD "AT+QIOPEN=1,%u"
|
||||
#define EC200T_CLOSE_SOCKET_CMD "AT+QICLOSE=%u\r\n"
|
||||
#define EC200T_ACTIVE_PDP_CMD "AT+QIACT=1\r\n"
|
||||
#define EC200T_DEACTIVE_PDP_CMD "AT+QIDEACT=1\r\n"
|
||||
#define EC200T_CFG_TCP_CMD "AT+QICSGP"
|
||||
|
||||
#define EC200T_OK_REPLY "OK"
|
||||
#define EC200T_READY_REPLY "READY"
|
||||
#define EC200T_CREG_REPLY ",1"
|
||||
#define EC200T_CONNECT_REPLY "CONNECT"
|
||||
|
||||
static void Ec200tPowerSet(void)
|
||||
{
|
||||
int pin_fd;
|
||||
|
@ -28,7 +43,7 @@ static void Ec200tPowerSet(void)
|
|||
|
||||
struct PinParam pin_param;
|
||||
pin_param.cmd = GPIO_CONFIG_MODE;
|
||||
pin_param.mode = GPIO_CFG_INPUT_PULLUP;
|
||||
pin_param.mode = GPIO_CFG_OUTPUT;
|
||||
pin_param.pin = ADAPTER_EC200T_PWRKEY;
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
|
@ -47,6 +62,8 @@ static void Ec200tPowerSet(void)
|
|||
PrivWrite(pin_fd, &pin_stat, 1);
|
||||
|
||||
PrivClose(pin_fd);
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
}
|
||||
|
||||
static int Ec200tOpen(struct Adapter *adapter)
|
||||
|
@ -70,32 +87,6 @@ static int Ec200tOpen(struct Adapter *adapter)
|
|||
adapter->agent = at_agent;
|
||||
}
|
||||
|
||||
/*step3: serial write "+++", quit transparent mode*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
|
||||
/*step4: serial write "AT+CCID", get SIM ID*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+CCID\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
/*step5: serial write "AT+CPIN?", check SIM status*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+CPIN?\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
/*step6: serial write "AT+CREG?", check whether registered to GSM net*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+CREG?\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
/*step7: serial write "AT+QICLOSE", close socket connect before open socket*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+QICLOSE=0\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
/*step8: serial write "AT+QIDEACT", close TCP net before open socket*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+QIDEACT=1\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
ADAPTER_DEBUG("Ec200t open done\n");
|
||||
|
@ -105,31 +96,41 @@ static int Ec200tOpen(struct Adapter *adapter)
|
|||
|
||||
static int Ec200tClose(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t ec200t_cmd[64];
|
||||
|
||||
if (!adapter->agent) {
|
||||
printf("Ec200tClose AT agent NULL\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
|
||||
/*step2: serial write "AT+QICLOSE", close socket connect before open socket*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+QICLOSE=0\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
memset(ec200t_cmd, 0, sizeof(ec200t_cmd));
|
||||
sprintf(ec200t_cmd, EC200T_CLOSE_SOCKET_CMD, adapter->socket.socket_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec200t_cmd, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+QIDEACT", close TCP net before open socket*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+QIDEACT=1\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC200T_DEACTIVE_PDP_CMD, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
/*step4: close ec200t serial port*/
|
||||
PrivClose(adapter->fd);
|
||||
|
||||
/*step5: power down ec200t*/
|
||||
Ec200tPowerSet();
|
||||
|
||||
return 0;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args)
|
||||
|
@ -159,15 +160,41 @@ static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args)
|
|||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = &serial_cfg;
|
||||
PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
|
||||
|
||||
Ec200tPowerSet();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Ec200tConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t ec200t_cmd[64];
|
||||
|
||||
/*step1: serial write "AT+QICSGP", connect to China Mobile using ipv4 or ipv6*/
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
|
||||
/*step2: serial write "AT+CCID", get SIM ID*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC200T_GET_CCID_CMD, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step3: serial write "AT+CPIN?", check SIM status*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC200T_GET_CPIN_CMD, EC200T_READY_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step4: serial write "AT+CREG?", check whether registered to GSM net*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC200T_GET_CREG_CMD, EC200T_CREG_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step5: serial write "AT+QICSGP", connect to China Mobile using ipv4 or ipv6*/
|
||||
memset(ec200t_cmd, 0, sizeof(ec200t_cmd));
|
||||
|
||||
if (IPV4 == ip_type) {
|
||||
|
@ -176,58 +203,105 @@ static int Ec200tConnect(struct Adapter *adapter, enum NetRoleType net_role, con
|
|||
strcpy(ec200t_cmd, "AT+QICSGP=1,2,\"CMNET\",\"\",\"\",1\r\n");
|
||||
}
|
||||
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, ec200t_cmd);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec200t_cmd, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
/*step2: serial write "AT+QIACT", open TCP net*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+QIACT=1\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
|
||||
/*step3: serial write "AT+QIOPEN", connect socket using TCP*/
|
||||
/*step6: serial write "AT+QICLOSE", close socket connect before open socket*/
|
||||
memset(ec200t_cmd, 0, sizeof(ec200t_cmd));
|
||||
strcpy(ec200t_cmd, "AT+QIOPEN=1,0,\"TCP\",\"");
|
||||
sprintf(ec200t_cmd, EC200T_CLOSE_SOCKET_CMD, adapter->socket.socket_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec200t_cmd, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step7: serial write "AT+QIDEACT", close TCP net before open socket*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC200T_DEACTIVE_PDP_CMD, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step8: serial write "AT+QIACT", open TCP net*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, EC200T_ACTIVE_PDP_CMD, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/*step9: serial write "AT+QIOPEN", connect socket using TCP*/
|
||||
memset(ec200t_cmd, 0, sizeof(ec200t_cmd));
|
||||
sprintf(ec200t_cmd, EC200T_OPEN_SOCKET_CMD, adapter->socket.socket_id);
|
||||
strcat(ec200t_cmd, ",\"TCP\",\"");
|
||||
strcat(ec200t_cmd, ip);
|
||||
strcat(ec200t_cmd, "\",");
|
||||
strcat(ec200t_cmd, port);
|
||||
strcat(ec200t_cmd, ",0,2\r\n");
|
||||
|
||||
ADAPTER_DEBUG("Ec200t connect AT CMD :%s\n", ec200t_cmd);
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, ec200t_cmd);
|
||||
AtSetReplyEndChar(adapter->agent, 0x43, 0x54);
|
||||
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec200t_cmd, EC200T_CONNECT_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
ADAPTER_DEBUG("Ec200t connect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec200t connect TCP failed. Power down\n");
|
||||
Ec200tPowerSet();
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int Ec200tSend(struct Adapter *adapter, const void *buf, size_t len)
|
||||
{
|
||||
PrivWrite(adapter->fd, buf, len);
|
||||
x_err_t result = EOK;
|
||||
if (adapter->agent) {
|
||||
EntmSend(adapter->agent, (const char *)buf, len);
|
||||
} else {
|
||||
printf("Ec200tSend can not find agent\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Ec200tRecv(struct Adapter *adapter, void *buf, size_t len)
|
||||
{
|
||||
PrivRead(adapter->fd, buf, len);
|
||||
return 0;
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 6);
|
||||
} else {
|
||||
printf("Ec200tRecv can not find agent\n");
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int Ec200tDisconnect(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t ec200t_cmd[64];
|
||||
|
||||
AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B);
|
||||
|
||||
/*step1: serial write "+++", quit transparent mode*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
|
||||
/*step2: serial write "AT+QICLOSE", close socket connect before open socket*/
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+QICLOSE=0\r\n");
|
||||
|
||||
PrivTaskDelay(2500);
|
||||
memset(ec200t_cmd, 0, sizeof(ec200t_cmd));
|
||||
sprintf(ec200t_cmd, EC200T_CLOSE_SOCKET_CMD, adapter->socket.socket_id);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, ec200t_cmd, EC200T_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
ADAPTER_DEBUG("Ec200t disconnect TCP done\n");
|
||||
|
||||
return 0;
|
||||
|
||||
out:
|
||||
ADAPTER_DEBUG("Ec200t disconnect TCP failed. Power down\n");
|
||||
Ec200tPowerSet();
|
||||
return -1;
|
||||
}
|
||||
|
||||
static const struct IpProtocolDone ec200t_done =
|
||||
|
@ -250,21 +324,16 @@ static const struct IpProtocolDone ec200t_done =
|
|||
|
||||
AdapterProductInfoType Ec200tAttach(struct Adapter *adapter)
|
||||
{
|
||||
struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo));
|
||||
struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("Ec200tAttach malloc product_info error\n");
|
||||
free(product_info);
|
||||
PrivFree(product_info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
product_info->model_name = ADAPTER_4G_EC200T;
|
||||
strcpy(product_info->model_name, ADAPTER_4G_EC200T);
|
||||
|
||||
product_info->model_done = (void *)&ec200t_done;
|
||||
|
||||
Ec200tPowerSet();
|
||||
|
||||
return product_info;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
SRC_FILES := adapter_5G.c
|
||||
SRC_FILES := adapter_5g.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -11,7 +11,7 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* @file adapter_5G.c
|
||||
* @file adapter_5g.c
|
||||
* @brief Implement the connection 5G adapter function
|
||||
* @version 1.1
|
||||
* @author AIIT XUOS Lab
|
|
@ -18,7 +18,7 @@ if SUPPORT_CONNECTION_FRAMEWORK
|
|||
bool "Using 4G adapter device"
|
||||
default n
|
||||
if CONNECTION_ADAPTER_4G
|
||||
source "$APP_DIR/Framework/connection/4G/Kconfig"
|
||||
source "$APP_DIR/Framework/connection/4g/Kconfig"
|
||||
endif
|
||||
|
||||
menuconfig CONNECTION_ADAPTER_NB
|
||||
|
@ -60,6 +60,6 @@ if SUPPORT_CONNECTION_FRAMEWORK
|
|||
bool "Using 5G adapter device"
|
||||
default n
|
||||
if CONNECTION_ADAPTER_5G
|
||||
source "$APP_DIR/Framework/connection/5G/Kconfig"
|
||||
source "$APP_DIR/Framework/connection/5g/Kconfig"
|
||||
endif
|
||||
endif
|
||||
|
|
|
@ -1,35 +1,35 @@
|
|||
SRC_FILES := adapter.c adapter_agent.c
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_LORA),y)
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_LORA),y)
|
||||
SRC_DIR += lora
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_4G),y)
|
||||
SRC_DIR += 4G
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_4G),y)
|
||||
SRC_DIR += 4g
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_NB),y)
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_NB),y)
|
||||
SRC_DIR += nbiot
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_WIFI),y)
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_WIFI),y)
|
||||
SRC_DIR += wifi
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_ETHERNET),y)
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_ETHERNET),y)
|
||||
SRC_DIR += ethernet
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_BLUETOOTH),y)
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_BLUETOOTH),y)
|
||||
SRC_DIR += bluetooth
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_ZIGBEE),y)
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_ZIGBEE),y)
|
||||
SRC_DIR += zigbee
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_5G),y)
|
||||
SRC_DIR += 5G
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER_5G),y)
|
||||
SRC_DIR += 5g
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -395,7 +395,7 @@ int AdapterDeviceConnect(struct Adapter *adapter, enum NetRoleType net_role, con
|
|||
* @param priv_net_group - private net group
|
||||
* @return success: 0 , failure: other
|
||||
*/
|
||||
int AdapterDeviceJoin(struct Adapter *adapter, const char *priv_net_group)
|
||||
int AdapterDeviceJoin(struct Adapter *adapter, unsigned char *priv_net_group)
|
||||
{
|
||||
if (!adapter)
|
||||
return -1;
|
||||
|
@ -419,9 +419,10 @@ int AdapterDeviceJoin(struct Adapter *adapter, const char *priv_net_group)
|
|||
/**
|
||||
* @description: Adapter disconnect from ip net or private net group
|
||||
* @param adapter - adapter device pointer
|
||||
* @param priv_net_group - private net group for PRIVATE_PROTOCOL quit function
|
||||
* @return success: 0 , failure: other
|
||||
*/
|
||||
int AdapterDeviceDisconnect(struct Adapter *adapter)
|
||||
int AdapterDeviceDisconnect(struct Adapter *adapter, unsigned char *priv_net_group)
|
||||
{
|
||||
if (!adapter)
|
||||
return -1;
|
||||
|
@ -432,7 +433,7 @@ int AdapterDeviceDisconnect(struct Adapter *adapter)
|
|||
if (NULL == priv_done->quit)
|
||||
return -1;
|
||||
|
||||
return priv_done->quit(adapter);
|
||||
return priv_done->quit(adapter, priv_net_group);
|
||||
} else if (IP_PROTOCOL == adapter->net_protocol) {
|
||||
struct IpProtocolDone *ip_done = (struct IpProtocolDone *)adapter->done;
|
||||
|
||||
|
|
|
@ -39,19 +39,10 @@ extern "C" {
|
|||
#define ADAPTER_LWIP_OPERATION 2
|
||||
#define ADAPTER_RAWIP_OPERATION 3
|
||||
|
||||
#define ADAPTER_LORA_FUNC ((uint32_t)(1 << ATAPTER_LORA))
|
||||
#define ADAPTER_4G_FUNC ((uint32_t)(1 << ADAPTER_4G))
|
||||
#define ADAPTER_NBIOT_FUNC ((uint32_t)(1 << ADAPTER_NBIOT))
|
||||
#define ADAPTER_WIFI_FUNC ((uint32_t)(1 << ADAPTER_WIFI))
|
||||
#define ADAPTER_ETHERNET_FUNC ((uint32_t)(1 << ADAPTER_ETHERNET))
|
||||
#define ADAPTER_BLUETOOTH_FUNC ((uint32_t)(1 << ADAPTER_BLUETOOTH))
|
||||
#define ADAPTER_ZIGBEE_FUNC ((uint32_t)(1 << ADAPTER_ZIGBEE))
|
||||
#define ADAPTER_5G_FUNC ((uint32_t)(1 << ADAPTER_5G))
|
||||
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
#define ADAPTER_DEBUG printf
|
||||
#else
|
||||
#define ADAPTER_DEBUF
|
||||
#define ADAPTER_DEBUG
|
||||
#endif
|
||||
|
||||
struct Adapter;
|
||||
|
@ -61,8 +52,14 @@ typedef struct AdapterProductInfo *AdapterProductInfoType;
|
|||
|
||||
struct Socket
|
||||
{
|
||||
int id;
|
||||
struct Adapter *adapter;
|
||||
uint8_t type; ///< socket type:DGRAM->UDP,STREAM->TCP
|
||||
uint8_t protocal; ///< udp or tcp
|
||||
unsigned short listen_port; ///< 0-65535
|
||||
uint8_t socket_id; ///< socket id
|
||||
uint8_t recv_control; ///< receive control
|
||||
uint8_t af_type; ///< IPv4 or IPv6
|
||||
char *src_ip_addr; ///< source P address
|
||||
char *dst_ip_addr; ///< destination IP address
|
||||
};
|
||||
|
||||
enum AdapterType
|
||||
|
@ -90,6 +87,10 @@ enum NetRoleType
|
|||
SERVER,
|
||||
MASTER,
|
||||
SLAVE,
|
||||
COORDINATOR,
|
||||
ROUTER,
|
||||
END_DEVICE,
|
||||
GATEWAY,
|
||||
ROLE_NONE,
|
||||
};
|
||||
|
||||
|
@ -110,8 +111,9 @@ enum IpType
|
|||
struct AdapterProductInfo
|
||||
{
|
||||
uint32_t functions;
|
||||
const char *vendor_name;
|
||||
const char *model_name;
|
||||
char vendor_name[NAME_NUM_MAX];
|
||||
char model_name[NAME_NUM_MAX];
|
||||
uint32_t work_mode;
|
||||
|
||||
void *model_done;
|
||||
};
|
||||
|
@ -146,23 +148,24 @@ struct PrivProtocolDone
|
|||
int (*setdhcp)(struct Adapter *adapter, int enable);
|
||||
int (*ping)(struct Adapter *adapter, const char *destination);
|
||||
int (*netstat)(struct Adapter *adapter);
|
||||
int (*join)(struct Adapter *adapter, const char *priv_net_group);
|
||||
int (*join)(struct Adapter *adapter, unsigned char *priv_net_group);
|
||||
int (*send)(struct Adapter *adapter, const void *buf, size_t len);
|
||||
int (*recv)(struct Adapter *adapter, void *buf, size_t len);
|
||||
int (*quit)(struct Adapter *adapter);
|
||||
int (*quit)(struct Adapter *adapter, unsigned char *priv_net_group);
|
||||
};
|
||||
|
||||
struct Adapter
|
||||
{
|
||||
char *name;
|
||||
char name[NAME_NUM_MAX];
|
||||
int fd;
|
||||
|
||||
int product_info_flag;
|
||||
struct AdapterProductInfo *info;
|
||||
ATAgentType agent;
|
||||
|
||||
//struct Socket *socket;
|
||||
struct Socket socket;
|
||||
|
||||
int net_role_id;
|
||||
enum NetProtocolType net_protocol;
|
||||
enum NetRoleType net_role;
|
||||
enum AdapterStatus adapter_status;
|
||||
|
@ -170,6 +173,7 @@ struct Adapter
|
|||
char buffer[ADAPTER_BUFFSIZE];
|
||||
|
||||
void *done;
|
||||
void *adapter_param;
|
||||
|
||||
struct DoublelistNode link;
|
||||
};
|
||||
|
@ -205,10 +209,10 @@ int AdapterDeviceControl(struct Adapter *adapter, int cmd, void *args);
|
|||
int AdapterDeviceConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type);
|
||||
|
||||
/*Join to a certain private net, only support PRIVATE_PROTOCOL*/
|
||||
int AdapterDeviceJoin(struct Adapter *adapter, const char *priv_net_group);
|
||||
int AdapterDeviceJoin(struct Adapter *adapter, unsigned char *priv_net_group);
|
||||
|
||||
/*Adapter disconnect from ip net or private net group*/
|
||||
int AdapterDeviceDisconnect(struct Adapter *adapter);
|
||||
int AdapterDeviceDisconnect(struct Adapter *adapter, unsigned char *priv_net_group);
|
||||
|
||||
/*Set up to net*/
|
||||
int AdapterDeviceSetUp(struct Adapter *adapter);
|
||||
|
@ -219,10 +223,10 @@ int AdapterDeviceSetDown(struct Adapter *adapter);
|
|||
/*Set ip/gateway/netmask address*/
|
||||
int AdapterDeviceSetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask);
|
||||
|
||||
/**/
|
||||
/*Set DNS function*/
|
||||
int AdapterDeviceSetDns(struct Adapter *adapter, const char *dns_addr, uint8 dns_count);
|
||||
|
||||
/**/
|
||||
/*Set DHCP function*/
|
||||
int AdapterDeviceSetDhcp(struct Adapter *adapter, int enable);
|
||||
|
||||
/*ping function*/
|
||||
|
|
|
@ -27,7 +27,6 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <user_api.h>
|
||||
#include <bus.h>
|
||||
|
||||
#define AT_CMD_MAX_LEN 128
|
||||
#define AT_AGENT_MAX 2
|
||||
|
@ -36,18 +35,19 @@ static uint32 last_cmd_len = 0;
|
|||
|
||||
static struct ATAgent at_agent_table[AT_AGENT_MAX] = {0};
|
||||
|
||||
uint IpTint(char *ipstr){
|
||||
unsigned int IpTint(char *ipstr)
|
||||
{
|
||||
if (ipstr == NULL)
|
||||
return 0;
|
||||
|
||||
char *token;
|
||||
uint i = 3, total = 0, cur;
|
||||
unsigned int i = 3, total = 0, cur;
|
||||
|
||||
token = strtok(ipstr, ".");
|
||||
|
||||
while (token != NULL){
|
||||
while (token != NULL) {
|
||||
cur = atoi(token);
|
||||
if (cur >= 0 && cur <= 255){
|
||||
if (cur >= 0 && cur <= 255) {
|
||||
total += cur * pow(256, i);
|
||||
}
|
||||
i--;
|
||||
|
@ -61,8 +61,8 @@ void SwapStr(char *str, int begin, int end)
|
|||
{
|
||||
int i, j;
|
||||
|
||||
for (i = begin, j = end; i <= j; i++, j--){
|
||||
if (str[i] != str[j]){
|
||||
for (i = begin, j = end; i <= j; i++, j--) {
|
||||
if (str[i] != str[j]) {
|
||||
str[i] = str[i] ^ str[j];
|
||||
str[j] = str[i] ^ str[j];
|
||||
str[i] = str[i] ^ str[j];
|
||||
|
@ -70,16 +70,16 @@ void SwapStr(char *str, int begin, int end)
|
|||
}
|
||||
}
|
||||
|
||||
char *IpTstr(uint ipint)
|
||||
char *IpTstr(unsigned int ipint)
|
||||
{
|
||||
int LEN = 16;
|
||||
char *new = (char *)malloc(LEN);
|
||||
char *new = (char *)PrivMalloc(LEN);
|
||||
memset(new, '\0', LEN);
|
||||
new[0] = '.';
|
||||
char token[4];
|
||||
int bt, ed, len, cur;
|
||||
|
||||
while (ipint){
|
||||
while (ipint) {
|
||||
cur = ipint % 256;
|
||||
sprintf(token, "%d", cur);
|
||||
strcat(new, token);
|
||||
|
@ -91,8 +91,8 @@ char *IpTstr(uint ipint)
|
|||
len = strlen(new);
|
||||
SwapStr(new, 0, len - 1);
|
||||
|
||||
for (bt = ed = 0; ed < len;){
|
||||
while (ed < len && new[ed] != '.'){
|
||||
for (bt = ed = 0; ed < len;) {
|
||||
while (ed < len && new[ed] != '.') {
|
||||
ed++;
|
||||
}
|
||||
SwapStr(new, bt, ed - 1);
|
||||
|
@ -117,20 +117,25 @@ int ParseATReply(char *str, const char *format, ...)
|
|||
return counts;
|
||||
}
|
||||
|
||||
uint32 ATSprintf(int fd, const char *format, va_list params)
|
||||
void ATSprintf(int fd, const char *format, va_list params)
|
||||
{
|
||||
last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, params);
|
||||
printf("ATSprintf send %s\n",send_buf);
|
||||
printf("AT send %s len %u\n",send_buf, last_cmd_len);
|
||||
PrivWrite(fd, send_buf, last_cmd_len);
|
||||
}
|
||||
|
||||
int ATOrderSend(ATAgentType agent, uint32 timeout, ATReplyType reply, const char *cmd_expr, ...)
|
||||
int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const char *cmd_expr, ...)
|
||||
{
|
||||
if (agent == NULL){
|
||||
if (agent == NULL) {
|
||||
printf("ATAgent is null");
|
||||
return -ERROR;
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct timespec abstime;
|
||||
|
||||
abstime.tv_sec = timeout_s;
|
||||
|
||||
PrivMutexObtain(&agent->lock);
|
||||
agent->receive_mode = AT_MODE;
|
||||
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
|
@ -141,78 +146,171 @@ int ATOrderSend(ATAgentType agent, uint32 timeout, ATReplyType reply, const char
|
|||
|
||||
va_list params;
|
||||
uint32 cmd_size = 0;
|
||||
uint32 result = EOK;
|
||||
uint32 result = 0;
|
||||
const char *cmd = NULL;
|
||||
PrivMutexObtain(&agent->lock);
|
||||
|
||||
agent->reply = reply;
|
||||
|
||||
if(agent->reply != NULL){
|
||||
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 (UserSemaphoreObtain(agent->rsp_sem, timeout) != EOK){
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != EOK) {
|
||||
printf("take sem %d timeout\n",agent->rsp_sem);
|
||||
result = -ETIMEOUT;
|
||||
goto __out;
|
||||
}
|
||||
}else{
|
||||
} 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 = ENTM_MODE;
|
||||
|
||||
// agent->reply = NULL;
|
||||
return result;
|
||||
}
|
||||
|
||||
int AtCmdConfigAndCheck(ATAgentType agent, char *cmd, char *check)
|
||||
{
|
||||
int ret = 0;
|
||||
char *result = NULL;
|
||||
if (NULL == agent || NULL == cmd || NULL == check ) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ATReplyType reply = CreateATReply(256);
|
||||
if (NULL == reply) {
|
||||
printf("%s %d at_create_resp failed!\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
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) {
|
||||
printf("%s %n get reply failed.\n",__func__,__LINE__);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
printf("[reply result :\n");
|
||||
printf("%s]\n", result);
|
||||
if(!strstr(result, check)) {
|
||||
printf("%s %d check[%s] reply[%s] failed.\n",__func__,__LINE__,check,result);
|
||||
ret = -1;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
DeleteATReply(reply);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char *GetReplyText(ATReplyType reply)
|
||||
{
|
||||
return reply->reply_buffer;
|
||||
}
|
||||
|
||||
int AtSetReplyLrEnd(ATAgentType agent, char enable)
|
||||
{
|
||||
if (!agent) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
agent->reply_lr_end = enable;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int AtSetReplyEndChar(ATAgentType agent, char last_ch, char end_ch)
|
||||
{
|
||||
if (!agent) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
agent->reply_end_last_char = last_ch;
|
||||
agent->reply_end_char = end_ch;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int AtSetReplyCharNum(ATAgentType agent, unsigned int num)
|
||||
{
|
||||
if (!agent) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
agent->reply_char_num = num;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
write(agent->fd, send_buf, len + 2);
|
||||
// memcpy(send_buf + len, "!@", 2);
|
||||
|
||||
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 time_out)
|
||||
int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
|
||||
{
|
||||
UserTaskDelay(1000);
|
||||
struct timespec abstime;
|
||||
|
||||
abstime.tv_sec = timeout_s;
|
||||
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);
|
||||
|
||||
|
||||
printf("EntmRecv once len %d.\n", agent->entm_recv_len);
|
||||
|
||||
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);
|
||||
|
||||
UserSemaphoreSetValue(agent->entm_rx_notice, 0);
|
||||
|
||||
if (UserSemaphoreObtain(agent->entm_rx_notice, time_out)){
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
if (buffer_len < agent->entm_recv_len){
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
printf("EntmRecv once .\n");
|
||||
|
||||
agent->entm_recv_buf[agent->entm_recv_len - 2] = '\0';
|
||||
memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len - 2);
|
||||
|
||||
return EOK;
|
||||
return buffer_len;
|
||||
}
|
||||
|
||||
static int GetCompleteATReply(ATAgentType agent)
|
||||
|
@ -221,49 +319,79 @@ 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){
|
||||
read(agent->fd, &ch, 1);
|
||||
memset(agent->entm_recv_buf, 0x00, 256);
|
||||
agent->entm_recv_len = 0;
|
||||
|
||||
printf(" %c(0x%x)\n", ch, ch);
|
||||
PrivMutexAbandon(&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;
|
||||
while (1)
|
||||
{
|
||||
PrivRead(agent->fd, &ch, 1);
|
||||
#ifdef CONNECTION_FRAMEWORK_DEBUG
|
||||
printf(" %c (0x%x)\n", ch, ch);
|
||||
#endif
|
||||
|
||||
if (last_ch == '!' && ch == '@'){
|
||||
UserSemaphoreAbandon(agent->entm_rx_notice);
|
||||
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){
|
||||
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{
|
||||
} else {
|
||||
printf("entm_recv_buf is_full ...\n");
|
||||
}
|
||||
}
|
||||
else if (agent->receive_mode == AT_MODE){
|
||||
if (read_len < agent->maintain_max){
|
||||
agent->maintain_buffer[read_len++] = ch;
|
||||
}
|
||||
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;
|
||||
}else{
|
||||
} else {
|
||||
printf("maintain_len is_full ...\n");
|
||||
is_full = true;
|
||||
}
|
||||
|
||||
if ((ch == '\n' && last_ch == '\r')){
|
||||
if (is_full){
|
||||
if (((ch == '\n') && (last_ch == '\r') && (agent->reply_lr_end)) ||
|
||||
((ch == agent->reply_end_char) && (agent->reply_end_char) &&
|
||||
(last_ch == agent->reply_end_last_char) && (agent->reply_end_last_char)) ||
|
||||
((read_len == agent->reply_char_num) && (agent->reply_char_num))){
|
||||
if (is_full) {
|
||||
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;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
return -ERROR;
|
||||
}
|
||||
printf("GetCompleteATReply get n r ...\n");
|
||||
|
||||
printf("GetCompleteATReply done\n");
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
break;
|
||||
}
|
||||
last_ch = ch;
|
||||
}
|
||||
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
}
|
||||
|
||||
return read_len;
|
||||
|
@ -272,8 +400,8 @@ static int GetCompleteATReply(ATAgentType agent)
|
|||
ATAgentType GetATAgent(const char *agent_name)
|
||||
{
|
||||
struct ATAgent* result = NULL;
|
||||
for (int i = 0; i < AT_AGENT_MAX; i++){
|
||||
if (strcmp(at_agent_table[i].agent_name, agent_name) == 0){
|
||||
for (int i = 0; i < AT_AGENT_MAX; i++) {
|
||||
if (strcmp(at_agent_table[i].agent_name, agent_name) == 0) {
|
||||
result = &at_agent_table[i];
|
||||
}
|
||||
}
|
||||
|
@ -282,122 +410,124 @@ ATAgentType GetATAgent(const char *agent_name)
|
|||
}
|
||||
|
||||
|
||||
static int DeleteATAgent(ATAgentType agent)
|
||||
int DeleteATAgent(ATAgentType agent)
|
||||
{
|
||||
if (agent->lock){
|
||||
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){
|
||||
UserSemaphoreDelete(agent->entm_rx_notice);
|
||||
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){
|
||||
close(agent->fd);
|
||||
if (agent->rsp_sem) {
|
||||
printf("delete agent rsp_sem = %d\n",agent->rsp_sem);
|
||||
PrivSemaphoreDelete(&agent->rsp_sem);
|
||||
}
|
||||
|
||||
if (agent->rsp_sem){
|
||||
UserSemaphoreDelete(agent->rsp_sem);
|
||||
}
|
||||
|
||||
if (agent->maintain_buffer){
|
||||
free(agent->maintain_buffer);
|
||||
if (agent->maintain_buffer) {
|
||||
PrivFree(agent->maintain_buffer);
|
||||
}
|
||||
|
||||
memset(agent, 0x00, sizeof(struct ATAgent));
|
||||
printf("delete ATagent\n");
|
||||
}
|
||||
|
||||
static void ATAgentReceiveProcess(void *param)
|
||||
static void *ATAgentReceiveProcess(void *param)
|
||||
{
|
||||
ATAgentType agent = (ATAgentType)param;
|
||||
const struct at_urc *urc;
|
||||
|
||||
while (1){
|
||||
if (GetCompleteATReply(agent) > 0){
|
||||
while (1) {
|
||||
if (GetCompleteATReply(agent) > 0)
|
||||
{
|
||||
PrivMutexObtain(&agent->lock);
|
||||
if (agent->reply != NULL){
|
||||
ATReplyType reply = agent->reply;
|
||||
|
||||
agent->maintain_buffer[agent->maintain_len - 1] = '\0';
|
||||
|
||||
if (agent->maintain_len < reply->reply_max_len){
|
||||
agent->maintain_buffer[agent->maintain_len] = '\0';
|
||||
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);
|
||||
} else {
|
||||
printf("out of memory (%d)!\n", reply->reply_max_len);
|
||||
}
|
||||
|
||||
agent->reply = NULL;
|
||||
UserSemaphoreAbandon(agent->rsp_sem);
|
||||
// agent->reply = NULL;
|
||||
PrivSemaphoreAbandon(&agent->rsp_sem);
|
||||
}
|
||||
PrivMutexAbandon(&agent->lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int ATAgentInit(ATAgentType agent)
|
||||
{
|
||||
int result = EOK;
|
||||
UtaskType at_utask;
|
||||
do
|
||||
{
|
||||
agent->maintain_len = 0;
|
||||
agent->maintain_buffer = (char *)malloc(agent->maintain_max);
|
||||
int result = 0;
|
||||
|
||||
if (agent->maintain_buffer == NONE){
|
||||
break;
|
||||
}
|
||||
agent->maintain_len = 0;
|
||||
agent->maintain_buffer = (char *)PrivMalloc(agent->maintain_max);
|
||||
|
||||
agent->entm_rx_notice = UserSemaphoreCreate(0);
|
||||
if (agent->entm_rx_notice == 0){
|
||||
break;
|
||||
}
|
||||
if (agent->maintain_buffer == NULL) {
|
||||
printf("ATAgentInit malloc maintain_buffer error\n");
|
||||
goto __out;
|
||||
}
|
||||
|
||||
agent->rsp_sem = UserSemaphoreCreate(0);
|
||||
if (agent->rsp_sem == 0){
|
||||
break;
|
||||
}
|
||||
if(PrivMutexCreate(&agent->lock, 0) < 0) {
|
||||
printf("AdapterFrameworkInit mutex create failed.\n");
|
||||
}
|
||||
if (agent->lock == 0){
|
||||
break;
|
||||
}
|
||||
memset(agent->maintain_buffer, 0, agent->maintain_max);
|
||||
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
result = PrivSemaphoreCreate(&agent->entm_rx_notice, 0, 0);
|
||||
if (result < 0) {
|
||||
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;
|
||||
}
|
||||
|
||||
strncpy(at_utask.name, "recv_task", strlen("recv_task"));
|
||||
at_utask.func_entry = ATAgentReceiveProcess;
|
||||
at_utask.func_param = agent;
|
||||
at_utask.stack_size = 1024;
|
||||
at_utask.prio = 18;
|
||||
agent->receive_mode = DEFAULT_MODE;
|
||||
|
||||
agent->at_handler = UserTaskCreate(at_utask);
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 18;
|
||||
attr.stacksize = 4096;
|
||||
|
||||
// struct SerialDataCfg data_cfg;
|
||||
// memset(&data_cfg, 0, sizeof(struct SerialDataCfg));
|
||||
// data_cfg.serial_baud_rate = 57600;
|
||||
// ioctl(agent->fd, OPE_INT, &data_cfg);
|
||||
|
||||
if (agent->at_handler == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
result = EOK;
|
||||
return result;
|
||||
} while (1);
|
||||
PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, agent);
|
||||
printf("create agent->at_handler = %d\n",agent->at_handler);
|
||||
return result;
|
||||
|
||||
__out:
|
||||
DeleteATAgent(agent);
|
||||
result = -ERROR;
|
||||
result = -1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int InitATAgent(const char *agent_name, int agent_fd, uint32 maintain_max)
|
||||
{
|
||||
int i = 0;
|
||||
int result = EOK;
|
||||
int open_result = EOK;
|
||||
struct ATAgent *agent = NONE;
|
||||
int result = 0;
|
||||
int open_result = 0;
|
||||
struct ATAgent *agent = NULL;
|
||||
|
||||
if (GetATAgent(agent_name) != NULL) {
|
||||
return result;
|
||||
|
@ -409,7 +539,7 @@ int InitATAgent(const char *agent_name, int agent_fd, uint32 maintain_max)
|
|||
|
||||
if (i >= AT_AGENT_MAX) {
|
||||
printf("agent buffer(%d) is full.", AT_AGENT_MAX);
|
||||
result = -ERROR;
|
||||
result = -1;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
@ -422,9 +552,8 @@ int InitATAgent(const char *agent_name, int agent_fd, uint32 maintain_max)
|
|||
agent->maintain_max = maintain_max;
|
||||
|
||||
result = ATAgentInit(agent);
|
||||
if (result == EOK)
|
||||
{
|
||||
UserTaskStartup(agent->at_handler);
|
||||
if (result == 0) {
|
||||
PrivTaskStartup(&agent->at_handler);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
@ -434,38 +563,38 @@ ATReplyType CreateATReply(uint32 reply_max_len)
|
|||
{
|
||||
ATReplyType reply = NULL;
|
||||
|
||||
reply = (ATReplyType)malloc(sizeof(struct ATReply));
|
||||
if (reply == NULL){
|
||||
reply = (ATReplyType)PrivMalloc(sizeof(struct ATReply));
|
||||
if (reply == NULL) {
|
||||
printf("no more memory\n");
|
||||
return NULL;
|
||||
}
|
||||
memset(reply, 0, sizeof(struct ATReply));
|
||||
|
||||
reply->reply_max_len = reply_max_len;
|
||||
|
||||
reply->reply_buffer = (char *)malloc(reply_max_len);
|
||||
if (reply->reply_buffer == NULL){
|
||||
reply->reply_buffer = (char *)PrivMalloc(reply_max_len);
|
||||
if (reply->reply_buffer == NULL) {
|
||||
printf("no more memory\n");
|
||||
free(reply);
|
||||
PrivFree(reply);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(reply->reply_buffer, 0, reply_max_len);
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
void DeleteATReply(ATReplyType reply)
|
||||
{
|
||||
if (reply){
|
||||
if (reply->reply_buffer){
|
||||
free(reply->reply_buffer);
|
||||
if (reply) {
|
||||
if (reply->reply_buffer) {
|
||||
PrivFree(reply->reply_buffer);
|
||||
reply->reply_buffer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (reply){
|
||||
free(reply);
|
||||
if (reply) {
|
||||
PrivFree(reply);
|
||||
reply = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -26,8 +26,11 @@
|
|||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#define REPLY_TIME_OUT 10
|
||||
|
||||
enum ReceiveMode
|
||||
{
|
||||
DEFAULT_MODE = 0,
|
||||
ENTM_MODE = 1,
|
||||
AT_MODE = 2,
|
||||
};
|
||||
|
@ -44,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;
|
||||
|
@ -52,9 +56,13 @@ struct ATAgent
|
|||
int lock;
|
||||
|
||||
ATReplyType reply;
|
||||
char reply_lr_end;
|
||||
char reply_end_last_char;
|
||||
char reply_end_char;
|
||||
uint32 reply_char_num;
|
||||
int rsp_sem;
|
||||
|
||||
int32 at_handler;
|
||||
pthread_t at_handler;
|
||||
|
||||
#define ENTM_RECV_MAX 256
|
||||
char entm_recv_buf[ENTM_RECV_MAX];
|
||||
|
@ -65,18 +73,21 @@ struct ATAgent
|
|||
typedef struct ATAgent *ATAgentType;
|
||||
|
||||
int EntmSend(ATAgentType agent, const char *data, int len);
|
||||
int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int time_out);
|
||||
int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s);
|
||||
char *GetReplyText(ATReplyType reply);
|
||||
int AtSetReplyEndChar(ATAgentType agent, char last_ch, char end_ch);
|
||||
int AtSetReplyCharNum(ATAgentType agent, unsigned int num);
|
||||
int AtSetReplyLrEnd(ATAgentType agent, char enable);
|
||||
ATReplyType CreateATReply(uint32 reply_max_len);
|
||||
uint IpTint(char *ipstr);
|
||||
unsigned int IpTint(char *ipstr);
|
||||
void SwapStr(char *str, int begin, int end);
|
||||
char* IpTstr(uint ipint);
|
||||
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, ATReplyType reply, const char *cmd_expr, ...);
|
||||
|
||||
#define REPLY_TIME_OUT 3000
|
||||
int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const char *cmd_expr, ...);
|
||||
int AtCmdConfigAndCheck(ATAgentType agent, char *cmd, char *check);
|
||||
|
||||
#endif
|
|
@ -1,170 +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 hc08.c
|
||||
* @brief Implement the connection Bluetooth adapter function, using HC08 device
|
||||
* @version 1.1
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.07.12
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
static int rx_sem;
|
||||
static sem_t *hc08_sem;
|
||||
static pthread_t hc08_recv_thread;
|
||||
|
||||
void Hc08RecvThreadEntry(void *parameter)
|
||||
{
|
||||
|
||||
while (1)
|
||||
{
|
||||
PrivRead(adapter->fd, buf, len);
|
||||
|
||||
UserSemaphoreAbandon(rx_sem);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
static int Hc08Open(struct Adapter *adapter)
|
||||
{
|
||||
if (INSTALL == adapter->adapter_status) {
|
||||
printf("Hc08 has already been open\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*step1: open hc08 serial port*/
|
||||
adapter->fd = PrivOpen(ADAPTER_HC08_DRIVER, O_RDWR);
|
||||
if (adapter->fd < 0) {
|
||||
printf("Hc08Open get serial %s fd error\n", ADAPTER_HC08_DRIVER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*step2: init AT agent*/
|
||||
if (!adapter->agent) {
|
||||
char *agent_name = "bluetooth_uart_client";
|
||||
if (EOK != InitATAgent(agent_name, adapter->fd, 512)) {
|
||||
printf("at agent init failed !\n");
|
||||
return -1;
|
||||
}
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
|
||||
adapter->agent = at_agent;
|
||||
}
|
||||
|
||||
/*step3: create bluetooth receive task*/
|
||||
PrivSemaphoreCreate(hc08_sem, 0, rx_sem);
|
||||
|
||||
PrivTaskCreate(&hc08_recv_thread, NULL, Hc08RecvThreadEntry, NULL);
|
||||
|
||||
ADAPTER_DEBUG("Hc08 open done\n");
|
||||
}
|
||||
|
||||
static int Hc08Close(struct Adapter *adapter)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Ioctl(struct Adapter *adapter, int cmd, void *args)
|
||||
{
|
||||
if (OPE_INT != cmd) {
|
||||
printf("Hc08Ioctl only support OPE_INT, do not support %d\n", cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t baud_rate = *((uint32_t *)args);
|
||||
|
||||
struct SerialDataCfg serial_cfg;
|
||||
memset(&serial_cfg, 0 ,sizeof(struct SerialDataCfg));
|
||||
serial_cfg.serial_baud_rate = baud_rate;
|
||||
serial_cfg.serial_data_bits = DATA_BITS_8;
|
||||
serial_cfg.serial_stop_bits = STOP_BITS_1;
|
||||
serial_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
serial_cfg.serial_parity_mode = PARITY_NONE;
|
||||
serial_cfg.serial_bit_order = STOP_BITS_1;
|
||||
serial_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
#ifdef ADAPTER_HC08_DRIVER_EXT_PORT
|
||||
serial_cfg.ext_uart_no = ADAPTER_HC08_DRIVER_EXT_PORT;
|
||||
serial_cfg.port_configure = PORT_CFG_INIT;
|
||||
#endif
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = &serial_cfg;
|
||||
PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
|
||||
|
||||
ADAPTER_DEBUG("Hc08 ioctl done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08SetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static int Hc08Connect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static int Hc08Send(struct Adapter *adapter, const void *buf, size_t len)
|
||||
{
|
||||
PrivWrite(adapter->fd, buf, len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Recv(struct Adapter *adapter, void *buf, size_t len)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Disconnect(struct Adapter *adapter)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static const struct IpProtocolDone hc08_done =
|
||||
{
|
||||
.open = Hc08Open,
|
||||
.close = Hc08Close,
|
||||
.ioctl = Hc08Ioctl,
|
||||
.setup = NULL,
|
||||
.setdown = NULL,
|
||||
.setaddr = Hc08SetAddr,
|
||||
.setdns = NULL,
|
||||
.setdhcp = NULL,
|
||||
.ping = NULL,
|
||||
.netstat = NULL,
|
||||
.connect = Hc08Connect,
|
||||
.send = Hc08Send,
|
||||
.recv = Hc08Recv,
|
||||
.disconnect = Hc08Disconnect,
|
||||
};
|
||||
|
||||
AdapterProductInfoType Hc08Attach(struct Adapter *adapter)
|
||||
{
|
||||
struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("Hc08Attach malloc product_info error\n");
|
||||
free(product_info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
product_info->model_name = ADAPTER_BLUETOOTH_HC08;
|
||||
|
||||
product_info->model_done = (void *)&hc08_done;
|
||||
|
||||
return product_info;
|
||||
}
|
|
@ -1,14 +1,7 @@
|
|||
config ADAPTER_BLUETOOTH
|
||||
bool "Using bluetooth adapter function"
|
||||
config ADAPTER_HC08
|
||||
bool "Using bluetooth adapter device HC08"
|
||||
default y
|
||||
|
||||
if ADAPTER_BLUETOOTH
|
||||
config ADAPTER_HC08
|
||||
bool "Using bluetooth adapter device HC08"
|
||||
default y
|
||||
|
||||
if ADAPTER_HC08
|
||||
source "$APP_DIR/Framework/connection/bluetooth/HC08/Kconfig"
|
||||
endif
|
||||
|
||||
endif
|
||||
if ADAPTER_HC08
|
||||
source "$APP_DIR/Framework/connection/bluetooth/hc08/Kconfig"
|
||||
endif
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
SRC_FILES := adapter_bluetooth.c
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_HC08),y)
|
||||
SRC_DIR += HC08
|
||||
SRC_DIR += hc08
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
@ -17,3 +17,96 @@
|
|||
* @author AIIT XUOS Lab
|
||||
* @date 2021.06.25
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
|
||||
#ifdef ADAPTER_HC08
|
||||
extern AdapterProductInfoType Hc08Attach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
#define ADAPTER_BLUETOOTH_NAME "bluetooth"
|
||||
|
||||
static int AdapterBlueToothRegister(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
strncpy(adapter->name, ADAPTER_BLUETOOTH_NAME, NAME_NUM_MAX);
|
||||
|
||||
adapter->net_protocol = IP_PROTOCOL;
|
||||
adapter->adapter_status = UNREGISTERED;
|
||||
|
||||
ret = AdapterDeviceRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("AdapterBlueToothRegister register error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int AdapterBlueToothInit(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter));
|
||||
if (!adapter) {
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(adapter, 0, sizeof(struct Adapter));
|
||||
|
||||
ret = AdapterBlueToothRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("AdapterBlueToothInit register BT adapter error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef ADAPTER_HC08
|
||||
AdapterProductInfoType product_info = Hc08Attach(adapter);
|
||||
if (!product_info) {
|
||||
printf("AdapterBlueToothInit hc08 attach error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
adapter->product_info_flag = 1;
|
||||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************4G TEST*********************/
|
||||
int AdapterBlueToothTest(void)
|
||||
{
|
||||
const char *bluetooth_msg = "BT Adapter Test";
|
||||
char bluetooth_recv_msg[128];
|
||||
int len;
|
||||
int baud_rate = BAUD_RATE_115200;
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_BLUETOOTH_NAME);
|
||||
|
||||
#ifdef ADAPTER_HC08
|
||||
AdapterDeviceOpen(adapter);
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
|
||||
len = strlen(bluetooth_msg);
|
||||
|
||||
while (1) {
|
||||
AdapterDeviceRecv(adapter, bluetooth_recv_msg, 128);
|
||||
printf("bluetooth_recv_msg %s\n", bluetooth_recv_msg);
|
||||
AdapterDeviceSend(adapter, bluetooth_msg, len);
|
||||
printf("send %s after recv\n", bluetooth_msg);
|
||||
PrivTaskDelay(100);
|
||||
memset(bluetooth_recv_msg, 0, 128);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
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, AdapterBlueToothTest, AdapterBlueToothTest, show adapter BT information);
|
||||
|
|
|
@ -3,6 +3,14 @@ config ADAPTER_BLUETOOTH_HC08
|
|||
default "hc08"
|
||||
|
||||
if ADD_XIUOS_FETURES
|
||||
config ADAPTER_HC08_RECV_BUFFER_SIZE
|
||||
int "HC08 recv data buffer size"
|
||||
default "128"
|
||||
|
||||
config ADAPTER_HC08_WORK_ROLE
|
||||
string "HC08 work role M(MASTER) or S(SLAVER)"
|
||||
default "M"
|
||||
|
||||
config ADAPTER_HC08_DRIVER_EXTUART
|
||||
bool "Using extra uart to support bluetooth"
|
||||
default n
|
|
@ -0,0 +1,430 @@
|
|||
/*
|
||||
* 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 hc08.c
|
||||
* @brief Implement the connection Bluetooth adapter function, using HC08 device
|
||||
* @version 1.1
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.07.12
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
#define HC08_DETECT_CMD "AT"
|
||||
#define HC08_DEFAULT_CMD "AT+DEFAULT"
|
||||
#define HC08_RESET_CMD "AT+RESET"
|
||||
#define HC08_CLEAR_CMD "AT+CLEAR"
|
||||
#define HC08_GET_DEVICE_INFO "AT+RX"
|
||||
|
||||
#define HC08_GET_BAUDRATE_CMD "AT+BAUD=?"
|
||||
#define HC08_SET_BAUDRATE_CMD "AT+BAUD=%u"
|
||||
#define HC08_GET_CONNECTABLE "AT+CONT=?"
|
||||
#define HC08_SET_CONNECTABLE "AT+CONT=%s"
|
||||
#define HC08_GET_ROLE_CMD "AT+ROLE=?"
|
||||
#define HC08_SET_ROLE_CMD "AT+ROLE=%s"
|
||||
#define HC08_GET_ADDR_CMD "AT+ADDR=?"
|
||||
#define HC08_SET_ADDR_CMD "AT+ADDR=%s"
|
||||
#define HC08_GET_NAME_CMD "AT+NAME=%s"
|
||||
#define HC08_SET_NAME_CMD "AT+NAME=?"
|
||||
|
||||
#define HC08_OK_RESP "OK"
|
||||
|
||||
#define HC08_CMD_STR_DEFAULT_SIZE 64
|
||||
#define HC08_RESP_DEFAULT_SIZE 64
|
||||
|
||||
enum Hc08AtCmd
|
||||
{
|
||||
HC08_AT_CMD_DETECT = 0,
|
||||
HC08_AT_CMD_DEFAULT,
|
||||
HC08_AT_CMD_RESET,
|
||||
HC08_AT_CMD_CLEAR,
|
||||
HC08_AT_CMD_GET_DEVICE_INFO,
|
||||
HC08_AT_CMD_GET_BAUDRATE,
|
||||
HC08_AT_CMD_SET_BAUDRATE,
|
||||
HC08_AT_CMD_SET_CONNECTABLE,
|
||||
HC08_AT_CMD_GET_CONNECTABLE,
|
||||
HC08_AT_CMD_SET_ROLE,
|
||||
HC08_AT_CMD_GET_ROLE,
|
||||
HC08_AT_CMD_SET_ADDR,
|
||||
HC08_AT_CMD_GET_ADDR,
|
||||
HC08_AT_CMD_SET_NAME,
|
||||
HC08_AT_CMD_GET_NAME,
|
||||
HC08_AT_CMD_END,
|
||||
};
|
||||
|
||||
static int Hc08CheckMacHex(char *hex, int len)
|
||||
{
|
||||
int i = 0;
|
||||
while (i < len)
|
||||
{
|
||||
if ((hex[i] >= 'A' && hex[i] <= 'F') ||
|
||||
(hex[i] >= 'a' && hex[i] <= 'f') ||
|
||||
(hex[i] >= '0' && hex[i] <= '9'))
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//HC08 AT cmd function
|
||||
static int Hc08AtConfigure(ATAgentType agent, enum Hc08AtCmd hc08_at_cmd, void *param, char *reply_info)
|
||||
{
|
||||
const char *result_buf;
|
||||
char *connectable, *role;
|
||||
unsigned int baudrate;
|
||||
char reply_ok_flag = 1;
|
||||
char cmd_str[HC08_CMD_STR_DEFAULT_SIZE] = {0};
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
if (NULL == reply) {
|
||||
printf("Hc08AtConfigure CreateATReply failed !\n");
|
||||
return -ENOMEMORY;
|
||||
}
|
||||
|
||||
switch (hc08_at_cmd)
|
||||
{
|
||||
case HC08_AT_CMD_DETECT:
|
||||
AtSetReplyEndChar(agent, 0x4F, 0x4B);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_DETECT_CMD);
|
||||
break;
|
||||
case HC08_AT_CMD_DEFAULT:
|
||||
AtSetReplyEndChar(agent, 0x4F, 0x4B);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_DEFAULT_CMD);
|
||||
break;
|
||||
case HC08_AT_CMD_RESET:
|
||||
AtSetReplyEndChar(agent, 0x4F, 0x4B);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_RESET_CMD);
|
||||
break;
|
||||
case HC08_AT_CMD_CLEAR:
|
||||
AtSetReplyEndChar(agent, 0x4F, 0x4B);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_CLEAR_CMD);
|
||||
break;
|
||||
case HC08_AT_CMD_GET_DEVICE_INFO:
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_DEVICE_INFO);
|
||||
reply_ok_flag = 0;
|
||||
break;
|
||||
case HC08_AT_CMD_SET_BAUDRATE:
|
||||
baudrate = *(unsigned int *)param;
|
||||
sprintf(cmd_str, HC08_SET_BAUDRATE_CMD, baudrate);
|
||||
strcat(cmd_str, ",N");
|
||||
AtSetReplyEndChar(agent, 0x4E, 0x45);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd_str);
|
||||
reply_ok_flag = 0;
|
||||
break;
|
||||
case HC08_AT_CMD_GET_BAUDRATE:
|
||||
AtSetReplyEndChar(agent, 0x2C, 0x4E);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_BAUDRATE_CMD);
|
||||
reply_ok_flag = 0;
|
||||
break;
|
||||
case HC08_AT_CMD_SET_CONNECTABLE:
|
||||
connectable = (char *)param;
|
||||
sprintf(cmd_str, HC08_SET_CONNECTABLE, connectable);
|
||||
AtSetReplyEndChar(agent, 0x4F, 0x4B);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd_str);
|
||||
break;
|
||||
case HC08_AT_CMD_GET_CONNECTABLE:
|
||||
AtSetReplyEndChar(agent, 0x6C, 0x65);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_CONNECTABLE);
|
||||
reply_ok_flag = 0;
|
||||
break;
|
||||
case HC08_AT_CMD_SET_ROLE:
|
||||
role = (char *)param;
|
||||
sprintf(cmd_str, HC08_SET_ROLE_CMD, role);
|
||||
AtSetReplyCharNum(agent, 1);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd_str);
|
||||
break;
|
||||
case HC08_AT_CMD_GET_ROLE:
|
||||
AtSetReplyCharNum(agent, 1);
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_ROLE_CMD);
|
||||
reply_ok_flag = 0;
|
||||
break;
|
||||
// case HC08_AT_CMD_SET_ADDR:
|
||||
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_SET_ROLE_CMD);
|
||||
// break;
|
||||
// case HC08_AT_CMD_GET_ADDR:
|
||||
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_ROLE_CMD);
|
||||
// reply_ok_flag = 0;
|
||||
// break;
|
||||
// case HC08_AT_CMD_SET_NAME:
|
||||
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_SET_NAME_CMD);
|
||||
// break;
|
||||
// case HC08_AT_CMD_GET_NAME:
|
||||
// ATOrderSend(agent, REPLY_TIME_OUT, reply, HC08_GET_NAME_CMD);
|
||||
// reply_ok_flag = 0;
|
||||
// break;
|
||||
default:
|
||||
printf("hc08 do not support no.%d cmd\n", hc08_at_cmd);
|
||||
DeleteATReply(reply);
|
||||
return -1;
|
||||
}
|
||||
|
||||
PrivTaskDelay(200);
|
||||
|
||||
result_buf = GetReplyText(reply);
|
||||
|
||||
if (reply_ok_flag) {
|
||||
if (!strstr(HC08_OK_RESP, result_buf)) {
|
||||
printf("%u cmd get reply OK failed:get reply %s\n", hc08_at_cmd, result_buf);
|
||||
}
|
||||
}
|
||||
|
||||
if (reply_info) {
|
||||
strncpy(reply_info, result_buf, reply->reply_len);
|
||||
}
|
||||
|
||||
DeleteATReply(reply);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Open(struct Adapter *adapter)
|
||||
{
|
||||
if (INSTALL == adapter->adapter_status) {
|
||||
printf("Hc08 has already been open\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
//step1: open hc08 serial port
|
||||
adapter->fd = PrivOpen(ADAPTER_HC08_DRIVER, O_RDWR);
|
||||
if (adapter->fd < 0) {
|
||||
printf("Hc08Open get serial %s fd error\n", ADAPTER_HC08_DRIVER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//step2: init AT agent
|
||||
if (!adapter->agent) {
|
||||
char *agent_name = "bluetooth_uart_client";
|
||||
printf("InitATAgent agent_name %s fd %u\n", agent_name, adapter->fd);
|
||||
if (EOK != InitATAgent(agent_name, adapter->fd, 512)) {
|
||||
printf("at agent init failed !\n");
|
||||
return -1;
|
||||
}
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
|
||||
adapter->agent = at_agent;
|
||||
|
||||
printf("Hc08Open adapter agent %p\n", adapter->agent);
|
||||
}
|
||||
|
||||
PrivTaskDelay(200);
|
||||
|
||||
ADAPTER_DEBUG("Hc08 open done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Close(struct Adapter *adapter)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Ioctl(struct Adapter *adapter, int cmd, void *args)
|
||||
{
|
||||
if (OPE_INT != cmd) {
|
||||
printf("Hc08Ioctl only support OPE_INT, do not support %d\n", cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char hc08_baudrate[HC08_RESP_DEFAULT_SIZE] = {0};
|
||||
uint32_t baud_rate = *((uint32_t *)args);
|
||||
|
||||
struct SerialDataCfg serial_cfg;
|
||||
memset(&serial_cfg, 0 ,sizeof(struct SerialDataCfg));
|
||||
serial_cfg.serial_baud_rate = baud_rate;
|
||||
serial_cfg.serial_data_bits = DATA_BITS_8;
|
||||
serial_cfg.serial_stop_bits = STOP_BITS_1;
|
||||
serial_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
serial_cfg.serial_parity_mode = PARITY_NONE;
|
||||
serial_cfg.serial_bit_order = STOP_BITS_1;
|
||||
serial_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
#ifdef ADAPTER_HC08_DRIVER_EXT_PORT
|
||||
serial_cfg.ext_uart_no = ADAPTER_HC08_DRIVER_EXT_PORT;
|
||||
serial_cfg.port_configure = PORT_CFG_INIT;
|
||||
#endif
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = &serial_cfg;
|
||||
PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
|
||||
|
||||
//Step1 : detect hc08 serial function
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_DETECT, NULL, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Step2 : set hc08 device serial baud, hc08_set_baud send "AT+BAUD=%s"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_BAUDRATE, args, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
PrivTaskDelay(200);
|
||||
|
||||
//Step3 : show hc08 device info, hc08_get send "AT+RX" response device info
|
||||
char device_info[HC08_RESP_DEFAULT_SIZE * 2] = {0};
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_DEVICE_INFO, NULL, device_info) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ADAPTER_DEBUG("Hc08 ioctl done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08SetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask)
|
||||
{
|
||||
char mac_addr[HC08_RESP_DEFAULT_SIZE] = {0};
|
||||
|
||||
//Step1 : hc08_get_addr send "AT+ADDR=?" response mac "XX,XX,XX,XX,XX,XX"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_ADDR, NULL, mac_addr) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("HC08 old mac addr: %s\n", mac_addr);
|
||||
|
||||
//Step2 : hc08_set_addr send "AT+ADDR=%s" response "OK"
|
||||
if (!Hc08CheckMacHex((char *)gateway, 12)) {
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_ADDR, (void *)gateway, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
//Step3 : check mac addr, hc08_get_addr send "AT+ADDR=?" response mac "XX,XX,XX,XX,XX,XX"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_ADDR, NULL, mac_addr) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("HC08 new mac addr: %s\n", mac_addr);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Connect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
|
||||
{
|
||||
char connect_status[HC08_RESP_DEFAULT_SIZE] = {0};
|
||||
|
||||
//Step1 : hc08_detect send "AT" response "OK"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_DETECT, NULL, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Step2 : hc08_set_role send "AT+ROLE=%s" response "OK"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_ROLE, ADAPTER_HC08_WORK_ROLE, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Step3 : hc08_connectable send "AT+CONT=0" response "OK"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_CONNECTABLE, "0", NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Step4 : check connectable status,hc08_connectable send "AT+CONT=?" response "Connectable"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_CONNECTABLE, NULL, connect_status) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Hc08Connect status %s\n", connect_status);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Send(struct Adapter *adapter, const void *buf, size_t len)
|
||||
{
|
||||
x_err_t result = EOK;
|
||||
if (adapter->agent) {
|
||||
EntmSend(adapter->agent, (const char *)buf, len);
|
||||
} else {
|
||||
printf("Hc08Send can not find agent\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int Hc08Recv(struct Adapter *adapter, void *buf, size_t len)
|
||||
{
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)buf, len, 40);
|
||||
} else {
|
||||
printf("Hc08Recv can not find agent\n");
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int Hc08Disconnect(struct Adapter *adapter)
|
||||
{
|
||||
char connect_status[HC08_RESP_DEFAULT_SIZE] = {0};
|
||||
|
||||
//Step1 : hc08_detect send "AT" response "OK"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_DETECT, NULL, NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Step2 : hc08_connectable send "AT+CONT=1" response "OK"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_SET_CONNECTABLE, "1", NULL) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
//Step3 : check connectable status,hc08_connectable send "AT+CONT=?" response "Not-Connectable"
|
||||
if (Hc08AtConfigure(adapter->agent, HC08_AT_CMD_GET_CONNECTABLE, NULL, connect_status) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Hc08Disconnect status %s\n", connect_status);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct IpProtocolDone hc08_done =
|
||||
{
|
||||
.open = Hc08Open,
|
||||
.close = Hc08Close,
|
||||
.ioctl = Hc08Ioctl,
|
||||
.setup = NULL,
|
||||
.setdown = NULL,
|
||||
.setaddr = Hc08SetAddr,
|
||||
.setdns = NULL,
|
||||
.setdhcp = NULL,
|
||||
.ping = NULL,
|
||||
.netstat = NULL,
|
||||
.connect = Hc08Connect,
|
||||
.send = Hc08Send,
|
||||
.recv = Hc08Recv,
|
||||
.disconnect = Hc08Disconnect,
|
||||
};
|
||||
|
||||
AdapterProductInfoType Hc08Attach(struct Adapter *adapter)
|
||||
{
|
||||
struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("Hc08Attach malloc product_info error\n");
|
||||
free(product_info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ("M" == ADAPTER_HC08_WORK_ROLE) {
|
||||
adapter->net_role = MASTER;
|
||||
} else if ("S" == ADAPTER_HC08_WORK_ROLE) {
|
||||
adapter->net_role = SLAVE;
|
||||
} else {
|
||||
adapter->net_role = ROLE_NONE;
|
||||
}
|
||||
|
||||
strcpy(product_info->model_name, ADAPTER_BLUETOOTH_HC08);
|
||||
|
||||
product_info->model_done = (void *)&hc08_done;
|
||||
|
||||
return product_info;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
config ADAPTER_HFA21_ETHERNET
|
||||
help
|
||||
Please check HFA21 can only work for adapter_wifi or adapter_ethernet in the meantime!
|
||||
bool "Using ethernet adapter device HFA21"
|
||||
default n
|
||||
|
||||
if ADAPTER_HFA21_ETHERNET
|
||||
source "$APP_DIR/Framework/connection/ethernet/hfa21_ethernet/Kconfig"
|
||||
endif
|
|
@ -1,3 +1,7 @@
|
|||
SRC_FILES := adapter_ethernet.c
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_HFA21_ETHERNET),y)
|
||||
SRC_DIR += hfa21_ethernet
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
@ -15,5 +15,112 @@
|
|||
* @brief Implement the connection ethernet adapter function
|
||||
* @version 1.1
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.06.25
|
||||
* @date 2021.10.15
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
|
||||
#ifdef ADAPTER_HFA21_ETHERNET
|
||||
extern AdapterProductInfoType Hfa21EthernetAttach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
#define ADAPTER_ETHERNET_NAME "ethernet"
|
||||
|
||||
static int AdapterEthernetRegister(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
strncpy(adapter->name, ADAPTER_ETHERNET_NAME, NAME_NUM_MAX);
|
||||
|
||||
adapter->net_protocol = IP_PROTOCOL;
|
||||
adapter->adapter_status = UNREGISTERED;
|
||||
|
||||
ret = AdapterDeviceRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("AdapterEthernet register error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int AdapterEthernetInit(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter));
|
||||
if (!adapter) {
|
||||
printf("AdapterEthernetInit malloc error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(adapter, 0, sizeof(struct Adapter));
|
||||
|
||||
ret = AdapterEthernetRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("AdapterEthernetInit register ethernet adapter error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef ADAPTER_HFA21_ETHERNET
|
||||
AdapterProductInfoType product_info = Hfa21EthernetAttach(adapter);
|
||||
if (!product_info) {
|
||||
printf("AdapterEthernetInit hfa21 attach error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
adapter->product_info_flag = 1;
|
||||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************ethernet TEST*********************/
|
||||
int AdapterEthernetTest(void)
|
||||
{
|
||||
int baud_rate = BAUD_RATE_57600;
|
||||
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_ETHERNET_NAME);
|
||||
|
||||
#ifdef ADAPTER_HFA21_ETHERNET
|
||||
|
||||
char ethernet_recv_msg[128] = {0};
|
||||
int i, len = 0;
|
||||
const char *ethernet_msg = "Adapter Ethernet Test";
|
||||
|
||||
AdapterDeviceOpen(adapter);
|
||||
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
|
||||
|
||||
AdapterDeviceSetUp(adapter);
|
||||
|
||||
const char *ip = "10.10.100.50";
|
||||
const char *port = "12345";
|
||||
enum NetRoleType net_role = SERVER;//CLIENT
|
||||
enum IpType ip_type = IPV4;
|
||||
AdapterDeviceConnect(adapter, net_role, ip, port, ip_type);
|
||||
|
||||
printf("ready to test data transfer\n");
|
||||
|
||||
len = strlen(ethernet_msg);
|
||||
for (i = 0;i < 10; i ++) {
|
||||
printf("AdapterEthernetTest send %s\n", ethernet_msg);
|
||||
AdapterDeviceSend(adapter, ethernet_msg, len);
|
||||
PrivTaskDelay(4000);
|
||||
}
|
||||
|
||||
while (1) {
|
||||
AdapterDeviceRecv(adapter, ethernet_recv_msg, 128);
|
||||
printf("AdapterEthernetTest recv %s\n", ethernet_recv_msg);
|
||||
memset(ethernet_recv_msg, 0, 128);
|
||||
}
|
||||
#endif
|
||||
|
||||
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, AdapterEthernetTest, AdapterEthernetTest, show adapter ethernet information);
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
config ADAPTER_ETHERNET_HFA21
|
||||
string "HFA21 ETHERNET adapter name"
|
||||
default "hfa21_ethernet"
|
||||
|
||||
if ADD_XIUOS_FETURES
|
||||
|
||||
config ADAPTER_HFA21_DRIVER_EXTUART
|
||||
bool "Using extra uart to support ethernet"
|
||||
default n
|
||||
|
||||
config ADAPTER_HFA21_DRIVER
|
||||
string "HFA21 device uart driver path"
|
||||
default "/dev/usart3_dev3"
|
||||
depends on !ADAPTER_HFA21_DRIVER_EXTUART
|
||||
|
||||
if ADAPTER_HFA21_DRIVER_EXTUART
|
||||
config ADAPTER_HFA21_DRIVER
|
||||
string "HFA21 device extra uart driver path"
|
||||
default "/dev/extuart_dev6"
|
||||
|
||||
config ADAPTER_HFA21_DRIVER_EXT_PORT
|
||||
int "if HFA21 device using extuart, choose port"
|
||||
default "6"
|
||||
endif
|
||||
endif
|
||||
|
||||
if ADD_NUTTX_FETURES
|
||||
|
||||
endif
|
||||
|
||||
if ADD_RTTHREAD_FETURES
|
||||
|
||||
endif
|
|
@ -0,0 +1,3 @@
|
|||
SRC_FILES := hfa21_ethernet.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
|
@ -0,0 +1,453 @@
|
|||
/*
|
||||
* 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 hfa21_ethernet.c
|
||||
* @brief Implement the connection ethernet adapter function, using HFA21 device
|
||||
* @version 1.1
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.10.15
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
#include <at_agent.h>
|
||||
|
||||
#define HFA21_ETHERNET_AT_CMD "+++"
|
||||
#define HFA21_ETHERNET_AT_CONFIRM_CMD "a"
|
||||
#define HFA21_ETHERNET_AT_FCLR_CMD "AT+FCLR\r"
|
||||
#define HFA21_ETHERNET_AT_FEPHY_CMD "AT+FEPHY=on\r"
|
||||
#define HFA21_ETHERNET_AT_ENABLE_WANN_CMD "AT+FVEW=enable\r"
|
||||
#define HFA21_ETHERNET_AT_DISABLE_WANN_CMD "AT+FVEW=disable\r"
|
||||
#define HFA21_ETHERNET_AT_RELD_CMD "AT+RELD\r"
|
||||
#define HFA21_ETHERNET_AT_WMODE_CMD "AT+WMODE\r"
|
||||
#define HFA21_ETHERNET_AT_WANN_CMD "AT+WANN\r"
|
||||
#define HFA21_ETHERNET_AT_SET_WANN_CMD "AT+WANN=%s,%s,%s,%s\r"
|
||||
#define HFA21_ETHERNET_AT_PING_CMD "AT+PING=%s\r"
|
||||
#define HFA21_ETHERNET_AT_NETP_CMD "AT+NETP=%s,%s,%s,%s\r"
|
||||
#define HFA21_ETHERNET_AT_REBOOT_CMD "AT+Z\r"
|
||||
|
||||
#define HFA21_ETHERNET_OK_REPLY "+ok"
|
||||
|
||||
static int Hfa21EthernetSetDown(struct Adapter *adapter_at);
|
||||
|
||||
/**
|
||||
* @description: enter AT command mode
|
||||
* @param at_agent - ethernet device agent pointer
|
||||
* @return success: 0
|
||||
*/
|
||||
static int Hfa21EthernetInitAtCmd(ATAgentType at_agent)
|
||||
{
|
||||
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, HFA21_ETHERNET_AT_CMD);
|
||||
PrivTaskDelay(100);
|
||||
|
||||
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, HFA21_ETHERNET_AT_CONFIRM_CMD);
|
||||
PrivTaskDelay(500);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Open HFA21 uart function
|
||||
* @param adapter - ethernet device pointer
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int Hfa21EthernetOpen(struct Adapter *adapter)
|
||||
{
|
||||
/*step1: open hfa21 serial port*/
|
||||
adapter->fd = PrivOpen(ADAPTER_HFA21_DRIVER, O_RDWR);
|
||||
if (adapter->fd < 0) {
|
||||
printf("Hfa21EthernetOpen get serial %s fd error\n", ADAPTER_HFA21_DRIVER);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*step2: init AT agent*/
|
||||
if (!adapter->agent) {
|
||||
char *agent_name = "ethernet_uart_client";
|
||||
if (EOK != InitATAgent(agent_name, adapter->fd, 512)) {
|
||||
printf("at agent init failed !\n");
|
||||
return -1;
|
||||
}
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
|
||||
adapter->agent = at_agent;
|
||||
}
|
||||
|
||||
ADAPTER_DEBUG("Hfa21Ethernet open done\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Close HFA21 ethernet and uart function
|
||||
* @param adapter - ethernet device pointer
|
||||
* @return success: 0
|
||||
*/
|
||||
static int Hfa21EthernetClose(struct Adapter *adapter)
|
||||
{
|
||||
return Hfa21EthernetSetDown(adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: send data to adapter
|
||||
* @param adapter - ethernet device pointer
|
||||
* @param data - data buffer
|
||||
* @param data - data length
|
||||
* @return success: 0
|
||||
*/
|
||||
static int Hfa21EthernetSend(struct Adapter *adapter, const void *data, size_t len)
|
||||
{
|
||||
int result = 0;
|
||||
if (adapter->agent) {
|
||||
EntmSend(adapter->agent, (const char *)data, len);
|
||||
}else {
|
||||
printf("Hfa21EthernetSend can not find agent!\n");
|
||||
}
|
||||
|
||||
__exit:
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: receive data from adapter
|
||||
* @param adapter - ethernet device pointer
|
||||
* @param data - data buffer
|
||||
* @param data - data length
|
||||
* @return success: 0
|
||||
*/
|
||||
static int Hfa21EthernetReceive(struct Adapter *adapter, void *rev_buffer, size_t buffer_len)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if (adapter->agent) {
|
||||
return EntmRecv(adapter->agent, (char *)rev_buffer, buffer_len, 40000);
|
||||
} else {
|
||||
printf("Hfa21EthernetReceive can not find agent!\n");
|
||||
}
|
||||
|
||||
__exit:
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: connnect Ethernet to internet
|
||||
* @param adapter - Ethernet device pointer
|
||||
* @return success: 0
|
||||
*/
|
||||
static int Hfa21EthernetSetUp(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
AtSetReplyLrEnd(adapter->agent, 1);
|
||||
|
||||
/* wait hfa21 device startup finish */
|
||||
PrivTaskDelay(5000);
|
||||
|
||||
/*Step1 : enter AT mode*/
|
||||
Hfa21EthernetInitAtCmd(adapter->agent);
|
||||
|
||||
PrivTaskDelay(1000);
|
||||
|
||||
/*Step2 : FCLR reboot*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_FCLR_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
|
||||
Hfa21EthernetInitAtCmd(adapter->agent);
|
||||
|
||||
/*Step3 : FEPHY enable phy function*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_FEPHY_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/*Step4 : FVEW disable WANN function, ethernet work at LANN mode*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_DISABLE_WANN_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/*Step5 : RELD enable F-AT cmd*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_RELD_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
|
||||
Hfa21EthernetInitAtCmd(adapter->agent);
|
||||
|
||||
/*Step6 : AT+WMODE check work mode, AP or STA*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_WMODE_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/*Step7 : AT+WANN check if get ip、netmask、gateway*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_WANN_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/*Step8 : AT+Z reboot hfa21 device*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_REBOOT_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
|
||||
return ret;
|
||||
|
||||
__exit:
|
||||
Hfa21EthernetSetDown(adapter);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: disconnnect ethernet from internet
|
||||
* @param adapter - ethernet device pointer
|
||||
* @return success: 0
|
||||
*/
|
||||
static int Hfa21EthernetSetDown(struct Adapter *adapter)
|
||||
{
|
||||
Hfa21EthernetInitAtCmd(adapter->agent);
|
||||
|
||||
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, HFA21_ETHERNET_AT_FCLR_CMD);
|
||||
PrivTaskDelay(20000);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: set ethernet ip/gateway/netmask address(in sta mode) working at WANN mode
|
||||
* @param adapter - ethernet device pointer
|
||||
* @param ip - ip address
|
||||
* @param gateway - gateway address
|
||||
* @param netmask - netmask address
|
||||
* @return success: 0, failure: -ENOMEMORY or -1
|
||||
*/
|
||||
static int Hfa21EthernetSetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t hfa21_ethernet_cmd[64];
|
||||
|
||||
/*Step1 : enter AT mode*/
|
||||
Hfa21EthernetInitAtCmd(adapter->agent);
|
||||
|
||||
/*Step2 : set mode、ip、netmask and gateway*/
|
||||
memset(hfa21_ethernet_cmd, 0, sizeof(hfa21_ethernet_cmd));
|
||||
sprintf(hfa21_ethernet_cmd, HFA21_ETHERNET_AT_SET_WANN_CMD, "DHCP", ip, netmask, gateway);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, hfa21_ethernet_cmd, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/*Step3 : AT+WANN check if set ip、netmask、gateway successfully*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_WANN_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/*Step4 : AT+Z reboot hfa21 device*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_REBOOT_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
|
||||
return ret;
|
||||
|
||||
__exit:
|
||||
Hfa21EthernetSetDown(adapter);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: ethernet ping function
|
||||
* @param adapter - ethernet device pointer
|
||||
* @param destination - domain name or ip address
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int Hfa21EthernetPing(struct Adapter *adapter, const char *destination)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
char *ping_result = (char *) PrivCalloc(1, 17);
|
||||
char *dst = (char *) PrivCalloc(1, 17);
|
||||
|
||||
strcpy(dst, destination);
|
||||
strcat(dst, "\r");
|
||||
|
||||
printf("Hfa21EthernetPing [%s]\n", dst);
|
||||
|
||||
/*Step1 : enter AT mode*/
|
||||
Hfa21EthernetInitAtCmd(adapter->agent);
|
||||
|
||||
/*Step2 : ping dst ip address*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_PING_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/*Step3 : AT+Z reboot hfa21 device*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_REBOOT_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
__exit:
|
||||
Hfa21EthernetSetDown(adapter);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: ethernet connect function
|
||||
* @param adapter - ethernet device pointer
|
||||
* @param net_role - net role, CLIENT or SERVER
|
||||
* @param ip - ip address
|
||||
* @param port - port num
|
||||
* @param ip_type - ip type, IPV4 or IPV6
|
||||
* @return success: 0, failure: -1
|
||||
*/
|
||||
static int Hfa21EthernetConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
|
||||
{
|
||||
int ret = 0;
|
||||
char hfa21_ethernet_cmd[128];
|
||||
char net_role_string[6] = {0};
|
||||
|
||||
/*Step1 : enter AT mode*/
|
||||
Hfa21EthernetInitAtCmd(adapter->agent);
|
||||
|
||||
if (CLIENT == net_role) {
|
||||
strcpy(net_role_string, "CLIENT");
|
||||
} else if (SERVER == net_role) {
|
||||
strcpy(net_role_string, "SERVER");
|
||||
} else {
|
||||
printf("Hfa21EthernetConnect do not support %d net type\n", net_role);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (IPV4 == ip_type) {
|
||||
/*to do*/
|
||||
} else if (IPV6 == ip_type) {
|
||||
/*to do*/
|
||||
} else {
|
||||
printf("Hfa21EthernetConnect do not support %d ip type\n", ip_type);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*Step2 : create tcp connect*/
|
||||
memset(hfa21_ethernet_cmd, 0, sizeof(hfa21_ethernet_cmd));
|
||||
sprintf(hfa21_ethernet_cmd, HFA21_ETHERNET_AT_NETP_CMD, "TCP", net_role_string, port, ip);
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, hfa21_ethernet_cmd, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
adapter->net_role = net_role;
|
||||
|
||||
/*Step3 : AT+Z reboot hfa21 device*/
|
||||
ret = AtCmdConfigAndCheck(adapter->agent, HFA21_ETHERNET_AT_REBOOT_CMD, HFA21_ETHERNET_OK_REPLY);
|
||||
if (ret < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
PrivTaskDelay(10000);
|
||||
|
||||
return ret;
|
||||
|
||||
__exit:
|
||||
Hfa21EthernetSetDown(adapter);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int Hfa21EthernetIoctl(struct Adapter *adapter, int cmd, void *args)
|
||||
{
|
||||
if (OPE_INT != cmd) {
|
||||
printf("Hfa21EthernetIoctl only support OPE_INT, do not support %d\n", cmd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t baud_rate = *((uint32_t *)args);
|
||||
|
||||
struct SerialDataCfg serial_cfg;
|
||||
memset(&serial_cfg, 0 ,sizeof(struct SerialDataCfg));
|
||||
serial_cfg.serial_baud_rate = baud_rate;
|
||||
serial_cfg.serial_data_bits = DATA_BITS_8;
|
||||
serial_cfg.serial_stop_bits = STOP_BITS_1;
|
||||
serial_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
|
||||
serial_cfg.serial_parity_mode = PARITY_NONE;
|
||||
serial_cfg.serial_bit_order = BIT_ORDER_LSB;
|
||||
serial_cfg.serial_invert_mode = NRZ_NORMAL;
|
||||
#ifdef ADAPTER_HFA21_DRIVER_EXT_PORT
|
||||
serial_cfg.ext_uart_no = ADAPTER_HFA21_DRIVER_EXT_PORT;
|
||||
serial_cfg.port_configure = PORT_CFG_INIT;
|
||||
#endif
|
||||
|
||||
struct PrivIoctlCfg ioctl_cfg;
|
||||
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
|
||||
ioctl_cfg.args = &serial_cfg;
|
||||
PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
|
||||
|
||||
printf("Hfa21EthernetIoctl success\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const struct IpProtocolDone hfa21_ethernet_done =
|
||||
{
|
||||
.open = Hfa21EthernetOpen,
|
||||
.close = Hfa21EthernetClose,
|
||||
.ioctl = Hfa21EthernetIoctl,
|
||||
.setup = Hfa21EthernetSetUp,
|
||||
.setdown = Hfa21EthernetSetDown,
|
||||
.setaddr = Hfa21EthernetSetAddr,
|
||||
.setdns = NULL,
|
||||
.setdhcp = NULL,
|
||||
.ping = Hfa21EthernetPing,
|
||||
.netstat = NULL,
|
||||
.connect = Hfa21EthernetConnect,
|
||||
.send = Hfa21EthernetSend,
|
||||
.recv = Hfa21EthernetReceive,
|
||||
.disconnect = NULL,
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: Register ethernet device hfa21
|
||||
* @return success: product_info, failure: NULL
|
||||
*/
|
||||
AdapterProductInfoType Hfa21EthernetAttach(struct Adapter *adapter)
|
||||
{
|
||||
struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo));
|
||||
if (!product_info) {
|
||||
printf("Hfa21EthernetAttach Attach malloc product_info error\n");
|
||||
PrivFree(product_info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(product_info->model_name, ADAPTER_ETHERNET_HFA21);
|
||||
|
||||
product_info->model_done = (void *)&hfa21_ethernet_done;
|
||||
|
||||
return product_info;
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
config ADAPTER_SX1278
|
||||
bool "Using lora adapter device SX1278"
|
||||
default y
|
||||
|
||||
choice
|
||||
prompt "Lora device adapter select net role type "
|
||||
default AS_LORA_CLIENT_ROLE
|
||||
|
||||
config AS_LORA_GATEWAY_ROLE
|
||||
bool "config as a gateway"
|
||||
|
||||
config AS_LORA_CLIENT_ROLE
|
||||
bool "config as a client"
|
||||
endchoice
|
||||
|
||||
if AS_LORA_GATEWAY_ROLE
|
||||
config ADAPTER_LORA_NET_ROLE_ID
|
||||
hex "if Lora device config as a gateway, set gateway net id"
|
||||
default "0x10"
|
||||
endif
|
||||
|
||||
if AS_LORA_CLIENT_ROLE
|
||||
config ADAPTER_LORA_NET_ROLE_ID
|
||||
hex "if Lora device config as a client, set client net id"
|
||||
default "0x01"
|
||||
endif
|
||||
|
||||
if ADAPTER_SX1278
|
||||
source "$APP_DIR/Framework/connection/lora/sx1278/Kconfig"
|
||||
endif
|
|
@ -1,3 +1,7 @@
|
|||
SRC_FILES := adapter_lora.c
|
||||
|
||||
ifeq ($(CONFIG_ADAPTER_SX1278),y)
|
||||
SRC_DIR += sx1278
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
|
|
|
@ -15,5 +15,767 @@
|
|||
* @brief Implement the connection lora adapter function
|
||||
* @version 1.1
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.06.25
|
||||
* @date 2021.10.20
|
||||
*/
|
||||
|
||||
#include <adapter.h>
|
||||
|
||||
#ifdef ADAPTER_SX1278
|
||||
extern AdapterProductInfoType Sx1278Attach(struct Adapter *adapter);
|
||||
#endif
|
||||
|
||||
#define ADAPTER_LORA_NAME "lora"
|
||||
#define ADAPTER_LORA_CLIENT_NUM 6
|
||||
#define ADAPTER_LORA_DATA_LENGTH 128
|
||||
|
||||
#define ADAPTER_LORA_DATA_HEAD 0x3C
|
||||
#define ADAPTER_LORA_NET_PANID 0x0102
|
||||
#define ADAPTER_LORA_DATA_TYPE_JOIN 0x0A
|
||||
#define ADAPTER_LORA_DATA_TYPE_QUIT 0x0B
|
||||
#define ADAPTER_LORA_DATA_TYPE_JOIN_REPLY 0x0C
|
||||
#define ADAPTER_LORA_DATA_TYPE_QUIT_REPLY 0x0D
|
||||
#define ADAPTER_LORA_DATA_TYPE_USERDATA 0x0E
|
||||
#define ADAPTER_LORA_DATA_TYPE_CMD 0x0F
|
||||
|
||||
//need to change status if the lora client wants to quit the net when timeout or a certain event
|
||||
//eg.can also use sem to trigger quit function
|
||||
static int g_adapter_lora_quit_flag = 0;
|
||||
|
||||
enum ClientState
|
||||
{
|
||||
CLIENT_DISCONNECT = 0,
|
||||
CLIENT_CONNECT,
|
||||
CLIENT_DEFAULT,
|
||||
};
|
||||
|
||||
enum DataType
|
||||
{
|
||||
LORA_CLIENT_ACCESS = 0,
|
||||
LORA_GATEWAY_REPLY,
|
||||
LORA_USER_DATA,
|
||||
};
|
||||
|
||||
enum LoraGatewayState {
|
||||
LORA_STATE_IDLE = 0,
|
||||
LORA_JOIN_NET,
|
||||
LORA_QUIT_NET,
|
||||
LORA_RECV_DATA,
|
||||
};
|
||||
|
||||
static uint8 LoraGatewayState = LORA_STATE_IDLE;
|
||||
|
||||
struct LoraGatewayParam
|
||||
{
|
||||
uint8 gateway_id;
|
||||
uint16 panid;
|
||||
uint8 client_id[ADAPTER_LORA_CLIENT_NUM];
|
||||
int client_num;
|
||||
int receive_data_sem;
|
||||
};
|
||||
|
||||
struct LoraClientParam
|
||||
{
|
||||
uint8 client_id;
|
||||
uint16 panid;
|
||||
enum ClientState client_state;
|
||||
|
||||
uint8 gateway_id;
|
||||
};
|
||||
|
||||
/*LoraDataFormat:
|
||||
**flame_head : 0x3C
|
||||
**length : sizeof(struct LoraDataFormat)
|
||||
**panid : 0x0102
|
||||
**client_id : 0x01、0x02、0x03...
|
||||
**gateway_id : 0x11、0x12、0x13...
|
||||
**data_type : 0x0A(join net, client->gateway)、0x0B(join net reply, gateway->client)、
|
||||
0x0C(user data, client->gateway)、0x0D(user data cmd, gateway->client)
|
||||
**data : user data
|
||||
**crc16 : CRC 16 check data
|
||||
*/
|
||||
struct LoraDataFormat
|
||||
{
|
||||
uint8 flame_head;
|
||||
uint32 length;
|
||||
uint16 panid;
|
||||
uint8 client_id;
|
||||
uint8 gateway_id;
|
||||
|
||||
uint8 data_type;
|
||||
uint8 data[ADAPTER_LORA_DATA_LENGTH];
|
||||
|
||||
uint16 crc16;
|
||||
};
|
||||
|
||||
/*******************LORA MESH FUNCTION***********************/
|
||||
|
||||
/**
|
||||
* @description: create CRC16 data
|
||||
* @param data input data buffer
|
||||
* @param length input data buffer minus check code
|
||||
*/
|
||||
static uint16 LoraCrc16(uint8 *data, uint16 length)
|
||||
{
|
||||
int j;
|
||||
uint16 crc_data = 0xFFFF;
|
||||
|
||||
while (length--) {
|
||||
crc_data ^= *data++;
|
||||
for( j = 0 ; j < 8 ; j ++) {
|
||||
if(crc_data & 0x01) /* LSB(b0)=1 */
|
||||
crc_data = crc_data >> 1 ^ 0xA001;
|
||||
else
|
||||
crc_data = crc_data >> 1;
|
||||
}
|
||||
}
|
||||
|
||||
return crc_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: CRC16 check
|
||||
* @param data input data buffer
|
||||
* @param length input data buffer minus check code
|
||||
*/
|
||||
static int LoraCrc16Check(uint8 *data, uint16 length)
|
||||
{
|
||||
uint16 crc_data;
|
||||
uint16 input_data = (((uint16)data[length - 1] << 8) & 0xFF00) | ((uint16)data[length - 2] & 0x00FF);
|
||||
|
||||
crc_data = LoraCrc16(data, length - 2);
|
||||
|
||||
if (crc_data == input_data)
|
||||
return 0;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Gateway reply connect request to Client
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param gateway_recv_data Lora Client connect data
|
||||
*/
|
||||
static int LoraGatewayReply(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data)
|
||||
{
|
||||
int i;
|
||||
int client_join_flag = 0;
|
||||
struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)adapter->adapter_param;
|
||||
struct LoraDataFormat gateway_reply_data;
|
||||
|
||||
memset(&gateway_reply_data, 0, sizeof(struct LoraDataFormat));
|
||||
|
||||
if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) {
|
||||
|
||||
for (i = 0; i < gateway->client_num; i ++) {
|
||||
if (gateway_recv_data->client_id == gateway->client_id[i]) {
|
||||
printf("Lora client_%d 0x%x has already join net 0x%x\n", i, gateway_recv_data->client_id, gateway->gateway_id);
|
||||
client_join_flag = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!client_join_flag) {
|
||||
if (gateway->client_num > 6) {
|
||||
printf("Lora gateway only support 6(max) client\n");
|
||||
gateway->client_num = 0;
|
||||
}
|
||||
gateway->client_id[gateway->client_num] = gateway_recv_data->client_id;
|
||||
gateway->client_num ++;
|
||||
}
|
||||
|
||||
gateway_reply_data.flame_head = ADAPTER_LORA_DATA_HEAD;
|
||||
gateway_reply_data.length = sizeof(struct LoraDataFormat);
|
||||
gateway_reply_data.panid = gateway->panid;
|
||||
gateway_reply_data.data_type = ADAPTER_LORA_DATA_TYPE_JOIN_REPLY;
|
||||
gateway_reply_data.client_id = gateway_recv_data->client_id;
|
||||
gateway_reply_data.gateway_id = gateway->gateway_id;
|
||||
gateway_reply_data.crc16 = LoraCrc16((uint8 *)&gateway_reply_data, sizeof(struct LoraDataFormat) - 2);
|
||||
|
||||
if (AdapterDeviceSend(adapter, (uint8 *)&gateway_reply_data, gateway_reply_data.length) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Lora gateway 0x%x accept client 0x%x <join request>\n", gateway->gateway_id, gateway_recv_data->client_id);
|
||||
} else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) {
|
||||
for (i = 0; i < gateway->client_num; i ++) {
|
||||
if (gateway->client_id[i] == gateway_recv_data->client_id) {
|
||||
gateway->client_id[i] = 0;
|
||||
gateway->client_num --;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
gateway_reply_data.flame_head = ADAPTER_LORA_DATA_HEAD;
|
||||
gateway_reply_data.length = sizeof(struct LoraDataFormat);
|
||||
gateway_reply_data.panid = gateway->panid;
|
||||
gateway_reply_data.data_type = ADAPTER_LORA_DATA_TYPE_QUIT_REPLY;
|
||||
gateway_reply_data.client_id = gateway_recv_data->client_id;
|
||||
gateway_reply_data.gateway_id = gateway->gateway_id;
|
||||
gateway_reply_data.crc16 = LoraCrc16((uint8 *)&gateway_reply_data, sizeof(struct LoraDataFormat) - 2);
|
||||
|
||||
if (AdapterDeviceSend(adapter, (uint8 *)&gateway_reply_data, gateway_reply_data.length) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Lora gateway 0x%x accept client 0x%x <quit request>\n", gateway->gateway_id, gateway_recv_data->client_id);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Gateway send cmd to Client
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param client_id Lora Client id
|
||||
* @param cmd Lora cmd
|
||||
*/
|
||||
static int LoraGatewaySendCmd(struct Adapter *adapter, unsigned char client_id, int cmd)
|
||||
{
|
||||
struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)adapter->adapter_param;
|
||||
struct LoraDataFormat gateway_cmd_data;
|
||||
|
||||
memset(&gateway_cmd_data, 0, sizeof(struct LoraDataFormat));
|
||||
|
||||
gateway_cmd_data.flame_head = ADAPTER_LORA_DATA_HEAD;
|
||||
gateway_cmd_data.length = sizeof(struct LoraDataFormat);
|
||||
gateway_cmd_data.panid = gateway->panid;
|
||||
gateway_cmd_data.data_type = cmd;
|
||||
gateway_cmd_data.client_id = client_id;
|
||||
gateway_cmd_data.gateway_id = gateway->gateway_id;
|
||||
gateway_cmd_data.crc16 = LoraCrc16((uint8 *)&gateway_cmd_data, sizeof(struct LoraDataFormat) - 2);
|
||||
|
||||
if (AdapterDeviceSend(adapter, (uint8 *)&gateway_cmd_data, gateway_cmd_data.length) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Gateway handle the user data from the client
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param gateway_recv_data Lora Client user data
|
||||
*/
|
||||
static int LoraGatewayHandleData(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data)
|
||||
{
|
||||
/*User needs to handle client data depends on the requirement*/
|
||||
printf("Lora Gateway receive Client %d data:\n", gateway_recv_data->client_id);
|
||||
printf("%s\n", gateway_recv_data->data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Client update status after join the net
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param client_recv_data Lora Client recv data from Lora Gateway
|
||||
*/
|
||||
static int LoraClientUpdate(struct Adapter *adapter, struct LoraDataFormat *client_recv_data)
|
||||
{
|
||||
struct LoraClientParam *client = (struct LoraClientParam *)adapter->adapter_param;
|
||||
|
||||
if (ADAPTER_LORA_DATA_TYPE_JOIN_REPLY == client_recv_data->data_type) {
|
||||
client->gateway_id = client_recv_data->gateway_id;
|
||||
client->panid = client_recv_data->panid;
|
||||
client->client_state = CLIENT_CONNECT;
|
||||
printf("LoraClientUpdate client join panid 0x%x successfully\n", client->panid);
|
||||
} else if (ADAPTER_LORA_DATA_TYPE_QUIT_REPLY == client_recv_data->data_type) {
|
||||
client->gateway_id = 0;
|
||||
client->panid = 0;
|
||||
client->client_state = CLIENT_DISCONNECT;
|
||||
printf("LoraClientUpdate client quit panid 0x%x successfully\n", client->panid);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Client send the user data to the gateway
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param send_buf Lora Client send user data buf
|
||||
* @param length user data length (max size is ADAPTER_LORA_DATA_LENGTH)
|
||||
*/
|
||||
static int LoraClientSendData(struct Adapter *adapter, void *send_buf, int length)
|
||||
{
|
||||
struct LoraClientParam *client = (struct LoraClientParam *)adapter->adapter_param;
|
||||
|
||||
if (client->client_state != CLIENT_CONNECT) {
|
||||
printf("Lora client %d do not connect to Lora gateway\n", client->client_id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (length > ADAPTER_LORA_DATA_LENGTH) {
|
||||
printf("Lora client %d send data %d larger than max %d \n", client->client_id, length, ADAPTER_LORA_DATA_LENGTH);
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct LoraDataFormat client_user_data;
|
||||
|
||||
memset(&client_user_data, 0, sizeof(struct LoraDataFormat));
|
||||
|
||||
client_user_data.flame_head = ADAPTER_LORA_DATA_HEAD;
|
||||
client_user_data.length = sizeof(struct LoraDataFormat);
|
||||
client_user_data.panid = client->panid;
|
||||
client_user_data.data_type = ADAPTER_LORA_DATA_TYPE_USERDATA;
|
||||
client_user_data.client_id = client->client_id;
|
||||
client_user_data.gateway_id = client->gateway_id;
|
||||
|
||||
memcpy(client_user_data.data, (uint8 *)send_buf, length);
|
||||
|
||||
client_user_data.crc16 = LoraCrc16((uint8 *)&client_user_data, sizeof(struct LoraDataFormat) - 2);
|
||||
|
||||
if (AdapterDeviceSend(adapter, (uint8 *)&client_user_data, client_user_data.length) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Gateway receive data analyzing
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param gateway_recv_data Lora gateway receive data pointer
|
||||
*/
|
||||
static int LoraGateWayDataAnalyze(struct Adapter *adapter, struct LoraDataFormat *gateway_recv_data)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (LoraCrc16Check((uint8 *)gateway_recv_data, sizeof(struct LoraDataFormat)) < 0) {
|
||||
printf("LoraGateWayDataAnalyze CRC check error\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((ADAPTER_LORA_DATA_HEAD == gateway_recv_data->flame_head) &&
|
||||
(ADAPTER_LORA_NET_PANID == gateway_recv_data->panid)) {
|
||||
switch (gateway_recv_data->data_type)
|
||||
{
|
||||
case ADAPTER_LORA_DATA_TYPE_JOIN :
|
||||
case ADAPTER_LORA_DATA_TYPE_QUIT :
|
||||
ret = LoraGatewayReply(adapter, gateway_recv_data);
|
||||
break;
|
||||
case ADAPTER_LORA_DATA_TYPE_USERDATA :
|
||||
ret = LoraGatewayHandleData(adapter, gateway_recv_data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Client receive data analyzing
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param send_buf Lora Client send user data buf
|
||||
* @param length user data length (max size is ADAPTER_LORA_DATA_LENGTH)
|
||||
*/
|
||||
static int LoraClientDataAnalyze(struct Adapter *adapter, void *send_buf, int length)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
struct LoraDataFormat *client_recv_data = PrivMalloc(sizeof(struct LoraDataFormat));
|
||||
|
||||
memset(client_recv_data, 0, sizeof(struct LoraDataFormat));
|
||||
|
||||
ret = AdapterDeviceRecv(adapter, client_recv_data, sizeof(struct LoraDataFormat));
|
||||
if (0 == ret) {
|
||||
printf("LoraClientDataAnalyze recv error.Just return\n");
|
||||
PrivFree(client_recv_data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("client_recv_data\n");
|
||||
printf("head 0x%x length %d panid 0x%x data_type 0x%x client_id 0x%x gateway_id 0x%x crc 0x%x\n",
|
||||
client_recv_data->flame_head, client_recv_data->length, client_recv_data->panid, client_recv_data->data_type,
|
||||
client_recv_data->client_id, client_recv_data->gateway_id, client_recv_data->crc16);
|
||||
|
||||
if ((ADAPTER_LORA_DATA_HEAD == client_recv_data->flame_head) &&
|
||||
(ADAPTER_LORA_NET_PANID == client_recv_data->panid)) {
|
||||
if (LoraCrc16Check((uint8 *)client_recv_data, sizeof(struct LoraDataFormat)) < 0) {
|
||||
printf("LoraClientDataAnalyze CRC check error\n");
|
||||
PrivFree(client_recv_data);
|
||||
return -1;
|
||||
}
|
||||
|
||||
//only handle this client_id information from gateway
|
||||
if (client_recv_data->client_id == adapter->net_role_id) {
|
||||
switch (client_recv_data->data_type)
|
||||
{
|
||||
case ADAPTER_LORA_DATA_TYPE_JOIN_REPLY :
|
||||
case ADAPTER_LORA_DATA_TYPE_QUIT_REPLY :
|
||||
ret = LoraClientUpdate(adapter, client_recv_data);
|
||||
break;
|
||||
case ADAPTER_LORA_DATA_TYPE_CMD :
|
||||
if (send_buf) {
|
||||
ret = LoraClientSendData(adapter, send_buf, length);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PrivFree(client_recv_data);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Client join net function
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param panid Lora net panid
|
||||
*/
|
||||
static int LoraClientJoinNet(struct Adapter *adapter, unsigned short panid)
|
||||
{
|
||||
struct LoraDataFormat client_join_data;
|
||||
|
||||
memset(&client_join_data, 0, sizeof(struct LoraDataFormat));
|
||||
|
||||
client_join_data.flame_head = ADAPTER_LORA_DATA_HEAD;
|
||||
client_join_data.length = sizeof(struct LoraDataFormat);
|
||||
client_join_data.panid = panid;
|
||||
client_join_data.data_type = ADAPTER_LORA_DATA_TYPE_JOIN;
|
||||
client_join_data.client_id = adapter->net_role_id;
|
||||
client_join_data.crc16 = LoraCrc16((uint8 *)&client_join_data, sizeof(struct LoraDataFormat) - 2);
|
||||
|
||||
if (AdapterDeviceJoin(adapter, (uint8 *)&client_join_data) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Client quit net function
|
||||
* @param adapter Lora adapter pointer
|
||||
* @param panid Lora net panid
|
||||
*/
|
||||
static int LoraClientQuitNet(struct Adapter *adapter, unsigned short panid)
|
||||
{
|
||||
struct LoraDataFormat client_join_data;
|
||||
|
||||
memset(&client_join_data, 0, sizeof(struct LoraDataFormat));
|
||||
|
||||
client_join_data.flame_head = ADAPTER_LORA_DATA_HEAD;
|
||||
client_join_data.length = sizeof(struct LoraDataFormat);
|
||||
client_join_data.panid = panid;
|
||||
client_join_data.data_type = ADAPTER_LORA_DATA_TYPE_QUIT;
|
||||
client_join_data.client_id = adapter->net_role_id;
|
||||
client_join_data.crc16 = LoraCrc16((uint8 *)&client_join_data, sizeof(struct LoraDataFormat) - 2);
|
||||
|
||||
if (AdapterDeviceDisconnect(adapter, (uint8 *)&client_join_data) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Gateway Process function
|
||||
* @param lora_adapter Lora adapter pointer
|
||||
* @param gateway Lora gateway pointer
|
||||
* @param gateway_recv_data Lora gateway receive data pointer
|
||||
*/
|
||||
int LoraGatewayProcess(struct Adapter *lora_adapter, struct LoraGatewayParam *gateway, struct LoraDataFormat *gateway_recv_data)
|
||||
{
|
||||
int i, ret = 0;
|
||||
switch (LoraGatewayState)
|
||||
{
|
||||
case LORA_STATE_IDLE:
|
||||
ret = AdapterDeviceRecv(lora_adapter, gateway_recv_data, sizeof(struct LoraDataFormat));
|
||||
if (0 == ret) {
|
||||
printf("LoraGatewayProcess IDLE recv error.Just return\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) {
|
||||
LoraGatewayState = LORA_JOIN_NET;
|
||||
} else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) {
|
||||
LoraGatewayState = LORA_QUIT_NET;
|
||||
} else {
|
||||
LoraGatewayState = LORA_STATE_IDLE;
|
||||
}
|
||||
break;
|
||||
case LORA_JOIN_NET:
|
||||
case LORA_QUIT_NET:
|
||||
ret = LoraGateWayDataAnalyze(lora_adapter, gateway_recv_data);
|
||||
if (ret < 0) {
|
||||
printf("LoraGateWayDataAnalyze state %d error, re-send data cmd to client\n", LoraGatewayState);
|
||||
PrivTaskDelay(500);
|
||||
}
|
||||
LoraGatewayState = LORA_RECV_DATA;
|
||||
break;
|
||||
case LORA_RECV_DATA:
|
||||
for (i = 0; i < gateway->client_num; i ++) {
|
||||
if (gateway->client_id[i]) {
|
||||
printf("LoraGatewayProcess send to client %d for data\n", gateway->client_id[i]);
|
||||
ret = LoraGatewaySendCmd(lora_adapter, gateway->client_id[i], ADAPTER_LORA_DATA_TYPE_CMD);
|
||||
if (ret < 0) {
|
||||
printf("LoraGatewaySendCmd client ID %d error\n", gateway->client_id[i]);
|
||||
PrivTaskDelay(500);
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = AdapterDeviceRecv(lora_adapter, gateway_recv_data, sizeof(struct LoraDataFormat));
|
||||
if (0 == ret) {
|
||||
printf("LoraGatewayProcess recv error.Just return\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ADAPTER_LORA_DATA_TYPE_JOIN == gateway_recv_data->data_type) {
|
||||
LoraGatewayState = LORA_JOIN_NET;
|
||||
} else if (ADAPTER_LORA_DATA_TYPE_QUIT == gateway_recv_data->data_type) {
|
||||
LoraGatewayState = LORA_QUIT_NET;
|
||||
} else {
|
||||
ret = LoraGateWayDataAnalyze(lora_adapter, gateway_recv_data);
|
||||
if (ret < 0) {
|
||||
printf("LoraGateWayDataAnalyze error, re-send data cmd to client\n");
|
||||
PrivTaskDelay(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Gateway task
|
||||
* @param parameter - Lora adapter pointer
|
||||
*/
|
||||
static void *LoraGatewayTask(void *parameter)
|
||||
{
|
||||
int i;
|
||||
int ret = 0;
|
||||
struct Adapter *lora_adapter = (struct Adapter *)parameter;
|
||||
struct LoraGatewayParam *gateway = (struct LoraGatewayParam *)lora_adapter->adapter_param;
|
||||
struct LoraDataFormat gateway_recv_data;
|
||||
|
||||
while (1) {
|
||||
LoraGatewayProcess(lora_adapter, gateway, &gateway_recv_data);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Client data upload task
|
||||
* @param parameter - Lora adapter pointer
|
||||
*/
|
||||
static void *LoraClientDataTask(void *parameter)
|
||||
{
|
||||
int i, ret = 0;
|
||||
int join_times = 10;
|
||||
struct Adapter *lora_adapter = (struct Adapter *)parameter;
|
||||
struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param;
|
||||
|
||||
//set lora_send_buf for test
|
||||
uint8 lora_send_buf[ADAPTER_LORA_DATA_LENGTH];
|
||||
memset(lora_send_buf, 0, ADAPTER_LORA_DATA_LENGTH);
|
||||
sprintf(lora_send_buf, "Lora client %d adapter test\n", client->client_id);
|
||||
|
||||
while (1) {
|
||||
|
||||
PrivTaskDelay(100);
|
||||
|
||||
if ((CLIENT_DISCONNECT == client->client_state) && (!g_adapter_lora_quit_flag)) {
|
||||
ret = LoraClientJoinNet(lora_adapter, client->panid);
|
||||
if (ret < 0) {
|
||||
printf("LoraClientJoinNet error panid 0x%x\n", client->panid);
|
||||
}
|
||||
|
||||
ret = LoraClientDataAnalyze(lora_adapter, NULL, 0);
|
||||
if (ret < 0) {
|
||||
printf("LoraClientDataAnalyze error, reconnect to gateway\n");
|
||||
PrivTaskDelay(500);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (CLIENT_CONNECT == client->client_state) {
|
||||
ret = LoraClientDataAnalyze(lora_adapter, (void *)lora_send_buf, strlen(lora_send_buf));
|
||||
if (ret < 0) {
|
||||
printf("LoraClientDataAnalyze error, wait for next data cmd\n");
|
||||
PrivTaskDelay(500);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Lora Client quit task
|
||||
* @param parameter - Lora adapter pointer
|
||||
*/
|
||||
static void *LoraClientQuitTask(void *parameter)
|
||||
{
|
||||
int ret = 0;
|
||||
struct Adapter *lora_adapter = (struct Adapter *)parameter;
|
||||
struct LoraClientParam *client = (struct LoraClientParam *)lora_adapter->adapter_param;
|
||||
|
||||
while (1) {
|
||||
PrivTaskDelay(100);
|
||||
|
||||
if ((CLIENT_CONNECT == client->client_state) && (g_adapter_lora_quit_flag)) {
|
||||
ret = LoraClientQuitNet(lora_adapter, client->panid);
|
||||
if (ret < 0) {
|
||||
printf("LoraClientQuitNet error panid 0x%x, re-quit net\n", client->panid);
|
||||
continue;
|
||||
}
|
||||
|
||||
ret = LoraClientDataAnalyze(lora_adapter, NULL, 0);
|
||||
if (ret < 0) {
|
||||
printf("LoraClientQuitTask LoraClientDataAnalyze error\n");
|
||||
PrivTaskDelay(500);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*******************LORA ADAPTER FUNCTION********************/
|
||||
|
||||
static int AdapterLoraRegister(struct Adapter *adapter)
|
||||
{
|
||||
int ret = 0;
|
||||
struct LoraGatewayParam *lora_gateway;
|
||||
struct LoraClientParam *lora_client;
|
||||
|
||||
strncpy(adapter->name, ADAPTER_LORA_NAME, NAME_NUM_MAX);
|
||||
|
||||
adapter->net_protocol = PRIVATE_PROTOCOL;
|
||||
|
||||
#ifdef AS_LORA_GATEWAY_ROLE
|
||||
lora_gateway = PrivMalloc(sizeof(struct LoraGatewayParam));
|
||||
if (!lora_gateway) {
|
||||
PrivFree(lora_gateway);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(lora_gateway, 0, sizeof(struct LoraGatewayParam));
|
||||
|
||||
lora_gateway->gateway_id = ADAPTER_LORA_NET_ROLE_ID;
|
||||
lora_gateway->panid = ADAPTER_LORA_NET_PANID;
|
||||
|
||||
adapter->net_role = GATEWAY;
|
||||
adapter->adapter_param = (void *)lora_gateway;
|
||||
#else //AS_LORA_CLIENT_ROLE
|
||||
lora_client = PrivMalloc(sizeof(struct LoraClientParam));
|
||||
if (!lora_client) {
|
||||
PrivFree(lora_client);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(lora_client, 0, sizeof(struct LoraClientParam));
|
||||
|
||||
lora_client->client_id = ADAPTER_LORA_NET_ROLE_ID;
|
||||
lora_client->client_state = CLIENT_DISCONNECT;
|
||||
lora_client->panid = ADAPTER_LORA_NET_PANID;
|
||||
|
||||
adapter->net_role = CLIENT;
|
||||
adapter->adapter_param = (void *)lora_client;
|
||||
#endif
|
||||
|
||||
adapter->net_role_id = ADAPTER_LORA_NET_ROLE_ID;
|
||||
|
||||
adapter->adapter_status = UNREGISTERED;
|
||||
|
||||
ret = AdapterDeviceRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("Adapter4G register error\n");
|
||||
if (lora_gateway)
|
||||
PrivFree(lora_gateway);
|
||||
if (lora_client)
|
||||
PrivFree(lora_client);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int AdapterLoraInit(void)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter));
|
||||
if (!adapter) {
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(adapter, 0, sizeof(struct Adapter));
|
||||
|
||||
ret = AdapterLoraRegister(adapter);
|
||||
if (ret < 0) {
|
||||
printf("AdapterLoraInit register lora adapter error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
#ifdef ADAPTER_SX1278
|
||||
AdapterProductInfoType product_info = Sx1278Attach(adapter);
|
||||
if (!product_info) {
|
||||
printf("AdapterLoraInit sx1278 attach error\n");
|
||||
PrivFree(adapter);
|
||||
return -1;
|
||||
}
|
||||
|
||||
adapter->product_info_flag = 1;
|
||||
adapter->info = product_info;
|
||||
adapter->done = product_info->model_done;
|
||||
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/******************Lora TEST*********************/
|
||||
#ifdef AS_LORA_GATEWAY_ROLE
|
||||
static pthread_t lora_gateway_task;
|
||||
#else //AS_LORA_CLIENT_ROLE
|
||||
static pthread_t lora_client_data_task;
|
||||
static pthread_t lora_client_quit_task;
|
||||
#endif
|
||||
|
||||
int AdapterLoraTest(void)
|
||||
{
|
||||
struct Adapter *adapter = AdapterDeviceFindByName(ADAPTER_LORA_NAME);
|
||||
|
||||
AdapterDeviceOpen(adapter);
|
||||
|
||||
//create lora gateway task
|
||||
#ifdef AS_LORA_GATEWAY_ROLE
|
||||
pthread_attr_t lora_gateway_attr;
|
||||
lora_gateway_attr.schedparam.sched_priority = 20;
|
||||
lora_gateway_attr.stacksize = 2048;
|
||||
|
||||
PrivTaskCreate(&lora_gateway_task, &lora_gateway_attr, &LoraGatewayTask, (void *)adapter);
|
||||
PrivTaskStartup(&lora_gateway_task);
|
||||
|
||||
#else //AS_LORA_CLIENT_ROLE
|
||||
//create lora client task
|
||||
pthread_attr_t lora_client_attr;
|
||||
lora_client_attr.schedparam.sched_priority = 20;
|
||||
lora_client_attr.stacksize = 2048;
|
||||
|
||||
PrivTaskCreate(&lora_client_data_task, &lora_client_attr, &LoraClientDataTask, (void *)adapter);
|
||||
PrivTaskStartup(&lora_client_data_task);
|
||||
|
||||
lora_client_attr.stacksize = 1024;
|
||||
|
||||
PrivTaskCreate(&lora_client_quit_task, &lora_client_attr, &LoraClientQuitTask, (void *)adapter);
|
||||
PrivTaskStartup(&lora_client_quit_task);
|
||||
#endif
|
||||
|
||||
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, AdapterLoraTest, AdapterLoraTest, show adapter lora information);
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
config ADAPTER_LORA_SX1278
|
||||
string "SX1278 adapter name"
|
||||
default "sx1278"
|
||||
|
||||
if ADD_XIUOS_FETURES
|
||||
config ADAPTER_SX1278_DRIVER
|
||||
string "SX1278 device spi driver path"
|
||||
default "/dev/spi2_lora"
|
||||
endif
|
||||
|
||||
if ADD_NUTTX_FETURES
|
||||
|
||||
endif
|
||||
|
||||
if ADD_RTTHREAD_FETURES
|
||||
|
||||
endif
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue