Rev 1001 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1001 | Rev 1002 | ||
|---|---|---|---|
| 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 | * canidtranslator.cpp * |
4 | * canidtranslator.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 "canidtranslator.h" |
22 | #include "canidtranslator.h" |
| 23 | 23 | ||
| 24 | CanIdTranslator* CanIdTranslator::myInstance = NULL; |
24 | CanIdTranslator* CanIdTranslator::myInstance = NULL; |
| 25 | 25 | ||
| 26 | CanIdTranslator& CanIdTranslator::getInstance() |
26 | CanIdTranslator& CanIdTranslator::getInstance() |
| 27 | { |
27 | { |
| 28 | if (myInstance == NULL) |
28 | if (myInstance == NULL) |
| 29 | { |
29 | { |
| 30 | myInstance = new CanIdTranslator(); |
30 | myInstance = new CanIdTranslator(); |
| 31 | } |
31 | } |
| 32 | 32 | ||
| 33 | return *myInstance; |
33 | return *myInstance; |
| 34 | } |
34 | } |
| 35 | 35 | ||
| 36 | void CanIdTranslator::deleteInstance() |
36 | void CanIdTranslator::deleteInstance() |
| 37 | { |
37 | { |
| 38 | delete myInstance; |
38 | delete myInstance; |
| 39 | myInstance = NULL; |
39 | myInstance = NULL; |
| 40 | } |
40 | } |
| 41 | 41 | ||
| 42 | CanIdTranslator::CanIdTranslator() |
42 | CanIdTranslator::CanIdTranslator() |
| 43 | { |
43 | { |
| 44 | SyslogStream &slog = SyslogStream::getInstance(); |
44 | SyslogStream &slog = SyslogStream::getInstance(); |
| 45 | 45 | ||
| 46 | string filename = Settings::get("CanIdXMLFile"); |
46 | string filename = Settings::get("CanIdXMLFile"); |
| 47 | 47 | ||
| 48 | if (file_exists(filename)) |
48 | if (file_exists(filename)) |
| 49 | { |
49 | { |
| 50 | xmlNode.load(filename.c_str()); |
50 | xmlNode.load(filename.c_str()); |
| 51 | 51 | ||
| 52 | slog << "Loading definitions from " + filename + ".\n"; |
52 | slog << "Loading definitions from " + filename + ".\n"; |
| 53 | } |
53 | } |
| 54 | else |
54 | else |
| 55 | { |
55 | { |
| 56 | slog << "CanIdXMLFile is not defined in the config file, this will probably not work at all.\n"; |
56 | slog << "CanIdXMLFile is not defined in the config file, this will probably not work at all.\n"; |
| 57 | } |
57 | } |
| 58 | } |
58 | } |
| 59 | 59 | ||
| 60 | string CanIdTranslator::lookupClassName(int classId) |
60 | string CanIdTranslator::lookupClassName(int classId) |
| 61 | { |
61 | { |
| 62 | XmlNode classesNode = xmlNode.findChild("classes"); |
62 | XmlNode classesNode = xmlNode.findChild("classes"); |
| 63 | vector<XmlNode> classNodes = classesNode.getChildren(); |
63 | vector<XmlNode> classNodes = classesNode.getChildren(); |
| 64 | 64 | ||
| 65 | for (int n = 0; n < classNodes.size(); n++) |
65 | for (int n = 0; n < classNodes.size(); n++) |
| 66 | { |
66 | { |
| 67 | map<string, string> attributes = classNodes[n].getAttributes(); |
67 | map<string, string> attributes = classNodes[n].getAttributes(); |
| 68 | 68 | ||
| 69 | if (stoi(attributes["id"]) == classId) |
69 | if (stoi(attributes["id"]) == classId) |
| 70 | { |
70 | { |
| 71 | return attributes["name"]; |
71 | return attributes["name"]; |
| 72 | } |
72 | } |
| 73 | } |
73 | } |
| 74 | 74 | ||
| 75 | return ""; |
75 | return ""; |
| 76 | } |
76 | } |
| 77 | 77 | ||
| 78 | int CanIdTranslator::resolveClassId(string className) |
78 | int CanIdTranslator::resolveClassId(string className) |
| 79 | { |
79 | { |
| 80 | XmlNode classesNode = xmlNode.findChild("classes"); |
80 | XmlNode classesNode = xmlNode.findChild("classes"); |
| 81 | vector<XmlNode> classNodes = classesNode.getChildren(); |
81 | vector<XmlNode> classNodes = classesNode.getChildren(); |
| 82 | 82 | ||
| 83 | for (int n = 0; n < classNodes.size(); n++) |
83 | for (int n = 0; n < classNodes.size(); n++) |
| 84 | { |
84 | { |
| 85 | map<string, string> attributes = classNodes[n].getAttributes(); |
85 | map<string, string> attributes = classNodes[n].getAttributes(); |
| 86 | 86 | ||
| 87 | if (attributes["name"].compare(className) == 0) |
87 | if (attributes["name"].compare(className) == 0) |
| 88 | { |
88 | { |
| 89 | return stoi(attributes["id"]); |
89 | return stoi(attributes["id"]); |
| 90 | } |
90 | } |
| 91 | } |
91 | } |
| 92 | 92 | ||
| 93 | return -1; |
93 | return -1; |
| 94 | } |
94 | } |
| 95 | 95 | ||
| 96 | string CanIdTranslator::lookupCommandName(int commandId, string moduleName) |
96 | string CanIdTranslator::lookupCommandName(int commandId, string moduleName) |
| 97 | { |
97 | { |
| 98 | XmlNode commandsNode = xmlNode.findChild("commands"); |
98 | XmlNode commandsNode = xmlNode.findChild("commands"); |
| 99 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
99 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 100 | 100 | ||
| 101 | for (int n = 0; n < commandNodes.size(); n++) |
101 | for (int n = 0; n < commandNodes.size(); n++) |
| 102 | { |
102 | { |
| 103 | map<string, string> attributes = commandNodes[n].getAttributes(); |
103 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 104 | 104 | ||
| 105 | if (commandId >= 128) |
105 | if (commandId >= 128) |
| 106 | { |
106 | { |
| 107 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
107 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
| 108 | { |
108 | { |
| 109 | return attributes["name"]; |
109 | return attributes["name"]; |
| 110 | } |
110 | } |
| 111 | } |
111 | } |
| 112 | else if (stoi(attributes["id"]) == commandId) |
112 | else if (stoi(attributes["id"]) == commandId) |
| 113 | { |
113 | { |
| 114 | return attributes["name"]; |
114 | return attributes["name"]; |
| 115 | } |
115 | } |
| 116 | } |
116 | } |
| 117 | 117 | ||
| 118 | return ""; |
118 | return ""; |
| 119 | } |
119 | } |
| 120 | 120 | ||
| 121 | int CanIdTranslator::resolveCommandId(string commandName, string moduleName) |
121 | int CanIdTranslator::resolveCommandId(string commandName, string moduleName) |
| 122 | { |
122 | { |
| 123 | XmlNode commandsNode = xmlNode.findChild("commands"); |
123 | XmlNode commandsNode = xmlNode.findChild("commands"); |
| 124 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
124 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 125 | 125 | ||
| 126 | for (int n = 0; n < commandNodes.size(); n++) |
126 | for (int n = 0; n < commandNodes.size(); n++) |
| 127 | { |
127 | { |
| 128 | map<string, string> attributes = commandNodes[n].getAttributes(); |
128 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 129 | 129 | ||
| 130 | if (attributes["name"].compare(commandName) == 0) |
130 | if (attributes["name"].compare(commandName) == 0) |
| 131 | { |
131 | { |
| 132 | int commandId = stoi(attributes["id"]); |
132 | int commandId = stoi(attributes["id"]); |
| 133 | 133 | ||
| 134 | if (commandId >= 128) |
134 | if (commandId >= 128) |
| 135 | { |
135 | { |
| 136 | if (attributes["module"] == moduleName) |
136 | if (attributes["module"] == moduleName) |
| 137 | { |
137 | { |
| 138 | return commandId; |
138 | return commandId; |
| 139 | } |
139 | } |
| 140 | } |
140 | } |
| 141 | else |
141 | else |
| 142 | { |
142 | { |
| 143 | return commandId; |
143 | return commandId; |
| 144 | } |
144 | } |
| 145 | } |
145 | } |
| 146 | } |
146 | } |
| 147 | 147 | ||
| 148 | return -1; |
148 | return -1; |
| 149 | } |
149 | } |
| 150 | 150 | ||
| 151 | string CanIdTranslator::lookupNMTCommandName(int commandId) |
151 | string CanIdTranslator::lookupNMTCommandName(int commandId) |
| 152 | { |
152 | { |
| 153 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
153 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
| 154 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
154 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 155 | 155 | ||
| 156 | for (int n = 0; n < commandNodes.size(); n++) |
156 | for (int n = 0; n < commandNodes.size(); n++) |
| 157 | { |
157 | { |
| 158 | map<string, string> attributes = commandNodes[n].getAttributes(); |
158 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 159 | 159 | ||
| 160 | if (stoi(attributes["id"]) == commandId) |
160 | if (stoi(attributes["id"]) == commandId) |
| 161 | { |
161 | { |
| 162 | return attributes["name"]; |
162 | return attributes["name"]; |
| 163 | } |
163 | } |
| 164 | } |
164 | } |
| 165 | 165 | ||
| 166 | return ""; |
166 | return ""; |
| 167 | } |
167 | } |
| 168 | 168 | ||
| 169 | int CanIdTranslator::resolveNMTCommandId(string commandName) |
169 | int CanIdTranslator::resolveNMTCommandId(string commandName) |
| 170 | { |
170 | { |
| 171 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
171 | XmlNode commandsNode = xmlNode.findChild("nmt_messages"); |
| 172 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
172 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 173 | 173 | ||
| 174 | for (int n = 0; n < commandNodes.size(); n++) |
174 | for (int n = 0; n < commandNodes.size(); n++) |
| 175 | { |
175 | { |
| 176 | map<string, string> attributes = commandNodes[n].getAttributes(); |
176 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 177 | 177 | ||
| 178 | if (attributes["name"].compare(commandName) == 0) |
178 | if (attributes["name"].compare(commandName) == 0) |
| 179 | { |
179 | { |
| 180 | return stoi(attributes["id"]); |
180 | return stoi(attributes["id"]); |
| 181 | } |
181 | } |
| 182 | } |
182 | } |
| 183 | 183 | ||
| 184 | return -1; |
184 | return -1; |
| 185 | } |
185 | } |
| 186 | 186 | ||
| 187 | string CanIdTranslator::getDefineId(string name, string group) |
187 | string CanIdTranslator::getDefineId(string name, string group) |
| 188 | { |
188 | { |
| 189 | XmlNode definesNode = xmlNode.findChild("defines"); |
189 | XmlNode definesNode = xmlNode.findChild("defines"); |
| 190 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
190 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
| 191 | 191 | ||
| 192 | for (int n = 0; n < defineNodes.size(); n++) |
192 | for (int n = 0; n < defineNodes.size(); n++) |
| 193 | { |
193 | { |
| 194 | map<string, string> attributes = defineNodes[n].getAttributes(); |
194 | map<string, string> attributes = defineNodes[n].getAttributes(); |
| 195 | 195 | ||
| 196 | if (attributes["group"] == group && attributes["name"] == name) |
196 | if (attributes["group"] == group && attributes["name"] == name) |
| 197 | { |
197 | { |
| 198 | return attributes["id"]; |
198 | return attributes["id"]; |
| 199 | } |
199 | } |
| 200 | } |
200 | } |
| 201 | 201 | ||
| 202 | return ""; |
202 | return ""; |
| 203 | } |
203 | } |
| 204 | 204 | ||
| 205 | string CanIdTranslator::lookupDirectionFlag(int directionFlag) |
205 | string CanIdTranslator::lookupDirectionFlag(int directionFlag) |
| 206 | { |
206 | { |
| 207 | XmlNode definesNode = xmlNode.findChild("defines"); |
207 | XmlNode definesNode = xmlNode.findChild("defines"); |
| 208 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
208 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
| 209 | 209 | ||
| 210 | for (int n = 0; n < defineNodes.size(); n++) |
210 | for (int n = 0; n < defineNodes.size(); n++) |
| 211 | { |
211 | { |
| 212 | map<string, string> attributes = defineNodes[n].getAttributes(); |
212 | map<string, string> attributes = defineNodes[n].getAttributes(); |
| 213 | 213 | ||
| 214 | if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag) |
214 | if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag) |
| 215 | { |
215 | { |
| 216 | return attributes["name"]; |
216 | return attributes["name"]; |
| 217 | } |
217 | } |
| 218 | } |
218 | } |
| 219 | 219 | ||
| 220 | return ""; |
220 | return ""; |
| 221 | } |
221 | } |
| 222 | 222 | ||
| 223 | int CanIdTranslator::resolveDirectionFlag(string directionName) |
223 | int CanIdTranslator::resolveDirectionFlag(string directionName) |
| 224 | { |
224 | { |
| 225 | XmlNode definesNode = xmlNode.findChild("defines"); |
225 | XmlNode definesNode = xmlNode.findChild("defines"); |
| 226 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
226 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
| 227 | 227 | ||
| 228 | for (int n = 0; n < defineNodes.size(); n++) |
228 | for (int n = 0; n < defineNodes.size(); n++) |
| 229 | { |
229 | { |
| 230 | map<string, string> attributes = defineNodes[n].getAttributes(); |
230 | map<string, string> attributes = defineNodes[n].getAttributes(); |
| 231 | 231 | ||
| 232 | if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0) |
232 | if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0) |
| 233 | { |
233 | { |
| 234 | return stoi(attributes["id"]); |
234 | return stoi(attributes["id"]); |
| 235 | } |
235 | } |
| 236 | } |
236 | } |
| 237 | 237 | ||
| 238 | return -1; |
238 | return -1; |
| 239 | } |
239 | } |
| 240 | 240 | ||
| 241 | string CanIdTranslator::lookupModuleName(int moduleId) |
241 | string CanIdTranslator::lookupModuleName(int moduleId) |
| 242 | { |
242 | { |
| 243 | XmlNode modulesNode = xmlNode.findChild("modules"); |
243 | XmlNode modulesNode = xmlNode.findChild("modules"); |
| 244 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
244 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
| 245 | 245 | ||
| 246 | for (int n = 0; n < moduleNodes.size(); n++) |
246 | for (int n = 0; n < moduleNodes.size(); n++) |
| 247 | { |
247 | { |
| 248 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
248 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
| 249 | 249 | ||
| 250 | if (stoi(attributes["id"]) == moduleId) |
250 | if (stoi(attributes["id"]) == moduleId) |
| 251 | { |
251 | { |
| 252 | return attributes["name"]; |
252 | return attributes["name"]; |
| 253 | } |
253 | } |
| 254 | } |
254 | } |
| 255 | 255 | ||
| 256 | return ""; |
256 | return ""; |
| 257 | } |
257 | } |
| 258 | 258 | ||
| 259 | int CanIdTranslator::resolveModuleId(string moduleName) |
259 | int CanIdTranslator::resolveModuleId(string moduleName) |
| 260 | { |
260 | { |
| 261 | XmlNode modulesNode = xmlNode.findChild("modules"); |
261 | XmlNode modulesNode = xmlNode.findChild("modules"); |
| 262 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
262 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
| 263 | 263 | ||
| 264 | for (int n = 0; n < moduleNodes.size(); n++) |
264 | for (int n = 0; n < moduleNodes.size(); n++) |
| 265 | { |
265 | { |
| 266 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
266 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
| 267 | 267 | ||
| 268 | if (attributes["name"].compare(moduleName) == 0) |
268 | if (attributes["name"].compare(moduleName) == 0) |
| 269 | { |
269 | { |
| 270 | return stoi(attributes["id"]); |
270 | return stoi(attributes["id"]); |
| 271 | } |
271 | } |
| 272 | } |
272 | } |
| 273 | 273 | ||
| 274 | return -1; |
274 | return -1; |
| 275 | } |
275 | } |
| 276 | 276 | ||
| 277 | void CanIdTranslator::makeNMTDataValid(int commandId, map<string, CanVariable> &data) |
277 | void CanIdTranslator::makeNMTDataValid(int commandId, map<string, CanVariable> &data) |
| 278 | { |
278 | { |
| 279 | vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
279 | vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
| 280 | 280 | ||
| 281 | for (int n = 0; n < commandNodes.size(); n++) |
281 | for (int n = 0; n < commandNodes.size(); n++) |
| 282 | { |
282 | { |
| 283 | map<string, string> attributes = commandNodes[n].getAttributes(); |
283 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 284 | 284 | ||
| 285 | if (stoi(attributes["id"]) == commandId) |
285 | if (stoi(attributes["id"]) == commandId) |
| 286 | { |
286 | { |
| 287 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
287 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 288 | 288 | ||
| 289 | makeDataValid(varablesNode.getChildren(), data); |
289 | makeDataValid(varablesNode.getChildren(), data); |
| 290 | 290 | ||
| 291 | return; |
291 | return; |
| 292 | } |
292 | } |
| 293 | } |
293 | } |
| 294 | } |
294 | } |
| 295 | 295 | ||
| 296 | void CanIdTranslator::makeDataValid(int commandId, string moduleName, map<string, CanVariable> &data) |
296 | void CanIdTranslator::makeDataValid(int commandId, string moduleName, map<string, CanVariable> &data) |
| 297 | { |
297 | { |
| 298 | vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren(); |
298 | vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren(); |
| 299 | 299 | ||
| 300 | for (int n = 0; n < commandNodes.size(); n++) |
300 | for (int n = 0; n < commandNodes.size(); n++) |
| 301 | { |
301 | { |
| 302 | map<string, string> attributes = commandNodes[n].getAttributes(); |
302 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 303 | 303 | ||
| 304 | bool correct = false; |
304 | bool correct = false; |
| 305 | 305 | ||
| 306 | if (commandId >= 128) |
306 | if (commandId >= 128) |
| 307 | { |
307 | { |
| 308 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
308 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
| 309 | { |
309 | { |
| 310 | correct = true; |
310 | correct = true; |
| 311 | } |
311 | } |
| 312 | } |
312 | } |
| 313 | else if (stoi(attributes["id"]) == commandId) |
313 | else if (stoi(attributes["id"]) == commandId) |
| 314 | { |
314 | { |
| 315 | correct = true; |
315 | correct = true; |
| 316 | } |
316 | } |
| 317 | 317 | ||
| 318 | if (correct) |
318 | if (correct) |
| 319 | { |
319 | { |
| 320 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
320 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 321 | 321 | ||
| 322 | makeDataValid(varablesNode.getChildren(), data); |
322 | makeDataValid(varablesNode.getChildren(), data); |
| 323 | 323 | ||
| 324 | return; |
324 | return; |
| 325 | } |
325 | } |
| 326 | } |
326 | } |
| 327 | } |
327 | } |
| 328 | 328 | ||
| 329 | void CanIdTranslator::makeDataValid(vector<XmlNode> variableNodes, map<string, CanVariable> &data) |
329 | void CanIdTranslator::makeDataValid(vector<XmlNode> variableNodes, map<string, CanVariable> &data) |
| 330 | { |
330 | { |
| 331 | ///FIXME: Remove variables that do not exist in the xmlfile |
331 | ///FIXME: Remove variables that do not exist in the xmlfile |
| 332 | for (int c = 0; c < variableNodes.size(); c++) |
332 | for (int c = 0; c < variableNodes.size(); c++) |
| 333 | { |
333 | { |
| 334 | map<string, string> attributes = variableNodes[c].getAttributes(); |
334 | map<string, string> attributes = variableNodes[c].getAttributes(); |
| 335 | 335 | ||
| 336 | if (data.find(attributes["name"]) != data.end()) |
336 | if (data.find(attributes["name"]) != data.end()) |
| 337 | { |
337 | { |
| 338 | data[attributes["name"]].setType(attributes["type"]); |
338 | data[attributes["name"]].setType(attributes["type"]); |
| 339 | data[attributes["name"]].setUnit(attributes["unit"]); |
339 | data[attributes["name"]].setUnit(attributes["unit"]); |
| 340 | data[attributes["name"]].setBitLength(stoi(attributes["bit_length"])); |
340 | data[attributes["name"]].setBitLength(stoi(attributes["bit_length"])); |
| 341 | data[attributes["name"]].setStartBit(stoi(attributes["start_bit"])); |
341 | data[attributes["name"]].setStartBit(stoi(attributes["start_bit"])); |
| 342 | } |
342 | } |
| 343 | } |
343 | } |
| 344 | } |
344 | } |
| 345 | 345 | ||
| 346 | string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> &data) |
346 | string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> &data) |
| 347 | { |
347 | { |
| 348 | makeDataValid(commandId, moduleName, data); |
348 | makeDataValid(commandId, moduleName, data); |
| 349 | 349 | ||
| 350 | return translateValidDataToHex(data); |
350 | return translateValidDataToHex(data); |
| 351 | } |
351 | } |
| 352 | 352 | ||
| 353 | 353 | ||
| 354 | map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex) |
354 | map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex) |
| 355 | { |
355 | { |
| 356 | vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren(); |
356 | vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren(); |
| 357 | 357 | ||
| 358 | for (int n = 0; n < commandNodes.size(); n++) |
358 | for (int n = 0; n < commandNodes.size(); n++) |
| 359 | { |
359 | { |
| 360 | map<string, string> attributes = commandNodes[n].getAttributes(); |
360 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 361 | 361 | ||
| 362 | bool correct = false; |
362 | bool correct = false; |
| 363 | 363 | ||
| 364 | if (commandId >= 128) |
364 | if (commandId >= 128) |
| 365 | { |
365 | { |
| 366 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
366 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
| 367 | { |
367 | { |
| 368 | correct = true; |
368 | correct = true; |
| 369 | } |
369 | } |
| 370 | } |
370 | } |
| 371 | else if (stoi(attributes["id"]) == commandId) |
371 | else if (stoi(attributes["id"]) == commandId) |
| 372 | { |
372 | { |
| 373 | correct = true; |
373 | correct = true; |
| 374 | } |
374 | } |
| 375 | 375 | ||
| 376 | if (correct) |
376 | if (correct) |
| 377 | { |
377 | { |
| 378 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
378 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 379 | 379 | ||
| 380 | return translateData(varablesNode.getChildren(), dataHex); |
380 | return translateData(varablesNode.getChildren(), dataHex); |
| 381 | } |
381 | } |
| 382 | } |
382 | } |
| 383 | 383 | ||
| 384 | map<string, CanVariable> variables; |
384 | map<string, CanVariable> variables; |
| 385 | return variables; |
385 | return variables; |
| 386 | } |
386 | } |
| 387 | 387 | ||
| 388 | map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex) |
388 | map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex) |
| 389 | { |
389 | { |
| 390 | vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
390 | vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren(); |
| 391 | 391 | ||
| 392 | for (int n = 0; n < commandNodes.size(); n++) |
392 | for (int n = 0; n < commandNodes.size(); n++) |
| 393 | { |
393 | { |
| 394 | map<string, string> attributes = commandNodes[n].getAttributes(); |
394 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 395 | 395 | ||
| 396 | if (stoi(attributes["id"]) == commandId) |
396 | if (stoi(attributes["id"]) == commandId) |
| 397 | { |
397 | { |
| 398 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
398 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 399 | 399 | ||
| 400 | return translateData(varablesNode.getChildren(), dataHex); |
400 | return translateData(varablesNode.getChildren(), dataHex); |
| 401 | } |
401 | } |
| 402 | } |
402 | } |
| 403 | 403 | ||
| 404 | map<string, CanVariable> variables; |
404 | map<string, CanVariable> variables; |
| 405 | return variables; |
405 | return variables; |
| 406 | } |
406 | } |
| 407 | 407 | ||
| 408 | string CanIdTranslator::translateNMTDataToHex(int commandId, map<string, CanVariable> &data) |
408 | string CanIdTranslator::translateNMTDataToHex(int commandId, map<string, CanVariable> &data) |
| 409 | { |
409 | { |
| 410 | makeNMTDataValid(commandId, data); |
410 | makeNMTDataValid(commandId, data); |
| 411 | 411 | ||
| 412 | return translateValidDataToHex(data); |
412 | return translateValidDataToHex(data); |
| 413 | } |
413 | } |
| 414 | 414 | ||
| 415 | string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data) |
415 | string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data) |
| 416 | { |
416 | { |
| 417 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
417 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 418 | int highestBit = 0; |
418 | int highestBit = 0; |
| 419 | 419 | ||
| 420 | for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++) |
420 | for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++) |
| 421 | { |
421 | { |
| 422 | if (iter->second.getType() == "uint") |
422 | if (iter->second.getType() == "uint") |
| 423 | { |
423 | { |
| 424 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
424 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
| 425 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
425 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 426 | 426 | ||
| 427 | unsigned int a = |
427 | unsigned int a = stou(iter->second.getValue()); |
| 428 | 428 | ||
| 429 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
429 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
| 430 | } |
430 | } |
| 431 | else if (iter->second.getType() == "float") |
431 | else if (iter->second.getType() == "float") |
| 432 | { |
432 | { |
| 433 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
433 | if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit) |
| 434 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
434 | highestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 435 | 435 | ||
| 436 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
436 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
| 437 | } |
437 | } |
| 438 | else if (iter->second.getType() == "ascii") |
438 | else if (iter->second.getType() == "ascii") |
| 439 | { |
439 | { |
| 440 | for (int n = 0; n < iter->second.getValue().length(); n++) |
440 | for (int n = 0; n < iter->second.getValue().length(); n++) |
| 441 | { |
441 | { |
| 442 | if (iter->second.getStartBit()+n*8 + 8 > highestBit) |
442 | if (iter->second.getStartBit()+n*8 + 8 > highestBit) |
| 443 | highestBit = iter->second.getStartBit()+n*8 + 8; |
443 | highestBit = iter->second.getStartBit()+n*8 + 8; |
| 444 | 444 | ||
| 445 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
445 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
| 446 | } |
446 | } |
| 447 | } |
447 | } |
| 448 | else if (iter->second.getType() == "hexstring") |
448 | else if (iter->second.getType() == "hexstring") |
| 449 | { |
449 | { |
| 450 | for (int n = 0; n < iter->second.getValue().length(); n++) |
450 | for (int n = 0; n < iter->second.getValue().length(); n++) |
| 451 | { |
451 | { |
| 452 | if (iter->second.getStartBit()+n*4 + 4 > highestBit) |
452 | if (iter->second.getStartBit()+n*4 + 4 > highestBit) |
| 453 | highestBit = iter->second.getStartBit()+n*4 + 4; |
453 | highestBit = iter->second.getStartBit()+n*4 + 4; |
| 454 | 454 | ||
| 455 | string character; |
455 | string character; |
| 456 | character += iter->second.getValue()[n]; |
456 | character += iter->second.getValue()[n]; |
| 457 | 457 | ||
| 458 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
458 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
| 459 | } |
459 | } |
| 460 | } |
460 | } |
| 461 | } |
461 | } |
| 462 | 462 | ||
| 463 | try |
463 | try |
| 464 | { |
464 | { |
| 465 | bin = bin.substr(0, highestBit); |
465 | bin = bin.substr(0, highestBit); |
| 466 | } |
466 | } |
| 467 | catch (std::out_of_range& e) |
467 | catch (std::out_of_range& e) |
| 468 | { |
468 | { |
| 469 | cout << "DEBUG: translateValidDataToHex exception: " << e.what() << "\n"; |
469 | cout << "DEBUG: translateValidDataToHex exception: " << e.what() << "\n"; |
| 470 | cout << "DEBUG: A bin=" << bin << " :: highestBit=" << highestBit << endl; |
470 | cout << "DEBUG: A bin=" << bin << " :: highestBit=" << highestBit << endl; |
| 471 | } |
471 | } |
| 472 | 472 | ||
| 473 | return bin2hex(bin); |
473 | return bin2hex(bin); |
| 474 | } |
474 | } |
| 475 | 475 | ||
| 476 | map<string, CanVariable> CanIdTranslator::translateData(vector<XmlNode> variableNodes, string dataHex) |
476 | map<string, CanVariable> CanIdTranslator::translateData(vector<XmlNode> variableNodes, string dataHex) |
| 477 | { |
477 | { |
| 478 | map<string, CanVariable> variables; |
478 | map<string, CanVariable> variables; |
| 479 | string dataBin = hex2bin(dataHex); |
479 | string dataBin = hex2bin(dataHex); |
| 480 | //dataBin = rpad(dataBin, 64, '0'); |
480 | //dataBin = rpad(dataBin, 64, '0'); |
| 481 | 481 | ||
| 482 | for (int c = 0; c < variableNodes.size(); c++) |
482 | for (int c = 0; c < variableNodes.size(); c++) |
| 483 | { |
483 | { |
| 484 | map<string, string> attributes = variableNodes[c].getAttributes(); |
484 | map<string, string> attributes = variableNodes[c].getAttributes(); |
| 485 | 485 | ||
| 486 | int bitStart = stoi(attributes["start_bit"]); |
486 | int bitStart = stoi(attributes["start_bit"]); |
| 487 | int bitLength = stoi(attributes["bit_length"]); |
487 | int bitLength = stoi(attributes["bit_length"]); |
| 488 | string bits; |
488 | string bits; |
| 489 | 489 | ||
| 490 | try |
490 | try |
| 491 | { |
491 | { |
| 492 | bits = dataBin.substr(bitStart, bitLength); |
492 | bits = dataBin.substr(bitStart, bitLength); |
| 493 | } |
493 | } |
| 494 | catch (std::out_of_range& e) |
494 | catch (std::out_of_range& e) |
| 495 | { |
495 | { |
| 496 | return variables; |
496 | return variables; |
| 497 | } |
497 | } |
| 498 | 498 | ||
| 499 | CanVariable variable; |
499 | CanVariable variable; |
| 500 | 500 | ||
| 501 | variable.setName(attributes["name"]); |
501 | variable.setName(attributes["name"]); |
| 502 | variable.setType(attributes["type"]); |
502 | variable.setType(attributes["type"]); |
| 503 | variable.setUnit(attributes["unit"]); |
503 | variable.setUnit(attributes["unit"]); |
| 504 | variable.setStartBit(bitStart); |
504 | variable.setStartBit(bitStart); |
| 505 | variable.setBitLength(bitLength); |
505 | variable.setBitLength(bitLength); |
| 506 | 506 | ||
| 507 | if (attributes["type"] == "uint") |
507 | if (attributes["type"] == "uint") |
| 508 | { |
508 | { |
| 509 | variable.setValue( |
509 | variable.setValue(utos(bin2uint(bits))); |
| 510 | } |
510 | } |
| 511 | else if (attributes["type"] == "float") |
511 | else if (attributes["type"] == "float") |
| 512 | { |
512 | { |
| 513 | variable.setValue(ftos(bin2float(bits))); |
513 | variable.setValue(ftos(bin2float(bits))); |
| 514 | } |
514 | } |
| 515 | else if (attributes["type"] == "ascii") |
515 | else if (attributes["type"] == "ascii") |
| 516 | { |
516 | { |
| 517 | string str; |
517 | string str; |
| 518 | 518 | ||
| 519 | for (int k = 0; k < bits.length(); k += 8) |
519 | for (int k = 0; k < bits.length(); k += 8) |
| 520 | { |
520 | { |
| 521 | try |
521 | try |
| 522 | { |
522 | { |
| 523 | str += (char)bin2uint(bits.substr(k, 8)); |
523 | str += (char)bin2uint(bits.substr(k, 8)); |
| 524 | } |
524 | } |
| 525 | catch (std::out_of_range& e) |
525 | catch (std::out_of_range& e) |
| 526 | { |
526 | { |
| 527 | cout << "DEBUG: TranslateData exception: " << e.what() << "\n"; |
527 | cout << "DEBUG: TranslateData exception: " << e.what() << "\n"; |
| 528 | cout << "DEBUG: B bits=" << bits << " :: k=" << k << endl; |
528 | cout << "DEBUG: B bits=" << bits << " :: k=" << k << endl; |
| 529 | continue; |
529 | continue; |
| 530 | } |
530 | } |
| 531 | } |
531 | } |
| 532 | 532 | ||
| 533 | variable.setValue(str); |
533 | variable.setValue(str); |
| 534 | } |
534 | } |
| 535 | else if (attributes["type"] == "hexstring") |
535 | else if (attributes["type"] == "hexstring") |
| 536 | { |
536 | { |
| 537 | string str; |
537 | string str; |
| 538 | 538 | ||
| 539 | for (int k = 0; k < bits.length(); k += 4) |
539 | for (int k = 0; k < bits.length(); k += 4) |
| 540 | { |
540 | { |
| 541 | try |
541 | try |
| 542 | { |
542 | { |
| 543 | str += bin2hex(bits.substr(k, 4)); |
543 | str += bin2hex(bits.substr(k, 4)); |
| 544 | } |
544 | } |
| 545 | catch (std::out_of_range& e) |
545 | catch (std::out_of_range& e) |
| 546 | { |
546 | { |
| 547 | cout << "DEBUG: TranslateData exception: " << e.what() << "\n"; |
547 | cout << "DEBUG: TranslateData exception: " << e.what() << "\n"; |
| 548 | cout << "DEBUG: C bits=" << bits << " :: k=" << k << endl; |
548 | cout << "DEBUG: C bits=" << bits << " :: k=" << k << endl; |
| 549 | continue; |
549 | continue; |
| 550 | } |
550 | } |
| 551 | } |
551 | } |
| 552 | 552 | ||
| 553 | variable.setValue(str); |
553 | variable.setValue(str); |
| 554 | } |
554 | } |
| 555 | 555 | ||
| 556 | variables[attributes["name"]] = variable; |
556 | variables[attributes["name"]] = variable; |
| 557 | } |
557 | } |
| 558 | 558 | ||
| 559 | return variables; |
559 | return variables; |
| 560 | } |
560 | } |
| 561 | 561 | ||
| 562 | 562 | ||
| 563 | 563 | ||