Subversion Repositories HomeAutomation

Rev

Rev 1314 | Rev 1326 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1314 Rev 1323
Line 4... Line 4...
4
 *  Created on: Jul 23, 2009
4
 *  Created on: Jul 23, 2009
5
 *      Author: Mattias Runge
5
 *      Author: Mattias Runge
6
 */
6
 */
7
 
7
 
8
#include "Header.h"
8
#include "Header.h"
-
 
9
 
-
 
10
#include "message/types/UnsignedInteger.hpp"
9
 
11
 
10
namespace atom {
12
namespace atom {
11
namespace message {
13
namespace message {
12
 
14
 
13
Header::Header()
15
Header::Header()
14
{
16
{
-
 
17
}
-
 
18
 
-
 
19
Header::Header(BitBuffer & buffer)
-
 
20
{
-
 
21
    this->readBits(buffer);
-
 
22
}
-
 
23
 
-
 
24
Header::Header(unsigned long moduleId, string moduleType, string messageType)
-
 
25
{
-
 
26
    if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get()))
-
 
27
    {
-
 
28
        moduleIdDatatype->setValue(moduleId);
-
 
29
    }
-
 
30
    else
-
 
31
    {
-
 
32
        // TODO throw exception
-
 
33
    }
-
 
34
 
-
 
35
    if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get()))
-
 
36
    {
-
 
37
        unsigned long moduleTypeId;
-
 
38
 
-
 
39
        // TODO convert moduleType to moduleTypeId
-
 
40
 
-
 
41
        moduleTypeDatatype->setValue(moduleTypeId);
-
 
42
    }
-
 
43
    else
-
 
44
    {
-
 
45
        // TODO throw exception
-
 
46
    }
-
 
47
 
-
 
48
    if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get()))
-
 
49
    {
-
 
50
        unsigned long messageTypeId;
-
 
51
 
-
 
52
        // TODO convert messageType to messageTypeId
-
 
53
 
-
 
54
        messageTypeDatatype->setValue(messageTypeId);
-
 
55
    }
-
 
56
    else
-
 
57
    {
-
 
58
        // TODO throw exception
-
 
59
    }
-
 
60
}
-
 
61
 
-
 
62
Header::~Header()
-
 
63
{
-
 
64
}
-
 
65
 
-
 
66
void Header::loadVariables()
-
 
67
{
15
    xml::Node::nodeList nodes = Protocol::getInstance()->getRootNode().selectFirst("header").select("variable");
68
    xml::Node::nodeList nodes = Protocol::getInstance()->getRootNode().selectFirst("header").select("variable");
16
 
69
 
17
    for (unsigned int n = 0; n < nodes.size(); n++)
70
    for (unsigned int n = 0; n < nodes.size(); n++)
18
    {
71
    {
19
        Variable variable(nodes[n]["name"], nodes[n]["datatype"], convert::string2bool(nodes[n]["required"]), nodes[n]["unit"]);
72
        Variable variable(nodes[n]);
20
        this->myVariables[nodes[n]["name"]] = variable;
73
        this->myVariables[nodes[n]["name"]] = variable;
21
    }
-
 
22
}
74
    }
23
 
75
 
-
 
76
    // TODO check that moduleType, moduleId and messageId are present
-
 
77
}
-
 
78
 
-
 
79
unsigned long Header::getModuleId()
-
 
80
{
-
 
81
    if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get()))
-
 
82
    {
-
 
83
        return moduleIdDatatype->getValue();
-
 
84
    }
-
 
85
    else
-
 
86
    {
-
 
87
        // TODO throw exception
-
 
88
    }
-
 
89
}
-
 
90
 
-
 
91
string Header::getModuleType()
-
 
92
{
-
 
93
    if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get()))
-
 
94
    {
-
 
95
        unsigned long moduleTypeId = moduleTypeDatatype->getValue();
-
 
96
 
-
 
97
        string moduleType;
-
 
98
 
-
 
99
        // TODO convert moduleTypeId to moduleType
-
 
100
 
-
 
101
        return moduleType;
-
 
102
    }
-
 
103
    else
-
 
104
    {
-
 
105
        // TODO throw exception
-
 
106
    }
-
 
107
}
-
 
108
 
24
Header::~Header()
109
string Header::getMessageType()
-
 
110
{
-
 
111
    if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get()))
-
 
112
    {
-
 
113
        unsigned long messageTypeId = messageTypeDatatype->getValue();
-
 
114
 
-
 
115
        string messageType;
-
 
116
 
-
 
117
        // TODO convert messageTypeId to messageType
-
 
118
 
-
 
119
        return messageType;
-
 
120
    }
-
 
121
    else
-
 
122
    {
-
 
123
        // TODO throw exception
-
 
124
    }
-
 
125
}
-
 
126
 
-
 
127
void Header::readBits(BitBuffer & buffer)
25
{
128
{
-
 
129
    // Header is only 29 bits but our buffer is whole bytes, padded in front
-
 
130
    buffer.incrementPointer(3); // Increment 3 bits
-
 
131
 
-
 
132
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
-
 
133
    {
-
 
134
        iter->second.readBits(buffer);
-
 
135
    }
26
}
136
}
27
 
137
 
28
Variable & Header::getVariable(string name)
138
void Header::writeBits(BitBuffer & buffer)
29
{
139
{
30
    return this->myVariables[name];
140
    // Header is only 29 bits but our buffer is whole bytes, padded in front
31
 
-
 
32
    // TODO Check if it does not exist, throw exception?
141
    buffer.writeBasicType(3, (long) 0); // Write 3 bits
33
}
-
 
34
 
142
 
35
void Header::readBits(BitBuffer & buffer)
-
 
36
{
-
 
37
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
143
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
38
    {
144
    {
39
        iter->second.readBits(buffer);
145
        iter->second.writeBits(buffer);
40
    }
146
    }
41
}
147
}
42
 
148
 
43
}
149
}
44
}
150
}