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:
Sun Jul 19 15:07:14 2020 +0000
Revision:
14:ad92261497ca
Parent:
10:26f5a66f05a4
Change the SSDP listener thread to "normal", and tweak a few debug statements.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:f782e7bc66ad 1 //
WiredHome 4:2260bac62038 2 // SSDP Server
WiredHome 0:f782e7bc66ad 3 //
WiredHome 4:2260bac62038 4 // This block creates and manages the network interface for an SSDP Server,
WiredHome 4:2260bac62038 5 // which can make this node easily discoverable on the network.
WiredHome 4:2260bac62038 6 //
WiredHome 4:2260bac62038 7 // @note SSDP is not a secure protocol and is cited as creating vulnerabilities,
WiredHome 4:2260bac62038 8 // so this should be understood or used with caution on a controlled
WiredHome 4:2260bac62038 9 // private network.
WiredHome 0:f782e7bc66ad 10 //
WiredHome 0:f782e7bc66ad 11
WiredHome 0:f782e7bc66ad 12 #include "mbed.h"
WiredHome 0:f782e7bc66ad 13 #include "rtos.h"
WiredHome 0:f782e7bc66ad 14
WiredHome 1:def15d0b2fae 15 /// Configuration data for the SSDP server.
WiredHome 1:def15d0b2fae 16 ///
WiredHome 1:def15d0b2fae 17 /// When internal to the SSDP server, it will manage the memory (dynamic allocations
WiredHome 1:def15d0b2fae 18 /// and free).
WiredHome 1:def15d0b2fae 19 ///
WiredHome 1:def15d0b2fae 20 /// When an instance of this structure is passed to the SSDP constructor, it is
WiredHome 1:def15d0b2fae 21 /// up to the caller to ensure that the data being pointed to is persistent, where
WiredHome 1:def15d0b2fae 22 /// it should be. The 'name' is one field that could be changed at runtime, in
WiredHome 1:def15d0b2fae 23 /// order to reflect the name change to the network.
WiredHome 0:f782e7bc66ad 24 ///
WiredHome 0:f782e7bc66ad 25 typedef struct {
WiredHome 1:def15d0b2fae 26 char * name; ///< pointer to the friendly name
WiredHome 0:f782e7bc66ad 27 char * ident; ///< pointer to a unique identity number, like a mac address
WiredHome 0:f782e7bc66ad 28 char * ipAddr; ///< pointer to the node IP address
WiredHome 0:f782e7bc66ad 29 int port; ///< port number of the local server
WiredHome 0:f782e7bc66ad 30 } SSDP_Config_T;
WiredHome 0:f782e7bc66ad 31
WiredHome 0:f782e7bc66ad 32
WiredHome 0:f782e7bc66ad 33 /// SSDP Server
WiredHome 0:f782e7bc66ad 34 ///
WiredHome 0:f782e7bc66ad 35 /// This file documents the SSDP Server, which can be implanted into a node
WiredHome 0:f782e7bc66ad 36 /// so that it is discoverable on Ethernet, and in Windows Network Explorer
WiredHome 0:f782e7bc66ad 37 /// view.
WiredHome 0:f782e7bc66ad 38 ///
WiredHome 0:f782e7bc66ad 39 /// Many basics are defined in order to satisfy the SSDP discovery process.
WiredHome 0:f782e7bc66ad 40 /// - the hosting node has a web server running, which will serve various responses.
WiredHome 0:f782e7bc66ad 41 /// - the hosting node shall provide a /setup.xml which satisfies the SSDP requirements.
WiredHome 0:f782e7bc66ad 42 /// - depending on the application, many additional files may need to be supported.
WiredHome 0:f782e7bc66ad 43 ///
WiredHome 0:f782e7bc66ad 44 /// @code
WiredHome 0:f782e7bc66ad 45 /// HTTPServer svr(Server_Port, Server_Root, 15, 30, 20, 50, &pc);
WiredHome 0:f782e7bc66ad 46 /// svr.RegisterHandler("/", RootPage);
WiredHome 0:f782e7bc66ad 47 /// svr.RegisterHandler("/setup.xml", Setup_xml);
WiredHome 0:f782e7bc66ad 48 /// SSDP ssdp("Friendly Node", eth.getMACAddress(), eth.getIPAddress(), Server_Port);
WiredHome 0:f782e7bc66ad 49 ///
WiredHome 0:f782e7bc66ad 50 /// while (1) {
WiredHome 0:f782e7bc66ad 51 /// led4 = !led4;
WiredHome 0:f782e7bc66ad 52 /// svr.Poll(); // non-blocking, but also not deterministic
WiredHome 0:f782e7bc66ad 53 /// Thread::yield();
WiredHome 0:f782e7bc66ad 54 /// if (criteria)
WiredHome 0:f782e7bc66ad 55 /// ssdp.SetFriendlyName("New Node Name");
WiredHome 0:f782e7bc66ad 56 /// }
WiredHome 0:f782e7bc66ad 57 /// @endcode
WiredHome 0:f782e7bc66ad 58 ///
WiredHome 0:f782e7bc66ad 59 class SSDP {
WiredHome 14:ad92261497ca 60 private:
WiredHome 14:ad92261497ca 61
WiredHome 0:f782e7bc66ad 62 public:
WiredHome 0:f782e7bc66ad 63 /// Constructor for the SSDP server
WiredHome 0:f782e7bc66ad 64 ///
WiredHome 0:f782e7bc66ad 65 /// @param[in] name is a pointer to a string containing the name.
WiredHome 0:f782e7bc66ad 66 /// @param[in] ident is a pointer to an identity string of this node, e.g. a mac address.
WiredHome 0:f782e7bc66ad 67 /// @param[in] ipAddr is a pointer to an IP Address string of this node.
WiredHome 0:f782e7bc66ad 68 /// @param[in] port is an integer port number for the local web server.
WiredHome 0:f782e7bc66ad 69 ///
WiredHome 0:f782e7bc66ad 70 SSDP(const char * name, const char * ident, const char * ipAddr, int port);
WiredHome 0:f782e7bc66ad 71
WiredHome 0:f782e7bc66ad 72 /// Constructor for the SSDP server, as an alternate to the parameter version.
WiredHome 0:f782e7bc66ad 73 ///
WiredHome 0:f782e7bc66ad 74 /// @param[in] config is a pointer to configuration structure which contains
WiredHome 0:f782e7bc66ad 75 /// the individual elements;
WiredHome 0:f782e7bc66ad 76 /// - pointer to the const char * friendly name
WiredHome 0:f782e7bc66ad 77 /// - pointer to the const char * identity
WiredHome 0:f782e7bc66ad 78 /// - pointer to the const char * IP Address of this node
WiredHome 0:f782e7bc66ad 79 /// - port number for the web server
WiredHome 0:f782e7bc66ad 80 ///
WiredHome 1:def15d0b2fae 81 /// @note the items being pointed to in this structure must be persistent. Only
WiredHome 1:def15d0b2fae 82 /// the friendly name can change at runtime.
WiredHome 1:def15d0b2fae 83 ///
WiredHome 0:f782e7bc66ad 84 SSDP(const SSDP_Config_T * config);
WiredHome 0:f782e7bc66ad 85
WiredHome 0:f782e7bc66ad 86 /// Destructor
WiredHome 0:f782e7bc66ad 87 ///
WiredHome 0:f782e7bc66ad 88 ~SSDP();
WiredHome 0:f782e7bc66ad 89
WiredHome 0:f782e7bc66ad 90 /// Set the friendly name for this node
WiredHome 0:f782e7bc66ad 91 ///
WiredHome 0:f782e7bc66ad 92 /// @param[in] name is a pointer to a string containing the name.
WiredHome 0:f782e7bc66ad 93 /// @returns true if the name was set (memory was available).
WiredHome 0:f782e7bc66ad 94 ///
WiredHome 0:f782e7bc66ad 95 bool SetFriendlyName(const char * name);
WiredHome 0:f782e7bc66ad 96
WiredHome 0:f782e7bc66ad 97 /// Set the identity for this node
WiredHome 0:f782e7bc66ad 98 ///
WiredHome 0:f782e7bc66ad 99 /// @param[in] ident is a pointer to a string containing the identity.
WiredHome 0:f782e7bc66ad 100 /// @returns true if the name was set (memory was available).
WiredHome 0:f782e7bc66ad 101 ///
WiredHome 0:f782e7bc66ad 102 bool SetIdentity(const char * ident);
WiredHome 0:f782e7bc66ad 103
WiredHome 0:f782e7bc66ad 104 /// Set the IP Address for this node
WiredHome 0:f782e7bc66ad 105 ///
WiredHome 0:f782e7bc66ad 106 /// @param[in] ipAddr is a pointer to a string containing the ipAddress.
WiredHome 0:f782e7bc66ad 107 /// @returns true if the IP Address was set (memory was available).
WiredHome 0:f782e7bc66ad 108 ///
WiredHome 0:f782e7bc66ad 109 bool SetIPAddress(const char * ipAddr);
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 14:ad92261497ca 118
WiredHome 0:f782e7bc66ad 119 private:
WiredHome 0:f782e7bc66ad 120
WiredHome 4:2260bac62038 121 /// Delete the friendly name that was assigned.
WiredHome 4:2260bac62038 122 ///
WiredHome 4:2260bac62038 123 /// This frees the memory that was previously allocated.
WiredHome 4:2260bac62038 124 ///
WiredHome 4:2260bac62038 125 void DelFriendlyName();
WiredHome 4:2260bac62038 126
WiredHome 4:2260bac62038 127 /// Delete the identity that was assigned.
WiredHome 4:2260bac62038 128 ///
WiredHome 4:2260bac62038 129 /// This frees the memory that was previously allocated.
WiredHome 4:2260bac62038 130 ///
WiredHome 4:2260bac62038 131 void DelIdentity();
WiredHome 4:2260bac62038 132
WiredHome 4:2260bac62038 133 /// Delete the friendly IP Address that was assigned.
WiredHome 4:2260bac62038 134 ///
WiredHome 4:2260bac62038 135 /// This frees the memory that was previously allocated.
WiredHome 4:2260bac62038 136 ///
WiredHome 4:2260bac62038 137 void DelIPAddress();
WiredHome 4:2260bac62038 138
WiredHome 4:2260bac62038 139 /// Starts a dedicated thread just to listen for SSDP Queries
WiredHome 4:2260bac62038 140 /// and respond to them.
WiredHome 4:2260bac62038 141 ///
WiredHome 0:f782e7bc66ad 142 void StartListener();
WiredHome 4:2260bac62038 143
WiredHome 14:ad92261497ca 144 Thread * pThr;
WiredHome 14:ad92261497ca 145
WiredHome 10:26f5a66f05a4 146 /// Type of Notification
WiredHome 10:26f5a66f05a4 147 typedef enum {
WiredHome 10:26f5a66f05a4 148 nt_root, ///< upnp:rootdevice notification
WiredHome 10:26f5a66f05a4 149 } NotifyType_t;
WiredHome 10:26f5a66f05a4 150
WiredHome 4:2260bac62038 151 /// On power-up and on state-change, notify events are sent
WiredHome 4:2260bac62038 152 /// for which listeners may react.
WiredHome 4:2260bac62038 153 ///
WiredHome 10:26f5a66f05a4 154 void SendNotify(NotifyType_t nt = nt_root);
WiredHome 0:f782e7bc66ad 155
WiredHome 0:f782e7bc66ad 156 SSDP_Config_T _config; ///< the configuration
WiredHome 0:f782e7bc66ad 157
WiredHome 0:f782e7bc66ad 158 };