Skip to content

Commit 1f6205e

Browse files
John Doeigrr
authored andcommitted
generate UUID automatically based on chip ID and MAC address
1 parent 966cd82 commit 1f6205e

File tree

3 files changed

+36
-35
lines changed

3 files changed

+36
-35
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266SSDP/ESP8266SSDP.cpp

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ extern "C" {
4141
#define SSDP_URI_SIZE 2
4242
#define SSDP_BUFFER_SIZE 64
4343

44+
#define SSDP_UUID_SIZE 37
45+
#define SSDP_FRIENDLY_NAME_SIZE 64
46+
#define SSDP_SERIAL_NUMBER_SIZE 32
47+
#define SSDP_PRESENTATION_URL_SIZE 128
48+
#define SSDP_MODEL_NAME_SIZE 64
49+
#define SSDP_MODEL_URL_SIZE 128
50+
#define SSDP_MODEL_VERSION_SIZE 32
51+
#define SSDP_MANUFACTURER_SIZE 64
52+
#define SSDP_MANUFACTURER_URL_SIZE 128
53+
4454
static const IPAddress SSDP_MULTICAST_ADDR(239, 255, 255, 250);
4555

4656
const char* _ssdp_response_template =
@@ -58,7 +68,7 @@ const char* _ssdp_packet_template =
5868
"%s" // _ssdp_response_template / _ssdp_notify_template
5969
"CACHE-CONTROL: max-age=%u\r\n" // SSDP_INTERVAL
6070
"SERVER: Arduino/1.0 UPNP/1.1 %s/%s\r\n" // _modelName, _modelNumber
61-
"USN: uuid:%s-%02X%02X%02X%02X%02X%02X\r\n" // _base, _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]
71+
"USN: uuid:%s\r\n" // _uuid
6272
"LOCATION: http://%u.%u.%u.%u/ssdp/schema.xml\r\n" // WiFi.localIP()
6373
"\r\n";
6474

@@ -84,14 +94,13 @@ const char* _ssdp_schema_template =
8494
"<modelURL>%s</modelURL>"
8595
"<manufacturer>%s</manufacturer>"
8696
"<manufacturerURL>%s</manufacturerURL>"
87-
"<UDN>%s-%02X%02X%02X%02X%02X%02X</UDN>" //uuid:UUID
97+
"<UDN>%s</UDN>"
8898
"</device>"
8999
"</root>\r\n"
90100
"\r\n";
91101

92102
SSDPClass::SSDPClass(){
93-
_base = (char*)os_malloc(SSDP_BASE_SIZE);
94-
_mac = (byte*)os_malloc(6);
103+
_uuid = (char*)os_malloc(SSDP_UUID_SIZE);
95104
_friendlyName = (char*)os_malloc(SSDP_FRIENDLY_NAME_SIZE);
96105
_presentationURL = (char*)os_malloc(SSDP_PRESENTATION_URL_SIZE);
97106
_serialNumber = (char*)os_malloc(SSDP_SERIAL_NUMBER_SIZE);
@@ -101,6 +110,7 @@ SSDPClass::SSDPClass(){
101110
_manufacturer = (char*)os_malloc(SSDP_MANUFACTURER_SIZE);
102111
_manufacturerURL = (char*)os_malloc(SSDP_MANUFACTURER_URL_SIZE);
103112

113+
_uuid[0] = '\0';
104114
_modelNumber[0] = '\0';
105115
_friendlyName[0] = '\0';
106116
_presentationURL[0] = '\0';
@@ -113,8 +123,7 @@ SSDPClass::SSDPClass(){
113123
}
114124

115125
SSDPClass::~SSDPClass(){
116-
os_free(_base);
117-
os_free(_mac);
126+
os_free(_uuid);
118127
os_free(_friendlyName);
119128
os_free(_presentationURL);
120129
os_free(_serialNumber);
@@ -126,18 +135,26 @@ SSDPClass::~SSDPClass(){
126135
_pending = false;
127136
}
128137

129-
void SSDPClass::begin(char *base){
138+
void SSDPClass::begin(){
130139
ip_addr_t ifaddr;
131140
ip_addr_t multicast_addr;
132141

133142
_pending = false;
134-
strcpy(_base, base);
135143

136-
WiFi.macAddress(_mac);
137144
ifaddr.addr = WiFi.localIP();
138145
multicast_addr.addr = (uint32_t) SSDP_MULTICAST_ADDR;
139146
igmp_joingroup(&ifaddr, &multicast_addr);
140-
147+
148+
uint8_t mac[6];
149+
WiFi.macAddress(mac);
150+
uint32_t chipId = ESP.getChipId();
151+
sprintf(_uuid, "38323636-4558-%04X-%04X-%02X%02X%02X%02X%02X%02X",
152+
(chipId >> 16) & 0xFFFF, chipId & 0xFFFF,
153+
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]
154+
);
155+
#ifdef DEBUG_SSDP
156+
DEBUG_SSDP.printf("SSDP UUID: %s\n", (char *)_uuid);
157+
#endif
141158
_server.begin(SSDP_PORT);
142159
}
143160

@@ -165,7 +182,7 @@ void SSDPClass::send(ssdp_method_t method){
165182
(method == NONE)?_ssdp_response_template:_ssdp_notify_template,
166183
SSDP_INTERVAL,
167184
_modelName, _modelNumber,
168-
_base, _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5],
185+
_uuid,
169186
(uint8_t)(ip & 0xFF), (uint8_t)((ip >> 8) & 0xFF), (uint8_t)((ip >> 16) & 0xFF), (uint8_t)((ip >> 24) & 0xFF)
170187
);
171188

@@ -182,7 +199,7 @@ void SSDPClass::schema(WiFiClient client){
182199
_modelURL,
183200
_manufacturer,
184201
_manufacturerURL,
185-
_base, _mac[0], _mac[1], _mac[2], _mac[3], _mac[4], _mac[5]
202+
_uuid
186203
);
187204
}
188205

hardware/esp8266com/esp8266/libraries/ESP8266SSDP/ESP8266SSDP.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ License (MIT license):
3535

3636
#define DEBUG_SSDP Serial
3737

38-
#define SSDP_BASE_SIZE 24
39-
#define SSDP_FRIENDLY_NAME_SIZE 32
40-
#define SSDP_SERIAL_NUMBER_SIZE 32
41-
#define SSDP_PRESENTATION_URL_SIZE 32
42-
#define SSDP_MODEL_NAME_SIZE 32
43-
#define SSDP_MODEL_URL_SIZE 32
44-
#define SSDP_MODEL_VERSION_SIZE 32
45-
#define SSDP_MANUFACTURER_SIZE 32
46-
#define SSDP_MANUFACTURER_URL_SIZE 32
47-
4838
typedef enum {
4939
NONE,
5040
SEARCH,
@@ -56,7 +46,7 @@ class SSDPClass{
5646
SSDPClass();
5747
~SSDPClass();
5848

59-
void begin(char *base);
49+
void begin();
6050
uint8_t update();
6151
void send(ssdp_method_t method);
6252
void schema(WiFiClient client);
@@ -78,7 +68,7 @@ class SSDPClass{
7868
unsigned long _notify_time;
7969

8070
uint8_t *_mac;
81-
char *_base;
71+
char *_uuid;
8272
char *_friendlyName;
8373
char *_serialNumber;
8474
char *_presentationURL;

hardware/esp8266com/esp8266/libraries/ESP8266SSDP/examples/SSDP.ino

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,23 @@ ESP8266WebServer HTTP(80);
1111
void setup() {
1212
Serial.begin(115200);
1313
Serial.println();
14-
Serial.println("Booting Sketch...");
14+
Serial.println("Starting WiFi...");
1515

1616
WiFi.mode(WIFI_STA);
1717
WiFi.begin(ssid, password);
1818
if(WiFi.waitForConnectResult() == WL_CONNECTED){
19+
20+
Serial.printf("Starting HTTP...\n");
1921
HTTP.on("/", HTTP_GET, [](){
20-
HTTP.sendHeader("Connection", "close");
21-
HTTP.sendHeader("Access-Control-Allow-Origin", "*");
2222
HTTP.send(200, "text/plain", "Hello World!");
2323
});
2424
HTTP.on("/ssdp/schema.xml", HTTP_GET, [](){
2525
SSDP.schema(HTTP.client());
2626
});
2727
HTTP.begin();
28-
29-
byte mac[6];
30-
char base[24];
31-
WiFi.macAddress(mac);
32-
sprintf(base, "esp8266x-%02x%02x-%02x%02x-%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
3328

34-
Serial.printf("Starting SSDP: %s\n", base);
35-
36-
SSDP.begin((char*)base);
29+
Serial.printf("Starting SSDP...\n");
30+
SSDP.begin();
3731
SSDP.setName((char*)"ESP8266");
3832
SSDP.setSerialNumber((char*)"A0123456789");
3933
SSDP.setURL((char*)"/");

0 commit comments

Comments
 (0)