| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179 |
- /*
- * Copyright (c) 2006-2023, RT-Thread Development Team
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Change Logs:
- * Date Author Notes
- * 2022-12-16 Jonas first version
- */
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- #include <at_device_ml307.h>
- //#define AT_DEBUG
- #define LOG_TAG "at.dev.ml307"
- #include <at_log.h>
- #ifdef AT_DEVICE_USING_ML307
- #define ML307_AT_DEFAULT_TIMEOUT 1000
- #define ML307_WAIT_CONNECT_TIME 5000
- #define ML307_THREAD_STACK_SIZE 2048
- #define ML307_POWER_OFF RT_FALSE
- #define ML307_POWER_ON RT_TRUE
- #define ML307_POWER_ON_TIME 3
- #define ML307_POWER_OFF_TIME 4
- #define ML307_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 rt_bool_t ml307_get_power_state(struct at_device *device)
- {
- struct at_device_ml307 *ml307 = RT_NULL;
- ml307 = (struct at_device_ml307 *) device->user_data;
- return (!rt_pin_read(ml307->power_status_pin)) ? ML307_POWER_ON : ML307_POWER_OFF;
- }
- static void ml307_power_on(struct at_device *device)
- {
- struct at_device_ml307 *ml307 = RT_NULL;
- ml307 = (struct at_device_ml307 *) device->user_data;
- /* not nead to set pin configuration for ml307 device power on */
- if (ml307->power_pin == -1 || ml307->power_status_pin == -1)
- {
- return;
- }
- if(ml307_get_power_state(device) == ML307_POWER_ON)
- {
- return;
- }
- rt_pin_write(ml307->power_pin, PIN_HIGH);
- rt_thread_mdelay(ML307_POWER_ON_TIME * RT_TICK_PER_SECOND);
- rt_pin_write(ml307->power_pin, PIN_LOW);
- }
- static void ml307_power_off(struct at_device *device)
- {
- struct at_device_ml307 *ml307 = RT_NULL;
- ml307 = (struct at_device_ml307 *) device->user_data;
- do{
- /* not nead to set pin configuration for ml307 device power on */
- if (ml307->power_pin == -1 || ml307->power_status_pin == -1)
- {
- return;
- }
- if(ml307_get_power_state(device) == ML307_POWER_OFF)
- {
- return;
- }
- rt_pin_write(ml307->power_pin, PIN_HIGH);
- rt_thread_mdelay(ML307_POWER_OFF_TIME * RT_TICK_PER_SECOND);
- rt_pin_write(ml307->power_pin, PIN_LOW);
- rt_thread_mdelay(100);
- }while(ml307_get_power_state(device) != ML307_POWER_OFF);
- }
- static int ml307_check_link_status(struct at_device *device)
- {
- at_response_t resp = RT_NULL;
- struct at_device_ml307 *ml307 = RT_NULL;
- int result = -RT_ERROR;
- int link_stat = 0;
- RT_ASSERT(device);
- ml307 = (struct at_device_ml307 *)device->user_data;
- resp = at_create_resp(96, 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+MIPCALL?") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %d,%*d,%*s", &link_stat) > 0)
- {
- if (link_stat == 1)
- {
- result = RT_EOK;
- }
- }
- if(at_obj_exec_cmd(device->client, resp, "AT+CGACT?") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- //+CGACT: 1,1
- if (at_resp_parse_line_args_by_kw(resp, "+CGACT:", "+CGACT: %d,%*d", &link_stat) > 0)
- {
- result = link_stat;
- }
- __exit:
- if(resp)
- {
- at_delete_resp(resp);
- }
- return(result);
- }
- /* ============================= ml307 network interface operations ============================= */
- /* set ml307 network interface device status and address information */
- static int ml307_netdev_set_info(struct netdev *netdev)
- {
- #define ML307_IMEI_RESP_SIZE 32
- #define ML307_IPADDR_RESP_SIZE 64
- #define ML307_DNS_RESP_SIZE 96
- #define ML307_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(ML307_IMEI_RESP_SIZE, 0, ML307_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 ML307_NETDEV_HWADDR_LEN 8
- #define ML307_IMEI_LEN 15
- char imei[ML307_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=1") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- if(at_resp_parse_line_args_by_kw(resp, "+GSN:", "+GSN:%s", imei) <= 0)
- {
- LOG_E("ml307 device(%s) prase \"AT+GSN\" commands resposne data error.", device->name);
- result = -RT_ERROR;
- goto __exit;
- }
- LOG_D("ml307 device(%s) IMEI number: %s", device->name, imei);
- netdev->hwaddr_len = ML307_NETDEV_HWADDR_LEN;
- /* get hardware address by IMEI */
- for (i = 0, j = 0; i < ML307_NETDEV_HWADDR_LEN && j < ML307_IMEI_LEN; i++, j += 2)
- {
- if (j != ML307_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, ML307_IPADDR_RESP_SIZE*2, 2, ML307_INFO_RESP_TIMO);
- /* send "AT+CIFSR" commond to get IP address */
- if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*d,\"%[^\"]", ipaddr) <= 0)
- {
- LOG_E("ml307 device(%s) prase \"AT+CGPADDR=1\" commands resposne data error!", device->name);
- result = -RT_ERROR;
- goto __exit;
- }
- LOG_D("ml307 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};
- char dns_str[DNS_ADDR_SIZE_MAX*3] = {0};
- at_resp_set_info(resp, ML307_DNS_RESP_SIZE, 0, ML307_INFO_RESP_TIMO);
- /* send "AT+MDNSCFG=\"priority\",0" commond to set resolve IPV4 address priority */
- if (at_obj_exec_cmd(device->client, resp, "AT+MDNSCFG=\"priority\",0") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- /* send "AT+MDNSCFG=\"ip\"" commond to get DNS servers address */
- if (at_obj_exec_cmd(device->client, resp, "AT+MDNSCFG=\"ip\"") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- //+MDNSCFG: "ip","183.230.126.224",,"183.230.126.225"
- if (at_resp_parse_line_args_by_kw(resp, "+MDNSCFG:", "+MDNSCFG: \"ip\",%s", dns_str) < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- const char *dns1_str = strstr(dns_str, "\"");
- rt_sscanf(dns1_str, "\"%[^\"]", dns_server1);
- const char *dns2_str = strstr(dns_str, "\",\"");
- rt_sscanf(dns2_str, "\",\"%[^\"]", dns_server2);
- LOG_D("ml307 device(%s) primary DNS server address: %s", device->name, dns_server1);
- LOG_D("ml307 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 ML307_LINK_STATUS_OK 1
- #define ML307_LINK_RESP_SIZE 128
- #define ML307_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
- #define ML307_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(ML307_LINK_RESP_SIZE, 0, ML307_LINK_RESP_TIMO);
- if (resp == RT_NULL)
- {
- LOG_E("no memory for response create.");
- return;
- }
- while(1)
- {
- link_status = ml307_check_link_status(device);
- if(link_status < 0)
- {
- rt_thread_mdelay(ML307_LINK_DELAY_TIME);
- continue;
- }
- /* check the network interface device link status */
- if ((ML307_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
- {
- netdev_low_level_set_link_status(netdev, (ML307_LINK_STATUS_OK == link_status));
- }
- rt_thread_mdelay(ML307_LINK_DELAY_TIME);
- }
- }
- static int ml307_netdev_check_link_status(struct netdev *netdev)
- {
- #define ML307_LINK_THREAD_TICK 20
- #define ML307_LINK_THREAD_STACK_SIZE (1024 + 512)
- #define ML307_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,
- ML307_LINK_THREAD_STACK_SIZE, ML307_LINK_THREAD_PRIORITY, ML307_LINK_THREAD_TICK);
- if (tid)
- {
- rt_thread_startup(tid);
- }
- return RT_EOK;
- }
- static int ml307_net_init(struct at_device *device);
- static int ml307_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 ml307 device by netdev name(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- if (device->is_init == RT_FALSE)
- {
- device->is_init = RT_TRUE;
- ml307_net_init(device);
- netdev_low_level_set_status(netdev, RT_TRUE);
- LOG_D("the network interface device(%s) set up status.", netdev->name);
- }
- return RT_EOK;
- }
- static int ml307_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 ml307 device by netdev name(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- if (device->is_init == RT_TRUE)
- {
- ml307_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 ml307_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
- {
- #define ML307_DNS_RESP_LEN 8
- #define ML307_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 ml307 device by netdev name(%s) failed.", netdev->name);
- return -RT_ERROR;
- }
- resp = at_create_resp(ML307_DNS_RESP_LEN, 0, ML307_DNS_RESP_TIMEO);
- if (resp == RT_NULL)
- {
- LOG_D("ml307 set dns server failed, no memory for response object.");
- result = -RT_ENOMEM;
- goto __exit;
- }
- /* send "AT+CDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
- if (at_obj_exec_cmd(device->client, resp, "AT+MDNSCFG=\"%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 ml307_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 ml307 device(%s) response structure.", device->name);
- return -RT_ENOMEM;
- }
- if (at_obj_exec_cmd(device->client, resp, "AT+CGACT?") < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- if (at_obj_exec_cmd(device->client, resp, "AT+MDNSGIP=\"%s\"", name) < 0)
- {
- result = -RT_ERROR;
- goto __exit;
- }
- if (at_resp_parse_line_args_by_kw(resp, "+MDNSGIP:", "%*[^,],\"%[^\"]", 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_ML307
- static int ml307_netdev_ping(struct netdev *netdev, const char *host,
- size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
- #if RT_VER_NUM >= 0x50100
- , rt_bool_t is_bind
- #endif
- )
- {
- #define ML307_PING_RESP_SIZE 128
- #define ML307_PING_IP_SIZE 16
- #define ML307_PING_TIMEO (5 * RT_TICK_PER_SECOND)
- int result = -RT_ERROR;
- int response, time, ttl, bytes;
- char ip_addr[ML307_PING_IP_SIZE] = {0};
- char ip_addr_resp[ML307_PING_IP_SIZE + 8] = {0};
- at_response_t resp = RT_NULL;
- struct at_device *device = RT_NULL;
- #if RT_VER_NUM >= 0x50100
- RT_UNUSED(is_bind);
- #endif
- 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(ML307_PING_RESP_SIZE, 4, ML307_PING_TIMEO);
- if (resp == RT_NULL)
- {
- LOG_E("no memory for resp create.");
- result = -RT_ERROR;
- goto __exit;
- }
- /* send "AT+MPING="<host>"[,[<timeout>][,<pingnum>]]" timeout:1-255 second, pingnum:1-10, commond to send ping request */
- if(ml307_ping_domain_resolve(device, host, ip_addr) == RT_EOK)
- {
- at_obj_exec_cmd(device->client, resp, "AT+MPING=\"%s\", 10, 1", ip_addr);
- }
- else
- {
- at_obj_exec_cmd(device->client, resp, "AT+MPING=\"%s\", 10, 1", host);
- }
- rt_sscanf(at_resp_get_line_by_kw(resp, "+MPING:"), "+MPING:%d,%*s", &response);
- switch (response)
- {
- case 0:
- if (at_resp_parse_line_args(resp, 4, "+MPING: %d, %[^,], %d, %d, %d",
- &response, ip_addr_resp, &bytes, &time, &ttl) != RT_NULL)
- {
- rt_sscanf(ip_addr_resp, "%[^\"]", ip_addr);
- /* ping result reponse at the sixth line */
- // if (at_resp_parse_line_args(resp, 12, "+MPING: %*[^,], %d, %d, %d, %d, %d",
- // &sent, &lost, &min, &max, &avg) != RT_NULL)
- // {
- //
- // }
- 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 DNS resolution failed.", device->name);
- break;
- case 2:
- LOG_E("%s device DNS resolution timeout.", device->name);
- break;
- case 3:
- LOG_E("%s device Response error.", device->name);
- break;
- case 4:
- LOG_E("%s device Response timeout.", device->name);
- break;
- case 5:
- LOG_E("%s device Other errors.", device->name);
- break;
- default:
- break;
- }
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- return result;
- }
- #endif
- #endif /* NETDEV_USING_PING */
- #ifdef NETDEV_USING_NETSTAT
- void ml307_netdev_netstat(struct netdev *netdev)
- {
- #define ML307_NETSTAT_RESP_SIZE 320
- #define ML307_NETSTAT_TYPE_SIZE 4
- #define ML307_NETSTAT_IPADDR_SIZE 17
- #define ML307_NETSTAT_EXPRESSION "+MIPSTATE:%*d,\"%[^\"]\",\"%[^\"]\",%d,%d"
- at_response_t resp = RT_NULL;
- struct at_device *device = RT_NULL;
- int remote_port = 0, link_sta, 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, ML307_NETSTAT_TYPE_SIZE);
- ipaddr = (char *) rt_calloc(1, ML307_NETSTAT_IPADDR_SIZE);
- if ((type && ipaddr) == RT_NULL)
- {
- LOG_E("no memory for ipaddr create.");
- goto __exit;
- }
- resp = at_create_resp(ML307_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+MIPSTATE?" and wait response */
- if (at_obj_exec_cmd(device->client, resp, "AT+MIPSTATE?") < 0)
- {
- goto __exit;
- }
- for (i = 1; i <= resp->line_counts; i++)
- {
- if (strstr(at_resp_get_line(resp, i), "+MIPSTATE:"))
- {
- /* parse the third line of response data, get the network connection information */
- if (at_resp_parse_line_args(resp, i, ML307_NETSTAT_EXPRESSION, type, ipaddr, &remote_port, &link_sta) < 0)
- {
- goto __exit;
- }
- else
- {
- /* link_sta==2?"LINK_INTNET_UP":"LINK_INTNET_DOWN" */
- LOG_RAW("%s: %s ==> %s:%d\n", type, inet_ntoa(netdev->ip_addr), ipaddr, remote_port);
- }
- }
- }
- __exit:
- if (resp)
- {
- at_delete_resp(resp);
- }
- if (type)
- {
- rt_free(type);
- }
- if (ipaddr)
- {
- rt_free(ipaddr);
- }
- }
- #endif /* NETDEV_USING_NETSTAT */
- const struct netdev_ops ml307_netdev_ops =
- {
- ml307_netdev_set_up,
- ml307_netdev_set_down,
- RT_NULL, /* not support set ip, netmask, gatway address */
- ml307_netdev_set_dns_server,
- RT_NULL, /* not support set DHCP status */
- #ifdef NETDEV_USING_PING
- ml307_netdev_ping,
- #endif
- #ifdef NETDEV_USING_NETSTAT
- ml307_netdev_netstat,
- #endif
- };
- static struct netdev *ml307_netdev_add(const char *netdev_name)
- {
- #define ML307_NETDEV_MTU 1500
- struct netdev *netdev = RT_NULL;
- RT_ASSERT(netdev_name);
- 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 ml307 device(%s) netdev structure.", netdev_name);
- return RT_NULL;
- }
- netdev->mtu = ML307_NETDEV_MTU;
- netdev->ops = &ml307_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;
- }
- /* ============================= ml307 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 ml307 */
- static void ml307_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 IPADDR_RETRY 10
- #define CGATT_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 ml307 device(%s) response structure.", device->name);
- return;
- }
- LOG_D("start initializing the device(%s)", device->name);
- ml307_power_off(device);
- while (retry_num--)
- {
- rt_memset(parsed_data, 0, sizeof(parsed_data));
- rt_thread_mdelay(500);
- ml307_power_on(device);
- rt_thread_mdelay(1000);
- /* wait ml307 startup finish */
- if (at_client_obj_wait_connect(client, ML307_WAIT_CONNECT_TIME))
- {
- result = -RT_ETIMEOUT;
- goto __exit;
- }
- /* disable echo */
- AT_SEND_CMD(client, resp, 0, ML307_AT_DEFAULT_TIMEOUT, "ATE0");
- /* get module version */
- AT_SEND_CMD(client, resp, 4, 2*ML307_AT_DEFAULT_TIMEOUT, "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_I("ml307 device(%s) SIM card detection success.", device->name);
- break;
- }
- rt_thread_mdelay(500);
- }
- if (i == CPIN_RETRY)
- {
- LOG_E("ml307 device(%s) SIM card detection failed.", device->name);
- result = -RT_ERROR;
- goto __exit;
- }
- /* check SIM card */
- for (i = 0; i < CPIN_RETRY; i++)
- {
- AT_SEND_CMD(client, resp, 2, 10 * 1000, "AT+ICCID");
- if (at_resp_get_line_by_kw(resp, "+ICCID:"))
- {
- 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;
- }
- /* check signal strength */
- for (i = 0; i < CSQ_RETRY; i++)
- {
- AT_SEND_CMD(client, resp, 0, 3*ML307_AT_DEFAULT_TIMEOUT, "AT+CSQ");
- at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
- if (rt_strncmp(parsed_data, "99,99", sizeof(parsed_data)))
- {
- LOG_D("signal strength: %s", parsed_data);
- break;
- }
- rt_thread_mdelay(2000);
- }
- if(i == CSQ_RETRY)
- {
- LOG_E("%s device signal strength check failed(%s).", device->name, parsed_data);
- result = -RT_ERROR;
- goto __exit;
- }
- /* check the GSM network is registered */
- // for (i = 0; i < CREG_RETRY; i++)
- // {
- // AT_SEND_CMD(client, resp, 0, ML307_AT_DEFAULT_TIMEOUT, "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_D("ml307 device(%s) GSM network is registered(%s),", device->name, parsed_data);
- // break;
- // }
- // if(!strncmp(parsed_data, "0,3", sizeof(parsed_data)))
- // {
- // LOG_E("%s device GSM network is register failed(%s).", device->name, parsed_data);
- // result = -RT_ERROR;
- // goto __exit;
- // }
- // rt_thread_mdelay(1000 + 500 * (i + 1));
- // }
- // if (i == CREG_RETRY)
- // {
- // LOG_E("%s device GSM network 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, ML307_AT_DEFAULT_TIMEOUT, "AT+CGATT?");
- // at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT: %s", &parsed_data);
- // if (!rt_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(client, resp, "AT+CGDCONT=1,\"IPV4V6\",\"cmnet\"") == RT_EOK)
- // {
- // LOG_D("%s device Define PDP Context Success.", device->name);
- // break;
- // }
- // rt_thread_mdelay(100);
- // }
- // 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(client, resp, "AT+MIPCALL=1,1") == RT_EOK)
- // {
- // LOG_D("%s device PDP Context Activate Success.", device->name);
- // break;
- // }
- // rt_thread_mdelay(500);
- // }
- // if (i == COMMON_RETRY)
- // {
- // LOG_E("%s device PDP Context Activate failed.", device->name);
- // result = -RT_ERROR;
- // goto __exit;
- // }
- #if defined (AT_DEBUG)
- /* check the GPRS network IP address */
- for (i = 0; i < IPADDR_RETRY; i++)
- {
- if (at_obj_exec_cmd(client, resp, "AT+MIPCALL?") == RT_EOK)
- {
- #define IP_ADDR_SIZE_MAX 16
- char ipaddr_str[8*IP_ADDR_SIZE_MAX] = {0};
- char ipaddr_v4[IP_ADDR_SIZE_MAX] = {0};
- char ipaddr_v6[4*IP_ADDR_SIZE_MAX] = {0};
- /* parse response data "+CGPADDR: 1,<IP_address>" */
- if (at_resp_parse_line_args_by_kw(resp, "+MIPCALL:", "+MIPCALL: %*d,%*d,%s", ipaddr_str) > 0)
- {
- const char *ipaddr_v4_str = strstr(ipaddr_str, "\"");
- rt_sscanf(ipaddr_v4_str, "\"%[^\"]", ipaddr_v4);
- const char *ipaddr_v6_str = strstr(ipaddr_str, "\",\"");
- rt_sscanf(ipaddr_v6_str, "\",\"%[^\"]", ipaddr_v6);
- LOG_D("%s device IP address: %s - %s", device->name, ipaddr_v4, ipaddr_v6);
- 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;
- }
- #endif
- result = RT_EOK;
- __exit:
- if (result == RT_EOK)
- {
- break;
- }
- else
- {
- /* power off the ml307 device */
- ml307_power_off(device);
- rt_thread_mdelay(1000);
- LOG_I("ml307 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 */
- ml307_netdev_set_info(device->netdev);
- /* */
- ml307_netdev_check_link_status(device->netdev);
- /* */
- LOG_I("ml307 device(%s) network initialize success!", device->name);
- }
- else
- {
- device->is_init = RT_FALSE;
- netdev_low_level_set_status(device->netdev, RT_FALSE);
- LOG_E("ml307 device(%s) network initialize failed(%d)!", device->name, result);
- }
- }
- static int ml307_net_init(struct at_device *device)
- {
- #ifdef AT_DEVICE_ML307_INIT_ASYN
- rt_thread_t tid;
- tid = rt_thread_create("ml307_net_init", ml307_init_thread_entry, (void *)device,
- ML307_THREAD_STACK_SIZE, ML307_THREAD_PRIORITY, 20);
- if (tid)
- {
- rt_thread_startup(tid);
- }
- else
- {
- LOG_E("create ml307 device(%s) initialization thread failed.", device->name);
- return -RT_ERROR;
- }
- #else
- ml307_init_thread_entry(device);
- #endif /* AT_DEVICE_ML307_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);
- }
- /* ml307 device URC table for the device control */
- static const struct at_urc urc_table[] =
- {
- {"READY", "\r\n", urc_func},
- };
- static int ml307_init(struct at_device *device)
- {
- struct at_device_ml307 *ml307 = (struct at_device_ml307 *) device->user_data;
- struct serial_configure serial_config = RT_SERIAL_CONFIG_DEFAULT;
- rt_device_t serial = rt_device_find(ml307->client_name);
- if (serial == RT_NULL)
- {
- LOG_E("ml307 device(%s) initialize failed, get AT client(%s) failed.", ml307->device_name, ml307->client_name);
- return -RT_ERROR;
- }
- serial_config.bufsz = ml307->recv_buff_size;
- rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &serial_config);
- /* initialize AT client */
- #if RT_VER_NUM >= 0x50100
- at_client_init(ml307->client_name, ml307->recv_buff_size, ml307->recv_buff_size);
- #else
- at_client_init(ml307->client_name, ml307->recv_buff_size);
- #endif
- device->client = at_client_get(ml307->client_name);
- if (device->client == RT_NULL)
- {
- LOG_E("ml307 device(%s) initialize failed, get AT client(%s) failed.", ml307->device_name, ml307->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
- ml307_socket_init(device);
- #endif
- /* add ml307 device to the netdev list */
- device->netdev = ml307_netdev_add(ml307->device_name);
- if (device->netdev == RT_NULL)
- {
- LOG_E("ml307 device(%s) initialize failed, get network interface device failed.", ml307->device_name);
- return -RT_ERROR;
- }
- /* initialize ml307 pin configuration */
- if (ml307->power_pin != -1)
- {
- rt_pin_mode(ml307->power_pin, PIN_MODE_OUTPUT);
- }
- if (ml307->power_status_pin != -1)
- {
- rt_pin_mode(ml307->power_status_pin, PIN_MODE_INPUT_PULLUP);
- }
- /* initialize ml307 device network */
- return ml307_netdev_set_up(device->netdev);
- }
- static int ml307_deinit(struct at_device *device)
- {
- return ml307_netdev_set_down(device->netdev);
- }
- static int ml307_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("ml307 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 ml307_device_ops =
- {
- ml307_init,
- ml307_deinit,
- ml307_control,
- };
- static int ml307_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 ml307 device class create.");
- return -RT_ENOMEM;
- }
- /* fill ml307 device class object */
- #ifdef AT_USING_SOCKET
- ml307_socket_class_register(class);
- #endif
- class->device_ops = &ml307_device_ops;
- return at_device_class_register(class, AT_DEVICE_CLASS_ML307);
- }
- INIT_DEVICE_EXPORT(ml307_device_class_register);
- #endif /* AT_DEVICE_USING_ML307 */
|