-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathREACSlaveDataStream.cpp
173 lines (149 loc) · 6.32 KB
/
REACSlaveDataStream.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
/*
* REACSlaveDataStream.cpp
* REAC
*
* Created by Per Eckerdal on 18/03/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 "REACSlaveDataStream.h"
#include "REACConnection.h"
#define super REACDataStream
OSDefineMetaClassAndStructors(REACSlaveDataStream, super)
bool REACSlaveDataStream::initConnection(REACConnection *conn) {
resetHandshakeState();
lastCdeaTwoBytes[0] = lastCdeaTwoBytes[1] = 0;
return super::initConnection(conn);
}
IOReturn REACSlaveDataStream::processPacket(REACPacketHeader *packet, UInt32 dhostLen, UInt8 *dhost) {
# define setPacketTypeMacro(packetType) \
memcpy(packet->type, STREAM_TYPE_IDENTIFIERS[packetType], sizeof(packet->type));
super::processPacket(packet, dhostLen, dhost);
packet->setCounter(counter);
if (sizeof(masterDevice.addr) != dhostLen) {
return kIOReturnError;
}
IOReturn ret = kIOReturnAborted;
if (HANDSHAKE_GOT_MAC_ADDRESS_INFO == handshakeState) {
memset(dhost, 0xff, dhostLen);
setPacketTypeMacro(REAC_STREAM_FILLER);
memset(packet->data, 0, sizeof(packet->data));
ret = kIOReturnSuccess;
}
else if (HANDSHAKE_SENDING_INITIAL_ANNOUNCE == handshakeState) {
const UInt8 handshakeData[][19] = {
{ 0x00, 0x00, 0x02, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00 },
{ 0x02, 0x00, 0xfe, 0x0f, 0xf0, 0x41, 0x0a, 0x00, 0x00, 0x12, 0x12, 0x01, 0x00, 0x06, 0x00, 0x01, 0x00, 0x78, 0xf7 },
{ 0x02, 0x00, 0xfe, 0x0f, 0xf0, 0x41, 0x0a, 0x00, 0x00, 0x12, 0x12, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x7d, 0xf7 },
{ 0x02, 0x00, 0xfe, 0x0e, 0xf0, 0x41, 0x0a, 0x00, 0x00, 0x12, 0x12, 0x03, 0x02, 0x00, 0x01, 0x00, 0x7a, 0xf7, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
};
memcpy(dhost, masterDevice.addr, dhostLen);
setPacketTypeMacro(REAC_STREAM_CONTROL);
if (0 == handshakeSubState) {
memset(packet->data, 0, sizeof(packet->data));
}
else {
memcpy(packet->data,
REAC_STREAM_CONTROL_PACKET_TYPE[CONTROL_PACKET_TYPE_SLAVE_ANNOUNCE1+handshakeSubState-1],
REAC_STREAM_CONTROL_PACKET_TYPE_SIZE);
memcpy(packet->data+REAC_STREAM_CONTROL_PACKET_TYPE_SIZE,
handshakeData[handshakeSubState-1],
sizeof(handshakeData[0]));
}
if (5 == handshakeSubState) {
setHandshakeState(HANDSHAKE_HAS_SENT_ANNOUNCE);
}
else {
handshakeSubState++;
}
applyChecksum(packet);
lastCdeaTwoBytes[0] = packet->data[sizeof(packet->data)-2];
lastCdeaTwoBytes[1] = REACDataStream::applyChecksum(packet);
ret = kIOReturnSuccess;
}
else if (HANDSHAKE_HAS_SENT_ANNOUNCE == handshakeState) {
memcpy(dhost, masterDevice.addr, dhostLen);
setPacketTypeMacro(REAC_STREAM_FILLER);
for (int i=0; i<31; i+=2) {
packet->data[i+0] = lastCdeaTwoBytes[0];
packet->data[i+1] = lastCdeaTwoBytes[1];
}
ret = kIOReturnSuccess;
}
return ret;
}
bool REACSlaveDataStream::gotPacket(const REACPacketHeader *packet, const EthernetHeader *header) {
if (super::gotPacket(packet, header)) {
return true;
}
if (HANDSHAKE_NOT_INITIATED == handshakeState) {
if (0 == handshakeSubState) {
if (isControlPacketType(packet, CONTROL_PACKET_TYPE_ONE) &&
0xc0 == packet->data[29] &&
0xa8 == packet->data[30]) {
handshakeSubState = 1;
}
}
else if (1 == handshakeSubState &&
isControlPacketType(packet, CONTROL_PACKET_TYPE_ONE) &&
0x01 == packet->data[5] &&
0x01 == packet->data[6] &&
0 == memcmp(packet->data+7, packet->data+17, ETHER_ADDR_LEN)) {
memcpy(masterDevice.addr, packet->data+7, sizeof(masterDevice.addr));
setHandshakeState(HANDSHAKE_GOT_MAC_ADDRESS_INFO);
lastGotMacAddressInfoStateUpdate = recievedPacketCounter;
firstChannelInfoId = -1;
}
else {
resetHandshakeState();
}
}
else if (HANDSHAKE_GOT_MAC_ADDRESS_INFO == handshakeState) {
if ((SInt64)recievedPacketCounter-(SInt64)lastGotMacAddressInfoStateUpdate > 2*REAC_PACKETS_PER_SECOND) {
resetHandshakeState();
}
else if (isControlPacketType(packet, CONTROL_PACKET_TYPE_THREE)) {
lastGotMacAddressInfoStateUpdate = recievedPacketCounter;
if (true) {
setHandshakeState(HANDSHAKE_SENDING_INITIAL_ANNOUNCE);
}
else {
for (UInt32 i=5; i<8*3; i+=3) {
if (-1 == firstChannelInfoId) {
firstChannelInfoId = packet->data[i];
}
else if (firstChannelInfoId == packet->data[i]) {
setHandshakeState(HANDSHAKE_SENDING_INITIAL_ANNOUNCE);
}
}
}
}
}
return false;
}
void REACSlaveDataStream::setHandshakeState(HandshakeState state) {
IOLog("REACSlaveDataStream::setHandshakeState(): Set handshake state: %d\n", state); // TODO Debug
handshakeState = state;
handshakeSubState = 0;
}
void REACSlaveDataStream::resetHandshakeState() {
setHandshakeState(HANDSHAKE_NOT_INITIATED);
}