espconn.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: espconn.c
  5. *
  6. * Description: espconn interface for user
  7. *
  8. * Modification history:
  9. * 2014/3/31, v1.0 create this file.
  10. *******************************************************************************/
  11. #include "lwip/netif.h"
  12. #include "lwip/inet.h"
  13. #include "netif/etharp.h"
  14. #include "lwip/tcp.h"
  15. #include "lwip/ip.h"
  16. #include "lwip/init.h"
  17. #include "ets_sys.h"
  18. #include "os_type.h"
  19. //#include "os.h"
  20. #include "lwip/mem.h"
  21. #include "lwip/app/espconn_tcp.h"
  22. #include "lwip/app/espconn_udp.h"
  23. #include "lwip/app/espconn.h"
  24. #include "user_interface.h"
  25. #ifdef MEMLEAK_DEBUG
  26. static const char mem_debug_file[] ICACHE_RODATA_ATTR = __FILE__;
  27. #endif
  28. espconn_msg *plink_active = NULL;
  29. espconn_msg *pserver_list = NULL;
  30. remot_info premot[linkMax];
  31. struct espconn_packet pktinfo[2];
  32. static uint8 espconn_tcp_get_buf_count(espconn_buf *pesp_buf);
  33. /******************************************************************************
  34. * FunctionName : espconn_copy_partial
  35. * Description : reconnect with host
  36. * Parameters : arg -- Additional argument to pass to the callback function
  37. * Returns : none
  38. *******************************************************************************/
  39. void ICACHE_FLASH_ATTR
  40. espconn_copy_partial(struct espconn *pesp_dest, struct espconn *pesp_source)
  41. {
  42. pesp_dest->type = pesp_source->type;
  43. pesp_dest->state = pesp_source->state;
  44. if (pesp_source->type == ESPCONN_TCP){
  45. pesp_dest->proto.tcp->remote_port = pesp_source->proto.tcp->remote_port;
  46. pesp_dest->proto.tcp->local_port = pesp_source->proto.tcp->local_port;
  47. os_memcpy(pesp_dest->proto.tcp->remote_ip, pesp_source->proto.tcp->remote_ip, 4);
  48. os_memcpy(pesp_dest->proto.tcp->local_ip, pesp_source->proto.tcp->local_ip, 4);
  49. pesp_dest->proto.tcp->connect_callback = pesp_source->proto.tcp->connect_callback;
  50. pesp_dest->proto.tcp->reconnect_callback = pesp_source->proto.tcp->reconnect_callback;
  51. pesp_dest->proto.tcp->disconnect_callback = pesp_source->proto.tcp->disconnect_callback;
  52. } else {
  53. pesp_dest->proto.udp->remote_port = pesp_source->proto.udp->remote_port;
  54. pesp_dest->proto.udp->local_port = pesp_source->proto.udp->local_port;
  55. os_memcpy(pesp_dest->proto.udp->remote_ip, pesp_source->proto.udp->remote_ip, 4);
  56. os_memcpy(pesp_dest->proto.udp->local_ip, pesp_source->proto.udp->local_ip, 4);
  57. }
  58. pesp_dest->recv_callback = pesp_source->recv_callback;
  59. pesp_dest->sent_callback = pesp_source->sent_callback;
  60. pesp_dest->link_cnt = pesp_source->link_cnt;
  61. pesp_dest->reverse = pesp_source->reverse;
  62. }
  63. /******************************************************************************
  64. * FunctionName : espconn_copy_partial
  65. * Description : insert the node to the active connection list
  66. * Parameters : arg -- Additional argument to pass to the callback function
  67. * Returns : none
  68. *******************************************************************************/
  69. void ICACHE_FLASH_ATTR espconn_list_creat(espconn_msg **phead, espconn_msg* pinsert)
  70. {
  71. espconn_msg *plist = NULL;
  72. // espconn_msg *ptest = NULL;
  73. if (*phead == NULL)
  74. *phead = pinsert;
  75. else {
  76. plist = *phead;
  77. while (plist->pnext != NULL) {
  78. plist = plist->pnext;
  79. }
  80. plist->pnext = pinsert;
  81. }
  82. pinsert->pnext = NULL;
  83. /* ptest = *phead;
  84. while(ptest != NULL){
  85. os_printf("espconn_list_creat %p\n", ptest);
  86. ptest = ptest->pnext;
  87. }*/
  88. }
  89. /******************************************************************************
  90. * FunctionName : espconn_list_delete
  91. * Description : remove the node from the active connection list
  92. * Parameters : arg -- Additional argument to pass to the callback function
  93. * Returns : none
  94. *******************************************************************************/
  95. void ICACHE_FLASH_ATTR espconn_list_delete(espconn_msg **phead, espconn_msg* pdelete)
  96. {
  97. espconn_msg *plist = NULL;
  98. // espconn_msg *ptest = NULL;
  99. plist = *phead;
  100. if (plist == NULL){
  101. *phead = NULL;
  102. } else {
  103. if (plist == pdelete){
  104. *phead = plist->pnext;
  105. } else {
  106. while (plist != NULL) {
  107. if (plist->pnext == pdelete){
  108. plist->pnext = pdelete->pnext;
  109. }
  110. plist = plist->pnext;
  111. }
  112. }
  113. }
  114. /* ptest = *phead;
  115. while(ptest != NULL){
  116. os_printf("espconn_list_delete %p\n", ptest);
  117. ptest = ptest->pnext;
  118. }*/
  119. }
  120. /******************************************************************************
  121. * FunctionName : espconn_pbuf_create
  122. * Description : insert the node to the active connection list
  123. * Parameters : arg -- Additional argument to pass to the callback function
  124. * Returns : none
  125. *******************************************************************************/
  126. void ICACHE_FLASH_ATTR espconn_pbuf_create(espconn_buf **phead, espconn_buf* pinsert)
  127. {
  128. espconn_buf *plist = NULL;
  129. if (*phead == NULL)
  130. *phead = pinsert;
  131. else {
  132. plist = *phead;
  133. while (plist->pnext != NULL) {
  134. plist = plist->pnext;
  135. }
  136. plist->pnext = pinsert;
  137. }
  138. pinsert->pnext = NULL;
  139. }
  140. /******************************************************************************
  141. * FunctionName : espconn_pbuf_delete
  142. * Description : remove the node from the active connection list
  143. * Parameters : arg -- Additional argument to pass to the callback function
  144. * Returns : none
  145. *******************************************************************************/
  146. void ICACHE_FLASH_ATTR espconn_pbuf_delete(espconn_buf **phead, espconn_buf* pdelete)
  147. {
  148. espconn_buf *plist = NULL;
  149. plist = *phead;
  150. if (plist == NULL){
  151. *phead = NULL;
  152. } else {
  153. if (plist == pdelete){
  154. *phead = plist->pnext;
  155. } else {
  156. while (plist != NULL) {
  157. if (plist->pnext == pdelete){
  158. plist->pnext = pdelete->pnext;
  159. }
  160. plist = plist->pnext;
  161. }
  162. }
  163. }
  164. }
  165. /******************************************************************************
  166. * FunctionName : espconn_find_connection
  167. * Description : Initialize the server: set up a listening PCB and bind it to
  168. * the defined port
  169. * Parameters : espconn -- the espconn used to build server
  170. * Returns : true or false
  171. *******************************************************************************/
  172. bool ICACHE_FLASH_ATTR espconn_find_connection(struct espconn *pespconn, espconn_msg **pnode)
  173. {
  174. espconn_msg *plist = NULL;
  175. struct ip_addr ip_remot;
  176. struct ip_addr ip_list;
  177. if (pespconn == NULL)
  178. return false;
  179. /*find the active connection node*/
  180. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  181. if (pespconn == plist->pespconn) {
  182. *pnode = plist;
  183. return true;
  184. }
  185. }
  186. /*find the active server node*/
  187. for (plist = pserver_list; plist != NULL; plist = plist->pnext){
  188. if (pespconn == plist->pespconn) {
  189. if (pespconn->proto.tcp == NULL)
  190. return false;
  191. IP4_ADDR(&ip_remot, pespconn->proto.tcp->remote_ip[0],
  192. pespconn->proto.tcp->remote_ip[1],
  193. pespconn->proto.tcp->remote_ip[2],
  194. pespconn->proto.tcp->remote_ip[3]);
  195. if ((ip_remot.addr == IPADDR_ANY) || (pespconn->proto.tcp->remote_port == 0))
  196. return false;
  197. /*find the active connection node*/
  198. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  199. IP4_ADDR(&ip_list, plist->pcommon.remote_ip[0],
  200. plist->pcommon.remote_ip[1], plist->pcommon.remote_ip[2],
  201. plist->pcommon.remote_ip[3]);
  202. if ((ip_list.addr == ip_remot.addr) && (pespconn->proto.tcp->remote_port == plist->pcommon.remote_port)) {
  203. *pnode = plist;
  204. return true;
  205. }
  206. }
  207. return false;
  208. }
  209. }
  210. return false;
  211. }
  212. /******************************************************************************
  213. * FunctionName : espconn_get_acticve_num
  214. * Description : get the count of simulatenously active connections
  215. * Parameters : type -- the type
  216. * Returns : the count of simulatenously active connections
  217. *******************************************************************************/
  218. static uint8 ICACHE_FLASH_ATTR
  219. espconn_get_acticve_num(uint8 type)
  220. {
  221. espconn_msg *plist = NULL;
  222. uint8 num_tcp_active = 0;
  223. for (plist = plink_active; plist != NULL; plist = plist->pnext) {
  224. if (plist->pespconn != NULL && plist->pespconn->type == type) {
  225. num_tcp_active++;
  226. }
  227. }
  228. return num_tcp_active;
  229. }
  230. /******************************************************************************
  231. * FunctionName : espconn_connect
  232. * Description : The function given as the connect
  233. * Parameters : espconn -- the espconn used to listen the connection
  234. * Returns : none
  235. *******************************************************************************/
  236. sint8 ICACHE_FLASH_ATTR
  237. espconn_connect(struct espconn *espconn)
  238. {
  239. struct ip_addr ipaddr;
  240. struct ip_info ipinfo;
  241. uint8 connect_status = 0;
  242. sint8 value = ESPCONN_OK;
  243. espconn_msg *plist = NULL;
  244. remot_info *pinfo = NULL;
  245. if (espconn == NULL) {
  246. return ESPCONN_ARG;
  247. } else if (espconn ->type != ESPCONN_TCP)
  248. return ESPCONN_ARG;
  249. /*Check the active node count whether is the limit or not*/
  250. if (espconn_get_acticve_num(ESPCONN_TCP) >= espconn_tcp_get_max_con())
  251. return ESPCONN_ISCONN;
  252. /*Check the IP address whether is zero or not in different mode*/
  253. if (wifi_get_opmode() == ESPCONN_STA){
  254. wifi_get_ip_info(STA_NETIF,&ipinfo);
  255. if (ipinfo.ip.addr == 0){
  256. return ESPCONN_RTE;
  257. }
  258. } else if(wifi_get_opmode() == ESPCONN_AP){
  259. wifi_get_ip_info(AP_NETIF,&ipinfo);
  260. if (ipinfo.ip.addr == 0){
  261. return ESPCONN_RTE;
  262. }
  263. } else if(wifi_get_opmode() == ESPCONN_AP_STA){
  264. IP4_ADDR(&ipaddr, espconn->proto.tcp->remote_ip[0],
  265. espconn->proto.tcp->remote_ip[1],
  266. espconn->proto.tcp->remote_ip[2],
  267. espconn->proto.tcp->remote_ip[3]);
  268. ipaddr.addr <<= 8;
  269. wifi_get_ip_info(AP_NETIF,&ipinfo);
  270. ipinfo.ip.addr <<= 8;
  271. espconn_printf("softap_addr = %x, remote_addr = %x\n", ipinfo.ip.addr, ipaddr.addr);
  272. if (ipaddr.addr != ipinfo.ip.addr){
  273. connect_status = wifi_station_get_connect_status();
  274. if (connect_status == STATION_GOT_IP){
  275. wifi_get_ip_info(STA_NETIF,&ipinfo);
  276. if (ipinfo.ip.addr == 0)
  277. return ESPCONN_RTE;
  278. } else if (connect_status == STATION_IDLE){
  279. return ESPCONN_RTE;
  280. } else {
  281. return connect_status;
  282. }
  283. }
  284. }
  285. /*check the active node information whether is the same as the entity or not*/
  286. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  287. if (plist->pespconn && plist->pespconn->type == ESPCONN_TCP){
  288. if (espconn->proto.tcp->local_port == plist->pespconn->proto.tcp->local_port){
  289. return ESPCONN_ISCONN;
  290. }
  291. }
  292. }
  293. value = espconn_tcp_client(espconn);
  294. return value;
  295. }
  296. /******************************************************************************
  297. * FunctionName : espconn_create
  298. * Description : sent data for client or server
  299. * Parameters : espconn -- espconn to the data transmission
  300. * Returns : result
  301. *******************************************************************************/
  302. sint8 ICACHE_FLASH_ATTR
  303. espconn_create(struct espconn *espconn)
  304. {
  305. sint8 value = ESPCONN_OK;
  306. espconn_msg *plist = NULL;
  307. if (espconn == NULL) {
  308. return ESPCONN_ARG;
  309. } else if (espconn ->type != ESPCONN_UDP){
  310. return ESPCONN_ARG;
  311. }
  312. /*check the active node information whether is the same as the entity or not*/
  313. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  314. if (plist->pespconn && plist->pespconn->type == ESPCONN_UDP){
  315. if (espconn->proto.udp->local_port == plist->pespconn->proto.udp->local_port){
  316. return ESPCONN_ISCONN;
  317. }
  318. }
  319. }
  320. value = espconn_udp_server(espconn);
  321. return value;
  322. }
  323. /******************************************************************************
  324. * FunctionName : espconn_sent
  325. * Description : sent data for client or server
  326. * Parameters : espconn -- espconn to set for client or server
  327. * psent -- data to send
  328. * length -- length of data to send
  329. * Returns : none
  330. *******************************************************************************/
  331. sint8 ICACHE_FLASH_ATTR
  332. espconn_sent(struct espconn *espconn, uint8 *psent, uint16 length)
  333. {
  334. espconn_msg *pnode = NULL;
  335. bool value = false;
  336. err_t error = ESPCONN_OK;
  337. if (espconn == NULL || psent == NULL || length == 0) {
  338. return ESPCONN_ARG;
  339. }
  340. /*Find the node depend on the espconn message*/
  341. value = espconn_find_connection(espconn, &pnode);
  342. if (value){
  343. espconn ->state = ESPCONN_WRITE;
  344. switch (espconn ->type) {
  345. case ESPCONN_TCP:
  346. /* calling sent function frequently,make sure last packet has been backup or sent fully*/
  347. if (pnode->pcommon.write_flag){
  348. espconn_buf *pbuf = NULL;
  349. /*If total number of espconn_buf on the unsent lists exceeds the set maximum, return an error */
  350. if (espconn_copy_enabled(pnode)){
  351. if (espconn_tcp_get_buf_count(pnode->pcommon.pbuf) >= pnode ->pcommon.pbuf_num)
  352. return ESPCONN_MAXNUM;
  353. } else {
  354. struct tcp_pcb *pcb = pnode->pcommon.pcb;
  355. if (pcb->snd_queuelen >= TCP_SND_QUEUELEN)
  356. return ESPCONN_MAXNUM;
  357. }
  358. pbuf = (espconn_buf*) os_zalloc(sizeof(espconn_buf));
  359. if (pbuf == NULL)
  360. return ESPCONN_MEM;
  361. else {
  362. /*Backup the application packet information for send more data*/
  363. pbuf->payload = psent;
  364. pbuf->punsent = pbuf->payload;
  365. pbuf->unsent = length;
  366. pbuf->len = length;
  367. /*insert the espconn_pbuf to the list*/
  368. espconn_pbuf_create(&pnode->pcommon.pbuf, pbuf);
  369. if (pnode->pcommon.ptail == NULL)
  370. pnode->pcommon.ptail = pbuf;
  371. }
  372. /*when set the data copy option. change the flag for next packet*/
  373. if (espconn_copy_disabled(pnode))
  374. pnode->pcommon.write_flag = false;
  375. error = espconn_tcp_write(pnode);
  376. // if (error != ESPCONN_OK){
  377. // /*send the application packet fail,
  378. // * ensure that each allocated is deleted*/
  379. // espconn_pbuf_delete(&pnode->pcommon.pbuf, pbuf);
  380. // os_free(pbuf);
  381. // pbuf = NULL;
  382. // }
  383. return error;
  384. } else
  385. return ESPCONN_ARG;
  386. break;
  387. case ESPCONN_UDP:
  388. return espconn_udp_sent(pnode, psent, length);
  389. break;
  390. default :
  391. break;
  392. }
  393. }
  394. return ESPCONN_ARG;
  395. }
  396. sint16 ICACHE_FLASH_ATTR espconn_recv(struct espconn *espconn, void *mem, size_t len)
  397. {
  398. espconn_msg *pnode = NULL;
  399. bool value = false;
  400. int bytes_used = 0;
  401. struct tcp_pcb *tpcb = NULL;
  402. if (espconn == NULL || mem == NULL || len == 0)
  403. return ESPCONN_ARG;
  404. /*Find the node depend on the espconn message*/
  405. value = espconn_find_connection(espconn, &pnode);
  406. if (value && espconn->type == ESPCONN_TCP){
  407. if (pnode->readbuf != NULL){
  408. bytes_used = ringbuf_bytes_used(pnode->readbuf);
  409. if (bytes_used != 0) {
  410. if (len > bytes_used) {
  411. len = bytes_used;
  412. }
  413. ringbuf_memcpy_from(mem, pnode->readbuf, len);
  414. tpcb = pnode->pcommon.pcb;
  415. if (tpcb && tpcb->state == ESTABLISHED)
  416. tcp_recved(pnode->pcommon.pcb, len);
  417. return len;
  418. } else {
  419. return ESPCONN_OK;
  420. }
  421. } else{
  422. return ESPCONN_MEM;
  423. }
  424. } else{
  425. return ESPCONN_ARG;
  426. }
  427. return ESPCONN_ARG;
  428. }
  429. /******************************************************************************
  430. * FunctionName : espconn_sendto
  431. * Description : send data for UDP
  432. * Parameters : espconn -- espconn to set for UDP
  433. * psent -- data to send
  434. * length -- length of data to send
  435. * Returns : error
  436. *******************************************************************************/
  437. sint16 ICACHE_FLASH_ATTR
  438. espconn_sendto(struct espconn *espconn, uint8 *psent, uint16 length)
  439. {
  440. espconn_msg *pnode = NULL;
  441. bool value = false;
  442. err_t error = ESPCONN_OK;
  443. if (espconn == NULL || psent == NULL || length == 0) {
  444. return ESPCONN_ARG;
  445. }
  446. /*Find the node depend on the espconn message*/
  447. value = espconn_find_connection(espconn, &pnode);
  448. if (value && espconn->type == ESPCONN_UDP)
  449. return espconn_udp_sendto(pnode, psent, length);
  450. else
  451. return ESPCONN_ARG;
  452. }
  453. /******************************************************************************
  454. * FunctionName : espconn_send
  455. * Description : sent data for client or server
  456. * Parameters : espconn -- espconn to set for client or server
  457. * psent -- data to send
  458. * length -- length of data to send
  459. * Returns : none
  460. *******************************************************************************/
  461. sint8 espconn_send(struct espconn *espconn, uint8 *psent, uint16 length) __attribute__((alias("espconn_sent")));
  462. /******************************************************************************
  463. * FunctionName : espconn_tcp_get_wnd
  464. * Description : get the window size of simulatenously active TCP connections
  465. * Parameters : none
  466. * Returns : the number of TCP_MSS active TCP connections
  467. *******************************************************************************/
  468. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_wnd(void)
  469. {
  470. uint8 tcp_num = 0;
  471. tcp_num = (TCP_WND / TCP_MSS);
  472. return tcp_num;
  473. }
  474. /******************************************************************************
  475. * FunctionName : espconn_tcp_set_max_con
  476. * Description : set the window size simulatenously active TCP connections
  477. * Parameters : num -- the number of TCP_MSS
  478. * Returns : ESPCONN_ARG -- Illegal argument
  479. * ESPCONN_OK -- No error
  480. *******************************************************************************/
  481. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_wnd(uint8 num)
  482. {
  483. if (num == 0 || num > linkMax)
  484. return ESPCONN_ARG;
  485. TCP_WND = (num * TCP_MSS);
  486. return ESPCONN_OK;
  487. }
  488. /******************************************************************************
  489. * FunctionName : espconn_tcp_get_mss
  490. * Description : get the mss size of simulatenously active TCP connections
  491. * Parameters : none
  492. * Returns : the size of TCP_MSS active TCP connections
  493. *******************************************************************************/
  494. uint16 ICACHE_FLASH_ATTR espconn_tcp_get_mss(void)
  495. {
  496. uint16 tcp_num = 0;
  497. tcp_num = TCP_MSS;
  498. return tcp_num;
  499. }
  500. /******************************************************************************
  501. * FunctionName : espconn_tcp_get_max_con
  502. * Description : get the number of simulatenously active TCP connections
  503. * Parameters : espconn -- espconn to set the connect callback
  504. * Returns : none
  505. *******************************************************************************/
  506. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con(void)
  507. {
  508. uint8 tcp_num = 0;
  509. tcp_num = MEMP_NUM_TCP_PCB;
  510. return tcp_num;
  511. }
  512. /******************************************************************************
  513. * FunctionName : espconn_tcp_set_max_con
  514. * Description : set the number of simulatenously active TCP connections
  515. * Parameters : espconn -- espconn to set the connect callback
  516. * Returns : none
  517. *******************************************************************************/
  518. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_con(uint8 num)
  519. {
  520. if (num == 0 || num > linkMax)
  521. return ESPCONN_ARG;
  522. MEMP_NUM_TCP_PCB = num;
  523. return ESPCONN_OK;
  524. }
  525. /******************************************************************************
  526. * FunctionName : espconn_tcp_get_max_retran
  527. * Description : get the Maximum number of retransmissions of data active TCP connections
  528. * Parameters : none
  529. * Returns : the Maximum number of retransmissions
  530. *******************************************************************************/
  531. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_retran(void)
  532. {
  533. uint8 tcp_num = 0;
  534. tcp_num = TCP_MAXRTX;
  535. return tcp_num;
  536. }
  537. /******************************************************************************
  538. * FunctionName : espconn_tcp_set_max_retran
  539. * Description : set the Maximum number of retransmissions of data active TCP connections
  540. * Parameters : num -- the Maximum number of retransmissions
  541. * Returns : result
  542. *******************************************************************************/
  543. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_retran(uint8 num)
  544. {
  545. if (num == 0 || num > 12)
  546. return ESPCONN_ARG;
  547. TCP_MAXRTX = num;
  548. return ESPCONN_OK;
  549. }
  550. /******************************************************************************
  551. * FunctionName : espconn_tcp_get_max_syn
  552. * Description : get the Maximum number of retransmissions of SYN segments
  553. * Parameters : none
  554. * Returns : the Maximum number of retransmissions
  555. *******************************************************************************/
  556. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_syn(void)
  557. {
  558. uint8 tcp_num = 0;
  559. tcp_num = TCP_SYNMAXRTX;
  560. return tcp_num;
  561. }
  562. /******************************************************************************
  563. * FunctionName : espconn_tcp_set_max_syn
  564. * Description : set the Maximum number of retransmissions of SYN segments
  565. * Parameters : num -- the Maximum number of retransmissions
  566. * Returns : result
  567. *******************************************************************************/
  568. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_syn(uint8 num)
  569. {
  570. if (num == 0 || num > 12)
  571. return ESPCONN_ARG;
  572. TCP_SYNMAXRTX = num;
  573. return ESPCONN_OK;
  574. }
  575. /******************************************************************************
  576. * FunctionName : espconn_tcp_get_max_con_allow
  577. * Description : get the count of simulatenously active connections on the server
  578. * Parameters : espconn -- espconn to get the count
  579. * Returns : result
  580. *******************************************************************************/
  581. sint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con_allow(struct espconn *espconn)
  582. {
  583. espconn_msg *pget_msg = NULL;
  584. if ((espconn == NULL) || (espconn->type == ESPCONN_UDP))
  585. return ESPCONN_ARG;
  586. pget_msg = pserver_list;
  587. while (pget_msg != NULL){
  588. if (pget_msg->pespconn == espconn){
  589. return pget_msg->count_opt;
  590. }
  591. pget_msg = pget_msg->pnext;
  592. }
  593. return ESPCONN_ARG;
  594. }
  595. /******************************************************************************
  596. * FunctionName : espconn_tcp_set_max_con_allow
  597. * Description : set the count of simulatenously active connections on the server
  598. * Parameters : espconn -- espconn to set the count
  599. * Returns : result
  600. *******************************************************************************/
  601. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num)
  602. {
  603. espconn_msg *pset_msg = NULL;
  604. if ((espconn == NULL) || (num > MEMP_NUM_TCP_PCB) || (espconn->type == ESPCONN_UDP))
  605. return ESPCONN_ARG;
  606. pset_msg = pserver_list;
  607. while (pset_msg != NULL){
  608. if (pset_msg->pespconn == espconn){
  609. pset_msg->count_opt = num;
  610. return ESPCONN_OK;
  611. }
  612. pset_msg = pset_msg->pnext;
  613. }
  614. return ESPCONN_ARG;
  615. }
  616. /******************************************************************************
  617. * FunctionName : espconn_tcp_set_buf_count
  618. * Description : set the total number of espconn_buf on the unsent lists for one
  619. * activate connection
  620. * Parameters : espconn -- espconn to set the count
  621. * num -- the total number of espconn_buf
  622. * Returns : result
  623. *******************************************************************************/
  624. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_buf_count(struct espconn *espconn, uint8 num)
  625. {
  626. espconn_msg *plist = NULL;
  627. if (espconn == NULL || (num > TCP_SND_QUEUELEN))
  628. return ESPCONN_ARG;
  629. /*find the node from the active connection list*/
  630. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  631. if (plist->pespconn && plist->pespconn == espconn && espconn->type == ESPCONN_TCP){
  632. plist->pcommon.pbuf_num = num;
  633. return ESPCONN_OK;
  634. }
  635. }
  636. if (plist == NULL)
  637. return ESPCONN_ARG;
  638. }
  639. /******************************************************************************
  640. * FunctionName : espconn_tcp_get_buf_count
  641. * Description : get the count of the current node which has espconn_buf
  642. * Parameters : pesp_buf -- the list head of espconn_buf type
  643. * Returns : the count of the current node which has espconn_buf
  644. *******************************************************************************/
  645. static uint8 ICACHE_FLASH_ATTR espconn_tcp_get_buf_count(espconn_buf *pesp_buf)
  646. {
  647. espconn_buf *pbuf_list = pesp_buf;
  648. uint8 pbuf_num = 0;
  649. /*polling the list get the count of the current node*/
  650. while (pbuf_list != NULL){
  651. pbuf_list = pbuf_list->pnext;
  652. pbuf_num ++;
  653. }
  654. return pbuf_num;
  655. }
  656. /******************************************************************************
  657. * FunctionName : espconn_regist_sentcb
  658. * Description : Used to specify the function that should be called when data
  659. * has been successfully delivered to the remote host.
  660. * Parameters : espconn -- espconn to set the sent callback
  661. * sent_cb -- sent callback function to call for this espconn
  662. * when data is successfully sent
  663. * Returns : none
  664. *******************************************************************************/
  665. sint8 ICACHE_FLASH_ATTR
  666. espconn_regist_sentcb(struct espconn *espconn, espconn_sent_callback sent_cb)
  667. {
  668. if (espconn == NULL) {
  669. return ESPCONN_ARG;
  670. }
  671. espconn ->sent_callback = sent_cb;
  672. return ESPCONN_OK;
  673. }
  674. /******************************************************************************
  675. * FunctionName : espconn_regist_sentcb
  676. * Description : Used to specify the function that should be called when data
  677. * has been successfully delivered to the remote host.
  678. * Parameters : espconn -- espconn to set the sent callback
  679. * sent_cb -- sent callback function to call for this espconn
  680. * when data is successfully sent
  681. * Returns : none
  682. *******************************************************************************/
  683. sint8 ICACHE_FLASH_ATTR
  684. espconn_regist_write_finish(struct espconn *espconn, espconn_connect_callback write_finish_fn)
  685. {
  686. if (espconn == NULL || espconn ->proto.tcp == NULL || espconn->type == ESPCONN_UDP) {
  687. return ESPCONN_ARG;
  688. }
  689. espconn ->proto.tcp->write_finish_fn = write_finish_fn;
  690. return ESPCONN_OK;
  691. }
  692. /******************************************************************************
  693. * FunctionName : espconn_regist_connectcb
  694. * Description : used to specify the function that should be called when
  695. * connects to host.
  696. * Parameters : espconn -- espconn to set the connect callback
  697. * connect_cb -- connected callback function to call when connected
  698. * Returns : none
  699. *******************************************************************************/
  700. sint8 ICACHE_FLASH_ATTR
  701. espconn_regist_connectcb(struct espconn *espconn, espconn_connect_callback connect_cb)
  702. {
  703. if (espconn == NULL) {
  704. return ESPCONN_ARG;
  705. }
  706. espconn->proto.tcp->connect_callback = connect_cb;
  707. return ESPCONN_OK;
  708. }
  709. /******************************************************************************
  710. * FunctionName : espconn_regist_recvcb
  711. * Description : used to specify the function that should be called when recv
  712. * data from host.
  713. * Parameters : espconn -- espconn to set the recv callback
  714. * recv_cb -- recv callback function to call when recv data
  715. * Returns : none
  716. *******************************************************************************/
  717. sint8 ICACHE_FLASH_ATTR
  718. espconn_regist_recvcb(struct espconn *espconn, espconn_recv_callback recv_cb)
  719. {
  720. if (espconn == NULL) {
  721. return ESPCONN_ARG;
  722. }
  723. espconn ->recv_callback = recv_cb;
  724. return ESPCONN_OK;
  725. }
  726. /******************************************************************************
  727. * FunctionName : espconn_regist_reconcb
  728. * Description : used to specify the function that should be called when connection
  729. * because of err disconnect.
  730. * Parameters : espconn -- espconn to set the err callback
  731. * recon_cb -- err callback function to call when err
  732. * Returns : none
  733. *******************************************************************************/
  734. sint8 ICACHE_FLASH_ATTR
  735. espconn_regist_reconcb(struct espconn *espconn, espconn_reconnect_callback recon_cb)
  736. {
  737. if (espconn == NULL) {
  738. return ESPCONN_ARG;
  739. }
  740. espconn ->proto.tcp->reconnect_callback = recon_cb;
  741. return ESPCONN_OK;
  742. }
  743. /******************************************************************************
  744. * FunctionName : espconn_regist_disconcb
  745. * Description : used to specify the function that should be called when disconnect
  746. * Parameters : espconn -- espconn to set the err callback
  747. * discon_cb -- err callback function to call when err
  748. * Returns : none
  749. *******************************************************************************/
  750. sint8 ICACHE_FLASH_ATTR
  751. espconn_regist_disconcb(struct espconn *espconn, espconn_connect_callback discon_cb)
  752. {
  753. if (espconn == NULL) {
  754. return ESPCONN_ARG;
  755. }
  756. espconn ->proto.tcp->disconnect_callback = discon_cb;
  757. return ESPCONN_OK;
  758. }
  759. /******************************************************************************
  760. * FunctionName : espconn_get_connection_info
  761. * Description : used to specify the function that should be called when disconnect
  762. * Parameters : espconn -- espconn to set the err callback
  763. * discon_cb -- err callback function to call when err
  764. * Returns : none
  765. *******************************************************************************/
  766. sint8 ICACHE_FLASH_ATTR
  767. espconn_get_connection_info(struct espconn *pespconn, remot_info **pcon_info, uint8 typeflags)
  768. {
  769. espconn_msg *plist = NULL;
  770. if (pespconn == NULL)
  771. return ESPCONN_ARG;
  772. os_memset(premot, 0, sizeof(premot));
  773. pespconn->link_cnt = 0;
  774. plist = plink_active;
  775. switch (pespconn->type){
  776. case ESPCONN_TCP:
  777. while(plist != NULL){
  778. if (plist->preverse == pespconn){
  779. premot[pespconn->link_cnt].state = plist->pespconn->state;
  780. premot[pespconn->link_cnt].remote_port = plist->pcommon.remote_port;
  781. os_memcpy(premot[pespconn->link_cnt].remote_ip, plist->pcommon.remote_ip, 4);
  782. pespconn->link_cnt ++;
  783. }
  784. plist = plist->pnext;
  785. }
  786. break;
  787. case ESPCONN_UDP:
  788. while(plist != NULL){
  789. if (plist->pespconn == pespconn){
  790. premot[pespconn->link_cnt].state = plist->pespconn->state;
  791. premot[pespconn->link_cnt].remote_port = plist->pcommon.remote_port;
  792. os_memcpy(premot[pespconn->link_cnt].remote_ip, plist->pcommon.remote_ip, 4);
  793. pespconn->link_cnt ++;
  794. }
  795. plist = plist->pnext;
  796. }
  797. break;
  798. default:
  799. break;
  800. }
  801. *pcon_info = premot;
  802. if (pespconn->link_cnt == 0)
  803. return ESPCONN_ARG;
  804. return ESPCONN_OK;
  805. }
  806. /******************************************************************************
  807. * FunctionName : espconn_accept
  808. * Description : The function given as the listen
  809. * Parameters : espconn -- the espconn used to listen the connection
  810. * Returns :
  811. *******************************************************************************/
  812. sint8 ICACHE_FLASH_ATTR
  813. espconn_accept(struct espconn *espconn)
  814. {
  815. sint8 value = ESPCONN_OK;
  816. espconn_msg *plist = NULL;
  817. if (espconn == NULL) {
  818. return ESPCONN_ARG;
  819. } else if (espconn ->type != ESPCONN_TCP)
  820. return ESPCONN_ARG;
  821. /*check the active node information whether is the same as the entity or not*/
  822. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  823. if (plist->pespconn && plist->pespconn->type == ESPCONN_TCP){
  824. if (espconn->proto.tcp->local_port == plist->pespconn->proto.tcp->local_port){
  825. return ESPCONN_ISCONN;
  826. }
  827. }
  828. }
  829. value = espconn_tcp_server(espconn);
  830. return value;
  831. }
  832. /******************************************************************************
  833. * FunctionName : espconn_regist_time
  834. * Description : used to specify the time that should be called when don't recv data
  835. * Parameters : espconn -- the espconn used to the connection
  836. * interval -- the timer when don't recv data
  837. * Returns : none
  838. *******************************************************************************/
  839. sint8 ICACHE_FLASH_ATTR espconn_regist_time(struct espconn *espconn, uint32 interval, uint8 type_flag)
  840. {
  841. espconn_msg *pnode = NULL;
  842. espconn_msg *ptime_msg = NULL;
  843. bool value = false;
  844. if ((espconn == NULL) || (type_flag > 0x01))
  845. return ESPCONN_ARG;
  846. if (type_flag == 0x01){
  847. /*set the timeout time for one active connection of the server*/
  848. value = espconn_find_connection(espconn, &pnode);
  849. if (value){
  850. pnode->pcommon.timeout = interval;
  851. return ESPCONN_OK;
  852. } else
  853. return ESPCONN_ARG;
  854. } else {
  855. /*set the timeout time for all active connection of the server*/
  856. ptime_msg = pserver_list;
  857. while (ptime_msg != NULL){
  858. if (ptime_msg->pespconn == espconn){
  859. ptime_msg->pcommon.timeout = interval;
  860. return ESPCONN_OK;
  861. }
  862. ptime_msg = ptime_msg->pnext;
  863. }
  864. }
  865. return ESPCONN_ARG;
  866. }
  867. /******************************************************************************
  868. * FunctionName : espconn_disconnect
  869. * Description : disconnect with host
  870. * Parameters : espconn -- the espconn used to disconnect the connection
  871. * Returns : none
  872. *******************************************************************************/
  873. sint8 ICACHE_FLASH_ATTR
  874. espconn_disconnect(struct espconn *espconn)
  875. {
  876. espconn_msg *pnode = NULL;
  877. bool value = false;
  878. if (espconn == NULL) {
  879. return ESPCONN_ARG;;
  880. } else if (espconn ->type != ESPCONN_TCP)
  881. return ESPCONN_ARG;
  882. /*Find the node depend on the espconn message*/
  883. value = espconn_find_connection(espconn, &pnode);
  884. if (value){
  885. /*protect for redisconnection*/
  886. if (pnode->preverse == NULL && espconn->state == ESPCONN_CLOSE)
  887. return ESPCONN_INPROGRESS;
  888. espconn_tcp_disconnect(pnode,0); //1 force, 0 normal
  889. return ESPCONN_OK;
  890. } else
  891. return ESPCONN_ARG;
  892. }
  893. /******************************************************************************
  894. * FunctionName : espconn_abort
  895. * Description : Forcely abort with host
  896. * Parameters : espconn -- the espconn used to disconnect the connection
  897. * Returns : none
  898. *******************************************************************************/
  899. sint8 ICACHE_FLASH_ATTR
  900. espconn_abort(struct espconn *espconn)
  901. {
  902. espconn_msg *pnode = NULL;
  903. bool value = false;
  904. if (espconn == NULL) {
  905. return ESPCONN_ARG;;
  906. } else if (espconn ->type != ESPCONN_TCP)
  907. return ESPCONN_ARG;
  908. /*Find the node depend on the espconn message*/
  909. value = espconn_find_connection(espconn, &pnode);
  910. if (value){
  911. /*protect for redisconnection*/
  912. if (espconn->state == ESPCONN_CLOSE)
  913. return ESPCONN_INPROGRESS;
  914. espconn_tcp_disconnect(pnode,1); //1 force, 0 normal
  915. return ESPCONN_OK;
  916. } else
  917. return ESPCONN_ARG;
  918. }
  919. /******************************************************************************
  920. * FunctionName : espconn_get_packet_info
  921. * Description : get the packet info with host
  922. * Parameters : espconn -- the espconn used to disconnect the connection
  923. * infoarg -- the packet info
  924. * Returns : the errur code
  925. *******************************************************************************/
  926. sint8 ICACHE_FLASH_ATTR
  927. espconn_get_packet_info(struct espconn *espconn, struct espconn_packet* infoarg)
  928. {
  929. espconn_msg *pnode = NULL;
  930. err_t err;
  931. bool value = false;
  932. if (espconn == NULL || infoarg == NULL) {
  933. return ESPCONN_ARG;;
  934. } else if (espconn->type != ESPCONN_TCP)
  935. return ESPCONN_ARG;
  936. /*Find the node depend on the espconn message*/
  937. value = espconn_find_connection(espconn, &pnode);
  938. if (value) {
  939. struct tcp_pcb *pcb = pnode->pcommon.pcb;
  940. if (pcb == NULL)
  941. return ESPCONN_ARG;
  942. pnode->pcommon.packet_info.packseq_nxt = pcb->rcv_nxt;
  943. pnode->pcommon.packet_info.packseqno = pcb->snd_nxt;
  944. pnode->pcommon.packet_info.snd_buf_size = pcb->snd_buf;
  945. pnode->pcommon.packet_info.total_queuelen = TCP_SND_QUEUELEN;
  946. pnode->pcommon.packet_info.snd_queuelen = pnode->pcommon.packet_info.total_queuelen - pcb->snd_queuelen;
  947. os_memcpy(infoarg,(void*)&pnode->pcommon.packet_info, sizeof(struct espconn_packet));
  948. return ESPCONN_OK;
  949. } else {
  950. switch (espconn->state){
  951. case ESPCONN_CLOSE:
  952. os_memcpy(infoarg,(void*)&pktinfo[0], sizeof(struct espconn_packet));
  953. err = ESPCONN_OK;
  954. break;
  955. case ESPCONN_NONE:
  956. os_memcpy(infoarg,(void*)&pktinfo[1], sizeof(struct espconn_packet));
  957. err = ESPCONN_OK;
  958. break;
  959. default:
  960. err = ESPCONN_ARG;
  961. break;
  962. }
  963. return err;
  964. }
  965. }
  966. /******************************************************************************
  967. * FunctionName : espconn_set_opt
  968. * Description : set the option for connections so that we don't end up bouncing
  969. * all connections at the same time .
  970. * Parameters : espconn -- the espconn used to set the connection
  971. * opt -- the option for set
  972. * Returns : the result
  973. *******************************************************************************/
  974. sint8 ICACHE_FLASH_ATTR
  975. espconn_set_opt(struct espconn *espconn, uint8 opt)
  976. {
  977. espconn_msg *pnode = NULL;
  978. struct tcp_pcb *tpcb;
  979. bool value = false;
  980. if (espconn == NULL) {
  981. return ESPCONN_ARG;;
  982. } else if (espconn->type != ESPCONN_TCP)
  983. return ESPCONN_ARG;
  984. /*Find the node depend on the espconn message*/
  985. value = espconn_find_connection(espconn, &pnode);
  986. if (value) {
  987. pnode->pcommon.espconn_opt |= opt;
  988. tpcb = pnode->pcommon.pcb;
  989. if (espconn_delay_disabled(pnode))
  990. tcp_nagle_disable(tpcb);
  991. if (espconn_keepalive_disabled(pnode))
  992. espconn_keepalive_enable(tpcb);
  993. return ESPCONN_OK;
  994. } else
  995. return ESPCONN_ARG;
  996. }
  997. /******************************************************************************
  998. * FunctionName : espconn_clear_opt
  999. * Description : clear the option for connections so that we don't end up bouncing
  1000. * all connections at the same time .
  1001. * Parameters : espconn -- the espconn used to set the connection
  1002. * opt -- the option for clear
  1003. * Returns : the result
  1004. *******************************************************************************/
  1005. sint8 ICACHE_FLASH_ATTR
  1006. espconn_clear_opt(struct espconn *espconn, uint8 opt)
  1007. {
  1008. espconn_msg *pnode = NULL;
  1009. struct tcp_pcb *tpcb;
  1010. bool value = false;
  1011. if (espconn == NULL) {
  1012. return ESPCONN_ARG;;
  1013. } else if (espconn->type != ESPCONN_TCP)
  1014. return ESPCONN_ARG;
  1015. /*Find the node depend on the espconn message*/
  1016. value = espconn_find_connection(espconn, &pnode);
  1017. if (value) {
  1018. pnode->pcommon.espconn_opt &= ~opt;
  1019. tpcb = pnode->pcommon.pcb;
  1020. if (espconn_keepalive_enabled(pnode))
  1021. espconn_keepalive_disable(tpcb);
  1022. if (espconn_delay_enabled(pnode))
  1023. tcp_nagle_enable(tpcb);
  1024. return ESPCONN_OK;
  1025. } else
  1026. return ESPCONN_ARG;
  1027. }
  1028. /******************************************************************************
  1029. * FunctionName : espconn_set_keepalive
  1030. * Description : access level value for connection so that we set the value for
  1031. * keep alive
  1032. * Parameters : espconn -- the espconn used to set the connection
  1033. * level -- the connection's level
  1034. * value -- the value of time(s)
  1035. * Returns : access port value
  1036. *******************************************************************************/
  1037. sint8 ICACHE_FLASH_ATTR espconn_set_keepalive(struct espconn *espconn, uint8 level, void* optarg)
  1038. {
  1039. espconn_msg *pnode = NULL;
  1040. bool value = false;
  1041. sint8 ret = ESPCONN_OK;
  1042. if (espconn == NULL || optarg == NULL) {
  1043. return ESPCONN_ARG;;
  1044. } else if (espconn->type != ESPCONN_TCP)
  1045. return ESPCONN_ARG;
  1046. /*Find the node depend on the espconn message*/
  1047. value = espconn_find_connection(espconn, &pnode);
  1048. if (value && espconn_keepalive_disabled(pnode)) {
  1049. struct tcp_pcb *pcb = pnode->pcommon.pcb;
  1050. switch (level){
  1051. case ESPCONN_KEEPIDLE:
  1052. pcb->keep_idle = 1000 * (u32_t)(*(int*)optarg);
  1053. ret = ESPCONN_OK;
  1054. break;
  1055. case ESPCONN_KEEPINTVL:
  1056. pcb->keep_intvl = 1000 * (u32_t)(*(int*)optarg);
  1057. ret = ESPCONN_OK;
  1058. break;
  1059. case ESPCONN_KEEPCNT:
  1060. pcb->keep_cnt = (u32_t)(*(int*)optarg);
  1061. ret = ESPCONN_OK;
  1062. break;
  1063. default:
  1064. ret = ESPCONN_ARG;
  1065. break;
  1066. }
  1067. return ret;
  1068. } else
  1069. return ESPCONN_ARG;
  1070. }
  1071. /******************************************************************************
  1072. * FunctionName : espconn_get_keepalive
  1073. * Description : access level value for connection so that we get the value for
  1074. * keep alive
  1075. * Parameters : espconn -- the espconn used to get the connection
  1076. * level -- the connection's level
  1077. * Returns : access keep alive value
  1078. *******************************************************************************/
  1079. sint8 ICACHE_FLASH_ATTR espconn_get_keepalive(struct espconn *espconn, uint8 level, void *optarg)
  1080. {
  1081. espconn_msg *pnode = NULL;
  1082. bool value = false;
  1083. sint8 ret = ESPCONN_OK;
  1084. if (espconn == NULL || optarg == NULL) {
  1085. return ESPCONN_ARG;;
  1086. } else if (espconn->type != ESPCONN_TCP)
  1087. return ESPCONN_ARG;
  1088. /*Find the node depend on the espconn message*/
  1089. value = espconn_find_connection(espconn, &pnode);
  1090. if (value && espconn_keepalive_disabled(pnode)) {
  1091. struct tcp_pcb *pcb = pnode->pcommon.pcb;
  1092. switch (level) {
  1093. case ESPCONN_KEEPIDLE:
  1094. *(int*)optarg = (int)(pcb->keep_idle/1000);
  1095. ret = ESPCONN_OK;
  1096. break;
  1097. case ESPCONN_KEEPINTVL:
  1098. *(int*)optarg = (int)(pcb->keep_intvl/1000);
  1099. ret = ESPCONN_OK;
  1100. break;
  1101. case ESPCONN_KEEPCNT:
  1102. *(int*)optarg = (int)(pcb->keep_cnt);
  1103. ret = ESPCONN_OK;
  1104. break;
  1105. default:
  1106. ret = ESPCONN_ARG;
  1107. break;
  1108. }
  1109. return ret;
  1110. } else
  1111. return ESPCONN_ARG;
  1112. }
  1113. /******************************************************************************
  1114. * FunctionName : espconn_delete
  1115. * Description : disconnect with host
  1116. * Parameters : espconn -- the espconn used to disconnect the connection
  1117. * Returns : none
  1118. *******************************************************************************/
  1119. sint8 ICACHE_FLASH_ATTR
  1120. espconn_delete(struct espconn *espconn)
  1121. {
  1122. espconn_msg *pnode = NULL;
  1123. bool value = false;
  1124. if (espconn == NULL) {
  1125. return ESPCONN_ARG;
  1126. } else if (espconn ->type != ESPCONN_UDP)
  1127. return espconn_tcp_delete(espconn);
  1128. /*Find the node depend on the espconn message*/
  1129. value = espconn_find_connection(espconn, &pnode);
  1130. if (value){
  1131. espconn_udp_disconnect(pnode);
  1132. return ESPCONN_OK;
  1133. } else
  1134. return ESPCONN_ARG;
  1135. }
  1136. /******************************************************************************
  1137. * FunctionName : espconn_port
  1138. * Description : access port value for client so that we don't end up bouncing
  1139. * all connections at the same time .
  1140. * Parameters : none
  1141. * Returns : access port value
  1142. *******************************************************************************/
  1143. uint32 ICACHE_FLASH_ATTR
  1144. espconn_port(void)
  1145. {
  1146. uint32 port = 0;
  1147. static uint32 randnum = 0;
  1148. do {
  1149. port = os_random();
  1150. if (port < 0) {
  1151. port = os_random() - port;
  1152. }
  1153. port %= 0xc350;
  1154. if (port < 0x400) {
  1155. port += 0x400;
  1156. }
  1157. } while (port == randnum);
  1158. randnum = port;
  1159. return port;
  1160. }
  1161. /******************************************************************************
  1162. * FunctionName : espconn_gethostbyname
  1163. * Description : Resolve a hostname (string) into an IP address.
  1164. * Parameters : pespconn -- espconn to resolve a hostname
  1165. * hostname -- the hostname that is to be queried
  1166. * addr -- pointer to a ip_addr_t where to store the address if
  1167. * it is already cached in the dns_table (only valid if
  1168. * ESPCONN_OK is returned!)
  1169. * found -- a callback function to be called on success, failure
  1170. * or timeout (only if ERR_INPROGRESS is returned!)
  1171. * Returns : err_t return code
  1172. * - ESPCONN_OK if hostname is a valid IP address string or the host
  1173. * name is already in the local names table.
  1174. * - ESPCONN_INPROGRESS enqueue a request to be sent to the DNS server
  1175. * for resolution if no errors are present.
  1176. * - ESPCONN_ARG: dns client not initialized or invalid hostname
  1177. *******************************************************************************/
  1178. err_t ICACHE_FLASH_ATTR
  1179. espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t *addr, dns_found_callback found)
  1180. {
  1181. return dns_gethostbyname(hostname, addr, found, pespconn);
  1182. }
  1183. /******************************************************************************
  1184. * FunctionName : espconn_dns_setserver
  1185. * Description : Initialize one of the DNS servers.
  1186. * Parameters : numdns -- the index of the DNS server to set must
  1187. * be < DNS_MAX_SERVERS = 2
  1188. * dnsserver -- IP address of the DNS server to set
  1189. * Returns : none
  1190. *******************************************************************************/
  1191. void ICACHE_FLASH_ATTR
  1192. espconn_dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
  1193. {
  1194. dns_setserver(numdns,dnsserver);
  1195. }