Subversion Repositories HomeAutomation

Rev

Rev 997 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 997 Rev 999
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) December 27, 2008 by Mattias Runge                             *
2
 *   Copyright (C) December 27, 2008 by Mattias Runge                             *
3
 *   mattias@runge.se                                                      *
3
 *   mattias@runge.se                                                      *
4
 *   candebug.cpp                                            *
4
 *   candebug.cpp                                            *
5
 *                                                                         *
5
 *                                                                         *
6
 *   This program is free software; you can redistribute it and/or modify  *
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  *
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     *
8
 *   the Free Software Foundation; either version 2 of the License, or     *
9
 *   (at your option) any later version.                                   *
9
 *   (at your option) any later version.                                   *
10
 *                                                                         *
10
 *                                                                         *
11
 *   This program is distributed in the hope that it will be useful,       *
11
 *   This program is distributed in the hope that it will be useful,       *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14
 *   GNU General Public License for more details.                          *
14
 *   GNU General Public License for more details.                          *
15
 *                                                                         *
15
 *                                                                         *
16
 *   You should have received a copy of the GNU General Public License     *
16
 *   You should have received a copy of the GNU General Public License     *
17
 *   along with this program; if not, write to the                         *
17
 *   along with this program; if not, write to the                         *
18
 *   Free Software Foundation, Inc.,                                       *
18
 *   Free Software Foundation, Inc.,                                       *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
20
 ***************************************************************************/
20
 ***************************************************************************/
21
 
21
 
22
#include "candebug.h"
22
#include "candebug.h"
23
 
23
 
24
CanDebug* CanDebug::myInstance = NULL;
24
CanDebug* CanDebug::myInstance = NULL;
25
 
25
 
26
CanDebug& CanDebug::getInstance()
26
CanDebug& CanDebug::getInstance()
27
{
27
{
28
    if (myInstance == NULL)
28
    if (myInstance == NULL)
29
    {
29
    {
30
        myInstance = new CanDebug();
30
        myInstance = new CanDebug();
31
    }
31
    }
32
 
32
 
33
    return *myInstance;
33
    return *myInstance;
34
}
34
}
35
 
35
 
36
void CanDebug::deleteInstance()
36
void CanDebug::deleteInstance()
37
{
37
{
38
    if (myInstance != NULL)
38
    if (myInstance != NULL)
39
    {
39
    {
40
        delete myInstance;
40
        delete myInstance;
41
        myInstance = NULL;
41
        myInstance = NULL;
42
    }
42
    }
43
}
43
}
44
 
44
 
45
CanDebug::CanDebug()
45
CanDebug::CanDebug()
46
{
46
{
47
}
47
}
48
 
48
 
49
CanDebug::~CanDebug()
49
CanDebug::~CanDebug()
50
{
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
}
51
}
131
 
52
 
132
void CanDebug::sendCanMessage(CanMessage canMessage)
53
void CanDebug::sendCanMessageToAll(CanMessage canMessage)
133
{
54
{
134
    if (myClientSockets.size() > 0)
55
    if (myClientSockets.size() > 0)
135
    {
56
    {
136
        string debugData = "";
57
        string debugData = "";
137
 
58
 
138
        if (canMessage.getClassName() == "nmt")
59
        if (canMessage.getClassName() == "nmt")
139
        {
60
        {
140
            debugData += "NMT";
61
            debugData += "NMT";
141
        }
62
        }
142
        else
63
        else
143
        {
64
        {
144
            if (canMessage.getDirectionFlag() == "To_Owner")
65
            if (canMessage.getDirectionFlag() == "To_Owner")
145
            {
66
            {
146
                debugData += "RX";
67
                debugData += "RX";
147
            }
68
            }
148
            else if (canMessage.getDirectionFlag() == "From_Owner")
69
            else if (canMessage.getDirectionFlag() == "From_Owner")
149
            {
70
            {
150
                debugData += "TX";
71
                debugData += "TX";
151
            }
72
            }
152
            else
73
            else
153
            {
74
            {
154
                debugData += "??";
75
                debugData += "??";
155
            }
76
            }
156
 
77
 
157
            debugData += " " + canMessage.getClassName();
78
            debugData += " " + canMessage.getClassName();
158
            debugData += "_" + canMessage.getModuleName();
79
            debugData += "_" + canMessage.getModuleName();
159
            debugData += ":" + itos(canMessage.getModuleId());
80
            debugData += ":" + itos(canMessage.getModuleId());
160
        }
81
        }
161
 
82
 
162
        debugData += " CMD=" + canMessage.getCommandName() + " ";
83
        debugData += " CMD=" + canMessage.getCommandName() + " ";
163
 
84
 
164
        map<string, CanVariable> vars = canMessage.getData();
85
        map<string, CanVariable> vars = canMessage.getData();
165
        map<int, string> sortMap;
86
        map<int, string> sortMap;
166
 
87
 
167
        for (map<string, CanVariable>::iterator iter = vars.begin(); iter != vars.end(); iter++)
88
        for (map<string, CanVariable>::iterator iter = vars.begin(); iter != vars.end(); iter++)
168
        {
89
        {
169
            sortMap[iter->second.getStartBit()] = " " + iter->second.getName() + "=" + iter->second.getValue();
90
            sortMap[iter->second.getStartBit()] = " " + iter->second.getName() + "=" + iter->second.getValue();
170
        }
91
        }
171
 
92
 
172
        for (map<int, string>::iterator iter = sortMap.begin(); iter != sortMap.end(); iter++)
93
        for (map<int, string>::iterator iter = sortMap.begin(); iter != sortMap.end(); iter++)
173
        {
94
        {
174
            debugData += iter->second;
95
            debugData += iter->second;
175
        }
96
        }
176
 
97
 
177
        sendData("[" + niceTime() + "] " + debugData + "\n");
98
        sendToAll("[" + niceTime() + "] " + debugData + "\n");
178
    }
99
    }
-
 
100
}
-
 
101
 
-
 
102
void CanDebug::handleClientData(int id, string data)
-
 
103
{
-
 
104
 
179
}
105
}
180
 
106