| 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 <stddef.h>
|
|---|
| 39 | #include <stdint.h>
|
|---|
| 40 | #include <sysinfo.h>
|
|---|
| 41 | #include <stdlib.h>
|
|---|
| 42 | #include <str.h>
|
|---|
| 43 |
|
|---|
| 44 | static void dump_bytes_hex(char *data, size_t size)
|
|---|
| 45 | {
|
|---|
| 46 | for (size_t i = 0; i < size; i++) {
|
|---|
| 47 | if (i > 0)
|
|---|
| 48 | putchar(' ');
|
|---|
| 49 | printf("0x%02x", (uint8_t) data[i]);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | static void dump_bytes_text(char *data, size_t size)
|
|---|
| 54 | {
|
|---|
| 55 | size_t offset = 0;
|
|---|
| 56 |
|
|---|
| 57 | while (offset < size) {
|
|---|
| 58 | wchar_t c = str_decode(data, &offset, size);
|
|---|
| 59 | printf("%lc", (wint_t) c);
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | static errno_t print_item_val(char *ipath)
|
|---|
| 64 | {
|
|---|
| 65 | sysarg_t value;
|
|---|
| 66 | errno_t rc = sysinfo_get_value(ipath, &value);
|
|---|
| 67 | if (rc != EOK) {
|
|---|
| 68 | printf("Error reading item '%s'.\n", ipath);
|
|---|
| 69 | return rc;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
|
|---|
| 73 | (uint64_t) value, (uint64_t) value);
|
|---|
| 74 |
|
|---|
| 75 | return EOK;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | static int print_item_data(char *ipath)
|
|---|
| 79 | {
|
|---|
| 80 | size_t size;
|
|---|
| 81 | void *data = sysinfo_get_data(ipath, &size);
|
|---|
| 82 | if (data == NULL) {
|
|---|
| 83 | printf("Error reading item '%s'.\n", ipath);
|
|---|
| 84 | return -1;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | printf("%s -> ", ipath);
|
|---|
| 88 | dump_bytes_hex(data, size);
|
|---|
| 89 | fputs(" ('", stdout);
|
|---|
| 90 | dump_bytes_text(data, size);
|
|---|
| 91 | fputs("')\n", stdout);
|
|---|
| 92 |
|
|---|
| 93 | return EOK;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | static int print_item_property(char *ipath, char *iprop)
|
|---|
| 97 | {
|
|---|
| 98 | size_t size;
|
|---|
| 99 | void *data = sysinfo_get_property(ipath, iprop, &size);
|
|---|
| 100 | if (data == NULL) {
|
|---|
| 101 | printf("Error reading property '%s' of item '%s'.\n", iprop,
|
|---|
| 102 | ipath);
|
|---|
| 103 | return -1;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | printf("%s property %s -> ", ipath, iprop);
|
|---|
| 107 | dump_bytes_hex(data, size);
|
|---|
| 108 | fputs(" ('", stdout);
|
|---|
| 109 | dump_bytes_text(data, size);
|
|---|
| 110 | fputs("')\n", stdout);
|
|---|
| 111 |
|
|---|
| 112 | return EOK;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | static void print_spaces(size_t spaces)
|
|---|
| 116 | {
|
|---|
| 117 | for (size_t i = 0; i < spaces; i++)
|
|---|
| 118 | printf(" ");
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | static void print_keys(const char *path, size_t spaces)
|
|---|
| 122 | {
|
|---|
| 123 | size_t size;
|
|---|
| 124 | char *keys = sysinfo_get_keys(path, &size);
|
|---|
| 125 | if ((keys == NULL) || (size == 0))
|
|---|
| 126 | return;
|
|---|
| 127 |
|
|---|
| 128 | size_t pos = 0;
|
|---|
| 129 | while (pos < size) {
|
|---|
| 130 | /* Process each key with sanity checks */
|
|---|
| 131 | size_t cur_size = str_nsize(keys + pos, size - pos);
|
|---|
| 132 | if (keys[pos + cur_size] != 0)
|
|---|
| 133 | break;
|
|---|
| 134 |
|
|---|
| 135 | size_t path_size = str_size(path) + cur_size + 2;
|
|---|
| 136 | char *cur_path = (char *) malloc(path_size);
|
|---|
| 137 | if (cur_path == NULL)
|
|---|
| 138 | break;
|
|---|
| 139 |
|
|---|
| 140 | size_t length;
|
|---|
| 141 |
|
|---|
| 142 | if (path[0] != 0) {
|
|---|
| 143 | print_spaces(spaces);
|
|---|
| 144 | printf(".%s\n", keys + pos);
|
|---|
| 145 | length = str_length(keys + pos) + 1;
|
|---|
| 146 |
|
|---|
| 147 | snprintf(cur_path, path_size, "%s.%s", path, keys + pos);
|
|---|
| 148 | } else {
|
|---|
| 149 | printf("%s\n", keys + pos);
|
|---|
| 150 | length = str_length(keys + pos);
|
|---|
| 151 |
|
|---|
| 152 | snprintf(cur_path, path_size, "%s", keys + pos);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | print_keys(cur_path, spaces + length);
|
|---|
| 156 |
|
|---|
| 157 | free(cur_path);
|
|---|
| 158 | pos += cur_size + 1;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | free(keys);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | int main(int argc, char *argv[])
|
|---|
| 165 | {
|
|---|
| 166 | int rc = 0;
|
|---|
| 167 |
|
|---|
| 168 | if (argc < 2) {
|
|---|
| 169 | /* Print keys */
|
|---|
| 170 | print_keys("", 0);
|
|---|
| 171 | return rc;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | char *ipath = argv[1];
|
|---|
| 175 |
|
|---|
| 176 | if (argc < 3) {
|
|---|
| 177 | sysinfo_item_val_type_t tag = sysinfo_get_val_type(ipath);
|
|---|
| 178 |
|
|---|
| 179 | switch (tag) {
|
|---|
| 180 | case SYSINFO_VAL_UNDEFINED:
|
|---|
| 181 | printf("Error: Sysinfo item '%s' not defined.\n", ipath);
|
|---|
| 182 | rc = 2;
|
|---|
| 183 | break;
|
|---|
| 184 | case SYSINFO_VAL_VAL:
|
|---|
| 185 | rc = print_item_val(ipath);
|
|---|
| 186 | break;
|
|---|
| 187 | case SYSINFO_VAL_DATA:
|
|---|
| 188 | rc = print_item_data(ipath);
|
|---|
| 189 | break;
|
|---|
| 190 | default:
|
|---|
| 191 | printf("Error: Sysinfo item '%s' with unknown value type.\n",
|
|---|
| 192 | ipath);
|
|---|
| 193 | rc = 2;
|
|---|
| 194 | break;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | return rc;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | char *iprop = argv[2];
|
|---|
| 201 | rc = print_item_property(ipath, iprop);
|
|---|
| 202 | return rc;
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | /** @}
|
|---|
| 206 | */
|
|---|