Subversion Repositories HomeAutomation

Rev

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

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