root / logic / trunk / src / init / devicelist.c @ 41
History | View | Annotate | Download (3.52 KB)
1 |
#include "devicelist.h" |
---|---|
2 |
#include <string.h> |
3 |
#include <mxml.h> |
4 |
#include <time.h> |
5 |
|
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 |
|
42 |
// fill the list of known devices
|
43 |
while(node!=NULL) |
44 |
{ |
45 |
curr = (device *)malloc(sizeof(device));
|
46 |
|
47 |
curr->id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
48 |
strcpy(curr->id, node->value.element.attrs->value); |
49 |
//printf("id:\t %s\n", curr->id );
|
50 |
|
51 |
// getting data for individual device
|
52 |
mxml_node_t *mxml_deviceNode; |
53 |
mxml_deviceNode=node->child; |
54 |
//TODO malloc()
|
55 |
curr->name = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
56 |
strcpy(curr->name, mxml_deviceNode->value.element.attrs->value); |
57 |
mxml_deviceNode= mxml_deviceNode->next; |
58 |
//printf("name:\t %s\n", curr->name );
|
59 |
|
60 |
curr->readitv = atoi(mxml_deviceNode->value.element.attrs->value); |
61 |
mxml_deviceNode= mxml_deviceNode->next; |
62 |
//printf("readitv:\t %d s\n", curr->readitv );
|
63 |
|
64 |
curr->action = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
65 |
strcpy(curr->action, mxml_deviceNode->value.element.attrs->value); |
66 |
mxml_deviceNode= mxml_deviceNode->next; |
67 |
//printf("action:\t %s\n", curr->action );
|
68 |
|
69 |
curr->param = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value)); |
70 |
strcpy(curr->param, mxml_deviceNode->value.element.attrs->value); |
71 |
//printf("param:\t %s\n", curr->param );
|
72 |
curr->buf = 0;
|
73 |
curr->running = 0;
|
74 |
// moving to the next XML node
|
75 |
node=node->next; |
76 |
|
77 |
curr->nxt = head; |
78 |
head = curr; |
79 |
|
80 |
NUMBER_OF_DEVICES++; |
81 |
} |
82 |
|
83 |
//delete obsolete xml tree
|
84 |
mxmlDelete(tree); |
85 |
mxmlDelete(node); |
86 |
|
87 |
return head;
|
88 |
} |
89 |
|
90 |
int getNumberOfDevices()
|
91 |
{ |
92 |
return NUMBER_OF_DEVICES;
|
93 |
} |
94 |
|
95 |
device *getDevice1(char *devName)
|
96 |
{ |
97 |
return NULL; |
98 |
} |
99 |
|
100 |
device *getDevice2(device *dev) |
101 |
{ |
102 |
return NULL; |
103 |
} |
104 |
|
105 |
device *getDevice(device *dev, char *devName)
|
106 |
{ |
107 |
while(dev)
|
108 |
{ |
109 |
if(0==strcmp(dev->id,devName)) |
110 |
{ |
111 |
return dev;
|
112 |
} |
113 |
dev=dev->nxt; |
114 |
} |
115 |
return NULL; |
116 |
} |
117 |
|
118 |
device *getAvailableDevice(device *dev) |
119 |
{ |
120 |
while(dev)
|
121 |
{ |
122 |
if(!dev->running)
|
123 |
{ |
124 |
dev->running = 1;
|
125 |
//printf("activating device %s with status %d\n", dev->id, dev->running);
|
126 |
return dev;
|
127 |
} |
128 |
dev=dev->nxt; |
129 |
} |
130 |
|
131 |
return NULL; |
132 |
} |
133 |
|
134 |
int getLocalFormattedTime()
|
135 |
{ |
136 |
// desired format hhmm, 4 digit int; 1430 = 14:30
|
137 |
time_t epoch_time; |
138 |
struct tm *tm_p;
|
139 |
int wntForm=0; |
140 |
|
141 |
epoch_time = time( NULL );
|
142 |
tm_p = localtime( &epoch_time ); |
143 |
|
144 |
return tm_p->tm_hour*100+tm_p->tm_min; |
145 |
} |
146 |
|
147 |
/*
|
148 |
void updateDeviceList(device_d *deviceList)
|
149 |
{
|
150 |
|
151 |
}
|
152 |
/**/
|