Subversion Repositories HomeAutomation

Rev

Rev 1326 | 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
{
1326 runge 17
    LOG.setName("Header");
18
    this->loadVariables();
1323 runge 19
}
20
 
21
Header::Header(BitBuffer & buffer)
22
{
1326 runge 23
    LOG.setName("Header");
24
    this->loadVariables();
1323 runge 25
    this->readBits(buffer);
26
}
27
 
28
Header::Header(unsigned long moduleId, string moduleType, string messageType)
29
{
1326 runge 30
    LOG.setName("Header");
31
 
32
    this->loadVariables();
33
 
1323 runge 34
    if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get()))
35
    {
36
        moduleIdDatatype->setValue(moduleId);
37
    }
38
    else
39
    {
1326 runge 40
        throw new Exception("moduleId is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
1323 runge 41
    }
42
 
43
    if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get()))
44
    {
1326 runge 45
        unsigned long moduleTypeId = convert::string2uint(Protocol::getInstance()->getRootCacheNode().selectFirst("module", xml::Node::attributePair("name", moduleType))["id"]);
1323 runge 46
 
47
        moduleTypeDatatype->setValue(moduleTypeId);
48
    }
49
    else
50
    {
1326 runge 51
        throw new Exception("moduletype is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
1323 runge 52
    }
53
 
54
    if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get()))
55
    {
1326 runge 56
        unsigned long messageTypeId = convert::string2uint(Protocol::getInstance()->getRootCacheNode().selectFirst("messagetype", xml::Node::attributePair("name", messageType))["id"]);
1323 runge 57
 
58
        messageTypeDatatype->setValue(messageTypeId);
59
    }
60
    else
61
    {
1326 runge 62
        throw new Exception("messageType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
1323 runge 63
    }
64
}
65
 
66
Header::~Header()
67
{
68
}
69
 
70
void Header::loadVariables()
71
{
1311 runge 72
    xml::Node::nodeList nodes = Protocol::getInstance()->getRootNode().selectFirst("header").select("variable");
1310 runge 73
 
74
    for (unsigned int n = 0; n < nodes.size(); n++)
75
    {
1323 runge 76
        Variable variable(nodes[n]);
1310 runge 77
        this->myVariables[nodes[n]["name"]] = variable;
78
    }
1323 runge 79
 
80
    // TODO check that moduleType, moduleId and messageId are present
1309 runge 81
}
82
 
1323 runge 83
unsigned long Header::getModuleId()
1309 runge 84
{
1323 runge 85
    if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get()))
86
    {
87
        return moduleIdDatatype->getValue();
88
    }
1326 runge 89
 
90
    throw new Exception("moduleIdDatatype is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
1309 runge 91
}
92
 
1323 runge 93
string Header::getModuleType()
1309 runge 94
{
1323 runge 95
    if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get()))
96
    {
97
        unsigned long moduleTypeId = moduleTypeDatatype->getValue();
1309 runge 98
 
1326 runge 99
        string moduleType = Protocol::getInstance()->getRootCacheNode().selectFirst("module", xml::Node::attributePair("id", convert::uint2string(moduleTypeId)))["name"];
1323 runge 100
 
101
        return moduleType;
102
    }
1326 runge 103
 
104
    throw new Exception("moduleType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
1309 runge 105
}
106
 
1323 runge 107
string Header::getMessageType()
108
{
109
    if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get()))
110
    {
111
        unsigned long messageTypeId = messageTypeDatatype->getValue();
112
 
1326 runge 113
        string messageType = Protocol::getInstance()->getRootCacheNode().selectFirst("messagetype", xml::Node::attributePair("id", convert::uint2string(messageTypeId)))["name"];
1323 runge 114
 
115
        return messageType;
116
    }
1326 runge 117
 
118
    throw new Exception("messageType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification");
1323 runge 119
}
120
 
1309 runge 121
void Header::readBits(BitBuffer & buffer)
122
{
1323 runge 123
    // Header is only 29 bits but our buffer is whole bytes, padded in front
124
    buffer.incrementPointer(3); // Increment 3 bits
125
 
1309 runge 126
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
127
    {
128
        iter->second.readBits(buffer);
129
    }
130
}
131
 
1323 runge 132
void Header::writeBits(BitBuffer & buffer)
133
{
134
    // Header is only 29 bits but our buffer is whole bytes, padded in front
1326 runge 135
    buffer.write(3, (long) 0); // Write 3 bits
1323 runge 136
 
137
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
138
    {
139
        iter->second.writeBits(buffer);
140
    }
1309 runge 141
}
1323 runge 142
 
1326 runge 143
string Header::toString()
144
{
145
    string buffer = "Header(";
146
 
147
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
148
    {
149
        buffer += iter->second.toString() + ",";
150
    }
151
 
152
    trim_if(buffer, is_any_of(","));
153
 
154
    buffer += ")";
155
 
156
    return buffer;
1309 runge 157
}
1326 runge 158
 
1323 runge 159
}
1326 runge 160
}