sysinfo.c

00001 /*
00002  * Copyright (c) 2010 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00036 #include <errno.h>
00037 #include <stdio.h>
00038 #include <sysinfo.h>
00039 #include <sys/types.h>
00040 
00041 static int print_item_val(char *ipath);
00042 static int print_item_data(char *ipath);
00043 
00044 static void dump_bytes_hex(char *data, size_t size);
00045 static void dump_bytes_text(char *data, size_t size);
00046 
00047 static void print_syntax(void);
00048 
00049 int main(int argc, char *argv[])
00050 {
00051         int rc;
00052         char *ipath;
00053         sysinfo_item_tag_t tag;
00054 
00055         if (argc != 2) {
00056                 print_syntax();
00057                 return 1;
00058         }
00059 
00060         ipath = argv[1];
00061 
00062         tag = sysinfo_get_tag(ipath);
00063 
00064         /* Silence warning */
00065         rc = EOK;
00066 
00067         switch (tag) {
00068         case SYSINFO_VAL_UNDEFINED:
00069                 printf("Error: Sysinfo item '%s' not defined.\n", ipath);
00070                 rc = 2;
00071                 break;
00072         case SYSINFO_VAL_VAL:
00073                 rc = print_item_val(ipath);
00074                 break;
00075         case SYSINFO_VAL_DATA:
00076                 rc = print_item_data(ipath);
00077                 break;
00078         }
00079 
00080         return rc;
00081 }
00082 
00083 static int print_item_val(char *ipath)
00084 {
00085         sysarg_t value;
00086         int rc;
00087 
00088         rc = sysinfo_get_value(ipath, &value);
00089         if (rc != EOK) {
00090                 printf("Error reading item '%s'.\n", ipath);
00091                 return rc;
00092         }
00093 
00094         printf("%s -> %" PRIu64 " (0x%" PRIx64 ")\n", ipath,
00095             (uint64_t) value, (uint64_t) value);
00096 
00097         return EOK;
00098 }
00099 
00100 static int print_item_data(char *ipath)
00101 {
00102         void *data;
00103         size_t size;
00104 
00105         data = sysinfo_get_data(ipath, &size);
00106         if (data == NULL) {
00107                 printf("Error reading item '%s'.\n", ipath);
00108                 return -1;
00109         }
00110 
00111         printf("%s -> ", ipath);
00112         dump_bytes_hex(data, size);
00113         fputs(" ('", stdout);
00114         dump_bytes_text(data, size);
00115         fputs("')\n", stdout);
00116 
00117         return EOK;
00118 }
00119 
00120 static void dump_bytes_hex(char *data, size_t size)
00121 {
00122         size_t i;
00123 
00124         for (i = 0; i < size; ++i) {
00125                 if (i > 0) putchar(' ');
00126                 printf("0x%02x", (uint8_t) data[i]);
00127         }
00128 }
00129 
00130 static void dump_bytes_text(char *data, size_t size)
00131 {
00132         wchar_t c;
00133         size_t offset;
00134 
00135         offset = 0;
00136 
00137         while (offset < size) {
00138                 c = str_decode(data, &offset, size);
00139                 printf("%lc", (wint_t) c);
00140         }
00141 }
00142 
00143 
00144 static void print_syntax(void)
00145 {
00146         printf("Syntax: sysinfo <item_path>\n");
00147 }
00148 

Generated on Thu Jun 2 07:45:43 2011 for HelenOS/USB by  doxygen 1.4.7