at_device_esp8266.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-06-20 chenyong first version
  9. * 2019-05-09 chenyong multi AT socket client support
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <at_device_esp8266.h>
  14. #define LOG_TAG "at.dev.esp"
  15. #include <at_log.h>
  16. #ifdef AT_DEVICE_USING_ESP8266
  17. #define ESP8266_WAIT_CONNECT_TIME 5000
  18. #define ESP8266_THREAD_STACK_SIZE 2048
  19. #define ESP8266_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX / 2)
  20. unsigned int ESP8266_GMR_AT_VERSION;
  21. /* ============================= esp8266 network interface operations ============================= */
  22. static int esp8266_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server);
  23. static void esp8266_get_netdev_info(struct rt_work *work, void *work_data)
  24. {
  25. #define AT_ADDR_LEN 32
  26. #define AT_ERR_DNS_SERVER "255.255.255.255"
  27. #define AT_DEF_DNS_SERVER "114.114.114.114"
  28. at_response_t resp = RT_NULL;
  29. char ip[AT_ADDR_LEN] = {0}, mac[AT_ADDR_LEN] = {0};
  30. char gateway[AT_ADDR_LEN] = {0}, netmask[AT_ADDR_LEN] = {0};
  31. char dns_server1[AT_ADDR_LEN] = {0}, dns_server2[AT_ADDR_LEN] = {0};
  32. const char *resp_expr = "%*[^\"]\"%[^\"]\"";
  33. ip_addr_t ip_addr;
  34. rt_uint32_t mac_addr[6] = {0};
  35. rt_uint32_t num = 0;
  36. rt_uint32_t dhcp_stat = 0;
  37. struct at_device *device = (struct at_device *)work_data;
  38. struct netdev *netdev = device->netdev;
  39. struct at_client *client = device->client;
  40. if (work != RT_NULL)
  41. {
  42. rt_free(work);
  43. }
  44. resp = at_create_resp(512, 0, rt_tick_from_millisecond(300));
  45. if (resp == RT_NULL)
  46. {
  47. LOG_E("no memory for resp create.");
  48. return;
  49. }
  50. /* send mac addr query commond "AT+CIFSR" and wait response */
  51. if (at_obj_exec_cmd(client, resp, "AT+CIFSR") < 0)
  52. {
  53. goto __exit;
  54. }
  55. if (at_resp_parse_line_args_by_kw(resp, "STAMAC", resp_expr, mac) <= 0)
  56. {
  57. LOG_E("%s device parse \"AT+CIFSR\" cmd error.", device->name);
  58. goto __exit;
  59. }
  60. /* send addr info query commond "AT+CIPSTA?" and wait response */
  61. if (at_obj_exec_cmd(client, resp, "AT+CIPSTA?") < 0)
  62. {
  63. LOG_E("%s device send \"AT+CIPSTA?\" cmd error.", device->name);
  64. goto __exit;
  65. }
  66. if (at_resp_parse_line_args_by_kw(resp, "ip", resp_expr, ip) <= 0 ||
  67. at_resp_parse_line_args_by_kw(resp, "gateway", resp_expr, gateway) <= 0 ||
  68. at_resp_parse_line_args_by_kw(resp, "netmask", resp_expr, netmask) <= 0)
  69. {
  70. LOG_E("%s device prase \"AT+CIPSTA?\" cmd error.", device->name);
  71. goto __exit;
  72. }
  73. /* set netdev info */
  74. inet_aton(gateway, &ip_addr);
  75. netdev_low_level_set_gw(netdev, &ip_addr);
  76. inet_aton(netmask, &ip_addr);
  77. netdev_low_level_set_netmask(netdev, &ip_addr);
  78. inet_aton(ip, &ip_addr);
  79. netdev_low_level_set_ipaddr(netdev, &ip_addr);
  80. rt_sscanf(mac, "%x:%x:%x:%x:%x:%x",
  81. &mac_addr[0], &mac_addr[1], &mac_addr[2], &mac_addr[3], &mac_addr[4], &mac_addr[5]);
  82. for (num = 0; num < netdev->hwaddr_len; num++)
  83. {
  84. netdev->hwaddr[num] = mac_addr[num];
  85. }
  86. /* send dns server query commond "AT+CIPDNS?" and wait response */
  87. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS?") < 0)
  88. {
  89. LOG_W("please check and update %s device firmware to support the \"AT+CIPDNS?\" cmd.", device->name);
  90. goto __exit;
  91. }
  92. /* +CIPDNS:0,"208.67.222.222","8.8.8.8" */
  93. if (at_resp_parse_line_args_by_kw(resp, "+CIPDNS:", "%*[^\"]\"%[^\"]\",\"%[^\"]\"", dns_server1, dns_server2) < 0)
  94. {
  95. LOG_E("%s device prase \"AT+CIPDNS?\" cmd error.", device->name);
  96. goto __exit;
  97. }
  98. /* set primary DNS server address */
  99. if (rt_strlen(dns_server1) > 0 &&
  100. rt_strncmp(dns_server1, AT_ERR_DNS_SERVER, rt_strlen(AT_ERR_DNS_SERVER)) != 0)
  101. {
  102. inet_aton(dns_server1, &ip_addr);
  103. netdev_low_level_set_dns_server(netdev, 0, &ip_addr);
  104. }
  105. else
  106. {
  107. inet_aton(AT_DEF_DNS_SERVER, &ip_addr);
  108. esp8266_netdev_set_dns_server(netdev, 0, &ip_addr);
  109. }
  110. /* set secondary DNS server address */
  111. if (rt_strlen(dns_server2) > 0 )
  112. {
  113. inet_aton(dns_server2, &ip_addr);
  114. netdev_low_level_set_dns_server(netdev, 1, &ip_addr);
  115. }
  116. /* send DHCP query commond " AT+CWDHCP" and wait response */
  117. if (at_obj_exec_cmd(client, resp, "AT+CWDHCP?") < 0)
  118. {
  119. goto __exit;
  120. }
  121. /* parse response data, get the DHCP status */
  122. if (at_resp_parse_line_args_by_kw(resp, "+CWDHCP:", "+CWDHCP:%d", &dhcp_stat) < 0)
  123. {
  124. LOG_E("%s device prase DHCP status error.", device->name);
  125. goto __exit;
  126. }
  127. /* Bit0 - Station DHCP status, Bit1 - SoftAP DHCP status */
  128. netdev_low_level_set_dhcp_status(netdev, dhcp_stat & 0x01 ? RT_TRUE : RT_FALSE);
  129. __exit:
  130. if (resp)
  131. {
  132. at_delete_resp(resp);
  133. }
  134. }
  135. static int esp8266_net_init(struct at_device *device);
  136. static int esp8266_netdev_set_up(struct netdev *netdev)
  137. {
  138. struct at_device *device = RT_NULL;
  139. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  140. if (device == RT_NULL)
  141. {
  142. LOG_E("get device(%s) failed.", netdev->name);
  143. return -RT_ERROR;
  144. }
  145. if (device->is_init == RT_FALSE)
  146. {
  147. esp8266_net_init(device);
  148. netdev_low_level_set_status(netdev, RT_TRUE);
  149. LOG_D("network interface device(%s) set up status", netdev->name);
  150. }
  151. return RT_EOK;
  152. }
  153. static int esp8266_netdev_set_down(struct netdev *netdev)
  154. {
  155. struct at_device *device = RT_NULL;
  156. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  157. if (device == RT_NULL)
  158. {
  159. LOG_E("get device by netdev(%s) failed.", netdev->name);
  160. return -RT_ERROR;
  161. }
  162. if (device->is_init == RT_TRUE)
  163. {
  164. device->is_init = RT_FALSE;
  165. netdev_low_level_set_status(netdev, RT_FALSE);
  166. LOG_D("network interface device(%s) set down status", netdev->name);
  167. }
  168. return RT_EOK;
  169. }
  170. static int esp8266_netdev_set_addr_info(struct netdev *netdev, ip_addr_t *ip_addr, ip_addr_t *netmask, ip_addr_t *gw)
  171. {
  172. #define IPADDR_RESP_SIZE 128
  173. #define IPADDR_SIZE 16
  174. int result = RT_EOK;
  175. at_response_t resp = RT_NULL;
  176. struct at_device *device = RT_NULL;
  177. char ip_str[IPADDR_SIZE] = {0};
  178. char gw_str[IPADDR_SIZE] = {0};
  179. char netmask_str[IPADDR_SIZE] = {0};
  180. RT_ASSERT(netdev);
  181. RT_ASSERT(ip_addr || netmask || gw);
  182. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  183. if (device == RT_NULL)
  184. {
  185. LOG_E("get device(%s) failed.", netdev->name);
  186. return -RT_ERROR;
  187. }
  188. resp = at_create_resp(IPADDR_RESP_SIZE, 0, rt_tick_from_millisecond(300));
  189. if (resp == RT_NULL)
  190. {
  191. LOG_E("no memory for resp create.");
  192. result = -RT_ENOMEM;
  193. goto __exit;
  194. }
  195. /* Convert numeric IP address into decimal dotted ASCII representation. */
  196. if (ip_addr)
  197. rt_memcpy(ip_str, inet_ntoa(*ip_addr), IPADDR_SIZE);
  198. else
  199. rt_memcpy(ip_str, inet_ntoa(netdev->ip_addr), IPADDR_SIZE);
  200. if (gw)
  201. rt_memcpy(gw_str, inet_ntoa(*gw), IPADDR_SIZE);
  202. else
  203. rt_memcpy(gw_str, inet_ntoa(netdev->gw), IPADDR_SIZE);
  204. if (netmask)
  205. rt_memcpy(netmask_str, inet_ntoa(*netmask), IPADDR_SIZE);
  206. else
  207. rt_memcpy(netmask_str, inet_ntoa(netdev->netmask), IPADDR_SIZE);
  208. /* send addr info set commond "AT+CIPSTA=<ip>[,<gateway>,<netmask>]" and wait response */
  209. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTA=\"%s\",\"%s\",\"%s\"",
  210. ip_str, gw_str, netmask_str) < 0)
  211. {
  212. LOG_E("%s device set address failed.", device->name);
  213. result = -RT_ERROR;
  214. }
  215. else
  216. {
  217. /* Update netdev information */
  218. if (ip_addr)
  219. netdev_low_level_set_ipaddr(netdev, ip_addr);
  220. if (gw)
  221. netdev_low_level_set_gw(netdev, gw);
  222. if (netmask)
  223. netdev_low_level_set_netmask(netdev, netmask);
  224. LOG_D("%s device set address success.", device->name);
  225. }
  226. __exit:
  227. if (resp)
  228. {
  229. at_delete_resp(resp);
  230. }
  231. return result;
  232. }
  233. static int esp8266_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  234. {
  235. #define DNS_RESP_SIZE 128
  236. int result = RT_EOK;
  237. at_response_t resp = RT_NULL;
  238. struct at_device *device = RT_NULL;
  239. RT_ASSERT(netdev);
  240. RT_ASSERT(dns_server);
  241. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  242. if (device == RT_NULL)
  243. {
  244. LOG_E("get device by netdev(%s) failed.", netdev->name);
  245. return -RT_ERROR;
  246. }
  247. resp = at_create_resp(DNS_RESP_SIZE, 0, rt_tick_from_millisecond(300));
  248. if (resp == RT_NULL)
  249. {
  250. LOG_E("no memory for resp create.");
  251. return -RT_ENOMEM;
  252. }
  253. /* send dns server set commond "AT+CIPDNS=<enable>[,<"DNS IP1">][,<"DNS IP2">][,<"DNS IP3">]" and wait response */
  254. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDNS=1,\"%s\"", inet_ntoa(*dns_server)) < 0)
  255. {
  256. LOG_E("%s device set DNS failed.", device->name);
  257. result = -RT_ERROR;
  258. }
  259. else
  260. {
  261. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  262. LOG_D("%s device set DNS(%s) success.", device->name, inet_ntoa(*dns_server));
  263. }
  264. if (resp)
  265. {
  266. at_delete_resp(resp);
  267. }
  268. return result;
  269. }
  270. static int esp8266_netdev_set_dhcp(struct netdev *netdev, rt_bool_t is_enabled)
  271. {
  272. #define ESP8266_STATION 1 << 0
  273. #define ESP8266_SOFTAP 1 << 1
  274. #define RESP_SIZE 128
  275. int result = RT_EOK;
  276. at_response_t resp = RT_NULL;
  277. struct at_device *device = RT_NULL;
  278. RT_ASSERT(netdev);
  279. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  280. if (device == RT_NULL)
  281. {
  282. LOG_E("get device by netdev(%s) failed.", netdev->name);
  283. return -RT_ERROR;
  284. }
  285. resp = at_create_resp(RESP_SIZE, 0, rt_tick_from_millisecond(300));
  286. if (resp == RT_NULL)
  287. {
  288. LOG_E("no memory for resp struct.");
  289. return -RT_ENOMEM;
  290. }
  291. /* send dhcp set commond "AT+CWDHCP=<operate>,<mode>" and wait response */
  292. if (at_obj_exec_cmd(device->client, resp, "AT+CWDHCP=%d,%d", is_enabled, ESP8266_STATION) < 0)
  293. {
  294. LOG_E("%s device set DHCP status(%d) failed.", device->name, is_enabled);
  295. result = -RT_ERROR;
  296. goto __exit;
  297. }
  298. else
  299. {
  300. netdev_low_level_set_dhcp_status(netdev, is_enabled);
  301. LOG_D("%s device set DHCP status(%d) ok.", device->name, is_enabled);
  302. }
  303. __exit:
  304. if (resp)
  305. {
  306. at_delete_resp(resp);
  307. }
  308. return result;
  309. }
  310. #ifdef NETDEV_USING_PING
  311. static int esp8266_netdev_ping(struct netdev *netdev, const char *host,
  312. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
  313. #if RT_VER_NUM >= 0x50100
  314. , rt_bool_t is_bind
  315. #endif
  316. )
  317. {
  318. #define ESP8266_PING_IP_SIZE 16
  319. rt_err_t result = RT_EOK;
  320. at_response_t resp = RT_NULL;
  321. struct at_device *device = RT_NULL;
  322. char ip_addr[ESP8266_PING_IP_SIZE] = {0};
  323. int req_time;
  324. #if RT_VER_NUM >= 0x50100
  325. RT_UNUSED(is_bind);
  326. #endif
  327. RT_ASSERT(netdev);
  328. RT_ASSERT(host);
  329. RT_ASSERT(ping_resp);
  330. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  331. if (device == RT_NULL)
  332. {
  333. LOG_E("get device(%s) failed.", netdev->name);
  334. return -RT_ERROR;
  335. }
  336. resp = at_create_resp(64, 0, timeout);
  337. if (resp == RT_NULL)
  338. {
  339. LOG_E("no memory for resp create.");
  340. return -RT_ENOMEM;
  341. }
  342. /* send domain commond "AT+CIPDOMAIN=<domain name>" and wait response */
  343. if (at_obj_exec_cmd(device->client, resp, "AT+CIPDOMAIN=\"%s\"", host) < 0)
  344. {
  345. result = -RT_ERROR;
  346. goto __exit;
  347. }
  348. /* parse the third line of response data, get the IP address */
  349. if (at_resp_parse_line_args_by_kw(resp, "+CIPDOMAIN:", (esp8266_get_at_version() <= ESP8266_DEFAULT_AT_VERSION_NUM) ? "+CIPDOMAIN:%s" : "+CIPDOMAIN:\"%[^\"]\"", ip_addr) < 0)
  350. {
  351. result = -RT_ERROR;
  352. goto __exit;
  353. }
  354. /* send ping commond "AT+PING=<IP>" and wait response */
  355. if (at_obj_exec_cmd(device->client, resp, "AT+PING=\"%s\"", host) < 0)
  356. {
  357. result = -RT_ERROR;
  358. goto __exit;
  359. }
  360. if (at_resp_parse_line_args_by_kw(resp, "+", (esp8266_get_at_version() <= ESP8266_DEFAULT_AT_VERSION_NUM) ? "+%d" : "+PING:%d", &req_time) < 0)
  361. {
  362. result = -RT_ERROR;
  363. goto __exit;
  364. }
  365. if (req_time)
  366. {
  367. inet_aton(ip_addr, &(ping_resp->ip_addr));
  368. ping_resp->data_len = data_len;
  369. ping_resp->ttl = 0;
  370. ping_resp->ticks = req_time;
  371. }
  372. __exit:
  373. if (resp)
  374. {
  375. at_delete_resp(resp);
  376. }
  377. return result;
  378. }
  379. #endif /* NETDEV_USING_PING */
  380. #ifdef NETDEV_USING_NETSTAT
  381. void esp8266_netdev_netstat(struct netdev *netdev)
  382. {
  383. #define ESP8266_NETSTAT_RESP_SIZE 320
  384. #define ESP8266_NETSTAT_TYPE_SIZE 4
  385. #define ESP8266_NETSTAT_IPADDR_SIZE 17
  386. #define ESP8266_NETSTAT_EXPRESSION "+CIPSTATUS:%*d,\"%[^\"]\",\"%[^\"]\",%d,%d,%*d"
  387. at_response_t resp = RT_NULL;
  388. struct at_device *device = RT_NULL;
  389. int remote_port, local_port, i;
  390. char *type = RT_NULL;
  391. char *ipaddr = RT_NULL;
  392. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  393. if (device == RT_NULL)
  394. {
  395. LOG_E("get device(%s) failed.", netdev->name);
  396. return;
  397. }
  398. type = (char *) rt_calloc(1, ESP8266_NETSTAT_TYPE_SIZE);
  399. ipaddr = (char *) rt_calloc(1, ESP8266_NETSTAT_IPADDR_SIZE);
  400. if ((type && ipaddr) == RT_NULL)
  401. {
  402. LOG_E("no memory for ipaddr create.");
  403. goto __exit;
  404. }
  405. resp = at_create_resp(ESP8266_NETSTAT_RESP_SIZE, 0, 5 * RT_TICK_PER_SECOND);
  406. if (resp == RT_NULL)
  407. {
  408. LOG_E("no memory for resp create.", device->name);
  409. goto __exit;
  410. }
  411. /* send network connection information commond "AT+CIPSTATUS" and wait response */
  412. if (at_obj_exec_cmd(device->client, resp, "AT+CIPSTATUS") < 0)
  413. {
  414. goto __exit;
  415. }
  416. for (i = 1; i <= resp->line_counts; i++)
  417. {
  418. if (strstr(at_resp_get_line(resp, i), "+CIPSTATUS"))
  419. {
  420. /* parse the third line of response data, get the network connection information */
  421. if (at_resp_parse_line_args(resp, i, ESP8266_NETSTAT_EXPRESSION, type, ipaddr, &remote_port, &local_port) < 0)
  422. {
  423. goto __exit;
  424. }
  425. else
  426. {
  427. LOG_RAW("%s: %s:%d ==> %s:%d\n", type, inet_ntoa(netdev->ip_addr), local_port, ipaddr, remote_port);
  428. }
  429. }
  430. }
  431. __exit:
  432. if (resp)
  433. {
  434. at_delete_resp(resp);
  435. }
  436. if (type)
  437. {
  438. rt_free(type);
  439. }
  440. if (ipaddr)
  441. {
  442. rt_free(ipaddr);
  443. }
  444. }
  445. #endif /* NETDEV_USING_NETSTAT */
  446. static const struct netdev_ops esp8266_netdev_ops =
  447. {
  448. esp8266_netdev_set_up,
  449. esp8266_netdev_set_down,
  450. esp8266_netdev_set_addr_info,
  451. esp8266_netdev_set_dns_server,
  452. esp8266_netdev_set_dhcp,
  453. #ifdef NETDEV_USING_PING
  454. esp8266_netdev_ping,
  455. #endif
  456. #ifdef NETDEV_USING_NETSTAT
  457. esp8266_netdev_netstat,
  458. #endif
  459. };
  460. static struct netdev *esp8266_netdev_add(const char *netdev_name)
  461. {
  462. #define ETHERNET_MTU 1500
  463. #define HWADDR_LEN 6
  464. struct netdev *netdev = RT_NULL;
  465. RT_ASSERT(netdev_name);
  466. netdev = netdev_get_by_name(netdev_name);
  467. if (netdev != RT_NULL)
  468. {
  469. return (netdev);
  470. }
  471. netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
  472. if (netdev == RT_NULL)
  473. {
  474. LOG_E("no memory for netdev create.");
  475. return RT_NULL;
  476. }
  477. netdev->mtu = ETHERNET_MTU;
  478. netdev->ops = &esp8266_netdev_ops;
  479. netdev->hwaddr_len = HWADDR_LEN;
  480. #ifdef SAL_USING_AT
  481. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  482. /* set the network interface socket/netdb operations */
  483. sal_at_netdev_set_pf_info(netdev);
  484. #endif
  485. netdev_register(netdev, netdev_name, RT_NULL);
  486. return netdev;
  487. }
  488. /* ============================= esp8266 device operations ============================= */
  489. #define AT_SEND_CMD(client, resp, cmd) \
  490. do { \
  491. (resp) = at_resp_set_info((resp), 256, 0, 5 * RT_TICK_PER_SECOND); \
  492. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  493. { \
  494. result = -RT_ERROR; \
  495. goto __exit; \
  496. } \
  497. } while(0) \
  498. static void esp8266_netdev_start_delay_work(struct at_device *device)
  499. {
  500. struct rt_work *net_work = RT_NULL;
  501. net_work = (struct rt_work *)rt_calloc(1, sizeof(struct rt_work));
  502. if (net_work == RT_NULL)
  503. {
  504. return;
  505. }
  506. rt_work_init(net_work, esp8266_get_netdev_info, (void *)device);
  507. rt_work_submit(net_work, RT_TICK_PER_SECOND);
  508. }
  509. static void esp8266_init_thread_entry(void *parameter)
  510. {
  511. #define INIT_RETRY 5
  512. struct at_device *device = (struct at_device *) parameter;
  513. struct at_device_esp8266 *esp8266 = (struct at_device_esp8266 *) device->user_data;
  514. struct at_client *client = device->client;
  515. at_response_t resp = RT_NULL;
  516. rt_err_t result = RT_EOK;
  517. rt_size_t i = 0, retry_num = INIT_RETRY;
  518. rt_bool_t wifi_is_conn = RT_FALSE;
  519. LOG_D("%s device initialize start.", device->name);
  520. /* wait esp8266 device startup finish */
  521. if (at_client_obj_wait_connect(client, ESP8266_WAIT_CONNECT_TIME))
  522. {
  523. return;
  524. }
  525. resp = at_create_resp(128, 0, 5 * RT_TICK_PER_SECOND);
  526. if (resp == RT_NULL)
  527. {
  528. LOG_E("no memory for resp create.");
  529. return;
  530. }
  531. while (retry_num--)
  532. {
  533. /* reset module */
  534. AT_SEND_CMD(client, resp, "AT+RST");
  535. /* reset waiting delay */
  536. rt_thread_mdelay(1000);
  537. /* disable echo */
  538. AT_SEND_CMD(client, resp, "ATE0");
  539. /* set current mode to Wi-Fi station */
  540. AT_SEND_CMD(client, resp, "AT+CWMODE=1");
  541. /* get module version */
  542. AT_SEND_CMD(client, resp, "AT+GMR");
  543. /* get ESP8266 AT version*/
  544. ESP8266_GMR_AT_VERSION = esp8266_at_version_to_hex(at_resp_get_line(resp, 1));
  545. /* show module version */
  546. for (i = 0; i < resp->line_counts - 1; i++)
  547. {
  548. LOG_D("%s", at_resp_get_line(resp, i + 1));
  549. }
  550. AT_SEND_CMD(client, resp, "AT+CIPMUX=1");
  551. /* initialize successfully */
  552. result = RT_EOK;
  553. break;
  554. __exit:
  555. if (result != RT_EOK)
  556. {
  557. rt_thread_mdelay(1000);
  558. LOG_I("%s device initialize retry...", device->name);
  559. }
  560. }
  561. /* connect to WiFi AP */
  562. if (at_obj_exec_cmd(client, at_resp_set_info(resp, 128, 0, 20 * RT_TICK_PER_SECOND),
  563. "AT+CWJAP=\"%s\",\"%s\"", esp8266->wifi_ssid, esp8266->wifi_password) != RT_EOK)
  564. {
  565. LOG_W("%s device wifi connect failed, check ssid(%s) and password(%s).",
  566. device->name, esp8266->wifi_ssid, esp8266->wifi_password);
  567. }
  568. else
  569. {
  570. wifi_is_conn = RT_TRUE;
  571. }
  572. if (resp)
  573. {
  574. at_delete_resp(resp);
  575. }
  576. if (result != RT_EOK)
  577. {
  578. netdev_low_level_set_status(device->netdev, RT_FALSE);
  579. LOG_E("%s device network initialize failed(%d).", device->name, result);
  580. }
  581. else
  582. {
  583. device->is_init = RT_TRUE;
  584. netdev_low_level_set_status(device->netdev, RT_TRUE);
  585. if (wifi_is_conn)
  586. {
  587. netdev_low_level_set_link_status(device->netdev, RT_TRUE);
  588. }
  589. esp8266_netdev_start_delay_work(device);
  590. LOG_I("%s device network initialize successfully.", device->name);
  591. }
  592. }
  593. static int esp8266_net_init(struct at_device *device)
  594. {
  595. #ifdef AT_DEVICE_ESP8266_INIT_ASYN
  596. rt_thread_t tid;
  597. tid = rt_thread_create("esp_net", esp8266_init_thread_entry, (void *) device,
  598. ESP8266_THREAD_STACK_SIZE, ESP8266_THREAD_PRIORITY, 20);
  599. if (tid)
  600. {
  601. rt_thread_startup(tid);
  602. }
  603. else
  604. {
  605. LOG_E("create %s device init thread failed.", device->name);
  606. return -RT_ERROR;
  607. }
  608. #else
  609. esp8266_init_thread_entry(device);
  610. #endif /* AT_DEVICE_ESP8266_INIT_ASYN */
  611. return RT_EOK;
  612. }
  613. static void urc_busy_p_func(struct at_client *client, const char *data, rt_size_t size)
  614. {
  615. LOG_D("system is processing a commands...");
  616. }
  617. static void urc_busy_s_func(struct at_client *client, const char *data, rt_size_t size)
  618. {
  619. LOG_D("system is sending data...");
  620. }
  621. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  622. {
  623. struct at_device *device = RT_NULL;
  624. RT_ASSERT(client && data && size);
  625. char *client_name = client->device->parent.name;
  626. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_CLIENT, client_name);
  627. if (device == RT_NULL)
  628. {
  629. LOG_E("get device(%s) failed.", client_name);
  630. return;
  631. }
  632. if (rt_strstr(data, "WIFI CONNECTED"))
  633. {
  634. LOG_I("%s device wifi is connected.", device->name);
  635. if (device->is_init)
  636. {
  637. netdev_low_level_set_link_status(device->netdev, RT_TRUE);
  638. esp8266_netdev_start_delay_work(device);
  639. }
  640. }
  641. else if (rt_strstr(data, "WIFI DISCONNECT"))
  642. {
  643. LOG_I("%s device wifi is disconnect.", device->name);
  644. if (device->is_init)
  645. {
  646. netdev_low_level_set_link_status(device->netdev, RT_FALSE);
  647. }
  648. }
  649. }
  650. static const struct at_urc urc_table[] =
  651. {
  652. {"busy p", "\r\n", urc_busy_p_func},
  653. {"busy s", "\r\n", urc_busy_s_func},
  654. {"WIFI CONNECTED", "\r\n", urc_func},
  655. {"WIFI DISCONNECT", "\r\n", urc_func},
  656. };
  657. static int esp8266_init(struct at_device *device)
  658. {
  659. struct at_device_esp8266 *esp8266 = (struct at_device_esp8266 *) device->user_data;
  660. /* initialize AT client */
  661. #if RT_VER_NUM >= 0x50100
  662. at_client_init(esp8266->client_name, esp8266->recv_line_num, esp8266->recv_line_num);
  663. #else
  664. at_client_init(esp8266->client_name, esp8266->recv_line_num);
  665. #endif
  666. device->client = at_client_get(esp8266->client_name);
  667. if (device->client == RT_NULL)
  668. {
  669. LOG_E("get AT client(%s) failed.", esp8266->client_name);
  670. return -RT_ERROR;
  671. }
  672. /* register URC data execution function */
  673. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  674. #ifdef AT_USING_SOCKET
  675. esp8266_socket_init(device);
  676. #endif
  677. /* add esp8266 device to the netdev list */
  678. device->netdev = esp8266_netdev_add(esp8266->device_name);
  679. if (device->netdev == RT_NULL)
  680. {
  681. LOG_E("add netdev(%s) failed.", esp8266->device_name);
  682. return -RT_ERROR;
  683. }
  684. /* initialize esp8266 device network */
  685. return esp8266_netdev_set_up(device->netdev);
  686. }
  687. static int esp8266_deinit(struct at_device *device)
  688. {
  689. return esp8266_netdev_set_down(device->netdev);
  690. }
  691. /* reset eap8266 device and initialize device network again */
  692. static int esp8266_reset(struct at_device *device)
  693. {
  694. int result = RT_EOK;
  695. struct at_client *client = device->client;
  696. /* send "AT+RST" commonds to esp8266 device */
  697. result = at_obj_exec_cmd(client, RT_NULL, "AT+RST");
  698. rt_thread_mdelay(1000);
  699. /* waiting 10 seconds for esp8266 device reset */
  700. device->is_init = RT_FALSE;
  701. if (at_client_obj_wait_connect(client, ESP8266_WAIT_CONNECT_TIME))
  702. {
  703. return -RT_ETIMEOUT;
  704. }
  705. /* initialize esp8266 device network */
  706. esp8266_net_init(device);
  707. device->is_init = RT_TRUE;
  708. return result;
  709. }
  710. /* change eap8266 wifi ssid and password information */
  711. static int esp8266_wifi_info_set(struct at_device *device, struct at_device_ssid_pwd *info)
  712. {
  713. int result = RT_EOK;
  714. struct at_response *resp = RT_NULL;
  715. if (info->ssid == RT_NULL || info->password == RT_NULL)
  716. {
  717. LOG_E("input wifi ssid(%s) and password(%s) error.", info->ssid, info->password);
  718. return -RT_ERROR;
  719. }
  720. resp = at_create_resp(128, 0, 20 * RT_TICK_PER_SECOND);
  721. if (resp == RT_NULL)
  722. {
  723. LOG_E("no memory for resp create.");
  724. return -RT_ENOMEM;
  725. }
  726. /* connect to input wifi ap */
  727. if (at_obj_exec_cmd(device->client, resp, "AT+CWJAP=\"%s\",\"%s\"", info->ssid, info->password) != RT_EOK)
  728. {
  729. LOG_E("%s device wifi connect failed, check ssid(%s) and password(%s).",
  730. device->name, info->ssid, info->password);
  731. result = -RT_ERROR;
  732. }
  733. if (resp)
  734. {
  735. at_delete_resp(resp);
  736. }
  737. return result;
  738. }
  739. static int esp8266_control(struct at_device *device, int cmd, void *arg)
  740. {
  741. int result = -RT_ERROR;
  742. RT_ASSERT(device);
  743. switch (cmd)
  744. {
  745. case AT_DEVICE_CTRL_POWER_ON:
  746. case AT_DEVICE_CTRL_POWER_OFF:
  747. case AT_DEVICE_CTRL_LOW_POWER:
  748. case AT_DEVICE_CTRL_SLEEP:
  749. case AT_DEVICE_CTRL_WAKEUP:
  750. case AT_DEVICE_CTRL_NET_CONN:
  751. case AT_DEVICE_CTRL_NET_DISCONN:
  752. case AT_DEVICE_CTRL_GET_SIGNAL:
  753. case AT_DEVICE_CTRL_GET_GPS:
  754. case AT_DEVICE_CTRL_GET_VER:
  755. LOG_W("not support the control cmd(%d).", cmd);
  756. break;
  757. case AT_DEVICE_CTRL_RESET:
  758. result = esp8266_reset(device);
  759. break;
  760. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  761. result = esp8266_wifi_info_set(device, (struct at_device_ssid_pwd *) arg);
  762. break;
  763. default:
  764. LOG_E("input error control cmd(%d).", cmd);
  765. break;
  766. }
  767. return result;
  768. }
  769. static const struct at_device_ops esp8266_device_ops =
  770. {
  771. esp8266_init,
  772. esp8266_deinit,
  773. esp8266_control,
  774. };
  775. static int esp8266_device_class_register(void)
  776. {
  777. struct at_device_class *class = RT_NULL;
  778. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  779. if (class == RT_NULL)
  780. {
  781. LOG_E("no memory for class create.");
  782. return -RT_ENOMEM;
  783. }
  784. /* fill ESP8266 device class object */
  785. #ifdef AT_USING_SOCKET
  786. esp8266_socket_class_register(class);
  787. #endif
  788. class->device_ops = &esp8266_device_ops;
  789. return at_device_class_register(class, AT_DEVICE_CLASS_ESP8266);
  790. }
  791. INIT_DEVICE_EXPORT(esp8266_device_class_register);
  792. /**
  793. * Convert the ESP8266 AT version string to hexadecimal
  794. *
  795. * @param string containing the AT version
  796. *
  797. * @return hex_number: Hexadecimal AT version number, example: 1.4.1.1 -> 0x1040101
  798. */
  799. unsigned int esp8266_at_version_to_hex(const char *str)
  800. {
  801. const char *version_prefix = "AT version:";
  802. char *version_start;
  803. unsigned int numbers[4];
  804. unsigned int hex_number;
  805. if (str == NULL || *str == '\0')
  806. {
  807. LOG_E("Invalid AT version format\n");
  808. return 0;
  809. }
  810. /* Get AT version string */
  811. version_start = strstr(str, version_prefix);
  812. if (version_start)
  813. {
  814. if (rt_sscanf(version_start, "AT version:%d.%d.%d.%d", &numbers[0], &numbers[1], &numbers[2], &numbers[3]) == 4)
  815. {
  816. hex_number = (numbers[0] << 24) | (numbers[1] << 16) | (numbers[2] << 8) | numbers[3];
  817. }
  818. else
  819. {
  820. LOG_W("The AT instruction format is not standard\n");
  821. return 0;
  822. }
  823. }
  824. else
  825. {
  826. LOG_W("The AT+GMR instruction is not supported\n");
  827. return 0;
  828. }
  829. return hex_number;
  830. }
  831. /**
  832. * This function is used to obtain the ESP8266 AT version number.
  833. *
  834. * @return hex_number: Hexadecimal AT version number, example: 1.4.1.1 -> 0x1040101
  835. */
  836. unsigned int esp8266_get_at_version(void)
  837. {
  838. return ESP8266_GMR_AT_VERSION;
  839. }
  840. #endif /* AT_DEVICE_USING_ESP8266 */