Subversion Repositories HomeAutomation

Rev

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

  1. /***************************************************************************
  2.  *   Copyright (C) November 28, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   canmessage.cpp                                            *
  5.  *                                                                         *
  6.  *   This program is free software; you can redistribute it and/or modify  *
  7.  *   it under the terms of the GNU General Public License as published by  *
  8.  *   the Free Software Foundation; either version 2 of the License, or     *
  9.  *   (at your option) any later version.                                   *
  10.  *                                                                         *
  11.  *   This program is distributed in the hope that it will be useful,       *
  12.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  13.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  14.  *   GNU General Public License for more details.                          *
  15.  *                                                                         *
  16.  *   You should have received a copy of the GNU General Public License     *
  17.  *   along with this program; if not, write to the                         *
  18.  *   Free Software Foundation, Inc.,                                       *
  19.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  20.  ***************************************************************************/
  21.  
  22. #include <string>
  23. #include <iostream>
  24. #include "canidtranslator.h"
  25. #include "canmessage.h"
  26.  
  27. void CanMessage::setRaw(string rawHex)
  28. {
  29.     CanIdTranslator &translator = CanIdTranslator::getInstance();
  30.     //SyslogStream &slog = SyslogStream::getInstance();
  31.  
  32.     //PKT 00060102 1 0 00 00 c4
  33.  
  34.     rawHex = trim(rawHex, '\n');
  35.     rawHex = trim(rawHex);
  36.  
  37.     myRawHex = rawHex;
  38.  
  39.     vector<string> parts = explode(" ", rawHex);
  40.  
  41.     if (parts.size() < 2 || parts[0].compare("PKT") != 0)
  42.     {
  43.         cout << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n";
  44.         throw new CanMessageException("Malformed can message received from canDaemon");
  45.     }
  46.  
  47.     string rawHeaderBin = hex2bin(parts[1]);
  48.    
  49.     try
  50.     {
  51.         myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4)));
  52.     }
  53.     catch (std::out_of_range& e)
  54.     {
  55.         cout << "DEBUG: setRaw exception: " << e.what() << "\n";
  56.         cout << "DEBUG: A rawHeaderBin=" << rawHeaderBin << endl;
  57.     }
  58.  
  59.  
  60.     if (myClassName == "")
  61.     {
  62.         myIsUnknown = true;
  63.         return;
  64.     }
  65.     else
  66.     {
  67.         myIsUnknown = false;
  68.     }
  69.  
  70.     string rawHexData = "";
  71.     for (int n = 4; n < parts.size(); n++)
  72.     {
  73.         rawHexData += parts[n];
  74.     }
  75.  
  76.     if (myClassName == "nmt")
  77.     {
  78.         int commandId;
  79.  
  80.         try
  81.         {
  82.             commandId = bin2uint(rawHeaderBin.substr(8, 8));
  83.         }
  84.         catch (std::out_of_range& e)
  85.         {
  86.             cout << "DEBUG: setRaw exception: " << e.what() << "\n";
  87.             cout << "DEBUG: B rawHeaderBin=" << rawHeaderBin << endl;
  88.         }
  89.  
  90.         myCommandName = translator.lookupNMTCommandName(commandId);
  91.  
  92.         myData = translator.translateNMTData(commandId, rawHexData);
  93.  
  94.         myDirectionFlag = "";
  95.         myModuleName = "";
  96.         myModuleId = 0;
  97.     }
  98.     else
  99.     {
  100.         try
  101.         {
  102.             myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1)));
  103.         }
  104.         catch (std::out_of_range& e)
  105.         {
  106.             cout << "DEBUG: setRaw exception: " << e.what() << "\n";
  107.             cout << "DEBUG: C rawHeaderBin=" << rawHeaderBin << endl;
  108.         }
  109.  
  110.         try
  111.         {
  112.             myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8)));
  113.         }
  114.         catch (std::out_of_range& e)
  115.         {
  116.             cout << "DEBUG: setRaw exception: " << e.what() << "\n";
  117.             cout << "DEBUG: D rawHeaderBin=" << rawHeaderBin << endl;
  118.         }
  119.  
  120.         try
  121.         {
  122.             myModuleId = bin2uint(rawHeaderBin.substr(16, 8));
  123.         }
  124.         catch (std::out_of_range& e)
  125.         {
  126.             cout << "DEBUG: setRaw exception: " << e.what() << "\n";
  127.             cout << "DEBUG: E rawHeaderBin=" << rawHeaderBin << endl;
  128.         }
  129.  
  130.         int commandId;
  131.  
  132.         try
  133.         {
  134.             commandId = bin2uint(rawHeaderBin.substr(24, 8));
  135.         }
  136.         catch (std::out_of_range& e)
  137.         {
  138.             cout << "DEBUG: setRaw exception: " << e.what() << "\n";
  139.             cout << "DEBUG: F rawHeaderBin=" << rawHeaderBin << endl;
  140.         }
  141.        
  142.         myCommandName = translator.lookupCommandName(commandId, myModuleName);
  143.  
  144.         myData = translator.translateData(commandId, myModuleName, rawHexData);
  145.     }
  146. }
  147.  
  148. string CanMessage::getRaw()
  149. {
  150.     CanIdTranslator &translator = CanIdTranslator::getInstance();
  151.  
  152.     string dataHex;
  153.  
  154.     string rawHex = "PKT ";
  155.  
  156.     string bin = "000";
  157.  
  158.     int classId = translator.resolveClassId(myClassName);
  159.     if (classId == -1)
  160.         bin += "0000";
  161.     else
  162.         bin += uint2bin(classId, 4);
  163.  
  164.     if (myClassName == "nmt")
  165.     {
  166.         bin +="0";
  167.  
  168.         int commandNameId = translator.resolveNMTCommandId(myCommandName);
  169.         if (commandNameId == -1)
  170.             bin += "00000000";
  171.         else
  172.             bin += uint2bin(commandNameId, 8);
  173.  
  174.         bin += "0000000000000000";
  175.  
  176.         dataHex = translator.translateNMTDataToHex(commandNameId, myData);
  177.     }
  178.     else
  179.     {
  180.         bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1);
  181.  
  182.  
  183.         int moduleTypeId = translator.resolveModuleId(myModuleName);
  184.         if (moduleTypeId == -1)
  185.             bin += "00000000";
  186.         else
  187.             bin += uint2bin(moduleTypeId, 8);
  188.  
  189.  
  190.         int moduleId = myModuleId;
  191.         bin += uint2bin(moduleId, 8);
  192.  
  193.  
  194.         int commandNameId = translator.resolveCommandId(myCommandName, myModuleName);
  195.         if (commandNameId == -1)
  196.             bin += "00000000";
  197.         else
  198.             bin += uint2bin(commandNameId, 8);
  199.  
  200.         dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData);
  201.     }
  202.  
  203.     rawHex += bin2hex(bin);
  204.  
  205.     rawHex += " 1 0";
  206.  
  207.     while (dataHex.size() > 0)
  208.     {
  209.         string hex;
  210.  
  211.         try
  212.         {
  213.             hex = dataHex.substr(0, 2);
  214.         }
  215.         catch (std::out_of_range& e)
  216.         {
  217.             cout << "DEBUG: getRaw exception: " << e.what() << "\n";
  218.             cout << "DEBUG: A dataHex=" << dataHex << endl;
  219.             continue;
  220.         }
  221.  
  222.        
  223.         dataHex.erase(0, 2);
  224.         rawHex += " " + hex;
  225.     }
  226.  
  227.     rawHex += "\n";
  228.  
  229.     return rawHex;
  230. }
  231.  
  232. void CanMessage::setData(map<string, CanVariable> data)
  233. {
  234.     myData = data;
  235.  
  236.     CanIdTranslator &translator = CanIdTranslator::getInstance();
  237.  
  238.     int commandNameId = translator.resolveNMTCommandId(myCommandName);
  239.  
  240.     if (myClassName == "nmt")
  241.     {
  242.         translator.makeNMTDataValid(commandNameId, data);
  243.     }
  244.     else
  245.     {
  246.         translator.makeDataValid(commandNameId, myModuleName, data);
  247.     }
  248. }
  249.  
  250. string CanMessage::getJSONData()
  251. {
  252.     string json = "{";
  253.  
  254.     map<string, CanVariable>::iterator iter;
  255.  
  256.     for (iter = myData.begin(); iter != myData.end(); iter++)
  257.     {
  258.         json += " " + iter->first + " : ";
  259.  
  260.         if (iter->second.getType() == "uint")
  261.         {
  262.             json += iter->second.getValue();
  263.         }
  264.         else if (iter->second.getType() == "float")
  265.         {
  266.             json += iter->second.getValue();
  267.         }
  268.         else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring")
  269.         {
  270.             json += "'" + escape(iter->second.getValue()) + "'";
  271.         }
  272.  
  273.         json += ",";
  274.     }
  275.  
  276.     json = trim(json, ',');
  277.  
  278.     return json + " }";
  279. }
  280.  
  281. string CanMessage::toString()
  282. {
  283.     string result = "[CanMessage] ClassName: " + myClassName + "\n";
  284.     result += "             DirectionFlag: " + myDirectionFlag + "\n";
  285.     result += "             ModuleName: " + myModuleName + "\n";
  286.     result += "             ModuleId: " + itos(myModuleId) + "\n";
  287.     result += "             CommandName: " + myCommandName + "\n";
  288.     result += "             Data: " + getJSONData() + "\n";
  289.     result += "             RawHex: " + myRawHex + "\n";
  290.  
  291.     return result;
  292. }
  293.