forked from TeamAntumbra/glow-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi-core.c
145 lines (120 loc) · 3.33 KB
/
api-core.c
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
/* Copyright (c) 2015 Antumbra
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/>. */
#include "api-core.h"
#include "option.h"
#include "proto.h"
#include <string.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <avr/eeprom.h>
#include <util/atomic.h>
#define DEVID_OPTION_ID 0x4f564944 // OVID
static void cmd_echo(const void *cmdbuf)
{
proto_send(0, cmdbuf, 56);
}
static void cmd_ask(const void *cmdbuf)
{
uint8_t sup = api_get_supported(proto_get_u32(&cmdbuf)) ? 1 : 0;
proto_send(0, &sup, 1);
}
static void cmd_diagnostic(const void *cmdbuf)
{
uint8_t diagbuf[56];
memset(diagbuf, 0, sizeof diagbuf);
api_core_fill_diagnostic(diagbuf);
proto_send_start(0);
proto_send_add(diagbuf, sizeof diagbuf);
proto_send_end();
}
static void cmd_implid(const void *cmdbuf)
{
proto_send_start(0);
for (int i = 0; i < 56; ++i) {
char c = pgm_read_byte(api_core_implementation_id + i);
proto_send_u8(c);
if (!c)
break;
}
proto_send_end();
}
static const uint8_t *get_override_devid(uint8_t *lenout)
{
uint8_t len;
uint8_t *start;
if (!option_find(DEVID_OPTION_ID, &len, &start) || len == 0)
return NULL;
uint8_t overridep = eeprom_read_byte(start);
if (!overridep)
return NULL;
*lenout = len - 1;
return start + 1;
}
static void cmd_devid(const void *cmdbuf)
{
proto_send_start(0);
uint8_t devidoptlen;
const uint8_t *devidopt = get_override_devid(&devidoptlen);
if (devidopt) {
for (uint8_t i = 0; i < devidoptlen; ++i)
proto_send_u8(eeprom_read_byte(devidopt + i));
}
else {
for (uint8_t i = 0; i < api_core_device_id_len; ++i)
proto_send_u8(pgm_read_byte(api_core_device_id + i));
proto_send_u8(SIGNATURE_0);
proto_send_u8(SIGNATURE_1);
proto_send_u8(SIGNATURE_2);
proto_send_u8(OSCCAL);
}
proto_send_end();
}
static void cmd_reset(const void *cmdbuf)
{
proto_send_start(0);
proto_send_end();
wdt_enable(WDTO_120MS);
while (1);
}
void api_core_recover_reset(void)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
MCUSR = 0;
wdt_reset();
wdt_disable();
}
}
static void cmd_hwid(const void *cmdbuf)
{
proto_send_start(0);
for (int i = 0; i < 56; ++i) {
char c = pgm_read_byte(api_core_hardware_id + i);
proto_send_u8(c);
if (!c)
break;
}
proto_send_end();
}
static const api_cmd cmds[] = {
{0, 0, cmd_echo},
{0, 1, cmd_ask},
{0, 2, cmd_diagnostic},
{0, 3, cmd_implid},
{0, 4, cmd_devid},
{0, 5, cmd_reset},
{0, 6, cmd_hwid},
};
const api_cmd_list api_core = {
.ncmds = sizeof cmds / sizeof *cmds,
.cmds = cmds,
};