forked from per-gron/reacdriver
/
REACConnection.cpp
681 lines (582 loc) · 24.7 KB
/
REACConnection.cpp
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
* REACConnection.cpp
* REAC
*
* Created by Per Eckerdal on 02/01/2011.
* Copyright 2011 Per Eckerdal. All rights reserved.
*
*
* This file is part of the OS X REAC driver.
*
* The OS X REAC driver 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.
*
* The OS X REAC driver 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 OS X REAC driver. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "REACConnection.h"
#include <IOKit/IOLib.h>
#include <IOKit/IOTimerEventSource.h>
#include <sys/errno.h>
#include <sys/socket.h>
#include "MbufUtils.h"
#include "REACSplitDataStream.h"
#include "REACMasterDataStream.h"
#define REAC_CONNECTION_CHECK_TIMEOUT_MS 500
#define REAC_TIMEOUT_UNTIL_DISCONNECT 1000
#define super OSObject
OSDefineMetaClassAndStructors(REACConnection, super)
bool REACConnection::initWithInterface(IOWorkLoop *workLoop_, ifnet_t interface_, REACMode mode_,
reac_connection_callback_t connectionCallback_,
reac_samples_callback_t samplesCallback_,
reac_get_samples_callback_t getSamplesCallback_,
void *cookieA_,
void *cookieB_,
UInt8 inChannels_,
UInt8 outChannels_) {
dataStream = NULL;
deviceInfo = NULL;
filterCommandGate = NULL;
workLoop = NULL;
timerEventSource = NULL;
interface = NULL;
if (NULL == workLoop_) {
goto Fail;
}
workLoop = workLoop_;
workLoop->retain();
// Add the command gate to the workloop
filterCommandGate = IOCommandGate::commandGate(this, (IOCommandGate::Action)filterCommandGateMsg);
if (NULL == filterCommandGate ||
(workLoop->addEventSource(filterCommandGate) != kIOReturnSuccess) ) {
IOLog("REACConnection::initWithInterface() - Error: Can't create or add commandGate\n");
goto Fail;
}
// Add the timer event source to the workloop
connectionCounter = 0;
lastSeenConnectionCounter = 0;
timerEventSource = IOTimerEventSource::timerEventSource(this, (IOTimerEventSource::Action)&REACConnection::timerFired);
if (NULL == timerEventSource) {
IOLog("REACConnection::initWithInterface() - Error: Failed to create timer event source.\n");
goto Fail;
}
// Hack: Pretend to be connected immediately
deviceInfo = (REACDeviceInfo*) IOMalloc(sizeof(REACDeviceInfo));
if (NULL == deviceInfo) {
IOLog("REACConnection::initWithInterface() - Error: Failed to allocate device info object.\n");
goto Fail;
}
deviceInfo->addr[0] = 0x00;
deviceInfo->addr[1] = 0x40;
deviceInfo->addr[2] = 0xab;
deviceInfo->addr[3] = 0xc4;
deviceInfo->addr[4] = 0x80;
deviceInfo->addr[5] = 0xf6;
deviceInfo->in_channels = 16;
deviceInfo->out_channels = 8;
started = false;
connected = false;
lastCounter = 0;
lastSeenConnectionCounter = 0;
lastSentAnnouncementCounter = 0;
splitAnnouncementCounter = 0;
connectionCallback = connectionCallback_;
samplesCallback = samplesCallback_;
getSamplesCallback = getSamplesCallback_;
cookieA = cookieA_;
cookieB = cookieB_;
mode = mode_;
inChannels = inChannels_;
outChannels = outChannels_;
dataStream = REACDataStream::withConnection(this); // mode has to be set before this is called.
if (NULL == dataStream) {
goto Fail;
}
ifnet_reference(interface_);
interface = interface_;
if (kIOReturnSuccess != REACConnection::getInterfaceMacAddress(interface, interfaceAddr, sizeof(interfaceAddr))) {
IOLog("REACConnection::initWithInterface() - Error: Failed to get interface address.\n");
goto Fail;
}
// TODO This is a hack. It seems to be needless though.
//static const UInt8 counterfeitMac[] = {
// 0x00, 0x40, 0xab, 0xc4, 0xb7, 0x58
//};
//memcpy(interfaceAddr, counterfeitMac, sizeof(interfaceAddr));
// Calculate our timeout in nanosecs, taking care to keep 64bits
if (REAC_MASTER == mode_) {
timeoutNS = 1000000000;
timeoutNS /= REAC_PACKETS_PER_SECOND;
}
else {
timeoutNS = REAC_CONNECTION_CHECK_TIMEOUT_MS;
timeoutNS *= 1000000;
}
// Hack: Announce connect
if (REAC_MASTER == mode) {
if (NULL != connectionCallback) {
connectionCallback(this, &cookieA, &cookieB, deviceInfo);
}
}
return true;
Fail:
deinit();
return false;
}
REACConnection *REACConnection::withInterface(IOWorkLoop *workLoop, ifnet_t interface, REACMode mode,
reac_connection_callback_t connectionCallback,
reac_samples_callback_t samplesCallback,
reac_get_samples_callback_t getSamplesCallback,
void *cookieA,
void *cookieB,
UInt8 inChannels,
UInt8 outChannels) {
REACConnection *p = new REACConnection;
if (NULL == p) return NULL;
bool result = p->initWithInterface(workLoop, interface, mode, connectionCallback, samplesCallback,
getSamplesCallback, cookieA, cookieB, inChannels, outChannels);
if (!result) {
p->release();
return NULL;
}
return p;
}
void REACConnection::deinit() {
stop();
if (NULL != dataStream) {
dataStream->release();
dataStream = NULL;
}
if (NULL != deviceInfo) {
IOFree(deviceInfo, sizeof(REACDeviceInfo));
}
if (NULL != filterCommandGate) {
workLoop->removeEventSource(filterCommandGate);
filterCommandGate->release();
filterCommandGate = NULL;
}
if (NULL != workLoop) {
workLoop->release();
}
if (NULL != timerEventSource) {
timerEventSource->cancelTimeout();
workLoop->removeEventSource(timerEventSource);
timerEventSource->release();
timerEventSource = NULL;
}
if (NULL != interface) {
ifnet_release(interface);
interface = NULL;
}
}
void REACConnection::free() {
deinit();
super::free();
}
bool REACConnection::start() {
if (NULL == timerEventSource || workLoop->addEventSource(timerEventSource) != kIOReturnSuccess) {
IOLog("REACConnection::start() - Error: Failed to add timer event source to work loop!\n");
return false;
}
timerEventSource->setTimeout(timeoutNS);
uint64_t time;
clock_get_uptime(&time);
absolutetime_to_nanoseconds(time, &nextTime);
nextTime += timeoutNS;
iff_filter filter;
filter.iff_cookie = this;
filter.iff_name = "REAC driver input filter";
filter.iff_protocol = 0;
filter.iff_input = &REACConnection::filterInputFunc;
filter.iff_output = NULL;
filter.iff_event = NULL;
filter.iff_ioctl = NULL;
filter.iff_detached = &REACConnection::filterDetachedFunc;
if (0 != iflt_attach(interface, &filter, &filterRef)) {
return false;
}
started = true;
return true;
}
void REACConnection::stop() {
if (started) {
if (NULL != timerEventSource) {
timerEventSource->cancelTimeout();
workLoop->removeEventSource(timerEventSource);
}
if (isConnected()) {
// Announce disconnect
if (NULL != connectionCallback) {
connectionCallback(this, &cookieA, &cookieB, NULL);
}
}
iflt_detach(filterRef);
started = false;
}
}
const REACDeviceInfo *REACConnection::getDeviceInfo() const {
return deviceInfo;
}
void REACConnection::timerFired(OSObject *target, IOTimerEventSource *sender) {
REACConnection *proto = OSDynamicCast(REACConnection, target);
if (NULL == proto) {
// This should never happen
IOLog("REACConnection::timerFired(): Internal error!\n");
return;
}
UInt64 thisTimeNS;
uint64_t time;
SInt64 diff;
do {
if (proto->isConnected()) {
if ((proto->connectionCounter - proto->lastSeenConnectionCounter)*proto->timeoutNS >
(UInt64)REAC_TIMEOUT_UNTIL_DISCONNECT*1000000) {
proto->connected = false;
if (NULL != proto->connectionCallback) {
proto->connectionCallback(proto, &proto->cookieA, &proto->cookieB, NULL);
}
}
proto->connectionCounter++;
}
if (REAC_MASTER == proto->mode) {
proto->getAndSendSamples();
}
else if (REAC_SPLIT == proto->mode) {
proto->lastSentAnnouncementCounter++;
if (proto->lastSentAnnouncementCounter*proto->timeoutNS >= 1000000000) {
proto->lastSentAnnouncementCounter = 0;
proto->sendSplitAnnouncementPacket();
}
}
// Calculate next time to fire, by taking the time and comparing it to the time we requested.
clock_get_uptime(&time);
absolutetime_to_nanoseconds(time, &thisTimeNS);
proto->nextTime += proto->timeoutNS;
// This next calculation must be signed
diff = ((SInt64)proto->nextTime - (SInt64)thisTimeNS);
if (diff < -((SInt64)proto->timeoutNS)*10) {
// TODO After a certain amount of lost packets we probably ought to skip output packets
IOLog("REACConnection::timerFired(): Lost the time by %lld us\n", diff/1000);
}
} while (diff < 0);
sender->setTimeout((UInt64)diff);
}
IOReturn REACConnection::getAndSendSamples() {
UInt8 *sampleBuffer = NULL;
UInt32 bufSize = 0;
if (getSamplesCallback) {
getSamplesCallback(this, &cookieA, &cookieB, &sampleBuffer, &bufSize);
}
return sendSamples(bufSize, sampleBuffer);
}
IOReturn REACConnection::sendSamples(UInt32 bufSize, UInt8 *sampleBuffer) {
REACMasterDataStream *masterDataStream = OSDynamicCast(REACMasterDataStream, dataStream);
const UInt32 ourSamplesSize = REAC_SAMPLES_PER_PACKET*REAC_RESOLUTION*
(NULL != masterDataStream ?
inChannels : deviceInfo->out_channels);
// TODO This is not complete
const UInt32 slaveSamplesSize = (NULL != masterDataStream && masterDataStream->isConnectedToSlave()) ? ourSamplesSize : 0;
const UInt32 sentSamplesSize = ourSamplesSize+slaveSamplesSize;
const UInt32 sampleOffset = sizeof(EthernetHeader)+sizeof(REACPacketHeader);
const UInt32 endingOffset = sampleOffset+sentSamplesSize;
const UInt32 packetLen = endingOffset+sizeof(REACConstants::ENDING);
REACPacketHeader rph;
mbuf_t mbuf = NULL;
IOReturn result = kIOReturnError;
IOReturn processPacketRet;
/// Do some argument checks
if (!(REAC_SLAVE == mode || REAC_MASTER == mode)) {
result = kIOReturnInvalid;
goto Done;
}
if (ourSamplesSize != bufSize && NULL != sampleBuffer) { // bufSize is ignored when sampleBuffer is NULL
result = kIOReturnBadArgument;
goto Done;
}
/// Prepare ethernet header
EthernetHeader header;
memcpy(header.shost, interfaceAddr, sizeof(header.shost));
// Don't initialize header.dhost; that's the responsibility of dataStream->processPacket
memcpy(&header.type, REACConstants::PROTOCOL, sizeof(REACConstants::PROTOCOL));
/// Do REAC data stream processing
processPacketRet = dataStream->processPacket(&rph, sizeof(header.dhost), header.dhost);
if (kIOReturnAborted == processPacketRet) {
// The REACDataStream indicates to us that it doesn't want us to send a packet.
goto Done;
}
else if (kIOReturnSuccess != processPacketRet) {
IOLog("REACConnection::sendSamples() - Error: Failed to process packet data stream.\n");
goto Done;
}
/// Allocate mbuf
if (0 != mbuf_allocpacket(MBUF_DONTWAIT, packetLen, NULL, &mbuf) ||
kIOReturnSuccess != MbufUtils::setChainLength(mbuf, packetLen)) {
IOLog("REACConnection::sendSamples() - Error: Failed to allocate packet mbuf.\n");
goto Done;
}
/// Copy ethernet header
if (kIOReturnSuccess != MbufUtils::copyFromBufferToMbuf(mbuf, 0, sizeof(EthernetHeader), &header)) {
IOLog("REACConnection::sendSamples() - Error: Failed to copy REAC header to packet mbuf.\n");
goto Done;
}
/// Copy REAC header
if (kIOReturnSuccess != MbufUtils::copyFromBufferToMbuf(mbuf, sizeof(EthernetHeader), sizeof(REACPacketHeader), &rph)) {
IOLog("REACConnection::sendSamples() - Error: Failed to copy REAC header to packet mbuf.\n");
goto Done;
}
/// Copy sample data
if (NULL != sampleBuffer) {
if (kIOReturnSuccess != MbufUtils::copyAudioFromBufferToMbuf(mbuf, sampleOffset, bufSize, sampleBuffer)) {
IOLog("REACConnection::sendSamples() - Error: Failed to copy sample data to packet mbuf.\n");
goto Done;
}
}
else {
if (kIOReturnSuccess != MbufUtils::zeroMbuf(mbuf, sampleOffset, ourSamplesSize)) {
IOLog("REACConnection::sendSamples() - Error: Failed to zero sample data in mbuf.\n");
goto Done;
}
}
if (NULL != masterDataStream && masterDataStream->isConnectedToSlave()) {
// TODO This is very incorrect: It doesn't send the slave data, and even if it would, the order of the
// data would be jumbled, because it has to send the whole first sample first and so on.
if (kIOReturnSuccess != MbufUtils::zeroMbuf(mbuf, sampleOffset+ourSamplesSize, slaveSamplesSize)) {
IOLog("REACConnection::sendSamples() - Error: Failed to zero slave sample data in mbuf.\n");
goto Done;
}
}
/// Copy packet ending
if (kIOReturnSuccess != MbufUtils::copyFromBufferToMbuf(mbuf, endingOffset,
sizeof(REACConstants::ENDING), (void *)REACConstants::ENDING)) {
IOLog("REACConnection::sendSamples() - Error: Failed to copy ending to packet mbuf.\n");
goto Done;
}
/// Send packet
if (0 != ifnet_output_raw(interface, 0, mbuf)) {
mbuf = NULL; // ifnet_output_raw always frees the mbuf
IOLog("REACConnection::sendSamples() - Error: Failed to send packet.\n");
goto Done;
}
mbuf = NULL; // ifnet_output_raw always frees the mbuf
result = kIOReturnSuccess;
Done:
if (NULL != mbuf) {
mbuf_freem(mbuf);
mbuf = NULL;
}
return result;
}
IOReturn REACConnection::sendSplitAnnouncementPacket() {
const UInt32 fillerSize = 288;
const UInt32 fillerOffset = sizeof(EthernetHeader)+sizeof(REACPacketHeader);
const UInt32 endingOffset = fillerOffset+fillerSize;
const UInt32 packetLen = endingOffset+sizeof(REACConstants::ENDING);
REACSplitDataStream *splitDataStream;
REACPacketHeader rph;
mbuf_t mbuf = NULL;
int result = kIOReturnError;
/// Do some argument checks
if (!REAC_SPLIT == mode) {
result = kIOReturnInvalid;
goto Done;
}
/// Prepare REAC packet header
splitDataStream = OSDynamicCast(REACSplitDataStream, dataStream);
if (NULL == splitDataStream) {
IOLog("REACConnection::sendSplitAnnouncementPacket(): Internal error!\n");
result = kIOReturnInternalError;
goto Done;
}
rph.setCounter(splitAnnouncementCounter++);
if (!splitDataStream->prepareSplitAnnounce(&rph)) {
goto Done;
}
/// Allocate mbuf
if (0 != mbuf_allocpacket(MBUF_DONTWAIT, packetLen, NULL, &mbuf) ||
kIOReturnSuccess != MbufUtils::setChainLength(mbuf, packetLen)) {
IOLog("REACConnection::sendSplitAnnouncementPacket() - Error: Failed to allocate packet mbuf.\n");
goto Done;
}
/// Copy ethernet header
EthernetHeader header;
memcpy(header.shost, interfaceAddr, sizeof(header.shost));
memcpy(header.dhost, deviceInfo->addr, sizeof(header.dhost));
memcpy(&header.type, REACConstants::PROTOCOL, sizeof(REACConstants::PROTOCOL));
if (kIOReturnSuccess != MbufUtils::copyFromBufferToMbuf(mbuf, 0, sizeof(EthernetHeader), &header)) {
IOLog("REACConnection::sendSplitAnnouncementPacket() - Error: Failed to copy REAC header to packet mbuf.\n");
goto Done;
}
/// Copy REAC header
if (kIOReturnSuccess != MbufUtils::copyFromBufferToMbuf(mbuf, sizeof(EthernetHeader), sizeof(REACPacketHeader), &rph)) {
IOLog("REACConnection::sendSplitAnnouncementPacket() - Error: Failed to copy REAC header to packet mbuf.\n");
goto Done;
}
/// Copy filler
if (kIOReturnSuccess != MbufUtils::zeroMbuf(mbuf, fillerOffset, fillerSize)) {
IOLog("REACConnection::sendSplitAnnouncementPacket() - Error: Failed to zero filler data in mbuf.\n");
goto Done;
}
/// Copy packet ending
if (kIOReturnSuccess != MbufUtils::copyFromBufferToMbuf(mbuf, endingOffset,
sizeof(REACConstants::ENDING), (void *)REACConstants::ENDING)) {
IOLog("REACConnection::sendSplitAnnouncementPacket() - Error: Failed to copy ending to packet mbuf.\n");
goto Done;
}
/// Send packet
if (0 != ifnet_output_raw(interface, 0, mbuf)) {
mbuf = NULL; // ifnet_output_raw always frees the mbuf
IOLog("REACConnection::sendSplitAnnouncementPacket() - Error: Failed to send packet.\n");
goto Done;
}
mbuf = NULL; // ifnet_output_raw always frees the mbuf
result = kIOReturnSuccess;
Done:
if (NULL != mbuf) {
mbuf_freem(mbuf);
mbuf = NULL;
}
return result;
}
void REACConnection::filterCommandGateMsg(OSObject *target, void *data_mbuf, void *eth_header_ptr, void*, void*) {
REACConnection *proto = OSDynamicCast(REACConnection, target);
if (NULL == proto) {
// This should never happen
IOLog("REACConnection::filterCommandGateMsg(): Internal error.\n");
return;
}
const EthernetHeader *ethernetHeader = (const EthernetHeader *)eth_header_ptr;
const int samplesSize = REAC_SAMPLES_PER_PACKET*REAC_RESOLUTION*proto->deviceInfo->in_channels;
mbuf_t *data = (mbuf_t *)data_mbuf;
UInt32 len = MbufUtils::mbufTotalLength(*data);
REACPacketHeader packetHeader;
// Check that the packet length is long enough
if (len < sizeof(REACPacketHeader)+sizeof(REACConstants::ENDING)) {
IOLog("REACConnection[%p]::filterCommandGateMsg(): Got packet of too short length\n", proto);
return;
}
// Check packet ending
UInt8 packetEnding[sizeof(REACConstants::ENDING)];
if (0 != mbuf_copydata(*data, len-sizeof(REACConstants::ENDING), sizeof(REACConstants::ENDING), &packetEnding)) {
IOLog("REACConnection[%p]::filterCommandGateMsg(): Failed to fetch REAC packet ending\n", proto);
return;
}
if (0 != memcmp(packetEnding, REACConstants::ENDING, sizeof(packetEnding))) {
// Incorrect ending. Not a REAC packet?
IOLog("REACConnection[%p]::filterCommandGateMsg(): Incorrect packet ending.\n", proto);
return;
}
// Fetch packet header
if (0 != mbuf_copydata(*data, 0, sizeof(REACPacketHeader), &packetHeader)) {
IOLog("REACConnection[%p]::filterCommandGateMsg(): Failed to fetch REAC packet header\n", proto);
return;
}
// Check packet counter
// TODO This doesn't work when more than one unit (for instance two splits) is connected
if (proto->isConnected() && /* This prunes a lost packet message when connecting */
proto->lastCounter+1 != packetHeader.getCounter()) {
if (!(65535 == proto->lastCounter && 0 == packetHeader.getCounter())) {
IOLog("REACConnection[%p]::filterCommandGateMsg(): Lost packet [%d %d]\n",
proto, proto->lastCounter, packetHeader.getCounter());
}
}
// Process packet header
proto->dataStream->gotPacket(&packetHeader, ethernetHeader);
// Check packet length
if (sizeof(REACPacketHeader)+samplesSize+sizeof(UInt16) == len) {
// Hack: Announce connect
if (!proto->isConnected()) {
proto->connected = true;
if (NULL != proto->connectionCallback) {
proto->connectionCallback(proto, &proto->cookieA, &proto->cookieB, proto->deviceInfo);
}
}
// Save the time we got the packet, for use by REACConnection::timerFired
proto->lastSeenConnectionCounter = proto->connectionCounter;
if (proto->isConnected()) {
if (NULL != proto->samplesCallback) {
UInt8* inBuffer = NULL;
UInt32 inBufferSize = 0;
proto->samplesCallback(proto, &proto->cookieA, &proto->cookieB, &inBuffer, &inBufferSize);
if (NULL != inBuffer) {
const UInt32 bytesPerSample = REAC_RESOLUTION * proto->deviceInfo->in_channels;
const UInt32 bytesPerPacket = bytesPerSample * REAC_SAMPLES_PER_PACKET;
if (inBufferSize != bytesPerPacket) {
IOLog("REACConnection::filterCommandGateMsg(): Got incorrectly sized buffer (not the same as a packet).\n");
}
else {
MbufUtils::copyAudioFromMbufToBuffer(*data, sizeof(REACPacketHeader), inBufferSize, inBuffer);
}
}
}
}
}
if (REAC_SLAVE == proto->mode) {
proto->getAndSendSamples();
}
proto->lastCounter = packetHeader.getCounter();
}
errno_t REACConnection::filterInputFunc(void *cookie,
ifnet_t interface,
protocol_family_t protocol,
mbuf_t *data,
char **frame_ptr) {
REACConnection *proto = (REACConnection *)cookie;
EthernetHeader *header = (EthernetHeader *)*frame_ptr;
if (0 != memcmp(header->type, REACConstants::PROTOCOL, sizeof(header->type))) {
// This is not a REAC packet. Ignore.
return 0; // Continue normal processing of the package.
}
proto->filterCommandGate->runCommand(data, header);
return EINPROGRESS; // Skip the processing of the package.
}
void REACConnection::filterDetachedFunc(void *cookie,
ifnet_t interface) {
// IOLog("REACConnection[%p]::filterDetachedFunc()\n", cookie);
}
// I can't find this class in any header (??)
#define REACSockaddr com_pereckerdal_driver_REACSockaddr
struct REACSockaddr {
u_char sdl_len; /* Total length of sockaddr */
u_char sdl_family; /* AF_LINK */
u_short sdl_index; /* if != 0, system given index for interface */
u_char sdl_type; /* interface type */
u_char sdl_nlen; /* interface name length, no trailing 0 reqd. */
u_char sdl_alen; /* link level address length */
u_char sdl_slen; /* link layer selector length */
char sdl_data[16]; /* minimum work area, can be larger;
contains both if name and ll address */
};
IOReturn REACConnection::getInterfaceMacAddress(ifnet_t interface, UInt8* resultAddr, UInt32 addrLen) {
ifaddr_t *addresses;
ifaddr_t *address;
REACSockaddr addr;
IOReturn ret = kIOReturnError;
if (ETHER_ADDR_LEN != addrLen) {
return kIOReturnBadArgument;
}
if (ifnet_get_address_list_family(interface, &addresses, AF_LINK)) {
return kIOReturnError;
}
address = addresses;
while (*address) {
if (0 == ifaddr_address(*address, (sockaddr*) &addr, sizeof(addr))) {
if (addr.sdl_alen == ETHER_ADDR_LEN) {
memcpy(resultAddr, addr.sdl_data+addr.sdl_nlen, addrLen);
ret = kIOReturnSuccess;
break;
}
}
++address;
}
ifnet_free_address_list(addresses);
return ret;
}