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