Subversion Repositories HomeAutomation

Rev

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

Rev 999 Rev 1021
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
    //SyslogStream &slog = SyslogStream::getInstance();
30
    //SyslogStream &slog = SyslogStream::getInstance();
31
 
31
 
32
    //PKT 00060102 1 0 00 00 c4
32
    //PKT 00060102 1 0 00 00 c4
33
 
33
 
34
    rawHex = trim(rawHex, '\n');
34
    rawHex = trim(rawHex, '\n');
35
    rawHex = trim(rawHex);
35
    rawHex = trim(rawHex);
36
 
36
 
37
    myRawHex = rawHex;
37
    myRawHex = rawHex;
38
 
38
 
39
    vector<string> parts = explode(" ", rawHex);
39
    vector<string> parts = explode(" ", rawHex);
40
 
40
 
41
    if (parts.size() < 2 || parts[0].compare("PKT") != 0)
41
    if (parts.size() < 2 || parts[0].compare("PKT") != 0)
42
    {
42
    {
43
        cout << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n";
43
        cout << "Malformed can message received from canDaemon: \"" << rawHex << "\"\n";
44
        throw new CanMessageException("Malformed can message received from canDaemon");
44
        throw new CanMessageException("Malformed can message received from canDaemon");
45
    }
45
    }
46
 
46
 
47
    string rawHeaderBin = hex2bin(parts[1]);
47
    string rawHeaderBin = hex2bin(parts[1]);
48
   
48
   
49
    try
49
    try
50
    {
50
    {
51
        myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4)));
51
        myClassName = translator.lookupClassName(bin2uint(rawHeaderBin.substr(3, 4)));
52
    }
52
    }
53
    catch (std::out_of_range& e)
53
    catch (std::out_of_range& e)
54
    {
54
    {
55
        cout << "DEBUG: setRaw exception: " << e.what() << "\n";
55
        cout << "DEBUG: setRaw exception: " << e.what() << "\n";
56
        cout << "DEBUG: A rawHeaderBin=" << rawHeaderBin << endl;
56
        cout << "DEBUG: A rawHeaderBin=" << rawHeaderBin << endl;
57
    }
57
    }
58
 
58
 
59
 
59
 
60
    if (myClassName == "")
60
    if (myClassName == "")
61
    {
61
    {
62
        myIsUnknown = true;
62
        myIsUnknown = true;
63
        return;
63
        return;
64
    }
64
    }
65
    else
65
    else
66
    {
66
    {
67
        myIsUnknown = false;
67
        myIsUnknown = false;
68
    }
68
    }
69
 
69
 
70
    string rawHexData = "";
70
    string rawHexData = "";
71
    for (int n = 4; n < parts.size(); n++)
71
    for (int n = 4; n < parts.size(); n++)
72
    {
72
    {
73
        rawHexData += parts[n];
73
        rawHexData += parts[n];
74
    }
74
    }
75
 
75
 
76
    if (myClassName == "nmt")
76
    if (myClassName == "nmt")
77
    {
77
    {
78
        int commandId;
78
        int commandId;
79
 
79
 
80
        try
80
        try
81
        {
81
        {
82
            commandId = bin2uint(rawHeaderBin.substr(8, 8));
82
            commandId = bin2uint(rawHeaderBin.substr(8, 8));
83
        }
83
        }
84
        catch (std::out_of_range& e)
84
        catch (std::out_of_range& e)
85
        {
85
        {
86
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
86
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
87
            cout << "DEBUG: B rawHeaderBin=" << rawHeaderBin << endl;
87
            cout << "DEBUG: B rawHeaderBin=" << rawHeaderBin << endl;
88
        }
88
        }
89
 
89
 
90
        myCommandName = translator.lookupNMTCommandName(commandId);
90
        myCommandName = translator.lookupNMTCommandName(commandId);
91
 
91
 
92
        myData = translator.translateNMTData(commandId, rawHexData);
92
        myData = translator.translateNMTData(commandId, rawHexData);
93
 
93
 
94
        myDirectionFlag = "";
94
        myDirectionFlag = "";
95
        myModuleName = "";
95
        myModuleName = "";
96
        myModuleId = 0;
96
        myModuleId = 0;
97
    }
97
    }
98
    else
98
    else
99
    {
99
    {
100
        try
100
        try
101
        {
101
        {
102
            myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1)));
102
            myDirectionFlag = translator.lookupDirectionFlag(bin2uint(rawHeaderBin.substr(7, 1)));
103
        }
103
        }
104
        catch (std::out_of_range& e)
104
        catch (std::out_of_range& e)
105
        {
105
        {
106
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
106
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
107
            cout << "DEBUG: C rawHeaderBin=" << rawHeaderBin << endl;
107
            cout << "DEBUG: C rawHeaderBin=" << rawHeaderBin << endl;
108
        }
108
        }
109
 
109
 
110
        try
110
        try
111
        {
111
        {
112
            myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8)));
112
            myModuleName = translator.lookupModuleName(bin2uint(rawHeaderBin.substr(8, 8)));
113
        }
113
        }
114
        catch (std::out_of_range& e)
114
        catch (std::out_of_range& e)
115
        {
115
        {
116
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
116
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
117
            cout << "DEBUG: D rawHeaderBin=" << rawHeaderBin << endl;
117
            cout << "DEBUG: D rawHeaderBin=" << rawHeaderBin << endl;
118
        }
118
        }
119
 
119
 
120
        try
120
        try
121
        {
121
        {
122
            myModuleId = bin2uint(rawHeaderBin.substr(16, 8));
122
            myModuleId = bin2uint(rawHeaderBin.substr(16, 8));
123
        }
123
        }
124
        catch (std::out_of_range& e)
124
        catch (std::out_of_range& e)
125
        {
125
        {
126
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
126
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
127
            cout << "DEBUG: E rawHeaderBin=" << rawHeaderBin << endl;
127
            cout << "DEBUG: E rawHeaderBin=" << rawHeaderBin << endl;
128
        }
128
        }
129
 
129
 
130
        int commandId;
130
        int commandId;
131
 
131
 
132
        try
132
        try
133
        {
133
        {
134
            commandId = bin2uint(rawHeaderBin.substr(24, 8));
134
            commandId = bin2uint(rawHeaderBin.substr(24, 8));
135
        }
135
        }
136
        catch (std::out_of_range& e)
136
        catch (std::out_of_range& e)
137
        {
137
        {
138
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
138
            cout << "DEBUG: setRaw exception: " << e.what() << "\n";
139
            cout << "DEBUG: F rawHeaderBin=" << rawHeaderBin << endl;
139
            cout << "DEBUG: F rawHeaderBin=" << rawHeaderBin << endl;
140
        }
140
        }
141
       
141
       
142
        myCommandName = translator.lookupCommandName(commandId, myModuleName);
142
        myCommandName = translator.lookupCommandName(commandId, myModuleName);
143
 
143
 
144
        myData = translator.translateData(commandId, myModuleName, rawHexData);
144
        myData = translator.translateData(commandId, myModuleName, rawHexData);
145
    }
145
    }
146
}
146
}
147
 
147
 
148
string CanMessage::getRaw()
148
string CanMessage::getRaw()
149
{
149
{
150
    CanIdTranslator &translator = CanIdTranslator::getInstance();
150
    CanIdTranslator &translator = CanIdTranslator::getInstance();
151
 
151
 
152
    string dataHex;
152
    string dataHex;
153
 
153
 
154
    string rawHex = "PKT ";
154
    string rawHex = "PKT ";
155
 
155
 
156
    string bin = "000";
156
    string bin = "000";
157
 
157
 
158
    int classId = translator.resolveClassId(myClassName);
158
    int classId = translator.resolveClassId(myClassName);
159
    if (classId == -1)
159
    if (classId == -1)
160
        bin += "0000";
160
        bin += "0000";
161
    else
161
    else
162
        bin += uint2bin(classId, 4);
162
        bin += uint2bin(classId, 4);
163
 
163
 
164
    if (myClassName == "nmt")
164
    if (myClassName == "nmt")
165
    {
165
    {
166
        bin +="0";
166
        bin +="0";
167
 
167
 
168
        int commandNameId = translator.resolveNMTCommandId(myCommandName);
168
        int commandNameId = translator.resolveNMTCommandId(myCommandName);
169
        if (commandNameId == -1)
169
        if (commandNameId == -1)
170
            bin += "00000000";
170
            bin += "00000000";
171
        else
171
        else
172
            bin += uint2bin(commandNameId, 8);
172
            bin += uint2bin(commandNameId, 8);
173
 
173
 
174
        bin += "0000000000000000";
174
        bin += "0000000000000000";
175
 
175
 
176
        dataHex = translator.translateNMTDataToHex(commandNameId, myData);
176
        dataHex = translator.translateNMTDataToHex(myCommandName, myData);
177
    }
177
    }
178
    else
178
    else
179
    {
179
    {
180
        bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1);
180
        bin += uint2bin(translator.resolveDirectionFlag(myDirectionFlag), 1);
181
 
181
 
182
 
182
 
183
        int moduleTypeId = translator.resolveModuleId(myModuleName);
183
        int moduleTypeId = translator.resolveModuleId(myModuleName);
184
        if (moduleTypeId == -1)
184
        if (moduleTypeId == -1)
185
            bin += "00000000";
185
            bin += "00000000";
186
        else
186
        else
187
            bin += uint2bin(moduleTypeId, 8);
187
            bin += uint2bin(moduleTypeId, 8);
188
 
188
 
189
 
189
 
190
        int moduleId = myModuleId;
190
        int moduleId = myModuleId;
191
        bin += uint2bin(moduleId, 8);
191
        bin += uint2bin(moduleId, 8);
192
 
192
 
193
 
193
 
194
        int commandNameId = translator.resolveCommandId(myCommandName, myModuleName);
194
        int commandNameId = translator.resolveCommandId(myCommandName, myModuleName);
195
        if (commandNameId == -1)
195
        if (commandNameId == -1)
196
            bin += "00000000";
196
            bin += "00000000";
197
        else
197
        else
198
            bin += uint2bin(commandNameId, 8);
198
            bin += uint2bin(commandNameId, 8);
199
 
199
 
200
        dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData);
200
        dataHex = translator.translateDataToHex(commandNameId, myModuleName, myData);
201
    }
201
    }
202
 
202
 
203
    rawHex += bin2hex(bin);
203
    rawHex += bin2hex(bin);
204
 
204
 
205
    rawHex += " 1 0";
205
    rawHex += " 1 0";
206
 
206
 
207
    while (dataHex.size() > 0)
207
    while (dataHex.size() > 0)
208
    {
208
    {
209
        string hex;
209
        string hex;
210
 
210
 
211
        try
211
        try
212
        {
212
        {
213
            hex = dataHex.substr(0, 2);
213
            hex = dataHex.substr(0, 2);
214
        }
214
        }
215
        catch (std::out_of_range& e)
215
        catch (std::out_of_range& e)
216
        {
216
        {
217
            cout << "DEBUG: getRaw exception: " << e.what() << "\n";
217
            cout << "DEBUG: getRaw exception: " << e.what() << "\n";
218
            cout << "DEBUG: A dataHex=" << dataHex << endl;
218
            cout << "DEBUG: A dataHex=" << dataHex << endl;
219
            continue;
219
            continue;
220
        }
220
        }
221
 
221
 
222
       
222
       
223
        dataHex.erase(0, 2);
223
        dataHex.erase(0, 2);
224
        rawHex += " " + hex;
224
        rawHex += " " + hex;
225
    }
225
    }
226
 
226
 
227
    rawHex += "\n";
227
    rawHex += "\n";
228
 
228
 
229
    return rawHex;
229
    return rawHex;
230
}
230
}
231
 
231
 
232
void CanMessage::setData(map<string, CanVariable> data)
232
void CanMessage::setData(map<string, CanVariable> data)
233
{
233
{
234
    myData = data;
234
    myData = data;
235
 
235
 
236
    CanIdTranslator &translator = CanIdTranslator::getInstance();
236
    CanIdTranslator &translator = CanIdTranslator::getInstance();
237
 
237
 
238
    int commandNameId = translator.resolveNMTCommandId(myCommandName);
238
    int commandNameId = translator.resolveNMTCommandId(myCommandName);
239
 
239
 
240
    if (myClassName == "nmt")
240
    if (myClassName == "nmt")
241
    {
241
    {
242
        translator.makeNMTDataValid(commandNameId, data);
242
        translator.makeNMTDataValid(myCommandName, data);
243
    }
243
    }
244
    else
244
    else
245
    {
245
    {
246
        translator.makeDataValid(commandNameId, myModuleName, data);
246
        translator.makeDataValid(commandNameId, myModuleName, data);
247
    }
247
    }
248
}
248
}
249
 
249
 
250
string CanMessage::getJSONData()
250
string CanMessage::getJSONData()
251
{
251
{
252
    string json = "{";
252
    string json = "{";
253
 
253
 
254
    map<string, CanVariable>::iterator iter;
254
    map<string, CanVariable>::iterator iter;
255
 
255
 
256
    for (iter = myData.begin(); iter != myData.end(); iter++)
256
    for (iter = myData.begin(); iter != myData.end(); iter++)
257
    {
257
    {
258
        json += " " + iter->first + " : ";
258
        json += " " + iter->first + " : ";
259
 
259
 
260
        if (iter->second.getType() == "uint")
260
        if (iter->second.getType() == "uint")
261
        {
261
        {
262
            json += iter->second.getValue();
262
            json += iter->second.getValue();
263
        }
263
        }
264
        else if (iter->second.getType() == "float")
264
        else if (iter->second.getType() == "float")
265
        {
265
        {
266
            json += iter->second.getValue();
266
            json += iter->second.getValue();
267
        }
267
        }
268
        else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring")
268
        else if (iter->second.getType() == "ascii" || iter->second.getType() == "hexstring")
269
        {
269
        {
270
            json += "'" + escape(iter->second.getValue()) + "'";
270
            json += "'" + escape(iter->second.getValue()) + "'";
271
        }
271
        }
272
 
272
 
273
        json += ",";
273
        json += ",";
274
    }
274
    }
275
 
275
 
276
    json = trim(json, ',');
276
    json = trim(json, ',');
277
 
277
 
278
    return json + " }";
278
    return json + " }";
279
}
279
}
280
 
280
 
281
string CanMessage::toString()
281
string CanMessage::toString()
282
{
282
{
283
    string result = "[CanMessage] ClassName: " + myClassName + "\n";
283
    string result = "[CanMessage] ClassName: " + myClassName + "\n";
284
    result += "             DirectionFlag: " + myDirectionFlag + "\n";
284
    result += "             DirectionFlag: " + myDirectionFlag + "\n";
285
    result += "             ModuleName: " + myModuleName + "\n";
285
    result += "             ModuleName: " + myModuleName + "\n";
286
    result += "             ModuleId: " + itos(myModuleId) + "\n";
286
    result += "             ModuleId: " + itos(myModuleId) + "\n";
287
    result += "             CommandName: " + myCommandName + "\n";
287
    result += "             CommandName: " + myCommandName + "\n";
288
    result += "             Data: " + getJSONData() + "\n";
288
    result += "             Data: " + getJSONData() + "\n";
289
    result += "             RawHex: " + myRawHex + "\n";
289
    result += "             RawHex: " + myRawHex + "\n";
290
 
290
 
291
    return result;
291
    return result;
292
}
292
}
293
 
293