diff --git a/README.md b/README.md index 68888db..196f7b3 100644 --- a/README.md +++ b/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 开发团队及社区开发者 diff --git a/SConscript b/SConscript index 0ee0ef3..6346985 100644 --- a/SConscript +++ b/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') diff --git a/class/a9g/at_device_a9g.c b/class/a9g/at_device_a9g.c new file mode 100644 index 0000000..685b57a --- /dev/null +++ b/class/a9g/at_device_a9g.c @@ -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 +#include +#include + +#include + +#define LOG_TAG "at.dev.a9g" +#include + +#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=[,]" 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 */ diff --git a/class/a9g/at_device_a9g.h b/class/a9g/at_device_a9g.h new file mode 100644 index 0000000..38be45a --- /dev/null +++ b/class/a9g/at_device_a9g.h @@ -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 + +#include + +/* 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__*/ diff --git a/class/a9g/at_socket_a9g.c b/class/a9g/at_socket_a9g.c new file mode 100644 index 0000000..aeb1ca0 --- /dev/null +++ b/class/a9g/at_socket_a9g.c @@ -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 +#include + +#include + +#define LOG_TAG "at.skt.a9g" +#include + +#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 */ diff --git a/class/air720/at_device_air720.c b/class/air720/at_device_air720.c new file mode 100644 index 0000000..189a86a --- /dev/null +++ b/class/air720/at_device_air720.c @@ -0,0 +1,1013 @@ +/* + * 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 + * 2019-12-07 liang.shao multi AT socket client support + */ + +#include +#include +#include +#include + +#if !defined(AT_SW_VERSION_NUM) || AT_SW_VERSION_NUM < 0x10300 +#error "This AT Client version is older, please check and update latest AT Client!" +#endif + +#define LOG_TAG "at.dev" +#include + +#ifdef AT_DEVICE_USING_AIR720 + +#define AIR720_WAIT_CONNECT_TIME 30000 +#define AIR720_THREAD_STACK_SIZE 2048 +#define AIR720_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2) +// #define AIR720_THREAD_PRIORITY 6 +/* 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 air720_power_on(struct at_device *device) +{ + + struct at_device_air720 *air720 = RT_NULL; + + air720 = (struct at_device_air720 *)device->user_data; + + /* not nead to set pin configuration for air720 device power on */ + if (air720->power_pin == -1 || air720->power_status_pin == -1) + { + return; + } + if (rt_pin_read(air720->power_status_pin) == PIN_HIGH) + { + return; + } + rt_pin_write(air720->power_pin, PIN_HIGH); + while (rt_pin_read(air720->power_status_pin) == PIN_LOW) + { + rt_thread_mdelay(10); + } + rt_pin_write(air720->power_pin, PIN_LOW); +} + +static void air720_power_off(struct at_device *device) +{ + struct at_device_air720 *air720 = RT_NULL; + + air720 = (struct at_device_air720 *)device->user_data; + + /* not nead to set pin configuration for m26 device power on */ + if (air720->power_pin == -1 || air720->power_status_pin == -1) + { + return; + } + if (rt_pin_read(air720->power_status_pin) == PIN_LOW) + { + return; + } + + rt_pin_write(air720->power_pin, PIN_HIGH); + while (rt_pin_read(air720->power_status_pin) == PIN_HIGH) + { + rt_thread_mdelay(10); + } + rt_pin_write(air720->power_pin, PIN_LOW); +} + +/* ============================= sim76xx network interface operations ============================= */ + +/* set air720 network interface device status and address information */ +static int air720_netdev_set_info(struct netdev *netdev) +{ +#define air720_IEMI_RESP_SIZE 32 +#define air720_IPADDR_RESP_SIZE 32 +#define air720_DNS_RESP_SIZE 96 +#define air720_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; + + if (netdev == RT_NULL) + { + LOG_E("input network interface device is NULL."); + return -RT_ERROR; + } + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get air720 device by netdev name(%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(air720_IEMI_RESP_SIZE, 0, air720_INFO_RESP_TIMO); + if (resp == RT_NULL) + { + LOG_E("air720 device(%s) set IP address failed, no memory for response object.", device->name); + result = -RT_ENOMEM; + goto __exit; + } + + /* set network interface device hardware address(IEMI) */ + { +#define air720_NETDEV_HWADDR_LEN 8 +#define air720_IEMI_LEN 15 + + char iemi[air720_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; + } + + netdev->hwaddr_len = air720_NETDEV_HWADDR_LEN; + /* get hardware address by IEMI */ + for (i = 0, j = 0; i < air720_NETDEV_HWADDR_LEN && j < air720_IEMI_LEN; i++, j += 2) + { + if (j != air720_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, air720_IPADDR_RESP_SIZE, 2, air720_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("air720 device(%s) prase \"AT+CIFSR\" commands resposne data error!", device->name); + result = -RT_ERROR; + goto __exit; + } + + LOG_E("air720 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, air720_DNS_RESP_SIZE, 0, air720_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("air720 device(%s) primary DNS server address: %s", device->name, dns_server1); + LOG_D("air720 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 air720_LINK_STATUS_OK 1 +#define air720_LINK_RESP_SIZE 64 +#define air720_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND) +#define air720_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 at_device_air720 *air720 = RT_NULL; + + char parsed_data[10] = {0}; + struct netdev *netdev = (struct netdev *)parameter; + + LOG_D("statrt air720 device(%s) link status check \n"); + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get air720 device by netdev name(%s) failed.", netdev->name); + return; + } + air720 = (struct at_device_air720 *)device->user_data; + resp = at_create_resp(air720_LINK_RESP_SIZE, 0, air720_LINK_RESP_TIMO); + if (resp == RT_NULL) + { + LOG_E("air720 device(%s) set check link status failed, no memory for response object.", device->name); + 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(air720_LINK_DELAY_TIME); + LOG_E("air720 device(%s) send cgreg failed", device->name); + 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 ((air720_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev)) + { + + netdev_low_level_set_link_status(netdev, (air720_LINK_STATUS_OK == link_status)); + } + + if (rt_pin_read(air720->power_status_pin) == PIN_HIGH) //check the module_status , if moduble_status is Low, user can do your logic here + { + if (at_obj_exec_cmd(device->client, resp, "AT+CSQ") == 0) + { + at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data); + if (strncmp(parsed_data, "99,99", sizeof(parsed_data))) + { + LOG_D("air720 device(%s) signal strength: %s", device->name, parsed_data); + } + } + } + else + { + //LTE down + LOG_E("the lte pin is low"); + } + + rt_thread_mdelay(air720_LINK_DELAY_TIME); + } +} + +static int air720_netdev_check_link_status(struct netdev *netdev) +{ + +#define air720_LINK_THREAD_TICK 20 +#define air720_LINK_THREAD_STACK_SIZE 1024 +#define air720_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 22) + + 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, + air720_LINK_THREAD_STACK_SIZE, air720_LINK_THREAD_PRIORITY, air720_LINK_THREAD_TICK); + if (tid) + { + rt_thread_startup(tid); + } + + return RT_EOK; +} + +static int air720_net_init(struct at_device *device); + +static int air720_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 air720 device by netdev name(%s) failed.", netdev->name); + return -RT_ERROR; + } + + if (device->is_init == RT_FALSE) + { + air720_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 air720_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 air720 device by netdev name(%s) failed.", netdev->name); + return -RT_ERROR; + } + + if (device->is_init == RT_TRUE) + { + air720_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 air720_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server) +{ +#define air720_DNS_RESP_LEN 8 +#define air720_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 air720 device by netdev name(%s) failed.", netdev->name); + return -RT_ERROR; + } + + resp = at_create_resp(air720_DNS_RESP_LEN, 0, air720_DNS_RESP_TIMEO); + if (resp == RT_NULL) + { + LOG_D("air720 set dns server failed, no memory for response object."); + result = -RT_ENOMEM; + goto __exit; + } + + /* send "AT+CDNSCFG=[,]" 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 air720_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(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; + } + + if (at_obj_exec_cmd(device->client, resp, "AT+CDNSGIP=\"%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, "+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 +static int air720_netdev_ping(struct netdev *netdev, const char *host, + size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp) +{ +#define air720_PING_RESP_SIZE 128 +#define air720_PING_IP_SIZE 16 +#define air720_PING_TIMEO (5 * RT_TICK_PER_SECOND) + +#define air720_PING_ERR_TIME 600 +#define air720_PING_ERR_TTL 255 + + int result = RT_EOK; + int response, time, ttl, i, err_code = 0; + char ip_addr[air720_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 air720 device by netdev name(%s) failed.", netdev->name); + return -RT_ERROR; + } + + for (i = 0; i < rt_strlen(host) && !isalpha(host[i]); i++) + ; + + if (i < strlen(host)) + { + /* check domain name is usable */ + if (air720_ping_domain_resolve(device, host, ip_addr) < 0) + { + return -RT_ERROR; + } + rt_memset(ip_addr, 0x00, air720_PING_IP_SIZE); + } + + resp = at_create_resp(air720_PING_RESP_SIZE, 0, air720_PING_TIMEO); + if (resp == RT_NULL) + { + LOG_E("air720 device(%s) set dns server failed, no memory for response object.", device->name); + 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; + } + } + + /* send "AT+CIPPING=[,[,[,[,]]]]" commond to send ping request */ + if (at_obj_exec_cmd(device->client, resp, "AT+CIPPING=%s,1,%d,%d,64", + host, data_len, air720_PING_TIMEO / (RT_TICK_PER_SECOND / 10)) < 0) + { + result = -RT_ERROR; + goto __exit; + } + + if (at_resp_parse_line_args_by_kw(resp, "+CIPPING:", "+CIPPING:%d,\"%[^\"]\",%d,%d", + &response, ip_addr, &time, &ttl) <= 0) + { + result = -RT_ERROR; + goto __exit; + } + + /* the ping request timeout expires, the response time settting to 600 and ttl setting to 255 */ + if (time == air720_PING_ERR_TIME && ttl == air720_PING_ERR_TTL) + { + result = -RT_ETIMEOUT; + goto __exit; + } + + inet_aton(ip_addr, &(ping_resp->ip_addr)); + ping_resp->data_len = data_len; + /* reply time, in units of 100 ms */ + ping_resp->ticks = time * 100; + ping_resp->ttl = ttl; + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} +#endif /* NETDEV_USING_PING */ + +#ifdef NETDEV_USING_NETSTAT +void air720_netdev_netstat(struct netdev *netdev) +{ + // TODO netstat support +} +#endif /* NETDEV_USING_NETSTAT */ + +const struct netdev_ops air720_netdev_ops = + { + air720_netdev_set_up, + air720_netdev_set_down, + + RT_NULL, /* not support set ip, netmask, gatway address */ + air720_netdev_set_dns_server, + RT_NULL, /* not support set DHCP status */ + +#ifdef NETDEV_USING_PING + air720_netdev_ping, +#endif +#ifdef NETDEV_USING_NETSTAT + air720_netdev_netstat, +#endif +}; + +static struct netdev *air720_netdev_add(const char *netdev_name) +{ +#define air720_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 air720 device(%s) netdev structure.", netdev_name); + return RT_NULL; + } + + netdev->mtu = air720_NETDEV_MTU; + netdev->ops = &air720_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; +} + +/* ============================= sim76xx 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 air720 */ +static void air720_init_thread_entry(void *parameter) +{ +#define INIT_RETRY 5 +#define CPIN_RETRY 10 +#define CSQ_RETRY 10 +#define CREG_RETRY 10 +#define CGREG_RETRY 30 + + 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 air720 device(%s) response structure.", device->name); + return; + } + + while (retry_num--) + { + rt_memset(parsed_data, 0, sizeof(parsed_data)); + rt_thread_mdelay(1000); + air720_power_on(device); + rt_thread_mdelay(5000); //check the air720 hardware manual, when we use the pow_key to start air720, it takes about 20s,so we put 25s here to ensure starting air720 normally. + + LOG_I("start initializing the air720 device(%s)", device->name); + /* wait air720 startup finish */ + if (at_client_obj_wait_connect(client, AIR720_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_E("%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_E("air720 device(%s) SIM card detection success.", device->name); + break; + } + rt_thread_mdelay(1000); + } + if (i == CPIN_RETRY) + { + LOG_E("air720 device(%s) 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", sizeof(parsed_data)) || + !strncmp(parsed_data, "0,5", sizeof(parsed_data))) + { + LOG_E("air720 device(%s) GSM network is registered(%s),", device->name, parsed_data); + break; + } + rt_thread_mdelay(1000); + } + if (i == CREG_RETRY) + { + LOG_E("air720 device(%s) GSM network is register failed(%s).", device->name, parsed_data); + 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", sizeof(parsed_data)) || + !strncmp(parsed_data, "0,5", sizeof(parsed_data))) + { + LOG_E("air720 device(%s) GPRS network is registered(%s).", device->name, parsed_data); + break; + } + rt_thread_mdelay(1000); + } + if (i == CGREG_RETRY) + { + LOG_E("air720 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_E("air720 device(%s) signal strength: %s", device->name, parsed_data); + break; + } + rt_thread_mdelay(1000); + } + if (i == CSQ_RETRY) + { + LOG_E("air720 device(%s) signal strength check 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. */ + AT_SEND_CMD(client, resp, 2, 20 * 1000, "AT+CIPSHUT"); + + /* 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, 300, "AT+CIPMUX=1"); + } + + // AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_MOBILE); + + AT_SEND_CMD(client, resp, 0, 300, "AT+COPS=3,0"); + 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_E("air720 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_E("air720 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, "CHINA TELECOM") == 0) + { + AT_SEND_CMD(client, resp, 0, 300, CSTT_CHINA_TELECOM); + /* "CT" */ + LOG_E("air720 device(%s) network operator: %s", device->name, parsed_data); + } + //client = device->client; + /* the device default response timeout is 150 seconds, but it set to 20 seconds is convenient to use. */ + AT_SEND_CMD(client, resp, 0, 20 * 1000, "AT+CIICR"); + + AT_SEND_CMD(client, resp, 2, 300, "AT+CIFSR"); + if (at_resp_get_line_by_kw(resp, "ERROR") != RT_NULL) + { + LOG_E("air720 device(%s) get the local address failed.", device->name); + result = -RT_ERROR; + goto __exit; + } + result = RT_EOK; + + __exit: + if (result == RT_EOK) + { + break; + } + else + { + /* power off the air720 device */ + air720_power_off(device); + rt_thread_mdelay(1000); + + LOG_I("air720 device(%s) initialize retry...", device->name); + } + } + + if (resp) + { + at_delete_resp(resp); + } + + if (result == RT_EOK) + { + /* set network interface device status and address information */ + air720_netdev_set_info(device->netdev); + air720_netdev_check_link_status(device->netdev); + LOG_I("air720 device(%s) network initialize success!", device->name); + } + else + { + LOG_E("air720 device(%s) network initialize failed(%d)!", device->name, result); + } +} + +static int air720_net_init(struct at_device *device) +{ +#ifdef AT_DEVICE_AIR720_INIT_ASYN + rt_thread_t tid; + tid = rt_thread_create("air720_net_init", air720_init_thread_entry, (void *)device, + AIR720_THREAD_STACK_SIZE, AIR720_THREAD_PRIORITY, 20); + if (tid) + { + rt_thread_startup(tid); + } + else + { + LOG_E("create air720 device(%s) initialization thread failed.", device->name); + return -RT_ERROR; + } +#else + air720_init_thread_entry(device); +#endif /* AT_DEVICE_air720_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); +} + +/* air720 device URC table for the device control */ +static const struct at_urc urc_table[] = + { + {"RDY", "\r\n", urc_func}, + {"CLOSED", "\r\n", urc_func}, + {"SMS READY", "\r\n", urc_func}, + {"UART Boot Completed", "\r\n", urc_func}, +}; + +static int air720_init(struct at_device *device) +{ + struct at_device_air720 *air720 = (struct at_device_air720 *)device->user_data; + + /* initialize AT client */ + at_client_init(air720->client_name, air720->recv_line_num); + + device->client = at_client_get(air720->client_name); + if (device->client == RT_NULL) + { + LOG_E("air720 device(%s) initialize failed, get AT client(%s) failed.", air720->device_name, air720->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 + air720_socket_init(device); +#endif + + /* add air720 device to the netdev list */ + device->netdev = air720_netdev_add(air720->device_name); + if (device->netdev == RT_NULL) + { + LOG_E("air720 device(%s) initialize failed, get network interface device failed.", air720->device_name); + return -RT_ERROR; + } + + /* initialize air720 pin configuration */ + if (air720->power_pin != -1 && air720->power_status_pin != -1) + { + rt_pin_mode(air720->power_pin, PIN_MODE_OUTPUT); + rt_pin_mode(air720->power_status_pin, PIN_MODE_INPUT); + } + + /* initialize air720 device network */ + return air720_netdev_set_up(device->netdev); +} + +static int air720_deinit(struct at_device *device) +{ + return air720_netdev_set_down(device->netdev); +} + +static int air720_reset(struct at_device *device) +{ + int result = RT_EOK; + struct at_client *client = device->client; + + /* send "AT+RST" commonds to mw31 device */ + result = at_obj_exec_cmd(client, RT_NULL, "AT+RESET"); + rt_thread_mdelay(1000); + + /* waiting 10 seconds for mw31 device reset */ + device->is_init = RT_FALSE; + if (at_client_obj_wait_connect(client, AIR720_WAIT_CONNECT_TIME)) + { + return -RT_ETIMEOUT; + } + + /* initialize air720 device network */ + air720_net_init(device); + + device->is_init = RT_TRUE; + + return result; +} + +static int air720_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_SET_WIFI_INFO: + case AT_DEVICE_CTRL_GET_SIGNAL: + case AT_DEVICE_CTRL_GET_GPS: + case AT_DEVICE_CTRL_GET_VER: + LOG_W("air720 not support the control command(%d).", cmd); + break; + case AT_DEVICE_CTRL_RESET: + result = air720_reset(device); + break; + default: + LOG_E("input error control command(%d).", cmd); + break; + } + + return result; +} + +const struct at_device_ops air720_device_ops = + { + air720_init, + air720_deinit, + air720_control, +}; + +static int air720_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 air720 device class create."); + return -RT_ENOMEM; + } + + /* fill air720 device class object */ +#ifdef AT_USING_SOCKET + air720_socket_class_register(class); +#endif + class->device_ops = &air720_device_ops; + + return at_device_class_register(class, AT_DEVICE_CLASS_AIR720); +} +INIT_DEVICE_EXPORT(air720_device_class_register); + +#endif /* AT_DEVICE_USING_air720 */ diff --git a/class/air720/at_device_air720.h b/class/air720/at_device_air720.h new file mode 100644 index 0000000..0c4f53c --- /dev/null +++ b/class/air720/at_device_air720.h @@ -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 + +#include + +/* 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__ */ diff --git a/class/air720/at_socket_air720.c b/class/air720/at_socket_air720.c new file mode 100644 index 0000000..acfec66 --- /dev/null +++ b/class/air720/at_socket_air720.c @@ -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 +#include +#include + + +#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 + +#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 */ diff --git a/class/bc26/at_device_bc26.c b/class/bc26/at_device_bc26.c new file mode 100644 index 0000000..f0cb44a --- /dev/null +++ b/class/bc26/at_device_bc26.c @@ -0,0 +1,1018 @@ +/* + * File : at_device_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 +#include + +#include + +#define LOG_TAG "at.dev.bc26" +#include + +#ifdef AT_DEVICE_USING_BC26 + +#define BC26_WAIT_CONNECT_TIME 5000 +#define BC26_THREAD_STACK_SIZE 2048 +#define BC26_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2) + +static int bc26_power_on(struct at_device *device) +{ + struct at_device_bc26 *bc26 = RT_NULL; + + bc26 = (struct at_device_bc26 *)device->user_data; + bc26->power_status = RT_TRUE; + + /* not nead to set pin configuration for bc26 device power on */ + if (bc26->power_pin == -1) + { + return(RT_EOK); + } + + rt_pin_write(bc26->power_pin, PIN_HIGH); + rt_thread_mdelay(500); + rt_pin_write(bc26->power_pin, PIN_LOW); + + return(RT_EOK); +} + +static int bc26_power_off(struct at_device *device) +{ + at_response_t resp = RT_NULL; + struct at_device_bc26 *bc26 = RT_NULL; + + resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_D("no memory for resp create."); + return(-RT_ERROR); + } + + if (at_obj_exec_cmd(device->client, resp, "AT+QPOWD=0") != RT_EOK) + { + LOG_D("power off fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + + at_delete_resp(resp); + + bc26 = (struct at_device_bc26 *)device->user_data; + bc26->power_status = RT_FALSE; + + return(RT_EOK); +} + +static int bc26_sleep(struct at_device *device) +{ + at_response_t resp = RT_NULL; + struct at_device_bc26 *bc26 = RT_NULL; + + bc26 = (struct at_device_bc26 *)device->user_data; + if ( ! bc26->power_status)//power off + { + return(RT_EOK); + } + if (bc26->sleep_status)//is sleep status + { + return(RT_EOK); + } + + resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_D("no memory for resp create."); + return(-RT_ERROR); + } + + /* enable sleep mode */ + if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=1") != RT_EOK) + + { + LOG_D("enable sleep fail.\"AT+QSCLK=1\" execute fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + + /* enable PSM mode */ + if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=1,,,\"01000011\",\"00000001\"") != RT_EOK) + + { + LOG_D("enable sleep fail.\"AT+CPSMS=1...\" execute fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + + if (at_obj_exec_cmd(device->client, resp, "AT+QRELLOCK") != RT_EOK) + { + LOG_D("startup entry into sleep fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + + bc26->sleep_status = RT_TRUE; + + at_delete_resp(resp); + return(RT_EOK); +} + +static int bc26_wakeup(struct at_device *device) +{ + at_response_t resp = RT_NULL; + struct at_device_bc26 *bc26 = RT_NULL; + + bc26 = (struct at_device_bc26 *)device->user_data; + if ( ! bc26->power_status)//power off + { + LOG_E("the power is off and the wake-up cannot be performed"); + return(-RT_ERROR); + } + if ( ! bc26->sleep_status)//no sleep status + { + return(RT_EOK); + } + + resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_D("no memory for resp create."); + return(-RT_ERROR); + } + + if (bc26->power_pin != -1) + { + rt_pin_write(bc26->power_pin, PIN_HIGH); + rt_thread_mdelay(100); + rt_pin_write(bc26->power_pin, PIN_LOW); + rt_thread_mdelay(200); + } + + /* disable sleep mode */ + if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=0") != RT_EOK) + { + LOG_D("wake up fail. \"AT+QSCLK=0\" execute fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + + /* disable PSM mode */ + if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=0") != RT_EOK) + { + LOG_D("wake up fail.\"AT+CPSMS=0\" execute fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + + bc26->sleep_status = RT_FALSE; + + at_delete_resp(resp); + return(RT_EOK); +} + +static int bc26_check_link_status(struct at_device *device) +{ + at_response_t resp = RT_NULL; + struct at_device_bc26 *bc26 = RT_NULL; + int result = -RT_ERROR; + + bc26 = (struct at_device_bc26 *)device->user_data; + if ( ! bc26->power_status)//power off + { + LOG_D("the power is off."); + return(-RT_ERROR); + } + if (bc26->sleep_status)//is sleep status + { + if (bc26->power_pin != -1) + { + rt_pin_write(bc26->power_pin, PIN_HIGH); + rt_thread_mdelay(100); + rt_pin_write(bc26->power_pin, PIN_LOW); + rt_thread_mdelay(200); + } + } + + resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_D("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 (bc26->sleep_status)//is sleep status + { + if (at_obj_exec_cmd(device->client, resp, "AT+QRELLOCK") != RT_EOK) + { + LOG_D("startup entry into sleep fail."); + } + } + + at_delete_resp(resp); + + return(result); +} + + +/* ============================= bc26 network interface operations ============================= */ +/* set bc26 network interface device status and address information */ +static int bc26_netdev_set_info(struct netdev *netdev) +{ +#define BC26_INFO_RESP_SIZE 128 +#define BC26_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(BC26_INFO_RESP_SIZE, 0, BC26_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 BC26_NETDEV_HWADDR_LEN 8 + #define BC26_IMEI_LEN 15 + + char imei[BC26_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 = BC26_NETDEV_HWADDR_LEN; + /* get hardware address by IMEI */ + for (i = 0, j = 0; i < BC26_NETDEV_HWADDR_LEN && j < BC26_IMEI_LEN; i++, j+=2) + { + if (j != BC26_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," */ + if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*d,%s", 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); + } + + /* 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=1" commond to get DNS servers address */ + if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=1") != RT_EOK) + { + result = -RT_ERROR; + goto __exit; + } + + /* parse response data "+QIDNSCFG: ,," */ + if (at_resp_parse_line_args_by_kw(resp, "+QIDNSCFG:", "+QIDNSCFG: 1,\"%[^\"]\",\"%[^\"]\"", + dns_server1, dns_server2) <= 0) + { + LOG_E("%s device prase \"AT+QIDNSCFG=1\" 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 bc26_check_link_status_entry(void *parameter) +{ +#define BC26_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 = (bc26_check_link_status(device) == RT_EOK); + + netdev_low_level_set_link_status(netdev, is_link_up); + + rt_thread_delay(BC26_LINK_DELAY_TIME); + } +} + +static int bc26_netdev_check_link_status(struct netdev *netdev) +{ +#define BC26_LINK_THREAD_TICK 20 +#define BC26_LINK_THREAD_STACK_SIZE (1024 + 512) +#define BC26_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 bc26 link status polling thread */ + tid = rt_thread_create(tname, bc26_check_link_status_entry, (void *)netdev, + BC26_LINK_THREAD_STACK_SIZE, BC26_LINK_THREAD_PRIORITY, BC26_LINK_THREAD_TICK); + if (tid != RT_NULL) + { + rt_thread_startup(tid); + } + + return RT_EOK; +} + +static int bc26_net_init(struct at_device *device); + +static int bc26_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) + { + bc26_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 bc26_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) + { + bc26_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 bc26_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server) +{ +#define BC26_DNS_RESP_LEN 64 +#define BC26_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(BC26_DNS_RESP_LEN, 0, BC26_DNS_RESP_TIMEO); + if (resp == RT_NULL) + { + LOG_D("no memory for resp create."); + result = -RT_ENOMEM; + goto __exit; + } + + /* send "AT+QIDNSCFG=[,]" 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 NETDEV_USING_PING +static int bc26_netdev_ping(struct netdev *netdev, const char *host, + size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp) +{ +#define BC26_PING_RESP_SIZE 128 +#define BC26_PING_IP_SIZE 16 +#define BC26_PING_TIMEO (5 * RT_TICK_PER_SECOND) + + rt_err_t result = RT_EOK; + int response = -1, recv_data_len, ping_time, ttl; + char ip_addr[BC26_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(BC26_PING_RESP_SIZE, 4, BC26_PING_TIMEO); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create"); + return -RT_ENOMEM; + } + + /* send "AT+QPING=""[,[][,]]" commond to send ping request */ + if (at_obj_exec_cmd(device->client, resp, "AT+QPING=1,\"%s\",%d,1", host, timeout / RT_TICK_PER_SECOND) < 0) + { + result = -RT_ERROR; + goto __exit; + } + + at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d", &response); + /* Received the ping response from the server */ + if (response == 0) + { + if (at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d,\"%[^\"]\",%d,%d,%d", + &response, ip_addr, &recv_data_len, &ping_time, &ttl) <= 0) + { + result = -RT_ERROR; + goto __exit; + } + } + + /* prase response number */ + switch (response) + { + case 0: + inet_aton(ip_addr, &(ping_resp->ip_addr)); + ping_resp->data_len = recv_data_len; + ping_resp->ticks = ping_time; + ping_resp->ttl = ttl; + result = RT_EOK; + break; + case 569: + result = -RT_ETIMEOUT; + break; + default: + result = -RT_ERROR; + break; + } + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} +#endif /* NETDEV_USING_PING */ + + + +const struct netdev_ops bc26_netdev_ops = +{ + bc26_netdev_set_up, + bc26_netdev_set_down, + + RT_NULL, + bc26_netdev_set_dns_server, + RT_NULL, + +#ifdef NETDEV_USING_PING + bc26_netdev_ping, +#endif + RT_NULL, +}; + +static struct netdev *bc26_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 = &bc26_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; +} + +/* ============================= bc26 device operations ============================= */ + +/* initialize for bc26 */ +static void bc26_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 + + 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(300)); + 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 bc26 device */ + bc26_power_on(device); + rt_thread_mdelay(1000); + + /* wait bc26 startup finish, send AT every 500ms, if receive OK, SYNC success*/ + if (at_client_obj_wait_connect(client, BC26_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 sleep mode */ + if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=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 PSM mode */ + if (at_obj_exec_cmd(device->client, resp, "AT+CPSMS=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", &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," */ + 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 bc26 device */ + bc26_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 */ + bc26_netdev_set_info(device->netdev); + /* check and create link staus sync thread */ + if (rt_thread_find(device->netdev->name) == RT_NULL) + { + bc26_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); + } +} + +/* bc26 device network initialize */ +static int bc26_net_init(struct at_device *device) +{ +#ifdef AT_DEVICE_BC26_INIT_ASYN + rt_thread_t tid; + + tid = rt_thread_create("bc26_net", bc26_init_thread_entry, (void *)device, + BC26_THREAD_STACK_SIZE, BC26_THREAD_PRIORITY, 20); + if (tid) + { + rt_thread_startup(tid); + } + else + { + LOG_E("create %s device init thread failed.", device->name); + return -RT_ERROR; + } +#else + bc26_init_thread_entry(device); +#endif /* AT_DEVICE_BC26_INIT_ASYN */ + + return RT_EOK; +} + +static int bc26_init(struct at_device *device) +{ + struct at_device_bc26 *bc26 = RT_NULL; + + RT_ASSERT(device); + + bc26 = (struct at_device_bc26 *) device->user_data; + bc26->power_status = RT_FALSE;//default power is off. + bc26->sleep_status = RT_FALSE;//default sleep is disabled. + + /* initialize AT client */ + at_client_init(bc26->client_name, bc26->recv_line_num); + + device->client = at_client_get(bc26->client_name); + if (device->client == RT_NULL) + { + LOG_E("get AT client(%s) failed.", bc26->client_name); + return -RT_ERROR; + } + + /* register URC data execution function */ +#ifdef AT_USING_SOCKET + bc26_socket_init(device); +#endif + + /* add bc26 device to the netdev list */ + device->netdev = bc26_netdev_add(bc26->device_name); + if (device->netdev == RT_NULL) + { + LOG_E("add netdev(%s) failed.", bc26->device_name); + return -RT_ERROR; + } + + /* initialize bc26 pin configuration */ + if (bc26->power_pin != -1) + { + rt_pin_write(bc26->power_pin, PIN_LOW); + rt_pin_mode(bc26->power_pin, PIN_MODE_OUTPUT); + } + + /* initialize bc26 device network */ + return bc26_netdev_set_up(device->netdev); +} + +static int bc26_deinit(struct at_device *device) +{ + RT_ASSERT(device); + + return bc26_netdev_set_down(device->netdev); +} + +static int bc26_control(struct at_device *device, int cmd, void *arg) +{ + int result = -RT_ERROR; + + RT_ASSERT(device); + + switch (cmd) + { + case AT_DEVICE_CTRL_SLEEP: + result = bc26_sleep(device); + break; + case AT_DEVICE_CTRL_WAKEUP: + result = bc26_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 bc26_device_ops = +{ + bc26_init, + bc26_deinit, + bc26_control, +}; + +static int bc26_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 bc26 device class object */ +#ifdef AT_USING_SOCKET + bc26_socket_class_register(class); +#endif + class->device_ops = &bc26_device_ops; + + return at_device_class_register(class, AT_DEVICE_CLASS_BC26); +} +INIT_DEVICE_EXPORT(bc26_device_class_register); + +#endif /* AT_DEVICE_USING_BC26 */ + diff --git a/class/bc26/at_device_bc26.h b/class/bc26/at_device_bc26.h new file mode 100644 index 0000000..e04bc51 --- /dev/null +++ b/class/bc26/at_device_bc26.h @@ -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 + +#include + +/* 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__ */ + diff --git a/class/bc26/at_socket_bc26.c b/class/bc26/at_socket_bc26.c new file mode 100644 index 0000000..16f6d47 --- /dev/null +++ b/class/bc26/at_socket_bc26.c @@ -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 +#include + +#include + +#define LOG_TAG "at.skt.bc26" +#include + +#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; iclient, 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 */ + diff --git a/class/bc28/at_device_bc28.c b/class/bc28/at_device_bc28.c new file mode 100644 index 0000000..beafaa5 --- /dev/null +++ b/class/bc28/at_device_bc28.c @@ -0,0 +1,953 @@ + /* + * File : at_device_bc28.c + * This file is part of RT-Thread RTOS + * Copyright (c) 2020, RudyLo + * + * 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 +#include + +#include + +#define LOG_TAG "at.dev.bc28" +#include + +#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," */ + 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:" + * "SecondaryDns:" + */ + 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=[,]" 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," */ + 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 */ \ No newline at end of file diff --git a/class/bc28/at_device_bc28.h b/class/bc28/at_device_bc28.h new file mode 100644 index 0000000..57743c3 --- /dev/null +++ b/class/bc28/at_device_bc28.h @@ -0,0 +1,70 @@ + /* + * File : at_device_bc28.h + * This file is part of RT-Thread RTOS + * Copyright (c) 2020, RudyLo + * + * 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 + +#include + +/* 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__ */ diff --git a/class/bc28/at_socket_bc28.c b/class/bc28/at_socket_bc28.c new file mode 100644 index 0000000..c36f515 --- /dev/null +++ b/class/bc28/at_socket_bc28.c @@ -0,0 +1,773 @@ +/* + * File : at_socket_bc28.c + * This file is part of RT-Thread RTOS + * Copyright (c) 2020, RudyLo + * + * 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 +#include + +#include + +#define LOG_TAG "at.skt.bc28" +#include + +#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; iclient, 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 */ + diff --git a/class/ec20/at_device_ec20.c b/class/ec20/at_device_ec20.c index e566f4e..197c16c 100644 --- a/class/ec20/at_device_ec20.c +++ b/class/ec20/at_device_ec20.c @@ -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); - ec20_netdev_check_link_status(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); } diff --git a/class/ec200x/at_device_ec200x.c b/class/ec200x/at_device_ec200x.c new file mode 100644 index 0000000..84e676f --- /dev/null +++ b/class/ec200x/at_device_ec200x.c @@ -0,0 +1,1019 @@ +/* + * File : at_device_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 +#include + +#include + +#define LOG_TAG "at.dev.ec200x" +#include + +#ifdef AT_DEVICE_USING_EC200X + +#define EC200X_WAIT_CONNECT_TIME 10000 +#define EC200X_THREAD_STACK_SIZE 2048 +#define EC200X_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2) + +static int ec200x_power_on(struct at_device *device) +{ + struct at_device_ec200x *ec200x = RT_NULL; + + ec200x = (struct at_device_ec200x *)device->user_data; + + if (ec200x->power_pin == -1)//no power on pin + { + return(RT_EOK); + } + if (ec200x->power_status_pin != -1)//use power status pin + { + ec200x->power_status = rt_pin_read(ec200x->power_status_pin);//read power status + } + if (ec200x->power_status)//power is on + { + return(RT_EOK); + } + + rt_pin_write(ec200x->power_pin, PIN_HIGH); + + if (ec200x->power_status_pin != -1)//use power status pin + { + while (rt_pin_read(ec200x->power_status_pin) == PIN_LOW) + { + rt_thread_mdelay(10); + } + } + + rt_thread_mdelay(500); + + rt_pin_write(ec200x->power_pin, PIN_LOW); + + ec200x->power_status = RT_TRUE; + + return(RT_EOK); +} + +static int ec200x_power_off(struct at_device *device) +{ + struct at_device_ec200x *ec200x = RT_NULL; + + ec200x = (struct at_device_ec200x *)device->user_data; + + if (ec200x->power_pin == -1)//no power on pin + { + return(RT_EOK); + } + if (ec200x->power_status_pin != -1)//use power status pin + { + ec200x->power_status = rt_pin_read(ec200x->power_status_pin);//read power status + } + if ( ! ec200x->power_status)//power is off + { + return(RT_EOK); + } + + if (ec200x->power_status_pin != -1)//use power status pin + { + rt_pin_write(ec200x->power_pin, PIN_HIGH); + rt_thread_mdelay(1000); + rt_pin_write(ec200x->power_pin, PIN_LOW); + + while (rt_pin_read(ec200x->power_status_pin) == PIN_HIGH)//wait power down + { + rt_thread_mdelay(100); + } + } + else + { + at_obj_exec_cmd(device->client, RT_NULL, "AT+QPOWD=0"); + rt_thread_mdelay(5*1000); + } + + ec200x->power_status = RT_FALSE; + + return(RT_EOK); +} + +static int ec200x_sleep(struct at_device *device) +{ + at_response_t resp = RT_NULL; + struct at_device_ec200x *ec200x = RT_NULL; + + ec200x = (struct at_device_ec200x *)device->user_data; + if ( ! ec200x->power_status)//power off + { + return(RT_EOK); + } + if (ec200x->sleep_status)//is sleep status + { + return(RT_EOK); + } + if (ec200x->wakeup_pin == -1)//use wakeup pin + { + LOG_E("no config wakeup pin, can not entry into sleep mode."); + return(-RT_ERROR); + } + /* + resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_D("no memory for resp create."); + return(-RT_ERROR); + } + + if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=1") != RT_EOK)//enable sleep mode + + { + LOG_D("enable sleep fail.\"AT+QSCLK=1\" execute fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + + at_delete_resp(resp); + */ + + rt_pin_write(ec200x->wakeup_pin, PIN_HIGH); + + ec200x->sleep_status = RT_TRUE; + + return(RT_EOK); +} + +static int ec200x_wakeup(struct at_device *device) +{ + at_response_t resp = RT_NULL; + struct at_device_ec200x *ec200x = RT_NULL; + + ec200x = (struct at_device_ec200x *)device->user_data; + if ( ! ec200x->power_status)//power off + { + LOG_E("the power is off and the wake-up cannot be performed"); + return(-RT_ERROR); + } + if ( ! ec200x->sleep_status)//no sleep status + { + return(RT_EOK); + } + + rt_pin_write(ec200x->wakeup_pin, PIN_LOW); + rt_thread_mdelay(200); + + /* + resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_D("no memory for resp create."); + return(-RT_ERROR); + } + if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=0") != RT_EOK)//disable sleep mode + { + LOG_D("wake up fail. \"AT+QSCLK=0\" execute fail."); + at_delete_resp(resp); + return(-RT_ERROR); + } + at_delete_resp(resp); + */ + + ec200x->sleep_status = RT_FALSE; + + return(RT_EOK); +} + +static int ec200x_check_link_status(struct at_device *device) +{ + at_response_t resp = RT_NULL; + struct at_device_ec200x *ec200x = RT_NULL; + int result = -RT_ERROR; + + ec200x = (struct at_device_ec200x *)device->user_data; + if ( ! ec200x->power_status)//power off + { + LOG_D("the power is off."); + return(-RT_ERROR); + } + if (ec200x->sleep_status)//is sleep status + { + rt_pin_write(ec200x->wakeup_pin, PIN_LOW); + rt_thread_mdelay(200); + } + + resp = at_create_resp(64, 0, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_D("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; + } + } + } + + at_delete_resp(resp); + + if (ec200x->sleep_status)//is sleep status + { + rt_pin_write(ec200x->wakeup_pin, PIN_HIGH); + } + + return(result); +} + + +/* ============================= ec200x network interface operations ============================= */ +/* set ec200x network interface device status and address information */ +static int ec200x_netdev_set_info(struct netdev *netdev) +{ +#define EC200X_INFO_RESP_SIZE 128 +#define EC200X_INFO_RESP_TIMO rt_tick_from_millisecond(1000) + + 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(EC200X_INFO_RESP_SIZE, 0, EC200X_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 EC200X_NETDEV_HWADDR_LEN 8 + #define EC200X_IMEI_LEN 15 + + char imei[EC200X_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 = EC200X_NETDEV_HWADDR_LEN; + /* get hardware address by IMEI */ + for (i = 0, j = 0; i < EC200X_NETDEV_HWADDR_LEN && j < EC200X_IMEI_LEN; i++, j+=2) + { + if (j != EC200X_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," */ + 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); + } + + /* 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=1" commond to get DNS servers address */ + if (at_obj_exec_cmd(device->client, resp, "AT+QIDNSCFG=1") != RT_EOK) + { + result = -RT_ERROR; + goto __exit; + } + + /* parse response data "+QIDNSCFG: ,," */ + if (at_resp_parse_line_args_by_kw(resp, "+QIDNSCFG:", "+QIDNSCFG: 1,\"%[^\"]\",\"%[^\"]\"", + dns_server1, dns_server2) <= 0) + { + LOG_E("%s device prase \"AT+QIDNSCFG=1\" 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 ec200x_check_link_status_entry(void *parameter) +{ +#define EC200X_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) + { + rt_thread_delay(EC200X_LINK_DELAY_TIME); + + is_link_up = (ec200x_check_link_status(device) == RT_EOK); + + netdev_low_level_set_link_status(netdev, is_link_up); + } +} + +static int ec200x_netdev_check_link_status(struct netdev *netdev) +{ +#define EC200X_LINK_THREAD_TICK 20 +#define EC200X_LINK_THREAD_STACK_SIZE (1024 + 512) +#define EC200X_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 ec200x link status polling thread */ + tid = rt_thread_create(tname, ec200x_check_link_status_entry, (void *)netdev, + EC200X_LINK_THREAD_STACK_SIZE, EC200X_LINK_THREAD_PRIORITY, EC200X_LINK_THREAD_TICK); + if (tid != RT_NULL) + { + rt_thread_startup(tid); + } + + return RT_EOK; +} + +static int ec200x_net_init(struct at_device *device); + +static int ec200x_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) + { + ec200x_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 ec200x_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) + { + ec200x_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 ec200x_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server) +{ +#define EC200X_DNS_RESP_LEN 64 +#define EC200X_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(EC200X_DNS_RESP_LEN, 0, EC200X_DNS_RESP_TIMEO); + if (resp == RT_NULL) + { + LOG_D("no memory for resp create."); + result = -RT_ENOMEM; + goto __exit; + } + + /* send "AT+QIDNSCFG=[,]" 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 NETDEV_USING_PING +static int ec200x_netdev_ping(struct netdev *netdev, const char *host, + size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp) +{ +#define EC200X_PING_RESP_SIZE 128 +#define EC200X_PING_IP_SIZE 16 +#define EC200X_PING_TIMEO (5 * RT_TICK_PER_SECOND) + + rt_err_t result = RT_EOK; + int response = -1, recv_data_len, ping_time, ttl; + char ip_addr[EC200X_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(EC200X_PING_RESP_SIZE, 4, EC200X_PING_TIMEO); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create"); + return -RT_ENOMEM; + } + + /* send "AT+QPING=""[,[][,]]" commond to send ping request */ + if (at_obj_exec_cmd(device->client, resp, "AT+QPING=1,%s,%d,1", host, timeout / RT_TICK_PER_SECOND) < 0) + { + result = -RT_ERROR; + goto __exit; + } + + at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d", &response); + /* Received the ping response from the server */ + if (response == 0) + { + if (at_resp_parse_line_args_by_kw(resp, "+QPING:", "+QPING:%d,\"%[^\"]\",%d,%d,%d", + &response, ip_addr, &recv_data_len, &ping_time, &ttl) <= 0) + { + result = -RT_ERROR; + goto __exit; + } + } + + /* prase response number */ + switch (response) + { + case 0: + inet_aton(ip_addr, &(ping_resp->ip_addr)); + ping_resp->data_len = recv_data_len; + ping_resp->ticks = ping_time; + ping_resp->ttl = ttl; + result = RT_EOK; + break; + case 569: + result = -RT_ETIMEOUT; + break; + default: + result = -RT_ERROR; + break; + } + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} +#endif /* NETDEV_USING_PING */ + + + +const struct netdev_ops ec200x_netdev_ops = +{ + ec200x_netdev_set_up, + ec200x_netdev_set_down, + + RT_NULL, + ec200x_netdev_set_dns_server, + RT_NULL, + +#ifdef NETDEV_USING_PING + ec200x_netdev_ping, +#endif + RT_NULL, +}; + +static struct netdev *ec200x_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 = &ec200x_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; +} + +/* ============================= ec200x device operations ============================= */ + +/* initialize for ec200x */ +static void ec200x_init_thread_entry(void *parameter) +{ +#define RESP_SIZE 128 +#define INIT_RETRY 5 +#define CPIN_RETRY 10 +#define CSQ_RETRY 20 +#define CGREG_RETRY 50 +#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(RESP_SIZE, 0, rt_tick_from_millisecond(300)); + 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 ec200x device */ + ec200x_power_on(device); + rt_thread_mdelay(1000); + + /* wait ec200x startup finish, send AT every 500ms, if receive OK, SYNC success*/ + if (at_client_obj_wait_connect(client, EC200X_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; + } + + /* 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", &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; + } + + if (((struct at_device_ec200x *)(device->user_data))->wakeup_pin != -1)//use wakeup pin + { + if (at_obj_exec_cmd(device->client, resp, "AT+QSCLK=1") != RT_EOK)// enable sleep mode fail + { + result = -RT_ERROR; + goto __exit; + } + } + + /* Close Echo the Data */ + if (at_obj_exec_cmd(device->client, resp, "AT+QISDE=0") != RT_EOK) + { + result = -RT_ERROR; + goto __exit; + } + + /* Deactivate context profile */ + resp = at_resp_set_info(resp, RESP_SIZE, 0, rt_tick_from_millisecond(40*1000)); + if (at_obj_exec_cmd(device->client, resp, "AT+QIDEACT=1") != RT_EOK) + { + result = -RT_ERROR; + goto __exit; + } + + /* Activate context profile */ + resp = at_resp_set_info(resp, RESP_SIZE, 0, rt_tick_from_millisecond(150*1000)); + if (at_obj_exec_cmd(device->client, resp, "AT+QIACT=1") != RT_EOK) + { + result = -RT_ERROR; + goto __exit; + } + + /* initialize successfully */ + result = RT_EOK; + break; + + __exit: + if (result != RT_EOK) + { + /* power off the ec200x device */ + ec200x_power_off(device); + rt_thread_mdelay(3000); + + 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 */ + ec200x_netdev_set_info(device->netdev); + /* check and create link staus sync thread */ + if (rt_thread_find(device->netdev->name) == RT_NULL) + { + ec200x_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); + } +} + +/* ec200x device network initialize */ +static int ec200x_net_init(struct at_device *device) +{ +#ifdef AT_DEVICE_EC200X_INIT_ASYN + rt_thread_t tid; + + tid = rt_thread_create("ec200x_net", ec200x_init_thread_entry, (void *)device, + EC200X_THREAD_STACK_SIZE, EC200X_THREAD_PRIORITY, 20); + if (tid) + { + rt_thread_startup(tid); + } + else + { + LOG_E("create %s device init thread failed.", device->name); + return -RT_ERROR; + } +#else + ec200x_init_thread_entry(device); +#endif /* AT_DEVICE_EC200X_INIT_ASYN */ + + return RT_EOK; +} + +static int ec200x_init(struct at_device *device) +{ + struct at_device_ec200x *ec200x = RT_NULL; + + RT_ASSERT(device); + + ec200x = (struct at_device_ec200x *) device->user_data; + ec200x->power_status = RT_FALSE;//default power is off. + ec200x->sleep_status = RT_FALSE;//default sleep is disabled. + + /* initialize AT client */ + at_client_init(ec200x->client_name, ec200x->recv_line_num); + + device->client = at_client_get(ec200x->client_name); + if (device->client == RT_NULL) + { + LOG_E("get AT client(%s) failed.", ec200x->client_name); + return -RT_ERROR; + } + + /* register URC data execution function */ +#ifdef AT_USING_SOCKET + ec200x_socket_init(device); +#endif + + /* add ec200x device to the netdev list */ + device->netdev = ec200x_netdev_add(ec200x->device_name); + if (device->netdev == RT_NULL) + { + LOG_E("add netdev(%s) failed.", ec200x->device_name); + return -RT_ERROR; + } + + /* initialize ec200x pin configuration */ + if (ec200x->power_pin != -1) + { + rt_pin_write(ec200x->power_pin, PIN_LOW); + rt_pin_mode(ec200x->power_pin, PIN_MODE_OUTPUT); + } + if (ec200x->power_status_pin != -1) + { + rt_pin_mode(ec200x->power_status_pin, PIN_MODE_INPUT); + } + if (ec200x->wakeup_pin != -1) + { + rt_pin_write(ec200x->wakeup_pin, PIN_LOW); + rt_pin_mode(ec200x->wakeup_pin, PIN_MODE_OUTPUT); + } + + /* initialize ec200x device network */ + return ec200x_netdev_set_up(device->netdev); +} + +static int ec200x_deinit(struct at_device *device) +{ + RT_ASSERT(device); + + return ec200x_netdev_set_down(device->netdev); +} + +static int ec200x_control(struct at_device *device, int cmd, void *arg) +{ + int result = -RT_ERROR; + + RT_ASSERT(device); + + switch (cmd) + { + case AT_DEVICE_CTRL_SLEEP: + result = ec200x_sleep(device); + break; + case AT_DEVICE_CTRL_WAKEUP: + result = ec200x_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 ec200x_device_ops = +{ + ec200x_init, + ec200x_deinit, + ec200x_control, +}; + +static int ec200x_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 ec200x device class object */ +#ifdef AT_USING_SOCKET + ec200x_socket_class_register(class); +#endif + class->device_ops = &ec200x_device_ops; + + return at_device_class_register(class, AT_DEVICE_CLASS_EC200X); +} +INIT_DEVICE_EXPORT(ec200x_device_class_register); + +#endif /* AT_DEVICE_USING_EC200X */ + diff --git a/class/ec200x/at_device_ec200x.h b/class/ec200x/at_device_ec200x.h new file mode 100644 index 0000000..4bd5a3d --- /dev/null +++ b/class/ec200x/at_device_ec200x.h @@ -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 + +#include + +/* 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__ */ + diff --git a/class/ec200x/at_socket_ec200x.c b/class/ec200x/at_socket_ec200x.c new file mode 100644 index 0000000..c48098f --- /dev/null +++ b/class/ec200x/at_socket_ec200x.c @@ -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 +#include + +#include + +#define LOG_TAG "at.skt.ec200x" +#include + +#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; iclient, 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 */ + diff --git a/class/esp32/at_device_esp32.c b/class/esp32/at_device_esp32.c new file mode 100644 index 0000000..f0a23f2 --- /dev/null +++ b/class/esp32/at_device_esp32.c @@ -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 +#include + +#include + +#define LOG_TAG "at.dev.esp32" + +#include + +#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=[,,]" 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=[,,]" 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=," 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=" 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=" 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 */ diff --git a/class/esp32/at_device_esp32.h b/class/esp32/at_device_esp32.h new file mode 100644 index 0000000..0791826 --- /dev/null +++ b/class/esp32/at_device_esp32.h @@ -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 + +#include + +/* 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__ */ diff --git a/class/esp32/at_socket_esp32.c b/class/esp32/at_socket_esp32.c new file mode 100644 index 0000000..16daad3 --- /dev/null +++ b/class/esp32/at_socket_esp32.c @@ -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 +#include + +#include + +#define LOG_TAG "at.skt.esp32" +#include + +#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 */ diff --git a/class/esp8266/at_device_esp8266.c b/class/esp8266/at_device_esp8266.c index 29e09d0..0db5504 100644 --- a/class/esp8266/at_device_esp8266.c +++ b/class/esp8266/at_device_esp8266.c @@ -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; diff --git a/class/m26/at_device_m26.c b/class/m26/at_device_m26.c index 6a4cf45..fb489cf 100644 --- a/class/m26/at_device_m26.c +++ b/class/m26/at_device_m26.c @@ -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); - m26_netdev_check_link_status(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); } diff --git a/class/m26/at_socket_m26.c b/class/m26/at_socket_m26.c index 89e5013..75906f6 100644 --- a/class/m26/at_socket_m26.c +++ b/class/m26/at_socket_m26.c @@ -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; } diff --git a/class/m6315/at_device_m6315.c b/class/m6315/at_device_m6315.c new file mode 100644 index 0000000..69f3770 --- /dev/null +++ b/class/m6315/at_device_m6315.c @@ -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 +#include +#include + +#include + +#define LOG_TAG "at.dev.m6315" +#include + +#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=[,]" 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=""[,[][,]]" 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," */ + 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 */ diff --git a/class/m6315/at_device_m6315.h b/class/m6315/at_device_m6315.h new file mode 100644 index 0000000..660a0a2 --- /dev/null +++ b/class/m6315/at_device_m6315.h @@ -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 + +#include + +/* 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__ */ diff --git a/class/m6315/at_socket_m6315.c b/class/m6315/at_socket_m6315.c new file mode 100644 index 0000000..af460cc --- /dev/null +++ b/class/m6315/at_socket_m6315.c @@ -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 +#include + +#include + +#define LOG_TAG "at.skt.m6315" +#include + +#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 */ diff --git a/class/me3616/at_device_me3616.c b/class/me3616/at_device_me3616.c new file mode 100644 index 0000000..d74c74e --- /dev/null +++ b/class/me3616/at_device_me3616.c @@ -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 +#include + +#include + +#define LOG_TAG "at.dev.me3616" +#include + +#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," */ + 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=[-l/L ] [-n/N ][-w/W