-
Notifications
You must be signed in to change notification settings - Fork 1
/
wsdiscovery_api.c
458 lines (401 loc) · 10.1 KB
/
wsdiscovery_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/***************************************************************************
* Copyright (C) 2017 - 2020, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "utilx9.h"
#define DBG_TMP_Y(format,args...) //DBG_LN_Y(format, ## args)
#define DBG_TMP_DUMP(ibuf,len,delim,format,args...) //DBG_TR_DUMP(ibuf,len,delim,format,## args)
CLIST(WSListHead);
static WSDiscoveryX_t *wsd_info = NULL;
clist_t ws_list_head(void)
{
return WSListHead;
}
static WSList_t *ws_entry_push(clist_t head, int mask, const char *XAddrs, const char *Types, const char *MetadataVersion)
{
WSList_t *ws_entry = (WSList_t *)SAFE_CALLOC(1, sizeof(WSList_t));
ws_entry->mask = mask;
SAFE_SNPRINTF(ws_entry->url, sizeof(ws_entry->url), "%s", XAddrs);
if (Types)
{
#if (0)
char *cTypes = SAFE_STRCHR(Types, ':');
if (cTypes)
{
cTypes=cTypes+1;
}
else
{
cTypes = Types;
}
#endif
SAFE_SNPRINTF(ws_entry->types, sizeof(ws_entry->types), "%s", Types);
}
if (MetadataVersion)
{
SAFE_SNPRINTF(ws_entry->version, sizeof(ws_entry->version), "%s", MetadataVersion);
}
clist_push(head, ws_entry);
return ws_entry;
}
static WSList_t *ws_entry_search(clist_t head, const char *XAddrs)
{
WSList_t *cur;
for (cur = clist_head(head); cur != NULL; cur = clist_item_next(cur))
{
if (!SAFE_STRCMP((char *)XAddrs, cur->url))
{
return cur;
}
}
return NULL;
}
static void ws_entry_print_ex(clist_t head)
{
WSList_t *cur = NULL;
for (cur = clist_head(head); cur != NULL; cur = clist_item_next(cur))
{
DBG_IF_LN("(types: %s, version: %s, url: %s)", cur->types, cur->version, cur->url);
}
}
static void ws_entry_item_free(void *item)
{
//WSList_t *cur = (WSList_t *)item;
}
void ws_entry_print(void)
{
ws_entry_print_ex(ws_list_head());
}
void ws_entry_init(void)
{
clist_init(ws_list_head());
}
void ws_entry_free(void)
{
clist_free_ex(ws_list_head(), ws_entry_item_free);
}
void ws_devices_refresh(void)
{
WSDiscoveryX_t *wsd_req = wsdiscovery_get();
if (wsd_req)
{
ws_entry_free();
ws_entry_init();
wsdiscovery_sender(wsd_req, SOAP_ACTION_ID_PROBE_DEVICE);
wsdiscovery_sender(wsd_req, SOAP_ACTION_ID_PROBE_NETWORKVIDEOTRANSMITTER);
}
}
void ws_device_add(ChainX_t *chainX_req, soap_node_t *node)
{
const char *Types = NULL;
soap_node_t *Types_node = soap_element_fetch(node, NULL, "Types", NULL, NULL);
if (Types_node)
{
Types = soap_element_text(Types_node, 0);
}
const char *Scopes = NULL;
soap_node_t *Scopes_node = soap_element_fetch(node, NULL, "Scopes", NULL, NULL);
if (Scopes_node)
{
Scopes = soap_element_text(Scopes_node, 0);
}
const char *XAddrs = NULL;
soap_node_t *XAddrs_node = soap_element_fetch(node, NULL, "XAddrs", NULL, NULL);
if (XAddrs_node)
{
XAddrs = soap_element_text(XAddrs_node, 0);
}
const char *MetadataVersion = NULL;
soap_node_t *MetadataVersion_node = soap_element_fetch(node, NULL, "MetadataVersion", NULL, NULL);
if (MetadataVersion_node)
{
MetadataVersion = soap_element_text(MetadataVersion_node, 0);
}
char *XAddrs_new = NULL;
SAFE_ASPRINTF(XAddrs_new, "%s", XAddrs);
if (XAddrs_new)
{
char *first = NULL;
char *saveptr = NULL;
if ((first = SAFE_STRTOK_R(XAddrs_new, " ", &saveptr)))
{
DBG_TR_LN("(XAddrs: %s)", first);
WSList_t *ws_entry = ws_entry_search(ws_list_head(), first);
if (ws_entry)
{
// found the same
}
else
{
int mask = WSDISCOVERY_FILTER_MASK_OTHERS;
if ((Scopes) && (SAFE_STRSTR((char*)Scopes, "onvif")))
{
mask = WSDISCOVERY_FILTER_MASK_ONVIF;
}
ws_entry_push(ws_list_head(), mask, first, Types, MetadataVersion);
ws_entry_print();
}
}
else
{
DBG_ER_LN("XAddrs is NULL !!!");
}
SAFE_FREE(XAddrs_new);
}
}
void wsdiscovery_probematches_register(wsdiscovery_response_fn cb)
{
WSDiscoveryX_t *wsd_req = wsdiscovery_get();
if (wsd_req)
{
wsd_req->probematches_cb = cb;
}
}
void wsdiscovery_probe_register(wsdiscovery_response_fn cb)
{
WSDiscoveryX_t *wsd_req = wsdiscovery_get();
if (wsd_req)
{
wsd_req->probe_cb = cb;
}
}
void wsdiscovery_hello_register(wsdiscovery_response_fn cb)
{
WSDiscoveryX_t *wsd_req = wsdiscovery_get();
if (wsd_req)
{
wsd_req->hello_cb = cb;
}
}
void wsdiscovery_others_register(wsdiscovery_response_fn cb)
{
WSDiscoveryX_t *wsd_req = wsdiscovery_get();
if (wsd_req)
{
wsd_req->others_cb = cb;
}
}
void wsdiscovery_linked_register(chainX_linked_fn cb)
{
WSDiscoveryX_t *wsd_req = wsdiscovery_get();
if ((wsd_req) && (wsd_req->chainX_req))
{
chainX_linked_register(wsd_req->chainX_req, cb);
}
}
static void wsdiscovery_response(ChainX_t *chainX_req, char *buff, int buff_len)
{
if ((chainX_req) && (buff) &&(buff_len))
{
//DBG_TMP_Y("%s:%d - [%s] \n", inet_ntoa(chainX_req->addr_frm.sin_addr), ntohs(chainX_req->addr_frm.sin_port), buff);
WSDiscoveryX_t *wsd_req = (WSDiscoveryX_t *)chainX_req->c_data;
if (wsd_req)
{
soap_node_t *response_node = soap_load_string(buff);
if (response_node)
{
soap_node_t *Hello_node = soap_element_fetch(response_node, NULL, "Hello", NULL, NULL);
soap_node_t *Probe_node = soap_element_fetch(response_node, NULL, "Probe", NULL, NULL);
soap_node_t *ProbeMatch_node = soap_element_fetch(response_node, NULL, "ProbeMatch", NULL, NULL);
if (Hello_node)
{
if (wsd_req->hello_cb)
{
wsd_req->hello_cb(chainX_req, Hello_node);
}
}
else if (ProbeMatch_node)
{
if (wsd_req->probematches_cb)
{
wsd_req->probematches_cb(chainX_req, ProbeMatch_node);
}
}
else if (Probe_node)
{
if (wsd_req->probe_cb)
{
wsd_req->probe_cb(chainX_req, Probe_node);
}
}
else
{
if (wsd_req->others_cb)
{
wsd_req->others_cb(chainX_req, response_node);
}
}
soap_element_delete(response_node);
}
}
}
}
static char *wsdiscovery_xml(SOAP_ACTION_ID act_id)
{
switch (act_id)
{
case SOAP_ACTION_ID_PROBE_DEVICE:
return WSD_XML_PROBE_DEVICE;
break;
case SOAP_ACTION_ID_PROBE_NETWORKVIDEOTRANSMITTER:
return WSD_XML_PROBE_NETWORKVIDEOTRANSMITTER;
break;
case SOAP_ACTION_ID_PROBE_NETWORKVIDEODISPLAY:
return WSD_XML_PROBE_NETWORKVIDEODISPLAY;
break;
default:
return SOAP_XML_NULL;
break;
}
}
static void wsdiscovery_uuid_gen(ChainX_t *chainX_req, soap_node_t *request_node)
{
//ChainX_t *chainX_req = wsd_req->chainX_req;
//SoapX_t *soap = wsd_req->soap;
if ((chainX_req) && (request_node))
{
soap_node_t *MessageID_node = soap_element_fetch(request_node, NULL, "MessageID", NULL, NULL);
if (MessageID_node)
{
os_random_uuid(chainX_req->session, sizeof(chainX_req->session));
char *messageid = NULL;
SAFE_ASPRINTF(messageid, "uuid:%s", chainX_req->session);
if (messageid)
{
DBG_TR_LN("(messageid: %s)", messageid);
soap_element_text_set(MessageID_node, 0, messageid);
SAFE_FREE(messageid);
}
}
}
}
void wsdiscovery_sender(WSDiscoveryX_t *wsd_req, SOAP_ACTION_ID act_id)
{
if (wsd_req)
{
soap_node_t *request_node = soap_load_string(wsdiscovery_xml(act_id));
if (request_node)
{
wsdiscovery_uuid_gen(wsd_req->chainX_req, request_node);
int buffer_len = 0;
char *buffer = soap_element_2string(request_node);
if ((buffer) && (buffer_len = strlen(buffer)))
{
chainX_multi_sender(wsd_req->chainX_req, buffer, buffer_len);
SAFE_FREE(buffer);
}
soap_element_delete(request_node);
}
}
}
void wsdiscovery_probe(chainX_post_fn cb)
{
if (cb)
{
ChainX_t chainXms =
{
.mode = CHAINX_MODE_ID_MULTI_SENDER,
.sockfd = -1,
.status = 0,
.isfree = 0,
.security = 0,
.noblock = 1,
.select_wait = TIMEOUT_OF_SELECT_1,
.recycle = 5,
};
chainX_ip_set(&chainXms, WS_DISCOVERY_IPV4);
chainX_port_set(&chainXms, WS_DISCOVERY_PORT);
chainX_post_register(&chainXms, cb);
soap_node_t *request_node = soap_load_string(wsdiscovery_xml(SOAP_ACTION_ID_PROBE_DEVICE));
if (request_node)
{
{
wsdiscovery_uuid_gen(&chainXms, request_node);
}
int buffer_len = 0;
char *buffer = soap_element_2string(request_node);
if ((buffer) && (buffer_len = strlen(buffer)))
{
DBG_TMP_Y("%s", buffer);
chainX_multi_sender_and_post(&chainXms, buffer, buffer_len);
SAFE_FREE(buffer);
}
soap_element_delete(request_node);
}
}
}
WSDiscoveryX_t *wsdiscovery_get(void)
{
return wsd_info;
}
static void wsdiscovery_free(WSDiscoveryX_t *wsd_req)
{
if (wsd_req)
{
SAFE_FREE(wsd_req->chainX_req);
SAFE_FREE(wsd_req);
}
}
void wsdiscovery_stop(void)
{
WSDiscoveryX_t *wsd_req = wsdiscovery_get();
if (wsd_req)
{
chainX_thread_stop(wsd_req->chainX_req);
}
}
void wsdiscovery_close(void)
{
WSDiscoveryX_t *wsd_req = wsd_info;
if (wsd_req)
{
chainX_thread_close(wsd_req->chainX_req);
wsdiscovery_free(wsd_req);
wsd_info = NULL;
}
}
static WSDiscoveryX_t *wsdiscovery_init(void)
{
WSDiscoveryX_t *wsd_req = (WSDiscoveryX_t*)SAFE_CALLOC(1, sizeof(WSDiscoveryX_t));
if (wsd_req)
{
ChainX_t *chainX_req = (ChainX_t*)SAFE_CALLOC(1, sizeof(ChainX_t));
if (chainX_req)
{
wsd_req->chainX_req = chainX_req;
chainX_req->mode = CHAINX_MODE_ID_MULTI_RECEIVER;
chainX_req->sockfd = -1;
chainX_req->c_data = (void*)wsd_req;
chainX_req->select_wait = TIMEOUT_OF_SELECT_1;
chainX_req->retry_hold = TIMEOUT_OF_RETRY_HOLD,
chainX_req->noblock = 1;
chainX_ip_set(chainX_req, WS_DISCOVERY_IPV4);
chainX_port_set(chainX_req, WS_DISCOVERY_PORT);
chainX_post_register(chainX_req, wsdiscovery_response);
chainX_thread_init(chainX_req);
}
else
{
SAFE_FREE(wsd_req);
}
}
return wsd_req;
}
WSDiscoveryX_t *wsdiscovery_open(void)
{
if (wsd_info==NULL)
{
wsd_info = wsdiscovery_init();
}
return wsd_info;
}