Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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