Download this file

ohradio.cpp    348 lines (314 with data), 9.7 kB

  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
/* Copyright (C) 2013 J.F.Dockes
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/////////////// libupnpp OhRadio trial driver
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include "libupnpp/upnpplib.hxx"
#include "libupnpp/log.hxx"
#include "libupnpp/upnpputils.hxx"
#include "libupnpp/control/cdirectory.hxx"
#include "libupnpp/control/discovery.hxx"
#include "libupnpp/control/mediarenderer.hxx"
#include "libupnpp/control/ohradio.hxx"
#include "libupnpp/control/ohinfo.hxx"
using namespace std;
using namespace UPnPClient;
using namespace UPnPP;
UPnPDeviceDirectory *superdir;
int channelid;
class MReporter : public UPnPClient::VarEventReporter {
public:
void changed(const char *nm, int value) {
if (!strcmp(nm, "TransportState")) {
string tpstate;
switch(value) {
case OHPlaylist::TPS_Unknown: tpstate = "Unknown"; break;
case OHPlaylist::TPS_Buffering: tpstate = "Buffering"; break;
case OHPlaylist::TPS_Paused: tpstate = "Paused"; break;
case OHPlaylist::TPS_Playing: tpstate = "Playing"; break;
case OHPlaylist::TPS_Stopped: tpstate = "Stopped"; break;
}
cout << "Changed: " << nm << " : " << tpstate << endl;
} else if (!strcmp(nm, "Id")) {
cout << "Changed: " << nm << " : " << value << endl;
channelid = value;
} else {
cout << "Changed: " << nm << " : " << value << endl;
}
}
void changed(const char *nm, const char *value) {
cout << "Changed: " << nm << " : " << value << endl;
}
void changed(const char *nm, UPnPDirObject meta) {
cout << "Changed: " << nm << " : " << meta.dump() << endl;
}
void changed(const char * nm, std::vector<int> ids) {
cout << "Changed: " << nm << " : ";
for (unsigned int i = 0; i < ids.size(); i++) {
cout << SoapHelp::i2s(ids[i]) << " ";
}
cout << endl;
}
};
MRDH getRenderer(const string& friendlyName)
{
if (superdir == 0) {
superdir = UPnPDeviceDirectory::getTheDir();
}
UPnPDeviceDesc ddesc;
if (superdir->getDevByFName(friendlyName, ddesc)) {
return MRDH(new MediaRenderer(ddesc));
}
cerr << "getDevByFname failed" << endl;
return MRDH();
}
void rdMonitor(OHRDH hdl, OHIFH hdlif)
{
MReporter reporter;
hdl->installReporter(&reporter);
hdlif->installReporter(&reporter);
while (true) {
static int prevchan;
sleep(2);
string uri;
UPnPDirObject dirent;
if (0&& prevchan != channelid) {
cerr << "New ChannelId: " << channelid << endl;
prevchan = channelid;
#if 0
if (hdl->channel(&uri, &dirent) == 0) {
cout << "Channel: uri " << uri << "\nMetadata " <<
dirent.dump() << endl;
}
#endif
int ret;
if ((ret = hdlif->metatext(&dirent)) == 0) {
cout << "Metatext: " << dirent.dump() << endl;
} else {
cerr << "Metatext: failed: " << ret << endl;
}
}
}
}
void rdIdArray(OHRDH hdl)
{
vector<int> ids;
int token = 0;
int ret;
if ((ret = hdl->idArray(&ids, &token)) != 0) {
cerr << "idArray failed: " << ret << endl;
return;
}
cout << "token: " << token << ". " << ids.size() << " ids: ";
for (unsigned int i = 0; i < ids.size(); i++) {
cout << SoapHelp::i2s(ids[i]) << " ";
}
cout << endl;
}
string rdReadList(OHRDH hdl, int id = -1)
{
vector<int> ids;
int token = 0;
int ret;
if ((ret = hdl->idArray(&ids, &token)) != 0) {
cerr << "idArray failed: " << ret << endl;
return string();
}
vector<OHPlaylist::TrackListEntry> ents;
if ((ret = hdl->readList(ids, &ents)) != 0) {
cerr << "readList failed: " << ret << endl;
return string();
}
for (unsigned int i = 0; i < ents.size(); i++) {
if (id == -1) {
cout << "Id: " << SoapHelp::i2s(ents[i].id) <<
" url " << ents[i].url <<
"\nmetadata: " << ents[i].dirent.dump() << "\n";
} else {
if (ents[i].id == id) {
return ents[i].url;
}
}
}
cout << endl;
return string();
}
// Could not get this to work. Gets UPNP_E_BAD_RESPONSE on sendAction??
void rdRead(OHRDH hdl, int id)
{
UPnPDirObject dirent;
int ret;
if ((ret = hdl->read(id, &dirent)) != 0) {
cerr << "read failed: " << ret << endl;
return;
}
cout << "read: metadata: " << dirent.dump() << endl;
}
void rdSetId(OHRDH hdl, int id)
{
int ret;
string url = rdReadList(hdl, id);
if (url.empty()) {
cerr << "Id " << id << " not found\n";
return;
}
if ((ret = hdl->setId(id, url)) != 0) {
cerr << "setId failed: " << ret << endl;
return;
}
cout << "setId ok\n";
}
static char *thisprog;
static char usage [] =
" -a <renderer>: run idArray\n"
" -M <renderer>: monitor OHRadio\n"
" -p <renderer>: pause radio\n"
" -P <renderer>: play radio\n"
" -r <renderer> id: run read\n"
" -R <renderer>: run ReadList\n"
" -s <renderer> id: run setId\n"
" -S <renderer>: stop\n"
" \n"
;
static void
Usage(void)
{
fprintf(stderr, "%s: usage:\n%s", thisprog, usage);
exit(1);
}
static int op_flags;
#define OPT_M 0x1
#define OPT_a 0x2
#define OPT_r 0x4
#define OPT_R 0x8
#define OPT_s 0x10
#define OPT_P 0x20
#define OPT_p 0x40
#define OPT_S 0x80
static struct option long_options[] = {
{0, 0, 0, 0}
};
int main(int argc, char *argv[])
{
string fname;
string arg;
thisprog = argv[0];
int ret;
int option_index = 0;
while ((ret = getopt_long(argc, argv, "aMPpRrSs",
long_options, &option_index)) != -1) {
switch (ret) {
case 'a': if (op_flags) Usage(); op_flags |= OPT_a; break;
case 'M': if (op_flags) Usage(); op_flags |= OPT_M; break;
case 'P': if (op_flags) Usage(); op_flags |= OPT_P; break;
case 'p': if (op_flags) Usage(); op_flags |= OPT_p; break;
case 'R': if (op_flags) Usage(); op_flags |= OPT_R; break;
case 'r': if (op_flags) Usage(); op_flags |= OPT_r; break;
case 's': if (op_flags) Usage(); op_flags |= OPT_s; break;
case 'S': if (op_flags) Usage(); op_flags |= OPT_S; break;
default:
Usage();
}
}
if (op_flags & (OPT_M|OPT_a|OPT_R|OPT_p|OPT_P|OPT_S)) {
if (optind != argc - 1)
Usage();
fname = argv[optind++];
}
if (op_flags & (OPT_r|OPT_s)) {
if (optind != argc - 2)
Usage();
fname = argv[optind++];
arg = argv[optind++];
}
if (Logger::getTheLog("/tmp/ohradio.log") == 0) {
cerr << "Can't initialize log" << endl;
return 1;
}
Logger::getTheLog("")->setLogLevel(Logger::LLDEB1);
LibUPnP *mylib = LibUPnP::getLibUPnP();
if (!mylib) {
cerr << "Can't get LibUPnP" << endl;
return 1;
}
if (!mylib->ok()) {
cerr << "Lib init failed: " <<
mylib->errAsString("main", mylib->getInitError()) << endl;
return 1;
}
mylib->setLogFileName("/tmp/libupnp.log", LibUPnP::LogLevelDebug);
MRDH rdr = getRenderer(fname);
if (!rdr) {
cerr << "Can't connect torenderer\n";
return 1;
}
OHRDH hdl = rdr->ohrd();
if (!hdl) {
cerr << "Device has no OHRadio service" << endl;
return 1;
}
OHIFH hdlif = rdr->ohif();
if (!hdlif) {
cerr << "Device has no OHInfo service" << endl;
return 1;
}
if ((op_flags & OPT_M)) {
rdMonitor(hdl, hdlif);
} else if ((op_flags & OPT_a)) {
rdIdArray(hdl);
} else if ((op_flags & OPT_R)) {
rdReadList(hdl);
} else if ((op_flags & OPT_p)) {
int ret = hdl->pause();
if (ret) {
cerr << "Pause: " << SoapHelp::i2s(ret);
return 1;
} else {
cout << "Pause Ok\n";
}
} else if ((op_flags & OPT_P)) {
int ret = hdl->play();
if (ret) {
cerr << "Play: " << SoapHelp::i2s(ret);
return 1;
} else {
cout << "Play Ok\n";
}
} else if ((op_flags & OPT_S)) {
int ret = hdl->stop();
if (ret) {
cerr << "Stop: " << SoapHelp::i2s(ret);
return 1;
} else {
cout << "Stop Ok\n";
}
} else if ((op_flags & OPT_r)) {
rdRead(hdl, atoi(arg.c_str()));
} else if ((op_flags & OPT_s)) {
rdSetId(hdl, atoi(arg.c_str()));
} else {
Usage();
}
return 0;
}