-
Notifications
You must be signed in to change notification settings - Fork 8
/
commands.c
267 lines (202 loc) · 5.68 KB
/
commands.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
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
/*
* Copyright (c) 2019 Maciej Suminski <orson@orson.net.pl>
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "commands.h"
#include "command_handlers.h"
#include "apps_list.h"
#include "buttons.h"
#include "udi_cdc.h"
#include "lcd.h"
#include <stddef.h>
#include <string.h>
static uint8_t cmd_overflow = 0;
static cmd_mode_t cmd_mode = CMD_MULTIPROTOCOL;
// Received command buffer
#define CMD_BUF_SIZE 257
static uint8_t cmd_buf[CMD_BUF_SIZE] = {0,};
static uint16_t cmd_buf_idx = 0;
// General-purpose response buffer
static uint8_t cmd_resp_buf[CMD_BUF_SIZE];
// Current reponse
static const uint8_t *cmd_resp = NULL;
static uint8_t cmd_resp_len = 0;
void cmd_new_data(uint8_t data)
{
cmd_buf[cmd_buf_idx] = data;
++cmd_buf_idx;
if (cmd_buf_idx == CMD_BUF_SIZE) {
cmd_overflow = 1;
cmd_buf_idx = 0;
}
}
void cmd_response(const uint8_t* buf, unsigned int len)
{
cmd_resp = buf;
cmd_resp_len = len;
}
void cmd_resp_init(cmd_resp_t response)
{
cmd_resp_buf[0] = 1; /* response length */
cmd_resp_buf[1] = response;
}
void cmd_resp_write(uint8_t data)
{
++cmd_resp_buf[0]; /* response length */
cmd_resp_buf[cmd_resp_buf[0]] = data;
}
void cmd_resp_writen(const uint8_t *data, unsigned int len)
{
memcpy(&cmd_resp_buf[cmd_resp_buf[0] + 1], data, len);
cmd_resp_buf[0] += len;
}
static inline void cmd_buf_reset(void)
{
cmd_overflow = 0;
cmd_buf_idx = 0;
}
static uint8_t cmd_crc(uint8_t *buf, unsigned int len)
{
uint8_t crc = 0;
for(unsigned int i = 0; i < len; ++i) {
crc ^= buf[i];
}
return crc;
}
static int cmd_valid_crc(void)
{
unsigned int cmd_len = cmd_buf[0];
uint8_t crc = cmd_crc(&cmd_buf[1], cmd_len);
return (crc == cmd_buf[cmd_len + 1]);
}
static void cmd_fix_resp_crc(void)
{
unsigned int cmd_len = cmd_resp_buf[0];
uint8_t crc = cmd_crc(&cmd_resp_buf[1], cmd_len);
cmd_resp_buf[cmd_len + 1] = crc;
}
static int cmd_execute_normal(void)
{
unsigned int cmd_len = cmd_buf[0];
/* Check for overflows */
if (cmd_overflow) {
cmd_resp_init(CMD_RESP_OVERFLOW);
}
/* Handle reset request */
else if (cmd_buf_idx == 1 && cmd_len == CMD_TYPE_RESET) {
cmd_resp_init(CMD_RESP_RESET);
}
/* Is the command complete? (+2 stands for the cmd_len and crc fields) */
else if (cmd_len + 2 > cmd_buf_idx) {
return 0;
}
else {
if (!cmd_valid_crc()) {
cmd_resp_init(CMD_RESP_CRC_ERR);
} else {
cmd_resp_buf[0] = 0; /* reset response length */
/* Handle usual commands */
switch(cmd_buf[1]) {
case CMD_TYPE_UART:
cmd_uart(&cmd_buf[2], cmd_len - 1);
break;
case CMD_TYPE_BTN:
cmd_btn(&cmd_buf[2], cmd_len - 1);
break;
case CMD_TYPE_LED:
cmd_led(&cmd_buf[2], cmd_len - 1);
break;
case CMD_TYPE_I2C:
cmd_i2c(&cmd_buf[2], cmd_len - 1);
break;
case CMD_TYPE_SPI:
cmd_spi(&cmd_buf[2], cmd_len - 1);
break;
case CMD_TYPE_LCD:
cmd_lcd(&cmd_buf[2], cmd_len - 1);
break;
default:
cmd_resp_init(CMD_RESP_INVALID_CMD);
break;
}
}
}
cmd_buf_reset();
cmd_fix_resp_crc();
cmd_response(cmd_resp_buf, cmd_raw_len(cmd_resp_buf));
return 1;
}
int cmd_try_execute(void)
{
int ret = 0;
switch (cmd_mode) {
case CMD_MULTIPROTOCOL:
ret = cmd_execute_normal();
break;
case CMD_SUMP:
ret = cmd_sump(cmd_buf, cmd_buf_idx);
break;
}
if (ret) {
cmd_buf_reset();
}
return ret;
}
void cmd_get_resp(const uint8_t **data, unsigned int *len)
{
*data = cmd_resp;
*len = cmd_resp_len;
}
void cmd_resp_processed(void)
{
cmd_resp = NULL;
cmd_resp_len = 0;
}
void cmd_set_mode(cmd_mode_t mode)
{
cmd_mode = mode;
cmd_buf_reset();
}
cmd_mode_t cmd_get_mode(void)
{
return cmd_mode;
}
void app_command_func(void)
{
const uint8_t *resp;
unsigned int resp_len;
int processed;
cmd_set_mode(CMD_MULTIPROTOCOL);
while(SSD1306_isBusy());
SSD1306_clearBufferFull();
SSD1306_setString(10, 3, "Command interface", 17, WHITE);
SSD1306_drawBufferDMA();
while(btn_state() != BUT_LEFT) {
if (udi_cdc_is_rx_ready()) {
cmd_new_data(udi_cdc_getc());
processed = cmd_try_execute();
if (processed) {
cmd_get_resp(&resp, &resp_len);
if(resp && resp_len > 0) {
udi_cdc_write_buf(resp, resp_len);
cmd_resp_processed();
}
}
}
}
while(btn_state()); /* wait for the button release */
}