Rev 981 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 981 | Rev 985 | ||
|---|---|---|---|
| 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)) |
|
| - | 49 | { |
|
| 48 | xmlNode.load(filename.c_str()); |
50 | xmlNode.load(filename.c_str()); |
| 49 | 51 | ||
| 50 | slog << "Loading definitions from " + filename + ".\n"; |
52 | slog << "Loading definitions from " + filename + ".\n"; |
| - | 53 | } |
|
| - | 54 | else |
|
| - | 55 | { |
|
| - | 56 | slog << "CanIdXMLFile is not defined in the config file, this will probably not work at all.\n"; |
|
| - | 57 | } |
|
| 51 | } |
58 | } |
| 52 | 59 | ||
| 53 | string CanIdTranslator::lookupClassName(int classId) |
60 | string CanIdTranslator::lookupClassName(int classId) |
| 54 | { |
61 | { |
| 55 | XmlNode classesNode = xmlNode.findChild("classes"); |
62 | XmlNode classesNode = xmlNode.findChild("classes"); |
| 56 | vector<XmlNode> classNodes = classesNode.getChildren(); |
63 | vector<XmlNode> classNodes = classesNode.getChildren(); |
| 57 | 64 | ||
| 58 | for (int n = 0; n < classNodes.size(); n++) |
65 | for (int n = 0; n < classNodes.size(); n++) |
| 59 | { |
66 | { |
| 60 | map<string, string> attributes = classNodes[n].getAttributes(); |
67 | map<string, string> attributes = classNodes[n].getAttributes(); |
| 61 | 68 | ||
| 62 | if (stoi(attributes["id"]) == classId) |
69 | if (stoi(attributes["id"]) == classId) |
| 63 | { |
70 | { |
| 64 | return attributes["name"]; |
71 | return attributes["name"]; |
| 65 | } |
72 | } |
| 66 | } |
73 | } |
| 67 | 74 | ||
| 68 | return ""; |
75 | return ""; |
| 69 | } |
76 | } |
| 70 | 77 | ||
| 71 | int CanIdTranslator::resolveClassId(string className) |
78 | int CanIdTranslator::resolveClassId(string className) |
| 72 | { |
79 | { |
| 73 | XmlNode classesNode = xmlNode.findChild("classes"); |
80 | XmlNode classesNode = xmlNode.findChild("classes"); |
| 74 | vector<XmlNode> classNodes = classesNode.getChildren(); |
81 | vector<XmlNode> classNodes = classesNode.getChildren(); |
| 75 | 82 | ||
| 76 | for (int n = 0; n < classNodes.size(); n++) |
83 | for (int n = 0; n < classNodes.size(); n++) |
| 77 | { |
84 | { |
| 78 | map<string, string> attributes = classNodes[n].getAttributes(); |
85 | map<string, string> attributes = classNodes[n].getAttributes(); |
| 79 | 86 | ||
| 80 | if (attributes["name"].compare(className) == 0) |
87 | if (attributes["name"].compare(className) == 0) |
| 81 | { |
88 | { |
| 82 | return stoi(attributes["id"]); |
89 | return stoi(attributes["id"]); |
| 83 | } |
90 | } |
| 84 | } |
91 | } |
| 85 | 92 | ||
| 86 | return -1; |
93 | return -1; |
| 87 | } |
94 | } |
| 88 | 95 | ||
| 89 | string CanIdTranslator::lookupCommandName(int commandId, string moduleName) |
96 | string CanIdTranslator::lookupCommandName(int commandId, string moduleName) |
| 90 | { |
97 | { |
| 91 | XmlNode commandsNode = xmlNode.findChild("commands"); |
98 | XmlNode commandsNode = xmlNode.findChild("commands"); |
| 92 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
99 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 93 | 100 | ||
| 94 | for (int n = 0; n < commandNodes.size(); n++) |
101 | for (int n = 0; n < commandNodes.size(); n++) |
| 95 | { |
102 | { |
| 96 | map<string, string> attributes = commandNodes[n].getAttributes(); |
103 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 97 | 104 | ||
| 98 | if (commandId >= 128) |
105 | if (commandId >= 128) |
| 99 | { |
106 | { |
| 100 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
107 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
| 101 | { |
108 | { |
| 102 | return attributes["name"]; |
109 | return attributes["name"]; |
| 103 | } |
110 | } |
| 104 | } |
111 | } |
| 105 | else if (stoi(attributes["id"]) == commandId) |
112 | else if (stoi(attributes["id"]) == commandId) |
| 106 | { |
113 | { |
| 107 | return attributes["name"]; |
114 | return attributes["name"]; |
| 108 | } |
115 | } |
| 109 | } |
116 | } |
| 110 | 117 | ||
| 111 | return ""; |
118 | return ""; |
| 112 | } |
119 | } |
| 113 | 120 | ||
| 114 | int CanIdTranslator::resolveCommandId(string commandName, string moduleName) |
121 | int CanIdTranslator::resolveCommandId(string commandName, string moduleName) |
| 115 | { |
122 | { |
| 116 | XmlNode commandsNode = xmlNode.findChild("commands"); |
123 | XmlNode commandsNode = xmlNode.findChild("commands"); |
| 117 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
124 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 118 | 125 | ||
| 119 | for (int n = 0; n < commandNodes.size(); n++) |
126 | for (int n = 0; n < commandNodes.size(); n++) |
| 120 | { |
127 | { |
| 121 | map<string, string> attributes = commandNodes[n].getAttributes(); |
128 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 122 | 129 | ||
| 123 | if (attributes["name"].compare(commandName) == 0) |
130 | if (attributes["name"].compare(commandName) == 0) |
| 124 | { |
131 | { |
| 125 | int commandId = stoi(attributes["id"]); |
132 | int commandId = stoi(attributes["id"]); |
| 126 | 133 | ||
| 127 | if (commandId >= 128) |
134 | if (commandId >= 128) |
| 128 | { |
135 | { |
| 129 | if (attributes["module"] == moduleName) |
136 | if (attributes["module"] == moduleName) |
| 130 | { |
137 | { |
| 131 | return commandId; |
138 | return commandId; |
| 132 | } |
139 | } |
| 133 | } |
140 | } |
| 134 | else |
141 | else |
| 135 | { |
142 | { |
| 136 | return commandId; |
143 | return commandId; |
| 137 | } |
144 | } |
| 138 | } |
145 | } |
| 139 | } |
146 | } |
| 140 | 147 | ||
| 141 | return -1; |
148 | return -1; |
| 142 | } |
149 | } |
| 143 | 150 | ||
| 144 | string CanIdTranslator::getDefineId(string name, string group) |
151 | string CanIdTranslator::getDefineId(string name, string group) |
| 145 | { |
152 | { |
| 146 | XmlNode definesNode = xmlNode.findChild("defines"); |
153 | XmlNode definesNode = xmlNode.findChild("defines"); |
| 147 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
154 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
| 148 | 155 | ||
| 149 | for (int n = 0; n < defineNodes.size(); n++) |
156 | for (int n = 0; n < defineNodes.size(); n++) |
| 150 | { |
157 | { |
| 151 | map<string, string> attributes = defineNodes[n].getAttributes(); |
158 | map<string, string> attributes = defineNodes[n].getAttributes(); |
| 152 | 159 | ||
| 153 | if (attributes["group"] == group && attributes["name"] == name) |
160 | if (attributes["group"] == group && attributes["name"] == name) |
| 154 | { |
161 | { |
| 155 | return attributes["id"]; |
162 | return attributes["id"]; |
| 156 | } |
163 | } |
| 157 | } |
164 | } |
| 158 | 165 | ||
| 159 | return ""; |
166 | return ""; |
| 160 | } |
167 | } |
| 161 | 168 | ||
| 162 | string CanIdTranslator::lookupDirectionFlag(int directionFlag) |
169 | string CanIdTranslator::lookupDirectionFlag(int directionFlag) |
| 163 | { |
170 | { |
| 164 | XmlNode definesNode = xmlNode.findChild("defines"); |
171 | XmlNode definesNode = xmlNode.findChild("defines"); |
| 165 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
172 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
| 166 | 173 | ||
| 167 | for (int n = 0; n < defineNodes.size(); n++) |
174 | for (int n = 0; n < defineNodes.size(); n++) |
| 168 | { |
175 | { |
| 169 | map<string, string> attributes = defineNodes[n].getAttributes(); |
176 | map<string, string> attributes = defineNodes[n].getAttributes(); |
| 170 | 177 | ||
| 171 | if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag) |
178 | if (attributes["group"].compare("DirectionFlag") == 0 && stoi(attributes["id"]) == directionFlag) |
| 172 | { |
179 | { |
| 173 | return attributes["name"]; |
180 | return attributes["name"]; |
| 174 | } |
181 | } |
| 175 | } |
182 | } |
| 176 | 183 | ||
| 177 | return ""; |
184 | return ""; |
| 178 | } |
185 | } |
| 179 | 186 | ||
| 180 | int CanIdTranslator::resolveDirectionFlag(string directionName) |
187 | int CanIdTranslator::resolveDirectionFlag(string directionName) |
| 181 | { |
188 | { |
| 182 | XmlNode definesNode = xmlNode.findChild("defines"); |
189 | XmlNode definesNode = xmlNode.findChild("defines"); |
| 183 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
190 | vector<XmlNode> defineNodes = definesNode.getChildren(); |
| 184 | 191 | ||
| 185 | for (int n = 0; n < defineNodes.size(); n++) |
192 | for (int n = 0; n < defineNodes.size(); n++) |
| 186 | { |
193 | { |
| 187 | map<string, string> attributes = defineNodes[n].getAttributes(); |
194 | map<string, string> attributes = defineNodes[n].getAttributes(); |
| 188 | 195 | ||
| 189 | if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0) |
196 | if (attributes["group"].compare("DirectionFlag") == 0 && attributes["name"].compare(directionName) == 0) |
| 190 | { |
197 | { |
| 191 | return stoi(attributes["id"]); |
198 | return stoi(attributes["id"]); |
| 192 | } |
199 | } |
| 193 | } |
200 | } |
| 194 | 201 | ||
| 195 | return -1; |
202 | return -1; |
| 196 | } |
203 | } |
| 197 | 204 | ||
| 198 | string CanIdTranslator::lookupModuleName(int moduleId) |
205 | string CanIdTranslator::lookupModuleName(int moduleId) |
| 199 | { |
206 | { |
| 200 | XmlNode modulesNode = xmlNode.findChild("modules"); |
207 | XmlNode modulesNode = xmlNode.findChild("modules"); |
| 201 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
208 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
| 202 | 209 | ||
| 203 | for (int n = 0; n < moduleNodes.size(); n++) |
210 | for (int n = 0; n < moduleNodes.size(); n++) |
| 204 | { |
211 | { |
| 205 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
212 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
| 206 | 213 | ||
| 207 | if (stoi(attributes["id"]) == moduleId) |
214 | if (stoi(attributes["id"]) == moduleId) |
| 208 | { |
215 | { |
| 209 | return attributes["name"]; |
216 | return attributes["name"]; |
| 210 | } |
217 | } |
| 211 | } |
218 | } |
| 212 | 219 | ||
| 213 | return ""; |
220 | return ""; |
| 214 | } |
221 | } |
| 215 | 222 | ||
| 216 | int CanIdTranslator::resolveModuleId(string moduleName) |
223 | int CanIdTranslator::resolveModuleId(string moduleName) |
| 217 | { |
224 | { |
| 218 | XmlNode modulesNode = xmlNode.findChild("modules"); |
225 | XmlNode modulesNode = xmlNode.findChild("modules"); |
| 219 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
226 | vector<XmlNode> moduleNodes = modulesNode.getChildren(); |
| 220 | 227 | ||
| 221 | for (int n = 0; n < moduleNodes.size(); n++) |
228 | for (int n = 0; n < moduleNodes.size(); n++) |
| 222 | { |
229 | { |
| 223 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
230 | map<string, string> attributes = moduleNodes[n].getAttributes(); |
| 224 | 231 | ||
| 225 | if (attributes["name"].compare(moduleName) == 0) |
232 | if (attributes["name"].compare(moduleName) == 0) |
| 226 | { |
233 | { |
| 227 | return stoi(attributes["id"]); |
234 | return stoi(attributes["id"]); |
| 228 | } |
235 | } |
| 229 | } |
236 | } |
| 230 | 237 | ||
| 231 | return -1; |
238 | return -1; |
| 232 | } |
239 | } |
| 233 | 240 | ||
| 234 | string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> data) |
241 | string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> data) |
| 235 | { |
242 | { |
| 236 | XmlNode commandsNode = xmlNode.findChild("commands"); |
243 | XmlNode commandsNode = xmlNode.findChild("commands"); |
| 237 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
244 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 238 | 245 | ||
| 239 | map<string, CanVariable> sortedData; |
246 | map<string, CanVariable> sortedData; |
| 240 | 247 | ||
| 241 | for (int n = 0; n < commandNodes.size(); n++) |
248 | for (int n = 0; n < commandNodes.size(); n++) |
| 242 | { |
249 | { |
| 243 | map<string, string> attributes = commandNodes[n].getAttributes(); |
250 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 244 | 251 | ||
| 245 | bool correct = false; |
252 | bool correct = false; |
| 246 | 253 | ||
| 247 | if (commandId >= 128) |
254 | if (commandId >= 128) |
| 248 | { |
255 | { |
| 249 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
256 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
| 250 | { |
257 | { |
| 251 | correct = true; |
258 | correct = true; |
| 252 | } |
259 | } |
| 253 | } |
260 | } |
| 254 | else if (stoi(attributes["id"]) == commandId) |
261 | else if (stoi(attributes["id"]) == commandId) |
| 255 | { |
262 | { |
| 256 | correct = true; |
263 | correct = true; |
| 257 | } |
264 | } |
| 258 | 265 | ||
| 259 | if (correct) |
266 | if (correct) |
| 260 | { |
267 | { |
| 261 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
268 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 262 | 269 | ||
| 263 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
270 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
| 264 | 271 | ||
| 265 | for (int c = 0; c < variableNodes.size(); c++) |
272 | for (int c = 0; c < variableNodes.size(); c++) |
| 266 | { |
273 | { |
| 267 | map<string, string> attributes = variableNodes[c].getAttributes(); |
274 | map<string, string> attributes = variableNodes[c].getAttributes(); |
| 268 | 275 | ||
| 269 | if (data.find(attributes["name"]) != data.end()) |
276 | if (data.find(attributes["name"]) != data.end()) |
| 270 | { |
277 | { |
| 271 | int bitStart = stoi(attributes["start_bit"]);///FIXME: This is not a good way |
278 | int bitStart = stoi(attributes["start_bit"]);///FIXME: This is not a good way |
| 272 | int bitLength = stoi(attributes["bit_length"]); |
279 | int bitLength = stoi(attributes["bit_length"]); |
| 273 | 280 | ||
| 274 | sortedData[attributes["name"]].setType(attributes["type"]); |
281 | sortedData[attributes["name"]].setType(attributes["type"]); |
| 275 | sortedData[attributes["name"]].setUnit(attributes["unit"]); |
282 | sortedData[attributes["name"]].setUnit(attributes["unit"]); |
| 276 | sortedData[attributes["name"]].setBitLength(bitLength); |
283 | sortedData[attributes["name"]].setBitLength(bitLength); |
| 277 | sortedData[attributes["name"]].setBitLength(bitLength); |
284 | sortedData[attributes["name"]].setBitLength(bitLength); |
| 278 | sortedData[attributes["name"]].setStartBit(bitStart); |
285 | sortedData[attributes["name"]].setStartBit(bitStart); |
| 279 | sortedData[attributes["name"]].setName(attributes["name"]); |
286 | sortedData[attributes["name"]].setName(attributes["name"]); |
| 280 | sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue()); |
287 | sortedData[attributes["name"]].setValue(data[attributes["name"]].getValue()); |
| 281 | } |
288 | } |
| 282 | } |
289 | } |
| 283 | } |
290 | } |
| 284 | } |
291 | } |
| 285 | 292 | ||
| 286 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
293 | string bin = "0000000000000000000000000000000000000000000000000000000000000000"; |
| 287 | int heighestBit = 0; |
294 | int heighestBit = 0; |
| 288 | 295 | ||
| 289 | map<string, CanVariable>::iterator iter; |
296 | map<string, CanVariable>::iterator iter; |
| 290 | 297 | ||
| 291 | for (iter = sortedData.begin(); iter != sortedData.end(); iter++)///FIXME: We should check order and length |
298 | for (iter = sortedData.begin(); iter != sortedData.end(); iter++)///FIXME: We should check order and length |
| 292 | { |
299 | { |
| 293 | if (iter->second.getType() == "uint") |
300 | if (iter->second.getType() == "uint") |
| 294 | { |
301 | { |
| 295 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
302 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
| 296 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
303 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 297 | 304 | ||
| 298 | //cout << iter->first << ":" << stoi(iter->second.getValue()) << endl; |
305 | //cout << iter->first << ":" << stoi(iter->second.getValue()) << endl; |
| 299 | unsigned int a = stoi(iter->second.getValue()); |
306 | unsigned int a = stoi(iter->second.getValue()); |
| 300 | 307 | ||
| 301 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
308 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength())); |
| 302 | } |
309 | } |
| 303 | else if (iter->second.getType() == "float") |
310 | else if (iter->second.getType() == "float") |
| 304 | { |
311 | { |
| 305 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
312 | if (iter->second.getStartBit() + iter->second.getBitLength() > heighestBit) |
| 306 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
313 | heighestBit = iter->second.getStartBit() + iter->second.getBitLength(); |
| 307 | 314 | ||
| 308 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
315 | bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength())); |
| 309 | } |
316 | } |
| 310 | else if (iter->second.getType() == "ascii") |
317 | else if (iter->second.getType() == "ascii") |
| 311 | { |
318 | { |
| 312 | //cout << iter->second.getValue() << endl; |
319 | //cout << iter->second.getValue() << endl; |
| 313 | for (int n = 0; n < iter->second.getValue().length(); n++) |
320 | for (int n = 0; n < iter->second.getValue().length(); n++) |
| 314 | { |
321 | { |
| 315 | if (iter->second.getStartBit()+n*8 + 8 > heighestBit) |
322 | if (iter->second.getStartBit()+n*8 + 8 > heighestBit) |
| 316 | heighestBit = iter->second.getStartBit()+n*8 + 8; |
323 | heighestBit = iter->second.getStartBit()+n*8 + 8; |
| 317 | 324 | ||
| 318 | //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl; |
325 | //cout << iter->second.getValue()[n] << ":" << (unsigned int)iter->second.getValue()[n] << endl; |
| 319 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
326 | bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8)); |
| 320 | } |
327 | } |
| 321 | } |
328 | } |
| 322 | else if (iter->second.getType() == "hexstring") |
329 | else if (iter->second.getType() == "hexstring") |
| 323 | { |
330 | { |
| 324 | for (int n = 0; n < iter->second.getValue().length(); n++) |
331 | for (int n = 0; n < iter->second.getValue().length(); n++) |
| 325 | { |
332 | { |
| 326 | if (iter->second.getStartBit()+n*4 + 4 > heighestBit) |
333 | if (iter->second.getStartBit()+n*4 + 4 > heighestBit) |
| 327 | heighestBit = iter->second.getStartBit()+n*4 + 4; |
334 | heighestBit = iter->second.getStartBit()+n*4 + 4; |
| 328 | 335 | ||
| 329 | string character; |
336 | string character; |
| 330 | character += iter->second.getValue()[n]; |
337 | character += iter->second.getValue()[n]; |
| 331 | 338 | ||
| 332 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
339 | bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character)); |
| 333 | } |
340 | } |
| 334 | } |
341 | } |
| 335 | } |
342 | } |
| 336 | 343 | ||
| 337 | bin = bin.substr(0, heighestBit); |
344 | bin = bin.substr(0, heighestBit); |
| 338 | 345 | ||
| 339 | //cout << bin2hex(bin) << endl; |
346 | //cout << bin2hex(bin) << endl; |
| 340 | 347 | ||
| 341 | return bin2hex(bin); |
348 | return bin2hex(bin); |
| 342 | } |
349 | } |
| 343 | 350 | ||
| 344 | map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex) |
351 | map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex) |
| 345 | { |
352 | { |
| 346 | map<string, CanVariable> variables; |
353 | map<string, CanVariable> variables; |
| 347 | XmlNode commandsNode = xmlNode.findChild("commands"); |
354 | XmlNode commandsNode = xmlNode.findChild("commands"); |
| 348 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
355 | vector<XmlNode> commandNodes = commandsNode.getChildren(); |
| 349 | 356 | ||
| 350 | string dataBin = hex2bin(dataHex); |
357 | string dataBin = hex2bin(dataHex); |
| 351 | //string dataString = "{"; |
358 | //string dataString = "{"; |
| 352 | 359 | ||
| 353 | for (int n = 0; n < commandNodes.size(); n++) |
360 | for (int n = 0; n < commandNodes.size(); n++) |
| 354 | { |
361 | { |
| 355 | map<string, string> attributes = commandNodes[n].getAttributes(); |
362 | map<string, string> attributes = commandNodes[n].getAttributes(); |
| 356 | 363 | ||
| 357 | bool correct = false; |
364 | bool correct = false; |
| 358 | 365 | ||
| 359 | if (commandId >= 128) |
366 | if (commandId >= 128) |
| 360 | { |
367 | { |
| 361 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
368 | if (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName) |
| 362 | { |
369 | { |
| 363 | correct = true; |
370 | correct = true; |
| 364 | } |
371 | } |
| 365 | } |
372 | } |
| 366 | else if (stoi(attributes["id"]) == commandId) |
373 | else if (stoi(attributes["id"]) == commandId) |
| 367 | { |
374 | { |
| 368 | correct = true; |
375 | correct = true; |
| 369 | } |
376 | } |
| 370 | 377 | ||
| 371 | if (correct) |
378 | if (correct) |
| 372 | { |
379 | { |
| 373 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
380 | XmlNode varablesNode = commandNodes[n].findChild("variables"); |
| 374 | 381 | ||
| 375 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
382 | vector<XmlNode> variableNodes = varablesNode.getChildren(); |
| 376 | 383 | ||
| 377 | for (int c = 0; c < variableNodes.size(); c++) |
384 | for (int c = 0; c < variableNodes.size(); c++) |
| 378 | { |
385 | { |
| 379 | map<string, string> attributes = variableNodes[c].getAttributes(); |
386 | map<string, string> attributes = variableNodes[c].getAttributes(); |
| 380 | 387 | ||
| 381 | int bitStart = stoi(attributes["start_bit"]); |
388 | int bitStart = stoi(attributes["start_bit"]); |
| 382 | int bitLength = stoi(attributes["bit_length"]); |
389 | int bitLength = stoi(attributes["bit_length"]); |
| 383 | 390 | ||
| 384 | string bits = dataBin.substr(bitStart, bitLength); |
391 | string bits = dataBin.substr(bitStart, bitLength); |
| 385 | 392 | ||
| 386 | CanVariable variable; |
393 | CanVariable variable; |
| 387 | 394 | ||
| 388 | variable.setName(attributes["name"]); |
395 | variable.setName(attributes["name"]); |
| 389 | variable.setType(attributes["type"]); |
396 | variable.setType(attributes["type"]); |
| 390 | variable.setUnit(attributes["unit"]); |
397 | variable.setUnit(attributes["unit"]); |
| 391 | variable.setBitLength(bitLength); |
398 | variable.setBitLength(bitLength); |
| 392 | 399 | ||
| 393 | //dataString += " " + attributes["name"] + " : { value : "; |
400 | //dataString += " " + attributes["name"] + " : { value : "; |
| 394 | //dataString += attributes["name"] + "="; |
401 | //dataString += attributes["name"] + "="; |
| 395 | //dataString += attributes["name"] + ":" + attributes["type"] + ":" + attributes["bit_length"] + "=\""; |
402 | //dataString += attributes["name"] + ":" + attributes["type"] + ":" + attributes["bit_length"] + "=\""; |
| 396 | 403 | ||
| 397 | if (attributes["type"] == "uint") |
404 | if (attributes["type"] == "uint") |
| 398 | { |
405 | { |
| 399 | variable.setValue(itos(bin2uint(bits))); |
406 | variable.setValue(itos(bin2uint(bits))); |
| 400 | //dataString += itos(bin2uint(bits)); |
407 | //dataString += itos(bin2uint(bits)); |
| 401 | } |
408 | } |
| 402 | else if (attributes["type"] == "float") |
409 | else if (attributes["type"] == "float") |
| 403 | { |
410 | { |
| 404 | variable.setValue(ftos(bin2float(bits))); |
411 | variable.setValue(ftos(bin2float(bits))); |
| 405 | //dataString += ftos(bin2float(bits)); |
412 | //dataString += ftos(bin2float(bits)); |
| 406 | } |
413 | } |
| 407 | else if (attributes["type"] == "ascii") |
414 | else if (attributes["type"] == "ascii") |
| 408 | { |
415 | { |
| 409 | string str; |
416 | string str; |
| 410 | //dataString += "'"; |
417 | //dataString += "'"; |
| 411 | 418 | ||
| 412 | for (int k = 0; k < bits.length(); k += 8) |
419 | for (int k = 0; k < bits.length(); k += 8) |
| 413 | { |
420 | { |
| 414 | str += (char)bin2uint(bits.substr(k, 8)); |
421 | str += (char)bin2uint(bits.substr(k, 8)); |
| 415 | //dataString += (char)bin2uint(bits.substr(k, 4)); |
422 | //dataString += (char)bin2uint(bits.substr(k, 4)); |
| 416 | } |
423 | } |
| 417 | 424 | ||
| 418 | variable.setValue(str); |
425 | variable.setValue(str); |
| 419 | //cout << "Parsed ascii: " << str << "\n"; |
426 | //cout << "Parsed ascii: " << str << "\n"; |
| 420 | //dataString += "'"; |
427 | //dataString += "'"; |
| 421 | } |
428 | } |
| 422 | else if (attributes["type"] == "hexstring") |
429 | else if (attributes["type"] == "hexstring") |
| 423 | { |
430 | { |
| 424 | string str; |
431 | string str; |
| 425 | 432 | ||
| 426 | for (int k = 0; k < bits.length(); k += 4) |
433 | for (int k = 0; k < bits.length(); k += 4) |
| 427 | { |
434 | { |
| 428 | str += bin2hex(bits.substr(k, 4)); |
435 | str += bin2hex(bits.substr(k, 4)); |
| 429 | //dataString += (char)bin2uint(bits.substr(k, 4)); |
436 | //dataString += (char)bin2uint(bits.substr(k, 4)); |
| 430 | } |
437 | } |
| 431 | 438 | ||
| 432 | variable.setValue(str); |
439 | variable.setValue(str); |
| 433 | } |
440 | } |
| 434 | 441 | ||
| 435 | variables[attributes["name"]] = variable; |
442 | variables[attributes["name"]] = variable; |
| 436 | 443 | ||
| 437 | //dataString += ", type : '" + attributes["type"] + "', bits : " + attributes["bit_length"] + ", unit : '" + attributes["unit"] + "' },"; |
444 | //dataString += ", type : '" + attributes["type"] + "', bits : " + attributes["bit_length"] + ", unit : '" + attributes["unit"] + "' },"; |
| 438 | 445 | ||
| 439 | //dataString += ","; |
446 | //dataString += ","; |
| 440 | //dataString += "\","; |
447 | //dataString += "\","; |
| 441 | } |
448 | } |
| 442 | 449 | ||
| 443 | //dataString = trim(dataString, ','); |
450 | //dataString = trim(dataString, ','); |
| 444 | 451 | ||
| 445 | //dataString += " }"; |
452 | //dataString += " }"; |
| 446 | 453 | ||
| 447 | //return dataString; |
454 | //return dataString; |
| 448 | 455 | ||
| 449 | 456 | ||
| 450 | return variables; |
457 | return variables; |
| 451 | } |
458 | } |
| 452 | } |
459 | } |
| 453 | 460 | ||
| 454 | //return dataString + " ]"; |
461 | //return dataString + " ]"; |
| 455 | return variables; |
462 | return variables; |
| 456 | } |
463 | } |
| 457 | 464 | ||
| 458 | map<string, CanVariable> CanIdTranslator::translateHeartbeatData(string dataHex) |
465 | map<string, CanVariable> CanIdTranslator::translateHeartbeatData(string dataHex) |
| 459 | { |
466 | { |
| 460 | map<string, CanVariable> variables; |
467 | map<string, CanVariable> variables; |
| 461 | 468 | ||
| 462 | string dataBin = hex2bin(dataHex); |
469 | string dataBin = hex2bin(dataHex); |
| 463 | string bits = dataBin.substr(0, 32); |
470 | string bits = dataBin.substr(0, 32); |
| 464 | 471 | ||
| 465 | CanVariable canVariable("HardwareId", itos(bin2uint(bits)), "uint", ""); |
472 | CanVariable canVariable("HardwareId", itos(bin2uint(bits)), "uint", ""); |
| 466 | canVariable.setBitLength(32); |
473 | canVariable.setBitLength(32); |
| 467 | 474 | ||
| 468 | variables["HardwareId"] = canVariable; |
475 | variables["HardwareId"] = canVariable; |
| 469 | 476 | ||
| 470 | return variables; |
477 | return variables; |
| 471 | 478 | ||
| 472 | //return "{ HardwareId : { value : " + itos(bin2uint(bits)) + ", type : 'uint', bits : 32, unit : '' }"; |
479 | //return "{ HardwareId : { value : " + itos(bin2uint(bits)) + ", type : 'uint', bits : 32, unit : '' }"; |
| 473 | //return "HardwareId:uint:32=\"" + itos(bin2uint(bits)) + "\""; |
480 | //return "HardwareId:uint:32=\"" + itos(bin2uint(bits)) + "\""; |
| 474 | } |
481 | } |
| 475 | 482 | ||