Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.7 KiB

  1. // SSDPDevice.h
  2. #ifndef _SSDPDEVICE_h
  3. #define _SSDPDEVICE_h
  4. #if defined(ARDUINO) && ARDUINO >= 100
  5. #include "Arduino.h"
  6. #else
  7. #include "WProgram.h"
  8. #endif
  9. #include <ESP8266WiFi.h>
  10. #include <WiFiUdp.h>
  11. #define SSDP_INTERVAL 1200
  12. #define SSDP_PORT 1900
  13. //#define SSDP_METHOD_SIZE 10
  14. //#define SSDP_URI_SIZE 2
  15. //#define SSDP_BUFFER_SIZE 64
  16. #define SSDP_MULTICAST_TTL 2
  17. #define SSDP_QUEUE_SIZE 21
  18. static const IPAddress SSDP_MULTICAST_ADDR(239, 255, 255, 250);
  19. #define SSDP_UUID_SIZE 37
  20. #define SSDP_SCHEMA_URL_SIZE 64
  21. #define SSDP_DEVICE_TYPE_SIZE 64
  22. #define SSDP_FRIENDLY_NAME_SIZE 64
  23. #define SSDP_SERIAL_NUMBER_SIZE 32
  24. #define SSDP_PRESENTATION_URL_SIZE 128
  25. #define SSDP_MODEL_NAME_SIZE 64
  26. #define SSDP_MODEL_URL_SIZE 128
  27. #define SSDP_MODEL_VERSION_SIZE 32
  28. #define SSDP_MANUFACTURER_SIZE 64
  29. #define SSDP_MANUFACTURER_URL_SIZE 128
  30. static const char* PROGMEM SSDP_RESPONSE_TEMPLATE =
  31. "HTTP/1.1 200 OK\r\n"
  32. "EXT:\r\n";
  33. static const char* PROGMEM SSDP_NOTIFY_ALIVE_TEMPLATE =
  34. "NOTIFY * HTTP/1.1\r\n"
  35. "HOST: 239.255.255.250:1900\r\n"
  36. "NTS: ssdp:alive\r\n";
  37. static const char* PROGMEM SSDP_NOTIFY_UPDATE_TEMPLATE =
  38. "NOTIFY * HTTP/1.1\r\n"
  39. "HOST: 239.255.255.250:1900\r\n"
  40. "NTS: ssdp:update\r\n";
  41. static const char* PROGMEM SSDP_PACKET_TEMPLATE =
  42. "%s" // _ssdp_response_template / _ssdp_notify_template
  43. "CACHE-CONTROL: max-age=%u\r\n" // SSDP_INTERVAL
  44. "SERVER: UPNP/1.1 %s/%s\r\n" // m_modelName, m_modelNumber
  45. "USN: %s%s%s\r\n" // m_uuid
  46. "%s: %s\r\n" // "NT" or "ST", m_deviceType
  47. "LOCATION: http://%u.%u.%u.%u:%u/%s\r\n" // WiFi.localIP(), m_port, m_schemaURL
  48. "\r\n";
  49. static const char* PROGMEM SSDP_SCHEMA_TEMPLATE =
  50. "HTTP/1.1 200 OK\r\n"
  51. "Content-Type: text/xml\r\n"
  52. "Connection: close\r\n"
  53. "Access-Control-Allow-Origin: *\r\n"
  54. "\r\n"
  55. "<?xml version=\"1.0\"?>"
  56. "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"
  57. "<specVersion>"
  58. "<major>1</major>"
  59. "<minor>0</minor>"
  60. "</specVersion>"
  61. "<URLBase>http://%u.%u.%u.%u:%u/%s</URLBase>" // WiFi.localIP(), _port
  62. "<device>"
  63. "<deviceType>%s</deviceType>"
  64. "<friendlyName>%s</friendlyName>"
  65. "<presentationURL>%s</presentationURL>"
  66. "<serialNumber>%s</serialNumber>"
  67. "<modelName>%s</modelName>"
  68. "<modelNumber>%s</modelNumber>"
  69. "<modelURL>%s</modelURL>"
  70. "<manufacturer>%s</manufacturer>"
  71. "<manufacturerURL>%s</manufacturerURL>"
  72. "<UDN>uuid:%s</UDN>"
  73. "</device>"
  74. // "<iconList>"
  75. // "<icon>"
  76. // "<mimetype>image/png</mimetype>"
  77. // "<height>48</height>"
  78. // "<width>48</width>"
  79. // "<depth>24</depth>"
  80. // "<url>icon48.png</url>"
  81. // "</icon>"
  82. // "<icon>"
  83. // "<mimetype>image/png</mimetype>"
  84. // "<height>120</height>"
  85. // "<width>120</width>"
  86. // "<depth>24</depth>"
  87. // "<url>icon120.png</url>"
  88. // "</icon>"
  89. // "</iconList>"
  90. "</root>\r\n"
  91. "\r\n";
  92. typedef enum {
  93. NOTIFY_ALIVE_INIT,
  94. NOTIFY_ALIVE,
  95. NOTIFY_UPDATE,
  96. RESPONSE
  97. } ssdp_message_t;
  98. typedef enum {
  99. ROOT_FOR_ALL,
  100. ROOT_BY_UUID,
  101. ROOT_BY_TYPE
  102. } ssdp_udn_t;
  103. typedef struct {
  104. unsigned long time;
  105. ssdp_message_t type;
  106. ssdp_udn_t udn;
  107. uint32_t address;
  108. uint16_t port;
  109. } ssdp_send_parameters_t;
  110. class SSDPDeviceClass {
  111. private:
  112. WiFiUDP *m_server;
  113. IPAddress m_last;
  114. char m_schemaURL[SSDP_SCHEMA_URL_SIZE];
  115. char m_uuid[SSDP_UUID_SIZE];
  116. char m_deviceType[SSDP_DEVICE_TYPE_SIZE];
  117. char m_friendlyName[SSDP_FRIENDLY_NAME_SIZE];
  118. char m_serialNumber[SSDP_SERIAL_NUMBER_SIZE];
  119. char m_presentationURL[SSDP_PRESENTATION_URL_SIZE];
  120. char m_manufacturer[SSDP_MANUFACTURER_SIZE];
  121. char m_manufacturerURL[SSDP_MANUFACTURER_URL_SIZE];
  122. char m_modelName[SSDP_MODEL_NAME_SIZE];
  123. char m_modelURL[SSDP_MODEL_URL_SIZE];
  124. char m_modelNumber[SSDP_MODEL_VERSION_SIZE];
  125. uint16_t m_port;
  126. uint8_t m_ttl;
  127. ssdp_send_parameters_t m_queue[SSDP_QUEUE_SIZE];
  128. protected:
  129. bool readLine(String &value);
  130. bool readKeyValue(String &key, String &value);
  131. void postNotifyALive();
  132. void postNotifyUpdate();
  133. void postResponse(long mx);
  134. void postResponse(ssdp_udn_t udn, long mx);
  135. void post(ssdp_message_t type, ssdp_udn_t udn, IPAddress address, uint16_t port, unsigned long time);
  136. void send(ssdp_send_parameters_t *parameters);
  137. public:
  138. SSDPDeviceClass();
  139. void update();
  140. String schema();
  141. void handleClient();
  142. void setDeviceType(const String& deviceType) { setDeviceType(deviceType.c_str()); }
  143. void setDeviceType(const char *deviceType);
  144. void setName(const String& name) { setName(name.c_str()); }
  145. void setName(const char *name);
  146. void setURL(const String& url) { setURL(url.c_str()); }
  147. void setURL(const char *url);
  148. void setSchemaURL(const String& url) { setSchemaURL(url.c_str()); }
  149. void setSchemaURL(const char *url);
  150. void setSerialNumber(const String& serialNumber) { setSerialNumber(serialNumber.c_str()); }
  151. void setSerialNumber(const char *serialNumber);
  152. void setSerialNumber(const uint32_t serialNumber);
  153. void setModelName(const String& name) { setModelName(name.c_str()); }
  154. void setModelName(const char *name);
  155. void setModelNumber(const String& num) { setModelNumber(num.c_str()); }
  156. void setModelNumber(const char *num);
  157. void setModelURL(const String& url) { setModelURL(url.c_str()); }
  158. void setModelURL(const char *url);
  159. void setManufacturer(const String& name) { setManufacturer(name.c_str()); }
  160. void setManufacturer(const char *name);
  161. void setManufacturerURL(const String& url) { setManufacturerURL(url.c_str()); }
  162. void setManufacturerURL(const char *url);
  163. void setHTTPPort(uint16_t port);
  164. void setTTL(uint8_t ttl);
  165. };
  166. extern SSDPDeviceClass SSDPDevice;
  167. #endif