Browse Source

wifi adapter on XIUOS/aiit-arm32-board

it is OK
pull/3/head
xuedongliang 1 year ago
parent
commit
fde280b6a0
  1. 3
      APP_Framework/Applications/connection_app/Makefile
  2. 150
      APP_Framework/Applications/connection_app/wifi_demo/wifi_demo.c
  3. 58
      APP_Framework/Applications/framework_init.c
  4. 2
      APP_Framework/Framework/connection/Makefile
  5. 381
      APP_Framework/Framework/connection/adapter.c
  6. 32
      APP_Framework/Framework/connection/adapter.h
  7. 471
      APP_Framework/Framework/connection/adapter_agent.c
  8. 82
      APP_Framework/Framework/connection/at_agent.h
  9. 33
      APP_Framework/Framework/connection/wifi/HFA21/Kconfig
  10. 3
      APP_Framework/Framework/connection/wifi/HFA21/Makefile
  11. 516
      APP_Framework/Framework/connection/wifi/HFA21/hfa21.c
  12. 14
      APP_Framework/Framework/connection/wifi/Kconfig
  13. 4
      APP_Framework/Framework/connection/wifi/Makefile
  14. 156
      APP_Framework/Framework/connection/wifi/adapter_wifi.c
  15. 9
      APP_Framework/Framework/transform_layer/xiuos/transform.c
  16. 2
      Ubiquitous/XiUOS/framework/Makefile

3
APP_Framework/Applications/connection_app/Makefile

@ -0,0 +1,3 @@
SRC_DIR :=wifi_demo
include $(KERNEL_ROOT)/compiler.mk

150
APP_Framework/Applications/connection_app/wifi_demo/wifi_demo.c

@ -18,136 +18,179 @@
* @date 2021.04.22
*/
#include <string.h>
#include <user_api.h>
#include <xs_adapter_manager.h>
#include <xs_adapter_at_wifi.h>
#include <at_agent.h>
#include <adapter.h>
void SendWiftMsg(int argc, char *argv[])
{
char wifi_msg[128];
int len;
if (argc >= 1) {
memset(wifi_msg, 0, 128);
strncpy(wifi_msg, argv[1], strlen(argv[1]));
strncpy(wifi_msg, argv[1], (len = strlen(argv[1])));
printf("SendWiftMsg(%s).\n", wifi_msg);
}
struct AdapterAT *at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
AdapterDeviceOpen(adapter);
at_adapter->parent.done.NetAiitSend(&at_adapter->parent, wifi_msg, strlen(wifi_msg), true, 1000, 0, NULL, NULL, NULL);
AdapterDeviceSend(adapter, wifi_msg, len);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(0), SendWiftMsg, SendWiftMsg, SendWiftMsg);
void RecvWifiMsg()
{
char wifi_recv_msg[128];
memset(wifi_recv_msg, 0, sizeof(wifi_recv_msg));
struct AdapterAT *at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
AdapterDeviceOpen(adapter);
while (1) {
memset(wifi_recv_msg, 0, sizeof(wifi_recv_msg));
if (EOK == at_adapter->parent.done.NetAiitReceive(&at_adapter->parent, wifi_recv_msg, 128, 40000, true, NULL)) {
if (EOK == AdapterDeviceRecv(adapter, wifi_recv_msg, 128)) {
printf("wifi_recv_msg (%s)\n", wifi_recv_msg);
} else {
printf("wifi_recv_msg failed .\n");
}
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), RecvWifiMsg, RecvWifiMsg, RecvWifiMsg);
void SetAddrWifi()
void SetUpWifi()
{
char wifi_recv_msg[128];
int baud_rate = BAUD_RATE_57600;
memset(wifi_recv_msg, 0, sizeof(wifi_recv_msg));
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
printf("Waiting for msg...\n");
AdapterDeviceOpen(adapter);
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
if (EOK == AdapterDeviceSetUp(adapter)) {
printf("SetUpWifi success \n");
} else {
printf("SetUpWifi failed \n");
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), SetUpWifi, SetUpWifi, SetUpWifi);
void SetDownWifi()
{
struct AdapterAT* at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
char wifi_recv_msg[128];
memset(wifi_recv_msg, 0, sizeof(wifi_recv_msg));
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
printf("Waiting for msg...\n");
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
AdapterDeviceOpen(adapter);
if(EOK != at_adapter->atdone.ATOperateAddr(at_adapter, IpTint("10.10.100.222"), IpTint("255.255.255.0"), IpTint("255.255.255.0")))
printf("WifiSetAddr failed \n");
if (EOK == AdapterDeviceSetDown(adapter)) {
printf("SetDownWifi success \n");
} else {
printf("SetDownWifi failed \n");
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), SetDownWifi, SetDownWifi, SetDownWifi);
void DhcpWifi()
void SetAddrWifi()
{
struct AdapterAT* at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
printf("Waiting for msg...\n");
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
AdapterDeviceOpen(adapter);
if(EOK != at_adapter->atdone.ATOperateDHCP(at_adapter,1))
printf("WifiDHCP failed \n");
if(EOK == AdapterDeviceSetAddr(adapter, "192.168.66.253", "255.255.255.0", "192.168.66.1")){
printf("SetAddrWifi success \n");
} else {
printf("SetAddrWifi failed \n");
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), SetAddrWifi, SetAddrWifi, SetAddrWifi);
void PingWifi()
{
char wifi_recv_msg[128];
memset(wifi_recv_msg, 0, sizeof(wifi_recv_msg));
struct AdapterAT *at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
printf("Waiting for msg...\n");
struct PingResult result;
//www.baidu.com
char *ip_str = "36.152.44.95";
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
AdapterDeviceOpen(adapter);
at_adapter->atdone.ATPing(at_adapter, ip_str, &result);
if (EOK == AdapterDevicePing(adapter, ip_str)) {
printf("PingWifi success \n");
} else {
printf("PingWifi failed \n");
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), PingWifi, PingWifi, PingWifi);
void SetUpWifi()
void NetstatWifi()
{
char wifi_recv_msg[128];
memset(wifi_recv_msg, 0, sizeof(wifi_recv_msg));
struct AdapterAT* at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
printf("Waiting for msg...\n");
struct PingResult result;
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
AdapterDeviceOpen(adapter);
if (EOK == at_adapter->atdone.ATOperateUp(at_adapter)) {
printf("WifiSetUp success (%s)\n", result.ip_addr.ipv4);
if (EOK == AdapterDeviceNetstat(adapter)) {
printf("NetstatWifi success \n");
} else {
printf("WifiSetUp failed \n");
printf("NetstatWifi failed \n");
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), NetstatWifi, NetstatWifi, NetstatWifi);
void NetstatWifi()
int ConnectWifi()
{
struct AdapterAT* at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
const char *ip = "192.168.66.33";
const char *port = "12345";
enum NetRoleType net_role = CLIENT;
enum IpType ip_type = IPV4;
if (!adapter) {
printf("ATAdapterFind failed .\n");
}
printf("Waiting for msg...\n");
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
AdapterDeviceOpen(adapter);
if (EOK != at_adapter->atdone.ATNetstat(at_adapter))
printf("WifiNetstat failed \n");
if (EOK == AdapterDeviceConnect(adapter, net_role, ip, port, ip_type)) {
printf("ConnectWifi success \n");
} else {
printf("ConnectWifi failed \n");
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0), ConnectWifi, ConnectWifi, ConnectWifi);
void AtTestCmdWifi(int argc, char *argv[])
{
@ -159,21 +202,22 @@ void AtTestCmdWifi(int argc, char *argv[])
}
strcat(cmd,"\r");
struct AdapterAT* at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
// AdapterDeviceOpen(adapter);
printf("Waiting for msg...\n");
ATOrderSend(at_adapter->agent, REPLY_TIME_OUT, NULL, "+++");
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "+++");
UserTaskDelay(100);
ATOrderSend(at_adapter->agent, REPLY_TIME_OUT, NULL, "a");
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "a");
UserTaskDelay(2500);
ATOrderSend(at_adapter->agent,REPLY_TIME_OUT, NULL,cmd);
ATOrderSend(adapter->agent,REPLY_TIME_OUT, NULL,cmd);
UserTaskDelay(2500);
ATOrderSend(at_adapter->agent,REPLY_TIME_OUT, NULL,"AT+Z\r");
ATOrderSend(adapter->agent,REPLY_TIME_OUT, NULL,"AT+Z\r");
UserTaskDelay(5000);
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(0), AtTestCmdWifi, AtTestCmdWifi, AtTestCmdWifi);

58
APP_Framework/Applications/framework_init.c

@ -14,12 +14,10 @@
#include <user_api.h>
extern int SensorFrameworkInit(void);
extern int RegisterAdapterEthernet(void);
extern int RegisterAdapterWifi(void);
extern int RegisterAdapterZigbee(void);
extern int RegisterAdapterNBIoT(void);
extern int RegisterAdapterBluetooth(void);
extern int LoraSx12xxSpiDeviceInit();
extern int AdapterFrameworkInit(void);
extern int Adapter4GInit(void);
extern int AdapterWifiInit(void);
extern int D124VoiceInit(void);
extern int Hs300xTemperatureInit(void);
@ -54,6 +52,10 @@ static struct InitDesc framework[] =
{ "perception_framework", SensorFrameworkInit },
#endif
#ifdef SUPPORT_CONNECTION_FRAMEWORK
{ "connection_framework", AdapterFrameworkInit },
#endif
{ "NULL", NULL },
};
@ -87,27 +89,11 @@ static struct InitDesc perception_desc[] =
static struct InitDesc connection_desc[] =
{
#ifdef CONNECTION_COMMUNICATION_ETHERNET
{ "ethernet adpter", RegisterAdapterEthernet },
#endif
#ifdef CONNECTION_COMMUNICATION_WIFI
{ "wifi adpter", RegisterAdapterWifi },
#endif
#ifdef CONNECTION_COMMUNICATION_LORA
{ "lora adpter", LoraSx12xxSpiDeviceInit},
#endif
#ifdef CONNECTION_COMMUNICATION_ZIGBEE
{ "zigbee adpter", RegisterAdapterZigbee},
#ifdef ADAPTER_4G
{ "4G adpter", Adapter4GInit},
#endif
#ifdef CONNECTION_COMMUNICATION_NB_IOT
{ "NB-IoT adpter", RegisterAdapterNBIoT},
#endif
#ifdef CONNECTION_COMMUNICATION_BLUETOOTH
{ "bluetooth adpter", RegisterAdapterBluetooth},
#ifdef ADAPTER_WIFI
{ "Wifi adpter", AdapterWifiInit},
#endif
{ "NULL", NULL },
};
@ -132,6 +118,8 @@ static int PerceptionFrameworkInit(struct InitDesc sub_desc[])
printf("initialize perception_framework success.\n");
AppInitDesc(perception_desc);
}
return ret;
}
/**
@ -141,7 +129,21 @@ static int PerceptionFrameworkInit(struct InitDesc sub_desc[])
*/
static int ConnectionFrameworkInit(struct InitDesc sub_desc[])
{
return AppInitDesc(connection_desc);
int i = 0;
int ret = 0;
for ( i = 0; sub_desc[i].fn != NULL; i++ ) {
if (0 == strncmp(sub_desc[i].fn_name, "connection_framework", strlen("connection_framework"))) {
ret = sub_desc[i].fn();
break;
}
}
if (0 == ret) {
printf("initialize connection_framework success.\n");
AppInitDesc(connection_desc);
}
return ret;
}
/**
@ -154,7 +156,7 @@ int FrameworkInit()
PerceptionFrameworkInit(framework);
#endif
#ifdef CONNECTION_ADAPTER
#ifdef SUPPORT_CONNECTION_FRAMEWORK
ConnectionFrameworkInit(framework);
#endif

2
APP_Framework/Framework/connection/Makefile

@ -1,4 +1,4 @@
SRC_FILES := adapter.c
SRC_FILES := adapter.c adapter_agent.c
ifeq ($(CONFIG_ADAPTER_LORA),y)
SRC_DIR += lora

381
APP_Framework/Framework/connection/adapter.c

@ -62,6 +62,7 @@ AdapterType AdapterDeviceFindByName(const char *name)
ret = adapter;
break;
}
printf("PrivMutexObtain in loop\n");
}
PrivMutexAbandon(&adapter_list_lock);
@ -115,10 +116,8 @@ int AdapterDeviceUnregister(struct Adapter *adapter)
* @param name - adapter device name
* @return success: 0 , failure: other
*/
int AdapterDeviceOpen(const char *name)
int AdapterDeviceOpen(struct Adapter *adapter)
{
struct Adapter *adapter = AdapterDeviceFindByName(name);
if (!adapter)
return -1;
@ -235,6 +234,68 @@ int AdapterDeviceClose(struct Adapter *adapter)
return result;
}
/**
* @description: Configure adapter device
* @param adapter - adapter device pointer
* @param cmd - command
* @param args - command parameter
* @return success: 0 , failure: other
*/
int AdapterDeviceControl(struct Adapter *adapter, int cmd, void *args)
{
if (!adapter)
return -1;
int result = 0;
struct IpProtocolDone *ip_done = NULL;
struct PrivProtocolDone *priv_done = NULL;
switch (adapter->net_protocol)
{
case PRIVATE_PROTOCOL:
priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->ioctl)
return 0;
result = priv_done->ioctl(adapter, cmd, args);
if (0 == result) {
printf("Device %s ioctl success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s ioctl failed(%d).\n", adapter->name, result);
}
break;
case IP_PROTOCOL:
ip_done = (struct IpProtocolDone *)adapter->done;
if (NULL == ip_done->ioctl)
return 0;
result = ip_done->ioctl(adapter, cmd, args);
if (0 == result) {
printf("Device %s ioctl success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s ioctl failed(%d).\n", adapter->name, result);
}
break;
default:
break;
}
return result;
}
/**
* @description: Receice data from adapter
* @param adapter - adapter device pointer
@ -299,38 +360,6 @@ ssize_t AdapterDeviceSend(struct Adapter *adapter, const void *src, size_t len)
}
}
/**
* @description: Configure adapter device
* @param adapter - adapter device pointer
* @param cmd - command
* @param args - command parameter
* @return success: 0 , failure: other
*/
int AdapterDeviceControl(struct Adapter *adapter, int cmd, void *args)
{
if (!adapter)
return -1;
if (PRIVATE_PROTOCOL == adapter->net_protocol) {
struct PrivProtocolDone *priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->ioctl)
return -1;
return priv_done->ioctl(adapter, cmd, args);
} else if (IP_PROTOCOL == adapter->net_protocol) {
struct IpProtocolDone *ip_done = (struct IpProtocolDone *)adapter->done;
if (NULL == ip_done->ioctl)
return -1;
return ip_done->ioctl(adapter, cmd, args);
} else {
printf("AdapterDeviceControl net_protocol %d not support\n", adapter->net_protocol);
return -1;
}
}
/**
* @description: Connect to a certain ip net, only support IP_PROTOCOL
* @param adapter - adapter device pointer
@ -339,7 +368,7 @@ int AdapterDeviceControl(struct Adapter *adapter, int cmd, void *args)
* @param ip_type - ip type, IPV4 or IPV6
* @return success: 0 , failure: other
*/
int AdapterDeviceConnect(struct Adapter *adapter, const char *ip, const char *port, enum IpType ip_type)
int AdapterDeviceConnect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
{
if (!adapter)
return -1;
@ -353,7 +382,7 @@ int AdapterDeviceConnect(struct Adapter *adapter, const char *ip, const char *po
if (NULL == ip_done->connect)
return -1;
return ip_done->connect(adapter, ip, port, ip_type);
return ip_done->connect(adapter, net_role, ip, port, ip_type);
} else {
printf("AdapterDeviceConnect net_protocol %d not support\n", adapter->net_protocol);
return -1;
@ -379,7 +408,7 @@ int AdapterDeviceJoin(struct Adapter *adapter, const char *priv_net_group)
return priv_done->join(adapter, priv_net_group);
} else if (IP_PROTOCOL == adapter->net_protocol) {
printf("AdapterDeviceJoin not suuport ip_protocol, please use connect\n");
printf("AdapterDeviceJoin not support ip_protocol, please use connect\n");
return -1;
} else {
printf("AdapterDeviceJoin net_protocol %d not support\n", adapter->net_protocol);
@ -416,3 +445,279 @@ int AdapterDeviceDisconnect(struct Adapter *adapter)
return -1;
}
}
int AdapterDeviceSetUp(struct Adapter *adapter)
{
if (!adapter)
return -1;
int result = 0;
struct IpProtocolDone *ip_done = NULL;
struct PrivProtocolDone *priv_done = NULL;
switch (adapter->net_protocol)
{
case PRIVATE_PROTOCOL:
priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->setup)
return 0;
result = priv_done->setup(adapter);
if (0 == result) {
printf("Device %s setup success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s setup failed(%d).\n", adapter->name, result);
}
break;
case IP_PROTOCOL:
ip_done = (struct IpProtocolDone *)adapter->done;
if (NULL == ip_done->setup)
return 0;
result = ip_done->setup(adapter);
if (0 == result) {
printf("Device %s setup success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s setup failed(%d).\n", adapter->name, result);
}
break;
default:
break;
}
return result;
}
int AdapterDeviceSetDown(struct Adapter *adapter)
{
if (!adapter)
return -1;
int result = 0;
struct IpProtocolDone *ip_done = NULL;
struct PrivProtocolDone *priv_done = NULL;
switch (adapter->net_protocol)
{
case PRIVATE_PROTOCOL:
priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->setdown)
return 0;
result = priv_done->setdown(adapter);
if (0 == result) {
printf("Device %s setdown success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s setdown failed(%d).\n", adapter->name, result);
}
break;
case IP_PROTOCOL:
ip_done = (struct IpProtocolDone *)adapter->done;
if (NULL == ip_done->setdown)
return 0;
result = ip_done->setdown(adapter);
if (0 == result) {
printf("Device %s setdown success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s setdown failed(%d).\n", adapter->name, result);
}
break;
default:
break;
}
return result;
}
int AdapterDeviceSetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask)
{
if (!adapter)
return -1;
int result = 0;
struct IpProtocolDone *ip_done = NULL;
struct PrivProtocolDone *priv_done = NULL;
switch (adapter->net_protocol)
{
case PRIVATE_PROTOCOL:
priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->setaddr)
return 0;
result = priv_done->setaddr(adapter, ip, gateway, netmask);
if (0 == result) {
printf("Device %s setaddr success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s setaddr failed(%d).\n", adapter->name, result);
}
break;
case IP_PROTOCOL:
ip_done = (struct IpProtocolDone *)adapter->done;
if (NULL == ip_done->setaddr)
return 0;
result = ip_done->setaddr(adapter, ip, gateway, netmask);
if (0 == result) {
printf("Device %s setaddr success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s setaddr failed(%d).\n", adapter->name, result);
}
break;
default:
break;
}
return result;
}
int AdapterDevicePing(struct Adapter *adapter, const char *destination)
{
if (!adapter)
return -1;
int result = 0;
struct IpProtocolDone *ip_done = NULL;
struct PrivProtocolDone *priv_done = NULL;
switch (adapter->net_protocol)
{
case PRIVATE_PROTOCOL:
priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->ping)
return 0;
result = priv_done->ping(adapter, destination);
if (0 == result) {
printf("Device %s ping success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s ping failed(%d).\n", adapter->name, result);
}
break;
case IP_PROTOCOL:
ip_done = (struct IpProtocolDone *)adapter->done;
if (NULL == ip_done->ping)
return 0;
result = ip_done->ping(adapter, destination);
if (0 == result) {
printf("Device %s ping success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s ping failed(%d).\n", adapter->name, result);
}
break;
default:
break;
}
return result;
}
int AdapterDeviceNetstat(struct Adapter *adapter)
{
if (!adapter)
return -1;
int result = 0;
struct IpProtocolDone *ip_done = NULL;
struct PrivProtocolDone *priv_done = NULL;
switch (adapter->net_protocol)
{
case PRIVATE_PROTOCOL:
priv_done = (struct PrivProtocolDone *)adapter->done;
if (NULL == priv_done->netstat)
return 0;
result = priv_done->netstat(adapter);
if (0 == result) {
printf("Device %s netstat success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s netstat failed(%d).\n", adapter->name, result);
}
break;
case IP_PROTOCOL:
ip_done = (struct IpProtocolDone *)adapter->done;
if (NULL == ip_done->netstat)
return 0;
result = ip_done->netstat(adapter);
if (0 == result) {
printf("Device %s netstat success.\n", adapter->name);
adapter->adapter_status = INSTALL;
} else {
if (adapter->fd) {
PrivClose(adapter->fd);
adapter->fd = 0;
}
printf("Device %s netstat failed(%d).\n", adapter->name, result);
}
break;
default:
break;
}
return result;
}

32
APP_Framework/Framework/connection/adapter.h

@ -27,6 +27,7 @@
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <at_agent.h>
#ifdef __cplusplus
extern "C" {
@ -58,6 +59,8 @@ struct AdapterProductInfo;
typedef struct Adapter *AdapterType;
typedef struct AdapterProductInfo *AdapterProductInfoType;
#define ADAPTER_WIFI_NAME "wifi"
struct Socket
{
int id;
@ -118,7 +121,14 @@ struct IpProtocolDone
int (*open)(struct Adapter *adapter);
int (*close)(struct Adapter *adapter);
int (*ioctl)(struct Adapter *adapter, int cmd, void *args);
int (*connect)(struct Adapter *adapter, const char *ip, const char *port, enum IpType ip_type);
int (*setup)(struct Adapter *adapter);
int (*setdown)(struct Adapter *adapter);
int (*setaddr)(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask);
int (*setdns)(struct Adapter *adapter, const char *dns_addr, uint8 dns_count);
int (*setdhcp)(struct Adapter *adapter, int enable);
int (*ping)(struct Adapter *adapter, const char *destination);
int (*netstat)(struct Adapter *adapter);
int (*connect)(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type);
int (*send)(struct Adapter *adapter, const void *buf, size_t len);
int (*recv)(struct Adapter *adapter, void *buf, size_t len);
int (*disconnect)(struct Adapter *adapter);
@ -129,6 +139,13 @@ struct PrivProtocolDone
int (*open)(struct Adapter *adapter);
int (*close)(struct Adapter *adapter);
int (*ioctl)(struct Adapter *adapter, int cmd, void *args);
int (*setup)(struct Adapter *adapter);
int (*setdown)(struct Adapter *adapter);
int (*setaddr)(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask);
int (*setdns)(struct Adapter *adapter, const char *dns_addr, uint8 dns_count);
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 (*send)(struct Adapter *adapter, const void *buf, size_t len);
int (*recv)(struct Adapter *adapter, void *buf, size_t len);
@ -142,6 +159,7 @@ struct Adapter
int product_info_flag;
struct AdapterProductInfo *info;
ATAgentType agent;
//struct Socket *socket;
@ -169,7 +187,7 @@ int AdapterDeviceRegister(struct Adapter *adapter);
int AdapterDeviceUnregister(struct Adapter *adapter);
/*Open adapter device*/
int AdapterDeviceOpen(const char *name);
int AdapterDeviceOpen(struct Adapter *adapter);
/*Close adapter device*/
int AdapterDeviceClose(struct Adapter *adapter);
@ -184,7 +202,7 @@ ssize_t AdapterDeviceSend(struct Adapter *adapter, const void *src, size_t len);
int AdapterDeviceControl(struct Adapter *adapter, int cmd, void *args);
/*Connect to a certain ip net, only support IP_PROTOCOL*/
int AdapterDeviceConnect(struct Adapter *adapter, const char *ip, const char *port, enum IpType ip_type);
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);
@ -192,6 +210,14 @@ int AdapterDeviceJoin(struct Adapter *adapter, const char *priv_net_group);
/*Adapter disconnect from ip net or private net group*/
int AdapterDeviceDisconnect(struct Adapter *adapter);
int AdapterDeviceSetUp(struct Adapter *adapter);
int AdapterDeviceSetDown(struct Adapter *adapter);
int AdapterDeviceSetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask);
int AdapterDeviceSetDns(struct Adapter *adapter, const char *dns_addr, uint8 dns_count);
int AdapterDeviceSetDhcp(struct Adapter *adapter, int enable);
int AdapterDevicePing(struct Adapter *adapter, const char *destination);
int AdapterDeviceNetstat(struct Adapter *adapter);
#ifdef __cplusplus
}
#endif

471
APP_Framework/Framework/connection/adapter_agent.c

@ -0,0 +1,471 @@
/*
* 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 <at_agent.h>
#include <adapter.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);
printf("ATSprintf send %s\n",send_buf);
PrivWrite(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;
PrivMutexObtain(&agent->lock);
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;
PrivMutexAbandon(&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);
return EOK;
}
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){
PrivMutexDelete(&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;
UtaskType 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;
}
if(PrivMutexCreate(&agent->lock, 0) < 0) {
printf("AdapterFrameworkInit mutex create failed.\n");
}
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);
// 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);
DeleteATAgent(agent);
result = -ERROR;
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;
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];
agent->fd = agent_fd;
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;
}
}

82
APP_Framework/Framework/connection/at_agent.h

@ -0,0 +1,82 @@
/*
* 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 <list.h>
#include <transform.h>
#include <stdio.h>
#include <sys/types.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[64];
int fd;
char *maintain_buffer;
uint32 maintain_len;
uint32 maintain_max;
int 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, int fd, uint32 maintain_max);
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

33
APP_Framework/Framework/connection/wifi/HFA21/Kconfig

@ -0,0 +1,33 @@
config ADAPTER_WIFI_HFA21
string "HFA21 adapter name"
default "hfa21"
if ADD_XIUOS_FETURES
config ADAPTER_HFA21_DRIVER_EXTUART
bool "Using extra uart to support wifi"
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

3
APP_Framework/Framework/connection/wifi/HFA21/Makefile

@ -0,0 +1,3 @@
SRC_FILES := hfa21.c
include $(KERNEL_ROOT)/compiler.mk

516
APP_Framework/Framework/connection/wifi/HFA21/hfa21.c

@ -0,0 +1,516 @@
/*
* 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.
*/