【添加】诺行 MW31 wifi模块
This commit is contained in:
parent
ff05ab7d8f
commit
0791f440c6
11
SConscript
11
SConscript
|
@ -30,7 +30,16 @@ if GetDepend(['AT_DEVICE_USING_ESP8266']):
|
|||
src += Glob('class/esp8266/at_socket_esp8266.c')
|
||||
if GetDepend(['AT_DEVICE_ESP8266_SAMPLE']):
|
||||
src += Glob('samples/at_sample_esp8266.c')
|
||||
|
||||
|
||||
# MW31
|
||||
if GetDepend(['AT_DEVICE_USING_MW31']):
|
||||
path += [cwd + '/class/mw31']
|
||||
src += Glob('class/mw31/at_device_mw31.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/mw31/at_socket_mw31.c')
|
||||
if GetDepend(['AT_DEVICE_MW31_SAMPLE']):
|
||||
src += Glob('samples/at_sample_mw31.c')
|
||||
|
||||
# RW007
|
||||
if GetDepend(['AT_DEVICE_USING_RW007']):
|
||||
path += [cwd + '/class/rw007']
|
||||
|
|
|
@ -0,0 +1,752 @@
|
|||
/*
|
||||
* File : at_socket_mw31.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-06-23 flybreak first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_mw31.h>
|
||||
|
||||
#if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10300
|
||||
#error "This AT Client version is older, please check and update latest AT Client!"
|
||||
#endif
|
||||
|
||||
#define LOG_TAG "at.dev"
|
||||
|
||||
#include <at_log.h>
|
||||
|
||||
#ifdef AT_DEVICE_USING_MW31
|
||||
|
||||
#define MW31_WAIT_CONNECT_TIME 5000
|
||||
#define MW31_THREAD_STACK_SIZE 1024
|
||||
#define MW31_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
|
||||
|
||||
/* ============================= mw31 network interface operations ============================= */
|
||||
|
||||
#define AT_ADDR_LEN 32
|
||||
#define IPADDR_RESP_SIZE 128
|
||||
#define IPADDR_SIZE 16
|
||||
|
||||
char mw31_ip_addr[AT_ADDR_LEN] = {0};
|
||||
char mw31_gw_addr[AT_ADDR_LEN] = {0};
|
||||
char mw31_netmask_addr[AT_ADDR_LEN] = {0};
|
||||
|
||||
static void mw31_get_netdev_info(struct rt_work *work, void *work_data)
|
||||
{
|
||||
at_response_t resp = RT_NULL;
|
||||
char mac[AT_ADDR_LEN] = {0};
|
||||
char dns_server1[AT_ADDR_LEN] = {0};
|
||||
char dhcp_stat_buf[5] = {0};
|
||||
ip_addr_t ip_addr;
|
||||
rt_uint32_t mac_addr[6] = {0};
|
||||
rt_uint32_t num = 0;
|
||||
rt_uint8_t dhcp_stat = 0;
|
||||
struct rt_delayed_work *delay_work = (struct rt_delayed_work *)work;
|
||||
struct at_device *device = (struct at_device *)work_data;
|
||||
struct netdev *netdev = device->netdev;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
if (delay_work)
|
||||
{
|
||||
rt_free(delay_work);
|
||||
}
|
||||
|
||||
resp = at_create_resp(512, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%d) response structure.", device->name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* send mac addr query commond "AT+CIFSR" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+WMAC?") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+WMAC:", "+WMAC:%s", mac) <= 0)
|
||||
{
|
||||
LOG_E("mw31 device(%s) parse \"AT+WMAC\" command response data error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send addr info query commond "AT+CIPSTA?" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+WJAPIP?") < 0)
|
||||
{
|
||||
LOG_E("mw31 device(%s) send \"AT+WJAPIP?\" commands error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+WJAPIP?:", "+WJAPIP?:%[^,],%[^,],%[^,],%s", mw31_ip_addr, mw31_netmask_addr, mw31_gw_addr, dns_server1) < 0)
|
||||
{
|
||||
LOG_E("mw31 device(%s) prase \"AT+WJAPIP?\" command resposne data error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set netdev info */
|
||||
inet_aton(mw31_gw_addr, &ip_addr);
|
||||
netdev_low_level_set_gw(netdev, &ip_addr);
|
||||
inet_aton(mw31_netmask_addr, &ip_addr);
|
||||
netdev_low_level_set_netmask(netdev, &ip_addr);
|
||||
inet_aton(mw31_ip_addr, &ip_addr);
|
||||
netdev_low_level_set_ipaddr(netdev, &ip_addr);
|
||||
|
||||
sscanf(mac, "%2x%2x%2x%2x%2x%2x",
|
||||
&mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
|
||||
for (num = 0; num < netdev->hwaddr_len; num++)
|
||||
{
|
||||
netdev->hwaddr[num] = mac_addr[num];
|
||||
}
|
||||
|
||||
if (rt_strlen(dns_server1) > 0)
|
||||
{
|
||||
inet_aton(dns_server1, &ip_addr);
|
||||
netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
|
||||
}
|
||||
|
||||
/* send DHCP query commond " AT+WDHCP?" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+WDHCP?") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse response data, get the DHCP status */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+WDHCP:", "+WDHCP:%s", dhcp_stat_buf) < 0)
|
||||
{
|
||||
LOG_E("mw31 device(%s) get DHCP status failed.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (rt_strstr(dhcp_stat_buf, "ON"))
|
||||
{
|
||||
dhcp_stat |= 0x03;
|
||||
}
|
||||
/* Bit0 - SoftAP DHCP status, Bit1 - Station DHCP status */
|
||||
netdev_low_level_set_dhcp_status(netdev, dhcp_stat & 0x02 ? RT_TRUE : RT_FALSE);
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
}
|
||||
|
||||
static int mw31_net_init(struct at_device *device);
|
||||
|
||||
static int mw31_netdev_set_up(struct netdev *netdev)
|
||||
{
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get mw31 device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_FALSE)
|
||||
{
|
||||
mw31_net_init(device);
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
LOG_D("the network interface device(%s) set up status", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int mw31_netdev_set_down(struct netdev *netdev)
|
||||
{
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get mw31 device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_TRUE)
|
||||
{
|
||||
device->is_init = RT_FALSE;
|
||||
netdev_low_level_set_status(netdev, RT_FALSE);
|
||||
LOG_D("the network interface device(%s) set down status", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int mw31_netdev_set_addr_info(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
RT_ASSERT(ip_addr || netmask || gw);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get mw31 device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(IPADDR_RESP_SIZE, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Convert numeric IP address into decimal dotted ASCII representation. */
|
||||
if (ip_addr)
|
||||
rt_memcpy(mw31_ip_addr, inet_ntoa(*ip_addr), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(mw31_ip_addr, inet_ntoa(netdev->ip_addr), IPADDR_SIZE);
|
||||
|
||||
if (gw)
|
||||
rt_memcpy(mw31_gw_addr, inet_ntoa(*gw), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(mw31_gw_addr, inet_ntoa(netdev->gw), IPADDR_SIZE);
|
||||
|
||||
if (netmask)
|
||||
rt_memcpy(mw31_netmask_addr, inet_ntoa(*netmask), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(mw31_netmask_addr, inet_ntoa(netdev->netmask), IPADDR_SIZE);
|
||||
|
||||
/* send addr info set commond "AT+WJAPIP=<ip>,<network>,<gateway>[,<dns>] " and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+WJAPIP=%s,%s,%s",
|
||||
mw31_ip_addr, mw31_netmask_addr, mw31_gw_addr) < 0)
|
||||
{
|
||||
LOG_E("mw31 device(%s) set address information failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Update netdev information */
|
||||
if (ip_addr)
|
||||
netdev_low_level_set_ipaddr(netdev, ip_addr);
|
||||
|
||||
if (gw)
|
||||
netdev_low_level_set_gw(netdev, gw);
|
||||
|
||||
if (netmask)
|
||||
netdev_low_level_set_netmask(netdev, netmask);
|
||||
|
||||
LOG_D("mw31 device(%s) set address information successfully.", device->name);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int mw31_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
|
||||
{
|
||||
#define DNS_RESP_SIZE 128
|
||||
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
RT_ASSERT(dns_server);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get mw31 device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(DNS_RESP_SIZE, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* send dns server set commond "AT+WJAPIP=<ip>,<network>,<gateway>[,<dns>] " and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+WJAPIP=%s,%s,%s,%s",
|
||||
mw31_ip_addr, mw31_netmask_addr, mw31_gw_addr, inet_ntoa(*dns_server)) < 0)
|
||||
{
|
||||
LOG_E("mw31 device(%s) set DNS server(%s) failed.", device->name, inet_ntoa(*dns_server));
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
|
||||
LOG_D("mw31 device(%s) set DNS server(%s) successfully.", device->name, inet_ntoa(*dns_server));
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int mw31_netdev_set_dhcp(struct netdev *netdev, rt_bool_t is_enabled)
|
||||
{
|
||||
#define MW31_STATION 1
|
||||
#define RESP_SIZE 128
|
||||
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
const char *send_buf;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get AT device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(RESP_SIZE, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (is_enabled)
|
||||
{
|
||||
send_buf = "AT+WDHCP=ON";
|
||||
}
|
||||
else
|
||||
{
|
||||
send_buf = "AT+WDHCP=OFF";
|
||||
}
|
||||
/* send dhcp set commond "AT+WDHCP=" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, send_buf) < 0)
|
||||
{
|
||||
LOG_E("mw31 device(%s) set DHCP status(%d) failed.", device->name, is_enabled);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev_low_level_set_dhcp_status(netdev, is_enabled);
|
||||
LOG_D("mw31 device(%d) set DHCP status(%d) successfully.", device->name, is_enabled);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static const struct netdev_ops mw31_netdev_ops =
|
||||
{
|
||||
mw31_netdev_set_up,
|
||||
mw31_netdev_set_down,
|
||||
|
||||
mw31_netdev_set_addr_info,
|
||||
mw31_netdev_set_dns_server,
|
||||
mw31_netdev_set_dhcp,
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
RT_NULL,
|
||||
#endif
|
||||
#ifdef NETDEV_USING_NETSTAT
|
||||
RT_NULL,
|
||||
#endif
|
||||
};
|
||||
|
||||
static struct netdev *mw31_netdev_add(const char *netdev_name)
|
||||
{
|
||||
#define ETHERNET_MTU 1500
|
||||
#define HWADDR_LEN 6
|
||||
struct netdev *netdev = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev_name);
|
||||
|
||||
netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
|
||||
if (netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) netdev structure.", netdev_name);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
netdev->mtu = ETHERNET_MTU;
|
||||
netdev->ops = &mw31_netdev_ops;
|
||||
netdev->hwaddr_len = HWADDR_LEN;
|
||||
|
||||
#ifdef SAL_USING_AT
|
||||
extern int sal_at_netdev_set_pf_info(struct netdev * netdev);
|
||||
/* set the network interface socket/netdb operations */
|
||||
sal_at_netdev_set_pf_info(netdev);
|
||||
#endif
|
||||
|
||||
netdev_register(netdev, netdev_name, RT_NULL);
|
||||
|
||||
return netdev;
|
||||
}
|
||||
|
||||
/* ============================= mw31 device operations ============================= */
|
||||
|
||||
#define AT_SEND_CMD(client, resp, cmd) \
|
||||
do { \
|
||||
(resp) = at_resp_set_info((resp), 256, 0, 5 * RT_TICK_PER_SECOND); \
|
||||
if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
|
||||
{ \
|
||||
result = -RT_ERROR; \
|
||||
goto __exit; \
|
||||
} \
|
||||
} while(0) \
|
||||
|
||||
static void mw31_netdev_start_delay_work(struct at_device *device)
|
||||
{
|
||||
struct rt_delayed_work *net_work = RT_NULL;
|
||||
net_work = (struct rt_delayed_work *)rt_calloc(1, sizeof(struct rt_delayed_work));
|
||||
if (net_work == RT_NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
rt_delayed_work_init(net_work, mw31_get_netdev_info, (void *)device);
|
||||
rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
static void mw31_init_thread_entry(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 2
|
||||
|
||||
struct at_device *device = (struct at_device *) parameter;
|
||||
struct at_device_mw31 *mw31 = (struct at_device_mw31 *) device->user_data;
|
||||
struct at_client *client = device->client;
|
||||
at_response_t resp = RT_NULL;
|
||||
rt_err_t result = RT_EOK;
|
||||
rt_size_t i = 0, retry_num = INIT_RETRY;
|
||||
|
||||
LOG_D("mw31 device(%s) initialize start.", device->name);
|
||||
|
||||
/* wait mw31 device startup finish */
|
||||
if (at_client_obj_wait_connect(client, MW31_WAIT_CONNECT_TIME))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%d) response structure.", device->name);
|
||||
return;
|
||||
}
|
||||
|
||||
while (retry_num--)
|
||||
{
|
||||
/* reset module */
|
||||
AT_SEND_CMD(client, resp, "AT+REBOOT");
|
||||
/* reset waiting delay */
|
||||
rt_thread_mdelay(2000);
|
||||
/* set current mode to Wi-Fi station */
|
||||
AT_SEND_CMD(client, resp, "AT+WSAPQ");
|
||||
/* get module version */
|
||||
AT_SEND_CMD(client, resp, "AT+FWVER?");
|
||||
/* show module version */
|
||||
for (i = 0; i < resp->line_counts - 1; i++)
|
||||
{
|
||||
LOG_D("%s", at_resp_get_line(resp, i + 1));
|
||||
}
|
||||
|
||||
/* connect to WiFi AP */
|
||||
if (at_obj_exec_cmd(client, at_resp_set_info(resp, 128, 0, 20 * RT_TICK_PER_SECOND),
|
||||
"AT+WJAP=%s,%s", mw31->wifi_ssid, mw31->wifi_password) != RT_EOK)
|
||||
{
|
||||
LOG_E("AT device(%s) network initialize failed, check ssid(%s) and password(%s).",
|
||||
device->name, mw31->wifi_ssid, mw31->wifi_password);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
LOG_I("mw31 device(%s) initialize retry...", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
netdev_low_level_set_status(device->netdev, RT_FALSE);
|
||||
LOG_E("mw31 device(%s) network initialize failed(%d).", device->name, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
device->is_init = RT_TRUE;
|
||||
netdev_low_level_set_status(device->netdev, RT_TRUE);
|
||||
netdev_low_level_set_link_status(device->netdev, RT_TRUE);
|
||||
|
||||
LOG_I("mw31 device(%s) network initialize successfully.", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
static int mw31_net_init(struct at_device *device)
|
||||
{
|
||||
#ifdef AT_DEVICE_MW31_INIT_ASYN
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("mw31_net_init", mw31_init_thread_entry, (void *) device,
|
||||
MW31_THREAD_STACK_SIZE, MW31_THREAD_PRIORITY, 20);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("create mw31 device(%s) initialize thread failed.", device->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#else
|
||||
mw31_init_thread_entry(device);
|
||||
#endif /* AT_DEVICE_MW31_INIT_ASYN */
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void urc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(client && data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get mw31 device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rt_strstr(data, "STATION_UP"))
|
||||
{
|
||||
LOG_I("mw31 device(%s) WIFI is connected.", device->name);
|
||||
|
||||
if (device->is_init)
|
||||
{
|
||||
netdev_low_level_set_link_status(device->netdev, RT_TRUE);
|
||||
|
||||
mw31_netdev_start_delay_work(device);
|
||||
}
|
||||
}
|
||||
else if (rt_strstr(data, "STATION_DOWN"))
|
||||
{
|
||||
LOG_I("mw31 device(%s) WIFI is disconnect.", device->name);
|
||||
|
||||
if (device->is_init)
|
||||
{
|
||||
netdev_low_level_set_link_status(device->netdev, RT_FALSE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"+WEVENT:", "\r\n", urc_func},
|
||||
};
|
||||
|
||||
static int mw31_init(struct at_device *device)
|
||||
{
|
||||
struct at_device_mw31 *mw31 = (struct at_device_mw31 *) device->user_data;
|
||||
|
||||
/* initialize AT client */
|
||||
at_client_init(mw31->client_name, mw31->recv_line_num);
|
||||
|
||||
device->client = at_client_get(mw31->client_name);
|
||||
if (device->client == RT_NULL)
|
||||
{
|
||||
LOG_E("mw31 device(%s) initialize failed, get AT client(%s) failed.",
|
||||
mw31->device_name, mw31->client_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* register URC data execution function */
|
||||
at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
mw31_socket_init(device);
|
||||
#endif
|
||||
|
||||
/* add mw31 device to the netdev list */
|
||||
device->netdev = mw31_netdev_add(mw31->device_name);
|
||||
if (device->netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("mw31 device(%s) initialize failed, get network interface device failed.", mw31->device_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize mw31 device network */
|
||||
return mw31_netdev_set_up(device->netdev);
|
||||
}
|
||||
|
||||
static int mw31_deinit(struct at_device *device)
|
||||
{
|
||||
return mw31_netdev_set_down(device->netdev);
|
||||
}
|
||||
|
||||
/* reset eap8266 device and initialize device network again */
|
||||
static int mw31_reset(struct at_device *device)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
/* send "AT+RST" commonds to mw31 device */
|
||||
result = at_obj_exec_cmd(client, RT_NULL, "AT+RST");
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* waiting 10 seconds for mw31 device reset */
|
||||
device->is_init = RT_FALSE;
|
||||
if (at_client_obj_wait_connect(client, MW31_WAIT_CONNECT_TIME))
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
/* initialize mw31 device network */
|
||||
mw31_net_init(device);
|
||||
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* change eap8266 wifi ssid and password information */
|
||||
static int mw31_wifi_info_set(struct at_device *device, struct at_device_ssid_pwd *info)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
struct at_response *resp = RT_NULL;
|
||||
|
||||
if (info->ssid == RT_NULL || info->password == RT_NULL)
|
||||
{
|
||||
LOG_E("input mw31 wifi ssid(%s) and password(%s) error.", info->ssid, info->password);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* connect to input wifi ap */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CWJAP=\"%s\",\"%s\"", info->ssid, info->password) != RT_EOK)
|
||||
{
|
||||
LOG_E("mw31 device(%s) wifi connect failed, check ssid(%s) and password(%s).",
|
||||
device->name, info->ssid, info->password);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int mw31_control(struct at_device *device, int cmd, void *arg)
|
||||
{
|
||||
int result = -RT_ERROR;
|
||||
|
||||
RT_ASSERT(device);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case AT_DEVICE_CTRL_POWER_ON:
|
||||
case AT_DEVICE_CTRL_POWER_OFF:
|
||||
case AT_DEVICE_CTRL_LOW_POWER:
|
||||
case AT_DEVICE_CTRL_SLEEP:
|
||||
case AT_DEVICE_CTRL_WAKEUP:
|
||||
case AT_DEVICE_CTRL_NET_CONN:
|
||||
case AT_DEVICE_CTRL_NET_DISCONN:
|
||||
case AT_DEVICE_CTRL_GET_SIGNAL:
|
||||
case AT_DEVICE_CTRL_GET_GPS:
|
||||
case AT_DEVICE_CTRL_GET_VER:
|
||||
LOG_W("mw31 not support the control command(%d).", cmd);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_RESET:
|
||||
result = mw31_reset(device);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_SET_WIFI_INFO:
|
||||
result = mw31_wifi_info_set(device, (struct at_device_ssid_pwd *) arg);
|
||||
break;
|
||||
default:
|
||||
LOG_E("input error control command(%d).", cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static const struct at_device_ops mw31_device_ops =
|
||||
{
|
||||
mw31_init,
|
||||
mw31_deinit,
|
||||
mw31_control,
|
||||
};
|
||||
|
||||
static int mw31_device_class_register(void)
|
||||
{
|
||||
struct at_device_class *class = RT_NULL;
|
||||
|
||||
class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
|
||||
if (class == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device class create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* fill MW31 device class object */
|
||||
#ifdef AT_USING_SOCKET
|
||||
mw31_socket_class_register(class);
|
||||
#endif
|
||||
class->device_ops = &mw31_device_ops;
|
||||
|
||||
return at_device_class_register(class, AT_DEVICE_CLASS_MW31);
|
||||
}
|
||||
INIT_DEVICE_EXPORT(mw31_device_class_register);
|
||||
|
||||
#endif /* AT_DEVICE_USING_MW31 */
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* File : at_device_mw31.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-06-23 flybreak first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_MW31_H__
|
||||
#define __AT_DEVICE_MW31_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the mw31 device */
|
||||
#define AT_DEVICE_MW31_SOCKETS_NUM 5
|
||||
|
||||
struct at_device_mw31
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
char *wifi_ssid;
|
||||
char *wifi_password;
|
||||
size_t recv_line_num;
|
||||
struct at_device device;
|
||||
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* mw31 device socket initialize */
|
||||
int mw31_socket_init(struct at_device *device);
|
||||
|
||||
/* mw31 device class socket register */
|
||||
int mw31_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_MW31_H__ */
|
|
@ -0,0 +1,477 @@
|
|||
/*
|
||||
* File : at_socket_mw31.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-06-23 flybreak first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_mw31.h>
|
||||
|
||||
#if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10300
|
||||
#error "This AT Client version is older, please check and update latest AT Client!"
|
||||
#endif
|
||||
|
||||
#define LOG_TAG "at.skt"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_MW31) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define MW31_MODULE_SEND_MAX_SIZE 1024
|
||||
/* set real event by current socket and current state */
|
||||
#define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
|
||||
|
||||
/* AT socket event type */
|
||||
#define MW31_EVENT_CONN_OK (1L << 0)
|
||||
#define MW31_EVENT_SEND_OK (1L << 1)
|
||||
#define MW31_EVENT_RECV_OK (1L << 2)
|
||||
#define MW31_EVNET_CLOSE_OK (1L << 3)
|
||||
#define MW31_EVENT_CONN_FAIL (1L << 4)
|
||||
#define MW31_EVENT_SEND_FAIL (1L << 5)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] =
|
||||
{
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* close socket by AT commands.
|
||||
*
|
||||
* @param current socket
|
||||
*
|
||||
* @return 0: close socket success
|
||||
* -1: send AT commands error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int mw31_socket_close(struct at_socket *socket)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
char type[15] = {0}, status[15] = {0};
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
at_obj_exec_cmd(device->client, resp, "AT+CIPSTATUS=%d", device_socket);
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CIPSTATU:", "+CIPSTATU:%[^,],%s", type, status) > 0)
|
||||
{
|
||||
LOG_D("%s\n%s\n", type, status);
|
||||
}
|
||||
|
||||
if (status[0] == 'd' || status[2] == 'o')
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+CIPSTOP=%d", device_socket);
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* create TCP/UDP client or server connect by AT commands.
|
||||
*
|
||||
* @param socket current socket
|
||||
* @param ip server or client IP address
|
||||
* @param port server or client port
|
||||
* @param type connect socket type(tcp, udp)
|
||||
* @param is_client connection is client
|
||||
*
|
||||
* @return 0: connect success
|
||||
* -1: connect failed, send commands error or type error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int mw31_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_bool_t retryed = RT_FALSE;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
RT_ASSERT(ip);
|
||||
RT_ASSERT(port >= 0);
|
||||
|
||||
resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
__retry:
|
||||
if (is_client)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
/* send AT commands to connect TCP server */
|
||||
if (at_obj_exec_cmd(device->client, resp,
|
||||
"AT+CIPSTART=%d,tcp_client,%s,%d,%d", device_socket, ip, port, device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case AT_SOCKET_UDP:
|
||||
if (at_obj_exec_cmd(device->client, resp,
|
||||
"AT+CIPSTART=%d,udp_unicast,%s,%d,%d", device_socket, ip, port, device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG_E("mw31 device(%s) not supported connect type %d.", device->name, type);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (result != RT_EOK && retryed == RT_FALSE)
|
||||
{
|
||||
LOG_D("mw31 device(%s) socket (%d) connect failed, maybe the socket was not be closed at the last time and now will retry.",
|
||||
device->name, device_socket);
|
||||
if (mw31_socket_close(socket) < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
retryed = RT_TRUE;
|
||||
result = RT_EOK;
|
||||
goto __retry;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* send data to server or client by AT commands.
|
||||
*
|
||||
* @param socket current socket
|
||||
* @param buff send buffer
|
||||
* @param bfsz send buffer size
|
||||
* @param type connect socket type(tcp, udp)
|
||||
*
|
||||
* @return >=0: the size of send success
|
||||
* -1: send AT commands error or send data error
|
||||
* -2: waited socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int mw31_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
size_t cur_pkt_size = 0, sent_size = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
struct at_device_mw31 *mw31 = (struct at_device_mw31 *) device->user_data;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
char send_buf[20] = {0};
|
||||
|
||||
RT_ASSERT(buff);
|
||||
RT_ASSERT(bfsz > 0);
|
||||
|
||||
resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* set current socket for send URC event */
|
||||
mw31->user_data = (void *) device_socket;
|
||||
|
||||
/* set AT client end sign to deal with '>' sign */
|
||||
at_obj_set_end_sign(device->client, '>');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < MW31_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = MW31_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
sprintf(send_buf, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size);
|
||||
/* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line */
|
||||
at_client_obj_send(device->client, send_buf, strlen(send_buf));
|
||||
|
||||
at_client_obj_send(device->client, "\r", 1);
|
||||
|
||||
/* send the real data to server or client */
|
||||
result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size);
|
||||
if (result == 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reset the end sign for data */
|
||||
at_obj_set_end_sign(device->client, 0);
|
||||
|
||||
rt_mutex_release(lock);
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* domain resolve by AT commands.
|
||||
*
|
||||
* @param name domain name
|
||||
* @param ip parsed IP address, it's length must be 16
|
||||
*
|
||||
* @return 0: domain resolve success
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int mw31_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 5
|
||||
|
||||
int i, result = RT_EOK;
|
||||
rt_uint8_t recv_ip_num = 0;
|
||||
char recv_ip[16] = { 0 };
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(name);
|
||||
RT_ASSERT(ip);
|
||||
|
||||
device = at_device_get_first_initialized();
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get first initialization mw31 device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
for (i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=%s", name) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse the third line of response data, get the IP address */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", "+CIPDOMAIN:%d", &recv_ip_num) < 0 ||
|
||||
recv_ip_num == 0 ||
|
||||
at_resp_parse_line_args(resp, 3, "%s", recv_ip) < 0)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (rt_strlen(recv_ip) < 8)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_strncpy(ip, recv_ip, 15);
|
||||
ip[15] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* set AT socket event notice callback
|
||||
*
|
||||
* @param event notice event
|
||||
* @param cb notice callback
|
||||
*/
|
||||
static void mw31_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)
|
||||
{
|
||||
if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1]))
|
||||
{
|
||||
at_evt_cb_set[event] = cb;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct at_socket_ops mw31_socket_ops =
|
||||
{
|
||||
mw31_socket_connect,
|
||||
mw31_socket_close,
|
||||
mw31_socket_send,
|
||||
mw31_domain_resolve,
|
||||
mw31_socket_set_event_cb,
|
||||
};
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout = 0;
|
||||
rt_size_t bfsz = 0, temp_size = 0;
|
||||
char *recv_buf = RT_NULL, temp[8] = {0};
|
||||
struct at_socket *socket = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
rt_uint8_t i;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get mw31 device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
at_client_obj_recv(client, temp, 2, 1000);
|
||||
/* get the at deveice socket and receive buffer size by receive data */
|
||||
sscanf(temp, "%d,", &device_socket);
|
||||
temp[0] = 0;
|
||||
temp[1] = 0;
|
||||
for (i = 0; i < 6 && temp[i - 1] != ','; i++)
|
||||
{
|
||||
at_client_obj_recv(client, &temp[i], 1, 1000);
|
||||
}
|
||||
sscanf(temp, "%ld,", &bfsz);
|
||||
|
||||
LOG_D("socket:%d,size:%ld\n", device_socket, bfsz);
|
||||
/* get receive timeout by receive buffer length */
|
||||
timeout = bfsz;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
return;
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for mw31 device(%s) URC receive buffer(%d).", device->name, bfsz);
|
||||
/* read and clean the coming data */
|
||||
while (temp_size < bfsz)
|
||||
{
|
||||
if (bfsz - temp_size > sizeof(temp))
|
||||
{
|
||||
at_client_obj_recv(client, temp, sizeof(temp), timeout);
|
||||
}
|
||||
else
|
||||
{
|
||||
at_client_obj_recv(client, temp, bfsz - temp_size, timeout);
|
||||
}
|
||||
temp_size += sizeof(temp);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* sync receive data */
|
||||
if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
|
||||
{
|
||||
LOG_E("mw31 device(%s) receive size(%d) data failed.", device->name, bfsz);
|
||||
rt_free(recv_buf);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get at socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the receive buffer and buffer size */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz);
|
||||
}
|
||||
}
|
||||
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"+CIPEVENT:SOCKET", ",", urc_recv_func},
|
||||
{":SOCKET", ",", urc_recv_func},
|
||||
};
|
||||
|
||||
int mw31_socket_init(struct at_device *device)
|
||||
{
|
||||
RT_ASSERT(device);
|
||||
|
||||
/* register URC data execution function */
|
||||
at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
int mw31_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_MW31_SOCKETS_NUM;
|
||||
class->socket_ops = &mw31_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_MW31 && AT_USING_SOCKET */
|
|
@ -49,6 +49,7 @@ extern "C" {
|
|||
#define AT_DEVICE_CLASS_SIM800C 0x04U
|
||||
#define AT_DEVICE_CLASS_SIM76XX 0x05U
|
||||
#define AT_DEVICE_CLASS_RW007 0x06U
|
||||
#define AT_DEVICE_CLASS_MW31 0x07U
|
||||
|
||||
/* Options and Commands for AT device control opreations */
|
||||
#define AT_DEVICE_CTRL_POWER_ON 0x01L
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* File : at_sample_mw31.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2019-06-23 flybreak first version
|
||||
*/
|
||||
|
||||
#include <at_device_mw31.h>
|
||||
|
||||
#define LOG_TAG "at.sample"
|
||||
#include <at_log.h>
|
||||
|
||||
#define MW31_SAMPLE_DEIVCE_NAME "mw0"
|
||||
|
||||
static struct at_device_mw31 mw0 =
|
||||
{
|
||||
MW31_SAMPLE_DEIVCE_NAME,
|
||||
MW31_SAMPLE_CLIENT_NAME,
|
||||
|
||||
MW31_SAMPLE_WIFI_SSID,
|
||||
MW31_SAMPLE_WIFI_PASSWORD,
|
||||
MW31_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int mw31_device_register(void)
|
||||
{
|
||||
struct at_device_mw31 *mw31 = &mw0;
|
||||
|
||||
return at_device_register(&(mw31->device),
|
||||
mw31->device_name,
|
||||
mw31->client_name,
|
||||
AT_DEVICE_CLASS_MW31,
|
||||
(void *) mw31);
|
||||
}
|
||||
INIT_APP_EXPORT(mw31_device_register);
|
Loading…
Reference in New Issue