-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial implementation for register map parser
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#ifndef _REG_H | ||
#define _REG_H | ||
|
||
#include <stdint.h> | ||
|
||
struct reg { | ||
const char* error_str; | ||
uint16_t leds_out; | ||
}; | ||
|
||
int reg_init(struct reg* reg); | ||
int reg_from_map(struct reg* reg, char* map); | ||
int reg_validate(struct reg* reg); | ||
|
||
#endif // _REG_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <reg.h> | ||
|
||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
|
||
static char* x_strchr(const char *s, int c) { | ||
char* n = NULL; | ||
|
||
if ((n = strchr(s, c))) { | ||
*(n++) = '\0'; | ||
return n; | ||
} | ||
|
||
return n; | ||
} | ||
|
||
static int reg_from_pair(struct reg* reg, char* key, unsigned long int value) { | ||
|
||
assert(value > 0); | ||
assert(value <= 0xFFFF); | ||
|
||
reg->leds_out = value; | ||
|
||
return 0; | ||
} | ||
|
||
static int reg_from_line(struct reg* reg, char* line) { | ||
char* key = line; | ||
char* value = NULL; | ||
|
||
if (line[0] == '\0' || line[0] == '#') | ||
return 0; | ||
|
||
if (!(value = strchr(line, '='))) | ||
return -1; | ||
*(value++) = '\0'; | ||
|
||
return reg_from_pair(reg, key, strtoul(value, NULL, 16)); | ||
} | ||
|
||
int reg_init(struct reg* reg) { | ||
memset(reg, 0, sizeof(struct reg)); | ||
|
||
return 0; | ||
} | ||
|
||
int reg_from_map(struct reg* reg, char* map) { | ||
char* c = map; | ||
char* n = NULL; | ||
|
||
do { | ||
n = x_strchr(c, '\n'); | ||
if (reg_from_line(reg, c)) { | ||
reg->error_str = "Can not parse register map"; | ||
|
||
return -1; | ||
} | ||
c = n; | ||
} while (n); | ||
|
||
return 0; | ||
} | ||
|
||
int reg_validate(struct reg* reg) { | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <check.h> | ||
#include <stdlib.h> | ||
|
||
#include <reg.h> | ||
|
||
START_TEST (test_reg_init1) { | ||
struct reg reg; | ||
ck_assert_int_eq(reg_init(®), 0); | ||
} | ||
END_TEST | ||
START_TEST (test_reg_from_map1) { | ||
char x[] = "LEDS_OUT = 0x42"; | ||
struct reg reg; | ||
ck_assert_int_eq(reg_init(®), 0); | ||
ck_assert_int_eq(reg_from_map(®, x), 0); | ||
ck_assert_int_eq(reg.leds_out, 0x42); | ||
} | ||
END_TEST | ||
|
||
Suite* range_suite(void) { | ||
Suite *s; | ||
TCase *tc_core; | ||
|
||
s = suite_create("reg"); | ||
|
||
tc_core = tcase_create("Core"); | ||
|
||
tcase_add_test(tc_core, test_reg_init1); | ||
tcase_add_test(tc_core, test_reg_from_map1); | ||
suite_add_tcase(s, tc_core); | ||
|
||
return s; | ||
} | ||
|
||
int main(void) { | ||
int number_failed; | ||
Suite *s; | ||
SRunner *sr; | ||
|
||
s = range_suite(); | ||
sr = srunner_create(s); | ||
|
||
srunner_run_all(sr, CK_NORMAL); | ||
number_failed = srunner_ntests_failed(sr); | ||
srunner_free(sr); | ||
return (number_failed == 0) ? 0 : 1; | ||
} | ||
|