From 0f92b0ba222335f45742c58d632eae21db349444 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sat, 24 Jun 2017 11:07:56 -0700 Subject: [PATCH 1/9] Take a step towards parsing keymaps --- right/src/config/parse_keymap.c | 33 +++++++++++++++++++++++--------- right/src/config/parse_keymap.h | 2 +- right/src/usb_protocol_handler.c | 2 +- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 7b2ebfd..07daa1a 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -25,7 +25,6 @@ static uint16_t readCompactLength(serialized_buffer_t *buffer) { return length; } -/* static const char *readString(serialized_buffer_t *buffer, uint16_t *len) { const char *str = (const char *)&(buffer->buffer[buffer->offset]); @@ -34,7 +33,6 @@ static const char *readString(serialized_buffer_t *buffer, uint16_t *len) { return str; } -*/ static void parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_None; @@ -171,15 +169,32 @@ static void clearModule(uint8_t layer, uint8_t moduleId) { memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t)); } -void ParseLayer(uint8_t *data, uint8_t layer) { - serialized_buffer_t buffer; - buffer.buffer = data; - buffer.offset = 0; - - uint8_t moduleCount = readCompactLength(&buffer); +void parseLayer(serialized_buffer_t *buffer, uint8_t layer) { + uint8_t moduleCount = readCompactLength(buffer); for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { clearModule(layer, moduleIdx); - parseModule(&buffer, layer); + parseModule(buffer, layer); } } + +void ParseKeymap(uint8_t *data) { + serialized_buffer_t *buffer = &(serialized_buffer_t){ data, 0 }; + uint16_t abbreviationLen; + uint16_t nameLen; + uint16_t descriptionLen; + const char *abbreviation = readString(buffer, &abbreviationLen); + bool isDefault = readBool(buffer); + const char *name = readString(buffer, &nameLen); + const char *description = readString(buffer, &descriptionLen); + uint8_t layerCount = readCompactLength(buffer); + + (void)abbreviation; + (void)isDefault; + (void)name; + (void)description; + for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { + parseLayer(buffer, layerIdx); + } +} + diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index 2e7d199..e6ec488 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -55,6 +55,6 @@ // Functions: - void ParseLayer(uint8_t *data, uint8_t layer); + void ParseKeymap(uint8_t *data); #endif diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index ca0749b..68223c4 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -193,7 +193,7 @@ void uploadConfig() void applyConfig() { - ParseLayer(ConfigBuffer, 0); + ParseKeymap(ConfigBuffer); } void setLedPwm() From 3d58445f2900dc3b758a8f88bfabaf9e17875e57 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Sun, 2 Jul 2017 10:21:23 -0700 Subject: [PATCH 2/9] Make parseLayer static --- right/src/config/parse_keymap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 07daa1a..0c08a62 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -169,7 +169,7 @@ static void clearModule(uint8_t layer, uint8_t moduleId) { memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t)); } -void parseLayer(serialized_buffer_t *buffer, uint8_t layer) { +static void parseLayer(serialized_buffer_t *buffer, uint8_t layer) { uint8_t moduleCount = readCompactLength(buffer); for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { From 02e5dea778b5c67ea3bd516e7799aad236dc3ff0 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Mon, 3 Jul 2017 19:40:41 -0700 Subject: [PATCH 3/9] Add basic error checking to the configuration parser --- right/src/config/parse_keymap.c | 50 ++++++++++++++++++++++----------- right/src/config/parse_keymap.h | 3 +- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 0c08a62..c2da2f1 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -34,11 +34,12 @@ static const char *readString(serialized_buffer_t *buffer, uint16_t *len) { return str; } -static void parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static bool parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_None; + return false; } -static void parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) { +static bool parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_Keystroke; uint8_t keystrokeType = (SERIALIZED_KEYSTROKE_TYPE_MASK_KEYSTROKE_TYPE & keyStrokeAction) >> SERIALIZED_KEYSTROKE_TYPE_OFFSET_KEYSTROKE_TYPE; @@ -53,6 +54,8 @@ static void parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeActio case SerializedKeystrokeType_System: keyAction->keystroke.keystrokeType = KeystrokeType_System; break; + default: + return true; } if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_SCANCODE) { keyAction->keystroke.scancode = keystrokeType == SerializedKeystrokeType_LongMedia ? readUInt16(buffer) : readUInt8(buffer); @@ -63,27 +66,30 @@ static void parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeActio if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_LONGPRESS) { keyAction->keystroke.longPressAction = readUInt8(buffer); } + return false; } -static void parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) { +static bool parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) { uint8_t layer = readUInt8(buffer) + 1; bool isToggle = readBool(buffer); KeyAction->type = KeyActionType_SwitchLayer; KeyAction->switchLayer.layer = layer; KeyAction->switchLayer.isToggle = isToggle; + return false; } -static void parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static bool parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { // uint16_t len; // const char *keymap = readString(buffer, &len); keyAction->type = KeyActionType_SwitchKeymap; // TODO: Implement this + return false; } -static void parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static bool parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) { uint8_t mouseAction = readUInt8(buffer); keyAction->type = KeyActionType_Mouse; @@ -127,10 +133,13 @@ static void parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffe case SerializedMouseAction_Decelerate: keyAction->mouse.moveActions |= MouseMove_Decelerate; break; + default: + return true; } + return false; } -static void parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static bool parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) { uint8_t keyActionType = readUInt8(buffer); switch (keyActionType) { @@ -145,40 +154,46 @@ static void parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) case SerializedKeyActionType_Mouse: return parseMouseAction(keyAction, buffer); default: - // TODO: Handle the case where the actionType is unknown. - break; + return true; } + return false; } -static void parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) { +static bool parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) { uint8_t actionCount = readCompactLength(buffer); for (uint8_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { key_action_t *keyAction = &(CurrentKeymap[targetLayer][moduleId][actionIdx]); - parseKeyAction(keyAction, buffer); + if (parseKeyAction(keyAction, buffer)) { + return true; + } } + return false; } -static void parseModule(serialized_buffer_t *buffer, uint8_t layer) { +static bool parseModule(serialized_buffer_t *buffer, uint8_t layer) { uint8_t moduleId = readUInt8(buffer); uint8_t pointerRole = readUInt8(buffer); - parseKeyActions(layer, buffer, moduleId, pointerRole); + return parseKeyActions(layer, buffer, moduleId, pointerRole); } static void clearModule(uint8_t layer, uint8_t moduleId) { memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t)); } -static void parseLayer(serialized_buffer_t *buffer, uint8_t layer) { +static bool parseLayer(serialized_buffer_t *buffer, uint8_t layer) { uint8_t moduleCount = readCompactLength(buffer); for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { clearModule(layer, moduleIdx); - parseModule(buffer, layer); + if (parseModule(buffer, layer)) { + return true; + } } + return false; } -void ParseKeymap(uint8_t *data) { +bool ParseKeymap(uint8_t *data) { serialized_buffer_t *buffer = &(serialized_buffer_t){ data, 0 }; uint16_t abbreviationLen; uint16_t nameLen; @@ -194,7 +209,10 @@ void ParseKeymap(uint8_t *data) { (void)name; (void)description; for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { - parseLayer(buffer, layerIdx); + if (parseLayer(buffer, layerIdx)) { + return true; + } } + return false; } diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index e6ec488..89d3d3a 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -4,6 +4,7 @@ // Includes: #include + #include // Macros: @@ -55,6 +56,6 @@ // Functions: - void ParseKeymap(uint8_t *data); + bool ParseKeymap(uint8_t *data); #endif From ebb154472cca7a521712eac65ecc07f2902cbd44 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Mon, 3 Jul 2017 19:41:17 -0700 Subject: [PATCH 4/9] Send the status code returned by the configuration parser to the host --- right/src/usb_protocol_handler.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index 68223c4..e5c2581 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -193,7 +193,7 @@ void uploadConfig() void applyConfig() { - ParseKeymap(ConfigBuffer); + setError(ParseKeymap(ConfigBuffer)); } void setLedPwm() From c7977c905c89392e3f5c53fb02af684f22409f24 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Mon, 3 Jul 2017 19:42:45 -0700 Subject: [PATCH 5/9] Send the final position of the configuration parser to the host --- right/src/config/parse_keymap.c | 3 +-- right/src/config/parse_keymap.h | 2 +- right/src/usb_protocol_handler.c | 6 +++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index c2da2f1..7e47fb3 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -193,8 +193,7 @@ static bool parseLayer(serialized_buffer_t *buffer, uint8_t layer) { return false; } -bool ParseKeymap(uint8_t *data) { - serialized_buffer_t *buffer = &(serialized_buffer_t){ data, 0 }; +bool ParseKeymap(serialized_buffer_t *buffer) {; uint16_t abbreviationLen; uint16_t nameLen; uint16_t descriptionLen; diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index 89d3d3a..89074d8 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -56,6 +56,6 @@ // Functions: - bool ParseKeymap(uint8_t *data); + bool ParseKeymap(serialized_buffer_t *buffer); #endif diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index e5c2581..8b5f391 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -193,7 +193,11 @@ void uploadConfig() void applyConfig() { - setError(ParseKeymap(ConfigBuffer)); + serialized_buffer_t buffer = { ConfigBuffer, 0 }; + + GenericHidOutBuffer[0] = ParseKeymap(&buffer); + GenericHidOutBuffer[1] = buffer.offset; + GenericHidOutBuffer[2] = buffer.offset >> 8; } void setLedPwm() From 0d68f190e1f8699dbc87c82cde142d635d52a6d5 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Thu, 6 Jul 2017 15:44:25 -0700 Subject: [PATCH 6/9] Make the configuration parser return a more detailed error code --- right/src/config/parse_keymap.c | 69 ++++++++++++++++++++------------- right/src/config/parse_keymap.h | 2 +- 2 files changed, 42 insertions(+), 29 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 7e47fb3..38c06c5 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -4,6 +4,13 @@ #define longCompactLengthPrefix 0xff +enum { + ParserError_Success, + ParserError_InvalidSerializedKeystrokeType, + ParserError_InvalidSerializedMouseAction, + ParserError_InvalidSerializedKeyActionType, +}; + static uint8_t readUInt8(serialized_buffer_t *buffer) { return buffer->buffer[buffer->offset++]; } @@ -34,12 +41,12 @@ static const char *readString(serialized_buffer_t *buffer, uint16_t *len) { return str; } -static bool parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static uint8_t parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_None; - return false; + return ParserError_Success; } -static bool parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) { +static uint8_t parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_Keystroke; uint8_t keystrokeType = (SERIALIZED_KEYSTROKE_TYPE_MASK_KEYSTROKE_TYPE & keyStrokeAction) >> SERIALIZED_KEYSTROKE_TYPE_OFFSET_KEYSTROKE_TYPE; @@ -55,7 +62,7 @@ static bool parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeActio keyAction->keystroke.keystrokeType = KeystrokeType_System; break; default: - return true; + return ParserError_InvalidSerializedKeystrokeType; } if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_SCANCODE) { keyAction->keystroke.scancode = keystrokeType == SerializedKeystrokeType_LongMedia ? readUInt16(buffer) : readUInt8(buffer); @@ -66,30 +73,30 @@ static bool parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeActio if (keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_LONGPRESS) { keyAction->keystroke.longPressAction = readUInt8(buffer); } - return false; + return ParserError_Success; } -static bool parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) { +static uint8_t parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) { uint8_t layer = readUInt8(buffer) + 1; bool isToggle = readBool(buffer); KeyAction->type = KeyActionType_SwitchLayer; KeyAction->switchLayer.layer = layer; KeyAction->switchLayer.isToggle = isToggle; - return false; + return ParserError_Success; } -static bool parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static uint8_t parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { // uint16_t len; // const char *keymap = readString(buffer, &len); keyAction->type = KeyActionType_SwitchKeymap; // TODO: Implement this - return false; + return ParserError_Success; } -static bool parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static uint8_t parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) { uint8_t mouseAction = readUInt8(buffer); keyAction->type = KeyActionType_Mouse; @@ -134,12 +141,12 @@ static bool parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffe keyAction->mouse.moveActions |= MouseMove_Decelerate; break; default: - return true; + return ParserError_InvalidSerializedMouseAction; } - return false; + return ParserError_Success; } -static bool parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static uint8_t parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) { uint8_t keyActionType = readUInt8(buffer); switch (keyActionType) { @@ -154,24 +161,26 @@ static bool parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) case SerializedKeyActionType_Mouse: return parseMouseAction(keyAction, buffer); default: - return true; + return ParserError_InvalidSerializedKeyActionType; } - return false; + return ParserError_Success; } -static bool parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) { +static uint8_t parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) { + uint8_t errorCode; uint8_t actionCount = readCompactLength(buffer); for (uint8_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { key_action_t *keyAction = &(CurrentKeymap[targetLayer][moduleId][actionIdx]); - if (parseKeyAction(keyAction, buffer)) { - return true; + errorCode = parseKeyAction(keyAction, buffer); + if (errorCode != ParserError_Success) { + return errorCode; } } - return false; + return ParserError_Success; } -static bool parseModule(serialized_buffer_t *buffer, uint8_t layer) { +static uint8_t parseModule(serialized_buffer_t *buffer, uint8_t layer) { uint8_t moduleId = readUInt8(buffer); uint8_t pointerRole = readUInt8(buffer); return parseKeyActions(layer, buffer, moduleId, pointerRole); @@ -181,19 +190,22 @@ static void clearModule(uint8_t layer, uint8_t moduleId) { memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t)); } -static bool parseLayer(serialized_buffer_t *buffer, uint8_t layer) { +static uint8_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { + uint8_t errorCode; uint8_t moduleCount = readCompactLength(buffer); for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { clearModule(layer, moduleIdx); - if (parseModule(buffer, layer)) { - return true; + errorCode = parseModule(buffer, layer); + if (errorCode != ParserError_Success) { + return errorCode; } } - return false; + return ParserError_Success; } -bool ParseKeymap(serialized_buffer_t *buffer) {; +uint8_t ParseKeymap(serialized_buffer_t *buffer) {; + uint8_t errorCode; uint16_t abbreviationLen; uint16_t nameLen; uint16_t descriptionLen; @@ -208,10 +220,11 @@ bool ParseKeymap(serialized_buffer_t *buffer) {; (void)name; (void)description; for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { - if (parseLayer(buffer, layerIdx)) { - return true; + errorCode = parseLayer(buffer, layerIdx); + if (errorCode != ParserError_Success) { + return errorCode; } } - return false; + return ParserError_Success; } diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index 89074d8..186fee4 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -56,6 +56,6 @@ // Functions: - bool ParseKeymap(serialized_buffer_t *buffer); + uint8_t ParseKeymap(serialized_buffer_t *buffer); #endif From 042fb60192bc02fdc0d75b021b3f6c0377e1d6d8 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Thu, 6 Jul 2017 17:41:54 -0700 Subject: [PATCH 7/9] Make the configuration parser check that every array has an expected length --- right/src/config/parse_keymap.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 38c06c5..454de59 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -9,6 +9,9 @@ enum { ParserError_InvalidSerializedKeystrokeType, ParserError_InvalidSerializedMouseAction, ParserError_InvalidSerializedKeyActionType, + ParserError_InvalidLayerCount, + ParserError_InvalidModuleCount, + ParserError_InvalidActionCount, }; static uint8_t readUInt8(serialized_buffer_t *buffer) { @@ -170,6 +173,9 @@ static uint8_t parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t errorCode; uint8_t actionCount = readCompactLength(buffer); + if (actionCount > MAX_KEY_COUNT_PER_MODULE) { + return ParserError_InvalidActionCount; + } for (uint8_t actionIdx = 0; actionIdx < actionCount; actionIdx++) { key_action_t *keyAction = &(CurrentKeymap[targetLayer][moduleId][actionIdx]); errorCode = parseKeyAction(keyAction, buffer); @@ -194,6 +200,9 @@ static uint8_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { uint8_t errorCode; uint8_t moduleCount = readCompactLength(buffer); + if (moduleCount > SLOT_COUNT) { + return ParserError_InvalidModuleCount; + } for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) { clearModule(layer, moduleIdx); errorCode = parseModule(buffer, layer); @@ -219,6 +228,9 @@ uint8_t ParseKeymap(serialized_buffer_t *buffer) {; (void)isDefault; (void)name; (void)description; + if (layerCount != LAYER_COUNT) { + return ParserError_InvalidLayerCount; + } for (uint8_t layerIdx = 0; layerIdx < layerCount; layerIdx++) { errorCode = parseLayer(buffer, layerIdx); if (errorCode != ParserError_Success) { From 24ed2685182eda3a3c4e8872e2d5d05a64d93096 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Fri, 7 Jul 2017 08:00:10 -0700 Subject: [PATCH 8/9] Ensure that parseSwitchKeymapAction advances buffer.offset by the correct amount --- right/src/config/parse_keymap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 454de59..2feef89 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -90,11 +90,11 @@ static uint8_t parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer } static uint8_t parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { -// uint16_t len; -// const char *keymap = readString(buffer, &len); + uint16_t keymapAbbreviationLen; + const char *keymapAbbreviation = readString(buffer, &keymapAbbreviationLen); + (void)keymapAbbreviation; keyAction->type = KeyActionType_SwitchKeymap; - // TODO: Implement this return ParserError_Success; } From 55328d719fb043023f024ec7a32d16d526fe5b38 Mon Sep 17 00:00:00 2001 From: Eric Tang Date: Fri, 7 Jul 2017 10:15:03 -0700 Subject: [PATCH 9/9] Create a type for the parser error codes --- right/src/config/parse_keymap.c | 36 ++++++++++++--------------------- right/src/config/parse_keymap.h | 12 ++++++++++- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/right/src/config/parse_keymap.c b/right/src/config/parse_keymap.c index 2feef89..bf53581 100644 --- a/right/src/config/parse_keymap.c +++ b/right/src/config/parse_keymap.c @@ -4,16 +4,6 @@ #define longCompactLengthPrefix 0xff -enum { - ParserError_Success, - ParserError_InvalidSerializedKeystrokeType, - ParserError_InvalidSerializedMouseAction, - ParserError_InvalidSerializedKeyActionType, - ParserError_InvalidLayerCount, - ParserError_InvalidModuleCount, - ParserError_InvalidActionCount, -}; - static uint8_t readUInt8(serialized_buffer_t *buffer) { return buffer->buffer[buffer->offset++]; } @@ -44,12 +34,12 @@ static const char *readString(serialized_buffer_t *buffer, uint16_t *len) { return str; } -static uint8_t parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static parser_error_t parseNoneAction(key_action_t *keyAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_None; return ParserError_Success; } -static uint8_t parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) { +static parser_error_t parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAction, serialized_buffer_t *buffer) { keyAction->type = KeyActionType_Keystroke; uint8_t keystrokeType = (SERIALIZED_KEYSTROKE_TYPE_MASK_KEYSTROKE_TYPE & keyStrokeAction) >> SERIALIZED_KEYSTROKE_TYPE_OFFSET_KEYSTROKE_TYPE; @@ -79,7 +69,7 @@ static uint8_t parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyStrokeAc return ParserError_Success; } -static uint8_t parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) { +static parser_error_t parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer_t *buffer) { uint8_t layer = readUInt8(buffer) + 1; bool isToggle = readBool(buffer); @@ -89,7 +79,7 @@ static uint8_t parseSwitchLayerAction(key_action_t *KeyAction, serialized_buffer return ParserError_Success; } -static uint8_t parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static parser_error_t parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffer_t *buffer) { uint16_t keymapAbbreviationLen; const char *keymapAbbreviation = readString(buffer, &keymapAbbreviationLen); @@ -99,7 +89,7 @@ static uint8_t parseSwitchKeymapAction(key_action_t *keyAction, serialized_buffe return ParserError_Success; } -static uint8_t parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static parser_error_t parseMouseAction(key_action_t *keyAction, serialized_buffer_t *buffer) { uint8_t mouseAction = readUInt8(buffer); keyAction->type = KeyActionType_Mouse; @@ -149,7 +139,7 @@ static uint8_t parseMouseAction(key_action_t *keyAction, serialized_buffer_t *bu return ParserError_Success; } -static uint8_t parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) { +static parser_error_t parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buffer) { uint8_t keyActionType = readUInt8(buffer); switch (keyActionType) { @@ -169,8 +159,8 @@ static uint8_t parseKeyAction(key_action_t *keyAction, serialized_buffer_t *buff return ParserError_Success; } -static uint8_t parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) { - uint8_t errorCode; +static parser_error_t parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, uint8_t moduleId, uint8_t pointerRole) { + parser_error_t errorCode; uint8_t actionCount = readCompactLength(buffer); if (actionCount > MAX_KEY_COUNT_PER_MODULE) { @@ -186,7 +176,7 @@ static uint8_t parseKeyActions(uint8_t targetLayer, serialized_buffer_t *buffer, return ParserError_Success; } -static uint8_t parseModule(serialized_buffer_t *buffer, uint8_t layer) { +static parser_error_t parseModule(serialized_buffer_t *buffer, uint8_t layer) { uint8_t moduleId = readUInt8(buffer); uint8_t pointerRole = readUInt8(buffer); return parseKeyActions(layer, buffer, moduleId, pointerRole); @@ -196,8 +186,8 @@ static void clearModule(uint8_t layer, uint8_t moduleId) { memset(&CurrentKeymap[layer][moduleId], 0, MAX_KEY_COUNT_PER_MODULE * sizeof(key_action_t)); } -static uint8_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { - uint8_t errorCode; +static parser_error_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { + parser_error_t errorCode; uint8_t moduleCount = readCompactLength(buffer); if (moduleCount > SLOT_COUNT) { @@ -213,8 +203,8 @@ static uint8_t parseLayer(serialized_buffer_t *buffer, uint8_t layer) { return ParserError_Success; } -uint8_t ParseKeymap(serialized_buffer_t *buffer) {; - uint8_t errorCode; +parser_error_t ParseKeymap(serialized_buffer_t *buffer) {; + parser_error_t errorCode; uint16_t abbreviationLen; uint16_t nameLen; uint16_t descriptionLen; diff --git a/right/src/config/parse_keymap.h b/right/src/config/parse_keymap.h index 186fee4..3e0360b 100644 --- a/right/src/config/parse_keymap.h +++ b/right/src/config/parse_keymap.h @@ -54,8 +54,18 @@ uint16_t offset; } serialized_buffer_t; + typedef enum { + ParserError_Success, + ParserError_InvalidSerializedKeystrokeType, + ParserError_InvalidSerializedMouseAction, + ParserError_InvalidSerializedKeyActionType, + ParserError_InvalidLayerCount, + ParserError_InvalidModuleCount, + ParserError_InvalidActionCount, + } parser_error_t; + // Functions: - uint8_t ParseKeymap(serialized_buffer_t *buffer); + parser_error_t ParseKeymap(serialized_buffer_t *buffer); #endif