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

Dependents:   X10Svr SSDP_Server

Committer:
WiredHome
Date:
Tue Jul 03 00:16:45 2018 +0000
Revision:
0:f782e7bc66ad
Child:
1:def15d0b2fae
Initial working version that seems to run well (only tested with a near zero-load main( ), which hosted only the web server.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:f782e7bc66ad 1 //
WiredHome 0:f782e7bc66ad 2 //
WiredHome 0:f782e7bc66ad 3 //
WiredHome 0:f782e7bc66ad 4
WiredHome 0:f782e7bc66ad 5 #include "mbed.h"
WiredHome 0:f782e7bc66ad 6 #include "rtos.h"
WiredHome 0:f782e7bc66ad 7
WiredHome 0:f782e7bc66ad 8
WiredHome 0:f782e7bc66ad 9 /// Configuration data for the SSDP server
WiredHome 0:f782e7bc66ad 10 ///
WiredHome 0:f782e7bc66ad 11 typedef struct {
WiredHome 0:f782e7bc66ad 12 char * name; ///< pointer to the friendly name, storage managed here
WiredHome 0:f782e7bc66ad 13 char * ident; ///< pointer to a unique identity number, like a mac address
WiredHome 0:f782e7bc66ad 14 char * ipAddr; ///< pointer to the node IP address
WiredHome 0:f782e7bc66ad 15 int port; ///< port number of the local server
WiredHome 0:f782e7bc66ad 16 } SSDP_Config_T;
WiredHome 0:f782e7bc66ad 17
WiredHome 0:f782e7bc66ad 18
WiredHome 0:f782e7bc66ad 19 ///
WiredHome 0:f782e7bc66ad 20 /// SSDP Server
WiredHome 0:f782e7bc66ad 21 ///
WiredHome 0:f782e7bc66ad 22 /// This file documents the SSDP Server, which can be implanted into a node
WiredHome 0:f782e7bc66ad 23 /// so that it is discoverable on Ethernet, and in Windows Network Explorer
WiredHome 0:f782e7bc66ad 24 /// view.
WiredHome 0:f782e7bc66ad 25 ///
WiredHome 0:f782e7bc66ad 26 /// Many basics are defined in order to satisfy the SSDP discovery process.
WiredHome 0:f782e7bc66ad 27 /// - the hosting node has a web server running, which will serve various responses.
WiredHome 0:f782e7bc66ad 28 /// - the hosting node shall provide a /setup.xml which satisfies the SSDP requirements.
WiredHome 0:f782e7bc66ad 29 /// - depending on the application, many additional files may need to be supported.
WiredHome 0:f782e7bc66ad 30 ///
WiredHome 0:f782e7bc66ad 31 /// @code
WiredHome 0:f782e7bc66ad 32 /// HTTPServer svr(Server_Port, Server_Root, 15, 30, 20, 50, &pc);
WiredHome 0:f782e7bc66ad 33 /// svr.RegisterHandler("/", RootPage);
WiredHome 0:f782e7bc66ad 34 /// svr.RegisterHandler("/setup.xml", Setup_xml);
WiredHome 0:f782e7bc66ad 35 /// SSDP ssdp("Friendly Node", eth.getMACAddress(), eth.getIPAddress(), Server_Port);
WiredHome 0:f782e7bc66ad 36 ///
WiredHome 0:f782e7bc66ad 37 /// while (1) {
WiredHome 0:f782e7bc66ad 38 /// led4 = !led4;
WiredHome 0:f782e7bc66ad 39 /// svr.Poll(); // non-blocking, but also not deterministic
WiredHome 0:f782e7bc66ad 40 /// Thread::yield();
WiredHome 0:f782e7bc66ad 41 /// if (criteria)
WiredHome 0:f782e7bc66ad 42 /// ssdp.SetFriendlyName("New Node Name");
WiredHome 0:f782e7bc66ad 43 /// }
WiredHome 0:f782e7bc66ad 44 /// @endcode
WiredHome 0:f782e7bc66ad 45 ///
WiredHome 0:f782e7bc66ad 46 class SSDP {
WiredHome 0:f782e7bc66ad 47 public:
WiredHome 0:f782e7bc66ad 48 /// Constructor for the SSDP server
WiredHome 0:f782e7bc66ad 49 ///
WiredHome 0:f782e7bc66ad 50 /// @param[in] name is a pointer to a string containing the name.
WiredHome 0:f782e7bc66ad 51 /// @param[in] ident is a pointer to an identity string of this node, e.g. a mac address.
WiredHome 0:f782e7bc66ad 52 /// @param[in] ipAddr is a pointer to an IP Address string of this node.
WiredHome 0:f782e7bc66ad 53 /// @param[in] port is an integer port number for the local web server.
WiredHome 0:f782e7bc66ad 54 ///
WiredHome 0:f782e7bc66ad 55 SSDP(const char * name, const char * ident, const char * ipAddr, int port);
WiredHome 0:f782e7bc66ad 56
WiredHome 0:f782e7bc66ad 57 /// Constructor for the SSDP server, as an alternate to the parameter version.
WiredHome 0:f782e7bc66ad 58 ///
WiredHome 0:f782e7bc66ad 59 /// @param[in] config is a pointer to configuration structure which contains
WiredHome 0:f782e7bc66ad 60 /// the individual elements;
WiredHome 0:f782e7bc66ad 61 /// - pointer to the const char * friendly name
WiredHome 0:f782e7bc66ad 62 /// - pointer to the const char * identity
WiredHome 0:f782e7bc66ad 63 /// - pointer to the const char * IP Address of this node
WiredHome 0:f782e7bc66ad 64 /// - port number for the web server
WiredHome 0:f782e7bc66ad 65 ///
WiredHome 0:f782e7bc66ad 66 SSDP(const SSDP_Config_T * config);
WiredHome 0:f782e7bc66ad 67
WiredHome 0:f782e7bc66ad 68 /// Destructor
WiredHome 0:f782e7bc66ad 69 ///
WiredHome 0:f782e7bc66ad 70 ~SSDP();
WiredHome 0:f782e7bc66ad 71
WiredHome 0:f782e7bc66ad 72 /// Set the friendly name for this node
WiredHome 0:f782e7bc66ad 73 ///
WiredHome 0:f782e7bc66ad 74 /// @param[in] name is a pointer to a string containing the name.
WiredHome 0:f782e7bc66ad 75 /// @returns true if the name was set (memory was available).
WiredHome 0:f782e7bc66ad 76 ///
WiredHome 0:f782e7bc66ad 77 bool SetFriendlyName(const char * name);
WiredHome 0:f782e7bc66ad 78
WiredHome 0:f782e7bc66ad 79 /// Delete the friendly name that was assigned.
WiredHome 0:f782e7bc66ad 80 ///
WiredHome 0:f782e7bc66ad 81 /// This frees the memory that was previously allocated.
WiredHome 0:f782e7bc66ad 82 ///
WiredHome 0:f782e7bc66ad 83 void DelFriendlyName();
WiredHome 0:f782e7bc66ad 84
WiredHome 0:f782e7bc66ad 85 /// Set the identity for this node
WiredHome 0:f782e7bc66ad 86 ///
WiredHome 0:f782e7bc66ad 87 /// @param[in] ident is a pointer to a string containing the identity.
WiredHome 0:f782e7bc66ad 88 /// @returns true if the name was set (memory was available).
WiredHome 0:f782e7bc66ad 89 ///
WiredHome 0:f782e7bc66ad 90 bool SetIdentity(const char * ident);
WiredHome 0:f782e7bc66ad 91
WiredHome 0:f782e7bc66ad 92 /// Delete the identity that was assigned.
WiredHome 0:f782e7bc66ad 93 ///
WiredHome 0:f782e7bc66ad 94 /// This frees the memory that was previously allocated.
WiredHome 0:f782e7bc66ad 95 ///
WiredHome 0:f782e7bc66ad 96 void DelIdentity();
WiredHome 0:f782e7bc66ad 97
WiredHome 0:f782e7bc66ad 98 /// Set the IP Address for this node
WiredHome 0:f782e7bc66ad 99 ///
WiredHome 0:f782e7bc66ad 100 /// @param[in] ipAddr is a pointer to a string containing the ipAddress.
WiredHome 0:f782e7bc66ad 101 /// @returns true if the IP Address was set (memory was available).
WiredHome 0:f782e7bc66ad 102 ///
WiredHome 0:f782e7bc66ad 103 bool SetIPAddress(const char * ipAddr);
WiredHome 0:f782e7bc66ad 104
WiredHome 0:f782e7bc66ad 105 /// Delete the friendly IP Address that was assigned.
WiredHome 0:f782e7bc66ad 106 ///
WiredHome 0:f782e7bc66ad 107 /// This frees the memory that was previously allocated.
WiredHome 0:f782e7bc66ad 108 ///
WiredHome 0:f782e7bc66ad 109 void DelIPAddress();
WiredHome 0:f782e7bc66ad 110
WiredHome 0:f782e7bc66ad 111 /// Set the Port
WiredHome 0:f782e7bc66ad 112 ///
WiredHome 0:f782e7bc66ad 113 /// @param[in] port is the port number of the local web server.
WiredHome 0:f782e7bc66ad 114 /// @returns true if the port number was set.
WiredHome 0:f782e7bc66ad 115 ///
WiredHome 0:f782e7bc66ad 116 bool SetPort(int port);
WiredHome 0:f782e7bc66ad 117
WiredHome 0:f782e7bc66ad 118 private:
WiredHome 0:f782e7bc66ad 119
WiredHome 0:f782e7bc66ad 120 void StartListener();
WiredHome 0:f782e7bc66ad 121 void SendNotify();
WiredHome 0:f782e7bc66ad 122
WiredHome 0:f782e7bc66ad 123 SSDP_Config_T _config; ///< the configuration
WiredHome 0:f782e7bc66ad 124
WiredHome 0:f782e7bc66ad 125 Thread * pThr;
WiredHome 0:f782e7bc66ad 126 };