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