diff --git a/src/platform/platform.c b/src/platform/platform.c index 5b2f5167b6..b75749664f 100644 --- a/src/platform/platform.c +++ b/src/platform/platform.c @@ -28,6 +28,9 @@ #include #include "platform.h" +static const platformConfig_t* active_config = 0; + + int platformParseDeviceTypeString(const char* deviceTypeString, char* deviceType) { if (deviceTypeString[0] != '0' || deviceTypeString[1] != ';') { return 1; @@ -51,3 +54,30 @@ int platformParseDeviceTypeString(const char* deviceTypeString, char* deviceType deviceType[length] = '\0'; return 0; } + +int platformInitConfiguration(const platformConfig_t* configs, const int nrOfConfigs) { + char deviceTypeString[PLATFORM_DEVICE_TYPE_STRING_MAX_LEN]; + char deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN]; + + platformGetDeviceTypeString(deviceTypeString); + platformParseDeviceTypeString(deviceTypeString, deviceType); + + for (int i = 0; i < nrOfConfigs; i++) { + const platformConfig_t* config = &configs[i]; + if (strcmp(config->deviceType, deviceType) == 0) { + active_config = config; + return 0; + } + } + + return 1; +} + +const char* platformConfigGetDeviceTypeName() { + return active_config->deviceTypeName; +} + +SensorImplementation_t platformConfigGetSensorImplementation() { + return active_config->sensorImplementation; +} + diff --git a/src/platform/platform.h b/src/platform/platform.h index e495e92f8f..a5bbc2d3a2 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -51,6 +51,12 @@ typedef enum { SensorImplementation_COUNT, } SensorImplementation_t; +typedef struct { + char deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN]; + char deviceTypeName[14]; + SensorImplementation_t sensorImplementation; +} platformConfig_t; + /** * Initilizes all platform specific things. */ @@ -58,6 +64,7 @@ int platformInit(void); void platformGetDeviceTypeString(char* deviceTypeString); int platformParseDeviceTypeString(const char* deviceTypeString, char* deviceType); +int platformInitConfiguration(const platformConfig_t* configs, const int nrOfConfigs); void platformSetLowInterferenceRadioMode(void); diff --git a/src/platform/platform_cf2.c b/src/platform/platform_cf2.c index 01bccfe37f..1b34f73bfb 100644 --- a/src/platform/platform_cf2.c +++ b/src/platform/platform_cf2.c @@ -39,17 +39,9 @@ #define PLATFORM_NRF51_LOW_INTERFERENCE_TX_POWER_DBM (-12) #endif -static int initPlatformConfiguration(); static void initHardware(); - -typedef struct { - char deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN]; - char deviceTypeName[14]; - SensorImplementation_t sensorImplementation; -} config_t; - -static config_t configs[] = { +static platformConfig_t configs[] = { { .deviceType = "CF20", .deviceTypeName = "Crazyflie 2.0", @@ -62,11 +54,10 @@ static config_t configs[] = { } }; -static config_t* active_config = 0; int platformInit(void) { - int err = initPlatformConfiguration(); + int err = platformInitConfiguration(configs, sizeof(configs) / sizeof(platformConfig_t)); if (err != 0) { // This firmware is not compatible, abort init @@ -85,24 +76,6 @@ void platformSetLowInterferenceRadioMode(void) { } -static int initPlatformConfiguration() { - char deviceTypeString[PLATFORM_DEVICE_TYPE_STRING_MAX_LEN]; - char deviceType[PLATFORM_DEVICE_TYPE_MAX_LEN]; - - platformGetDeviceTypeString(deviceTypeString); - platformParseDeviceTypeString(deviceTypeString, deviceType); - - for (int i = 0; i < (sizeof(configs) / sizeof(config_t)); i++) { - config_t* config = &configs[i]; - if (strcmp(config->deviceType, deviceType) == 0) { - active_config = config; - return 0; - } - } - - return 1; -} - static void initHardware() { //Low level init: Clock and Interrupt controller nvicInit(); @@ -118,11 +91,3 @@ const char* platformConfigGetPlatformName() { return "cf2"; } -const char* platformConfigGetDeviceTypeName() { - return active_config->deviceTypeName; -} - -SensorImplementation_t platformConfigGetSensorImplementation() { - return active_config->sensorImplementation; -} - diff --git a/test/platform/Test_platform.c b/test/platform/Test_platform.c index c6a17065d6..a9ad34b586 100644 --- a/test/platform/Test_platform.c +++ b/test/platform/Test_platform.c @@ -7,8 +7,18 @@ static char actualDeviceType[100]; + +// Mock implementation +static char* deviceTypeStringToReturn = ""; +void platformGetDeviceTypeString(char* deviceTypeString) { + strcpy(deviceTypeString, deviceTypeStringToReturn); +} + +static platformConfig_t fixtureConfig[]; + void setUp(void) { strcpy(actualDeviceType, "abcdefghijklmn"); + deviceTypeStringToReturn = ""; } void tearDown(void) { @@ -83,3 +93,58 @@ void testThatDeviceTypeIsNotReturnedIfSecondCharIsNotSemicolon() { // Assert TEST_ASSERT_NOT_EQUAL(0, actual); } + +void testThatFirstMatchingPlatformConfigIsReturned() { + // Fixture + deviceTypeStringToReturn = "0;ASD"; + + // Test + int actual = platformInitConfiguration(fixtureConfig, 3); + + // Assert + const char* actualName = platformConfigGetDeviceTypeName(); + + TEST_ASSERT_EQUAL(0, actual); + TEST_ASSERT_EQUAL_STRING("Second", actualName); +} + +void testThatErrorIsReturnedWhenDeviceIsNotInConfig() { + // Fixture + deviceTypeStringToReturn = "0;WRNG"; + + // Test + int actual = platformInitConfiguration(fixtureConfig, 3); + + // Assert + TEST_ASSERT_EQUAL(1, actual); +} + +void testThatItIsNotSearchingOutsideListOfPlatformConfigs() { + // Fixture + deviceTypeStringToReturn = "0;ZXC"; + + // Test + int actual = platformInitConfiguration(fixtureConfig, 2); + + // Assert + TEST_ASSERT_EQUAL(1, actual); +} + + +// Fixtures ------------------------- + + +static platformConfig_t fixtureConfig[] = { + { + .deviceType = "QWE", + .deviceTypeName = "First", + }, + { + .deviceType = "ASD", + .deviceTypeName = "Second", + }, + { + .deviceType = "ZXC", + .deviceTypeName = "Third", + } +};