2
2
3
3
char data[1024 ];
4
4
5
- #ifdef MDNS_H
6
- extern " C" {
7
- #include " osapi.h"
8
- #include " ets_sys.h"
9
- #include " user_interface.h"
10
- }
11
-
12
- char txtRecordSeparator[64 ]=" #" ;
13
-
14
- struct TXTRecord {
15
- char name[128 ] = " " ;
16
- char value[128 ] = " " ;
17
- TXTRecord *_next = NULL ;
18
- };
19
-
20
- struct GoogleHomeService {
21
- char serviceName[256 ] = " " ;
22
- char hostName[256 ] = " " ;
23
- char txtRecordsStr[256 ] = " " ;
24
- uint16_t port = 0 ;
25
- uint8_t ip[4 ] = {0 };
26
- TXTRecord *txtRecords = NULL ;
27
- GoogleHomeService *_next = NULL ;
28
- };
29
-
30
- #define QUESTION_SERVICE " _googlecast._tcp.local"
31
- GoogleHomeService* m_services = NULL ;
32
- #define MAX_MDNS_PACKET_SIZE 512
33
-
34
- void answerCallback (const mdns::Answer* answer){
35
- int i = 0 ;
36
- answer->Display ();
37
- GoogleHomeService* service = m_services;
38
- GoogleHomeService* prev = NULL ;
39
- if (answer->rrtype == MDNS_TYPE_PTR) {
40
- // rdata_buffer is serviceName
41
- while (service != NULL ) {
42
- if (strcmp (service->serviceName , answer->rdata_buffer ) == 0 ) {
43
- //
44
- break ;
45
- }
46
- prev = service;
47
- service = prev->_next ;
48
- }
49
- if (service == NULL ) {
50
- GoogleHomeService* tmp = (GoogleHomeService*)os_malloc (sizeof (struct GoogleHomeService ));
51
- os_strcpy (tmp->serviceName , answer->rdata_buffer );
52
- if (prev == NULL ) {
53
- m_services = tmp;
54
- } else {
55
- prev->_next = tmp;
56
- }
57
- }
58
- } else if (answer->rrtype == MDNS_TYPE_TXT) {
59
- // rdata_buffer is TXT record
60
- while (service != NULL ) {
61
- if (strcmp (service->serviceName , answer->name_buffer ) == 0 ) {
62
- break ;
63
- }
64
- prev = service;
65
- service = prev->_next ;
66
- }
67
- if (service != NULL ) {
68
-
69
- i = 0 ;
70
- char * txt;
71
- char * pos;
72
- TXTRecord* currentTxtRecord = service->txtRecords = (TXTRecord*)os_malloc (sizeof (struct TXTRecord ));
73
- TXTRecord* prevTxtRecord;
74
- do {
75
- txt = strtok (i == 0 ? (char *)answer->rdata_buffer : NULL , (const char *)txtRecordSeparator);
76
- if (txt == NULL ) {
77
- os_free (currentTxtRecord);
78
- prevTxtRecord->_next = NULL ;
79
- break ;
80
- }
81
-
82
- currentTxtRecord->_next = (TXTRecord*)os_malloc (sizeof (struct TXTRecord ));
83
-
84
- pos = strchr (txt, ' =' );
85
- *pos = ' \0 ' ;
86
-
87
- strcpy (currentTxtRecord->name , txt);
88
- strcpy (currentTxtRecord->value , pos + 1 );
89
-
90
- prevTxtRecord = currentTxtRecord;
91
- currentTxtRecord = prevTxtRecord->_next ;
92
-
93
- i++;
94
- } while (true );
95
-
96
- }
97
-
98
- } else if (answer->rrtype == MDNS_TYPE_SRV) {
99
- // rdata_buffer is hostname, port
100
- while (service != NULL ) {
101
- if (strcmp (service->serviceName , answer->name_buffer ) == 0 ) {
102
- break ;
103
- }
104
- prev = service;
105
- service = prev->_next ;
106
- }
107
- if (service != NULL ) {
108
- char * hostCh = strstr (answer->rdata_buffer , " ;host=" );
109
- char * portCh = strstr (answer->rdata_buffer , " ;port=" );
110
- strcpy (service->hostName , hostCh + 6 );
111
- char portStr[6 ] = " " ;
112
- strncpy (portStr, portCh + 6 , hostCh - portCh - 6 );
113
- service->port = atoi (portStr);
114
- }
115
-
116
- } else if (answer->rrtype == MDNS_TYPE_A) {
117
- // rdata_buffer is IP address
118
- unsigned char ip[4 ];
119
- char * ipaddressStr = (char *)answer->rdata_buffer ;
120
- char * iprange;
121
- i = 0 ;
122
- do {
123
- iprange = strtok (i == 0 ? ipaddressStr : NULL , " ." );
124
- if (iprange == NULL ) break ;
125
- ip[i] = (uint8_t )atoi (iprange);
126
- i++;
127
- } while (i < 4 );
128
- while (service != NULL ) {
129
- if (strcmp (service->hostName , answer->name_buffer ) == 0 ) {
130
- for (i=0 ;i<4 ;i++) {
131
- service->ip [i] = ip[i];
132
- }
133
- }
134
- prev = service;
135
- service = prev->_next ;
136
- }
137
- Serial.println (" ========================" );
138
- if (prev->serviceName != NULL ) {
139
- Serial.print (" serv:" );
140
- Serial.println (prev->serviceName );
141
- }
142
- if (prev->txtRecords != NULL ) {
143
- TXTRecord* txt = prev->txtRecords ;
144
- Serial.println (" txt:" );
145
- int j = 0 ;
146
- while (txt != NULL ) {
147
- Serial.print (" " );
148
- Serial.println (j);
149
- Serial.print (" n:" );
150
- Serial.println (txt->name );
151
- Serial.print (" v:" );
152
- Serial.println (txt->value );
153
- txt = txt->_next ;
154
- j++;
155
- }
156
- }
157
- if (prev->hostName != NULL ) {
158
- Serial.print (" host:" );
159
- Serial.println (prev->hostName );
160
- }
161
- if (prev->port > 0 ) {
162
- Serial.print (" port:" );
163
- Serial.println (prev->port );
164
- }
165
- Serial.print (" iadr:" );
166
- Serial.print (prev->ip [0 ]);
167
- Serial.print (" ." );
168
- Serial.print (prev->ip [1 ]);
169
- Serial.print (" ." );
170
- Serial.print (prev->ip [2 ]);
171
- Serial.print (" ." );
172
- Serial.println (prev->ip [3 ]);
173
- Serial.println (" ========================" );
174
- }
175
-
176
- // GoogleHomeService* svc = m_services;
177
-
178
- // i = 0;
179
- // while (svc != NULL) {
180
- // if (svc->ip[0] != 0 || svc->ip[1] != 0 || svc->ip[2] != 0 || svc->ip[3] != 0) {
181
- // Serial.println("========================");
182
- // if (svc->serviceName != NULL) {
183
- // Serial.print(" serv:");
184
- // Serial.println(svc->serviceName);
185
- // }
186
- // if (svc->txtRecords != NULL) {
187
- // TXTRecord* txt = svc->txtRecords;
188
- // Serial.println(" txt:");
189
- // int j = 0;
190
- // while (txt != NULL) {
191
- // Serial.print(" ");
192
- // Serial.println(j);
193
- // Serial.print(" n:");
194
- // Serial.println(txt->name);
195
- // Serial.print(" v:");
196
- // Serial.println(txt->value);
197
- // txt = txt->_next;
198
- // j++;
199
- // }
200
- // }
201
- // if (svc->hostName != NULL) {
202
- // Serial.print(" host:");
203
- // Serial.println(svc->hostName);
204
- // }
205
- // if (svc->port > 0) {
206
- // Serial.print(" port:");
207
- // Serial.println(svc->port);
208
- // }
209
- // Serial.print(" iadr:");
210
- // Serial.print(svc->ip[0]);
211
- // Serial.print(".");
212
- // Serial.print(svc->ip[1]);
213
- // Serial.print(".");
214
- // Serial.print(svc->ip[2]);
215
- // Serial.print(".");
216
- // Serial.println(svc->ip[3]);
217
- // Serial.println("========================");
218
- // }
219
- // svc = svc->_next;
220
- // i++;
221
- // }
222
- // Serial.println();
223
- // answer->Display();
224
- // Serial.println();
225
- }
226
-
227
- byte buffer[MAX_MDNS_PACKET_SIZE];
228
- mdns::MDns my_mdns (NULL , NULL , answerCallback, buffer, MAX_MDNS_PACKET_SIZE);
229
- #endif
230
-
231
5
boolean GoogleHomeNotifier::device (const char * name)
232
6
{
233
7
return this ->device (name, " en" );
@@ -241,27 +15,6 @@ boolean GoogleHomeNotifier::device(const char * name, const char * locale)
241
15
sprintf (hostString, " ESP_%06X" , ESP.getChipId ());
242
16
243
17
if (strcmp (this ->m_name , name) != 0 ) {
244
- #ifdef MDNS_H
245
- for (n = 1 ; n < 32 ; n++) {
246
- txtRecordSeparator[n] = n;
247
- }
248
- txtRecordSeparator[32 ] = ' \' ' ;
249
-
250
- struct mdns ::Query query_mqtt;
251
- strncpy (query_mqtt.qname_buffer , QUESTION_SERVICE, MAX_MDNS_NAME_LEN);
252
- query_mqtt.qtype = MDNS_TYPE_PTR;
253
- query_mqtt.qclass = 1 ; // "INternet"
254
- query_mqtt.unicast_response = 0 ;
255
- query_mqtt.Display ();
256
- my_mdns.AddQuery (query_mqtt);
257
-
258
- my_mdns.Send ();
259
- while (true ) {
260
- my_mdns.loop ();
261
- }
262
- #endif
263
-
264
- #ifdef ESP8266MDNS_H
265
18
int i = 0 ;
266
19
if (!MDNS.begin (hostString)) {
267
20
this ->setLastError (" Failed to set up MDNS responder." );
@@ -285,7 +38,6 @@ boolean GoogleHomeNotifier::device(const char * name, const char * locale)
285
38
286
39
this ->m_ipaddress = MDNS.IP (i);
287
40
this ->m_port = MDNS.port (i);
288
- #endif
289
41
}
290
42
sprintf (this ->m_name , " %s" , name);
291
43
sprintf (this ->m_locale , " %s" , locale);
0 commit comments