pupnp (libupnp) snapshot from SourceForge: git clone git://pupnp.git.sourceforge...
[igd2-for-linux:pandonghui1211s-igd2-for-linux.git] / pupnp_branch-1.6.x / upnp / sample / tvcombo / linux / upnp_tv_combo_main.c
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 #include "sample_util.h"
34 #include "upnp_tv_ctrlpt.h"
35 #include "upnp_tv_device.h"
36
37
38 #include <stdio.h>
39 #include <string.h>
40
41 /*
42    Tags for valid commands issued at the command prompt 
43  */
44 enum cmdloop_tvcmds {
45     PRTHELP = 0, PRTFULLHELP, POWON, POWOFF,
46     SETCHAN, SETVOL, SETCOL, SETTINT, SETCONT, SETBRT,
47     CTRLACTION, PICTACTION, CTRLGETVAR, PICTGETVAR,
48     PRTDEV, LSTDEV, REFRESH, EXITCMD
49 };
50
51 /*
52    Data structure for parsing commands from the command line 
53  */
54 struct cmdloop_commands {
55     char *str;                  // the string 
56     int cmdnum;                 // the command
57     int numargs;                // the number of arguments
58     char *args;                 // the args
59 } cmdloop_commands;
60
61 /*
62    Mappings between command text names, command tag,
63    and required command arguments for command line
64    commands 
65  */
66 static struct cmdloop_commands cmdloop_cmdlist[] = {
67     {"Help", PRTHELP, 1, ""},
68     {"HelpFull", PRTFULLHELP, 1, ""},
69     {"ListDev", LSTDEV, 1, ""},
70     {"Refresh", REFRESH, 1, ""},
71     {"PrintDev", PRTDEV, 2, "<devnum>"},
72     {"PowerOn", POWON, 2, "<devnum>"},
73     {"PowerOff", POWOFF, 2, "<devnum>"},
74     {"SetChannel", SETCHAN, 3, "<devnum> <channel (int)>"},
75     {"SetVolume", SETVOL, 3, "<devnum> <volume (int)>"},
76     {"SetColor", SETCOL, 3, "<devnum> <color (int)>"},
77     {"SetTint", SETTINT, 3, "<devnum> <tint (int)>"},
78     {"SetContrast", SETCONT, 3, "<devnum> <contrast (int)>"},
79     {"SetBrightness", SETBRT, 3, "<devnum> <brightness (int)>"},
80     {"CtrlAction", CTRLACTION, 2, "<devnum> <action (string)>"},
81     {"PictAction", PICTACTION, 2, "<devnum> <action (string)>"},
82     {"CtrlGetVar", CTRLGETVAR, 2, "<devnum> <varname (string)>"},
83     {"PictGetVar", PICTGETVAR, 2, "<devnum> <varname (string)>"},
84     {"Exit", EXITCMD, 1, ""}
85 };
86
87 void
88 linux_print( const char *string )
89 {
90     puts( string );
91 }
92
93 /********************************************************************************
94  * TvCtrlPointPrintHelp
95  *
96  * Description: 
97  *       Print help info for this application.
98  ********************************************************************************/
99 void
100 TvCtrlPointPrintShortHelp( void )
101 {
102     SampleUtil_Print( "Commands:" );
103     SampleUtil_Print( "  Help" );
104     SampleUtil_Print( "  HelpFull" );
105     SampleUtil_Print( "  ListDev" );
106     SampleUtil_Print( "  Refresh" );
107     SampleUtil_Print( "  PrintDev      <devnum>" );
108     SampleUtil_Print( "  PowerOn       <devnum>" );
109     SampleUtil_Print( "  PowerOff      <devnum>" );
110     SampleUtil_Print( "  SetChannel    <devnum> <channel>" );
111     SampleUtil_Print( "  SetVolume     <devnum> <volume>" );
112     SampleUtil_Print( "  SetColor      <devnum> <color>" );
113     SampleUtil_Print( "  SetTint       <devnum> <tint>" );
114     SampleUtil_Print( "  SetContrast   <devnum> <contrast>" );
115     SampleUtil_Print( "  SetBrightness <devnum> <brightness>" );
116     SampleUtil_Print( "  CtrlAction    <devnum> <action>" );
117     SampleUtil_Print( "  PictAction    <devnum> <action>" );
118     SampleUtil_Print( "  CtrlGetVar    <devnum> <varname>" );
119     SampleUtil_Print( "  PictGetVar    <devnum> <action>" );
120     SampleUtil_Print( "  Exit" );
121 }
122
123 void
124 TvCtrlPointPrintLongHelp( void )
125 {
126     SampleUtil_Print( "" );
127     SampleUtil_Print( "******************************" );
128     SampleUtil_Print( "* TV Control Point Help Info *" );
129     SampleUtil_Print( "******************************" );
130     SampleUtil_Print( "" );
131     SampleUtil_Print( "This sample control point application automatically searches" );
132     SampleUtil_Print( "for and subscribes to the services of television device emulator" );
133     SampleUtil_Print( "devices. While registers a tv device itself." );
134     SampleUtil_Print( "" );
135     SampleUtil_Print( "Commands:" );
136     SampleUtil_Print( "  Help" );
137     SampleUtil_Print( "       Print this help info." );
138     SampleUtil_Print( "  ListDev" );
139     SampleUtil_Print( "       Print the current list of TV Device Emulators that this" );
140     SampleUtil_Print( "         control point is aware of.  Each device is preceded by a" );
141     SampleUtil_Print( "         device number which corresponds to the devnum argument of" );
142     SampleUtil_Print( "         commands listed below." );
143     SampleUtil_Print( "  Refresh" );
144     SampleUtil_Print( "       Delete all of the devices from the device list and issue new" );
145     SampleUtil_Print( "         search request to rebuild the list from scratch." );
146     SampleUtil_Print( "  PrintDev       <devnum>" );
147     SampleUtil_Print( "       Print the state table for the device <devnum>." );
148     SampleUtil_Print( "         e.g., 'PrintDev 1' prints the state table for the first" );
149     SampleUtil_Print( "         device in the device list." );
150     SampleUtil_Print( "  PowerOn        <devnum>" );
151     SampleUtil_Print( "       Sends the PowerOn action to the Control Service of" );
152     SampleUtil_Print( "         device <devnum>." );
153     SampleUtil_Print( "  PowerOff       <devnum>" );
154     SampleUtil_Print( "       Sends the PowerOff action to the Control Service of" );
155     SampleUtil_Print( "         device <devnum>." );
156     SampleUtil_Print( "  SetChannel     <devnum> <channel>" );
157     SampleUtil_Print( "       Sends the SetChannel action to the Control Service of" );
158     SampleUtil_Print( "         device <devnum>, requesting the channel to be changed" );
159     SampleUtil_Print( "         to <channel>." );
160     SampleUtil_Print( "  SetVolume      <devnum> <volume>" );
161     SampleUtil_Print( "       Sends the SetVolume action to the Control Service of" );
162     SampleUtil_Print( "         device <devnum>, requesting the volume to be changed" );
163     SampleUtil_Print( "         to <volume>." );
164     SampleUtil_Print( "  SetColor       <devnum> <color>" );
165     SampleUtil_Print( "       Sends the SetColor action to the Control Service of" );
166     SampleUtil_Print( "         device <devnum>, requesting the color to be changed" );
167     SampleUtil_Print( "         to <color>." );
168     SampleUtil_Print( "  SetTint        <devnum> <tint>" );
169     SampleUtil_Print( "       Sends the SetTint action to the Control Service of" );
170     SampleUtil_Print( "         device <devnum>, requesting the tint to be changed" );
171     SampleUtil_Print( "         to <tint>." );
172     SampleUtil_Print( "  SetContrast    <devnum> <contrast>" );
173     SampleUtil_Print( "       Sends the SetContrast action to the Control Service of" );
174     SampleUtil_Print( "         device <devnum>, requesting the contrast to be changed" );
175     SampleUtil_Print( "         to <contrast>." );
176     SampleUtil_Print( "  SetBrightness  <devnum> <brightness>" );
177     SampleUtil_Print( "       Sends the SetBrightness action to the Control Service of" );
178     SampleUtil_Print( "         device <devnum>, requesting the brightness to be changed" );
179     SampleUtil_Print( "         to <brightness>." );
180     SampleUtil_Print( "  CtrlAction     <devnum> <action>" );
181     SampleUtil_Print( "       Sends an action request specified by the string <action>" );
182     SampleUtil_Print( "         to the Control Service of device <devnum>.  This command" );
183     SampleUtil_Print( "         only works for actions that have no arguments." );
184     SampleUtil_Print( "         (e.g., \"CtrlAction 1 IncreaseChannel\")" );
185     SampleUtil_Print( "  PictAction     <devnum> <action>" );
186     SampleUtil_Print( "       Sends an action request specified by the string <action>" );
187     SampleUtil_Print( "         to the Picture Service of device <devnum>.  This command" );
188     SampleUtil_Print( "         only works for actions that have no arguments." );
189     SampleUtil_Print( "         (e.g., \"PictAction 1 DecreaseContrast\")" );
190     SampleUtil_Print( "  CtrlGetVar     <devnum> <varname>" );
191     SampleUtil_Print( "       Requests the value of a variable specified by the string <varname>" );
192     SampleUtil_Print( "         from the Control Service of device <devnum>." );
193     SampleUtil_Print( "         (e.g., \"CtrlGetVar 1 Volume\")" );
194     SampleUtil_Print( "  PictGetVar     <devnum> <action>" );
195     SampleUtil_Print( "       Requests the value of a variable specified by the string <varname>" );
196     SampleUtil_Print( "         from the Picture Service of device <devnum>." );
197     SampleUtil_Print( "         (e.g., \"PictGetVar 1 Tint\")" );
198     SampleUtil_Print( "  Exit" );
199     SampleUtil_Print( "       Exits the control point application." );
200 }
201
202 /********************************************************************************
203  * TvCtrlPointPrintCommands
204  *
205  * Description: 
206  *       Print the list of valid command line commands to the user
207  *
208  * Parameters:
209  *   None
210  *
211  ********************************************************************************/
212 void
213 TvCtrlPointPrintCommands()
214 {
215     int i;
216     int numofcmds = sizeof( cmdloop_cmdlist ) / sizeof( cmdloop_commands );
217
218     SampleUtil_Print( "Valid Commands:" );
219     for( i = 0; i < numofcmds; i++ ) {
220         SampleUtil_Print( "  %-14s %s", cmdloop_cmdlist[i].str,
221                           cmdloop_cmdlist[i].args );
222     }
223     SampleUtil_Print( "" );
224 }
225
226 /********************************************************************************
227  * TvCtrlPointCommandLoop
228  *
229  * Description: 
230  *       Function that receives commands from the user at the command prompt
231  *       during the lifetime of the control point, and calls the appropriate
232  *       functions for those commands.
233  *
234  * Parameters:
235  *    None
236  *
237  ********************************************************************************/
238 void *
239 TvCtrlPointCommandLoop( void *args )
240 {
241     char cmdline[100];
242
243     while( 1 ) {
244         SampleUtil_Print( "\n>> " );
245         fgets( cmdline, 100, stdin );
246         TvCtrlPointProcessCommand( cmdline );
247     }
248
249     return NULL;
250 }
251
252 int
253 TvCtrlPointProcessCommand( char *cmdline )
254 {
255     char cmd[100];
256     char strarg[100];
257     int arg_val_err = -99999;
258     int arg1 = arg_val_err;
259     int arg2 = arg_val_err;
260     int cmdnum = -1;
261     int numofcmds = sizeof( cmdloop_cmdlist ) / sizeof( cmdloop_commands );
262     int cmdfound = 0;
263     int i,
264       rc;
265     int invalidargs = 0;
266     int validargs;
267
268     validargs = sscanf( cmdline, "%s %d %d", cmd, &arg1, &arg2 );
269
270     for( i = 0; i < numofcmds; i++ ) {
271         if( strcasecmp( cmd, cmdloop_cmdlist[i].str ) == 0 ) {
272             cmdnum = cmdloop_cmdlist[i].cmdnum;
273             cmdfound++;
274             if( validargs != cmdloop_cmdlist[i].numargs )
275                 invalidargs++;
276             break;
277         }
278     }
279
280     if( !cmdfound ) {
281         SampleUtil_Print( "Command not found; try 'Help'" );
282         return TV_SUCCESS;
283     }
284
285     if( invalidargs ) {
286         SampleUtil_Print( "Invalid arguments; try 'Help'" );
287         return TV_SUCCESS;
288     }
289
290     switch ( cmdnum ) {
291         case PRTHELP:
292             TvCtrlPointPrintShortHelp();
293             break;
294
295         case PRTFULLHELP:
296             TvCtrlPointPrintLongHelp();
297             break;
298
299         case POWON:
300             TvCtrlPointSendPowerOn( arg1 );
301             break;
302
303         case POWOFF:
304             TvCtrlPointSendPowerOff( arg1 );
305             break;
306
307         case SETCHAN:
308             TvCtrlPointSendSetChannel( arg1, arg2 );
309             break;
310
311         case SETVOL:
312             TvCtrlPointSendSetVolume( arg1, arg2 );
313             break;
314
315         case SETCOL:
316             TvCtrlPointSendSetColor( arg1, arg2 );
317             break;
318
319         case SETTINT:
320             TvCtrlPointSendSetTint( arg1, arg2 );
321             break;
322
323         case SETCONT:
324             TvCtrlPointSendSetContrast( arg1, arg2 );
325             break;
326
327         case SETBRT:
328             TvCtrlPointSendSetBrightness( arg1, arg2 );
329             break;
330
331         case CTRLACTION:
332             /*
333                re-parse commandline since second arg is string 
334              */
335             validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
336             if( 3 == validargs )
337                 TvCtrlPointSendAction( TV_SERVICE_CONTROL, arg1, strarg,
338                                        NULL, NULL, 0 );
339             else
340                 invalidargs++;
341             break;
342
343         case PICTACTION:
344             /*
345                re-parse commandline since second arg is string 
346              */
347             validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
348             if( 3 == validargs )
349                 TvCtrlPointSendAction( TV_SERVICE_PICTURE, arg1, strarg,
350                                        NULL, NULL, 0 );
351             else
352                 invalidargs++;
353             break;
354
355         case CTRLGETVAR:
356             /*
357                re-parse commandline since second arg is string 
358              */
359             validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
360             if( 3 == validargs )
361                 TvCtrlPointGetVar( TV_SERVICE_CONTROL, arg1, strarg );
362             else
363                 invalidargs++;
364             break;
365
366         case PICTGETVAR:
367             /*
368                re-parse commandline since second arg is string 
369              */
370             validargs = sscanf( cmdline, "%s %d %s", cmd, &arg1, strarg );
371             if( 3 == validargs )
372                 TvCtrlPointGetVar( TV_SERVICE_PICTURE, arg1, strarg );
373             else
374                 invalidargs++;
375             break;
376
377         case PRTDEV:
378             TvCtrlPointPrintDevice( arg1 );
379             break;
380
381         case LSTDEV:
382             TvCtrlPointPrintList();
383             break;
384
385         case REFRESH:
386             TvCtrlPointRefresh();
387             break;
388
389         case EXITCMD:
390             rc = TvCtrlPointStop();
391             exit( rc );
392             break;
393
394         default:
395             SampleUtil_Print( "Command not implemented; see 'Help'" );
396             break;
397     }
398
399     if( invalidargs )
400         SampleUtil_Print( "Invalid args in command; see 'Help'" );
401
402     return TV_SUCCESS;
403 }
404
405 int
406 device_main( int argc, char **argv )
407 {
408
409     unsigned int portTemp = 0;
410     char *ip_address = NULL,
411      *desc_doc_name = NULL,
412      *web_dir_path = NULL;
413     unsigned int port = 0;
414
415     int i = 0;
416
417     SampleUtil_Initialize( linux_print );
418
419     // Parse options
420     for( i = 1; i < argc; i++ ) {
421         if( strcmp( argv[i], "-ip" ) == 0 ) {
422             ip_address = argv[++i];
423         } else if( strcmp( argv[i], "-port" ) == 0 ) {
424             sscanf( argv[++i], "%u", &portTemp );
425         } else if( strcmp( argv[i], "-desc" ) == 0 ) {
426             desc_doc_name = argv[++i];
427         } else if( strcmp( argv[i], "-webdir" ) == 0 ) {
428             web_dir_path = argv[++i];
429         } else if( strcmp( argv[i], "-help" ) == 0 ) {
430             SampleUtil_Print( "Usage: %s -ip ipaddress -port port"
431                               " -desc desc_doc_name -webdir web_dir_path"
432                               " -help (this message)\n", argv[0] );
433             SampleUtil_Print( "\tipaddress:     IP address of the device"
434                               " (must match desc. doc)\n" );
435             SampleUtil_Print( "\t\te.g.: 192.168.0.4\n" );
436             SampleUtil_Print( "\tport:          Port number to use for "
437                               "receiving UPnP messages (must match desc. doc)\n" );
438             SampleUtil_Print( "\t\te.g.: 5431\n" );
439             SampleUtil_Print
440                 ( "\tdesc_doc_name: name of device description document\n" );
441             SampleUtil_Print( "\t\te.g.: tvcombodesc.xml\n" );
442             SampleUtil_Print
443                 ( "\tweb_dir_path: Filesystem path where web files "
444                   "related to the device are stored\n" );
445             SampleUtil_Print( "\t\te.g.: /upnp/sample/web\n" );
446             return 1;
447         }
448     }
449
450     port = ( unsigned short )portTemp;
451
452     return TvDeviceStart( ip_address, port, desc_doc_name, web_dir_path, linux_print );
453 }
454
455 int main( int argc, char **argv )
456 {
457     int rc;
458     ithread_t cmdloop_thread;
459 #ifndef WIN32
460     int sig;
461     sigset_t sigs_to_catch;
462 #endif
463     int code;
464
465     device_main(argc, argv);
466     rc = TvCtrlPointStart( linux_print, NULL );
467     if( rc != TV_SUCCESS ) {
468         SampleUtil_Print( "Error starting UPnP TV Control Point" );
469         return rc;
470     }
471     /* start a command loop thread */
472     code = ithread_create( &cmdloop_thread, NULL, TvCtrlPointCommandLoop, NULL );
473
474 #ifndef WIN32
475     /*
476        Catch Ctrl-C and properly shutdown 
477      */
478     sigemptyset( &sigs_to_catch );
479     sigaddset( &sigs_to_catch, SIGINT );
480     sigwait( &sigs_to_catch, &sig );
481
482     SampleUtil_Print( "Shutting down on signal %d...\n", sig );
483 #else
484         ithread_join(cmdloop_thread, NULL);
485 #endif
486     TvDeviceStop();
487     rc = TvCtrlPointStop();
488     
489     return rc;
490 }
491