summaryrefslogtreecommitdiff
path: root/cesar/lib
diff options
context:
space:
mode:
authordufour2010-02-12 15:51:39 +0000
committerdufour2010-02-12 15:51:39 +0000
commit6baca2a6547ddbfd5c32d852cc4cc61dd1b61bd7 (patch)
tree2d13ec5ad8083b20398bc7d1f040c324b1ae6c2b /cesar/lib
parented9c3dab7f687e5eca51436752a6db7b6ba28782 (diff)
cesar/lib/stats: add a function to read a config, refs #1240
git-svn-id: svn+ssh://pessac/svn/cesar/trunk@6718 017c9cb6-072f-447c-8318-d5b54f68fe89
Diffstat (limited to 'cesar/lib')
-rw-r--r--cesar/lib/src/stats.c42
-rw-r--r--cesar/lib/stats.h13
-rw-r--r--cesar/lib/test/stats/src/test_stats.c176
3 files changed, 164 insertions, 67 deletions
diff --git a/cesar/lib/src/stats.c b/cesar/lib/src/stats.c
index f7d34e9cba..fcb7c599ef 100644
--- a/cesar/lib/src/stats.c
+++ b/cesar/lib/src/stats.c
@@ -16,6 +16,8 @@
#include "stats.h"
#include "slab.h"
+#include "common/defs/homeplugAV.h"
+
#include <string.h>
#include <stdlib.h>
#include <errno.h>
@@ -454,6 +456,46 @@ lib_stats_write_stat (const char *name, const char *value)
return false;
}
+bool
+lib_stats_write_stats (const char *stats)
+{
+ /* Check parameter. */
+ dbg_assert (stats);
+ dbg_assert (strlen (stats) < HPAV_MME_PAYLOAD_MAX_SIZE);
+
+ const char delim[] = " ";
+ char *stat;
+ char copy_stats[HPAV_MME_PAYLOAD_MAX_SIZE];
+
+ /* Copy stats. */
+ strcpy (copy_stats, stats);
+ /* For each "name:value". */
+ stat = strtok (copy_stats, delim);
+ if (stat)
+ {
+ do {
+ /* Extract name and value. */
+ char *name = stat;
+ char *value = strchr (name, ':');
+ /* If found and only one. */
+ if (value && (strchr (value + 1, ':') == NULL))
+ {
+ /* Split. */
+ *value = '\0';
+ value++;
+ /* Write statistic. */
+ lib_stats_write_stat (name, value);
+ }
+ else
+ /* Error, stop here. */
+ return false;
+ } while ((stat = strtok (NULL, delim)));
+ }
+ else
+ return false;
+ return true;
+}
+
void
lib_stats_set_stat_value (const char *name, void *value,
lib_stats_access_t mode,
diff --git a/cesar/lib/stats.h b/cesar/lib/stats.h
index 6d97cda66c..b36c9d7724 100644
--- a/cesar/lib/stats.h
+++ b/cesar/lib/stats.h
@@ -109,6 +109,18 @@ bool
lib_stats_write_stat (const char *name, const char *value);
/**
+ * Write to many statistics.
+ * \param stats string containing many statistics to write/configure
+ * \return true if the string is correctly formated and all statistics has
+ * been correctly written. False otherwise
+ *
+ * stats is in the format of the config field, DRV_STA_SET_CONFIG:
+ * "name_1:value name_2:value". The string must end by a '\0'.
+ */
+bool
+lib_stats_write_stats (const char *stats);
+
+/**
* Add a new stat to the stat book using pointer to value.
* \param name string to be displayed before the stat value (no copy of the
* name is done)
@@ -158,6 +170,7 @@ lib_stats_set_stat_callback (const char *name, lib_stats_cb_r_t callback_read,
#define lib_stats_set_stat_callback(name, callback, type) ((void) 0)
#define lib_stats_get_page(bitstream, page) ((void) 0)
#define lib_stats_write_stat(name, value) ((void) 0)
+#define lib_stats_write_stats(stats) ((void) 0)
#endif /* !CONFIG_STATS */
diff --git a/cesar/lib/test/stats/src/test_stats.c b/cesar/lib/test/stats/src/test_stats.c
index 69f08c49df..d16ea66bd7 100644
--- a/cesar/lib/test/stats/src/test_stats.c
+++ b/cesar/lib/test/stats/src/test_stats.c
@@ -383,66 +383,61 @@ stats_access_mode_test_case (test_t t)
lib_stats_uninit ();
} test_end;
- test_begin (t, "write to a statistic")
- {
- /* Initialize statistics module. */
- lib_stats_init ();
-
- uint i, j;
- /* Set some default values. */
- value_u8 = 0x01;
- value_u16 = 0x0123;
- value_u32 = 0x01234567l;
- value_u64 = 0x0123456789ABCDEFll;
-
- char name[3 * 4][20];
- char mode[3][20] = { "read", "write", "read_write" };
- const struct my_type_t some_types[] = {
- { &value_u8, 1, "v", NULL },
- { &value_u16, 2, "v", NULL },
- { &value_u32, 4, "v", NULL },
- { &value_u64, 8, "v", NULL },
- { &callback_u8, 1, "c", &callback_w_u8 },
- { &callback_u16, 2, "c", &callback_w_u16 },
- { &callback_u32, 4, "c", &callback_w_u32 },
- { &callback_u64, 8, "c", &callback_w_u64 },
- };
+ /* Initialize statistics module. */
+ lib_stats_init ();
- /* Add some statistics. */
- for (i = 0; i < 3; i++)
+ uint i, j;
+
+ char mode[3][20] = { "read", "write", "read_write" };
+ const struct my_type_t some_types[] = {
+ { &value_u8, 1, "v", NULL },
+ { &value_u16, 2, "v", NULL },
+ { &value_u32, 4, "v", NULL },
+ { &value_u64, 8, "v", NULL },
+ { &callback_u8, 1, "c", &callback_w_u8 },
+ { &callback_u16, 2, "c", &callback_w_u16 },
+ { &callback_u32, 4, "c", &callback_w_u32 },
+ { &callback_u64, 8, "c", &callback_w_u64 },
+ };
+ char name[3 * COUNT (some_types)][20];
+
+ /* Add some statistics. */
+ for (i = 0; i < 3; i++)
+ {
+ for (j = 0; j < COUNT (some_types); j++)
{
- for (j = 0; j < COUNT (some_types); j++)
+ u8 pos = i * COUNT (some_types) + j;
+ snprintf (name[pos], COUNT (name[pos]), "%s_%s_%1d",
+ mode[i], some_types[j].format, some_types[j].size);
+ if (some_types[j].format[0] == 'v')
+ lib_stats_set_stat_value (name[pos], some_types[j].ptr,
+ i + 1, some_types[j].size);
+ else
{
- u8 pos = i * COUNT (some_types) + j;
- snprintf (name[pos], COUNT (name[pos]), "%s_%s_%1d",
- mode[i], some_types[j].format, some_types[j].size);
- if (some_types[j].format[0] == 'v')
- lib_stats_set_stat_value (name[pos], some_types[j].ptr,
- i + 1, some_types[j].size);
- else
+ void *cr = NULL, *cw = NULL;
+ switch (i + 1)
{
- void *cr = NULL, *cw = NULL;
- switch (i + 1)
- {
- case LIB_STATS_ACCESS_READ_ONLY:
- cr = some_types[j].ptr;
- break;
- case LIB_STATS_ACCESS_WRITE_ONLY:
- cw = some_types[j].w_ptr;
- break;
- case LIB_STATS_ACCESS_READ_WRITE:
- cr = some_types[j].ptr;
- cw = some_types[j].w_ptr;
- break;
- default:
- dbg_assert_default ();
- }
- lib_stats_set_stat_callback (name[pos], cr, cw,
- some_types[j].size);
+ case LIB_STATS_ACCESS_READ_ONLY:
+ cr = some_types[j].ptr;
+ break;
+ case LIB_STATS_ACCESS_WRITE_ONLY:
+ cw = some_types[j].w_ptr;
+ break;
+ case LIB_STATS_ACCESS_READ_WRITE:
+ cr = some_types[j].ptr;
+ cw = some_types[j].w_ptr;
+ break;
+ default:
+ dbg_assert_default ();
}
+ lib_stats_set_stat_callback (name[pos], cr, cw,
+ some_types[j].size);
}
}
+ }
+ test_begin (t, "write to a statistic")
+ {
/* Set some default values. */
value_u8 = 0x01;
value_u16 = 0x0123;
@@ -496,44 +491,91 @@ stats_access_mode_test_case (test_t t)
test_fail_if (lib_stats_write_stat ("read_c_2", "0x42") != false);
test_fail_if (value_u16 != 0x2424);
test_fail_if (lib_stats_write_stat ("read_c_4", "0x42") != false);
- test_fail_if (value_u32 != 0x24242424l);
+ test_fail_if (value_u32 != 0x24242424ul);
test_fail_if (lib_stats_write_stat ("read_c_8", "0x42") != false);
- test_fail_if (value_u64 != 0x2424242424242424ll);
+ test_fail_if (value_u64 != 0x2424242424242424ull);
/* Callback write only. */
test_fail_if (lib_stats_write_stat ("write_c_1", "0x42") != true);
test_fail_if (value_u8 != 0x42);
test_fail_if (lib_stats_write_stat ("write_c_2", "0x4242") != true);
test_fail_if (value_u16 != 0x4242);
- test_fail_if (lib_stats_write_stat ("write_c_4", "0x42424242l")
+ test_fail_if (lib_stats_write_stat ("write_c_4", "0x42424242")
!= true);
- test_fail_if (value_u32 != 0x42424242l);
+ test_fail_if (value_u32 != 0x42424242ul);
test_fail_if (lib_stats_write_stat ("write_c_8",
- "0x4242424242424242ll") != true);
- test_fail_if (value_u64 != 0x4242424242424242ll);
+ "0x4242424242424242") != true);
+ test_fail_if (value_u64 != 0x4242424242424242ull);
/* Callback read/write. */
- test_fail_if (lib_stats_write_stat ("read_write_c_1", "0x24")
+ test_fail_if (lib_stats_write_stat ("read_write_c_1", "0x98")
!= true);
- test_fail_if (value_u8 != 0x24);
- test_fail_if (lib_stats_write_stat ("read_write_c_2", "0x2424")
+ test_fail_if (value_u8 != 0x98);
+ test_fail_if (lib_stats_write_stat ("read_write_c_2", "0x0123")
!= true);
- test_fail_if (value_u16 != 0x2424);
- test_fail_if (lib_stats_write_stat ("read_write_c_4", "0x24242424l")
+ test_fail_if (value_u16 != 0x0123);
+ test_fail_if (lib_stats_write_stat ("read_write_c_4", "0x01234567")
!= true);
- test_fail_if (value_u32 != 0x24242424l);
+ test_fail_if (value_u32 != 0x01234567ul);
test_fail_if (lib_stats_write_stat ("read_write_c_8",
- "0x2424242424242424ll") != true);
- test_fail_if (value_u64 != 0x2424242424242424ll);
+ "0x0123456789ABCDEF") != true);
+ test_fail_if (value_u64 != 0x123456789ABCDEFull);
/* Overflow. */
test_fail_if (lib_stats_write_stat ("read_write_c_1", "0x4242")
!= false);
+ test_fail_if (value_u8 != 0x98);
test_fail_if (lib_stats_write_stat ("read_write_v_1", "0x4242")
!= false);
+ test_fail_if (value_u8 != 0x98);
+
+ } test_end;
+
+ /* Write to many statistics. */
+ test_begin (t, "write to many statistics")
+ {
+ /* Set some default values. */
+ value_u8 = 0x01;
+ value_u16 = 0x0123;
+ value_u32 = 0x01234567ul;
+ value_u64 = 0x0123456789ABCDEFull;
+
+ /* Normal behavior. */
+ char normals[] = "read_write_c_1:0x42 "
+ "read_write_v_8:0x4242424242424242";
+
+ test_fail_if (lib_stats_write_stats (normals) != true);
+ test_fail_if (value_u8 != 0x42);
+ test_fail_if (value_u64 != 0x4242424242424242ull);
+
+ char normal[] = "read_write_v_1:0x24";
+
+ test_fail_if (lib_stats_write_stats (normal) != true);
+ test_fail_if (value_u8 != 0x24);
+
+ /* Empty string. */
+ char empty[] = "";
+
+ test_fail_if (lib_stats_write_stats (empty) != false);
+
+ /* Mal formated. */
+ char no_sep[] = "read_write_c_10x42";
+ test_fail_if (lib_stats_write_stats (no_sep) != false);
+
+ /* One good, one bad, one good. */
+ char multiple[] = "read_write_c_1:0x42 "
+ "read_write_v_80x2424242424242424 "
+ "read_write_v_4:0x42424242";
+
+ test_fail_if (lib_stats_write_stats (multiple) != false);
+ test_fail_if (value_u8 != 0x42);
+ test_fail_if (value_u64 != 0x4242424242424242ull);
+ test_fail_if (value_u32 != 0x01234567ul);
- lib_stats_uninit ();
} test_end;
+
+ /* Clean. */
+ lib_stats_uninit ();
}
void