Statistics
| Revision:

root / logic / trunk / src / init / devicelist.c @ 24

History | View | Annotate | Download (3.62 KB)

1 10 Janez1
#include "devicelist.h"
2 19 Janez1
//#include "buf.h"
3 10 Janez1
#include <string.h>
4
#include <mxml.h>
5
6
device *hDevice; //device list
7
8 19 Janez1
int setDeviceList()
9 10 Janez1
{
10
    device *curr, *head;
11
    head = NULL;
12
13
    FILE *fp;
14
    mxml_node_t *tree, *node;
15
16 19 Janez1
        if(fp = fopen("./src/init/devicetree.xml", "r")) //TODO !hardcoded
17
        {
18
        printf("File read!\n");
19
        }
20
        else
21
        printf("Error!");
22
23 10 Janez1
        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
    //getting to the correct level of the xml tree (descend 2 levels)
39
    node=node->child->child;
40 19 Janez1
    // printf("hDevice %d\n", hDevice);
41 10 Janez1
42
    // fill the list of known devices
43 19 Janez1
    //      sorry for the ugly chunk of code, but I had to allocate memory
44 10 Janez1
    // for each string separately in order to keep it scalable
45
    while(node!=NULL)
46
    {
47
        curr = (device *)malloc(sizeof(device));
48
49
        curr->id = (char *)malloc(sizeof(node->value.element.attrs->value));
50
        strcpy(curr->id, node->value.element.attrs->value);
51 24 Janez1
        printf("id:\t %s\n", curr->id );
52 10 Janez1
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));
58
        strcpy(curr->name, mxml_deviceNode->value.element.attrs->value);
59
            mxml_deviceNode= mxml_deviceNode->next;
60 24 Janez1
        printf("name:\t %s\n", curr->name );
61 10 Janez1
62 19 Janez1
        curr->readitv = atoi(mxml_deviceNode->value.element.attrs->value);
63 10 Janez1
            mxml_deviceNode= mxml_deviceNode->next;
64 24 Janez1
        printf("readitv:\t %d s\n", curr->readitv );
65 10 Janez1
66
        curr->action = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value));
67
        strcpy(curr->action, mxml_deviceNode->value.element.attrs->value);
68
            mxml_deviceNode= mxml_deviceNode->next;
69 24 Janez1
        printf("action:\t %s\n", curr->action );
70 10 Janez1
71
        curr->param = (char *)malloc(sizeof(mxml_deviceNode->value.element.attrs->value));
72
        strcpy(curr->param, mxml_deviceNode->value.element.attrs->value);
73 24 Janez1
        printf("param:\t %s\n", curr->param );
74
75 10 Janez1
        // done
76 23 Janez1
        /*
77 24 Janez1
        // TA DEL KODE V TEM KOMENTARJU NE BO UPORABLJEN, IGNORE!
78 19 Janez1
        // TODO create buffer
79
        curr->buf = init_buf((int)(HISTORY_LENGTH/curr->readitv));
80 23 Janez1
        // -------------- buffer test-----------------------------!
81 19 Janez1
        //printf("curr buf_size %d\n", curr->buf->buffer_size);
82
        //printf("curr buf_load %d\n", curr->buf->buffer_load);
83
        //printf("buf_pos %d\n", &curr->buf);
84
        //*
85
        int num;
86
        buf_put(10, curr->buf);
87
        //printf("num %d\n",*curr->buf->start_el );
88
        buf_get(&num, curr->buf);
89
        printf("num %d\n",num);
90 23 Janez1
        // -------------- buffer test end-------------------------!
91 19 Janez1
        /**/
92 10 Janez1
        // moving to the next XML node
93
        node=node->next;
94
95
        curr->nxt = head;
96
        head = curr;
97
    }
98
    //delete obsolete xml tree
99
    mxmlDelete(tree);
100
    mxmlDelete(node);
101 19 Janez1
    //*
102 10 Janez1
    // TODO delete - test
103
    hDevice=head;
104
    curr=head;
105
    printf("test device list\n");
106 19 Janez1
    /*
107
    // DEBUG
108 10 Janez1
    while(curr)
109
    {
110
        printf("name:\t %s\n", curr->name );
111
        printf("id:\t %s\n", curr->id );
112 19 Janez1
        printf("readitv:\t %d s\n", curr->readitv );
113 10 Janez1
        printf("action:\t %s\n", curr->action );
114
        printf("param:\t %s\n", curr->param );
115
        curr=curr->nxt;
116
    }
117
    /**/
118 19 Janez1
    return 1;
119 10 Janez1
}
120
121
/*
122
void updateDeviceList(device_d *deviceList)
123
{
124

125
}
126
/**/