Advamation C Library

Version: 2014-03-06
Status: beta
Author: Advamation, Roland Koebler (rk@advamation.de)
Copyright: (c) Advamation (info@advamation.de)
License:open-source / MIT
RCS:advamation.c,v 1.12 2014/03/06 13:43:23 rk Exp

Generic functionality, used in various Advamation software.



1   logging

1.1   configure logging

All messages up to "level" are printed and/or stored in the logfile. DEBUG/INFO are printed to stdout, MESSAGE/WARNING/CRITICAL/ERROR to stderr.

The available levels are:

  • AM_LOG_LEVEL_DEBUG
  • AM_LOG_LEVEL_INFO
  • AM_LOG_LEVEL_MESSAGE
  • AM_LOG_LEVEL_WARNING
  • AM_LOG_LEVEL_CRITICAL
  • AM_LOG_LEVEL_ERROR
Usage:

AM_ERROR am_logging(char const *log_domain, FILE **f, enum am_log_level level, char const *logfile, enum am_log_level logfile_level)

Parameters:
  • log_domain: configure logging for this domain (see G_LOG_DOMAIN in GLib-documentation)
  • f: output pointer for logfile (so different logfiles can be used)
  • level: log-level for "print"
  • logfile: filename of the logfile (NULL: no logfile)
  • logfile_level: log-level for the logfile
Returns:
0 on success,
<0 on error:
AM_ERROR_INVALID_ARGUMENT,
AM_ERROR_FILE_OPEN (see errno for details)
Example:
static FILE *f_log = NULL;
am_logging(G_LOG_DOMAIN, &f_log, level, logfile, logfile_level);

2   misc

2.1   CRC: SMBus PEC

Calculate CRC/PEC according to SMBus.

Usage:

uint8_t am_crc_smbus(uint8_t crc, uint8_t byte)

Parameters:
  • crc: current CRC (or 0)
  • byte: data-byte
Returns:

new crc

3   map integers to strings (and vice-versa)

e.g. for mapping error-codes to error-messages, or for mapping user-given-strings to ids.

3.1   lookup integer -> string

Get string for a given integer.

Usage:

const char* am_int2str_lookup(const struct am_int2str *map, int32_t key, bool or_enable)

Parameters:
  • map: mapping, must end with {0, NULL} !
  • key: integer to convert
  • or_enable: If TRUE and no entry with the key is found, matching the key to a or'd combination of some entries is tried, and the remaining integer is appended.
Returns:

the string/or-combinated string, or NULL

Note:

A returned "or-combinated" string is truncated to 255 bytes (with the last 2 characters being ".."), and will be overwritten by the next call!

Example:

simple example:

const struct am_int2str mapping[] = {
    {MY_VAL01, "MY_VAL1"},
    {MY_VAL02, "foo"},
    {MY_VAL04, "bar"},
    {0, NULL}
};
printf("\"%s\"\n", am_int2str_lookup(mapping, MY_VAL02, FALSE));
printf("\"%s\"\n", am_int2str_lookup(mapping, MY_VAL02|MY_VAL04, FALSE));
printf("\"%s\"\n", am_int2str_lookup(mapping, MY_VAL02|MY_VAL04, TRUE));
printf("\"%s\"\n", am_int2str_lookup(mapping, MY_VAL02|MY_VAL04|128, TRUE));

result:

"foo"
"(null)"
"foo|bar"
"foo|bar|128"

3.2   lookup groupname + integer -> string

Like am_int2str_lookup, but with a whole group of different mappings. The 1st group is the "fallback-mapping", which is used if either the "mapname" or the key in the "mapname" cannot be found.

Usage:

const char* am_int2str_group_lookup(const struct am_int2str_group *group, const char *name, int32_t key, bool or_enable)

Parameters:
  • group: mapping-group, must end with {NULL, NULL} !
  • name: name of the mapping to use
  • key: key to translate
Returns:

the string, or NULL if the group or key is not found

SeeAlso:

am_int2str

Example:

simple example:

const struct am_int2str_group mapping_group[] = {
    {"_FALLBACK_",  _M{ {-1, "ERROR"},
                        { 0, "OK"},
                        {0,NULL}} },
    {"MY_MAP1",     _M{ {100, "foo"},
                        {101, "bar"},
                        {0,NULL}} },
    {NULL,NULL}
};

char *str1 = am_int2str_group_lookup(mapping_group, "MY_MAP1", 101);
//-> "bar"
char *str2 = am_int2str_group_lookup(mapping_group, "MY_MAP1", -1);
//-> "ERROR"

3.3   reverse-lookup string -> integer

Get an integer for a given (case-insensitive) string.

Usage:

int am_int2str_rlookup(struct am_int2str const *map, char const *str, int fallback)

Parameters:
  • map: mapping, must end with {0, NULL} !
  • str: string to convert
  • fallback: value to return if the string is not found
Returns:

the integer, or the default value

4   UI-functions

4.2   map AM_ERROR_* to a string

Usage:const char* am_error2str(AM_ERROR err)
Returns:the string or "unknown"

4.3   map AM_CMD_* to a string

Map an am_cmd-value to a string, e.g. AM_CMD_ECHO or 0x20 is mapped to "ECHO".

Usage:const char* am_cmd2str(enum am_cmd cmd)
Returns:the string or "unknown"

4.4   map string to a AM_CMD_*-value

Parse a string and map it to an am_cmd-value, e.g. "ECHO" to AM_CMD_ECHO (0x20).

Usage:int am_str2cmd(const char *str)
Returns:the AM_CMD_*-value or -1