Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #253 from UltimateHackingKeyboard/parse_targets
Browse files Browse the repository at this point in the history
Parse connections from config.
  • Loading branch information
mondalaci authored Oct 5, 2024
2 parents e4a8f30 + ed46720 commit ea00210
Show file tree
Hide file tree
Showing 20 changed files with 222 additions and 28 deletions.
6 changes: 6 additions & 0 deletions device/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,9 @@ CONFIG_BT_L2CAP_TX_MTU=247
CONFIG_BT_ATT_PREPARE_COUNT=4

CONFIG_UDC_DRIVER_LOG_LEVEL_WRN=y

# Apply config requires deep stack, and since Agent expects response code,
# we can't just do it from the main thread
CONFIG_C2USB_UDC_MAC_THREAD_STACK_SIZE=2048


1 change: 1 addition & 0 deletions device/src/legacy/config_parser/error_reporting.c
1 change: 1 addition & 0 deletions device/src/legacy/config_parser/error_reporting.h
1 change: 1 addition & 0 deletions device/src/legacy/config_parser/parse_host_connection.c
1 change: 1 addition & 0 deletions device/src/legacy/config_parser/parse_host_connection.h
1 change: 1 addition & 0 deletions device/src/legacy/host_connection.h
29 changes: 29 additions & 0 deletions right/src/config_parser/error_reporting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "error_reporting.h"
#include "config_parser/basic_types.h"
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <stdarg.h>
#include "macros/status_buffer.h"

void ConfigParser_Error(config_buffer_t *buffer, const char *fmt, ...)
{
va_list myargs;
va_start(myargs, fmt);
char printBuffer[256];
vsprintf(printBuffer, fmt, myargs);
Macros_ReportErrorPrintf(NULL, "%d: %s", buffer->offset, printBuffer);
config_buffer_t myBuffer = *buffer;
myBuffer.offset = buffer->offset >= 10 ? buffer->offset - 10 : 0;
uint8_t windowCount = 5;
for (uint8_t window = 0; window < windowCount; window++)
{
uint8_t context[10];
for (uint8_t i = 0; i < 10; i++)
{
context[i] = ReadUInt8(&myBuffer);
}
Macros_ReportErrorPrintf(NULL, "%d: %u %u %u %u %u %u %u %u %u %u", myBuffer.offset-10, context[0], context[1], context[2], context[3], context[4], context[5], context[6], context[7], context[8], context[9]);
}
}

17 changes: 17 additions & 0 deletions right/src/config_parser/error_reporting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef __CONFIG_PARSER_ERROR_REPORTING_H__
#define __CONFIG_PARSER_ERROR_REPORTING_H__

// Includes:

#include "basic_types.h"
#include "parse_config.h"
#include <stdint.h>
#include <stdbool.h>

// Typedefs:

// Functions:

void ConfigParser_Error(config_buffer_t *buffer, const char *fmt, ...);

#endif
44 changes: 30 additions & 14 deletions right/src/config_parser/parse_config.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string.h>
#include "basic_types.h"
#include "macros/status_buffer.h"
#include "parse_config.h"
#include "parse_keymap.h"
#include "parse_macro.h"
Expand All @@ -21,10 +22,11 @@
#include "config_manager.h"
#include "led_manager.h"
#include "attributes.h"
#include "parse_host_connection.h"
#include "error_reporting.h"
#include "versioning.h"

uint16_t DataModelMajorVersion = 0;
uint16_t DataModelMinorVersion = 0;
uint16_t DataModelPatchVersion = 0;
version_t DataModelVersion = {0, 0, 0};

bool PerKeyRgbPresent = false;

Expand All @@ -45,10 +47,10 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
uint16_t keymapCount;
parser_error_t errorCode;

DataModelMajorVersion = ReadUInt16(buffer);
DataModelMinorVersion = ReadUInt16(buffer);
DataModelPatchVersion = ReadUInt16(buffer);
uint32_t userConfigLength = DataModelMajorVersion < 6 ? ReadUInt16(buffer) : ReadUInt32(buffer);
DataModelVersion.major = ReadUInt16(buffer);
DataModelVersion.minor = ReadUInt16(buffer);
DataModelVersion.patch = ReadUInt16(buffer);
uint32_t userConfigLength = DataModelVersion.major < 6 ? ReadUInt16(buffer) : ReadUInt32(buffer);
const char *deviceName = ReadString(buffer, &len);
uint16_t doubleTapSwitchLayerTimeout = ReadUInt16(buffer);

Expand All @@ -61,7 +63,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
uint8_t alphanumericSegmentsBrightness = 0xff;
uint8_t keyBacklightBrightness = 0xff;

if (DataModelMajorVersion < 8) {
if (DataModelVersion.major < 8) {
iconsAndLayerTextsBrightness = ReadUInt8(buffer);
alphanumericSegmentsBrightness = ReadUInt8(buffer);
keyBacklightBrightness = ReadUInt8(buffer);
Expand All @@ -72,8 +74,8 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
backlighting_mode_t backlightingMode = Cfg.BacklightingMode;
rgb_t keyActionColors[keyActionColor_Length];

if (DataModelMajorVersion >= 6) {
if (DataModelMajorVersion < 8) {
if (DataModelVersion.major >= 6) {
if (DataModelVersion.major < 8) {
ledsFadeTimeout = 1000 * ReadUInt16(buffer);
}
PerKeyRgbPresent = ReadBool(buffer);
Expand Down Expand Up @@ -113,6 +115,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
mouseScrollBaseSpeed == 0 ||
mouseScrollAcceleratedSpeed == 0)
{
ConfigParser_Error(buffer, "Invalid mouse kinetic property");
return ParserError_InvalidMouseKineticProperty;
}

Expand All @@ -133,7 +136,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
bool secondaryRoles_AdvancedStrategyDoubletapToPrimary = Cfg.SecondaryRoles_AdvancedStrategyDoubletapToPrimary;
serialized_secondary_role_action_type_t secondaryRoles_AdvancedStrategyTimeoutAction = SerializedSecondaryRoleActionType_Secondary;

if (DataModelMajorVersion >= 7) {
if (DataModelVersion.major >= 7) {
secondaryRoles_Strategy = ReadUInt8(buffer);
secondaryRoles_AdvancedStrategyDoubletapTimeout = ReadUInt16(buffer);
secondaryRoles_AdvancedStrategyTimeout = ReadUInt16(buffer);
Expand Down Expand Up @@ -161,7 +164,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
uint32_t keyBacklightFadeOutTimeout;
uint32_t keyBacklightFadeOutBatteryTimeout;

if (DataModelMajorVersion >= 8) {
if (DataModelVersion.major >= 8) {
displayBrightness = ReadUInt8(buffer);
displayBrightnessBattery = ReadUInt8(buffer);
keyBacklightBrightness = ReadUInt8(buffer);
Expand All @@ -181,11 +184,21 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
keyBacklightFadeOutBatteryTimeout = ledsFadeTimeout;
}

// HostConnection configuration

if (VERSION_AT_LEAST(DataModelVersion, 8, 1, 0)) {
RETURN_ON_ERROR(
ParseHostConnections(buffer);
)
}


// Module configurations

uint16_t moduleConfigurationCount = ReadCompactLength(buffer);

if (moduleConfigurationCount > 255) {
ConfigParser_Error(buffer, "Invalid module configuration count: %u", moduleConfigurationCount);
return ParserError_InvalidModuleConfigurationCount;
}

Expand All @@ -199,6 +212,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)

macroCount = ReadCompactLength(buffer);
if (macroCount > MacroIndex_MaxUserDefinableCount) {
ConfigParser_Error(buffer, "Too many macros: %u", macroCount);
return ParserError_InvalidMacroCount;
}

Expand All @@ -212,6 +226,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
//
keymapCount = ReadCompactLength(buffer);
if (keymapCount == 0 || keymapCount > MAX_KEYMAP_NUM) {
ConfigParser_Error(buffer, "Invalid keymap count: %u", keymapCount);
return ParserError_InvalidKeymapCount;
}

Expand Down Expand Up @@ -253,7 +268,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)

// Version 6

if (DataModelMajorVersion >= 6) {
if (DataModelVersion.major >= 6) {
// removed in version 8
// Cfg.LedsFadeTimeout = ledsFadeTimeout;

Expand All @@ -264,7 +279,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)

// Version 7

if (DataModelMajorVersion >= 7) {
if (DataModelVersion.major >= 7) {
Cfg.SecondaryRoles_Strategy = secondaryRoles_Strategy;
Cfg.SecondaryRoles_AdvancedStrategyDoubletapTimeout = secondaryRoles_AdvancedStrategyDoubletapTimeout;
Cfg.SecondaryRoles_AdvancedStrategyTimeout = secondaryRoles_AdvancedStrategyTimeout;
Expand All @@ -279,6 +294,7 @@ parser_error_t ParseConfig(config_buffer_t *buffer)
Cfg.SecondaryRoles_AdvancedStrategyTimeoutAction = SecondaryRoleState_Secondary;
break;
default:
ConfigParser_Error(buffer, "Invalid secondary role action type: %u", secondaryRoles_AdvancedStrategyTimeoutAction);
return ParserError_InvalidSecondaryRoleActionType;
}

Expand Down
5 changes: 2 additions & 3 deletions right/src/config_parser/parse_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Includes:

#include "basic_types.h"
#include "versioning.h"

// Macros:

Expand Down Expand Up @@ -42,9 +43,7 @@
SerializedSecondaryRoleActionType_Secondary,
} serialized_secondary_role_action_type_t;

extern uint16_t DataModelMajorVersion;
extern uint16_t DataModelMinorVersion;
extern uint16_t DataModelPatchVersion;
extern version_t DataModelVersion;

extern bool PerKeyRgbPresent;

Expand Down
30 changes: 30 additions & 0 deletions right/src/config_parser/parse_host_connection.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "parse_host_connection.h"
#include "config_manager.h"
#include "config_parser/basic_types.h"
#include "config_parser/parse_config.h"
#include "host_connection.h"

static void parseHostConnection(config_buffer_t* buffer, host_connection_t* host_connection) {
host_connection->type = ReadUInt8(buffer);

if (host_connection->type == HostConnectionType_Ble || host_connection->type == HostConnectionType_Dongle) {
for (uint8_t i = 0; i < BLE_ADDRESS_LENGTH; i++) {
host_connection->bleAddress[i] = ReadUInt8(buffer);
}
}

if (host_connection->type != HostConnectionType_Empty) {
uint16_t len;
host_connection->name.start = ReadString(buffer, &len);
host_connection->name.end = host_connection->name.start + len;
}
}

parser_error_t ParseHostConnections(config_buffer_t *buffer) {
for (uint8_t host_connectionId = 0; host_connectionId < HOST_CONNECTION_COUNT_MAX; host_connectionId++) {
host_connection_t dummy;
parseHostConnection(buffer, &dummy);
}

return ParserError_Success;
}
18 changes: 18 additions & 0 deletions right/src/config_parser/parse_host_connection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __HOST_CONNECTIONS_H__
#define __HOST_CONNECTIONS_H__


// Includes:

#include "basic_types.h"
#include "parse_config.h"

// Macros:

// Typedefs:

// Functions:

parser_error_t ParseHostConnections(config_buffer_t *buffer);

#endif
14 changes: 13 additions & 1 deletion right/src/config_parser/parse_keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "slave_protocol.h"
#include "slot.h"
#include "slave_drivers/uhk_module_driver.h"
#include "error_reporting.h"

#ifdef __ZEPHYR__
#include "state_sync.h"
Expand Down Expand Up @@ -59,6 +60,7 @@ static parser_error_t parseKeyStrokeAction(key_action_t *keyAction, uint8_t keyS
keyAction->keystroke.keystrokeType = KeystrokeType_System;
break;
default:
ConfigParser_Error(buffer, "Invalid keystroke type: %d", keystrokeType);
return ParserError_InvalidSerializedKeystrokeType;
}
keyAction->keystroke.scancode = keyStrokeAction & SERIALIZED_KEYSTROKE_TYPE_MASK_HAS_SCANCODE
Expand Down Expand Up @@ -91,6 +93,7 @@ static parser_error_t parseSwitchKeymapAction(key_action_t *keyAction, config_bu
uint8_t keymapIndex = ReadUInt8(buffer);

if (keymapIndex >= tempKeymapCount) {
ConfigParser_Error(buffer, "Invalid keymap index: %d", keymapIndex);
return ParserError_InvalidSerializedSwitchKeymapAction;
}
keyAction->type = KeyActionType_SwitchKeymap;
Expand All @@ -104,6 +107,7 @@ static parser_error_t parsePlayMacroAction(key_action_t *keyAction, config_buffe
uint8_t macroIndex = ReadUInt8(buffer);

if (macroIndex >= tempMacroCount) {
ConfigParser_Error(buffer, "Invalid macro index: %d", macroIndex);
return ParserError_InvalidSerializedPlayMacroAction;
}
keyAction->type = KeyActionType_PlayMacro;
Expand All @@ -118,6 +122,7 @@ static parser_error_t parseMouseAction(key_action_t *keyAction, config_buffer_t

uint8_t mouseAction = ReadUInt8(buffer);
if (mouseAction > SerializedMouseAction_Last) {
ConfigParser_Error(buffer, "Invalid mouse action: %d", mouseAction);
return ParserError_InvalidSerializedMouseAction;
}

Expand Down Expand Up @@ -154,6 +159,8 @@ static parser_error_t parseKeyAction(key_action_t *keyAction, config_buffer_t *b
case SerializedKeyActionType_PlayMacro:
return parsePlayMacroAction(keyAction, buffer);
}

ConfigParser_Error(buffer, "Invalid key action type: %d", keyActionType);
return ParserError_InvalidSerializedKeyActionType;
}

Expand All @@ -163,6 +170,7 @@ static parser_error_t parseKeyActions(uint8_t targetLayer, config_buffer_t *buff
uint16_t actionCount = ReadCompactLength(buffer);

if (actionCount > MAX_KEY_COUNT_PER_MODULE) {
ConfigParser_Error(buffer, "Invalid action count: %d", actionCount);
return ParserError_InvalidActionCount;
}
if (moduleId == ModuleId_LeftKeyboardHalf || moduleId == ModuleId_KeyClusterLeft) {
Expand Down Expand Up @@ -193,7 +201,7 @@ static parser_error_t parseModule(config_buffer_t *buffer, uint8_t layer, parse_

static parser_error_t parseLayerId(config_buffer_t *buffer, uint8_t layer, layer_id_t* parsedLayerId)
{
if(DataModelMajorVersion >= 5) {
if(DataModelVersion.major >= 5) {
uint8_t layerId = ReadUInt8(buffer);
switch(layerId) {
case SerializedLayerName_base:
Expand All @@ -203,6 +211,7 @@ static parser_error_t parseLayerId(config_buffer_t *buffer, uint8_t layer, layer
*parsedLayerId = layerId + 1;
break;
default:
ConfigParser_Error(buffer, "Invalid layer id: %d", layerId);
return ParserError_InvalidLayerId;
}
} else {
Expand Down Expand Up @@ -237,6 +246,7 @@ static parser_error_t parseLayer(config_buffer_t *buffer, uint8_t layer, parse_m
uint16_t moduleCount = ReadCompactLength(buffer);

if (moduleCount > ModuleId_AllCount) {
ConfigParser_Error(buffer, "Invalid module count: %d", moduleCount);
return ParserError_InvalidModuleCount;
}
for (uint8_t moduleIdx = 0; moduleIdx < moduleCount; moduleIdx++) {
Expand Down Expand Up @@ -322,9 +332,11 @@ parser_error_t ParseKeymap(config_buffer_t *buffer, uint8_t keymapIdx, uint8_t k
(void)name;
(void)description;
if (!abbreviationLen || abbreviationLen > 3) {
ConfigParser_Error(buffer, "Invalid abbreviation length: %d", abbreviationLen);
return ParserError_InvalidAbbreviationLen;
}
if (layerCount > LayerId_Count) {
ConfigParser_Error(buffer, "Invalid layer count: %d", layerCount);
return ParserError_InvalidLayerCount;
}
if (parseConfig.mode == ParseKeymapMode_FullRun) {
Expand Down
2 changes: 2 additions & 0 deletions right/src/config_parser/parse_macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "str_utils.h"
#include "macros/core.h"
#include "macros/status_buffer.h"
#include "error_reporting.h"

parser_error_t parseKeyMacroAction(config_buffer_t *buffer, macro_action_t *macroAction, serialized_macro_action_type_t macroActionType)
{
Expand Down Expand Up @@ -117,6 +118,7 @@ parser_error_t ParseMacroAction(config_buffer_t *buffer, macro_action_t *macroAc
case SerializedMacroActionType_CommandMacroAction:
return parseCommandMacroAction(buffer, macroAction);
}
ConfigParser_Error(buffer, "Invalid macro action type: %d", macroActionType);
return ParserError_InvalidSerializedMacroActionType;
}

Expand Down
Loading

0 comments on commit ea00210

Please sign in to comment.