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) November 28, 2008 by Mattias Runge * |
2 | * Copyright (C) November 28, 2008 by Mattias Runge * |
| 3 | * mattias@runge.se * |
3 | * mattias@runge.se * |
| 4 | * canmessage.cpp * |
4 | * canmessage.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 <string> |
22 | #include <string> |
| 23 | #include <iostream> |
23 | #include <iostream> |
| 24 | #include "canidtranslator.h" |
24 | #include "canidtranslator.h" |
| 25 | #include "canmessage.h" |
25 | #include "canmessage.h" |
| 26 | 26 | ||
| 27 | void CanMessage::setRaw(string rawHex) |
27 | void CanMessage::setRaw(string rawHex) |
| 28 | { |
28 | { |
| 29 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
29 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
| 30 | //SyslogStream &slog = SyslogStream::getInstance(); |
30 | //SyslogStream &slog = SyslogStream::getInstance(); |
| 31 | 31 | ||
| 32 | //PKT 00060102 1 0 00 00 c4 |
32 | //PKT 00060102 1 0 00 00 c4 |
| 33 | 33 | ||
| 34 | rawHex = trim(rawHex, '\n'); |
34 | rawHex = trim(rawHex, '\n'); |
| 35 | rawHex = trim(rawHex); |
35 | rawHex = trim(rawHex); |
| - | 36 | ||
| - | 37 | myRawHex = rawHex; |
|
| 36 | 38 | ||
| 37 | vector<string> parts = explode(" ", rawHex); |
39 | vector<string> parts = explode(" ", rawHex); |
| 38 | 40 | ||
| 39 | if (parts.size() < 2 || parts[0].compare("PKT") != 0) |
41 | if (parts.size() < 2 || parts[0].compare("PKT") != 0) |
| 40 | { |
42 | { |
| 41 |
|
43 | cout << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n"; |
| 42 | throw new CanMessageException("Malformed can message received from canDaemon"); |
44 | throw new CanMessageException("Malformed can message received from canDaemon"); |
| 43 | } |
45 | } |
| 44 | 46 | ||
| 45 | string rawHeaderBin = hex2bin(parts[1]); |
47 | string rawHeaderBin = hex2bin(parts[1]); |
| 46 | 48 | ||
| 47 | try |
49 | try |
| 48 | { |
50 | { |
| 49 | myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4))); |
51 | myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4))); |
| 50 | } |
52 | } |
| 51 | catch (std::out_of_range& e) |
53 | catch (std::out_of_range& e) |
| 52 | { |
54 | { |
| 53 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
55 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
| 54 | cout << "DEBUG: A rawHeaderBin=" << rawHeaderBin << endl; |
56 | cout << "DEBUG: A rawHeaderBin=" << rawHeaderBin << endl; |
| 55 | } |
57 | } |
| 56 | 58 | ||
| 57 | 59 | ||
| 58 | if (myClassName == "") |
60 | if (myClassName == "") |
| 59 | { |
61 | { |
| 60 | myIsUnknown = true; |
62 | myIsUnknown = true; |
| 61 | return; |
63 | return; |
| 62 | } |
64 | } |
| 63 | else |
65 | else |
| 64 | { |
66 | { |
| 65 | myIsUnknown = false; |
67 | myIsUnknown = false; |
| 66 | } |
68 | } |
| 67 | 69 | ||
| 68 | string rawHexData = ""; |
70 | string rawHexData = ""; |
| 69 | for (int n = 4; n < parts.size(); n++) |
71 | for (int n = 4; n < parts.size(); n++) |
| 70 | { |
72 | { |
| 71 | rawHexData += parts[n]; |
73 | rawHexData += parts[n]; |
| 72 | } |
74 | } |
| 73 | 75 | ||
| 74 | if (myClassName == "nmt") |
76 | if (myClassName == "nmt") |
| 75 | { |
77 | { |
| 76 | int commandId; |
78 | int commandId; |
| 77 | 79 | ||
| 78 | try |
80 | try |
| 79 | { |
81 | { |
| 80 | commandId = bin2uint(rawHeaderBin.substr(8, 8)); |
82 | commandId = bin2uint(rawHeaderBin.substr(8, 8)); |
| 81 | } |
83 | } |
| 82 | catch (std::out_of_range& e) |
84 | catch (std::out_of_range& e) |
| 83 | { |
85 | { |
| 84 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
86 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
| 85 | cout << "DEBUG: B rawHeaderBin=" << rawHeaderBin << endl; |
87 | cout << "DEBUG: B rawHeaderBin=" << rawHeaderBin << endl; |
| 86 | } |
88 | } |
| 87 | 89 | ||
| 88 | myCommandName = translator.lookupNMTCommandName(commandId); |
90 | myCommandName = translator.lookupNMTCommandName(commandId); |
| 89 | 91 | ||
| 90 | myData = translator.translateNMTData(commandId, rawHexData); |
92 | myData = translator.translateNMTData(commandId, rawHexData); |
| - | 93 | ||
| - | 94 | myDirectionFlag = ""; |
|
| - | 95 | myModuleName = ""; |
|
| - | 96 | myModuleId = 0; |
|
| 91 | } |
97 | } |
| 92 | else |
98 | else |
| 93 | { |
99 | { |
| 94 | try |
100 | try |
| 95 | { |
101 | { |
| 96 | myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1))); |
102 | myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1))); |
| 97 | } |
103 | } |
| 98 | catch (std::out_of_range& e) |
104 | catch (std::out_of_range& e) |
| 99 | { |
105 | { |
| 100 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
106 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
| 101 | cout << "DEBUG: C rawHeaderBin=" << rawHeaderBin << endl; |
107 | cout << "DEBUG: C rawHeaderBin=" << rawHeaderBin << endl; |
| 102 | } |
108 | } |
| 103 | 109 | ||
| 104 | try |
110 | try |
| 105 | { |
111 | { |
| 106 | myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8))); |
112 | myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8))); |
| 107 | } |
113 | } |
| 108 | catch (std::out_of_range& e) |
114 | catch (std::out_of_range& e) |
| 109 | { |
115 | { |
| 110 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
116 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
| 111 | cout << "DEBUG: D rawHeaderBin=" << rawHeaderBin << endl; |
117 | cout << "DEBUG: D rawHeaderBin=" << rawHeaderBin << endl; |
| 112 | } |
118 | } |
| 113 | 119 | ||
| 114 | try |
120 | try |
| 115 | { |
121 | { |
| 116 | myModuleId = bin2uint(rawHeaderBin.substr(16, 8)); |
122 | myModuleId = bin2uint(rawHeaderBin.substr(16, 8)); |
| 117 | } |
123 | } |
| 118 | catch (std::out_of_range& e) |
124 | catch (std::out_of_range& e) |
| 119 | { |
125 | { |
| 120 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
126 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
| 121 | cout << "DEBUG: E rawHeaderBin=" << rawHeaderBin << endl; |
127 | cout << "DEBUG: E rawHeaderBin=" << rawHeaderBin << endl; |
| 122 | } |
128 | } |
| 123 | 129 | ||
| 124 | int commandId; |
130 | int commandId; |
| 125 | 131 | ||
| 126 | try |
132 | try |
| 127 | { |
133 | { |
| 128 | commandId = bin2uint(rawHeaderBin.substr(24, 8)); |
134 | commandId = bin2uint(rawHeaderBin.substr(24, 8)); |
| 129 | } |
135 | } |
| 130 | catch (std::out_of_range& e) |
136 | catch (std::out_of_range& e) |
| 131 | { |
137 | { |
| 132 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
138 | cout << "DEBUG: setRaw exception: " << e.what() << "\n"; |
| 133 | cout << "DEBUG: F rawHeaderBin=" << rawHeaderBin << endl; |
139 | cout << "DEBUG: F rawHeaderBin=" << rawHeaderBin << endl; |
| 134 | } |
140 | } |
| 135 | 141 | ||
| 136 | myCommandName = translator.lookupCommandName(commandId, myModuleName); |
142 | myCommandName = translator.lookupCommandName(commandId, myModuleName); |
| 137 | 143 | ||
| 138 | myData = translator.translateData(commandId, myModuleName, rawHexData); |
144 | myData = translator.translateData(commandId, myModuleName, rawHexData); |
| 139 | } |
145 | } |
| 140 | } |
146 | } |
| 141 | 147 | ||
| 142 | string CanMessage::getRaw() |
148 | string CanMessage::getRaw() |
| 143 | { |
149 | { |
| 144 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
150 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
| 145 | 151 | ||
| 146 | string dataHex; |
152 | string dataHex; |
| 147 | 153 | ||
| 148 | string rawHex = "PKT "; |
154 | string rawHex = "PKT "; |
| 149 | 155 | ||
| 150 | string bin = "000"; |
156 | string bin = "000"; |
| 151 | 157 | ||
| 152 | int classId = translator.resolveClassId(myClassName); |
158 | int classId = translator.resolveClassId(myClassName); |
| 153 | if (classId == -1) |
159 | if (classId == -1) |
| 154 | bin += "0000"; |
160 | bin += "0000"; |
| 155 | else |
161 | else |
| 156 | bin += uint2bin(classId, 4); |
162 | bin += uint2bin(classId, 4); |
| 157 | 163 | ||
| 158 | if (myClassName == "nmt") |
164 | if (myClassName == "nmt") |
| 159 | { |
165 | { |
| 160 | bin +="0"; |
166 | bin +="0"; |
| 161 | 167 | ||
| 162 | int commandNameId = translator.resolveNMTCommandId(myCommandName); |
168 | int commandNameId = translator.resolveNMTCommandId(myCommandName); |
| 163 | if (commandNameId == -1) |
169 | if (commandNameId == -1) |
| 164 | bin += "00000000"; |
170 | bin += "00000000"; |
| 165 | else |
171 | else |
| 166 | bin += uint2bin(commandNameId, 8); |
172 | bin += uint2bin(commandNameId, 8); |
| 167 | 173 | ||
| 168 | bin += "0000000000000000"; |
174 | bin += "0000000000000000"; |
| 169 | 175 | ||
| 170 | dataHex = translator.translateNMTDataToHex(commandNameId, myData); |
176 | dataHex = translator.translateNMTDataToHex(commandNameId, myData); |
| 171 | } |
177 | } |
| 172 | else |
178 | else |
| 173 | { |
179 | { |
| 174 | bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1); |
180 | bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1); |
| 175 | 181 | ||
| 176 | 182 | ||
| 177 | int moduleTypeId = translator.resolveModuleId(myModuleName); |
183 | int moduleTypeId = translator.resolveModuleId(myModuleName); |
| 178 | if (moduleTypeId == -1) |
184 | if (moduleTypeId == -1) |
| 179 | bin += "00000000"; |
185 | bin += "00000000"; |
| 180 | else |
186 | else |
| 181 | bin += uint2bin(moduleTypeId, 8); |
187 | bin += uint2bin(moduleTypeId, 8); |
| 182 | 188 | ||
| 183 | 189 | ||
| 184 | int moduleId = myModuleId; |
190 | int moduleId = myModuleId; |
| 185 | bin += uint2bin(moduleId, 8); |
191 | bin += uint2bin(moduleId, 8); |
| 186 | 192 | ||
| 187 | 193 | ||
| 188 | int commandNameId = translator.resolveCommandId(myCommandName, myModuleName); |
194 | int commandNameId = translator.resolveCommandId(myCommandName, myModuleName); |
| 189 | if (commandNameId == -1) |
195 | if (commandNameId == -1) |
| 190 | bin += "00000000"; |
196 | bin += "00000000"; |
| 191 | else |
197 | else |
| 192 | bin += uint2bin(commandNameId, 8); |
198 | bin += uint2bin(commandNameId, 8); |
| 193 | 199 | ||
| 194 | dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData); |
200 | dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData); |
| 195 | } |
201 | } |
| 196 | 202 | ||
| 197 | rawHex += bin2hex(bin); |
203 | rawHex += bin2hex(bin); |
| 198 | 204 | ||
| 199 | rawHex += " 1 0"; |
205 | rawHex += " 1 0"; |
| 200 | 206 | ||
| 201 | while (dataHex.size() > 0) |
207 | while (dataHex.size() > 0) |
| 202 | { |
208 | { |
| 203 | string hex; |
209 | string hex; |
| 204 | 210 | ||
| 205 | try |
211 | try |
| 206 | { |
212 | { |
| 207 | hex = dataHex.substr(0, 2); |
213 | hex = dataHex.substr(0, 2); |
| 208 | } |
214 | } |
| 209 | catch (std::out_of_range& e) |
215 | catch (std::out_of_range& e) |
| 210 | { |
216 | { |
| 211 | cout << "DEBUG: getRaw exception: " << e.what() << "\n"; |
217 | cout << "DEBUG: getRaw exception: " << e.what() << "\n"; |
| 212 | cout << "DEBUG: A dataHex=" << dataHex << endl; |
218 | cout << "DEBUG: A dataHex=" << dataHex << endl; |
| 213 | continue; |
219 | continue; |
| 214 | } |
220 | } |
| 215 | 221 | ||
| 216 | 222 | ||
| 217 | dataHex.erase(0, 2); |
223 | dataHex.erase(0, 2); |
| 218 | rawHex += " " + hex; |
224 | rawHex += " " + hex; |
| 219 | } |
225 | } |
| 220 | 226 | ||
| 221 | rawHex += "\n"; |
227 | rawHex += "\n"; |
| 222 | 228 | ||
| 223 | return rawHex; |
229 | return rawHex; |
| 224 | } |
230 | } |
| 225 | 231 | ||
| 226 | void CanMessage::setData(map<string, CanVariable> data) |
232 | void CanMessage::setData(map<string, CanVariable> data) |
| 227 | { |
233 | { |
| 228 | myData = data; |
234 | myData = data; |
| 229 | 235 | ||
| 230 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
236 | CanIdTranslator &translator = CanIdTranslator::getInstance(); |
| 231 | 237 | ||
| 232 | int commandNameId = translator.resolveNMTCommandId(myCommandName); |
238 | int commandNameId = translator.resolveNMTCommandId(myCommandName); |
| 233 | 239 | ||
| 234 | if (myClassName == "nmt") |
240 | if (myClassName == "nmt") |
| 235 | { |
241 | { |
| 236 | translator.makeNMTDataValid(commandNameId, data); |
242 | translator.makeNMTDataValid(commandNameId, data); |
| 237 | } |
243 | } |
| 238 | else |
244 | else |
| 239 | { |
245 | { |
| 240 | translator.makeDataValid(commandNameId, myModuleName, data); |
246 | translator.makeDataValid(commandNameId, myModuleName, data); |
| 241 | } |
247 | } |
| 242 | } |
248 | } |
| 243 | 249 | ||
| 244 | string CanMessage::getJSONData() |
250 | string CanMessage::getJSONData() |
| 245 | { |
251 | { |
| 246 | string json = "{"; |
252 | string json = "{"; |
| 247 | 253 | ||
| 248 | map<string, CanVariable>::iterator iter; |
254 | map<string, CanVariable>::iterator iter; |
| 249 | 255 | ||
| 250 | for (iter = myData.begin(); iter != myData.end(); iter++) |
256 | for (iter = myData.begin(); iter != myData.end(); iter++) |
| 251 | { |
257 | { |
| 252 | json += " " + iter->first + " : "; |
258 | json += " " + iter->first + " : "; |
| 253 | 259 | ||
| 254 | if (iter->second.getType() == "uint") |
260 | if (iter->second.getType() == "uint") |
| 255 | { |
261 | { |
| 256 | json += iter->second.getValue(); |
262 | json += iter->second.getValue(); |
| 257 | } |
263 | } |
| 258 | else if (iter->second.getType() == "float") |
264 | else if (iter->second.getType() == "float") |
| 259 | { |
265 | { |
| 260 | json += iter->second.getValue(); |
266 | json += iter->second.getValue(); |
| 261 | } |
267 | } |
| 262 | else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring") |
268 | else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring") |
| 263 | { |
269 | { |
| 264 | json += "'" + escape(iter->second.getValue()) + "'"; |
270 | json += "'" + escape(iter->second.getValue()) + "'"; |
| 265 | } |
271 | } |
| 266 | 272 | ||
| 267 | json += ","; |
273 | json += ","; |
| 268 | } |
274 | } |
| 269 | 275 | ||
| 270 | json = trim(json, ','); |
276 | json = trim(json, ','); |
| 271 | 277 | ||
| 272 | return json + " }"; |
278 | return json + " }"; |
| 273 | } |
279 | } |
| 274 | 280 | ||
| 275 | string CanMessage::toString() |
281 | string CanMessage::toString() |
| 276 | { |
282 | { |
| 277 | string result = "[CanMessage] ClassName: " + myClassName + "\n"; |
283 | string result = "[CanMessage] ClassName: " + myClassName + "\n"; |
| 278 | result += " DirectionFlag: " + myDirectionFlag + "\n"; |
284 | result += " DirectionFlag: " + myDirectionFlag + "\n"; |
| 279 | result += " ModuleName: " + myModuleName + "\n"; |
285 | result += " ModuleName: " + myModuleName + "\n"; |
| 280 | result += " ModuleId: " + itos(myModuleId) + "\n"; |
286 | result += " ModuleId: " + itos(myModuleId) + "\n"; |
| 281 | result += " CommandName: " + myCommandName + "\n"; |
287 | result += " CommandName: " + myCommandName + "\n"; |
| 282 | result += " Data: " + getJSONData() + "\n"; |
288 | result += " Data: " + getJSONData() + "\n"; |
| - | 289 | result += " RawHex: " + myRawHex + "\n"; |
|
| 283 | 290 | ||
| 284 | return result; |
291 | return result; |
| 285 | } |
292 | } |
| 286 | 293 | ||