• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/uqmi/uqmid/osmocom/utils.h

  1 #pragma once
  2 
  3 /* a minimal version of libosmocore utils.h */
  4 
  5 #include <stdbool.h>
  6 #include <stdint.h>
  7 #include <stdio.h>
  8 #include <string.h>
  9 #include <stdlib.h>
 10 
 11 /*! \defgroup utils General-purpose utility functions
 12  *  @{
 13  * \file utils.h */
 14 
 15 /*! Determine number of elements in an array of static size */
 16 #ifndef ARRAY_SIZE
 17 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 18 #endif
 19 
 20 #define OSMO_ASSERT(exp)    \
 21 do { \
 22     if (OSMO_UNLIKELY(!(exp))) { \
 23         fprintf(stderr, "Assert failed %s %s:%d\n", #exp, __FILE__, __LINE__); \
 24                 exit(2342); \
 25     } \
 26 } while (0)
 27 
 28 /*! Return the maximum of two specified values */
 29 #define OSMO_MAX(a, b) ((a) >= (b) ? (a) : (b))
 30 /*! Return the minimum of two specified values */
 31 #define OSMO_MIN(a, b) ((a) >= (b) ? (b) : (a))
 32 /*! Return a typical cmp result for comparable entities a and b. */
 33 #define OSMO_CMP(a, b) ((a) < (b)? -1 : ((a) > (b)? 1 : 0))
 34 
 35 /*! Stringify the name of a macro x, e.g. an FSM event name.
 36  * Note: if nested within another preprocessor macro, this will
 37  * stringify the value of x instead of its name. */
 38 #define OSMO_STRINGIFY(x) #x
 39 /*! Stringify the value of a macro x, e.g. a port number. */
 40 #define OSMO_STRINGIFY_VAL(x) OSMO_STRINGIFY(x)
 41 /*! Make a value_string entry from an enum value name */
 42 #define OSMO_VALUE_STRING(x) { x, #x }
 43 
 44 /*! Branch prediction optimizations */
 45 #if defined(__GNUC__)
 46 #define OSMO_LIKELY(exp) __builtin_expect(!!(exp), 1)
 47 #define OSMO_UNLIKELY(exp) __builtin_expect(!!(exp), 0)
 48 #else
 49 #define OSMO_LIKELY(exp) exp
 50 #define OSMO_UNLIKELY(exp) exp
 51 #endif
 52 
 53 
 54 /*! A mapping between human-readable string and numeric value */
 55 struct value_string {
 56         uint32_t value;         /*!< numeric value */
 57         const char *str;        /*!< human-readable string */
 58 };
 59 
 60 const char *get_value_string(const struct value_string *vs, uint32_t val);
 61 const char *get_value_string_or_null(const struct value_string *vs,
 62                                      uint32_t val);
 63 
 64 int get_string_value(const struct value_string *vs, const char *str);
 65 
 66 bool osmo_identifier_valid(const char *str);
 67 bool osmo_separated_identifiers_valid(const char *str, const char *sep_chars);
 68 void osmo_identifier_sanitize_buf(char *str, const char *sep_chars, char replace_with);
 69 
 70 char osmo_bcd2char(uint8_t bcd);
 71 /* only works for numbers in ASCII */
 72 uint8_t osmo_char2bcd(char c);
 73 
 74 int osmo_bcd2str(char *dst, size_t dst_size, const uint8_t *bcd, int start_nibble, int end_nibble, bool allow_hex);
 75 int osmo_str2bcd(uint8_t *dst, size_t dst_size, const char *digits, int start_nibble, int end_nibble, bool allow_hex);
 76 
 77 /*! @} */
 78 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt