root / logic / trunk / src / init / devicelist.c @ 34
History | View | Annotate | Download (3.47 KB)
1 |
#include "devicelist.h" |
---|---|
2 |
#include <string.h> |
3 |
#include <mxml.h> |
4 |
|
5 |
device *hDevice; //device list
|
6 |
short NUMBER_OF_DEVICES = 0; |
7 |
|
8 |
device *setDeviceList() |
9 |
{ |
10 |
device *curr, *head; |
11 |
head = NULL;
|
12 |
|
13 |
FILE *fp; |
14 |
mxml_node_t *tree, *node; |
15 |
|
16 |
if(fp = fopen("./devicetree.xml", "r")) //TODO !hardcoded |
17 |
{ |
18 |
//printf("File read!\n");
|
19 |
} |
20 |
else
|
21 |
printf("Error!");
|
22 |
|
23 |
tree = mxmlLoadFile(NULL, fp, MXML_IGNORE_CALLBACK);
|
24 |
node = tree; |
25 |
fclose(fp); |
26 |
|
27 |
/** //structure of XML tree
|
28 |
?xml
|
29 |
|
|
30 |
devicetree
|
31 |
|
|
32 |
dev1 - dev2 - ...
|
33 |
| |
|
34 |
| |
|
35 |
| ID - name - minvalue - maxvalue - action - param
|
36 |
ID - name - minvalue - maxvalue - action - param
|
37 |
/**/
|
38 |
|
39 |
//getting to the correct level of the xml tree (descend 2 levels)
|
40 |
node=node->child->child; |
41 |
// printf("hDevice %d\n", hDevice);
|
42 |
|
43 |
// fill the list of known devices
|
44 |
while(node!=NULL) |
45 |
{ |
46 |
curr = (device *)malloc(sizeof(device));
|
47 |
|
48 |
curr->id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
49 |
strcpy(curr->id, node->value.element.attrs->value); |
50 |
//printf("id:\t %s\n", curr->id );
|
51 |
|
52 |
// getting data for individual device
|
53 |
mxml_node_t *mxml_deviceNode; |
54 |
mxml_deviceNode=node->child; |
55 |
//TODO malloc()
|
56 |
curr->name = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
57 |
strcpy(curr->name, mxml_deviceNode->value.element.attrs->value); |
58 |
mxml_deviceNode= mxml_deviceNode->next; |
59 |
//printf("name:\t %s\n", curr->name );
|
60 |
|
61 |
curr->readitv = atoi(mxml_deviceNode->value.element.attrs->value); |
62 |
mxml_deviceNode= mxml_deviceNode->next; |
63 |
//printf("readitv:\t %d s\n", curr->readitv );
|
64 |
|
65 |
curr->action = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
66 |
strcpy(curr->action, mxml_deviceNode->value.element.attrs->value); |
67 |
mxml_deviceNode= mxml_deviceNode->next; |
68 |
//printf("action:\t %s\n", curr->action );
|
69 |
|
70 |
curr->param = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
71 |
strcpy(curr->param, mxml_deviceNode->value.element.attrs->value); |
72 |
//printf("param:\t %s\n", curr->param );
|
73 |
|
74 |
curr->running = 0;
|
75 |
// moving to the next XML node
|
76 |
node=node->next; |
77 |
|
78 |
curr->nxt = head; |
79 |
head = curr; |
80 |
|
81 |
NUMBER_OF_DEVICES++; |
82 |
} |
83 |
|
84 |
//delete obsolete xml tree
|
85 |
mxmlDelete(tree); |
86 |
mxmlDelete(node); |
87 |
|
88 |
return head;
|
89 |
} |
90 |
|
91 |
int getNumberOfDevices()
|
92 |
{ |
93 |
return NUMBER_OF_DEVICES;
|
94 |
} |
95 |
|
96 |
// TODO add this func
|
97 |
device *getDevice(device *dev, char *devName)
|
98 |
{ |
99 |
printf("getDevice(device *dev, char *devName)\n");
|
100 |
printf("%s\n",devName);
|
101 |
while(dev)
|
102 |
{ |
103 |
printf("loop\n");
|
104 |
printf("dev->id %s\n", dev->id);
|
105 |
if(0==strcmp(dev->id,devName)) |
106 |
{ |
107 |
printf("hooking device %s with status %d\n", dev->id, dev->running);
|
108 |
return dev;
|
109 |
} |
110 |
dev=dev->nxt; |
111 |
} |
112 |
printf("Device not found!\n");
|
113 |
return NULL; |
114 |
} |
115 |
|
116 |
device *getAvailableDevice(device *dev) |
117 |
{ |
118 |
while(dev)
|
119 |
{ |
120 |
if(!dev->running)
|
121 |
{ |
122 |
dev->running = 1;
|
123 |
printf("activating device %s with status %d\n", dev->id, dev->running);
|
124 |
return dev;
|
125 |
} |
126 |
dev=dev->nxt; |
127 |
} |
128 |
|
129 |
return NULL; |
130 |
} |
131 |
/*
|
132 |
void updateDeviceList(device_d *deviceList)
|
133 |
{
|
134 |
|
135 |
}
|
136 |
/**/
|