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

Dependents:   X10Svr SSDP_Server

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Tue Jul 03 00:46:34 2018 +0000
Parent:
0:f782e7bc66ad
Child:
2:3d6d70556fca
Commit message:
Documentation update.

Changed in this revision

SSDP.cpp Show annotated file Show diff for this revision Revisions of this file
SSDP.h Show annotated file Show diff for this revision Revisions of this file
--- 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));
--- a/SSDP.h	Tue Jul 03 00:16:45 2018 +0000
+++ b/SSDP.h	Tue Jul 03 00:46:34 2018 +0000
@@ -6,10 +6,18 @@
 #include "rtos.h"
 
 
-/// Configuration data for the SSDP server
+/// Configuration data for the SSDP server.
+///
+/// When internal to the SSDP server, it will manage the memory (dynamic allocations
+/// and free).
+///
+/// When an instance of this structure is passed to the SSDP constructor, it is
+/// up to the caller to ensure that the data being pointed to is persistent, where
+/// it should be. The 'name' is one field that could be changed at runtime, in 
+/// order to reflect the name change to the network.
 ///
 typedef struct {
-    char * name;        ///< pointer to the friendly name, storage managed here
+    char * name;        ///< pointer to the friendly name
     char * ident;       ///< pointer to a unique identity number, like a mac address
     char * ipAddr;      ///< pointer to the node IP address
     int port;           ///< port number of the local server
@@ -63,6 +71,9 @@
     ///             - pointer to the const char * IP Address of this node
     ///             - port number for the web server 
     ///
+    /// @note the items being pointed to in this structure must be persistent. Only
+    ///         the friendly name can change at runtime.
+    ///
     SSDP(const SSDP_Config_T * config);
     
     /// Destructor