forked from RFD-FHEM/RFFHEM
/
00_SIGNALduino.pm
7745 lines (6885 loc) · 269 KB
/
00_SIGNALduino.pm
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
##############################################
# $Id: 00_SIGNALduino.pm 3417 2023-11-07 19:00:00Z v3.4.17-Ralf9 $
#
# v3.4.17
# The module is inspired by the FHEMduino project and modified in serval ways for processing the incomming messages
# see http://www.fhemwiki.de/wiki/SIGNALDuino
# It was modified also to provide support for raw message handling which can be send from the SIGNALduino
# The purpos is to use it as addition to the SIGNALduino which runs on an arduno nano or arduino uno.
# It routes Messages serval Modules which are already integrated in FHEM. But there are also modules which comes with it.
# N. Butzek, S. Butzek, 2014-2015
# S.Butzek,Ralf9 2016-2019
# Ralf9 2020-2023
package main;
my $missingModulSIGNALduino="";
use strict;
use warnings;
use DevIo;
no warnings 'portable';
require "lib/signalduino_protocols.pm";
use Data::Dumper qw(Dumper);
eval "use JSON;1" or $missingModulSIGNALduino .= "JSON ";
use Scalar::Util qw(looks_like_number);
#use POSIX qw( floor); # can be removed
#use Math::Round qw();
use constant {
SDUINO_VERSION => "v3.4.17-ralf_07.11.23",
SDUINO_INIT_WAIT_XQ => 2.5, # wait disable device
SDUINO_INIT_WAIT => 3,
SDUINO_INIT_MAXRETRY => 3,
SDUINO_CMD_TIMEOUT => 10,
SDUINO_KEEPALIVE_TIMEOUT => 60,
SDUINO_KEEPALIVE_MAXRETRY => 3,
SDUINO_WRITEQUEUE_NEXT => 0.3,
SDUINO_WRITEQUEUE_TIMEOUT => 2,
SDUINO_recAwNotMatch_Max => 10,
SDUINO_parseRespMaxReading => 130,
SDUINO_DISPATCH_VERBOSE => 5, # default 5
SDUINO_MC_DISPATCH_VERBOSE => 3, # wenn kleiner 5, z.B. 3 dann wird vor dem dispatch mit loglevel 3 die ID und rmsg ausgegeben
SDUINO_MC_DISPATCH_LOG_ID => '12.1', # die o.g. Ausgabe erfolgt nur wenn der Wert mit der ID uebereinstimmt
SDUINO_PARSE_DEFAULT_LENGHT_MIN => 8,
};
my %ProtocolListSIGNALduino = %SD_Protocols::ProtocolListSIGNALduino;
my $VersionProtocolList = \%SD_Protocols::VersionProtocolList;
my %rfmode = %SD_Protocols::rfmode;
my %rfmodeTesting = %SD_Protocols::rfmodeTesting;
#sub SIGNALduino_Attr(@);
#sub SIGNALduino_HandleWriteQueue($);
#sub SIGNALduino_Parse($$$$@);
#sub SIGNALduino_Read($);
#sub SIGNALduino_Ready($);
#sub SIGNALduino_Write($$$);
#sub SIGNALduino_SimpleWrite(@);
#my $debug=0;
my %gets = ( # Name, Data to send to the SIGNALduino, Regexp for the answer
"version" => ["V", 'V\s.*SIGNAL(duino|ESP).*'],
"freeram" => ["R", '^[0-9]+'],
"raw" => ["", '.*'],
"uptime" => ["t", '^[0-9]+' ],
"cmds" => ["?", '(.*Use one of[\? 0-9A-Za-z]+[\r\n]*$)|(CSmcmbl=)' ],
"ping" => ["P",'^OK$'],
"config" => ["CG",'(MS.*MU.*MC.*)|(ccmode=)'],
"protocolIdToJson" => ['none','none'],
"ccconf" => ["C0DnF", 'C0Dn11.*'],
"ccreg" => ["C", '^C.* = .*'],
"ccpatable" => ["C3E", '^C3E = .*'],
"cmdBank" => ["b", '(b=\d.* ccmode=\d.*)|(switch)|(Bank)|(bank)|(radio)|(not valid)'],
"zAvailableFirmware" => ["none",'none'],
);
my %sets = (
"raw" => 'textFieldNL',
"flash" => 'textFieldNL',
"reset" => 'noArg',
"close" => 'noArg',
"enableMessagetype_3" => 'syncedMS,unsyncedMU,manchesterMC',
"enableMessagetype_4" => 'syncedMS,syncedMSEQ,unsyncedMU,manchesterMC',
"disableMessagetype_3" => 'syncedMS,unsyncedMU,manchesterMC',
"disableMessagetype_4" => 'syncedMS,syncedMSEQ,unsyncedMU,manchesterMC',
"LaCrossePairForSec" => 'textFieldNL',
"sendMsg" => 'textFieldNL',
"cc1101_freq" => 'textFieldNL',
"cc1101_bWidth" => '58,68,81,102,116,135,162,203,232,270,325,406,464,541,650,812',
"cc1101_rAmpl" => '24,27,30,33,36,38,40,42',
"cc1101_sens" => '4,8,12,16',
"cc1101_patable_433" => '-10_dBm,-5_dBm,0_dBm,5_dBm,7_dBm,10_dBm',
"cc1101_patable_868" => '-10_dBm,-5_dBm,0_dBm,5_dBm,7_dBm,10_dBm',
"cc1101_reg" => 'textFieldNL',
"cc1101_dataRate" => 'textFieldNL',
"cc1101_deviatn" => 'textFieldNL',
);
my %patable = (
"433" =>
{
"-10_dBm" => '34',
"-5_dBm" => '68',
"0_dBm" => '60',
"5_dBm" => '84',
"7_dBm" => 'C8',
"10_dBm" => 'C0',
},
"868" =>
{
"-10_dBm" => '27',
"-5_dBm" => '67',
"0_dBm" => '50',
"5_dBm" => '81',
"7_dBm" => 'CB',
"10_dBm" => 'C2',
},
);
my @ampllist = (24, 27, 30, 33, 36, 38, 40, 42); # rAmpl(dB)
my @modformat = ("2-FSK","GFSK","-","ASK/OOK","4-FSK","-","-","MSK"); # modulation format
my @SYNC_MODE = ("No preamble/sync","15/16 sync","16/16 sync","30/32 sync",
"No preamble/sync, carrier-sense above threshold", "15/16 + carrier-sense above threshold", "16/16 + carrier-sense above threshold", "30/32 + carrier-sense above threshold");
my %cc1101_register = ( # for get ccreg 99
"00" => ['IOCFG2 ', '0D', '29' ],
"01" => ['IOCFG1 ', '2E' ],
"02" => ['IOCFG0 ', '2D', '3F' ],
"03" => ['FIFOTHR ', '07' ],
"04" => ['SYNC1 ', 'D3' ],
"05" => ['SYNC0 ', '91' ],
"06" => ['PKTLEN ', '3D', '0F' ],
"07" => ['PKTCTRL1', '04' ],
"08" => ['PKTCTRL0', '32', '45' ],
"09" => ['ADDR ', '00' ],
"0A" => ['CHANNR ', '00' ],
"0B" => ['FSCTRL1 ', '06', '0F' ],
"0C" => ['FSCTRL0 ', '00' ],
"0D" => ['FREQ2 ', '10', '1E' ],
"0E" => ['FREQ1 ', 'B0', 'C4' ],
"0F" => ['FREQ0 ', '71', 'EC' ],
"10" => ['MDMCFG4 ', '57', '8C' ],
"11" => ['MDMCFG3 ', 'C4', '22' ],
"12" => ['MDMCFG2 ', '30', '02' ],
"13" => ['MDMCFG1 ', '23', '22' ],
"14" => ['MDMCFG0 ', 'B9', 'F8' ],
"15" => ['DEVIATN ', '00', '47' ],
"16" => ['MCSM2 ', '07', '07' ],
"17" => ['MCSM1 ', '00', '30' ],
"18" => ['MCSM0 ', '18', '04' ],
"19" => ['FOCCFG ', '14', '36' ],
"1A" => ['BSCFG ', '6C' ],
"1B" => ['AGCCTRL2', '07', '03' ],
"1C" => ['AGCCTRL1', '00', '40' ],
"1D" => ['AGCCTRL0', '90', '91' ],
"1E" => ['WOREVT1 ', '87' ],
"1F" => ['WOREVT0 ', '6B' ],
"20" => ['WORCTRL ', 'F8' ],
"21" => ['FREND1 ', '56' ],
"22" => ['FREND0 ', '11', '16' ],
"23" => ['FSCAL3 ', 'E9', 'A9' ],
"24" => ['FSCAL2 ', '2A', '0A' ],
"25" => ['FSCAL1 ', '00', '20' ],
"26" => ['FSCAL0 ', '1F', '0D' ],
"27" => ['RCCTRL1 ', '41' ],
"28" => ['RCCTRL0 ', '00' ],
"29" => ['FSTEST ' ],
"2A" => ['PTEST ' ],
"2B" => ['AGCTEST ' ],
"2C" => ['TEST2 ', '88' ],
"2D" => ['TEST1 ', '31' ],
"2E" => ['TEST0 ', '0B' ]
);
## Supported Clients per default
my $clientsSIGNALduino = ":CUL_TCM97001:"
."SD_WS:"
."SD_WS07:"
."SD_WS09:"
."Hideki:"
."LaCrosse:"
."OREGON:"
."CUL_EM:"
."CUL_WS:"
."CUL_TX:"
."SD_AS:"
."IT:"
." :" # Zeilenumbruch
."FS10:"
."FS20:"
."SOMFY:"
."FLAMINGO:"
."SD_WS_Maverick:"
."KOPP_FC:"
."PCA301:"
."SD_BELL:" ## bells
."SD_GT:"
."SD_RSL:"
."SD_UT:" ## universal - more devices with different protocol
."WMBUS:"
."HMS:"
." :" # Zeilenumbruch
."IFB:"
."CUL_FHTTK:"
."FHT:"
."RFXX10REC:"
."Revolt:"
."Dooya:"
."Fernotron:"
."SD_Keeloq:"
."SD_Rojaflex:"
."Siro:"
."LTECH:"
."CUL_MAX:"
."SD_Tool:"
."SIGNALduino_un:"
;
## default regex match List for dispatching message to logical modules, can be updated during runtime because it is referenced
my %matchListSIGNALduino = (
'01:IT' => '^i......', # Intertechno Format
'02:CUL_TCM97001' => '^s[A-Fa-f0-9]+', # Any hex string beginning with s
'03:SD_RSL' => '^P1#[A-Fa-f0-9]{8}',
'04:OREGON' => '^(3[8-9A-F]|[4-6][0-9A-F]|7[0-8]).*',
'05:CUL_TX' => '^TX..........', # Need TX to avoid FHTTK
'06:SD_AS' => '^P2#[A-Fa-f0-9]{7,8}', # Arduino based Sensors, should not be default
'07:Hideki' => '^P12#75[A-F0-9]+',
'09:CUL_FHTTK' => '^T[A-F0-9]{8}',
'10:SD_WS07' => '^P7#[A-Fa-f0-9]{6}[AFaf][A-Fa-f0-9]{2,3}',
'11:SD_WS09' => '^P9#F[A-Fa-f0-9]+',
'12:SD_WS' => '^W\d+x{0,1}#.*',
'13:RFXX10REC' => '^(20|29)[A-Fa-f0-9]+',
'14:Dooya' => '^P16#[A-Fa-f0-9]+',
'15:SOMFY' => '^Ys[0-9A-F]+',
'16:SD_WS_Maverick' => '^P47#[A-Fa-f0-9]+',
'17:SD_UT' => '^P(?:14|20|24|26|29|30|34|46|56|68|69|76|78|81|83|86|90|91|91.1|92|93|95|97|99|104|105|114|118|121|124|127|128|199)#.*', # universal - more devices with different protocols
'18:FLAMINGO' => '^P13\.?1?#[A-Fa-f0-9]+', # Flamingo Smoke
'19:CUL_WS' => '^K[A-Fa-f0-9]{5,}',
'20:Revolt' => '^r[A-Fa-f0-9]{22}',
'21:FS10' => '^P61#[A-F0-9]+',
'22:Siro' => '^P72#[A-Fa-f0-9]+',
'23:FHT' => '^81..(04|09|0d)..(0909a001|83098301|c409c401)..',
'24:FS20' => '^81..(04|0c)..0101a001',
'25:CUL_EM' => '^E0.................',
'26:Fernotron' => '^P82#.*',
'27:SD_BELL' => '^P(?:15|32|41|42|57|79|96|98|112)#.*',
'28:SD_Keeloq' => '^P(?:87|88)#.*',
'29:SD_GT' => '^P49#[A-Fa-f0-9]+',
'30:LaCrosse' => '^(\\S+\\s+9 |OK\\sWS\\s)',
'31:KOPP_FC' => '^kr..................',
'32:PCA301' => '^\\S+\\s+24',
'33:SD_Rojaflex' => '^P109#[A-Fa-f0-9]+',
'34:WMBUS' => '^b.*',
'35:HMS' => '^810e04......a001',
'36:IFB' => '^J............',
'37:LTECH' => '^P31#[A-Fa-f0-9]{26,}',
'38:CUL_MAX' => '^Z.*',
'90:SD_Tool' => '^pt([0-9]+(\.[0-9])?)(#.*)?',
'X:SIGNALduino_un' => '^[u]\d+#.*',
);
sub
SIGNALduino_Initialize
{
my ($hash) = @_;
my $dev = ",1";
#my $dev = "";
#if (index(SDUINO_VERSION, "dev") >= 0) {
# $dev = ",1";
#}
# Provider
$hash->{ReadFn} = \&SIGNALduino_Read;
$hash->{WriteFn} = \&SIGNALduino_Write;
$hash->{ReadyFn} = \&SIGNALduino_Ready;
# Normal devices
$hash->{DefFn} = \&SIGNALduino_Define;
$hash->{FingerprintFn} = \&SIGNALduino_FingerprintFn;
$hash->{UndefFn} = \&SIGNALduino_Undef;
$hash->{GetFn} = \&SIGNALduino_Get;
$hash->{SetFn} = \&SIGNALduino_Set;
$hash->{AttrFn} = \&SIGNALduino_Attr;
$hash->{AttrList} =
"Clients MatchList do_not_notify:1,0 dummy:1"
." hexFile"
." initCommands"
." flashCommand"
." hardware:ESP32_sduino_devkitV1,ESP8266cc1101,nano328,nano328_optiboot,nanoCC1101,nanoCC1101_optiboot,miniculCC1101,3v3prominiCC1101,promini,radinoCC1101,uno,culV3CC1101,"
."Maple_sduino_USB,Maple_sduino_serial,Maple_sduino_LAN,Maple_cul_USB,Maple_cul_serial,Maple_cul_LAN"
." updateChannelFW:stable,testing,Ralf9"
." debug:0$dev"
." longids"
." minsecs"
." whitelist_IDs"
." blacklist_IDs"
." WS09_WSModel:WH3080,WH1080,CTW600"
." WS09_CRCAUS:0,1,2"
." addvaltrigger"
." rawmsgEvent:1,0"
." cc1101_frequency"
." doubleMsgCheck_IDs"
." parseMUclockCheck:0,1,2" # wenn > 0 dann ist bei MU Nachrichten der test ob die clock in der Toleranz ist, aktiv
." rfmode_user"
." sendSlowRF_A_IDs"
." suppressDeviceRawmsg:1,0"
." development"
." noMsgVerbose:0,1,2,3,4,5"
." maxMuMsgRepeat"
." userProtocol"
." $readingFnAttributes";
$hash->{ShutdownFn} = "SIGNALduino_Shutdown";
$hash->{FW_detailFn} = "SIGNALduino_FW_Detail";
$hash->{msIdList} = ();
$hash->{muIdList} = ();
$hash->{mcIdList} = ();
$hash->{mnIdList} = ();
#ours %attr{};
}
#
# Predeclare Variables from other modules may be loaded later from fhem
#
our $FW_wname;
our $FW_ME;
#
# Predeclare Variables from other modules may be loaded later from fhem
#
our $FW_CSRF;
our $FW_detail;
sub
SIGNALduino_FingerprintFn
{
my ($name, $msg) = @_;
if (substr($msg,0,2) eq "OK") {
return;
}
# Store only the "relevant" part, as the Signalduino won't compute the checksum
#$msg = substr($msg, 8) if($msg =~ m/^81/ && length($msg) > 8);
$name = "" if (!IsDummy($name));
return ($name, $msg);
}
#####################################
sub
SIGNALduino_Define
{
my ($hash, $def) = @_;
my @a = split("[ \t][ \t]*", $def);
if(@a != 3) {
my $msg = "wrong syntax: define <name> SIGNALduino {none | devicename[\@baudrate] | devicename\@directio | hostname:port}";
Log3 undef, 2, $msg;
return $msg;
}
DevIo_CloseDev($hash);
my $name = $a[0];
if (!exists &round)
{
Log3 $name, 1, "$name: Signalduino can't be activated (sub round not found). Please update Fhem via update command";
return;
}
my $dev = $a[2];
#Debug "dev: $dev" if ($debug);
#my $hardware=AttrVal($name,"hardware","nano328");
#Debug "hardware: $hardware" if ($debug);
if($dev eq "none") {
Log3 $name, 1, "$name: device is none, commands will be echoed only";
$attr{$name}{dummy} = 1;
#return undef;
}
if ($dev ne "none" && $dev =~ m/[a-zA-Z]/ && $dev !~ m/\@/) { # bei einer IP wird kein \@57600 angehaengt
$dev .= "\@57600";
}
#$hash->{CMDS} = "";
$hash->{ClientsKeepOrder} = 1;
$hash->{Clients} = $clientsSIGNALduino;
$hash->{MatchList} = \%matchListSIGNALduino;
$hash->{DeviceName} = $dev;
my $rfmodelist = "";
#$rfmodelist .= 'test1,test2,';
foreach my $rf (sort keys %rfmode) {
$rfmodelist .= $rf . ",";
}
$rfmodelist .= 'custom';
$hash->{rfmodesets}{rfmode} = $rfmodelist;
$rfmodelist = "";
foreach my $rf (sort keys %rfmodeTesting) {
$rfmodelist .= $rf . ",";
}
$rfmodelist =~ s/,$//;
$hash->{rfmodesetsTesting}{rfmodeTesting} = $rfmodelist;
my $ret=undef;
InternalTimer(gettimeofday(), 'SIGNALduino_IdList',"sduino_IdList:$name",0); # verzoegern bis alle Attribute eingelesen sind
if($dev ne "none") {
$ret = DevIo_OpenDev($hash, 0, "SIGNALduino_DoInit", 'SIGNALduino_Connect');
} else {
$hash->{DevState} = 'initialized';
readingsSingleUpdate($hash, "state", "opened", 1);
}
$hash->{DMSG}="nothing";
$hash->{LASTDMSG} = "nothing";
$hash->{LASTDMSGID} = "nothing";
$hash->{TIME}=time();
$hash->{versionmodul} = SDUINO_VERSION;
if (defined($VersionProtocolList->{version})) {
$hash->{versionprotoL} = $VersionProtocolList->{version};
Log3 $name, 3, "$name: Protocolhashversion: " . $hash->{versionprotoL};
}
#Log3 $name, 3, "$name: Firmwareversion: ".$hash->{READINGS}{version}{VAL} if ($hash->{READINGS}{version}{VAL});
return $ret;
}
###############################
sub SIGNALduino_Connect
{
my ($hash, $err) = @_;
# damit wird die err-msg nur einmal ausgegeben
if (!defined($hash->{disConnFlag}) && $err) {
Log3($hash, 3, "$hash->{NAME}: ${err}");
$hash->{disConnFlag} = 1;
}
}
#####################################
sub
SIGNALduino_Undef
{
my ($hash, $arg) = @_;
my $name = $hash->{NAME};
foreach my $d (sort keys %defs) {
if(defined($defs{$d}) &&
defined($defs{$d}{IODev}) &&
$defs{$d}{IODev} == $hash)
{
my $lev = ($reread_active ? 4 : 2);
Log3 $name, $lev, "$name: deleting port for $d";
delete $defs{$d}{IODev};
}
}
SIGNALduino_Shutdown($hash);
DevIo_CloseDev($hash);
RemoveInternalTimer($hash);
RemoveInternalTimer("HandleWriteQueue:$name");
RemoveInternalTimer("sduino_IdList:$name");
return;
}
#####################################
sub
SIGNALduino_Shutdown
{
my ($hash) = @_;
#DevIo_SimpleWrite($hash, "XQ\n",2);
SIGNALduino_SimpleWrite($hash, "XQ"); # Switch reception off, it may hang up the SIGNALduino
return;
}
#####################################
#$hash,$name,"sendmsg","P17;R6#".substr($arg,2)
sub
SIGNALduino_RemoveLaCrossePair
{
my $hash = shift;
delete($hash->{LaCrossePair});
}
sub
SIGNALduino_Set
{
my ($hash, @a) = @_;
return "\"set SIGNALduino\" needs at least one parameter" if(@a < 2);
#Log3 $hash, 3, "SIGNALduino_Set called with params @a";
my $name = shift @a;
my $hasCC1101 = 0;
my $hasFSK = 0;
my $CC1101Frequency = "433";
my $mVer = 3; # 4 = Maple
if ($hash->{version}) {
if ($hash->{version} =~ m/cc1101/) {
$hasCC1101 = 1;
if (defined($hash->{cc1101_frequency}) && $hash->{cc1101_frequency} >= 800) {
$CC1101Frequency = 868;
}
}
if ($hash->{version} =~ m/V\s4\./) { # MapleCul oder MapleSduino
$mVer = 4;
$hasFSK = 1;
}
elsif ($hash->{version} =~ m/V\s3\.3\.[4-9]/) {
$hasFSK = 1;
}
}
my %my_sets = %sets;
#Log3 $hash, 3, "SIGNALduino_Set addionals set commands: ".Dumper(%{$hash->{additionalSets}});
#Log3 $hash, 3, "SIGNALduino_Set addionals rfmode set commands: ".Dumper(%{$hash->{rfmodesets}});
#Log3 $hash, 3, "SIGNALduino_Set global set commands: ".Dumper(%sets);
%my_sets = ( %my_sets, %{$hash->{additionalSets}} ) if ( defined($hash->{additionalSets}) );
%my_sets = ( %my_sets, %{$hash->{rfmodesets}} ) if ( defined($hash->{rfmodesets}) );
%my_sets = ( %my_sets, %{$hash->{rfmodesetsTesting}} ) if ( defined($hash->{rfmodesetsTesting}) );
#Log3 $hash, 3, "SIGNALduino_Set normal set commands: ".Dumper(%my_sets);
if (!defined($my_sets{$a[0]})) {
my $arguments = ' ';
foreach my $arg (sort keys %my_sets) {
next if ($arg =~ m/cc1101/ && $hasCC1101 == 0);
next if ($my_sets{$arg} ne "" && $arg ne "flash" && $arg ne "LaCrossePairForSec" && IsDummy($hash->{NAME}));
next if (($arg eq "rfmode" || $arg eq "rfmodeTesting") && $hasFSK == 0);
next if ($arg eq "LaCrossePairForSec" && !IsDummy($hash->{NAME}) && $hasFSK == 0);
if ($arg =~ m/patable/) {
next if (substr($arg, -3) ne $CC1101Frequency);
}
elsif ($arg =~ m/Messagetype/) {
next if (substr($arg, -1) ne $mVer);
}
$arguments.= $arg . ($my_sets{$arg} ? (':' . $my_sets{$arg}) : '') . ' ';
}
#Log3 $hash->{NAME}, 3, $hash->{NAME} . ": set $a[0] arg = $arguments";
return "Unknown argument $a[0], choose one of " . $arguments;
}
my $cmd = shift @a;
my $arg = join(" ", @a);
if ($cmd =~ m/cc1101/ && $hasCC1101 == 0) {
return "This command is only available with a cc1101 receiver";
}
return "$name is not active, may firmware is not supported, please flash or reset" if ($cmd ne 'reset' && $cmd ne 'flash' && exists($hash->{DevState}) && $hash->{DevState} ne 'initialized');
if ($cmd =~ m/^cc1101_/) {
$cmd = substr($cmd,7);
}
if($cmd eq "raw") {
Log3 $name, 4, "set $name $cmd $arg";
my $newarg;
if ($arg =~ m/^b(s|d)(s|t|c)[A-Fa-f0-9]+/) { # send WMBus
if (substr($arg,2,1) eq 's') {
$newarg = 'SN;N=11;D=';
}
else {
$newarg = 'SN;N=12;D=';
}
$newarg .= $arg . ';';
}
else {
$newarg = $arg;
}
#SIGNALduino_SimpleWrite($hash, $newarg);
SIGNALduino_AddSendQueue($hash,$newarg);
} elsif( $cmd eq "flash" ) {
my @args = split(' ', $arg);
my $log = "";
my $hexFile = "";
my @deviceName = split('@', $hash->{DeviceName});
my $port = $deviceName[0];
my $hardware=AttrVal($name,"hardware","");
my $baudrate=57600;
if (($hardware =~ m/optiboot/) || $hardware eq "uno") {
$baudrate=115200;
$hardware =~ s/_optiboot$//;
}
my $defaultHexFile = "./FHEM/firmware/$hash->{TYPE}_$hardware.hex";
my $logFile = AttrVal("global", "logdir", "./log/") . "$hash->{TYPE}-Flash.log";
return "Please define your hardware! (attr $name hardware <model of your receiver>) " if ($hardware eq "");
return "ERROR: argument failed! flash [hexFile|url]" if (!$args[0]);
#Log3 $hash, 3, "SIGNALduino_Set choosen flash option: $args[0] of available: ".Dumper($my_sets{flash});
if( grep $args[0] eq $_ , split(",",$my_sets{flash}) )
{
Log3 $hash, 3, "$name: SIGNALduino_Set flash $args[0] try to fetch github assets for tag $args[0]";
my $channel=AttrVal($name,"updateChannelFW","stable");
my $account = "RFD-FHEM";
if ($channel ne "stable" && $channel ne "testing") {
$account = $channel;
}
my ($tags, undef) = split("__", $args[0]);
my $ghurl = "https://api.github.com/repos/$account/SIGNALDuino/releases/tags/$tags";
Log3 $hash, 3, "$name: SIGNALduino_Set flash $tags try to fetch release $ghurl";
my $http_param = {
url => $ghurl,
timeout => 5,
hash => $hash, # Muss gesetzt werden, damit die Callback funktion wieder $hash hat
method => "GET", # Lesen von Inhalten
header => "User-Agent: perl_fhem\r\nAccept: application/json", # Den Header gemaess abzufragender Daten aendern
callback => \&SIGNALduino_githubParseHttpResponse, # Diese Funktion soll das Ergebnis dieser HTTP Anfrage bearbeiten
command => "getReleaseByTag"
};
HttpUtils_NonblockingGet($http_param); # Starten der HTTP Abfrage. Es gibt keinen Return-Code.
return;
}
elsif(!$arg || $args[0] !~ m/^(\w|\/|.)+$/) {
$hexFile = AttrVal($name, "hexFile", "");
if ($hexFile eq "") {
$hexFile = $defaultHexFile;
}
}
elsif ($args[0] =~ m/^https?:\/\// ) {
my $http_param = {
url => $args[0],
timeout => 5,
hash => $hash, # Muss gesetzt werden, damit die Callback funktion wieder $hash hat
method => "GET", # Lesen von Inhalten
callback => \&SIGNALduino_ParseHttpResponse, # Diese Funktion soll das Ergebnis dieser HTTP Anfrage bearbeiten
command => 'flash',
};
HttpUtils_NonblockingGet($http_param);
return;
} else {
$hexFile = $args[0];
}
Log3 $name, 3, "$name: filename $hexFile provided, trying to flash";
return "Usage: set $name flash [filename]\n\nor use the hexFile attribute" if($hexFile !~ m/^(\w|\/|.)+$/);
# Only for Arduino , not for ESP or MapleMini
if ($hardware =~ m/(?:nano|mini|radino|uno)/)
{
my $flashCommand;
if( !defined( $attr{$name}{flashCommand} ) ) { # check defined flashCommand from user | not, use standard flashCommand | yes, use user flashCommand
Log3 $name, 5, "$hash->{TYPE} $name: flashCommand is not defined. standard used to flash.";
if ($hardware eq "radinoCC1101") { # radinoCC1101 Port not /dev/ttyUSB0 --> /dev/ttyACM0
$flashCommand = "avrdude -c avr109 -b [BAUDRATE] -P [PORT] -p atmega32u4 -vv -D -U flash:w:[HEXFILE] 2>[LOGFILE]";
} else {
$flashCommand = "avrdude -c arduino -b [BAUDRATE] -P [PORT] -p atmega328p -vv -U flash:w:[HEXFILE] 2>[LOGFILE]";
}
} else {
$flashCommand = $attr{$name}{flashCommand};
Log3 $name, 3, "$hash->{TYPE} $name: flashCommand is manual defined! $flashCommand";
}
# check if flashtool (custom or default) exists, abort otherwise
my $flashTool = (split / /, $flashCommand)[0];
my $flashToolFound=0;
for my $path ( split /:/, $ENV{PATH} ) {
if ( -f "$path/$flashTool" && -x _ ) {
$flashToolFound=1;
last;
}
}
Log3 $name, 5, "$name: flashTool $flashTool found = $flashToolFound";
return "$flashTool could not be found. Either set PATH properly or provide $flashTool via: sudo apt-get install $flashTool" if($flashToolFound == 0);
# strip IP-port in case port is an IP-address
my $host = $port;
$host =~ s/:\d+//;
$log .= "flashing Arduino $name\n";
$log .= "hex file: $hexFile\n";
$log .= "port: $port\n";
$log .= "host: $host\n";
$log .= "log file: $logFile\n";
if($flashCommand ne "" && !IsDummy($name)) {
if (-e $logFile) {
unlink $logFile;
}
DevIo_CloseDev($hash);
readingsSingleUpdate($hash,'state','FIRMWARE UPDATE running',1);
$log .= "$name closed\n";
my $avrdude = $flashCommand;
$avrdude =~ s/\Q[PORT]\E/$port/g;
$avrdude =~ s/\Q[HOST]\E/$host/g;
$avrdude =~ s/\Q[BAUDRATE]\E/$baudrate/g;
$avrdude =~ s/\Q[HEXFILE]\E/$hexFile/g;
$avrdude =~ s/\Q[LOGFILE]\E/$logFile/g;
$log .= "command: $avrdude\n\n";
# `$avrdude`;
qx($avrdude);
local $/=undef;
if (-e $logFile) {
open FILE, $logFile;
my $logText = <FILE>;
close FILE;
if ($logText =~ m/flash verified/) {
Log3 $name, 3, "$name: avrdude, Firmware update was successfull";
readingsSingleUpdate($hash,'state','FIRMWARE UPDATE successfull',1);
FW_directNotify("FILTER=$name", "#FHEMWEB:WEB", "FW_okDialog('avrdude, Firmware update was successfull')", '');
} else {
readingsSingleUpdate($hash,'state','FIRMWARE UPDATE with error',1);
Log3 $name, 3, "$name: avrdude, ERROR: avrdude exited with error";
FW_directNotify("FILTER=$name", "#FHEMWEB:WEB", "FW_okDialog('ERROR: avrdude exited with error, for details see last flashlog.')", '');
$log .= 'ERROR: avrdude exited with error';
}
$log .= "--- AVRDUDE ---------------------------------------------------------------------------------\n";
$log .= $logText;
$log .= "--- AVRDUDE ---------------------------------------------------------------------------------\n\n";
}
else {
$log .= "WARNING: avrdude created no log file\n\n";
}
}
else {
$log .= "\n\nNo flashCommand found. Please define this attribute.\n\n";
}
if (!IsDummy($name)) {
DevIo_OpenDev($hash, 0, "SIGNALduino_DoInit", 'SIGNALduino_Connect');
}
else {
Log3 $name, 3, "$name; flashCommand=$flashCommand";
}
$log .= "$name opened\n";
$hash->{helper}{avrdudelogs} = $log;
return;
} else
{
FW_directNotify("FILTER=$name", "#FHEMWEB:WEB", "FW_okDialog('<u>ERROR:</u><br>Sorry, flashing your $hardware is currently not supported.<br>The file is only downloaded in /opt/fhem/FHEM/firmware.')", '');
return "Sorry, Flashing your ESP or Maple via Module is currently not supported.";
}
} elsif ($cmd =~ m/reset/i) {
delete($hash->{initResetFlag}) if defined($hash->{initResetFlag});
return SIGNALduino_ResetDevice($hash);
} elsif( $cmd eq "close" ) {
$hash->{DevState} = 'closed';
return SIGNALduino_CloseDevice($hash);
} elsif( $cmd =~ m/Messagetype/ ) {
my $argm;
if (substr($cmd,0,1) eq 'd') { # disableMessagetype
$argm = 'CD' . substr($arg,-1,1);
}
else { # enableMessagetype
$argm = 'CE' . substr($arg,-1,1);
}
#SIGNALduino_SimpleWrite($hash, $argm);
SIGNALduino_AddSendQueue($hash,$argm);
Log3 $name, 4, "set $name $cmd $arg $argm";;
} elsif( $cmd eq "freq" ) {
if ($arg eq "") {
$arg = AttrVal($name,"cc1101_frequency", 433.92);
}
my $f = $arg/26*65536;
my $f2 = sprintf("%02x", $f / 65536);
my $f1 = sprintf("%02x", int($f % 65536) / 256);
my $f0 = sprintf("%02x", $f % 256);
$arg = sprintf("%.3f", (hex($f2)*65536+hex($f1)*256+hex($f0))/65536*26);
Log3 $name, 3, "$name: Setting FREQ2..0 (0D,0E,0F) to $f2 $f1 $f0 = $arg MHz";
SIGNALduino_AddSendQueue($hash,"W0F$f2");
SIGNALduino_AddSendQueue($hash,"W10$f1");
SIGNALduino_AddSendQueue($hash,"W11$f0");
SIGNALduino_WriteInit($hash);
} elsif( $cmd eq "bWidth" ) {
SIGNALduino_AddSendQueue($hash,"C10");
$hash->{getcmd}->{cmd} = "bWidth";
$hash->{getcmd}->{arg} = $arg;
} elsif( $cmd eq "rAmpl" ) {
return "a numerical value between 24 and 42 is expected" if($arg !~ m/^\d+$/ || $arg < 24 || $arg > 42);
my ($v, $w);
for($v = 0; $v < @ampllist; $v++) {
last if($ampllist[$v] > $arg);
}
$v = sprintf("%02d", $v-1);
$w = $ampllist[$v];
Log3 $name, 3, "$name: Setting AGCCTRL2 (1B) to $v / $w dB";
SIGNALduino_AddSendQueue($hash,"W1D$v");
SIGNALduino_WriteInit($hash);
} elsif( $cmd eq "sens" ) {
return "a numerical value between 4 and 16 is expected" if($arg !~ m/^\d+$/ || $arg < 4 || $arg > 16);
my $w = int($arg/4)*4;
my $v = sprintf("9%d",$arg/4-1);
Log3 $name, 3, "$name: Setting AGCCTRL0 (1D) to $v / $w dB";
SIGNALduino_AddSendQueue($hash,"W1F$v");
SIGNALduino_WriteInit($hash);
} elsif( substr($cmd,0,7) eq "patable" ) {
my $paFreq = substr($cmd,8);
my $pa = "x" . $patable{$paFreq}{$arg};
Log3 $name, 3, "$name: Setting patable $paFreq $arg $pa";
SIGNALduino_AddSendQueue($hash,$pa);
SIGNALduino_WriteInit($hash);
} elsif( $cmd eq "dataRate" ) {
if ($arg >= 600 and $arg <= 500000) {
SIGNALduino_AddSendQueue($hash,"C10");
$hash->{getcmd}->{cmd} = "dataRate";
$hash->{getcmd}->{arg} = $arg;
}
else {
return "$name: set datarate $arg out of range (0.6 - 500kBaud)";
}
} elsif( $cmd eq "deviatn" ) {
my $deviatn;
my $bits;
my $devlast = 0;
my $bitlast = 0;
OUTDEVLOOP:
for (my $e=0; $e<8; $e++) {
for (my $m=0; $m<8; $m++) {
$deviatn = (8+$m)*(2**$e) *26000/(2**17);
$bits = $m + ($e << 4);
if ($arg > $deviatn) {
$devlast = $deviatn;
$bitlast = $bits;
}
else {
if (($deviatn - $arg) < ($arg - $devlast)) {
$devlast = $deviatn;
$bitlast = $bits;
}
last OUTDEVLOOP;
}
}
}
my $hexbits = sprintf("%02x",$bitlast);
my $devstr = sprintf("% 5.3f",$devlast);
Log3 $name, 3, "$name: Setting deviatn (15) to $hexbits = $devstr kHz";
SIGNALduino_AddSendQueue($hash,"W17$hexbits");
SIGNALduino_WriteInit($hash);
} elsif( $cmd eq "reg" ) {
## check for four hex digits
my @nonHex = grep (!/^[0-9A-Fa-f]{4}$/,@a[0..$#a]) ;
return "ERROR: wrong parameter value @nonHex, only hexadecimal four digits allowed" if (@nonHex);
## check allowed register position
#my (@wrongRegisters) = grep { !exists($cc1101_register{substr($_,0,2)}) } @a[0..$#a] ;
#return "ERROR: unknown register position ".substr($wrongRegisters[0],0,2) if (@wrongRegisters);
Log3 $name, 3, "$name: SetRegisters, cc1101_reg @a[0..$#a]";
my @tmpSendQueue=();
foreach my $argcmd (@a[0..$#a]) {
$argcmd = sprintf("W%02X%s",hex(substr($argcmd,0,2)) + 2,substr($argcmd,2,2));
SIGNALduino_AddSendQueue($hash,$argcmd);
}
SIGNALduino_WriteInit($hash);
} elsif( $cmd eq "LaCrossePairForSec" ) {
return "Usage: set $name LaCrossePairForSec <seconds_active> [ignore_battery]" if(!$arg || $a[0] !~ m/^\d+$/ || ($a[1] && $a[1] ne "ignore_battery") );
$hash->{LaCrossePair} = $a[1]?2:1;
InternalTimer(gettimeofday()+$a[0], "SIGNALduino_RemoveLaCrossePair", $hash, 0);
} elsif( $cmd eq "rfmode" || $cmd eq "rfmodeTesting") {
my $rfcw;
if ($arg eq "custom") {
my $carg = AttrVal($name,"rfmode_user",undef);
if (defined($carg)) {
$rfcw = $carg;
}
else {
return "ERROR: Attribute rfmode_user not defined";
}
}
elsif ($cmd eq "rfmode") {
$rfcw = $rfmode{$arg};
}
else {
$rfcw = $rfmodeTesting{$arg};
}
Log3 $name, 5, "$name: $cmd msg=$arg $rfcw";
$hash->{getcmd}->{cmd} = "rfmode";
$hash->{getcmd}->{arg} = $arg;
SIGNALduino_AddSendQueue($hash,$rfcw);
} elsif( $cmd eq "sendMsg" ) {
Log3 $name, 5, "$name: sendmsg msg=$arg";
# Split args in serval variables
my ($protocol,$data,$repeats,$clock,$frequency,$datalength,$dataishex);
my $slowrfA = '';
my $n=0;
foreach my $s (split "#", $arg) {
my $c = substr($s,0,1);
if ($n == 0 ) { # protocol
$protocol = substr($s,1);
} elsif ($n == 1) { # Data
$data = $s;
if ( substr($s,0,2) eq "0x" ) { $dataishex=1; $data=substr($data,2); }
else { $dataishex=0; }
} else {
if ($c eq 'R') { $repeats = substr($s,1); }
elsif ($c eq 'C') { $clock = substr($s,1); }
elsif ($c eq 'F') { $frequency = substr($s,1); }
elsif ($c eq 'L') { $datalength = substr($s,1); }
}
$n++;
}
return "$name: sendmsg, unknown protocol: $protocol" if (!exists($ProtocolListSIGNALduino{$protocol}));
if (defined($hash->{sendAslowrfID}{$protocol})) {
$slowrfA = 'A';
}
$repeats=1 if (!defined($repeats));
if (exists($ProtocolListSIGNALduino{$protocol}{frequency}) && $hasCC1101 && !defined($frequency)) {
$frequency = $ProtocolListSIGNALduino{$protocol}{frequency};
}
if (defined($frequency) && $hasCC1101) {
$frequency="F=$frequency;";
} else {
$frequency="";
}
#print ("data = $data \n");
#print ("protocol = $protocol \n");
#print ("repeats = $repeats \n");
my %signalHash;
my %patternHash;
my $pattern="";
my $cnt=0;
my $sendData;
if ($protocol == 119) { # Funkbus
$sendData = SIGNALduino_PreparingSend_Funkbus($hash, $data);
}
## modulation ASK/OOK - MC
elsif (exists($ProtocolListSIGNALduino{$protocol}{format}) && $ProtocolListSIGNALduino{$protocol}{format} eq 'manchester')
{
#$clock = (map { $clock += $_ } @{$ProtocolListSIGNALduino{$protocol}{clockrange}}) / 2 if (!defined($clock));
$clock += $_ for(@{$ProtocolListSIGNALduino{$protocol}{clockrange}});
$clock = round($clock/2,0);
if ($protocol == 43) {
#$data =~ tr/0123456789ABCDEF/FEDCBA9876543210/;
}
my $intro = "";
my $outro = "";
$intro = $ProtocolListSIGNALduino{$protocol}{msgIntro} if ($ProtocolListSIGNALduino{$protocol}{msgIntro});
$outro = $ProtocolListSIGNALduino{$protocol}{msgOutro}.";" if ($ProtocolListSIGNALduino{$protocol}{msgOutro});
if ($intro ne "" || $outro ne "")
{
$intro = "SC$slowrfA;R=$repeats;" . $intro;
$repeats = 0;
$slowrfA = '';
}
$sendData = $intro . "SM$slowrfA;" . ($repeats > 0 ? "R=$repeats;" : "") . "C=$clock;D=$data;" . $outro . $frequency; # SM;R=2;C=400;D=AFAFAF;
Log3 $name, 5, "$name: sendmsg Preparing manchester protocol=$protocol, repeats=$repeats, clock=$clock data=$data";
## Format of RX and TX data = normal, use FIFOs for RX and TX
} elsif (exists $ProtocolListSIGNALduino{$protocol}{cc1101FIFOmode}) {
my $ccN = $ProtocolListSIGNALduino{$protocol}{N}[0];
Log3 $name, 5, "$name: sendmsg Preparing cc1101 FIFO send, protocol=$protocol, repeats=$repeats, N=$ccN, data=$data";
$sendData = 'SN;' . ($repeats > 0 ? "R=$repeats;" : '') . "N=$ccN;D=$data;" # SN;R=1;N=9;D=08C11484498ABCDE;
## modulation ASK/OOK - MS MU
} else {
if ($protocol == 3 || substr($data,0,2) eq "is") {
if (substr($data,0,2) eq "is") {
$data = substr($data,2); # is am Anfang entfernen
}
if ($protocol == 3) {
$data = SIGNALduino_ITV1_tristateToBit($data);
} else {
$data = SIGNALduino_ITV1_31_tristateToBit($data); # $protocolId 3.1
}