-
Notifications
You must be signed in to change notification settings - Fork 2
/
jbus_common.c
959 lines (818 loc) · 26 KB
/
jbus_common.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
#include "jbus_common.h"
#include "jbus485.h"
#include "ch.h"
#include "hal.h"
#include "printf.h"
#include "globalVar.h"
#include "stdutil.h"
#include "servo_pwm.h"
#include "rtcAccess.h"
#include "lcdDisplay.h"
#include "analogicIO.h"
#include "calibration.h"
#include "modbus/port/port.h"
#include "modbus/include/mb.h"
#include "modbus/include/mbport.h"
#include "modbus/include/mbutils.h"
#include "jbus485.h"
#include <string.h>
/* ----------------------- Static Function Declaration ------------------------------------------*/
static bool_t isItValidAdress (uint32_t addr, uint32_t base, uint32_t len);
static bool_t isInInterval_f (float val, float min, float max);
static bool_t isInInterval_i (int32_t val, int32_t min, int32_t max);
static void storeReg ( uint8_t** pucReg, uint16_t val);
static uint16_t loadReg ( uint8_t** pucReg);
static bool_t getOrSetRegHoldingValue (uint16_t iRegIndex, uint8_t **curPucReg, eMBRegisterMode eMode);
static bool_t getOrSetProtectedRegHoldingValue (uint16_t iRegIndex, uint8_t **curPucReg,
eMBRegisterMode eMode);
static bool_t getOrSetCoilValue (uint16_t iRegIndex, uint8_t *curPucReg, eMBRegisterMode eMode);
static bool_t getRegInputValue (uint16_t iRegIndex, uint8_t **curPucReg);
static bool_t getRegInputSyslogValue (uint16_t iRegIndex, uint8_t **curPucReg);
static bool_t getDiscreteValue (uint16_t iRegIndex, uint8_t *curPucReg);
static uint16_t normalisedToReg (float val);
static float regToNormalised (uint16_t val);
static uint16_t floatToRegFact1000 (float val);
static float regToFloatFact1000 (uint16_t val);
static void setLastFunction (ModbusFunction f);
static void setLastAction (eMBRegisterMode m);
static void setLastAddress (uint16_t a);
static void setLastNbReg (uint16_t n);
static void setLastValue (uint16_t n);
/* ----------------------- Defines ------------------------------------------*/
#define NORMALISED_TO_REG_SCALE (1000.0f) // valeur de multiplication des valeur flottante
// normalisée (0 <= 1) vers des registres unsigned short dialogue automate
/* ----------------------- Static variables ---------------------------------*/
const uint32_t regHoldingBase= 100;
const uint32_t regInputBase= 200;
const uint32_t regCoilBase= 300;
const uint32_t regDiscreteBase= 400;
const uint32_t regInputSyslogBase= 1000;
const uint32_t protectedRegHoldingBase= 2000;
typedef enum {LCD1=0, LCD2, JOUR, MOIS, ANNEE, JOUR_SEMAINE, HEURE, MINUTE, SECONDE,
CONSIGNE, VITESSE_MAX, REG_HOLDING_END} RegHolding;
typedef enum {POS_COURANTE=0, VITESSE_COURANTE, POSITION_PARK, POS_POTAR_SERVO_RAW,
POS_POTAR_SERVO_HOMOGENE, INTENSITE_SERVO, REG_INPUT_END} RegInput;
typedef enum {SETPARK=0, GOTO_PARK, REG_COIL_END} RegCoil;
typedef enum {MOVING=0, JAMED, ENABLED, BUTEE_MIN, BUTEE_MAX, REG_DISCRETE_END} RegDiscrete;
typedef enum {PASSWD1, PASSWD2, ADRJBUS, BAUD, NBBITSSTOP, PARITE,
ADDR_IP_1, ADDR_IP_2, NETMASK_1, NETMASK_2, PWMMIN, PWMMAX,
PROTECTED_REG_HOLDING_END} ProtectedRegHolding;
typedef enum {PROTECTED, HALF_PROTECTED, UNPROTECTED} ProtectedState;
static ProtectedState protectedState = PROTECTED;
static ModbusFunction modbusFunction = FUNC_NONE;
static eMBRegisterMode modbusAction = MB_REG_READ;
static uint16_t lastAddress = 0;
static uint16_t lastNbRegs = 0;
static uint16_t lastValue = 0;
static eMBErrorCode lastStatus;
static char l1[30], l2[30], l3[30];
/* ----------------------- Start implementation -----------------------------*/
static bool_t isItValidAdress (uint32_t addr, uint32_t base, uint32_t len)
{
return ((addr >= base) && (addr <= (base+len)));
}
static bool_t isInInterval_f (float val, float min, float max)
{
return ((val >= min) && (val <= max));
}
static bool_t isInInterval_i (int32_t val, int32_t min, int32_t max)
{
return ((val >= min) && (val <= max));
}
static void storeReg ( uint8_t** pucReg, uint16_t val)
{
// DebugTrace ("addr =0x%x v=%d", *pucReg, val);
setLastValue(val);
**pucReg = (uint8_t) (val >> 8);
(*pucReg)++;
**pucReg = (uint8_t) (val & 0xFF) ;
(*pucReg)++;
}
static uint16_t loadReg ( uint8_t** pucReg)
{
uint16_t val = **pucReg << 8;
(*pucReg)++;
val |= **pucReg;
(*pucReg)++;
setLastValue(val);
return val;
}
eMBErrorCode
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
{
eMBErrorCode eStatus = MB_ENOERR;
uint32_t iRegIndex;
uint8_t *curPucReg = pucRegBuffer;
const uint32_t protectedRegHoldingEnd = PROTECTED_REG_HOLDING_END +
((PROTECTED_REG_HOLDING_END - PWMMIN) * SERVO_COUNT);
const uint32_t regHoldingEnd = REG_HOLDING_END +
((REG_HOLDING_END - CONSIGNE) * SERVO_COUNT);
usAddress += getRegOffset(); // in case of offset range between plc and freemodbus
// DebugTrace ("HoldingCB ad[0x%x] usNRegs[%d] eMode[%d]", usAddress, usNRegs, eMode);
setLastFunction (FUNC_HOLDING);
setLastAction (eMode);
setLastAddress (usAddress);
setLastNbReg (usNRegs);
if (isItValidAdress (usAddress, regHoldingBase, regHoldingEnd) &&
isItValidAdress (usAddress+usNRegs, regHoldingBase, regHoldingEnd)) {
iRegIndex = (uint32_t) usAddress - regHoldingBase;
while (usNRegs > 0) {
if (getOrSetRegHoldingValue (iRegIndex, &curPucReg, eMode) == FALSE) {
eStatus = MB_EINVAL;
goto cleanAndExitHCB;
}
iRegIndex++;
usNRegs--;
}
} else if (isItValidAdress (usAddress, protectedRegHoldingBase, protectedRegHoldingEnd) &&
isItValidAdress (usAddress+usNRegs,protectedRegHoldingBase, protectedRegHoldingEnd)) {
iRegIndex = (uint32_t) usAddress - protectedRegHoldingBase;
while (usNRegs > 0) {
if (getOrSetProtectedRegHoldingValue (iRegIndex, &curPucReg, eMode) == FALSE) {
eStatus = MB_EINVAL;
goto cleanAndExitHCB;
}
iRegIndex++;
usNRegs--;
}
} else {
eStatus = MB_ENOREG;
DebugTrace ("Register bad @ %d/%d", usAddress, usNRegs);
syslog (LOG_ERROR, "Register bad @ %d/%d", usAddress, usNRegs);
}
cleanAndExitHCB:
return (lastStatus = eStatus);
}
eMBErrorCode
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
eMBErrorCode eStatus = MB_ENOERR;
uint32_t iRegIndex;
uint8_t *curPucReg = pucRegBuffer;
usAddress += getRegOffset(); // in case of offset range between plc and freemodbus
//DebugTrace ("InputCB ad[0x%x] usNRegs[%d]", usAddress, usNRegs);
setLastFunction (FUNC_INPUT);
setLastAddress (usAddress);
setLastNbReg (usNRegs);
if (isItValidAdress (usAddress, regInputBase, (REG_INPUT_END*SERVO_COUNT)+1) &&
isItValidAdress (usAddress+usNRegs, regInputBase, (REG_INPUT_END*SERVO_COUNT)+1)) {
iRegIndex = (uint32_t) usAddress - regInputBase;
while (usNRegs > 0) {
if (getRegInputValue (iRegIndex, &curPucReg) == FALSE) {
eStatus = MB_EINVAL;
goto cleanAndExitICB;
}
iRegIndex++;
usNRegs--;
}
} else if (isItValidAdress (usAddress, regInputSyslogBase,
(SYSLOG_BUFFLEN*SYSLOG_LINELEN)-1) &&
isItValidAdress (usAddress+usNRegs, regInputSyslogBase,
(SYSLOG_BUFFLEN*SYSLOG_LINELEN)-1)) {
iRegIndex = (uint32_t) usAddress - regInputSyslogBase;
while (usNRegs > 0) {
if (getRegInputSyslogValue (iRegIndex, &curPucReg) == FALSE) {
eStatus = MB_EINVAL;
goto cleanAndExitICB;
}
iRegIndex++;
usNRegs--;
}
} else {
eStatus = MB_ENOREG;
syslog (LOG_ERROR, "Input bad @ %d/%d", usAddress, usNRegs);
}
cleanAndExitICB:
return (lastStatus = eStatus);
}
eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
{
eMBErrorCode eStatus = MB_ENOERR;
uint32_t iRegIndex;
usAddress += getRegOffset(); // in case of offset range between plc and freemodbus
//DebugTrace ("RegCoilsCB ad[0x%x] usNCoils[%d]", usAddress, usNCoils);
setLastFunction (FUNC_COIL);
setLastAction (eMode);
setLastAddress (usAddress);
setLastNbReg (usNCoils);
if (isItValidAdress (usAddress, regCoilBase, (REG_COIL_END*SERVO_COUNT)+1) &&
isItValidAdress (usAddress+usNCoils, regCoilBase, (REG_COIL_END*SERVO_COUNT)+1)) {
iRegIndex = (uint32_t) usAddress - regCoilBase;
while (usNCoils > 0) {
if (getOrSetCoilValue (iRegIndex, pucRegBuffer, eMode) == FALSE) {
eStatus = MB_EINVAL;
goto cleanAndExitICB;
}
iRegIndex++;
usNCoils--;
}
} else {
eStatus = MB_ENOREG;
syslog (LOG_ERROR, "Coil bad @ %d/%d", usAddress, usNCoils);
}
cleanAndExitICB:
return (lastStatus = eStatus);
}
eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
eMBErrorCode eStatus = MB_ENOERR;
uint32_t iRegIndex;
usAddress += getRegOffset(); // in case of offset range between plc and freemodbus
//DebugTrace ("RegDiscreteCB ad[0x%x] usNDiscrete[%d]", usAddress, usNDiscrete);
setLastFunction (FUNC_DISCRETE);
setLastAddress (usAddress);
setLastNbReg (usNDiscrete);
if (isItValidAdress (usAddress, regDiscreteBase, REG_DISCRETE_END*SERVO_COUNT) &&
isItValidAdress (usAddress+usNDiscrete, regDiscreteBase, REG_DISCRETE_END*SERVO_COUNT)) {
iRegIndex = (uint32_t) usAddress - regDiscreteBase;
while (usNDiscrete > 0) {
if (getDiscreteValue (iRegIndex, pucRegBuffer) == FALSE) {
eStatus = MB_EINVAL;
goto cleanAndExitDCB;
}
iRegIndex++;
usNDiscrete--;
}
} else {
eStatus = MB_ENOREG;
syslog (LOG_ERROR, "Discrete bad @ %d/%d", usAddress, usNDiscrete);
}
cleanAndExitDCB:
return (lastStatus = eStatus);
}
static uint16_t normalisedToReg (float val)
{
return (( uint16_t) (val * NORMALISED_TO_REG_SCALE));
}
static float regToNormalised (uint16_t val)
{
return ((float) val / NORMALISED_TO_REG_SCALE);
}
static uint16_t floatToRegFact1000 (float val)
{
return (( uint16_t) (val * 1000.0f));
}
static float regToFloatFact1000 (uint16_t val)
{
return ((float) val / 1000.0f);
}
// JOUR, MOIS, ANNEE, JOUR_SEMAINE, HEURE, MINUTE, SECONDE, CONSIGNE (x 5), VITESSE_MAX (x 5)
bool_t getOrSetRegHoldingValue (uint16_t iRegIndex, uint8_t **curPucReg,
eMBRegisterMode eMode)
{
/* RegHolding type = iRegIndex % REG_HOLDING_END; */
/* uint32_t servoNum = iRegIndex / REG_HOLDING_END; */
/* float loadVal; */
RegHolding type = iRegIndex ;
uint32_t servoNum = -1;
float loadVal;
uint32_t rtcVal;
// first register are uniq, but after (and including) CONSIGNE, it's SERVO_NUM tupples of CONSIGNE,VITESSE_MAX
if (iRegIndex >= CONSIGNE) {
type = CONSIGNE + ((iRegIndex-CONSIGNE) % (REG_HOLDING_END-CONSIGNE));
servoNum = (iRegIndex-CONSIGNE) / (REG_HOLDING_END-CONSIGNE);
if (servoNum >= SERVO_COUNT) {
syslog (LOG_WARN, "servo>5 : %d", servoNum);
DebugTrace ("getOrSetRegHoldingValue servo>5 : %d", servoNum);
return FALSE;
}
}
//DebugTrace ("iRegIndex=%d type=%d servoNum=%d", iRegIndex, type, servoNum);
switch (type) {
case LCD1:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getLcd1Register());
} else {
setLcd1Register (loadReg (curPucReg));
}
break;
case LCD2:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getLcd2Register());
} else {
setLcd2Register (loadReg (curPucReg));
}
break;
case JOUR:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getMonthDay());
} else {
rtcVal = loadReg (curPucReg);
if (isInInterval_i (rtcVal, 0, 31)) {
setMonthDay (rtcVal);
} else {
DebugTrace ("JOUR, val %d is not in range 0-31", rtcVal);
syslog (LOG_WARN, "jour[0-31] : %d", rtcVal);
return FALSE;
}
}
break;
case MOIS:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getMonth());
} else {
rtcVal = loadReg (curPucReg);
if (isInInterval_i (rtcVal, 1, 12)) {
setMonth (rtcVal);
} else {
DebugTrace ("MOIS, val %d is not in range 1-12", rtcVal);
syslog (LOG_WARN, "mois[1-12] : %d", rtcVal);
return FALSE;
}
}
break;
case ANNEE:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getYear()+1900);
} else {
rtcVal = loadReg (curPucReg);
if (isInInterval_i (rtcVal, 2000, 2100)) {
setYear (rtcVal-1900);
} else {
DebugTrace ("ANNEE, val %d is not in range 2000-2100", rtcVal);
syslog (LOG_WARN, "ANNEE[2000-2100] : %d", rtcVal);
return FALSE;
}
}
break;
case JOUR_SEMAINE:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getWeekDay());
} else {
rtcVal = loadReg (curPucReg);
if (isInInterval_i (rtcVal, 1, 7)) {
setWeekDay (rtcVal);
} else {
DebugTrace ("JOUR_SEMAINE, val %d is not in range 1-7", rtcVal);
syslog (LOG_WARN, "jour semaine [1-7] : %d", rtcVal);
return FALSE;
}
}
break;
case HEURE:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getHour());
} else {
rtcVal = loadReg (curPucReg);
if (isInInterval_i (rtcVal, 0, 23)) {
setHour (rtcVal);
} else {
DebugTrace ("HEURE, val %d is not in range 0-23", rtcVal);
syslog (LOG_WARN, "heure[0-23] : %d", rtcVal);
return FALSE;
}
}
break;
case MINUTE:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getMinute());
} else {
rtcVal = loadReg (curPucReg);
if (isInInterval_i (rtcVal, 0, 59)) {
setMinute (rtcVal);
} else {
DebugTrace ("MINUTE, val %d is not in range 0-59", rtcVal);
syslog (LOG_WARN, "minute [0-59] : %d", rtcVal);
return FALSE;
}
}
break;
case SECONDE:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getSecond());
} else {
rtcVal = loadReg (curPucReg);
if (isInInterval_i (rtcVal, 0, 59)) {
setSecond (rtcVal);
} else {
DebugTrace ("SECONDE, val %d is not in range 0-59", rtcVal);
syslog (LOG_WARN, "seconde [0-59] : %d", rtcVal);
return FALSE;
}
}
break;
case CONSIGNE:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, normalisedToReg (servoGetPos (servoNum)));
} else {
loadVal = regToNormalised (loadReg (curPucReg));
if (isInInterval_f (loadVal, 0.0f, 1.0f)) {
servoSetPos (servoNum, loadVal);
} else {
DebugTrace ("CONSIGNE, val %f is not in range 0-1", loadVal);
syslog (LOG_WARN, "position [0-1000] : %d", normalisedToReg(loadVal));
return FALSE;
}
}
break;
case VITESSE_MAX:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, normalisedToReg (servoGetTopSpeed (servoNum)));
} else {
loadVal = regToNormalised (loadReg (curPucReg));
if (isInInterval_f (loadVal, 0.0f, 60.0f)) {
servoSetTopSpeed (servoNum, loadVal);
} else {
DebugTrace ("VITESSE_MAX, val %f is not in range 0-1000", loadVal);
syslog (LOG_WARN, "vitesse max [0-60000] : %d", normalisedToReg(loadVal));
return FALSE;
}
}
break;
case REG_HOLDING_END:
break;
}
return TRUE;
}
static bool_t getOrSetProtectedRegHoldingValue (uint16_t iRegIndex, uint8_t **curPucReg,
eMBRegisterMode eMode)
{
ProtectedRegHolding type = iRegIndex;
int32_t servoNum=-1;
static int32_t ipAddress=0, netmask=0;
if (iRegIndex >= PWMMIN) {
type = PWMMIN + ((iRegIndex-PWMMIN) % (PROTECTED_REG_HOLDING_END-PWMMIN));
servoNum = (iRegIndex-PWMMIN) / (PROTECTED_REG_HOLDING_END-PWMMIN);
}
if (servoNum >= SERVO_COUNT) {
syslog (LOG_WARN, "servo>5 : %d", servoNum);
return FALSE;
}
switch (type) {
case PASSWD1:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, 0xBADA); // on ne doit pas lire le mot de passe
} else {
if (loadReg (curPucReg) == 0xB16B) {
protectedState = HALF_PROTECTED;
} else {
DebugTrace ("Bad Passwd1");
syslog (LOG_WARN, "Bad Passwd1", servoNum);
return FALSE;
}
}
break;
case PASSWD2:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, 0xCCE5); // on ne doit pas lire le mot de passe
} else {
if (loadReg (curPucReg) == 0x00B5) {
protectedState = protectedState == HALF_PROTECTED ? UNPROTECTED : PROTECTED;
} else {
DebugTrace ("Bad Passwd2");
syslog (LOG_WARN, "Bad Passwd2", servoNum);
return FALSE;
}
}
break;
case ADRJBUS:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getMbAddressFor485());
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "Addr jbus: Protected Mode", servoNum);
return FALSE;
} else {
setMbAddressFor485 ((uint8_t) loadReg (curPucReg));
}
}
break;
case BAUD:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getBaudRate());
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "setBaud: Protected Mode", servoNum);
return FALSE;
} else {
setBaudRate (loadReg (curPucReg));
}
}
break;
case NBBITSSTOP:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getNbBitsStop());
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "nb bit stop: Protected Mode", servoNum);
return FALSE;
} else {
setNbBitsStop ((uint8_t) loadReg (curPucReg));
}
}
break;
case PARITE:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, getParity());
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "parite: Protected Mode", servoNum);
return FALSE;
} else {
setParity (loadReg (curPucReg));
}
}
break;
case ADDR_IP_1:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, 0xffff); // recuperer l'adresse ip
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "addr ip/1: Protected Mode", servoNum);
return FALSE;
} else {
ipAddress = loadReg (curPucReg) << 16;
}
}
break;
case ADDR_IP_2:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, 0xffff); // recuperer l'adresse ip
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "addr ip/2: Protected Mode", servoNum);
return FALSE;
} else {
ipAddress |= loadReg (curPucReg);
}
}
break;
case NETMASK_1:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, 0xffff); // recuperer l'adresse ip
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "netmask1: Protected Mode", servoNum);
return FALSE;
} else {
netmask = loadReg (curPucReg) << 16;
}
}
break;
case NETMASK_2:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, 0xff00); // recuperer l'adresse ip
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "netmask2: Protected Mode", servoNum);
return FALSE;
} else {
netmask |= loadReg (curPucReg);
}
}
break;
case PWMMIN:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, servoGetPwmMin (servoNum)); // recuperer l'adresse ip
} else {
if (protectedState != UNPROTECTED) {
DebugTrace ("Protected Mode");
syslog (LOG_WARN, "pwmmin: Protected Mode", servoNum);
return FALSE;
} else {
servoSetPwmMin (servoNum, loadReg (curPucReg));
}
}
break;
case PWMMAX:
if (eMode == MB_REG_READ) {
storeReg (curPucReg, servoGetPwmMax (servoNum)); // recuperer l'adresse ip
} else {
if (protectedState != UNPROTECTED) {
syslog (LOG_WARN, "Protected Mode", servoNum);
DebugTrace ("pwmmax: Protected Mode");
return FALSE;
} else {
servoSetPwmMax (servoNum, loadReg (curPucReg));
}
}
break;
case PROTECTED_REG_HOLDING_END:
break;
}
return TRUE;
}
static bool_t getRegInputValue (uint16_t iRegIndex, uint8_t **curPucReg)
{
const RegInput type = iRegIndex % REG_INPUT_END;
const uint32_t servoNum = iRegIndex / REG_INPUT_END;
float val;
if (iRegIndex > (SERVO_COUNT*REG_INPUT_END)) {
syslog (LOG_WARN, "servo>5 : %d", servoNum);
return FALSE;
}
if (iRegIndex == (SERVO_COUNT*REG_INPUT_END)) {
// last value of reg input is (nominal 5v) power supply voltage
storeReg (curPucReg, floatToRegFact1000 (analogGet5VoltPowerVoltage()));
return TRUE;
}
/* if (servoNum >= SERVO_COUNT) { */
/* syslog (LOG_WARN, "servo>5 : %d", servoNum); */
/* return FALSE; */
/* } */
switch (type) {
case POS_COURANTE:
storeReg (curPucReg, normalisedToReg (servoGetCurrentPos (servoNum)));
break;
case VITESSE_COURANTE:
val = servoGetCurrentSpeed (servoNum);
if (val == -1.0f) {
storeReg (curPucReg, (1<<15)-1);
} else {
storeReg (curPucReg, floatToRegFact1000 (val));
}
break;
case POSITION_PARK:
storeReg (curPucReg, normalisedToReg (servoGetParkPos (servoNum)));
break;
case POS_POTAR_SERVO_RAW:
storeReg (curPucReg, normalisedToReg (analogGetRawPos (servoNum)));
break;
case POS_POTAR_SERVO_HOMOGENE:
storeReg (curPucReg, normalisedToReg (getReadPosNormalised (servoNum)));
break;
// in milliamp
case INTENSITE_SERVO:
storeReg (curPucReg, floatToRegFact1000(analogGetCurrentIntensity (servoNum)));
break;
case REG_INPUT_END:
break;
}
return TRUE;
}
static bool_t getRegInputSyslogValue (uint16_t iRegIndex, uint8_t **curPucReg)
{
if (iRegIndex >= ((SYSLOG_BUFFLEN*SYSLOG_LINELEN)/2)) {
syslog (LOG_WARN, "syslog ERR : %d>%d", iRegIndex,
((SYSLOG_BUFFLEN*SYSLOG_LINELEN)/2)-1);
return FALSE;
}
storeReg (curPucReg, getSyslogDataAsPlcRegister (iRegIndex));
return TRUE;
}
static bool_t getOrSetCoilValue (uint16_t iRegIndex, uint8_t *curPucReg, eMBRegisterMode eMode)
{
const RegCoil type = iRegIndex % REG_COIL_END;
const uint32_t servoNum = iRegIndex / REG_COIL_END;
if (servoNum < SERVO_COUNT) {
switch (type) {
case SETPARK:
if (eMode == MB_REG_READ) {
xMBUtilSetBits (curPucReg, iRegIndex, 1, 0);
setLastValue (0);
} else {
setLastValue (xMBUtilGetBits (curPucReg, iRegIndex, 1));
if (xMBUtilGetBits (curPucReg, iRegIndex, 1)) {
for (uint32_t idx=0; idx<SERVO_COUNT; idx++)
servoSetCurrentPosAsPark(idx);
}
}
break;
case GOTO_PARK:
if (eMode == MB_REG_READ) {
xMBUtilSetBits (curPucReg, iRegIndex, 1, 0);
setLastValue (0);
} else {
setLastValue (xMBUtilGetBits (curPucReg, iRegIndex, 1));
if (xMBUtilGetBits (curPucReg, iRegIndex, 1)) {
for (uint32_t idx=0; idx<SERVO_COUNT; idx++)
servoPark(idx);
}
}
break;
case REG_COIL_END:
break;
}
} else {
const uint16_t afterServoRegIndex = iRegIndex - (REG_COIL_END*SERVO_COUNT);
if (afterServoRegIndex == 0) {
// trigger syslog print copy when read or write this coil
if (eMode == MB_REG_READ) {
xMBUtilSetBits (curPucReg, iRegIndex, 1, 0);
setLastValue (0);
} else {
setLastValue (xMBUtilGetBits (curPucReg, iRegIndex, 1));
}
printCopySyslogDataForPlc ();
}
}
return TRUE;
}
static bool_t getDiscreteValue (uint16_t iRegIndex, uint8_t *curPucReg)
{
const RegDiscrete type = iRegIndex % REG_DISCRETE_END;
const uint32_t servoNum = iRegIndex / REG_DISCRETE_END;
if (servoNum >= SERVO_COUNT) {
syslog (LOG_WARN, "servo>5 : %d", servoNum);
return FALSE;
}
switch (type) {
case JAMED:
xMBUtilSetBits (curPucReg, iRegIndex, 1, servoGetState(servoNum) == ADS_Jam);
setLastValue (servoGetState(servoNum) == ADS_Jam);
break;
case ENABLED:
xMBUtilSetBits (curPucReg, iRegIndex, 1, isServoEngaged(servoNum));
setLastValue (isServoEngaged(servoNum));
break;
case MOVING:
xMBUtilSetBits (curPucReg, iRegIndex, 1, isServoMoving(servoNum));
setLastValue (isServoMoving(servoNum));
break;
case BUTEE_MIN:
xMBUtilSetBits (curPucReg, iRegIndex, 1, isAtLowStopPos (servoNum));
setLastValue (isAtLowStopPos (servoNum));
break;
case BUTEE_MAX:
xMBUtilSetBits (curPucReg, iRegIndex, 1, isAtHighStopPos (servoNum));
setLastValue (isAtHighStopPos (servoNum));
break;
case REG_DISCRETE_END:
break;
}
return TRUE;
}
const char* getLastFunctionL1 (void)
{
switch (modbusFunction) {
case FUNC_NONE:
chsnprintf (l1, sizeof(l1), "** En attente du **");
chsnprintf (l2, sizeof(l2), "** Premier Message **");
chsnprintf (l3, sizeof(l3), "---------------------");
break;
case FUNC_HOLDING:
chsnprintf (l1, sizeof(l1), "%d Reg %s @ %d",
lastNbRegs,
modbusAction == MB_REG_READ ? "READ" : "WRITE",
lastAddress);
chsnprintf (l2, sizeof(l2), "%s",
lastStatus == MB_ENOERR ? "OK" :
lastStatus == MB_ENOREG ? "adresse incorrecte" : "valeur incorrecte");
chsnprintf (l3, sizeof(l3), "last reg val: %d", lastValue);
break;
case FUNC_INPUT:
chsnprintf (l1, sizeof(l1), "%d Input @ %d", lastNbRegs, lastAddress);
chsnprintf (l2, sizeof(l2), "%s",
lastStatus == MB_ENOERR ? "OK" :
lastStatus == MB_ENOREG ? "adresse incorrecte" : "valeur incorrecte");
chsnprintf (l3, sizeof(l3), "last input val: %d", lastValue);
break;
case FUNC_COIL:
chsnprintf (l1, sizeof(l1), "%d Coil %s @ %d",
lastNbRegs,
modbusAction == MB_REG_READ ? "READ" : "WRITE",
lastAddress);
chsnprintf (l2, sizeof(l2), "%s",
lastStatus == MB_ENOERR ? "OK" :
lastStatus == MB_ENOREG ? "adresse incorrecte" : "valeur incorrecte");
chsnprintf (l3, sizeof(l3), "last coil val: %s", lastValue ? "VRAI" : "FAUX");
break;
case FUNC_DISCRETE:
chsnprintf (l1, sizeof(l1), "%d Discrete @ %d", lastNbRegs, lastAddress);
chsnprintf (l2, sizeof(l2), "%s",
lastStatus == MB_ENOERR ? "OK" :
lastStatus == MB_ENOREG ? "adresse incorrecte" : "valeur incorrecte");
chsnprintf (l3, sizeof(l3), "last discrete val: %s", lastValue ? "VRAI" : "FAUX");
break;
}
l1[sizeof(l1)-1] = l2[sizeof(l2)-1] =0;
return l1;
}
const char* getLastFunctionL2 (void)
{
return l2;
}
const char* getLastFunctionL3 (void)
{
return l3;
}
static void setLastFunction (ModbusFunction f)
{
modbusFunction = f;
}
static void setLastAction (eMBRegisterMode m)
{
modbusAction = m;
}
static void setLastAddress (uint16_t a)
{
lastAddress = a;
}
static void setLastNbReg (uint16_t n)
{
lastNbRegs = n;
}
static void setLastValue (uint16_t n)
{
lastValue = n;
}