| 1 | /* | 
|---|
| 2 | * Copyright (c) 2010 Jiri Svoboda | 
|---|
| 3 | * All rights reserved. | 
|---|
| 4 | * | 
|---|
| 5 | * Redistribution and use in source and binary forms, with or without | 
|---|
| 6 | * modification, are permitted provided that the following conditions | 
|---|
| 7 | * are met: | 
|---|
| 8 | * | 
|---|
| 9 | * - Redistributions of source code must retain the above copyright | 
|---|
| 10 | *   notice, this list of conditions and the following disclaimer. | 
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright | 
|---|
| 12 | *   notice, this list of conditions and the following disclaimer in the | 
|---|
| 13 | *   documentation and/or other materials provided with the distribution. | 
|---|
| 14 | * - The name of the author may not be used to endorse or promote products | 
|---|
| 15 | *   derived from this software without specific prior written permission. | 
|---|
| 16 | * | 
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | 
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | 
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | 
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | 
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
|---|
| 27 | */ | 
|---|
| 28 |  | 
|---|
| 29 | /** @addtogroup sysinfo | 
|---|
| 30 | * @{ | 
|---|
| 31 | */ | 
|---|
| 32 | /** @file sysinfo.c | 
|---|
| 33 | * @brief Print value of item from sysinfo tree. | 
|---|
| 34 | */ | 
|---|
| 35 |  | 
|---|
| 36 | #include <errno.h> | 
|---|
| 37 | #include <stdio.h> | 
|---|
| 38 | #include <sysinfo.h> | 
|---|
| 39 | #include <sys/types.h> | 
|---|
| 40 |  | 
|---|
| 41 | static int print_item_val(char *ipath); | 
|---|
| 42 | static int print_item_data(char *ipath); | 
|---|
| 43 |  | 
|---|
| 44 | static void dump_bytes_hex(char *data, size_t size); | 
|---|
| 45 | static void dump_bytes_text(char *data, size_t size); | 
|---|
| 46 |  | 
|---|
| 47 | static void print_syntax(void); | 
|---|
| 48 |  | 
|---|
| 49 | int main(int argc, char *argv[]) | 
|---|
| 50 | { | 
|---|
| 51 | int rc; | 
|---|
| 52 | char *ipath; | 
|---|
| 53 | sysinfo_item_val_type_t tag; | 
|---|
| 54 |  | 
|---|
| 55 | if (argc != 2) { | 
|---|
| 56 | print_syntax(); | 
|---|
| 57 | return 1; | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | ipath = argv[1]; | 
|---|
| 61 |  | 
|---|
| 62 | tag = sysinfo_get_val_type(ipath); | 
|---|
| 63 |  | 
|---|
| 64 | /* Silence warning */ | 
|---|
| 65 | rc = EOK; | 
|---|
| 66 |  | 
|---|
| 67 | switch (tag) { | 
|---|
| 68 | case SYSINFO_VAL_UNDEFINED: | 
|---|
| 69 | printf("Error: Sysinfo item '%s' not defined.\n", ipath); | 
|---|
| 70 | rc = 2; | 
|---|
| 71 | break; | 
|---|
| 72 | case SYSINFO_VAL_VAL: | 
|---|
| 73 | rc = print_item_val(ipath); | 
|---|
| 74 | break; | 
|---|
| 75 | case SYSINFO_VAL_DATA: | 
|---|
| 76 | rc = print_item_data(ipath); | 
|---|
| 77 | break; | 
|---|
| 78 | default: | 
|---|
| 79 | printf("Error: Sysinfo item '%s' with unknown value type.\n", | 
|---|
| 80 | ipath); | 
|---|
| 81 | rc = 2; | 
|---|
| 82 | break; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | return rc; | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | static int print_item_val(char *ipath) | 
|---|
| 89 | { | 
|---|
| 90 | sysarg_t value; | 
|---|
| 91 | int rc; | 
|---|
| 92 |  | 
|---|
| 93 | rc = sysinfo_get_value(ipath, &value); | 
|---|
| 94 | if (rc != EOK) { | 
|---|
| 95 | printf("Error reading item '%s'.\n", ipath); | 
|---|
| 96 | return rc; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath, | 
|---|
| 100 | (uint64_t) value, (uint64_t) value); | 
|---|
| 101 |  | 
|---|
| 102 | return EOK; | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | static int print_item_data(char *ipath) | 
|---|
| 106 | { | 
|---|
| 107 | void *data; | 
|---|
| 108 | size_t size; | 
|---|
| 109 |  | 
|---|
| 110 | data = sysinfo_get_data(ipath, &size); | 
|---|
| 111 | if (data == NULL) { | 
|---|
| 112 | printf("Error reading item '%s'.\n", ipath); | 
|---|
| 113 | return -1; | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | printf("%s -> ", ipath); | 
|---|
| 117 | dump_bytes_hex(data, size); | 
|---|
| 118 | fputs(" ('", stdout); | 
|---|
| 119 | dump_bytes_text(data, size); | 
|---|
| 120 | fputs("')\n", stdout); | 
|---|
| 121 |  | 
|---|
| 122 | return EOK; | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | static void dump_bytes_hex(char *data, size_t size) | 
|---|
| 126 | { | 
|---|
| 127 | size_t i; | 
|---|
| 128 |  | 
|---|
| 129 | for (i = 0; i < size; ++i) { | 
|---|
| 130 | if (i > 0) putchar(' '); | 
|---|
| 131 | printf("0x%02x", (uint8_t) data[i]); | 
|---|
| 132 | } | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | static void dump_bytes_text(char *data, size_t size) | 
|---|
| 136 | { | 
|---|
| 137 | wchar_t c; | 
|---|
| 138 | size_t offset; | 
|---|
| 139 |  | 
|---|
| 140 | offset = 0; | 
|---|
| 141 |  | 
|---|
| 142 | while (offset < size) { | 
|---|
| 143 | c = str_decode(data, &offset, size); | 
|---|
| 144 | printf("%lc", (wint_t) c); | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 |  | 
|---|
| 149 | static void print_syntax(void) | 
|---|
| 150 | { | 
|---|
| 151 | printf("Syntax: sysinfo <item_path>\n"); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | /** @} | 
|---|
| 155 | */ | 
|---|