at_device_ml305.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. * 2022-12-16 Jonas first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <at_device_ml305.h>
  14. //#define AT_DEBUG
  15. #define LOG_TAG "at.dev.ml305"
  16. #include <at_log.h>
  17. #ifdef AT_DEVICE_USING_ML305
  18. #define ML305_AT_DEFAULT_TIMEOUT 1000
  19. #define ML305_WAIT_CONNECT_TIME 5000
  20. #define ML305_THREAD_STACK_SIZE 2048
  21. #define ML305_POWER_OFF RT_FALSE
  22. #define ML305_POWER_ON RT_TRUE
  23. #define ML305_POWER_ON_TIME 3
  24. #define ML305_POWER_OFF_TIME 4
  25. #define ML305_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  26. /* AT+CSTT command default*/
  27. static char *CSTT_CHINA_MOBILE = "AT+CSTT=\"CMNET\"";
  28. static char *CSTT_CHINA_UNICOM = "AT+CSTT=\"UNINET\"";
  29. static char *CSTT_CHINA_TELECOM = "AT+CSTT=\"CTNET\"";
  30. static rt_bool_t ml305_get_power_state(struct at_device *device)
  31. {
  32. struct at_device_ml305 *ml305 = RT_NULL;
  33. ml305 = (struct at_device_ml305 *) device->user_data;
  34. return (!rt_pin_read(ml305->power_status_pin)) ? ML305_POWER_ON : ML305_POWER_OFF;
  35. }
  36. static void ml305_power_on(struct at_device *device)
  37. {
  38. struct at_device_ml305 *ml305 = RT_NULL;
  39. ml305 = (struct at_device_ml305 *) device->user_data;
  40. /* not nead to set pin configuration for ml305 device power on */
  41. if (ml305->power_pin == -1 || ml305->power_status_pin == -1)
  42. {
  43. return;
  44. }
  45. if(ml305_get_power_state(device) == ML305_POWER_ON)
  46. {
  47. return;
  48. }
  49. rt_pin_write(ml305->power_pin, PIN_HIGH);
  50. rt_thread_mdelay(ML305_POWER_ON_TIME * RT_TICK_PER_SECOND);
  51. rt_pin_write(ml305->power_pin, PIN_LOW);
  52. }
  53. static void ml305_power_off(struct at_device *device)
  54. {
  55. struct at_device_ml305 *ml305 = RT_NULL;
  56. ml305 = (struct at_device_ml305 *) device->user_data;
  57. do{
  58. /* not nead to set pin configuration for ml305 device power on */
  59. if (ml305->power_pin == -1 || ml305->power_status_pin == -1)
  60. {
  61. return;
  62. }
  63. if(ml305_get_power_state(device) == ML305_POWER_OFF)
  64. {
  65. return;
  66. }
  67. rt_pin_write(ml305->power_pin, PIN_HIGH);
  68. rt_thread_mdelay(ML305_POWER_OFF_TIME * RT_TICK_PER_SECOND);
  69. rt_pin_write(ml305->power_pin, PIN_LOW);
  70. rt_thread_mdelay(100);
  71. }while(ml305_get_power_state(device) != ML305_POWER_OFF);
  72. }
  73. static int ml305_check_link_status(struct at_device *device)
  74. {
  75. at_response_t resp = RT_NULL;
  76. struct at_device_ml305 *ml305 = RT_NULL;
  77. int result = -RT_ERROR;
  78. int link_stat = 0;
  79. RT_ASSERT(device);
  80. ml305 = (struct at_device_ml305 *)device->user_data;
  81. resp = at_create_resp(64, 0, rt_tick_from_millisecond(300));
  82. if (resp == RT_NULL)
  83. {
  84. LOG_E("no memory for resp create.");
  85. return -RT_ERROR;
  86. }
  87. if(at_obj_exec_cmd(device->client, resp, "AT+CGREG?") < 0)
  88. {
  89. result = -RT_ERROR;
  90. goto __exit;
  91. }
  92. if (at_resp_parse_line_args_by_kw(resp, "+CGREG:", "+CGREG: %*d,%d", &link_stat) > 0)
  93. {
  94. if (link_stat == 1 || link_stat == 5)
  95. {
  96. result = RT_EOK;
  97. }
  98. }
  99. if(at_obj_exec_cmd(device->client, resp, "AT+CGACT?") < 0)
  100. {
  101. result = -RT_ERROR;
  102. goto __exit;
  103. }
  104. //+CGACT: 1,1
  105. if (at_resp_parse_line_args_by_kw(resp, "+CGACT:", "+CGACT: %*d,%d", &link_stat) > 0)
  106. {
  107. result = link_stat;
  108. }
  109. __exit:
  110. if(resp)
  111. {
  112. at_delete_resp(resp);
  113. }
  114. return(result);
  115. }
  116. /* ============================= ml305 network interface operations ============================= */
  117. /* set ml305 network interface device status and address information */
  118. static int ml305_netdev_set_info(struct netdev *netdev)
  119. {
  120. #define ML305_IMEI_RESP_SIZE 32
  121. #define ML305_IPADDR_RESP_SIZE 64
  122. #define ML305_DNS_RESP_SIZE 96
  123. #define ML305_INFO_RESP_TIMO rt_tick_from_millisecond(300)
  124. int result = RT_EOK;
  125. ip_addr_t addr;
  126. at_response_t resp = RT_NULL;
  127. struct at_device *device = RT_NULL;
  128. RT_ASSERT(netdev);
  129. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  130. if (device == RT_NULL)
  131. {
  132. LOG_E("get device(%s) failed.", netdev->name);
  133. return -RT_ERROR;
  134. }
  135. /* set network interface device status */
  136. netdev_low_level_set_status(netdev, RT_TRUE);
  137. netdev_low_level_set_link_status(netdev, RT_TRUE);
  138. netdev_low_level_set_dhcp_status(netdev, RT_TRUE);
  139. resp = at_create_resp(ML305_IMEI_RESP_SIZE, 0, ML305_INFO_RESP_TIMO);
  140. if (resp == RT_NULL)
  141. {
  142. LOG_E("no memory for resp create.");
  143. result = -RT_ENOMEM;
  144. goto __exit;
  145. }
  146. /* set network interface device hardware address(IMEI) */
  147. {
  148. #define ML305_NETDEV_HWADDR_LEN 8
  149. #define ML305_IMEI_LEN 15
  150. char imei[ML305_IMEI_LEN + 1] = {0};
  151. int i = 0, j = 0;
  152. /* send "AT+GSN" commond to get device IMEI */
  153. if (at_obj_exec_cmd(device->client, resp, "AT+GSN=1") < 0)
  154. {
  155. result = -RT_ERROR;
  156. goto __exit;
  157. }
  158. if(at_resp_parse_line_args_by_kw(resp, "+GSN:", "+GSN:%s", imei) <= 0)
  159. {
  160. LOG_E("ml305 device(%s) prase \"AT+GSN\" commands resposne data error.", device->name);
  161. result = -RT_ERROR;
  162. goto __exit;
  163. }
  164. LOG_D("ml305 device(%s) IMEI number: %s", device->name, imei);
  165. netdev->hwaddr_len = ML305_NETDEV_HWADDR_LEN;
  166. /* get hardware address by IMEI */
  167. for (i = 0, j = 0; i < ML305_NETDEV_HWADDR_LEN && j < ML305_IMEI_LEN; i++, j += 2)
  168. {
  169. if (j != ML305_IMEI_LEN - 1)
  170. {
  171. netdev->hwaddr[i] = (imei[j] - '0') * 10 + (imei[j + 1] - '0');
  172. }
  173. else
  174. {
  175. netdev->hwaddr[i] = (imei[j] - '0');
  176. }
  177. }
  178. }
  179. /* set network interface device IP address */
  180. {
  181. #define IP_ADDR_SIZE_MAX 16
  182. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  183. at_resp_set_info(resp, ML305_IPADDR_RESP_SIZE, 0, ML305_INFO_RESP_TIMO);
  184. /* send "AT+CIFSR" commond to get IP address */
  185. if (at_obj_exec_cmd(device->client, resp, "AT+CGPADDR=1") < 0)
  186. {
  187. result = -RT_ERROR;
  188. goto __exit;
  189. }
  190. if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*d,\"%[^\"]", ipaddr) <= 0)
  191. {
  192. LOG_E("ml305 device(%s) prase \"AT+CGPADDR=1\" commands resposne data error!", device->name);
  193. result = -RT_ERROR;
  194. goto __exit;
  195. }
  196. LOG_D("ml305 device(%s) IP address: %s", device->name, ipaddr);
  197. /* set network interface address information */
  198. inet_aton(ipaddr, &addr);
  199. netdev_low_level_set_ipaddr(netdev, &addr);
  200. }
  201. /* set network interface device dns server */
  202. {
  203. #define DNS_ADDR_SIZE_MAX 16
  204. char dns_server1[DNS_ADDR_SIZE_MAX] = {0}, dns_server2[DNS_ADDR_SIZE_MAX] = {0};
  205. at_resp_set_info(resp, ML305_DNS_RESP_SIZE, 0, ML305_INFO_RESP_TIMO);
  206. /* send "AT+CDNSCFG?" commond to get DNS servers address */
  207. if (at_obj_exec_cmd(device->client, resp, "AT+MDNSCFG?") < 0)
  208. {
  209. result = -RT_ERROR;
  210. goto __exit;
  211. }
  212. //+MDNSCFG: IPV4 DNS1:211.136.17.107 IPV4 DNS2:211.136.20.203
  213. const char *dns_str = at_resp_get_line_by_kw(resp, "DNS1:");
  214. const char *dns1_str = strstr(dns_str, "DNS1:");
  215. rt_sscanf(dns1_str, "DNS1:%s", dns_server1);
  216. const char *dns2_str = strstr(dns_str, "DNS2:");
  217. rt_sscanf(dns2_str, "DNS2:%s", dns_server2);
  218. LOG_D("ml305 device(%s) primary DNS server address: %s", device->name, dns_server1);
  219. LOG_D("ml305 device(%s) secondary DNS server address: %s", device->name, dns_server2);
  220. inet_aton(dns_server1, &addr);
  221. netdev_low_level_set_dns_server(netdev, 0, &addr);
  222. inet_aton(dns_server2, &addr);
  223. netdev_low_level_set_dns_server(netdev, 1, &addr);
  224. }
  225. __exit:
  226. if (resp)
  227. {
  228. at_delete_resp(resp);
  229. }
  230. return result;
  231. }
  232. static void check_link_status_entry(void *parameter)
  233. {
  234. #define ML305_LINK_STATUS_OK 1
  235. #define ML305_LINK_RESP_SIZE 128
  236. #define ML305_LINK_RESP_TIMO (3 * RT_TICK_PER_SECOND)
  237. #define ML305_LINK_DELAY_TIME (30 * RT_TICK_PER_SECOND)
  238. at_response_t resp = RT_NULL;
  239. int result_code, link_status;
  240. struct at_device *device = RT_NULL;
  241. struct netdev *netdev = (struct netdev *)parameter;
  242. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  243. if (device == RT_NULL)
  244. {
  245. LOG_E("get device(%s) failed.", netdev->name);
  246. return;
  247. }
  248. resp = at_create_resp(ML305_LINK_RESP_SIZE, 0, ML305_LINK_RESP_TIMO);
  249. if (resp == RT_NULL)
  250. {
  251. LOG_E("no memory for response create.");
  252. return;
  253. }
  254. while(1)
  255. {
  256. link_status = ml305_check_link_status(device);
  257. if(link_status < 0)
  258. {
  259. rt_thread_mdelay(ML305_LINK_DELAY_TIME);
  260. continue;
  261. }
  262. /* check the network interface device link status */
  263. if ((ML305_LINK_STATUS_OK == link_status) != netdev_is_link_up(netdev))
  264. {
  265. netdev_low_level_set_link_status(netdev, (ML305_LINK_STATUS_OK == link_status));
  266. }
  267. rt_thread_mdelay(ML305_LINK_DELAY_TIME);
  268. }
  269. }
  270. static int ml305_netdev_check_link_status(struct netdev *netdev)
  271. {
  272. #define ML305_LINK_THREAD_TICK 20
  273. #define ML305_LINK_THREAD_STACK_SIZE (1024 + 512)
  274. #define ML305_LINK_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX - 2)
  275. rt_thread_t tid;
  276. char tname[RT_NAME_MAX] = {0};
  277. if (netdev == RT_NULL)
  278. {
  279. LOG_E("input network interface device is NULL.\n");
  280. return -RT_ERROR;
  281. }
  282. rt_snprintf(tname, RT_NAME_MAX, "%s_link", netdev->name);
  283. tid = rt_thread_create(tname, check_link_status_entry, (void *) netdev,
  284. ML305_LINK_THREAD_STACK_SIZE, ML305_LINK_THREAD_PRIORITY, ML305_LINK_THREAD_TICK);
  285. if (tid)
  286. {
  287. rt_thread_startup(tid);
  288. }
  289. return RT_EOK;
  290. }
  291. static int ml305_net_init(struct at_device *device);
  292. static int ml305_netdev_set_up(struct netdev *netdev)
  293. {
  294. struct at_device *device = RT_NULL;
  295. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  296. if (device == RT_NULL)
  297. {
  298. LOG_E("get ml305 device by netdev name(%s) failed.", netdev->name);
  299. return -RT_ERROR;
  300. }
  301. if (device->is_init == RT_FALSE)
  302. {
  303. device->is_init = RT_TRUE;
  304. ml305_net_init(device);
  305. netdev_low_level_set_status(netdev, RT_TRUE);
  306. LOG_D("the network interface device(%s) set up status.", netdev->name);
  307. }
  308. return RT_EOK;
  309. }
  310. static int ml305_netdev_set_down(struct netdev *netdev)
  311. {
  312. struct at_device *device = RT_NULL;
  313. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  314. if (device == RT_NULL)
  315. {
  316. LOG_E("get ml305 device by netdev name(%s) failed.", netdev->name);
  317. return -RT_ERROR;
  318. }
  319. if (device->is_init == RT_TRUE)
  320. {
  321. ml305_power_off(device);
  322. device->is_init = RT_FALSE;
  323. netdev_low_level_set_status(netdev, RT_FALSE);
  324. LOG_D("the network interface device(%s) set down status.", netdev->name);
  325. }
  326. return RT_EOK;
  327. }
  328. static int ml305_netdev_set_dns_server(struct netdev *netdev, uint8_t dns_num, ip_addr_t *dns_server)
  329. {
  330. #define ML305_DNS_RESP_LEN 8
  331. #define ML305_DNS_RESP_TIMEO rt_tick_from_millisecond(300)
  332. int result = RT_EOK;
  333. at_response_t resp = RT_NULL;
  334. struct at_device *device = RT_NULL;
  335. RT_ASSERT(netdev);
  336. RT_ASSERT(dns_server);
  337. device = at_device_get_by_name(AT_DEVICE_NAMETYPE_NETDEV, netdev->name);
  338. if (device == RT_NULL)
  339. {
  340. LOG_E("get ml305 device by netdev name(%s) failed.", netdev->name);
  341. return -RT_ERROR;
  342. }
  343. resp = at_create_resp(ML305_DNS_RESP_LEN, 0, ML305_DNS_RESP_TIMEO);
  344. if (resp == RT_NULL)
  345. {
  346. LOG_D("ml305 set dns server failed, no memory for response object.");
  347. result = -RT_ENOMEM;
  348. goto __exit;
  349. }
  350. /* send "AT+CDNSCFG=<pri_dns>[,<sec_dns>]" commond to set dns servers */
  351. if (at_obj_exec_cmd(device->client, resp, "AT+CDNSCFG=\"%s\"", inet_ntoa(*dns_server)) < 0)
  352. {
  353. result = -RT_ERROR;
  354. goto __exit;
  355. }
  356. netdev_low_level_set_dns_server(netdev, dns_num, dns_server);
  357. __exit:
  358. if (resp)
  359. {
  360. at_delete_resp(resp);
  361. }
  362. return result;
  363. }
  364. static int ml305_ping_domain_resolve(struct at_device *device, const char *name, char ip[16])
  365. {
  366. int result = RT_EOK;
  367. char recv_ip[16] = { 0 };
  368. at_response_t resp = RT_NULL;
  369. /* The maximum response time is 14 seconds, affected by network status */
  370. resp = at_create_resp(512, 4, 14 * RT_TICK_PER_SECOND);
  371. if (resp == RT_NULL)
  372. {
  373. LOG_E("no memory for ml305 device(%s) response structure.", device->name);
  374. return -RT_ENOMEM;
  375. }
  376. if (at_obj_exec_cmd(device->client, resp, "AT+CGACT=1,1") < 0)
  377. {
  378. result = -RT_ERROR;
  379. goto __exit;
  380. }
  381. if (at_obj_exec_cmd(device->client, resp, "AT+MDNSGIP=\"%s\"", name) < 0)
  382. {
  383. result = -RT_ERROR;
  384. goto __exit;
  385. }
  386. if (at_resp_parse_line_args_by_kw(resp, "+MDNSGIP:", "%*[^,],%*[^,],\"%[^\"]", recv_ip) < 0)
  387. {
  388. rt_thread_mdelay(100);
  389. /* resolve failed, maybe receive an URC CRLF */
  390. }
  391. if (rt_strlen(recv_ip) < 8)
  392. {
  393. rt_thread_mdelay(100);
  394. /* resolve failed, maybe receive an URC CRLF */
  395. }
  396. else
  397. {
  398. rt_strncpy(ip, recv_ip, 15);
  399. ip[15] = '\0';
  400. }
  401. __exit:
  402. if (resp)
  403. {
  404. at_delete_resp(resp);
  405. }
  406. return result;
  407. }
  408. #ifdef NETDEV_USING_PING
  409. #ifdef AT_DEVICE_USING_ML305
  410. static int ml305_netdev_ping(struct netdev *netdev, const char *host,
  411. size_t data_len, uint32_t timeout, struct netdev_ping_resp *ping_resp
  412. #if RT_VER_NUM >= 0x50100
  413. , rt_bool_t is_bind
  414. #endif
  415. )
  416. {
  417. #if RT_VER_NUM >= 0x50100
  418. RT_UNUSED(is_bind);
  419. #endif
  420. LOG_E("ping doesn't support in ml305 device.");
  421. return RT_EOK;
  422. }
  423. #endif
  424. #endif /* NETDEV_USING_PING */
  425. #ifdef NETDEV_USING_NETSTAT
  426. void ml305_netdev_netstat(struct netdev *netdev)
  427. {
  428. // TODO netstat support
  429. }
  430. #endif /* NETDEV_USING_NETSTAT */
  431. const struct netdev_ops ml305_netdev_ops =
  432. {
  433. ml305_netdev_set_up,
  434. ml305_netdev_set_down,
  435. RT_NULL, /* not support set ip, netmask, gatway address */
  436. ml305_netdev_set_dns_server,
  437. RT_NULL, /* not support set DHCP status */
  438. #ifdef NETDEV_USING_PING
  439. ml305_netdev_ping,
  440. #endif
  441. #ifdef NETDEV_USING_NETSTAT
  442. ml305_netdev_netstat,
  443. #endif
  444. };
  445. static struct netdev *ml305_netdev_add(const char *netdev_name)
  446. {
  447. #define ML305_NETDEV_MTU 1500
  448. struct netdev *netdev = RT_NULL;
  449. RT_ASSERT(netdev_name);
  450. netdev = netdev_get_by_name(netdev_name);
  451. if (netdev != RT_NULL)
  452. {
  453. return (netdev);
  454. }
  455. netdev = (struct netdev *) rt_calloc(1, sizeof(struct netdev));
  456. if (netdev == RT_NULL)
  457. {
  458. LOG_E("no memory for ml305 device(%s) netdev structure.", netdev_name);
  459. return RT_NULL;
  460. }
  461. netdev->mtu = ML305_NETDEV_MTU;
  462. netdev->ops = &ml305_netdev_ops;
  463. #ifdef SAL_USING_AT
  464. extern int sal_at_netdev_set_pf_info(struct netdev *netdev);
  465. /* set the network interface socket/netdb operations */
  466. sal_at_netdev_set_pf_info(netdev);
  467. #endif
  468. netdev_register(netdev, netdev_name, RT_NULL);
  469. return netdev;
  470. }
  471. /* ============================= ml305 device operations ============================= */
  472. #define AT_SEND_CMD(client, resp, resp_line, timeout, cmd) \
  473. do { \
  474. (resp) = at_resp_set_info((resp), 128, (resp_line), rt_tick_from_millisecond(timeout)); \
  475. if (at_obj_exec_cmd((client), (resp), (cmd)) < 0) \
  476. { \
  477. result = -RT_ERROR; \
  478. goto __exit; \
  479. } \
  480. } while(0) \
  481. /* init for ml305 */
  482. static void ml305_init_thread_entry(void *parameter)
  483. {
  484. #define INIT_RETRY 5
  485. #define CPIN_RETRY 10
  486. #define CSQ_RETRY 10
  487. #define CREG_RETRY 10
  488. #define CGREG_RETRY 20
  489. #define IPADDR_RETRY 10
  490. #define CGATT_RETRY 10
  491. #define COMMON_RETRY 10
  492. int i, qimux, retry_num = INIT_RETRY;
  493. char parsed_data[10] = {0};
  494. rt_err_t result = RT_EOK;
  495. at_response_t resp = RT_NULL;
  496. struct at_device *device = (struct at_device *)parameter;
  497. struct at_client *client = device->client;
  498. resp = at_create_resp(128, 0, rt_tick_from_millisecond(500));
  499. if (resp == RT_NULL)
  500. {
  501. LOG_E("no memory for ml305 device(%s) response structure.", device->name);
  502. return;
  503. }
  504. LOG_D("start initializing the device(%s)", device->name);
  505. ml305_power_off(device);
  506. while (retry_num--)
  507. {
  508. rt_memset(parsed_data, 0, sizeof(parsed_data));
  509. rt_thread_mdelay(500);
  510. ml305_power_on(device);
  511. rt_thread_mdelay(1000);
  512. /* wait ml305 startup finish */
  513. if (at_client_obj_wait_connect(client, ML305_WAIT_CONNECT_TIME))
  514. {
  515. result = -RT_ETIMEOUT;
  516. goto __exit;
  517. }
  518. /* disable echo */
  519. AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "ATE0");
  520. /* get module version */
  521. AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "ATI");
  522. /* show module version */
  523. for (i = 0; i < (int)resp->line_counts - 1; i++)
  524. {
  525. LOG_D("%s", at_resp_get_line(resp, i + 1));
  526. }
  527. /* check SIM card */
  528. for (i = 0; i < CPIN_RETRY; i++)
  529. {
  530. AT_SEND_CMD(client, resp, 0, 5 * RT_TICK_PER_SECOND, "AT+CPIN?");
  531. if (at_resp_get_line_by_kw(resp, "READY"))
  532. {
  533. LOG_I("ml305 device(%s) SIM card detection success.", device->name);
  534. break;
  535. }
  536. rt_thread_mdelay(500);
  537. }
  538. if (i == CPIN_RETRY)
  539. {
  540. LOG_E("ml305 device(%s) SIM card detection failed.", device->name);
  541. result = -RT_ERROR;
  542. goto __exit;
  543. }
  544. /* check SIM card */
  545. for (i = 0; i < CPIN_RETRY; i++)
  546. {
  547. AT_SEND_CMD(client, resp, 0, 10 * 1000, "AT+ICCID");
  548. if (at_resp_get_line_by_kw(resp, "+ICCID:"))
  549. {
  550. LOG_D("%s device SIM card detection success.", device->name);
  551. break;
  552. }
  553. rt_thread_mdelay(1000);
  554. }
  555. if (i == CPIN_RETRY)
  556. {
  557. LOG_E("%s device SIM card detection failed.", device->name);
  558. result = -RT_ERROR;
  559. goto __exit;
  560. }
  561. /* check signal strength */
  562. for (i = 0; i < CSQ_RETRY; i++)
  563. {
  564. AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "AT+CSQ");
  565. at_resp_parse_line_args_by_kw(resp, "+CSQ:", "+CSQ: %s", &parsed_data);
  566. if (rt_strncmp(parsed_data, "99,99", sizeof(parsed_data)))
  567. {
  568. LOG_D("signal strength: %s", parsed_data);
  569. break;
  570. }
  571. rt_thread_mdelay(2000);
  572. }
  573. if(i == CSQ_RETRY)
  574. {
  575. LOG_E("%s device signal strength check failed(%s).", device->name, parsed_data);
  576. result = -RT_ERROR;
  577. goto __exit;
  578. }
  579. /* check the GSM network is registered */
  580. for (i = 0; i < CREG_RETRY; i++)
  581. {
  582. AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "AT+CREG?");
  583. at_resp_parse_line_args_by_kw(resp, "+CREG:", "+CREG: %s", &parsed_data);
  584. if (!strncmp(parsed_data, "0,1", sizeof(parsed_data)) ||
  585. !strncmp(parsed_data, "0,5", sizeof(parsed_data)))
  586. {
  587. LOG_D("ml305 device(%s) GSM network is registered(%s),", device->name, parsed_data);
  588. break;
  589. }
  590. if(!strncmp(parsed_data, "0,3", sizeof(parsed_data)))
  591. {
  592. LOG_E("%s device GSM network is register failed(%s).", device->name, parsed_data);
  593. result = -RT_ERROR;
  594. goto __exit;
  595. }
  596. rt_thread_mdelay(1000 + 500 * (i + 1));
  597. }
  598. if (i == CREG_RETRY)
  599. {
  600. LOG_E("%s device GSM network is register failed(%s).", device->name, parsed_data);
  601. result = -RT_ERROR;
  602. goto __exit;
  603. }
  604. /* check packet domain attach or detach */
  605. for (i = 0; i < CGATT_RETRY; i++)
  606. {
  607. AT_SEND_CMD(client, resp, 0, ML305_AT_DEFAULT_TIMEOUT, "AT+CGATT?");
  608. at_resp_parse_line_args_by_kw(resp, "+CGATT:", "+CGATT: %s", &parsed_data);
  609. if (!rt_strncmp(parsed_data, "1", 1))
  610. {
  611. LOG_D("%s device Packet domain attach.", device->name);
  612. break;
  613. }
  614. rt_thread_mdelay(1000);
  615. }
  616. if (i == CGATT_RETRY)
  617. {
  618. LOG_E("%s device GPRS attach failed.", device->name);
  619. result = -RT_ERROR;
  620. goto __exit;
  621. }
  622. /* Define PDP Context */
  623. for (i = 0; i < COMMON_RETRY; i++)
  624. {
  625. if (at_obj_exec_cmd(client, resp, "AT+CGDCONT=1,\"IP\",\"CMIOT\"") == RT_EOK)
  626. {
  627. LOG_D("%s device Define PDP Context Success.", device->name);
  628. break;
  629. }
  630. rt_thread_mdelay(100);
  631. }
  632. if (i == COMMON_RETRY)
  633. {
  634. LOG_E("%s device Define PDP Context failed.", device->name);
  635. result = -RT_ERROR;
  636. goto __exit;
  637. }
  638. /* PDP Context Activate*/
  639. for (i = 0; i < COMMON_RETRY; i++)
  640. {
  641. if (at_obj_exec_cmd(client, resp, "AT+CGACT=1,1") == RT_EOK)
  642. {
  643. LOG_D("%s device PDP Context Activate Success.", device->name);
  644. break;
  645. }
  646. rt_thread_mdelay(500);
  647. }
  648. if (i == COMMON_RETRY)
  649. {
  650. LOG_E("%s device PDP Context Activate failed.", device->name);
  651. result = -RT_ERROR;
  652. goto __exit;
  653. }
  654. #if defined (AT_DEBUG)
  655. /* check the GPRS network IP address */
  656. for (i = 0; i < IPADDR_RETRY; i++)
  657. {
  658. if (at_obj_exec_cmd(client, resp, "AT+CGPADDR=1") == RT_EOK)
  659. {
  660. #define IP_ADDR_SIZE_MAX 16
  661. char ipaddr[IP_ADDR_SIZE_MAX] = {0};
  662. /* parse response data "+CGPADDR: 1,<IP_address>" */
  663. if (at_resp_parse_line_args_by_kw(resp, "+CGPADDR:", "+CGPADDR: %*d,%s", ipaddr) > 0)
  664. {
  665. LOG_D("%s device IP address: %s", device->name, ipaddr);
  666. break;
  667. }
  668. }
  669. rt_thread_mdelay(1000);
  670. }
  671. if (i == IPADDR_RETRY)
  672. {
  673. LOG_E("%s device GPRS is get IP address failed", device->name);
  674. result = -RT_ERROR;
  675. goto __exit;
  676. }
  677. #endif
  678. result = RT_EOK;
  679. __exit:
  680. if (result == RT_EOK)
  681. {
  682. break;
  683. }
  684. else
  685. {
  686. /* power off the ml305 device */
  687. ml305_power_off(device);
  688. rt_thread_mdelay(1000);
  689. LOG_I("ml305 device(%s) initialize retry...", device->name);
  690. }
  691. }
  692. if (resp)
  693. {
  694. at_delete_resp(resp);
  695. }
  696. if (result == RT_EOK)
  697. {
  698. device->is_init = RT_TRUE;
  699. /* set network interface device status and address information */
  700. ml305_netdev_set_info(device->netdev);
  701. /* */
  702. ml305_netdev_check_link_status(device->netdev);
  703. /* */
  704. LOG_I("ml305 device(%s) network initialize success!", device->name);
  705. }
  706. else
  707. {
  708. device->is_init = RT_FALSE;
  709. netdev_low_level_set_status(device->netdev, RT_FALSE);
  710. LOG_E("ml305 device(%s) network initialize failed(%d)!", device->name, result);
  711. }
  712. }
  713. static int ml305_net_init(struct at_device *device)
  714. {
  715. #ifdef AT_DEVICE_ML305_INIT_ASYN
  716. rt_thread_t tid;
  717. tid = rt_thread_create("ml305_net_init", ml305_init_thread_entry, (void *)device,
  718. ML305_THREAD_STACK_SIZE, ML305_THREAD_PRIORITY, 20);
  719. if (tid)
  720. {
  721. rt_thread_startup(tid);
  722. }
  723. else
  724. {
  725. LOG_E("create ml305 device(%s) initialization thread failed.", device->name);
  726. return -RT_ERROR;
  727. }
  728. #else
  729. ml305_init_thread_entry(device);
  730. #endif /* AT_DEVICE_ML305_INIT_ASYN */
  731. return RT_EOK;
  732. }
  733. static void urc_func(struct at_client *client, const char *data, rt_size_t size)
  734. {
  735. RT_ASSERT(data);
  736. LOG_I("URC data : %.*s", size, data);
  737. }
  738. /* ml305 device URC table for the device control */
  739. static const struct at_urc urc_table[] =
  740. {
  741. {"READY", "\r\n", urc_func},
  742. };
  743. static int ml305_init(struct at_device *device)
  744. {
  745. struct at_device_ml305 *ml305 = (struct at_device_ml305 *) device->user_data;
  746. struct serial_configure serial_config = RT_SERIAL_CONFIG_DEFAULT;
  747. rt_device_t serial = rt_device_find(ml305->client_name);
  748. if (serial == RT_NULL)
  749. {
  750. LOG_E("ml305 device(%s) initialize failed, get AT client(%s) failed.", ml305->device_name, ml305->client_name);
  751. return -RT_ERROR;
  752. }
  753. serial_config.bufsz = ml305->recv_buff_size;
  754. rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &serial_config);
  755. /* initialize AT client */
  756. #if RT_VER_NUM >= 0x50100
  757. at_client_init(ml305->client_name, ml305->recv_buff_size, ml305->recv_buff_size);
  758. #else
  759. at_client_init(ml305->client_name, ml305->recv_buff_size);
  760. #endif
  761. device->client = at_client_get(ml305->client_name);
  762. if (device->client == RT_NULL)
  763. {
  764. LOG_E("ml305 device(%s) initialize failed, get AT client(%s) failed.", ml305->device_name, ml305->client_name);
  765. return -RT_ERROR;
  766. }
  767. /* register URC data execution function */
  768. at_obj_set_urc_table(device->client, urc_table, sizeof(urc_table) / sizeof(urc_table[0]));
  769. #ifdef AT_USING_SOCKET
  770. ml305_socket_init(device);
  771. #endif
  772. /* add ml305 device to the netdev list */
  773. device->netdev = ml305_netdev_add(ml305->device_name);
  774. if (device->netdev == RT_NULL)
  775. {
  776. LOG_E("ml305 device(%s) initialize failed, get network interface device failed.", ml305->device_name);
  777. return -RT_ERROR;
  778. }
  779. /* initialize ml305 pin configuration */
  780. if (ml305->power_pin != -1)
  781. {
  782. rt_pin_mode(ml305->power_pin, PIN_MODE_OUTPUT);
  783. }
  784. if (ml305->power_status_pin != -1)
  785. {
  786. rt_pin_mode(ml305->power_status_pin, PIN_MODE_INPUT_PULLUP);
  787. }
  788. /* initialize ml305 device network */
  789. return ml305_netdev_set_up(device->netdev);
  790. }
  791. static int ml305_deinit(struct at_device *device)
  792. {
  793. return ml305_netdev_set_down(device->netdev);
  794. }
  795. static int ml305_control(struct at_device *device, int cmd, void *arg)
  796. {
  797. int result = -RT_ERROR;
  798. RT_ASSERT(device);
  799. switch (cmd)
  800. {
  801. case AT_DEVICE_CTRL_POWER_ON:
  802. case AT_DEVICE_CTRL_POWER_OFF:
  803. case AT_DEVICE_CTRL_RESET:
  804. case AT_DEVICE_CTRL_LOW_POWER:
  805. case AT_DEVICE_CTRL_SLEEP:
  806. case AT_DEVICE_CTRL_WAKEUP:
  807. case AT_DEVICE_CTRL_NET_CONN:
  808. case AT_DEVICE_CTRL_NET_DISCONN:
  809. case AT_DEVICE_CTRL_SET_WIFI_INFO:
  810. case AT_DEVICE_CTRL_GET_SIGNAL:
  811. case AT_DEVICE_CTRL_GET_GPS:
  812. case AT_DEVICE_CTRL_GET_VER:
  813. LOG_W("ml305 not support the control command(%d).", cmd);
  814. break;
  815. default:
  816. LOG_E("input error control command(%d).", cmd);
  817. break;
  818. }
  819. return result;
  820. }
  821. const struct at_device_ops ml305_device_ops =
  822. {
  823. ml305_init,
  824. ml305_deinit,
  825. ml305_control,
  826. };
  827. static int ml305_device_class_register(void)
  828. {
  829. struct at_device_class *class = RT_NULL;
  830. class = (struct at_device_class *) rt_calloc(1, sizeof(struct at_device_class));
  831. if (class == RT_NULL)
  832. {
  833. LOG_E("no memory for ml305 device class create.");
  834. return -RT_ENOMEM;
  835. }
  836. /* fill ml305 device class object */
  837. #ifdef AT_USING_SOCKET
  838. ml305_socket_class_register(class);
  839. #endif
  840. class->device_ops = &ml305_device_ops;
  841. return at_device_class_register(class, AT_DEVICE_CLASS_ML305);
  842. }
  843. INIT_DEVICE_EXPORT(ml305_device_class_register);
  844. #endif /* AT_DEVICE_USING_ML305 */