Subversion Repositories HomeAutomation

Rev

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