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