Subversion Repositories HomeAutomation

Rev

Rev 1026 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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