wifi adapter on XIUOS/aiit-arm32-board

it is OK
This commit is contained in:
xuedongliang 2021-07-30 09:54:14 +08:00
commit fde280b6a0
16 changed files with 1800 additions and 146 deletions

View File

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

View File

@ -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 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()
{
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");
AdapterDeviceOpen(adapter);
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 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.ATOperateAddr(at_adapter, IpTint("10.10.100.222"), IpTint("255.255.255.0"), IpTint("255.255.255.0")))
printf("WifiSetAddr failed \n");
}
void DhcpWifi()
{
struct AdapterAT* at_adapter = ATAdapterFind(WIFI_ADAPTER_ID);
if (!at_adapter) {
printf("ATAdapterFind 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");
}
printf("Waiting for msg...\n");
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
if(EOK != at_adapter->atdone.ATOperateDHCP(at_adapter,1))
printf("WifiDHCP 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);
}
void SetUpWifi()
{
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) {
printf("ATAdapterFind failed .\n");
}
printf("Waiting for msg...\n");
struct PingResult result;
at_adapter->parent.done.NetAiitOpen(&at_adapter->parent);
if (EOK == at_adapter->atdone.ATOperateUp(at_adapter)) {
printf("WifiSetUp success (%s)\n", result.ip_addr.ipv4);
if (EOK == AdapterDevicePing(adapter, ip_str)) {
printf("PingWifi success \n");
} else {
printf("WifiSetUp failed \n");
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 NetstatWifi()
{
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.ATNetstat(at_adapter))
printf("WifiNetstat failed \n");
if (EOK == AdapterDeviceNetstat(adapter)) {
printf("NetstatWifi success \n");
} else {
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);
int ConnectWifi()
{
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");
AdapterDeviceOpen(adapter);
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);

View File

@ -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 },
#ifdef ADAPTER_4G
{ "4G adpter", Adapter4GInit},
#endif
#ifdef CONNECTION_COMMUNICATION_WIFI
{ "wifi adpter", RegisterAdapterWifi },
#endif
#ifdef CONNECTION_COMMUNICATION_LORA
{ "lora adpter", LoraSx12xxSpiDeviceInit},
#endif
#ifdef CONNECTION_COMMUNICATION_ZIGBEE
{ "zigbee adpter", RegisterAdapterZigbee},
#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

View File

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

View File

@ -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;
}

View File

@ -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

View File

@ -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;
}
}

View File

@ -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

View File

@ -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

View File

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

View File

@ -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.
*/
/**
* @file hfa21.c
* @brief Implement the connection wifi adapter function, using HFA21 device
* @version 1.1
* @author AIIT XUOS Lab
* @date 2021.07.08
*/
#include <adapter.h>
#include <at_agent.h>
#define LEN_PARA_BUF 128
static int Hfa21SetDown(struct Adapter *adapter_at);
/**
* @description: enter AT command mode
* @param at_agent - wifi device agent pointer
* @return success: EOK
*/
static int Hfa21InitAtCmd(ATAgentType at_agent)
{
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, "+++");
PrivTaskDelay(100);
ATOrderSend(at_agent, REPLY_TIME_OUT, NULL, "a");
PrivTaskDelay(500);
return 0;
}
/**
* @description: Open wifi
* @param adapter - wifi device pointer
* @return success: EOK, failure: ENOMEMORY
*/
static int Hfa21Open(struct Adapter *adapter)
{
uint8_t hfa21_cmd[64];
/*step1: open ec200t serial port*/
adapter->fd = PrivOpen(ADAPTER_HFA21_DRIVER, O_RDWR);
if (adapter->fd < 0) {
printf("Hfa21Open get serial %s fd error\n", ADAPTER_HFA21_DRIVER);
return -1;
}
/*step2: init AT agent*/
char *agent_name = "uart3_client";
if (InitATAgent(agent_name, adapter->fd, 512)) {
printf("at agent init failed !\n");
return -1;
}
ATAgentType at_agent = GetATAgent(agent_name);
adapter->agent = at_agent;
ADAPTER_DEBUG("Hfa21 open done\n");
return 0;
}
/**
* @description: Close wifi
* @param adapter - wifi device pointer
* @return success: EOK
*/
static int Hfa21Close(struct Adapter *adapter)
{
return Hfa21SetDown(adapter);
}
/**
* @description: send data to adapter
* @param adapter - wifi device pointer
* @param data - data bufferd
* @param data - data length
* @return success: EOK
*/
static int Hfa21Send(struct Adapter *adapter, const void *data, size_t len)
{
x_err_t result = EOK;
if (adapter->agent) {
EntmSend(adapter->agent, (const char *)data, len);
}else {
printf("Can not find agent \n");
}
__exit:
return result;
}
/**
* @description: receive data from adapter
* @param adapter - wifi device pointer
* @param data - data bufferd
* @param data - data length
* @return success: EOK
*/
static int Hfa21Receive(struct Adapter *adapter, void *rev_buffer, size_t buffer_len)
{
x_err_t result = EOK;
printf("hfa21 receive waiting ... \n");
if (adapter->agent) {
return EntmRecv(adapter->agent, (char *)rev_buffer, buffer_len, 40000);
} else {
printf("Can not find agent \n");
}
__exit:
return result;
}
/**
* @description: connnect wifi to internet
* @param adapter - wifi device pointer
* @return success: EOK
*/
static int Hfa21SetUp(struct Adapter *adapter)
{
uint8 wifi_ssid[LEN_PARA_BUF] = "AIIT-Guest";
uint8 wifi_pwd[LEN_PARA_BUF] = "";
char cmd[LEN_PARA_BUF];
//struct at_device_esp8266 *esp8266 = (struct at_device_esp8266 *) device->UserData;
struct ATAgent *agent = adapter->agent;
/* wait hfa21 device startup finish */
PrivTaskDelay(5000);
Hfa21InitAtCmd(agent);
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"AT+FCLR\r");
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
PrivTaskDelay(20000);
Hfa21InitAtCmd(agent);
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"AT+WSSSID=");
strcat(cmd,wifi_ssid);
strcat(cmd,"\r");
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
PrivTaskDelay(2500);
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"AT+WSKEY=OPEN,NONE,");
strcat(cmd,wifi_pwd);
strcat(cmd,"\r");
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
PrivTaskDelay(2500);
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"AT+WMODE=sta\r");
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
PrivTaskDelay(2500);
memset(cmd,0,sizeof(cmd));
strcat(cmd,"AT+Z\r");
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
PrivTaskDelay(10000);
return 0;
}
/**
* @description: disconnnect wifi from internet
* @param adapter - wifi device pointer
* @return success: EOK
*/
static int Hfa21SetDown(struct Adapter *adapter)
{
Hfa21InitAtCmd(adapter->agent);
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+FCLR\r");
PrivTaskDelay(20000);
return 0;
}
/**
* @description: set wifi ip/gatewy/netmask address(in sta mode)
* @param adapter - wifi device pointer
* @param ip - ip address
* @param gateway - gateway address
* @param netmask - netmask address
* @return success: EOK, failure: ENOMEMORY
*/
static int Hfa21SetAddr(struct Adapter *adapter, const char *ip, const char *gateway, const char *netmask)
{
#define HFA21_SET_ADDR_EXPRESSION "+ok=%[^,],%[^,],%[^,],%[^,]\r"
char *dhcp_mode =NULL;
char *ip_str = NULL;
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);
Hfa21InitAtCmd(adapter->agent);
x_err_t result = EOK;
ATReplyType reply = CreateATReply(64);
if (NULL == reply) {
printf("at_create_resp failed ! \n");
result = ENOMEMORY;
goto __exit;
}
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+WANN=%s,%s,%s,%s\r", "dhcp", ip, netmask, gateway);
PrivTaskDelay(2500);
ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, "AT+WANN\r");
PrivTaskDelay(2500);
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
PrivTaskDelay(10000);
const char * result_buf = GetReplyText(reply);
char* str = strstr(result_buf, "+ok=");
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;
}
/**
* @description: ping
* @param adapter - wifi device pointer
* @param destination - domain name or ip address
* @return success: EOK, failure: ENOMEMORY
*/
static int Hfa21Ping(struct Adapter *adapter, const char *destination)
{
char *ping_result = NONE;
char *dst = NONE;
ping_result = (char *) UserCalloc(1, 17);
dst = (char *) UserCalloc(1, 17);
strcpy(dst, destination);
strcat(dst, "\r");
Hfa21InitAtCmd(adapter->agent);
uint32 result = EOK;
ATReplyType reply = CreateATReply(64);
if (NULL == reply) {
printf("at_create_resp failed ! \n");
result = ENOMEMORY;
goto __exit;
}
//ping baidu.com
ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, "AT+PING=%s", dst);
PrivTaskDelay(2500);
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
PrivTaskDelay(10000);
const char * result_buf = GetReplyText(reply);
char* str = strstr(result_buf, "+ok=");
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;
}
/**
* @description: display network configuration
* @param adapter - wifi device pointer
* @return success: EOK, failure: ENOMEMORY
*/
static int Hfa21Netstat(struct Adapter *adapter)
{
#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->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) {
result = ENOMEMORY;
goto __exit;
}
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "+++");
PrivTaskDelay(100);
ATOrderSend(agent, REPLY_TIME_OUT, NULL, "a");
PrivTaskDelay(2500);
ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+WMODE\r");
PrivTaskDelay(2500);
result_buf = GetReplyText(reply);
str = strstr(result_buf, "+ok=");
/* 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;
}
PrivTaskDelay(2500);
GetReplyText(reply);
str = strstr(result_buf, "+ok=");
/* parse the third line of response data, get the network connection information */
ParseATReply(str, HFA21_WANN_EXPRESSION, ip_mode, local_ipaddr, netmask, gateway);
} else {
ATOrderSend(agent, REPLY_TIME_OUT, reply, "AT+LANN\r");
PrivTaskDelay(2500);
GetReplyText(reply);
str = strstr(result_buf, "+ok=");
/* parse the third line of response data, get the network connection information */
ParseATReply(str, HFA21_LANN_EXPRESSION, local_ipaddr, netmask);
}
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
PrivTaskDelay(10000);
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);
}
static int Hfa21Connect(struct Adapter *adapter, enum NetRoleType net_role, const char *ip, const char *port, enum IpType ip_type)
{
int result = EOK;
ATReplyType reply = NONE;
char cmd[LEN_PARA_BUF];
struct ATAgent *agent = adapter->agent;
reply = CreateATReply(64);
if (reply == NONE) {
printf("no memory for reply struct.");
return ENOMEMORY;
}
Hfa21InitAtCmd(adapter->agent);
memset(cmd,0,sizeof(cmd));
strcpy(cmd,"AT+NETP=TCP,");
if(net_role == CLIENT)
strcat(cmd,"CLIENT,");
else if(net_role == SERVER)
strcat(cmd,"SERVER,");
strcat(cmd,port);
strcat(cmd,",");
if(ip_type == IPV4)
strcat(cmd,ip);
else if(ip_type == IPV6)
{
}
strcat(cmd,"\r");
ATOrderSend(agent, REPLY_TIME_OUT, NULL, cmd);
PrivTaskDelay(2500);
ATOrderSend(adapter->agent, REPLY_TIME_OUT, NULL, "AT+Z\r");
PrivTaskDelay(10000);
adapter->net_role = net_role;
__exit:
if (reply) {
DeleteATReply(reply);
}
return result;
}
static int Hfa21Ioctl(struct Adapter *adapter, int cmd, void *args)
{
if (OPE_INT != cmd) {
printf("Hfa21Ioctl only support OPE_INT, do not support %d\n", cmd);
return -1;
}
uint32_t baud_rate = *((uint32_t *)args);
struct SerialDataCfg serial_cfg;
memset(&serial_cfg, 0 ,sizeof(struct SerialDataCfg));
serial_cfg.serial_baud_rate = baud_rate;
serial_cfg.serial_data_bits = DATA_BITS_8;
serial_cfg.serial_stop_bits = STOP_BITS_1;
serial_cfg.serial_buffer_size = SERIAL_RB_BUFSZ;
serial_cfg.serial_parity_mode = PARITY_NONE;
serial_cfg.serial_bit_order = BIT_ORDER_LSB;
serial_cfg.serial_invert_mode = NRZ_NORMAL;
#ifdef ADAPTER_HFA21_DRIVER_EXT_PORT
serial_cfg.ext_uart_no = ADAPTER_HFA21_DRIVER_EXT_PORT;
serial_cfg.port_configure = PORT_CFG_INIT;
#endif
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = SERIAL_TYPE;
ioctl_cfg.args = &serial_cfg;
PrivIoctl(adapter->fd, OPE_INT, &ioctl_cfg);
printf("Hfa21Ioctl success\n");
return 0;
}
static const struct IpProtocolDone hfa21_done =
{
.open = Hfa21Open,
.close = Hfa21Close,
.ioctl = Hfa21Ioctl,
.setup = Hfa21SetUp,
.setdown = Hfa21SetDown,
.setaddr = Hfa21SetAddr,
.setdns = NULL,
.setdhcp = NULL,
.ping = Hfa21Ping,
.netstat = Hfa21Netstat,
.connect = Hfa21Connect,
.send = Hfa21Send,
.recv = Hfa21Receive,
.disconnect = NULL,
};
/**
* @description: Register wifi device hfa21
* @return success: EOK, failure: ERROR
*/
AdapterProductInfoType Hfa21Attach(struct Adapter *adapter)
{
struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo));
if (!product_info) {
printf("Hfa21Attach Attach malloc product_info error\n");
free(product_info);
return NULL;
}
product_info->model_name = ADAPTER_WIFI_HFA21;
product_info->model_done = (void *)&hfa21_done;
return product_info;
}

View File

@ -0,0 +1,14 @@
config ADAPTER_WIFI
bool "Using WIFI adapter function"
default y
if ADAPTER_WIFI
config ADAPTER_HFA21
bool "Using wifi adapter device HFA21"
default y
if ADAPTER_HFA21
source "$APP_DIR/Framework/connection/wifi/HFA21/Kconfig"
endif
endif

View File

@ -1,3 +1,7 @@
SRC_FILES := adapter_wifi.c
ifeq ($(CONFIG_ADAPTER_HFA21),y)
SRC_DIR += HFA21
endif
include $(KERNEL_ROOT)/compiler.mk

View File

@ -11,9 +11,161 @@
*/
/**
* @file adapter_wifi.c
* @file adapter_wifiiiii.c
* @brief Implement the connection wifi adapter function
* @version 1.1
* @author AIIT XUOS Lab
* @date 2021.06.25
* @date 2021.07.25
*/
#include <adapter.h>
#include <bus_pin.h>
#ifdef ADAPTER_HFA21
extern AdapterProductInfoType Hfa21Attach(struct Adapter *adapter);
#endif
static int AdapterWifiRegister(struct Adapter *adapter)
{
int ret = 0;
strncpy(adapter->name, ADAPTER_WIFI_NAME, NAME_NUM_MAX);
adapter->net_protocol = IP_PROTOCOL;
adapter->adapter_status = UNREGISTERED;
ret = AdapterDeviceRegister(adapter);
if (ret < 0) {
printf("AdapterWifi register error\n");
return -1;
}
return ret;
}
int AdapterWifiInit(void)
{
int ret = 0;
struct Adapter *adapter = malloc(sizeof(struct Adapter));
if (!adapter) {
printf("AdapterWifiInit malloc error\n");
free(adapter);
return -1;
}
ret = AdapterWifiRegister(adapter);
if (ret < 0) {
printf("AdapterWifiInit register wifi adapter error\n");
free(adapter);
return -1;
}
#ifdef ADAPTER_HFA21
AdapterProductInfoType product_info = Hfa21Attach(adapter);
if (!product_info) {
printf("AdapterWifiInit hfa21 attach error\n");
free(adapter);
return -1;
}
adapter->product_info_flag = 1;
adapter->info = product_info;
adapter->done = product_info->model_done;
#endif
return ret;
}
/******************wifi TEST*********************/
int AdapterWifiTest(void)
{
char cmd[64];
int baud_rate = BAUD_RATE_57600;
struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_WIFI_NAME);
#ifdef ADAPTER_HFA21_DRIVER_EXT_PORT
// static BusType ch438_pin;
// ch438_pin = PinBusInitGet();
// struct PinParam pin_cfg;
// int ret = 0;
// struct BusConfigureInfo configure_info;
// configure_info.configure_cmd = OPE_CFG;
// configure_info.private_data = (void *)&pin_cfg;
// pin_cfg.cmd = GPIO_CONFIG_MODE;
// pin_cfg.pin = 22;
// pin_cfg.mode = GPIO_CFG_OUTPUT;
// ret = BusDrvConfigure(ch438_pin->owner_driver, &configure_info);
// struct PinStat pin_stat;
// struct BusBlockWriteParam write_param;
// struct BusBlockReadParam read_param;
// write_param.buffer = (void *)&pin_stat;
// pin_stat.val = GPIO_HIGH;
// pin_stat.pin = 22;
// BusDevWriteData(ch438_pin->owner_haldev, &write_param);
int pin_fd;
pin_fd = PrivOpen("/dev/pin_dev", O_RDWR);
struct PinParam pin_param;
pin_param.cmd = GPIO_CONFIG_MODE;
pin_param.mode = GPIO_CFG_OUTPUT;
pin_param.pin = 22;
struct PrivIoctlCfg ioctl_cfg;
ioctl_cfg.ioctl_driver_type = PIN_TYPE;
ioctl_cfg.args = &pin_param;
PrivIoctl(pin_fd, OPE_CFG, &ioctl_cfg);
struct PinStat pin_stat;
pin_stat.pin = 52;
pin_stat.val = GPIO_HIGH;
PrivWrite(pin_fd, &pin_stat, 1);
PrivClose(pin_fd);
#endif
AdapterDeviceOpen(adapter);
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
AdapterDeviceSetUp(adapter);
AdapterDeviceSetAddr(adapter, "192.168.66.253", "255.255.255.0", "192.168.66.1");
AdapterDevicePing(adapter, "36.152.44.95");
AdapterDeviceNetstat(adapter);
const char *ip = "192.168.66.211";
const char *port = "12345";
enum NetRoleType net_role = CLIENT;
enum IpType ip_type = IPV4;
AdapterDeviceConnect(adapter, net_role, ip, port, ip_type);
const char *wifi_msg = "LiuKai Test";
int len = strlen(wifi_msg);
for(int i=0;i<10;++i)
{
AdapterDeviceSend(adapter, wifi_msg, len);
PrivTaskDelay(4000);
}
char wifi_recv_msg[128];
while (1)
{
AdapterDeviceRecv(adapter, wifi_recv_msg, 128);
}
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, AdapterWifiTest, AdapterWifiTest, show adapter wifi information);

View File

@ -39,7 +39,7 @@ int PrivMutexObtain(pthread_mutex_t *p_mutex)
int PrivMutexAbandon(pthread_mutex_t *p_mutex)
{
return pthread_mutex_lock(p_mutex);
return pthread_mutex_unlock(p_mutex);
}
/**********************semaphore****************************/
@ -121,15 +121,14 @@ int PrivWrite(int fd, const void *buf, size_t len)
static int PrivSerialIoctl(int fd, int cmd, void *args)
{
struct SerialDataCfg *serial_cfg = (struct SerialDataCfg *)args;
return ioctl(fd, cmd, &serial_cfg);
return ioctl(fd, cmd, serial_cfg);
}
static int PrivPinIoctl(int fd, int cmd, void *args)
{
struct PinParam *pin_cfg = (struct PinParam *)args;
return ioctl(fd, cmd, &pin_cfg);
return ioctl(fd, cmd, pin_cfg);
}
int PrivIoctl(int fd, int cmd, void *args)
@ -143,7 +142,7 @@ int PrivIoctl(int fd, int cmd, void *args)
ret = PrivSerialIoctl(fd, cmd, ioctl_cfg->args);
break;
case PIN_TYPE:
ret = PrivSerialIoctl(fd, cmd, ioctl_cfg->args);
ret = PrivPinIoctl(fd, cmd, ioctl_cfg->args);
break;
default:
break;

View File

@ -1,3 +1,3 @@
SRC_DIR := connection intelligent security
SRC_DIR := intelligent security
include $(KERNEL_ROOT)/compiler.mk