-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.cc
447 lines (334 loc) · 12.3 KB
/
api.cc
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
/*
* A library for working with DVSI's AMBE vocoder chips
*
* Copyright (C) 2019-2020 Internet Real-Time Lab, Columbia University
*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "api.h"
#include <termios.h>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/uio.h>
#include <assert.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stddef.h>
#include <sys/types.h>
#include <stdint.h>
#include <iomanip>
using namespace std;
using namespace std::placeholders;
using namespace ambe;
namespace ambe {
void swap(int16_t* dst, const int16_t* src, size_t count) {
for(uint i = 0; i < count; i++) dst[i] = bswap_16(src[i]);
}
template<typename T, typename... Args>
static T* parse(const Packet& packet, PacketType type, Args ...args) {
if (packet.type() != type)
throw runtime_error("Invalid packet type");
auto payload = packet.payload<T>();
if (!payload->valid(forward<Args>(args)...))
throw runtime_error("Invalid response received");
return payload;
}
template<typename T, typename... Args>
static T* parse(const Packet& packet, PacketType type, uint8_t channel, Args ...args) {
if (packet.type() != type)
throw runtime_error("Invalid packet type");
// The parse method variant with a channel argument is intended for
// multi-channel devices such as USB-3003. Those devices respond with a
// status field even for requests to change channel, e.g., PKT_CHANNEL0. The
// status field to the actual command then follows.
auto ch = packet.payload<StatusField>();
if (!ch->valid(ChannelField::type(channel)))
throw runtime_error("Received response for the wrong channel");
if (ch->status != 0)
throw runtime_error("Request to change channel failed");
auto payload = packet.payload<T>(sizeof(*ch));
if (!payload->valid(forward<Args>(args)...))
throw runtime_error("Invalid response received");
return payload;
}
template<typename T, typename... Args>
static T* parse(const Packet& packet, Args ...args) {
return parse<T>(packet, CONTROL, forward<Args>(args)...);
}
template<typename T, typename... Args>
static T* parse(const Packet& packet, uint8_t channel, Args ...args) {
return parse<T>(packet, CONTROL, channel, forward<Args>(args)...);
}
template<typename... Args>
static bool parseStatus(const Packet& packet, Args ...args) {
return parse<StatusField>(packet, forward<Args>(args)...)->status == 0;
}
template<typename... Args>
static bool parseStatus(const Packet& packet, uint8_t channel, Args ...args) {
return parse<StatusField>(packet, channel, forward<Args>(args)...)->status == 0;
}
}
int Rate::parseNumber(const char* rate, int max) {
char* end;
errno = 0;
auto val = strtol(rate, &end, 0);
if (errno != 0) return -1;
if (rate == end) return -1;
if (val < 0 || val > max) return -1;
return val;
}
int Rate::parseRatet(const char* rate) {
return parseNumber(rate, 255);
}
uint16_t* Rate::parseRatep(const char* rate) {
static uint16_t rcw[6];
char* tmp = strdup(rate);
int i = 0;
char* c = strtok(tmp, ",");
for(i = 0; i < 6; i++) {
if (c == nullptr) goto error;
auto v = parseNumber(c, 65535);
if (v < 0) goto error;
rcw[i] = v;
c = strtok(nullptr, ",");
}
if (c != nullptr) goto error;
return rcw;
error:
free(tmp);
return nullptr;
}
Rate::Rate(uint8_t index) : type(RATET), index(index) {
}
Rate::Rate(uint16_t* rcw) : type(RATEP) {
memcpy(this->rcw, rcw, 6 * sizeof(this->rcw[0]));
}
Rate::Rate(const char* rate) {
int index = parseRatet(rate);
if (index >= 0) {
type = RATET;
this->index = index;
} else {
uint16_t* rcw = parseRatep(rate);
if (rcw == nullptr) throw runtime_error("Invalid AMBE rate value");
type = RATEP;
memcpy(this->rcw, rcw, 6 * sizeof(this->rcw[0]));
}
}
ostream& ambe::operator<<(ostream& o, const Rate& rate) {
switch(rate.type) {
case Rate::RATET:
o << to_string(rate.index);
break;
case Rate::RATEP:
for(int i = 0; i < 6; i++) {
o << "0x" << setfill('0') << setw(4) << hex << rate.rcw[i];
if (i < 5) o << ",";
}
break;
default:
throw logic_error("Bug: Invalid AMBE rate type");
}
return o;
}
API::API(Device& device, Scheduler& scheduler, bool check_parity) :
device(device), scheduler(scheduler), check_parity(check_parity) {
}
void API::hardReset() {
// Perform hardware reset of USB-3003 or USB-3012 by sending a UART break
// signal to it.
HardResetInterface* resettable = dynamic_cast<HardResetInterface*>(&device);
FifoDevice* dev = dynamic_cast<FifoDevice*>(&device);
promise<void> retval;
auto future = retval.get_future();
// First, we install our own callback to receive any packets from the
// dongle. The callback ignores packets other than AMBE_READY. Once
// AMBE_READY has been received, the callback fullfills the above promise.
auto prev = dev->setCallback([this, &retval](const string& packet) {
try {
// Do not check parity when waiting for PKT_READY after a reset
parse<Field>(Packet(move(packet), true, false), READY);
} catch(const runtime_error& e) {
return;
}
retval.set_value();
});
try {
// Perform a hard reset of the dongle. Only USB-3003 dongles support
// hard reset. To perform a hard reset, we send an UART break signal to
// the device and then receive the AMBE_READY packet from the dongle.
resettable->reset();
// Wait for the promise to get resolved. That indicates that an
// AMBE_READY packet was received.
future.get();
} catch(...) {
dev->setCallback(prev);
throw;
}
// Restore the original packet callback
dev->setCallback(prev);
}
void API::softReset() {
FifoDevice* dev = dynamic_cast<FifoDevice*>(&device);
// First, send 350 zero characters to the AMBE chip. This will terminate any
// previously sent unfinished packet. Inspired by DVSI's official Linux
// client software.
string zero(10, '\0');
for(int i = 0; i < 3500; i++) dev->send(zero);
// Perform a soft reset by sending the AMBE_RESET packet to the dongle.
// Always send a RESET packet with parity on so that we can reset the
// device no matter what state the dongle is in.
Packet request;
request.append<Field>(RESET);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
// Do not check parity when waiting for PKT_READY after a reset
parse<Field>(response, READY);
}
void API::reset(bool hard) {
if (hard) hardReset();
else softReset();
device.uses_parity = true;
}
void API::compand(bool enabled, bool alaw) {
Packet request;
request.append<CompandField>(enabled, alaw);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
if (!parseStatus(response, COMPAND))
throw runtime_error("PKT_COMPAND request failed");
}
void API::paritymode(unsigned char mode) {
const bool parity = mode > 0;
Packet request;
request.append<ParityModeField>(parity);
request.finalize(device.uses_parity);
// Reconfigure the device with the new parity setting before sending the
// request so that when the response comes, the device will have the correct
// setting. This request must not be used concurrently with other requests.
device.uses_parity = parity;
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
if (!parseStatus(response, PARITYMODE))
throw runtime_error("PKT_PARITYMODE request failed");
}
// FIXME: We should check that the string is zero-terminated
string API::prodid() {
Packet request;
request.append<Field>(PRODID);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
auto fld = parse<StringField>(response, PRODID);
return &fld->value[0];
}
// FIXME: We should check that the string is zero-terminated
string API::verstring() {
Packet request;
request.append<Field>(VERSTRING);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
auto fld = parse<StringField>(response, VERSTRING);
return &fld->value[0];
}
void API::setMode(uint8_t channel, FieldType type, bool ns_e, bool cp_s, bool cp_e, bool dtx_e, bool td_e, bool ts_e) {
Packet request;
request.append<ChannelField>(channel);
request.append<ModeField>(type, ns_e, cp_s, cp_e, dtx_e, td_e, ts_e);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
if (!parseStatus(response, type))
throw runtime_error("PKT_{E,D}CMODE request on channel " + to_string(channel) + " failed");
}
void API::ecmode(uint8_t channel, bool ns_e, bool cp_s, bool cp_e, bool dtx_e, bool td_e, bool ts_e) {
setMode(channel, ECMODE, ns_e, cp_s, cp_e, dtx_e, td_e, ts_e);
}
void API::dcmode(uint8_t channel, bool ns_e, bool cp_s, bool cp_e, bool dtx_e, bool td_e, bool ts_e) {
setMode(channel, DCMODE, ns_e, cp_s, cp_e, dtx_e, td_e, ts_e);
}
void API::ratet(uint8_t channel, uint8_t index) {
Packet request;
request.append<ChannelField>(channel);
request.append<RatetField>(index);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
if (!parseStatus(response, channel, RATET))
throw runtime_error("PKT_RATET request on channel " + to_string(channel) + " failed");
}
void API::ratep(uint8_t channel, const uint16_t* rcw) {
Packet request;
request.append<ChannelField>(channel);
request.append<RatepField>(rcw);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
if (!parseStatus(response, channel, RATEP))
throw runtime_error("PKT_RATEP request on channel " + to_string(channel) + " failed");
}
void API::rate(uint8_t channel, const Rate& rate) {
switch(rate.type) {
case Rate::RATET: ratet(channel, rate.index); break;
case Rate::RATEP: ratep(channel, rate.rcw); break;
default: throw logic_error("Bug: Unsupported rate type");
}
}
void API::init(uint8_t channel, bool encoder, bool decoder) {
Packet request;
request.append<ChannelField>(channel);
request.append<InitField>(encoder, decoder);
request.finalize(device.uses_parity);
auto response = scheduler.submit(request).get();
if (check_parity && device.uses_parity && !response.checkParity())
throw runtime_error("Invalid packet parity");
if (!parseStatus(response, channel, INIT))
throw runtime_error("PKT_INIT request on channel " + to_string(channel) + " failed");
}
future<Packet> API::compress(uint8_t channel, const int16_t* samples, size_t count) {
Packet request(SPEECH);
request.append<ChannelField>(channel);
request.append<SpchdField>(count);
auto data = request.appendArray<int16_t>(count);
memcpy(data, samples, count * sizeof(samples[0]));
request.finalize(device.uses_parity);
return scheduler.submit(request);
}
future<Packet> API::decompress(uint8_t channel, const char* bits, size_t count) {
Packet request(CHANNEL);
request.append<ChannelField>(channel);
request.append<ChandField>(count);
size_t bytes = AmbeFrame::byteLength(count);
auto data = request.appendArray<char>(bytes);
memcpy(data, bits, bytes);
request.finalize(device.uses_parity);
return scheduler.submit(request);
}