Commit 082ca161 authored by Lucas Russo's avatar Lucas Russo

hal/ll_io/ll_io_utils.*: add ethernet devices and generic type functions

Now, we can reuse these functions for similar string to enum
conversions
parent a1c0c2dc
...@@ -35,18 +35,17 @@ ...@@ -35,18 +35,17 @@
CHECK_HAL_ERR(err, LL_IO, "[ll_io:utils]", \ CHECK_HAL_ERR(err, LL_IO, "[ll_io:utils]", \
llio_err_str (err_type)) llio_err_str (err_type))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
/************** Utility functions ****************/ /************** Utility functions ****************/
const llio_types_t llio_types [LLIO_TYPE_NUM] ={ const llio_types_t llio_dev_types_map [] ={
{.name = GENERIC_DEV_STR, .type = GENERIC_DEV}, {.name = GENERIC_DEV_STR, .type = GENERIC_DEV},
{.name = PCIE_DEV_STR, .type = PCIE_DEV}, {.name = PCIE_DEV_STR, .type = PCIE_DEV},
{.name = ETH_DEV_STR, .type = ETH_DEV}, {.name = ETH_DEV_STR, .type = ETH_DEV},
{.name = INVALID_DEV_STR, .type = INVALID_DEV} {.name = INVALID_DEV_STR, .type = INVALID_DEV},
{.name = LLIO_TYPE_NAME_END, .type = LLIO_TYPE_END} /* End marker */
}; };
char *llio_type_to_str (llio_type_e type) char *llio_gen_type_to_str (int type, const llio_types_t *llio_types)
{ {
/* Should be large enough for all possible debug levels */ /* Should be large enough for all possible debug levels */
int size = 16; int size = 16;
...@@ -55,7 +54,7 @@ char *llio_type_to_str (llio_type_e type) ...@@ -55,7 +54,7 @@ char *llio_type_to_str (llio_type_e type)
/* Do a simple linear search to look for matching IDs */ /* Do a simple linear search to look for matching IDs */
uint32_t i; uint32_t i;
for (i = 0; i < ARRAY_SIZE(llio_types); ++i) { for (i = 0; llio_types [i].type != LLIO_TYPE_END; ++i) {
if (type == llio_types [i].type) { if (type == llio_types [i].type) {
const char *type_str = llio_types [i].name; const char *type_str = llio_types [i].name;
strncpy (str, type_str, size-1); strncpy (str, type_str, size-1);
...@@ -65,18 +64,29 @@ char *llio_type_to_str (llio_type_e type) ...@@ -65,18 +64,29 @@ char *llio_type_to_str (llio_type_e type)
} }
} }
/* No device found */
if (llio_types [i].type == LLIO_TYPE_END) {
free (str);
str = NULL;
}
err_alloc_str: err_alloc_str:
return str; return str;
} }
llio_type_e llio_str_to_type (const char *type_str) char *llio_type_to_str (llio_type_e type)
{
return llio_gen_type_to_str (type, llio_dev_types_map);
}
int llio_str_to_gen_type (const char *type_str, const llio_types_t *llio_types)
{ {
assert (type_str); assert (type_str);
llio_type_e type = INVALID_DEV; int type = LLIO_TYPE_END; /* Not found */
/* Do a simple linear search to look for matching names */ /* Do a simple linear search to look for matching names */
uint32_t i; uint32_t i;
for (i = 0; i < ARRAY_SIZE(llio_types); ++i) { for (i = 0; llio_types [i].type != LLIO_TYPE_END; ++i) {
if (streq (type_str, llio_types [i].name)) { if (streq (type_str, llio_types [i].name)) {
type = llio_types [i].type; type = llio_types [i].type;
break; break;
...@@ -85,3 +95,11 @@ llio_type_e llio_str_to_type (const char *type_str) ...@@ -85,3 +95,11 @@ llio_type_e llio_str_to_type (const char *type_str)
return type; return type;
} }
llio_type_e llio_str_to_type (const char *type_str)
{
llio_type_e ret = llio_str_to_gen_type (type_str, llio_dev_types_map);
return (ret == LLIO_TYPE_END)? INVALID_DEV : ret;
}
...@@ -8,6 +8,11 @@ ...@@ -8,6 +8,11 @@
#ifndef _LL_IO_UTILS_H_ #ifndef _LL_IO_UTILS_H_
#define _LL_IO_UTILS_H_ #define _LL_IO_UTILS_H_
#include <inttypes.h>
#define LLIO_TYPE_END 0xFFFF
#define LLIO_TYPE_NAME_END ""
/* Device type */ /* Device type */
enum _llio_type_e { enum _llio_type_e {
GENERIC_DEV = 0, GENERIC_DEV = 0,
...@@ -19,16 +24,12 @@ enum _llio_type_e { ...@@ -19,16 +24,12 @@ enum _llio_type_e {
typedef enum _llio_type_e llio_type_e; typedef enum _llio_type_e llio_type_e;
struct _llio_types_t { struct _llio_types_t {
llio_type_e type; int type;
char *name; char *name;
}; };
typedef struct _llio_types_t llio_types_t; typedef struct _llio_types_t llio_types_t;
/* FIXME: This is a hardcoded value that refers to an enum
that can grow arbitrarily */
#define LLIO_TYPE_NUM 4
#define GENERIC_DEV_STR "generic" #define GENERIC_DEV_STR "generic"
#define PCIE_DEV_STR "pcie" #define PCIE_DEV_STR "pcie"
#define ETH_DEV_STR "eth" #define ETH_DEV_STR "eth"
...@@ -36,6 +37,15 @@ typedef struct _llio_types_t llio_types_t; ...@@ -36,6 +37,15 @@ typedef struct _llio_types_t llio_types_t;
/************** Utility functions ****************/ /************** Utility functions ****************/
/* Generic mapping functions */
/* Converts the LLIO type ID into a string */
char *llio_gen_type_to_str (int type, const llio_types_t *llio_types);
/* Converts a string to an LLIO type ID. If no match if found,
* a INVALID_DEV is returned */
int llio_str_to_gen_type (const char *type_str,
const llio_types_t *llio_types);
/* Converts the llio_type enumeration into a string */ /* Converts the llio_type enumeration into a string */
char *llio_type_to_str (llio_type_e type); char *llio_type_to_str (llio_type_e type);
/* Converts a string to the llio_type enumeration. If no match if found, /* Converts a string to the llio_type enumeration. If no match if found,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment