Subversion Repositories HomeAutomation

Rev

Rev 1326 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

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