Rev 1318 | Rev 1322 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1318 | Rev 1321 | ||
---|---|---|---|
- | 1 | /* |
|
- | 2 | * Copyright (C) 2006 Jakub Vana |
|
- | 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 | ||
1 | #include <sysinfo/sysinfo.h> |
29 | #include <sysinfo/sysinfo.h> |
2 | #include <mm/slab.h> |
30 | #include <mm/slab.h> |
3 | #include <print.h> |
31 | #include <print.h> |
4 | #include <syscall/copy.h> |
32 | #include <syscall/copy.h> |
5 | 33 | ||
6 | sysinfo_item_t *_root=NULL; |
34 | sysinfo_item_t *_root=NULL; |
7 | 35 | ||
8 | 36 | ||
9 | static sysinfo_item_t* sysinfo_find_item(const char *name,sysinfo_item_t *subtree) |
37 | static sysinfo_item_t* sysinfo_find_item(const char *name,sysinfo_item_t *subtree) |
10 | { |
38 | { |
11 | if(subtree==NULL) return NULL; |
39 | if(subtree==NULL) return NULL; |
12 | while(subtree!=NULL) |
40 | while(subtree!=NULL) |
13 | { |
41 | { |
14 | int i; |
42 | int i; |
15 | char *a,*b; |
43 | char *a,*b; |
16 | a=(char *)name; |
44 | a=(char *)name; |
17 | b=subtree->name; |
45 | b=subtree->name; |
18 | while((a[i]==b[i])&&(b[i])) i++; |
46 | while((a[i]==b[i])&&(b[i])) i++; |
19 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
47 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
20 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
48 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
21 | { |
49 | { |
22 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_find_item(a+i+1,subtree->subinfo.table); |
50 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_find_item(a+i+1,subtree->subinfo.table); |
23 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
51 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
24 | return NULL; /*No subinfo*/ |
52 | return NULL; /*No subinfo*/ |
25 | } |
53 | } |
26 | /* No matches try next*/ |
54 | /* No matches try next*/ |
27 | subtree=subtree->next; |
55 | subtree=subtree->next; |
28 | i=0; |
56 | i=0; |
29 | } |
57 | } |
30 | return NULL; |
58 | return NULL; |
31 | } |
59 | } |
32 | 60 | ||
33 | static sysinfo_item_t* sysinfo_create_path(const char *name,sysinfo_item_t **psubtree) |
61 | static sysinfo_item_t* sysinfo_create_path(const char *name,sysinfo_item_t **psubtree) |
34 | { |
62 | { |
35 | sysinfo_item_t *subtree; |
63 | sysinfo_item_t *subtree; |
36 | subtree = *psubtree; |
64 | subtree = *psubtree; |
37 | 65 | ||
38 | if(subtree==NULL) |
66 | if(subtree==NULL) |
39 | { |
67 | { |
40 | sysinfo_item_t *item; |
68 | sysinfo_item_t *item; |
41 | int i=0,j; |
69 | int i=0,j; |
42 | 70 | ||
43 | item = malloc(sizeof(sysinfo_item_t),0); |
71 | item = malloc(sizeof(sysinfo_item_t),0); |
44 | ASSERT(item); |
72 | ASSERT(item); |
45 | *psubtree = item; |
73 | *psubtree = item; |
46 | item -> next = NULL; |
74 | item -> next = NULL; |
47 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
75 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
48 | item -> subinfo.table = NULL; |
76 | item -> subinfo.table = NULL; |
49 | 77 | ||
50 | while(name[i]&&(name[i]!='.')) i++; |
78 | while(name[i]&&(name[i]!='.')) i++; |
51 | 79 | ||
52 | item -> name = malloc(i,0); |
80 | item -> name = malloc(i,0); |
53 | ASSERT(item -> name); |
81 | ASSERT(item -> name); |
54 | 82 | ||
55 | for(j=0;j<i;j++) item->name[j]=name[j]; |
83 | for(j=0;j<i;j++) item->name[j]=name[j]; |
56 | item->name[j]=0; |
84 | item->name[j]=0; |
57 | 85 | ||
58 | if(name[i]/*=='.'*/) |
86 | if(name[i]/*=='.'*/) |
59 | { |
87 | { |
60 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
88 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
61 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
89 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
62 | } |
90 | } |
63 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
91 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
64 | return item; |
92 | return item; |
65 | } |
93 | } |
66 | 94 | ||
67 | while(subtree!=NULL) |
95 | while(subtree!=NULL) |
68 | { |
96 | { |
69 | int i=0,j; |
97 | int i=0,j; |
70 | char *a,*b; |
98 | char *a,*b; |
71 | a=(char *)name; |
99 | a=(char *)name; |
72 | b=subtree->name; |
100 | b=subtree->name; |
73 | while((a[i]==b[i])&&(b[i])) i++; |
101 | while((a[i]==b[i])&&(b[i])) i++; |
74 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
102 | if((!a[i]) && (!b[i])) return subtree; /*Last name in path matches*/ |
75 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
103 | if((a[i]=='.') && (!b[i])) /*Middle name in path matches*/ |
76 | { |
104 | { |
77 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
105 | if(subtree->subinfo_type==SYSINFO_SUBINFO_TABLE) return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
78 | if(subtree->subinfo_type==SYSINFO_SUBINFO_NONE) |
106 | if(subtree->subinfo_type==SYSINFO_SUBINFO_NONE) |
79 | { |
107 | { |
80 | subtree->subinfo_type=SYSINFO_SUBINFO_TABLE; |
108 | subtree->subinfo_type=SYSINFO_SUBINFO_TABLE; |
81 | return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
109 | return sysinfo_create_path(a+i+1,&(subtree->subinfo.table)); |
82 | } |
110 | } |
83 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
111 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/ |
84 | return NULL; |
112 | return NULL; |
85 | } |
113 | } |
86 | /* No matches try next or create new*/ |
114 | /* No matches try next or create new*/ |
87 | if(subtree->next==NULL) |
115 | if(subtree->next==NULL) |
88 | { |
116 | { |
89 | sysinfo_item_t *item; |
117 | sysinfo_item_t *item; |
90 | 118 | ||
91 | item = malloc(sizeof(sysinfo_item_t),0); |
119 | item = malloc(sizeof(sysinfo_item_t),0); |
92 | ASSERT(item); |
120 | ASSERT(item); |
93 | subtree -> next = item; |
121 | subtree -> next = item; |
94 | item -> next = NULL; |
122 | item -> next = NULL; |
95 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
123 | item -> val_type = SYSINFO_VAL_UNDEFINED; |
96 | item -> subinfo.table = NULL; |
124 | item -> subinfo.table = NULL; |
97 | 125 | ||
98 | i=0; |
126 | i=0; |
99 | while(name[i]&&(name[i]!='.')) i++; |
127 | while(name[i]&&(name[i]!='.')) i++; |
100 | 128 | ||
101 | item -> name = malloc(i,0); |
129 | item -> name = malloc(i,0); |
102 | ASSERT(item -> name); |
130 | ASSERT(item -> name); |
103 | for(j=0;j<i;j++) item->name[j]=name[j]; |
131 | for(j=0;j<i;j++) item->name[j]=name[j]; |
104 | item->name[j]=0; |
132 | item->name[j]=0; |
105 | 133 | ||
106 | if(name[i]/*=='.'*/) |
134 | if(name[i]/*=='.'*/) |
107 | { |
135 | { |
108 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
136 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE; |
109 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
137 | return sysinfo_create_path(name+i+1,&(item->subinfo.table)); |
110 | } |
138 | } |
111 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
139 | item -> subinfo_type = SYSINFO_SUBINFO_NONE; |
112 | return item; |
140 | return item; |
113 | 141 | ||
114 | } |
142 | } |
115 | else |
143 | else |
116 | { |
144 | { |
117 | subtree=subtree->next; |
145 | subtree=subtree->next; |
118 | i=0; |
146 | i=0; |
119 | } |
147 | } |
120 | } |
148 | } |
121 | panic("Not reached\n"); |
149 | panic("Not reached\n"); |
122 | return NULL; |
150 | return NULL; |
123 | } |
151 | } |
124 | 152 | ||
125 | void sysinfo_set_item_val(const char *name,sysinfo_item_t **root,__native val) |
153 | void sysinfo_set_item_val(const char *name,sysinfo_item_t **root,__native val) |
126 | { |
154 | { |
127 | if(root==NULL) root=&_root; |
155 | if(root==NULL) root=&_root; |
128 | sysinfo_item_t *item; |
156 | sysinfo_item_t *item; |
129 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
157 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
130 | If ! , create it */ |
158 | If ! , create it */ |
131 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
159 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
132 | { |
160 | { |
133 | item->val.val=val; |
161 | item->val.val=val; |
134 | item -> val_type = SYSINFO_VAL_VAL; |
162 | item -> val_type = SYSINFO_VAL_VAL; |
135 | } |
163 | } |
136 | } |
164 | } |
137 | 165 | ||
138 | void sysinfo_set_item_function(const char *name,sysinfo_item_t **root,sysinfo_val_fn_t fn) |
166 | void sysinfo_set_item_function(const char *name,sysinfo_item_t **root,sysinfo_val_fn_t fn) |
139 | { |
167 | { |
140 | if(root==NULL) root=&_root; |
168 | if(root==NULL) root=&_root; |
141 | sysinfo_item_t *item; |
169 | sysinfo_item_t *item; |
142 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
170 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer |
143 | If ! , create it */ |
171 | If ! , create it */ |
144 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
172 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */ |
145 | { |
173 | { |
146 | item->val.fn=fn; |
174 | item->val.fn=fn; |
147 | item -> val_type = SYSINFO_VAL_FUNCTION; |
175 | item -> val_type = SYSINFO_VAL_FUNCTION; |
148 | } |
176 | } |
149 | } |
177 | } |
150 | 178 | ||
151 | 179 | ||
152 | void sysinfo_set_item_undefined(const char *name,sysinfo_item_t **root) |
180 | void sysinfo_set_item_undefined(const char *name,sysinfo_item_t **root) |
153 | { |
181 | { |
154 | if(root==NULL) root=&_root; |
182 | if(root==NULL) root=&_root; |
155 | sysinfo_item_t *item; |
183 | sysinfo_item_t *item; |
156 | item = sysinfo_find_item(name,*root); |
184 | item = sysinfo_find_item(name,*root); |
157 | if(item!=NULL) item -> val_type = SYSINFO_VAL_UNDEFINED; |
185 | if(item!=NULL) item -> val_type = SYSINFO_VAL_UNDEFINED; |
158 | } |
186 | } |
159 | 187 | ||
160 | 188 | ||
161 | void sysinfo_dump(sysinfo_item_t **proot,int depth) |
189 | void sysinfo_dump(sysinfo_item_t **proot,int depth) |
162 | { |
190 | { |
163 | sysinfo_item_t *root; |
191 | sysinfo_item_t *root; |
164 | if(proot==NULL) proot=&_root; |
192 | if(proot==NULL) proot=&_root; |
165 | root = *proot; |
193 | root = *proot; |
166 | 194 | ||
167 | while(root!=NULL) |
195 | while(root!=NULL) |
168 | { |
196 | { |
169 | 197 | ||
170 | int i; |
198 | int i; |
171 | __native val=0; |
199 | __native val=0; |
172 | char *vtype=NULL; |
200 | char *vtype=NULL; |
173 | 201 | ||
174 | 202 | ||
175 | for(i=0;i<depth;i++) printf(" "); |
203 | for(i=0;i<depth;i++) printf(" "); |
176 | 204 | ||
177 | switch (root->val_type) |
205 | switch (root->val_type) |
178 | { |
206 | { |
179 | case (SYSINFO_VAL_UNDEFINED): |
207 | case (SYSINFO_VAL_UNDEFINED): |
180 | val=0; |
208 | val=0; |
181 | vtype="UND"; |
209 | vtype="UND"; |
182 | break; |
210 | break; |
183 | case (SYSINFO_VAL_VAL): |
211 | case (SYSINFO_VAL_VAL): |
184 | val=root->val.val; |
212 | val=root->val.val; |
185 | vtype="VAL"; |
213 | vtype="VAL"; |
186 | break; |
214 | break; |
187 | case (SYSINFO_VAL_FUNCTION): |
215 | case (SYSINFO_VAL_FUNCTION): |
188 | val=((sysinfo_val_fn_t)(root->val.fn))(root); |
216 | val=((sysinfo_val_fn_t)(root->val.fn))(root); |
189 | vtype="FUN"; |
217 | vtype="FUN"; |
190 | break; |
218 | break; |
191 | } |
219 | } |
192 | printf("%s %s val:%d(%X) sub:%s\n",root->name,vtype,val,val, |
220 | printf("%s %s val:%d(%X) sub:%s\n",root->name,vtype,val,val, |
193 | (root->subinfo_type==SYSINFO_SUBINFO_NONE)?"NON":((root->subinfo_type==SYSINFO_SUBINFO_TABLE)?"TAB":"FUN")); |
221 | (root->subinfo_type==SYSINFO_SUBINFO_NONE)?"NON":((root->subinfo_type==SYSINFO_SUBINFO_TABLE)?"TAB":"FUN")); |
194 | 222 | ||
195 | 223 | ||
196 | if(root -> subinfo_type == SYSINFO_SUBINFO_TABLE) |
224 | if(root -> subinfo_type == SYSINFO_SUBINFO_TABLE) |
197 | sysinfo_dump(&(root->subinfo.table),depth+1); |
225 | sysinfo_dump(&(root->subinfo.table),depth+1); |
198 | root=root->next; |
226 | root=root->next; |
199 | } |
227 | } |
200 | } |
228 | } |
201 | 229 | ||
202 | sysinfo_rettype_t sysinfo_get_val(const char *name,sysinfo_item_t **root) |
230 | sysinfo_rettype_t sysinfo_get_val(const char *name,sysinfo_item_t **root) |
203 | { |
231 | { |
204 | /*TODO: |
232 | /*TODO: |
205 | Implement Subsystem subinfo (by function implemented subinfo) |
233 | Implement Subsystem subinfo (by function implemented subinfo) |
206 | */ |
234 | */ |
207 | 235 | ||
208 | sysinfo_rettype_t ret={0,false}; |
236 | sysinfo_rettype_t ret={0,false}; |
209 | 237 | ||
210 | if(root==NULL) root=&_root; |
238 | if(root==NULL) root=&_root; |
211 | sysinfo_item_t *item; |
239 | sysinfo_item_t *item; |
212 | item = sysinfo_find_item(name,*root); |
240 | item = sysinfo_find_item(name,*root); |
213 | if(item!=NULL) |
241 | if(item!=NULL) |
214 | { |
242 | { |
215 | if (item -> val_type == SYSINFO_VAL_UNDEFINED) |
243 | if (item -> val_type == SYSINFO_VAL_UNDEFINED) |
216 | return ret; |
244 | return ret; |
217 | else ret.valid=true; |
245 | else ret.valid=true; |
218 | if (item -> val_type == SYSINFO_VAL_VAL) |
246 | if (item -> val_type == SYSINFO_VAL_VAL) |
219 | ret.val=item -> val.val; |
247 | ret.val=item -> val.val; |
220 | else ret.val=((sysinfo_val_fn_t)(item->val.fn))(item); |
248 | else ret.val=((sysinfo_val_fn_t)(item->val.fn))(item); |
221 | } |
249 | } |
222 | return ret; |
250 | return ret; |
223 | } |
251 | } |
224 | 252 | ||
225 | __native sys_sysinfo_valid(__native ptr,__native len) |
253 | __native sys_sysinfo_valid(__native ptr,__native len) |
226 | { |
254 | { |
227 | char *str; |
255 | char *str; |
228 | sysinfo_rettype_t ret; |
256 | sysinfo_rettype_t ret; |
229 | str=malloc(len+1,0); |
257 | str=malloc(len+1,0); |
230 | ASSERT(str); |
258 | ASSERT(str); |
231 | copy_from_uspace(str,(void *)ptr,len+1); |
259 | if(copy_from_uspace(str,(void *)ptr,len+1)) return 0; |
232 | if(str[len]) return 0; /*This is not len lenght C string*/ |
260 | if(str[len]) return 0; /*This is not len lenght C string*/ |
233 | ret=sysinfo_get_val(str,NULL); |
261 | ret=sysinfo_get_val(str,NULL); |
234 | free(str); |
262 | free(str); |
235 | return ret.valid; |
263 | return ret.valid; |
236 | } |
264 | } |
237 | 265 | ||
238 | __native sys_sysinfo_value(__native ptr,__native len) |
266 | __native sys_sysinfo_value(__native ptr,__native len) |
239 | { |
267 | { |
240 | char *str; |
268 | char *str; |
241 | sysinfo_rettype_t ret; |
269 | sysinfo_rettype_t ret; |
242 | str=malloc(len+1,0); |
270 | str=malloc(len+1,0); |
243 | ASSERT(str); |
271 | ASSERT(str); |
244 | copy_from_uspace(str,(void *)ptr,len+1); |
272 | if(copy_from_uspace(str,(void *)ptr,len+1)) return 0; |
245 | if(str[len]) return 0; /*This is not len lenght C string*/ |
273 | if(str[len]) return 0; /*This is not len lenght C string*/ |
246 | ret=sysinfo_get_val(str,NULL); |
274 | ret=sysinfo_get_val(str,NULL); |
247 | free(str); |
275 | free(str); |
248 | return ret.val; |
276 | return ret.val; |
249 | } |
277 | } |
250 | 278 | ||
251 | 279 | ||
252 | 280 |