Subversion Repositories HomeAutomation

Rev

Rev 1314 | Rev 1326 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1309 runge 1
/*
2
 * Header.cpp
3
 *
4
 *  Created on: Jul 23, 2009
5
 *      Author: Mattias Runge
6
 */
7
 
8
#include "Header.h"
9
 
1323 runge 10
#include "message/types/UnsignedInteger.hpp"
11
 
1309 runge 12
namespace atom {
13
namespace message {
14
 
15
Header::Header()
16
{
1323 runge 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
{
1311 runge 68
    xml::Node::nodeList nodes = Protocol::getInstance()->getRootNode().selectFirst("header").select("variable");
1310 runge 69
 
70
    for (unsigned int n = 0; n < nodes.size(); n++)
71
    {
1323 runge 72
        Variable variable(nodes[n]);
1310 runge 73
        this->myVariables[nodes[n]["name"]] = variable;
74
    }
1323 runge 75
 
76
    // TODO check that moduleType, moduleId and messageId are present
1309 runge 77
}
78
 
1323 runge 79
unsigned long Header::getModuleId()
1309 runge 80
{
1323 runge 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
    }
1309 runge 89
}
90
 
1323 runge 91
string Header::getModuleType()
1309 runge 92
{
1323 runge 93
    if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get()))
94
    {
95
        unsigned long moduleTypeId = moduleTypeDatatype->getValue();
1309 runge 96
 
1323 runge 97
        string moduleType;
98
 
99
        // TODO convert moduleTypeId to moduleType
100
 
101
        return moduleType;
102
    }
103
    else
104
    {
105
        // TODO throw exception
106
    }
1309 runge 107
}
108
 
1323 runge 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
 
1309 runge 127
void Header::readBits(BitBuffer & buffer)
128
{
1323 runge 129
    // Header is only 29 bits but our buffer is whole bytes, padded in front
130
    buffer.incrementPointer(3); // Increment 3 bits
131
 
1309 runge 132
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
133
    {
134
        iter->second.readBits(buffer);
135
    }
136
}
137
 
1323 runge 138
void Header::writeBits(BitBuffer & buffer)
139
{
140
    // Header is only 29 bits but our buffer is whole bytes, padded in front
141
    buffer.writeBasicType(3, (long) 0); // Write 3 bits
142
 
143
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
144
    {
145
        iter->second.writeBits(buffer);
146
    }
1309 runge 147
}
1323 runge 148
 
1309 runge 149
}
1323 runge 150
}