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