Revision 34

View differences:

logic/trunk/src/init/devicelist.c
1 1
#include "devicelist.h"
2
//#include "buf.h"
3 2
#include <string.h>
4 3
#include <mxml.h>
5 4

  
6 5
device *hDevice; //device list
6
short NUMBER_OF_DEVICES   = 0;
7 7

  
8
int setDeviceList()
8
device *setDeviceList()
9 9
{
10 10
    device *curr, *head;
11 11
    head = NULL;
......
13 13
    FILE *fp;
14 14
    mxml_node_t *tree, *node;
15 15

  
16
	if(fp = fopen("./src/init/devicetree.xml", "r")) //TODO !hardcoded
16
	if(fp = fopen("./devicetree.xml", "r")) //TODO !hardcoded
17 17
	{
18
        printf("File read!\n");
18
        //printf("File read!\n");
19 19
	}
20 20
	else
21 21
        printf("Error!");
......
24 24
	node = tree;
25 25
	fclose(fp);
26 26

  
27
    /* //structure of XML tree
27
    /** //structure of XML tree
28 28
    ?xml
29 29
      |
30 30
    devicetree
......
35 35
      |     ID - name - minvalue - maxvalue - action - param
36 36
    ID - name - minvalue - maxvalue - action - param
37 37
    /**/
38

  
38 39
    //getting to the correct level of the xml tree (descend 2 levels)
39 40
    node=node->child->child;
40 41
    // printf("hDevice %d\n", hDevice);
41 42

  
42 43
    // fill the list of known devices
43
    //      sorry for the ugly chunk of code, but I had to allocate memory
44
    // for each string separately in order to keep it scalable
45 44
    while(node!=NULL)
46 45
    {
47 46
        curr = (device *)malloc(sizeof(device));
48 47

  
49 48
        curr->id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(node->value.element.attrs->value));
50 49
        strcpy(curr->id, node->value.element.attrs->value);
51
        printf("id:\t %s\n", curr->id );
52
        printf("----------------------------------------------\n");
53
        //*
50
        //printf("id:\t %s\n", curr->id );
51

  
54 52
        // getting data for individual device
55 53
        mxml_node_t *mxml_deviceNode;
56 54
        mxml_deviceNode=node->child;
......
58 56
        curr->name = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value));
59 57
        strcpy(curr->name, mxml_deviceNode->value.element.attrs->value);
60 58
            mxml_deviceNode= mxml_deviceNode->next;
61
        printf("name:\t %s\n", curr->name );
59
        //printf("name:\t %s\n", curr->name );
62 60

  
63 61
        curr->readitv = atoi(mxml_deviceNode->value.element.attrs->value);
64 62
            mxml_deviceNode= mxml_deviceNode->next;
65
        printf("readitv:\t %d s\n", curr->readitv );
63
        //printf("readitv:\t %d s\n", curr->readitv );
66 64

  
67 65
        curr->action = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value));
68 66
        strcpy(curr->action, mxml_deviceNode->value.element.attrs->value);
69 67
            mxml_deviceNode= mxml_deviceNode->next;
70
        printf("action:\t %s\n", curr->action );
68
        //printf("action:\t %s\n", curr->action );
71 69

  
72 70
        curr->param = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value)*strlen(node->value.element.attrs->value));
73 71
        strcpy(curr->param, mxml_deviceNode->value.element.attrs->value);
74
        printf("param:\t %s\n", curr->param );
72
        //printf("param:\t %s\n", curr->param );
75 73

  
76
        // done
77
        /*
78
        // TA DEL KODE V TEM KOMENTARJU NE BO UPORABLJEN, IGNORE!
79
        // TODO create buffer
80
        curr->buf = init_buf((int)(HISTORY_LENGTH/curr->readitv));
81
        // -------------- buffer test-----------------------------!
82
        //printf("curr buf_size %d\n", curr->buf->buffer_size);
83
        //printf("curr buf_load %d\n", curr->buf->buffer_load);
84
        //printf("buf_pos %d\n", &curr->buf);
85
        //*
86
        int num;
87
        buf_put(10, curr->buf);
88
        //printf("num %d\n",*curr->buf->start_el );
89
        buf_get(&num, curr->buf);
90
        printf("num %d\n",num);
91
        // -------------- buffer test end-------------------------!
92
        /**/
74
        curr->running = 0;
93 75
        // moving to the next XML node
94 76
        node=node->next;
95 77

  
96 78
        curr->nxt = head;
97 79
        head = curr;
80

  
81
        NUMBER_OF_DEVICES++;
98 82
    }
99
    hDevice=head; // handle to device list
83

  
100 84
    //delete obsolete xml tree
101 85
    mxmlDelete(tree);
102 86
    mxmlDelete(node);
103
    /*
104
    // TODO delete - test
105
    curr=head;
106
    printf("test device list\n");
107
    // DEBUG
108
    while(curr)
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)
109 102
    {
110
        printf("name:\t %s\n", curr->name );
111
        printf("id:\t %s\n", curr->id );
112
        printf("readitv:\t %d s\n", curr->readitv );
113
        printf("action:\t %s\n", curr->action );
114
        printf("param:\t %s\n", curr->param );
115
        curr=curr->nxt;
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;
116 111
    }
117
    /**/
118
    return 1;
112
    printf("Device not found!\n");
113
    return NULL;
119 114
}
120 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
}
121 131
/*
122 132
void updateDeviceList(device_d *deviceList)
123 133
{
logic/trunk/src/init/scenariolist.c
3 3
#include <string.h>
4 4
#include <mxml.h>
5 5

  
6
short NUMBER_OF_SCENARIOS = 0;
6 7

  
8
int getOperatorType(char *val)
9
{
10
    if(0 == strcmp(val, "conditions"))
11
        return TOP_NODE;
12
    else if(0 == strcmp(val, "and"))
13
        return AND;
14
    else if(0 == strcmp(val, "nand"))
15
        return NOT_AND;
16
    else if(0 == strcmp(val, "or"))
17
        return OR;
18
    else if(0 == strcmp(val, "xor"))
19
        return XOR;
20
    else if(0 == strcmp(val, "greater"))
21
        return GREATER;
22
    else if(0 == strcmp(val, "greater_or_equal"))
23
        return GREATER_OR_EQUAL;
24
    else if(0 == strcmp(val, "less"))
25
        return LESS;
26
    else if(0 == strcmp(val, "less_or_equal"))
27
        return LESS_OR_EQUAL;
28
    else
29
        printf("Unidentified condition \"%s\".\n", val);
30
    return NULL;
31
    /**/
32
}
7 33

  
8
scenario *hScenario; //scenario list
34
cond_lst *getScenCond(mxml_node_t *node_t)
35
{
36
    cond_lst *cl, *head;
37
    head = NULL;
38
    //cl->OPERATOR_TYPE =
39
    while(node_t) // same level
40
    {
41
        //printf("operator name: %s\n", node_t->value.opaque);
42
        //*
43
        cl = (cond_lst *)malloc(sizeof(cond_lst));
9 44

  
10
//parseScenCondLeaf()
11
//parseScenCondNode()
45
        /** possible BUG, random pointer values (uncomment this block)
46
        cl->OPERATOR_TYPE = NULL;
47
        cl->dev = NULL;
48
        cl->nxt_lst = NULL;
49
        cl->sub_lst = NULL;
50
        cl->value = NULL;
51
        /**/
52
        (*cl).OPERATOR_TYPE = getOperatorType(node_t->value.opaque);
53
        //printf("cl->OPERATOR_TYPE %d\n", cl->OPERATOR_TYPE);
12 54

  
13
cond_lst *parseScenCond()
14
{
15
    return NULL;
55
        if(node_t->child)
56
        {
57
            //printf("create child\n");
58
            cl->sub_lst = getScenCond(node_t->child);
59
        }
60
        else
61
        {
62
            //printf("create leaf\n");
63
            cl->dev = (char *)malloc(sizeof(node_t->value.element.attrs->value)*strlen(mxmlElementGetAttr(node_t, "inDeviceId")));
64
            cl->dev = mxmlElementGetAttr(node_t, "inDeviceId");
65
            //printf("cl->dev %s\n", cl->dev);
66

  
67
            (*cl).value = atoi(mxmlElementGetAttr(node_t, "val"));
68
            //printf("cl->value %d\n", (*cl).value);
69
        }
70

  
71
        node_t=node_t->next;
72

  
73
        cl->nxt_lst = head;
74
        head = cl;
75
    }
76
    return cl;
16 77
}
17 78

  
18
int setNewScenarioList() // ony called on startup
79
scenario *setNewScenarioList() // ony called on startup
19 80
{
20 81
    //TODO vedno preverjaj, ce so vse naprave, ki jih uporabljas, prisotne! npr. termometer mora biti v device list-u
21 82
    scenario *curr, *head;
......
24 85
    FILE *fp;
25 86
    mxml_node_t *tree, *node;
26 87

  
27
	if(fp = fopen("./src/init/scenariotree.xml", "r")) //TODO !hardcoded
88
	if(fp = fopen("./scenariotree.xml", "r")) //TODO !hardcoded
28 89
	{
29
        printf("File read!\n");
90
        //printf("File read!\n");
30 91
        //TODO replace with syslog error
31 92
	}
32 93
	else
33
        printf("Error!");
94
        printf("Error file not found!");
34 95
        //TODO replace with syslog error
35 96

  
36 97
	tree = mxmlLoadFile(NULL, fp, MXML_IGNORE_CALLBACK);
37 98
	node = tree;
38 99
	fclose(fp);
39 100

  
40
    /* //structure of XML tree
41
    ?xml
42
      |
43
    devicetree
44
      |
45
    dev1 - dev2 - ...
46
      |      |
47
      |      |
48
      |     ID - name - minvalue - maxvalue - action - param
49
    ID - name - minvalue - maxvalue - action - param
50
    /**/
51
    //getting to the correct level of the xml tree (descend 2 levels)
101
    //getting to the correct level of the xml tree (descend 2 levels) TODO add null check
52 102
    node=node->child->child;
53
    // printf("hDevice %d\n", hDevice);
54 103

  
55
    // fill the list of known devices
56
    //      sorry for the ugly chunk of code, but I had to allocate memory
57
    // for each string separately in order to keep it scalable
104
    // fill the list of scenarios
58 105
    while(node!=NULL)
59 106
    {
60 107
        curr = (scenario *)malloc(sizeof(scenario));
......
62 109
        /// char    *scen_id;
63 110
        curr->scen_id = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "id")));
64 111
        curr->scen_id = mxmlElementGetAttr(node, "id"); // possible BUG, perhaps use strcpy here!
65
        printf("id:\t %s\n", curr->scen_id );
112
        //printf("id:\t %s\n", curr->scen_id );
66 113

  
67 114
        /// char    *scen_name;
68 115
        curr->scen_name = (char *)malloc(sizeof(node->value.element.attrs->value)*strlen(mxmlElementGetAttr(node, "name")));
69 116
        curr->scen_name = mxmlElementGetAttr(node, "name"); // possible BUG, perhaps use strcpy here!
70
        printf("scenario name:\t %s\n", curr->scen_name);
117
        //printf("scenario name:\t %s\n", curr->scen_name);
71 118

  
72 119
        // getting data for individual scenario
73 120
        mxml_node_t *mxml_scenarioNode;
......
77 124
        curr->inDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id")));
78 125
        curr->inDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
79 126
            mxml_scenarioNode = mxml_scenarioNode->next;
80
        printf("inDevice_id:\t %s\n", curr->inDevice_id );
127
        //printf("inDevice_id:\t %s\n", curr->inDevice_id );
81 128

  
82 129
        /// char    *outDevice_id;
83 130
        curr->outDevice_id = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "id")));
84 131
        curr->outDevice_id = mxmlElementGetAttr(mxml_scenarioNode, "id");
85 132
            mxml_scenarioNode = mxml_scenarioNode->next;
86
        printf("outDevice_id:\t %s\n", curr->outDevice_id );
133
        //printf("outDevice_id:\t %s\n", curr->outDevice_id );
87 134

  
88 135
        /// char    *func;
89 136
        curr->func = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "func")));
90 137
        curr->func = mxmlElementGetAttr(mxml_scenarioNode, "func");
91 138
            mxml_scenarioNode = mxml_scenarioNode->next;
92
        printf("func:\t %s\n", curr->func );
139
        //printf("func:\t %s\n", curr->func );
93 140

  
94 141
        /// int     idleChkItv;
95 142
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
96 143
            mxml_scenarioNode= mxml_scenarioNode->next;
97
        printf("idleChkItv:\t %d s\n", curr->idleChkItv );
144
        //printf("idleChkItv:\t %d s\n", curr->idleChkItv );
98 145

  
99 146
        /// int     actvChkItv;
100 147
        curr->idleChkItv = atoi(mxmlElementGetAttr(mxml_scenarioNode, "val"));
101 148
            mxml_scenarioNode= mxml_scenarioNode->next;
102
        printf("idleChkItv:\t %d s\n", curr->idleChkItv );
149
        //printf("idleChkItv:\t %d s\n", curr->idleChkItv );
103 150

  
104 151
        /// char    *params_val;    // TBD kaj je to?
105 152
        curr->params_val = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "val")));
106 153
        curr->params_val = mxmlElementGetAttr(mxml_scenarioNode, "val");
107 154
            mxml_scenarioNode = mxml_scenarioNode->next;
108
        printf("params_val:\t %s\n", curr->params_val );
155
        //printf("params_val:\t %s\n", curr->params_val );
109 156

  
110 157
        /// int     minval;
111 158
        curr->minval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "minval"));
112
        printf("minval:\t %d\n", curr->minval );
159
        //printf("minval:\t %d\n", curr->minval );
113 160

  
114 161
        /// int     maxval;
115 162
        curr->maxval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "maxval"));
116
        printf("maxval:\t %d\n", curr->maxval );
163
        //printf("maxval:\t %d\n", curr->maxval );
117 164

  
118 165
        /// int     optval;
119 166
        curr->optval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "optval"));
120
        printf("optval:\t %d\n", curr->optval );
167
        //printf("optval:\t %d\n", curr->optval );
121 168

  
122 169
        /// int     tolval;
123 170
        curr->tolval = atoi(mxmlElementGetAttr(mxml_scenarioNode, "tolerance"));
124 171
            mxml_scenarioNode = mxml_scenarioNode->next;
125
        printf("tolval:\t %d\n", curr->tolval );
172
        //printf("tolval:\t %d\n", curr->tolval );
126 173

  
127 174
        /// char    *action;
128 175
        curr->action = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "act")));
129 176
        curr->action = mxmlElementGetAttr(mxml_scenarioNode, "act");
130 177
            mxml_scenarioNode = mxml_scenarioNode->next;
131
        printf("action:\t %s\n", curr->action );
178
        //printf("action:\t %s\n", curr->action );
132 179

  
133 180
        /// char    *param;
134 181
        curr->param = (char *)malloc(sizeof(mxml_scenarioNode->value.element.attrs->value)*strlen(mxmlElementGetAttr(mxml_scenarioNode, "par")));
135 182
        curr->param = mxmlElementGetAttr(mxml_scenarioNode, "par");
136 183
            mxml_scenarioNode = mxml_scenarioNode->next;
137
        printf("param:\t %s\n", curr->param );
184
        //printf("param:\t %s\n", curr->param );
138 185

  
139
        // TODO parse struct cond
186
        /// conditions
187
        curr->cond_l = getScenCond(mxml_scenarioNode);
188
        //printf("----------------------------------------------\n");
140 189

  
141
        printf("----------------------------------------------\n");
190
        //** TEST conditions
191
        getConditionsResult(curr->cond_l);
192
        /**/
193

  
194
        curr->running = 0;
142 195
        // done
143 196
        // moving to the next XML node
144 197
        node=node->next;
145

  
198
        NUMBER_OF_SCENARIOS++  ;
146 199
        /// struct  scenario_s *nxt;
147 200
        curr->nxt = head;
148 201
        head = curr;
149 202
    }
203
    /*
204
    hScenario=head; // handle to scenario list
205
    head=hScenario;
150 206

  
151
    hScenario=head; // handle to device list
207
    printf("hScenario %d\n", hScenario);
208
    printf("head %d\n", head);
209

  
210

  
211
    while(head)
212
    {
213
        printf("scenario id %s\n", head->scen_id);
214
        printf("scenario name %s\n", head->scen_name);
215
        head=head->nxt;
216
    }
152 217
    //delete obsolete xml tree
153 218
    mxmlDelete(tree);
154 219
    mxmlDelete(node);
220
    /**/
221
    return head;
222
}
155 223

  
156
    return 1;
224
int getNumberOfScenarios()
225
{
226
    return NUMBER_OF_SCENARIOS;
157 227
}
158 228

  
229
scenario *getAvailableScenario(scenario *scen)
230
{
231
    while(scen)
232
    {
233
        if(!scen->running)
234
        {
235
            scen->running = 1;
236
            printf("activating scenario %s with status %d\n", scen->scen_id, scen->running);
237
            return scen;
238
        }
239
        scen=scen->nxt;
240
    }
241

  
242
    return NULL;
243
}
159 244
/*
160 245
void updateDeviceList(device_d *deviceList)
161 246
{
logic/trunk/src/init/devicelist.h
1 1
#ifndef DEVICELIST_H_INCLUDED
2 2
#define DEVICELIST_H_INCLUDED
3

  
4
// in seconds, history for each sensor is at most 600 seconds old
5
// TODO maybe this isn't the best idea, perhaps every sensor needs different length of history
6
#define HISTORY_LENGTH 600
7

  
8
#include "buf.h"
9 3

  
10
//struct device_d ; /* Forward declaration */
11

  
12 4
typedef struct device_d
13 5
{
14 6
    char    *name;
......
16 8
    int     readitv; //in seconds
17 9
    char    *action;
18 10
    char    *param;
19
    d_bufr  *buf;
20
    //TODO add circular buffer for each device
11
    //d_bufr  *buf;
12
    int     buf;
13
    int     running;
21 14
    struct  device_d *nxt;
22 15
} device;
23 16

  
24 17
int setNewDeviceList(); // ony called on startup
18
int getNumberOfDevices();
19
device *getDevice(device *dev, char *devName);
20
device *getAvailableDevice(device *dev);
21

  
25 22
/*
26 23
void updateDeviceList(device_d *deviceList); // called on HMI change
27 24
char *getDeviceName();
logic/trunk/src/init/scenariolist.h
17 17
    int     tolval;
18 18
    char    *action;
19 19
    char    *param;
20
    int     status; // 0 - OK, 1 - fault reason 1, 2 - fault reason 2 ... etc.
20 21
    struct  condition_list *cond_l;
21
    // struct cond
22
    int     running;
22 23
    struct  scenario_s *nxt;
23 24
}scenario;
24 25

  
25
int setNewScenarioList(); // ony called on startup
26
// update scenario list
27
// get scenario (id)
28
// ...
26
scenario *setNewScenarioList(); // ony called on startup
27
//       TODO add update scenario list function
28
char     *getScenarioId(/*arg*/);
29
int      getNumberOfScenarios();
30
scenario *getAvailableScenario(scenario *scen);
31

  
29 32
#endif // SCENARIOLIST_H_INCLUDED

Also available in: Unified diff