summaryrefslogtreecommitdiffstats
path: root/src/cmdline.c
blob: d646815256b3fe2e0fe31a9d58e44f7a1c51a80d (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
/* 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 <netinet/in.h>
#include <netdb.h>

#include <errno.h>

#include <avremote.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;

// 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 [file]\n"
	      "\n"
	      "Commands:\n"
	      "\n"
	      " load        load a file and prepare it for playback\n"
	      " play        start playing the selected file\n"
	      " pause       pause currently running playback\n"
	      " stop        stop playback and return to menu\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,"AVRemote - simple commandline tool to send AVTransport commands over UPNP\n"
	      "version 0.1 (Apr/2011) 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");
      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(!dry_run) {
    // check requires args
    if(!command[0]) {
      fprintf(stderr,"command not specified, see %s -h for help\n",argv[0]);
      exit(1);
    }
    
    if(!port) {
      fprintf(stderr,"port not specified, use -p\n");
      exit(1);
    }
    
    if(!server[0]) {
      fprintf(stderr,"server not specified, using localhost\n");
      sprintf(server,"%s","localhost");
    }
  }
  
}

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

  cmdline(argc, argv);

  upnp = create_upnp();

  if(!dry_run) {

    if ( connect_upnp(upnp, server, port) < 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;

  }

  // command parsing is a cascade switch on single letters
  // this is supposedly faster than strcmp
  switch(command[0]) {

  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':
    render_upnp(upnp,"GetTransportInfo","");
    break;

  default:
    fprintf(stderr,"error: command not understood.\n");
    free_upnp(upnp);
    exit(1);
  }

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

  }
  // TODO recv when needed

  free_upnp(upnp);

  exit(0);
}