-
Notifications
You must be signed in to change notification settings - Fork 1
/
Commandes.ino
324 lines (211 loc) · 10.2 KB
/
Commandes.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
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
//-------- High level commandes
// Get status
// Turn device On/Off
// Turn Lamp On/Off ( not same DPS as device )
// ... Add your own depending on DPS needed
//--------------------------------------------------
void getStatus( ){
Serial.println();
Serial.println("-- Send Commande 10 : Request status --");
tryConnect(10);
//memcpy( Msg_key, dev.key, KEY_LENGTH );
getKey( ) ;
ret = cmdStatus();
_client.stop();
}
//--------------------------------------------------
int turnOn() {
byte Pload1[] = { 0x22, 0x31, 0x22, 0x3a, 0x74, 0x72, 0x75, 0x65 } ; // "1":true
Serial.println();
Serial.println("-- Send Command OD DPS1:true : Turn ON --");
Serial.println();
tryConnect(5);
getKey( ) ;
ret = sendCommande( Pload1, sizeof(Pload1) );
_client.stop();
return ret ;
}
//--------------------------------------------------
int turnOff() {
byte Pload1[] = { 0x22, 0x31, 0x22, 0x3a, 0x66, 0x61, 0x6c, 0x73, 0x65 } ; // "1":false
Serial.println();
Serial.println("-- Send Command OD DPS1:false : Turn OFF --");
Serial.println();
tryConnect(5);
getKey( ) ;
ret = sendCommande( Pload1, sizeof(Pload1) );
_client.stop();
return ret ;
}
//--------------------------------------------------
int lampOn() {
byte Pload1[] = { 0x22, 0x32, 0x30, 0x22, 0x3a, 0x74, 0x72, 0x75, 0x65 } ; // "20":true
Serial.println();
Serial.println("-- Send Command OD DPS20:true : Lamp ON --");
Serial.println();
tryConnect(5);
getKey( ) ;
ret = sendCommande( Pload1, sizeof(Pload1) );
_client.stop();
return ret ;
}
//--------------------------------------------------
int lampOff() {
byte Pload1[] = { 0x22, 0x32, 0x30, 0x22, 0x3a,0x66, 0x61, 0x6c, 0x73, 0x65 } ; // "20":false
Serial.println();
Serial.println("-- Send Command OD DPS20:false : Lamp OFF --");
Serial.println();
tryConnect(5);
getKey( ) ;
ret = sendCommande( Pload1, sizeof(Pload1) );
_client.stop();
return ret ;
}
//--------------------------------------------------
int listen() { // Not checked yet!!!!!!!!!
tryConnect(5);
getKey( ) ;
Serial.println();
Serial.println("-- Send Commande : Listen --");
Serial.println();
//----------------------- Reception de la réponse
memcpy( crypt_key, Xor_key, 16 ) ; // On copie la clé de decryptage
while (1) {
len = receiveMsg( ) ;
if ( len ) {
DEBUG_PRINTLN("----------------------");
DEBUG_PRINT("Received listen : " ); p5( incomingPacket, len );
DEBUG_PRINTLN("----------------------");
Lmes.TotL = len ;
InitLmes( 'M' ); // Init Message structure ( length)4
unpackMsg();
}
}
_client.stop();
return ret ;
}
//--------------------------------------------------
int cmdStatus() { // Send commande 10 status
byte Pref34[] = { 0x00, 0x00, 0x55, 0xaa, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x34 } ;
byte m3a35[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x1e } ;
Tmes.Pload[0] = 0x7B ;
Tmes.Pload[1] = 0x7D ;
Lmes.PloadL = 2;
if ( dev.vers == 4 ) {
padPload();
//------------ Crypt AES le pload with new key
encrypt_v34( Tmes.Pload, Xor_key, 16 );
//------------ Format prefix + pload
outMsgL = 0 ;
addTooutMsg( Pref34, sizeof(Pref34) ) ;
addTooutMsg( Tmes.Pload, Lmes.PloadL ) ;
//------------ Compute SHA256Key on prefix + pload with new key
genSHA256Key( Xor_key, outMsg, SHA256Key, 32 );
//------------ Finalize Message
addTooutMsg( SHA256Key, SHA256HMAC_SIZE ) ;
addTooutMsg( Suf34, sizeof( Suf34 ) ) ;
}
if ( dev.vers == 5 ) {
memcpy( wIV, local_nonce, 12 ) ; // Initial vector
encrypt_v35( Tmes.Pload, Xor_key, wIV, Lmes.PloadL, m3a35, 14 ) ;
//------------ Format message
outMsgL = 0 ;
addTooutMsg( head66, sizeof( head66 ) ) ;
addTooutMsg( m3a35, sizeof( m3a35 ) ) ;
addTooutMsg( wIV, sizeof( wIV ) ) ;
addTooutMsg( Tmes.Pload, Lmes.PloadL ) ;
addTooutMsg( wTag, sizeof( wTag ) ) ;
addTooutMsg( Suf35, sizeof( Suf35 ) ) ;
}
DEBUG_PRINTLN("----------------------");
DEBUG_PRINT("OutMsg 3 : " ); p5( outMsg, outMsgL );
DEBUG_PRINTLN("----------------------");
//----------------------- Send message
sendMsg( outMsg, outMsgL ) ;
//----------------------- Receive answer
len = receiveMsg( ) ;
DEBUG_PRINTLN("----------------------");
DEBUG_PRINT("Received Status : " ); p5( incomingPacket, len );
DEBUG_PRINTLN("----------------------");
Lmes.TotL = len ;
InitLmes( 'M' ); // Init Message structure ( length)
memcpy( crypt_key, Xor_key, 16 ) ; // On copie la nouvelle clé de decryptage
unpackMsg();
return len ;
}
//--------------------------------------------------
int sendCommande( byte dps[], int dpslen ) { // Send commande OD DPS update status
byte Pref34[] = { 0x00, 0x00, 0x55, 0xaa, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x74 } ;
byte m4a35[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x1e } ;
byte Pload134[] = { 0x33, 0x2E, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // b'3.4\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
byte Pload135[] = { 0x33, 0x2E, 0x35, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // b'3.5\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00
byte Pload2p[] = { 0x7b, 0x22, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x22, 0x3a, 0x35, 0x2c, 0x22, 0x74, 0x22, 0x3a }; // "protocol":5,"t":
byte Pload3d[] = { 0x2c, 0x22, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3a, 0x7b, 0x22, 0x64, 0x70, 0x73, 0x22, 0x3a, 0x7b }; // ,"data":{"dps":{
byte Pload4f[] = { 0x7d, 0x7d, 0x7d } ; // }}}
//------------------ Create Pload
getTime();
Lmes.PloadL = 0 ;
if ( dev.vers == 4 ) { addToPload( Pload134 , sizeof(Pload134) ) ; } // Pload1
if ( dev.vers == 5 ) { addToPload( Pload135 , sizeof(Pload135) ) ; } // Pload1
addToPload( Pload2p , sizeof(Pload2p) ) ; // Pload2
addToPload( timebuf , 10 ) ; // timestamp
addToPload( Pload3d , sizeof(Pload3d) ) ; // Pload3
addToPload( dps , dpslen ) ; // dps
addToPload( Pload4f , sizeof(Pload4f) ) ; // End
DEBUG_PRINT("Pload : " ); p5( Tmes.Pload, Lmes.PloadL );
if ( dev.vers == 4 ) {
padPload ( );
//------------ Crypte AES pload with new Key
encrypt_v34( Tmes.Pload, Xor_key, Lmes.PloadL );
//------------ Format prefix + pload
outMsgL = 0 ;
addTooutMsg( Pref34, sizeof(Pref34) ) ; // Prefix
addTooutMsg( Tmes.Pload, Lmes.PloadL ) ; // Pload
//------------ Compute SHA256Key on prefix + pload with new Key
genSHA256Key( Xor_key, outMsg, SHA256Key, Lmes.PloadL +16 );
//------------ Finalize Message
addTooutMsg( SHA256Key, SHA256HMAC_SIZE ) ; // CRC
addTooutMsg( Suf34, sizeof(Suf34) ) ; // Suffix
}
if ( dev.vers == 5 ) {
//------------------------- Adjust Datalength
unsigned long DataLen = 0;
DataLen = 12 + Lmes.PloadL + 16 ; // DataLen = IV + Msg + Tag = 12 + 71 + 16 = 99
m4a35[13] = DataLen ; // ajust length ok pour Msgs < 256c
memcpy( wIV, local_nonce, 12 ) ; // Copie IV
encrypt_v35( Tmes.Pload, Xor_key, wIV, Lmes.PloadL, m4a35, 14 ) ;
//--------------- Format message
outMsgL = 0 ;
addTooutMsg( head66, sizeof( head66 ) ) ; // Heading
addTooutMsg( m4a35, sizeof( m4a35 ) ) ; // Preffix
addTooutMsg( wIV, sizeof( wIV ) ) ; // IV
addTooutMsg(Tmes.Pload, Lmes.PloadL ) ; // Pload
addTooutMsg( wTag, sizeof( wTag ) ) ; // Tag
addTooutMsg( Suf35, sizeof( Suf35 ) ) ; // Suffix
}
DEBUG_PRINTLN("----------------------");
DEBUG_PRINT("OutMsg 4 : " ); p5( outMsg, outMsgL );
DEBUG_PRINTLN("----------------------");
//----------------------- Send message
sendMsg( outMsg, outMsgL ) ;
//----------------------- Process responses : We can receive 2 messages : Ignored
// 1 response to the comman 0D with Pload null commande 0D
// 1 response Comman 08, Heartbeat ??
//----------------------- Reception de la réponse 1
len = receiveMsg( ) ;
DEBUG_PRINTLN("----------------------");
DEBUG_PRINT("Received 4.1 : " ); p5( incomingPacket, len );
DEBUG_PRINTLN("----------------------");
Lmes.TotL = len ;
InitLmes( 'M' ); // Init Message structure ( length)
memcpy( crypt_key, Xor_key, 16 ) ; // Copie decrypt key
unpackMsg();
//----------------------- Reception de la réponse 2
len = receiveMsg( ) ;
DEBUG_PRINTLN("----------------------");
DEBUG_PRINT("Received 4.2 : " ); p5( incomingPacket, len );
DEBUG_PRINTLN("----------------------");
Lmes.TotL = len ;
unpackMsg();
return len ;
}