]> the.earth.li Git - energenie-attiny.git/blob - main.c
Track the requested state so we can return it when asked
[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 CMD_ALL_ON 0xfe
32 #define CMD_ALL_OFF 0xfc
33 #define CMD_ON 0xff
34 #define CMD_OFF 0xfd
35 #define CMD_SET_SERIAL 0xfa
36
37 int serno_str[] = {
38         USB_STRING_DESCRIPTOR_HEADER(5),
39         '1', '2', '3', '4', '5',
40 };
41 uint32_t serno;
42 unsigned long cmd = 0;
43 int repeat = 0, wait = 0;
44 uint8_t state = 0;
45
46 PROGMEM const char usbHidReportDescriptor[22] = {
47         0x06, 0x00, 0xff,               /* USAGE PAGE (Generic Desktop) */
48         0x09, 0x01,                     /* USAGE (Vendor Usage 1) */
49         0xa1, 0x01,                     /* COLLECTION (Application) */
50         0x15, 0x00,                     /*   LOGICAL_MINIMUM (0) */
51         0x26, 0xff, 0x00,               /*   LOGICAL_MAXIMUM (255) */
52         0x75, 0x08,                     /*   REPORT_SIZE (8) */
53         0x95, 0x08,                     /*   REPORT_COUNT (8) */
54         0x09, 0x00,                     /*   USAGE (Undefined) */
55         0xb2, 0x02, 0x01,               /*   FEATURE (Data, Var, Abs, Buf) */
56         0xc0                            /* END_COLLECTION */
57 };
58
59 inline char hexdigit(int i)
60 {
61         return (i < 10) ? ('0' + i) : ('A' - 10 + i);
62 }
63
64 inline int digithex(char i)
65 {
66         if (i >= '0' && i <= '9')
67                 return i - '0';
68
69         if (i >= 'A' && i <= 'F')
70                 return i - 'A' + 10;
71
72         if (i >= 'a' && i <= 'f')
73                 return i - 'a' + 10;
74
75         return 0;
76 }
77
78 void fetch_serno(void)
79 {
80         eeprom_read_block(&serno, 0, 4);
81         if (serno == 0xffffffff) {
82                 /* If the EEPROM is blank, return a default serial # */
83                 serno_str[1] = '1';
84                 serno_str[2] = '2';
85                 serno_str[3] = '3';
86                 serno_str[4] = '4';
87                 serno_str[5] = '5';
88         } else {
89                 serno_str[1] = hexdigit((serno >> 20) & 0xF);
90                 serno_str[2] = hexdigit((serno >> 16) & 0xF);
91                 serno_str[3] = hexdigit((serno >> 12) & 0xF);
92                 serno_str[4] = hexdigit((serno >>  8) & 0xF);
93                 serno_str[5] = hexdigit((serno >>  4) & 0xF);
94         }
95 }
96
97 void update_serno(uchar *buf, uchar len)
98 {
99         uchar i;
100
101         serno = 0;
102         for (i = 0; i < 5; i++) {
103                 serno |= digithex(buf[i]);
104                 serno <<= 4;
105         }
106
107         /*
108          * I have no idea why this gets stored 3 times, but the original
109          * firmware does it.
110          */
111         eeprom_write_block(&serno, (void *) 0x00, 4);
112         eeprom_write_block(&serno, (void *) 0x40, 4);
113         eeprom_write_block(&serno, (void *) 0x80, 4);
114
115         for (i = 0; i < 5; i++) {
116                 serno_str[i + 1] = buf[i];
117         }
118 }
119
120 usbMsgLen_t usbFunctionSetup(uchar data[8])
121 {
122         usbRequest_t *rq = (void *) data;
123
124         if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {
125                 if ((rq->bRequest == USBRQ_HID_GET_REPORT) ||
126                                 (rq->bRequest == USBRQ_HID_SET_REPORT)) {
127                         return 0xFF;
128                 }
129         }
130
131         return 0;
132 }
133
134 usbMsgLen_t usbFunctionDescriptor(usbRequest_t *rq)
135 {
136         if (rq->wValue.bytes[1] == USBDESCR_STRING &&
137                         rq->wValue.bytes[0] == 3) {
138                 usbMsgPtr = (usbMsgPtr_t) serno_str;
139                 return sizeof(serno_str);
140         }
141         return 0;
142 }
143
144 uchar usbFunctionRead(uchar *data, uchar len)
145 {
146         uchar i;
147
148         if (len != 0) {
149                 for (i = 0; i < 5; i++) {
150                         data[i] = serno_str[i + 1];
151                 }
152                 data[5] = data[6] = 0;
153                 data[7] = state;
154                 return len;
155         }
156
157         return 0;
158 }
159
160 uchar usbFunctionWrite(uchar *data, uchar len)
161 {
162         if (data[0] == CMD_ALL_ON) {
163                 cmd = serno | 0xd;
164                 state = 0xf;
165                 wait = 200;
166                 repeat = 5;
167         } else if (data[0] == CMD_ALL_OFF) {
168                 cmd = serno | 0xc;
169                 state = 0;
170                 wait = 10;
171                 repeat = 5;
172         } else if (data[0] == CMD_ON) {
173                 wait = 200;
174                 switch (data[1]) {
175                 case 1:
176                         cmd = serno | 0xf;
177                         break;
178                 case 2:
179                         cmd = serno | 0x7;
180                         break;
181                 case 3:
182                         cmd = serno | 0xb;
183                         break;
184                 case 4:
185                         cmd = serno | 0x3;
186                         break;
187                 default:
188                         return len;
189                 }
190                 repeat = 5;
191                 state |= (1 << (data[1] - 1));
192         } else if (data[0] == CMD_OFF) {
193                 wait = 200;
194                 switch (data[1]) {
195                 case 1:
196                         cmd = serno | 0xe;
197                         break;
198                 case 2:
199                         cmd = serno | 0x6;
200                         break;
201                 case 3:
202                         cmd = serno | 0xa;
203                         break;
204                 case 4:
205                         cmd = serno | 0x2;
206                         break;
207                 default:
208                         return len;
209                 }
210                 repeat = 5;
211                 state &= ~(1 << (data[1] - 1));
212         } else if (data[0] == CMD_SET_SERIAL) {
213                 update_serno(&data[1], 6);
214         }
215
216         return len;
217 }
218
219 void t433_transmit_bit(bool value)
220 {
221         PORTB |= 1 << PB0;
222         if (value)
223                 _delay_us(600);
224         else
225                 _delay_us(200);
226
227         PORTB &= ~(1 << PB0);
228         if (value)
229                 _delay_us(200);
230         else
231                 _delay_us(600);
232 }
233
234 void t433_send(unsigned long code, unsigned int length)
235 {
236         int i;
237
238         cli();
239         for (i = length - 1; i >= 0; i--) {
240                 if (code & (1L << i)) {
241                         t433_transmit_bit(true);
242                 } else {
243                         t433_transmit_bit(false);
244                 }
245         }
246         /* Send a sync bit */
247         PORTB |= 1 << PB0;
248         _delay_us(200);
249         PORTB &= ~(1 << PB0);
250         sei();
251         _delay_ms(30);
252 }
253
254 int __attribute__((noreturn)) main(void)
255 {
256         unsigned char i;
257
258         wdt_enable(WDTO_1S);
259
260         fetch_serno();
261
262         usbInit();
263         usbDeviceDisconnect();
264
265         i = 0;
266         while (--i) {
267                 wdt_reset();
268                 _delay_ms(1);
269         }
270
271         usbDeviceConnect();
272
273         /* Set the 433MHz transmitter bit to output mode */
274         DDRB |= (1 << PB0);
275         PORTB &= (1 << PB0);
276
277         sei(); /* We're ready to go; enable interrupts */
278
279         while (1) {
280                 wdt_reset();
281                 usbPoll();
282                 if (cmd) {
283                         if (wait) {
284                                 wait--;
285                         } else {
286                                 t433_send(cmd, 24);
287                                 if (--repeat == 0)
288                                         cmd = 0;
289                         }
290                 }
291         }
292 }