test_lib.c0000644000175000017500000000700212741702625012376 0ustar sergiosergio#include #include #include #include #include os_timer_t ctimer,dtimer; uint8_t ICACHE_FLASH_ATTR UTILS_StrToIP(const int8_t* str, void *ip) { /* The count of the number of bytes processed. */ int i; /* A pointer to the next digit to process. */ const char * start; start = str; for (i = 0; i < 4; i++) { /* The digit being processed. */ char c; /* The value of this byte. */ int n = 0; while (1) { c = * start; start++; if (c >= '0' && c <= '9') { n *= 10; n += c - '0'; } /* We insist on stopping at "." if we are still parsing the first, second, or third numbers. If we have reached the end of the numbers, we will allow any character. */ else if ((i < 3 && c == '.') || i == 3) { break; } else { return 0; } } if (n >= 256) { return 0; } ((uint8_t*)ip)[i] = n; } return 1; } ICACHE_FLASH_ATTR void timerDisconnect(void* c) { struct espconn * pCon = (struct espconn *) c; os_printf("***timeriDisconnect***\n"); espconn_secure_disconnect(pCon); //espconn_disconnect(pCon); } void ICACHE_FLASH_ATTR tcpclient_discon_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; espconn_delete(pCon); os_free(pCon->proto.tcp); os_free(pCon); pCon = NULL; os_printf("Disconnected\n"); } void ICACHE_FLASH_ATTR tcpclient_connect_cb(void *arg) { struct espconn *pCon = (struct espconn *)arg; os_printf("Connected\n"); os_timer_disarm(&dtimer); os_timer_setfn(&dtimer, (os_timer_func_t *)timerDisconnect,pCon); os_timer_arm(&dtimer, 3000, 0); } void ICACHE_FLASH_ATTR tcpclient_recon_cb(void *arg, sint8 errType) { struct espconn *pCon = (struct espconn *)arg; os_printf("Connection error %d\n",errType); tcpclient_discon_cb(pCon); } ICACHE_FLASH_ATTR void connect(char* host,int port) { struct espconn * pCon; pCon = (struct espconn *)os_zalloc(sizeof(struct espconn)); pCon->type = ESPCONN_TCP; pCon->state = ESPCONN_NONE; pCon->proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); pCon->proto.tcp->local_port = espconn_port(); pCon->proto.tcp->remote_port = port; pCon->reverse = NULL; UTILS_StrToIP(host, pCon->proto.tcp->remote_ip); espconn_regist_connectcb(pCon, tcpclient_connect_cb); espconn_regist_reconcb(pCon, tcpclient_recon_cb); espconn_regist_disconcb(pCon, tcpclient_discon_cb); os_printf("TCP: Connect to ip %s:%d\r\n", host, port); espconn_secure_connect(pCon); //espconn_connect(pCon); } ICACHE_FLASH_ATTR void timerConnect(void* c) { os_printf("***timerConnect***\n"); connect("XXX.XXX.XXX.XXX",4343); } ICACHE_FLASH_ATTR bool TestSSL(int caAddr) { espconn_secure_cert_req_enable(0x01,caAddr); //espconn_secure_set_size(ESPCONN_CLIENT,5000); os_timer_disarm(&ctimer); os_timer_setfn(&ctimer, (os_timer_func_t *)timerConnect,NULL); os_timer_arm(&ctimer, 10000, 1); }