Revision 34 logic/trunk/src/init/devicelist.c

View differences:

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
{

Also available in: Unified diff