1 /*******************************************************************************
2 *
3 * Copyright (c) 2000-2003 Intel Corporation
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * - Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * - Neither name of Intel Corporation nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 ******************************************************************************/
31
32 /*!
33 * \addtogroup UpnpSamples
34 *
35 * @{
36 *
37 * \file
38 */
39
40 #define SAMPLE_UTIL_C
41
42 #include "sample_util.h"
43
44 #include <stdarg.h>
45 #include <stdio.h>
46
47 #if !UPNP_HAVE_TOOLS
48 #error "Need upnptools.h to compile samples ; try ./configure --enable-tools"
49 #endif
50
51 static int initialize_init = 1;
52 static int initialize_register = 1;
53
54 /*! Function pointers to use for displaying formatted strings.
55 * Set on Initialization of device. */
56 print_string gPrintFun = NULL;
57 state_update gStateUpdateFun = NULL;
58
59 /*! mutex to control displaying of events */
60 ithread_mutex_t display_mutex;
61
SampleUtil_Initialize(print_string print_function)62 int SampleUtil_Initialize(print_string print_function)
63 {
64 if (initialize_init) {
65 ithread_mutexattr_t attr;
66
67 ithread_mutexattr_init(&attr);
68 ithread_mutexattr_setkind_np(&attr, ITHREAD_MUTEX_RECURSIVE_NP);
69 ithread_mutex_init(&display_mutex, &attr);
70 ithread_mutexattr_destroy(&attr);
71 /* To shut up valgrind mutex warning. */
72 ithread_mutex_lock(&display_mutex);
73 gPrintFun = print_function;
74 ithread_mutex_unlock(&display_mutex);
75 /* Finished initializing. */
76 initialize_init = 0;
77 }
78
79 return UPNP_E_SUCCESS;
80 }
81
SampleUtil_RegisterUpdateFunction(state_update update_function)82 int SampleUtil_RegisterUpdateFunction(state_update update_function)
83 {
84 if (initialize_register) {
85 gStateUpdateFun = update_function;
86 initialize_register = 0;
87 }
88
89 return UPNP_E_SUCCESS;
90 }
91
SampleUtil_Finish()92 int SampleUtil_Finish()
93 {
94 ithread_mutex_destroy(&display_mutex);
95 gPrintFun = NULL;
96 gStateUpdateFun = NULL;
97 initialize_init = 1;
98 initialize_register = 1;
99
100 return UPNP_E_SUCCESS;
101 }
102
SampleUtil_GetElementValue(IXML_Element * element)103 char *SampleUtil_GetElementValue(IXML_Element *element)
104 {
105 IXML_Node *child = ixmlNode_getFirstChild((IXML_Node *)element);
106 char *temp = NULL;
107
108 if (child != 0 && ixmlNode_getNodeType(child) == eTEXT_NODE)
109 temp = strdup(ixmlNode_getNodeValue(child));
110
111 return temp;
112 }
113
SampleUtil_GetFirstServiceList(IXML_Document * doc)114 IXML_NodeList *SampleUtil_GetFirstServiceList(IXML_Document *doc)
115 {
116 IXML_NodeList *ServiceList = NULL;
117 IXML_NodeList *servlistnodelist = NULL;
118 IXML_Node *servlistnode = NULL;
119
120 servlistnodelist =
121 ixmlDocument_getElementsByTagName(doc, "serviceList");
122 if (servlistnodelist && ixmlNodeList_length(servlistnodelist)) {
123 /* we only care about the first service list, from the root
124 * device */
125 servlistnode = ixmlNodeList_item(servlistnodelist, 0);
126 /* create as list of DOM nodes */
127 ServiceList = ixmlElement_getElementsByTagName(
128 (IXML_Element *)servlistnode, "service");
129 }
130 if (servlistnodelist)
131 ixmlNodeList_free(servlistnodelist);
132
133 return ServiceList;
134 }
135
136 #define OLD_FIND_SERVICE_CODE
137 #ifdef OLD_FIND_SERVICE_CODE
138 #else
139 /*
140 * Obtain the service list
141 * n == 0 the first
142 * n == 1 the next in the device list, etc..
143 */
SampleUtil_GetNthServiceList(IXML_Document * doc,unsigned int n)144 static IXML_NodeList *SampleUtil_GetNthServiceList(
145 /*! [in] . */
146 IXML_Document *doc,
147 /*! [in] . */
148 unsigned int n)
149 {
150 IXML_NodeList *ServiceList = NULL;
151 IXML_NodeList *servlistnodelist = NULL;
152 IXML_Node *servlistnode = NULL;
153
154 /* ixmlDocument_getElementsByTagName()
155 * Returns a NodeList of all Elements that match the given
156 * tag name in the order in which they were encountered in a preorder
157 * traversal of the Document tree.
158 *
159 * return (NodeList*) A pointer to a NodeList containing the
160 * matching items or NULL on an error. */
161 SampleUtil_Print("SampleUtil_GetNthServiceList called : n = %d\n", n);
162 servlistnodelist =
163 ixmlDocument_getElementsByTagName(doc, "serviceList");
164 if (servlistnodelist && ixmlNodeList_length(servlistnodelist) &&
165 n < ixmlNodeList_length(servlistnodelist)) {
166 /* For the first service list (from the root device),
167 * we pass 0 */
168 /*servlistnode = ixmlNodeList_item( servlistnodelist, 0 );*/
169
170 /* Retrieves a Node from a NodeList} specified by a
171 * numerical index.
172 *
173 * return (Node*) A pointer to a Node or NULL if there was an
174 * error. */
175 servlistnode = ixmlNodeList_item(servlistnodelist, n);
176 if (!servlistnode) {
177 /* create as list of DOM nodes */
178 ServiceList = ixmlElement_getElementsByTagName(
179 (IXML_Element *)servlistnode, "service");
180 } else
181 SampleUtil_Print("%s(%d): ixmlNodeList_item(nodeList, "
182 "n) returned NULL\n",
183 __FILE__,
184 __LINE__);
185 }
186 if (servlistnodelist)
187 ixmlNodeList_free(servlistnodelist);
188
189 return ServiceList;
190 }
191 #endif
192
SampleUtil_GetFirstDocumentItem(IXML_Document * doc,const char * item)193 char *SampleUtil_GetFirstDocumentItem(IXML_Document *doc, const char *item)
194 {
195 IXML_NodeList *nodeList = NULL;
196 IXML_Node *textNode = NULL;
197 IXML_Node *tmpNode = NULL;
198 const char *nodeValue = NULL;
199 char *ret = NULL;
200
201 nodeList = ixmlDocument_getElementsByTagName(doc, (char *)item);
202 if (nodeList) {
203 tmpNode = ixmlNodeList_item(nodeList, 0);
204 if (tmpNode) {
205 textNode = ixmlNode_getFirstChild(tmpNode);
206 if (!textNode) {
207 SampleUtil_Print("%s(%d): (BUG) "
208 "ixmlNode_getFirstChild("
209 "tmpNode) returned NULL\n",
210 __FILE__,
211 __LINE__);
212 ret = strdup("");
213 goto epilogue;
214 }
215 nodeValue = ixmlNode_getNodeValue(textNode);
216 if (!nodeValue) {
217 SampleUtil_Print(
218 "%s(%d): ixmlNode_getNodeValue "
219 "returned NULL\n",
220 __FILE__,
221 __LINE__);
222 ret = strdup("");
223 goto epilogue;
224 }
225 ret = strdup(nodeValue);
226 if (!ret) {
227 SampleUtil_Print("%s(%d): Error allocating "
228 "memory for XML Node value\n",
229 __FILE__,
230 __LINE__);
231 ret = strdup("");
232 }
233 } else
234 SampleUtil_Print("%s(%d): ixmlNodeList_item(nodeList, "
235 "0) returned NULL\n",
236 __FILE__,
237 __LINE__);
238 } else
239 SampleUtil_Print("%s(%d): Error finding %s in XML Node\n",
240 __FILE__,
241 __LINE__,
242 item);
243
244 epilogue:
245 if (nodeList)
246 ixmlNodeList_free(nodeList);
247
248 return ret;
249 }
250
SampleUtil_GetFirstElementItem(IXML_Element * element,const char * item)251 char *SampleUtil_GetFirstElementItem(IXML_Element *element, const char *item)
252 {
253 IXML_NodeList *nodeList = NULL;
254 IXML_Node *textNode = NULL;
255 IXML_Node *tmpNode = NULL;
256 char *ret = NULL;
257
258 nodeList = ixmlElement_getElementsByTagName(element, (char *)item);
259 if (nodeList == NULL) {
260 SampleUtil_Print("%s(%d): Error finding %s in XML Node\n",
261 __FILE__,
262 __LINE__,
263 item);
264 return NULL;
265 }
266 tmpNode = ixmlNodeList_item(nodeList, 0);
267 if (!tmpNode) {
268 SampleUtil_Print("%s(%d): Error finding %s value in XML Node\n",
269 __FILE__,
270 __LINE__,
271 item);
272 ixmlNodeList_free(nodeList);
273 return NULL;
274 }
275 textNode = ixmlNode_getFirstChild(tmpNode);
276 ret = strdup(ixmlNode_getNodeValue(textNode));
277 if (!ret) {
278 SampleUtil_Print(
279 "%s(%d): Error allocating memory for %s in XML Node\n",
280 __FILE__,
281 __LINE__,
282 item);
283 ixmlNodeList_free(nodeList);
284 return NULL;
285 }
286 ixmlNodeList_free(nodeList);
287
288 return ret;
289 }
290
SampleUtil_PrintEventType(Upnp_EventType S)291 void SampleUtil_PrintEventType(Upnp_EventType S)
292 {
293 switch (S) {
294 /* Discovery */
295 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
296 SampleUtil_Print("UPNP_DISCOVERY_ADVERTISEMENT_ALIVE\n");
297 break;
298 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
299 SampleUtil_Print("UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE\n");
300 break;
301 case UPNP_DISCOVERY_SEARCH_RESULT:
302 SampleUtil_Print("UPNP_DISCOVERY_SEARCH_RESULT\n");
303 break;
304 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
305 SampleUtil_Print("UPNP_DISCOVERY_SEARCH_TIMEOUT\n");
306 break;
307 /* SOAP */
308 case UPNP_CONTROL_ACTION_REQUEST:
309 SampleUtil_Print("UPNP_CONTROL_ACTION_REQUEST\n");
310 break;
311 case UPNP_CONTROL_ACTION_COMPLETE:
312 SampleUtil_Print("UPNP_CONTROL_ACTION_COMPLETE\n");
313 break;
314 case UPNP_CONTROL_GET_VAR_REQUEST:
315 SampleUtil_Print("UPNP_CONTROL_GET_VAR_REQUEST\n");
316 break;
317 case UPNP_CONTROL_GET_VAR_COMPLETE:
318 SampleUtil_Print("UPNP_CONTROL_GET_VAR_COMPLETE\n");
319 break;
320 /* GENA */
321 case UPNP_EVENT_SUBSCRIPTION_REQUEST:
322 SampleUtil_Print("UPNP_EVENT_SUBSCRIPTION_REQUEST\n");
323 break;
324 case UPNP_EVENT_RECEIVED:
325 SampleUtil_Print("UPNP_EVENT_RECEIVED\n");
326 break;
327 case UPNP_EVENT_RENEWAL_COMPLETE:
328 SampleUtil_Print("UPNP_EVENT_RENEWAL_COMPLETE\n");
329 break;
330 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
331 SampleUtil_Print("UPNP_EVENT_SUBSCRIBE_COMPLETE\n");
332 break;
333 case UPNP_EVENT_UNSUBSCRIBE_COMPLETE:
334 SampleUtil_Print("UPNP_EVENT_UNSUBSCRIBE_COMPLETE\n");
335 break;
336 case UPNP_EVENT_AUTORENEWAL_FAILED:
337 SampleUtil_Print("UPNP_EVENT_AUTORENEWAL_FAILED\n");
338 break;
339 case UPNP_EVENT_SUBSCRIPTION_EXPIRED:
340 SampleUtil_Print("UPNP_EVENT_SUBSCRIPTION_EXPIRED\n");
341 break;
342 }
343 }
344
SampleUtil_PrintEvent(Upnp_EventType EventType,const void * Event)345 int SampleUtil_PrintEvent(Upnp_EventType EventType, const void *Event)
346 {
347 ithread_mutex_lock(&display_mutex);
348
349 SampleUtil_Print("====================================================="
350 "=================\n"
351 "-----------------------------------------------------"
352 "-----------------\n");
353 SampleUtil_PrintEventType(EventType);
354 switch (EventType) {
355 /* SSDP */
356 case UPNP_DISCOVERY_ADVERTISEMENT_ALIVE:
357 case UPNP_DISCOVERY_ADVERTISEMENT_BYEBYE:
358 case UPNP_DISCOVERY_SEARCH_RESULT: {
359 UpnpDiscovery *d_event = (UpnpDiscovery *)Event;
360 SampleUtil_Print("ErrCode = %d\n"
361 "Expires = %d\n"
362 "DeviceId = %s\n"
363 "DeviceType = %s\n"
364 "ServiceType = %s\n"
365 "ServiceVer = %s\n"
366 "Location = %s\n"
367 "OS = %s\n"
368 "Date = %s\n"
369 "Ext = %s\n",
370 UpnpDiscovery_get_ErrCode(d_event),
371 UpnpDiscovery_get_Expires(d_event),
372 UpnpString_get_String(
373 UpnpDiscovery_get_DeviceID(d_event)),
374 UpnpString_get_String(
375 UpnpDiscovery_get_DeviceType(d_event)),
376 UpnpString_get_String(
377 UpnpDiscovery_get_ServiceType(d_event)),
378 UpnpString_get_String(
379 UpnpDiscovery_get_ServiceVer(d_event)),
380 UpnpString_get_String(
381 UpnpDiscovery_get_Location(d_event)),
382 UpnpString_get_String(UpnpDiscovery_get_Os(d_event)),
383 UpnpString_get_String(UpnpDiscovery_get_Date(d_event)),
384 UpnpString_get_String(UpnpDiscovery_get_Ext(d_event)));
385 break;
386 }
387 case UPNP_DISCOVERY_SEARCH_TIMEOUT:
388 /* Nothing to print out here */
389 break;
390 /* SOAP */
391 case UPNP_CONTROL_ACTION_REQUEST: {
392 UpnpActionRequest *a_event = (UpnpActionRequest *)Event;
393 IXML_Document *actionRequestDoc = NULL;
394 IXML_Document *actionResultDoc = NULL;
395 char *xmlbuff = NULL;
396
397 SampleUtil_Print("ErrCode = %d\n"
398 "ErrStr = %s\n"
399 "ActionName = %s\n"
400 "UDN = %s\n"
401 "ServiceID = %s\n",
402 UpnpActionRequest_get_ErrCode(a_event),
403 UpnpString_get_String(
404 UpnpActionRequest_get_ErrStr(a_event)),
405 UpnpString_get_String(
406 UpnpActionRequest_get_ActionName(a_event)),
407 UpnpString_get_String(
408 UpnpActionRequest_get_DevUDN(a_event)),
409 UpnpString_get_String(
410 UpnpActionRequest_get_ServiceID(a_event)));
411 actionRequestDoc = UpnpActionRequest_get_ActionRequest(a_event);
412 if (actionRequestDoc) {
413 xmlbuff = ixmlPrintNode((IXML_Node *)actionRequestDoc);
414 if (xmlbuff) {
415 SampleUtil_Print(
416 "ActRequest = %s\n", xmlbuff);
417 ixmlFreeDOMString(xmlbuff);
418 }
419 xmlbuff = NULL;
420 } else {
421 SampleUtil_Print("ActRequest = (null)\n");
422 }
423 actionResultDoc = UpnpActionRequest_get_ActionResult(a_event);
424 if (actionResultDoc) {
425 xmlbuff = ixmlPrintNode((IXML_Node *)actionResultDoc);
426 if (xmlbuff) {
427 SampleUtil_Print(
428 "ActResult = %s\n", xmlbuff);
429 ixmlFreeDOMString(xmlbuff);
430 }
431 xmlbuff = NULL;
432 } else {
433 SampleUtil_Print("ActResult = (null)\n");
434 }
435 break;
436 }
437 case UPNP_CONTROL_ACTION_COMPLETE: {
438 UpnpActionComplete *a_event = (UpnpActionComplete *)Event;
439 char *xmlbuff = NULL;
440 int errCode = UpnpActionComplete_get_ErrCode(a_event);
441 const char *ctrlURL = UpnpString_get_String(
442 UpnpActionComplete_get_CtrlUrl(a_event));
443 IXML_Document *actionRequest =
444 UpnpActionComplete_get_ActionRequest(a_event);
445 IXML_Document *actionResult =
446 UpnpActionComplete_get_ActionResult(a_event);
447
448 SampleUtil_Print("ErrCode = %d\n"
449 "CtrlUrl = %s\n",
450 errCode,
451 ctrlURL);
452 if (actionRequest) {
453 xmlbuff = ixmlPrintNode((IXML_Node *)actionRequest);
454 if (xmlbuff) {
455 SampleUtil_Print(
456 "ActRequest = %s\n", xmlbuff);
457 ixmlFreeDOMString(xmlbuff);
458 }
459 xmlbuff = NULL;
460 } else {
461 SampleUtil_Print("ActRequest = (null)\n");
462 }
463 if (actionResult) {
464 xmlbuff = ixmlPrintNode((IXML_Node *)actionResult);
465 if (xmlbuff) {
466 SampleUtil_Print(
467 "ActResult = %s\n", xmlbuff);
468 ixmlFreeDOMString(xmlbuff);
469 }
470 xmlbuff = NULL;
471 } else {
472 SampleUtil_Print("ActResult = (null)\n");
473 }
474 break;
475 }
476 case UPNP_CONTROL_GET_VAR_REQUEST: {
477 UpnpStateVarRequest *sv_event = (UpnpStateVarRequest *)Event;
478
479 SampleUtil_Print("ErrCode = %d\n"
480 "ErrStr = %s\n"
481 "UDN = %s\n"
482 "ServiceID = %s\n"
483 "StateVarName= %s\n"
484 "CurrentVal = %s\n",
485 UpnpStateVarRequest_get_ErrCode(sv_event),
486 UpnpString_get_String(
487 UpnpStateVarRequest_get_ErrStr(sv_event)),
488 UpnpString_get_String(
489 UpnpStateVarRequest_get_DevUDN(sv_event)),
490 UpnpString_get_String(
491 UpnpStateVarRequest_get_ServiceID(sv_event)),
492 UpnpString_get_String(
493 UpnpStateVarRequest_get_StateVarName(sv_event)),
494 UpnpStateVarRequest_get_CurrentVal(sv_event));
495 break;
496 }
497 case UPNP_CONTROL_GET_VAR_COMPLETE: {
498 UpnpStateVarComplete *sv_event = (UpnpStateVarComplete *)Event;
499
500 SampleUtil_Print("ErrCode = %d\n"
501 "CtrlUrl = %s\n"
502 "StateVarName= %s\n"
503 "CurrentVal = %s\n",
504 UpnpStateVarComplete_get_ErrCode(sv_event),
505 UpnpString_get_String(
506 UpnpStateVarComplete_get_CtrlUrl(sv_event)),
507 UpnpString_get_String(
508 UpnpStateVarComplete_get_StateVarName(
509 sv_event)),
510 UpnpStateVarComplete_get_CurrentVal(sv_event));
511 break;
512 }
513 /* GENA */
514 case UPNP_EVENT_SUBSCRIPTION_REQUEST: {
515 UpnpSubscriptionRequest *sr_event =
516 (UpnpSubscriptionRequest *)Event;
517
518 SampleUtil_Print("ServiceID = %s\n"
519 "UDN = %s\n"
520 "SID = %s\n",
521 UpnpString_get_String(
522 UpnpSubscriptionRequest_get_ServiceId(
523 sr_event)),
524 UpnpString_get_String(
525 UpnpSubscriptionRequest_get_UDN(sr_event)),
526 UpnpString_get_String(
527 UpnpSubscriptionRequest_get_SID(sr_event)));
528 break;
529 }
530 case UPNP_EVENT_RECEIVED: {
531 UpnpEvent *e_event = (UpnpEvent *)Event;
532 char *xmlbuff = NULL;
533
534 xmlbuff = ixmlPrintNode(
535 (IXML_Node *)UpnpEvent_get_ChangedVariables(e_event));
536 SampleUtil_Print("SID = %s\n"
537 "EventKey = %d\n"
538 "ChangedVars = %s\n",
539 UpnpString_get_String(UpnpEvent_get_SID(e_event)),
540 UpnpEvent_get_EventKey(e_event),
541 xmlbuff);
542 ixmlFreeDOMString(xmlbuff);
543 break;
544 }
545 case UPNP_EVENT_RENEWAL_COMPLETE: {
546 UpnpEventSubscribe *es_event = (UpnpEventSubscribe *)Event;
547
548 SampleUtil_Print("SID = %s\n"
549 "ErrCode = %d\n"
550 "TimeOut = %d\n",
551 UpnpString_get_String(
552 UpnpEventSubscribe_get_SID(es_event)),
553 UpnpEventSubscribe_get_ErrCode(es_event),
554 UpnpEventSubscribe_get_TimeOut(es_event));
555 break;
556 }
557 case UPNP_EVENT_SUBSCRIBE_COMPLETE:
558 case UPNP_EVENT_UNSUBSCRIBE_COMPLETE: {
559 UpnpEventSubscribe *es_event = (UpnpEventSubscribe *)Event;
560
561 SampleUtil_Print("SID = %s\n"
562 "ErrCode = %d\n"
563 "PublisherURL= %s\n"
564 "TimeOut = %d\n",
565 UpnpString_get_String(
566 UpnpEventSubscribe_get_SID(es_event)),
567 UpnpEventSubscribe_get_ErrCode(es_event),
568 UpnpString_get_String(
569 UpnpEventSubscribe_get_PublisherUrl(es_event)),
570 UpnpEventSubscribe_get_TimeOut(es_event));
571 break;
572 }
573 case UPNP_EVENT_AUTORENEWAL_FAILED:
574 case UPNP_EVENT_SUBSCRIPTION_EXPIRED: {
575 UpnpEventSubscribe *es_event = (UpnpEventSubscribe *)Event;
576
577 SampleUtil_Print("SID = %s\n"
578 "ErrCode = %d\n"
579 "PublisherURL= %s\n"
580 "TimeOut = %d\n",
581 UpnpString_get_String(
582 UpnpEventSubscribe_get_SID(es_event)),
583 UpnpEventSubscribe_get_ErrCode(es_event),
584 UpnpString_get_String(
585 UpnpEventSubscribe_get_PublisherUrl(es_event)),
586 UpnpEventSubscribe_get_TimeOut(es_event));
587 break;
588 }
589 }
590 SampleUtil_Print("-----------------------------------------------------"
591 "-----------------\n"
592 "====================================================="
593 "=================\n"
594 "\n\n\n");
595
596 ithread_mutex_unlock(&display_mutex);
597
598 return 0;
599 }
600
SampleUtil_FindAndParseService(IXML_Document * DescDoc,const char * location,const char * serviceType,char ** serviceId,char ** eventURL,char ** controlURL)601 int SampleUtil_FindAndParseService(IXML_Document *DescDoc,
602 const char *location,
603 const char *serviceType,
604 char **serviceId,
605 char **eventURL,
606 char **controlURL)
607 {
608 unsigned int i;
609 unsigned long length;
610 int found = 0;
611 int ret;
612 #ifdef OLD_FIND_SERVICE_CODE
613 #else /* OLD_FIND_SERVICE_CODE */
614 unsigned int sindex = 0;
615 #endif /* OLD_FIND_SERVICE_CODE */
616 char *tempServiceType = NULL;
617 char *baseURL = NULL;
618 const char *base = NULL;
619 char *relcontrolURL = NULL;
620 char *releventURL = NULL;
621 IXML_NodeList *serviceList = NULL;
622 IXML_Element *service = NULL;
623
624 baseURL = SampleUtil_GetFirstDocumentItem(DescDoc, "URLBase");
625 if (baseURL)
626 base = baseURL;
627 else
628 base = location;
629 #ifdef OLD_FIND_SERVICE_CODE
630 serviceList = SampleUtil_GetFirstServiceList(DescDoc);
631 #else /* OLD_FIND_SERVICE_CODE */
632 for (sindex = 0; (serviceList = SampleUtil_GetNthServiceList(
633 DescDoc, sindex)) != NULL;
634 sindex++) {
635 tempServiceType = NULL;
636 relcontrolURL = NULL;
637 releventURL = NULL;
638 service = NULL;
639 #endif /* OLD_FIND_SERVICE_CODE */
640 length = ixmlNodeList_length(serviceList);
641 for (i = 0; i < length; i++) {
642 service = (IXML_Element *)ixmlNodeList_item(serviceList, i);
643 tempServiceType = SampleUtil_GetFirstElementItem(
644 (IXML_Element *)service, "serviceType");
645 if (tempServiceType &&
646 strcmp(tempServiceType, serviceType) == 0) {
647 SampleUtil_Print("Found service: %s\n", serviceType);
648 *serviceId = SampleUtil_GetFirstElementItem(
649 service, "serviceId");
650 SampleUtil_Print("serviceId: %s\n", *serviceId);
651 relcontrolURL = SampleUtil_GetFirstElementItem(
652 service, "controlURL");
653 releventURL = SampleUtil_GetFirstElementItem(
654 service, "eventSubURL");
655 ret = UpnpResolveURL2(base, relcontrolURL, controlURL);
656 if (ret != UPNP_E_SUCCESS)
657 SampleUtil_Print("Error generating controlURL "
658 "from %s + %s\n",
659 base,
660 relcontrolURL);
661 ret = UpnpResolveURL2(base, releventURL, eventURL);
662 if (ret != UPNP_E_SUCCESS)
663 SampleUtil_Print("Error generating eventURL "
664 "from %s + %s\n",
665 base,
666 releventURL);
667 free(relcontrolURL);
668 free(releventURL);
669 relcontrolURL = NULL;
670 releventURL = NULL;
671 found = 1;
672 break;
673 }
674 free(tempServiceType);
675 tempServiceType = NULL;
676 }
677 free(tempServiceType);
678 tempServiceType = NULL;
679 if (serviceList)
680 ixmlNodeList_free(serviceList);
681 serviceList = NULL;
682 #ifdef OLD_FIND_SERVICE_CODE
683 #else /* OLD_FIND_SERVICE_CODE */
684 }
685 #endif /* OLD_FIND_SERVICE_CODE */
686 free(baseURL);
687
688 return found;
689 }
690
SampleUtil_Print(const char * fmt,...)691 int SampleUtil_Print(const char *fmt, ...)
692 {
693 #define MAX_BUF (8 * 1024)
694 va_list ap;
695 static char buf[MAX_BUF];
696 int rc;
697
698 /* Protect both the display and the static buffer with the mutex */
699 ithread_mutex_lock(&display_mutex);
700
701 va_start(ap, fmt);
702 rc = vsnprintf(buf, MAX_BUF, fmt, ap);
703 va_end(ap);
704 if (gPrintFun)
705 gPrintFun("%s", buf);
706
707 ithread_mutex_unlock(&display_mutex);
708
709 return rc;
710 }
711
SampleUtil_StateUpdate(const char * varName,const char * varValue,const char * UDN,eventType type)712 void SampleUtil_StateUpdate(const char *varName,
713 const char *varValue,
714 const char *UDN,
715 eventType type)
716 {
717 /* TBD: Add mutex here? */
718 if (gStateUpdateFun)
719 gStateUpdateFun(varName, varValue, UDN, type);
720 }
721
722 /*!
723 * \brief Prints a string to standard out.
724 */
linux_print(const char * format,...)725 void linux_print(const char *format, ...)
726 {
727 va_list argList;
728
729 va_start(argList, format);
730 vfprintf(stdout, format, argList);
731 fflush(stdout);
732 va_end(argList);
733 }
734
735 /*! @} UpnpSamples */
736