Subversion Repositories HomeAutomation

Rev

Rev 986 | Blame | 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.     vector<string> parts = explode(" ", rawHex);
  38.  
  39.     if (parts.size() < 2 || parts[0].compare("PKT") != 0)
  40.     {
  41.         //slog << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n";
  42.         throw new CanMessageException("Malformed can message received from canDaemon");
  43.     }
  44.  
  45.     string rawHeaderBin = hex2bin(parts[1]);
  46.  
  47.     myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4)));
  48.  
  49.     if (myClassName == "")
  50.     {
  51.         myIsUnknown = true;
  52.         return;
  53.     }
  54.  
  55.     string rawHexData = "";
  56.     for (int n = 4; n < parts.size(); n++)
  57.     {
  58.         rawHexData += parts[n];
  59.     }
  60.  
  61.     if (myClassName == "nmt")
  62.     {
  63.         int commandId = bin2uint(rawHeaderBin.substr(8, 8));
  64.         myCommandName = translator.lookupNMTCommandName(commandId);
  65.        
  66.         myData = translator.translateNMTData(commandId, rawHexData);
  67.     }
  68.     else
  69.     {
  70.         myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1)));
  71.         myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8)));
  72.  
  73.         myModuleId = bin2uint(rawHeaderBin.substr(16, 8));
  74.  
  75.         int commandId = bin2uint(rawHeaderBin.substr(24, 8));
  76.         myCommandName = translator.lookupCommandName(commandId, myModuleName);
  77.  
  78.         myData = translator.translateData(commandId, myModuleName, rawHexData);
  79.     }
  80. }
  81.  
  82. string CanMessage::getRaw()
  83. {
  84.     CanIdTranslator &translator = CanIdTranslator::getInstance();
  85.  
  86.     string dataHex;
  87.  
  88.     string rawHex = "PKT ";
  89.  
  90.     string bin = "000";
  91.  
  92.     int classId = translator.resolveClassId(myClassName);
  93.     if (classId == -1)
  94.         bin += "0000";
  95.     else
  96.         bin += uint2bin(classId, 4);
  97.  
  98.     if (myClassName == "nmt")
  99.     {
  100.         bin +="0";
  101.  
  102.         int commandNameId = translator.resolveNMTCommandId(myCommandName);
  103.         if (commandNameId == -1)
  104.             bin += "00000000";
  105.         else
  106.             bin += uint2bin(commandNameId, 8);
  107.  
  108.         bin += "0000000000000000";
  109.  
  110.         dataHex = translator.translateNMTDataToHex(commandNameId, myData);
  111.     }
  112.     else
  113.     {
  114.         bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1);
  115.  
  116.  
  117.         int moduleTypeId = translator.resolveModuleId(myModuleName);
  118.         if (moduleTypeId == -1)
  119.             bin += "00000000";
  120.         else
  121.             bin += uint2bin(moduleTypeId, 8);
  122.  
  123.  
  124.         int moduleId = myModuleId;
  125.         bin += uint2bin(moduleId, 8);
  126.  
  127.  
  128.         int commandNameId = translator.resolveCommandId(myCommandName, myModuleName);
  129.         if (commandNameId == -1)
  130.             bin += "00000000";
  131.         else
  132.             bin += uint2bin(commandNameId, 8);
  133.  
  134.         dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData);
  135.     }
  136.  
  137.     rawHex += bin2hex(bin);
  138.  
  139.     rawHex += " 1 0";
  140.  
  141.     while (dataHex.size() > 0)
  142.     {
  143.         string hex = dataHex.substr(0, 2);
  144.         dataHex.erase(0, 2);
  145.         rawHex += " " + hex;
  146.     }
  147.  
  148.     rawHex += "\n";
  149.  
  150.     return rawHex;
  151. }
  152.  
  153. void CanMessage::setData(map<string, CanVariable> data)
  154. {
  155.     myData = data;
  156.  
  157.     CanIdTranslator &translator = CanIdTranslator::getInstance();
  158.  
  159.     int commandNameId = translator.resolveNMTCommandId(myCommandName);
  160.  
  161.     if (myClassName == "nmt")
  162.     {
  163.         translator.makeNMTDataValid(commandNameId, data);
  164.     }
  165.     else
  166.     {
  167.         translator.makeDataValid(commandNameId, myModuleName, data);
  168.     }
  169. }
  170.  
  171. string CanMessage::getJSONData()
  172. {
  173.     string json = "{";
  174.  
  175.     map<string, CanVariable>::iterator iter;
  176.  
  177.     for (iter = myData.begin(); iter != myData.end(); iter++)
  178.     {
  179.         json += " " + iter->first + " : ";
  180.  
  181.         if (iter->second.getType() == "uint")
  182.         {
  183.             json += iter->second.getValue();
  184.         }
  185.         else if (iter->second.getType() == "float")
  186.         {
  187.             json += iter->second.getValue();
  188.         }
  189.         else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring")
  190.         {
  191.             json += "'" + escape(iter->second.getValue()) + "'";
  192.         }
  193.  
  194.         json += ",";
  195.     }
  196.  
  197.     json = trim(json, ',');
  198.  
  199.     return json + " }";
  200. }
  201.