| 1 | #include <sysinfo/sysinfo.h>
|
|---|
| 2 | #include <mm/slab.h>
|
|---|
| 3 | #include <print.h>
|
|---|
| 4 |
|
|---|
| 5 | sysinfo_item_t *_root=NULL;
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 | static sysinfo_item_t* sysinfo_find_item(const char *name,sysinfo_item_t *subtree)
|
|---|
| 9 | {
|
|---|
| 10 | if(subtree==NULL) return NULL;
|
|---|
| 11 | while(subtree!=NULL)
|
|---|
| 12 | {
|
|---|
| 13 | int i;
|
|---|
| 14 | char *a,*b;
|
|---|
| 15 | a=(char *)name;
|
|---|
| 16 | b=subtree->name;
|
|---|
| 17 | 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])) /*Middle name in path matches*/
|
|---|
| 20 | {
|
|---|
| 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_FUNCTION) return NULL; /* Subinfo managed by subsystem*/
|
|---|
| 23 | return NULL; /*No subinfo*/
|
|---|
| 24 | }
|
|---|
| 25 | /* No matches try next*/
|
|---|
| 26 | subtree=subtree->next;
|
|---|
| 27 | i=0;
|
|---|
| 28 | }
|
|---|
| 29 | return NULL;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | static sysinfo_item_t* sysinfo_create_path(const char *name,sysinfo_item_t **psubtree)
|
|---|
| 33 | {
|
|---|
| 34 | sysinfo_item_t *subtree;
|
|---|
| 35 | subtree = *psubtree;
|
|---|
| 36 |
|
|---|
| 37 | if(subtree==NULL)
|
|---|
| 38 | {
|
|---|
| 39 | sysinfo_item_t *item;
|
|---|
| 40 | int i=0,j;
|
|---|
| 41 |
|
|---|
| 42 | item = malloc(sizeof(sysinfo_item_t),0);
|
|---|
| 43 | ASSERT(item);
|
|---|
| 44 | *psubtree = item;
|
|---|
| 45 | item -> next = NULL;
|
|---|
| 46 | item -> val_type = SYSINFO_VAL_UNDEFINED;
|
|---|
| 47 | item -> subinfo.table = NULL;
|
|---|
| 48 |
|
|---|
| 49 | while(name[i]&&(name[i]!='.')) i++;
|
|---|
| 50 |
|
|---|
| 51 | item -> name = malloc(i,0);
|
|---|
| 52 | ASSERT(item -> name);
|
|---|
| 53 |
|
|---|
| 54 | for(j=0;j<i;j++) item->name[j]=name[j];
|
|---|
| 55 | item->name[j]=0;
|
|---|
| 56 |
|
|---|
| 57 | if(name[i]/*=='.'*/)
|
|---|
| 58 | {
|
|---|
| 59 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE;
|
|---|
| 60 | return sysinfo_create_path(name+i+1,&(item->subinfo.table));
|
|---|
| 61 | }
|
|---|
| 62 | item -> subinfo_type = SYSINFO_SUBINFO_NONE;
|
|---|
| 63 | return item;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | while(subtree!=NULL)
|
|---|
| 67 | {
|
|---|
| 68 | int i=0,j;
|
|---|
| 69 | char *a,*b;
|
|---|
| 70 | a=(char *)name;
|
|---|
| 71 | b=subtree->name;
|
|---|
| 72 | 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])) /*Middle name in path matches*/
|
|---|
| 75 | {
|
|---|
| 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_NONE)
|
|---|
| 78 | {
|
|---|
| 79 | subtree->subinfo_type=SYSINFO_SUBINFO_TABLE;
|
|---|
| 80 | return sysinfo_create_path(a+i+1,&(subtree->subinfo.table));
|
|---|
| 81 | }
|
|---|
| 82 | //if(subtree->subinfo_type==SYSINFO_SUBINFO_FUNCTION) return NULL; /* Subinfo managed by subsystem*/
|
|---|
| 83 | return NULL;
|
|---|
| 84 | }
|
|---|
| 85 | /* No matches try next or create new*/
|
|---|
| 86 | if(subtree->next==NULL)
|
|---|
| 87 | {
|
|---|
| 88 | sysinfo_item_t *item;
|
|---|
| 89 |
|
|---|
| 90 | item = malloc(sizeof(sysinfo_item_t),0);
|
|---|
| 91 | ASSERT(item);
|
|---|
| 92 | subtree -> next = item;
|
|---|
| 93 | item -> next = NULL;
|
|---|
| 94 | item -> val_type = SYSINFO_VAL_UNDEFINED;
|
|---|
| 95 | item -> subinfo.table = NULL;
|
|---|
| 96 |
|
|---|
| 97 | i=0;
|
|---|
| 98 | while(name[i]&&(name[i]!='.')) i++;
|
|---|
| 99 |
|
|---|
| 100 | item -> name = malloc(i,0);
|
|---|
| 101 | ASSERT(item -> name);
|
|---|
| 102 | for(j=0;j<i;j++) item->name[j]=name[j];
|
|---|
| 103 | item->name[j]=0;
|
|---|
| 104 |
|
|---|
| 105 | if(name[i]/*=='.'*/)
|
|---|
| 106 | {
|
|---|
| 107 | item -> subinfo_type = SYSINFO_SUBINFO_TABLE;
|
|---|
| 108 | return sysinfo_create_path(name+i+1,&(item->subinfo.table));
|
|---|
| 109 | }
|
|---|
| 110 | item -> subinfo_type = SYSINFO_SUBINFO_NONE;
|
|---|
| 111 | return item;
|
|---|
| 112 |
|
|---|
| 113 | }
|
|---|
| 114 | else
|
|---|
| 115 | {
|
|---|
| 116 | subtree=subtree->next;
|
|---|
| 117 | i=0;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | panic("Not reached\n");
|
|---|
| 121 | return NULL;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | void sysinfo_set_item_val(const char *name,sysinfo_item_t **root,__native val)
|
|---|
| 125 | {
|
|---|
| 126 | if(root==NULL) root=&_root;
|
|---|
| 127 | sysinfo_item_t *item;
|
|---|
| 128 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer
|
|---|
| 129 | If ! , create it */
|
|---|
| 130 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */
|
|---|
| 131 | {
|
|---|
| 132 | item->val.val=val;
|
|---|
| 133 | item -> val_type = SYSINFO_VAL_VAL;
|
|---|
| 134 | }
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | void sysinfo_set_item_function(const char *name,sysinfo_item_t **root,sysinfo_val_fn_t fn)
|
|---|
| 138 | {
|
|---|
| 139 | if(root==NULL) root=&_root;
|
|---|
| 140 | sysinfo_item_t *item;
|
|---|
| 141 | item = sysinfo_create_path(name,root); /* If already created create only returns pointer
|
|---|
| 142 | If ! , create it */
|
|---|
| 143 | if(item!=NULL) /* If in subsystem, unable to create or return so unable to set */
|
|---|
| 144 | {
|
|---|
| 145 | item->val.fn=fn;
|
|---|
| 146 | item -> val_type = SYSINFO_VAL_FUNCTION;
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 | void sysinfo_set_item_undefined(const char *name,sysinfo_item_t **root)
|
|---|
| 152 | {
|
|---|
| 153 | if(root==NULL) root=&_root;
|
|---|
| 154 | sysinfo_item_t *item;
|
|---|
| 155 | item = sysinfo_find_item(name,*root);
|
|---|
| 156 | if(item!=NULL) item -> val_type = SYSINFO_VAL_UNDEFINED;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 | void sysinfo_dump(sysinfo_item_t **proot,int depth)
|
|---|
| 161 | {
|
|---|
| 162 | sysinfo_item_t *root;
|
|---|
| 163 | if(proot==NULL) proot=&_root;
|
|---|
| 164 | root = *proot;
|
|---|
| 165 |
|
|---|
| 166 | while(root!=NULL)
|
|---|
| 167 | {
|
|---|
| 168 |
|
|---|
| 169 | int i;
|
|---|
| 170 | __native val=0;
|
|---|
| 171 | char *vtype=NULL;
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 | for(i=0;i<depth;i++) printf(" ");
|
|---|
| 175 |
|
|---|
| 176 | switch (root->val_type)
|
|---|
| 177 | {
|
|---|
| 178 | case (SYSINFO_VAL_UNDEFINED):
|
|---|
| 179 | val=0;
|
|---|
| 180 | vtype="UND";
|
|---|
| 181 | break;
|
|---|
| 182 | case (SYSINFO_VAL_VAL):
|
|---|
| 183 | val=root->val.val;
|
|---|
| 184 | vtype="VAL";
|
|---|
| 185 | break;
|
|---|
| 186 | case (SYSINFO_VAL_FUNCTION):
|
|---|
| 187 | val=((sysinfo_val_fn_t)(root->val.fn))(root);
|
|---|
| 188 | vtype="FUN";
|
|---|
| 189 | break;
|
|---|
| 190 | }
|
|---|
| 191 | 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 |
|
|---|
| 194 |
|
|---|
| 195 | if(root -> subinfo_type == SYSINFO_SUBINFO_TABLE)
|
|---|
| 196 | sysinfo_dump(&(root->subinfo.table),depth+1);
|
|---|
| 197 | root=root->next;
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | sysinfo_rettype_t sysinfo_get_val(const char *name,sysinfo_item_t **root)
|
|---|
| 202 | {
|
|---|
| 203 | /*TODO:
|
|---|
| 204 | Implement Subsystem subinfo (by function implemented subinfo)
|
|---|
| 205 | */
|
|---|
| 206 |
|
|---|
| 207 | sysinfo_rettype_t ret={0,false};
|
|---|
| 208 |
|
|---|
| 209 | if(root==NULL) root=&_root;
|
|---|
| 210 | sysinfo_item_t *item;
|
|---|
| 211 | item = sysinfo_find_item(name,*root);
|
|---|
| 212 | if(item!=NULL)
|
|---|
| 213 | {
|
|---|
| 214 | if (item -> val_type == SYSINFO_VAL_UNDEFINED)
|
|---|
| 215 | return ret;
|
|---|
| 216 | else ret.valid=true;
|
|---|
| 217 | if (item -> val_type == SYSINFO_VAL_VAL)
|
|---|
| 218 | ret.val=item -> val.val;
|
|---|
| 219 | else ret.val=((sysinfo_val_fn_t)(item->val.fn))(item);
|
|---|
| 220 | }
|
|---|
| 221 | return ret;
|
|---|
| 222 | }
|
|---|