Verify the integrity of key state update messages using CRC16-CCITT.

This commit is contained in:
László Monda
2017-05-31 03:26:50 +02:00
parent 57319489d2
commit 1e290ebc34
6 changed files with 38 additions and 3 deletions

View File

@@ -35,6 +35,7 @@ void SlaveProtocolHandler(void)
case SlaveCommand_GetKeyStates:
SlaveTxSize = KEY_STATE_BUFFER_SIZE;
BoolBytesToBits(keyMatrix.keyStates, SlaveTxBuffer, LEFT_KEYBOARD_HALF_KEY_COUNT);
CRC16_AppendToMessage(SlaveTxBuffer, KEY_STATE_SIZE);
break;
case SlaveCommand_SetTestLed:
SlaveTxSize = 0;

View File

@@ -4,6 +4,7 @@
// Includes:
#include "fsl_port.h"
#include "crc16.h"
// Macros:
@@ -14,7 +15,8 @@
#define PROTOCOL_RESPONSE_GENERIC_ERROR 1
#define LEFT_KEYBOARD_HALF_KEY_COUNT (KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM)
#define KEY_STATE_BUFFER_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
#define KEY_STATE_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
#define KEY_STATE_BUFFER_SIZE (KEY_STATE_SIZE + CRC16_HASH_LENGTH)
// Variables:

View File

@@ -6,6 +6,7 @@
#include "peripherals/test_led.h"
#include "test_states.h"
#include "bool_array_converter.h"
#include "crc16.h"
uhk_module_state_t UhkModuleStates[UHK_MODULE_MAX_COUNT];
uhk_module_field_t currentUhkModuleField = UhkModuleField_SendKeystatesRequestCommand;
@@ -38,7 +39,9 @@ void UhkModuleSlaveDriver_Update(uint8_t uhkModuleId)
currentUhkModuleField = UhkModuleField_SendPwmBrightnessCommand;
break;
case UhkModuleField_SendPwmBrightnessCommand:
BoolBitsToBytes(rxBuffer, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
if (CRC16_IsMessageValid(rxBuffer, KEY_STATE_SIZE)) {
BoolBitsToBytes(rxBuffer, CurrentKeyStates[SLOT_ID_LEFT_KEYBOARD_HALF], LEFT_KEYBOARD_HALF_KEY_COUNT);
}
txBuffer[0] = SlaveCommand_SetLedPwmBrightness;
txBuffer[1] = uhkModuleInternalState->ledPwmBrightness;
I2cAsyncWrite(I2C_ADDRESS_LEFT_KEYBOARD_HALF, txBuffer, 2);

View File

@@ -4,11 +4,13 @@
// Includes:
#include "fsl_common.h"
#include "crc16.h"
// Macros:
#define UHK_MODULE_MAX_COUNT 1
#define KEY_STATE_BUFFER_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
#define KEY_STATE_SIZE (LEFT_KEYBOARD_HALF_KEY_COUNT/8 + 1)
#define KEY_STATE_BUFFER_SIZE (KEY_STATE_SIZE + CRC16_HASH_LENGTH)
// Typedefs:

View File

@@ -33,3 +33,24 @@ void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash)
{
*hash = crc16Config->currentCrc;
}
crc16_data_t crc16data;
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes)
{
uint16_t hash;
crc16_init(&crc16data);
crc16_update(&crc16data, message, lengthInBytes);
crc16_finalize(&crc16data, &hash);
message[lengthInBytes] = hash & 0xff;
message[lengthInBytes+1] = hash >> 8;
}
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes)
{
uint16_t hash;
crc16_init(&crc16data);
crc16_update(&crc16data, message, lengthInBytes);
crc16_finalize(&crc16data, &hash);
return (message[lengthInBytes] == (hash & 0xff)) && (message[lengthInBytes+1] == (hash >> 8));
}

View File

@@ -2,6 +2,9 @@
#define __CRC16_H__
#include <stdint.h>
#include <stdbool.h>
#define CRC16_HASH_LENGTH 2 // bytes
typedef struct Crc16Data {
uint16_t currentCrc;
@@ -22,4 +25,7 @@ void crc16_update(crc16_data_t *crc16Config, const uint8_t *src, uint32_t length
//! @param hash Pointer to the value returned for the final calculated crc value.
void crc16_finalize(crc16_data_t *crc16Config, uint16_t *hash);
void CRC16_AppendToMessage(uint8_t *message, uint32_t lengthInBytes);
bool CRC16_IsMessageValid(uint8_t *message, uint32_t lengthInBytes);
#endif