Subversion Repositories HomeAutomation

Rev

Rev 1314 | Rev 1326 | Go to most recent revision | 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. }
  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. {
  68.     xml::Node::nodeList nodes = Protocol::getInstance()->getRootNode().selectFirst("header").select("variable");
  69.  
  70.     for (unsigned int n = 0; n < nodes.size(); n++)
  71.     {
  72.         Variable variable(nodes[n]);
  73.         this->myVariables[nodes[n]["name"]] = variable;
  74.     }
  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.  
  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)
  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.     }
  136. }
  137.  
  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.     }
  147. }
  148.  
  149. }
  150. }
  151.