source: mainline/kernel/generic/src/sysinfo/sysinfo.c@ cf5ddf6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since cf5ddf6 was df4ed85, checked in by Jakub Jermar <jakub@…>, 18 years ago

© versus ©

  • Property mode set to 100644
File size: 7.8 KB
RevLine 
[6326f5e6]1/*
[df4ed85]2 * Copyright (c) 2006 Jakub Vana
[6326f5e6]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
[cc73a8a1]29/** @addtogroup generic
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[2666daa]35#include <sysinfo/sysinfo.h>
36#include <mm/slab.h>
37#include <print.h>
[35a96cf]38#include <syscall/copy.h>
[2666daa]39
[749122b]40sysinfo_item_t *_root = NULL;
[2666daa]41
42
[749122b]43static sysinfo_item_t *sysinfo_find_item(const char *name, sysinfo_item_t *subtree)
[2666daa]44{
[749122b]45 if (subtree == NULL)
46 return NULL;
47
48 while (subtree != NULL) {
49 int i = 0;
50 char *a = (char *) name;
51 char *b = subtree->name;
52
53 while ((a[i] == b[i]) && (b[i]))
54 i++;
55
56 if ((!a[i]) && (!b[i])) /* Last name in path matches */
57 return subtree;
58
59 if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
60 if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
61 return sysinfo_find_item(a + i + 1, subtree->subinfo.table);
62
63 //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
64 // return NULL;
65
66 return NULL; /* No subinfo */
[2666daa]67 }
[749122b]68 /* No matches try next */
69 subtree = subtree->next;
70 i = 0;
[2666daa]71 }
72 return NULL;
73}
74
[749122b]75static sysinfo_item_t *sysinfo_create_path(const char *name, sysinfo_item_t **psubtree)
[2666daa]76{
77 sysinfo_item_t *subtree;
78 subtree = *psubtree;
79
[749122b]80 if (subtree == NULL) {
[7bb6b06]81 sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
82 int i = 0, j;
[2666daa]83
[7bb6b06]84 ASSERT(item);
85 *psubtree = item;
86 item->next = NULL;
87 item->val_type = SYSINFO_VAL_UNDEFINED;
88 item->subinfo.table = NULL;
[2666daa]89
[7bb6b06]90 while (name[i] && (name[i] != '.'))
91 i++;
[2666daa]92
[7bb6b06]93 item->name = malloc(i, 0);
94 ASSERT(item->name);
[2666daa]95
[7bb6b06]96 for (j = 0; j < i; j++)
97 item->name[j] = name[j];
98 item->name[j] = 0;
[2666daa]99
[7bb6b06]100 if (name[i]) { /* =='.' */
101 item->subinfo_type = SYSINFO_SUBINFO_TABLE;
102 return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
103 }
104 item->subinfo_type = SYSINFO_SUBINFO_NONE;
105 return item;
[2666daa]106 }
107
[749122b]108 while (subtree != NULL) {
109 int i = 0, j;
110 char *a = (char *) name;
111 char *b = subtree->name;
112
113 while ((a[i] == b[i]) && (b[i]))
114 i++;
115
116 if ((!a[i]) && (!b[i])) /* Last name in path matches */
117 return subtree;
118
119 if ((a[i] == '.') && (!b[i])) { /* Middle name in path matches */
120 if (subtree->subinfo_type == SYSINFO_SUBINFO_TABLE)
121 return sysinfo_create_path(a + i + 1, &(subtree->subinfo.table));
122
123 if (subtree->subinfo_type == SYSINFO_SUBINFO_NONE) {
124 subtree->subinfo_type = SYSINFO_SUBINFO_TABLE;
125 return sysinfo_create_path(a + i + 1,&(subtree->subinfo.table));
126 }
127
128 //if (subtree->subinfo_type == SYSINFO_SUBINFO_FUNCTION) /* Subinfo managed by subsystem */
129 // return NULL;
130
[2666daa]131 return NULL;
132 }
133 /* No matches try next or create new*/
[749122b]134 if (subtree->next == NULL) {
135 sysinfo_item_t *item = malloc(sizeof(sysinfo_item_t), 0);
[2666daa]136
137 ASSERT(item);
[749122b]138 subtree->next = item;
139 item->next = NULL;
140 item->val_type = SYSINFO_VAL_UNDEFINED;
141 item->subinfo.table = NULL;
[2666daa]142
[749122b]143 i = 0;
144 while (name[i] && (name[i] != '.'))
145 i++;
[2666daa]146
[749122b]147 item->name = malloc(i, 0);
148 ASSERT(item->name);
149
150 for (j = 0; j < i; j++)
151 item->name[j] = name[j];
152
153 item->name[j] = 0;
[2666daa]154
[749122b]155 if(name[i]) { /* =='.' */
156 item->subinfo_type = SYSINFO_SUBINFO_TABLE;
157 return sysinfo_create_path(name + i + 1, &(item->subinfo.table));
[2666daa]158 }
[749122b]159 item->subinfo_type = SYSINFO_SUBINFO_NONE;
[2666daa]160 return item;
[749122b]161 } else {
162 subtree = subtree->next;
163 i = 0;
[2666daa]164 }
165 }
166 panic("Not reached\n");
167 return NULL;
168}
169
[7f1c620]170void sysinfo_set_item_val(const char *name, sysinfo_item_t **root, unative_t val)
[2666daa]171{
[749122b]172 if (root == NULL)
173 root = &_root;
174
175 /* If already created create only returns pointer
176 If not, create it */
177 sysinfo_item_t *item = sysinfo_create_path(name, root);
178
179 if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
[2666daa]180 item->val.val=val;
[749122b]181 item->val_type = SYSINFO_VAL_VAL;
182 }
[2666daa]183}
184
[749122b]185void sysinfo_set_item_function(const char *name, sysinfo_item_t **root, sysinfo_val_fn_t fn)
[2666daa]186{
[749122b]187 if (root == NULL)
188 root = &_root;
189
190 /* If already created create only returns pointer
191 If not, create it */
192 sysinfo_item_t *item = sysinfo_create_path(name, root);
193
194 if (item != NULL) { /* If in subsystem, unable to create or return so unable to set */
[2666daa]195 item->val.fn=fn;
[749122b]196 item->val_type = SYSINFO_VAL_FUNCTION;
197 }
[2666daa]198}
199
200
[749122b]201void sysinfo_set_item_undefined(const char *name, sysinfo_item_t **root)
[2666daa]202{
[749122b]203 if (root == NULL)
204 root = &_root;
205
206 /* If already created create only returns pointer
207 If not, create it */
208 sysinfo_item_t *item = sysinfo_create_path(name, root);
209
210 if (item != NULL)
211 item->val_type = SYSINFO_VAL_UNDEFINED;
[2666daa]212}
213
214
[749122b]215void sysinfo_dump(sysinfo_item_t **proot, int depth)
[2666daa]216{
217 sysinfo_item_t *root;
[749122b]218 if (proot == NULL)
219 proot = &_root;
220
[2666daa]221 root = *proot;
222
[749122b]223 while (root != NULL) {
[2666daa]224 int i;
[7f1c620]225 unative_t val = 0;
[749122b]226 char *vtype = NULL;
227
[2666daa]228
[749122b]229 for (i = 0; i < depth; i++)
230 printf(" ");
[2666daa]231
[749122b]232 switch (root->val_type) {
[f8ddd17]233 case SYSINFO_VAL_UNDEFINED:
234 val = 0;
235 vtype = "UND";
236 break;
237 case SYSINFO_VAL_VAL:
238 val = root->val.val;
239 vtype = "VAL";
240 break;
241 case SYSINFO_VAL_FUNCTION:
242 val = ((sysinfo_val_fn_t) (root->val.fn)) (root);
243 vtype = "FUN";
244 break;
[749122b]245 }
[2666daa]246
[f8ddd17]247 printf("%s %s val:%d(%x) sub:%s\n", root->name, vtype, val,
248 val, (root->subinfo_type == SYSINFO_SUBINFO_NONE) ?
249 "NON" : ((root->subinfo_type == SYSINFO_SUBINFO_TABLE) ?
250 "TAB" : "FUN"));
[2666daa]251
[749122b]252 if (root->subinfo_type == SYSINFO_SUBINFO_TABLE)
253 sysinfo_dump(&(root -> subinfo.table), depth + 1);
[2666daa]254
[749122b]255 root = root->next;
256 }
[2666daa]257}
258
[749122b]259sysinfo_rettype_t sysinfo_get_val(const char *name, sysinfo_item_t **root)
[2666daa]260{
[749122b]261 // TODO: Implement Subsystem subinfo (by function implemented subinfo)
[2666daa]262
[749122b]263 sysinfo_rettype_t ret = {0, false};
[2666daa]264
[749122b]265 if (root == NULL)
266 root = &_root;
267
268 sysinfo_item_t *item = sysinfo_find_item(name, *root);
269
270 if (item != NULL) {
271 if (item->val_type == SYSINFO_VAL_UNDEFINED)
[2666daa]272 return ret;
[749122b]273 else
274 ret.valid = true;
275
276 if (item->val_type == SYSINFO_VAL_VAL)
277 ret.val = item->val.val;
278 else
279 ret.val = ((sysinfo_val_fn_t) (item->val.fn)) (item);
[2666daa]280 }
281 return ret;
282}
[35a96cf]283
[7f1c620]284unative_t sys_sysinfo_valid(unative_t ptr, unative_t len)
[35a96cf]285{
286 char *str;
[749122b]287 sysinfo_rettype_t ret = {0, 0};
288 str = malloc(len + 1, 0);
289
[35a96cf]290 ASSERT(str);
[749122b]291 if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
292 ret = sysinfo_get_val(str, NULL);
293
[35a96cf]294 free(str);
295 return ret.valid;
296}
297
[7f1c620]298unative_t sys_sysinfo_value(unative_t ptr, unative_t len)
[35a96cf]299{
300 char *str;
[749122b]301 sysinfo_rettype_t ret = {0, 0};
302 str = malloc(len + 1, 0);
303
[35a96cf]304 ASSERT(str);
[749122b]305 if (!((copy_from_uspace(str, (void *) ptr, len + 1)) || (str[len])))
306 ret = sysinfo_get_val(str, NULL);
307
[35a96cf]308 free(str);
309 return ret.val;
310}
[b45c443]311
[cc73a8a1]312/** @}
[b45c443]313 */
Note: See TracBrowser for help on using the repository browser.