SSDP Server - working version provides SSDP based network discovery, and with a companion web server, may provide other functionalities.

Dependents:   X10Svr SSDP_Server

Revision:
1:def15d0b2fae
Parent:
0:f782e7bc66ad
Child:
2:3d6d70556fca
--- a/SSDP.cpp	Tue Jul 03 00:16:45 2018 +0000
+++ b/SSDP.cpp	Tue Jul 03 00:46:34 2018 +0000
@@ -7,13 +7,24 @@
 #include "SSDP.h"
 #include "EthernetInterface.h"
 
-extern DigitalOut led1;
+//#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__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#define INFO(x, ...)
+#endif
 
 static const char* MCAST_GRP = "239.255.255.250";
 static const int MCAST_PORT = 1900;
 
-
 // sprintf(buffer, SSDP_HTTP, "myIPString", myPort, "myIdentity", "myIdentity");
 // Requires IP address as a string
 static const char * SSDP_HTTP = 
@@ -39,7 +50,7 @@
 
 // sprintf(buffer, SSDP_NOTIFY, myIPasString, myPort);
 // Requires IP address as a string
-const char * SSDP_NOTIFY = 
+static const char * SSDP_NOTIFY = 
     "NOTIFY * HTTP/1.1\r\n"
     "HOST: 239.255.255.250:1900\r\n"
     "CACHE-CONTROL: max-age=1800\r\n"
@@ -53,23 +64,23 @@
 #define SSDP_NOTIFY_OVERHEAD 20   // Number of bytes to fill in the information
 
 
+// The SSDP listener thread
 static void SSDPListener(void const * args) {
     UDPSocket server;
     SSDP_Config_T * cfg = (SSDP_Config_T *)args;
     
     server.bind(MCAST_PORT);
     if (server.join_multicast_group(MCAST_GRP) != 0) {
-        printf("Error joining the multicast group\n");
+        ERR("Error joining the multicast group");
         while (true) {}
     }
 
     Endpoint client;
     char buffer[256];
     while (true) {
-        printf("Wait for packet...\n");
+        INFO("Wait for packet...");
         int n = server.receiveFrom(client, buffer, sizeof(buffer)-1);
         buffer[n] = '\0';
-        led1 = true;
         if (n) {
             char * p = buffer;
             volatile int delay = 1;
@@ -99,7 +110,6 @@
                 free(out_buffer);
             }
         }
-        led1 = false;
     }
 }
 
@@ -113,7 +123,7 @@
     SetIPAddress(ipAddr);
     _config.port = port;
     StartListener();
-    printf("SSDP(......) constructor done. Listener Started.\n");
+    INFO("SSDP(......) constructor done. Listener Started.");
     SendNotify();
 }
 
@@ -121,7 +131,7 @@
     pThr = NULL;
     memcpy(&_config, config, sizeof(SSDP_Config_T));
     StartListener();
-    printf("SSDP() constructor done. Listener Started.\n");
+    INFO("SSDP(.) constructor done. Listener Started.");
     SendNotify();
 }
 
@@ -138,10 +148,9 @@
     char * out_buffer = (char *)malloc(strlen(SSDP_NOTIFY) + SSDP_NOTIFY_OVERHEAD);
     if (out_buffer) {
         UDPSocket sock;
+        Endpoint broadcast;
         sock.init();
         sock.set_broadcasting();
-        
-        Endpoint broadcast;
         broadcast.set_address(MCAST_GRP, MCAST_PORT);
         sprintf(out_buffer, SSDP_NOTIFY, _config.ipAddr, _config.port);
         sock.sendTo(broadcast, out_buffer, strlen(out_buffer));