Subversion Repositories HomeAutomation

Rev

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

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