Skip to content

Commit 20a8b9d

Browse files
committed
Merge pull request #680 from nkolban/master
Function for #589 and comment removals
2 parents 2e32742 + cb11ad9 commit 20a8b9d

File tree

7 files changed

+318
-225
lines changed

7 files changed

+318
-225
lines changed

libs/network/esp8266/jswrap_esp8266.c

+68-12
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ void jswrap_ESP8266_wifi_stopAP(JsVar *jsCallback) {
259259
* When the outcome is known, the callback function is invoked. The options object contains
260260
* properties that control the connection. Included in this object are:
261261
* * autoConnect (Boolean) - When true, these settings will be used to autoConnect on next boot.
262+
* * dnsServers (array of String) - An array of up to two DNS servers in dotted decimal format string.
262263
*/
263264
void jswrap_ESP8266_wifi_connect(
264265
JsVar *jsSsid,
@@ -401,7 +402,48 @@ void jswrap_ESP8266_wifi_connect(
401402
}
402403
}
403404
jsvUnLock(jsAutoConnect);
404-
}
405+
// End of autoConnect processing
406+
407+
// Do we have a child property called dnsServers?
408+
JsVar *jsDNSServers = jsvObjectGetChild(jsOptions, "dnsServers", 0);
409+
if (jsvIsArray(jsDNSServers) != false) {
410+
os_printf(" - We have DNS servers!!\n");
411+
JsVarInt numDNSServers = jsvGetArrayLength(jsDNSServers);
412+
ip_addr_t dnsAddresses[2];
413+
int count;
414+
if (numDNSServers == 0) {
415+
os_printf("No servers!!");
416+
count = 0;
417+
}
418+
if (numDNSServers > 0) {
419+
// One server
420+
count = 1;
421+
JsVar *jsCurrentDNSServer = jsvGetArrayItem(jsDNSServers, 0);
422+
char buffer[50];
423+
size_t size = jsvGetString(jsCurrentDNSServer, buffer, sizeof(buffer)-1);
424+
buffer[size] = '\0';
425+
jsvUnLock(jsCurrentDNSServer);
426+
dnsAddresses[0].addr = networkParseIPAddress(buffer);
427+
}
428+
if (numDNSServers > 1) {
429+
// Two servers
430+
count = 2;
431+
JsVar *jsCurrentDNSServer = jsvGetArrayItem(jsDNSServers, 1);
432+
char buffer[50];
433+
size_t size = jsvGetString(jsCurrentDNSServer, buffer, sizeof(buffer)-1);
434+
buffer[size] = '\0';
435+
jsvUnLock(jsCurrentDNSServer);
436+
dnsAddresses[1].addr = networkParseIPAddress(buffer);
437+
}
438+
if (numDNSServers > 2) {
439+
os_printf("Ignoring DNS servers after first 2.");
440+
}
441+
if (count > 0) {
442+
espconn_dns_setserver((char)count, dnsAddresses);
443+
}
444+
}
445+
jsvUnLock(jsDNSServers);
446+
} // End of dnsServers processing
405447

406448
// Perform the network level connection.
407449
wifi_station_connect();
@@ -1204,13 +1246,12 @@ void jswrap_ESP8266_updateCPUFreq(
12041246
*/
12051247
/*JSON{
12061248
"type" : "staticmethod",
1207-
"class" : "ESP8266WiFi",
1249+
"class" : "ESP8266",
12081250
"name" : "getState",
1209-
"generate" : "jswrap_ESP8266WiFi_getState",
1210-
"return" : ["JsVar","The state of the ESP8266"],
1211-
"return_object" : "ESP8266State"
1251+
"generate" : "jswrap_ESP8266_getState",
1252+
"return" : ["JsVar", "The state of the ESP8266"]
12121253
}*/
1213-
JsVar *jswrap_ESP8266WiFi_getState() {
1254+
JsVar *jswrap_ESP8266_getState() {
12141255
// Create a new variable and populate it with the properties of the ESP8266 that we
12151256
// wish to return.
12161257
JsVar *esp8266State = jspNewObject(NULL, "ESP8266State");
@@ -1348,11 +1389,14 @@ static void dnsFoundCallback(
13481389
void *arg //!< Parameter passed in from espconn_gethostbyname.
13491390
) {
13501391
os_printf(">> dnsFoundCallback - %s %x\n", hostname, ipAddr->addr);
1351-
assert(g_jsHostByNameCallback != NULL);
1352-
JsVar *params[1];
1353-
params[0] = jsvNewFromInteger(ipAddr->addr);
1354-
jsiQueueEvents(NULL, g_jsHostByNameCallback, params, 1);
1355-
jsvUnLock(params[0]);
1392+
if (g_jsHostByNameCallback != NULL) {
1393+
JsVar *params[1];
1394+
params[0] = jsvNewFromInteger(ipAddr->addr);
1395+
jsiQueueEvents(NULL, g_jsHostByNameCallback, params, 1);
1396+
jsvUnLock(params[0]);
1397+
jsvUnLock(g_jsHostByNameCallback);
1398+
g_jsHostByNameCallback = NULL;
1399+
}
13561400
os_printf("<< dnsFoundCallback\n");
13571401
}
13581402

@@ -1676,13 +1720,25 @@ void jswrap_ESP8266_ping(
16761720
["socketId","JsVar","The socket to be dumped."]
16771721
]
16781722
}*/
1679-
16801723
void jswrap_ESP8266_dumpSocket(
16811724
JsVar *socketId //!< The socket to be dumped.
16821725
) {
16831726
esp8266_dumpSocket(jsvGetInteger(socketId)-1);
16841727
}
16851728

1729+
/*JSON{
1730+
"type" : "staticmethod",
1731+
"class" : "ESP8266",
1732+
"name" : "dumpAllSocketData",
1733+
"generate" : "jswrap_ESP8266_dumpAllSocketData"
1734+
}
1735+
* Write all the socket data structures to the debug log.
1736+
* This is purely a diagnostic function.
1737+
*/
1738+
void jswrap_ESP8266_dumpAllSocketData() {
1739+
esp8266_dumpAllSocketData();
1740+
}
1741+
16861742
/**
16871743
* Null terminate a string.
16881744
*/

libs/network/esp8266/jswrap_esp8266.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ JsVar *jswrap_ESP8266WiFi_getDHCPHostname();
3434
// Deprecated
3535
JsVar *jswrap_ESP8266WiFi_getIPInfo();
3636
JsVar *jswrap_ESP8266WiFi_getRSSI();
37-
JsVar *jswrap_ESP8266WiFi_getState();
3837
JsVar *jswrap_ESP8266WiFi_getStationConfig();
3938
void jswrap_ESP8266WiFi_init();
4039
void jswrap_ESP8266WiFi_kill();
@@ -54,10 +53,12 @@ JsVar *jswrap_ESP8266_wifi_getIP();
5453
void jswrap_ESP8266_wifi_scan(JsVar *jsCallback);
5554
void jswrap_ESP8266_wifi_stopAP(JsVar *jsCallback);
5655

56+
void jswrap_ESP8266_dumpAllSocketData();
5757
void jswrap_ESP8266_dumpSocket(JsVar *jsSocketId);
5858
JsVar *jswrap_ESP8266_getAddressAsString(JsVar *jsAddress);
5959
void jswrap_ESP8266_getHostByName(JsVar *jsHostname, JsVar *jsCallback);
6060
JsVar *jswrap_ESP8266_getRstInfo();
61+
JsVar *jswrap_ESP8266_getState();
6162
void jswrap_ESP8266_logDebug(JsVar *jsDebug);
6263
void jswrap_ESP8266_ping(JsVar *jsIpAddr, JsVar *jsPingCallback);
6364
void jswrap_ESP8266_restart();

libs/network/esp8266/network_esp8266.c

+59-9
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static void doClose(int socketId);
8282
static void releaseSocket(int socketId);
8383
static void resetSocketByData(struct socketData *pSocketData);
8484
static void resetSocketById(int sckt);
85+
static void esp8266_dumpSocketData(struct socketData *pSocketData);
8586

8687
static void esp8266_callback_connectCB_inbound(void *arg);
8788
static void esp8266_callback_connectCB_outbound(void *arg);
@@ -219,6 +220,18 @@ static struct socketData socketArray[MAX_SOCKETS];
219220
*/
220221
static bool g_socketsInitialized = false;
221222

223+
/**
224+
* Dump all the socket structures.
225+
* This is used exclusively for debugging. It walks through each of the
226+
* socket structures and dumps their state to the debug log.
227+
*/
228+
void esp8266_dumpAllSocketData() {
229+
int i;
230+
for (i=0; i<MAX_SOCKETS; i++) {
231+
esp8266_dumpSocketData(&socketArray[i]);
232+
}
233+
}
234+
222235

223236
/**
224237
* Write the details of a socket to the debug log.
@@ -228,7 +241,18 @@ void esp8266_dumpSocket(
228241
int socketId //!< The ID of the socket data structure to be logged.
229242
) {
230243
struct socketData *pSocketData = getSocketData(socketId);
231-
LOG("Dump of socket=%d\n", socketId);
244+
esp8266_dumpSocketData(pSocketData);
245+
}
246+
247+
248+
/**
249+
* Write the details of a socketData to the debug log.
250+
* The data associated with the socketData is dumped to the debug log.
251+
*/
252+
static void esp8266_dumpSocketData(
253+
struct socketData *pSocketData //!< The socket data structure to be logged
254+
) {
255+
LOG("Dump of socket=%d\n", pSocketData->socketId);
232256
LOG(" - isConnected=%d", pSocketData->isConnected);
233257
char *creationTypeMsg;
234258
switch(pSocketData->creationType) {
@@ -284,6 +308,7 @@ void esp8266_dumpSocket(
284308
break;
285309
}
286310
LOG(", state=%s", stateMsg);
311+
LOG(", espconn=0x%x", (unsigned int)pSocketData->pEspconn);
287312
LOG(", errorCode=%d", pSocketData->errorCode);
288313

289314
// Print the errorMsg if it has anything to say
@@ -481,7 +506,6 @@ static void resetSocketByData(
481506

482507
pSocketData->acceptedSocketsHead = 0; // Set the head to 0
483508
pSocketData->acceptedSocketsTail = 0; // Set the tail to 9.
484-
pSocketData->pEspconn = NULL;
485509
}
486510

487511

@@ -511,9 +535,10 @@ static void releaseSocket(
511535

512536
// If this socket is not an incoming socket that means that the espconn structure was created
513537
// by us and we should release the storage we allocated.
514-
if (pSocketData->creationType != SOCKET_CREATED_INBOUND) {
538+
if (pSocketData->creationType != SOCKET_CREATED_INBOUND && pSocketData->creationType != SOCKET_CREATED_SERVER) {
515539
os_free(pSocketData->pEspconn->proto.tcp);
516540
pSocketData->pEspconn->proto.tcp = NULL;
541+
os_printf(" - freeing espconn: 0x%x\n", (unsigned int)pSocketData->pEspconn);
517542
os_free(pSocketData->pEspconn);
518543
pSocketData->pEspconn = NULL;
519544
}
@@ -556,14 +581,23 @@ static void doClose(
556581

557582
struct socketData *pSocketData = getSocketData(socketId);
558583

559-
if (pSocketData->state != SOCKET_STATE_CLOSING) {
560-
int rc = espconn_disconnect(pSocketData->pEspconn);
584+
585+
if (pSocketData->creationType == SOCKET_CREATED_SERVER) {
586+
int rc = espconn_delete(pSocketData->pEspconn);
587+
if (rc != 0) {
588+
os_printf("espconn_delete: rc=%s (%d)\n",esp8266_errorToString(rc), rc);
589+
setSocketInError(socketId, "espconn_disconnect", rc);
590+
}
561591
pSocketData->state = SOCKET_STATE_CLOSING;
592+
}
562593

594+
if (pSocketData->state != SOCKET_STATE_CLOSING) {
595+
int rc = espconn_disconnect(pSocketData->pEspconn);
563596
if (rc != 0) {
564-
os_printf("espconn_disconnect: rc=%d\n", rc);
597+
os_printf("espconn_disconnect: rc=%s (%d)\n",esp8266_errorToString(rc), rc);
565598
setSocketInError(socketId, "espconn_disconnect", rc);
566599
}
600+
pSocketData->state = SOCKET_STATE_CLOSING;
567601
}
568602
// Our existing state on entry was SOCKET_STATE_CLOSING which means that we got here
569603
// because we were previously flagged as closing.
@@ -695,6 +729,7 @@ static void esp8266_callback_disconnectCB(
695729
// If the socket state is SOCKET_STATE_CLOSING then that means we can release the socket. The reason
696730
// for this is that the last thing the user did was request an explicit socket close.
697731
if (pSocketData->state == SOCKET_STATE_CLOSING) {
732+
698733
releaseSocket(pSocketData->socketId);
699734
} else {
700735
pSocketData->state = SOCKET_STATE_CLOSING;
@@ -787,7 +822,7 @@ static void esp8266_callback_recvCB(
787822
if (pSocketData->rxBufLen == 0) {
788823
pSocketData->rxBuf = (void *)os_malloc(len);
789824
if (pSocketData->rxBuf == NULL) {
790-
os_printf(" - Out of memory\n");
825+
os_printf(" - #1 Out of memory allocating %d\n", len);
791826
return;
792827
}
793828
os_memcpy(pSocketData->rxBuf, pData, len);
@@ -801,7 +836,7 @@ static void esp8266_callback_recvCB(
801836
// Update the socket data.
802837
uint8 *pNewBuf = (uint8 *)os_malloc(len + pSocketData->rxBufLen);
803838
if (pNewBuf == NULL) {
804-
os_printf(" - Out of memory\n");
839+
os_printf(" - #2 Out of memory allocating %d\n", len + pSocketData->rxBufLen);
805840
return;
806841
}
807842
os_memcpy(pNewBuf, pSocketData->rxBuf, pSocketData->rxBufLen);
@@ -1039,6 +1074,7 @@ int net_ESP8266_BOARD_createSocket(
10391074

10401075
pSocketData->pEspconn = (struct espconn *)os_malloc(sizeof(struct espconn));
10411076
assert(pSocketData->pEspconn);
1077+
os_printf(" - espconn: 0x%x\n", (unsigned int)pSocketData->pEspconn);
10421078

10431079
struct espconn *pEspconn = pSocketData->pEspconn;
10441080

@@ -1124,7 +1160,7 @@ void net_ESP8266_BOARD_closeSocket(
11241160
//dumpEspConn(pSocketData->pEspconn);
11251161

11261162
// How we close the socket is a function of what kind of socket it is.
1127-
1163+
/*
11281164
// Close a server socket
11291165
if (pSocketData->creationType == SOCKET_CREATED_SERVER) {
11301166
int rc = espconn_delete(pSocketData->pEspconn);
@@ -1146,6 +1182,20 @@ void net_ESP8266_BOARD_closeSocket(
11461182
pSocketData->shouldClose = true;
11471183
}
11481184
}
1185+
*/
1186+
1187+
// If the state of the socket is idle, closing or error then we can actually close the socket.
1188+
// If it is not one of these states, then we are in the middle of something so let that
1189+
// something completed and then we can finish.
1190+
if (pSocketData->state == SOCKET_STATE_IDLE ||
1191+
pSocketData->state == SOCKET_STATE_CLOSING ||
1192+
pSocketData->state == SOCKET_STATE_ERROR) {
1193+
doClose(socketId);
1194+
} else {
1195+
pSocketData->shouldClose = true;
1196+
}
1197+
1198+
os_printf("< net_ESP8266_BOARD_closeSocket\n");
11491199
}
11501200

11511201

libs/network/esp8266/network_esp8266.h

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
void netInit_esp8266_board();
2222
void netSetCallbacks_esp8266_board(JsNetwork *net);
2323
void esp8266_dumpSocket(int socketId);
24+
void esp8266_dumpAllSocketData();
2425
int net_ESP8266_BOARD_accept(JsNetwork *net, int serverSckt);
2526
int net_ESP8266_BOARD_recv(JsNetwork *net, int sckt, void *buf, size_t len);
2627
int net_ESP8266_BOARD_send(JsNetwork *net, int sckt, const void *buf, size_t len);

0 commit comments

Comments
 (0)