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