forked from pupnp/pupnp
/
tv_ctrlpt.c
1713 lines (1568 loc) · 65.8 KB
/
tv_ctrlpt.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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
*
* Copyright (c) 2000-2003 Intel Corporation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither name of Intel Corporation nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************/
/*!
* \addtogroup UpnpSamples
*
* @{
*
* \name Control Point Sample Module
*
* @{
*
* \file
*/
#include "tv_ctrlpt.h"
#include "upnp.h"
/*!
* Mutex for protecting the global device list in a multi-threaded,
* asynchronous environment. All functions should lock this mutex before
* reading or writing the device list.
*/
pthread_mutex_t DeviceListMutex;
UpnpClient_Handle ctrlpt_handle = -1;
/*! Device type for tv device. */
const char TvDeviceType[] = "urn:schemas-upnp-org:device:tvdevice:1";
/*! Service names.*/
const char *TvServiceName[] = {"Control", "Picture"};
/*!
Global arrays for storing variable names and counts for
TvControl and TvPicture services
*/
const char *TvVarName[TV_SERVICE_SERVCOUNT][TV_MAXVARS] = {
{"Power", "Channel", "Volume", ""},
{"Color", "Tint", "Contrast", "Brightness"}};
char TvVarCount[TV_SERVICE_SERVCOUNT] = {
TV_CONTROL_VARCOUNT, TV_PICTURE_VARCOUNT};
/*!
Timeout to request during subscriptions
*/
int default_timeout = 1801;
/*!
The first node in the global device list, or NULL if empty
*/
struct TvDeviceNode *GlobalDeviceList = NULL;
/********************************************************************************
* TvCtrlPointDeleteNode
*
* Description:
* Delete a device node from the global device list. Note that this
* function is NOT thread safe, and should be called from another
* function that has already locked the global device list.
*
* Parameters:
* node -- The device node
*
********************************************************************************/
int TvCtrlPointDeleteNode(UpnpLib *p, struct TvDeviceNode *node)
{
int rc, service, var;
if (NULL == node) {
SampleUtil_Print(
"ERROR: TvCtrlPointDeleteNode: Node is empty\n");
return TV_ERROR;
}
for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) {
/*
If we have a valid control SID, then unsubscribe
*/
if (strcmp(node->device.TvService[service].SID, "") != 0) {
rc = UpnpUnSubscribe(p,
ctrlpt_handle,
node->device.TvService[service].SID);
if (UPNP_E_SUCCESS == rc) {
SampleUtil_Print("Unsubscribed from Tv %s "
"EventURL with SID=%s\n",
TvServiceName[service],
node->device.TvService[service].SID);
} else {
SampleUtil_Print("Error unsubscribing to Tv %s "
"EventURL -- %d\n",
TvServiceName[service],
rc);
}
}
for (var = 0; var < TvVarCount[service]; var++) {
if (node->device.TvService[service]
.VariableStrVal[var]) {
free(node->device.TvService[service]
.VariableStrVal[var]);
}
}
}
/*Notify New Device Added */
SampleUtil_StateUpdate(NULL, NULL, node->device.UDN, DEVICE_REMOVED);
free(node);
node = NULL;
return TV_SUCCESS;
}
/********************************************************************************
* TvCtrlPointRemoveDevice
*
* Description:
* Remove a device from the global device list.
*
* Parameters:
* UDN -- The Unique Device Name for the device to remove
*
********************************************************************************/
int TvCtrlPointRemoveDevice(UpnpLib *p, const char *UDN)
{
struct TvDeviceNode *curdevnode;
struct TvDeviceNode *prevdevnode;
pthread_mutex_lock(&DeviceListMutex);
curdevnode = GlobalDeviceList;
if (!curdevnode) {
SampleUtil_Print("WARNING: TvCtrlPointRemoveDevice: Device "
"list empty\n");
} else {
if (0 == strcmp(curdevnode->device.UDN, UDN)) {
GlobalDeviceList = curdevnode->next;
TvCtrlPointDeleteNode(p, curdevnode);
} else {
prevdevnode = curdevnode;
curdevnode = curdevnode->next;
while (curdevnode) {
if (strcmp(curdevnode->device.UDN, UDN) == 0) {
prevdevnode->next = curdevnode->next;
TvCtrlPointDeleteNode(p, curdevnode);
break;
}
prevdevnode = curdevnode;
curdevnode = curdevnode->next;
}
}
}
pthread_mutex_unlock(&DeviceListMutex);
return TV_SUCCESS;
}
/********************************************************************************
* TvCtrlPointRemoveAll
*
* Description:
* Remove all devices from the global device list.
*
* Parameters:
* None
*
********************************************************************************/
int TvCtrlPointRemoveAll(UpnpLib *p)
{
struct TvDeviceNode *curdevnode, *next;
pthread_mutex_lock(&DeviceListMutex);
curdevnode = GlobalDeviceList;
GlobalDeviceList = NULL;
while (curdevnode) {
next = curdevnode->next;
TvCtrlPointDeleteNode(p, curdevnode);
curdevnode = next;
}
pthread_mutex_unlock(&DeviceListMutex);
return TV_SUCCESS;
}
/********************************************************************************
* TvCtrlPointRefresh
*
* Description:
* Clear the current global device list and issue new search
* requests to build it up again from scratch.
*
* Parameters:
* None
*
********************************************************************************/
int TvCtrlPointRefresh(UpnpLib *p)
{
int rc;
TvCtrlPointRemoveAll(p);
/* Search for all devices of type tvdevice version 1,
* waiting for up to 5 seconds for the response */
rc = UpnpSearchAsync(p, ctrlpt_handle, 5, TvDeviceType, NULL);
if (UPNP_E_SUCCESS != rc) {
SampleUtil_Print("Error sending search request%d\n", rc);
return TV_ERROR;
}
return TV_SUCCESS;
}
/********************************************************************************
* TvCtrlPointGetVar
*
* Description:
* Send a GetVar request to the specified service of a device.
*
* Parameters:
* service -- The service
* devnum -- The number of the device (order in the list,
* starting with 1)
* varname -- The name of the variable to request.
*
********************************************************************************/
int TvCtrlPointGetVar(UpnpLib *p, int service, int devnum, const char *varname)
{
struct TvDeviceNode *devnode;
int rc;
pthread_mutex_lock(&DeviceListMutex);
rc = TvCtrlPointGetDevice(devnum, &devnode);
if (TV_SUCCESS == rc) {
rc = UpnpGetServiceVarStatusAsync(p,
ctrlpt_handle,
devnode->device.TvService[service].ControlURL,
varname,
TvCtrlPointCallbackEventHandler,
NULL);
if (rc != UPNP_E_SUCCESS) {
SampleUtil_Print(
"Error in UpnpGetServiceVarStatusAsync -- %d\n",
rc);
rc = TV_ERROR;
}
}
pthread_mutex_unlock(&DeviceListMutex);
return rc;
}
int TvCtrlPointGetPower(UpnpLib *p, int devnum)
{
return TvCtrlPointGetVar(p, TV_SERVICE_CONTROL, devnum, "Power");
}
int TvCtrlPointGetChannel(UpnpLib *p, int devnum)
{
return TvCtrlPointGetVar(p, TV_SERVICE_CONTROL, devnum, "Channel");
}
int TvCtrlPointGetVolume(UpnpLib *p, int devnum)
{
return TvCtrlPointGetVar(p, TV_SERVICE_CONTROL, devnum, "Volume");
}
int TvCtrlPointGetColor(UpnpLib *p, int devnum)
{
return TvCtrlPointGetVar(p, TV_SERVICE_PICTURE, devnum, "Color");
}
int TvCtrlPointGetTint(UpnpLib *p, int devnum)
{
return TvCtrlPointGetVar(p, TV_SERVICE_PICTURE, devnum, "Tint");
}
int TvCtrlPointGetContrast(UpnpLib *p, int devnum)
{
return TvCtrlPointGetVar(p, TV_SERVICE_PICTURE, devnum, "Contrast");
}
int TvCtrlPointGetBrightness(UpnpLib *p, int devnum)
{
return TvCtrlPointGetVar(p, TV_SERVICE_PICTURE, devnum, "Brightness");
}
/********************************************************************************
* TvCtrlPointSendAction
*
* Description:
* Send an Action request to the specified service of a device.
*
* Parameters:
* service -- The service
* devnum -- The number of the device (order in the list,
* starting with 1)
* actionname -- The name of the action.
* param_name -- An array of parameter names
* param_val -- The corresponding parameter values
* param_count -- The number of parameters
*
********************************************************************************/
int TvCtrlPointSendAction(UpnpLib *p,
int service,
int devnum,
const char *actionname,
const char **param_name,
char **param_val,
int param_count)
{
struct TvDeviceNode *devnode;
IXML_Document *actionNode = NULL;
int rc = TV_SUCCESS;
int param;
pthread_mutex_lock(&DeviceListMutex);
rc = TvCtrlPointGetDevice(devnum, &devnode);
if (TV_SUCCESS == rc) {
if (0 == param_count) {
actionNode = UpnpMakeAction(
actionname, TvServiceType[service], 0, NULL);
} else {
for (param = 0; param < param_count; param++) {
if (UpnpAddToAction(&actionNode,
actionname,
TvServiceType[service],
param_name[param],
param_val[param]) !=
UPNP_E_SUCCESS) {
SampleUtil_Print(
"ERROR: TvCtrlPointSendAction: "
"Trying to add action param\n");
/*return -1; // TBD - BAD! leaves mutex
* locked */
}
}
}
rc = UpnpSendActionAsync(p,
ctrlpt_handle,
devnode->device.TvService[service].ControlURL,
TvServiceType[service],
NULL,
actionNode,
TvCtrlPointCallbackEventHandler,
NULL);
if (rc != UPNP_E_SUCCESS) {
SampleUtil_Print(
"Error in UpnpSendActionAsync -- %d\n", rc);
rc = TV_ERROR;
}
}
pthread_mutex_unlock(&DeviceListMutex);
if (actionNode)
ixmlDocument_free(actionNode);
return rc;
}
/********************************************************************************
* TvCtrlPointSendActionNumericArg
*
* Description:Send an action with one argument to a device in the global device
*list.
*
* Parameters:
* devnum -- The number of the device (order in the list, starting with 1)
* service -- TV_SERVICE_CONTROL or TV_SERVICE_PICTURE
* actionName -- The device action, i.e., "SetChannel"
* paramName -- The name of the parameter that is being passed
* paramValue -- Actual value of the parameter being passed
*
********************************************************************************/
int TvCtrlPointSendActionNumericArg(UpnpLib *p,
int devnum,
int service,
const char *actionName,
const char *paramName,
int paramValue)
{
char param_val_a[50];
char *param_val = param_val_a;
sprintf(param_val_a, "%d", paramValue);
return TvCtrlPointSendAction(
p, service, devnum, actionName, ¶mName, ¶m_val, 1);
}
int TvCtrlPointSendPowerOn(UpnpLib *p, int devnum)
{
return TvCtrlPointSendAction(
p, TV_SERVICE_CONTROL, devnum, "PowerOn", NULL, NULL, 0);
}
int TvCtrlPointSendPowerOff(UpnpLib *p, int devnum)
{
return TvCtrlPointSendAction(
p, TV_SERVICE_CONTROL, devnum, "PowerOff", NULL, NULL, 0);
}
int TvCtrlPointSendSetChannel(UpnpLib *p, int devnum, int channel)
{
return TvCtrlPointSendActionNumericArg(p,
devnum,
TV_SERVICE_CONTROL,
"SetChannel",
"Channel",
channel);
}
int TvCtrlPointSendSetVolume(UpnpLib *p, int devnum, int volume)
{
return TvCtrlPointSendActionNumericArg(
p, devnum, TV_SERVICE_CONTROL, "SetVolume", "Volume", volume);
}
int TvCtrlPointSendSetColor(UpnpLib *p, int devnum, int color)
{
return TvCtrlPointSendActionNumericArg(
p, devnum, TV_SERVICE_PICTURE, "SetColor", "Color", color);
}
int TvCtrlPointSendSetTint(UpnpLib *p, int devnum, int tint)
{
return TvCtrlPointSendActionNumericArg(
p, devnum, TV_SERVICE_PICTURE, "SetTint", "Tint", tint);
}
int TvCtrlPointSendSetContrast(UpnpLib *p, int devnum, int contrast)
{
return TvCtrlPointSendActionNumericArg(p,
devnum,
TV_SERVICE_PICTURE,
"SetContrast",
"Contrast",
contrast);
}
int TvCtrlPointSendSetBrightness(UpnpLib *p, int devnum, int brightness)
{
return TvCtrlPointSendActionNumericArg(p,
devnum,
TV_SERVICE_PICTURE,
"SetBrightness",
"Brightness",
brightness);
}
/********************************************************************************
* TvCtrlPointGetDevice
*
* Description:
* Given a list number, returns the pointer to the device
* node at that position in the global device list. Note
* that this function is not thread safe. It must be called
* from a function that has locked the global device list.
*
* Parameters:
* devnum -- The number of the device (order in the list,
* starting with 1)
* devnode -- The output device node pointer
*
********************************************************************************/
int TvCtrlPointGetDevice(int devnum, struct TvDeviceNode **devnode)
{
int count = devnum;
struct TvDeviceNode *tmpdevnode = NULL;
if (count)
tmpdevnode = GlobalDeviceList;
while (--count && tmpdevnode) {
tmpdevnode = tmpdevnode->next;
}
if (!tmpdevnode) {
SampleUtil_Print(
"Error finding TvDevice number -- %d\n", devnum);
return TV_ERROR;
}
*devnode = tmpdevnode;
return TV_SUCCESS;
}
/********************************************************************************
* TvCtrlPointPrintList
*
* Description:
* Print the universal device names for each device in the global device
*list
*
* Parameters:
* None
*
********************************************************************************/
int TvCtrlPointPrintList()
{
struct TvDeviceNode *tmpdevnode;
int i = 0;
pthread_mutex_lock(&DeviceListMutex);
SampleUtil_Print("TvCtrlPointPrintList:\n");
tmpdevnode = GlobalDeviceList;
while (tmpdevnode) {
SampleUtil_Print(" %3d -- %s\n", ++i, tmpdevnode->device.UDN);
tmpdevnode = tmpdevnode->next;
}
SampleUtil_Print("\n");
pthread_mutex_unlock(&DeviceListMutex);
return TV_SUCCESS;
}
/********************************************************************************
* TvCtrlPointPrintDevice
*
* Description:
* Print the identifiers and state table for a device from
* the global device list.
*
* Parameters:
* devnum -- The number of the device (order in the list,
* starting with 1)
*
********************************************************************************/
int TvCtrlPointPrintDevice(int devnum)
{
struct TvDeviceNode *tmpdevnode;
int i = 0, service, var;
char spacer[15];
if (devnum <= 0) {
SampleUtil_Print("Error in TvCtrlPointPrintDevice: "
"invalid devnum = %d\n",
devnum);
return TV_ERROR;
}
pthread_mutex_lock(&DeviceListMutex);
SampleUtil_Print("TvCtrlPointPrintDevice:\n");
tmpdevnode = GlobalDeviceList;
while (tmpdevnode) {
i++;
if (i == devnum)
break;
tmpdevnode = tmpdevnode->next;
}
if (!tmpdevnode) {
SampleUtil_Print(
"Error in TvCtrlPointPrintDevice: "
"invalid devnum = %d -- actual device count = %d\n",
devnum,
i);
} else {
SampleUtil_Print(" TvDevice -- %d\n"
" | \n"
" +- UDN = %s\n"
" +- DescDocURL = %s\n"
" +- FriendlyName = %s\n"
" +- PresURL = %s\n"
" +- Adver. TimeOut = %d\n",
devnum,
tmpdevnode->device.UDN,
tmpdevnode->device.DescDocURL,
tmpdevnode->device.FriendlyName,
tmpdevnode->device.PresURL,
tmpdevnode->device.AdvrTimeOut);
for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) {
if (service < TV_SERVICE_SERVCOUNT - 1)
sprintf(spacer, " | ");
else
sprintf(spacer, " ");
SampleUtil_Print(" | \n"
" +- Tv %s Service\n"
"%s+- ServiceId = %s\n"
"%s+- ServiceType = %s\n"
"%s+- EventURL = %s\n"
"%s+- ControlURL = %s\n"
"%s+- SID = %s\n"
"%s+- ServiceStateTable\n",
TvServiceName[service],
spacer,
tmpdevnode->device.TvService[service].ServiceId,
spacer,
tmpdevnode->device.TvService[service]
.ServiceType,
spacer,
tmpdevnode->device.TvService[service].EventURL,
spacer,
tmpdevnode->device.TvService[service]
.ControlURL,
spacer,
tmpdevnode->device.TvService[service].SID,
spacer);
for (var = 0; var < TvVarCount[service]; var++) {
SampleUtil_Print("%s +- %-10s = %s\n",
spacer,
TvVarName[service][var],
tmpdevnode->device.TvService[service]
.VariableStrVal[var]);
}
}
}
SampleUtil_Print("\n");
pthread_mutex_unlock(&DeviceListMutex);
return TV_SUCCESS;
}
/********************************************************************************
* TvCtrlPointAddDevice
*
* Description:
* If the device is not already included in the global device list,
* add it. Otherwise, update its advertisement expiration timeout.
*
* Parameters:
* DescDoc -- The description document for the device
* location -- The location of the description document URL
* expires -- The expiration time for this advertisement
*
********************************************************************************/
void TvCtrlPointAddDevice(
UpnpLib *p, IXML_Document *DescDoc, const char *location, int expires)
{
char *deviceType = NULL;
char *friendlyName = NULL;
char *presURL = NULL;
char *baseURL = NULL;
char *relURL = NULL;
char *UDN = NULL;
char *serviceId[TV_SERVICE_SERVCOUNT] = {NULL, NULL};
char *eventURL[TV_SERVICE_SERVCOUNT] = {NULL, NULL};
char *controlURL[TV_SERVICE_SERVCOUNT] = {NULL, NULL};
Upnp_SID eventSID[TV_SERVICE_SERVCOUNT];
int TimeOut[TV_SERVICE_SERVCOUNT];
struct TvDeviceNode *deviceNode;
struct TvDeviceNode *tmpdevnode;
int ret = 1;
int found = 0;
int service;
int var;
TimeOut[0] = default_timeout;
TimeOut[1] = default_timeout;
pthread_mutex_lock(&DeviceListMutex);
/* Read key elements from description document */
UDN = SampleUtil_GetFirstDocumentItem(DescDoc, "UDN");
deviceType = SampleUtil_GetFirstDocumentItem(DescDoc, "deviceType");
friendlyName = SampleUtil_GetFirstDocumentItem(DescDoc, "friendlyName");
baseURL = SampleUtil_GetFirstDocumentItem(DescDoc, "URLBase");
relURL = SampleUtil_GetFirstDocumentItem(DescDoc, "presentationURL");
ret = UpnpResolveURL2(
p, (baseURL ? baseURL : location), relURL, &presURL);
if (UPNP_E_SUCCESS != ret)
SampleUtil_Print("Error generating presURL from %s + %s\n",
baseURL,
relURL);
if (strcmp(deviceType, TvDeviceType) == 0) {
SampleUtil_Print("Found Tv device\n");
/* Check if this device is already in the list */
tmpdevnode = GlobalDeviceList;
while (tmpdevnode) {
if (strcmp(tmpdevnode->device.UDN, UDN) == 0) {
found = 1;
break;
}
tmpdevnode = tmpdevnode->next;
}
if (found) {
/* The device is already there, so just update */
/* the advertisement timeout field */
tmpdevnode->device.AdvrTimeOut = expires;
} else {
for (service = 0; service < TV_SERVICE_SERVCOUNT;
service++) {
if (SampleUtil_FindAndParseService(p,
DescDoc,
location,
TvServiceType[service],
&serviceId[service],
&eventURL[service],
&controlURL[service])) {
SampleUtil_Print("Subscribing to "
"EventURL %s...\n",
eventURL[service]);
ret = UpnpSubscribe(p,
ctrlpt_handle,
eventURL[service],
&TimeOut[service],
eventSID[service]);
if (ret == UPNP_E_SUCCESS) {
SampleUtil_Print(
"Subscribed to "
"EventURL with "
"SID=%s\n",
eventSID[service]);
} else {
SampleUtil_Print(
"Error Subscribing to "
"EventURL -- %d\n",
ret);
strcpy(eventSID[service], "");
}
} else {
SampleUtil_Print("Error: Could not "
"find Service: %s\n",
TvServiceType[service]);
}
}
/* Create a new device node */
deviceNode = (struct TvDeviceNode *)malloc(
sizeof(struct TvDeviceNode));
strcpy(deviceNode->device.UDN, UDN);
strcpy(deviceNode->device.DescDocURL, location);
strcpy(deviceNode->device.FriendlyName, friendlyName);
strcpy(deviceNode->device.PresURL, presURL);
deviceNode->device.AdvrTimeOut = expires;
for (service = 0; service < TV_SERVICE_SERVCOUNT;
service++) {
if (serviceId[service] == NULL) {
/* not found */
continue;
}
strcpy(deviceNode->device.TvService[service]
.ServiceId,
serviceId[service]);
strcpy(deviceNode->device.TvService[service]
.ServiceType,
TvServiceType[service]);
strcpy(deviceNode->device.TvService[service]
.ControlURL,
controlURL[service]);
strcpy(deviceNode->device.TvService[service]
.EventURL,
eventURL[service]);
strcpy(deviceNode->device.TvService[service]
.SID,
eventSID[service]);
for (var = 0; var < TvVarCount[service];
var++) {
deviceNode->device.TvService[service]
.VariableStrVal[var] =
(char *)malloc(TV_MAX_VAL_LEN);
strcpy(deviceNode->device
.TvService[service]
.VariableStrVal[var],
"");
}
}
deviceNode->next = NULL;
/* Insert the new device node in the list */
if ((tmpdevnode = GlobalDeviceList)) {
while (tmpdevnode) {
if (tmpdevnode->next) {
tmpdevnode = tmpdevnode->next;
} else {
tmpdevnode->next = deviceNode;
break;
}
}
} else {
GlobalDeviceList = deviceNode;
}
/*Notify New Device Added */
SampleUtil_StateUpdate(NULL,
NULL,
deviceNode->device.UDN,
DEVICE_ADDED);
}
}
pthread_mutex_unlock(&DeviceListMutex);
if (deviceType)
free(deviceType);
if (friendlyName)
free(friendlyName);
if (UDN)
free(UDN);
if (baseURL)
free(baseURL);
if (relURL)
free(relURL);
if (presURL)
free(presURL);
for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) {
if (serviceId[service])
free(serviceId[service]);
if (controlURL[service])
free(controlURL[service]);
if (eventURL[service])
free(eventURL[service]);
}
}
void TvStateUpdate(
char *UDN, int Service, IXML_Document *ChangedVariables, char **State)
{
IXML_NodeList *properties;
IXML_NodeList *variables;
IXML_Element *property;
IXML_Element *variable;
long unsigned int length;
long unsigned int length1;
long unsigned int i;
int j;
char *tmpstate = NULL;
(void)UDN;
SampleUtil_Print("Tv State Update (service %d):\n", Service);
/* Find all of the e:property tags in the document */
properties = ixmlDocument_getElementsByTagName(
ChangedVariables, "e:property");
if (properties) {
length = ixmlNodeList_length(properties);
for (i = 0; i < length; i++) {
/* Loop through each property change found */
property = (IXML_Element *)ixmlNodeList_item(
properties, i);
/* For each variable name in the state table,
* check if this is a corresponding property change */
for (j = 0; j < TvVarCount[Service]; j++) {
variables = ixmlElement_getElementsByTagName(
property, TvVarName[Service][j]);
/* If a match is found, extract
* the value, and update the state table */
if (variables) {
length1 =
ixmlNodeList_length(variables);
if (length1) {
variable = (IXML_Element *)
ixmlNodeList_item(
variables, 0);
tmpstate =
SampleUtil_GetElementValue(
variable);
if (tmpstate) {
strcpy(State[j],
tmpstate);
SampleUtil_Print(
" Variable "
"Name: %s New "
"Value:'%s'\n",
TvVarName
[Service]
[j],
State[j]);
}
if (tmpstate)
free(tmpstate);
tmpstate = NULL;
}
ixmlNodeList_free(variables);
variables = NULL;
}
}
}
ixmlNodeList_free(properties);
}
return;
}
/********************************************************************************
* TvCtrlPointHandleEvent
*
* Description:
* Handle a UPnP event that was received. Process the event and update
* the appropriate service state table.
*
* Parameters:
* sid -- The subscription id for the event
* eventkey -- The eventkey number for the event
* changes -- The DOM document representing the changes
*
********************************************************************************/
void TvCtrlPointHandleEvent(
const char *sid, int evntkey, IXML_Document *changes)
{
struct TvDeviceNode *tmpdevnode;
int service;
pthread_mutex_lock(&DeviceListMutex);
tmpdevnode = GlobalDeviceList;
while (tmpdevnode) {
for (service = 0; service < TV_SERVICE_SERVCOUNT; ++service) {
if (strcmp(tmpdevnode->device.TvService[service].SID,
sid) == 0) {
SampleUtil_Print(
"Received Tv %s Event: %d for SID %s\n",
TvServiceName[service],
evntkey,
sid);
TvStateUpdate(tmpdevnode->device.UDN,
service,
changes,
(char **)&tmpdevnode->device
.TvService[service]
.VariableStrVal);
break;
}
}
tmpdevnode = tmpdevnode->next;
}
pthread_mutex_unlock(&DeviceListMutex);
}
/********************************************************************************
* TvCtrlPointHandleSubscribeUpdate
*
* Description:
* Handle a UPnP subscription update that was received. Find the
* service the update belongs to, and update its subscription
* timeout.
*
* Parameters:
* eventURL -- The event URL for the subscription
* sid -- The subscription id for the subscription
* timeout -- The new timeout for the subscription
*
********************************************************************************/
void TvCtrlPointHandleSubscribeUpdate(
const char *eventURL, const Upnp_SID sid, int timeout)
{
struct TvDeviceNode *tmpdevnode;
int service;
(void)timeout;
pthread_mutex_lock(&DeviceListMutex);
tmpdevnode = GlobalDeviceList;
while (tmpdevnode) {
for (service = 0; service < TV_SERVICE_SERVCOUNT; service++) {
if (strcmp(tmpdevnode->device.TvService[service]
.EventURL,
eventURL) == 0) {
SampleUtil_Print("Received Tv %s Event Renewal "
"for eventURL %s\n",
TvServiceName[service],
eventURL);
strcpy(tmpdevnode->device.TvService[service]
.SID,
sid);
break;
}
}
tmpdevnode = tmpdevnode->next;
}
pthread_mutex_unlock(&DeviceListMutex);