forked from arendst/Tasmota
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdrv_36_keeloq.ino
287 lines (243 loc) · 8.8 KB
/
xdrv_36_keeloq.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
xdrv_36_keeloq.ino - Jarolift Keeloq shutter support for Tasmota
Copyright (C) 2020 he-so
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef USE_KEELOQ
/*********************************************************************************************\
* Keeloq shutter support
*
* Uses hardware SPI and two user configurable GPIO's (CC1101 GDO0 and CC1101 GDO2)
*
* Considering the implementation these two user GPIO's are fake.
* Only CC1101 GDO0 is used and must always be GPIO05 dictated by the used CC1101 library.
\*********************************************************************************************/
#define XDRV_36 36
#include "cc1101.h"
#include <KeeloqLib.h>
#define SYNC_WORD 199
#define Lowpulse 400
#define Highpulse 800
const char kJaroliftCommands[] PROGMEM = "Keeloq|" // prefix
"SendRaw|SendButton|Set";
void (* const jaroliftCommand[])(void) PROGMEM = {
&CmndSendRaw, &CmdSendButton, &CmdSet};
CC1101 cc1101;
struct JAROLIFT_DEVICE {
int device_key_msb = 0x0; // stores cryptkey MSB
int device_key_lsb = 0x0; // stores cryptkey LSB
uint64_t button = 0x0; // 1000=0x8 up, 0100=0x4 stop, 0010=0x2 down, 0001=0x1 learning
int disc = 0x0100; // 0x0100 for single channel remote
uint32_t enc = 0x0; // stores the 32Bit encrypted code
uint64_t pack = 0; // Contains data to send.
int count = 0;
uint32_t serial = 0x0;
uint8_t port_tx;
uint8_t port_rx;
} jaroliftDevice;
void CmdSet(void)
{
if (XdrvMailbox.data_len > 0) {
if (XdrvMailbox.payload > 0) {
char *p;
uint32_t i = 0;
uint32_t param[4] = { 0 };
for (char *str = strtok_r(XdrvMailbox.data, ", ", &p); str && i < 4; str = strtok_r(nullptr, ", ", &p)) {
param[i] = strtoul(str, nullptr, 0);
i++;
}
for (uint32_t i = 0; i < 3; i++) {
if (param[i] < 1) { param[i] = 1; } // msb, lsb, serial, counter
}
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("params: %08x %08x %08x %08x"), param[0], param[1], param[2], param[3]);
Settings.keeloq_master_msb = param[0];
Settings.keeloq_master_lsb = param[1];
Settings.keeloq_serial = param[2];
Settings.keeloq_count = param[3];
jaroliftDevice.serial = param[2];
jaroliftDevice.count = param[3];
GenerateDeviceCryptKey();
ResponseCmndDone();
} else {
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("no payload"));
}
} else {
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("no param"));
}
}
void GenerateDeviceCryptKey()
{
Keeloq k(Settings.keeloq_master_msb, Settings.keeloq_master_lsb);
jaroliftDevice.device_key_msb = k.decrypt(jaroliftDevice.serial | 0x60000000L);
jaroliftDevice.device_key_lsb = k.decrypt(jaroliftDevice.serial | 0x20000000L);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("generated device keys: %08x %08x"), jaroliftDevice.device_key_msb, jaroliftDevice.device_key_lsb);
}
void CmdSendButton(void)
{
noInterrupts();
entertx();
if (XdrvMailbox.data_len > 0)
{
if (XdrvMailbox.payload > 0)
{
jaroliftDevice.button = strtoul(XdrvMailbox.data, nullptr, 0);
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("msb: %08x"), jaroliftDevice.device_key_msb);
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("lsb: %08x"), jaroliftDevice.device_key_lsb);
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("serial: %08x"), jaroliftDevice.serial);
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("disc: %08x"), jaroliftDevice.disc);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("KLQ: count: %08x"), jaroliftDevice.count);
CreateKeeloqPacket();
jaroliftDevice.count++;
Settings.keeloq_count = jaroliftDevice.count;
for(int repeat = 0; repeat <= 1; repeat++)
{
uint64_t bitsToSend = jaroliftDevice.pack;
digitalWrite(jaroliftDevice.port_tx, LOW);
delayMicroseconds(1150);
SendSyncPreamble(13);
delayMicroseconds(3500);
for(int i=72; i>0; i--)
{
SendBit(bitsToSend & 0x0000000000000001);
bitsToSend >>= 1;
}
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("finished sending bits at %d"), micros());
delay(16); // delay in loop context is save for wdt
}
}
}
interrupts();
enterrx();
ResponseCmndDone();
}
void SendBit(byte bitToSend)
{
if (bitToSend==1)
{
digitalWrite(jaroliftDevice.port_tx, LOW); // Simple encoding of bit state 1
delayMicroseconds(Lowpulse);
digitalWrite(jaroliftDevice.port_tx, HIGH);
delayMicroseconds(Highpulse);
}
else
{
digitalWrite(jaroliftDevice.port_tx, LOW); // Simple encoding of bit state 0
delayMicroseconds(Highpulse);
digitalWrite(jaroliftDevice.port_tx, HIGH);
delayMicroseconds(Lowpulse);
}
}
void CmndSendRaw(void)
{
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("cmd send called at %d"), micros());
noInterrupts();
entertx();
for(int repeat = 0; repeat <= 1; repeat++)
{
if (XdrvMailbox.data_len > 0)
{
digitalWrite(jaroliftDevice.port_tx, LOW);
delayMicroseconds(1150);
SendSyncPreamble(13);
delayMicroseconds(3500);
for(int i=XdrvMailbox.data_len-1; i>=0; i--)
{
SendBit(XdrvMailbox.data[i] == '1');
}
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("finished sending bits at %d"), micros());
delay(16); // delay in loop context is save for wdt
}
interrupts();
}
enterrx();
ResponseCmndDone();
}
void enterrx() {
unsigned char marcState = 0;
cc1101.setRxState();
delay(2);
unsigned long rx_time = micros();
while (((marcState = cc1101.readStatusReg(CC1101_MARCSTATE)) & 0x1F) != 0x0D )
{
if (micros() - rx_time > 50000) break; // Quit when marcState does not change...
}
}
void entertx() {
unsigned char marcState = 0;
cc1101.setTxState();
delay(2);
unsigned long rx_time = micros();
while (((marcState = cc1101.readStatusReg(CC1101_MARCSTATE)) & 0x1F) != 0x13 && 0x14 && 0x15)
{
if (micros() - rx_time > 50000) break; // Quit when marcState does not change...
}
}
void SendSyncPreamble(int l)
{
for (int i = 0; i < l; ++i)
{
digitalWrite(jaroliftDevice.port_tx, LOW);
delayMicroseconds(400);
digitalWrite(jaroliftDevice.port_tx, HIGH);
delayMicroseconds(380);
}
}
void CreateKeeloqPacket()
{
Keeloq k(jaroliftDevice.device_key_msb, jaroliftDevice.device_key_lsb);
unsigned int result = (jaroliftDevice.disc << 16) | jaroliftDevice.count;
jaroliftDevice.pack = (uint64_t)0;
jaroliftDevice.pack |= jaroliftDevice.serial & 0xfffffffL;
jaroliftDevice.pack |= (jaroliftDevice.button & 0xfL) << 28;
jaroliftDevice.pack <<= 32;
jaroliftDevice.enc = k.encrypt(result);
jaroliftDevice.pack |= jaroliftDevice.enc;
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("pack high: %08x"), jaroliftDevice.pack>>32);
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("pack low: %08x"), jaroliftDevice.pack);
}
void KeeloqInit()
{
jaroliftDevice.port_tx = pin[GPIO_CC1101_GDO2]; // Output port for transmission
jaroliftDevice.port_rx = pin[GPIO_CC1101_GDO0]; // Input port for reception
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("cc1101.init()"));
delay(100);
cc1101.init();
AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR("CC1101 done."));
cc1101.setSyncWord(SYNC_WORD, false);
cc1101.setCarrierFreq(CFREQ_433);
cc1101.disableAddressCheck();
pinMode(jaroliftDevice.port_tx, OUTPUT);
pinMode(jaroliftDevice.port_rx, INPUT_PULLUP);
jaroliftDevice.serial = Settings.keeloq_serial;
jaroliftDevice.count = Settings.keeloq_count;
GenerateDeviceCryptKey();
}
/*********************************************************************************************\
* Interface
\*********************************************************************************************/
bool Xdrv36(uint8_t function)
{
if ((99 == pin[GPIO_CC1101_GDO0]) || (99 == pin[GPIO_CC1101_GDO2])) { return false; }
bool result = false;
switch (function) {
case FUNC_COMMAND:
AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR("calling command"));
result = DecodeCommand(kJaroliftCommands, jaroliftCommand);
break;
case FUNC_INIT:
KeeloqInit();
DEBUG_DRIVER_LOG(LOG_LEVEL_DEBUG_MORE, PSTR("init done."));
break;
}
return result;
}
#endif // USE_KEELOQ