forked from sfeakes/AqualinkD
-
Notifications
You must be signed in to change notification settings - Fork 2
/
aq_serial.c
1168 lines (1009 loc) · 34 KB
/
aq_serial.c
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2017 Shaun Feakes - All rights reserved
*
* You may use redistribute and/or modify this code under the terms of
* the GNU General Public License version 2 as published by the
* Free Software Foundation. For the terms of this license,
* see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, 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.
*
* https://github.com/sfeakes/aqualinkd
*/
#include <stdio.h>
#include <stdarg.h>
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdbool.h>
// Below is needed to set low latency.
#include <linux/serial.h>
#include "aq_serial.h"
#include "utils.h"
#include "config.h"
#include "packetLogger.h"
#include "timespec_subtract.h"
/*
Notes for serial usb speed
File should exist if using ftdi chip, ie ftdi_sio driver.
/sys/bus/usb-serial/devices/ttyUSB0/latency_timer
Set to 1 for fastest latency.
Can also be set in code
ioctl(fd, TIOCGSERIAL, &serial);
serial.flags |= ASYNC_LOW_LATENCY;
ioctl(fd, TIOCSSERIAL, &serial);
*/
// Default to send command with leading NUL, this changes that
//#define SEND_CMD_WITH_TRAILING_NUL
//#define BLOCKING_MODE
static bool _blocking_mode = false;
int _blocking_fds = -1;
static struct termios _oldtio;
static struct timespec last_serial_read_time;
void send_packet(int fd, unsigned char *packet, int length);
//unsigned char getProtocolType(unsigned char* packet);
const char* get_pentair_packet_type(unsigned char* packet , int length)
{
static char buf[15];
if (length <= 0 )
return "";
switch (packet[PEN_PKT_CMD]) {
case PEN_CMD_SPEED:
if (packet[PEN_PKT_DEST]== PEN_DEV_MASTER)
return "VSP SetSpeed rtn";
else
return "VSP SetSpeed";
break;
case PEN_CMD_REMOTECTL:
if (packet[PEN_PKT_DEST]== PEN_DEV_MASTER)
return "VSP RemoteCtl rtn";
else
return "VSP RemoteCtl";
break;
case PEN_CMD_POWER:
if (packet[PEN_PKT_DEST]== PEN_DEV_MASTER)
return "VSP SetPower rtn";
else
return "VSP SetPower";
break;
case PEN_CMD_STATUS:
if (packet[PEN_PKT_DEST]== PEN_DEV_MASTER)
return "VSP Status";
else
return "VSP GetStatus";
break;
default:
sprintf(buf, "Unknown '0x%02hhx'", packet[PEN_PKT_CMD]);
return buf;
break;
}
}
const char* get_jandy_packet_type(unsigned char* packet , int length)
{
static char buf[15];
if (length <= 0 )
return "";
switch (packet[PKT_CMD]) {
case CMD_ACK:
if (packet[5] == NUL)
return "Ack";
else
return "Ack w/ Command";
break;
case CMD_STATUS:
return "Status";
break;
case CMD_MSG:
return "Message";
break;
case CMD_MSG_LONG:
return "Lng Message";
break;
case CMD_PROBE:
return "Probe";
break;
case CMD_GETID:
return "GetID";
break;
case CMD_PERCENT:
return "AR %%";
break;
case CMD_PPM:
return "AR PPM";
break;
case CMD_PDA_0x05:
return "PDA Unknown";
break;
case CMD_PDA_0x1B:
return "PDA Init (guess)";
break;
case CMD_PDA_HIGHLIGHT:
return "PDA Hlight";
break;
case CMD_PDA_CLEAR:
return "PDA Clear";
break;
case CMD_PDA_SHIFTLINES:
return "PDA Shiftlines";
break;
case CMD_PDA_HIGHLIGHTCHARS:
return "PDA HlightChars";
break;
case CMD_IAQ_PAGE_MSG:
if (packet[4] == 0x31)
return "iAq setDevice";
else
return "iAq pMessage";
break;
case CMD_IAQ_PAGE_BUTTON:
return "iAq pButton";
break;
case CMD_IAQ_POLL:
return "iAq Poll";
break;
case CMD_IAQ_PAGE_END:
return "iAq PageEnd";
break;
case CMD_IAQ_PAGE_START:
return "iAq PageStart";
break;
case CMD_IAQ_TABLE_MSG:
return "iAq TableMsg";
break;
case CMD_IAQ_PAGE_CONTINUE:
return "iAq PageContinue";
break;
case CMD_IAQ_STARTUP:
return "iAq init";
break;
case CMD_IAQ_TITLE_MESSAGE:
return "iAq ProductName";
break;
case CMD_IAQ_MSG_LONG:
return "iAq Popup message";
break;
case RSSA_DEV_STATUS:
// This is a fail reply 0x10|0x02|0x48|0x13|0x02|0x00|0x10|0x00|0x7f|0x10|0x03|
// Rather than check all, just check 0x02 and checksum sin't I'm not sure 0x10 means faiure without 0x00 around it.
if (packet[4] == 0x02 && packet[8] == 0x7f)
return "RSSA Cmd Error";
else
return "RSSA DevStatus";
break;
case RSSA_DEV_READY:
return "RSSA SendCommand";
break;
case CMD_EPUMP_STATUS:
if (packet[4] == CMD_EPUMP_RPM)
return "ePump RPM";
else if (packet[4] == CMD_EPUMP_WATTS)
return "ePump Watts";
else
return "ePump (unknown)";
break;
case CMD_EPUMP_RPM:
return "ePump set RPM";
break;
case CMD_EPUMP_WATTS:
return "ePump get Watts";
break;
case CMD_JXI_PING:
if (packet[4] == 0x19)
return "LXi heater on";
else // 0x11 is normal off
return "LXi heater ping";
break;
case CMD_JXI_STATUS:
if (packet[6] == 0x10)
return "LXi error";
else
return "LXi status";
break;
default:
sprintf(buf, "Unknown '0x%02hhx'", packet[PKT_CMD]);
return buf;
break;
}
}
const char* get_packet_type(unsigned char* packet , int length)
{
if (getProtocolType(packet)==PENTAIR) {
return get_pentair_packet_type(packet, length);
}
return get_jandy_packet_type(packet, length);
}
// Generate and return checksum of packet.
int generate_checksum(unsigned char* packet, int length)
{
int i, sum, n;
n = length - 3;
sum = 0;
for (i = 0; i < n; i++)
sum += (int) packet[i];
return(sum & 0x0ff);
}
bool check_jandy_checksum(unsigned char* packet, int length)
{
//printf("Checking 0x%02hhx against 0x%02hhx\n",generate_checksum(packet, length), packet[length-3]);
if (generate_checksum(packet, length) == packet[length-3])
return true;
// There seems to be a bug with jandy one touch protocol where on a long msg to one touch you get
// a bad checksum but everything is actually accurate, so forcing a good return on this.
// Example message (always one touch / status message / line 3 and always 0x0a checksum)
// 0x10|0x02|0x43|0x04|0x03|0x20|0x20|0x20|0x20|0x35|0x3a|0x30|0x35|0x20|0x50|0x4d|0x20|0x20|0x20|0x20|0x20|0x0a|0x10|0x03|
if (packet[3] == 0x04 && packet[4] == 0x03 && packet[length-3] == 0x0a) {
LOG(RSSD_LOG,LOG_INFO, "Ignoring bad checksum, seems to be bug in Jandy protocol\n");
if (getLogLevel(RSSD_LOG) >= LOG_DEBUG) {
static char buf[1000];
beautifyPacket(buf,packet,length,true);
LOG(RSSD_LOG,LOG_DEBUG, "Packetin question %s\n",buf);
}
return true;
}
return false;
}
bool check_pentair_checksum(unsigned char* packet, int length)
{
//printf("check_pentair_checksum \n");
int i, sum, n;
n = packet[8] + 9;
//n = packet[8] + 8;
sum = 0;
for (i = 3; i < n; i++) {
//printf("Sum 0x%02hhx\n",packet[i]);
sum += (int) packet[i];
}
//printf("Check High 0x%02hhx = 0x%02hhx = 0x%02hhx\n",packet[n], packet[length-2],((sum >> 8) & 0xFF) );
//printf("Check Low 0x%02hhx = 0x%02hhx = 0x%02hhx\n",packet[n + 1], packet[length-1], (sum & 0xFF) );
// Check against caculated length
if (sum == (packet[length-2] * 256 + packet[length-1]))
return true;
// Check against actual # length
if (sum == (packet[n] * 256 + packet[n+1])) {
LOG(RSSD_LOG,LOG_ERR, "Pentair checksum is accurate but length is not\n");
return true;
}
return false;
}
void generate_pentair_checksum(unsigned char* packet, int length)
{
int i, sum, n;
n = packet[8] + 9;
//n = packet[8] + 6;
sum = 0;
for (i = 3; i < n; i++) {
//printf("Sum 0x%02hhx\n",packet[i]);
sum += (int) packet[i];
}
packet[n+1] = (unsigned char) (sum & 0xFF); // Low Byte
packet[n] = (unsigned char) ((sum >> 8) & 0xFF); // High Byte
}
protocolType getProtocolType(unsigned char* packet) {
if (packet[0] == DLE)
return JANDY;
else if (packet[0] == PP1)
return PENTAIR;
return P_UNKNOWN;
}
/*
unsigned char getProtocolType(unsigned char* packet) {
if (packet[0] == DLE)
return PCOL_JANDY;
else if (packet[0] == PP1)
return PCOL_PENTAIR;
return PCOL_UNKNOWN;
}
*/
#ifndef PLAYBACK_MODE
/*
Open and Initialize the serial communications port to the Aqualink RS8 device.
Arg is tty or port designation string
returns the file descriptor
*/
//#define TXDEN_DUMMY_RS485_MODE
#ifdef TXDEN_DUMMY_RS485_MODE
#include <linux/serial.h>
/* RS485 ioctls: */
#define TIOCGRS485 0x542E
#define TIOCSRS485 0x542F
int init_serial_port_Pi(const char* tty)
{
struct serial_rs485 rs485conf = {0};
//int fd = open(tty, O_RDWR | O_NOCTTY | O_NONBLOCK);
int fd = open(tty, O_RDWR);
if (fd < 0) {
LOG(RSSD_LOG,LOG_ERR, "Unable to open port: %s\n", tty);
return -1;
}
LOG(RSSD_LOG,LOG_DEBUG_SERIAL, "Openeded serial port %s\n",tty);
if (ioctl (fd, TIOCGRS485, &rs485conf) < 0) {
LOG(RSSD_LOG,LOG_ERR, "Error reading ioctl port (%d): %s\n", errno, strerror( errno ));
return -1;
}
LOG(RSSD_LOG,LOG_DEBUG, "Port currently RS485 mode is %s\n", (rs485conf.flags & SER_RS485_ENABLED) ? "set" : "NOT set");
/* Enable RS485 mode: */
rs485conf.flags |= SER_RS485_ENABLED;
/* Set logical level for RTS pin equal to 1 when sending: */
rs485conf.flags |= SER_RS485_RTS_ON_SEND;
/* or, set logical level for RTS pin equal to 0 when sending: */
//rs485conf.flags &= ~(SER_RS485_RTS_ON_SEND);
/* Set logical level for RTS pin equal to 1 after sending: */
rs485conf.flags |= SER_RS485_RTS_AFTER_SEND;
/* or, set logical level for RTS pin equal to 0 after sending: */
//rs485conf.flags &= ~(SER_RS485_RTS_AFTER_SEND);
/* Set this flag if you want to receive data even whilst sending data */
//rs485conf.flags |= SER_RS485_RX_DURING_TX;
if (ioctl (fd, TIOCSRS485, &rs485conf) < 0) {
LOG(RSSD_LOG,LOG_ERR, "Unable to set port to RS485 %s (%d): %s\n", tty, errno, strerror( errno ));
return -1;
}
return fd;
}
#endif // TXDEN_DUMMY_RS485_MODE
int _init_serial_port(const char* tty, bool blocking, bool readahead);
int init_serial_port(const char* tty)
{
#ifdef AQ_NO_THREAD_NETSERVICE
if (_aqconfig_.rs_poll_speed < 0) {
return init_blocking_serial_port(_aqconfig_.serial_port);
}
#else
return init_blocking_serial_port(_aqconfig_.serial_port);
#endif
}
int init_blocking_serial_port(const char* tty)
{
_blocking_fds = _init_serial_port(tty, true, false);
return _blocking_fds;
}
int set_port_low_latency(int fd, const char* tty)
{
struct serial_struct serial;
if (ioctl (fd, TIOCGSERIAL, &serial) < 0) {
LOG(RSSD_LOG,LOG_WARNING, "Doesn't look like your USB2RS485 device (%s) supports low latency, this might cause problems on a busy RS485 bus (%d): %s\n ", tty,errno, strerror( errno ));
//LOG(RSSD_LOG,LOG_WARNING, "Error reading low latency mode for port %s (%d): %s\n", tty, errno, strerror( errno ));
return -1;
}
LOG(RSSD_LOG,LOG_NOTICE, "Port %s low latency mode is %s\n", tty, (serial.flags & ASYNC_LOW_LATENCY) ? "set" : "NOT set, resetting to low latency!");
serial.flags |= ASYNC_LOW_LATENCY;
if (ioctl (fd, TIOCSSERIAL, &serial) < 0) {
LOG(RSSD_LOG,LOG_ERR, "Unable to set port %s to low latency mode (%d): %s\n", tty, errno, strerror( errno ));
return -1;
}
return 0;
}
#include <sys/file.h>
int lock_port(int fd, const char* tty)
{
if (ioctl (fd, TIOCEXCL) < 0) {
LOG(RSSD_LOG,LOG_ERR, "Can't put (%s) into exclusive mode (%d): %s\n", tty,errno, strerror( errno ));
return -1;
}
if (flock(fd, LOCK_EX | LOCK_NB) < 0) {
LOG(RSSD_LOG,LOG_ERR, "Can't lock (%s) (%d): %s\n", tty,errno, strerror( errno ));
return -1;
}
return 0;
}
int unlock_port(int fd)
{
if (flock(fd, LOCK_UN) < 0) {
LOG(RSSD_LOG,LOG_ERR, "Can't unlock serial port (%d): %s\n",errno, strerror( errno ));
return -1;
}
return 0;
}
// https://www.cmrr.umn.edu/~strupp/serial.html#2_5_2
// http://unixwiz.net/techtips/termios-vmin-vtime.html
// Unless AQ_RS_EXTRA_OPTS is defined, blocking will always be true
int _init_serial_port(const char* tty, bool blocking, bool readahead)
{
//B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400
const int BAUD = B9600;
const int PARITY = 0;
struct termios newtio;
_blocking_mode = blocking;
int fd = open(tty, O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
//int fd = open(tty, O_RDWR | O_NOCTTY | O_SYNC); // This is way to slow at reading
if (fd < 0) {
LOG(RSSD_LOG,LOG_ERR, "Unable to open port: %s, error %d\n", tty, errno);
return -1;
}
LOG(RSSD_LOG,LOG_DEBUG, "Openeded serial port %s\n",tty);
if (tcgetattr(fd, &newtio) != 0) {
LOG(RSSD_LOG,LOG_ERR, "Unable to get port attributes: %s, error %d\n", tty,errno);
return -1;
}
if ( lock_port(fd, tty) < 0) {
//LOG(RSSD_LOG,LOG_ERR, "Unable to lock port: %s, error %d\n", tty, errno);
return -1;
}
if (_aqconfig_.ftdi_low_latency)
set_port_low_latency(fd, tty);
memcpy(&_oldtio, &newtio, sizeof(struct termios));
cfsetospeed(&newtio, BAUD);
cfsetispeed(&newtio, BAUD);
newtio.c_cflag = (newtio.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
//newtio.c_iflag &= ~IGNBRK; // disable break processing
newtio.c_iflag = 0; // raw input
newtio.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
newtio.c_oflag = 0; // no remapping, no delays, raw output
if (_blocking_mode) {
fcntl(fd, F_SETFL, 0); //efficient blocking for the read
//newtio.c_cc[VMIN] = 1; // read blocks for 1 character or timeout below
//newtio.c_cc[VTIME] = 0; // 0.5 seconds read timeout
//newtio.c_cc[VTIME] = 255; // 25 seconds read timeout
//newtio.c_cc[VTIME] = 10; // (1 to 255) 1 = 0.1 sec, 255 = 25.5 sec
newtio.c_cc[VTIME] = SERIAL_BLOCKING_TIME;
newtio.c_cc[VMIN] = 0;
} else {
newtio.c_cc[VMIN]= 0; // read doesn't block
//newtio.c_cc[VTIME]= 1;
newtio.c_cc[VTIME]= (readahead?0:1);
}
/*
Raw output is selected by resetting the OPOST option in the c_oflag member:
newtio.c_oflag &= ~OPOST;
When the OPOST option is disabled, all other option bits in c_oflag are ignored.
*/
//newtio.c_oflag &= ~OPOST; // Raw output
newtio.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
newtio.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
// enable reading
newtio.c_cflag &= ~(PARENB | PARODD); // shut off parity
newtio.c_cflag |= PARITY;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CRTSCTS;
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd, TCSANOW, &newtio) != 0) {
LOG(RSSD_LOG,LOG_ERR, "Unable to set port attributes: %s, error %d\n", tty,errno);
return -1;
}
LOG(RSSD_LOG,LOG_INFO, "Port %s set I/O %s attributes\n",tty,_blocking_mode?"blocking":"non blocking");
return fd;
}
void close_blocking_serial_port()
{
if (_blocking_fds > 0) {
LOG(RSSD_LOG,LOG_INFO, "Forcing close of blocking serial port, ignore following read errors\n");
close_serial_port(_blocking_fds);
} else {
LOG(RSSD_LOG,LOG_ERR, "Didn't find valid blocking serial port file descripter\n");
}
}
/* close tty port */
void close_serial_port(int fd)
{
unlock_port(fd);
tcsetattr(fd, TCSANOW, &_oldtio);
close(fd);
LOG(RSSD_LOG,LOG_DEBUG_SERIAL, "Closed serial port\n");
}
bool serial_blockingmode()
{
return _blocking_mode;
}
// Send an ack packet to the Aqualink RS8 master device.
// fd: the file descriptor of the serial port connected to the device
// command: the command byte to send to the master device, NUL if no command
//
// NUL = '\x00'
// DLE = '\x10'
// STX = '\x02'
// ETX = '\x03'
//
// masterAddr = '\x00' # address of Aqualink controller
//
//msg = DLE+STX+dest+cmd+args
//msg = msg+self.checksum(msg)+DLE+ETX
// DLE+STX+DEST+CMD+ARGS+CHECKSUM+DLE+ETX
/*
void print_hex(char *pk, int length)
{
int i=0;
for (i=0;i<length;i++)
{
printf("0x%02hhx|",pk[i]);
}
printf("\n");
}
*/
void send_pentair_command(int fd, unsigned char *packet_buffer, int size)
{
unsigned char packet[AQ_MAXPKTLEN];
int i=0;
packet[0] = NUL;
packet[1] = PP1;
packet[2] = PP2;
packet[3] = PP3;
packet[4] = PP4;
//packet[i++] = 0x00; // from
//packet[i++] = // to
for (i=5; i-5 < size; i++) {
//printf("added 0x%02hhx at position %d\n",packet_buffer[i-4],i);
if (i==6) {
// Replace source
//packet[i] = 0x00;
// Don't replace source
packet[i] = packet_buffer[i-5];
} else if (i==9) {
// Replace length
//packet[i] = 0xFF;
packet[i] = (unsigned char)size-5;
} else {
packet[i] = packet_buffer[i-5];
}
//packet[i] = packet_buffer[i-4];
}
packet[++i] = NUL; // Checksum
packet[++i] = NUL; // Checksum
generate_pentair_checksum(&packet[1], i);
//packet[++i] = NUL;
//logPacket(packet, i);
send_packet(fd,packet,++i);
}
#ifndef SEND_CMD_WITH_TRAILING_NUL
void send_jandy_command(int fd, unsigned char *packet_buffer, int size)
{
unsigned char packet[AQ_MAXPKTLEN];
int i=0;
packet[0] = NUL;
packet[1] = DLE;
packet[2] = STX;
for (i=3; i-3 < size; i++) {
//printf("added 0x%02hhx at position %d\n",packet_buffer[i-2],i);
packet[i] = packet_buffer[i-3];
}
packet[++i] = DLE;
packet[++i] = ETX;
packet[i-2] = generate_checksum(packet, i+1);
send_packet(fd,packet,++i);
}
#else
void send_jandy_command(int fd, unsigned char *packet_buffer, int size)
{
unsigned char packet[AQ_MAXPKTLEN];
int i=0;
packet[0] = DLE;
packet[1] = STX;
for (i=2; i-2 < size; i++) {
packet[i] = packet_buffer[i-2];
}
packet[++i] = DLE;
packet[++i] = ETX;
packet[++i] = NUL;
packet[i-3] = generate_checksum(packet, i);
send_packet(fd,packet,++i);
}
#endif
/*
unsigned char tp[] = {PCOL_PENTAIR, 0x07, 0x0F, 0x10, 0x08, 0x0D, 0x55, 0x55, 0x5B, 0x2A, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00};
send_command(0, tp, 19);
Should produce
{0xFF, 0x00, 0xFF, 0xA5, 0x07, 0x0F, 0x10, 0x08, 0x0D, 0x55, 0x55, 0x5B, 0x2A, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00, 0x02, 0x9E};
<------- headder ----> <-- type to from type-> <len> <------------------------------ data ----------------------------------------> <checksum>
*/
//unsigned char packet_buffer[] = {PCOL_PENTAIR, 0x07, 0x0F, 0x10, 0x08, 0x0D, 0x55, 0x55, 0x5B, 0x2A, 0x2B, 0x00, 0x00, 0x00, 0x00, 0x64, 0x00, 0x00, 0x00};
//unsigned char packet_buffer[] = {PCOL_JANDY, 0x07, 0x0F, 0x00, 0x00};
void send_command(int fd, unsigned char *packet_buffer, int size)
{
//unsigned char packet[AQ_MAXPKTLEN];
//int i=0;
if (packet_buffer[0] == PCOL_JANDY) {
//LOG(RSSD_LOG,LOG_ERR, "Only Jandy protocol supported at present!\n");
send_jandy_command(fd, &packet_buffer[1], size-1);
return;
}
if (packet_buffer[0] == PCOL_PENTAIR) {
//LOG(RSSD_LOG,LOG_ERR, "Only Jandy protocol supported at present!\n");
send_pentair_command(fd, &packet_buffer[1], size-1);
return;
}
}
/* Clean out any NULL in serial line.
* Return 0x00 if nothing or only nuls read, or return byte if something else was read
* leave fullclean as option as may want to use this to cleanout end of message on get_packet function
*/
unsigned char cleanOutSerial(int fd, bool fullClean)
{
unsigned char byte = 0x00;
while ( (read(fd, &byte, 1) == 1) && (byte == 0x00 || fullClean) ) {
//printf("*** Peek Read 0x%02hhx ***\n",byte);
}
return byte;
}
/*
NEWPACKETADDRESSSPACE
is test to copy packet to unused address space before send, just incase tc_drain doesn't work correctly
and we change buffer after function call. (shouldn;t even happen / be needed buyt good for testing)
*/
#ifdef NEWPACKETADDRESSSPACE
void send_packet(int fd, unsigned char *packet_buffer, int length)
{
static unsigned char packet[AQ_MAXPKTLEN];
memcpy(packet, packet_buffer, length);
#else
void send_packet(int fd, unsigned char *packet, int length)
{
#endif
// No spec for aqualink protocol but looking at similar RS-485 protocol
// https://www.tascam.eu/en/docs/MX-8A_RS-485_protocol.pdf there is a
// min frame to frame time of 4 milliseconds and there seems to be issues
// with RPi 4 sending a response too quickly
struct timespec elapsed_time;
struct timespec now;
if (_aqconfig_.frame_delay > 0) {
struct timespec min_frame_wait_time;
struct timespec frame_wait_time;
struct timespec remainder_time;
min_frame_wait_time.tv_sec = 0;
min_frame_wait_time.tv_nsec = _aqconfig_.frame_delay * 1000000;
do {
clock_gettime(CLOCK_REALTIME, &now);
timespec_subtract(&elapsed_time, &now, &last_serial_read_time);
if (timespec_subtract(&frame_wait_time, &min_frame_wait_time, &elapsed_time)) {
break;
}
} while (nanosleep(&frame_wait_time, &remainder_time) != 0);
}
clock_gettime(CLOCK_REALTIME, &now);
if (_blocking_mode) {
//int nwrite = write(fd, packet, length);
//LOG(RSSD_LOG,LOG_DEBUG, "Serial write %d bytes of %d\n",nwrite,length);
int nwrite = write(fd, packet, length);
//if (nwrite < 0)
if (nwrite != length)
LOG(RSSD_LOG, LOG_ERR, "write to serial port failed\n");
} else {
int nwrite, i;
for (i = 0; i < length; i += nwrite) {
nwrite = write(fd, packet + i, length - i);
if (nwrite < 0)
LOG(RSSD_LOG, LOG_ERR, "write to serial port failed\n");
//else
// LOG(RSSD_LOG,LOG_DEBUG, "Serial write %d bytes of %d\n",nwrite,length-1);
}
} // _blockine_mode
/*
#ifdef AQ_DEBUG
// Need to take this out for release
if ( getLogLevel(RSSD_LOG) >= LOG_DEBUG) {
debuglogPacket(&packet[1], length-2);
}
#endif
*/
// MAYBE Change this back to debug serial
// LOG(RSSD_LOG,LOG_DEBUG_SERIAL, "Serial write %d bytes\n",length-2);
//LOG(RSSD_LOG,LOG_DEBUG, "Serial write %d bytes, type 0x%02hhx cmd 0x%02hhx\n",length-2,packet[5],packet[6]);
if (_aqconfig_.log_protocol_packets || getLogLevel(RSSD_LOG) >= LOG_DEBUG_SERIAL)
logPacketWrite(&packet[1], length-2);
/*
if (getLogLevel(PDA_LOG) == LOG_DEBUG) {
char buff[1024];
beautifyPacket(buff, &packet[1], length, false);
LOG(PDA_LOG,LOG_DEBUG, "%s", buff);
}
*/
/*
if ( getLogLevel(RSSD_LOG) >= LOG_DEBUG_SERIAL || ) {
// Packet is padded with 0x00, so discard for logging
LOG(RSSD_LOG,LOG_DEBUG_SERIAL, "Serial write %d bytes\n",length-2);
logPacket(&packet[1], length-2);
}*/ /*else if (getLogLevel(RSSD_LOG) >= LOG_DEBUG) {
static char buf[1000];
beautifyPacket(buf,&packet[1],length-2);
LOG(RSSD_LOG,LOG_DEBUG, "Serial write %s\n",buf);
}*/
tcdrain(fd); // Make sure buffer has been sent.
//if (_aqconfig_.frame_delay > 0) {
#ifndef SERIAL_LOGGER
if (_aqconfig_.frame_delay > 0) {
timespec_subtract(&elapsed_time, &now, &last_serial_read_time);
LOG(RSTM_LOG, LOG_DEBUG, "Time from recv to %s send is %.3f sec\n",
(_blocking_mode?"blocking":"non-blocking"),
roundf3(timespec2float(&elapsed_time)));
}
#endif
//}
}
#ifndef SEND_CMD_WITH_TRAILING_NUL
void _send_ack(int fd, unsigned char ack_type, unsigned char command)
{
//const int length = 11;
int length = 10;
// Default null ack with checksum generated, don't mess with it, just over right
unsigned char ackPacket[] = { NUL, DLE, STX, DEV_MASTER, CMD_ACK, NUL, NUL, 0x13, DLE, ETX, NUL };
//unsigned char ackPacket[] = { NUL, DLE, STX, DEV_MASTER, CMD_ACK, NUL, NUL, 0x13, DLE, ETX };
// To overcome Pentair VSP bug in Jandy control panel, we need to NOT send trailing NUL on
// a normal ACK, but sent the trailing NUL on in ack with command.
// Always send trailing NUL causes VSP to loose connection
// Never sending trailing NUL causes come commands to be missed.
// Update the packet and checksum if command argument is not NUL.
if(command != NUL || ack_type != NUL) {
//ackPacket[5] = 0x00 normal, 0x03 some pause, 0x01 some pause ending (0x01 = Screen Busy (also return from logn message))
ackPacket[5] = ack_type;
ackPacket[6] = command;
ackPacket[7] = generate_checksum(ackPacket, length);
if (command == DLE) { // We shuld probably also check the ack type as well, just for future proofing.
// 0x10(DLE) that's not part of the headder or footer needs to be escaped AFTER with NUL, so shif everyting uo one
ackPacket[8] = ackPacket[7]; // move the caculated checksum
ackPacket[7] = NUL; // escape the DLE
ackPacket[9] = DLE; // add new end sequence
ackPacket[10] = ETX; // add new end sequence
length = 11;
}
}
send_packet(fd, ackPacket, length);
}
#else
void _send_ack(int fd, unsigned char ack_type, unsigned char command)
{
//const int length = 11;
int length = 9;
// Default null ack with checksum generated, don't mess with it, just over right
unsigned char ackPacket[] = { DLE, STX, DEV_MASTER, CMD_ACK, NUL, NUL, 0x13, DLE, ETX, NUL };
if(command != NUL || ack_type != NUL) {
//ackPacket[5] = 0x00 normal, 0x03 some pause, 0x01 some pause ending (0x01 = Screen Busy (also return from logn message))
ackPacket[4] = ack_type;
ackPacket[5] = command;
ackPacket[6] = generate_checksum(ackPacket, length);
if (command == DLE) { // We shuld probably also check the ack type as well, just for future proofing.
// 0x10(DLE) that's not part of the headder or footer needs to be escaped AFTER with NUL, so shif everyting uo one
ackPacket[7] = ackPacket[7]; // move the caculated checksum
ackPacket[6] = NUL; // escape the DLE
ackPacket[8] = DLE; // add new end sequence
ackPacket[9] = ETX; // add new end sequence
length = 10;
}
}
send_packet(fd, ackPacket, length);
}
#endif // SEND_CMD_WITH_TRAILING_NUL
void send_ack(int fd, unsigned char command)
{
_send_ack(fd, ACK_NORMAL, command);
}
// ack_typ should only be ACK_PDA, ACK_NORMAL, ACK_SCREEN_BUSY, ACK_SCREEN_BUSY_DISPLAY
void send_extended_ack(int fd, unsigned char ack_type, unsigned char command)
{
_send_ack(fd, ack_type, command);
}
/*
int _get_packet(int fd, unsigned char* packet, bool rawlog);
int get_packet(int fd, unsigned char* packet)
{
return _get_packet(fd, packet, false);
}
int get_packet_lograw(int fd, unsigned char* packet)
{
return _get_packet(fd, packet, true);
}
int _get_packet(int fd, unsigned char* packet, bool rawlog)
*/
int get_packet(int fd, unsigned char* packet)
{
unsigned char byte = 0x00;
int bytesRead;
int index = 0;
bool endOfPacket = false;
//bool packetStarted = FALSE;
bool lastByteDLE = false;
int retry = 0;
bool jandyPacketStarted = false;
bool pentairPacketStarted = false;
//bool lastByteDLE = false;
int PentairPreCnt = 0;
int PentairDataCnt = -1;
struct timespec packet_elapsed;
struct timespec packet_end_time;
memset(packet, 0, AQ_MAXPKTLEN);
// Read packet in byte order below
// DLE STX ........ ETX DLE
// sometimes we get ETX DLE and no start, so for now just ignoring that. Seem to be more applicable when busy RS485 traffic
//#ifndef OLD_SERIAL_INIT .. Need to re-do ERROR like EAGAIN with new init
while (!endOfPacket) {
//printf("READ SERIAL\n");
bytesRead = read(fd, &byte, 1);
//printf("Read %d 0x%02hhx err=%d fd=%d\n",bytesRead,byte,errno,fd);
//if (bytesRead < 0 && errno == EAGAIN && packetStarted == FALSE && lastByteDLE == FALSE) {
//if (bytesRead < 0 && (errno == EAGAIN || errno == 0) &&
if (bytesRead <= 0 && (errno == EAGAIN || errno == 0 || errno == ENOTTY) ) { // We also get ENOTTY on some non FTDI adapters
if (_blocking_mode) {
// Something is wrong wrong
return AQSERR_TIMEOUT;
} else if (jandyPacketStarted == false && pentairPacketStarted == false && lastByteDLE == false) {
// We just have nothing else to read
return 0;
} else if (++retry > 120 ) {
LOG(RSSD_LOG,LOG_WARNING, "Serial read timeout\n");
//log_packet(LOG_WARNING, "Bad receive packet ", packet, index);
if (index > 0) { logPacketError(packet, index); }
return AQSERR_TIMEOUT;
}
delay(1);
} else if (bytesRead == 1) {
retry = 0;
if (_aqconfig_.log_raw_bytes)
logPacketByte(&byte);
if (lastByteDLE == true && byte == NUL)
{
// Check for DLE | NULL (that's escape DLE so delete the NULL)
//printf("IGNORE THIS PACKET\n");
lastByteDLE = false;
}
else if (lastByteDLE == true)
{
if (index == 0)
index++;
packet[index] = byte;
index++;
if (byte == STX && jandyPacketStarted == false)
{
jandyPacketStarted = true;
pentairPacketStarted = false;
}
else if (byte == ETX && jandyPacketStarted == true)