Subversion Repositories HomeAutomation

Rev

Rev 1916 | Rev 1918 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *  Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *  This program is free software; you can redistribute it and/or modify
  6.  *  it under the terms of the GNU General Public License as published by
  7.  *  the Free Software Foundation; either version 2 of the License, or
  8.  *  (at your option) any later version.
  9.  *
  10.  *  This program is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *  GNU General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU General Public License along
  16.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "Network.h"
  22.  
  23. #include <vector>
  24.  
  25. #include <boost/algorithm/string.hpp>
  26. #include <boost/lexical_cast.hpp>
  27.  
  28. #include "net/Manager.h"
  29.  
  30. #include "broker/Manager.h"
  31.  
  32. #include "Protocol.h"
  33. #include "Message.h"
  34.  
  35. #include "common/Bitset.h"
  36. #include "common/common.h"
  37.  
  38. namespace atom {
  39. namespace can {
  40.  
  41. enum
  42. {
  43.     PACKET_START = 253,
  44.     PACKET_END   = 250,
  45.     PACKET_PING  = 251
  46. };
  47.    
  48. Network::Network(std::string address): broker::Subscriber(false), buffer_(2048), LOG("can::Network")
  49. {
  50.     this->address_ = address;
  51.     this->client_id_ = 0;
  52.        
  53.     // Examples of address
  54.     // udp:192.168.1.250:1100
  55.     // serial:/dev/ttyUSB0:38400
  56.    
  57.     common::StringList parts;
  58.     boost::algorithm::split(parts, address, boost::is_any_of(":"), boost::algorithm::token_compress_off);
  59.    
  60.     if (parts.size() < 3)
  61.     {
  62.         LOG.Error("Malformed address string: " + address);
  63.         return;
  64.     }
  65.    
  66.     boost::algorithm::to_lower(parts[0]);
  67.    
  68.     if (parts[0] == "udp")
  69.     {
  70.         this->protocol_ = net::PROTOCOL_UDP;
  71.     }
  72.     else if (parts[0] == "serial")
  73.     {
  74.         this->protocol_ = net::PROTOCOL_SERIAL;
  75.     }
  76.     else
  77.     {
  78.         LOG.Error("Unknown protocol, only support udp and serial, got " + parts[0]);
  79.         return;
  80.     }
  81.    
  82.     this->address_ = parts[1];
  83.     this->port_or_baud_ = boost::lexical_cast<unsigned int>(parts[2]);
  84.    
  85.     try
  86.     {
  87.         this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_);
  88.         LOG.Info("Connected to " + address);
  89.        
  90.         LOG.Info("Sending ping...");
  91.  
  92.         common::Byteset buffer(1);
  93.         buffer[0] = PACKET_PING;
  94.        
  95.         net::Manager::Instance()->SendTo(this->client_id_, buffer);
  96.     }
  97.     catch (std::runtime_error& e)
  98.     {
  99.         LOG.Error(e.what());
  100.     }
  101. }
  102.  
  103. Network::~Network()
  104. {
  105.     net::Manager::Instance()->Disconnect(this->client_id_);
  106. }
  107.  
  108. void Network::SlotOnTimeout(timer::TimerId timer_id)
  109. {
  110.     if (timer_id == this->timer_id_)
  111.     {
  112.         this->io_service_.post(boost::bind(&Network::SlotOnTimeoutHandler, this, timer_id));
  113.     }
  114. }
  115.  
  116. void Network::SlotOnMessageHandler(broker::Message::Pointer message)
  117. {
  118.     if (message->GetType() == broker::Message::CAN_MESSAGE)
  119.     {
  120.         Message* payload = static_cast<Message*>(message->GetPayload().get());
  121.         common::Byteset data(17);
  122.        
  123.         data[0] = PACKET_START;
  124.        
  125.         unsigned int class_id = Protocol::Instance()->ResolveClassId(payload->GetClassName());
  126.         data[4] = class_id << 1;
  127.  
  128.         if (payload->GetClassName() == "nmt")
  129.         {
  130.             unsigned int command_id = Protocol::Instance()->ResolveNMTCommandId(payload->GetCommandName());
  131.             data[3] = command_id;
  132.         }
  133.         else
  134.         {
  135.             unsigned int direction_flag = Protocol::Instance()->ResolveDirectionFlag(payload->GetDirectionName());
  136.             data[4] |= (direction_flag & 0x01);
  137.            
  138.             unsigned int module_id = Protocol::Instance()->ResolveModuleId(payload->GetModuleName());
  139.             data[3] = module_id;
  140.            
  141.             data[2] = payload->GetId();
  142.            
  143.             unsigned int command_id = Protocol::Instance()->ResolveCommandId(payload->GetCommandName(), payload->GetModuleName());
  144.             data[1] = command_id;
  145.         }
  146.        
  147.         unsigned int highest_bit = 0;
  148.         common::Bitset databits(64);
  149.        
  150.         xml::Node::NodeList variable_nodes;
  151.         unsigned int start_bit;
  152.         unsigned int bit_length;
  153.         std::string type;
  154.         std::string value;
  155.        
  156.         if (payload->GetClassName() == "nmt")
  157.         {
  158.             variable_nodes = Protocol::Instance()->GetNMTCommandVariables(payload->GetCommandName());
  159.         }
  160.         else
  161.         {
  162.             variable_nodes = Protocol::Instance()->GetCommandVariables(payload->GetCommandName(), payload->GetModuleName());
  163.         }
  164.        
  165.         for (unsigned int n = 0; n < variable_nodes.size(); n++)
  166.         {
  167.             value = payload->GetVariable(variable_nodes[n].GetAttributeValue("name"));
  168.             //LOG.Debug(variable_nodes[n].GetAttributeValue("name") + "=" + value);
  169.             if (value == "")
  170.             {
  171.                 continue;
  172.             }
  173.            
  174.             start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
  175.             bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
  176.             type = variable_nodes[n].GetAttributeValue("type");
  177.            
  178.             if (type == "int")
  179.             {
  180.                 Protocol::Instance()->EncodeInt(databits, start_bit, bit_length, value);
  181.             }
  182.             else if (type == "float")
  183.             {
  184.                 Protocol::Instance()->EncodeFloat(databits, start_bit, bit_length, value);
  185.             }
  186.             else if (type == "ascii")
  187.             {
  188.                 Protocol::Instance()->EncodeAscii(databits, start_bit, bit_length, value);
  189.                 bit_length = value.length() * 8;
  190.             }
  191.             else if (type == "hexstring")
  192.             {
  193.                 Protocol::Instance()->EncodeHexstring(databits, start_bit, bit_length, value);
  194.                 bit_length = value.length() * 4;
  195.             }
  196.             else if (type == "enum")
  197.             {
  198.                 value = variable_nodes[n].SelectChild("name", value).GetAttributeValue("id");
  199.                 Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value);
  200.             }
  201.             else// if (type == "uint")
  202.             {
  203.                 Protocol::Instance()->EncodeUint(databits, start_bit, bit_length, value);
  204.             }
  205.  
  206.             if (highest_bit < start_bit + bit_length)
  207.             {
  208.                 highest_bit = start_bit + bit_length;
  209.             }
  210.         }
  211.        
  212.         //LOG.Debug(databits.ToDebugString());
  213.        
  214.         unsigned int length = std::min((int)ceil((float)highest_bit / 8.0f), 8);
  215.        
  216.         data[5] = 1;
  217.         data[6] = 0;
  218.        
  219.         data[7] = length;
  220.        
  221.         for (unsigned int n = 0; n < 8; n++)
  222.         {
  223.             data[8 + n] = databits.GetBytes()[n];
  224.         }
  225.        
  226.         data[16] = PACKET_END;
  227.        
  228.         data.SetSize(17);
  229.        
  230.         //LOG.Debug("Bytes: " + data.ToDebugString());
  231.         net::Manager::Instance()->SendTo(this->client_id_, data);
  232.     } else if (message->GetType() == broker::Message::CAN_RAW_MESSAGE)
  233.     {
  234.         std::string* payload_str;
  235.     payload_str = static_cast<std::string*>(message->GetPayload().get());
  236.     std::string line = *payload_str;
  237.         common::Byteset data(17);
  238.        
  239.         data[0] = PACKET_START;
  240.         std::string value = line.substr(4,2);
  241.     data[1] = boost::lexical_cast<int>(value);
  242.     LOG.Info("id1: " + value + " data: ");//+ data[1]);
  243.    
  244.     value = line.substr(6,2);
  245.     data[2] = boost::lexical_cast<int>(value);
  246.     LOG.Info("id2: " + value + " data: ");//+ data[2]);
  247.    
  248.     value = line.substr(8,2);
  249.     data[3] = boost::lexical_cast<int>(value);
  250.     LOG.Info("id3: " + value + " data: ");//+ data[3]);
  251.    
  252.     value = line.substr(10,2);
  253.     data[4] = boost::lexical_cast<int>(value);
  254.     LOG.Info("id4: " + value + " data: ");//+ data[4]);
  255.    
  256.     value = line.substr(13,1);
  257.     data[5] = boost::lexical_cast<int>(value);
  258.     LOG.Info("1: " + value + " data: ");//+ data[5]);
  259.    
  260.     value = line.substr(15,1);
  261.     data[6] = boost::lexical_cast<int>(value);
  262.     LOG.Info("1: " + value + " data: ");//+ data[6]);
  263.  
  264.         unsigned char length = 0;
  265.         unsigned char index = 3;
  266.         while (length < 8 && index + 16 < (unsigned char)line.length())
  267.         {
  268.             value = line.substr(index,2);
  269.         data[8+length] = boost::lexical_cast<int>(value);
  270.         LOG.Info("data: " + value + " data: ");//+ data[6]);
  271.         index += 3;
  272.         length++;
  273.         }
  274.        
  275.         data[7] = length;
  276.         data[16] = PACKET_END;
  277.        
  278.         data.SetSize(17);
  279.        
  280.         //LOG.Debug("Bytes: " + data.ToDebugString());
  281.         net::Manager::Instance()->SendTo(this->client_id_, data);
  282.     }
  283. }
  284.  
  285. void Network::SlotOnNewDataHandler(net::ClientId client_id, net::ServerId server_id, common::Byteset data)
  286. {
  287.     if (client_id != this->client_id_)
  288.     {
  289.             return;
  290.     }
  291.    
  292.     static bool have_start = false;
  293.    
  294.     for (unsigned int n = 0; n < data.GetSize(); n++)
  295.     {
  296.         if (have_start)
  297.         {
  298.             if (data[n] == PACKET_END && this->buffer_.GetSize() == 15)
  299.             {
  300.                 this->ProcessBuffer();
  301.                 have_start = false;
  302.             }
  303.             else
  304.             {
  305.                 this->buffer_.Append(data[n]);
  306.             }
  307.         }
  308.         else if (data[n] == PACKET_START)
  309.         {
  310.             this->buffer_.Clear();
  311.             have_start = true;
  312.         }
  313.         else if (data[n] == PACKET_PING)
  314.         {
  315.             LOG.Info("Received pong.");
  316.         }
  317.     }
  318. }
  319.  
  320. void Network::SlotOnNewStateHandler(net::ClientId client_id, net::ServerId server_id, net::ClientState client_state)
  321. {
  322.     if (client_id != this->client_id_)
  323.     {
  324.         return;
  325.     }
  326.    
  327.     if (client_state == net::CLIENT_STATE_DISCONNECTED)
  328.     {
  329.         LOG.Warning("Got disconnected, setting reconnect timer...");
  330.        
  331.         this->timer_id_ = timer::Manager::Instance()->SetTimer(10000, true);
  332.         this->client_id_ = 0;
  333.     }
  334.     else
  335.     {
  336.         LOG.Debug("Got state: " + boost::lexical_cast<std::string>((int)client_state));
  337.     }
  338. }
  339.  
  340. void Network::SlotOnTimeoutHandler(timer::TimerId timer_id)
  341. {
  342.     try
  343.     {
  344.         this->client_id_ = net::Manager::Instance()->Connect(this->protocol_, this->address_, this->port_or_baud_);
  345.         LOG.Info("Connected again.");
  346.        
  347.         timer::Manager::Instance()->Cancel(timer_id);
  348.         this->timer_id_ = 0;
  349.     }
  350.     catch (std::runtime_error& e)
  351.     {
  352.         LOG.Error(e.what());
  353.         LOG.Warning("Will try again soon...");
  354.     }
  355. }
  356.  
  357. void Network::ProcessBuffer()
  358. {
  359.     try
  360.     {
  361.         std::string class_name = "";
  362.         std::string direction_name = "";
  363.         std::string module_name = "";
  364.         unsigned int id = 0;
  365.         std::string command_name = "";
  366.        
  367.     std::string PKTstring = "PKT " +
  368.                 atom::common::ToHex8bit((unsigned int)this->buffer_[3]) +
  369.                 atom::common::ToHex8bit((unsigned int)this->buffer_[2]) +
  370.                 atom::common::ToHex8bit((unsigned int)this->buffer_[1]) +
  371.                 atom::common::ToHex8bit((unsigned int)this->buffer_[0]) +
  372.                 " " +
  373.                 atom::common::ToHex4bit((unsigned int)this->buffer_[4]) +
  374.                 " " +
  375.                 atom::common::ToHex4bit((unsigned int)this->buffer_[5]);
  376.    
  377.     for (unsigned int index = 7; index < 7 + (unsigned int)this->buffer_[6]; index++)
  378.     {
  379.         PKTstring += " " + atom::common::ToHex8bit((unsigned int)this->buffer_[index]) ;
  380.     }
  381.    
  382.     PKTstring += "\n";
  383.    
  384.     //LOG.Info(PKTstring);
  385.    
  386.     std::string* payload_str = new std::string(PKTstring);
  387.     broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_RAW_MESSAGE, broker::Message::PayloadPointer(payload_str), this)));
  388.  
  389.    
  390.         unsigned int class_id = (this->buffer_[3] >> 1) & 0x0F;
  391.         //LOG.Debug("class_id=" + boost::lexical_cast<std::string>(class_id));
  392.         class_name = Protocol::Instance()->LookupClassName(class_id);
  393.        
  394.         if (class_name == "nmt")
  395.         {
  396.             unsigned int command_id = this->buffer_[2];
  397.             //LOG.Debug("command_id=" + boost::lexical_cast<std::string>(command_id));
  398.             command_name = Protocol::Instance()->LookupNMTCommandName(command_id);
  399.         }
  400.         else
  401.         {
  402.             unsigned int direction_flag = this->buffer_[3] & 0x01;
  403.             //LOG.Debug("direction_flag=" + boost::lexical_cast<std::string>(direction_flag));
  404.             direction_name = Protocol::Instance()->LookupDirectionFlag(direction_flag);
  405.            
  406.             unsigned int module_id = this->buffer_[2];
  407.             //LOG.Debug("module_id=" + boost::lexical_cast<std::string>(module_id));
  408.             module_name = Protocol::Instance()->LookupModuleName(module_id);
  409.             //LOG.Debug("module_name=" + module_name);
  410.             id = this->buffer_[1];
  411.            
  412.             unsigned int command_id = this->buffer_[0];
  413.             //LOG.Debug("command_id=" + boost::lexical_cast<std::string>(command_id));
  414.             command_name = Protocol::Instance()->LookupCommandName(command_id, module_name);
  415.             //LOG.Debug("command_name=" + command_name);
  416.         }
  417.  
  418.         Message* payload = new Message(class_name, direction_name, module_name, id, command_name);
  419.  
  420.         unsigned int length = this->buffer_[6];
  421.        
  422.         common::Byteset data_set(length);
  423.        
  424.         for (unsigned int n = 0; n < length; n++)
  425.         {
  426.             data_set.Append(this->buffer_[n + 7]);
  427.         }
  428.  
  429.         common::Bitset databits(data_set);
  430.         xml::Node::NodeList variable_nodes;
  431.         unsigned int start_bit;
  432.         int bit_length;
  433.         std::string type;
  434.         std::string value;
  435.         std::string name;
  436.        
  437.         if (class_name == "nmt")
  438.         {
  439.             variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name);
  440.         }
  441.         else
  442.         {
  443.             variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name);
  444.         }
  445.        
  446.         for (unsigned int n = 0; n < variable_nodes.size(); n++)
  447.         {
  448.             name = variable_nodes[n].GetAttributeValue("name");
  449.             start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
  450.             bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
  451.            
  452.             if (databits.GetCount() < start_bit + bit_length)
  453.             {
  454.                 bit_length = databits.GetCount() - start_bit;
  455.                
  456.                 if (bit_length <= 0)
  457.                 {
  458.                     LOG.Warning("Can not read variable " + name + " for command " + command_name + ", message is to short, is there a match between the module and the protocol XML file?");
  459.                     //LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length) + ", databits.GetCount()=" + boost::lexical_cast<std::string>(databits.GetCount()));
  460.                     continue;
  461.                 }
  462.             }
  463.            
  464.             type = variable_nodes[n].GetAttributeValue("type");
  465.            
  466.             if (type == "int")
  467.             {
  468.                 value = Protocol::Instance()->DecodeInt(databits, start_bit, bit_length);
  469.             }
  470.             else if (type == "float")
  471.             {
  472.                 value = Protocol::Instance()->DecodeFloat(databits, start_bit, bit_length);
  473.             }
  474.             else if (type == "ascii")
  475.             {
  476.                 value = Protocol::Instance()->DecodeAscii(databits, start_bit, bit_length);
  477.             }
  478.             else if (type == "hexstring")
  479.             {
  480.                 value = Protocol::Instance()->DecodeHexstring(databits, start_bit, bit_length);
  481.             }
  482.             else if (type == "enum")
  483.             {
  484.                 //LOG.Debug("Enum: command name=" + command_name);
  485.                 value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length);
  486.                 //LOG.Debug("Enum: value=" + boost::lexical_cast<std::string>(value));
  487.                 //LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length));
  488.                
  489.                 value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name");
  490.             }
  491.             else// if (type == "uint")
  492.             {
  493.                 //LOG.Debug("type: type=" + type);
  494.                 value = Protocol::Instance()->DecodeUint(databits, start_bit, bit_length);
  495.             }
  496.            
  497.             payload->SetVariable(name, value);
  498.         }
  499.        
  500.         broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
  501.     }
  502.     catch (std::runtime_error& e)
  503.     {
  504.         LOG.Error("Malformed message received, " + std::string(e.what()));
  505.         LOG.Debug("Bytes: " + this->buffer_.ToDebugString());
  506.     }
  507.    
  508.     this->buffer_.Clear();
  509. }
  510.    
  511. }; // namespace can
  512. }; // namespace atom
  513.