Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 1 | |
| 2 | #include <string.h> |
| 3 | #include <stdint.h> |
| 4 | #include <errno.h> |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame^] | 5 | #include <stdio.h> |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 6 | |
| 7 | #include <osmocore/utils.h> |
| 8 | |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame^] | 9 | static char namebuf[255]; |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 10 | const char *get_value_string(const struct value_string *vs, uint32_t val) |
| 11 | { |
| 12 | int i; |
| 13 | |
| 14 | for (i = 0;; i++) { |
| 15 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 16 | break; |
| 17 | if (vs[i].value == val) |
| 18 | return vs[i].str; |
| 19 | } |
Harald Welte | 9870099 | 2010-03-25 12:15:24 +0800 | [diff] [blame^] | 20 | |
| 21 | snprintf(namebuf, sizeof(namebuf), "unknown 0x%x", val); |
| 22 | return namebuf; |
Harald Welte | c436245 | 2010-03-22 22:25:13 +0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | int get_string_value(const struct value_string *vs, const char *str) |
| 26 | { |
| 27 | int i; |
| 28 | |
| 29 | for (i = 0;; i++) { |
| 30 | if (vs[i].value == 0 && vs[i].str == NULL) |
| 31 | break; |
| 32 | if (!strcasecmp(vs[i].str, str)) |
| 33 | return vs[i].value; |
| 34 | } |
| 35 | return -EINVAL; |
| 36 | } |
| 37 | |
| 38 | char bcd2char(uint8_t bcd) |
| 39 | { |
| 40 | if (bcd < 0xa) |
| 41 | return '0' + bcd; |
| 42 | else |
| 43 | return 'A' + (bcd - 0xa); |
| 44 | } |
| 45 | |
| 46 | /* only works for numbers in ascci */ |
| 47 | uint8_t char2bcd(char c) |
| 48 | { |
| 49 | return c - 0x30; |
| 50 | } |