Merge remote-tracking branch 'rtt/master'
This commit is contained in:
commit
1818e4e76a
42
README.md
42
README.md
|
@ -1,8 +1,9 @@
|
|||
# AT device #
|
||||
# AT device #
|
||||
|
||||
## 1. 简介 ##
|
||||
|
||||
AT device 软件包是由 RT-Thread AT 组件针对不同 AT 设备的移植文件和示例代码组成,目前支持的 AT 设备有:ESP8266、M26、MC20、RW007、MW31、SIM800C 以及 SIM76XX 系列设备等,目前上述设备都完成对 `AT socket` 功能的移植,及设备通过 AT 命令实现标准 socket 编程接口,完成 socket 通讯的功能,具体功能介绍可参考 [《RT-Thread 编程指南》](https://www.rt-thread.org/document/site/programming-manual/at/at/)AT 命令章节 。
|
||||
|
||||
AT device 软件包是由 RT-Thread AT 组件针对不同 AT 设备的移植文件和示例代码组成,目前支持的 AT 设备有:ESP8266、ESP32、M26、MC20、RW007、MW31、SIM800C、W60X 、SIM76XX、A9/A9G、BC26 、AIR720、ME3616、M6315、BC28、EC200X系列设备等,目前上述设备都完成对 `AT socket` 功能的移植,及设备通过 AT 命令实现标准 socket 编程接口,完成 socket 通讯的功能,具体功能介绍可参考 [《RT-Thread 编程指南》](https://www.rt-thread.org/document/site/programming-manual/at/at/)AT 命令章节 。
|
||||
|
||||
### 1.1. 目录结构 ###
|
||||
|
||||
|
@ -13,12 +14,21 @@ AT device 软件包是由 RT-Thread AT 组件针对不同 AT 设备的移植文
|
|||
| sample | 不同设备示例文件目录 |
|
||||
| class | 不同设备针对 AT 组件的移植适配目录 |
|
||||
| class/esp8266 | ESP8266 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/esp32 | ESP32 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/rw007 | RW007 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/sim800c | SIM800C 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/sim76xx | SIM76XX 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/m26 | M26/MC20 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/ec20 | EC20 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/mw31 | MW31 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/w60x | W60X 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/a9g | A9G 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/bc26 | bc26 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/air720 | air720 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/me3616 | me3616 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/m6315 | m6315 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/bc28 | bc28 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
| class/ec200x | EC200T、EC200S 设备针对 AT 组件的移植目录,实现 AT Socket 功能 |
|
||||
### 1.2 许可证 ###
|
||||
|
||||
at_device package 遵循 LGPLv2.1 许可,详见 `LICENSE` 文件。
|
||||
|
@ -43,7 +53,8 @@ AT device 软件包目前已经发布多个版本,各个版本之间选项配
|
|||
- **V1.4.0**:适用于 RT-Thread 版本小于 V3.1.3或等于 V4.0.0, AT 组件版本等于 V1.2.0;
|
||||
- **V1.5.0**:适用于 RT-Thread 版本小于 V3.1.3 或等于 V4.0.0, AT 组件版本等于 V1.2.0;
|
||||
- **V1.6.0**:适用于 RT-Thread 版本等于 V3.1.3 或等于 V4.0.1, AT 组件版本等于 V1.2.0;
|
||||
- **laster**:只适用于 RT-Thread 版本大于 V4.0.1 或者大于 3.1.3, AT 组件版本大于 V1.2.0;
|
||||
- **V2.0.0/V2.0.1**:适用于 RT-Thread 版本大于 V4.0.1 或者大于 3.1.3, AT 组件版本等于 V1.3.0;
|
||||
- **laster**:只适用于 RT-Thread 版本大于 V4.0.1 或者大于 3.1.3, AT 组件版本等于 V1.3.0;
|
||||
|
||||
> 上述 版本判断在 menuconfig 中自动完成,at_device 软件包选择版本时会根据当前系统环境给出最佳版本支持,这里版本介绍作为运行环境参考。
|
||||
|
||||
|
@ -61,7 +72,7 @@ AT device 软件包目前已经发布多个版本,各个版本之间选项配
|
|||
Version (V1.6.0) --->
|
||||
|
||||
- **Enable at device init by thread**: 配置开启设备网络初始化是否通过创建线程完成;
|
||||
- **AT socket device modules**: AT 设备选择,目前支持 RW007、ESP8266、M26/MC20、EC20、SIM800C、SIM76XX 等设备;
|
||||
- **AT socket device modules**: AT 设备选择,目前支持 RW007、ESP8266、M26/MC20、EC20、SIM800C、SIM76XX、A9/A9G、BC26 、air720、ME3616、M6315、BC28、EC200X等设备;
|
||||
- **Version**: 下载软件包版本;
|
||||
|
||||
**V2.X.X (laster) 版本配置选项介绍**
|
||||
|
@ -80,6 +91,7 @@ RT-Thread online packages --->
|
|||
(uart3) AT client device name
|
||||
(512) The maximum length of receive line buffer
|
||||
[ ] Quectel EC20 --->
|
||||
[ ] Espressif ESP32 --->
|
||||
[*] Espressif ESP8266 --->
|
||||
[*] Enable initialize by thread
|
||||
[*] Enable sample
|
||||
|
@ -91,6 +103,14 @@ RT-Thread online packages --->
|
|||
[ ] SIMCom SIM800C --->
|
||||
[ ] SIMCom SIM76XX --->
|
||||
[ ] Notion MW31 --->
|
||||
[ ] WinnerMicro W60X --->
|
||||
[ ] AiThink A9/A9G --->
|
||||
[ ] Quectel BC26 --->
|
||||
[ ] Luat air720 --->
|
||||
[ ] GOSUNCN ME3616 --->
|
||||
[ ] ChinaMobile M6315 --->
|
||||
[ ] Quectel BC28 --->
|
||||
[ ] Quectel ec200x --->
|
||||
Version (latest) --->
|
||||
```
|
||||
|
||||
|
@ -109,10 +129,19 @@ RT-Thread online packages --->
|
|||
- **WIFI password**:配置该示例设备连接的 WIFI 密码;
|
||||
- **AT client device name**:配置该示例设备使用的串口设备名称;
|
||||
- **The maximum length of receive line buffer**:配置该示例设备最大一行接收的数据长度;
|
||||
- **Espressif ESP32**:开启 ESP32 (WIFI 模块)设备支持;
|
||||
- **Realthread RW007**:开启 RW007 (WIFI 模块)设备支持;
|
||||
- **SIMCom SIM800C**:开启 SIM800C (2G 模块)设备支持;
|
||||
- **SIMCom SIM76XX**:开启 SIM76XX (4G 模块)设备支持;
|
||||
- **Notion MW31**:开启 MW31 (WIFI 模块)设备支持;
|
||||
- **WinnerMicro W60X**:开启 W60X (WIFI 模块)设备支持;
|
||||
- **AiThink A9/A9G**:开启 A9/A9G (2G 模块)设备支持;
|
||||
- **Quectel BC26**:开启 BC26(NB-IOT 模块)设备支持;
|
||||
- **Luat Air720**:开启 air720(4g 模块)设备支持;
|
||||
- **GOSUNCN ME3616**:开启 ME3616(NB-IOT 模块)设备支持;
|
||||
- **ChinaMobile M6315**:开启 M6315 (2G 模块)设备支持;
|
||||
- **Quectel BC28**:开启 BC28(NB-IoT 模块)设备支持;
|
||||
- **Quectel EC200X**:开启 EC200T、EC200S(4G 模块)设备支持;
|
||||
- **Version** 下载软件包版本;
|
||||
|
||||
上面配置选项以 2G 模块和 WIFI 模块选项为例,介绍了`V2.X.X` 版本 AT device 软件包配置方式,如下几点值得注意:
|
||||
|
@ -153,6 +182,11 @@ RT-Thread Components --->
|
|||
- `laster` 版本支持多个选中多个 AT 设备接入实现 AT Socket 功能,`V1.X.X` 版本只支持单个 AT 设备接入。
|
||||
- AT device 软件包目前多个版本主要用于适配 AT 组件和系统的改动,推荐使用最新版本 RT-Thread 系统,并在 menuconfig 选项中选择 `latest` 版本;
|
||||
|
||||
## 4. 相关文档
|
||||
|
||||
- [AT 组件编程指南](https://www.rt-thread.org/document/site/programming-manual/at/at/)
|
||||
- [应用 AT 组件连接 ESP8266 模块](https://www.rt-thread.org/document/site/application-note/components/at/an0014-at-client/)
|
||||
|
||||
## 5. 联系方式
|
||||
|
||||
* 维护:RT-Thread 开发团队及社区开发者
|
||||
|
|
81
SConscript
81
SConscript
|
@ -4,6 +4,15 @@ cwd = GetCurrentDir()
|
|||
path = [cwd + '/inc']
|
||||
src = Glob('src/*.c')
|
||||
|
||||
# A9G
|
||||
if GetDepend(['AT_DEVICE_USING_A9G']):
|
||||
path += [cwd + '/class/a9g']
|
||||
src += Glob('class/a9g/at_device_a9g.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/a9g/at_socket_a9g.c')
|
||||
if GetDepend(['AT_DEVICE_A9G_SAMPLE']):
|
||||
src += Glob('samples/at_sample_a9g.c')
|
||||
|
||||
# M26/MC20
|
||||
if GetDepend(['AT_DEVICE_USING_M26']):
|
||||
path += [cwd + '/class/m26']
|
||||
|
@ -31,6 +40,15 @@ if GetDepend(['AT_DEVICE_USING_ESP8266']):
|
|||
if GetDepend(['AT_DEVICE_ESP8266_SAMPLE']):
|
||||
src += Glob('samples/at_sample_esp8266.c')
|
||||
|
||||
# ESP32
|
||||
if GetDepend(['AT_DEVICE_USING_ESP32']):
|
||||
path += [cwd + '/class/esp32']
|
||||
src += Glob('class/esp32/at_device_esp32.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/esp32/at_socket_esp32.c')
|
||||
if GetDepend(['AT_DEVICE_ESP32_SAMPLE']):
|
||||
src += Glob('samples/at_sample_esp32.c')
|
||||
|
||||
# MW31
|
||||
if GetDepend(['AT_DEVICE_USING_MW31']):
|
||||
path += [cwd + '/class/mw31']
|
||||
|
@ -67,6 +85,69 @@ if GetDepend(['AT_DEVICE_USING_SIM76XX']):
|
|||
if GetDepend(['AT_DEVICE_SIM76XX_SAMPLE']):
|
||||
src += Glob('samples/at_sample_sim76xx.c')
|
||||
|
||||
# W60X
|
||||
if GetDepend(['AT_DEVICE_USING_W60X']):
|
||||
path += [cwd + '/class/w60x']
|
||||
src += Glob('class/w60x/at_device_w60x.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/w60x/at_socket_w60x.c')
|
||||
if GetDepend(['AT_DEVICE_W60X_SAMPLE']):
|
||||
src += Glob('samples/at_sample_w60x.c')
|
||||
|
||||
# BC26
|
||||
if GetDepend(['AT_DEVICE_USING_BC26']):
|
||||
path += [cwd + '/class/bc26']
|
||||
src += Glob('class/bc26/at_device_bc26.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/bc26/at_socket_bc26.c')
|
||||
if GetDepend(['AT_DEVICE_BC26_SAMPLE']):
|
||||
src += Glob('samples/at_sample_bc26.c')
|
||||
|
||||
# air720
|
||||
if GetDepend(['AT_DEVICE_USING_AIR720']):
|
||||
path += [cwd + '/class/air720']
|
||||
src += Glob('class/air720/at_device_air720.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/air720/at_socket_air720.c')
|
||||
if GetDepend(['AT_DEVICE_AIR720_SAMPLE']):
|
||||
src += Glob('samples/at_sample_air720.c')
|
||||
|
||||
# ME3616
|
||||
if GetDepend(['AT_DEVICE_USING_ME3616']):
|
||||
path += [cwd + '/class/me3616']
|
||||
src += Glob('class/me3616/at_device_me3616.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/me3616/at_socket_me3616.c')
|
||||
if GetDepend(['AT_DEVICE_ME3616_SAMPLE']):
|
||||
src += Glob('samples/at_sample_me3616.c')
|
||||
|
||||
# M6315
|
||||
if GetDepend(['AT_DEVICE_USING_M6315']):
|
||||
path += [cwd + '/class/m6315']
|
||||
src += Glob('class/m6315/at_device_m6315.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/m6315/at_socket_m6315.c')
|
||||
if GetDepend(['AT_DEVICE_M6315_SAMPLE']):
|
||||
src += Glob('samples/at_sample_m6315.c')
|
||||
|
||||
# BC28
|
||||
if GetDepend(['AT_DEVICE_USING_BC28']):
|
||||
path += [cwd + '/class/bc28']
|
||||
src += Glob('class/bc28/at_device_bc28.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/bc28/at_socket_bc28.c')
|
||||
if GetDepend(['AT_DEVICE_BC28_SAMPLE']):
|
||||
src += Glob('samples/at_sample_bc28.c')
|
||||
|
||||
# EC200X
|
||||
if GetDepend(['AT_DEVICE_USING_EC200X']):
|
||||
path += [cwd + '/class/ec200x']
|
||||
src += Glob('class/ec200x/at_device_ec200x.c')
|
||||
if GetDepend(['AT_USING_SOCKET']):
|
||||
src += Glob('class/ec200x/at_socket_ec200x.c')
|
||||
if GetDepend(['AT_DEVICE_EC200X_SAMPLE']):
|
||||
src += Glob('samples/at_sample_ec200x.c')
|
||||
|
||||
group = DefineGroup('at_device', src, depend = ['PKG_USING_AT_DEVICE'], CPPPATH = path)
|
||||
|
||||
Return('group')
|
||||
|
|
|
@ -0,0 +1,923 @@
|
|||
/*
|
||||
* File : at_socket_a9g.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-11-23 luliang first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <at_device_a9g.h>
|
||||
|
||||
#define LOG_TAG "at.dev.a9g"
|
||||
#include <at_log.h>
|
||||
|
||||
#ifdef AT_DEVICE_USING_A9G
|
||||
|
||||
#define A9G_WAIT_CONNECT_TIME 5000
|
||||
#define A9G_THREAD_STACK_SIZE 2048
|
||||
#define A9G_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
|
||||
|
||||
/* AT+CSTT command default*/
|
||||
static char *CSTT_CHINA_MOBILE = "AT+CSTT=\"CMNET\"";
|
||||
static char *CSTT_CHINA_UNICOM = "AT+CSTT=\"UNINET\"";
|
||||
static char *CSTT_CHINA_TELECOM = "AT+CSTT=\"CTNET\"";
|
||||
|
||||
static void a9g_power_on(struct at_device *device)
|
||||
{
|
||||
struct at_device_a9g *a9g = RT_NULL;
|
||||
|
||||
a9g = (struct at_device_a9g *) device->user_data;
|
||||
|
||||
/* not nead to set pin configuration for m26 device power on */
|
||||
if (a9g->power_pin == -1 || a9g->power_status_pin == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (rt_pin_read(a9g->power_status_pin) == PIN_HIGH)
|
||||
{
|
||||
return;
|
||||
}
|
||||
rt_pin_write(a9g->power_pin, PIN_HIGH);
|
||||
|
||||
while (rt_pin_read(a9g->power_status_pin) == PIN_LOW)
|
||||
{
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
rt_pin_write(a9g->power_pin, PIN_LOW);
|
||||
}
|
||||
|
||||
static void a9g_power_off(struct at_device *device)
|
||||
{
|
||||
struct at_device_a9g *a9g = RT_NULL;
|
||||
|
||||
a9g = (struct at_device_a9g *) device->user_data;
|
||||
|
||||
/* not nead to set pin configuration for m26 device power on */
|
||||
if (a9g->power_pin == -1 || a9g->power_status_pin == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (rt_pin_read(a9g->power_status_pin) == PIN_LOW)
|
||||
{
|
||||
return;
|
||||
}
|
||||
rt_pin_write(a9g->power_pin, PIN_HIGH);
|
||||
|
||||
while (rt_pin_read(a9g->power_status_pin) == PIN_HIGH)
|
||||
{
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
rt_pin_write(a9g->power_pin, PIN_LOW);
|
||||
}
|
||||
|
||||
|
||||
/* ============================= a9g network interface operations ============================= */
|
||||
|
||||
/* set a9g network interface device status and address information */
|
||||
static int a9g_netdev_set_info(struct netdev *netdev)
|
||||
{
|
||||
#define A9G_IEMI_RESP_SIZE 32
|
||||
#define A9G_IPADDR_RESP_SIZE 32
|
||||
#define A9G_DNS_RESP_SIZE 96
|
||||
#define A9G_INFO_RESP_TIMO rt_tick_from_millisecond(300)
|
||||
|
||||
int result = RT_EOK;
|
||||
ip_addr_t addr;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* set network interface device status */
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_link_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
|
||||
|
||||
resp = at_create_resp(A9G_IEMI_RESP_SIZE, 0, A9G_INFO_RESP_TIMO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set network interface device hardware address(IEMI) */
|
||||
{
|
||||
#define A9G_NETDEV_HWADDR_LEN 8
|
||||
#define A9G_IEMI_LEN 15
|
||||
|
||||
char iemi[A9G_IEMI_LEN] = {0};
|
||||
int i = 0, j = 0;
|
||||
|
||||
/* send "AT+GSN" commond to get device IEMI */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+GSN") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 2, "%s", iemi) <= 0)
|
||||
{
|
||||
LOG_E("a9g device(%s) prase \"AT+GSN\" commands resposne data error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("a9g device(%s) IEMI number: %s", device->name, iemi);
|
||||
|
||||
netdev->hwaddr_len = A9G_NETDEV_HWADDR_LEN;
|
||||
/* get hardware address by IEMI */
|
||||
for (i = 0, j = 0; i < A9G_NETDEV_HWADDR_LEN && j < A9G_IEMI_LEN; i++, j += 2)
|
||||
{
|
||||
if (j != A9G_IEMI_LEN - 1)
|
||||
{
|
||||
netdev->hwaddr[i] = (iemi[j] - '0') * 10 + (iemi[j + 1] - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev->hwaddr[i] = (iemi[j] - '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* set network interface device IP address */
|
||||
{
|
||||
#define IP_ADDR_SIZE_MAX 16
|
||||
char ipaddr[IP_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
at_resp_set_info(resp, A9G_IPADDR_RESP_SIZE, 2, A9G_INFO_RESP_TIMO);
|
||||
|
||||
/* send "AT+CIFSR" commond to get IP address */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIFSR") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, ".", "%s", ipaddr) <= 0)
|
||||
{
|
||||
LOG_E("a9g device(%s) prase \"AT+CIFSR\" commands resposne data error!", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("a9g device(%s) IP address: %s", device->name, ipaddr);
|
||||
|
||||
/* set network interface address information */
|
||||
inet_aton(ipaddr, &addr);
|
||||
netdev_low_level_set_ipaddr(netdev, &addr);
|
||||
}
|
||||
|
||||
/* set network interface device dns server */
|
||||
{
|
||||
#define DNS_ADDR_SIZE_MAX 16
|
||||
char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
at_resp_set_info(resp, A9G_DNS_RESP_SIZE, 0, A9G_INFO_RESP_TIMO);
|
||||
|
||||
/* send "AT+CDNSCFG?" commond to get DNS servers address */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CDNSCFG?") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "PrimaryDns:", "PrimaryDns:%s", dns_server1) <= 0 ||
|
||||
at_resp_parse_line_args_by_kw(resp, "SecondaryDns:", "SecondaryDns:%s", dns_server2) <= 0)
|
||||
{
|
||||
LOG_E("Prase \"AT+CDNSCFG?\" commands resposne data error!");
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("a9g device(%s) primary DNS server address: %s", device->name, dns_server1);
|
||||
LOG_D("a9g device(%s) secondary DNS server address: %s", device->name, dns_server2);
|
||||
|
||||
inet_aton(dns_server1, &addr);
|
||||
netdev_low_level_set_dns_server(netdev, 0, &addr);
|
||||
|
||||
inet_aton(dns_server2, &addr);
|
||||
netdev_low_level_set_dns_server(netdev, 1, &addr);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void check_link_status_entry(void *parameter)
|
||||
{
|
||||
#define A9G_LINK_STATUS_OK 1
|
||||
#define A9G_LINK_RESP_SIZE 128
|
||||
#define A9G_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
|
||||
#define A9G_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
|
||||
|
||||
at_response_t resp = RT_NULL;
|
||||
int result_code, link_status;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct netdev *netdev = (struct netdev *)parameter;
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
resp = at_create_resp(A9G_LINK_RESP_SIZE, 0, A9G_LINK_RESP_TIMO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for response create.");
|
||||
return;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* send "AT+CREG?" commond to check netweork interface device link status */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CREG?") < 0)
|
||||
{
|
||||
rt_thread_mdelay(A9G_LINK_DELAY_TIME);
|
||||
continue;
|
||||
}
|
||||
link_status = -1;
|
||||
at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %d,%d", &result_code, &link_status);
|
||||
|
||||
/* check the network interface device link status */
|
||||
if ((A9G_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
|
||||
{
|
||||
netdev_low_level_set_link_status(netdev, (A9G_LINK_STATUS_OK == link_status));
|
||||
}
|
||||
rt_thread_mdelay(A9G_LINK_DELAY_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
static int a9g_netdev_check_link_status(struct netdev *netdev)
|
||||
{
|
||||
#define a9g_LINK_THREAD_TICK 20
|
||||
#define a9g_LINK_THREAD_STACK_SIZE 512
|
||||
#define a9g_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
|
||||
|
||||
rt_thread_t tid;
|
||||
char tname[RT_NAME_MAX] = {0};
|
||||
|
||||
if (netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("input network interface device is NULL.\n");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
|
||||
|
||||
tid = rt_thread_create(tname, check_link_status_entry, (void *) netdev,
|
||||
a9g_LINK_THREAD_STACK_SIZE, a9g_LINK_THREAD_PRIORITY, a9g_LINK_THREAD_TICK);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int a9g_net_init(struct at_device *device);
|
||||
|
||||
static int a9g_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 a9g device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_FALSE)
|
||||
{
|
||||
a9g_net_init(device);
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
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 a9g_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 a9g device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_TRUE)
|
||||
{
|
||||
a9g_power_off(device);
|
||||
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 a9g_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
|
||||
{
|
||||
#define a9g_DNS_RESP_LEN 8
|
||||
#define a9g_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
|
||||
|
||||
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 a9g device by netdev name(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(a9g_DNS_RESP_LEN, 0, a9g_DNS_RESP_TIMEO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_D("a9g set dns server failed, no memory for response object.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send "AT+CDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CDNSCFG=\"%s\"", inet_ntoa(*dns_server)) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int a9g_ping_domain_resolve(struct at_device *device, const char *name, char ip[16])
|
||||
{
|
||||
int result = RT_EOK;
|
||||
char recv_ip[16] = { 0 };
|
||||
at_response_t resp = RT_NULL;
|
||||
|
||||
/* The maximum response time is 14 seconds, affected by network status */
|
||||
resp = at_create_resp(512, 4, 14 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for a9g device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGACT=1,1") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%s\"", name) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", recv_ip) < 0)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
}
|
||||
|
||||
if (rt_strlen(recv_ip) < 8)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_strncpy(ip, recv_ip, 15);
|
||||
ip[15] = '\0';
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
#ifdef AT_DEVICE_USING_A9G
|
||||
static int a9g_netdev_ping(struct netdev *netdev, const char *host,
|
||||
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
|
||||
{
|
||||
rt_kprintf("I don't have PING function!\r\n");
|
||||
return RT_EOK;
|
||||
}
|
||||
#endif
|
||||
#endif /* NETDEV_USING_PING */
|
||||
|
||||
#ifdef NETDEV_USING_NETSTAT
|
||||
void a9g_netdev_netstat(struct netdev *netdev)
|
||||
{
|
||||
// TODO netstat support
|
||||
}
|
||||
#endif /* NETDEV_USING_NETSTAT */
|
||||
|
||||
const struct netdev_ops a9g_netdev_ops =
|
||||
{
|
||||
a9g_netdev_set_up,
|
||||
a9g_netdev_set_down,
|
||||
|
||||
RT_NULL, /* not support set ip, netmask, gatway address */
|
||||
a9g_netdev_set_dns_server,
|
||||
RT_NULL, /* not support set DHCP status */
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
a9g_netdev_ping,
|
||||
#endif
|
||||
#ifdef NETDEV_USING_NETSTAT
|
||||
a9g_netdev_netstat,
|
||||
#endif
|
||||
};
|
||||
|
||||
static struct netdev *a9g_netdev_add(const char *netdev_name)
|
||||
{
|
||||
#define A9G_NETDEV_MTU 1500
|
||||
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 a9g device(%s) netdev structure.", netdev_name);
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
netdev->mtu = A9G_NETDEV_MTU;
|
||||
netdev->ops = &a9g_netdev_ops;
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
/* ============================= a9g device operations ============================= */
|
||||
|
||||
#define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
|
||||
do { \
|
||||
(resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
|
||||
if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
|
||||
{ \
|
||||
result = -RT_ERROR; \
|
||||
goto __exit; \
|
||||
} \
|
||||
} while(0) \
|
||||
|
||||
/* init for a9g */
|
||||
static void a9g_init_thread_entry(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
#define CPIN_RETRY 10
|
||||
#define CSQ_RETRY 10
|
||||
#define CREG_RETRY 10
|
||||
#define CGREG_RETRY 20
|
||||
|
||||
int i, qimux, retry_num = INIT_RETRY;
|
||||
char parsed_data[10] = {0};
|
||||
rt_err_t result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = (struct at_device *)parameter;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for a9g device(%s) response structure.", device->name);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_D("start initializing the device(%s)", device->name);
|
||||
|
||||
while (retry_num--)
|
||||
{
|
||||
rt_memset(parsed_data, 0, sizeof(parsed_data));
|
||||
rt_thread_mdelay(500);
|
||||
a9g_power_on(device);
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* wait a9g startup finish */
|
||||
if (at_client_obj_wait_connect(client, A9G_WAIT_CONNECT_TIME))
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable echo */
|
||||
AT_SEND_CMD(client, resp, 0, 300, "ATE0");
|
||||
/* get module version */
|
||||
AT_SEND_CMD(client, resp, 5, 300, "ATI");
|
||||
/* show module version */
|
||||
for (i = 0; i < (int)resp->line_counts - 1; i++)
|
||||
{
|
||||
LOG_D("%s", at_resp_get_line(resp, i + 1));
|
||||
}
|
||||
/* check SIM card */
|
||||
for (i = 0; i < CPIN_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 2, 10 * 1000, "AT+CCID");
|
||||
if (at_resp_get_line_by_kw(resp, "+CCID:"))
|
||||
{
|
||||
LOG_D("a9g device(%s) SIM card detection success.", device->name);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CPIN_RETRY)
|
||||
{
|
||||
LOG_E("a9g device(%s) SIM card detection failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* make the device restart */
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+RST=1");
|
||||
for(i = 0; i<=30; i++)
|
||||
{
|
||||
i++;
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
|
||||
/* waiting for dirty data to be digested */
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* check the GSM network is registered */
|
||||
for (i = 0; i < CREG_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
|
||||
at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
|
||||
if (!strncmp(parsed_data, "1,1", sizeof(parsed_data)) ||
|
||||
!strncmp(parsed_data, "1,5", sizeof(parsed_data)))
|
||||
{
|
||||
LOG_D("a9g device(%s) GSM network is registered(%s),", device->name, parsed_data);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000 + 500 * (i+1));
|
||||
}
|
||||
if (i == CREG_RETRY)
|
||||
{
|
||||
LOG_E("a9g device(%s) GSM network is register failed(%s).", device->name, parsed_data);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* the device default response timeout is 40 seconds, but it set to 15 seconds is convenient to use. */
|
||||
for (uint8_t ii = 0; ii < INIT_RETRY; ii++)
|
||||
{
|
||||
resp = at_resp_set_info(resp, 128, 0, rt_tick_from_millisecond(10 * 1000));
|
||||
if (at_obj_exec_cmd(client, resp, "AT+CGATT=0") == RT_EOK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
|
||||
/* check the GPRS network is registered */
|
||||
for (i = 0; i < CGREG_RETRY; i++)
|
||||
{
|
||||
for (uint8_t ii = 0; ii < INIT_RETRY; ii++)
|
||||
{
|
||||
//AT_SEND_CMD(client, resp, 0, 5 * 1000, "AT+CGATT=1");
|
||||
resp = at_resp_set_info(resp, 128, 0, rt_tick_from_millisecond(10 * 1000));
|
||||
if (at_obj_exec_cmd(client, resp, "AT+CGATT=1") == RT_EOK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
|
||||
AT_SEND_CMD(client, resp, 0, 1000, "AT+CGDCONT=1,\"IP\",\"CMNET\"");
|
||||
rt_thread_mdelay(10);
|
||||
AT_SEND_CMD(client, resp, 0, 5 * 1000, "AT+CGACT=1,1");
|
||||
rt_thread_mdelay(10);
|
||||
at_resp_parse_line_args_by_kw(resp, "OK", "%s", &parsed_data);
|
||||
|
||||
if (!strncmp(parsed_data, "OK", sizeof(parsed_data)))
|
||||
{
|
||||
LOG_D("a9g device(%s) GPRS network is registered(%s).", device->name, parsed_data);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CGREG_RETRY)
|
||||
{
|
||||
LOG_E("a9g device(%s) GPRS network is register failed(%s).", device->name, parsed_data);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check signal strength */
|
||||
for (i = 0; i < CSQ_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+CSQ");
|
||||
at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
|
||||
if (strncmp(parsed_data, "99,99", sizeof(parsed_data)))
|
||||
{
|
||||
LOG_D("a9g device(%s) signal strength: %s", device->name, parsed_data);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CSQ_RETRY)
|
||||
{
|
||||
LOG_E("a9g device(%s) signal strength check failed (%s)", device->name, parsed_data);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Set to multiple connections */
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+CIPMUX?");
|
||||
|
||||
at_resp_parse_line_args_by_kw(resp, "+CIPMUX:", "+CIPMUX:%d", &qimux);
|
||||
|
||||
if (qimux == 0)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 1 * 1000, "AT+CIPMUX=1");
|
||||
}
|
||||
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+COPS?");
|
||||
at_resp_parse_line_args_by_kw(resp, "+COPS:", "+COPS: %*[^\"]\"%[^\"]", &parsed_data);
|
||||
if (rt_strcmp(parsed_data, "CHINA MOBILE") == 0)
|
||||
{
|
||||
/* "CMCC" */
|
||||
LOG_I("a9g device(%s) network operator: %s", device->name, parsed_data);
|
||||
AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_MOBILE);
|
||||
}
|
||||
else if (rt_strcmp(parsed_data, "CHN-UNICOM") == 0)
|
||||
{
|
||||
/* "UNICOM" */
|
||||
LOG_I("a9g device(%s) network operator: %s", device->name, parsed_data);
|
||||
AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_UNICOM);
|
||||
}
|
||||
else if (rt_strcmp(parsed_data, "CHN-CT") == 0)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_TELECOM);
|
||||
/* "CT" */
|
||||
LOG_I("a9g device(%s) network operator: %s", device->name, parsed_data);
|
||||
}
|
||||
|
||||
AT_SEND_CMD(client, resp, 2, 300, "AT+CIFSR");
|
||||
if (at_resp_get_line_by_kw(resp, "ERROR") != RT_NULL)
|
||||
{
|
||||
LOG_E("a9g device(%s) get the local address failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
#ifdef AT_USING_A9G_GPS
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+GPS?");
|
||||
at_resp_parse_line_args_by_kw(resp, "+GPS:", "+GPS: %d", &qimux);
|
||||
if (qimux == 0)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+GPS=1");
|
||||
}
|
||||
#endif
|
||||
result = RT_EOK;
|
||||
|
||||
__exit:
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* power off the a9g device */
|
||||
a9g_power_off(device);
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
LOG_I("a9g device(%s) initialize retry...", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
/* set network interface device status and address information */
|
||||
a9g_netdev_set_info(device->netdev);
|
||||
/* */
|
||||
a9g_netdev_check_link_status(device->netdev);
|
||||
/* */
|
||||
LOG_I("a9g device(%s) network initialize success!", device->name);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
device->is_init = RT_FALSE;
|
||||
|
||||
netdev_low_level_set_status(device->netdev, RT_FALSE);
|
||||
LOG_E("a9g device(%s) network initialize failed(%d)!", device->name, result);
|
||||
}
|
||||
}
|
||||
|
||||
static int a9g_net_init(struct at_device *device)
|
||||
{
|
||||
#ifdef AT_DEVICE_A9G_INIT_ASYN
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("a9g_net_init", a9g_init_thread_entry, (void *)device,
|
||||
A9G_THREAD_STACK_SIZE, A9G_THREAD_PRIORITY, 20);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("create a9g device(%s) initialization thread failed.", device->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#else
|
||||
a9g_init_thread_entry(device);
|
||||
#endif /* AT_DEVICE_a9g_INIT_ASYN */
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void urc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data);
|
||||
|
||||
LOG_I("URC data : %.*s", size, data);
|
||||
}
|
||||
|
||||
/* a9g device URC table for the device control */
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"READY", "\r\n", urc_func},
|
||||
};
|
||||
|
||||
static int a9g_init(struct at_device *device)
|
||||
{
|
||||
struct at_device_a9g *a9g = (struct at_device_a9g *) device->user_data;
|
||||
|
||||
/* initialize AT client */
|
||||
at_client_init(a9g->client_name, a9g->recv_line_num);
|
||||
|
||||
device->client = at_client_get(a9g->client_name);
|
||||
if (device->client == RT_NULL)
|
||||
{
|
||||
LOG_E("a9g device(%s) initialize failed, get AT client(%s) failed.", a9g->device_name, a9g->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
|
||||
a9g_socket_init(device);
|
||||
#endif
|
||||
|
||||
/* add a9g device to the netdev list */
|
||||
device->netdev = a9g_netdev_add(a9g->device_name);
|
||||
if (device->netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("a9g device(%s) initialize failed, get network interface device failed.", a9g->device_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize a9g pin configuration */
|
||||
if (a9g->power_pin != -1 && a9g->power_status_pin != -1)
|
||||
{
|
||||
rt_pin_mode(a9g->power_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_mode(a9g->power_status_pin, PIN_MODE_INPUT);
|
||||
}
|
||||
|
||||
/* initialize a9g device network */
|
||||
return a9g_netdev_set_up(device->netdev);
|
||||
}
|
||||
|
||||
static int a9g_deinit(struct at_device *device)
|
||||
{
|
||||
return a9g_netdev_set_down(device->netdev);
|
||||
}
|
||||
|
||||
static int a9g_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_RESET:
|
||||
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_SET_WIFI_INFO:
|
||||
case AT_DEVICE_CTRL_GET_SIGNAL:
|
||||
case AT_DEVICE_CTRL_GET_GPS:
|
||||
case AT_DEVICE_CTRL_GET_VER:
|
||||
LOG_W("a9g not support the control command(%d).", cmd);
|
||||
break;
|
||||
default:
|
||||
LOG_E("input error control command(%d).", cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const struct at_device_ops a9g_device_ops =
|
||||
{
|
||||
a9g_init,
|
||||
a9g_deinit,
|
||||
a9g_control,
|
||||
};
|
||||
|
||||
static int a9g_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 a9g device class create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* fill a9g device class object */
|
||||
#ifdef AT_USING_SOCKET
|
||||
a9g_socket_class_register(class);
|
||||
#endif
|
||||
class->device_ops = &a9g_device_ops;
|
||||
|
||||
return at_device_class_register(class, AT_DEVICE_CLASS_A9G);
|
||||
}
|
||||
INIT_DEVICE_EXPORT(a9g_device_class_register);
|
||||
|
||||
#endif /* AT_DEVICE_USING_a9g */
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* File : at_device_a9g.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-11-23 luliang first version
|
||||
*/
|
||||
|
||||
#ifndef __A9G_DEVICE_H__
|
||||
#define __A9G_DEVICE_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the a9g device */
|
||||
#define AT_DEVICE_A9G_SOCKETS_NUM 8
|
||||
|
||||
struct at_device_a9g
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
int power_pin;
|
||||
int power_status_pin;
|
||||
size_t recv_line_num;
|
||||
struct at_device device;
|
||||
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* a9g device socket initialize */
|
||||
int a9g_socket_init(struct at_device *device);
|
||||
|
||||
/* a9g device class socket register */
|
||||
int a9g_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* __AT_DEVICE_A9G_H__*/
|
|
@ -0,0 +1,623 @@
|
|||
/*
|
||||
* File : at_socket_a9g.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-11-23 luliang first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_a9g.h>
|
||||
|
||||
#define LOG_TAG "at.skt.a9g"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_A9G) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define A9G_MODULE_SEND_MAX_SIZE 1000
|
||||
|
||||
/* set real event by current socket and current state */
|
||||
#define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
|
||||
|
||||
/* AT socket event type */
|
||||
#define A9G_EVENT_CONN_OK (1L << 0)
|
||||
#define A9G_EVENT_SEND_OK (1L << 1)
|
||||
#define A9G_EVENT_RECV_OK (1L << 2)
|
||||
#define A9G_EVENT_CLOSE_OK (1L << 3)
|
||||
#define A9G_EVENT_CONN_FAIL (1L << 4)
|
||||
#define A9G_EVENT_SEND_FAIL (1L << 5)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static int a9g_socket_event_send(struct at_device *device, uint32_t event)
|
||||
{
|
||||
return (int) rt_event_send(device->socket_event, event);
|
||||
}
|
||||
|
||||
static int a9g_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_uint32_t recved;
|
||||
|
||||
result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
return recved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 a9g_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;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(600));
|
||||
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for a9g device(%s) response creat.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE=%d", device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__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 a9g_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
rt_bool_t retryed = RT_FALSE;
|
||||
at_response_t resp = RT_NULL;
|
||||
int result = RT_EOK, event_result = 0;
|
||||
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 a9g device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
__retry:
|
||||
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, A9G_EVENT_CONN_OK | A9G_EVENT_CONN_FAIL);
|
||||
a9g_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (is_client)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
/* send AT commands(eg: AT+QIOPEN=0,"TCP","x.x.x.x", 1234) to connect TCP server */
|
||||
if (at_obj_exec_cmd(device->client, RT_NULL, "AT+CIPSTART=\"TCP\",\"%s\",%d", ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
break;
|
||||
|
||||
case AT_SOCKET_UDP:
|
||||
if (at_obj_exec_cmd(device->client, RT_NULL, "AT+CIPSTART=\"UDP\",\"%s\",%d", ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG_E("a9g device(%s) not supported connect type : %d.", device->name, type);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use */
|
||||
if (a9g_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("a9g device(%s) socket(%d) connect failed, wait connect result timeout.",device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = a9g_socket_event_recv(device,
|
||||
A9G_EVENT_CONN_OK | A9G_EVENT_CONN_FAIL, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("a9g device(%s) socket(%d) connect failed, wait connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & A9G_EVENT_CONN_FAIL)
|
||||
{
|
||||
if (retryed == RT_FALSE)
|
||||
{
|
||||
LOG_D("a9g 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 (a9g_socket_close(socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
retryed = RT_TRUE;
|
||||
goto __retry;
|
||||
}
|
||||
LOG_E("a9g device(%s) socket(%d) connect failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__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 a9g_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;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
|
||||
resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for a9g device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* set AT client end sign to deal with '>' sign.*/
|
||||
at_obj_set_end_sign(device->client, '>');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < A9G_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = A9G_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line. */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* waiting OK or failed result */
|
||||
at_resp_set_info(resp, 128, 0, 30 * RT_TICK_PER_SECOND);
|
||||
if (at_obj_exec_cmd(device->client, resp, "") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
at_resp_set_info(resp, 128, 2, 5 * RT_TICK_PER_SECOND);
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reset the end sign for data conflict */
|
||||
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
|
||||
* -1: send AT commands error or response error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int a9g_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 5
|
||||
|
||||
int i, result = RT_EOK;
|
||||
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 a9g device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* The maximum response time is 14 seconds, affected by network status */
|
||||
resp = at_create_resp(1024, 3, 14 * RT_TICK_PER_SECOND);
|
||||
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for a9g device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
for (i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
int err_code = 0;
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%s\"", name) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* domain name prase error options */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP:", "+CDNSGIP: 0,%d", &err_code) > 0)
|
||||
{
|
||||
/* 3 - network error, 8 - dns common error */
|
||||
if (err_code == 3 || err_code == 8)
|
||||
{
|
||||
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, "+CDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", 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_thread_mdelay(10);
|
||||
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 a9g_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 void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get a9g device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (strstr(data, "CONNECT OK"))
|
||||
{
|
||||
a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_CONN_OK));
|
||||
}
|
||||
else if (strstr(data, "CONNECT FAIL"))
|
||||
{
|
||||
a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_CONN_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get a9g device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (rt_strstr(data, "SEND OK"))
|
||||
{
|
||||
a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_SEND_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "SEND FAIL"))
|
||||
{
|
||||
a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_SEND_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get a9g device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (rt_strstr(data, "CLOSE OK"))
|
||||
{
|
||||
a9g_socket_event_send(device, SET_EVENT(device_socket, A9G_EVENT_CLOSE_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "CLOSED"))
|
||||
{
|
||||
struct at_socket *socket = RT_NULL;
|
||||
|
||||
/* get AT socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout;
|
||||
rt_size_t temp_size = 0;
|
||||
char temp[8] = {0};
|
||||
rt_size_t bfsz = 0;
|
||||
char *recv_buf = RT_NULL;
|
||||
struct at_socket *socket = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
/* get the current socket and receive buffer size by receive data */
|
||||
sscanf(data, "+CIPRCV,%d,%d:", &device_socket, (int *) &bfsz);
|
||||
/* get receive timeout by receive buffer length */
|
||||
timeout = bfsz;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get a9g device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for a9g device(%s) URC receive buffer (%d).", device->name, bfsz);
|
||||
temp_size = (size-(rt_strstr(data,":")+1-data)-2);
|
||||
/* 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 */
|
||||
temp_size = (size-(rt_strstr(data,":")+1-data)-2);
|
||||
rt_memcpy(recv_buf,rt_strstr(data,":")+1,temp_size);
|
||||
if (at_client_obj_recv(client, recv_buf+temp_size, bfsz-temp_size, timeout) != (bfsz-temp_size))
|
||||
{
|
||||
LOG_E("a9g 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);
|
||||
}
|
||||
}
|
||||
|
||||
/* A9G device URC table for the socket data */
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"", "CONNECT OK\r\n", urc_connect_func},
|
||||
{"", ",CONNECT FAIL\r\n", urc_connect_func},
|
||||
{"", ",SEND FAIL\r\n", urc_send_func},
|
||||
{"CLOSED:", "\r\n", urc_close_func},
|
||||
{"+CIPRCV,", "\r\n", urc_recv_func},
|
||||
};
|
||||
|
||||
static const struct at_socket_ops a9g_socket_ops =
|
||||
{
|
||||
a9g_socket_connect,
|
||||
a9g_socket_close,
|
||||
a9g_socket_send,
|
||||
a9g_domain_resolve,
|
||||
a9g_socket_set_event_cb,
|
||||
};
|
||||
|
||||
int a9g_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 a9g_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_A9G_SOCKETS_NUM;
|
||||
class->socket_ops = &a9g_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_A9G && AT_USING_SOCKET */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* File : at_device_air720.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-05-16 chenyong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_AIR720_H__
|
||||
#define __AT_DEVICE_AIR720_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the air720 device */
|
||||
#define AT_DEVICE_AIR720_SOCKETS_NUM 6
|
||||
|
||||
struct at_device_air720
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
int power_pin;
|
||||
int power_status_pin;
|
||||
size_t recv_line_num;
|
||||
struct at_device device;
|
||||
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* air720 device socket initialize */
|
||||
int air720_socket_init(struct at_device *device);
|
||||
|
||||
/* air720 device class socket register */
|
||||
int air720_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_AIR720_H__ */
|
|
@ -0,0 +1,648 @@
|
|||
/*
|
||||
* File : at_socket_air720.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
|
||||
* 2018-06-12 malongwei first version
|
||||
* 2019-05-13 chenyong multi AT socket client support
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <at_device_air720.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_AIR720) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define AIR720_MODULE_SEND_MAX_SIZE 1000
|
||||
|
||||
/* set real event by current socket and current state */
|
||||
#define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
|
||||
|
||||
/* AT socket event type */
|
||||
#define AIR720_EVENT_CONN_OK (1L << 0)
|
||||
#define AIR720_EVENT_SEND_OK (1L << 1)
|
||||
#define AIR720_EVENT_RECV_OK (1L << 2)
|
||||
#define AIR720_EVNET_CLOSE_OK (1L << 3)
|
||||
#define AIR720_EVENT_CONN_FAIL (1L << 4)
|
||||
#define AIR720_EVENT_SEND_FAIL (1L << 5)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static int air720_socket_event_send(struct at_device *device, uint32_t event)
|
||||
{
|
||||
return (int)rt_event_send(device->socket_event, event);
|
||||
}
|
||||
|
||||
static int air720_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_uint32_t recved;
|
||||
|
||||
result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
return recved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 air720_socket_close(struct at_socket *socket)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
int result = RT_EOK;
|
||||
int device_socket = (int)socket->user_data;
|
||||
struct at_device *device = (struct at_device *)socket->device;
|
||||
|
||||
/* clear socket close event */
|
||||
event = SET_EVENT(device_socket, AIR720_EVNET_CLOSE_OK);
|
||||
air720_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (at_obj_exec_cmd(device->client, NULL, "AT+CIPCLOSE=%d", device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (air720_socket_event_recv(device, event, rt_tick_from_millisecond(300 * 3), RT_EVENT_FLAG_AND) < 0)
|
||||
{
|
||||
LOG_E("air720 device(%s) socket(%d) close failed, wait close OK timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
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 air720_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
rt_bool_t retryed = RT_FALSE;
|
||||
at_response_t resp = RT_NULL;
|
||||
int result = RT_EOK, event_result = 0;
|
||||
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 air720 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
__retry:
|
||||
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, AIR720_EVENT_CONN_OK | AIR720_EVENT_CONN_FAIL);
|
||||
air720_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (is_client)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
/* send AT commands(eg: AT+QIOPEN=0,"TCP","x.x.x.x", 1234) to connect TCP server */
|
||||
if (at_obj_exec_cmd(device->client, RT_NULL,
|
||||
"AT+CIPSTART=%d,\"TCP\",\"%s\",%d", device_socket, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
break;
|
||||
|
||||
case AT_SOCKET_UDP:
|
||||
if (at_obj_exec_cmd(device->client, RT_NULL,
|
||||
"AT+CIPSTART=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG_E("air720 device(%s) not supported connect type : %d.", device->name, type);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use */
|
||||
if (air720_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("air720 device(%s) socket(%d) connect failed, wait connect result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = air720_socket_event_recv(device,
|
||||
AIR720_EVENT_CONN_OK | AIR720_EVENT_CONN_FAIL, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("air720 device(%s) socket(%d) connect failed, wait connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & AIR720_EVENT_CONN_FAIL)
|
||||
{
|
||||
if (retryed == RT_FALSE)
|
||||
{
|
||||
LOG_D("air720 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 (air720_socket_close(socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
retryed = RT_TRUE;
|
||||
goto __retry;
|
||||
}
|
||||
LOG_E("air720 device(%s) socket(%d) connect failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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 air720_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
int result = RT_EOK, event_result = 0;
|
||||
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;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
|
||||
resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for air720 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, AIR720_EVENT_SEND_OK | AIR720_EVENT_SEND_FAIL);
|
||||
air720_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
/* set AT client end sign to deal with '>' sign.*/
|
||||
at_obj_set_end_sign(device->client, '>');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < AIR720_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = AIR720_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC */
|
||||
if (air720_socket_event_recv(device, SET_EVENT(device_socket, 0), 15 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("air720 device(%s) socket(%d) send failed, wait connect result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = air720_socket_event_recv(device,
|
||||
AIR720_EVENT_SEND_OK | AIR720_EVENT_SEND_FAIL, 5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("air720 device(%s) socket(%d) send failed, wait connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & AIR720_EVENT_SEND_FAIL)
|
||||
{
|
||||
LOG_E("air720 device(%s) socket(%d) send failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reset the end sign for data conflict */
|
||||
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
|
||||
* -1: send AT commands error or response error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int air720_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 5
|
||||
|
||||
int i, result = RT_EOK;
|
||||
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 air720 device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* The maximum response time is 14 seconds, affected by network status */
|
||||
resp = at_create_resp(128, 4, 14 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for air720 device(%s) response structure.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
for (i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
int err_code = 0;
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%s\"", name) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* domain name prase error options */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CDNSGIP: 0", "+CDNSGIP: 0,%d", &err_code) > 0)
|
||||
{
|
||||
/* 3 - network error, 8 - dns common error */
|
||||
if (err_code == 3 || err_code == 8)
|
||||
{
|
||||
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, "+CDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", 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_thread_mdelay(10);
|
||||
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 air720_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 void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get air720 device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (strstr(data, "CONNECT OK"))
|
||||
{
|
||||
air720_socket_event_send(device, SET_EVENT(device_socket, AIR720_EVENT_CONN_OK));
|
||||
}
|
||||
else if (strstr(data, "CONNECT FAIL"))
|
||||
{
|
||||
air720_socket_event_send(device, SET_EVENT(device_socket, AIR720_EVENT_CONN_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get air720 device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (rt_strstr(data, "SEND OK"))
|
||||
{
|
||||
air720_socket_event_send(device, SET_EVENT(device_socket, AIR720_EVENT_SEND_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "SEND FAIL"))
|
||||
{
|
||||
air720_socket_event_send(device, SET_EVENT(device_socket, AIR720_EVENT_SEND_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get air720 device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (rt_strstr(data, "CLOSE OK"))
|
||||
{
|
||||
air720_socket_event_send(device, SET_EVENT(device_socket, AIR720_EVNET_CLOSE_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "CLOSED"))
|
||||
{
|
||||
struct at_socket *socket = RT_NULL;
|
||||
|
||||
/* get AT socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout;
|
||||
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_ASSERT(data && size);
|
||||
|
||||
/* get the current socket and receive buffer size by receive data */
|
||||
sscanf(data, "+RECEIVE,%d,%d:", &device_socket, (int *)&bfsz);
|
||||
/* get receive timeout by receive buffer length */
|
||||
timeout = bfsz;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get air720 device by client name(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
recv_buf = (char *)rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for air720 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("air720 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);
|
||||
}
|
||||
}
|
||||
|
||||
/* air720 device URC table for the socket data */
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"", ", CONNECT OK\r\n", urc_connect_func},
|
||||
{"", ", CONNECT FAIL\r\n", urc_connect_func},
|
||||
{"", ", SEND OK\r\n", urc_send_func},
|
||||
{"", ", SEND FAIL\r\n", urc_send_func},
|
||||
{"", ", CLOSE OK\r\n", urc_close_func},
|
||||
{"", ", CLOSED\r\n", urc_close_func},
|
||||
{"+RECEIVE,", "\r\n", urc_recv_func},
|
||||
};
|
||||
|
||||
static const struct at_socket_ops air720_socket_ops =
|
||||
{
|
||||
air720_socket_connect,
|
||||
air720_socket_close,
|
||||
air720_socket_send,
|
||||
air720_domain_resolve,
|
||||
air720_socket_set_event_cb,
|
||||
};
|
||||
|
||||
int air720_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 air720_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_AIR720_SOCKETS_NUM;
|
||||
class->socket_ops = &air720_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_air720 && AT_USING_SOCKET */
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* File : at_device_bc26.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-12-13 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_BC26_H__
|
||||
#define __AT_DEVICE_BC26_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the bc26 device */
|
||||
#define AT_DEVICE_BC26_SOCKETS_NUM 5
|
||||
|
||||
struct at_device_bc26
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
int power_pin;
|
||||
int power_status_pin;
|
||||
size_t recv_line_num;
|
||||
struct at_device device;
|
||||
|
||||
void *socket_data;
|
||||
void *user_data;
|
||||
|
||||
rt_bool_t power_status;
|
||||
rt_bool_t sleep_status;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* bc26 device socket initialize */
|
||||
int bc26_socket_init(struct at_device *device);
|
||||
|
||||
/* bc26 device class socket register */
|
||||
int bc26_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_BC26_H__ */
|
||||
|
|
@ -0,0 +1,790 @@
|
|||
/*
|
||||
* File : at_socket_bc26.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-12-10 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_bc26.h>
|
||||
|
||||
#define LOG_TAG "at.skt.bc26"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_BC26) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define BC26_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 BC26_EVENT_CONN_OK (1L << 0)
|
||||
#define BC26_EVENT_SEND_OK (1L << 1)
|
||||
#define BC26_EVENT_RECV_OK (1L << 2)
|
||||
#define BC26_EVNET_CLOSE_OK (1L << 3)
|
||||
#define BC26_EVENT_CONN_FAIL (1L << 4)
|
||||
#define BC26_EVENT_SEND_FAIL (1L << 5)
|
||||
#define BC26_EVENT_DOMAIN_OK (1L << 6)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static void at_tcp_ip_errcode_parse(int result)//TCP/IP_QIGETERROR
|
||||
{
|
||||
switch(result)
|
||||
{
|
||||
case 0 : LOG_D("%d : Operation successful", result); break;
|
||||
case 550 : LOG_E("%d : Unknown error", result); break;
|
||||
case 551 : LOG_E("%d : Operation blocked", result); break;
|
||||
case 552 : LOG_E("%d : Invalid parameters", result); break;
|
||||
case 553 : LOG_E("%d : Memory not enough", result); break;
|
||||
case 554 : LOG_E("%d : Create socket failed", result); break;
|
||||
case 555 : LOG_E("%d : Operation not supported", result); break;
|
||||
case 556 : LOG_E("%d : Socket bind failed", result); break;
|
||||
case 557 : LOG_E("%d : Socket listen failed", result); break;
|
||||
case 558 : LOG_E("%d : Socket write failed", result); break;
|
||||
case 559 : LOG_E("%d : Socket read failed", result); break;
|
||||
case 560 : LOG_E("%d : Socket accept failed", result); break;
|
||||
case 561 : LOG_E("%d : Open PDP context failed", result); break;
|
||||
case 562 : LOG_E("%d : Close PDP context failed", result); break;
|
||||
case 563 : LOG_W("%d : Socket identity has been used", result); break;
|
||||
case 564 : LOG_E("%d : DNS busy", result); break;
|
||||
case 565 : LOG_E("%d : DNS parse failed", result); break;
|
||||
case 566 : LOG_E("%d : Socket connect failed", result); break;
|
||||
// case 567 : LOG_W("%d : Socket has been closed", result); break;
|
||||
case 567 : break;
|
||||
case 568 : LOG_E("%d : Operation busy", result); break;
|
||||
case 569 : LOG_E("%d : Operation timeout", result); break;
|
||||
case 570 : LOG_E("%d : PDP context broken down", result); break;
|
||||
case 571 : LOG_E("%d : Cancel send", result); break;
|
||||
case 572 : LOG_E("%d : Operation not allowed", result); break;
|
||||
case 573 : LOG_E("%d : APN not configured", result); break;
|
||||
case 574 : LOG_E("%d : Port busy", result); break;
|
||||
default : LOG_E("%d : Unknown err code", result); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int bc26_socket_event_send(struct at_device *device, uint32_t event)
|
||||
{
|
||||
return (int) rt_event_send(device->socket_event, event);
|
||||
}
|
||||
|
||||
static int bc26_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_uint32_t recved;
|
||||
|
||||
result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
return recved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 bc26_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;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+QICLOSE=%d", device_socket);
|
||||
|
||||
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 bc26_socket_connect(struct at_socket *socket, char *ip, int32_t port,
|
||||
enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
#define CONN_RETRY 2
|
||||
|
||||
int i = 0;
|
||||
const char *type_str = RT_NULL;
|
||||
uint32_t event = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int result = 0, event_result = 0;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
RT_ASSERT(ip);
|
||||
RT_ASSERT(port >= 0);
|
||||
|
||||
if ( ! is_client)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
type_str = "TCP";
|
||||
break;
|
||||
case AT_SOCKET_UDP:
|
||||
type_str = "UDP";
|
||||
break;
|
||||
default:
|
||||
LOG_E("%s device socket(%d) connect type error.", device->name, device_socket);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
for(i=0; i<CONN_RETRY; i++)
|
||||
{
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, BC26_EVENT_CONN_OK | BC26_EVENT_CONN_FAIL);
|
||||
bc26_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIOPEN=1,%d,\"%s\",\"%s\",%d,0,1",
|
||||
device_socket, type_str, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC, the device default connection timeout is 60 seconds*/
|
||||
if (bc26_socket_event_recv(device, SET_EVENT(device_socket, 0),
|
||||
60 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
break;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = bc26_socket_event_recv(device, BC26_EVENT_CONN_OK | BC26_EVENT_CONN_FAIL,
|
||||
1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
break;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & BC26_EVENT_CONN_OK)
|
||||
{
|
||||
result = RT_EOK;
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_D("%s device socket(%d) connect failed, the socket was not be closed and now will connect retry.",
|
||||
device->name, device_socket);
|
||||
if (bc26_socket_close(socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == CONN_RETRY)
|
||||
{
|
||||
LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_get_send_size(struct at_socket *socket, size_t *size, size_t *acked, size_t *nacked)
|
||||
{
|
||||
int result = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,0", device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+QISEND:", "+QISEND: %d,%d,%d", size, acked, nacked) <= 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_wait_send_finish(struct at_socket *socket, size_t settings_size)
|
||||
{
|
||||
/* get the timeout by the input data size */
|
||||
rt_tick_t timeout = rt_tick_from_millisecond(settings_size);
|
||||
rt_tick_t last_time = rt_tick_get();
|
||||
size_t size = 0, acked = 0, nacked = 0xFFFF;
|
||||
|
||||
while (rt_tick_get() - last_time <= timeout)
|
||||
{
|
||||
at_get_send_size(socket, &size, &acked, &nacked);
|
||||
if (nacked == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
rt_thread_mdelay(100);
|
||||
}
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 bc26_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
int result = 0, event_result = 0;
|
||||
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_bc26 *bc26 = (struct at_device_bc26 *) device->user_data;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
|
||||
resp = at_create_resp(128, 2, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* set current socket for send URC event */
|
||||
bc26->user_data = (void *) device_socket;
|
||||
|
||||
/* clear socket send event */
|
||||
event = SET_EVENT(device_socket, BC26_EVENT_SEND_OK | BC26_EVENT_SEND_FAIL);
|
||||
bc26_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
/* set AT client end sign to deal with '>' sign.*/
|
||||
at_obj_set_end_sign(device->client, '>');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < BC26_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = BC26_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,%d", device_socket, (int)cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
rt_thread_mdelay(5);//delay at least 4ms
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC */
|
||||
event = SET_EVENT(device_socket, 0);
|
||||
event_result = bc26_socket_event_recv(device, event, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait event timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event = BC26_EVENT_SEND_OK | BC26_EVENT_SEND_FAIL;
|
||||
event_result = bc26_socket_event_recv(device, event, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait sned OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & BC26_EVENT_SEND_FAIL)
|
||||
{
|
||||
LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (type == AT_SOCKET_TCP)
|
||||
{
|
||||
at_wait_send_finish(socket, 2*cur_pkt_size);
|
||||
}
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reset the end sign for data conflict */
|
||||
at_obj_set_end_sign(device->client, 0);
|
||||
|
||||
rt_mutex_release(lock);
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result > 0 ? sent_size : 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
|
||||
* -1: send AT commands error or response error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int bc26_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 3
|
||||
|
||||
int i, result;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_bc26 *bc26 = RT_NULL;
|
||||
|
||||
RT_ASSERT(name);
|
||||
RT_ASSERT(ip);
|
||||
|
||||
device = at_device_get_first_initialized();
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get first init device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (!resp)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* clear BC26_EVENT_DOMAIN_OK */
|
||||
bc26_socket_event_recv(device, BC26_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
bc26 = (struct at_device_bc26 *) device->user_data;
|
||||
bc26->socket_data = ip;
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSGIP=1,\"%s\"", name) != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
for(i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
/* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
|
||||
if (bc26_socket_event_recv(device, BC26_EVENT_DOMAIN_OK, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strlen(ip) < 8)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
result = -RT_ERROR;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = RT_EOK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
bc26->socket_data = RT_NULL;
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* set AT socket event notice callback
|
||||
*
|
||||
* @param event notice event
|
||||
* @param cb notice callback
|
||||
*/
|
||||
static void bc26_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 void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0, result = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+QIOPEN: %d,%d", &device_socket , &result);
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_CONN_OK));
|
||||
}
|
||||
else
|
||||
{
|
||||
at_tcp_ip_errcode_parse(result);
|
||||
bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_CONN_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_bc26 *bc26 = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
bc26 = (struct at_device_bc26 *) device->user_data;
|
||||
device_socket = (int) bc26->user_data;
|
||||
|
||||
if (rt_strstr(data, "SEND OK"))
|
||||
{
|
||||
bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_SEND_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "SEND FAIL"))
|
||||
{
|
||||
bc26_socket_event_send(device, SET_EVENT(device_socket, BC26_EVENT_SEND_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_socket *socket = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+QIURC: \"closed\",%d", &device_socket);
|
||||
/* get at socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout;
|
||||
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_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket and receive buffer size by receive data */
|
||||
sscanf(data, "+QIURC: \"recv\",%d,%d", &device_socket, (int *) &bfsz);
|
||||
/* set receive timeout by receive buffer length, not less than 10 ms */
|
||||
timeout = bfsz > 10 ? bfsz : 10;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for URC receive buffer(%d).", 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("%s device 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 void urc_dnsqip_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
char recv_ip[16] = {0};
|
||||
int result, ip_count, dns_ttl;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_bc26 *bc26 = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
bc26 = (struct at_device_bc26 *) device->user_data;
|
||||
if (bc26->socket_data == RT_NULL)
|
||||
{
|
||||
LOG_D("%s device socket_data no config.", bc26->device_name);
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (*(data + i) == '.')
|
||||
j++;
|
||||
}
|
||||
/* There would be several dns result, we just pickup one */
|
||||
if (j == 3)
|
||||
{
|
||||
sscanf(data, "+QIURC: \"dnsgip\",\"%[^\"]", recv_ip);
|
||||
recv_ip[15] = '\0';
|
||||
|
||||
rt_memcpy(bc26->socket_data, recv_ip, sizeof(recv_ip));
|
||||
|
||||
bc26_socket_event_send(device, BC26_EVENT_DOMAIN_OK);
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(data, "+QIURC: \"dnsgip\",%d,%d,%d", &result, &ip_count, &dns_ttl);
|
||||
if (result)
|
||||
{
|
||||
at_tcp_ip_errcode_parse(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data);
|
||||
|
||||
LOG_I("URC data : %.*s", size, data);
|
||||
}
|
||||
|
||||
static void urc_qiurc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
switch(*(data + 9))
|
||||
{
|
||||
case 'c' : urc_close_func(client, data, size); break;//+QIURC: "closed"
|
||||
case 'r' : urc_recv_func(client, data, size); break;//+QIURC: "recv"
|
||||
case 'd' : urc_dnsqip_func(client, data, size); break;//+QIURC: "dnsgip"
|
||||
default : urc_func(client, data, size); break;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"SEND OK", "\r\n", urc_send_func},
|
||||
{"SEND FAIL", "\r\n", urc_send_func},
|
||||
{"+QIOPEN:", "\r\n", urc_connect_func},
|
||||
{"+QIURC:", "\r\n", urc_qiurc_func},
|
||||
};
|
||||
|
||||
static const struct at_socket_ops bc26_socket_ops =
|
||||
{
|
||||
bc26_socket_connect,
|
||||
bc26_socket_close,
|
||||
bc26_socket_send,
|
||||
bc26_domain_resolve,
|
||||
bc26_socket_set_event_cb,
|
||||
};
|
||||
|
||||
int bc26_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 bc26_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_BC26_SOCKETS_NUM;
|
||||
class->socket_ops = &bc26_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_BC26 && AT_USING_SOCKET */
|
||||
|
|
@ -0,0 +1,953 @@
|
|||
/*
|
||||
* File : at_device_bc28.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* Copyright (c) 2020, RudyLo <luhuadong@163.com>
|
||||
*
|
||||
* 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
|
||||
* 2020-02-12 luhuadong first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_bc28.h>
|
||||
|
||||
#define LOG_TAG "at.dev.bc28"
|
||||
#include <at_log.h>
|
||||
|
||||
#ifdef AT_DEVICE_USING_BC28
|
||||
|
||||
#define BC28_WAIT_CONNECT_TIME 5000
|
||||
#define BC28_THREAD_STACK_SIZE 2048
|
||||
#define BC28_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
|
||||
|
||||
static int bc28_sleep(struct at_device *device)
|
||||
{
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
static int bc28_wakeup(struct at_device *device)
|
||||
{
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
static int bc28_reset(struct at_device *device)
|
||||
{
|
||||
struct at_device_bc28 *bc28 = (struct at_device_bc28 *)device->user_data;
|
||||
|
||||
rt_pin_mode(bc28->power_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_write(bc28->power_pin, PIN_HIGH);
|
||||
rt_thread_mdelay(300);
|
||||
rt_pin_write(bc28->power_pin, PIN_LOW);
|
||||
bc28->power_status = 1;
|
||||
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
static int bc28_check_link_status(struct at_device *device)
|
||||
{
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device_bc28 *bc28 = RT_NULL;
|
||||
int result = -RT_ERROR;
|
||||
|
||||
bc28 = (struct at_device_bc28 *)device->user_data;
|
||||
if ( ! bc28->power_status) // power off
|
||||
{
|
||||
LOG_E("the power is off.");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
if (bc28->sleep_status) // is sleep status
|
||||
{
|
||||
if (bc28->power_pin != -1)
|
||||
{
|
||||
rt_pin_write(bc28->power_pin, PIN_HIGH);
|
||||
rt_thread_mdelay(100);
|
||||
rt_pin_write(bc28->power_pin, PIN_LOW);
|
||||
rt_thread_mdelay(200);
|
||||
}
|
||||
}
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
result = -RT_ERROR;
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGATT?") == RT_EOK)
|
||||
{
|
||||
int link_stat = 0;
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT:%d", &link_stat) > 0)
|
||||
{
|
||||
if (link_stat == 1)
|
||||
{
|
||||
result = RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
at_delete_resp(resp);
|
||||
return(result);
|
||||
}
|
||||
|
||||
/* ============================= bc28 network interface operations ============================= */
|
||||
/* set bc28 network interface device status and address information */
|
||||
static int bc28_netdev_set_info(struct netdev *netdev)
|
||||
{
|
||||
#define BC28_INFO_RESP_SIZE 128
|
||||
#define BC28_INFO_RESP_TIMOUT rt_tick_from_millisecond(5000)
|
||||
|
||||
int result = RT_EOK;
|
||||
ip_addr_t addr;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* set network interface device status */
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_link_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
|
||||
|
||||
resp = at_create_resp(BC28_INFO_RESP_SIZE, 0, BC28_INFO_RESP_TIMOUT);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set network interface device hardware address(IMEI) */
|
||||
{
|
||||
#define BC28_NETDEV_HWADDR_LEN 8
|
||||
#define BC28_IMEI_LEN 15
|
||||
|
||||
char imei[BC28_IMEI_LEN] = {0};
|
||||
int i = 0, j = 0;
|
||||
|
||||
/* send "AT+CGSN=1" commond to get device IMEI */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGSN=1") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 2, "+CGSN:%s", imei) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+CGSN=1\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device IMEI number: %s", device->name, imei);
|
||||
|
||||
netdev->hwaddr_len = BC28_NETDEV_HWADDR_LEN;
|
||||
/* get hardware address by IMEI */
|
||||
for (i = 0, j = 0; i < BC28_NETDEV_HWADDR_LEN && j < BC28_IMEI_LEN; i++, j+=2)
|
||||
{
|
||||
if (j != BC28_IMEI_LEN - 1)
|
||||
{
|
||||
netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev->hwaddr[i] = (imei[j] - '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* set network interface device IP address */
|
||||
{
|
||||
#define IP_ADDR_SIZE_MAX 16
|
||||
char ipaddr[IP_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
/* send "AT+CGPADDR" commond to get IP address */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse response data "+CGPADDR: 0,<IP_address>" */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR:%*d,%s", ipaddr) <= 0)
|
||||
{
|
||||
LOG_E("%s device \"AT+CGPADDR\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device IP address: %s", device->name, ipaddr);
|
||||
|
||||
/* set network interface address information */
|
||||
inet_aton(ipaddr, &addr);
|
||||
netdev_low_level_set_ipaddr(netdev, &addr);
|
||||
}
|
||||
|
||||
/* set network interface device dns server */
|
||||
{
|
||||
#define DNS_ADDR_SIZE_MAX 16
|
||||
char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
/* send "AT+QIDNSCFG?" commond to get DNS servers address */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG?") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse response data "PrimaryDns:<pri_dns>"
|
||||
* "SecondaryDns:<sec_dns>"
|
||||
*/
|
||||
if (at_resp_parse_line_args_by_kw(resp, "PrimaryDns:", "PrimaryDns: %s", dns_server1) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+QIDNSCFG?\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
if (at_resp_parse_line_args_by_kw(resp, "SecondaryDns:", "SecondaryDns: %s", dns_server2) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+QIDNSCFG?\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device primary DNS server address: %s", device->name, dns_server1);
|
||||
LOG_D("%s device secondary DNS server address: %s", device->name, dns_server2);
|
||||
|
||||
inet_aton(dns_server1, &addr);
|
||||
netdev_low_level_set_dns_server(netdev, 0, &addr);
|
||||
|
||||
inet_aton(dns_server2, &addr);
|
||||
netdev_low_level_set_dns_server(netdev, 1, &addr);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void bc28_check_link_status_entry(void *parameter)
|
||||
{
|
||||
#define BC28_LINK_DELAY_TIME (60 * RT_TICK_PER_SECOND)
|
||||
|
||||
rt_bool_t is_link_up;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct netdev *netdev = (struct netdev *) parameter;
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
is_link_up = (bc28_check_link_status(device) == RT_EOK);
|
||||
|
||||
netdev_low_level_set_link_status(netdev, is_link_up);
|
||||
|
||||
rt_thread_delay(BC28_LINK_DELAY_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
static int bc28_netdev_check_link_status(struct netdev *netdev)
|
||||
{
|
||||
#define BC28_LINK_THREAD_STACK_SIZE (1024 + 512)
|
||||
#define BC28_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
|
||||
|
||||
rt_thread_t tid;
|
||||
char tname[RT_NAME_MAX] = {0};
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
|
||||
|
||||
/* create bc28 link status polling thread */
|
||||
tid = rt_thread_create(tname, bc28_check_link_status_entry, (void *)netdev,
|
||||
BC28_LINK_THREAD_STACK_SIZE, BC28_LINK_THREAD_PRIORITY, 20);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int bc28_net_init(struct at_device *device);
|
||||
|
||||
static int bc28_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_FALSE)
|
||||
{
|
||||
bc28_net_init(device);
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
LOG_D("network interface device(%s) set up status.", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int bc28_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_TRUE)
|
||||
{
|
||||
//bc28_power_off(device);
|
||||
device->is_init = RT_FALSE;
|
||||
|
||||
netdev_low_level_set_status(netdev, RT_FALSE);
|
||||
LOG_D("network interface device(%s) set down status.", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int bc28_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
|
||||
{
|
||||
#define BC28_DNS_RESP_LEN 64
|
||||
#define BC28_DNS_RESP_TIMEOUT rt_tick_from_millisecond(300)
|
||||
|
||||
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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(BC28_DNS_RESP_LEN, 0, BC28_DNS_RESP_TIMEOUT);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send "AT+QIDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=%d,%s",
|
||||
dns_num, inet_ntoa(*dns_server)) != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
int bc28_domain_resolve(const char *name, char ip[16]);
|
||||
#endif
|
||||
#ifdef NETDEV_USING_PING
|
||||
static int bc28_netdev_ping(struct netdev *netdev, const char *host,
|
||||
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
|
||||
{
|
||||
#define BC28_PING_RESP_SIZE 128
|
||||
#define BC28_PING_IP_SIZE 16
|
||||
#define BC28_PING_TIMEOUT (10 * RT_TICK_PER_SECOND)
|
||||
|
||||
rt_err_t result = RT_EOK;
|
||||
int response = -1, recv_data_len, ping_time, ttl;
|
||||
char ip_addr[BC28_PING_IP_SIZE] = {0};
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
RT_ASSERT(host);
|
||||
RT_ASSERT(ping_resp);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(BC28_PING_RESP_SIZE, 4, BC28_PING_TIMEOUT);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* DNS resolve */
|
||||
struct in_addr inp;
|
||||
if (inet_aton(host, &inp) > 0)
|
||||
{
|
||||
rt_strncpy(ip_addr, host, BC28_PING_IP_SIZE);
|
||||
}
|
||||
#ifdef AT_USING_SOCKET
|
||||
else
|
||||
{
|
||||
bc28_domain_resolve(host, ip_addr);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NPING=%s,%d,%d",
|
||||
ip_addr, data_len, timeout*1000/RT_TICK_PER_SECOND) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+NPINGERR:", "+NPINGERR:%d", &response) > 0)
|
||||
{
|
||||
switch (response)
|
||||
{
|
||||
case 1:
|
||||
result = -RT_ETIMEOUT;
|
||||
break;
|
||||
case 2:
|
||||
default:
|
||||
result = -RT_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (at_resp_parse_line_args_by_kw(resp, "+NPING:", "+NPING:%[^,],%d,%d",
|
||||
ip_addr, &ttl, &ping_time) > 0 )
|
||||
{
|
||||
inet_aton(ip_addr, &(ping_resp->ip_addr));
|
||||
ping_resp->data_len = data_len;
|
||||
ping_resp->ticks = rt_tick_from_millisecond(ping_time);
|
||||
ping_resp->ttl = ttl;
|
||||
result = RT_EOK;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif /* NETDEV_USING_PING */
|
||||
|
||||
const struct netdev_ops bc28_netdev_ops =
|
||||
{
|
||||
bc28_netdev_set_up,
|
||||
bc28_netdev_set_down,
|
||||
|
||||
RT_NULL,
|
||||
bc28_netdev_set_dns_server,
|
||||
RT_NULL,
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
bc28_netdev_ping,
|
||||
#endif
|
||||
RT_NULL,
|
||||
};
|
||||
|
||||
static struct netdev *bc28_netdev_add(const char *netdev_name)
|
||||
{
|
||||
#define ETHERNET_MTU 1500
|
||||
#define HWADDR_LEN 8
|
||||
struct netdev *netdev = RT_NULL;
|
||||
|
||||
netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
|
||||
if (netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for netdev create.");
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
netdev->mtu = ETHERNET_MTU;
|
||||
netdev->ops = &bc28_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;
|
||||
}
|
||||
|
||||
/* ============================= bc28 device operations ============================= */
|
||||
|
||||
/* initialize for bc28 */
|
||||
static void bc28_init_thread_entry(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
#define CPIN_RETRY 5
|
||||
#define CSQ_RETRY 20
|
||||
#define CGREG_RETRY 50
|
||||
#define IPADDR_RETRY 10
|
||||
#define AT_DEFAULT_TIMEOUT 5000
|
||||
|
||||
int i;
|
||||
int retry_num = INIT_RETRY;
|
||||
rt_err_t result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = (struct at_device *) parameter;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(AT_DEFAULT_TIMEOUT));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_D("start init %s device.", device->name);
|
||||
|
||||
while (retry_num--)
|
||||
{
|
||||
/* reset the bc28 device */
|
||||
bc28_reset(device);
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* wait bc28 startup finish, send AT every 500ms, if receive OK, SYNC success*/
|
||||
if (at_client_obj_wait_connect(client, BC28_WAIT_CONNECT_TIME))
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable echo */
|
||||
if (at_obj_exec_cmd(device->client, resp, "ATE0") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E("ATE0");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable auto register */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QREGSWT=2") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+QREGSWT=2");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable auto connect */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NCONFIG=AUTOCONNECT,FALSE") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+NCONFIG=AUTOCONNECT,FALSE");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* reboot */
|
||||
at_obj_exec_cmd(device->client, resp, "AT+NRB");
|
||||
rt_thread_mdelay(5000);
|
||||
|
||||
while (at_obj_exec_cmd(device->client, resp, "AT") != RT_EOK)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
|
||||
/* check IMEI */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGSN=1") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+CGSN=1");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* search band 8 */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NBAND=8") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+NBAND=8");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set max function */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CFUN=1") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+CFUN=1");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* auto report recv from tcp */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NSONMI=2") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+NSONMI=2");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable eDRX mode */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CEDRXS=0,5") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+CEDRXS=0,5");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable PSM mode */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+CPSMS=0");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check IMSI */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIMI") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+CIMI");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* attach */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGATT=1") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+CGATT=1");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Get the baudrate */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NATSPEED?") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> AT+NATSPEED?");
|
||||
goto __exit;
|
||||
}
|
||||
at_resp_parse_line_args_by_kw(resp, "+NATSPEED:", "+NATSPEED:%d", &i);
|
||||
LOG_D("%s device baudrate %d", device->name, i);
|
||||
|
||||
/* get module version */
|
||||
if (at_obj_exec_cmd(device->client, resp, "ATI") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
LOG_E(">> ATI");
|
||||
goto __exit;
|
||||
}
|
||||
for (i = 0; i < (int) resp->line_counts - 1; i++)
|
||||
{
|
||||
LOG_D("%s", at_resp_get_line(resp, i + 1));
|
||||
}
|
||||
|
||||
/* check SIM card */
|
||||
for (i = 0; i < CPIN_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CPIN?") == RT_EOK)
|
||||
{
|
||||
if (at_resp_get_line_by_kw(resp, "READY") != RT_NULL)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == CPIN_RETRY)
|
||||
{
|
||||
LOG_E("%s device SIM card detection failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check signal strength */
|
||||
for (i = 0; i < CSQ_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK)
|
||||
{
|
||||
int signal_strength = 0, err_rate = 0;
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ:%d,%d", &signal_strength, &err_rate) > 0)
|
||||
{
|
||||
if ((signal_strength != 99) && (signal_strength != 0))
|
||||
{
|
||||
LOG_D("%s device signal strength: %d, channel bit error rate: %d",
|
||||
device->name, signal_strength, err_rate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i == CSQ_RETRY)
|
||||
{
|
||||
LOG_E("%s device signal strength check failed", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check the GPRS network is registered */
|
||||
for (i = 0; i < CGREG_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGATT?") == RT_EOK)
|
||||
{
|
||||
int link_stat = 0;
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT:%d", &link_stat) > 0)
|
||||
{
|
||||
if (link_stat == 1)
|
||||
{
|
||||
LOG_D("%s device GPRS is registered", device->name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i == CGREG_RETRY)
|
||||
{
|
||||
LOG_E("%s device GPRS is register failed", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check the GPRS network IP address */
|
||||
for (i = 0; i < IPADDR_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR") == RT_EOK)
|
||||
{
|
||||
#define IP_ADDR_SIZE_MAX 16
|
||||
char ipaddr[IP_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
/* parse response data "+CGPADDR: 0,<IP_address>" */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR:%*d,%s", ipaddr) > 0)
|
||||
{
|
||||
LOG_D("%s device IP address: %s", device->name, ipaddr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i == IPADDR_RETRY)
|
||||
{
|
||||
LOG_E("%s device GPRS is get IP address failed", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* initialize successfully */
|
||||
result = RT_EOK;
|
||||
break;
|
||||
|
||||
__exit:
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
/* power off the bc28 device */
|
||||
//bc28_power_off(device);
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
LOG_I("%s device initialize retry...", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
/* set network interface device status and address information */
|
||||
bc28_netdev_set_info(device->netdev);
|
||||
/* check and create link staus sync thread */
|
||||
if (rt_thread_find(device->netdev->name) == RT_NULL)
|
||||
{
|
||||
bc28_netdev_check_link_status(device->netdev);
|
||||
}
|
||||
|
||||
LOG_I("%s device network initialize success.", device->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("%s device network initialize failed(%d).", device->name, result);
|
||||
}
|
||||
}
|
||||
|
||||
/* bc28 device network initialize */
|
||||
static int bc28_net_init(struct at_device *device)
|
||||
{
|
||||
#ifdef AT_DEVICE_BC28_INIT_ASYN
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("bc28_net", bc28_init_thread_entry, (void *)device,
|
||||
BC28_THREAD_STACK_SIZE, BC28_THREAD_PRIORITY, 20);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("create %s device init thread failed.", device->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#else
|
||||
bc28_init_thread_entry(device);
|
||||
#endif /* AT_DEVICE_BC28_INIT_ASYN */
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int bc28_init(struct at_device *device)
|
||||
{
|
||||
struct at_device_bc28 *bc28 = (struct at_device_bc28 *)device->user_data;
|
||||
|
||||
/* initialize AT client */
|
||||
at_client_init(bc28->client_name, bc28->recv_bufsz);
|
||||
|
||||
device->client = at_client_get(bc28->client_name);
|
||||
if (device->client == RT_NULL)
|
||||
{
|
||||
LOG_E("get AT client(%s) failed.", bc28->client_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* register URC data execution function */
|
||||
#ifdef AT_USING_SOCKET
|
||||
bc28_socket_init(device);
|
||||
#endif
|
||||
|
||||
/* add bc28 device to the netdev list */
|
||||
device->netdev = bc28_netdev_add(bc28->device_name);
|
||||
if (device->netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("add netdev(%s) failed.", bc28->device_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize bc28 pin configuration */
|
||||
if (bc28->power_pin != -1)
|
||||
{
|
||||
rt_pin_mode(bc28->power_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_write(bc28->power_pin, PIN_LOW);
|
||||
}
|
||||
|
||||
/* initialize bc28 device network */
|
||||
return bc28_netdev_set_up(device->netdev);
|
||||
}
|
||||
|
||||
static int bc28_deinit(struct at_device *device)
|
||||
{
|
||||
RT_ASSERT(device);
|
||||
|
||||
return bc28_netdev_set_down(device->netdev);
|
||||
}
|
||||
|
||||
static int bc28_control(struct at_device *device, int cmd, void *arg)
|
||||
{
|
||||
int result = -RT_ERROR;
|
||||
|
||||
RT_ASSERT(device);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case AT_DEVICE_CTRL_RESET:
|
||||
result = bc28_reset(device);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_SLEEP:
|
||||
case AT_DEVICE_CTRL_WAKEUP:
|
||||
case AT_DEVICE_CTRL_POWER_ON:
|
||||
case AT_DEVICE_CTRL_POWER_OFF:
|
||||
case AT_DEVICE_CTRL_LOW_POWER:
|
||||
case AT_DEVICE_CTRL_NET_CONN:
|
||||
case AT_DEVICE_CTRL_NET_DISCONN:
|
||||
case AT_DEVICE_CTRL_SET_WIFI_INFO:
|
||||
case AT_DEVICE_CTRL_GET_SIGNAL:
|
||||
case AT_DEVICE_CTRL_GET_GPS:
|
||||
case AT_DEVICE_CTRL_GET_VER:
|
||||
LOG_W("not support the control command(%d).", cmd);
|
||||
break;
|
||||
default:
|
||||
LOG_E("input error control command(%d).", cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const struct at_device_ops bc28_device_ops =
|
||||
{
|
||||
bc28_init,
|
||||
bc28_deinit,
|
||||
bc28_control,
|
||||
};
|
||||
|
||||
static int bc28_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 device class create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* fill bc28 device class object */
|
||||
#ifdef AT_USING_SOCKET
|
||||
bc28_socket_class_register(class);
|
||||
#endif
|
||||
class->device_ops = &bc28_device_ops;
|
||||
|
||||
return at_device_class_register(class, AT_DEVICE_CLASS_BC28);
|
||||
}
|
||||
INIT_DEVICE_EXPORT(bc28_device_class_register);
|
||||
|
||||
#endif /* AT_DEVICE_USING_BC28 */
|
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* File : at_device_bc28.h
|
||||
* This file is part of RT-Thread RTOS
|
||||
* Copyright (c) 2020, RudyLo <luhuadong@163.com>
|
||||
*
|
||||
* 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
|
||||
* 2020-02-12 luhuadong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_BC28_H__
|
||||
#define __AT_DEVICE_BC28_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the BC28 device */
|
||||
#define AT_DEVICE_BC28_SOCKETS_NUM 7
|
||||
|
||||
struct at_device_bc28
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
int power_pin; /* BC28 has not power_en, it should be reset pin */
|
||||
int power_status_pin; /* ADC */
|
||||
size_t recv_bufsz;
|
||||
struct at_device device;
|
||||
|
||||
void *socket_data;
|
||||
void *user_data;
|
||||
|
||||
rt_bool_t power_status;
|
||||
rt_bool_t sleep_status;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* bc28 device socket initialize */
|
||||
int bc28_socket_init(struct at_device *device);
|
||||
|
||||
/* bc28 device class socket register */
|
||||
int bc28_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_BC28_H__ */
|
|
@ -0,0 +1,773 @@
|
|||
/*
|
||||
* File : at_socket_bc28.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* Copyright (c) 2020, RudyLo <luhuadong@163.com>
|
||||
*
|
||||
* 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
|
||||
* 2020-02-13 luhuadong first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_bc28.h>
|
||||
|
||||
#define LOG_TAG "at.skt.bc28"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_BC28) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define BC28_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 BC28_EVENT_CONN_OK (1L << 0)
|
||||
#define BC28_EVENT_SEND_OK (1L << 1)
|
||||
#define BC28_EVENT_RECV_OK (1L << 2)
|
||||
#define BC28_EVNET_CLOSE_OK (1L << 3)
|
||||
#define BC28_EVENT_CONN_FAIL (1L << 4)
|
||||
#define BC28_EVENT_SEND_FAIL (1L << 5)
|
||||
#define BC28_EVENT_DOMAIN_OK (1L << 6)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static void at_tcp_ip_errcode_parse(int result)//TCP/IP_QIGETERROR
|
||||
{
|
||||
switch(result)
|
||||
{
|
||||
case 0 : LOG_D("%d : Operation successful", result); break;
|
||||
case 550 : LOG_E("%d : Unknown error", result); break;
|
||||
case 551 : LOG_E("%d : Operation blocked", result); break;
|
||||
case 552 : LOG_E("%d : Invalid parameters", result); break;
|
||||
case 553 : LOG_E("%d : Memory not enough", result); break;
|
||||
case 554 : LOG_E("%d : Create socket failed", result); break;
|
||||
case 555 : LOG_E("%d : Operation not supported", result); break;
|
||||
case 556 : LOG_E("%d : Socket bind failed", result); break;
|
||||
case 557 : LOG_E("%d : Socket listen failed", result); break;
|
||||
case 558 : LOG_E("%d : Socket write failed", result); break;
|
||||
case 559 : LOG_E("%d : Socket read failed", result); break;
|
||||
case 560 : LOG_E("%d : Socket accept failed", result); break;
|
||||
case 561 : LOG_E("%d : Open PDP context failed", result); break;
|
||||
case 562 : LOG_E("%d : Close PDP context failed", result); break;
|
||||
case 563 : LOG_W("%d : Socket identity has been used", result); break;
|
||||
case 564 : LOG_E("%d : DNS busy", result); break;
|
||||
case 565 : LOG_E("%d : DNS parse failed", result); break;
|
||||
case 566 : LOG_E("%d : Socket connect failed", result); break;
|
||||
// case 567 : LOG_W("%d : Socket has been closed", result); break;
|
||||
case 567 : break;
|
||||
case 568 : LOG_E("%d : Operation busy", result); break;
|
||||
case 569 : LOG_E("%d : Operation timeout", result); break;
|
||||
case 570 : LOG_E("%d : PDP context broken down", result); break;
|
||||
case 571 : LOG_E("%d : Cancel send", result); break;
|
||||
case 572 : LOG_E("%d : Operation not allowed", result); break;
|
||||
case 573 : LOG_E("%d : APN not configured", result); break;
|
||||
case 574 : LOG_E("%d : Port busy", result); break;
|
||||
default : LOG_E("%d : Unknown err code", result); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int bc28_socket_event_send(struct at_device *device, uint32_t event)
|
||||
{
|
||||
return (int) rt_event_send(device->socket_event, event);
|
||||
}
|
||||
|
||||
static int bc28_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_uint32_t recved;
|
||||
|
||||
result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
return recved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 bc28_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;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+NSOCL=%d", device_socket);
|
||||
|
||||
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 bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
|
||||
enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
#define CONN_RETRY 2
|
||||
|
||||
int i = 0;
|
||||
const char *type_str = RT_NULL;
|
||||
uint32_t event = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int result = 0, event_result = 0;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
RT_ASSERT(ip);
|
||||
RT_ASSERT(port >= 0);
|
||||
|
||||
if ( ! is_client)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (type != AT_SOCKET_TCP)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
for(i=0; i<CONN_RETRY; i++)
|
||||
{
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, BC28_EVENT_CONN_OK | BC28_EVENT_CONN_FAIL);
|
||||
bc28_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NSOCO=%d,%s,%d",
|
||||
device_socket, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC, the device default connection timeout is 60 seconds*/
|
||||
if (bc28_socket_event_recv(device, SET_EVENT(device_socket, 0),
|
||||
60 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
break;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = bc28_socket_event_recv(device, BC28_EVENT_CONN_OK | BC28_EVENT_CONN_FAIL,
|
||||
1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
break;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & BC28_EVENT_CONN_OK)
|
||||
{
|
||||
result = RT_EOK;
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_D("%s device socket(%d) connect failed, the socket was not be closed and now will connect retry.",
|
||||
device->name, device_socket);
|
||||
if (bc28_socket_close(socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == CONN_RETRY)
|
||||
{
|
||||
LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_get_send_size(struct at_socket *socket, size_t *size, size_t *acked, size_t *nacked)
|
||||
{
|
||||
int result = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,0", device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+QISEND:", "+QISEND: %d,%d,%d", size, acked, nacked) <= 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_wait_send_finish(struct at_socket *socket, size_t settings_size)
|
||||
{
|
||||
/* get the timeout by the input data size */
|
||||
rt_tick_t timeout = rt_tick_from_millisecond(settings_size);
|
||||
rt_tick_t last_time = rt_tick_get();
|
||||
size_t size = 0, acked = 0, nacked = 0xFFFF;
|
||||
|
||||
while (rt_tick_get() - last_time <= timeout)
|
||||
{
|
||||
at_get_send_size(socket, &size, &acked, &nacked);
|
||||
if (nacked == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
rt_thread_mdelay(100);
|
||||
}
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 bc28_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
int result = 0, event_result = 0;
|
||||
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_bc28 *bc28 = (struct at_device_bc28 *) device->user_data;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
|
||||
resp = at_create_resp(128, 2, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* set current socket for send URC event */
|
||||
bc28->user_data = (void *) device_socket;
|
||||
|
||||
/* clear socket send event */
|
||||
event = SET_EVENT(device_socket, BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL);
|
||||
bc28_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
/* set AT client end sign to deal with '>' sign.*/
|
||||
at_obj_set_end_sign(device->client, '>');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < BC28_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = BC28_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,%d", device_socket, (int)cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
rt_thread_mdelay(5);//delay at least 4ms
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC */
|
||||
event = SET_EVENT(device_socket, 0);
|
||||
event_result = bc28_socket_event_recv(device, event, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait event timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event = BC28_EVENT_SEND_OK | BC28_EVENT_SEND_FAIL;
|
||||
event_result = bc28_socket_event_recv(device, event, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait sned OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & BC28_EVENT_SEND_FAIL)
|
||||
{
|
||||
LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (type == AT_SOCKET_TCP)
|
||||
{
|
||||
at_wait_send_finish(socket, 2*cur_pkt_size);
|
||||
}
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reset the end sign for data conflict */
|
||||
at_obj_set_end_sign(device->client, 0);
|
||||
|
||||
rt_mutex_release(lock);
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result > 0 ? sent_size : 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
|
||||
* -1: send AT commands error or response error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
int bc28_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 3
|
||||
|
||||
int i, result;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_bc28 *bc28 = RT_NULL;
|
||||
|
||||
RT_ASSERT(name);
|
||||
RT_ASSERT(ip);
|
||||
|
||||
device = at_device_get_first_initialized();
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get first init device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (!resp)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* clear BC28_EVENT_DOMAIN_OK */
|
||||
bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
bc28 = (struct at_device_bc28 *) device->user_data;
|
||||
bc28->socket_data = ip;
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QDNS=0,%s", name) != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
for(i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
/* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
|
||||
if (bc28_socket_event_recv(device, BC28_EVENT_DOMAIN_OK, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strlen(ip) < 8)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
result = -RT_ERROR;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = RT_EOK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
bc28->socket_data = RT_NULL;
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* set AT socket event notice callback
|
||||
*
|
||||
* @param event notice event
|
||||
* @param cb notice callback
|
||||
*/
|
||||
static void bc28_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 void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0, result = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+QIOPEN: %d,%d", &device_socket , &result);
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_OK));
|
||||
}
|
||||
else
|
||||
{
|
||||
at_tcp_ip_errcode_parse(result);
|
||||
bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_bc28 *bc28 = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
bc28 = (struct at_device_bc28 *) device->user_data;
|
||||
device_socket = (int) bc28->user_data;
|
||||
|
||||
if (rt_strstr(data, "SEND OK"))
|
||||
{
|
||||
bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "SEND FAIL"))
|
||||
{
|
||||
bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_SEND_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_socket *socket = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+QIURC: \"closed\",%d", &device_socket);
|
||||
/* get at socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout;
|
||||
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_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket and receive buffer size by receive data */
|
||||
sscanf(data, "+QIURC: \"recv\",%d,%d", &device_socket, (int *) &bfsz);
|
||||
/* set receive timeout by receive buffer length, not less than 10 ms */
|
||||
timeout = bfsz > 10 ? bfsz : 10;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for URC receive buffer(%d).", 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("%s device 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 void urc_dnsqip_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
char recv_ip[16] = {0};
|
||||
int result, ip_count, dns_ttl;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_bc28 *bc28 = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
bc28 = (struct at_device_bc28 *) device->user_data;
|
||||
if (bc28->socket_data == RT_NULL)
|
||||
{
|
||||
LOG_D("%s device socket_data no config.", bc28->device_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+QDNS:%s", recv_ip);
|
||||
recv_ip[15] = '\0';
|
||||
|
||||
rt_memcpy(bc28->socket_data, recv_ip, sizeof(recv_ip));
|
||||
|
||||
bc28_socket_event_send(device, BC28_EVENT_DOMAIN_OK);
|
||||
}
|
||||
|
||||
static void urc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data);
|
||||
|
||||
LOG_I("URC data : %.*s", size, data);
|
||||
}
|
||||
|
||||
static void urc_dns_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
urc_dnsqip_func(client, data, size);
|
||||
}
|
||||
|
||||
static void urc_qiurc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
switch(*(data + 9))
|
||||
{
|
||||
case 'c' : urc_close_func(client, data, size); break;//+QIURC: "closed"
|
||||
case 'r' : urc_recv_func(client, data, size); break;//+QIURC: "recv"
|
||||
case 'd' : urc_dnsqip_func(client, data, size); break;//+QIURC: "dnsgip"
|
||||
default : urc_func(client, data, size); break;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"SEND OK", "\r\n", urc_send_func},
|
||||
{"SEND FAIL", "\r\n", urc_send_func},
|
||||
{"+QIOPEN:", "\r\n", urc_connect_func},
|
||||
{"+QIURC:", "\r\n", urc_qiurc_func},
|
||||
{"+QDNS:", "\r\n", urc_dns_func},
|
||||
};
|
||||
|
||||
static const struct at_socket_ops bc28_socket_ops =
|
||||
{
|
||||
bc28_socket_connect,
|
||||
bc28_socket_close,
|
||||
bc28_socket_send,
|
||||
bc28_domain_resolve,
|
||||
bc28_socket_set_event_cb,
|
||||
};
|
||||
|
||||
int bc28_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 bc28_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM;
|
||||
class->socket_ops = &bc28_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_BC28 && AT_USING_SOCKET */
|
||||
|
|
@ -436,11 +436,14 @@ static int ec20_netdev_check_link_status(struct netdev *netdev)
|
|||
#define EC20_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
|
||||
|
||||
rt_thread_t tid;
|
||||
char tname[RT_NAME_MAX] = {0};
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
|
||||
|
||||
/* create ec20 link status polling thread */
|
||||
tid = rt_thread_create("ec20_link", ec20_check_link_status_entry, (void *)netdev,
|
||||
tid = rt_thread_create(tname, ec20_check_link_status_entry, (void *)netdev,
|
||||
EC20_LINK_THREAD_STACK_SIZE, EC20_LINK_THREAD_PRIORITY, EC20_LINK_THREAD_TICK);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
|
@ -886,7 +889,11 @@ static void ec20_init_thread_entry(void *parameter)
|
|||
{
|
||||
/* set network interface device status and address information */
|
||||
ec20_netdev_set_info(device->netdev);
|
||||
/* check and create link staus sync thread */
|
||||
if (rt_thread_find(device->netdev->name) == RT_NULL)
|
||||
{
|
||||
ec20_netdev_check_link_status(device->netdev);
|
||||
}
|
||||
|
||||
LOG_I("%s device network initialize success.", device->name);
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* File : at_device_ec200x.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
|
||||
* 2020-04-20 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_EC200X_H__
|
||||
#define __AT_DEVICE_EC200X_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the ec200x device */
|
||||
#define AT_DEVICE_EC200X_SOCKETS_NUM 5
|
||||
|
||||
struct at_device_ec200x
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
int power_pin;
|
||||
int power_status_pin;
|
||||
int wakeup_pin;
|
||||
size_t recv_line_num;
|
||||
struct at_device device;
|
||||
|
||||
void *socket_data;
|
||||
void *user_data;
|
||||
|
||||
rt_bool_t power_status;
|
||||
rt_bool_t sleep_status;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* ec200x device socket initialize */
|
||||
int ec200x_socket_init(struct at_device *device);
|
||||
|
||||
/* ec200x device class socket register */
|
||||
int ec200x_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_EC200X_H__ */
|
||||
|
|
@ -0,0 +1,803 @@
|
|||
/*
|
||||
* File : at_socket_ec200x.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-12-10 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_ec200x.h>
|
||||
|
||||
#define LOG_TAG "at.skt.ec200x"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_EC200X) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define EC200X_MODULE_SEND_MAX_SIZE 1460
|
||||
|
||||
/* set real event by current socket and current state */
|
||||
#define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
|
||||
|
||||
/* AT socket event type */
|
||||
#define EC200X_EVENT_CONN_OK (1L << 0)
|
||||
#define EC200X_EVENT_SEND_OK (1L << 1)
|
||||
#define EC200X_EVENT_RECV_OK (1L << 2)
|
||||
#define EC200X_EVNET_CLOSE_OK (1L << 3)
|
||||
#define EC200X_EVENT_CONN_FAIL (1L << 4)
|
||||
#define EC200X_EVENT_SEND_FAIL (1L << 5)
|
||||
#define EC200X_EVENT_DOMAIN_OK (1L << 6)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static void at_tcp_ip_errcode_parse(int result)//TCP/IP_QIGETERROR
|
||||
{
|
||||
switch(result)
|
||||
{
|
||||
case 0 : LOG_D("%d : Operation successful", result); break;
|
||||
case 550 : LOG_E("%d : Unknown error", result); break;
|
||||
case 551 : LOG_E("%d : Operation blocked", result); break;
|
||||
case 552 : LOG_E("%d : Invalid parameters", result); break;
|
||||
case 553 : LOG_E("%d : Memory not enough", result); break;
|
||||
case 554 : LOG_E("%d : Create socket failed", result); break;
|
||||
case 555 : LOG_E("%d : Operation not supported", result); break;
|
||||
case 556 : LOG_E("%d : Socket bind failed", result); break;
|
||||
case 557 : LOG_E("%d : Socket listen failed", result); break;
|
||||
case 558 : LOG_E("%d : Socket write failed", result); break;
|
||||
case 559 : LOG_E("%d : Socket read failed", result); break;
|
||||
case 560 : LOG_E("%d : Socket accept failed", result); break;
|
||||
case 561 : LOG_E("%d : Open PDP context failed", result); break;
|
||||
case 562 : LOG_E("%d : Close PDP context failed", result); break;
|
||||
case 563 : LOG_W("%d : Socket identity has been used", result); break;
|
||||
case 564 : LOG_E("%d : DNS busy", result); break;
|
||||
case 565 : LOG_E("%d : DNS parse failed", result); break;
|
||||
case 566 : LOG_E("%d : Socket connect failed", result); break;
|
||||
// case 567 : LOG_W("%d : Socket has been closed", result); break;
|
||||
case 567 : break;
|
||||
case 568 : LOG_E("%d : Operation busy", result); break;
|
||||
case 569 : LOG_E("%d : Operation timeout", result); break;
|
||||
case 570 : LOG_E("%d : PDP context broken down", result); break;
|
||||
case 571 : LOG_E("%d : Cancel send", result); break;
|
||||
case 572 : LOG_E("%d : Operation not allowed", result); break;
|
||||
case 573 : LOG_E("%d : APN not configured", result); break;
|
||||
case 574 : LOG_E("%d : Port busy", result); break;
|
||||
default : LOG_E("%d : Unknown err code", result); break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static int ec200x_socket_event_send(struct at_device *device, uint32_t event)
|
||||
{
|
||||
return (int) rt_event_send(device->socket_event, event);
|
||||
}
|
||||
|
||||
static int ec200x_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_uint32_t recved;
|
||||
|
||||
result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
return recved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ec200x_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;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+QICLOSE=%d", device_socket);
|
||||
|
||||
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 ec200x_socket_connect(struct at_socket *socket, char *ip, int32_t port,
|
||||
enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
#define CONN_RETRY 2
|
||||
|
||||
int i = 0;
|
||||
const char *type_str = RT_NULL;
|
||||
uint32_t event = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int result = 0, event_result = 0;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
RT_ASSERT(ip);
|
||||
RT_ASSERT(port >= 0);
|
||||
|
||||
if ( ! is_client)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
type_str = "TCP";
|
||||
break;
|
||||
case AT_SOCKET_UDP:
|
||||
type_str = "UDP";
|
||||
break;
|
||||
default:
|
||||
LOG_E("%s device socket(%d) connect type error.", device->name, device_socket);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
for(i=0; i<CONN_RETRY; i++)
|
||||
{
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, EC200X_EVENT_CONN_OK | EC200X_EVENT_CONN_FAIL);
|
||||
ec200x_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIOPEN=1,%d,\"%s\",\"%s\",%d,0,1",
|
||||
device_socket, type_str, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
break;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC, the device default connection timeout is 60 seconds*/
|
||||
if (ec200x_socket_event_recv(device, SET_EVENT(device_socket, 0),
|
||||
60 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
break;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = ec200x_socket_event_recv(device, EC200X_EVENT_CONN_OK | EC200X_EVENT_CONN_FAIL,
|
||||
1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
break;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & EC200X_EVENT_CONN_OK)
|
||||
{
|
||||
result = RT_EOK;
|
||||
break;
|
||||
}
|
||||
|
||||
LOG_D("%s device socket(%d) connect failed, the socket was not be closed and now will connect retry.",
|
||||
device->name, device_socket);
|
||||
if (ec200x_socket_close(socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == CONN_RETRY)
|
||||
{
|
||||
LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_get_send_size(struct at_socket *socket, size_t *size, size_t *acked, size_t *nacked)
|
||||
{
|
||||
int result = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,0", device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+QISEND:", "+QISEND: %d,%d,%d", size, acked, nacked) <= 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_wait_send_finish(struct at_socket *socket, size_t settings_size)
|
||||
{
|
||||
/* get the timeout by the input data size */
|
||||
rt_tick_t timeout = rt_tick_from_millisecond(settings_size);
|
||||
rt_tick_t last_time = rt_tick_get();
|
||||
size_t size = 0, acked = 0, nacked = 0xFFFF;
|
||||
|
||||
while (rt_tick_get() - last_time <= timeout)
|
||||
{
|
||||
at_get_send_size(socket, &size, &acked, &nacked);
|
||||
if (nacked == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
rt_thread_mdelay(100);
|
||||
}
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ec200x_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
int result = 0, event_result = 0;
|
||||
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_ec200x *ec200x = (struct at_device_ec200x *) device->user_data;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
|
||||
resp = at_create_resp(128, 2, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* set current socket for send URC event */
|
||||
ec200x->user_data = (void *) device_socket;
|
||||
|
||||
/* clear socket send event */
|
||||
event = SET_EVENT(device_socket, EC200X_EVENT_SEND_OK | EC200X_EVENT_SEND_FAIL);
|
||||
ec200x_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
/* set AT client end sign to deal with '>' sign.*/
|
||||
at_obj_set_end_sign(device->client, '>');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < EC200X_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = EC200X_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,%d", device_socket, (int)cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
//rt_thread_mdelay(5);//delay at least 4ms
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC */
|
||||
event = SET_EVENT(device_socket, 0);
|
||||
event_result = ec200x_socket_event_recv(device, event, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait event timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event = EC200X_EVENT_SEND_OK | EC200X_EVENT_SEND_FAIL;
|
||||
event_result = ec200x_socket_event_recv(device, event, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait sned OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & EC200X_EVENT_SEND_FAIL)
|
||||
{
|
||||
LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (type == AT_SOCKET_TCP)
|
||||
{
|
||||
//at_wait_send_finish(socket, cur_pkt_size);
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reset the end sign for data conflict */
|
||||
at_obj_set_end_sign(device->client, 0);
|
||||
|
||||
rt_mutex_release(lock);
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result > 0 ? sent_size : 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
|
||||
* -1: send AT commands error or response error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int ec200x_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 3
|
||||
|
||||
int i, result;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_ec200x *ec200x = RT_NULL;
|
||||
|
||||
RT_ASSERT(name);
|
||||
RT_ASSERT(ip);
|
||||
|
||||
device = at_device_get_first_initialized();
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get first init device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* the maximum response time is 60 seconds, but it set to 10 seconds is convenient to use. */
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(300));
|
||||
if (!resp)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* clear EC200X_EVENT_DOMAIN_OK */
|
||||
ec200x_socket_event_recv(device, EC200X_EVENT_DOMAIN_OK, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
ec200x = (struct at_device_ec200x *) device->user_data;
|
||||
ec200x->socket_data = ip;
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSGIP=1,\"%s\"", name) != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
for(i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
/* waiting result event from AT URC, the device default connection timeout is 30 seconds.*/
|
||||
if (ec200x_socket_event_recv(device, EC200X_EVENT_DOMAIN_OK, 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (rt_strlen(ip) < 8)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
result = -RT_ERROR;
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = RT_EOK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
ec200x->socket_data = RT_NULL;
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* set AT socket event notice callback
|
||||
*
|
||||
* @param event notice event
|
||||
* @param cb notice callback
|
||||
*/
|
||||
static void ec200x_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 void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0, result = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+QIOPEN: %d,%d", &device_socket , &result);
|
||||
|
||||
if (result == 0)
|
||||
{
|
||||
ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_CONN_OK));
|
||||
}
|
||||
else
|
||||
{
|
||||
at_tcp_ip_errcode_parse(result);
|
||||
ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_CONN_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_ec200x *ec200x = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
ec200x = (struct at_device_ec200x *) device->user_data;
|
||||
device_socket = (int) ec200x->user_data;
|
||||
|
||||
if (rt_strstr(data, "SEND OK"))
|
||||
{
|
||||
ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_SEND_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "SEND FAIL"))
|
||||
{
|
||||
ec200x_socket_event_send(device, SET_EVENT(device_socket, EC200X_EVENT_SEND_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_socket *socket = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+QIURC: \"closed\",%d", &device_socket);
|
||||
/* get at socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout;
|
||||
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_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket and receive buffer size by receive data */
|
||||
sscanf(data, "+QIURC: \"recv\",%d,%d", &device_socket, (int *) &bfsz);
|
||||
/* set receive timeout by receive buffer length, not less than 10 ms */
|
||||
timeout = bfsz > 10 ? bfsz : 10;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for URC receive buffer(%d).", 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("%s device 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 void urc_pdpdeact_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int connectID = 0;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
sscanf(data, "+QIURC: \"pdpdeact\",%d", &connectID);
|
||||
|
||||
LOG_E("context (%d) is deactivated.", connectID);
|
||||
}
|
||||
|
||||
static void urc_dnsqip_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
char recv_ip[16] = {0};
|
||||
int result, ip_count, dns_ttl;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_ec200x *ec200x = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
ec200x = (struct at_device_ec200x *) device->user_data;
|
||||
if (ec200x->socket_data == RT_NULL)
|
||||
{
|
||||
LOG_D("%s device socket_data no config.", ec200x->device_name);
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
if (*(data + i) == '.')
|
||||
j++;
|
||||
}
|
||||
/* There would be several dns result, we just pickup one */
|
||||
if (j == 3)
|
||||
{
|
||||
sscanf(data, "+QIURC: \"dnsgip\",\"%[^\"]", recv_ip);
|
||||
recv_ip[15] = '\0';
|
||||
|
||||
rt_memcpy(ec200x->socket_data, recv_ip, sizeof(recv_ip));
|
||||
|
||||
ec200x_socket_event_send(device, EC200X_EVENT_DOMAIN_OK);
|
||||
}
|
||||
else
|
||||
{
|
||||
sscanf(data, "+QIURC: \"dnsgip\",%d,%d,%d", &result, &ip_count, &dns_ttl);
|
||||
if (result)
|
||||
{
|
||||
at_tcp_ip_errcode_parse(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data);
|
||||
|
||||
LOG_I("URC data : %.*s", size, data);
|
||||
}
|
||||
|
||||
static void urc_qiurc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
switch(*(data + 9))
|
||||
{
|
||||
case 'c' : urc_close_func(client, data, size); break;//+QIURC: "closed"
|
||||
case 'r' : urc_recv_func(client, data, size); break;//+QIURC: "recv"
|
||||
case 'p' : urc_pdpdeact_func(client, data, size); break;//+QIURC: "pdpdeact"
|
||||
case 'd' : urc_dnsqip_func(client, data, size); break;//+QIURC: "dnsgip"
|
||||
default : urc_func(client, data, size); break;
|
||||
}
|
||||
}
|
||||
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"SEND OK", "\r\n", urc_send_func},
|
||||
{"SEND FAIL", "\r\n", urc_send_func},
|
||||
{"+QIOPEN:", "\r\n", urc_connect_func},
|
||||
{"+QIURC:", "\r\n", urc_qiurc_func},
|
||||
};
|
||||
|
||||
static const struct at_socket_ops ec200x_socket_ops =
|
||||
{
|
||||
ec200x_socket_connect,
|
||||
ec200x_socket_close,
|
||||
ec200x_socket_send,
|
||||
ec200x_domain_resolve,
|
||||
ec200x_socket_set_event_cb,
|
||||
};
|
||||
|
||||
int ec200x_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 ec200x_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_EC200X_SOCKETS_NUM;
|
||||
class->socket_ops = &ec200x_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_EC200X && AT_USING_SOCKET */
|
||||
|
|
@ -0,0 +1,942 @@
|
|||
/*
|
||||
* File : at_socket_esp32.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-10-26 zylx first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_esp32.h>
|
||||
|
||||
#define LOG_TAG "at.dev.esp32"
|
||||
|
||||
#include <at_log.h>
|
||||
|
||||
#ifdef AT_DEVICE_USING_ESP32
|
||||
|
||||
#define ESP32_WAIT_CONNECT_TIME 5000
|
||||
#define ESP32_THREAD_STACK_SIZE 2048
|
||||
#define ESP32_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
|
||||
|
||||
/* ============================= esp32 network interface operations ============================= */
|
||||
|
||||
static int esp32_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server);
|
||||
|
||||
static void esp32_get_netdev_info(struct rt_work *work, void *work_data)
|
||||
{
|
||||
#define AT_ADDR_LEN 32
|
||||
#define AT_ERR_DNS_SERVER "255.255.255.255"
|
||||
#define AT_DEF_DNS_SERVER "114.114.114.114"
|
||||
|
||||
at_response_t resp = RT_NULL;
|
||||
char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
|
||||
char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
|
||||
char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
|
||||
const char *resp_expr = "%*[^\"]\"%[^\"]\"";
|
||||
const char *resp_dns = "+CIPDNS:%s";
|
||||
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 resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
/* send mac addr query commond "AT+CIFSR" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+CIFSR") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "STAMAC", resp_expr, mac) <= 0)
|
||||
{
|
||||
LOG_E("%s device parse \"AT+CIFSR\" cmd error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send addr info query commond "AT+CIPSTA?" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+CIPSTA?") < 0)
|
||||
{
|
||||
LOG_E("%s device send \"AT+CIPSTA?\" cmd error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "ip", resp_expr, ip) <= 0 ||
|
||||
at_resp_parse_line_args_by_kw(resp, "gateway", resp_expr, gateway) <= 0 ||
|
||||
at_resp_parse_line_args_by_kw(resp, "netmask", resp_expr, netmask) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+CIPSTA?\" cmd error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set netdev info */
|
||||
inet_aton(gateway, &ip_addr);
|
||||
netdev_low_level_set_gw(netdev, &ip_addr);
|
||||
inet_aton(netmask, &ip_addr);
|
||||
netdev_low_level_set_netmask(netdev, &ip_addr);
|
||||
inet_aton(ip, &ip_addr);
|
||||
netdev_low_level_set_ipaddr(netdev, &ip_addr);
|
||||
sscanf(mac, "%x:%x:%x:%x:%x:%x",
|
||||
&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];
|
||||
}
|
||||
|
||||
/* send dns server query commond "AT+CIPDNS?" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS?") < 0)
|
||||
{
|
||||
LOG_W("please check and update %s device firmware to support the \"AT+CIPDNS?\" cmd.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 1, resp_dns, dns_server1) <= 0 &&
|
||||
at_resp_parse_line_args(resp, 2, resp_dns, dns_server2) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+CIPDNS?\" cmd error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set primary DNS server address */
|
||||
if (rt_strlen(dns_server1) > 0 &&
|
||||
rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0)
|
||||
{
|
||||
inet_aton(dns_server1, &ip_addr);
|
||||
netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
inet_aton(AT_DEF_DNS_SERVER, &ip_addr);
|
||||
esp32_netdev_set_dns_server(netdev, 0, &ip_addr);
|
||||
}
|
||||
|
||||
/* set secondary DNS server address */
|
||||
if (rt_strlen(dns_server2) > 0 )
|
||||
{
|
||||
inet_aton(dns_server2, &ip_addr);
|
||||
netdev_low_level_set_dns_server(netdev, 1, &ip_addr);
|
||||
}
|
||||
|
||||
/* send DHCP query commond " AT+CWDHCP_CUR?" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+CWDHCP?") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse response data, get the DHCP status */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CWDHCP:", "+CWDHCP:%d", &dhcp_stat) < 0)
|
||||
{
|
||||
LOG_E("%s device prase DHCP status error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Bit0 - SoftAP DHCP status, Bit1 - Station DHCP status */
|
||||
netdev_low_level_set_dhcp_status(netdev, dhcp_stat & 0x01 ? RT_TRUE : RT_FALSE);
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
}
|
||||
|
||||
static int esp32_net_init(struct at_device *device);
|
||||
|
||||
static int esp32_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_FALSE)
|
||||
{
|
||||
esp32_net_init(device);
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
LOG_D("network interface device(%s) set up status", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int esp32_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 device by netdev(%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("network interface device(%s) set down status", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int esp32_netdev_set_addr_info(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
|
||||
{
|
||||
#define IPADDR_RESP_SIZE 128
|
||||
#define IPADDR_SIZE 16
|
||||
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char ip_str[IPADDR_SIZE] = {0};
|
||||
char gw_str[IPADDR_SIZE] = {0};
|
||||
char netmask_str[IPADDR_SIZE] = {0};
|
||||
|
||||
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 device(%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 resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Convert numeric IP address into decimal dotted ASCII representation. */
|
||||
if (ip_addr)
|
||||
rt_memcpy(ip_str, inet_ntoa(*ip_addr), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(ip_str, inet_ntoa(netdev->ip_addr), IPADDR_SIZE);
|
||||
|
||||
if (gw)
|
||||
rt_memcpy(gw_str, inet_ntoa(*gw), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(gw_str, inet_ntoa(netdev->gw), IPADDR_SIZE);
|
||||
|
||||
if (netmask)
|
||||
rt_memcpy(netmask_str, inet_ntoa(*netmask), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(netmask_str, inet_ntoa(netdev->netmask), IPADDR_SIZE);
|
||||
|
||||
/* send addr info set commond "AT+CIPSTA_CUR=<ip>[,<gateway>,<netmask>]" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTA_CUR=\"%s\",\"%s\",\"%s\"",
|
||||
ip_str, gw_str, netmask_str) < 0)
|
||||
{
|
||||
LOG_E("%s device set address 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("%s device set address success.", device->name);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int esp32_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 device by netdev(%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 resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* send dns server set commond "AT+CIPDNS_CUR=<enable>[,<DNS server0>,<DNS server1>]" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS_CUR=1,\"%s\"", inet_ntoa(*dns_server)) < 0)
|
||||
{
|
||||
LOG_E("%s device set DNS failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
|
||||
LOG_D("%s device set DNS(%s) success.", device->name, inet_ntoa(*dns_server));
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int esp32_netdev_set_dhcp(struct netdev *netdev, rt_bool_t is_enabled)
|
||||
{
|
||||
#define ESP32_STATION 1
|
||||
#define RESP_SIZE 128
|
||||
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device by netdev(%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 resp struct.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* send dhcp set commond "AT+CWDHCP_CUR=<mode>,<en>" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CWDHCP_CUR=%d,%d", ESP32_STATION, is_enabled) < 0)
|
||||
{
|
||||
LOG_E("%s device 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("%s device set DHCP status(%d) ok.", device->name, is_enabled);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
static int esp32_netdev_ping(struct netdev *netdev, const char *host,
|
||||
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
|
||||
{
|
||||
#define ESP32_PING_IP_SIZE 16
|
||||
|
||||
rt_err_t result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char ip_addr[ESP32_PING_IP_SIZE] = {0};
|
||||
int req_time;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
RT_ASSERT(host);
|
||||
RT_ASSERT(ping_resp);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(64, 0, timeout);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* send domain commond "AT+CIPDOMAIN=<domain name>" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", host) < 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:%s", ip_addr) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send ping commond "AT+PING=<IP>" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+PING=\"%s\"", host) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+", "+PING:%d", &req_time) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (req_time)
|
||||
{
|
||||
inet_aton(ip_addr, &(ping_resp->ip_addr));
|
||||
ping_resp->data_len = data_len;
|
||||
ping_resp->ttl = 0;
|
||||
ping_resp->ticks = req_time;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif /* NETDEV_USING_PING */
|
||||
|
||||
#ifdef NETDEV_USING_NETSTAT
|
||||
void esp32_netdev_netstat(struct netdev *netdev)
|
||||
{
|
||||
#define ESP32_NETSTAT_RESP_SIZE 320
|
||||
#define ESP32_NETSTAT_TYPE_SIZE 4
|
||||
#define ESP32_NETSTAT_IPADDR_SIZE 17
|
||||
#define ESP32_NETSTAT_EXPRESSION "+CIPSTATUS:%*d,\"%[^\"]\",\"%[^\"]\",%d,%d,%*d"
|
||||
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
int remote_port, local_port, i;
|
||||
char *type = RT_NULL;
|
||||
char *ipaddr = RT_NULL;
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
type = (char *) rt_calloc(1, ESP32_NETSTAT_TYPE_SIZE);
|
||||
ipaddr = (char *) rt_calloc(1, ESP32_NETSTAT_IPADDR_SIZE);
|
||||
if ((type && ipaddr) == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for ipaddr create.");
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
resp = at_create_resp(ESP32_NETSTAT_RESP_SIZE, 0, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send network connection information commond "AT+CIPSTATUS" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTATUS") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
for (i = 1; i <= resp->line_counts; i++)
|
||||
{
|
||||
if (strstr(at_resp_get_line(resp, i), "+CIPSTATUS"))
|
||||
{
|
||||
/* parse the third line of response data, get the network connection information */
|
||||
if (at_resp_parse_line_args(resp, i, ESP32_NETSTAT_EXPRESSION, type, ipaddr, &remote_port, &local_port) < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_RAW("%s: %s:%d ==> %s:%d\n", type, inet_ntoa(netdev->ip_addr), local_port, ipaddr, remote_port);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (type)
|
||||
{
|
||||
rt_free(type);
|
||||
}
|
||||
|
||||
if (ipaddr)
|
||||
{
|
||||
rt_free(ipaddr);
|
||||
}
|
||||
}
|
||||
#endif /* NETDEV_USING_NETSTAT */
|
||||
|
||||
static const struct netdev_ops esp32_netdev_ops =
|
||||
{
|
||||
esp32_netdev_set_up,
|
||||
esp32_netdev_set_down,
|
||||
|
||||
esp32_netdev_set_addr_info,
|
||||
esp32_netdev_set_dns_server,
|
||||
esp32_netdev_set_dhcp,
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
esp32_netdev_ping,
|
||||
#endif
|
||||
#ifdef NETDEV_USING_NETSTAT
|
||||
esp32_netdev_netstat,
|
||||
#endif
|
||||
};
|
||||
|
||||
static struct netdev *esp32_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 netdev create.");
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
netdev->mtu = ETHERNET_MTU;
|
||||
netdev->ops = &esp32_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;
|
||||
}
|
||||
|
||||
/* ============================= esp32 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 esp32_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, esp32_get_netdev_info, (void *)device);
|
||||
rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
static void esp32_init_thread_entry(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
|
||||
struct at_device *device = (struct at_device *) parameter;
|
||||
struct at_device_esp32 *esp32 = (struct at_device_esp32 *) 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;
|
||||
rt_bool_t wifi_is_conn = RT_FALSE;
|
||||
|
||||
LOG_D("%s device initialize start.", device->name);
|
||||
|
||||
/* wait esp32 device startup finish */
|
||||
if (at_client_obj_wait_connect(client, ESP32_WAIT_CONNECT_TIME))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
while (retry_num--)
|
||||
{
|
||||
/* reset module */
|
||||
AT_SEND_CMD(client, resp, "AT+RST");
|
||||
/* reset waiting delay */
|
||||
rt_thread_mdelay(1000);
|
||||
/* disable echo */
|
||||
AT_SEND_CMD(client, resp, "ATE0");
|
||||
/* set current mode to Wi-Fi station */
|
||||
AT_SEND_CMD(client, resp, "AT+CWMODE=1");
|
||||
/* get module version */
|
||||
AT_SEND_CMD(client, resp, "AT+GMR");
|
||||
/* show module version */
|
||||
for (i = 0; i < resp->line_counts - 1; i++)
|
||||
{
|
||||
LOG_D("%s", at_resp_get_line(resp, i + 1));
|
||||
}
|
||||
|
||||
AT_SEND_CMD(client, resp, "AT+CIPMUX=1");
|
||||
|
||||
/* initialize successfully */
|
||||
result = RT_EOK;
|
||||
break;
|
||||
|
||||
__exit:
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
LOG_I("%s device initialize retry...", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
/* connect to WiFi AP */
|
||||
if (at_obj_exec_cmd(client, at_resp_set_info(resp, 512, 0, 20 * RT_TICK_PER_SECOND),
|
||||
"AT+CWJAP=\"%s\",\"%s\"", esp32->wifi_ssid, esp32->wifi_password) != RT_EOK)
|
||||
{
|
||||
LOG_W("%s device wifi connect failed, check ssid(%s) and password(%s).",
|
||||
device->name, esp32->wifi_ssid, esp32->wifi_password);
|
||||
}
|
||||
else
|
||||
{
|
||||
wifi_is_conn = RT_TRUE;
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
netdev_low_level_set_status(device->netdev, RT_FALSE);
|
||||
LOG_E("%s device network initialize failed(%d).", device->name, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
device->is_init = RT_TRUE;
|
||||
netdev_low_level_set_status(device->netdev, RT_TRUE);
|
||||
if (wifi_is_conn)
|
||||
{
|
||||
netdev_low_level_set_link_status(device->netdev, RT_TRUE);
|
||||
}
|
||||
esp32_netdev_start_delay_work(device);
|
||||
LOG_I("%s device network initialize successfully.", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
static int esp32_net_init(struct at_device *device)
|
||||
{
|
||||
#ifdef AT_DEVICE_ESP32_INIT_ASYN
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("esp_net", esp32_init_thread_entry, (void *) device,
|
||||
ESP32_THREAD_STACK_SIZE, ESP32_THREAD_PRIORITY, 20);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("create %s device init thread failed.", device->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#else
|
||||
esp32_init_thread_entry(device);
|
||||
#endif /* AT_DEVICE_ESP32_INIT_ASYN */
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void urc_busy_p_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
LOG_D("system is processing a commands...");
|
||||
}
|
||||
|
||||
static void urc_busy_s_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
LOG_D("system is sending data...");
|
||||
}
|
||||
|
||||
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 device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (rt_strstr(data, "WIFI CONNECTED"))
|
||||
{
|
||||
LOG_I("%s device wifi is connected.", device->name);
|
||||
|
||||
if (device->is_init)
|
||||
{
|
||||
netdev_low_level_set_link_status(device->netdev, RT_TRUE);
|
||||
|
||||
esp32_netdev_start_delay_work(device);
|
||||
}
|
||||
}
|
||||
else if (rt_strstr(data, "WIFI DISCONNECT"))
|
||||
{
|
||||
LOG_I("%s device 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[] =
|
||||
{
|
||||
{"busy p", "\r\n", urc_busy_p_func},
|
||||
{"busy s", "\r\n", urc_busy_s_func},
|
||||
{"WIFI CONNECTED", "\r\n", urc_func},
|
||||
{"WIFI DISCONNECT", "\r\n", urc_func},
|
||||
};
|
||||
|
||||
static int esp32_init(struct at_device *device)
|
||||
{
|
||||
struct at_device_esp32 *esp32 = (struct at_device_esp32 *) device->user_data;
|
||||
|
||||
/* initialize AT client */
|
||||
at_client_init(esp32->client_name, esp32->recv_line_num);
|
||||
|
||||
device->client = at_client_get(esp32->client_name);
|
||||
if (device->client == RT_NULL)
|
||||
{
|
||||
LOG_E("get AT client(%s) failed.", esp32->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
|
||||
esp32_socket_init(device);
|
||||
#endif
|
||||
|
||||
/* add esp32 device to the netdev list */
|
||||
device->netdev = esp32_netdev_add(esp32->device_name);
|
||||
if (device->netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("add netdev(%s) failed.", esp32->device_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize esp32 device network */
|
||||
return esp32_netdev_set_up(device->netdev);
|
||||
}
|
||||
|
||||
static int esp32_deinit(struct at_device *device)
|
||||
{
|
||||
return esp32_netdev_set_down(device->netdev);
|
||||
}
|
||||
|
||||
/* reset eap8266 device and initialize device network again */
|
||||
static int esp32_reset(struct at_device *device)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
/* send "AT+RST" commonds to esp32 device */
|
||||
result = at_obj_exec_cmd(client, RT_NULL, "AT+RST");
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* waiting 10 seconds for esp32 device reset */
|
||||
device->is_init = RT_FALSE;
|
||||
if (at_client_obj_wait_connect(client, ESP32_WAIT_CONNECT_TIME))
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
/* initialize esp32 device network */
|
||||
esp32_net_init(device);
|
||||
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* change eap8266 wifi ssid and password information */
|
||||
static int esp32_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 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 resp create.");
|
||||
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("%s device 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 esp32_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("not support the control cmd(%d).", cmd);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_RESET:
|
||||
result = esp32_reset(device);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_SET_WIFI_INFO:
|
||||
result = esp32_wifi_info_set(device, (struct at_device_ssid_pwd *) arg);
|
||||
break;
|
||||
default:
|
||||
LOG_E("input error control cmd(%d).", cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static const struct at_device_ops esp32_device_ops =
|
||||
{
|
||||
esp32_init,
|
||||
esp32_deinit,
|
||||
esp32_control,
|
||||
};
|
||||
|
||||
static int esp32_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 class create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* fill ESP32 device class object */
|
||||
#ifdef AT_USING_SOCKET
|
||||
esp32_socket_class_register(class);
|
||||
#endif
|
||||
class->device_ops = &esp32_device_ops;
|
||||
|
||||
return at_device_class_register(class, AT_DEVICE_CLASS_ESP32);
|
||||
}
|
||||
INIT_DEVICE_EXPORT(esp32_device_class_register);
|
||||
|
||||
#endif /* AT_DEVICE_USING_ESP32 */
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* File : at_device_esp32.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-05-16 chenyong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_ESP32_H__
|
||||
#define __AT_DEVICE_ESP32_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the esp32 device */
|
||||
#define AT_DEVICE_ESP32_SOCKETS_NUM 5
|
||||
|
||||
struct at_device_esp32
|
||||
{
|
||||
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
|
||||
|
||||
/* esp32 device socket initialize */
|
||||
int esp32_socket_init(struct at_device *device);
|
||||
|
||||
/* esp32 device class socket register */
|
||||
int esp32_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_ESP32_H__ */
|
|
@ -0,0 +1,556 @@
|
|||
/*
|
||||
* File : at_socket_esp32.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
|
||||
* 2018-06-20 chenyong first version
|
||||
* 2019-05-09 chenyong multi AT socket client support
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_esp32.h>
|
||||
|
||||
#define LOG_TAG "at.skt.esp32"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_ESP32) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define ESP32_MODULE_SEND_MAX_SIZE 2048
|
||||
/* set real event by current socket and current state */
|
||||
#define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
|
||||
|
||||
/* AT socket event type */
|
||||
#define ESP32_EVENT_CONN_OK (1L << 0)
|
||||
#define ESP32_EVENT_SEND_OK (1L << 1)
|
||||
#define ESP32_EVENT_RECV_OK (1L << 2)
|
||||
#define ESP32_EVNET_CLOSE_OK (1L << 3)
|
||||
#define ESP32_EVENT_CONN_FAIL (1L << 4)
|
||||
#define ESP32_EVENT_SEND_FAIL (1L << 5)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static int esp32_socket_event_send(struct at_device *device, uint32_t event)
|
||||
{
|
||||
return (int) rt_event_send(device->socket_event, event);
|
||||
}
|
||||
|
||||
static int esp32_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
|
||||
{
|
||||
int result = 0;
|
||||
rt_uint32_t recved;
|
||||
|
||||
result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
return recved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 esp32_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;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE=%d", device_socket);
|
||||
|
||||
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 esp32_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 resp create.");
|
||||
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\",\"%s\",%d,60", device_socket, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case AT_SOCKET_UDP:
|
||||
if (at_obj_exec_cmd(device->client, resp,
|
||||
"AT+CIPSTART=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG_E("not supported connect type %d.", type);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
if (result != RT_EOK && retryed == RT_FALSE)
|
||||
{
|
||||
LOG_D("%s device socket (%d) connect failed, the socket was not be closed and now will connect retry.",
|
||||
device->name, device_socket);
|
||||
if (esp32_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 esp32_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
int event_result = 0;
|
||||
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_esp32 *esp32 = (struct at_device_esp32 *) device->user_data;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
RT_ASSERT(bfsz > 0);
|
||||
|
||||
resp = at_create_resp(128, 2, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* set current socket for send URC event */
|
||||
esp32->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 < ESP32_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = ESP32_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+CIPSEND" commands to AT server than receive the '>' response on the first line */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPSEND=%d,%d", device_socket, cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC */
|
||||
if (esp32_socket_event_recv(device, SET_EVENT(device_socket, 0),
|
||||
10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = esp32_socket_event_recv(device, ESP32_EVENT_SEND_OK | ESP32_EVENT_SEND_FAIL,
|
||||
5 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & ESP32_EVENT_SEND_FAIL)
|
||||
{
|
||||
LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
|
||||
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 > 0 ? sent_size : 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 esp32_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 5
|
||||
|
||||
int i, result = RT_EOK;
|
||||
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 init 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 resp create.");
|
||||
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:%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 esp32_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 esp32_socket_ops =
|
||||
{
|
||||
esp32_socket_connect,
|
||||
esp32_socket_close,
|
||||
esp32_socket_send,
|
||||
esp32_domain_resolve,
|
||||
esp32_socket_set_event_cb,
|
||||
};
|
||||
|
||||
static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct at_device_esp32 *esp32 = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
esp32 = (struct at_device_esp32 *) device->user_data;
|
||||
device_socket = (int) esp32->user_data;
|
||||
|
||||
if (rt_strstr(data, "SEND OK"))
|
||||
{
|
||||
esp32_socket_event_send(device, SET_EVENT(device_socket, ESP32_EVENT_SEND_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "SEND FAIL"))
|
||||
{
|
||||
esp32_socket_event_send(device, SET_EVENT(device_socket, ESP32_EVENT_SEND_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_send_bfsz_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
static int cur_send_bfsz = 0;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
sscanf(data, "Recv %d bytes", &cur_send_bfsz);
|
||||
}
|
||||
|
||||
static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int index = 0;
|
||||
struct at_socket *socket = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "%d,CLOSED", &index);
|
||||
socket = &(device->sockets[index]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
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_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the at deveice socket and receive buffer size by receive data */
|
||||
sscanf(data, "+IPD,%d,%d:", &device_socket, (int *) &bfsz);
|
||||
|
||||
/* set receive timeout by receive buffer length, not less than 10ms */
|
||||
timeout = bfsz > 10 ? bfsz : 10;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
return;
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory receive buffer(%d).", 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("%s device 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[] =
|
||||
{
|
||||
{"SEND OK", "\r\n", urc_send_func},
|
||||
{"SEND FAIL", "\r\n", urc_send_func},
|
||||
{"Recv", "bytes\r\n", urc_send_bfsz_func},
|
||||
{"", ",CLOSED\r\n", urc_close_func},
|
||||
{"+IPD", ":", urc_recv_func},
|
||||
};
|
||||
|
||||
int esp32_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 esp32_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_ESP32_SOCKETS_NUM;
|
||||
class->socket_ops = &esp32_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_ESP32 && AT_USING_SOCKET */
|
|
@ -57,7 +57,7 @@ static void esp8266_get_netdev_info(struct rt_work *work, void *work_data)
|
|||
ip_addr_t ip_addr;
|
||||
rt_uint32_t mac_addr[6] = {0};
|
||||
rt_uint32_t num = 0;
|
||||
rt_uint8_t dhcp_stat = 0;
|
||||
rt_uint32_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;
|
||||
|
|
|
@ -294,7 +294,7 @@ static int m26_netdev_check_link_status(struct netdev *netdev)
|
|||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
|
||||
|
||||
tid = rt_thread_create(tname, check_link_status_entry, (void *)netdev,
|
||||
M26_LINK_THREAD_STACK_SIZE, M26_LINK_THREAD_PRIORITY, M26_LINK_THREAD_TICK);
|
||||
|
@ -707,7 +707,11 @@ static void m26_init_thread_entry(void *parameter)
|
|||
if (result == RT_EOK)
|
||||
{
|
||||
m26_netdev_set_info(device->netdev);
|
||||
/* check and create link staus sync thread */
|
||||
if (rt_thread_find(device->netdev->name) == RT_NULL)
|
||||
{
|
||||
m26_netdev_check_link_status(device->netdev);
|
||||
}
|
||||
|
||||
LOG_I("%s device network initialize success.", device->name);
|
||||
}
|
||||
|
|
|
@ -84,21 +84,13 @@ static int m26_socket_event_recv(struct at_device *device, uint32_t event, uint3
|
|||
static int m26_socket_close(struct at_socket *socket)
|
||||
{
|
||||
int result = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socke = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* clear socket close event */
|
||||
m26_socket_event_recv(device, SET_EVENT(device_socke, M26_EVNET_CLOSE_OK), 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QICLOSE=%d", device_socke) < 0)
|
||||
if (at_obj_exec_cmd(device->client, NULL, "AT+QICLOSE=%d", device_socke) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
|
@ -113,11 +105,6 @@ static int m26_socket_close(struct at_socket *socket)
|
|||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,969 @@
|
|||
/*
|
||||
* File : at_socket_m6315.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
|
||||
* 2018-06-12 malongwei first version
|
||||
* 2019-05-13 chenyong multi AT socket client support
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <at_device_m6315.h>
|
||||
|
||||
#define LOG_TAG "at.dev.m6315"
|
||||
#include <at_log.h>
|
||||
|
||||
#ifdef AT_DEVICE_USING_M6315
|
||||
|
||||
#define M6315_WAIT_CONNECT_TIME 5000
|
||||
#define M6315_THREAD_STACK_SIZE 2048
|
||||
#define M6315_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
|
||||
|
||||
|
||||
static void m6315_power_on(struct at_device *device)
|
||||
{
|
||||
struct at_device_m6315 *m6315 = RT_NULL;
|
||||
|
||||
m6315 = (struct at_device_m6315 *) device->user_data;
|
||||
|
||||
/* not nead to set pin configuration for m26 device power on */
|
||||
if (m6315->power_pin == -1 || m6315->power_status_pin == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (rt_pin_read(m6315->power_status_pin) == PIN_HIGH)
|
||||
{
|
||||
return;
|
||||
}
|
||||
rt_pin_write(m6315->power_pin, PIN_HIGH);
|
||||
|
||||
while (rt_pin_read(m6315->power_status_pin) == PIN_LOW)
|
||||
{
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
rt_pin_write(m6315->power_pin, PIN_LOW);
|
||||
}
|
||||
|
||||
static void m6315_power_off(struct at_device *device)
|
||||
{
|
||||
struct at_device_m6315 *m6315 = RT_NULL;
|
||||
|
||||
m6315 = (struct at_device_m6315 *) device->user_data;
|
||||
|
||||
/* not nead to set pin configuration for m6315 device power on */
|
||||
if (m6315->power_pin == -1 || m6315->power_status_pin == -1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (rt_pin_read(m6315->power_status_pin) == PIN_LOW)
|
||||
{
|
||||
return;
|
||||
}
|
||||
rt_pin_write(m6315->power_pin, PIN_HIGH);
|
||||
|
||||
while (rt_pin_read(m6315->power_status_pin) == PIN_HIGH)
|
||||
{
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
rt_pin_write(m6315->power_pin, PIN_LOW);
|
||||
}
|
||||
|
||||
/* ============================= m6315 network interface operations ============================= */
|
||||
|
||||
/* set m6315 network interface device status and address information */
|
||||
static int m6315_netdev_set_info(struct netdev *netdev)
|
||||
{
|
||||
#define M6315_IMEI_RESP_SIZE 32
|
||||
#define M6315_IPADDR_RESP_SIZE 32
|
||||
#define M6315_DNS_RESP_SIZE 96
|
||||
#define M6315_INFO_RESP_TIMO rt_tick_from_millisecond(300)
|
||||
|
||||
int result = RT_EOK;
|
||||
ip_addr_t addr;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* set network interface device status */
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_link_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
|
||||
|
||||
resp = at_create_resp(M6315_IMEI_RESP_SIZE, 0, M6315_INFO_RESP_TIMO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set network interface device hardware address(IMEI) */
|
||||
{
|
||||
#define M6315_NETDEV_HWADDR_LEN 8
|
||||
#define M6315_IMEI_LEN 15
|
||||
|
||||
char imei[M6315_IMEI_LEN] = {0};
|
||||
int i = 0, j = 0;
|
||||
|
||||
/* send "AT+GSN" commond to get device IMEI */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+GSN") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 2, "%s", imei) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+GSN\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device IMEI number: %s", device->name, imei);
|
||||
|
||||
netdev->hwaddr_len = M6315_NETDEV_HWADDR_LEN;
|
||||
/* get hardware address by IMEI */
|
||||
for (i = 0, j = 0; i < M6315_NETDEV_HWADDR_LEN && j < M6315_IMEI_LEN; i++, j += 2)
|
||||
{
|
||||
if (j != M6315_IMEI_LEN - 1)
|
||||
{
|
||||
netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev->hwaddr[i] = (imei[j] - '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* set network interface device IP address */
|
||||
{
|
||||
#define IP_ADDR_SIZE_MAX 16
|
||||
char ipaddr[IP_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
at_resp_set_info(resp, M6315_IPADDR_RESP_SIZE, 2, M6315_INFO_RESP_TIMO);
|
||||
|
||||
/* send "AT+QILOCIP" commond to get IP address */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QILOCIP") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, ".", "%s", ipaddr) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+QILOCIP\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device IP address: %s", device->name, ipaddr);
|
||||
|
||||
/* set network interface address information */
|
||||
inet_aton(ipaddr, &addr);
|
||||
netdev_low_level_set_ipaddr(netdev, &addr);
|
||||
}
|
||||
|
||||
/* set network interface device dns server */
|
||||
{
|
||||
#define DNS_ADDR_SIZE_MAX 16
|
||||
char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
at_resp_set_info(resp, M6315_DNS_RESP_SIZE, 0, M6315_INFO_RESP_TIMO);
|
||||
|
||||
/* send "AT+QIDNSCFG?" commond to get DNS servers address */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG?") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "PrimaryDns:", "PrimaryDns:%s", dns_server1) <= 0 ||
|
||||
at_resp_parse_line_args_by_kw(resp, "SecondaryDns:", "SecondaryDns:%s", dns_server2) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+QIDNSCFG?\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device primary DNS server address: %s", device->name, dns_server1);
|
||||
LOG_D("%s device secondary DNS server address: %s", device->name, dns_server2);
|
||||
|
||||
inet_aton(dns_server1, &addr);
|
||||
netdev_low_level_set_dns_server(netdev, 0, &addr);
|
||||
|
||||
inet_aton(dns_server2, &addr);
|
||||
netdev_low_level_set_dns_server(netdev, 1, &addr);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void check_link_status_entry(void *parameter)
|
||||
{
|
||||
#define M6315_LINK_STATUS_OK 1
|
||||
#define M6315_LINK_RESP_SIZE 64
|
||||
#define M6315_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
|
||||
#define M6315_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
|
||||
|
||||
at_response_t resp = RT_NULL;
|
||||
int result_code, link_status;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct netdev *netdev = (struct netdev *)parameter;
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
resp = at_create_resp(M6315_LINK_RESP_SIZE, 0, M6315_LINK_RESP_TIMO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
/* send "AT+CGREG?" commond to check netweork interface device link status */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") < 0)
|
||||
{
|
||||
rt_thread_mdelay(M6315_LINK_DELAY_TIME);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
link_status = -1;
|
||||
at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %d,%d", &result_code, &link_status);
|
||||
|
||||
/* check the network interface device link status */
|
||||
if ((M6315_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
|
||||
{
|
||||
netdev_low_level_set_link_status(netdev, (M6315_LINK_STATUS_OK == link_status));
|
||||
}
|
||||
|
||||
rt_thread_mdelay(M6315_LINK_DELAY_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
static int m6315_netdev_check_link_status(struct netdev *netdev)
|
||||
{
|
||||
#define M6315_LINK_THREAD_TICK 20
|
||||
#define M6315_LINK_THREAD_STACK_SIZE (1024 + 512)
|
||||
#define M6315_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
|
||||
|
||||
rt_thread_t tid;
|
||||
char tname[RT_NAME_MAX] = {0};
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
|
||||
|
||||
tid = rt_thread_create(tname, check_link_status_entry, (void *) netdev,
|
||||
M6315_LINK_THREAD_STACK_SIZE, M6315_LINK_THREAD_PRIORITY, M6315_LINK_THREAD_TICK);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int m6315_net_init(struct at_device *device);
|
||||
|
||||
static int m6315_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_FALSE)
|
||||
{
|
||||
m6315_net_init(device);
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
LOG_D("network interface device(%s) set up status.", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int m6315_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_TRUE)
|
||||
{
|
||||
m6315_power_off(device);
|
||||
device->is_init = RT_FALSE;
|
||||
|
||||
netdev_low_level_set_status(netdev, RT_FALSE);
|
||||
LOG_D("network interface device(%s) set down status.", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int m6315_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
|
||||
{
|
||||
#define M6315_DNS_RESP_LEN 8
|
||||
#define M6315_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
|
||||
|
||||
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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(M6315_DNS_RESP_LEN, 0, M6315_DNS_RESP_TIMEO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_D("no memory for resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send "AT+QIDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=\"%s\"", inet_ntoa(*dns_server)) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
static int m6315_netdev_ping(struct netdev *netdev, const char *host,
|
||||
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
|
||||
{
|
||||
#define M6315_PING_RESP_SIZE 128
|
||||
#define M6315_PING_IP_SIZE 16
|
||||
#define M6315_PING_TIMEO (5 * RT_TICK_PER_SECOND)
|
||||
int result = -RT_ERROR;
|
||||
int response, time, ttl, bytes;
|
||||
char ip_addr[M6315_PING_IP_SIZE] = {0};
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
int sent, recv, lost, min, max, avg;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
RT_ASSERT(host);
|
||||
RT_ASSERT(ping_resp);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* Response line number set six because no \r\nOK\r\n at the end*/
|
||||
resp = at_create_resp(M6315_PING_RESP_SIZE, 6, M6315_PING_TIMEO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send "AT+QPING="<host>"[,[<timeout>][,<pingnum>]]" timeout:1-255 second, pingnum:1-10, commond to send ping request */
|
||||
at_obj_exec_cmd(device->client, resp, "AT+QPING= \"%s\", 100, 1", host);
|
||||
sscanf(at_resp_get_line_by_kw(resp, "+QPING:"), "+QPING:%d,%*s", &response);
|
||||
switch (response)
|
||||
{
|
||||
case 0:
|
||||
if (at_resp_parse_line_args(resp, 4, "+QPING: %d, %[^,], %d, %d, %d",
|
||||
&response, ip_addr, &bytes, &time, &ttl) != RT_NULL)
|
||||
{
|
||||
/* ping result reponse at the sixth line */
|
||||
if (at_resp_parse_line_args(resp, 6, "+QPING: %d, %d, %d, %d, %d, %d, %d",
|
||||
&response, &sent, &recv, &lost, &min, &max, &avg) != RT_NULL)
|
||||
{
|
||||
// ping result 2
|
||||
if (response == 2)
|
||||
{
|
||||
inet_aton(ip_addr, &(ping_resp->ip_addr));
|
||||
ping_resp->data_len = bytes;
|
||||
ping_resp->ticks = time;
|
||||
ping_resp->ttl = ttl;
|
||||
result = RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 1:
|
||||
LOG_E("%s device Ping request timeout.", device->name);
|
||||
break;
|
||||
case 3:
|
||||
LOG_E("%s device TCP/IP stack is busy.", device->name);
|
||||
break;
|
||||
case 4:
|
||||
LOG_E("%s device Remote server not found.", device->name);
|
||||
break;
|
||||
case 5:
|
||||
LOG_E("%s device Activate PDP context failed.", device->name);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif /* NETDEV_USING_PING */
|
||||
|
||||
const struct netdev_ops m6315_netdev_ops =
|
||||
{
|
||||
m6315_netdev_set_up,
|
||||
m6315_netdev_set_down,
|
||||
|
||||
RT_NULL, /* not support set ip, netmask, gatway address */
|
||||
m6315_netdev_set_dns_server,
|
||||
RT_NULL, /* not support set DHCP status */
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
m6315_netdev_ping,
|
||||
#endif
|
||||
RT_NULL,
|
||||
};
|
||||
|
||||
static struct netdev *m6315_netdev_add(const char *netdev_name)
|
||||
{
|
||||
#define M6315_NETDEV_MTU 1500
|
||||
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 netdev create.");
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
netdev->mtu = M6315_NETDEV_MTU;
|
||||
netdev->ops = &m6315_netdev_ops;
|
||||
|
||||
#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;
|
||||
}
|
||||
|
||||
/* ============================= m6315 device operations ============================= */
|
||||
|
||||
#define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
|
||||
do { \
|
||||
(resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
|
||||
if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
|
||||
{ \
|
||||
result = -RT_ERROR; \
|
||||
goto __exit; \
|
||||
} \
|
||||
} while(0) \
|
||||
|
||||
/* init for m6315 */
|
||||
static void m6315_init_thread_entry(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
#define CPIN_RETRY 10
|
||||
#define CSQ_RETRY 10
|
||||
#define CREG_RETRY 10
|
||||
#define CGREG_RETRY 20
|
||||
#define CGATT_RETRY 10
|
||||
#define IPADDR_RETRY 10
|
||||
#define COMMON_RETRY 10
|
||||
|
||||
int i, qimux, retry_num = INIT_RETRY;
|
||||
char parsed_data[10] = {0};
|
||||
rt_err_t result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = (struct at_device *)parameter;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
resp = at_create_resp(128, 0, rt_tick_from_millisecond(500));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_D("start init %s device", device->name);
|
||||
|
||||
while (retry_num--)
|
||||
{
|
||||
rt_memset(parsed_data, 0, sizeof(parsed_data));
|
||||
rt_thread_mdelay(500);
|
||||
m6315_power_on(device);
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* wait m6315 startup finish */
|
||||
if (at_client_obj_wait_connect(client, M6315_WAIT_CONNECT_TIME))
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable echo */
|
||||
AT_SEND_CMD(client, resp, 0, 300, "ATE0");
|
||||
/* get module version */
|
||||
AT_SEND_CMD(client, resp, 0, 300, "ATI");
|
||||
/* show module version */
|
||||
for (i = 0; i < (int)resp->line_counts - 1; i++)
|
||||
{
|
||||
LOG_D("%s", at_resp_get_line(resp, i + 1));
|
||||
}
|
||||
/* check SIM card */
|
||||
for (i = 0; i < CPIN_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 2, 5 * RT_TICK_PER_SECOND, "AT+CPIN?");
|
||||
|
||||
if (at_resp_get_line_by_kw(resp, "READY"))
|
||||
{
|
||||
LOG_D("%s device SIM card detection success.", device->name);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CPIN_RETRY)
|
||||
{
|
||||
LOG_E("%s device SIM card detection failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting for dirty data to be digested */
|
||||
rt_thread_mdelay(10);
|
||||
|
||||
/* check the GSM network is registered */
|
||||
for (i = 0; i < CREG_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+CREG?");
|
||||
at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
|
||||
if (!strncmp(parsed_data, "0,1", strlen(parsed_data)) ||
|
||||
!strncmp(parsed_data, "0,5", strlen(parsed_data)))
|
||||
{
|
||||
LOG_D("%s device GSM is registered(%s),", device->name, parsed_data);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CREG_RETRY)
|
||||
{
|
||||
LOG_E("%s device GSM is register failed(%s).", device->name, parsed_data);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
|
||||
/* check packet domain attach or detach */
|
||||
for (i = 0; i < CGATT_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+CGATT?");
|
||||
at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT: %s", &parsed_data);
|
||||
if (!strncmp(parsed_data, "1", 1))
|
||||
{
|
||||
LOG_D("%s device Packet domain attach.", device->name);
|
||||
break;
|
||||
}
|
||||
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CGATT_RETRY)
|
||||
{
|
||||
LOG_E("%s device GPRS attach failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Define PDP Context */
|
||||
for (i = 0; i < COMMON_RETRY; i++)
|
||||
{
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGDCONT=1,\"IP\",\"CMNET\"") == RT_EOK)
|
||||
{
|
||||
LOG_D("%s device Define PDP Context Success.", device->name);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == COMMON_RETRY)
|
||||
{
|
||||
LOG_E("%s device Define PDP Context failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* PDP Context Activate*/
|
||||
for (i = 0; i < COMMON_RETRY; i++)
|
||||
{
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGACT=1,1") == RT_EOK)
|
||||
{
|
||||
LOG_D("%s device PDP Context Activate Success.", device->name);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == COMMON_RETRY)
|
||||
{
|
||||
LOG_E("%s device PDP Context Activate failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check the GPRS network is registered */
|
||||
for (i = 0; i < CGREG_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+CGREG?");
|
||||
at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %s", &parsed_data);
|
||||
if (!strncmp(parsed_data, "0,1", strlen(parsed_data)) ||
|
||||
!strncmp(parsed_data, "0,5", strlen(parsed_data)))
|
||||
{
|
||||
LOG_D("%s device GPRS is registered(%s).", device->name, parsed_data);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CGREG_RETRY)
|
||||
{
|
||||
LOG_E("%s device GPRS is register failed(%s).", device->name, parsed_data);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check signal strength */
|
||||
for (i = 0; i < CSQ_RETRY; i++)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 2, 300, "AT+CSQ");
|
||||
at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
|
||||
if (strncmp(parsed_data, "99,99", strlen(parsed_data)))
|
||||
{
|
||||
LOG_D("%s device signal strength: %s", device->name, parsed_data);
|
||||
break;
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == CSQ_RETRY)
|
||||
{
|
||||
LOG_E("%s device signal strength check failed (%s)", device->name, parsed_data);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check the GPRS network IP address */
|
||||
for (i = 0; i < IPADDR_RETRY; i++)
|
||||
{
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") == RT_EOK)
|
||||
{
|
||||
#define IP_ADDR_SIZE_MAX 16
|
||||
char ipaddr[IP_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
/* parse response data "+CGPADDR: 1,<IP_address>" */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*d,%s", ipaddr) > 0)
|
||||
{
|
||||
LOG_D("%s device IP address: %s", device->name, ipaddr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rt_thread_mdelay(1000);
|
||||
}
|
||||
if (i == IPADDR_RETRY)
|
||||
{
|
||||
LOG_E("%s device GPRS is get IP address failed", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Set to multiple connections */
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+QIMUX?");
|
||||
at_resp_parse_line_args_by_kw(resp, "+QIMUX:", "+QIMUX: %d", &qimux);
|
||||
if (qimux == 0)
|
||||
{
|
||||
AT_SEND_CMD(client, resp, 0, 300, "AT+QIMUX=1");
|
||||
}
|
||||
else if (qimux == 1)
|
||||
{
|
||||
/* Close Already Opened GPRS/CSD PDP*/
|
||||
AT_SEND_CMD(device->client, resp, 2, 300, "AT+QIDEACT");
|
||||
if (at_resp_get_line_by_kw(resp, "DEACT OK") == RT_NULL)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+QIDEACT\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* Start task & set entry point default apn,username,password */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIREGAPP") < 0)
|
||||
{
|
||||
LOG_E("%s device Start task & set default params failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* PDP Context Activate */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIACT") < 0)
|
||||
{
|
||||
LOG_E("%s device PDP Context Activate failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* initialize successfully */
|
||||
result = RT_EOK;
|
||||
break;
|
||||
|
||||
__exit:
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
/* power off the m6315 device */
|
||||
m6315_power_off(device);
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
LOG_I("%s device initialize retry...", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
/* set network interface device status and address information */
|
||||
m6315_netdev_set_info(device->netdev);
|
||||
/* check and create link staus sync thread */
|
||||
if (rt_thread_find(device->netdev->name) == RT_NULL)
|
||||
{
|
||||
m6315_netdev_check_link_status(device->netdev);
|
||||
}
|
||||
|
||||
LOG_I("%s device network initialize success!", device->name);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("%s device network initialize failed(%d)!", device->name, result);
|
||||
}
|
||||
}
|
||||
|
||||
static int m6315_net_init(struct at_device *device)
|
||||
{
|
||||
#ifdef AT_DEVICE_M6315_INIT_ASYN
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("m6315_net", m6315_init_thread_entry, (void *)device,
|
||||
M6315_THREAD_STACK_SIZE, M6315_THREAD_PRIORITY, 20);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("create %s device init thread failed.", device->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#else
|
||||
m6315_init_thread_entry(device);
|
||||
#endif /* AT_DEVICE_M6315_INIT_ASYN */
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static void urc_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
RT_ASSERT(data);
|
||||
|
||||
LOG_I("URC data : %.*s", size, data);
|
||||
}
|
||||
|
||||
|
||||
/* m6315 device URC table for the device control */
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"RDY", "\r\n", urc_func},
|
||||
{"+PDP DEACT", "\r\n", urc_func},
|
||||
};
|
||||
|
||||
static int m6315_init(struct at_device *device)
|
||||
{
|
||||
struct at_device_m6315 *m6315 = (struct at_device_m6315 *) device->user_data;
|
||||
|
||||
/* initialize AT client */
|
||||
at_client_init(m6315->client_name, m6315->recv_line_num);
|
||||
|
||||
device->client = at_client_get(m6315->client_name);
|
||||
if (device->client == RT_NULL)
|
||||
{
|
||||
LOG_E("get AT client(%s) failed.", m6315->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
|
||||
m6315_socket_init(device);
|
||||
#endif
|
||||
|
||||
/* add m6315 device to the netdev list */
|
||||
device->netdev = m6315_netdev_add(m6315->device_name);
|
||||
if (device->netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("get netdev(%s) failed.", m6315->device_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize m6315 pin configuration */
|
||||
if (m6315->power_pin != -1 && m6315->power_status_pin != -1)
|
||||
{
|
||||
rt_pin_mode(m6315->power_pin, PIN_MODE_OUTPUT);
|
||||
rt_pin_mode(m6315->power_status_pin, PIN_MODE_INPUT);
|
||||
}
|
||||
|
||||
/* initialize m6315 device network */
|
||||
return m6315_netdev_set_up(device->netdev);
|
||||
}
|
||||
|
||||
static int m6315_deinit(struct at_device *device)
|
||||
{
|
||||
return m6315_netdev_set_down(device->netdev);
|
||||
}
|
||||
|
||||
static int m6315_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_RESET:
|
||||
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_SET_WIFI_INFO:
|
||||
case AT_DEVICE_CTRL_GET_SIGNAL:
|
||||
case AT_DEVICE_CTRL_GET_GPS:
|
||||
case AT_DEVICE_CTRL_GET_VER:
|
||||
LOG_W("not support the control command(%d).", cmd);
|
||||
break;
|
||||
default:
|
||||
LOG_E("input error control command(%d).", cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const struct at_device_ops m6315_device_ops =
|
||||
{
|
||||
m6315_init,
|
||||
m6315_deinit,
|
||||
m6315_control,
|
||||
};
|
||||
|
||||
static int m6315_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 device class create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* fill m6315 device class object */
|
||||
#ifdef AT_USING_SOCKET
|
||||
m6315_socket_class_register(class);
|
||||
#endif
|
||||
class->device_ops = &m6315_device_ops;
|
||||
|
||||
return at_device_class_register(class, AT_DEVICE_CLASS_M6315);
|
||||
}
|
||||
INIT_DEVICE_EXPORT(m6315_device_class_register);
|
||||
|
||||
#endif /* AT_DEVICE_USING_M6315 */
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* File : at_device_m6315.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-05-16 chenyong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_M6315_H__
|
||||
#define __AT_DEVICE_M6315_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the m6315 device */
|
||||
#define AT_DEVICE_M6315_SOCKETS_NUM 8
|
||||
|
||||
struct at_device_m6315
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
int power_pin;
|
||||
int power_status_pin;
|
||||
size_t recv_line_num;
|
||||
struct at_device device;
|
||||
|
||||
void *user_data;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* m6315 device socket initialize */
|
||||
int m6315_socket_init(struct at_device *device);
|
||||
|
||||
/* m6315 device class socket register */
|
||||
int m6315_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_M6315_H__ */
|
|
@ -0,0 +1,636 @@
|
|||
/*
|
||||
* File : at_socket_m6315.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
|
||||
* 2018-06-12 malongwei first version
|
||||
* 2019-05-13 chenyong multi AT socket client support
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_m6315.h>
|
||||
|
||||
#define LOG_TAG "at.skt.m6315"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_M6315) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define M6315_MODULE_SEND_MAX_SIZE 1000
|
||||
|
||||
/* set real event by current socket and current state */
|
||||
#define SET_EVENT(socket, event) (((socket + 1) << 16) | (event))
|
||||
|
||||
/* AT socket event type */
|
||||
#define M6315_EVENT_CONN_OK (1L << 0)
|
||||
#define M6315_EVENT_SEND_OK (1L << 1)
|
||||
#define M6315_EVENT_RECV_OK (1L << 2)
|
||||
#define M6315_EVNET_CLOSE_OK (1L << 3)
|
||||
#define M6315_EVENT_CONN_FAIL (1L << 4)
|
||||
#define M6315_EVENT_SEND_FAIL (1L << 5)
|
||||
#define M6315_EVENT_CONN_ALREADY (1L << 6)
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static int m6315_socket_event_send(struct at_device *device, uint32_t event)
|
||||
{
|
||||
return (int) rt_event_send(device->socket_event, event);
|
||||
}
|
||||
|
||||
static int m6315_socket_event_recv(struct at_device *device, uint32_t event, uint32_t timeout, rt_uint8_t option)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
rt_uint32_t recved;
|
||||
|
||||
result = rt_event_recv(device->socket_event, event, option | RT_EVENT_FLAG_CLEAR, timeout, &recved);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
return recved;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 m6315_socket_close(struct at_socket *socket)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
int result = RT_EOK;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
|
||||
/* clear socket close event */
|
||||
event = SET_EVENT(device_socket, M6315_EVNET_CLOSE_OK);
|
||||
m6315_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (at_obj_exec_cmd(device->client, NULL, "AT+QICLOSE=%d", device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (m6315_socket_event_recv(device, event, rt_tick_from_millisecond(300*3), RT_EVENT_FLAG_AND) < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait close OK timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__exit:
|
||||
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 m6315_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
rt_bool_t retryed = RT_FALSE;
|
||||
at_response_t resp = RT_NULL;
|
||||
int result = RT_EOK, event_result = 0;
|
||||
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 resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
__retry:
|
||||
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, M6315_EVENT_CONN_OK | M6315_EVENT_CONN_FAIL | M6315_EVENT_CONN_ALREADY);
|
||||
m6315_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (is_client)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
/* send AT commands(eg: AT+QIOPEN=0,"TCP","x.x.x.x", 1234) to connect TCP server */
|
||||
if (at_obj_exec_cmd(device->client, RT_NULL,
|
||||
"AT+QIOPEN=%d,\"TCP\",\"%s\",%d", device_socket, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
break;
|
||||
|
||||
case AT_SOCKET_UDP:
|
||||
if (at_obj_exec_cmd(device->client, RT_NULL,
|
||||
"AT+QIOPEN=%d,\"UDP\",\"%s\",%d", device_socket, ip, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG_E("%s device not supported connect type : %d.", device->name, type);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC, the device default connection timeout is 75 seconds, but it set to 10 seconds is convenient to use */
|
||||
if (m6315_socket_event_recv(device, SET_EVENT(device_socket, 0), 10 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = m6315_socket_event_recv(device,
|
||||
M6315_EVENT_CONN_OK | M6315_EVENT_CONN_FAIL | M6315_EVENT_CONN_ALREADY, 1 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait connect OK|FAIL|ALREADY timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & M6315_EVENT_CONN_FAIL)
|
||||
{
|
||||
if (retryed == RT_FALSE)
|
||||
{
|
||||
LOG_D("%s device socket(%d) connect failed, the socket was not be closedand now will connect retry.",
|
||||
device->name, device_socket);
|
||||
if (m6315_socket_close(socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
retryed = RT_TRUE;
|
||||
goto __retry;
|
||||
}
|
||||
LOG_E("%s device socket(%d) connect failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
__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 m6315_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
uint32_t event = 0;
|
||||
int result = RT_EOK, event_result = 0;
|
||||
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;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
|
||||
resp = at_create_resp(128, 2, 20 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* clear socket connect event */
|
||||
event = SET_EVENT(device_socket, M6315_EVENT_SEND_OK | M6315_EVENT_SEND_FAIL);
|
||||
m6315_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
/* set AT client end sign to deal with '>' sign.*/
|
||||
at_obj_set_end_sign(device->client, '>');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < M6315_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = M6315_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+QISEND" commands to AT server than receive the '>' response on the first line. */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QISEND=%d,%d", device_socket, cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* waiting result event from AT URC */
|
||||
if (m6315_socket_event_recv(device, SET_EVENT(device_socket, 0), 20 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR) < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait send result timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* waiting OK or failed result */
|
||||
event_result = m6315_socket_event_recv(device,
|
||||
M6315_EVENT_SEND_OK | M6315_EVENT_SEND_FAIL, 15 * RT_TICK_PER_SECOND, RT_EVENT_FLAG_OR);
|
||||
if (event_result < 0)
|
||||
{
|
||||
LOG_E("%s device socket(%d) wait send connect OK|FAIL timeout.", device->name, device_socket);
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
/* check result */
|
||||
if (event_result & M6315_EVENT_SEND_FAIL)
|
||||
{
|
||||
LOG_E("%s device socket(%d) send failed.", device->name, device_socket);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
/* reset the end sign for data conflict */
|
||||
at_obj_set_end_sign(device->client, 0);
|
||||
|
||||
rt_mutex_release(lock);
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result > 0 ? sent_size : 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
|
||||
* -1: send AT commands error or response error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int m6315_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 5
|
||||
|
||||
int i, result = RT_EOK;
|
||||
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 init device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* The maximum response time is 20 seconds, affected by network status */
|
||||
resp = at_create_resp(128, 4, 20 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
for (i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSGIP=\"%s\"", name) < 0) //MODIFY name
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 4, "%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_thread_mdelay(10);
|
||||
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 m6315_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 void urc_connect_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (strstr(data, "ALREADY CONNECT"))
|
||||
{
|
||||
m6315_socket_event_send(device, SET_EVENT(device_socket, M6315_EVENT_CONN_ALREADY));
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (strstr(data, "CONNECT OK"))
|
||||
{
|
||||
m6315_socket_event_send(device, SET_EVENT(device_socket, M6315_EVENT_CONN_OK));
|
||||
}
|
||||
else if (strstr(data, "CONNECT FAIL"))
|
||||
{
|
||||
m6315_socket_event_send(device, SET_EVENT(device_socket, M6315_EVENT_CONN_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_send_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
// sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (rt_strstr(data, "SEND OK"))
|
||||
{
|
||||
m6315_socket_event_send(device, SET_EVENT(device_socket, M6315_EVENT_SEND_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "SEND FAIL"))
|
||||
{
|
||||
m6315_socket_event_send(device, SET_EVENT(device_socket, M6315_EVENT_SEND_FAIL));
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the current socket by receive data */
|
||||
sscanf(data, "%d,%*s", &device_socket);
|
||||
|
||||
if (rt_strstr(data, "CLOSE OK"))
|
||||
{
|
||||
m6315_socket_event_send(device, SET_EVENT(device_socket, M6315_EVNET_CLOSE_OK));
|
||||
}
|
||||
else if (rt_strstr(data, "CLOSED"))
|
||||
{
|
||||
struct at_socket *socket = RT_NULL;
|
||||
|
||||
/* get AT socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, RT_NULL, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout;
|
||||
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_ASSERT(data && size);
|
||||
|
||||
/* get the current socket and receive buffer size by receive data */
|
||||
sscanf(data, "+RECEIVE:%d,%d:", &device_socket, (int *) &bfsz);
|
||||
/* set receive timeout by receive buffer length, not less than 10 ms */
|
||||
timeout = bfsz > 10 ? bfsz : 10;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for receive buffer(%d).", 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("%s device 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);
|
||||
}
|
||||
}
|
||||
|
||||
/* m6315 device URC table for the socket data */
|
||||
static const struct at_urc urc_table[] =
|
||||
{
|
||||
{"", ", CONNECT OK\r\n", urc_connect_func},
|
||||
{"", ", CONNECT FAIL\r\n", urc_connect_func},
|
||||
{"", "ALREADY CONNECT\r\n", urc_connect_func},
|
||||
{"", "SEND OK\r\n", urc_send_func},
|
||||
{"", "SEND FAIL\r\n", urc_send_func},
|
||||
{"", ", CLOSE OK\r\n", urc_close_func},
|
||||
{"", ", CLOSED\r\n", urc_close_func},
|
||||
{"+RECEIVE:", "\r\n", urc_recv_func},
|
||||
};
|
||||
|
||||
static const struct at_socket_ops m6315_socket_ops =
|
||||
{
|
||||
m6315_socket_connect,
|
||||
m6315_socket_close,
|
||||
m6315_socket_send,
|
||||
m6315_domain_resolve,
|
||||
m6315_socket_set_event_cb,
|
||||
};
|
||||
|
||||
int m6315_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 m6315_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_M6315_SOCKETS_NUM;
|
||||
class->socket_ops = &m6315_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_M6315 && AT_USING_SOCKET */
|
|
@ -0,0 +1,947 @@
|
|||
/*
|
||||
* File : at_device_me3616.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-12-30 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_me3616.h>
|
||||
|
||||
#define LOG_TAG "at.dev.me3616"
|
||||
#include <at_log.h>
|
||||
|
||||
#ifdef AT_DEVICE_USING_ME3616
|
||||
|
||||
#ifndef ME3616_DEEP_SLEEP_EN
|
||||
#define ME3616_DEEP_SLEEP_EN 0//module support deep sleep mode
|
||||
#endif
|
||||
|
||||
#define ME3616_WAIT_CONNECT_TIME 5000
|
||||
#define ME3616_THREAD_STACK_SIZE 2048
|
||||
#define ME3616_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
|
||||
|
||||
static int me3616_power_on(struct at_device *device)
|
||||
{
|
||||
struct at_device_me3616 *me3616 = RT_NULL;
|
||||
|
||||
me3616 = (struct at_device_me3616 *)device->user_data;
|
||||
me3616->power_status = RT_TRUE;
|
||||
|
||||
/* not nead to set pin configuration for me3616 device power on */
|
||||
if (me3616->power_pin == -1)
|
||||
{
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
rt_pin_write(me3616->power_pin, PIN_HIGH);
|
||||
rt_thread_mdelay(500);
|
||||
rt_pin_write(me3616->power_pin, PIN_LOW);
|
||||
|
||||
LOG_D("power on success.");
|
||||
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
static int me3616_power_off(struct at_device *device)
|
||||
{
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device_me3616 *me3616 = RT_NULL;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
at_obj_exec_cmd(device->client, resp, "AT+ZTURNOFF");//command response does not have "\r\n" after "OK"
|
||||
/*if (at_obj_exec_cmd(device->client, resp, "AT+ZTURNOFF") != RT_EOK)
|
||||
{
|
||||
LOG_D("power off fail.");
|
||||
at_delete_resp(resp);
|
||||
return(-RT_ERROR);
|
||||
}*/
|
||||
|
||||
at_delete_resp(resp);
|
||||
|
||||
me3616 = (struct at_device_me3616 *)device->user_data;
|
||||
me3616->power_status = RT_FALSE;
|
||||
|
||||
LOG_D("power off success.");
|
||||
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
static int me3616_sleep(struct at_device *device)
|
||||
{
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device_me3616 *me3616 = RT_NULL;
|
||||
|
||||
me3616 = (struct at_device_me3616 *)device->user_data;
|
||||
if ( ! me3616->power_status)//power off
|
||||
{
|
||||
return(RT_EOK);
|
||||
}
|
||||
if (me3616->sleep_status)//is sleep status
|
||||
{
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=1,,,\"00111110\",\"00000001\"") != RT_EOK)
|
||||
{
|
||||
LOG_D("enable sleep fail.");
|
||||
at_delete_resp(resp);
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
#if ME3616_DEEP_SLEEP_EN
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ZSLR") != RT_EOK)
|
||||
{
|
||||
LOG_D("startup entry into sleep fail.");
|
||||
at_delete_resp(resp);
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
#endif
|
||||
|
||||
at_delete_resp(resp);
|
||||
me3616->sleep_status = RT_TRUE;
|
||||
|
||||
LOG_D("sleep success.");
|
||||
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
static int me3616_wakeup(struct at_device *device)
|
||||
{
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device_me3616 *me3616 = RT_NULL;
|
||||
|
||||
me3616 = (struct at_device_me3616 *)device->user_data;
|
||||
if ( ! me3616->power_status)//power off
|
||||
{
|
||||
LOG_E("the power is off and the wake-up cannot be performed");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
if ( ! me3616->sleep_status)//no sleep status
|
||||
{
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
#if ME3616_DEEP_SLEEP_EN
|
||||
if (me3616->power_pin != -1)
|
||||
{
|
||||
rt_pin_write(me3616->power_pin, PIN_HIGH);
|
||||
rt_thread_mdelay(100);
|
||||
rt_pin_write(me3616->power_pin, PIN_LOW);
|
||||
rt_thread_mdelay(200);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK)
|
||||
{
|
||||
LOG_D("wake up fail.");
|
||||
at_delete_resp(resp);
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
at_delete_resp(resp);
|
||||
me3616->sleep_status = RT_FALSE;
|
||||
|
||||
LOG_D("wake up success.");
|
||||
|
||||
return(RT_EOK);
|
||||
}
|
||||
|
||||
static int me3616_check_link_status(struct at_device *device)
|
||||
{
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device_me3616 *me3616 = RT_NULL;
|
||||
int result = -RT_ERROR;
|
||||
|
||||
RT_ASSERT(device);
|
||||
|
||||
me3616 = (struct at_device_me3616 *)device->user_data;
|
||||
if ( ! me3616->power_status)//power off
|
||||
{
|
||||
LOG_D("the power is off.");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
#if ME3616_DEEP_SLEEP_EN
|
||||
if (me3616->sleep_status)//is sleep status
|
||||
{
|
||||
if (me3616->power_pin != -1)
|
||||
{
|
||||
rt_pin_write(me3616->power_pin, PIN_HIGH);
|
||||
rt_thread_mdelay(100);
|
||||
rt_pin_write(me3616->power_pin, PIN_LOW);
|
||||
rt_thread_mdelay(200);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return(-RT_ERROR);
|
||||
}
|
||||
|
||||
result = -RT_ERROR;
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") == RT_EOK)
|
||||
{
|
||||
int link_stat = 0;
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
|
||||
{
|
||||
if (link_stat == 1 || link_stat == 5)
|
||||
{
|
||||
result = RT_EOK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if ME3616_DEEP_SLEEP_EN
|
||||
if (me3616->sleep_status)//is sleep status
|
||||
{
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ZSLR") != RT_EOK)
|
||||
{
|
||||
LOG_D("startup entry into sleep fail.");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
at_delete_resp(resp);
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
|
||||
/* ============================= me3616 network interface operations ============================= */
|
||||
/* set me3616 network interface device status and address information */
|
||||
static int me3616_netdev_set_info(struct netdev *netdev)
|
||||
{
|
||||
#define ME3616_INFO_RESP_SIZE 128
|
||||
#define ME3616_INFO_RESP_TIMO rt_tick_from_millisecond(300)
|
||||
|
||||
int result = RT_EOK;
|
||||
ip_addr_t addr;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* set network interface device status */
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_link_status(netdev, RT_TRUE);
|
||||
netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
|
||||
|
||||
resp = at_create_resp(ME3616_INFO_RESP_SIZE, 0, ME3616_INFO_RESP_TIMO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* set network interface device hardware address(IMEI) */
|
||||
{
|
||||
#define ME3616_NETDEV_HWADDR_LEN 8
|
||||
#define ME3616_IMEI_LEN 15
|
||||
|
||||
char imei[ME3616_IMEI_LEN] = {0};
|
||||
int i = 0, j = 0;
|
||||
|
||||
/* send "AT+GSN" commond to get device IMEI */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+GSN") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 2, "%s", imei) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+GSN\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device IMEI number: %s", device->name, imei);
|
||||
|
||||
netdev->hwaddr_len = ME3616_NETDEV_HWADDR_LEN;
|
||||
/* get hardware address by IMEI */
|
||||
for (i = 0, j = 0; i < ME3616_NETDEV_HWADDR_LEN && j < ME3616_IMEI_LEN; i++, j+=2)
|
||||
{
|
||||
if (j != ME3616_IMEI_LEN - 1)
|
||||
{
|
||||
netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev->hwaddr[i] = (imei[j] - '0');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* set network interface device IP address */
|
||||
{
|
||||
#define IP_ADDR_SIZE_MAX 16
|
||||
char ipaddr[IP_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
/* send "AT+CGPADDR=1" commond to get IP address */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse response data "+CGPADDR: 1,<IP_address>" */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*[^\"]\"%[^\"]", ipaddr) <= 0)
|
||||
{
|
||||
LOG_E("%s device \"AT+CGPADDR=1\" cmd error.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_D("%s device IP address: %s", device->name, ipaddr);
|
||||
|
||||
/* set network interface address information */
|
||||
inet_aton(ipaddr, &addr);
|
||||
netdev_low_level_set_ipaddr(netdev, &addr);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static void me3616_check_link_status_entry(void *parameter)
|
||||
{
|
||||
#define ME3616_LINK_DELAY_TIME (60 * RT_TICK_PER_SECOND)
|
||||
|
||||
rt_bool_t is_link_up;
|
||||
struct at_device *device = RT_NULL;
|
||||
struct netdev *netdev = (struct netdev *) parameter;
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
is_link_up = (me3616_check_link_status(device) == RT_EOK);
|
||||
|
||||
netdev_low_level_set_link_status(netdev, is_link_up);
|
||||
|
||||
rt_thread_delay(ME3616_LINK_DELAY_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
static int me3616_netdev_check_link_status(struct netdev *netdev)
|
||||
{
|
||||
#define ME3616_LINK_THREAD_TICK 20
|
||||
#define ME3616_LINK_THREAD_STACK_SIZE (1024 + 512)
|
||||
#define ME3616_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
|
||||
|
||||
rt_thread_t tid;
|
||||
char tname[RT_NAME_MAX] = {0};
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
|
||||
|
||||
/* create me3616 link status polling thread */
|
||||
tid = rt_thread_create(tname, me3616_check_link_status_entry, (void *)netdev,
|
||||
ME3616_LINK_THREAD_STACK_SIZE, ME3616_LINK_THREAD_PRIORITY, ME3616_LINK_THREAD_TICK);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int me3616_net_init(struct at_device *device);
|
||||
|
||||
static int me3616_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_FALSE)
|
||||
{
|
||||
me3616_net_init(device);
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
LOG_D("network interface device(%s) set up status.", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int me3616_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_TRUE)
|
||||
{
|
||||
me3616_power_off(device);
|
||||
device->is_init = RT_FALSE;
|
||||
|
||||
netdev_low_level_set_status(netdev, RT_FALSE);
|
||||
LOG_D("network interface device(%s) set down status.", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
static int me3616_netdev_ping(struct netdev *netdev, const char *host,
|
||||
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
|
||||
{
|
||||
#define ME3616_PING_RESP_SIZE 256
|
||||
#define ME3616_PING_IP_SIZE 16
|
||||
#define ME3616_PING_TIMEO (10 * RT_TICK_PER_SECOND)
|
||||
|
||||
rt_err_t result = RT_EOK;
|
||||
int response = -1, recv_data_len, ping_time, ttl;
|
||||
char ip_addr[ME3616_PING_IP_SIZE] = {0};
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
RT_ASSERT(host);
|
||||
RT_ASSERT(ping_resp);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(ME3616_PING_RESP_SIZE, 0, ME3616_PING_TIMEO);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+EDNS=\"%s\"", host) != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "IPV4:", "IPV4:%s\r", ip_addr) <= 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
at_resp_set_info(resp, ME3616_PING_RESP_SIZE, 8, timeout);
|
||||
|
||||
/* send "AT+PING=<host>[-l/L <p_size>] [-n/N <count>][-w/W <time>][-6][-i <value>][-d<value>] */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+PING=%s -d 1 -n 1 -w %d", ip_addr, timeout) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "received=", "%*[^=]=%d%*[^=]=%d%*[^=]=%d",
|
||||
&recv_data_len, &ping_time, &ttl) <= 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
inet_aton(ip_addr, &(ping_resp->ip_addr));
|
||||
ping_resp->data_len = recv_data_len;
|
||||
ping_resp->ticks = ping_time;
|
||||
ping_resp->ttl = ttl;
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif /* NETDEV_USING_PING */
|
||||
|
||||
const struct netdev_ops me3616_netdev_ops =
|
||||
{
|
||||
me3616_netdev_set_up,
|
||||
me3616_netdev_set_down,
|
||||
|
||||
RT_NULL,
|
||||
RT_NULL,
|
||||
RT_NULL,
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
me3616_netdev_ping,
|
||||
RT_NULL,
|
||||
#endif
|
||||
RT_NULL,
|
||||
};
|
||||
|
||||
static struct netdev *me3616_netdev_add(const char *netdev_name)
|
||||
{
|
||||
#define ETHERNET_MTU 1500
|
||||
#define HWADDR_LEN 8
|
||||
struct netdev *netdev = RT_NULL;
|
||||
|
||||
netdev = netdev_get_by_name(netdev_name);
|
||||
if(netdev != RT_NULL)
|
||||
{
|
||||
return(netdev);
|
||||
}
|
||||
|
||||
netdev = (struct netdev *)rt_calloc(1, sizeof(struct netdev));
|
||||
if (netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for netdev create.");
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
netdev->mtu = ETHERNET_MTU;
|
||||
netdev->ops = &me3616_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;
|
||||
}
|
||||
|
||||
/* ============================= me3616 device operations ============================= */
|
||||
|
||||
/* initialize for me3616 */
|
||||
static void me3616_init_thread_entry(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
#define CPIN_RETRY 5
|
||||
#define CSQ_RETRY 20
|
||||
#define CGREG_RETRY 60
|
||||
#define IPADDR_RETRY 10
|
||||
|
||||
int i;
|
||||
int retry_num = INIT_RETRY;
|
||||
rt_err_t result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = (struct at_device *) parameter;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
resp = at_create_resp(256, 0, rt_tick_from_millisecond(500));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_D("start init %s device.", device->name);
|
||||
|
||||
while (retry_num--)
|
||||
{
|
||||
/* power on the me3616 device */
|
||||
me3616_power_on(device);
|
||||
rt_thread_mdelay(2000);
|
||||
|
||||
/* wait me3616 startup finish, send AT every 500ms, if receive OK, SYNC success*/
|
||||
if (at_client_obj_wait_connect(client, ME3616_WAIT_CONNECT_TIME))
|
||||
{
|
||||
result = -RT_ETIMEOUT;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable echo */
|
||||
if (at_obj_exec_cmd(device->client, resp, "ATE0") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable PSM mode */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable eDRX mode */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CEDRXS=0") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable low power mode */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ESOSETRPT=1") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* disable sleep function */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ZSLR=0") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Get the baudrate */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+IPR?") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
at_resp_parse_line_args_by_kw(resp, "+IPR:", "+IPR: %d\r", &i);
|
||||
LOG_D("%s device baudrate %d", device->name, i);
|
||||
|
||||
/* get module version */
|
||||
if (at_obj_exec_cmd(device->client, resp, "ATI") != RT_EOK)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
for (i = 0; i < (int) resp->line_counts - 1; i++)
|
||||
{
|
||||
LOG_D("%s", at_resp_get_line(resp, i + 1));
|
||||
}
|
||||
|
||||
/* check SIM card */
|
||||
for (i = 0; i < CPIN_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CPIN?") == RT_EOK)
|
||||
{
|
||||
if (at_resp_get_line_by_kw(resp, "READY") != RT_NULL)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == CPIN_RETRY)
|
||||
{
|
||||
LOG_E("%s device SIM card detection failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check signal strength */
|
||||
for (i = 0; i < CSQ_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == RT_EOK)
|
||||
{
|
||||
int signal_strength = 0, err_rate = 0;
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %d,%d", &signal_strength, &err_rate) > 0)
|
||||
{
|
||||
if ((signal_strength != 99) && (signal_strength != 0))
|
||||
{
|
||||
LOG_D("%s device signal strength: %d, channel bit error rate: %d",
|
||||
device->name, signal_strength, err_rate);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i == CSQ_RETRY)
|
||||
{
|
||||
LOG_E("%s device signal strength check failed", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check the GPRS network is registered */
|
||||
for (i = 0; i < CGREG_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGREG?") == RT_EOK)
|
||||
{
|
||||
int link_stat = 0;
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
|
||||
{
|
||||
if ((link_stat == 1) || (link_stat == 5))
|
||||
{
|
||||
LOG_D("%s device GPRS is registered", device->name);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i == CGREG_RETRY)
|
||||
{
|
||||
LOG_E("%s device GPRS is register failed", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* check the GPRS network IP address */
|
||||
for (i = 0; i < IPADDR_RETRY; i++)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") == RT_EOK)
|
||||
{
|
||||
#define IP_ADDR_SIZE_MAX 16
|
||||
char ipaddr[IP_ADDR_SIZE_MAX] = {0};
|
||||
|
||||
/* parse response data "+CGPADDR: 1,<IP_address>" */
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*[^\"]\"%[^\"]", ipaddr) > 0)
|
||||
{
|
||||
LOG_D("%s device IP address: %s", device->name, ipaddr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i == IPADDR_RETRY)
|
||||
{
|
||||
LOG_E("%s device GPRS is get IP address failed", device->name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* initialize successfully */
|
||||
result = RT_EOK;
|
||||
break;
|
||||
|
||||
__exit:
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
/* power off the me3616 device */
|
||||
me3616_power_off(device);
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
LOG_I("%s device initialize retry...", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result == RT_EOK)
|
||||
{
|
||||
/* set network interface device status and address information */
|
||||
me3616_netdev_set_info(device->netdev);
|
||||
/* check and create link staus sync thread */
|
||||
if (rt_thread_find(device->netdev->name) == RT_NULL)
|
||||
{
|
||||
me3616_netdev_check_link_status(device->netdev);
|
||||
}
|
||||
|
||||
LOG_I("%s device network initialize success.", device->name);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("%s device network initialize failed(%d).", device->name, result);
|
||||
}
|
||||
}
|
||||
|
||||
/* me3616 device network initialize */
|
||||
static int me3616_net_init(struct at_device *device)
|
||||
{
|
||||
#ifdef AT_DEVICE_ME3616_INIT_ASYN
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("me3616_net", me3616_init_thread_entry, (void *)device,
|
||||
ME3616_THREAD_STACK_SIZE, ME3616_THREAD_PRIORITY, 20);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("create %s device init thread failed.", device->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#else
|
||||
me3616_init_thread_entry(device);
|
||||
#endif /* AT_DEVICE_ME3616_INIT_ASYN */
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int me3616_init(struct at_device *device)
|
||||
{
|
||||
struct at_device_me3616 *me3616 = RT_NULL;
|
||||
|
||||
RT_ASSERT(device);
|
||||
|
||||
me3616 = (struct at_device_me3616 *) device->user_data;
|
||||
me3616->power_status = RT_FALSE;//default power is off.
|
||||
me3616->sleep_status = RT_FALSE;//default sleep is disabled.
|
||||
|
||||
/* initialize AT client */
|
||||
at_client_init(me3616->client_name, me3616->recv_line_num);
|
||||
|
||||
device->client = at_client_get(me3616->client_name);
|
||||
if (device->client == RT_NULL)
|
||||
{
|
||||
LOG_E("get AT client(%s) failed.", me3616->client_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* register URC data execution function */
|
||||
#ifdef AT_USING_SOCKET
|
||||
me3616_socket_init(device);
|
||||
#endif
|
||||
|
||||
/* add me3616 device to the netdev list */
|
||||
device->netdev = me3616_netdev_add(me3616->device_name);
|
||||
if (device->netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("add netdev(%s) failed.", me3616->device_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize me3616 pin configuration */
|
||||
if (me3616->power_pin != -1)
|
||||
{
|
||||
rt_pin_write(me3616->power_pin, PIN_LOW);
|
||||
rt_pin_mode(me3616->power_pin, PIN_MODE_OUTPUT);
|
||||
}
|
||||
|
||||
/* initialize me3616 device network */
|
||||
return me3616_netdev_set_up(device->netdev);
|
||||
}
|
||||
|
||||
static int me3616_deinit(struct at_device *device)
|
||||
{
|
||||
RT_ASSERT(device);
|
||||
|
||||
return me3616_netdev_set_down(device->netdev);
|
||||
}
|
||||
|
||||
static int me3616_control(struct at_device *device, int cmd, void *arg)
|
||||
{
|
||||
int result = -RT_ERROR;
|
||||
|
||||
RT_ASSERT(device);
|
||||
|
||||
switch (cmd)
|
||||
{
|
||||
case AT_DEVICE_CTRL_SLEEP:
|
||||
result = me3616_sleep(device);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_WAKEUP:
|
||||
result = me3616_wakeup(device);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_POWER_ON:
|
||||
case AT_DEVICE_CTRL_POWER_OFF:
|
||||
case AT_DEVICE_CTRL_RESET:
|
||||
case AT_DEVICE_CTRL_LOW_POWER:
|
||||
case AT_DEVICE_CTRL_NET_CONN:
|
||||
case AT_DEVICE_CTRL_NET_DISCONN:
|
||||
case AT_DEVICE_CTRL_SET_WIFI_INFO:
|
||||
case AT_DEVICE_CTRL_GET_SIGNAL:
|
||||
case AT_DEVICE_CTRL_GET_GPS:
|
||||
case AT_DEVICE_CTRL_GET_VER:
|
||||
LOG_W("not support the control command(%d).", cmd);
|
||||
break;
|
||||
default:
|
||||
LOG_E("input error control command(%d).", cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const struct at_device_ops me3616_device_ops =
|
||||
{
|
||||
me3616_init,
|
||||
me3616_deinit,
|
||||
me3616_control,
|
||||
};
|
||||
|
||||
static int me3616_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 device class create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* fill me3616 device class object */
|
||||
#ifdef AT_USING_SOCKET
|
||||
me3616_socket_class_register(class);
|
||||
#endif
|
||||
class->device_ops = &me3616_device_ops;
|
||||
|
||||
return at_device_class_register(class, AT_DEVICE_CLASS_ME3616);
|
||||
}
|
||||
INIT_DEVICE_EXPORT(me3616_device_class_register);
|
||||
|
||||
#endif /* AT_DEVICE_USING_ME3616 */
|
||||
|
|
@ -0,0 +1,71 @@
|
|||
/*
|
||||
* File : at_device_me3616.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-12-30 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_ME3616_H__
|
||||
#define __AT_DEVICE_ME3616_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the me3616 device */
|
||||
#define AT_DEVICE_ME3616_SOCKETS_NUM 5
|
||||
|
||||
struct at_device_me3616
|
||||
{
|
||||
char *device_name;
|
||||
char *client_name;
|
||||
|
||||
int power_pin;
|
||||
int power_status_pin;
|
||||
size_t recv_line_num;
|
||||
struct at_device device;
|
||||
|
||||
void *socket_data;
|
||||
void *user_data;
|
||||
|
||||
rt_bool_t power_status;
|
||||
rt_bool_t sleep_status;
|
||||
};
|
||||
|
||||
#ifdef AT_USING_SOCKET
|
||||
|
||||
/* me3616 device socket initialize */
|
||||
int me3616_socket_init(struct at_device *device);
|
||||
|
||||
/* me3616 device class socket register */
|
||||
int me3616_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_ME3616_H__ */
|
||||
|
|
@ -0,0 +1,594 @@
|
|||
/*
|
||||
* File : at_socket_me3616.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-12-30 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_me3616.h>
|
||||
|
||||
#define LOG_TAG "at.skt.me3616"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_ME3616) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define ME3616_MODULE_SEND_MAX_SIZE 512
|
||||
|
||||
static int me3616_socket_fd[AT_DEVICE_ME3616_SOCKETS_NUM] = {0};
|
||||
|
||||
static at_evt_cb_t at_evt_cb_set[] = {
|
||||
[AT_SOCKET_EVT_RECV] = NULL,
|
||||
[AT_SOCKET_EVT_CLOSED] = NULL,
|
||||
};
|
||||
|
||||
static int me3616_get_socket_idx(int sock)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (sock < 0)
|
||||
{
|
||||
return(-1);
|
||||
}
|
||||
|
||||
for (i=0; i<AT_DEVICE_ME3616_SOCKETS_NUM; i++)
|
||||
{
|
||||
if (me3616_socket_fd[i] == sock)
|
||||
return(i);
|
||||
}
|
||||
|
||||
return(-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 me3616_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;
|
||||
|
||||
if (me3616_socket_fd[device_socket] == -1)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+ESOCL=%d", me3616_socket_fd[device_socket]);
|
||||
me3616_socket_fd[device_socket] = -1;
|
||||
|
||||
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 me3616_socket_connect(struct at_socket *socket, char *ip, int32_t port,
|
||||
enum at_socket_type type, rt_bool_t is_client)
|
||||
{
|
||||
#define CONN_RESP_SIZE 128
|
||||
|
||||
int type_code = 0;
|
||||
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;
|
||||
int sock = -1;
|
||||
|
||||
RT_ASSERT(ip);
|
||||
RT_ASSERT(port >= 0);
|
||||
|
||||
if ( ! is_client)
|
||||
{
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
type_code = 1;
|
||||
break;
|
||||
case AT_SOCKET_UDP:
|
||||
type_code = 2;
|
||||
break;
|
||||
default:
|
||||
LOG_E("%s device socket(%d) connect type error.", device->name, device_socket);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(CONN_RESP_SIZE, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (me3616_socket_fd[device_socket] != -1)
|
||||
{
|
||||
at_obj_exec_cmd(device->client, resp, "AT+ESOCL=%d", me3616_socket_fd[device_socket]);
|
||||
me3616_socket_fd[device_socket] = -1;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ESOC=1,%d,1", type_code) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+ESOC=", "+ESOC=%d", &sock) <= 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
at_resp_set_info(resp, CONN_RESP_SIZE, 0, (45*RT_TICK_PER_SECOND));
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ESOCON=%d,%d,\"%s\"", sock, port, ip) < 0)
|
||||
{
|
||||
at_resp_set_info(resp, CONN_RESP_SIZE, 0, rt_tick_from_millisecond(300));
|
||||
at_obj_exec_cmd(device->client, resp, "AT+ESOCL=%d", sock);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
me3616_socket_fd[device_socket] = sock;
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_get_send_size(struct at_socket *socket, size_t *nacked)
|
||||
{
|
||||
int result = 0;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
int remain_size;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.", device->name);
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ESOTCPBUF=%d", me3616_socket_fd[device_socket]) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "+ESOTCPBUF=", "+ESOTCPBUF=%d", &remain_size) <= 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
*nacked = 4096 - remain_size;
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int at_wait_send_finish(struct at_socket *socket, rt_tick_t timeout)
|
||||
{
|
||||
rt_tick_t last_time = rt_tick_get();
|
||||
size_t nacked = 0xFFFF;
|
||||
|
||||
while (rt_tick_get() - last_time <= timeout)
|
||||
{
|
||||
at_get_send_size(socket, &nacked);
|
||||
if (nacked == 0)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
rt_thread_mdelay(50);
|
||||
}
|
||||
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 me3616_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type)
|
||||
{
|
||||
#define SEND_RESP_SIZE 128
|
||||
|
||||
int result = 0;
|
||||
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;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
|
||||
resp = at_create_resp(SEND_RESP_SIZE, 2, RT_TICK_PER_SECOND/2);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < ME3616_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = ME3616_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
at_resp_set_info(resp, SEND_RESP_SIZE, 2, RT_TICK_PER_SECOND/2);
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+ESOSENDRAW=%d,%d", me3616_socket_fd[device_socket], (int)cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_get_line_by_kw(resp, "CONNECT") == RT_NULL)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
rt_thread_mdelay(5);//delay at least 4 ms
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* wait respone "NO CARRIER ... OK " */
|
||||
at_resp_set_info(resp, SEND_RESP_SIZE, 0, (2*RT_TICK_PER_SECOND));
|
||||
if (at_obj_exec_cmd(device->client, resp, "") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (type == AT_SOCKET_TCP)
|
||||
{
|
||||
at_wait_send_finish(socket, (5*RT_TICK_PER_SECOND));
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_thread_mdelay(10);//delay at least 10 ms
|
||||
}
|
||||
|
||||
sent_size += cur_pkt_size;
|
||||
}
|
||||
|
||||
__exit:
|
||||
|
||||
rt_mutex_release(lock);
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result > 0 ? sent_size : 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
|
||||
* -1: send AT commands error or response error
|
||||
* -2: wait socket event timeout
|
||||
* -5: no memory
|
||||
*/
|
||||
static int me3616_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
int result;
|
||||
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 init device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 0, (15 * RT_TICK_PER_SECOND));
|
||||
if (!resp)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+EDNS=\"%s\"", name);
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
LOG_E("%s device \"AT+EDNS=\"%s\"\" cmd error.", device->name, name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args_by_kw(resp, "IPV4:", "IPV4:%s\r", ip) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+EDNS=\"%s\"\" cmd error.", device->name, name);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
ip[15] = 0;
|
||||
if (rt_strlen(ip) < 8)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = RT_EOK;
|
||||
}
|
||||
|
||||
__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 me3616_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 void urc_close_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int sock = -1;
|
||||
int err_code = 0;
|
||||
int device_socket = 0;
|
||||
struct at_socket *socket = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *client_name = client->device->parent.name;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+ESOERR=%d,%d", &sock, &err_code);
|
||||
|
||||
device_socket = me3616_get_socket_idx(sock);
|
||||
if (device_socket < 0 || err_code < 0 || err_code > 4)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/* get at socket object by device socket descriptor */
|
||||
socket = &(device->sockets[device_socket]);
|
||||
|
||||
/* notice the socket is disconnect by remote */
|
||||
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
|
||||
{
|
||||
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size)
|
||||
{
|
||||
int sock = -1;
|
||||
int device_socket = 0;
|
||||
rt_int32_t timeout;
|
||||
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_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(data, "+ESONMI=%d,", &sock);
|
||||
device_socket = me3616_get_socket_idx(sock);
|
||||
if (device_socket < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while(temp_size < sizeof(temp))
|
||||
{
|
||||
at_client_obj_recv(client, temp+temp_size, 1, 10);
|
||||
if ( *(temp+temp_size) == ',')
|
||||
{
|
||||
*(temp+temp_size) = 0;
|
||||
break;
|
||||
}
|
||||
temp_size++;
|
||||
}
|
||||
if (temp_size == sizeof(temp))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
sscanf(temp, "%d", (int *)&bfsz);
|
||||
if(bfsz == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
timeout = bfsz > 10 ? bfsz : 10;
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for URC receive buffer(%d).", bfsz);
|
||||
/* read and clean the coming data */
|
||||
temp_size = 0;
|
||||
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;
|
||||
}
|
||||
|
||||
if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz)
|
||||
{
|
||||
LOG_E("%s device receive size(%d) data failed.", device->name, bfsz);
|
||||
rt_free(recv_buf);
|
||||
return;
|
||||
}
|
||||
|
||||
/* read end "\r\n" */
|
||||
at_client_obj_recv(client, temp, 2, 5);
|
||||
|
||||
/* 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[] =
|
||||
{
|
||||
{"+ESOERR=", "\r\n", urc_close_func},
|
||||
{"+ESONMI=", ",", urc_recv_func},
|
||||
};
|
||||
|
||||
static const struct at_socket_ops me3616_socket_ops =
|
||||
{
|
||||
me3616_socket_connect,
|
||||
me3616_socket_close,
|
||||
me3616_socket_send,
|
||||
me3616_domain_resolve,
|
||||
me3616_socket_set_event_cb,
|
||||
};
|
||||
|
||||
int me3616_socket_init(struct at_device *device)
|
||||
{
|
||||
RT_ASSERT(device);
|
||||
|
||||
rt_memset(me3616_socket_fd, -1, sizeof(me3616_socket_fd));
|
||||
|
||||
/* 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 me3616_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_ME3616_SOCKETS_NUM;
|
||||
class->socket_ops = &me3616_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_ME3616 && AT_USING_SOCKET */
|
||||
|
|
@ -277,7 +277,7 @@ static int sim76xx_netdev_check_link_status(struct netdev *netdev)
|
|||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
|
||||
|
||||
tid = rt_thread_create(tname, check_link_status_entry, (void *)netdev,
|
||||
SIM76XX_LINK_THREAD_STACK_SIZE, SIM76XX_LINK_THREAD_PRIORITY, SIM76XX_LINK_THREAD_TICK);
|
||||
|
@ -529,7 +529,7 @@ static void sim76xx_init_thread_entry(void *parameter)
|
|||
at_obj_exec_cmd(client, resp, "AT+CPIN?");
|
||||
if (at_resp_get_line_by_kw(resp, "READY"))
|
||||
{
|
||||
LOG_D("%s device SIM card detection failed.", device->name);
|
||||
LOG_D("%s device SIM card detection success.", device->name);
|
||||
break;
|
||||
}
|
||||
LOG_I("\"AT+CPIN\" commands send retry...");
|
||||
|
@ -704,7 +704,11 @@ static void sim76xx_init_thread_entry(void *parameter)
|
|||
{
|
||||
/* set network interface device status and address information */
|
||||
sim76xx_netdev_set_info(device->netdev);
|
||||
/* check and create link staus sync thread */
|
||||
if (rt_thread_find(device->netdev->name) == RT_NULL)
|
||||
{
|
||||
sim76xx_netdev_check_link_status(device->netdev);
|
||||
}
|
||||
|
||||
LOG_I("%s device network initialize success!", device->name);
|
||||
}
|
||||
|
|
|
@ -298,7 +298,7 @@ static int sim800c_netdev_check_link_status(struct netdev *netdev)
|
|||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
|
||||
rt_snprintf(tname, RT_NAME_MAX, "%s", netdev->name);
|
||||
|
||||
tid = rt_thread_create(tname, check_link_status_entry, (void *) netdev,
|
||||
SIM800C_LINK_THREAD_STACK_SIZE, SIM800C_LINK_THREAD_PRIORITY, SIM800C_LINK_THREAD_TICK);
|
||||
|
@ -796,7 +796,11 @@ static void sim800c_init_thread_entry(void *parameter)
|
|||
{
|
||||
/* set network interface device status and address information */
|
||||
sim800c_netdev_set_info(device->netdev);
|
||||
/* check and create link staus sync thread */
|
||||
if (rt_thread_find(device->netdev->name) == RT_NULL)
|
||||
{
|
||||
sim800c_netdev_check_link_status(device->netdev);
|
||||
}
|
||||
|
||||
LOG_I("%s device network initialize success!", device->name);
|
||||
|
||||
|
|
|
@ -84,22 +84,14 @@ static int sim800c_socket_close(struct at_socket *socket)
|
|||
{
|
||||
uint32_t event = 0;
|
||||
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;
|
||||
|
||||
resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* clear socket close event */
|
||||
event = SET_EVENT(device_socket, SIM800C_EVNET_CLOSE_OK);
|
||||
sim800c_socket_event_recv(device, event, 0, RT_EVENT_FLAG_OR);
|
||||
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+CIPCLOSE=%d", device_socket) < 0)
|
||||
if (at_obj_exec_cmd(device->client, NULL, "AT+CIPCLOSE=%d", device_socket) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
|
@ -113,11 +105,6 @@ static int sim800c_socket_close(struct at_socket *socket)
|
|||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,955 @@
|
|||
/*
|
||||
* File : at_device_w60x.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
|
||||
* 2018-06-20 chenyong first version
|
||||
* 2019-05-09 chenyong multi AT socket client support
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_w60x.h>
|
||||
|
||||
#define LOG_TAG "at.dev.w60x"
|
||||
|
||||
#include <at_log.h>
|
||||
|
||||
#ifdef AT_DEVICE_USING_W60X
|
||||
|
||||
#define W60X_WAIT_CONNECT_TIME 5000
|
||||
#define W60X_THREAD_STACK_SIZE 2048
|
||||
#define W60X_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
|
||||
|
||||
/* ============================= w60x network interface operations ============================= */
|
||||
|
||||
static int w60x_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server);
|
||||
|
||||
static rt_bool_t w60x_is_join_start = RT_FALSE;
|
||||
|
||||
static void w60x_get_netdev_info(struct rt_work *work, void *work_data)
|
||||
{
|
||||
#define AT_ADDR_LEN 32
|
||||
#define AT_ERR_DNS_SERVER "0.0.0.0"
|
||||
#define AT_DEF_DNS_SERVER "114.114.114.114"
|
||||
|
||||
at_response_t resp = RT_NULL;
|
||||
char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
|
||||
char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
|
||||
char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
|
||||
const char *resp_expr1 = "+OK=%s";
|
||||
const char *resp_expr2 = "+OK=%d,\"%[^\"]\",\"%[^\"]\",\"%[^\"]\",\"%[^\"]\",\"%[^\"]\"";
|
||||
const char *resp_expr3 = "+OK=%d,%[^,],%[^,],%[^,],%[^,]";
|
||||
rt_int32_t link_status = 0;
|
||||
ip_addr_t ip_addr;
|
||||
rt_uint32_t mac_addr[6] = {0};
|
||||
rt_uint32_t num = 0;
|
||||
rt_int32_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;
|
||||
char *pos;
|
||||
|
||||
if (delay_work)
|
||||
{
|
||||
rt_free(delay_work);
|
||||
}
|
||||
|
||||
resp = at_create_resp(512, 1, rt_tick_from_millisecond(3000));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
at_obj_set_end_sign(device->client, '\r');
|
||||
|
||||
/* send mac addr query commond "AT+QMAC" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+QMAC") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
pos = rt_strstr(resp->buf, "+OK=");
|
||||
if (!pos)
|
||||
{
|
||||
LOG_E("%s device parse \"AT+QMAC\" cmd error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sscanf(pos, resp_expr1, mac);
|
||||
|
||||
/* send addr info query commond "AT+CIPSTA?" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+LKSTT") < 0)
|
||||
{
|
||||
LOG_E("%s device send \"AT+LKSTT\" cmd error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
pos = rt_strstr(resp->buf, "+OK=");
|
||||
if (!pos)
|
||||
{
|
||||
LOG_E("%s device prase \"AT+LKSTT\" cmd error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sscanf(pos, resp_expr2, &link_status, ip, netmask, gateway, dns_server1, dns_server2);
|
||||
|
||||
/* set netdev info */
|
||||
inet_aton(gateway, &ip_addr);
|
||||
netdev_low_level_set_gw(netdev, &ip_addr);
|
||||
inet_aton(netmask, &ip_addr);
|
||||
netdev_low_level_set_netmask(netdev, &ip_addr);
|
||||
inet_aton(ip, &ip_addr);
|
||||
netdev_low_level_set_ipaddr(netdev, &ip_addr);
|
||||
sscanf(mac, "%02x%02x%02x%02x%02x%02x",
|
||||
&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];
|
||||
}
|
||||
|
||||
/* set primary DNS server address */
|
||||
if (rt_strlen(dns_server1) > 0 &&
|
||||
rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0)
|
||||
{
|
||||
inet_aton(dns_server1, &ip_addr);
|
||||
netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
|
||||
}
|
||||
else
|
||||
{
|
||||
inet_aton(AT_DEF_DNS_SERVER, &ip_addr);
|
||||
w60x_netdev_set_dns_server(netdev, 0, &ip_addr);
|
||||
}
|
||||
|
||||
/* set secondary DNS server address */
|
||||
if (rt_strlen(dns_server2) > 0 )
|
||||
{
|
||||
inet_aton(dns_server2, &ip_addr);
|
||||
netdev_low_level_set_dns_server(netdev, 1, &ip_addr);
|
||||
}
|
||||
|
||||
/* send DHCP query commond " AT+NIP" and wait response */
|
||||
if (at_obj_exec_cmd(client, resp, "AT+NIP") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
pos = rt_strstr(resp->buf, "+OK=");
|
||||
|
||||
/* parse response data, get the DHCP status */
|
||||
if (!pos)
|
||||
{
|
||||
LOG_E("%s device prase DHCP status error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sscanf(pos, resp_expr3, &dhcp_stat, ip, netmask, gateway, dns_server1);
|
||||
|
||||
/* 0 - DHCP, 1 - static ip */
|
||||
netdev_low_level_set_dhcp_status(netdev, dhcp_stat ? RT_FALSE : RT_TRUE);
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
}
|
||||
|
||||
static int w60x_net_init(struct at_device *device);
|
||||
|
||||
static int w60x_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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
if (device->is_init == RT_FALSE)
|
||||
{
|
||||
w60x_net_init(device);
|
||||
netdev_low_level_set_status(netdev, RT_TRUE);
|
||||
LOG_D("network interface device(%s) set up status", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int w60x_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 device by netdev(%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("network interface device(%s) set down status", netdev->name);
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static int w60x_netdev_set_addr_info(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
|
||||
{
|
||||
#define IPADDR_SIZE 16
|
||||
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char ip_str[IPADDR_SIZE] = {0};
|
||||
char gw_str[IPADDR_SIZE] = {0};
|
||||
char netmask_str[IPADDR_SIZE] = {0};
|
||||
char dns_str[IPADDR_SIZE] = {0};
|
||||
rt_uint8_t dhcp_stat = 0;
|
||||
const char *resp_expr = "+OK=%d,%s,%s,%s,%s";
|
||||
|
||||
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 device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 1, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
result = -RT_ENOMEM;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send DHCP query commond " AT+NIP" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NIP") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 1, resp_expr, dhcp_stat, ip_str, netmask_str, gw_str, dns_str) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase DHCP status error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* Convert numeric IP address into decimal dotted ASCII representation. */
|
||||
if (ip_addr)
|
||||
rt_memcpy(ip_str, inet_ntoa(*ip_addr), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(ip_str, inet_ntoa(netdev->ip_addr), IPADDR_SIZE);
|
||||
|
||||
if (gw)
|
||||
rt_memcpy(gw_str, inet_ntoa(*gw), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(gw_str, inet_ntoa(netdev->gw), IPADDR_SIZE);
|
||||
|
||||
if (netmask)
|
||||
rt_memcpy(netmask_str, inet_ntoa(*netmask), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(netmask_str, inet_ntoa(netdev->netmask), IPADDR_SIZE);
|
||||
|
||||
/* send addr info set commond "AT+NIP=dhcptype,ip,gateway,netmask,dnsserver" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NIP=%hhu,%s,%s,%s,%s",
|
||||
dhcp_stat, ip_str, gw_str, netmask_str, dns_str) < 0)
|
||||
{
|
||||
LOG_E("%s device set address 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("%s device set address success.", device->name);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int w60x_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char ip_str[IPADDR_SIZE] = {0};
|
||||
char gw_str[IPADDR_SIZE] = {0};
|
||||
char netmask_str[IPADDR_SIZE] = {0};
|
||||
char dns_str[IPADDR_SIZE] = {0};
|
||||
rt_uint8_t dhcp_stat = 0;
|
||||
const char *resp_expr = "+OK=%d,%s,%s,%s,%s";
|
||||
|
||||
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 device by netdev(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 1, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* send DHCP query commond " AT+NIP" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NIP") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 1, resp_expr, dhcp_stat, ip_str, netmask_str, gw_str, dns_str) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase DHCP status error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (dns_server)
|
||||
rt_memcpy(dns_str, inet_ntoa(*dns_server), IPADDR_SIZE);
|
||||
else
|
||||
rt_memcpy(dns_str, inet_ntoa(netdev->dns_servers), IPADDR_SIZE);
|
||||
|
||||
/* send addr info set commond "AT+NIP=dhcptype,ip,gateway,netmask,dnsserver" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NIP=%hhu,%s,%s,%s,%s",
|
||||
dhcp_stat, ip_str, gw_str, netmask_str, dns_str) < 0)
|
||||
{
|
||||
LOG_E("%s device set DNS failed.", device->name);
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
|
||||
LOG_D("%s device set DNS(%s) success.", device->name, inet_ntoa(*dns_server));
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int w60x_netdev_set_dhcp(struct netdev *netdev, rt_bool_t is_enabled)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char ip_str[IPADDR_SIZE] = {0};
|
||||
char gw_str[IPADDR_SIZE] = {0};
|
||||
char netmask_str[IPADDR_SIZE] = {0};
|
||||
char dns_str[IPADDR_SIZE] = {0};
|
||||
rt_uint8_t dhcp_stat = 0;
|
||||
const char *resp_expr = "+OK=%d,%s,%s,%s,%s";
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device by netdev(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 1, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp struct.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* send DHCP query commond " AT+NIP" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NIP") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (at_resp_parse_line_args(resp, 1, resp_expr, dhcp_stat, ip_str, netmask_str, gw_str, dns_str) <= 0)
|
||||
{
|
||||
LOG_E("%s device prase DHCP status error.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send addr info set commond "AT+NIP=dhcptype,ip,gateway,netmask,dnsserver" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+NIP=%hhu,%s,%s,%s,%s",
|
||||
is_enabled ? 0 : 1, ip_str, gw_str, netmask_str, dns_str) < 0)
|
||||
{
|
||||
LOG_E("%s device 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("%s device set DHCP status(%d) ok.", device->name, is_enabled);
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
static int w60x_netdev_ping(struct netdev *netdev, const char *host,
|
||||
size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp)
|
||||
{
|
||||
#define W60X_IP_SIZE 16
|
||||
|
||||
rt_err_t result = RT_EOK;
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char ip_addr[W60X_IP_SIZE] = {0};
|
||||
char *pos;
|
||||
|
||||
RT_ASSERT(netdev);
|
||||
RT_ASSERT(host);
|
||||
RT_ASSERT(ping_resp);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", netdev->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(256, 1, timeout);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
at_obj_set_end_sign(device->client, '\r');
|
||||
|
||||
/* send domain commond "AT+SKGHBN=<domain name>" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+SKGHBN=%s", host) < 0)
|
||||
{
|
||||
LOG_E("AT+SKGHBN failed.");
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse the third line of response data, get the IP address */
|
||||
pos = rt_strstr(resp->buf, "+OK=");
|
||||
if (!pos)
|
||||
{
|
||||
LOG_E("AT+SKGHBN resp error.");
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
sscanf(pos, "+OK=\"%[^\"]\"", ip_addr);
|
||||
|
||||
inet_aton(ip_addr, &(ping_resp->ip_addr));
|
||||
ping_resp->data_len = data_len;
|
||||
ping_resp->ttl = 0;
|
||||
ping_resp->ticks = 0;
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif /* NETDEV_USING_PING */
|
||||
|
||||
#ifdef NETDEV_USING_NETSTAT
|
||||
void w60x_netdev_netstat(struct netdev *netdev)
|
||||
{
|
||||
at_response_t resp = RT_NULL;
|
||||
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 device(%s) failed.", netdev->name);
|
||||
return;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 1, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* send network connection information commond "AT+LKSTT" and wait response */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+LKSTT") < 0)
|
||||
{
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
LOG_RAW("%s\n", at_resp_get_line(resp, 1));
|
||||
|
||||
/* maybe query "AT+SKSTT"... */
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
}
|
||||
#endif /* NETDEV_USING_NETSTAT */
|
||||
|
||||
static const struct netdev_ops w60x_netdev_ops =
|
||||
{
|
||||
w60x_netdev_set_up,
|
||||
w60x_netdev_set_down,
|
||||
|
||||
w60x_netdev_set_addr_info,
|
||||
w60x_netdev_set_dns_server,
|
||||
w60x_netdev_set_dhcp,
|
||||
|
||||
#ifdef NETDEV_USING_PING
|
||||
w60x_netdev_ping,
|
||||
#endif
|
||||
#ifdef NETDEV_USING_NETSTAT
|
||||
w60x_netdev_netstat,
|
||||
#endif
|
||||
};
|
||||
|
||||
static struct netdev *w60x_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 netdev create.");
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
netdev->mtu = ETHERNET_MTU;
|
||||
netdev->ops = &w60x_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;
|
||||
}
|
||||
|
||||
/* ============================= w60x device operations ============================= */
|
||||
|
||||
#define AT_SEND_CMD(client, resp, cmd) \
|
||||
do { \
|
||||
(resp) = at_resp_set_info((resp), 256, 1, 3 * RT_TICK_PER_SECOND); \
|
||||
if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
|
||||
{ \
|
||||
result = -RT_ERROR; \
|
||||
goto __exit; \
|
||||
} \
|
||||
} while(0) \
|
||||
|
||||
static void w60x_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, w60x_get_netdev_info, (void *)device);
|
||||
rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND);
|
||||
}
|
||||
|
||||
static void w60x_init_thread_entry(void *parameter)
|
||||
{
|
||||
#define INIT_RETRY 5
|
||||
|
||||
struct at_device *device = (struct at_device *) parameter;
|
||||
struct at_device_w60x *w60x = (struct at_device_w60x *) 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;
|
||||
rt_bool_t wifi_is_conn = RT_FALSE;
|
||||
rt_uint8_t atcmd_str[128];
|
||||
|
||||
LOG_D("%s device initialize start.", device->name);
|
||||
|
||||
/* wait w60x device startup finish */
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
resp = at_create_resp(256, 1, 3 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return;
|
||||
}
|
||||
|
||||
while (retry_num--)
|
||||
{
|
||||
/* reset module */
|
||||
AT_SEND_CMD(client, resp, "AT+Z");
|
||||
/* reset waiting delay */
|
||||
rt_thread_mdelay(1000);
|
||||
/* get module version */
|
||||
AT_SEND_CMD(client, resp, "AT+QVER");
|
||||
/* show module version */
|
||||
for (i = 0; i < resp->line_counts - 1; i++)
|
||||
{
|
||||
LOG_D("%s", at_resp_get_line(resp, i + 1));
|
||||
}
|
||||
|
||||
/* initialize successfully */
|
||||
result = RT_EOK;
|
||||
break;
|
||||
|
||||
__exit:
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
rt_thread_mdelay(1000);
|
||||
LOG_I("%s device initialize retry...", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
/* set current mode to Wi-Fi station */
|
||||
rt_sprintf(atcmd_str, "AT+WPRT=%d", 0);
|
||||
AT_SEND_CMD(client, resp, atcmd_str);
|
||||
rt_sprintf(atcmd_str, "AT+SSID=\"%s\"", w60x->wifi_ssid);
|
||||
AT_SEND_CMD(client, resp, atcmd_str);
|
||||
rt_sprintf(atcmd_str, "AT+KEY=1,0,\"%s\"", w60x->wifi_password);
|
||||
AT_SEND_CMD(client, resp, atcmd_str);
|
||||
AT_SEND_CMD(client, resp, "AT+PMTF");
|
||||
|
||||
/* connect to WiFi AP */
|
||||
w60x_is_join_start = RT_TRUE;
|
||||
if (at_obj_exec_cmd(client, at_resp_set_info(resp, 128, 1, 20 * RT_TICK_PER_SECOND),
|
||||
"AT+WJOIN") != RT_EOK)
|
||||
{
|
||||
LOG_W("%s device wifi connect failed, check ssid(%s) and password(%s).",
|
||||
device->name, w60x->wifi_ssid, w60x->wifi_password);
|
||||
w60x_is_join_start = RT_FALSE;
|
||||
}
|
||||
else
|
||||
{
|
||||
wifi_is_conn = RT_TRUE;
|
||||
}
|
||||
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
if (result != RT_EOK)
|
||||
{
|
||||
netdev_low_level_set_status(device->netdev, RT_FALSE);
|
||||
LOG_E("%s device network initialize failed(%d).", device->name, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
device->is_init = RT_TRUE;
|
||||
netdev_low_level_set_status(device->netdev, RT_TRUE);
|
||||
if (wifi_is_conn)
|
||||
{
|
||||
netdev_low_level_set_link_status(device->netdev, RT_TRUE);
|
||||
}
|
||||
w60x_netdev_start_delay_work(device);
|
||||
LOG_I("%s device network initialize successfully.", device->name);
|
||||
}
|
||||
}
|
||||
|
||||
static int w60x_net_init(struct at_device *device)
|
||||
{
|
||||
#ifdef AT_DEVICE_W60X_INIT_ASYN
|
||||
rt_thread_t tid;
|
||||
|
||||
tid = rt_thread_create("w60x_net", w60x_init_thread_entry, (void *) device,
|
||||
W60X_THREAD_STACK_SIZE, W60X_THREAD_PRIORITY, 20);
|
||||
if (tid)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("create %s device init thread failed.", device->name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
#else
|
||||
w60x_init_thread_entry(device);
|
||||
#endif /* AT_DEVICE_W60X_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 device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
if (w60x_is_join_start && !rt_strncmp(data, "+OK=", rt_strlen("+OK=")))
|
||||
{
|
||||
w60x_is_join_start = RT_FALSE;
|
||||
|
||||
LOG_I("%s device wifi is connected.", device->name);
|
||||
|
||||
if (device->is_init)
|
||||
{
|
||||
netdev_low_level_set_link_status(device->netdev, RT_TRUE);
|
||||
|
||||
w60x_netdev_start_delay_work(device);
|
||||
}
|
||||
}
|
||||
else if (w60x_is_join_start && !rt_strncmp(data, "+ERR=", rt_strlen("+ERR=")))
|
||||
{
|
||||
w60x_is_join_start = RT_FALSE;
|
||||
|
||||
LOG_I("%s device 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[] =
|
||||
{
|
||||
{"+OK=", "\r\n", urc_func},
|
||||
{"+ERR=", "\r\n", urc_func},
|
||||
};
|
||||
|
||||
static int w60x_init(struct at_device *device)
|
||||
{
|
||||
struct at_device_w60x *w60x = (struct at_device_w60x *) device->user_data;
|
||||
|
||||
/* initialize AT client */
|
||||
at_client_init(w60x->client_name, w60x->recv_line_num);
|
||||
|
||||
device->client = at_client_get(w60x->client_name);
|
||||
if (device->client == RT_NULL)
|
||||
{
|
||||
LOG_E("get AT client(%s) failed.", w60x->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
|
||||
w60x_socket_init(device);
|
||||
#endif
|
||||
|
||||
/* add w60x device to the netdev list */
|
||||
device->netdev = w60x_netdev_add(w60x->device_name);
|
||||
if (device->netdev == RT_NULL)
|
||||
{
|
||||
LOG_E("add netdev(%s) failed.", w60x->device_name);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
/* initialize w60x device network */
|
||||
return w60x_netdev_set_up(device->netdev);
|
||||
}
|
||||
|
||||
static int w60x_deinit(struct at_device *device)
|
||||
{
|
||||
return w60x_netdev_set_down(device->netdev);
|
||||
}
|
||||
|
||||
/* reset w60x device and initialize device network again */
|
||||
static int w60x_reset(struct at_device *device)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
struct at_client *client = device->client;
|
||||
|
||||
/* send "AT+Z" commonds to w60x device */
|
||||
result = at_obj_exec_cmd(client, RT_NULL, "AT+Z");
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* waiting 10 seconds for w60x device reset */
|
||||
device->is_init = RT_FALSE;
|
||||
rt_thread_mdelay(1000);
|
||||
|
||||
/* initialize w60x device network */
|
||||
w60x_net_init(device);
|
||||
|
||||
device->is_init = RT_TRUE;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* change w60x wifi ssid and password information */
|
||||
static int w60x_wifi_info_set(struct at_device *device, struct at_device_ssid_pwd *info)
|
||||
{
|
||||
int result = RT_EOK;
|
||||
struct at_response *resp = RT_NULL;
|
||||
rt_uint8_t atcmd_str[128];
|
||||
|
||||
if (info->ssid == RT_NULL || info->password == RT_NULL)
|
||||
{
|
||||
LOG_E("input wifi ssid(%s) and password(%s) error.", info->ssid, info->password);
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 1, 20 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* set current mode to Wi-Fi station */
|
||||
rt_sprintf(atcmd_str, "AT+WPRT=%d", 0);
|
||||
AT_SEND_CMD(device->client, resp, atcmd_str);
|
||||
rt_sprintf(atcmd_str, "AT+SSID=\"%s\"", info->ssid);
|
||||
AT_SEND_CMD(device->client, resp, atcmd_str);
|
||||
rt_sprintf(atcmd_str, "AT+KEY=1,0,\"%s\"", info->password);
|
||||
AT_SEND_CMD(device->client, resp, atcmd_str);
|
||||
AT_SEND_CMD(device->client, resp, "AT+PMTF");
|
||||
|
||||
/* connect to input wifi ap */
|
||||
w60x_is_join_start = RT_TRUE;
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+WJOIN") != RT_EOK)
|
||||
{
|
||||
LOG_E("%s device wifi connect failed, check ssid(%s) and password(%s).",
|
||||
device->name, info->ssid, info->password);
|
||||
result = -RT_ERROR;
|
||||
w60x_is_join_start = RT_FALSE;
|
||||
}
|
||||
|
||||
__exit:
|
||||
if (resp)
|
||||
{
|
||||
at_delete_resp(resp);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static int w60x_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("not support the control cmd(%d).", cmd);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_RESET:
|
||||
result = w60x_reset(device);
|
||||
break;
|
||||
case AT_DEVICE_CTRL_SET_WIFI_INFO:
|
||||
result = w60x_wifi_info_set(device, (struct at_device_ssid_pwd *) arg);
|
||||
break;
|
||||
default:
|
||||
LOG_E("input error control cmd(%d).", cmd);
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static const struct at_device_ops w60x_device_ops =
|
||||
{
|
||||
w60x_init,
|
||||
w60x_deinit,
|
||||
w60x_control,
|
||||
};
|
||||
|
||||
static int w60x_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 class create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
/* fill W60X device class object */
|
||||
#ifdef AT_USING_SOCKET
|
||||
w60x_socket_class_register(class);
|
||||
#endif
|
||||
class->device_ops = &w60x_device_ops;
|
||||
|
||||
return at_device_class_register(class, AT_DEVICE_CLASS_W60X);
|
||||
}
|
||||
INIT_DEVICE_EXPORT(w60x_device_class_register);
|
||||
|
||||
#endif /* AT_DEVICE_USING_W60X */
|
|
@ -0,0 +1,66 @@
|
|||
/*
|
||||
* File : at_device_w60x.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-05-16 chenyong first version
|
||||
*/
|
||||
|
||||
#ifndef __AT_DEVICE_W60X_H__
|
||||
#define __AT_DEVICE_W60X_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <at_device.h>
|
||||
|
||||
/* The maximum number of sockets supported by the w60x device */
|
||||
#define AT_DEVICE_W60X_SOCKETS_NUM 20
|
||||
|
||||
struct at_device_w60x
|
||||
{
|
||||
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
|
||||
|
||||
/* w60x device socket initialize */
|
||||
int w60x_socket_init(struct at_device *device);
|
||||
|
||||
/* w60x device class socket register */
|
||||
int w60x_socket_class_register(struct at_device_class *class);
|
||||
|
||||
#endif /* AT_USING_SOCKET */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __AT_DEVICE_W60X_H__ */
|
|
@ -0,0 +1,469 @@
|
|||
/*
|
||||
* File : at_socket_w60x.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
|
||||
* 2018-06-20 chenyong first version
|
||||
* 2019-05-09 chenyong multi AT socket client support
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <at_device_w60x.h>
|
||||
|
||||
#define LOG_TAG "at.skt.w60x"
|
||||
#include <at_log.h>
|
||||
|
||||
#if defined(AT_DEVICE_USING_W60X) && defined(AT_USING_SOCKET)
|
||||
|
||||
#define W60X_MODULE_SEND_MAX_SIZE 512
|
||||
|
||||
static rt_int32_t w60x_socket_fd[AT_DEVICE_W60X_SOCKETS_NUM] = {-1};
|
||||
|
||||
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 w60x_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;
|
||||
|
||||
resp = at_create_resp(64, 1, rt_tick_from_millisecond(300));
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
at_obj_set_end_sign(device->client, '\r');
|
||||
|
||||
result = at_obj_exec_cmd(device->client, resp, "AT+SKCLS=%d", w60x_socket_fd[device_socket]);
|
||||
w60x_socket_fd[device_socket] = -1;
|
||||
|
||||
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 w60x_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;
|
||||
at_response_t resp = RT_NULL;
|
||||
int device_socket = (int) socket->user_data;
|
||||
struct at_device *device = (struct at_device *) socket->device;
|
||||
int socket_fd = -1;
|
||||
|
||||
RT_ASSERT(ip);
|
||||
RT_ASSERT(port >= 0);
|
||||
|
||||
resp = at_create_resp(64, 1, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
at_obj_set_end_sign(device->client, '\r');
|
||||
|
||||
/* send the "AT+SKRPTM" commands */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+SKRPTM=1") < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case AT_SOCKET_TCP:
|
||||
/* send AT commands */
|
||||
if (at_obj_exec_cmd(device->client, resp,
|
||||
"AT+SKCT=0,%d,%s,%d", is_client ? 0 : 1, is_client ? ip : 0, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
case AT_SOCKET_UDP:
|
||||
if (at_obj_exec_cmd(device->client, resp,
|
||||
"AT+SKCT=1,%d,%s,%d", is_client ? 0 : 1, is_client ? ip : 0, port) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
LOG_E("not supported connect type %d.", type);
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if ((result != RT_EOK) || !rt_strstr(at_resp_get_line(resp, 1), "+OK="))
|
||||
{
|
||||
LOG_D("%s device socket connect failed.", device->name);
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
socket_fd = atoi(rt_strstr(at_resp_get_line(resp, 1), "+OK=") + rt_strlen("+OK="));
|
||||
w60x_socket_fd[device_socket] = socket_fd;
|
||||
|
||||
__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 w60x_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_w60x *w60x = (struct at_device_w60x *) device->user_data;
|
||||
rt_mutex_t lock = device->client->lock;
|
||||
|
||||
RT_ASSERT(buff);
|
||||
RT_ASSERT(bfsz > 0);
|
||||
|
||||
resp = at_create_resp(128, 1, 5 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_mutex_take(lock, RT_WAITING_FOREVER);
|
||||
|
||||
/* set current socket for send URC event */
|
||||
w60x->user_data = (void *) device_socket;
|
||||
|
||||
/* set AT client end sign to deal with '\n' sign */
|
||||
at_obj_set_end_sign(device->client, '\n');
|
||||
|
||||
while (sent_size < bfsz)
|
||||
{
|
||||
if (bfsz - sent_size < W60X_MODULE_SEND_MAX_SIZE)
|
||||
{
|
||||
cur_pkt_size = bfsz - sent_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_pkt_size = W60X_MODULE_SEND_MAX_SIZE;
|
||||
}
|
||||
|
||||
/* send the "AT+SKSND" commands */
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+SKSND=%d,%d", w60x_socket_fd[device_socket], cur_pkt_size) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
if (!rt_strstr(at_resp_get_line(resp, 1), "+OK="))
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* 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 > 0 ? sent_size : 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 w60x_domain_resolve(const char *name, char ip[16])
|
||||
{
|
||||
#define RESOLVE_RETRY 5
|
||||
|
||||
int i, result = RT_EOK;
|
||||
char recv_ip[16] = { 0 };
|
||||
at_response_t resp = RT_NULL;
|
||||
struct at_device *device = RT_NULL;
|
||||
char *pos;
|
||||
|
||||
RT_ASSERT(name);
|
||||
RT_ASSERT(ip);
|
||||
|
||||
device = at_device_get_first_initialized();
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get first init device failed.");
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
resp = at_create_resp(128, 1, 20 * RT_TICK_PER_SECOND);
|
||||
if (resp == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory for resp create.");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
at_obj_set_end_sign(device->client, '\r');
|
||||
|
||||
for (i = 0; i < RESOLVE_RETRY; i++)
|
||||
{
|
||||
if (at_obj_exec_cmd(device->client, resp, "AT+SKGHBN=%s", name) < 0)
|
||||
{
|
||||
result = -RT_ERROR;
|
||||
goto __exit;
|
||||
}
|
||||
|
||||
/* parse the third line of response data, get the IP address */
|
||||
pos = rt_strstr(resp->buf, "+OK=");
|
||||
if (!pos)
|
||||
{
|
||||
rt_thread_mdelay(100);
|
||||
/* resolve failed, maybe receive an URC CRLF */
|
||||
continue;
|
||||
}
|
||||
|
||||
sscanf(pos, "+OK=\"%[^\"]\"", recv_ip);
|
||||
|
||||
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 w60x_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 w60x_socket_ops =
|
||||
{
|
||||
w60x_socket_connect,
|
||||
w60x_socket_close,
|
||||
w60x_socket_send,
|
||||
w60x_domain_resolve,
|
||||
w60x_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;
|
||||
char recv_ip[16] = { 0 };
|
||||
rt_int32_t recv_port = 0;
|
||||
rt_uint8_t i;
|
||||
char *pos;
|
||||
|
||||
RT_ASSERT(data && size);
|
||||
|
||||
device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
|
||||
if (device == RT_NULL)
|
||||
{
|
||||
LOG_E("get device(%s) failed.", client_name);
|
||||
return;
|
||||
}
|
||||
|
||||
/* get the at deveice socket and receive buffer size by receive data */
|
||||
pos = rt_strstr(data, "+SKTRPT=");
|
||||
sscanf(pos, "+SKTRPT=%d,%d,%[^,],%d", &device_socket, (int *) &bfsz, recv_ip, &recv_port);
|
||||
|
||||
for (i = 0; i < AT_DEVICE_W60X_SOCKETS_NUM; i++)
|
||||
{
|
||||
if (device_socket == w60x_socket_fd[i])
|
||||
{
|
||||
device_socket = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* set receive timeout by receive buffer length, not less than 10ms */
|
||||
timeout = bfsz > 10 ? bfsz : 10;
|
||||
|
||||
if (device_socket < 0 || bfsz == 0)
|
||||
return;
|
||||
|
||||
recv_buf = (char *) rt_calloc(1, bfsz);
|
||||
if (recv_buf == RT_NULL)
|
||||
{
|
||||
LOG_E("no memory receive buffer(%d).", 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("%s device 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[] =
|
||||
{
|
||||
{"+SKTRPT=", "\r", urc_recv_func},
|
||||
{"\r\n+SKTRPT=", "\r", urc_recv_func},
|
||||
{"\r+SKTRPT=", "\r", urc_recv_func},
|
||||
{"\n+SKTRPT=", "\r", urc_recv_func},
|
||||
};
|
||||
|
||||
int w60x_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 w60x_socket_class_register(struct at_device_class *class)
|
||||
{
|
||||
RT_ASSERT(class);
|
||||
|
||||
class->socket_num = AT_DEVICE_W60X_SOCKETS_NUM;
|
||||
class->socket_ops = &w60x_socket_ops;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#endif /* AT_DEVICE_USING_W60X && AT_USING_SOCKET */
|
|
@ -40,8 +40,8 @@ extern "C" {
|
|||
#include <netdev.h>
|
||||
#endif /* RT_USING_NETDEV */
|
||||
|
||||
#define AT_DEVICE_SW_VERSION "2.0.1"
|
||||
#define AT_DEVICE_SW_VERSION_NUM 0x20001
|
||||
#define AT_DEVICE_SW_VERSION "2.0.3"
|
||||
#define AT_DEVICE_SW_VERSION_NUM 0x20003
|
||||
|
||||
/* AT device class ID */
|
||||
#define AT_DEVICE_CLASS_ESP8266 0x01U
|
||||
|
@ -51,6 +51,15 @@ extern "C" {
|
|||
#define AT_DEVICE_CLASS_SIM76XX 0x05U
|
||||
#define AT_DEVICE_CLASS_RW007 0x06U
|
||||
#define AT_DEVICE_CLASS_MW31 0x07U
|
||||
#define AT_DEVICE_CLASS_ESP32 0x08U
|
||||
#define AT_DEVICE_CLASS_W60X 0x09U
|
||||
#define AT_DEVICE_CLASS_A9G 0x0AU
|
||||
#define AT_DEVICE_CLASS_BC26 0x0BU
|
||||
#define AT_DEVICE_CLASS_AIR720 0x0CU
|
||||
#define AT_DEVICE_CLASS_ME3616 0x0DU
|
||||
#define AT_DEVICE_CLASS_M6315 0x0EU
|
||||
#define AT_DEVICE_CLASS_BC28 0x0FU
|
||||
#define AT_DEVICE_CLASS_EC200X 0x10U
|
||||
|
||||
/* Options and Commands for AT device control opreations */
|
||||
#define AT_DEVICE_CTRL_POWER_ON 0x01L
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* File : at_sample_a9g.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-11-23 luliang first version
|
||||
*/
|
||||
|
||||
#include <at_device_a9g.h>
|
||||
|
||||
#define LOG_TAG "at.sample.a9g"
|
||||
#include <at_log.h>
|
||||
|
||||
#define A9G_SAMPLE_DEIVCE_NAME "a9g0"
|
||||
|
||||
static struct at_device_a9g a9g0 =
|
||||
{
|
||||
A9G_SAMPLE_DEIVCE_NAME,
|
||||
A9G_SAMPLE_CLIENT_NAME,
|
||||
|
||||
A9G_SAMPLE_POWER_PIN,
|
||||
A9G_SAMPLE_STATUS_PIN,
|
||||
A9G_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int a9g_device_register(void)
|
||||
{
|
||||
struct at_device_a9g *a9g = &a9g0;
|
||||
|
||||
return at_device_register(&(a9g->device),
|
||||
a9g->device_name,
|
||||
a9g->client_name,
|
||||
AT_DEVICE_CLASS_A9G,
|
||||
(void *) a9g);
|
||||
}
|
||||
INIT_APP_EXPORT(a9g_device_register);
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* File : at_sample_air720.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-12-07 liang.shao first version
|
||||
*/
|
||||
|
||||
#include <at_device_air720.h>
|
||||
|
||||
#define LOG_TAG "at.sample"
|
||||
#include <at_log.h>
|
||||
|
||||
#define AIR720_SAMPLE_DEIVCE_NAME "air720"
|
||||
|
||||
static struct at_device_air720 sim0 =
|
||||
{
|
||||
AIR720_SAMPLE_DEIVCE_NAME,
|
||||
AIR720_SAMPLE_CLIENT_NAME,
|
||||
|
||||
AIR720_SAMPLE_POWER_PIN,
|
||||
AIR720_SAMPLE_STATUS_PIN,
|
||||
AIR720_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int air720_device_register(void)
|
||||
{
|
||||
struct at_device_air720 *air720 = &sim0;
|
||||
|
||||
return at_device_register(&(air720->device),
|
||||
air720->device_name,
|
||||
air720->client_name,
|
||||
AT_DEVICE_CLASS_AIR720,
|
||||
(void *) air720);
|
||||
}
|
||||
INIT_APP_EXPORT(air720_device_register);
|
||||
//MSH_CMD_EXPORT(air720_device_register,air720_device_register);
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* File : at_sample_bc26.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-12-13 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#include <at_device_bc26.h>
|
||||
|
||||
#define LOG_TAG "at.sample.bc26"
|
||||
#include <at_log.h>
|
||||
|
||||
#define BC26_SAMPLE_DEIVCE_NAME "bc26"
|
||||
|
||||
static struct at_device_bc26 _dev =
|
||||
{
|
||||
BC26_SAMPLE_DEIVCE_NAME,
|
||||
BC26_SAMPLE_CLIENT_NAME,
|
||||
|
||||
BC26_SAMPLE_POWER_PIN,
|
||||
BC26_SAMPLE_STATUS_PIN,
|
||||
BC26_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int bc26_device_register(void)
|
||||
{
|
||||
struct at_device_bc26 *bc26 = &_dev;
|
||||
|
||||
return at_device_register(&(bc26->device),
|
||||
bc26->device_name,
|
||||
bc26->client_name,
|
||||
AT_DEVICE_CLASS_BC26,
|
||||
(void *) bc26);
|
||||
}
|
||||
INIT_APP_EXPORT(bc26_device_register);
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* File : at_sample_bc28.c
|
||||
* This file is part of RT-Thread RTOS
|
||||
* Copyright (c) 2020, RudyLo <luhuadong@163.com>
|
||||
*
|
||||
* 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
|
||||
* 2020-02-13 luhuadong first version
|
||||
*/
|
||||
|
||||
#include <at_device_bc28.h>
|
||||
|
||||
#define LOG_TAG "at.sample.bc28"
|
||||
#include <at_log.h>
|
||||
|
||||
#define BC28_SAMPLE_DEIVCE_NAME "bc28"
|
||||
|
||||
static struct at_device_bc28 _dev =
|
||||
{
|
||||
BC28_SAMPLE_DEIVCE_NAME,
|
||||
BC28_SAMPLE_CLIENT_NAME,
|
||||
|
||||
BC28_SAMPLE_POWER_PIN,
|
||||
BC28_SAMPLE_STATUS_PIN,
|
||||
BC28_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int bc28_device_register(void)
|
||||
{
|
||||
struct at_device_bc28 *bc28 = &_dev;
|
||||
|
||||
return at_device_register(&(bc28->device),
|
||||
bc28->device_name,
|
||||
bc28->client_name,
|
||||
AT_DEVICE_CLASS_BC28,
|
||||
(void *) bc28);
|
||||
}
|
||||
INIT_APP_EXPORT(bc28_device_register);
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* File : at_sample_ec200x.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-12-13 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#include <at_device_ec200x.h>
|
||||
|
||||
#define LOG_TAG "at.sample.ec200x"
|
||||
#include <at_log.h>
|
||||
|
||||
#define EC200X_SAMPLE_DEIVCE_NAME "ec200x"
|
||||
|
||||
static struct at_device_ec200x _dev =
|
||||
{
|
||||
EC200X_SAMPLE_DEIVCE_NAME,
|
||||
EC200X_SAMPLE_CLIENT_NAME,
|
||||
|
||||
EC200X_SAMPLE_POWER_PIN,
|
||||
EC200X_SAMPLE_STATUS_PIN,
|
||||
EC200X_SAMPLE_WAKEUP_PIN,
|
||||
EC200X_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int ec200x_device_register(void)
|
||||
{
|
||||
struct at_device_ec200x *ec200x = &_dev;
|
||||
|
||||
return at_device_register(&(ec200x->device),
|
||||
ec200x->device_name,
|
||||
ec200x->client_name,
|
||||
AT_DEVICE_CLASS_EC200X,
|
||||
(void *) ec200x);
|
||||
}
|
||||
INIT_APP_EXPORT(ec200x_device_register);
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* File : at_sample_esp32.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-05-10 chenyong first version
|
||||
*/
|
||||
|
||||
#include <at_device_esp32.h>
|
||||
|
||||
#define LOG_TAG "at.sample.esp"
|
||||
#include <at_log.h>
|
||||
|
||||
#define ESP32_SAMPLE_DEIVCE_NAME "esp32"
|
||||
|
||||
static struct at_device_esp32 esp0 =
|
||||
{
|
||||
ESP32_SAMPLE_DEIVCE_NAME,
|
||||
ESP32_SAMPLE_CLIENT_NAME,
|
||||
|
||||
ESP32_SAMPLE_WIFI_SSID,
|
||||
ESP32_SAMPLE_WIFI_PASSWORD,
|
||||
ESP32_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int esp32_device_register(void)
|
||||
{
|
||||
struct at_device_esp32 *esp32 = &esp0;
|
||||
|
||||
return at_device_register(&(esp32->device),
|
||||
esp32->device_name,
|
||||
esp32->client_name,
|
||||
AT_DEVICE_CLASS_ESP32,
|
||||
(void *) esp32);
|
||||
}
|
||||
INIT_APP_EXPORT(esp32_device_register);
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* File : at_sample_m6315.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-05-13 chenyong first version
|
||||
*/
|
||||
|
||||
#include <at_device_m6315.h>
|
||||
|
||||
#define LOG_TAG "at.sample.m6315"
|
||||
#include <at_log.h>
|
||||
|
||||
#define M6315_SAMPLE_DEIVCE_NAME "m6315"
|
||||
|
||||
static struct at_device_m6315 m6315_dev =
|
||||
{
|
||||
M6315_SAMPLE_DEIVCE_NAME,
|
||||
M6315_SAMPLE_CLIENT_NAME,
|
||||
|
||||
M6315_SAMPLE_POWER_PIN,
|
||||
M6315_SAMPLE_STATUS_PIN,
|
||||
M6315_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int m6315_device_register(void)
|
||||
{
|
||||
struct at_device_m6315 *m6315 = &m6315_dev;
|
||||
|
||||
return at_device_register(&(m6315->device),
|
||||
m6315->device_name,
|
||||
m6315->client_name,
|
||||
AT_DEVICE_CLASS_M6315,
|
||||
(void *) m6315);
|
||||
}
|
||||
INIT_APP_EXPORT(m6315_device_register);
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* File : at_sample_me3616.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-12-30 qiyongzhong first version
|
||||
*/
|
||||
|
||||
#include <at_device_me3616.h>
|
||||
|
||||
#define LOG_TAG "at.sample.me3616"
|
||||
#include <at_log.h>
|
||||
|
||||
#define ME3616_SAMPLE_DEIVCE_NAME "me3616"
|
||||
|
||||
static struct at_device_me3616 _dev =
|
||||
{
|
||||
ME3616_SAMPLE_DEIVCE_NAME,
|
||||
ME3616_SAMPLE_CLIENT_NAME,
|
||||
|
||||
ME3616_SAMPLE_POWER_PIN,
|
||||
ME3616_SAMPLE_STATUS_PIN,
|
||||
ME3616_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int me3616_device_register(void)
|
||||
{
|
||||
struct at_device_me3616 *me3616 = &_dev;
|
||||
|
||||
return at_device_register(&(me3616->device),
|
||||
me3616->device_name,
|
||||
me3616->client_name,
|
||||
AT_DEVICE_CLASS_ME3616,
|
||||
(void *) me3616);
|
||||
}
|
||||
INIT_APP_EXPORT(me3616_device_register);
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* File : at_sample_w60x.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-05-10 chenyong first version
|
||||
*/
|
||||
|
||||
#include <at_device_w60x.h>
|
||||
|
||||
#define LOG_TAG "at.sample.w60x"
|
||||
#include <at_log.h>
|
||||
|
||||
#define W60X_SAMPLE_DEIVCE_NAME "w60x0"
|
||||
|
||||
static struct at_device_w60x w60x0 =
|
||||
{
|
||||
W60X_SAMPLE_DEIVCE_NAME,
|
||||
W60X_SAMPLE_CLIENT_NAME,
|
||||
|
||||
W60X_SAMPLE_WIFI_SSID,
|
||||
W60X_SAMPLE_WIFI_PASSWORD,
|
||||
W60X_SAMPLE_RECV_BUFF_LEN,
|
||||
};
|
||||
|
||||
static int w60x_device_register(void)
|
||||
{
|
||||
struct at_device_w60x *w60x = &w60x0;
|
||||
|
||||
return at_device_register(&(w60x->device),
|
||||
w60x->device_name,
|
||||
w60x->client_name,
|
||||
AT_DEVICE_CLASS_W60X,
|
||||
(void *) w60x);
|
||||
}
|
||||
INIT_APP_EXPORT(w60x_device_register);
|
Loading…
Reference in New Issue