Subversion Repositories HomeAutomation

Rev

Rev 1598 | Rev 1600 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1596 runge 1
/*
2
 *
3
 *  Copyright (C) 2010  Mattias Runge
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "Protocol.h"
22
 
23
#include <stdexcept>
1598 runge 24
#include <string.h>
1596 runge 25
 
26
#include <boost/lexical_cast.hpp>
27
 
28
namespace atom {
29
namespace can {
30
 
1599 runge 31
Protocol::Pointer Protocol::instance_;
1596 runge 32
 
33
Protocol::Protocol() : LOG("can::Protocol")
34
{
35
}
36
 
37
Protocol::~Protocol()
38
{
39
}
40
 
1599 runge 41
void Protocol::Create()
42
{
43
    Protocol::instance_ = Protocol::Pointer(new Protocol());
44
}
45
 
1596 runge 46
Protocol::Pointer Protocol::Instance()
47
{
48
    return Protocol::instance_;
49
}
50
 
51
void Protocol::Delete()
52
{
53
    Protocol::instance_.reset();
54
}
55
 
56
bool Protocol::Load(std::string filename)
57
{
58
    try
59
    {
60
        this->root_node_.LoadFile(filename);
61
    }
62
    catch (std::runtime_error& e)
63
    {
64
        LOG.Error(e.what());
65
        return false;
66
    }
67
 
1598 runge 68
    LOG.Info("Protocol loaded successfully.");
1596 runge 69
    return true;
70
}
71
 
72
std::string Protocol::LookupClassName(unsigned int class_id)
73
{
74
    try
75
    {
76
        return this->root_node_.FindChild("classes").SelectChild("id", boost::lexical_cast<std::string>(class_id)).GetAttributeValue("name");
77
    }
78
    catch (std::runtime_error& e)
79
    {
80
        LOG.Error(e.what());
81
        throw std::runtime_error("No such class_id found, " + boost::lexical_cast<std::string>(class_id));
82
    }
83
}
84
 
85
unsigned int Protocol::ResolveClassId(std::string class_name)
86
{
87
    try
88
    {
89
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("classes").SelectChild("name", class_name).GetAttributeValue("id"));
90
    }
91
    catch (std::runtime_error& e)
92
    {
93
        LOG.Error(e.what());
94
        throw std::runtime_error("No such class_name found, " + class_name);
95
    }
96
}
97
 
98
std::string Protocol::LookupCommandName(unsigned int command_id, std::string module_name)
99
{
100
    try
101
    {
102
        if (command_id >= 128)
103
        {
104
            return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id), "module", module_name).GetAttributeValue("name");
105
        }
106
 
107
        return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
108
    }
109
    catch (std::runtime_error& e)
110
    {
111
        LOG.Error(e.what());
112
        throw std::runtime_error("No such command found, id = " + boost::lexical_cast<std::string>(command_id) + ", module = " + module_name);
113
    }
114
}
115
 
116
unsigned int Protocol::ResolveCommandId(std::string command_name, std::string module_name)
117
{
118
    try
119
    {
120
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).GetAttributeValue("id"));
121
    }
122
    catch (std::runtime_error& e)
123
    {
124
    }
125
 
126
    try
127
    {
128
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name).GetAttributeValue("id"));
129
    }
130
    catch (std::runtime_error& e)
131
    {
132
        LOG.Error(e.what());
133
        throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name);
134
    }
135
}
136
 
137
std::string Protocol::LookupNMTCommandName(unsigned int command_id)
138
{
139
    try
140
    {
141
        return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
142
    }
143
    catch (std::runtime_error& e)
144
    {
145
        LOG.Error(e.what());
146
        throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id));
147
    }
148
}
149
 
150
unsigned int Protocol::ResolveNMTCommandId(std::string command_name)
151
{
152
    try
153
    {
154
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id"));
155
    }
156
    catch (std::runtime_error& e)
157
    {
158
        LOG.Error(e.what());
159
        throw std::runtime_error("No such nmt command found, name = " + command_name);
160
    }
161
}
162
 
163
std::string Protocol::GetDefineId(std::string name, std::string group)
164
{
165
    try
166
    {
167
        return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id");
168
    }
169
    catch (std::runtime_error& e)
170
    {
171
        LOG.Error(e.what());
172
        throw std::runtime_error("No such define found, name = " + name + ", group = " + group);
173
    }
174
}
175
 
176
std::string Protocol::LookupDirectionFlag(unsigned int direction_flag)
177
{
178
    try
179
    {
180
        return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name");
181
    }
182
    catch (std::runtime_error& e)
183
    {
184
        LOG.Error(e.what());
185
        throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag));
186
    }
187
}
188
 
189
unsigned int Protocol::ResolveDirectionFlag(std::string direction_name)
190
{
191
    try
192
    {
193
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id"));
194
    }
195
    catch (std::runtime_error& e)
196
    {
197
        LOG.Error(e.what());
198
        throw std::runtime_error("No such direction flag found, direction_name = " + direction_name);
199
    }
200
}
201
 
202
std::string Protocol::LookupModuleName(unsigned int module_id)
203
{
1598 runge 204
    if (module_id == 0)
205
    {
206
        return "";
207
    }
208
 
1596 runge 209
    try
210
    {
211
        return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name");
212
    }
213
    catch (std::runtime_error& e)
214
    {
215
        LOG.Error(e.what());
216
        throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id));
217
    }
218
}
219
 
220
unsigned int Protocol::ResolveModuleId(std::string module_name)
221
{
1598 runge 222
    if (module_name == "")
223
    {
224
        return 0;
225
    }
226
 
1596 runge 227
    try
228
    {
229
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id"));
230
    }
231
    catch (std::runtime_error& e)
232
    {
233
        LOG.Error(e.what());
234
        throw std::runtime_error("No such module name found, " + module_name);
235
    }
236
}
237
 
238
xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name)
239
{
240
    try
241
    {
242
        return this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).FindChild("variables").GetChildren();
243
    }
244
    catch (std::runtime_error& e)
245
    {
246
    }
247
 
248
    try
249
    {
250
        return this->root_node_.FindChild("commands").SelectChild("name", command_name).FindChild("variables").GetChildren();
251
    }
252
    catch (std::runtime_error& e)
253
    {
254
        LOG.Error(e.what());
255
        throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name);
256
    }
257
}
258
 
259
xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name)
260
{
261
    try
262
    {
263
        return this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).FindChild("variables").GetChildren();
264
    }
265
    catch (std::runtime_error& e)
266
    {
267
        LOG.Error(e.what());
268
        throw std::runtime_error("No such nmt command found, name = " + command_name);
269
    }
270
}
271
 
1598 runge 272
std::string Protocol::DecodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
1596 runge 273
{
1598 runge 274
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
275
    int raw_value = 0;
1596 runge 276
 
1598 runge 277
    memcpy(&raw_value, &raw_bit_value, sizeof(raw_value));
278
 
279
    return boost::lexical_cast<std::string>(raw_value);
1596 runge 280
}
281
 
1598 runge 282
void Protocol::EncodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
1596 runge 283
{
1598 runge 284
    unsigned long raw_bit_value = 0;
285
    int raw_value = boost::lexical_cast<int>(value);
1596 runge 286
 
1598 runge 287
    memcpy(&raw_bit_value, &raw_value, sizeof(raw_value));
288
 
289
    bitset.Write(start_bit, bit_length, raw_bit_value);
1596 runge 290
}
291
 
1598 runge 292
std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
1596 runge 293
{
1598 runge 294
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
295
 
296
    return boost::lexical_cast<std::string>((unsigned int)raw_bit_value);
1596 runge 297
}
298
 
1598 runge 299
void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
1596 runge 300
{
1598 runge 301
    unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value);
1596 runge 302
 
1598 runge 303
    bitset.Write(start_bit, bit_length, raw_bit_value);
1596 runge 304
}
305
 
1598 runge 306
std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
1596 runge 307
{
1598 runge 308
    float raw_value = 0;
309
    bool negative = false;;
1596 runge 310
 
1598 runge 311
    if (bitset.Get(start_bit))
1596 runge 312
    {
1598 runge 313
        negative = true;
314
    }
315
 
1599 runge 316
    for (int c = bit_length - 1; c > 0; c--)
1598 runge 317
    {
318
        if (bitset.Get(start_bit + c) != negative)
1596 runge 319
        {
1599 runge 320
            raw_value += pow(2.0f, (int)(bit_length - 1 - c));
1596 runge 321
        }
322
    }
323
 
1598 runge 324
    raw_value /= 64.0f;
1596 runge 325
 
1598 runge 326
    if (negative)
1596 runge 327
    {
1598 runge 328
        raw_value = -raw_value;
1596 runge 329
    }
330
 
1598 runge 331
    return boost::lexical_cast<std::string>(raw_value);
1596 runge 332
}
333
 
1598 runge 334
void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
1596 runge 335
{
1599 runge 336
    // TODO:
337
    LOG.Error("EncodeFloat() is not implemented yet!");
1596 runge 338
}
339
 
1598 runge 340
std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
1596 runge 341
{
1598 runge 342
    std::string value;
343
    char c = 0;
1596 runge 344
 
1598 runge 345
    for (unsigned int p = 0; p < bit_length; p += 8)
1596 runge 346
    {
1598 runge 347
        unsigned long raw_bit_value = bitset.Read(start_bit + p, 8);
348
        memcpy(&c, &raw_bit_value, sizeof(c));
349
        value += c;
1596 runge 350
    }
351
 
1598 runge 352
    return value;
1596 runge 353
}
354
 
1598 runge 355
void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
1596 runge 356
{
1598 runge 357
    // TODO
1599 runge 358
    LOG.Error("EncodeAscii() is not implemented yet!");
1598 runge 359
}
360
 
361
std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
362
{
363
    std::string value;
364
    char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1596 runge 365
 
1598 runge 366
    for (unsigned int p = 0; p < bit_length; p += 4)
1596 runge 367
    {
1598 runge 368
        unsigned long raw_bit_value = bitset.Read(start_bit + p, 4);
1596 runge 369
 
1598 runge 370
        if (raw_bit_value >= sizeof(hex))
1596 runge 371
        {
1598 runge 372
            value += '-';
1596 runge 373
        }
1598 runge 374
        else
1596 runge 375
        {
1598 runge 376
            value += hex[raw_bit_value];
1596 runge 377
        }
378
    }
379
 
1598 runge 380
    return value;
1596 runge 381
}
1598 runge 382
 
383
void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
384
{
385
    // TODO:
1599 runge 386
    LOG.Error("EncodeHexstring() is not implemented yet!");
1598 runge 387
}
388
 
1596 runge 389
}; // namespace can
390
}; // namespace atom