-
Notifications
You must be signed in to change notification settings - Fork 13
/
commands.ino
269 lines (224 loc) · 6.99 KB
/
commands.ino
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
void transmitCommand(int iCommand, int iLength, byte data[])
{
int i;
uint8_t specialCharacter[1];
uint8_t message[256];
// Build message payload, starting with the type field
message[0] = (byte)(iCommand >> 8);
message[1] = (byte)iCommand;
// Add message length
message[2] = (byte)(iLength >> 8);
message[3] = (byte)iLength;
// Calculate checksum of header
byte csum = 0;
csum ^= message[0];
csum ^= message[1];
csum ^= message[2];
csum ^= message[3];
// Add message data and update checksum
if (iLength != 0)
{
for (i = 0; i < iLength; i++)
{
message[5 + i] = data[i];
csum ^= data[i];
}
}
// Add checksum
message[4] = csum;
// Transmit the message, send start character first
specialCharacter[0] = 1;
writeByte(specialCharacter[0]);
// Transmit message payload with byte stuffing as required
for (i = 0; i < iLength + 5; i++)
{
// Check if stuffing is required
if (message[i] < 0x10)
{
// First send escape character then message byte XOR'd with 0x10
specialCharacter[0] = 2;
writeByte(specialCharacter[0]);
int msg = message[i];
msg = msg ^ 0x10;
message[i] = (byte)msg;
writeByte(message[i]);
}
else
{
// Send the character with no modification
writeByte(message[i]);
}
}
// Send end character
specialCharacter[0] = 3;
writeByte(specialCharacter[0]);
}
void sendReadAttribRequest(uint16_t u16ShortAddr, byte u8SrcEndPoint, byte u8DstEndPoint, uint16_t u16ClusterID, byte u8Direction, byte u8ManuSpecific, uint16_t u16ManuID, byte u8AttribCount, uint16_t u16AttribID1)
{
byte commandData[14];
byte u8Len = 0;
// Build command payload
commandData[u8Len++] = 0x02; // Short address mode
commandData[u8Len++] = (byte)(u16ShortAddr >> 8);
commandData[u8Len++] = (byte)u16ShortAddr;
commandData[u8Len++] = u8SrcEndPoint;
commandData[u8Len++] = u8DstEndPoint;
commandData[u8Len++] = (byte)(u16ClusterID >> 8);
commandData[u8Len++] = (byte)u16ClusterID;
commandData[u8Len++] = u8Direction;
commandData[u8Len++] = u8ManuSpecific;
commandData[u8Len++] = (byte)(u16ManuID >> 8);
commandData[u8Len++] = (byte)u16ManuID;
commandData[u8Len++] = u8AttribCount;
commandData[u8Len++] = (byte)(u16AttribID1 >> 8);
commandData[u8Len++] = (byte)u16AttribID1;
// Transmit command
transmitCommand(0x0100, u8Len, commandData);
}
void setPermitJoin(uint16_t u16ShortAddr, byte u8Interval, byte u8TCsignificance)
{
byte commandData[4];
// Build command payload
commandData[0] = (byte)(u16ShortAddr >> 8);
commandData[1] = (byte)u16ShortAddr;
commandData[2] = u8Interval;
commandData[3] = u8TCsignificance;
// Transmit command
transmitCommand(0x0049, 4, commandData);
}
void activeEndpointDescriptorRequest(uint16_t u16ShortAddr)
{
byte commandData[2];
// Build command payload
commandData[0] = (byte)(u16ShortAddr >> 8);
commandData[1] = (byte)u16ShortAddr;
// Transmit command
transmitCommand(0x0045, 2, commandData);
}
void simpleDescriptorRequest(uint16_t u16ShortAddr, byte u8EndPoint)
{
byte commandData[3];
// Build command payload
commandData[0] = (byte)(u16ShortAddr >> 8);
commandData[1] = (byte)u16ShortAddr;
commandData[2] = u8EndPoint;
// Transmit command
transmitCommand(0x0043, 3, commandData);
}
void sendBindRequest(uint64_t u64TargetExtAddr, byte u8TargetEndPoint, uint16_t u16ClusterID, byte u8DstAddrMode, uint64_t u64DstAddr, byte u8DstEndPoint)
{
byte commandData[32];
byte u8Len = 0;
// Build command payload
commandData[u8Len++] = (byte)(u64TargetExtAddr >> 56);
commandData[u8Len++] = (byte)(u64TargetExtAddr >> 48);
commandData[u8Len++] = (byte)(u64TargetExtAddr >> 40);
commandData[u8Len++] = (byte)(u64TargetExtAddr >> 32);
commandData[u8Len++] = (byte)(u64TargetExtAddr >> 24);
commandData[u8Len++] = (byte)(u64TargetExtAddr >> 16);
commandData[u8Len++] = (byte)(u64TargetExtAddr >> 8);
commandData[u8Len++] = (byte)u64TargetExtAddr;
commandData[u8Len++] = u8TargetEndPoint;
commandData[u8Len++] = (byte)(u16ClusterID >> 8);
commandData[u8Len++] = (byte)u16ClusterID;
commandData[u8Len++] = u8DstAddrMode;
if (u8DstAddrMode == 3)
{
commandData[u8Len++] = (byte)(u64DstAddr >> 56);
commandData[u8Len++] = (byte)(u64DstAddr >> 48);
commandData[u8Len++] = (byte)(u64DstAddr >> 40);
commandData[u8Len++] = (byte)(u64DstAddr >> 32);
commandData[u8Len++] = (byte)(u64DstAddr >> 24);
commandData[u8Len++] = (byte)(u64DstAddr >> 16);
commandData[u8Len++] = (byte)(u64DstAddr >> 8);
commandData[u8Len++] = (byte)u64DstAddr;
commandData[u8Len++] = u8DstEndPoint;
}
else
{
commandData[u8Len++] = (byte)(u64DstAddr >> 8);
commandData[u8Len++] = (byte)u64DstAddr;
}
// Transmit command
transmitCommand(0x0030, u8Len, commandData);
}
void setChannel(int channel)
{
if (channel >= 11 && channel <= 26)
{
// User is specifying a single channel, we must create the 32-bit mask from this
uint32_t u32ChannelMaskTemp = 1;
for (int i = 0; i < channel; i++)
{
u32ChannelMaskTemp <<= 1;
}
// Set channel mask
setChannelMask(u32ChannelMaskTemp);
}
}
void setChannelMask(uint32_t uiMask)
{
byte commandData[4];
// Build command payload
commandData[0] = (byte)(uiMask >> 24);
commandData[1] = (byte)(uiMask >> 16);
commandData[2] = (byte)(uiMask >> 8);
commandData[3] = (byte)uiMask;
// Transmit command
transmitCommand(0x0021, 4, commandData);
}
void setTime(uint32_t timestamp)
{
byte commandData[4];
// Build command payload
commandData[0] = (byte)(timestamp >> 24);
commandData[1] = (byte)(timestamp >> 16);
commandData[2] = (byte)(timestamp >> 8);
commandData[3] = (byte)timestamp;
// Transmit command
transmitCommand(0x0016, 4, commandData);
}
void setDeviceType(byte deviceType) // 0 - Coordinator, 1 - router, 2 end device
{
byte commandData[1];
// Build command payload
commandData[0] = deviceType;
// Transmit command
transmitCommand(0x0023, 1, commandData);
}
void sendClusterOnOff(byte u8AddrMode, uint16_t u16ShortAddr, byte u8SrcEndPoint, byte u8DstEndPoint, byte u8CommandID)
{
byte commandData[6];
// Build command payload
commandData[0] = u8AddrMode;
commandData[1] = (byte)(u16ShortAddr >> 8);
commandData[2] = (byte)u16ShortAddr;
commandData[3] = u8SrcEndPoint;
commandData[4] = u8DstEndPoint;
commandData[5] = u8CommandID;
// Transmit command
transmitCommand(0x0092, 6, commandData);
}
void sendMgmtLqiRequest(uint16_t u16ShortAddr, byte u8StartIndex)
{
byte commandData[4];
// Build command payload
commandData[0] = (byte)(u16ShortAddr >> 8);
commandData[1] = (byte)u16ShortAddr;
commandData[2] = u8StartIndex;
// Transmit command
transmitCommand(0x004E, 3, commandData);
}
void DiscoverDevices()
{
uint16_t u16ShortAddr = 0x0000;
byte u8StartIndex = 0;
sendMgmtLqiRequest(u16ShortAddr, u8StartIndex);
}
void writeByte(byte data)
{
uint8_t dataArray[1];
dataArray[0] = data;
// Write data byte to serial port
jnSerial.write(dataArray, 1);
}