Subversion Repositories HomeAutomation

Rev

Rev 991 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 991 Rev 997
1
/***************************************************************************
1
/***************************************************************************
2
 *   Copyright (C) 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
        if ((stoi(attributes["id"]) == commandId) || (commandId >= 128 && (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)))
304
        if ((stoi(attributes["id"]) == commandId) || (commandId >= 128 && (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)))
305
        {
305
        {
306
            XmlNode varablesNode = commandNodes[n].findChild("variables");
306
            XmlNode varablesNode = commandNodes[n].findChild("variables");
307
 
307
 
308
            makeDataValid(varablesNode.getChildren(), data);
308
            makeDataValid(varablesNode.getChildren(), data);
309
 
309
 
310
            return;
310
            return;
311
        }
311
        }
312
    }
312
    }
313
}
313
}
314
 
314
 
315
void CanIdTranslator::makeDataValid(vector<XmlNode> variableNodes, map<string, CanVariable> &data)
315
void CanIdTranslator::makeDataValid(vector<XmlNode> variableNodes, map<string, CanVariable> &data)
316
{
316
{
317
    ///FIXME: Remove variables that do not exist in the xmlfile
317
    ///FIXME: Remove variables that do not exist in the xmlfile
318
    for (int c = 0; c < variableNodes.size(); c++)
318
    for (int c = 0; c < variableNodes.size(); c++)
319
    {
319
    {
320
        map<string, string> attributes = variableNodes[c].getAttributes();
320
        map<string, string> attributes = variableNodes[c].getAttributes();
321
 
321
 
322
        if (data.find(attributes["name"]) != data.end())
322
        if (data.find(attributes["name"]) != data.end())
323
        {
323
        {
324
            data[attributes["name"]].setType(attributes["type"]);
324
            data[attributes["name"]].setType(attributes["type"]);
325
            data[attributes["name"]].setUnit(attributes["unit"]);
325
            data[attributes["name"]].setUnit(attributes["unit"]);
326
            data[attributes["name"]].setBitLength(stoi(attributes["bit_length"]));
326
            data[attributes["name"]].setBitLength(stoi(attributes["bit_length"]));
327
            data[attributes["name"]].setStartBit(stoi(attributes["start_bit"]));
327
            data[attributes["name"]].setStartBit(stoi(attributes["start_bit"]));
328
        }
328
        }
329
    }
329
    }
330
}
330
}
331
 
331
 
332
string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> &data)
332
string CanIdTranslator::translateDataToHex(int commandId, string moduleName, map<string, CanVariable> &data)
333
{
333
{
334
    makeDataValid(commandId, moduleName, data);
334
    makeDataValid(commandId, moduleName, data);
335
 
335
 
336
    return translateValidDataToHex(data);
336
    return translateValidDataToHex(data);
337
}
337
}
338
 
338
 
339
 
339
 
340
map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex)
340
map<string, CanVariable> CanIdTranslator::translateData(int commandId, string moduleName, string dataHex)
341
{
341
{
342
    vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren();
342
    vector<XmlNode> commandNodes = xmlNode.findChild("commands").getChildren();
343
 
343
 
344
    for (int n = 0; n < commandNodes.size(); n++)
344
    for (int n = 0; n < commandNodes.size(); n++)
345
    {
345
    {
346
        map<string, string> attributes = commandNodes[n].getAttributes();
346
        map<string, string> attributes = commandNodes[n].getAttributes();
347
 
347
 
348
        if ((stoi(attributes["id"]) == commandId) || (commandId >= 128 && (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)))
348
        if ((stoi(attributes["id"]) == commandId) || (commandId >= 128 && (stoi(attributes["id"]) == commandId && attributes["module"] == moduleName)))
349
        {
349
        {
350
            XmlNode varablesNode = commandNodes[n].findChild("variables");
350
            XmlNode varablesNode = commandNodes[n].findChild("variables");
351
 
351
 
352
            return translateData(varablesNode.getChildren(), dataHex);
352
            return translateData(varablesNode.getChildren(), dataHex);
353
        }
353
        }
354
    }
354
    }
355
 
355
 
356
    map<string, CanVariable> variables;
356
    map<string, CanVariable> variables;
357
    return variables;
357
    return variables;
358
}
358
}
359
 
359
 
360
map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex)
360
map<string, CanVariable> CanIdTranslator::translateNMTData(int commandId, string dataHex)
361
{
361
{
362
    vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();
362
    vector<XmlNode> commandNodes = xmlNode.findChild("nmt_messages").getChildren();
363
 
363
 
364
    for (int n = 0; n < commandNodes.size(); n++)
364
    for (int n = 0; n < commandNodes.size(); n++)
365
    {
365
    {
366
        map<string, string> attributes = commandNodes[n].getAttributes();
366
        map<string, string> attributes = commandNodes[n].getAttributes();
367
 
367
 
368
        if (stoi(attributes["id"]) == commandId)
368
        if (stoi(attributes["id"]) == commandId)
369
        {
369
        {
370
            XmlNode varablesNode = commandNodes[n].findChild("variables");
370
            XmlNode varablesNode = commandNodes[n].findChild("variables");
371
 
371
 
372
            return translateData(varablesNode.getChildren(), dataHex);
372
            return translateData(varablesNode.getChildren(), dataHex);
373
        }
373
        }
374
    }
374
    }
375
 
375
 
376
    map<string, CanVariable> variables;
376
    map<string, CanVariable> variables;
377
    return variables;
377
    return variables;
378
}
378
}
379
 
379
 
380
string CanIdTranslator::translateNMTDataToHex(int commandId, map<string, CanVariable> &data)
380
string CanIdTranslator::translateNMTDataToHex(int commandId, map<string, CanVariable> &data)
381
{
381
{
382
    makeNMTDataValid(commandId, data);
382
    makeNMTDataValid(commandId, data);
383
 
383
 
384
    return translateValidDataToHex(data);
384
    return translateValidDataToHex(data);
385
}
385
}
386
 
386
 
387
string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data)
387
string CanIdTranslator::translateValidDataToHex(map<string, CanVariable> &data)
388
{
388
{
389
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
389
    string bin = "0000000000000000000000000000000000000000000000000000000000000000";
390
    int highestBit = 0;
390
    int highestBit = 0;
391
 
391
 
392
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
392
    for (map<string, CanVariable>::iterator iter = data.begin(); iter != data.end(); iter++)
393
    {
393
    {
394
        if (iter->second.getType() == "uint")
394
        if (iter->second.getType() == "uint")
395
        {
395
        {
396
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
396
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
397
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
397
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
398
 
398
 
399
            unsigned int a = stoi(iter->second.getValue());
399
            unsigned int a = stoi(iter->second.getValue());
400
 
400
 
401
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
401
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), uint2bin(a, iter->second.getBitLength()));
402
        }
402
        }
403
        else if (iter->second.getType() == "float")
403
        else if (iter->second.getType() == "float")
404
        {
404
        {
405
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
405
            if (iter->second.getStartBit() + iter->second.getBitLength() > highestBit)
406
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
406
                highestBit = iter->second.getStartBit() + iter->second.getBitLength();
407
 
407
 
408
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
408
            bin.replace(iter->second.getStartBit(), iter->second.getBitLength(), float2bin(stof(iter->second.getValue()), iter->second.getBitLength()));
409
        }
409
        }
410
        else if (iter->second.getType() == "ascii")
410
        else if (iter->second.getType() == "ascii")
411
        {
411
        {
412
            for (int n = 0; n < iter->second.getValue().length(); n++)
412
            for (int n = 0; n < iter->second.getValue().length(); n++)
413
            {
413
            {
414
                if (iter->second.getStartBit()+n*8 + 8 > highestBit)
414
                if (iter->second.getStartBit()+n*8 + 8 > highestBit)
415
                    highestBit = iter->second.getStartBit()+n*8 + 8;
415
                    highestBit = iter->second.getStartBit()+n*8 + 8;
416
 
416
 
417
                bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8));
417
                bin.replace(iter->second.getStartBit()+n*8, 8, uint2bin((unsigned int)iter->second.getValue()[n], 8));
418
            }
418
            }
419
        }
419
        }
420
        else if (iter->second.getType() == "hexstring")
420
        else if (iter->second.getType() == "hexstring")
421
        {
421
        {
422
            for (int n = 0; n < iter->second.getValue().length(); n++)
422
            for (int n = 0; n < iter->second.getValue().length(); n++)
423
            {
423
            {
424
                if (iter->second.getStartBit()+n*4 + 4 > highestBit)
424
                if (iter->second.getStartBit()+n*4 + 4 > highestBit)
425
                    highestBit = iter->second.getStartBit()+n*4 + 4;
425
                    highestBit = iter->second.getStartBit()+n*4 + 4;
426
 
426
 
427
                string character;
427
                string character;
428
                character += iter->second.getValue()[n];
428
                character += iter->second.getValue()[n];
429
 
429
 
430
                bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character));
430
                bin.replace(iter->second.getStartBit()+n*4, 4, hex2bin(character));
431
            }
431
            }
432
        }
432
        }
433
    }
433
    }
434
 
434
 
-
 
435
    try
-
 
436
    {
435
    bin = bin.substr(0, highestBit);
437
        bin = bin.substr(0, highestBit);
-
 
438
    }
-
 
439
    catch (std::out_of_range& e)
-
 
440
    {
-
 
441
        cout << "DEBUG: translateValidDataToHex exception: " << e.what() << "\n";
-
 
442
        cout << "DEBUG: A bin=" << bin << " :: highestBit=" << highestBit << endl;
-
 
443
    }
436
 
444
 
437
    return bin2hex(bin);
445
    return bin2hex(bin);
438
}
446
}
439
 
447
 
440
map<string, CanVariable> CanIdTranslator::translateData(vector<XmlNode> variableNodes, string dataHex)
448
map<string, CanVariable> CanIdTranslator::translateData(vector<XmlNode> variableNodes, string dataHex)
441
{
449
{
442
    map<string, CanVariable> variables;
450
    map<string, CanVariable> variables;
443
    string dataBin = hex2bin(dataHex);
451
    string dataBin = hex2bin(dataHex);
-
 
452
    //dataBin = rpad(dataBin, 64, '0');
444
 
453
 
445
    for (int c = 0; c < variableNodes.size(); c++)
454
    for (int c = 0; c < variableNodes.size(); c++)
446
    {
455
    {
447
        map<string, string> attributes = variableNodes[c].getAttributes();
456
        map<string, string> attributes = variableNodes[c].getAttributes();
448
 
457
 
449
        int bitStart = stoi(attributes["start_bit"]);
458
        int bitStart = stoi(attributes["start_bit"]);
450
        int bitLength = stoi(attributes["bit_length"]);
459
        int bitLength = stoi(attributes["bit_length"]);
-
 
460
        string bits;
451
 
461
 
-
 
462
        try
-
 
463
        {
452
        string bits = dataBin.substr(bitStart, bitLength);
464
            bits = dataBin.substr(bitStart, bitLength);
-
 
465
        }
-
 
466
        catch (std::out_of_range& e)
-
 
467
        {
-
 
468
            return variables;
-
 
469
        }
453
 
470
 
454
        CanVariable variable;
471
        CanVariable variable;
455
 
472
 
456
        variable.setName(attributes["name"]);
473
        variable.setName(attributes["name"]);
457
        variable.setType(attributes["type"]);
474
        variable.setType(attributes["type"]);
458
        variable.setUnit(attributes["unit"]);
475
        variable.setUnit(attributes["unit"]);
459
        variable.setStartBit(bitStart);
476
        variable.setStartBit(bitStart);
460
        variable.setBitLength(bitLength);
477
        variable.setBitLength(bitLength);
461
 
478
 
462
        if (attributes["type"] == "uint")
479
        if (attributes["type"] == "uint")
463
        {
480
        {
464
            variable.setValue(itos(bin2uint(bits)));
481
            variable.setValue(itos(bin2uint(bits)));
465
        }
482
        }
466
        else if (attributes["type"] == "float")
483
        else if (attributes["type"] == "float")
467
        {
484
        {
468
            variable.setValue(ftos(bin2float(bits)));
485
            variable.setValue(ftos(bin2float(bits)));
469
        }
486
        }
470
        else if (attributes["type"] == "ascii")
487
        else if (attributes["type"] == "ascii")
471
        {
488
        {
472
            string str;
489
            string str;
473
 
490
 
474
            for (int k = 0; k < bits.length(); k += 8)
491
            for (int k = 0; k < bits.length(); k += 8)
-
 
492
            {
-
 
493
                try
475
            {
494
                {
476
                str += (char)bin2uint(bits.substr(k, 8));
495
                    str += (char)bin2uint(bits.substr(k, 8));
-
 
496
                }
-
 
497
                catch (std::out_of_range& e)
-
 
498
                {
-
 
499
                    cout << "DEBUG: TranslateData exception: " << e.what() << "\n";
-
 
500
                    cout << "DEBUG: B bits=" << bits << " :: k=" << k << endl;
-
 
501
                    continue;
-
 
502
                }
477
            }
503
            }
478
 
504
 
479
            variable.setValue(str);
505
            variable.setValue(str);
480
        }
506
        }
481
        else if (attributes["type"] == "hexstring")
507
        else if (attributes["type"] == "hexstring")
482
        {
508
        {
483
            string str;
509
            string str;
484
 
510
 
485
            for (int k = 0; k < bits.length(); k += 4)
511
            for (int k = 0; k < bits.length(); k += 4)
-
 
512
            {
-
 
513
                try
486
            {
514
                {
487
                str += bin2hex(bits.substr(k, 4));
515
                    str += bin2hex(bits.substr(k, 4));
-
 
516
                }
-
 
517
                catch (std::out_of_range& e)
-
 
518
                {
-
 
519
                    cout << "DEBUG: TranslateData exception: " << e.what() << "\n";
-
 
520
                    cout << "DEBUG: C bits=" << bits << " :: k=" << k << endl;
-
 
521
                    continue;
-
 
522
                }
488
            }
523
            }
489
 
524
 
490
            variable.setValue(str);
525
            variable.setValue(str);
491
        }
526
        }
492
 
527
 
493
        variables[attributes["name"]] = variable;
528
        variables[attributes["name"]] = variable;
494
    }
529
    }
495
 
530
 
496
    return variables;
531
    return variables;
497
}
532
}
498
 
533
 
499
 
534
 
500
 
535