2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #define AO_CMAC_KEY_LEN AO_AES_LEN
21 #define AO_CMAC_MAX_LEN (128 - AO_CMAC_KEY_LEN)
23 static __xdata uint8_t ao_radio_cmac_mutex;
24 __pdata int16_t ao_radio_cmac_rssi;
25 static __xdata uint8_t cmac_data[AO_CMAC_MAX_LEN + AO_CMAC_KEY_LEN + 2 + AO_CMAC_KEY_LEN];
26 static __pdata uint8_t ao_radio_cmac_len;
33 b = ao_cmd_hexchar(getchar());
35 ao_cmd_status = ao_cmd_lex_error;
51 round_len(uint8_t len)
55 /* Make sure we transfer at least one packet, and
56 * then make sure every packet is full. Note that
57 * there is no length encoded, and that the receiver
58 * must deal with any extra bytes in the packet
60 if (len < AO_CMAC_KEY_LEN)
61 len = AO_CMAC_KEY_LEN;
62 rem = len % AO_CMAC_KEY_LEN;
64 len += (AO_CMAC_KEY_LEN - rem);
69 * Sign and deliver the data sitting in the cmac buffer
72 radio_cmac_send(uint8_t len) __reentrant
77 /* Make sure the AES key is loaded */
84 ao_mutex_get(&ao_aes_mutex);
85 ao_aes_set_mode(ao_aes_mode_cbc_mac);
86 ao_aes_set_key(ao_config.aes_key);
88 for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
89 if (i + AO_CMAC_KEY_LEN < len)
90 ao_aes_run(&cmac_data[i], NULL);
92 ao_aes_run(&cmac_data[i], &cmac_data[len]);
94 ao_mutex_put(&ao_aes_mutex);
96 ao_radio_send(cmac_data, len + AO_CMAC_KEY_LEN);
100 * Receive and validate an incoming packet
104 radio_cmac_recv(uint8_t len, uint16_t timeout) __reentrant
108 len = round_len(len);
115 i = ao_radio_recv(cmac_data, len + AO_CMAC_KEY_LEN + 2);
119 ao_radio_cmac_rssi = 0;
120 return AO_RADIO_CMAC_TIMEOUT;
123 ao_radio_cmac_rssi = (int16_t) (((int8_t) cmac_data[len + AO_CMAC_KEY_LEN]) >> 1) - 74;
124 if (!(cmac_data[len + AO_CMAC_KEY_LEN +1] & AO_RADIO_STATUS_CRC_OK))
125 return AO_RADIO_CMAC_CRC_ERROR;
129 /* Compute the packet signature
131 ao_mutex_get(&ao_aes_mutex);
132 ao_aes_set_mode(ao_aes_mode_cbc_mac);
133 ao_aes_set_key(ao_config.aes_key);
135 for (i = 0; i < len; i += AO_CMAC_KEY_LEN) {
136 if (i + AO_CMAC_KEY_LEN < len)
137 ao_aes_run(&cmac_data[i], NULL);
139 ao_aes_run(&cmac_data[i], &cmac_data[len + AO_CMAC_KEY_LEN + 2]);
141 ao_mutex_put(&ao_aes_mutex);
143 /* Check the packet signature against the signature provided
147 if (memcmp(&cmac_data[len],
148 &cmac_data[len + AO_CMAC_KEY_LEN + 2],
149 AO_CMAC_KEY_LEN) != 0) {
150 return AO_RADIO_CMAC_MAC_ERROR;
153 return AO_RADIO_CMAC_OK;
157 ao_radio_cmac_send(__xdata void *packet, uint8_t len) __reentrant
159 if (len > AO_CMAC_MAX_LEN)
160 return AO_RADIO_CMAC_LEN_ERROR;
161 ao_mutex_get(&ao_radio_cmac_mutex);
162 ao_xmemcpy(cmac_data, packet, len);
164 ao_led_on(AO_LED_TX);
166 radio_cmac_send(len);
168 ao_led_off(AO_LED_TX);
170 ao_mutex_put(&ao_radio_cmac_mutex);
171 return AO_RADIO_CMAC_OK;
175 ao_radio_cmac_recv(__xdata void *packet, uint8_t len, uint16_t timeout) __reentrant
178 if (len > AO_CMAC_MAX_LEN)
179 return AO_RADIO_CMAC_LEN_ERROR;
180 ao_mutex_get(&ao_radio_cmac_mutex);
182 ao_led_on(AO_LED_RX);
184 i = radio_cmac_recv(len, timeout);
186 ao_led_off(AO_LED_RX);
188 if (i == AO_RADIO_CMAC_OK)
189 ao_xmemcpy(packet, cmac_data, len);
190 ao_mutex_put(&ao_radio_cmac_mutex);
195 radio_cmac_send_cmd(void) __reentrant
201 if (ao_cmd_status != ao_cmd_success)
204 if (len > AO_CMAC_MAX_LEN) {
205 ao_cmd_status = ao_cmd_syntax_error;
209 ao_mutex_get(&ao_radio_cmac_mutex);
211 for (i = 0; i < len; i++) {
212 cmac_data[i] = getbyte();
213 if (ao_cmd_status != ao_cmd_success)
216 radio_cmac_send(len);
217 ao_mutex_put(&ao_radio_cmac_mutex);
221 radio_cmac_recv_cmd(void) __reentrant
227 if (ao_cmd_status != ao_cmd_success)
231 if (ao_cmd_status != ao_cmd_success)
233 timeout = AO_MS_TO_TICKS(ao_cmd_lex_i);
234 ao_mutex_get(&ao_radio_cmac_mutex);
235 i = radio_cmac_recv(len, timeout);
236 if (i == AO_RADIO_CMAC_OK) {
238 for (i = 0; i < len; i++)
239 printf("%02x", cmac_data[i]);
240 printf (" %d\n", ao_radio_cmac_rssi);
242 printf ("ERROR %d %d\n", i, ao_radio_cmac_rssi);
243 ao_mutex_put(&ao_radio_cmac_mutex);
246 static __xdata struct ao_launch_command command;
247 static __xdata struct ao_launch_query query;
248 static __pdata uint16_t launch_serial;
249 static __pdata uint8_t launch_channel;
250 static __pdata uint16_t tick_offset;
253 launch_args(void) __reentrant
256 launch_serial = ao_cmd_lex_i;
258 launch_channel = ao_cmd_lex_i;
265 int8_t r = AO_RADIO_CMAC_OK;
267 tick_offset = ao_time();
268 for (i = 0; i < 10; i++) {
269 printf ("."); flush();
270 command.tick = ao_time();
271 command.serial = launch_serial;
272 command.cmd = AO_LAUNCH_QUERY;
273 command.channel = launch_channel;
274 ao_radio_cmac_send(&command, sizeof (command));
275 r = ao_radio_cmac_recv(&query, sizeof (query), AO_MS_TO_TICKS(500));
276 if (r == AO_RADIO_CMAC_OK)
279 tick_offset -= query.tick;
280 printf("\n"); flush();
285 launch_report_cmd(void) __reentrant
290 if (ao_cmd_status != ao_cmd_success)
294 case AO_RADIO_CMAC_OK:
296 switch (query.arm_status) {
297 case ao_igniter_ready:
298 case ao_igniter_active:
302 printf("Disarmed: ");
304 switch (query.igniter_status) {
308 case ao_igniter_ready:
309 printf("igniter good\n");
311 case ao_igniter_open:
312 printf("igniter bad\n");
316 printf("Invalid channel %d\n", launch_channel);
318 printf("Rssi: %d\n", ao_radio_cmac_rssi);
321 printf("Error %d\n", r);
327 launch_arm(void) __reentrant
329 command.tick = ao_time() - tick_offset;
330 command.serial = launch_serial;
331 command.cmd = AO_LAUNCH_ARM;
332 command.channel = launch_channel;
333 ao_radio_cmac_send(&command, sizeof (command));
337 launch_ignite(void) __reentrant
339 command.tick = ao_time() - tick_offset;
340 command.serial = launch_serial;
341 command.cmd = AO_LAUNCH_FIRE;
343 ao_radio_cmac_send(&command, sizeof (command));
347 launch_fire_cmd(void) __reentrant
349 static __xdata struct ao_launch_command command;
357 if (ao_cmd_status != ao_cmd_success)
360 if (r != AO_RADIO_CMAC_OK) {
361 printf("query failed %d\n", r);
365 for (i = 0; i < 4; i++) {
366 printf("arm %d\n", i); flush();
370 secs = secs * 10 - 5;
373 for (i = 0; i < secs; i++) {
374 printf("fire %d\n", i); flush();
376 ao_delay(AO_MS_TO_TICKS(100));
381 launch_arm_cmd(void) __reentrant
387 if (r != AO_RADIO_CMAC_OK) {
388 printf("query failed %d\n", r);
391 for (i = 0; i < 4; i++)
396 launch_ignite_cmd(void) __reentrant
400 for (i = 0; i < 4; i++)
404 static __code struct ao_cmds ao_radio_cmac_cmds[] = {
405 { radio_cmac_send_cmd, "s <length>\0Send AES-CMAC packet. Bytes to send follow on next line" },
406 { radio_cmac_recv_cmd, "S <length> <timeout>\0Receive AES-CMAC packet. Timeout in ms" },
407 { launch_report_cmd, "l <serial> <channel>\0Get remote launch status" },
408 { launch_fire_cmd, "f <serial> <channel> <secs>\0Fire remote igniter" },
409 { launch_arm_cmd, "a <serial> <channel>\0Arm remote igniter" },
410 { launch_ignite_cmd, "i <serial> <channel>\0Pulse remote igniter" },
415 ao_radio_cmac_init(void)
417 ao_cmd_register(&ao_radio_cmac_cmds[0]);