espconn.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. if (espconn == NULL || mem == NULL || len == 0)
  402. return ESPCONN_ARG;
  403. /*Find the node depend on the espconn message*/
  404. value = espconn_find_connection(espconn, &pnode);
  405. if (value && espconn->type == ESPCONN_TCP){
  406. if (pnode->readbuf != NULL){
  407. bytes_used = ringbuf_bytes_used(pnode->readbuf);
  408. if (bytes_used != 0) {
  409. if (len > bytes_used) {
  410. len = bytes_used;
  411. }
  412. ringbuf_memcpy_from(mem, pnode->readbuf, len);
  413. espconn_recv_unhold(pnode->pespconn);
  414. return len;
  415. } else {
  416. return ESPCONN_OK;
  417. }
  418. } else{
  419. return ESPCONN_OK;
  420. }
  421. } else{
  422. return ESPCONN_ARG;
  423. }
  424. return ESPCONN_ARG;
  425. }
  426. /******************************************************************************
  427. * FunctionName : espconn_sendto
  428. * Description : send data for UDP
  429. * Parameters : espconn -- espconn to set for UDP
  430. * psent -- data to send
  431. * length -- length of data to send
  432. * Returns : error
  433. *******************************************************************************/
  434. sint16 ICACHE_FLASH_ATTR
  435. espconn_sendto(struct espconn *espconn, uint8 *psent, uint16 length)
  436. {
  437. espconn_msg *pnode = NULL;
  438. bool value = false;
  439. err_t error = ESPCONN_OK;
  440. if (espconn == NULL || psent == NULL || length == 0) {
  441. return ESPCONN_ARG;
  442. }
  443. /*Find the node depend on the espconn message*/
  444. value = espconn_find_connection(espconn, &pnode);
  445. if (value && espconn->type == ESPCONN_UDP)
  446. return espconn_udp_sendto(pnode, psent, length);
  447. else
  448. return ESPCONN_ARG;
  449. }
  450. /******************************************************************************
  451. * FunctionName : espconn_send
  452. * Description : sent data for client or server
  453. * Parameters : espconn -- espconn to set for client or server
  454. * psent -- data to send
  455. * length -- length of data to send
  456. * Returns : none
  457. *******************************************************************************/
  458. sint8 espconn_send(struct espconn *espconn, uint8 *psent, uint16 length) __attribute__((alias("espconn_sent")));
  459. /******************************************************************************
  460. * FunctionName : espconn_tcp_get_wnd
  461. * Description : get the window size of simulatenously active TCP connections
  462. * Parameters : none
  463. * Returns : the number of TCP_MSS active TCP connections
  464. *******************************************************************************/
  465. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_wnd(void)
  466. {
  467. uint8 tcp_num = 0;
  468. tcp_num = (TCP_WND / TCP_MSS);
  469. return tcp_num;
  470. }
  471. /******************************************************************************
  472. * FunctionName : espconn_tcp_set_max_con
  473. * Description : set the window size simulatenously active TCP connections
  474. * Parameters : num -- the number of TCP_MSS
  475. * Returns : ESPCONN_ARG -- Illegal argument
  476. * ESPCONN_OK -- No error
  477. *******************************************************************************/
  478. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_wnd(uint8 num)
  479. {
  480. if (num == 0 || num > linkMax)
  481. return ESPCONN_ARG;
  482. TCP_WND = (num * TCP_MSS);
  483. return ESPCONN_OK;
  484. }
  485. /******************************************************************************
  486. * FunctionName : espconn_tcp_get_mss
  487. * Description : get the mss size of simulatenously active TCP connections
  488. * Parameters : none
  489. * Returns : the size of TCP_MSS active TCP connections
  490. *******************************************************************************/
  491. uint16 ICACHE_FLASH_ATTR espconn_tcp_get_mss(void)
  492. {
  493. uint16 tcp_num = 0;
  494. tcp_num = TCP_MSS;
  495. return tcp_num;
  496. }
  497. /******************************************************************************
  498. * FunctionName : espconn_tcp_get_max_con
  499. * Description : get the number of simulatenously active TCP connections
  500. * Parameters : espconn -- espconn to set the connect callback
  501. * Returns : none
  502. *******************************************************************************/
  503. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con(void)
  504. {
  505. uint8 tcp_num = 0;
  506. tcp_num = MEMP_NUM_TCP_PCB;
  507. return tcp_num;
  508. }
  509. /******************************************************************************
  510. * FunctionName : espconn_tcp_set_max_con
  511. * Description : set the number of simulatenously active TCP connections
  512. * Parameters : espconn -- espconn to set the connect callback
  513. * Returns : none
  514. *******************************************************************************/
  515. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_con(uint8 num)
  516. {
  517. if (num == 0 || num > linkMax)
  518. return ESPCONN_ARG;
  519. MEMP_NUM_TCP_PCB = num;
  520. return ESPCONN_OK;
  521. }
  522. /******************************************************************************
  523. * FunctionName : espconn_tcp_get_max_retran
  524. * Description : get the Maximum number of retransmissions of data active TCP connections
  525. * Parameters : none
  526. * Returns : the Maximum number of retransmissions
  527. *******************************************************************************/
  528. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_retran(void)
  529. {
  530. uint8 tcp_num = 0;
  531. tcp_num = TCP_MAXRTX;
  532. return tcp_num;
  533. }
  534. /******************************************************************************
  535. * FunctionName : espconn_tcp_set_max_retran
  536. * Description : set the Maximum number of retransmissions of data active TCP connections
  537. * Parameters : num -- the Maximum number of retransmissions
  538. * Returns : result
  539. *******************************************************************************/
  540. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_retran(uint8 num)
  541. {
  542. if (num == 0 || num > 12)
  543. return ESPCONN_ARG;
  544. TCP_MAXRTX = num;
  545. return ESPCONN_OK;
  546. }
  547. /******************************************************************************
  548. * FunctionName : espconn_tcp_get_max_syn
  549. * Description : get the Maximum number of retransmissions of SYN segments
  550. * Parameters : none
  551. * Returns : the Maximum number of retransmissions
  552. *******************************************************************************/
  553. uint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_syn(void)
  554. {
  555. uint8 tcp_num = 0;
  556. tcp_num = TCP_SYNMAXRTX;
  557. return tcp_num;
  558. }
  559. /******************************************************************************
  560. * FunctionName : espconn_tcp_set_max_syn
  561. * Description : set the Maximum number of retransmissions of SYN segments
  562. * Parameters : num -- the Maximum number of retransmissions
  563. * Returns : result
  564. *******************************************************************************/
  565. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_syn(uint8 num)
  566. {
  567. if (num == 0 || num > 12)
  568. return ESPCONN_ARG;
  569. TCP_SYNMAXRTX = num;
  570. return ESPCONN_OK;
  571. }
  572. /******************************************************************************
  573. * FunctionName : espconn_tcp_get_max_con_allow
  574. * Description : get the count of simulatenously active connections on the server
  575. * Parameters : espconn -- espconn to get the count
  576. * Returns : result
  577. *******************************************************************************/
  578. sint8 ICACHE_FLASH_ATTR espconn_tcp_get_max_con_allow(struct espconn *espconn)
  579. {
  580. espconn_msg *pget_msg = NULL;
  581. if ((espconn == NULL) || (espconn->type == ESPCONN_UDP))
  582. return ESPCONN_ARG;
  583. pget_msg = pserver_list;
  584. while (pget_msg != NULL){
  585. if (pget_msg->pespconn == espconn){
  586. return pget_msg->count_opt;
  587. }
  588. pget_msg = pget_msg->pnext;
  589. }
  590. return ESPCONN_ARG;
  591. }
  592. /******************************************************************************
  593. * FunctionName : espconn_tcp_set_max_con_allow
  594. * Description : set the count of simulatenously active connections on the server
  595. * Parameters : espconn -- espconn to set the count
  596. * Returns : result
  597. *******************************************************************************/
  598. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_max_con_allow(struct espconn *espconn, uint8 num)
  599. {
  600. espconn_msg *pset_msg = NULL;
  601. if ((espconn == NULL) || (num > MEMP_NUM_TCP_PCB) || (espconn->type == ESPCONN_UDP))
  602. return ESPCONN_ARG;
  603. pset_msg = pserver_list;
  604. while (pset_msg != NULL){
  605. if (pset_msg->pespconn == espconn){
  606. pset_msg->count_opt = num;
  607. return ESPCONN_OK;
  608. }
  609. pset_msg = pset_msg->pnext;
  610. }
  611. return ESPCONN_ARG;
  612. }
  613. /******************************************************************************
  614. * FunctionName : espconn_tcp_set_buf_count
  615. * Description : set the total number of espconn_buf on the unsent lists for one
  616. * activate connection
  617. * Parameters : espconn -- espconn to set the count
  618. * num -- the total number of espconn_buf
  619. * Returns : result
  620. *******************************************************************************/
  621. sint8 ICACHE_FLASH_ATTR espconn_tcp_set_buf_count(struct espconn *espconn, uint8 num)
  622. {
  623. espconn_msg *plist = NULL;
  624. if (espconn == NULL || (num > TCP_SND_QUEUELEN))
  625. return ESPCONN_ARG;
  626. /*find the node from the active connection list*/
  627. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  628. if (plist->pespconn && plist->pespconn == espconn && espconn->type == ESPCONN_TCP){
  629. plist->pcommon.pbuf_num = num;
  630. return ESPCONN_OK;
  631. }
  632. }
  633. if (plist == NULL)
  634. return ESPCONN_ARG;
  635. }
  636. /******************************************************************************
  637. * FunctionName : espconn_tcp_get_buf_count
  638. * Description : get the count of the current node which has espconn_buf
  639. * Parameters : pesp_buf -- the list head of espconn_buf type
  640. * Returns : the count of the current node which has espconn_buf
  641. *******************************************************************************/
  642. static uint8 ICACHE_FLASH_ATTR espconn_tcp_get_buf_count(espconn_buf *pesp_buf)
  643. {
  644. espconn_buf *pbuf_list = pesp_buf;
  645. uint8 pbuf_num = 0;
  646. /*polling the list get the count of the current node*/
  647. while (pbuf_list != NULL){
  648. pbuf_list = pbuf_list->pnext;
  649. pbuf_num ++;
  650. }
  651. return pbuf_num;
  652. }
  653. /******************************************************************************
  654. * FunctionName : espconn_regist_sentcb
  655. * Description : Used to specify the function that should be called when data
  656. * has been successfully delivered to the remote host.
  657. * Parameters : espconn -- espconn to set the sent callback
  658. * sent_cb -- sent callback function to call for this espconn
  659. * when data is successfully sent
  660. * Returns : none
  661. *******************************************************************************/
  662. sint8 ICACHE_FLASH_ATTR
  663. espconn_regist_sentcb(struct espconn *espconn, espconn_sent_callback sent_cb)
  664. {
  665. if (espconn == NULL) {
  666. return ESPCONN_ARG;
  667. }
  668. espconn ->sent_callback = sent_cb;
  669. return ESPCONN_OK;
  670. }
  671. /******************************************************************************
  672. * FunctionName : espconn_regist_sentcb
  673. * Description : Used to specify the function that should be called when data
  674. * has been successfully delivered to the remote host.
  675. * Parameters : espconn -- espconn to set the sent callback
  676. * sent_cb -- sent callback function to call for this espconn
  677. * when data is successfully sent
  678. * Returns : none
  679. *******************************************************************************/
  680. sint8 ICACHE_FLASH_ATTR
  681. espconn_regist_write_finish(struct espconn *espconn, espconn_connect_callback write_finish_fn)
  682. {
  683. if (espconn == NULL || espconn ->proto.tcp == NULL || espconn->type == ESPCONN_UDP) {
  684. return ESPCONN_ARG;
  685. }
  686. espconn ->proto.tcp->write_finish_fn = write_finish_fn;
  687. return ESPCONN_OK;
  688. }
  689. /******************************************************************************
  690. * FunctionName : espconn_regist_connectcb
  691. * Description : used to specify the function that should be called when
  692. * connects to host.
  693. * Parameters : espconn -- espconn to set the connect callback
  694. * connect_cb -- connected callback function to call when connected
  695. * Returns : none
  696. *******************************************************************************/
  697. sint8 ICACHE_FLASH_ATTR
  698. espconn_regist_connectcb(struct espconn *espconn, espconn_connect_callback connect_cb)
  699. {
  700. if (espconn == NULL) {
  701. return ESPCONN_ARG;
  702. }
  703. espconn->proto.tcp->connect_callback = connect_cb;
  704. return ESPCONN_OK;
  705. }
  706. /******************************************************************************
  707. * FunctionName : espconn_regist_recvcb
  708. * Description : used to specify the function that should be called when recv
  709. * data from host.
  710. * Parameters : espconn -- espconn to set the recv callback
  711. * recv_cb -- recv callback function to call when recv data
  712. * Returns : none
  713. *******************************************************************************/
  714. sint8 ICACHE_FLASH_ATTR
  715. espconn_regist_recvcb(struct espconn *espconn, espconn_recv_callback recv_cb)
  716. {
  717. if (espconn == NULL) {
  718. return ESPCONN_ARG;
  719. }
  720. espconn ->recv_callback = recv_cb;
  721. return ESPCONN_OK;
  722. }
  723. /******************************************************************************
  724. * FunctionName : espconn_regist_reconcb
  725. * Description : used to specify the function that should be called when connection
  726. * because of err disconnect.
  727. * Parameters : espconn -- espconn to set the err callback
  728. * recon_cb -- err callback function to call when err
  729. * Returns : none
  730. *******************************************************************************/
  731. sint8 ICACHE_FLASH_ATTR
  732. espconn_regist_reconcb(struct espconn *espconn, espconn_reconnect_callback recon_cb)
  733. {
  734. if (espconn == NULL) {
  735. return ESPCONN_ARG;
  736. }
  737. espconn ->proto.tcp->reconnect_callback = recon_cb;
  738. return ESPCONN_OK;
  739. }
  740. /******************************************************************************
  741. * FunctionName : espconn_regist_disconcb
  742. * Description : used to specify the function that should be called when disconnect
  743. * Parameters : espconn -- espconn to set the err callback
  744. * discon_cb -- err callback function to call when err
  745. * Returns : none
  746. *******************************************************************************/
  747. sint8 ICACHE_FLASH_ATTR
  748. espconn_regist_disconcb(struct espconn *espconn, espconn_connect_callback discon_cb)
  749. {
  750. if (espconn == NULL) {
  751. return ESPCONN_ARG;
  752. }
  753. espconn ->proto.tcp->disconnect_callback = discon_cb;
  754. return ESPCONN_OK;
  755. }
  756. /******************************************************************************
  757. * FunctionName : espconn_get_connection_info
  758. * Description : used to specify the function that should be called when disconnect
  759. * Parameters : espconn -- espconn to set the err callback
  760. * discon_cb -- err callback function to call when err
  761. * Returns : none
  762. *******************************************************************************/
  763. sint8 ICACHE_FLASH_ATTR
  764. espconn_get_connection_info(struct espconn *pespconn, remot_info **pcon_info, uint8 typeflags)
  765. {
  766. espconn_msg *plist = NULL;
  767. if (pespconn == NULL)
  768. return ESPCONN_ARG;
  769. os_memset(premot, 0, sizeof(premot));
  770. pespconn->link_cnt = 0;
  771. plist = plink_active;
  772. switch (pespconn->type){
  773. case ESPCONN_TCP:
  774. while(plist != NULL){
  775. if (plist->preverse == pespconn){
  776. premot[pespconn->link_cnt].state = plist->pespconn->state;
  777. premot[pespconn->link_cnt].remote_port = plist->pcommon.remote_port;
  778. os_memcpy(premot[pespconn->link_cnt].remote_ip, plist->pcommon.remote_ip, 4);
  779. pespconn->link_cnt ++;
  780. }
  781. plist = plist->pnext;
  782. }
  783. break;
  784. case ESPCONN_UDP:
  785. while(plist != NULL){
  786. if (plist->pespconn == pespconn){
  787. premot[pespconn->link_cnt].state = plist->pespconn->state;
  788. premot[pespconn->link_cnt].remote_port = plist->pcommon.remote_port;
  789. os_memcpy(premot[pespconn->link_cnt].remote_ip, plist->pcommon.remote_ip, 4);
  790. pespconn->link_cnt ++;
  791. }
  792. plist = plist->pnext;
  793. }
  794. break;
  795. default:
  796. break;
  797. }
  798. *pcon_info = premot;
  799. if (pespconn->link_cnt == 0)
  800. return ESPCONN_ARG;
  801. return ESPCONN_OK;
  802. }
  803. /******************************************************************************
  804. * FunctionName : espconn_accept
  805. * Description : The function given as the listen
  806. * Parameters : espconn -- the espconn used to listen the connection
  807. * Returns :
  808. *******************************************************************************/
  809. sint8 ICACHE_FLASH_ATTR
  810. espconn_accept(struct espconn *espconn)
  811. {
  812. sint8 value = ESPCONN_OK;
  813. espconn_msg *plist = NULL;
  814. if (espconn == NULL) {
  815. return ESPCONN_ARG;
  816. } else if (espconn ->type != ESPCONN_TCP)
  817. return ESPCONN_ARG;
  818. /*check the active node information whether is the same as the entity or not*/
  819. for (plist = plink_active; plist != NULL; plist = plist->pnext){
  820. if (plist->pespconn && plist->pespconn->type == ESPCONN_TCP){
  821. if (espconn->proto.tcp->local_port == plist->pespconn->proto.tcp->local_port){
  822. return ESPCONN_ISCONN;
  823. }
  824. }
  825. }
  826. value = espconn_tcp_server(espconn);
  827. return value;
  828. }
  829. /******************************************************************************
  830. * FunctionName : espconn_regist_time
  831. * Description : used to specify the time that should be called when don't recv data
  832. * Parameters : espconn -- the espconn used to the connection
  833. * interval -- the timer when don't recv data
  834. * Returns : none
  835. *******************************************************************************/
  836. sint8 ICACHE_FLASH_ATTR espconn_regist_time(struct espconn *espconn, uint32 interval, uint8 type_flag)
  837. {
  838. espconn_msg *pnode = NULL;
  839. espconn_msg *ptime_msg = NULL;
  840. bool value = false;
  841. if ((espconn == NULL) || (type_flag > 0x01))
  842. return ESPCONN_ARG;
  843. if (type_flag == 0x01){
  844. /*set the timeout time for one active connection of the server*/
  845. value = espconn_find_connection(espconn, &pnode);
  846. if (value){
  847. pnode->pcommon.timeout = interval;
  848. return ESPCONN_OK;
  849. } else
  850. return ESPCONN_ARG;
  851. } else {
  852. /*set the timeout time for all active connection of the server*/
  853. ptime_msg = pserver_list;
  854. while (ptime_msg != NULL){
  855. if (ptime_msg->pespconn == espconn){
  856. ptime_msg->pcommon.timeout = interval;
  857. return ESPCONN_OK;
  858. }
  859. ptime_msg = ptime_msg->pnext;
  860. }
  861. }
  862. return ESPCONN_ARG;
  863. }
  864. /******************************************************************************
  865. * FunctionName : espconn_disconnect
  866. * Description : disconnect with host
  867. * Parameters : espconn -- the espconn used to disconnect the connection
  868. * Returns : none
  869. *******************************************************************************/
  870. sint8 ICACHE_FLASH_ATTR
  871. espconn_disconnect(struct espconn *espconn)
  872. {
  873. espconn_msg *pnode = NULL;
  874. bool value = false;
  875. if (espconn == NULL) {
  876. return ESPCONN_ARG;;
  877. } else if (espconn ->type != ESPCONN_TCP)
  878. return ESPCONN_ARG;
  879. /*Find the node depend on the espconn message*/
  880. value = espconn_find_connection(espconn, &pnode);
  881. if (value){
  882. /*protect for redisconnection*/
  883. if (pnode->preverse == NULL && espconn->state == ESPCONN_CLOSE)
  884. return ESPCONN_INPROGRESS;
  885. espconn_tcp_disconnect(pnode,0); //1 force, 0 normal
  886. return ESPCONN_OK;
  887. } else
  888. return ESPCONN_ARG;
  889. }
  890. /******************************************************************************
  891. * FunctionName : espconn_abort
  892. * Description : Forcely abort with host
  893. * Parameters : espconn -- the espconn used to disconnect the connection
  894. * Returns : none
  895. *******************************************************************************/
  896. sint8 ICACHE_FLASH_ATTR
  897. espconn_abort(struct espconn *espconn)
  898. {
  899. espconn_msg *pnode = NULL;
  900. bool value = false;
  901. if (espconn == NULL) {
  902. return ESPCONN_ARG;;
  903. } else if (espconn ->type != ESPCONN_TCP)
  904. return ESPCONN_ARG;
  905. /*Find the node depend on the espconn message*/
  906. value = espconn_find_connection(espconn, &pnode);
  907. if (value){
  908. /*protect for redisconnection*/
  909. if (espconn->state == ESPCONN_CLOSE)
  910. return ESPCONN_INPROGRESS;
  911. espconn_tcp_disconnect(pnode,1); //1 force, 0 normal
  912. return ESPCONN_OK;
  913. } else
  914. return ESPCONN_ARG;
  915. }
  916. /******************************************************************************
  917. * FunctionName : espconn_get_packet_info
  918. * Description : get the packet info with host
  919. * Parameters : espconn -- the espconn used to disconnect the connection
  920. * infoarg -- the packet info
  921. * Returns : the errur code
  922. *******************************************************************************/
  923. sint8 ICACHE_FLASH_ATTR
  924. espconn_get_packet_info(struct espconn *espconn, struct espconn_packet* infoarg)
  925. {
  926. espconn_msg *pnode = NULL;
  927. err_t err;
  928. bool value = false;
  929. if (espconn == NULL || infoarg == NULL) {
  930. return ESPCONN_ARG;;
  931. } else if (espconn->type != ESPCONN_TCP)
  932. return ESPCONN_ARG;
  933. /*Find the node depend on the espconn message*/
  934. value = espconn_find_connection(espconn, &pnode);
  935. if (value) {
  936. struct tcp_pcb *pcb = pnode->pcommon.pcb;
  937. if (pcb == NULL)
  938. return ESPCONN_ARG;
  939. pnode->pcommon.packet_info.packseq_nxt = pcb->rcv_nxt;
  940. pnode->pcommon.packet_info.packseqno = pcb->snd_nxt;
  941. pnode->pcommon.packet_info.snd_buf_size = pcb->snd_buf;
  942. pnode->pcommon.packet_info.total_queuelen = TCP_SND_QUEUELEN;
  943. pnode->pcommon.packet_info.snd_queuelen = pnode->pcommon.packet_info.total_queuelen - pcb->snd_queuelen;
  944. os_memcpy(infoarg,(void*)&pnode->pcommon.packet_info, sizeof(struct espconn_packet));
  945. return ESPCONN_OK;
  946. } else {
  947. switch (espconn->state){
  948. case ESPCONN_CLOSE:
  949. os_memcpy(infoarg,(void*)&pktinfo[0], sizeof(struct espconn_packet));
  950. err = ESPCONN_OK;
  951. break;
  952. case ESPCONN_NONE:
  953. os_memcpy(infoarg,(void*)&pktinfo[1], sizeof(struct espconn_packet));
  954. err = ESPCONN_OK;
  955. break;
  956. default:
  957. err = ESPCONN_ARG;
  958. break;
  959. }
  960. return err;
  961. }
  962. }
  963. /******************************************************************************
  964. * FunctionName : espconn_set_opt
  965. * Description : set the option for connections so that we don't end up bouncing
  966. * all connections at the same time .
  967. * Parameters : espconn -- the espconn used to set the connection
  968. * opt -- the option for set
  969. * Returns : the result
  970. *******************************************************************************/
  971. sint8 ICACHE_FLASH_ATTR
  972. espconn_set_opt(struct espconn *espconn, uint8 opt)
  973. {
  974. espconn_msg *pnode = NULL;
  975. struct tcp_pcb *tpcb;
  976. bool value = false;
  977. if (espconn == NULL) {
  978. return ESPCONN_ARG;;
  979. } else if (espconn->type != ESPCONN_TCP)
  980. return ESPCONN_ARG;
  981. /*Find the node depend on the espconn message*/
  982. value = espconn_find_connection(espconn, &pnode);
  983. if (value) {
  984. pnode->pcommon.espconn_opt |= opt;
  985. tpcb = pnode->pcommon.pcb;
  986. if (espconn_delay_disabled(pnode))
  987. tcp_nagle_disable(tpcb);
  988. if (espconn_keepalive_disabled(pnode))
  989. espconn_keepalive_enable(tpcb);
  990. return ESPCONN_OK;
  991. } else
  992. return ESPCONN_ARG;
  993. }
  994. /******************************************************************************
  995. * FunctionName : espconn_clear_opt
  996. * Description : clear the option for connections so that we don't end up bouncing
  997. * all connections at the same time .
  998. * Parameters : espconn -- the espconn used to set the connection
  999. * opt -- the option for clear
  1000. * Returns : the result
  1001. *******************************************************************************/
  1002. sint8 ICACHE_FLASH_ATTR
  1003. espconn_clear_opt(struct espconn *espconn, uint8 opt)
  1004. {
  1005. espconn_msg *pnode = NULL;
  1006. struct tcp_pcb *tpcb;
  1007. bool value = false;
  1008. if (espconn == NULL) {
  1009. return ESPCONN_ARG;;
  1010. } else if (espconn->type != ESPCONN_TCP)
  1011. return ESPCONN_ARG;
  1012. /*Find the node depend on the espconn message*/
  1013. value = espconn_find_connection(espconn, &pnode);
  1014. if (value) {
  1015. pnode->pcommon.espconn_opt &= ~opt;
  1016. tpcb = pnode->pcommon.pcb;
  1017. if (espconn_keepalive_enabled(pnode))
  1018. espconn_keepalive_disable(tpcb);
  1019. if (espconn_delay_enabled(pnode))
  1020. tcp_nagle_enable(tpcb);
  1021. return ESPCONN_OK;
  1022. } else
  1023. return ESPCONN_ARG;
  1024. }
  1025. /******************************************************************************
  1026. * FunctionName : espconn_set_keepalive
  1027. * Description : access level value for connection so that we set the value for
  1028. * keep alive
  1029. * Parameters : espconn -- the espconn used to set the connection
  1030. * level -- the connection's level
  1031. * value -- the value of time(s)
  1032. * Returns : access port value
  1033. *******************************************************************************/
  1034. sint8 ICACHE_FLASH_ATTR espconn_set_keepalive(struct espconn *espconn, uint8 level, void* optarg)
  1035. {
  1036. espconn_msg *pnode = NULL;
  1037. bool value = false;
  1038. sint8 ret = ESPCONN_OK;
  1039. if (espconn == NULL || optarg == NULL) {
  1040. return ESPCONN_ARG;;
  1041. } else if (espconn->type != ESPCONN_TCP)
  1042. return ESPCONN_ARG;
  1043. /*Find the node depend on the espconn message*/
  1044. value = espconn_find_connection(espconn, &pnode);
  1045. if (value && espconn_keepalive_disabled(pnode)) {
  1046. struct tcp_pcb *pcb = pnode->pcommon.pcb;
  1047. switch (level){
  1048. case ESPCONN_KEEPIDLE:
  1049. pcb->keep_idle = 1000 * (u32_t)(*(int*)optarg);
  1050. ret = ESPCONN_OK;
  1051. break;
  1052. case ESPCONN_KEEPINTVL:
  1053. pcb->keep_intvl = 1000 * (u32_t)(*(int*)optarg);
  1054. ret = ESPCONN_OK;
  1055. break;
  1056. case ESPCONN_KEEPCNT:
  1057. pcb->keep_cnt = (u32_t)(*(int*)optarg);
  1058. ret = ESPCONN_OK;
  1059. break;
  1060. default:
  1061. ret = ESPCONN_ARG;
  1062. break;
  1063. }
  1064. return ret;
  1065. } else
  1066. return ESPCONN_ARG;
  1067. }
  1068. /******************************************************************************
  1069. * FunctionName : espconn_get_keepalive
  1070. * Description : access level value for connection so that we get the value for
  1071. * keep alive
  1072. * Parameters : espconn -- the espconn used to get the connection
  1073. * level -- the connection's level
  1074. * Returns : access keep alive value
  1075. *******************************************************************************/
  1076. sint8 ICACHE_FLASH_ATTR espconn_get_keepalive(struct espconn *espconn, uint8 level, void *optarg)
  1077. {
  1078. espconn_msg *pnode = NULL;
  1079. bool value = false;
  1080. sint8 ret = ESPCONN_OK;
  1081. if (espconn == NULL || optarg == NULL) {
  1082. return ESPCONN_ARG;;
  1083. } else if (espconn->type != ESPCONN_TCP)
  1084. return ESPCONN_ARG;
  1085. /*Find the node depend on the espconn message*/
  1086. value = espconn_find_connection(espconn, &pnode);
  1087. if (value && espconn_keepalive_disabled(pnode)) {
  1088. struct tcp_pcb *pcb = pnode->pcommon.pcb;
  1089. switch (level) {
  1090. case ESPCONN_KEEPIDLE:
  1091. *(int*)optarg = (int)(pcb->keep_idle/1000);
  1092. ret = ESPCONN_OK;
  1093. break;
  1094. case ESPCONN_KEEPINTVL:
  1095. *(int*)optarg = (int)(pcb->keep_intvl/1000);
  1096. ret = ESPCONN_OK;
  1097. break;
  1098. case ESPCONN_KEEPCNT:
  1099. *(int*)optarg = (int)(pcb->keep_cnt);
  1100. ret = ESPCONN_OK;
  1101. break;
  1102. default:
  1103. ret = ESPCONN_ARG;
  1104. break;
  1105. }
  1106. return ret;
  1107. } else
  1108. return ESPCONN_ARG;
  1109. }
  1110. /******************************************************************************
  1111. * FunctionName : espconn_delete
  1112. * Description : disconnect with host
  1113. * Parameters : espconn -- the espconn used to disconnect the connection
  1114. * Returns : none
  1115. *******************************************************************************/
  1116. sint8 ICACHE_FLASH_ATTR
  1117. espconn_delete(struct espconn *espconn)
  1118. {
  1119. espconn_msg *pnode = NULL;
  1120. bool value = false;
  1121. if (espconn == NULL) {
  1122. return ESPCONN_ARG;
  1123. } else if (espconn ->type != ESPCONN_UDP)
  1124. return espconn_tcp_delete(espconn);
  1125. /*Find the node depend on the espconn message*/
  1126. value = espconn_find_connection(espconn, &pnode);
  1127. if (value){
  1128. espconn_udp_disconnect(pnode);
  1129. return ESPCONN_OK;
  1130. } else
  1131. return ESPCONN_ARG;
  1132. }
  1133. /******************************************************************************
  1134. * FunctionName : espconn_port
  1135. * Description : access port value for client so that we don't end up bouncing
  1136. * all connections at the same time .
  1137. * Parameters : none
  1138. * Returns : access port value
  1139. *******************************************************************************/
  1140. uint32 ICACHE_FLASH_ATTR
  1141. espconn_port(void)
  1142. {
  1143. uint32 port = 0;
  1144. static uint32 randnum = 0;
  1145. do {
  1146. port = os_random();
  1147. if (port < 0) {
  1148. port = os_random() - port;
  1149. }
  1150. port %= 0xc350;
  1151. if (port < 0x400) {
  1152. port += 0x400;
  1153. }
  1154. } while (port == randnum);
  1155. randnum = port;
  1156. return port;
  1157. }
  1158. /******************************************************************************
  1159. * FunctionName : espconn_gethostbyname
  1160. * Description : Resolve a hostname (string) into an IP address.
  1161. * Parameters : pespconn -- espconn to resolve a hostname
  1162. * hostname -- the hostname that is to be queried
  1163. * addr -- pointer to a ip_addr_t where to store the address if
  1164. * it is already cached in the dns_table (only valid if
  1165. * ESPCONN_OK is returned!)
  1166. * found -- a callback function to be called on success, failure
  1167. * or timeout (only if ERR_INPROGRESS is returned!)
  1168. * Returns : err_t return code
  1169. * - ESPCONN_OK if hostname is a valid IP address string or the host
  1170. * name is already in the local names table.
  1171. * - ESPCONN_INPROGRESS enqueue a request to be sent to the DNS server
  1172. * for resolution if no errors are present.
  1173. * - ESPCONN_ARG: dns client not initialized or invalid hostname
  1174. *******************************************************************************/
  1175. err_t ICACHE_FLASH_ATTR
  1176. espconn_gethostbyname(struct espconn *pespconn, const char *hostname, ip_addr_t *addr, dns_found_callback found)
  1177. {
  1178. return dns_gethostbyname(hostname, addr, found, pespconn);
  1179. }
  1180. /******************************************************************************
  1181. * FunctionName : espconn_dns_setserver
  1182. * Description : Initialize one of the DNS servers.
  1183. * Parameters : numdns -- the index of the DNS server to set must
  1184. * be < DNS_MAX_SERVERS = 2
  1185. * dnsserver -- IP address of the DNS server to set
  1186. * Returns : none
  1187. *******************************************************************************/
  1188. void ICACHE_FLASH_ATTR
  1189. espconn_dns_setserver(u8_t numdns, ip_addr_t *dnsserver)
  1190. {
  1191. dns_setserver(numdns,dnsserver);
  1192. }