Skip to content

Commit

Permalink
#379 Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
krichardsson committed Nov 14, 2018
1 parent cf868ac commit 7fb1ecb
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 37 deletions.
30 changes: 30 additions & 0 deletions src/platform/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <string.h>
#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;
Expand All @@ -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;
}

7 changes: 7 additions & 0 deletions src/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,20 @@ 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.
*/
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);

Expand Down
39 changes: 2 additions & 37 deletions src/platform/platform_cf2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -118,11 +91,3 @@ const char* platformConfigGetPlatformName() {
return "cf2";
}

const char* platformConfigGetDeviceTypeName() {
return active_config->deviceTypeName;
}

SensorImplementation_t platformConfigGetSensorImplementation() {
return active_config->sensorImplementation;
}

65 changes: 65 additions & 0 deletions test/platform/Test_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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",
}
};

0 comments on commit 7fb1ecb

Please sign in to comment.