Subversion Repositories HomeAutomation

Rev

Rev 1598 | Rev 1600 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1598 Rev 1599
Line 26... Line 26...
26
#include <boost/lexical_cast.hpp>
26
#include <boost/lexical_cast.hpp>
27
 
27
 
28
namespace atom {
28
namespace atom {
29
namespace can {
29
namespace can {
30
 
30
 
31
Protocol::Pointer Protocol::instance_ = Protocol::Pointer(new Protocol());
31
Protocol::Pointer Protocol::instance_;
32
 
32
 
33
Protocol::Protocol() : LOG("can::Protocol")
33
Protocol::Protocol() : LOG("can::Protocol")
34
{
34
{
35
}
35
}
36
 
36
 
37
Protocol::~Protocol()
37
Protocol::~Protocol()
38
{
38
{
-
 
39
}
-
 
40
 
-
 
41
void Protocol::Create()
-
 
42
{
-
 
43
    Protocol::instance_ = Protocol::Pointer(new Protocol());
39
}
44
}
40
 
45
 
41
Protocol::Pointer Protocol::Instance()
46
Protocol::Pointer Protocol::Instance()
42
{
47
{
43
    return Protocol::instance_;
48
    return Protocol::instance_;
Line 107... Line 112...
107
        throw std::runtime_error("No such command found, id = " + boost::lexical_cast<std::string>(command_id) + ", module = " + module_name);
112
        throw std::runtime_error("No such command found, id = " + boost::lexical_cast<std::string>(command_id) + ", module = " + module_name);
108
    }
113
    }
109
}
114
}
110
 
115
 
111
unsigned int Protocol::ResolveCommandId(std::string command_name, std::string module_name)
116
unsigned int Protocol::ResolveCommandId(std::string command_name, std::string module_name)
112
{
117
{
113
    try
118
    try
114
    {
119
    {
115
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).GetAttributeValue("id"));
120
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name).GetAttributeValue("id"));
116
    }
121
    }
117
    catch (std::runtime_error& e)
122
    catch (std::runtime_error& e)
118
    {
123
    {
119
    }
124
    }
120
   
125
   
121
    try
126
    try
122
    {
127
    {
123
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name).GetAttributeValue("id"));
128
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("commands").SelectChild("name", command_name).GetAttributeValue("id"));
124
    }
129
    }
125
    catch (std::runtime_error& e)
130
    catch (std::runtime_error& e)
126
    {
131
    {
127
        LOG.Error(e.what());
132
        LOG.Error(e.what());
128
        throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name);
133
        throw std::runtime_error("No such command found, name = " + command_name + ", module = " + module_name);
129
    }
134
    }
130
}
135
}
131
 
136
 
132
std::string Protocol::LookupNMTCommandName(unsigned int command_id)
137
std::string Protocol::LookupNMTCommandName(unsigned int command_id)
133
{
138
{
134
    try
139
    try
135
    {
140
    {
136
        return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
141
        return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
137
    }
142
    }
138
    catch (std::runtime_error& e)
143
    catch (std::runtime_error& e)
139
    {
144
    {
140
        LOG.Error(e.what());
145
        LOG.Error(e.what());
141
        throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id));
146
        throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id));
142
    }
147
    }
143
}
148
}
144
 
149
 
145
unsigned int Protocol::ResolveNMTCommandId(std::string command_name)
150
unsigned int Protocol::ResolveNMTCommandId(std::string command_name)
146
{
151
{
147
    try
152
    try
148
    {
153
    {
149
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id"));
154
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id"));
150
    }
155
    }
151
    catch (std::runtime_error& e)
156
    catch (std::runtime_error& e)
152
    {
157
    {
153
        LOG.Error(e.what());
158
        LOG.Error(e.what());
Line 215... Line 220...
215
unsigned int Protocol::ResolveModuleId(std::string module_name)
220
unsigned int Protocol::ResolveModuleId(std::string module_name)
216
{
221
{
217
    if (module_name == "")
222
    if (module_name == "")
218
    {
223
    {
219
        return 0;
224
        return 0;
220
    }
225
    }
221
   
226
   
222
    try
227
    try
223
    {
228
    {
224
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id"));
229
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id"));
225
    }
230
    }
Line 287... Line 292...
287
std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
292
std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
288
{
293
{
289
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
294
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
290
   
295
   
291
    return boost::lexical_cast<std::string>((unsigned int)raw_bit_value);
296
    return boost::lexical_cast<std::string>((unsigned int)raw_bit_value);
292
}
297
}
293
 
298
 
294
void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
299
void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
295
{
300
{
296
    unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value);
301
    unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value);
297
   
302
   
298
    bitset.Write(start_bit, bit_length, raw_bit_value);
303
    bitset.Write(start_bit, bit_length, raw_bit_value);
299
}
304
}
300
 
305
 
301
std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
306
std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
302
{
307
{
Line 304... Line 309...
304
    bool negative = false;;
309
    bool negative = false;;
305
   
310
   
306
    if (bitset.Get(start_bit))
311
    if (bitset.Get(start_bit))
307
    {
312
    {
308
        negative = true;
313
        negative = true;
309
    }
314
    }
310
   
315
   
311
    for (int c = bit_length-1; c > 0; c--)
316
    for (int c = bit_length - 1; c > 0; c--)
312
    {
317
    {
313
        if (bitset.Get(start_bit + c) != negative)
318
        if (bitset.Get(start_bit + c) != negative)
314
        {
319
        {
315
            raw_value += pow(2.0f, (int)(bit_length-1-c));
320
            raw_value += pow(2.0f, (int)(bit_length - 1 - c));
316
        }
321
        }
317
    }
322
    }
318
   
323
   
319
    raw_value /= 64.0f;
324
    raw_value /= 64.0f;
320
   
325
   
321
    if (negative)
326
    if (negative)
322
    {
327
    {
Line 326... Line 331...
326
    return boost::lexical_cast<std::string>(raw_value);
331
    return boost::lexical_cast<std::string>(raw_value);
327
}
332
}
328
 
333
 
329
void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
334
void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
330
{
335
{
331
 // TODO:
336
    // TODO:
-
 
337
    LOG.Error("EncodeFloat() is not implemented yet!");
332
}
338
}
333
 
339
 
334
std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
340
std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
335
{
341
{
336
    std::string value;
342
    std::string value;
Line 347... Line 353...
347
}
353
}
348
 
354
 
349
void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
355
void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
350
{
356
{
351
    // TODO
357
    // TODO
-
 
358
    LOG.Error("EncodeAscii() is not implemented yet!");
352
}
359
}
353
 
360
 
354
std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
361
std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
355
{
362
{
356
    std::string value;
363
    std::string value;
Line 374... Line 381...
374
}
381
}
375
 
382
 
376
void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
383
void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
377
{
384
{
378
    // TODO:
385
    // TODO:
-
 
386
    LOG.Error("EncodeHexstring() is not implemented yet!");
379
}
387
}
380
 
388
 
381
}; // namespace can
389
}; // namespace can
382
}; // namespace atom
390
}; // namespace atom