Skip to content

Commit

Permalink
Initial implementation for reg_init_from_fwpkg()
Browse files Browse the repository at this point in the history
  • Loading branch information
matwey committed Aug 9, 2019
1 parent d590e81 commit abe83a2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#ifndef _REG_H
#define _REG_H

#include <fwpkg.h>

#include <stdint.h>

enum reg_name {
Expand All @@ -21,5 +23,9 @@ struct reg {
};

int reg_init(struct reg* reg, char* map);
int reg_init_from_fwpkg(struct reg* reg, struct fwpkg* fwpkg);
int reg_init_from_reg(struct reg* reg, struct reg* other);

const char* reg_get_error_string(struct reg* reg);

#endif // _REG_H
49 changes: 49 additions & 0 deletions src/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,52 @@ int reg_init(struct reg* reg, char* map) {

return reg_init_from_map(reg, map);
}

int reg_init_from_fwpkg(struct reg* reg, struct fwpkg* fwpkg) {
size_t size = 0;
char* tmp = NULL;
int ret = 0;

size = fwpkg_map_size(fwpkg)+1;
tmp = malloc(size);
if (!tmp) {
reg->error_str = "Cannot allocate memory for register map";
goto fail_malloc;
}

ret = fwpkg_read_map(fwpkg, tmp, &size);
if (ret < 0) {
reg->error_str = fwpkg_get_error_string(fwpkg);
goto fail_fwpkg_read_map;
}
tmp[size] = '\0';

ret = reg_init(reg, tmp);
if (ret < 0) {
goto fail_reg_init;
}

free(tmp);
tmp = NULL;

return 0;

fail_reg_init:
fail_fwpkg_read_map:
if (tmp) {
free(tmp);
}
fail_malloc:

return -1;
}

int reg_init_from_reg(struct reg* reg, struct reg* other) {
memmove(reg, other, sizeof(struct reg));

return 0;
}

const char* reg_get_error_string(struct reg* reg) {
return reg->error_str;
}
25 changes: 25 additions & 0 deletions test/reg.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ START_TEST (test_reg_from_map2) {
}
END_TEST

START_TEST (test_reg_from_fwpkg1) {
struct reg reg;
ck_assert_int_eq(reg_init_from_fwpkg(&reg, &fwpkg), 0);
ck_assert_int_eq(reg.addr[CSTREAM_CFG], 0x800);
ck_assert_int_eq(reg.addr[CSTREAM_CONS_LO], 0x801);
ck_assert_int_eq(reg.addr[CSTREAM_CONS_HI], 0x802);
}
END_TEST

START_TEST (test_reg_from_reg1) {
struct reg reg;
struct reg reg2;
ck_assert_int_eq(reg_init_from_fwpkg(&reg, &fwpkg), 0);
ck_assert_int_eq(reg.addr[CSTREAM_CFG], 0x800);
ck_assert_int_eq(reg.addr[CSTREAM_CONS_LO], 0x801);
ck_assert_int_eq(reg.addr[CSTREAM_CONS_HI], 0x802);
ck_assert_int_eq(reg_init_from_reg(&reg2, &reg), 0);
ck_assert_int_eq(reg2.addr[CSTREAM_CFG], 0x800);
ck_assert_int_eq(reg2.addr[CSTREAM_CONS_LO], 0x801);
ck_assert_int_eq(reg2.addr[CSTREAM_CONS_HI], 0x802);
}
END_TEST

Suite* range_suite(void) {
Suite *s;
TCase *tc_core;
Expand All @@ -55,6 +78,8 @@ Suite* range_suite(void) {
tcase_add_unchecked_fixture(tc_core, setup, teardown);
tcase_add_test(tc_core, test_reg_from_map1);
tcase_add_test(tc_core, test_reg_from_map2);
tcase_add_test(tc_core, test_reg_from_fwpkg1);
tcase_add_test(tc_core, test_reg_from_reg1);
suite_add_tcase(s, tc_core);

return s;
Expand Down

0 comments on commit abe83a2

Please sign in to comment.