SSDP Server - working version provides SSDP based network discovery, and with a companion web server, may provide other functionalities.
Dependents: X10Svr SSDP_Server
Diff: SSDP.cpp
- Revision:
- 6:9df748509c3d
- Parent:
- 5:199656d96c72
- Child:
- 7:1e8c677e3d28
diff -r 199656d96c72 -r 9df748509c3d SSDP.cpp --- a/SSDP.cpp Sat Nov 17 01:30:19 2018 +0000 +++ b/SSDP.cpp Sun Nov 18 04:01:58 2018 +0000 @@ -8,21 +8,21 @@ #include "EthernetInterface.h" #include "SW_String.h" -//#define DEBUG "SSDP" //Debug is disabled by default +#define DEBUG "SSDP" //Debug is disabled by default #include <cstdio> #if (defined(DEBUG) && !defined(TARGET_LPC11U24)) -#define DBG(x, ...) std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); -#define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); -#define ERR(x, ...) std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); -#define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define DBG(x, ...) std::printf("[DBG %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); +#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__); #else #define DBG(x, ...) #define WARN(x, ...) #define ERR(x, ...) #define INFO(x, ...) #endif - +extern void ShowSignOfLife(int which); static const char* MCAST_GRP = "239.255.255.250"; static const int MCAST_PORT = 1900; static Thread * pThr; @@ -76,19 +76,24 @@ ERR("Error joining the multicast group"); while (true) {} } - + server.set_blocking(false, 50); // non-blocking with 50ms timeout Endpoint client; char buffer[256]; while (true) { //INFO("Wait for packet..."); + ShowSignOfLife(2); int n = server.receiveFrom(client, buffer, sizeof(buffer)-1); - buffer[n] = '\0'; + if (n > 0) { + buffer[n] = '\0'; + } if (n) { char * p = buffer; volatile int delay = 0; uint8_t mask = 0x00; // fragments we found in the received packet - //INFO("SSDP\n%s", buffer); + INFO("SSDP receiveFrom %d bytes from %s:%d", n, + client.get_address(), client.get_port()); + INFO("SSDP\n%s", buffer); while (*p) { char * e = strstr(p, "\r\n"); if (e && (e - buffer) < n) { @@ -113,7 +118,7 @@ } p++; } - //INFO("*********** %02X", mask); + INFO(" ***** %02X", mask); if ((mask & 0x1F) == 0x1F) { char * out_buffer = (char *)malloc(strlen(SSDP_HTTP) + SSDP_HTTP_OVERHEAD); @@ -121,9 +126,10 @@ sprintf(out_buffer, SSDP_HTTP, cfg->ipAddr, cfg->port, cfg->ident, cfg->ident); // It would be polite to delay, but the recommendation is from 1 to 2 seconds. // Send the response twice, to improve reliability of node discovery - for (int i=0; i<2; i++) { - server.sendTo(client, out_buffer, strlen(out_buffer)); - //Thread::wait(150); + for (int i=0; i<1; i++) { + INFO("SSDPListener: reply >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"); + int i = server.sendTo(client, out_buffer, strlen(out_buffer)); + INFO(" sendTo %3d: reply <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<", i); } free(out_buffer); INFO("SSDPListener: stack-used: %d, total: %d", pThr->max_stack(), pThr->stack_size());