Skip to content

Commit

Permalink
Add command to the base station where the user suffixes his input wit…
Browse files Browse the repository at this point in the history
…h a CRC32 (8 characters). If it doesn't match, the command is ignored. This is to be safe against interferences/problems on the serial line.
  • Loading branch information
breaker27 committed Mar 8, 2015
1 parent 17af90e commit 1684ea0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion firmware/shc_basestation/Makefile
@@ -1,6 +1,6 @@
#
# This file is part of smarthomatic, http://www.smarthomatic.org.
# Copyright (c) 2013 Uwe Freese
# Copyright (c) 2013..2015 Uwe Freese
#
# smarthomatic is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
Expand Down
2 changes: 1 addition & 1 deletion firmware/shc_basestation/request_buffer.c
@@ -1,6 +1,6 @@
/*
* This file is part of smarthomatic, http://www.smarthomatic.org.
* Copyright (c) 2013 Uwe Freese
* Copyright (c) 2013..2015 Uwe Freese
*
* smarthomatic is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down
2 changes: 1 addition & 1 deletion firmware/shc_basestation/shc_basestation.c
@@ -1,6 +1,6 @@
/*
* This file is part of smarthomatic, http://www.smarthomatic.org.
* Copyright (c) 2013 Uwe Freese
* Copyright (c) 2013..2015 Uwe Freese
*
* smarthomatic is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
Expand Down
33 changes: 29 additions & 4 deletions firmware/src_common/uart.c
Expand Up @@ -195,10 +195,26 @@ void process_cmd(void)
uint8_t val = eeprom_read_byte((uint8_t *)adr);
UART_PUTF2("EEPROM value at position 0x%x is 0x%x.\r\n", adr, val);
}
else if ((cmdbuf[0] == 's') && (strlen(cmdbuf) > 4)) // "send" command
else if ((cmdbuf[0] == 's') && (strlen(cmdbuf) > 6)) // "send" command
{
send_data_avail = true;
}
else if ((cmdbuf[0] == 'c') && (strlen(cmdbuf) > 14)) // "send" command with CRC
{
uint8_t len = strlen(cmdbuf);

uint32_t calculated_crc = crc32((uint8_t *)cmdbuf, len - 8);
uint32_t given_crc = hex_to_uint32((uint8_t *)cmdbuf, len - 8);

if (calculated_crc != given_crc)
{
UART_PUTF("CRC error! %08lx does not match. Ignoring command.\r\n", calculated_crc);
}
else
{
send_data_avail = true;
}
}
else
{
UART_PUTS("Unknown command.\r\n");
Expand Down Expand Up @@ -266,8 +282,8 @@ void process_rxbuf(void)
UART_PUTS("wAAXX..........write EEPROM at hex address AA to hex value XX\r\n");
UART_PUTS("x..............enable writing to EEPROM\r\n");
UART_PUTS("z..............disable writing to EEPROM\r\n");
UART_PUTS("sKK{T}{X}{D}...Use AES key KK to send a packet with MessageType T, followed\r\n");
UART_PUTS(" by all necessary extension header fields and message data.\r\n");
UART_PUTS("sKKTT{D}.......Use AES key KK to send a packet with MessageType TT, followed\r\n");
UART_PUTS(" by all necessary extension header fields and message data D.\r\n");
UART_PUTS(" Fields are: ReceiverID (RRRR), MessageGroup (GG), MessageID (MM)\r\n");
UART_PUTS(" AckSenderID (SSSS), AckPacketCounter (PPPPPP), Error (EE).\r\n");
UART_PUTS(" MessageData (DD) can be 0..17 bytes with bits moved to the left.\r\n");
Expand All @@ -278,6 +294,8 @@ void process_rxbuf(void)
UART_PUTS("sKK08GGMMDD...............Status\r\n");
UART_PUTS("sKK09SSSSPPPPPPEE.........Ack\r\n");
UART_PUTS("sKK0ASSSSPPPPPPEEGGMMDD...AckStatus\r\n");
UART_PUTS("cKKTT{D}{CRC}..Same as s..., but a CRC32 checksum of the command has to be appended.\r\n");
UART_PUTS(" If it doesn't match, the command is ignored.\r\n");
}
else if (input == 'x')
{
Expand Down Expand Up @@ -305,11 +323,18 @@ void process_rxbuf(void)
}
else if (input == 's')
{
UART_PUTS("*** Enter AES key nr, MessageType, header extension + data in hex format to send, finish with ENTER. ***\r\n");
UART_PUTS("*** Enter AES key nr, MessageType, header extension and data in hex format to send, finish with ENTER. ***\r\n");
cmdbuf[0] = 's';
bytes_to_read = 54; // 2 characters for key nr + 2 characters for MessageType + 16 characters for hdr.ext. + 2*17 characters for data
bytes_pos = 1;
}
else if (input == 'c')
{
UART_PUTS("*** Enter AES key nr, MessageType, header extension, data and CRC in hex format to send, finish with ENTER. ***\r\n");
cmdbuf[0] = 'c';
bytes_to_read = 62; // 2 characters for key nr + 2 characters for MessageType + 16 characters for hdr.ext. + 2*17 characters for data + 8 characters for CRC
bytes_pos = 1;
}
else
{
UART_PUTS("*** Character ignored. Press h for help. ***\r\n");
Expand Down
15 changes: 13 additions & 2 deletions firmware/src_common/util_generic.h
Expand Up @@ -47,12 +47,23 @@ uint8_t hex_to_uint8(uint8_t * buf, uint8_t offset);

static inline uint16_t hex_to_uint16(uint8_t * buf, uint8_t offset)
{
return ((uint16_t)hex_to_uint8(buf, offset) << 8) + hex_to_uint8(buf, offset + 2);
return ((uint16_t)hex_to_uint8(buf, offset) << 8)
+ hex_to_uint8(buf, offset + 2);
}

static inline uint32_t hex_to_uint24(uint8_t * buf, uint8_t offset)
{
return ((uint32_t)hex_to_uint8(buf, offset) << 16) + ((uint32_t)hex_to_uint8(buf, offset + 2) << 8) + hex_to_uint8(buf, offset + 4);
return ((uint32_t)hex_to_uint8(buf, offset) << 16)
+ ((uint32_t)hex_to_uint8(buf, offset + 2) << 8)
+ hex_to_uint8(buf, offset + 4);
}

static inline uint32_t hex_to_uint32(uint8_t * buf, uint8_t offset)
{
return ((uint32_t)hex_to_uint8(buf, offset) << 24)
+ ((uint32_t)hex_to_uint8(buf, offset + 2) << 16)
+ ((uint32_t)hex_to_uint8(buf, offset + 4) << 8)
+ hex_to_uint8(buf, offset + 6);
}

// ########## read/write 16 bit / 32 bit values to the byte buffer in PC byte order
Expand Down

0 comments on commit 1684ea0

Please sign in to comment.