forked from xuos/xiuos
First commit XiUOS
This commit is contained in:
21
framework/connection/Adapter/Kconfig
Normal file
21
framework/connection/Adapter/Kconfig
Normal file
@@ -0,0 +1,21 @@
|
||||
menuconfig CONNECTION_COMMUNICATION_WIFI
|
||||
bool "Enable Wifi"
|
||||
default n
|
||||
|
||||
|
||||
menuconfig CONNECTION_COMMUNICATION_ETHERNET
|
||||
bool "Enable Ethernet"
|
||||
default n
|
||||
|
||||
menuconfig CONNECTION_COMMUNICATION_ZIGBEE
|
||||
bool "Enable Zigbee"
|
||||
default n
|
||||
|
||||
|
||||
menuconfig CONNECTION_COMMUNICATION_NB_IOT
|
||||
bool "Enable NB-IOT"
|
||||
default n
|
||||
|
||||
menuconfig CONNECTION_COMMUNICATION_4G
|
||||
bool "Enable 4G"
|
||||
default n
|
||||
35
framework/connection/Adapter/Makefile
Normal file
35
framework/connection/Adapter/Makefile
Normal file
@@ -0,0 +1,35 @@
|
||||
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_LORA), y)
|
||||
# SRC_DIR += lora
|
||||
# endif
|
||||
|
||||
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_ETHERNET), y)
|
||||
SRC_DIR += ethernet
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_WIFI), y)
|
||||
SRC_DIR += wifi
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_CONNECTION_COMMUNICATION_ZIGBEE), y)
|
||||
SRC_DIR += zigbee
|
||||
endif
|
||||
|
||||
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_NB_IOT), y)
|
||||
# SRC_DIR += nbiot
|
||||
# endif
|
||||
|
||||
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_4G), y)
|
||||
# SRC_DIR += 4G
|
||||
# endif
|
||||
|
||||
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_BLUETOOTH), y)
|
||||
# SRC_DIR += bluetooth
|
||||
# endif
|
||||
|
||||
# ifeq ($(CONFIG_CONNECTION_COMMUNICATION_RS485), y)
|
||||
# SRC_DIR += rs485
|
||||
# endif
|
||||
|
||||
SRC_DIR += src
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
5
framework/connection/Adapter/ethernet/Makefile
Normal file
5
framework/connection/Adapter/ethernet/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
SRC_FILES += xs_adapterAT_ethernet.c \
|
||||
xs_adapterAT_ethernet_register.c
|
||||
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
409
framework/connection/Adapter/ethernet/xs_adapterAT_ethernet.c
Normal file
409
framework/connection/Adapter/ethernet/xs_adapterAT_ethernet.c
Normal file
@@ -0,0 +1,409 @@
|
||||
/*
|
||||
* 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 xs_AdapterAT_ethernet.c
|
||||
* @brief Structure and function declarations of the connection ethernet
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
#include <xs_adapter_at_ethernet.h>
|
||||
#include <xs_adapter_manager.h>
|
||||
#include <xs_adapter_at_agent.h>
|
||||
#include <xs_adapter_def.h>
|
||||
#include <user_api.h>
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* @description: Close ethernet
|
||||
* @param padapter - ethernet device pointer
|
||||
*/
|
||||
void EthernetClose(struct Adapter *padapter)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: open ethernet
|
||||
* @param padapter - ethernet device pointer
|
||||
*/
|
||||
int EthernetOpen(struct Adapter *padapter)
|
||||
{
|
||||
char *agent_name = "uart3_client";
|
||||
const char *device_name = ETHERNET_UART_NAME;
|
||||
|
||||
uint32 result;
|
||||
if (InitATAgent(agent_name, device_name, 512, NULL)){
|
||||
printf("InitATAgent failed ! \n");
|
||||
result = -ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
if (NULL == at_agent){
|
||||
printf("GetATAgent failed ! \n");
|
||||
return -ERROR;
|
||||
}
|
||||
UserTaskDelay(5000);
|
||||
struct AdapterAT *ethernetAT_adapter = (struct AdapterAT *)padapter;
|
||||
ethernetAT_adapter->agent = at_agent;
|
||||
return EOK;
|
||||
}
|
||||
|
||||
int EthernetSend(struct Adapter *padapter, const char *data, int len, bool block, int time_out, int delay, send_success cb, void *param, void* p)
|
||||
{
|
||||
struct AdapterAT *ethernetAT_adapter = (struct AdapterAT *)padapter;
|
||||
|
||||
if (ethernetAT_adapter->agent){
|
||||
EntmSend(ethernetAT_adapter->agent, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
int EthernetReceive(struct Adapter *padapter, char *rev_buffer, int buffer_len, int time_out, bool block, void* p)
|
||||
{
|
||||
printf("ethernet receive waiting ... \n");
|
||||
|
||||
struct AdapterAT *ethernetAT_adapter = (struct AdapterAT *)padapter;
|
||||
if (ethernetAT_adapter->agent){
|
||||
if (EntmRecv(ethernetAT_adapter->agent, rev_buffer, buffer_len, 40000))
|
||||
printf("EntmRecv failed ! \n");
|
||||
}else{
|
||||
printf("Can not find agent \n");
|
||||
}
|
||||
}
|
||||
|
||||
uint32 EthernetInitAtCmd(ATAgentType at_agent)
|
||||
{
|
||||
uint32 result;
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
if (NULL == reply){
|
||||
printf("CreateATReply failed ! \n");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
UserTaskDelay(100);
|
||||
|
||||
|
||||
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, "a");
|
||||
|
||||
UserTaskDelay(500);
|
||||
|
||||
return result;
|
||||
|
||||
__exit:
|
||||
if (reply)
|
||||
DeleteATReply(reply);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void EthernetSetUpAdapter(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
#define LEN_PARA_BUF 128
|
||||
uint8 server_addr_wifi[LEN_PARA_BUF]="192.168.1.183";
|
||||
uint8 server_port_wifi[LEN_PARA_BUF]="12345";
|
||||
uint8 WIFI_ssid[LEN_PARA_BUF]="DDST 2";
|
||||
uint8 WIFI_pwd[LEN_PARA_BUF]="passw0rd";
|
||||
char cmd[LEN_PARA_BUF];
|
||||
|
||||
struct AdapterAT *adapterAT = (struct AdapterAT *) parameter;
|
||||
|
||||
//struct at_device_esp8266 *esp8266 = (struct at_device_esp8266 *) device->UserData;
|
||||
struct ATAgent *agent = adapterAT->agent;
|
||||
ATReplyType reply = NONE;
|
||||
x_err_t result = EOK;
|
||||
x_size_t retry_num = INIT_RETRY;
|
||||
|
||||
//DBG("%s device initialize start.", adapterAT->parent.);
|
||||
|
||||
/* wait hfa21 device startup finish */
|
||||
UserTaskDelay(5000);
|
||||
|
||||
reply = CreateATReply(64);
|
||||
if (reply == NONE){
|
||||
printf("no memory for reply create.");
|
||||
return;
|
||||
}
|
||||
|
||||
while (retry_num--){
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
UserTaskDelay(100);
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "a");
|
||||
UserTaskDelay(2500);
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "AT+FCLR\r");
|
||||
UserTaskDelay(30000);
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
UserTaskDelay(100);
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "a");
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+WSSSID=");
|
||||
strcat(cmd,WIFI_ssid);
|
||||
strcat(cmd,"\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+WSKEY=WPA2PSK,AES,");
|
||||
strcat(cmd,WIFI_pwd);
|
||||
strcat(cmd,"\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+WMODE=sta\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+NETP=TCP,CLIENT,");
|
||||
strcat(cmd,server_port_wifi);
|
||||
strcat(cmd,",");
|
||||
strcat(cmd,server_addr_wifi);
|
||||
strcat(cmd,"\r");
|
||||
// strcpy(cmd,"AT+NETP\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcat(cmd,"AT+Z\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
/* initialize successfully */
|
||||
result = EOK;
|
||||
break;
|
||||
|
||||
__exit:
|
||||
if (result != EOK)
|
||||
UserTaskDelay(1000);
|
||||
}
|
||||
|
||||
if (reply)
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
|
||||
int EthernetSetUp(struct AdapterAT *adapterAT)
|
||||
{
|
||||
EthernetSetUpAdapter(adapterAT);
|
||||
|
||||
return EOK;
|
||||
}
|
||||
|
||||
int EthernetDHCP(struct AdapterAT *adapterAT, bool enable)
|
||||
{
|
||||
int result = EOK;
|
||||
ATReplyType reply = NONE;
|
||||
char dhcp_status[4];
|
||||
memset(dhcp_status,0,sizeof(dhcp_status));
|
||||
if(enable)
|
||||
strcpy(dhcp_status,"on");
|
||||
else
|
||||
strcpy(dhcp_status,"off");
|
||||
|
||||
reply = CreateATReply(64);
|
||||
if (reply == NONE){
|
||||
printf("no memory for reply struct.");
|
||||
return -ENOMEMORY;
|
||||
}
|
||||
|
||||
/* send dhcp set commond "AT+CWDHCP_CUR=<mode>,<en>" and wait response */
|
||||
if (ATOrderSend(adapterAT->agent, REPLY_TIME_OUT, reply, "AT+DHCPDEN=%s", dhcp_status) < 0){
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
|
||||
__exit:
|
||||
if (reply)
|
||||
DeleteATReply(reply);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int EthernetPing(struct AdapterAT *adapterAT, const char *destination,struct ping_result *ping_resp)
|
||||
{
|
||||
char *ping_result = NONE;
|
||||
ping_result = (char *) UserCalloc(1, 17);
|
||||
|
||||
EthernetInitAtCmd(adapterAT->agent);
|
||||
|
||||
uint32 result = EOK;
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
if (NULL == reply){
|
||||
printf("CreateATReply failed ! \n");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
printf("\\r = 0x%x, \\n = 0x%x\n", '\r', '\n');
|
||||
|
||||
//ping baidu.com
|
||||
uint32 err = ATOrderSend(adapterAT->agent, REPLY_TIME_OUT, reply, "AT+PING=%s", "192.168.250.240\r");
|
||||
if (err){
|
||||
printf("at_obj_exec_cmd(AT+PING)failed ! err = %d\n", err);
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
ATOrderSend(adapterAT->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
|
||||
UserTaskDelay(2000);
|
||||
|
||||
|
||||
const char * result_buf = GetReplyText(reply);
|
||||
if(!result_buf){
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
char* str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
|
||||
ParseATReply(str, "+ok=%s\r", ping_result);
|
||||
|
||||
printf("ping result is:%s\n", ping_result);
|
||||
|
||||
__exit:
|
||||
if (reply)
|
||||
DeleteATReply(reply);
|
||||
return result;
|
||||
}
|
||||
|
||||
int EthernetNetstat(struct AdapterAT *adapterAT)
|
||||
{
|
||||
#define HFA21_NETSTAT_RESP_SIZE 320
|
||||
#define HFA21_NETSTAT_TYPE_SIZE 10
|
||||
#define HFA21_NETSTAT_IPADDR_SIZE 17
|
||||
#define HFA21_NETP_EXPRESSION "+ok=%[^,],%[^,],%d,%s\r"
|
||||
#define HFA21_WANN_EXPRESSION "+ok=%[^,],%[^,],%[^,],%[^,]\r"
|
||||
#define HFA21_LANN_EXPRESSION "+ok=%[^,],%[^,]\r"
|
||||
#define HFA21_WMODE_EXPRESSION "+ok=%s\r"
|
||||
|
||||
ATReplyType reply = NULL;
|
||||
struct ATAgent *agent = adapterAT->agent;
|
||||
uint32 result;
|
||||
char * result_buf = NULL;
|
||||
char * str = NULL;
|
||||
|
||||
/* sta/ap */
|
||||
char *work_mode = NULL;
|
||||
/* dhcp/static */
|
||||
char *ip_mode = NULL;
|
||||
char *local_ipaddr = NULL;
|
||||
char *gateway = NULL;
|
||||
char *netmask = NULL;
|
||||
local_ipaddr = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
gateway = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
netmask = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
work_mode = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
ip_mode = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
|
||||
reply = CreateATReply(HFA21_NETSTAT_RESP_SIZE);
|
||||
if (reply == NULL)
|
||||
goto __exit;
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
UserTaskDelay(100);
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "a");
|
||||
UserTaskDelay(2500);
|
||||
|
||||
if (ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+WMODE\r") < 0)
|
||||
goto __exit;
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
result_buf = GetReplyText(reply);
|
||||
if(!result_buf){
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
/* parse the third line of response data, get the network connection information */
|
||||
ParseATReply(str, HFA21_WMODE_EXPRESSION, work_mode);
|
||||
|
||||
if(work_mode[0]=='S'){
|
||||
if (ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+WANN\r") < 0)
|
||||
goto __exit;
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
result_buf = GetReplyText(reply);
|
||||
if(!result_buf){
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
/* parse the third line of response data, get the network connection information */
|
||||
ParseATReply(str, HFA21_WANN_EXPRESSION, ip_mode, local_ipaddr, netmask, gateway);
|
||||
}else{
|
||||
if (ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+LANN\r") < 0)
|
||||
goto __exit;
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
result_buf = GetReplyText(reply);
|
||||
if(!result_buf){
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
/* parse the third line of response data, get the network connection information */
|
||||
ParseATReply(str, HFA21_LANN_EXPRESSION, local_ipaddr, netmask);
|
||||
}
|
||||
|
||||
ATOrderSend(adapterAT->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
|
||||
UserTaskDelay(2500);
|
||||
|
||||
printf("work mode: %s\n", work_mode);
|
||||
if(work_mode[0]=='S')
|
||||
printf("ip mode: %s\nlocal ip: %s\nnetmask: %s\ngateway: %s\n", ip_mode, local_ipaddr, netmask, gateway);
|
||||
else
|
||||
printf("local ip: %s\nnetmask: %s\n", local_ipaddr, netmask);
|
||||
|
||||
return EOK;
|
||||
|
||||
__exit:
|
||||
if (reply)
|
||||
DeleteATReply(reply);
|
||||
if (local_ipaddr)
|
||||
UserFree(local_ipaddr);
|
||||
if (netmask)
|
||||
UserFree(netmask);
|
||||
if (gateway)
|
||||
UserFree(gateway);
|
||||
if (work_mode)
|
||||
UserFree(work_mode);
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* 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 xs_AdapterAT_ethernet_register.c
|
||||
* @brief Structure and function declarations of the ethernet register
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#include <xs_adapter_at_ethernet.h>
|
||||
#include <xs_adapter_manager.h>
|
||||
#include <xs_adapter_at_agent.h>
|
||||
#include <user_api.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
const struct AdapterDone EthernetAdapterDone =
|
||||
{
|
||||
EthernetClose,
|
||||
EthernetOpen,
|
||||
NULL,
|
||||
EthernetSend,
|
||||
EthernetReceive,
|
||||
NULL,
|
||||
};
|
||||
|
||||
const struct ATDone EthernetATDone =
|
||||
{
|
||||
EthernetSetUp,
|
||||
NULL,
|
||||
NULL,
|
||||
NULL,
|
||||
EthernetDHCP,
|
||||
EthernetPing,
|
||||
EthernetNetstat,
|
||||
NULL,
|
||||
};
|
||||
|
||||
int RegisterAdapterEthernet(void)
|
||||
{
|
||||
struct AdapterEthernet *ethernet_adapter = malloc(sizeof(struct AdapterEthernet));
|
||||
if (ethernet_adapter == NULL){
|
||||
printf("out of memory\n");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
struct AdapterAT *ethernetAT_adapter = (struct AdapterAT *)ethernet_adapter;
|
||||
struct Adapter *adapter = (struct Adapter *)ethernet_adapter;
|
||||
|
||||
ethernet_adapter->parent.atdone = EthernetATDone;
|
||||
ethernet_adapter->parent.parent.done = EthernetAdapterDone;
|
||||
|
||||
ethernetAT_adapter->at_adapter_id = ETHERNET_ADAPTER_ID;
|
||||
|
||||
ATAdapterInit();
|
||||
ATAdapterRegister(ethernetAT_adapter);
|
||||
|
||||
return EOK;
|
||||
}
|
||||
72
framework/connection/Adapter/include/xs_adapter.h
Normal file
72
framework/connection/Adapter/include/xs_adapter.h
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* 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 xs_adapter.h
|
||||
* @brief Adapter interface
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#ifndef XS_ADAPTER_N
|
||||
#define XS_ADAPTER_N
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <xs_klist.h>
|
||||
#include <xs_adapter_def.h>
|
||||
|
||||
|
||||
|
||||
enum AdapterType {
|
||||
ADAPTER_LORA = 0, /* Lora */
|
||||
ADAPTER_4G , /* 4G */
|
||||
ADAPTER_NBIOT , /* _NBIot */
|
||||
ADAPTER_WIFI , /* WIFI */
|
||||
ADAPTER_ETHERNET , /* ETHERNET */
|
||||
ADAPTER_BLUETOOTH , /* BLUETOOTH */
|
||||
ADAPTER_ZIGBEE , /* ZIGBEE */
|
||||
ADAPTER_5G , /* 5G */
|
||||
};
|
||||
enum MeshType{
|
||||
NET_P2P = 0,
|
||||
NET_ADHOC_SINGLE_GROUP,
|
||||
NET_ADHOC_MULTI_GROUP,
|
||||
};
|
||||
|
||||
enum NetRoleType{
|
||||
ROLE_TYPE_SLAVE = 1,
|
||||
ROLE_TYPE_MASTER,
|
||||
ROLE_TYPE_NONE,
|
||||
};
|
||||
|
||||
struct Adapter;
|
||||
struct AdapterDone {
|
||||
void (*NetAiitClose)(struct Adapter *padapter);
|
||||
int (*NetAiitOpen)(struct Adapter *padapter);
|
||||
int (*NetAiitJoin)(struct Adapter *padapter, int dev_type, char* net_id);
|
||||
int (*NetAiitSend)(struct Adapter *padapter, const char* data, int len, bool block, int time_out, int delay, send_success cb, void* param, void* reserved);
|
||||
int (*NetAiitReceive)(struct Adapter *padapter, char* rev_buffer, int buffer_len,int time_out, bool block, void* reserved);
|
||||
int (*NetAiitIoctl)(struct Adapter *padapter, int cmd, void *arg);
|
||||
};
|
||||
|
||||
struct Adapter
|
||||
{
|
||||
enum AdapterType type; /* type of adapter, such as lora adapter */
|
||||
enum NetRoleType net_role_type;
|
||||
enum MeshType mesh_type;
|
||||
struct AdapterDone done; /* socket-like APIs for data transferring */
|
||||
struct SysDoubleLinklistNode link; /* link list node */
|
||||
};
|
||||
typedef struct Adapter *adapter_t;
|
||||
|
||||
#endif
|
||||
94
framework/connection/Adapter/include/xs_adapter_at.h
Normal file
94
framework/connection/Adapter/include/xs_adapter_at.h
Normal file
@@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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 xs_adapter_at.h
|
||||
* @brief AdapterAT interface
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#ifndef XS_ADAPTER_AT_H
|
||||
#define XS_ADAPTER_AT_H
|
||||
|
||||
#include "xs_adapter.h"
|
||||
#include <xs_adapter_at_agent.h>
|
||||
|
||||
#define SOCKET_STATUS_UNUSED (0)
|
||||
#define SOCKET_STATUS_INIT (1)
|
||||
#define SOCKET_STATUS_CONNECT (2)
|
||||
|
||||
#define SOCKET_TYPE_DGRAM (0)
|
||||
#define SOCKET_TYPE_STREAM (1)
|
||||
|
||||
#define SOCKET_PROTOCOL_TCP (6)
|
||||
#define SOCKET_PROTOCOL_UDP (17)
|
||||
|
||||
#define NET_TYPE_AF_INET (0)
|
||||
#define NET_TYPE_AF_INET6 (1)
|
||||
|
||||
#define SOCKET_MAX 8
|
||||
|
||||
struct Socket
|
||||
{
|
||||
uint8_t fd;
|
||||
uint8_t status ;
|
||||
struct ADDRESS_IPV4 src_ip;
|
||||
uint16_t src_port;
|
||||
struct ADDRESS_IPV4 dst_ip;
|
||||
uint16_t dst_port;
|
||||
uint8_t type;
|
||||
uint8_t af_type;
|
||||
uint8_t protocal;
|
||||
uint8 is_client;
|
||||
};
|
||||
|
||||
struct AdapterAT;
|
||||
struct ATDone
|
||||
{
|
||||
int (*ATOperateUp)(struct AdapterAT *adapterAT);
|
||||
int (*ATOperateDown)(struct AdapterAT *adapterAT);
|
||||
int (*ATOperateAddr)(struct AdapterAT *adapterAT, uint ip, uint gateway, uint netmask);
|
||||
int (*ATOperateDns)(struct AdapterAT *adapterAT, struct ADDRESS_IPV4 *dns_addr, uint8 dns_count);
|
||||
int (*ATOperateDHCP)(struct AdapterAT *adapterAT, bool enable);
|
||||
int (*ATPing)(struct AdapterAT *adapterAT, const char *destination, struct ping_result *ping_resp);
|
||||
int (*ATNetstat)(struct AdapterAT *adapterAT);
|
||||
int (*ATOperateDefault)(struct AdapterAT *adapterAT);
|
||||
|
||||
int (*ATSocketCreate)(struct AdapterAT *adapterAT , uint8_t socket_fd , uint8_t type , uint8_t af_type );
|
||||
int (*ATSocketConnect)(struct AdapterAT *adapterAT , uint8_t socket_fd , struct ADDRESS_IPV4 dst_ip , uint16_t dst_port, uint8 is_client);
|
||||
int (*ATSocketClose)(struct AdapterAT *adapterAT , uint8_t socket_fd );
|
||||
};
|
||||
|
||||
struct AdapterAT {
|
||||
struct Adapter parent;
|
||||
struct ATAgent *agent;
|
||||
struct ADDRESS_IPV4 ip;
|
||||
struct ADDRESS_IPV4 netmask;
|
||||
struct ADDRESS_IPV4 gateway;
|
||||
struct ADDRESS_IPV4 dns[DNS_COUNT];
|
||||
uint32 at_adapter_id;
|
||||
struct Socket socket[SOCKET_MAX];
|
||||
uint8 hardware_address_len;
|
||||
uint8 hardware_address[HW_MAX];
|
||||
uint16 flags;
|
||||
uint16 mtu;
|
||||
|
||||
void (*change_cb )(struct AdapterAT *adapterAT, enum cb_type type, enum change_type ch_type_);
|
||||
|
||||
struct ATDone atdone;
|
||||
|
||||
struct SysDoubleLinklistNode link;
|
||||
};
|
||||
|
||||
#endif
|
||||
81
framework/connection/Adapter/include/xs_adapter_at_agent.h
Normal file
81
framework/connection/Adapter/include/xs_adapter_at_agent.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file xs_adapter_at_agent.h
|
||||
* @brief AT proxy, auto receive AT reply and transparency data
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#ifndef XS_ADAPTER_AT_CLIENT_H
|
||||
#define XS_ADAPTER_AT_CLIENT_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stddef.h>
|
||||
#include <bus_serial.h>
|
||||
|
||||
enum ReceiveMode
|
||||
{
|
||||
ENTM_MODE = 1,
|
||||
AT_MODE = 2,
|
||||
};
|
||||
|
||||
struct ATReply
|
||||
{
|
||||
char *reply_buffer;
|
||||
uint32 reply_max_len;
|
||||
uint32 reply_len;
|
||||
};
|
||||
typedef struct ATReply *ATReplyType;
|
||||
|
||||
struct ATAgent
|
||||
{
|
||||
char agent_name[NAME_NUM_MAX];
|
||||
int fd;
|
||||
|
||||
char *maintain_buffer;
|
||||
uint32 maintain_len;
|
||||
uint32 maintain_max;
|
||||
|
||||
int32 lock;
|
||||
|
||||
ATReplyType reply;
|
||||
int rsp_sem;
|
||||
|
||||
int32 at_handler;
|
||||
|
||||
#define ENTM_RECV_MAX 256
|
||||
char entm_recv_buf[ENTM_RECV_MAX];
|
||||
uint32 entm_recv_len;
|
||||
enum ReceiveMode receive_mode;
|
||||
int entm_rx_notice;
|
||||
};
|
||||
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);
|
||||
char *GetReplyText(ATReplyType reply);
|
||||
ATReplyType CreateATReply(uint32 reply_max_len);
|
||||
uint IpTint(char *ipstr);
|
||||
void SwapStr(char *str, int begin, int end);
|
||||
char* IpTstr(uint ipint);
|
||||
ATAgentType GetATAgent(const char *agent_name);
|
||||
int InitATAgent(const char *agent_name, const char *device_name, uint32 maintain_max, struct SerialCfgParam* pserial_cfg);
|
||||
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
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 xs_adapter_at_ethernet.h
|
||||
* @brief Structure and function declarations of the connection ethernet
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#ifndef XS_ADAPTER_AT_ETHERNET_H
|
||||
#define XS_ADAPTER_AT_ETHERNET_H
|
||||
|
||||
#include "xs_adapter.h"
|
||||
#include "xs_adapter_at.h"
|
||||
#include "xs_adapter_def.h"
|
||||
#include "xs_klist.h"
|
||||
|
||||
struct AdapterEthernet {
|
||||
struct AdapterAT parent; /* inherit from Adapter */
|
||||
|
||||
char vendor_name[NAME_LEN_MAX];
|
||||
char product_ID_ethernet[NAME_LEN_MAX];
|
||||
|
||||
struct SingleLinklistNode link;
|
||||
};
|
||||
|
||||
void EthernetClose(struct Adapter *padapter);
|
||||
int EthernetOpen(struct Adapter *padapter);
|
||||
int EthernetSend(struct Adapter *padapter, const char *data, int len, bool block, int time_out, int delay, send_success cb, void *param, void *p);
|
||||
int EthernetReceive(struct Adapter *padapter, char *rev_buffer, int buffer_len, int time_out, bool block, void *p);
|
||||
|
||||
int EthernetSetUp(struct AdapterAT *adapterAT);
|
||||
int EthernetDHCP(struct AdapterAT *adapterAT, bool enable);
|
||||
int EthernetPing(struct AdapterAT *adapterAT, const char *destination, struct ping_result *ping_resp);
|
||||
int EthernetNetstat(struct AdapterAT *adapterAT);
|
||||
|
||||
#endif
|
||||
50
framework/connection/Adapter/include/xs_adapter_at_wifi.h
Normal file
50
framework/connection/Adapter/include/xs_adapter_at_wifi.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 xs_adapter_at_wifi.h
|
||||
* @brief Structure and function declarations of the connection wifi
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#ifndef XS_ADAPTER_AT_WIFI_H
|
||||
#define XS_ADAPTER_AT_WIFI_H
|
||||
|
||||
#include "xs_adapter.h"
|
||||
#include "xs_adapter_at.h"
|
||||
#include "xs_adapter_def.h"
|
||||
#include "xs_klist.h"
|
||||
|
||||
struct Adapterwifi {
|
||||
struct AdapterAT parent; /* inherit from Adapter */
|
||||
|
||||
char vendor_name[NAME_LEN_MAX];
|
||||
char product_id_wifi[NAME_LEN_MAX];
|
||||
|
||||
struct SingleLinklistNode link;
|
||||
};
|
||||
|
||||
void WifiClose(struct Adapter *padapter);
|
||||
int WifiOpen(struct Adapter *padapter);
|
||||
int WifiSend(struct Adapter *padapter, const char *data, int len, bool block, int time_out, int delay, send_success cb, void *param, void *p);
|
||||
int WifiReceive(struct Adapter *padapter, char *rev_buffer, int buffer_len, int time_out, bool block, void *p);
|
||||
|
||||
int WifiSetUp(struct AdapterAT *adapter_at);
|
||||
int WifiSetDown(struct AdapterAT *adapter_at);
|
||||
int WifiSetAddr(struct AdapterAT *adapter_at, uint ip, uint gateway, uint netmask);
|
||||
int WifiDHCP(struct AdapterAT *adapter_at, bool enable);
|
||||
int WifiPing(struct AdapterAT *adapter_at, const char *destination,struct ping_result *ping_resp);
|
||||
int WifiNetstat(struct AdapterAT *adapter_at);
|
||||
|
||||
#endif
|
||||
76
framework/connection/Adapter/include/xs_adapter_def.h
Normal file
76
framework/connection/Adapter/include/xs_adapter_def.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* 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 xs_adapter_def.h
|
||||
* @brief defines about adapter
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#ifndef __XS_ADAPTER_DEF_H__
|
||||
#define __XS_ADAPTER_DEF_H__
|
||||
|
||||
#include "stdbool.h"
|
||||
|
||||
typedef void(*send_success)(void* param);
|
||||
|
||||
#define NAME_LEN_MAX 32
|
||||
|
||||
#define IPV6__ADDRESS_COUNT 3U
|
||||
#define DNS_COUNT 2U
|
||||
#define HW_MAX 8U
|
||||
|
||||
enum cb_type
|
||||
{
|
||||
CB_ADDR_IP, /* IP address */
|
||||
CB_ADDR_NETMASK, /* subnet mask */
|
||||
CB_ADDR_GATEWAY, /* netmask */
|
||||
CB_ADDR_DNS_SERVER, /* dns server */
|
||||
CB_STATUS_UP, /* changed to 'up' */
|
||||
CB_STATUS_DOWN, /* changed to 'down' */
|
||||
CB_STATUS_LINK_UP, /* changed to 'link up' */
|
||||
CB_STATUS_LINK_DOWN, /* changed to 'link down' */
|
||||
CB_STATUS_INTERNET_UP, /* changed to 'internet up' */
|
||||
CB_STATUS_INTERNET_DOWN, /* changed to 'internet down' */
|
||||
CB_STATUS_DHCP_ENABLE, /* enable DHCP capability */
|
||||
CB_STATUS_DHCP_DISABLE, /* disable DHCP capability */
|
||||
};
|
||||
|
||||
enum change_type
|
||||
{
|
||||
addr_change,
|
||||
status_change,
|
||||
};
|
||||
|
||||
struct ADDRESS_IPV4
|
||||
{
|
||||
uint32 ipv4;
|
||||
};
|
||||
|
||||
struct ping_result
|
||||
{
|
||||
struct ADDRESS_IPV4 ip_addr; /* response IP address */
|
||||
uint16 data_len; /* response data length */
|
||||
uint16 ttl; /* time to live */
|
||||
uint32 ticks; /* response time, unit tick */
|
||||
void *UserData; /* user-specific data */
|
||||
};
|
||||
|
||||
#define ETHERNET_ADAPTER_ID 0x03U
|
||||
#define WIFI_ADAPTER_ID 0x04U
|
||||
#define fourG_ADAPTER_ID 0x05U
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
40
framework/connection/Adapter/include/xs_adapter_manager.h
Normal file
40
framework/connection/Adapter/include/xs_adapter_manager.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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 xs_adapter_manager.h
|
||||
* @brief manager adapter list
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#ifndef ADAPTER_MANAGER_H_
|
||||
#define ADAPTER_MANAGER_H_
|
||||
#include "xs_adapter.h"
|
||||
#include <xs_adapter_at.h>
|
||||
|
||||
void LoraAdapterInit();
|
||||
void LoraAdapterRegister(adapter_t padapter);
|
||||
void* LoraAdapterFind(char* name);
|
||||
|
||||
|
||||
void ATAdapterInit();
|
||||
void ATAdapterRegister(struct AdapterAT* at_adapter);
|
||||
void* ATAdapterFind(uint32 adapter_id);
|
||||
|
||||
void ZigbeeAdapterInit();
|
||||
void ZigbeeAdapterRegister(adapter_t padapter);
|
||||
void* ZigbeeAdapterFind(char* name);
|
||||
|
||||
|
||||
#endif
|
||||
41
framework/connection/Adapter/include/xs_adapter_zigbee.h
Normal file
41
framework/connection/Adapter/include/xs_adapter_zigbee.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: xs_adapter_zigbee.h
|
||||
* @brief: head file
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2020/4/25
|
||||
*
|
||||
*/
|
||||
#ifndef XS_ADAPTER_ZIGBEE_H
|
||||
#define XS_ADAPTER_ZIGBEE_H
|
||||
#include "xs_adapter.h"
|
||||
|
||||
struct AdapterZigbee {
|
||||
struct Adapter parent; /* inherit from Adapter */
|
||||
const char * name; /* name of the adapter instance */
|
||||
|
||||
const char * device_type; /* type of the adapter instance */
|
||||
|
||||
|
||||
};
|
||||
typedef struct AdapterZigbee *adapterZigbee_t;
|
||||
|
||||
int ZigbeeOpen(struct Adapter *padapter);
|
||||
int ZigbeeSend(struct Adapter *padapter, const char* data, int len, bool block, int time_out, int delay, send_success cb, void* param, void* reserved);
|
||||
int ZigbeeReceive(struct Adapter *padapter, char* rev_buffer, int buffer_len,int time_out, bool block, void* reserved);
|
||||
void ZigbeeClose(struct Adapter *padapter);
|
||||
|
||||
|
||||
#endif
|
||||
4
framework/connection/Adapter/src/Makefile
Normal file
4
framework/connection/Adapter/src/Makefile
Normal file
@@ -0,0 +1,4 @@
|
||||
SRC_FILES := xs_adapter_manager.c \
|
||||
xs_adapter_at_agent.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
478
framework/connection/Adapter/src/xs_adapter_at_agent.c
Normal file
478
framework/connection/Adapter/src/xs_adapter_at_agent.c
Normal file
@@ -0,0 +1,478 @@
|
||||
/*
|
||||
* 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 xs_adapterAT_client.c
|
||||
* @brief AT proxy, auto receive AT reply and transparency data
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
|
||||
#include <xs_adapter_at_agent.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <user_api.h>
|
||||
#include <bus.h>
|
||||
|
||||
#define AT_CMD_MAX_LEN 128
|
||||
#define AT_AGENT_MAX 2
|
||||
static char send_buf[AT_CMD_MAX_LEN];
|
||||
static uint32 last_cmd_len = 0;
|
||||
|
||||
static struct ATAgent at_agent_table[AT_AGENT_MAX] = {0};
|
||||
|
||||
uint IpTint(char *ipstr){
|
||||
if (ipstr == NULL)
|
||||
return 0;
|
||||
|
||||
char *token;
|
||||
uint i = 3, total = 0, cur;
|
||||
|
||||
token = strtok(ipstr, ".");
|
||||
|
||||
while (token != NULL){
|
||||
cur = atoi(token);
|
||||
if (cur >= 0 && cur <= 255){
|
||||
total += cur * pow(256, i);
|
||||
}
|
||||
i--;
|
||||
token = strtok(NULL, ".");
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
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]){
|
||||
str[i] = str[i] ^ str[j];
|
||||
str[j] = str[i] ^ str[j];
|
||||
str[i] = str[i] ^ str[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char *IpTstr(uint ipint)
|
||||
{
|
||||
int LEN = 16;
|
||||
char *new = (char *)malloc(LEN);
|
||||
memset(new, '\0', LEN);
|
||||
new[0] = '.';
|
||||
char token[4];
|
||||
int bt, ed, len, cur;
|
||||
|
||||
while (ipint){
|
||||
cur = ipint % 256;
|
||||
sprintf(token, "%d", cur);
|
||||
strcat(new, token);
|
||||
ipint /= 256;
|
||||
if (ipint)
|
||||
strcat(new, ".");
|
||||
}
|
||||
|
||||
len = strlen(new);
|
||||
SwapStr(new, 0, len - 1);
|
||||
|
||||
for (bt = ed = 0; ed < len;){
|
||||
while (ed < len && new[ed] != '.'){
|
||||
ed++;
|
||||
}
|
||||
SwapStr(new, bt, ed - 1);
|
||||
ed += 1;
|
||||
bt = ed;
|
||||
}
|
||||
|
||||
new[len - 1] = '\0';
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
int ParseATReply(char *str, const char *format, ...)
|
||||
{
|
||||
va_list params;
|
||||
int counts = 0;
|
||||
|
||||
va_start(params, format);
|
||||
counts = vsscanf(str, format, params);
|
||||
va_end(params);
|
||||
|
||||
return counts;
|
||||
}
|
||||
|
||||
uint32 ATSprintf(int fd, const char *format, va_list params)
|
||||
{
|
||||
last_cmd_len = vsnprintf(send_buf, sizeof(send_buf), format, params);
|
||||
return write(fd, send_buf, last_cmd_len);
|
||||
}
|
||||
|
||||
int ATOrderSend(ATAgentType agent, uint32 timeout, ATReplyType reply, const char *cmd_expr, ...)
|
||||
{
|
||||
if (agent == NULL){
|
||||
printf("ATAgent is null");
|
||||
return -ERROR;
|
||||
}
|
||||
|
||||
agent->receive_mode = AT_MODE;
|
||||
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
agent->maintain_len = 0;
|
||||
|
||||
memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX);
|
||||
agent->entm_recv_len = 0;
|
||||
|
||||
va_list params;
|
||||
uint32 cmd_size = 0;
|
||||
uint32 result = EOK;
|
||||
const char *cmd = NULL;
|
||||
|
||||
UserMutexObtain(agent->lock, WAITING_FOREVER);
|
||||
|
||||
agent->reply = reply;
|
||||
|
||||
if(agent->reply != NULL){
|
||||
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){
|
||||
result = -ETIMEOUT;
|
||||
goto __out;
|
||||
}
|
||||
}else{
|
||||
va_start(params, cmd_expr);
|
||||
ATSprintf(agent->fd, cmd_expr, params);
|
||||
va_end(params);
|
||||
goto __out;
|
||||
}
|
||||
|
||||
__out:
|
||||
agent->reply = NULL;
|
||||
UserMutexAbandon(agent->lock);
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
char *GetReplyText(ATReplyType reply)
|
||||
{
|
||||
return reply->reply_buffer;
|
||||
}
|
||||
|
||||
int EntmSend(ATAgentType agent, const char *data, int len)
|
||||
{
|
||||
char send_buf[128];
|
||||
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);
|
||||
}
|
||||
|
||||
int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int time_out)
|
||||
{
|
||||
UserTaskDelay(1000);
|
||||
|
||||
memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX);
|
||||
agent->entm_recv_len = 0;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static int GetCompleteATReply(ATAgentType agent)
|
||||
{
|
||||
uint32 read_len = 0;
|
||||
char ch = 0, last_ch = 0;
|
||||
bool is_full = false;
|
||||
|
||||
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
|
||||
agent->maintain_len = 0;
|
||||
|
||||
while (1){
|
||||
read(agent->fd, &ch, 1);
|
||||
|
||||
printf(" %c(0x%x)\n", ch, ch);
|
||||
|
||||
if (agent->receive_mode == ENTM_MODE){
|
||||
if (agent->entm_recv_len < ENTM_RECV_MAX){
|
||||
agent->entm_recv_buf[agent->entm_recv_len++] = ch;
|
||||
|
||||
if (last_ch == '!' && ch == '@'){
|
||||
UserSemaphoreAbandon(agent->entm_rx_notice);
|
||||
}
|
||||
|
||||
last_ch = ch;
|
||||
}
|
||||
else{
|
||||
printf("entm_recv_buf is_full ...\n");
|
||||
}
|
||||
}
|
||||
else if (agent->receive_mode == AT_MODE){
|
||||
if (read_len < agent->maintain_max){
|
||||
agent->maintain_buffer[read_len++] = ch;
|
||||
agent->maintain_len = read_len;
|
||||
}else{
|
||||
printf("maintain_len is_full ...\n");
|
||||
is_full = true;
|
||||
}
|
||||
|
||||
if ((ch == '\n' && last_ch == '\r')){
|
||||
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;
|
||||
return -ERROR;
|
||||
}
|
||||
printf("GetCompleteATReply get n r ...\n");
|
||||
break;
|
||||
}
|
||||
last_ch = ch;
|
||||
}
|
||||
}
|
||||
|
||||
return read_len;
|
||||
}
|
||||
|
||||
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){
|
||||
result = &at_agent_table[i];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
static int DeleteATAgent(ATAgentType agent)
|
||||
{
|
||||
if (agent->lock){
|
||||
UserMutexDelete(agent->lock);
|
||||
}
|
||||
|
||||
if (agent->entm_rx_notice){
|
||||
UserSemaphoreDelete(agent->entm_rx_notice);
|
||||
}
|
||||
|
||||
if (agent->fd > 0){
|
||||
close(agent->fd);
|
||||
}
|
||||
|
||||
if (agent->rsp_sem){
|
||||
UserSemaphoreDelete(agent->rsp_sem);
|
||||
}
|
||||
|
||||
if (agent->maintain_buffer){
|
||||
free(agent->maintain_buffer);
|
||||
}
|
||||
|
||||
memset(agent, 0x00, sizeof(struct ATAgent));
|
||||
}
|
||||
|
||||
static void ATAgentReceiveProcess(void *param)
|
||||
{
|
||||
ATAgentType agent = (ATAgentType)param;
|
||||
const struct at_urc *urc;
|
||||
|
||||
while (1){
|
||||
if (GetCompleteATReply(agent) > 0){
|
||||
if (agent->reply != NULL){
|
||||
ATReplyType reply = agent->reply;
|
||||
|
||||
agent->maintain_buffer[agent->maintain_len - 1] = '\0';
|
||||
|
||||
if (agent->maintain_len < 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);
|
||||
}
|
||||
|
||||
agent->reply = NULL;
|
||||
UserSemaphoreAbandon(agent->rsp_sem);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int ATAgentInit(ATAgentType agent)
|
||||
{
|
||||
int result = EOK;
|
||||
utask_x at_utask;
|
||||
do
|
||||
{
|
||||
agent->maintain_len = 0;
|
||||
agent->maintain_buffer = (char *)malloc(agent->maintain_max);
|
||||
|
||||
if (agent->maintain_buffer == NONE){
|
||||
break;
|
||||
}
|
||||
|
||||
agent->entm_rx_notice = UserSemaphoreCreate(0);
|
||||
if (agent->entm_rx_notice == 0){
|
||||
break;
|
||||
}
|
||||
|
||||
agent->rsp_sem = UserSemaphoreCreate(0);
|
||||
if (agent->rsp_sem == 0){
|
||||
break;
|
||||
}
|
||||
|
||||
agent->lock = UserMutexCreate();
|
||||
if (agent->lock == 0){
|
||||
break;
|
||||
}
|
||||
|
||||
agent->receive_mode = ENTM_MODE;
|
||||
|
||||
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->at_handler = UserTaskCreate(at_utask);
|
||||
|
||||
if (agent->at_handler == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
result = EOK;
|
||||
return result;
|
||||
} while (1);
|
||||
|
||||
DeleteATAgent(agent);
|
||||
result = -ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static int SerialBusCheck(struct ATAgent *agent, const char *device_name, struct SerialCfgParam *pserial_cfg)
|
||||
{
|
||||
int ret = EOK;
|
||||
struct SerialDataCfg data_cfg;
|
||||
memset(&data_cfg, 0, sizeof(struct SerialDataCfg));
|
||||
|
||||
agent->fd = open(device_name,O_RDWR);
|
||||
|
||||
data_cfg.serial_baud_rate = 57600;
|
||||
ioctl(agent->fd, OPE_INT, &data_cfg);
|
||||
|
||||
}
|
||||
|
||||
int InitATAgent(const char *agent_name, const char *device_name, uint32 maintain_max, struct SerialCfgParam *pserial_cfg)
|
||||
{
|
||||
int i = 0;
|
||||
int result = EOK;
|
||||
int open_result = EOK;
|
||||
struct ATAgent *agent = NONE;
|
||||
|
||||
if (GetATAgent(agent_name) != NULL) {
|
||||
return result;
|
||||
}
|
||||
|
||||
while (i < AT_AGENT_MAX && at_agent_table[i].fd > 0) {
|
||||
i++;
|
||||
}
|
||||
|
||||
if (i >= AT_AGENT_MAX) {
|
||||
printf("agent buffer(%d) is full .", AT_AGENT_MAX);
|
||||
result = -ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
agent = &at_agent_table[i];
|
||||
|
||||
SerialBusCheck(agent, device_name, pserial_cfg);
|
||||
|
||||
strcpy(agent->agent_name, agent_name);
|
||||
|
||||
agent->maintain_max = maintain_max;
|
||||
|
||||
result = ATAgentInit(agent);
|
||||
if (result == EOK)
|
||||
{
|
||||
UserTaskStartup(agent->at_handler);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ATReplyType CreateATReply(uint32 reply_max_len)
|
||||
{
|
||||
ATReplyType reply = NULL;
|
||||
|
||||
reply = (ATReplyType)malloc(sizeof(struct ATReply));
|
||||
if (reply == NULL){
|
||||
printf("no more memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
reply->reply_max_len = reply_max_len;
|
||||
|
||||
reply->reply_buffer = (char *)malloc(reply_max_len);
|
||||
if (reply->reply_buffer == NULL){
|
||||
printf("no more memory\n");
|
||||
free(reply);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
||||
void DeleteATReply(ATReplyType reply)
|
||||
{
|
||||
if (reply){
|
||||
if (reply->reply_buffer){
|
||||
free(reply->reply_buffer);
|
||||
reply->reply_buffer = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if (reply){
|
||||
free(reply);
|
||||
reply = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
100
framework/connection/Adapter/src/xs_adapter_manager.c
Normal file
100
framework/connection/Adapter/src/xs_adapter_manager.c
Normal file
@@ -0,0 +1,100 @@
|
||||
/*
|
||||
* 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 xs_adapter_manager.c
|
||||
* @brief manage adapter list
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#include "xs_adapter_manager.h"
|
||||
#include <xs_klist.h>
|
||||
#include <string.h>
|
||||
#include <xs_adapter_at.h>
|
||||
#include <xs_adapter.h>
|
||||
#ifdef CONNECTION_COMMUNICATION_ZIGBEE
|
||||
#include <xs_adapter_zigbee.h>
|
||||
#endif
|
||||
|
||||
|
||||
// Zigbee Adapter List
|
||||
#ifdef CONNECTION_COMMUNICATION_ZIGBEE
|
||||
static DoubleLinklistType zigbee_adapter_list;
|
||||
|
||||
void ZigbeeAdapterInit()
|
||||
{
|
||||
InitDoubleLinkList(&zigbee_adapter_list);
|
||||
}
|
||||
|
||||
void ZigbeeAdapterRegister(adapter_t padapter)
|
||||
{
|
||||
DoubleLinkListInsertNodeAfter(&zigbee_adapter_list, &(padapter->link));
|
||||
}
|
||||
|
||||
void* ZigbeeAdapterFind(char* name)
|
||||
{
|
||||
DoubleLinklistType* pnode = NONE;
|
||||
DoubleLinklistType* phead = &zigbee_adapter_list;
|
||||
struct AdapterZigbee* padapter = NONE;
|
||||
|
||||
for(pnode = phead->node_next; pnode != phead; pnode = pnode->node_next)
|
||||
{
|
||||
padapter = (struct AdapterZigbee*)SYS_DOUBLE_LINKLIST_ENTRY(pnode, struct Adapter, link);
|
||||
// KPrintf("ZigbeeReceiveDemo bbb\n");
|
||||
if (0 == strcmp(padapter->name, name)){
|
||||
return padapter;
|
||||
}
|
||||
}
|
||||
|
||||
return padapter;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// AT Adapter List
|
||||
static DoubleLinklistType at_adapter_list;
|
||||
static int at_adapter_list_inited = 0;
|
||||
void ATAdapterInit()
|
||||
{
|
||||
if(!at_adapter_list_inited){
|
||||
InitDoubleLinkList(&at_adapter_list);
|
||||
at_adapter_list_inited = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void ATAdapterRegister(struct AdapterAT* at_adapter)
|
||||
{
|
||||
DoubleLinkListInsertNodeAfter(&at_adapter_list, &(at_adapter->link));
|
||||
}
|
||||
|
||||
void* ATAdapterFind(uint32 adapter_id)
|
||||
{
|
||||
DoubleLinklistType* pnode = NONE;
|
||||
DoubleLinklistType* phead = &at_adapter_list;
|
||||
struct AdapterAT* padapter = NONE;
|
||||
|
||||
|
||||
for(pnode = phead->node_next; pnode != phead; pnode = pnode->node_next)
|
||||
{
|
||||
padapter = (struct AdapterAT*)SYS_DOUBLE_LINKLIST_ENTRY(pnode, struct AdapterAT, link);
|
||||
|
||||
if (padapter->at_adapter_id == adapter_id){
|
||||
return padapter;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
4
framework/connection/Adapter/wifi/Makefile
Normal file
4
framework/connection/Adapter/wifi/Makefile
Normal file
@@ -0,0 +1,4 @@
|
||||
SRC_FILES += xs_adapter_at_wifi.c \
|
||||
xs_adapter_at_wifi_register.c \
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
482
framework/connection/Adapter/wifi/xs_adapter_at_wifi.c
Normal file
482
framework/connection/Adapter/wifi/xs_adapter_at_wifi.c
Normal file
@@ -0,0 +1,482 @@
|
||||
/*
|
||||
* 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 xs_adapter_at_wifi.c
|
||||
* @brief HFA21 wifi driver base connection framework
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <user_api.h>
|
||||
#include <xs_adapter_at_wifi.h>
|
||||
#include <xs_adapter_manager.h>
|
||||
#include <xs_adapter_at_agent.h>
|
||||
#include <xs_adapter_def.h>
|
||||
|
||||
int WifiSetDown(struct AdapterAT *adapter_at);
|
||||
|
||||
/**
|
||||
* @description: Close wifi
|
||||
* @param padapter - wifi device pointer
|
||||
*/
|
||||
void WifiClose(struct Adapter *padapter)
|
||||
{
|
||||
WifiSetDown((struct AdapterAT *)padapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: Open wifi
|
||||
* @param padapter - wifi device pointer
|
||||
* @return success: EOK, failure: -ERROR
|
||||
*/
|
||||
int WifiOpen(struct Adapter *padapter)
|
||||
{
|
||||
uint32 result;
|
||||
char *agent_name = "uart3_client";
|
||||
const char *device_name = WIFI_UART_NAME;
|
||||
|
||||
if (InitATAgent(agent_name, device_name, 512, NULL)) {
|
||||
printf("at_client_init failed ! \n");
|
||||
result = -ERROR;
|
||||
return result;
|
||||
}
|
||||
|
||||
ATAgentType at_agent = GetATAgent(agent_name);
|
||||
if (NULL == at_agent) {
|
||||
printf("GetATAgent failed ! \n");
|
||||
return -ERROR;
|
||||
}
|
||||
UserTaskDelay(5000);
|
||||
struct AdapterAT *wifi_at_adapter = (struct AdapterAT *)padapter;
|
||||
wifi_at_adapter->agent = at_agent;
|
||||
}
|
||||
|
||||
int WifiSend(struct Adapter *padapter, const char *data, int len, bool block, int time_out, int delay, send_success cb, void *param, void* p)
|
||||
{
|
||||
struct AdapterAT *wifi_at_adapter = (struct AdapterAT *)padapter;
|
||||
|
||||
if (wifi_at_adapter->agent) {
|
||||
EntmSend(wifi_at_adapter->agent, data, len);
|
||||
}
|
||||
}
|
||||
|
||||
int WifiReceive(struct Adapter *padapter, char *rev_buffer, int buffer_len, int time_out, bool block, void* p)
|
||||
{
|
||||
printf("wifi receive waiting ... \n");
|
||||
|
||||
struct AdapterAT *wifi_at_adapter = (struct AdapterAT *)padapter;
|
||||
if (wifi_at_adapter->agent) {
|
||||
return EntmRecv(wifi_at_adapter->agent, rev_buffer, buffer_len, 40000);
|
||||
} else {
|
||||
printf("Can not find agent \n");
|
||||
}
|
||||
}
|
||||
|
||||
uint32 WifiInitAtCmd(ATAgentType at_agent)
|
||||
{
|
||||
uint32 result;
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
if (NULL == reply) {
|
||||
printf("at_create_resp failed ! \n");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
UserTaskDelay(100);
|
||||
|
||||
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, "a");
|
||||
|
||||
UserTaskDelay(500);
|
||||
|
||||
return result;
|
||||
|
||||
__exit:
|
||||
if (reply) {
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void WifiSetUpAdapter(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
#define LEN_PARA_BUF 128
|
||||
uint8 server_addr_wifi[LEN_PARA_BUF] = "192.168.1.183";
|
||||
uint8 server_port_wifi[LEN_PARA_BUF] = "12345";
|
||||
uint8 wifi_ssid[LEN_PARA_BUF] = "AIIT-Guest";
|
||||
uint8 wifi_pwd[LEN_PARA_BUF] = "";
|
||||
char cmd[LEN_PARA_BUF];
|
||||
|
||||
struct AdapterAT *adapter_at = (struct AdapterAT *) parameter;
|
||||
|
||||
//struct at_device_esp8266 *esp8266 = (struct at_device_esp8266 *) device->UserData;
|
||||
struct ATAgent *agent = adapter_at->agent;
|
||||
ATReplyType reply = NONE;
|
||||
x_err_t result = EOK;
|
||||
x_size_t retry_num = INIT_RETRY;
|
||||
|
||||
/* wait hfa21 device startup finish */
|
||||
UserTaskDelay(5000);
|
||||
|
||||
reply = CreateATReply(64);
|
||||
if (reply == NONE) {
|
||||
printf("no memory for reply create.");
|
||||
return;
|
||||
}
|
||||
|
||||
while (retry_num--) {
|
||||
WifiInitAtCmd(agent);
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "AT+FCLR\r");
|
||||
UserTaskDelay(20000);
|
||||
|
||||
WifiInitAtCmd(agent);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+WANN\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+WSSSID=");
|
||||
strcat(cmd,wifi_ssid);
|
||||
strcat(cmd,"\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+WSKEY=WPA2PSK,AES,");
|
||||
strcat(cmd,wifi_pwd);
|
||||
strcat(cmd,"\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcpy(cmd,"AT+WMODE=sta\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
memset(cmd,0,sizeof(cmd));
|
||||
strcat(cmd,"AT+Z\r");
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
|
||||
UserTaskDelay(5000);
|
||||
|
||||
/* initialize successfully */
|
||||
result = EOK;
|
||||
break;
|
||||
|
||||
__exit:
|
||||
if (result != EOK) {
|
||||
UserTaskDelay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
if (reply) {
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
}
|
||||
|
||||
int WifiSetDown(struct AdapterAT *adapter_at)
|
||||
{
|
||||
WifiInitAtCmd(adapter_at->agent);
|
||||
|
||||
ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, NULL, "AT+FCLR\r");
|
||||
UserTaskDelay(20000);
|
||||
}
|
||||
|
||||
int WifiSetUp(struct AdapterAT *adapter_at)
|
||||
{
|
||||
WifiSetUpAdapter(adapter_at);
|
||||
|
||||
return EOK;
|
||||
}
|
||||
|
||||
int WifiSetAddr(struct AdapterAT *adapter_at, uint ip, uint gateway, uint netmask)
|
||||
{
|
||||
#define HFA21_SET_ADDR_EXPRESSION "+ok=%[^,],%[^,],%[^,],%[^,]\r"
|
||||
char *dhcp_mode =NULL;
|
||||
char *ip_str = NULL;
|
||||
/* role type(server/agent) */
|
||||
char *gw_str = NULL;
|
||||
char *mask_str = NULL;
|
||||
|
||||
dhcp_mode = (char *) UserCalloc(1, 8);
|
||||
ip_str = (char *) UserCalloc(1, 17);
|
||||
gw_str = (char *) UserCalloc(1, 17);
|
||||
mask_str = (char *) UserCalloc(1, 17);
|
||||
|
||||
WifiInitAtCmd(adapter_at->agent);
|
||||
|
||||
uint32 result = EOK;
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
if (NULL == reply) {
|
||||
printf("at_create_resp failed ! \n");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
uint32 err = ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, NULL, "AT+WANN=%s,%s,%s,%s\r", "static", IpTstr(ip), IpTstr(netmask), IpTstr(gateway));
|
||||
if (err) {
|
||||
printf("ATOrderSend(AT+PING)failed ! err = %d\n", err);
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, reply, "AT+WANN\r");
|
||||
UserTaskDelay(2500);
|
||||
ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
|
||||
UserTaskDelay(5000);
|
||||
|
||||
const char * result_buf = GetReplyText(reply);
|
||||
if (!result_buf) {
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
char* str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
|
||||
ParseATReply(str, HFA21_SET_ADDR_EXPRESSION, dhcp_mode,ip_str,mask_str,gw_str);
|
||||
printf("after configure:\n mode:%s\n ip:%s\n netmask:%s\n gateway:%s\n", dhcp_mode, ip_str, mask_str, gw_str);
|
||||
|
||||
__exit:
|
||||
if (reply) {
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int WifiDHCP(struct AdapterAT *adapter_at, bool enable)
|
||||
{
|
||||
int result = EOK;
|
||||
ATReplyType reply = NONE;
|
||||
char dhcp_status[4];
|
||||
memset(dhcp_status,0,sizeof(dhcp_status));
|
||||
if(enable)
|
||||
strcpy(dhcp_status,"on");
|
||||
else
|
||||
strcpy(dhcp_status,"off");
|
||||
|
||||
reply = CreateATReply(64);
|
||||
if (reply == NONE) {
|
||||
printf("no memory for reply struct.");
|
||||
return -ENOMEMORY;
|
||||
}
|
||||
|
||||
WifiInitAtCmd(adapter_at->agent);
|
||||
|
||||
char cmd[LEN_PARA_BUF];
|
||||
memset(cmd, 0, sizeof(cmd));
|
||||
strcpy(cmd, "AT+DHCPDEN=");
|
||||
strcat(cmd, dhcp_status);
|
||||
strcat(cmd, "\r");
|
||||
ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, reply, cmd);
|
||||
UserTaskDelay(2500);
|
||||
|
||||
ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
|
||||
UserTaskDelay(2500);
|
||||
|
||||
__exit:
|
||||
if (reply) {
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int WifiPing(struct AdapterAT *adapter_at, const char *destination,struct ping_result *ping_resp)
|
||||
{
|
||||
char *ping_result = NONE;
|
||||
char *dst = NONE;
|
||||
ping_result = (char *) UserCalloc(1, 17);
|
||||
dst = (char *) UserCalloc(1, 17);
|
||||
strcpy(dst, destination);
|
||||
strcat(dst, "\r");
|
||||
|
||||
WifiInitAtCmd(adapter_at->agent);
|
||||
|
||||
uint32 result = EOK;
|
||||
|
||||
ATReplyType reply = CreateATReply(64);
|
||||
if (NULL == reply) {
|
||||
printf("at_create_resp failed ! \n");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
printf("\\r = 0x%x, \\n = 0x%x\n", '\r', '\n');
|
||||
|
||||
//ping baidu.com
|
||||
uint32 err = ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, reply, "AT+PING=%s", dst);
|
||||
if (err) {
|
||||
printf("ATOrderSend(AT+PING)failed ! err = %d\n", err);
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
|
||||
UserTaskDelay(2000);
|
||||
|
||||
const char * result_buf = GetReplyText(reply);
|
||||
if (!result_buf) {
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
char* str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
|
||||
ParseATReply(str, "+ok=%s\r", ping_result);
|
||||
|
||||
printf("ping www.baidu.com(36.152.44.95) result is:%s\n", ping_result);
|
||||
|
||||
__exit:
|
||||
if (reply) {
|
||||
DeleteATReply(reply);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int WifiNetstat(struct AdapterAT *adapter_at)
|
||||
{
|
||||
#define HFA21_NETSTAT_RESP_SIZE 320
|
||||
#define HFA21_NETSTAT_TYPE_SIZE 10
|
||||
#define HFA21_NETSTAT_IPADDR_SIZE 17
|
||||
#define HFA21_WANN_EXPRESSION "+ok=%[^,],%[^,],%[^,],%[^,]\r"
|
||||
#define HFA21_LANN_EXPRESSION "+ok=%[^,],%[^,]\r"
|
||||
#define HFA21_WMODE_EXPRESSION "+ok=%s\r"
|
||||
|
||||
ATReplyType reply = NULL;
|
||||
struct ATAgent *agent = adapter_at->agent;
|
||||
uint32 result;
|
||||
char * result_buf = NULL;
|
||||
char * str = NULL;
|
||||
|
||||
/* sta/ap */
|
||||
char *work_mode = NULL;
|
||||
/* dhcp/static */
|
||||
char *ip_mode = NULL;
|
||||
char *local_ipaddr = NULL;
|
||||
char *gateway = NULL;
|
||||
char *netmask = NULL;
|
||||
local_ipaddr = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
gateway = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
netmask = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
work_mode = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
ip_mode = (char *) UserCalloc(1, HFA21_NETSTAT_IPADDR_SIZE);
|
||||
|
||||
reply = CreateATReply(HFA21_NETSTAT_RESP_SIZE);
|
||||
if (reply == NULL) {
|
||||
//printf("no memory for reply create.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "+++");
|
||||
UserTaskDelay(100);
|
||||
|
||||
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "a");
|
||||
UserTaskDelay(2500);
|
||||
|
||||
if (ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+WMODE\r") < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
result_buf = GetReplyText(reply);
|
||||
if(!result_buf) {
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
/* parse the third line of response data, get the network connection information */
|
||||
ParseATReply(str, HFA21_WMODE_EXPRESSION, work_mode);
|
||||
|
||||
if (work_mode[0]=='S') {
|
||||
if (ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+WANN\r") < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
result_buf = GetReplyText(reply);
|
||||
if (!result_buf) {
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
/* parse the third line of response data, get the network connection information */
|
||||
ParseATReply(str, HFA21_WANN_EXPRESSION, ip_mode, local_ipaddr, netmask, gateway);
|
||||
} else {
|
||||
if (ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+LANN\r") < 0) {
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
UserTaskDelay(2500);
|
||||
|
||||
result_buf = GetReplyText(reply);
|
||||
if (!result_buf) {
|
||||
printf("send_dhcp_at_cmd AT+ result_buf = NULL");
|
||||
result = -ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
str = strstr(result_buf, "+ok=");
|
||||
printf("str is:%s\n",str);
|
||||
/* parse the third line of response data, get the network connection information */
|
||||
ParseATReply(str, HFA21_LANN_EXPRESSION, local_ipaddr, netmask);
|
||||
}
|
||||
|
||||
ATOrderSend(adapter_at->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
|
||||
UserTaskDelay(2500);
|
||||
|
||||
printf("work mode: %s\n", work_mode);
|
||||
if (work_mode[0]=='S')
|
||||
printf("ip mode: %s\nlocal ip: %s\nnetmask: %s\ngateway: %s\n", ip_mode, local_ipaddr, netmask, gateway);
|
||||
else
|
||||
printf("local ip: %s\nnetmask: %s\n", local_ipaddr, netmask);
|
||||
|
||||
return EOK;
|
||||
|
||||
__exit:
|
||||
if (reply)
|
||||
DeleteATReply(reply);
|
||||
if (local_ipaddr)
|
||||
UserFree(local_ipaddr);
|
||||
if (netmask)
|
||||
UserFree(netmask);
|
||||
if (gateway)
|
||||
UserFree(gateway);
|
||||
if (work_mode)
|
||||
UserFree(work_mode);
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* 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 xs_adapter_at_wifi_register.c
|
||||
* @brief HFA21 wifi driver register to connection framework
|
||||
* @version 1.0
|
||||
* @author AIIT XUOS Lab
|
||||
* @date 2021.04.22
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <user_api.h>
|
||||
#include <xs_adapter_at_wifi.h>
|
||||
#include <xs_adapter_manager.h>
|
||||
#include <xs_adapter_at_agent.h>
|
||||
|
||||
const struct AdapterDone wifi_adapter_done =
|
||||
{
|
||||
WifiClose,
|
||||
WifiOpen,
|
||||
NULL,
|
||||
WifiSend,
|
||||
WifiReceive,
|
||||
NULL,
|
||||
};
|
||||
|
||||
const struct ATDone wifi_at_done =
|
||||
{
|
||||
WifiSetUp,
|
||||
WifiSetDown,
|
||||
WifiSetAddr,
|
||||
NULL,
|
||||
WifiDHCP,
|
||||
WifiPing,
|
||||
WifiNetstat,
|
||||
NULL,
|
||||
};
|
||||
|
||||
/**
|
||||
* @description: Register wifi device
|
||||
* @return success: EOK, failure: ERROR
|
||||
*/
|
||||
int RegisterAdapterWifi(void)
|
||||
{
|
||||
struct Adapterwifi *wifi_adapter = malloc(sizeof(struct Adapterwifi));
|
||||
if (wifi_adapter == NULL) {
|
||||
printf("out of memory\n");
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
struct AdapterAT *wifi_at_adapter = (struct AdapterAT *)wifi_adapter;
|
||||
struct Adapter *adapter = (struct Adapter *)wifi_adapter;
|
||||
|
||||
wifi_adapter->parent.atdone = wifi_at_done;
|
||||
wifi_adapter->parent.parent.done = wifi_adapter_done;
|
||||
|
||||
wifi_at_adapter->at_adapter_id = WIFI_ADAPTER_ID;
|
||||
|
||||
ATAdapterInit();
|
||||
ATAdapterRegister(wifi_at_adapter);
|
||||
|
||||
return EOK;
|
||||
}
|
||||
7
framework/connection/Adapter/zigbee/Kconfig
Normal file
7
framework/connection/Adapter/zigbee/Kconfig
Normal file
@@ -0,0 +1,7 @@
|
||||
menuconfig CONNECTION_COMMUNICATION_ZIGBEE_AIIT
|
||||
bool "Enable zigbee for AIIT Board"
|
||||
default n
|
||||
|
||||
menuconfig CONNECTION_COMMUNICATION_ZIGBEE_KD233
|
||||
bool "Enable zigbee for kd233"
|
||||
default n
|
||||
3
framework/connection/Adapter/zigbee/Makefile
Normal file
3
framework/connection/Adapter/zigbee/Makefile
Normal file
@@ -0,0 +1,3 @@
|
||||
SRC_FILES := xs_adapter_zigbee.c xs_adaper_zigbee_register.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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: xs_AdaperZigbee_register.c
|
||||
* @brief: register zigbee in initialization
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2020/4/25
|
||||
*
|
||||
*/
|
||||
#include <xs_adapter_zigbee.h>
|
||||
#include <xs_adapter_manager.h>
|
||||
|
||||
/* initialize to the register list*/
|
||||
int RegisterAdapterZigbee(void)
|
||||
{
|
||||
static struct AdapterZigbee zigbee_adapter;
|
||||
memset(&zigbee_adapter, 0, sizeof(zigbee_adapter));
|
||||
|
||||
static struct AdapterDone zigbee_send_done = {
|
||||
.NetAiitOpen = ZigbeeOpen,
|
||||
.NetAiitClose = ZigbeeClose,
|
||||
.NetAiitSend = ZigbeeSend,
|
||||
.NetAiitReceive = ZigbeeReceive,
|
||||
.NetAiitJoin = NULL,
|
||||
.NetAiitIoctl = NULL,
|
||||
};
|
||||
zigbee_adapter.parent.done = zigbee_send_done;
|
||||
zigbee_adapter.name = "zigbee";
|
||||
|
||||
ZigbeeAdapterInit();
|
||||
ZigbeeAdapterRegister((adapter_t)&zigbee_adapter);
|
||||
|
||||
return EOK;
|
||||
}
|
||||
170
framework/connection/Adapter/zigbee/xs_adapter_zigbee.c
Normal file
170
framework/connection/Adapter/zigbee/xs_adapter_zigbee.c
Normal file
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* 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: xs_AdapterZigbee.c
|
||||
* @brief: zigbee open close function
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2020/4/25
|
||||
*
|
||||
*/
|
||||
|
||||
#include "xs_adapter_zigbee.h"
|
||||
#include "../applications/user_api/switch_api/user_api.h"
|
||||
#include "../applications/user_api/include/bus_serial.h"
|
||||
#include "../applications/user_api/include/dev_serial.h"
|
||||
|
||||
#ifdef CONNECTION_COMMUNICATION_ZIGBEE_AIIT
|
||||
#define SAMPLE_UART_NAME "/dev/extuart_dev0"
|
||||
int use_aiit = 1;
|
||||
#endif
|
||||
#ifdef CONNECTION_COMMUNICATION_ZIGBEE_KD233
|
||||
#define SAMPLE_UART_NAME "/dev/uart3_dev3"
|
||||
int use_aiit = 0;
|
||||
#endif
|
||||
|
||||
|
||||
static int serial_fd;
|
||||
static int32_t zigbeereceive;
|
||||
static int rx_sem;
|
||||
char zigbee_buffer[NAME_NUM_MAX ]={0};
|
||||
|
||||
/* initialize srial port to open zigbee*/
|
||||
int ZigbeeOpen(struct Adapter *padapter)
|
||||
{
|
||||
|
||||
/* Open device in read-write mode */
|
||||
serial_fd = open(SAMPLE_UART_NAME,O_RDWR);
|
||||
|
||||
/* set serial config, serial_baud_rate = 115200 */
|
||||
|
||||
struct SerialDataCfg cfg;
|
||||
cfg.serial_baud_rate = BAUD_RATE_115200;
|
||||
cfg.serial_data_bits = DATA_BITS_8;
|
||||
cfg.serial_stop_bits = STOP_BITS_1;
|
||||
cfg.serial_parity_mode = PARITY_NONE;
|
||||
cfg.serial_bit_order = 0;
|
||||
cfg.serial_invert_mode = 0;
|
||||
cfg.serial_buffer_size = 128;
|
||||
|
||||
/*aiit board use ch438, so it nees more serial configuration*/
|
||||
if (use_aiit==1){
|
||||
cfg.ext_uart_no = 0;
|
||||
cfg.port_configure = 0;
|
||||
}
|
||||
|
||||
ioctl(serial_fd, OPE_INT, &cfg);
|
||||
UserTaskDelay(1000);
|
||||
KPrintf("Zigbee ready\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* send message to srial port*/
|
||||
int ZigbeeSend(struct Adapter *padapter, const char* data, int len, bool block, int time_out, int delay, send_success cb, void* param, void* reserved)
|
||||
{
|
||||
write(serial_fd,data,strlen(data));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*thread to read message from srial port*/
|
||||
void SerialThreadEntry(void *parameter)
|
||||
{
|
||||
char ch;
|
||||
int i = 0;
|
||||
int n;
|
||||
int run = 0;
|
||||
while (1){
|
||||
n = read(serial_fd,&ch,1);
|
||||
if (n>0){
|
||||
if (ch == '~'){
|
||||
UserSemaphoreAbandon(rx_sem);
|
||||
run = 1;
|
||||
break;
|
||||
}
|
||||
zigbee_buffer[i++] = ch;
|
||||
}
|
||||
if (run ==1)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int ZigbeeReceive(struct Adapter *padapter, char* rev_buffer, int buffer_len,int time_out, bool block, void* reserved)
|
||||
{
|
||||
|
||||
x_err_t ret = EOK;
|
||||
/* Set callback function */
|
||||
/* Create thread serial */
|
||||
utask_x recv;
|
||||
recv.name[0] = 'z';
|
||||
recv.func_entry = SerialThreadEntry;
|
||||
recv.func_param = NONE;
|
||||
recv.stack_size = 1024;
|
||||
recv.prio = 25;
|
||||
memset(zigbee_buffer, 0, sizeof(zigbee_buffer));
|
||||
|
||||
/* Initialize semaphore */
|
||||
rx_sem = UserSemaphoreCreate(0);
|
||||
|
||||
zigbeereceive = UserTaskCreate(recv);
|
||||
UserTaskStartup(zigbeereceive);
|
||||
|
||||
/*copy to the receive buffer*/
|
||||
UserSemaphoreObtain(rx_sem,-1);
|
||||
memcpy(rev_buffer,zigbee_buffer,strlen(zigbee_buffer)+1 );
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
void ZigbeeSettingDemo(int argc, char *argv[])
|
||||
{
|
||||
/*zigbee communication settings*/
|
||||
/*it can be changed if needed*/
|
||||
char *set0 = "+++";
|
||||
char *set1_1 = "AT+DEV=C"; /*set device type for coordinater*/
|
||||
char *set1_2 = "AT+DEV=E"; /*set device type for end device*/
|
||||
char *set2 = "AT+MODE=1"; /*device mode 1 : passthrough */
|
||||
char *set3 = "AT+PANID=15D8"; /* set PANID*/
|
||||
char *set4 = "AT+CH=11"; /* set channel*/
|
||||
char *set5 = "AT+EXIT"; /* exit AT mode*/
|
||||
write(serial_fd,set0,strlen(set0));
|
||||
|
||||
/*type something in the command line to set this zigbee as coordinater*/
|
||||
/*otherwise it is an end device*/
|
||||
if (argc == 2){
|
||||
write(serial_fd,set1_1,strlen(set1_1));
|
||||
UserTaskDelay(500); /*zigbee needs some time to process input message*/
|
||||
}else{
|
||||
write(serial_fd,set1_2,strlen(set1_2));
|
||||
UserTaskDelay(500);
|
||||
}
|
||||
write(serial_fd,set2,strlen(set2));
|
||||
UserTaskDelay(500);
|
||||
write(serial_fd,set3,strlen(set3));
|
||||
UserTaskDelay(500);
|
||||
write(serial_fd,set4,strlen(set4));
|
||||
UserTaskDelay(500);
|
||||
write(serial_fd,set5,strlen(set5));
|
||||
UserTaskDelay(500);
|
||||
KPrintf("zigbee setting success!\n");
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),
|
||||
ZigbeeSettingDemo, ZigbeeSettingDemo, zigbee send function );
|
||||
|
||||
|
||||
void ZigbeeClose(struct Adapter *padapter)
|
||||
{
|
||||
UserTaskDelete(zigbeereceive);
|
||||
close(serial_fd);
|
||||
}
|
||||
11
framework/connection/Kconfig
Normal file
11
framework/connection/Kconfig
Normal file
@@ -0,0 +1,11 @@
|
||||
menu "connection"
|
||||
|
||||
menuconfig CONNECTION_ADAPTER
|
||||
bool "Enable adapter"
|
||||
default n
|
||||
if CONNECTION_ADAPTER
|
||||
source "$KERNEL_DIR/framework/connection/Adapter/Kconfig"
|
||||
endif
|
||||
|
||||
|
||||
endmenu
|
||||
5
framework/connection/Makefile
Normal file
5
framework/connection/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
ifeq ($(CONFIG_CONNECTION_ADAPTER), y)
|
||||
SRC_DIR += Adapter
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
Reference in New Issue
Block a user