Subversion Repositories HomeAutomation

Rev

Rev 991 | Blame | Last modification | View Log | SVN | RSS feed

  1. /***************************************************************************
  2.  *   Copyright (C) December 27, 2008 by Mattias Runge                             *
  3.  *   mattias@runge.se                                                      *
  4.  *   candebug.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 "candebug.h"
  23.  
  24. CanDebug* CanDebug::myInstance = NULL;
  25.  
  26. CanDebug& CanDebug::getInstance()
  27. {
  28.     if (myInstance == NULL)
  29.     {
  30.         myInstance = new CanDebug();
  31.     }
  32.  
  33.     return *myInstance;
  34. }
  35.  
  36. void CanDebug::deleteInstance()
  37. {
  38.     if (myInstance != NULL)
  39.     {
  40.         delete myInstance;
  41.         myInstance = NULL;
  42.     }
  43. }
  44.  
  45. CanDebug::CanDebug()
  46. {
  47. }
  48.  
  49. CanDebug::~CanDebug()
  50. {
  51.     stop();
  52.  
  53.     for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
  54.     {
  55.         if (iter->second->isConnected())
  56.         {
  57.             iter->second->stop();
  58.         }
  59.  
  60.         delete iter->second;
  61.     }
  62. }
  63.  
  64. void CanDebug::run()
  65. {
  66.     SyslogStream &slog = SyslogStream::getInstance();
  67.  
  68.     string port = Settings::get("CanDebugPort");
  69.  
  70.     if (port == "")
  71.     {
  72.         slog << "CanDebugPort is not defined in the config file, will not start debug interface\n";
  73.         return;
  74.     }
  75.  
  76.     try
  77.     {
  78.         mySocket.setPort(stoi(port));
  79.  
  80.         mySocket.startListen();
  81.  
  82.         slog << "CanDebug is listening for connections on port " + port + ".\n";
  83.  
  84.         while (true)
  85.         {
  86.             AsyncSocket *newSocket = new AsyncSocket();
  87.  
  88.             if (mySocket.accept(newSocket))
  89.             {
  90.                 newSocket->sendData("Welcome to Atom " + ftos(VERSION) + " - CanDebug output\n");
  91.  
  92.                 slog << "CanDebug accepted new client on socket " + itos(newSocket->getSocket()) + ".\n";
  93.                 myClientSockets[newSocket->getSocket()] = newSocket;
  94.                 myClientSockets[newSocket->getSocket()]->start();
  95.             }
  96.         }
  97.     }
  98.     catch (SocketException* e)
  99.     {
  100.         slog << "Exception was caught:" + e->getDescription() + ".\n";
  101.     }
  102. }
  103.  
  104. void CanDebug::sendData(string data)
  105. {
  106.     SyslogStream &slog = SyslogStream::getInstance();
  107.  
  108.     for (map<int, AsyncSocket*>::iterator iter = myClientSockets.begin(); iter != myClientSockets.end(); iter++)
  109.     {
  110.         if (iter->second->isConnected())
  111.         {
  112.             //slog << "CanDebug: sent data to client " + itos(iter->second->getSocket()) + ".\n";
  113.            
  114.             try
  115.             {
  116.                 iter->second->sendData(data);
  117.             }
  118.             catch (SocketException* e)
  119.             {
  120.                 slog << "CanDebug caught an exception:" + e->getDescription() + ".\n";
  121.             }
  122.         }
  123.         else
  124.         {
  125.             slog << "CanDebug had a client disconnect.\n";
  126.             delete iter->second;
  127.             myClientSockets.erase(iter);
  128.         }
  129.     }
  130. }
  131.  
  132. void CanDebug::sendCanMessage(CanMessage canMessage)
  133. {
  134.     if (myClientSockets.size() > 0)
  135.     {
  136.         string debugData = "";
  137.  
  138.         if (canMessage.getClassName() == "nmt")
  139.         {
  140.             debugData += "NMT";
  141.         }
  142.         else
  143.         {
  144.             if (canMessage.getDirectionFlag() == "To_Owner")
  145.             {
  146.                 debugData += "RX";
  147.             }
  148.             else if (canMessage.getDirectionFlag() == "From_Owner")
  149.             {
  150.                 debugData += "TX";
  151.             }
  152.             else
  153.             {
  154.                 debugData += "??";
  155.             }
  156.  
  157.             debugData += " " + canMessage.getClassName();
  158.             debugData += "_" + canMessage.getModuleName();
  159.             debugData += ":" + itos(canMessage.getModuleId());
  160.         }
  161.  
  162.         debugData += " CMD=" + canMessage.getCommandName() + " ";
  163.  
  164.         map<string, CanVariable> vars = canMessage.getData();
  165.         map<int, string> sortMap;
  166.  
  167.         for (map<string, CanVariable>::iterator iter = vars.begin(); iter != vars.end(); iter++)
  168.         {
  169.             sortMap[iter->second.getStartBit()] = " " + iter->second.getName() + "=" + iter->second.getValue();
  170.         }
  171.  
  172.         for (map<int, string>::iterator iter = sortMap.begin(); iter != sortMap.end(); iter++)
  173.         {
  174.             debugData += iter->second;
  175.         }
  176.  
  177.         sendData("[" + niceTime() + "] " + debugData + "\n");
  178.     }
  179. }
  180.