forked from benlaurie/arduino--
-
Notifications
You must be signed in to change notification settings - Fork 0
/
star.h
333 lines (306 loc) · 8.8 KB
/
star.h
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
// -*- mode: c++; indent-tabs-mode: nil; -*-
/*
* There is one master and up to 256 slaves. Slaves can only send to
* the master.
*
* The contents are:
*
* byte ID;
* byte TYPE;
* byte LENGTH;
* byte MESSAGE[LENGTH];
*
* When sent by a slave, ID is the id of the sender, if they have
* one. When sent by the master, it is the id of the recipient.
*
* Request ID (Slave Type 0):
*
* ID = 0. Note that id 0 is a valid id!
*
* MESSAGE is the MAC of the sender. It can be any length > 0. Two
* slaves should not have the same MAC! Note that the master may
* impose a limit on MAC length, in which case it will not respond
* to a MAC that is too long. A MAC of up to 8 bytes is always
* acceptable.
*
* The response is an Allocate ID packet.
*
* This message must be resent periodically until a response is
* received. No other message type can be send until an ID is
* allocated.
*
*
* Allocate ID (Master Type 0):
*
* ID = allocated id.
*
* MESSAGE is the MAC of the requestor.
*
*
* Out of IDs Error (Master Type 1):
*
* ID = 0
*
* MESSAGE = MAC of the requestor.
*
* Presumably the slave should give some kind of visual indication
* if it gets this error. It can retry in case the master changes
* its mind.
*
*/
#include "arduino--.h"
#include <string.h>
class StarBase
{
public:
enum MessageType
{
// Slave
REQUEST_ID = 0x00,
USER_SLAVE_MESSAGE = 0x80,
// Master
ALLOCATE_ID = 0x40,
OUT_OF_IDS = 0x41,
RESET_ID = 0x42,
USER_MASTER_MESSAGE = 0xc0,
};
enum MasterType
{
};
protected:
static uint32_t protocolError_;
};
template <class Network, class Observer> class StarNode : public StarBase
{
protected:
static void sendPacket(byte id, byte type, byte length, const byte *data)
{
if (!Network::canSend())
{
Observer::cantSend();
return;
}
Network::sendPacket(id, type, length, data);
Observer::sentPacket(id, type, length, data);
}
};
template <class Network, class Observer> class StarSlave
: public StarNode<Network, Observer>
{
public:
// Note that this just keeps a pointer, so you need to make sure
// |mac| stays around.
static void init(const byte *mac, byte length)
{
mac_ = mac;
length_ = length;
Network::init();
}
static bool pollNeeded()
{
if (Network::dataAvailable()
|| (Network::canSend() && (Observer::wantSend()
|| !idSet_)))
return true;
Network::enableReceive();
return false;
}
static void poll()
{
if (Network::dataAvailable())
processPacket();
if (Network::canSend())
{
if (!idSet_)
getID();
else
Observer::canSend();
}
Network::enableReceive();
}
static bool fastPollNeeded()
{ return Network::fastPollNeeded(); }
// Only call this from Observer::canSend()
static void sendPacket(byte type, byte length, const byte *data)
{
StarNode<Network, Observer>::sendPacket
(id_, type | StarBase::USER_SLAVE_MESSAGE, length, data);
}
private:
static void processPacket()
{
Observer::gotPacket(Network::getID(), Network::getType(),
Network::getLength(), Network::getData());
switch (Network::getType())
{
case StarBase::ALLOCATE_ID:
if (Network::getLength() != length_)
// Can't be us.
break;
{
const byte *data = Network::getData();
// our ID may have come back
for (byte n = 0; n < length_; ++n)
if (data[n] != mac_[n])
return;
}
id_ = Network::getID();
idSet_ = true;
break;
case StarBase::RESET_ID:
idSet_ = false;
break;
default:
++StarBase::protocolError_;
Observer::protocolError(Network::getID(), Network::getType(),
Network::getLength(), Network::getData());
break;
}
}
static void getID()
{
// try (or retry) to get an ID
StarNode<Network, Observer>::sendPacket(0, StarBase::REQUEST_ID,
length_, mac_);
}
static byte id_;
static bool idSet_;
static const byte *mac_;
static byte length_;
};
template <class Network, class Observer, class Processor> class StarMaster
: public StarNode<Network, Observer>
{
public:
static void init()
{
Network::init();
resetCount_ = 5;
}
static void poll()
{
Network::enableReceive();
if (Network::dataAvailable())
processPacket();
if (resetCount_ > 0)
{
if (!Network::canSend())
{
Observer::cantSend();
return;
}
StarNode<Network, Observer>::sendPacket(0, StarBase::RESET_ID, 0,
NULL);
--resetCount_;
return;
}
}
static void processPacket()
{
byte type = Network::getType();
Observer::gotPacket(Network::getID(), type, Network::getLength(),
Network::getData());
if (type != StarBase::REQUEST_ID
&& macs_[Network::getID()].length() == 0)
{
resetCount_ = 1;
return;
}
switch (type)
{
case StarBase::REQUEST_ID:
allocateID();
break;
default:
if (type >= StarBase::USER_SLAVE_MESSAGE
&& type < StarBase::USER_MASTER_MESSAGE)
Processor::processUserMessage(type, Network::getLength(),
Network::getData());
else
++StarBase::protocolError_;
break;
}
}
private:
static void allocateID()
{
if (Network::getLength() > Mac::MAX_MAC)
{
++StarBase::protocolError_;
return;
}
byte empty = 0;
for (byte n = 0; n < NMACS ; )
{
if (macs_[n].length() == 0)
empty = n;
else if (macs_[n].is(Network::getLength(), Network::getData()))
{
macs_[n].sendID(n);
return;
}
if (++n == 0)
break;
}
if (macs_[empty].length() != 0)
{
StarNode<Network, Observer>::sendPacket(
0,
StarBase::OUT_OF_IDS, Network::getLength(),
Network::getData());
return;
}
macs_[empty].set(Network::getLength(), Network::getData());
macs_[empty].sendID(empty);
}
static const byte NMACS = 8;
class Mac
{
public:
byte length() const { return length_; }
bool is(const byte length, const byte *mac)
{ return length == length_ && memcmp(mac, mac_, length) == 0; }
void set(byte length, const byte *mac)
{
length_ = length;
memcpy(mac_, mac, length_);
}
void sendID(byte id)
{
StarMaster::sendPacket(id, StarBase::ALLOCATE_ID, length_, mac_);
Observer::idSent(id, length_, mac_);
}
static const byte MAX_MAC = 8;
private:
byte length_;
byte mac_[MAX_MAC];
};
static Mac macs_[NMACS];
static byte resetCount_;
};
uint32_t StarBase::protocolError_;
#define T template <class Network, class Observer, class Processor>
#define M StarMaster<Network, Observer, Processor>
T typename M::Mac M::macs_[M::NMACS];
T byte M::resetCount_;
#undef D
#undef M
template <class Network, class Observer>
bool StarSlave<Network, Observer>::idSet_;
template <class Network, class Observer> byte StarSlave<Network, Observer>::id_;
template <class Network, class Observer>
byte StarSlave<Network, Observer>::length_;
template <class Network, class Observer>
const byte *StarSlave<Network, Observer>::mac_;
class NullSlaveObserver
{
public:
static void cantSend() {}
static void gotPacket(byte id, byte type, byte length, const byte *data)
{}
static void protocolError(byte id, byte type, byte length, const byte *data)
{}
static void sentPacket(byte id, byte type, byte length, const byte *data)
{}
static void canSend() {}
};