diff --git a/SConscript b/SConscript index 30bcf98..8bcf013 100644 --- a/SConscript +++ b/SConscript @@ -76,6 +76,15 @@ 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') + group = DefineGroup('at_device', src, depend = ['PKG_USING_AT_DEVICE'], CPPPATH = path) Return('group') diff --git a/class/w60x/at_device_w60x.c b/class/w60x/at_device_w60x.c new file mode 100644 index 0000000..a4b3b5b --- /dev/null +++ b/class/w60x/at_device_w60x.c @@ -0,0 +1,955 @@ +/* + * File : at_device_w60x.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Change Logs: + * Date Author Notes + * 2018-06-20 chenyong first version + * 2019-05-09 chenyong multi AT socket client support + */ + +#include +#include + +#include + +#define LOG_TAG "at.dev.w60x" + +#include + +#ifdef AT_DEVICE_USING_W60X + +#define W60X_WAIT_CONNECT_TIME 5000 +#define W60X_THREAD_STACK_SIZE 2048 +#define W60X_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2) + +/* ============================= w60x network interface operations ============================= */ + +static int w60x_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server); + +static rt_bool_t w60x_is_join_start = RT_FALSE; + +static void w60x_get_netdev_info(struct rt_work *work, void *work_data) +{ +#define AT_ADDR_LEN 32 +#define AT_ERR_DNS_SERVER "0.0.0.0" +#define AT_DEF_DNS_SERVER "114.114.114.114" + + at_response_t resp = RT_NULL; + char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0}; + char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0}; + char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0}; + const char *resp_expr1 = "+OK=%s"; + const char *resp_expr2 = "+OK=%d,\"%[^\"]\",\"%[^\"]\",\"%[^\"]\",\"%[^\"]\",\"%[^\"]\""; + const char *resp_expr3 = "+OK=%d,%[^,],%[^,],%[^,],%[^,]"; + rt_int32_t link_status = 0; + ip_addr_t ip_addr; + rt_uint32_t mac_addr[6] = {0}; + rt_uint32_t num = 0; + rt_int32_t dhcp_stat = 0; + struct rt_delayed_work *delay_work = (struct rt_delayed_work *)work; + struct at_device *device = (struct at_device *)work_data; + struct netdev *netdev = device->netdev; + struct at_client *client = device->client; + char *pos; + + if (delay_work) + { + rt_free(delay_work); + } + + resp = at_create_resp(512, 1, rt_tick_from_millisecond(3000)); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return; + } + + at_obj_set_end_sign(device->client, '\r'); + + /* send mac addr query commond "AT+QMAC" and wait response */ + if (at_obj_exec_cmd(client, resp, "AT+QMAC") < 0) + { + goto __exit; + } + + pos = rt_strstr(resp->buf, "+OK="); + if (!pos) + { + LOG_E("%s device parse \"AT+QMAC\" cmd error.", device->name); + goto __exit; + } + + sscanf(pos, resp_expr1, mac); + + /* send addr info query commond "AT+CIPSTA?" and wait response */ + if (at_obj_exec_cmd(client, resp, "AT+LKSTT") < 0) + { + LOG_E("%s device send \"AT+LKSTT\" cmd error.", device->name); + goto __exit; + } + + pos = rt_strstr(resp->buf, "+OK="); + if (!pos) + { + LOG_E("%s device prase \"AT+LKSTT\" cmd error.", device->name); + goto __exit; + } + + sscanf(pos, resp_expr2, &link_status, ip, netmask, gateway, dns_server1, dns_server2); + + /* set netdev info */ + inet_aton(gateway, &ip_addr); + netdev_low_level_set_gw(netdev, &ip_addr); + inet_aton(netmask, &ip_addr); + netdev_low_level_set_netmask(netdev, &ip_addr); + inet_aton(ip, &ip_addr); + netdev_low_level_set_ipaddr(netdev, &ip_addr); + sscanf(mac, "%02x%02x%02x%02x%02x%02x", + &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]); + for (num = 0; num < netdev->hwaddr_len; num++) + { + netdev->hwaddr[num] = mac_addr[num]; + } + + /* set primary DNS server address */ + if (rt_strlen(dns_server1) > 0 && + rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0) + { + inet_aton(dns_server1, &ip_addr); + netdev_low_level_set_dns_server(netdev, 0, &ip_addr); + } + else + { + inet_aton(AT_DEF_DNS_SERVER, &ip_addr); + w60x_netdev_set_dns_server(netdev, 0, &ip_addr); + } + + /* set secondary DNS server address */ + if (rt_strlen(dns_server2) > 0 ) + { + inet_aton(dns_server2, &ip_addr); + netdev_low_level_set_dns_server(netdev, 1, &ip_addr); + } + + /* send DHCP query commond " AT+NIP" and wait response */ + if (at_obj_exec_cmd(client, resp, "AT+NIP") < 0) + { + goto __exit; + } + + pos = rt_strstr(resp->buf, "+OK="); + + /* parse response data, get the DHCP status */ + if (!pos) + { + LOG_E("%s device prase DHCP status error.", device->name); + goto __exit; + } + + sscanf(pos, resp_expr3, &dhcp_stat, ip, netmask, gateway, dns_server1); + + /* 0 - DHCP, 1 - static ip */ + netdev_low_level_set_dhcp_status(netdev, dhcp_stat ? RT_FALSE : RT_TRUE); + +__exit: + if (resp) + { + at_delete_resp(resp); + } +} + +static int w60x_net_init(struct at_device *device); + +static int w60x_netdev_set_up(struct netdev *netdev) +{ + struct at_device *device = RT_NULL; + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get device(%s) failed.", netdev->name); + return -RT_ERROR; + } + + if (device->is_init == RT_FALSE) + { + w60x_net_init(device); + netdev_low_level_set_status(netdev, RT_TRUE); + LOG_D("network interface device(%s) set up status", netdev->name); + } + + return RT_EOK; +} + +static int w60x_netdev_set_down(struct netdev *netdev) +{ + struct at_device *device = RT_NULL; + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get device by netdev(%s) failed.", netdev->name); + return -RT_ERROR; + } + + if (device->is_init == RT_TRUE) + { + device->is_init = RT_FALSE; + netdev_low_level_set_status(netdev, RT_FALSE); + LOG_D("network interface device(%s) set down status", netdev->name); + } + + return RT_EOK; +} + +static int w60x_netdev_set_addr_info(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw) +{ +#define IPADDR_SIZE 16 + + int result = RT_EOK; + at_response_t resp = RT_NULL; + struct at_device *device = RT_NULL; + char ip_str[IPADDR_SIZE] = {0}; + char gw_str[IPADDR_SIZE] = {0}; + char netmask_str[IPADDR_SIZE] = {0}; + char dns_str[IPADDR_SIZE] = {0}; + rt_uint8_t dhcp_stat = 0; + const char *resp_expr = "+OK=%d,%s,%s,%s,%s"; + + RT_ASSERT(netdev); + RT_ASSERT(ip_addr || netmask || gw); + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get device(%s) failed.", netdev->name); + return -RT_ERROR; + } + + resp = at_create_resp(128, 1, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + result = -RT_ENOMEM; + goto __exit; + } + + /* send DHCP query commond " AT+NIP" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+NIP") < 0) + { + goto __exit; + } + + if (at_resp_parse_line_args(resp, 1, resp_expr, dhcp_stat, ip_str, netmask_str, gw_str, dns_str) <= 0) + { + LOG_E("%s device prase DHCP status error.", device->name); + goto __exit; + } + + /* Convert numeric IP address into decimal dotted ASCII representation. */ + if (ip_addr) + rt_memcpy(ip_str, inet_ntoa(*ip_addr), IPADDR_SIZE); + else + rt_memcpy(ip_str, inet_ntoa(netdev->ip_addr), IPADDR_SIZE); + + if (gw) + rt_memcpy(gw_str, inet_ntoa(*gw), IPADDR_SIZE); + else + rt_memcpy(gw_str, inet_ntoa(netdev->gw), IPADDR_SIZE); + + if (netmask) + rt_memcpy(netmask_str, inet_ntoa(*netmask), IPADDR_SIZE); + else + rt_memcpy(netmask_str, inet_ntoa(netdev->netmask), IPADDR_SIZE); + + /* send addr info set commond "AT+NIP=dhcptype,ip,gateway,netmask,dnsserver" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+NIP=%hhu,%s,%s,%s,%s", + dhcp_stat, ip_str, gw_str, netmask_str, dns_str) < 0) + { + LOG_E("%s device set address failed.", device->name); + result = -RT_ERROR; + } + else + { + /* Update netdev information */ + if (ip_addr) + netdev_low_level_set_ipaddr(netdev, ip_addr); + + if (gw) + netdev_low_level_set_gw(netdev, gw); + + if (netmask) + netdev_low_level_set_netmask(netdev, netmask); + + LOG_D("%s device set address success.", device->name); + } + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} + +static int w60x_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server) +{ + int result = RT_EOK; + at_response_t resp = RT_NULL; + struct at_device *device = RT_NULL; + char ip_str[IPADDR_SIZE] = {0}; + char gw_str[IPADDR_SIZE] = {0}; + char netmask_str[IPADDR_SIZE] = {0}; + char dns_str[IPADDR_SIZE] = {0}; + rt_uint8_t dhcp_stat = 0; + const char *resp_expr = "+OK=%d,%s,%s,%s,%s"; + + RT_ASSERT(netdev); + RT_ASSERT(dns_server); + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get device by netdev(%s) failed.", netdev->name); + return -RT_ERROR; + } + + resp = at_create_resp(128, 1, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return -RT_ENOMEM; + } + + /* send DHCP query commond " AT+NIP" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+NIP") < 0) + { + goto __exit; + } + + if (at_resp_parse_line_args(resp, 1, resp_expr, dhcp_stat, ip_str, netmask_str, gw_str, dns_str) <= 0) + { + LOG_E("%s device prase DHCP status error.", device->name); + goto __exit; + } + + if (dns_server) + rt_memcpy(dns_str, inet_ntoa(*dns_server), IPADDR_SIZE); + else + rt_memcpy(dns_str, inet_ntoa(netdev->dns_servers), IPADDR_SIZE); + + /* send addr info set commond "AT+NIP=dhcptype,ip,gateway,netmask,dnsserver" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+NIP=%hhu,%s,%s,%s,%s", + dhcp_stat, ip_str, gw_str, netmask_str, dns_str) < 0) + { + LOG_E("%s device set DNS failed.", device->name); + result = -RT_ERROR; + } + else + { + netdev_low_level_set_dns_server(netdev, dns_num, dns_server); + LOG_D("%s device set DNS(%s) success.", device->name, inet_ntoa(*dns_server)); + } + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} + +static int w60x_netdev_set_dhcp(struct netdev *netdev, rt_bool_t is_enabled) +{ + int result = RT_EOK; + at_response_t resp = RT_NULL; + struct at_device *device = RT_NULL; + char ip_str[IPADDR_SIZE] = {0}; + char gw_str[IPADDR_SIZE] = {0}; + char netmask_str[IPADDR_SIZE] = {0}; + char dns_str[IPADDR_SIZE] = {0}; + rt_uint8_t dhcp_stat = 0; + const char *resp_expr = "+OK=%d,%s,%s,%s,%s"; + + RT_ASSERT(netdev); + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get device by netdev(%s) failed.", netdev->name); + return -RT_ERROR; + } + + resp = at_create_resp(128, 1, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_E("no memory for resp struct."); + return -RT_ENOMEM; + } + + /* send DHCP query commond " AT+NIP" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+NIP") < 0) + { + goto __exit; + } + + if (at_resp_parse_line_args(resp, 1, resp_expr, dhcp_stat, ip_str, netmask_str, gw_str, dns_str) <= 0) + { + LOG_E("%s device prase DHCP status error.", device->name); + goto __exit; + } + + /* send addr info set commond "AT+NIP=dhcptype,ip,gateway,netmask,dnsserver" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+NIP=%hhu,%s,%s,%s,%s", + is_enabled ? 0 : 1, ip_str, gw_str, netmask_str, dns_str) < 0) + { + LOG_E("%s device set DHCP status(%d) failed.", device->name, is_enabled); + result = -RT_ERROR; + goto __exit; + } + else + { + netdev_low_level_set_dhcp_status(netdev, is_enabled); + LOG_D("%s device set DHCP status(%d) ok.", device->name, is_enabled); + } + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} + +#ifdef NETDEV_USING_PING +static int w60x_netdev_ping(struct netdev *netdev, const char *host, + size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp) +{ +#define W60X_IP_SIZE 16 + + rt_err_t result = RT_EOK; + at_response_t resp = RT_NULL; + struct at_device *device = RT_NULL; + char ip_addr[W60X_IP_SIZE] = {0}; + char *pos; + + RT_ASSERT(netdev); + RT_ASSERT(host); + RT_ASSERT(ping_resp); + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get device(%s) failed.", netdev->name); + return -RT_ERROR; + } + + resp = at_create_resp(256, 1, timeout); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return -RT_ENOMEM; + } + + at_obj_set_end_sign(device->client, '\r'); + + /* send domain commond "AT+SKGHBN=" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+SKGHBN=%s", host) < 0) + { + LOG_E("AT+SKGHBN failed."); + result = -RT_ERROR; + goto __exit; + } + + /* parse the third line of response data, get the IP address */ + pos = rt_strstr(resp->buf, "+OK="); + if (!pos) + { + LOG_E("AT+SKGHBN resp error."); + result = -RT_ERROR; + goto __exit; + } + + sscanf(pos, "+OK=\"%[^\"]\"", ip_addr); + + inet_aton(ip_addr, &(ping_resp->ip_addr)); + ping_resp->data_len = data_len; + ping_resp->ttl = 0; + ping_resp->ticks = 0; + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} +#endif /* NETDEV_USING_PING */ + +#ifdef NETDEV_USING_NETSTAT +void w60x_netdev_netstat(struct netdev *netdev) +{ + at_response_t resp = RT_NULL; + struct at_device *device = RT_NULL; + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name); + if (device == RT_NULL) + { + LOG_E("get device(%s) failed.", netdev->name); + return; + } + + resp = at_create_resp(128, 1, 5 * RT_TICK_PER_SECOND); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create.", device->name); + goto __exit; + } + + /* send network connection information commond "AT+LKSTT" and wait response */ + if (at_obj_exec_cmd(device->client, resp, "AT+LKSTT") < 0) + { + goto __exit; + } + + LOG_RAW("%s\n", at_resp_get_line(resp, 1)); + + /* maybe query "AT+SKSTT"... */ + +__exit: + if (resp) + { + at_delete_resp(resp); + } +} +#endif /* NETDEV_USING_NETSTAT */ + +static const struct netdev_ops w60x_netdev_ops = +{ + w60x_netdev_set_up, + w60x_netdev_set_down, + + w60x_netdev_set_addr_info, + w60x_netdev_set_dns_server, + w60x_netdev_set_dhcp, + +#ifdef NETDEV_USING_PING + w60x_netdev_ping, +#endif +#ifdef NETDEV_USING_NETSTAT + w60x_netdev_netstat, +#endif +}; + +static struct netdev *w60x_netdev_add(const char *netdev_name) +{ +#define ETHERNET_MTU 1500 +#define HWADDR_LEN 6 + struct netdev *netdev = RT_NULL; + + RT_ASSERT(netdev_name); + + netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev)); + if (netdev == RT_NULL) + { + LOG_E("no memory for netdev create."); + return RT_NULL; + } + + netdev->mtu = ETHERNET_MTU; + netdev->ops = &w60x_netdev_ops; + netdev->hwaddr_len = HWADDR_LEN; + +#ifdef SAL_USING_AT + extern int sal_at_netdev_set_pf_info(struct netdev *netdev); + /* set the network interface socket/netdb operations */ + sal_at_netdev_set_pf_info(netdev); +#endif + + netdev_register(netdev, netdev_name, RT_NULL); + + return netdev; +} + +/* ============================= w60x device operations ============================= */ + +#define AT_SEND_CMD(client, resp, cmd) \ + do { \ + (resp) = at_resp_set_info((resp), 256, 1, 3 * RT_TICK_PER_SECOND); \ + if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \ + { \ + result = -RT_ERROR; \ + goto __exit; \ + } \ + } while(0) \ + +static void w60x_netdev_start_delay_work(struct at_device *device) +{ + struct rt_delayed_work *net_work = RT_NULL; + net_work = (struct rt_delayed_work *)rt_calloc(1, sizeof(struct rt_delayed_work)); + if (net_work == RT_NULL) + { + return; + } + + rt_delayed_work_init(net_work, w60x_get_netdev_info, (void *)device); + rt_work_submit(&(net_work->work), RT_TICK_PER_SECOND); +} + +static void w60x_init_thread_entry(void *parameter) +{ +#define INIT_RETRY 5 + + struct at_device *device = (struct at_device *) parameter; + struct at_device_w60x *w60x = (struct at_device_w60x *) device->user_data; + struct at_client *client = device->client; + at_response_t resp = RT_NULL; + rt_err_t result = RT_EOK; + rt_size_t i = 0, retry_num = INIT_RETRY; + rt_bool_t wifi_is_conn = RT_FALSE; + rt_uint8_t atcmd_str[128]; + + LOG_D("%s device initialize start.", device->name); + + /* wait w60x device startup finish */ + rt_thread_mdelay(1000); + + resp = at_create_resp(256, 1, 3 * RT_TICK_PER_SECOND); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return; + } + + while (retry_num--) + { + /* reset module */ + AT_SEND_CMD(client, resp, "AT+Z"); + /* reset waiting delay */ + rt_thread_mdelay(1000); + /* get module version */ + AT_SEND_CMD(client, resp, "AT+QVER"); + /* show module version */ + for (i = 0; i < resp->line_counts - 1; i++) + { + LOG_D("%s", at_resp_get_line(resp, i + 1)); + } + + /* initialize successfully */ + result = RT_EOK; + break; + + __exit: + if (result != RT_EOK) + { + rt_thread_mdelay(1000); + LOG_I("%s device initialize retry...", device->name); + } + } + + /* set current mode to Wi-Fi station */ + rt_sprintf(atcmd_str, "AT+WPRT=%d", 0); + AT_SEND_CMD(client, resp, atcmd_str); + rt_sprintf(atcmd_str, "AT+SSID=\"%s\"", w60x->wifi_ssid); + AT_SEND_CMD(client, resp, atcmd_str); + rt_sprintf(atcmd_str, "AT+KEY=1,0,\"%s\"", w60x->wifi_password); + AT_SEND_CMD(client, resp, atcmd_str); + AT_SEND_CMD(client, resp, "AT+PMTF"); + + /* connect to WiFi AP */ + w60x_is_join_start = RT_TRUE; + if (at_obj_exec_cmd(client, at_resp_set_info(resp, 128, 1, 20 * RT_TICK_PER_SECOND), + "AT+WJOIN") != RT_EOK) + { + LOG_W("%s device wifi connect failed, check ssid(%s) and password(%s).", + device->name, w60x->wifi_ssid, w60x->wifi_password); + w60x_is_join_start = RT_FALSE; + } + else + { + wifi_is_conn = RT_TRUE; + } + + if (resp) + { + at_delete_resp(resp); + } + + if (result != RT_EOK) + { + netdev_low_level_set_status(device->netdev, RT_FALSE); + LOG_E("%s device network initialize failed(%d).", device->name, result); + } + else + { + device->is_init = RT_TRUE; + netdev_low_level_set_status(device->netdev, RT_TRUE); + if (wifi_is_conn) + { + netdev_low_level_set_link_status(device->netdev, RT_TRUE); + } + w60x_netdev_start_delay_work(device); + LOG_I("%s device network initialize successfully.", device->name); + } +} + +static int w60x_net_init(struct at_device *device) +{ +#ifdef AT_DEVICE_W60X_INIT_ASYN + rt_thread_t tid; + + tid = rt_thread_create("w60x_net", w60x_init_thread_entry, (void *) device, + W60X_THREAD_STACK_SIZE, W60X_THREAD_PRIORITY, 20); + if (tid) + { + rt_thread_startup(tid); + } + else + { + LOG_E("create %s device init thread failed.", device->name); + return -RT_ERROR; + } +#else + w60x_init_thread_entry(device); +#endif /* AT_DEVICE_W60X_INIT_ASYN */ + + return RT_EOK; +} + +static void urc_func(struct at_client *client, const char *data, rt_size_t size) +{ + struct at_device *device = RT_NULL; + char *client_name = client->device->parent.name; + + RT_ASSERT(client && data && size); + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name); + if (device == RT_NULL) + { + LOG_E("get device(%s) failed.", client_name); + return; + } + + if (w60x_is_join_start && !rt_strncmp(data, "+OK=", rt_strlen("+OK="))) + { + w60x_is_join_start = RT_FALSE; + + LOG_I("%s device wifi is connected.", device->name); + + if (device->is_init) + { + netdev_low_level_set_link_status(device->netdev, RT_TRUE); + + w60x_netdev_start_delay_work(device); + } + } + else if (w60x_is_join_start && !rt_strncmp(data, "+ERR=", rt_strlen("+ERR="))) + { + w60x_is_join_start = RT_FALSE; + + LOG_I("%s device wifi is disconnect.", device->name); + + if (device->is_init) + { + netdev_low_level_set_link_status(device->netdev, RT_FALSE); + } + } +} + +static const struct at_urc urc_table[] = +{ + {"+OK=", "\r\n", urc_func}, + {"+ERR=", "\r\n", urc_func}, +}; + +static int w60x_init(struct at_device *device) +{ + struct at_device_w60x *w60x = (struct at_device_w60x *) device->user_data; + + /* initialize AT client */ + at_client_init(w60x->client_name, w60x->recv_line_num); + + device->client = at_client_get(w60x->client_name); + if (device->client == RT_NULL) + { + LOG_E("get AT client(%s) failed.", w60x->client_name); + return -RT_ERROR; + } + + /* register URC data execution function */ + at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0])); + +#ifdef AT_USING_SOCKET + w60x_socket_init(device); +#endif + + /* add w60x device to the netdev list */ + device->netdev = w60x_netdev_add(w60x->device_name); + if (device->netdev == RT_NULL) + { + LOG_E("add netdev(%s) failed.", w60x->device_name); + return -RT_ERROR; + } + + /* initialize w60x device network */ + return w60x_netdev_set_up(device->netdev); +} + +static int w60x_deinit(struct at_device *device) +{ + return w60x_netdev_set_down(device->netdev); +} + +/* reset eap8266 device and initialize device network again */ +static int w60x_reset(struct at_device *device) +{ + int result = RT_EOK; + struct at_client *client = device->client; + + /* send "AT+Z" commonds to w60x device */ + result = at_obj_exec_cmd(client, RT_NULL, "AT+Z"); + rt_thread_mdelay(1000); + + /* waiting 10 seconds for w60x device reset */ + device->is_init = RT_FALSE; + rt_thread_mdelay(1000); + + /* initialize w60x device network */ + w60x_net_init(device); + + device->is_init = RT_TRUE; + + return result; +} + +/* change eap8266 wifi ssid and password information */ +static int w60x_wifi_info_set(struct at_device *device, struct at_device_ssid_pwd *info) +{ + int result = RT_EOK; + struct at_response *resp = RT_NULL; + rt_uint8_t atcmd_str[128]; + + if (info->ssid == RT_NULL || info->password == RT_NULL) + { + LOG_E("input wifi ssid(%s) and password(%s) error.", info->ssid, info->password); + return -RT_ERROR; + } + + resp = at_create_resp(128, 1, 20 * RT_TICK_PER_SECOND); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return -RT_ENOMEM; + } + + /* set current mode to Wi-Fi station */ + rt_sprintf(atcmd_str, "AT+WPRT=%d", 0); + AT_SEND_CMD(device->client, resp, atcmd_str); + rt_sprintf(atcmd_str, "AT+SSID=\"%s\"", info->ssid); + AT_SEND_CMD(device->client, resp, atcmd_str); + rt_sprintf(atcmd_str, "AT+KEY=1,0,\"%s\"", info->password); + AT_SEND_CMD(device->client, resp, atcmd_str); + AT_SEND_CMD(device->client, resp, "AT+PMTF"); + + /* connect to input wifi ap */ + w60x_is_join_start = RT_TRUE; + if (at_obj_exec_cmd(device->client, resp, "AT+WJOIN") != RT_EOK) + { + LOG_E("%s device wifi connect failed, check ssid(%s) and password(%s).", + device->name, info->ssid, info->password); + result = -RT_ERROR; + w60x_is_join_start = RT_FALSE; + } + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} + +static int w60x_control(struct at_device *device, int cmd, void *arg) +{ + int result = -RT_ERROR; + + RT_ASSERT(device); + + switch (cmd) + { + case AT_DEVICE_CTRL_POWER_ON: + case AT_DEVICE_CTRL_POWER_OFF: + case AT_DEVICE_CTRL_LOW_POWER: + case AT_DEVICE_CTRL_SLEEP: + case AT_DEVICE_CTRL_WAKEUP: + case AT_DEVICE_CTRL_NET_CONN: + case AT_DEVICE_CTRL_NET_DISCONN: + case AT_DEVICE_CTRL_GET_SIGNAL: + case AT_DEVICE_CTRL_GET_GPS: + case AT_DEVICE_CTRL_GET_VER: + LOG_W("not support the control cmd(%d).", cmd); + break; + case AT_DEVICE_CTRL_RESET: + result = w60x_reset(device); + break; + case AT_DEVICE_CTRL_SET_WIFI_INFO: + result = w60x_wifi_info_set(device, (struct at_device_ssid_pwd *) arg); + break; + default: + LOG_E("input error control cmd(%d).", cmd); + break; + } + + return result; +} + +static const struct at_device_ops w60x_device_ops = +{ + w60x_init, + w60x_deinit, + w60x_control, +}; + +static int w60x_device_class_register(void) +{ + struct at_device_class *class = RT_NULL; + + class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class)); + if (class == RT_NULL) + { + LOG_E("no memory for class create."); + return -RT_ENOMEM; + } + + /* fill W60X device class object */ +#ifdef AT_USING_SOCKET + w60x_socket_class_register(class); +#endif + class->device_ops = &w60x_device_ops; + + return at_device_class_register(class, AT_DEVICE_CLASS_W60X); +} +INIT_DEVICE_EXPORT(w60x_device_class_register); + +#endif /* AT_DEVICE_USING_W60X */ diff --git a/class/w60x/at_device_w60x.h b/class/w60x/at_device_w60x.h new file mode 100644 index 0000000..4b90248 --- /dev/null +++ b/class/w60x/at_device_w60x.h @@ -0,0 +1,66 @@ +/* + * File : at_device_w60x.h + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Change Logs: + * Date Author Notes + * 2019-05-16 chenyong first version + */ + +#ifndef __AT_DEVICE_W60X_H__ +#define __AT_DEVICE_W60X_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +#include + +/* The maximum number of sockets supported by the w60x device */ +#define AT_DEVICE_W60X_SOCKETS_NUM 20 + +struct at_device_w60x +{ + char *device_name; + char *client_name; + + char *wifi_ssid; + char *wifi_password; + size_t recv_line_num; + struct at_device device; + + void *user_data; +}; + +#ifdef AT_USING_SOCKET + +/* w60x device socket initialize */ +int w60x_socket_init(struct at_device *device); + +/* w60x device class socket register */ +int w60x_socket_class_register(struct at_device_class *class); + +#endif /* AT_USING_SOCKET */ + +#ifdef __cplusplus +} +#endif + +#endif /* __AT_DEVICE_W60X_H__ */ diff --git a/class/w60x/at_socket_w60x.c b/class/w60x/at_socket_w60x.c new file mode 100644 index 0000000..5aa543d --- /dev/null +++ b/class/w60x/at_socket_w60x.c @@ -0,0 +1,469 @@ +/* + * File : at_socket_w60x.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Change Logs: + * Date Author Notes + * 2018-06-20 chenyong first version + * 2019-05-09 chenyong multi AT socket client support + */ + +#include +#include + +#include + +#define LOG_TAG "at.skt.w60x" +#include + +#if defined(AT_DEVICE_USING_W60X) && defined(AT_USING_SOCKET) + +#define W60X_MODULE_SEND_MAX_SIZE 512 + +static rt_int32_t w60x_socket_fd[AT_DEVICE_W60X_SOCKETS_NUM] = {-1}; + +static at_evt_cb_t at_evt_cb_set[] = { + [AT_SOCKET_EVT_RECV] = NULL, + [AT_SOCKET_EVT_CLOSED] = NULL, +}; + +/** + * close socket by AT commands. + * + * @param current socket + * + * @return 0: close socket success + * -1: send AT commands error + * -2: wait socket event timeout + * -5: no memory + */ +static int w60x_socket_close(struct at_socket *socket) +{ + int result = RT_EOK; + at_response_t resp = RT_NULL; + int device_socket = (int) socket->user_data; + struct at_device *device = (struct at_device *) socket->device; + + resp = at_create_resp(64, 1, rt_tick_from_millisecond(300)); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return -RT_ENOMEM; + } + + at_obj_set_end_sign(device->client, '\r'); + + result = at_obj_exec_cmd(device->client, resp, "AT+SKCLS=%d", w60x_socket_fd[device_socket]); + w60x_socket_fd[device_socket] = -1; + + if (resp) + { + at_delete_resp(resp); + } + + return result; +} + +/** + * create TCP/UDP client or server connect by AT commands. + * + * @param socket current socket + * @param ip server or client IP address + * @param port server or client port + * @param type connect socket type(tcp, udp) + * @param is_client connection is client + * + * @return 0: connect success + * -1: connect failed, send commands error or type error + * -2: wait socket event timeout + * -5: no memory + */ +static int w60x_socket_connect(struct at_socket *socket, char *ip, int32_t port, enum at_socket_type type, rt_bool_t is_client) +{ + int result = RT_EOK; + at_response_t resp = RT_NULL; + int device_socket = (int) socket->user_data; + struct at_device *device = (struct at_device *) socket->device; + int socket_fd = -1; + + RT_ASSERT(ip); + RT_ASSERT(port >= 0); + + resp = at_create_resp(64, 1, 5 * RT_TICK_PER_SECOND); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return -RT_ENOMEM; + } + + at_obj_set_end_sign(device->client, '\r'); + + /* send the "AT+SKRPTM" commands */ + if (at_obj_exec_cmd(device->client, resp, "AT+SKRPTM=1") < 0) + { + result = -RT_ERROR; + goto __exit; + } + + switch (type) + { + case AT_SOCKET_TCP: + /* send AT commands */ + if (at_obj_exec_cmd(device->client, resp, + "AT+SKCT=0,%d,%s,%d", is_client ? 0 : 1, is_client ? ip : 0, port) < 0) + { + result = -RT_ERROR; + } + break; + + case AT_SOCKET_UDP: + if (at_obj_exec_cmd(device->client, resp, + "AT+SKCT=1,%d,%s,%d", is_client ? 0 : 1, is_client ? ip : 0, port) < 0) + { + result = -RT_ERROR; + } + break; + + default: + LOG_E("not supported connect type %d.", type); + result = -RT_ERROR; + goto __exit; + } + + if ((result != RT_EOK) || !rt_strstr(at_resp_get_line(resp, 1), "+OK=")) + { + LOG_D("%s device socket connect failed.", device->name); + goto __exit; + } + + socket_fd = atoi(rt_strstr(at_resp_get_line(resp, 1), "+OK=") + rt_strlen("+OK=")); + w60x_socket_fd[device_socket] = socket_fd; + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; +} + +/** + * send data to server or client by AT commands. + * + * @param socket current socket + * @param buff send buffer + * @param bfsz send buffer size + * @param type connect socket type(tcp, udp) + * + * @return >=0: the size of send success + * -1: send AT commands error or send data error + * -2: waited socket event timeout + * -5: no memory + */ +static int w60x_socket_send(struct at_socket *socket, const char *buff, size_t bfsz, enum at_socket_type type) +{ + int result = RT_EOK; + size_t cur_pkt_size = 0, sent_size = 0; + at_response_t resp = RT_NULL; + int device_socket = (int) socket->user_data; + struct at_device *device = (struct at_device *) socket->device; + struct at_device_w60x *w60x = (struct at_device_w60x *) device->user_data; + rt_mutex_t lock = device->client->lock; + + RT_ASSERT(buff); + RT_ASSERT(bfsz > 0); + + resp = at_create_resp(128, 1, 5 * RT_TICK_PER_SECOND); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return -RT_ENOMEM; + } + + rt_mutex_take(lock, RT_WAITING_FOREVER); + + /* set current socket for send URC event */ + w60x->user_data = (void *) device_socket; + + /* set AT client end sign to deal with '\n' sign */ + at_obj_set_end_sign(device->client, '\n'); + + while (sent_size < bfsz) + { + if (bfsz - sent_size < W60X_MODULE_SEND_MAX_SIZE) + { + cur_pkt_size = bfsz - sent_size; + } + else + { + cur_pkt_size = W60X_MODULE_SEND_MAX_SIZE; + } + + /* send the "AT+SKSND" commands */ + if (at_obj_exec_cmd(device->client, resp, "AT+SKSND=%d,%d", w60x_socket_fd[device_socket], cur_pkt_size) < 0) + { + result = -RT_ERROR; + goto __exit; + } + + if (!rt_strstr(at_resp_get_line(resp, 1), "+OK=")) + { + result = -RT_ERROR; + goto __exit; + } + + /* send the real data to server or client */ + result = (int) at_client_obj_send(device->client, buff + sent_size, cur_pkt_size); + if (result == 0) + { + result = -RT_ERROR; + goto __exit; + } + + sent_size += cur_pkt_size; + } + +__exit: + /* reset the end sign for data */ + at_obj_set_end_sign(device->client, 0); + + rt_mutex_release(lock); + + if (resp) + { + at_delete_resp(resp); + } + + return result > 0 ? sent_size : result; +} + +/** + * domain resolve by AT commands. + * + * @param name domain name + * @param ip parsed IP address, it's length must be 16 + * + * @return 0: domain resolve success + * -2: wait socket event timeout + * -5: no memory + */ +static int w60x_domain_resolve(const char *name, char ip[16]) +{ +#define RESOLVE_RETRY 5 + + int i, result = RT_EOK; + char recv_ip[16] = { 0 }; + at_response_t resp = RT_NULL; + struct at_device *device = RT_NULL; + char *pos; + + RT_ASSERT(name); + RT_ASSERT(ip); + + device = at_device_get_first_initialized(); + if (device == RT_NULL) + { + LOG_E("get first init device failed."); + return -RT_ERROR; + } + + resp = at_create_resp(128, 1, 20 * RT_TICK_PER_SECOND); + if (resp == RT_NULL) + { + LOG_E("no memory for resp create."); + return -RT_ENOMEM; + } + + at_obj_set_end_sign(device->client, '\r'); + + for (i = 0; i < RESOLVE_RETRY; i++) + { + if (at_obj_exec_cmd(device->client, resp, "AT+SKGHBN=%s", name) < 0) + { + result = -RT_ERROR; + goto __exit; + } + + /* parse the third line of response data, get the IP address */ + pos = rt_strstr(resp->buf, "+OK="); + if (!pos) + { + rt_thread_mdelay(100); + /* resolve failed, maybe receive an URC CRLF */ + continue; + } + + sscanf(pos, "+OK=\"%[^\"]\"", recv_ip); + + if (rt_strlen(recv_ip) < 8) + { + rt_thread_mdelay(100); + /* resolve failed, maybe receive an URC CRLF */ + continue; + } + else + { + rt_strncpy(ip, recv_ip, 15); + ip[15] = '\0'; + break; + } + } + +__exit: + if (resp) + { + at_delete_resp(resp); + } + + return result; + +} + +/** + * set AT socket event notice callback + * + * @param event notice event + * @param cb notice callback + */ +static void w60x_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb) +{ + if (event < sizeof(at_evt_cb_set) / sizeof(at_evt_cb_set[1])) + { + at_evt_cb_set[event] = cb; + } +} + +static const struct at_socket_ops w60x_socket_ops = +{ + w60x_socket_connect, + w60x_socket_close, + w60x_socket_send, + w60x_domain_resolve, + w60x_socket_set_event_cb, +}; + +static void urc_recv_func(struct at_client *client, const char *data, rt_size_t size) +{ + int device_socket = 0; + rt_int32_t timeout = 0; + rt_size_t bfsz = 0, temp_size = 0; + char *recv_buf = RT_NULL, temp[8] = {0}; + struct at_socket *socket = RT_NULL; + struct at_device *device = RT_NULL; + char *client_name = client->device->parent.name; + char recv_ip[16] = { 0 }; + rt_int32_t recv_port = 0; + rt_uint8_t i; + char *pos; + + RT_ASSERT(data && size); + + device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name); + if (device == RT_NULL) + { + LOG_E("get device(%s) failed.", client_name); + return; + } + + /* get the at deveice socket and receive buffer size by receive data */ + pos = rt_strstr(data, "+SKTRPT="); + sscanf(pos, "+SKTRPT=%d,%d,%[^,],%d", &device_socket, (int *) &bfsz, recv_ip, &recv_port); + + for (i = 0; i < AT_DEVICE_W60X_SOCKETS_NUM; i++) + { + if (device_socket == w60x_socket_fd[i]) + { + device_socket = i; + break; + } + } + + /* set receive timeout by receive buffer length, not less than 10ms */ + timeout = bfsz > 10 ? bfsz : 10; + + if (device_socket < 0 || bfsz == 0) + return; + + recv_buf = (char *) rt_calloc(1, bfsz); + if (recv_buf == RT_NULL) + { + LOG_E("no memory receive buffer(%d).", bfsz); + /* read and clean the coming data */ + while (temp_size < bfsz) + { + if (bfsz - temp_size > sizeof(temp)) + { + at_client_obj_recv(client, temp, sizeof(temp), timeout); + } + else + { + at_client_obj_recv(client, temp, bfsz - temp_size, timeout); + } + temp_size += sizeof(temp); + } + return; + } + + /* sync receive data */ + if (at_client_obj_recv(client, recv_buf, bfsz, timeout) != bfsz) + { + LOG_E("%s device receive size(%d) data failed.", device->name, bfsz); + rt_free(recv_buf); + return; + } + + /* get at socket object by device socket descriptor */ + socket = &(device->sockets[device_socket]); + + /* notice the receive buffer and buffer size */ + if (at_evt_cb_set[AT_SOCKET_EVT_RECV]) + { + at_evt_cb_set[AT_SOCKET_EVT_RECV](socket, AT_SOCKET_EVT_RECV, recv_buf, bfsz); + } +} + +static const struct at_urc urc_table[] = +{ + {"+SKTRPT=", "\r", urc_recv_func}, + {"\r\n+SKTRPT=", "\r", urc_recv_func}, + {"\r+SKTRPT=", "\r", urc_recv_func}, + {"\n+SKTRPT=", "\r", urc_recv_func}, +}; + +int w60x_socket_init(struct at_device *device) +{ + RT_ASSERT(device); + + /* register URC data execution function */ + at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0])); + + return RT_EOK; +} + +int w60x_socket_class_register(struct at_device_class *class) +{ + RT_ASSERT(class); + + class->socket_num = AT_DEVICE_W60X_SOCKETS_NUM; + class->socket_ops = &w60x_socket_ops; + + return RT_EOK; +} + +#endif /* AT_DEVICE_USING_W60X && AT_USING_SOCKET */ diff --git a/inc/at_device.h b/inc/at_device.h index 6a5bc7f..ea0cc69 100644 --- a/inc/at_device.h +++ b/inc/at_device.h @@ -52,6 +52,7 @@ extern "C" { #define AT_DEVICE_CLASS_RW007 0x06U #define AT_DEVICE_CLASS_MW31 0x07U #define AT_DEVICE_CLASS_ESP32 0x08U +#define AT_DEVICE_CLASS_W60X 0x09U /* Options and Commands for AT device control opreations */ #define AT_DEVICE_CTRL_POWER_ON 0x01L diff --git a/samples/at_sample_w60x.c b/samples/at_sample_w60x.c new file mode 100644 index 0000000..7a66330 --- /dev/null +++ b/samples/at_sample_w60x.c @@ -0,0 +1,52 @@ +/* + * File : at_sample_w60x.c + * This file is part of RT-Thread RTOS + * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Change Logs: + * Date Author Notes + * 2019-05-10 chenyong first version + */ + +#include + +#define LOG_TAG "at.sample.w60x" +#include + +#define W60X_SAMPLE_DEIVCE_NAME "w60x0" + +static struct at_device_w60x w60x0 = +{ + W60X_SAMPLE_DEIVCE_NAME, + W60X_SAMPLE_CLIENT_NAME, + + W60X_SAMPLE_WIFI_SSID, + W60X_SAMPLE_WIFI_PASSWORD, + W60X_SAMPLE_RECV_BUFF_LEN, +}; + +static int w60x_device_register(void) +{ + struct at_device_w60x *w60x = &w60x0; + + return at_device_register(&(w60x->device), + w60x->device_name, + w60x->client_name, + AT_DEVICE_CLASS_W60X, + (void *) w60x); +} +INIT_APP_EXPORT(w60x_device_register);