]> the.earth.li Git - energenie-attiny.git/blob - main.c
Add mqtt-power showing integration with MQTT via Python
[energenie-attiny.git] / main.c
1 /*
2  * Basic firmware to control Some Energenie 433MHz sockets (ENER002-4)
3  * as if they were a www.dcttech.com 4 port USB Relay board
4  *
5  * Copyright 2018 Jonathan McDowell <noodles@earth.li>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 #include <stdbool.h>
21 #include <avr/eeprom.h>
22 #include <avr/interrupt.h>
23 #include <avr/io.h>
24 #include <avr/wdt.h>
25 #include <util/delay.h>
26
27 #include <avr/pgmspace.h>
28 #include "usbdrv.h"
29 #include "libs-device/osccal.h"
30
31 #define REPEAT_COUNT 10
32
33 #define CMD_ALL_ON 0xfe
34 #define CMD_ALL_OFF 0xfc
35 #define CMD_ON 0xff
36 #define CMD_OFF 0xfd
37 #define CMD_SET_SERIAL 0xfa
38
39 int serno_str[] = {
40         USB_STRING_DESCRIPTOR_HEADER(5),
41         '1', '2', '3', '4', '5',
42 };
43 uint32_t serno;
44 unsigned long cmd = 0;
45 int repeat = 0, wait = 0;
46 uint8_t state = 0;
47
48 PROGMEM const char usbHidReportDescriptor[22] = {
49         0x06, 0x00, 0xff,               /* USAGE PAGE (Generic Desktop) */
50         0x09, 0x01,                     /* USAGE (Vendor Usage 1) */
51         0xa1, 0x01,                     /* COLLECTION (Application) */
52         0x15, 0x00,                     /*   LOGICAL_MINIMUM (0) */
53         0x26, 0xff, 0x00,               /*   LOGICAL_MAXIMUM (255) */
54         0x75, 0x08,                     /*   REPORT_SIZE (8) */
55         0x95, 0x08,                     /*   REPORT_COUNT (8) */
56         0x09, 0x00,                     /*   USAGE (Undefined) */
57         0xb2, 0x02, 0x01,               /*   FEATURE (Data, Var, Abs, Buf) */
58         0xc0                            /* END_COLLECTION */
59 };
60
61 inline char hexdigit(int i)
62 {
63         return (i < 10) ? ('0' + i) : ('A' - 10 + i);
64 }
65
66 inline int digithex(char i)
67 {
68         if (i >= '0' && i <= '9')
69                 return i - '0';
70
71         if (i >= 'A' && i <= 'F')
72                 return i - 'A' + 10;
73
74         if (i >= 'a' && i <= 'f')
75                 return i - 'a' + 10;
76
77         return 0;
78 }
79
80 void fetch_serno(void)
81 {
82         eeprom_read_block(&serno, 0, 4);
83         if (serno == 0xffffffff) {
84                 /* If the EEPROM is blank, return a default serial # */
85                 serno_str[1] = '1';
86                 serno_str[2] = '2';
87                 serno_str[3] = '3';
88                 serno_str[4] = '4';
89                 serno_str[5] = '5';
90         } else {
91                 serno_str[1] = hexdigit((serno >> 20) & 0xF);
92                 serno_str[2] = hexdigit((serno >> 16) & 0xF);
93                 serno_str[3] = hexdigit((serno >> 12) & 0xF);
94                 serno_str[4] = hexdigit((serno >>  8) & 0xF);
95                 serno_str[5] = hexdigit((serno >>  4) & 0xF);
96         }
97 }
98
99 void update_serno(uchar *buf, uchar len)
100 {
101         uchar i;
102
103         serno = 0;
104         for (i = 0; i < 5; i++) {
105                 serno |= digithex(buf[i]);
106                 serno <<= 4;
107         }
108
109         /*
110          * I have no idea why this gets stored 3 times, but the original
111          * firmware does it.
112          */
113         eeprom_write_block(&serno, (void *) 0x00, 4);
114         eeprom_write_block(&serno, (void *) 0x40, 4);
115         eeprom_write_block(&serno, (void *) 0x80, 4);
116
117         for (i = 0; i < 5; i++) {
118                 serno_str[i + 1] = buf[i];
119         }
120 }
121
122 usbMsgLen_t usbFunctionSetup(uchar data[8])
123 {
124         usbRequest_t *rq = (void *) data;
125
126         if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
127                 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
128                                 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
129                         return 0xFF;
130                 }
131         }
132
133         return 0;
134 }
135
136 usbMsgLen_t usbFunctionDescriptor(usbRequest_t *rq)
137 {
138         if (rq->wValue.bytes[1] == USBDESCR_STRING &&
139                         rq->wValue.bytes[0] == 3) {
140                 usbMsgPtr = (usbMsgPtr_t) serno_str;
141                 return sizeof(serno_str);
142         }
143         return 0;
144 }
145
146 uchar usbFunctionRead(uchar *data, uchar len)
147 {
148         uchar i;
149
150         if (len != 0) {
151                 for (i = 0; i < 5; i++) {
152                         data[i] = serno_str[i + 1];
153                 }
154                 data[5] = data[6] = 0;
155                 data[7] = state;
156                 return len;
157         }
158
159         return 0;
160 }
161
162 uchar usbFunctionWrite(uchar *data, uchar len)
163 {
164         if (data[0] == CMD_ALL_ON) {
165                 cmd = serno | 0xd;
166                 state = 0xf;
167                 wait = 200;
168                 repeat = REPEAT_COUNT;
169         } else if (data[0] == CMD_ALL_OFF) {
170                 cmd = serno | 0xc;
171                 state = 0;
172                 wait = 10;
173                 repeat = REPEAT_COUNT;
174         } else if (data[0] == CMD_ON) {
175                 wait = 200;
176                 switch (data[1]) {
177                 case 1:
178                         cmd = serno | 0xf;
179                         break;
180                 case 2:
181                         cmd = serno | 0x7;
182                         break;
183                 case 3:
184                         cmd = serno | 0xb;
185                         break;
186                 case 4:
187                         cmd = serno | 0x3;
188                         break;
189                 default:
190                         return len;
191                 }
192                 repeat = REPEAT_COUNT;
193                 state |= (1 << (data[1] - 1));
194         } else if (data[0] == CMD_OFF) {
195                 wait = 200;
196                 switch (data[1]) {
197                 case 1:
198                         cmd = serno | 0xe;
199                         break;
200                 case 2:
201                         cmd = serno | 0x6;
202                         break;
203                 case 3:
204                         cmd = serno | 0xa;
205                         break;
206                 case 4:
207                         cmd = serno | 0x2;
208                         break;
209                 default:
210                         return len;
211                 }
212                 repeat = REPEAT_COUNT;
213                 state &= ~(1 << (data[1] - 1));
214         } else if (data[0] == CMD_SET_SERIAL) {
215                 update_serno(&data[1], 6);
216         }
217
218         return len;
219 }
220
221 void t433_transmit_bit(bool value)
222 {
223         PORTB |= 1 << PB0;
224         if (value)
225                 _delay_us(600);
226         else
227                 _delay_us(200);
228
229         PORTB &= ~(1 << PB0);
230         if (value)
231                 _delay_us(200);
232         else
233                 _delay_us(600);
234 }
235
236 void t433_send(unsigned long code, unsigned int length)
237 {
238         int i;
239
240         cli();
241         for (i = length - 1; i >= 0; i--) {
242                 if (code & (1L << i)) {
243                         t433_transmit_bit(true);
244                 } else {
245                         t433_transmit_bit(false);
246                 }
247         }
248         /* Send a sync bit */
249         PORTB |= 1 << PB0;
250         _delay_us(200);
251         PORTB &= ~(1 << PB0);
252         sei();
253         _delay_ms(30);
254 }
255
256 int __attribute__((noreturn)) main(void)
257 {
258         unsigned char i;
259
260         wdt_enable(WDTO_1S);
261
262         fetch_serno();
263
264         usbInit();
265         usbDeviceDisconnect();
266
267         i = 0;
268         while (--i) {
269                 wdt_reset();
270                 _delay_ms(1);
271         }
272
273         usbDeviceConnect();
274
275         /* Set the 433MHz transmitter bit to output mode */
276         DDRB |= (1 << PB0);
277         PORTB &= (1 << PB0);
278
279         sei(); /* We're ready to go; enable interrupts */
280
281         while (1) {
282                 wdt_reset();
283                 usbPoll();
284                 if (cmd) {
285                         if (wait) {
286                                 wait--;
287                         } else {
288                                 t433_send(cmd, 24);
289                                 if (--repeat == 0)
290                                         cmd = 0;
291                         }
292                 }
293         }
294 }