-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNibeGw.cpp
398 lines (341 loc) · 9.23 KB
/
NibeGw.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
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*
* ----------------------------------------------------------------------------
*
* Author: pauli.anttila@gmail.com
*
*/
#include "NibeGw.h"
#include "Arduino.h"
#if defined(HARDWARE_SERIAL_WITH_PINS)
NibeGw::NibeGw(HardwareSerial* serial, int RS485DirectionPin, int RS485RxPin, int RS485TxPin)
#elif defined(HARDWARE_SERIAL)
NibeGw::NibeGw(HardwareSerial* serial, int RS485DirectionPin)
#else
NibeGw::NibeGw(Serial_* serial, int RS485DirectionPin)
#endif
{
#if defined(HARDWARE_SERIAL_WITH_PINS)
this->RS485RxPin = RS485RxPin;
this->RS485TxPin = RS485TxPin;
#endif
verbose = 0;
ackModbus40 = true;
ackSms40 = false;
ackRmu40 = false;
sendAcknowledge = true;
state = STATE_WAIT_START;
connectionState = false;
RS485 = serial;
directionPin = RS485DirectionPin;
pinMode(directionPin, OUTPUT);
digitalWrite(directionPin, LOW);
setCallback(NULL, NULL);
}
void NibeGw::connect() {
if (!connectionState) {
state = STATE_WAIT_START;
#if defined(HARDWARE_SERIAL_WITH_PINS)
RS485->begin(9600, SERIAL_8N1, RS485RxPin, RS485TxPin);
#else
RS485->begin(9600, SERIAL_8N1);
#endif
connectionState = true;
}
}
void NibeGw::disconnect() {
if (connectionState) {
RS485->end();
connectionState = false;
}
}
boolean NibeGw::connected() {
return connectionState;
}
void NibeGw::setVerboseLevel(byte level) {
verbose = level;
}
NibeGw& NibeGw::setCallback(void (*callback_msg_received)(const byte* const data, int len), int (*callback_msg_token_received)(eTokenType token, byte* data)) {
this->callback_msg_received = callback_msg_received;
this->callback_msg_token_received = callback_msg_token_received;
return *this;
}
#ifdef ENABLE_NIBE_DEBUG
NibeGw& NibeGw::setDebugCallback(void (*debug)(byte verbose, char* data)) {
this->debug = debug;
return *this;
}
#endif
void NibeGw::setAckModbus40Address(boolean val) {
ackModbus40 = val;
}
void NibeGw::setAckSms40Address(boolean val) {
ackSms40 = val;
}
void NibeGw::setAckRmu40Address(boolean val) {
ackRmu40 = val;
}
void NibeGw::setSendAcknowledge(boolean val) {
sendAcknowledge = val;
}
boolean NibeGw::messageStillOnProgress() {
if (!connectionState)
return false;
if (RS485->available() > 0)
return true;
if (state == STATE_CRC_FAILURE || state == STATE_OK_MESSAGE_RECEIVED)
return true;
return false;
}
void NibeGw::loop() {
if (!connectionState)
return;
switch (state) {
case STATE_WAIT_START:
if (RS485->available() > 0) {
byte b = RS485->read();
#ifdef ENABLE_NIBE_DEBUG
if (debug) {
sprintf(debug_buf, "%02X ", b);
debug(3, debug_buf);
}
#endif
if (b == 0x5C) {
buffer[0] = b;
index = 1;
state = STATE_WAIT_DATA;
#ifdef ENABLE_NIBE_DEBUG
if (debug)
debug(4, "\nFrame start found\n");
#endif
}
}
break;
case STATE_WAIT_DATA:
if (RS485->available() > 0) {
byte b = RS485->read();
#ifdef ENABLE_NIBE_DEBUG
if (debug) {
sprintf(debug_buf, "%02X", b);
debug(3, debug_buf);
}
#endif
if (index >= MAX_DATA_LEN) {
// too long message
state = STATE_WAIT_START;
} else {
buffer[index++] = b;
int msglen = checkNibeMessage(buffer, index);
#ifdef ENABLE_NIBE_DEBUG
if (debug) {
sprintf(debug_buf, "\ncheckMsg=%d\n", msglen);
debug(5, debug_buf);
}
#endif
switch (msglen) {
case 0: break; // Ok, but not ready
case -1: state = STATE_WAIT_START; break; // Invalid message
case -2: state = STATE_CRC_FAILURE; break; // Checksum error
default: state = STATE_OK_MESSAGE_RECEIVED; break;
}
}
}
break;
case STATE_CRC_FAILURE:
#ifdef ENABLE_NIBE_DEBUG
if (debug)
debug(1, "CRC failure\n");
#endif
if (shouldAckNakSend(buffer[2]))
sendNak();
state = STATE_WAIT_START;
break;
case STATE_OK_MESSAGE_RECEIVED:
// now clear double 0x5C, which are to indicate a real value of 0x5C
for (int i = 5; i < index-1; i++) {
if(buffer[i]==0x5C && buffer[i+1]==0x5C) {
memmove(&buffer[i], &buffer[i+1], index -2 - i);
buffer[4] -= 1;
}
}
index = buffer[4] + 6;
if (buffer[0] == 0x5C && buffer[1] == 0x00 && buffer[2] == 0x20 && buffer[4] == 0x00 && (buffer[3] == 0x69 || buffer[3] == 0x6B)) {
eTokenType token = buffer[3] == 0x6B ? WRITE_TOKEN : READ_TOKEN;
#ifdef ENABLE_NIBE_DEBUG
if (debug)
debug(1, "Token received\n");
#endif
int msglen = callback_msg_token_received(token, buffer);
if (msglen > 0) {
sendData(buffer, (byte)msglen);
} else {
#ifdef ENABLE_NIBE_DEBUG
if (debug)
debug(2, "No message to send\n");
#endif
if (shouldAckNakSend(buffer[2]))
sendAck();
}
} else {
#ifdef ENABLE_NIBE_DEBUG
if (debug) {
debug(1, "Message received\n");
}
#endif
if ((buffer[2] == 0x19) || (buffer[2] == 0x1A) || (buffer[2] == 0x1B) || (buffer[2] == 0x1C)) { // rmu
if (buffer[3] == 0x60) { // send data message
int msglen = callback_msg_token_received(RMU_TOKEN, buffer);
if (msglen > 0) {
sendData(buffer, (byte)msglen);
} else {
#ifdef ENABLE_NIBE_DEBUG
if (debug)
debug(2, "No message to send\n");
#endif
if (shouldAckNakSend(buffer[2]))
sendAck();
}
} else if (buffer[3] == 0x63) {
buffer[0] = 192;
buffer[1] = 96;
buffer[2] = 2;
buffer[3] = 99;
buffer[4] = 0;
buffer[5] = 193;
sendData(buffer, (byte)6);
} else if (buffer[3] == 0xEE) {
// Sending RMU Version v259
buffer[0] = 192;
buffer[1] = 238;
buffer[2] = 3;
buffer[3] = 238;
buffer[4] = 3;
buffer[5] = 1;
buffer[6] = 193;
sendData(buffer, (byte)7);
} else {
if (shouldAckNakSend(buffer[2]))
sendAck();
}
} else {
if (shouldAckNakSend(buffer[2]))
sendAck();
callback_msg_received(buffer, index);
}
}
state = STATE_WAIT_START;
break;
}
}
/*
Return:
>0 if valid message received (return message len)
0 if ok, but message not ready
-1 if invalid message
-2 if checksum fails
*/
int NibeGw::checkNibeMessage(const byte* const data, byte len) {
if (len <= 0)
return 0;
if (len >= 1) {
if (data[0] != 0x5C)
return -1;
if (len >= 2) {
if (data[1] != 0x00)
return -1;
}
if (len >= 6) {
int datalen = data[4];
if (len < datalen + 6)
return 0;
byte checksum = 0;
// calculate XOR checksum
for (int i = 2; i < (datalen + 5); i++)
checksum ^= data[i];
byte msg_checksum = data[datalen + 5];
#ifdef ENABLE_NIBE_DEBUG
if (debug) {
sprintf(debug_buf, "\nchecksum=%02X, msg_checksum=%02X\n", checksum, msg_checksum);
debug(4, debug_buf);
}
#endif
if (checksum != msg_checksum) {
// if checksum is 0x5C (start character),
// heat pump seems to send 0xC5 checksum
if (checksum != 0x5C && msg_checksum != 0xC5)
return -2;
}
return datalen + 6;
}
}
return 0;
}
void NibeGw::sendData(const byte* const data, byte len) {
#ifdef ENABLE_NIBE_DEBUG
if (debug) {
debug(1, "Send message to heat pump: ");
for (int i = 0; i < len; i++) {
sprintf(debug_buf, "%02X", data[i]);
debug(1, debug_buf);
}
debug(1, "\n");
}
#endif
digitalWrite(directionPin, HIGH);
delay(1);
RS485->write(data, len);
RS485->flush();
delay(1);
digitalWrite(directionPin, LOW);
}
void NibeGw::sendAck() {
#ifdef ENABLE_NIBE_DEBUG
if (debug)
debug(1, "Send ACK\n");
#endif
digitalWrite(directionPin, HIGH);
delay(1);
RS485->write(0x06);
RS485->flush();
delay(1);
digitalWrite(directionPin, LOW);
}
void NibeGw::sendNak() {
#ifdef ENABLE_NIBE_DEBUG
if (debug)
debug(1, "Send NACK\n");
#endif
digitalWrite(directionPin, HIGH);
delay(1);
RS485->write(0x15);
RS485->flush();
delay(1);
digitalWrite(directionPin, LOW);
}
boolean NibeGw::shouldAckNakSend(byte address) {
if (sendAcknowledge) {
if (address == MODBUS40 && ackModbus40)
return true;
else if (address == RMU40 && ackRmu40)
return true;
else if (address == SMS40 && ackSms40)
return true;
}
return false;
}
void NibeGw::testloop() {
uint16_t index;
for(index = 0; index < 10000; index++) {
sendAck();
}
}