summaryrefslogtreecommitdiffstats
path: root/src/cmdline.c
blob: dfeeddc516eae719857723cb2edfc46c2cb24ffe (plain)
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
/* AVRemote
   
 (c) 2011 Nederlands Instituut voor Mediakunst (NIMk)
     2011 Denis Roio <jaromil@nimk.nl>

     This program is free software: you can redistribute it and/or modify
     it under the terms of the GNU Affero General Public License as
     published by the Free Software Foundation, either version 3 of the
     License, or (at your option) any later version.
     
     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU Affero General Public License for more details.
     
     You should have received a copy of the GNU Affero General Public License
     along with this program.  If not, see <http://www.gnu.org/licenses/>

*/

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

#include <errno.h>

#include <avremote.h>
#include <parsers.h>

// our exit codes are shell style: 1 is error, 0 is success
#define ERR 1

// uncomment to debug
#define DEBUG 1

char filename[512];
char command[64];
char server[512];
int port = 0;
int dry_run = 0;
int discover = 0;
int pipe_stdin = 0;

parser_f *parser = NULL;

// we use only getopt, no _long
static const char *short_options = "-hvs:p:t";

void cmdline(int argc, char **argv) {
  command[0] = 0;
  filename[0] = 0;
  server[0] = 0;

  int res, optlen;
  do {
    res = getopt(argc, argv, short_options);
    switch(res) {
    case 'h':
      fprintf(stderr,
	      "%s %s - send AVTransport commands to UPNP media services\n"
	      "\n"
	      " Copyright (C) 2011 Jaromil @ NIMk.nl Artlab , License GNU AGPL v3+\n"
	      " This is free software: you are free to change and redistribute it.\n"
	      " The latest AVTransport sourcecode is published on <%s>\n"
	      "\n"
	      "Syntax: avremote [options] [command] [args...]\n"
	      "\n"
	      "Commands:\n"
	      "\n"
	      " discover    scan for upnp devices on the network\n"
	      " load        load a file and prepare it for playback\n"
	      " mode        set playback mode (NORMAL or REPEAT_ONE)\n"
	      " play        start playing the selected file\n"
	      " pause       pause currently running playback\n"
	      " stop        stop playback and return to menu\n"
	      " get         get the current status of the device\n"
	      " jump        seek to a position in time (00:00:00)\n"
	      "\n"
	      " none means load and play URL, or use - to read xml from stdin\n"
	      "\n"
	      "Options:\n"
	      "\n"
	      " -s          network address or hostname of the media server\n"
	      " -p          port on which the UPNP AVTransport daemon is listening\n"
	      " -t          dry run to test without a server (print out rendered xml)\n"
	      "\n"
	      " -h          print this help\n"
	      " -v          version information for this tool\n"
	      "\n"
	      "For more informations on AVRemote read the manual: man avremote\n"
	      "Please report bugs on <http://bugs.dyne.org>.\n",
	      PACKAGE, VERSION, PACKAGE_URL);
      exit(0);

    case 'v':
      fprintf(stderr,"%s - simple commandline tool to send AVTransport commands over UPNP\n"
	      "version %s by Jaromil - Netherlands Media Art Institute\n"
	      "Copyright (C) 2011 NIMk Artlab, License GNU AGPL v3+\n"
	      "This is free software: you are free to change and redistribute it\n",
	      PACKAGE, VERSION);
      exit(0);
      
    case 's':
      snprintf(server,511,"%s",optarg);
      break;

    case 'p':
      sscanf (optarg, "%u", &port);
      break;

    case 't':
      // test dry run
      dry_run = 1;
      fprintf(stderr,"dry run: printint out rendered upnp message without establishing connection\n");
      break;
      
    case '?':
      fprintf(stderr,"unrecognized option: %s\n",optarg);
      break;
      
    case 1:
      if(!command[0]) {
	snprintf(command,63,"%s",optarg);
      } else
	snprintf(filename,511,"%s",optarg);
      break;
    default:
      break;
    }

  } while(res != -1);

  if(command[0] == 'd') { // discover
    discover = 1;
  } else if(!dry_run) {
    // check requires args
    if( command[0]=='-' && !command[1]) pipe_stdin++;

        
  }
}


int main(int argc, char **argv) {
  upnp_t *upnp;
  int found;

  cmdline(argc, argv);

  upnp = create_upnp();
  
  // no server specified, force discovery
  if(!server[0] || !port) discover = 1;

  if (discover && !dry_run)
    {
      fprintf(stderr,"Performing upnp discovery...\n");
      found = upnp_discover(upnp);

      // we exit in case none or more than one found and no manual
      // server:port was specified
	if(!server[0] || !port)
	  {
	    if(found != 1)
	      exit(0);
	  }
	else
	  { // commandline specified explicit addresses
	    snprintf(upnp->hostname, MAX_HOSTNAME_SIZE-1,"%s",server);
	    upnp->port = port;
	  }

    }
  

  if(!dry_run)
    {
      if ( connect_upnp (upnp) < 0 )
	{
	  fprintf(stderr,"can't connect to %s:%u: operation aborted.\n", server, port);
	  exit(ERR);
	}  
      
    } 
  else
    {
      
      sprintf(upnp->hostname,"%s","dry run");
      upnp->port = 0;
      
    }

  // pipe raw xml commands from stdin
  if(pipe_stdin) {
    int res;
    int in = 0;
    char raw[8192];

    while( !feof(stdin) )
      {
	in = fread(raw,1,8191,stdin);
	res = write(upnp->sockfd,raw,in);
	if(res != in)
	  fprintf(stderr,"upnp pipe wrote only %u of %u bytes",res, in);
	recv_upnp(upnp, 1000);
	fprintf(stderr,"%s\n",upnp->res);
      }
    free_upnp(upnp);
    exit(0);
  }

  /* command parsing is a cascade switch on single letters
     this is supposedly faster than strcmp. mapping:

     D iscovery
     L oad
     P lay
     PA use
     S top
     G et
     M ode
     J ump

  */
  switch(command[0]) {

  case 'd': // discovery
    // was processed earlier
    break;

  case 'l': // load url
    render_uri_meta(upnp,filename);
    render_upnp(upnp,"SetAVTransportURI", upnp->meta);
    //    send_upnp(upnp);
    break;

  case 'p': 

    if(command[1]=='l')
      { // 'pl*' is play
	
	render_upnp(upnp,"Play","<Speed>1</Speed>");
	
      }
    else if (command[1]=='a')
      { // 'pa*' is pause
	
	render_upnp(upnp,"Pause","");
	
      }
    break;

  case 's': // stop
    render_upnp(upnp,"Stop","");
    break;

  case 'g': // dump a parsable full state of the device  
    render_upnp(upnp,"GetTransportInfo","");
    parser = GetTransportInfo;
    
    break;

  case 'm': // set the playmode:
    // "NORMAL", "REPEAT_ONE", "REPEAT_ALL", "RANDOM"
    {
      char tmp[256];
      snprintf(tmp,255,"<NewPlayMode>%s</NewPlayMode>",filename);
      render_upnp(upnp,"SetPlayMode",tmp);
    }
    break;

  case 'j': // jump aka seek
    // <SeekMode> and <SeekTarget>
    {
      char tmp[512];
      snprintf(tmp,511,"<Unit>REL_TIME</Unit><Target>%s</Target>",filename);
      render_upnp(upnp,"Seek",tmp);
    }
    break;

  default:
    if(!command[0]) break;
    fprintf(stderr,"warning: command not recognized, loading and playing as url\n");

    // load
    render_uri_meta(upnp,command);
    render_upnp(upnp,"SetAVTransportURI", upnp->meta);
    send_upnp(upnp);
    recv_upnp(upnp, 1000);

    // must re-connect socket between commands
    close(upnp->sockfd);
    upnp->sockfd = 0;
    connect_upnp(upnp);

    render_upnp(upnp,"Play","<Speed>1</Speed>");
  }

  if (dry_run)
    print_upnp(upnp);
  else
    {
      send_upnp(upnp);
      recv_upnp(upnp, 1000);
      if (parser)
	(*parser)(upnp->res);
      else
	fprintf(stderr,"%s\n",upnp->res);
    }
  
  free_upnp(upnp);

  exit(0);
}