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