Subversion Repositories HomeAutomation

Rev

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

Rev 1596 Rev 1597
Line 31... Line 31...
31
 
31
 
32
#include "Protocol.h"
32
#include "Protocol.h"
33
#include "Message.h"
33
#include "Message.h"
34
 
34
 
35
#include "type/Bitset.h"
35
#include "type/Bitset.h"
-
 
36
#include "type/common.h"
36
 
37
 
37
namespace atom {
38
namespace atom {
38
namespace can {
39
namespace can {
39
 
40
 
40
enum
41
enum
Line 267... Line 268...
267
        for (unsigned int n = 0; n < length; n++)
268
        for (unsigned int n = 0; n < length; n++)
268
        {
269
        {
269
            //LOG.Debug("add byte[" + boost::lexical_cast<std::string>(n + 7) + "] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[n + 7]));
270
            //LOG.Debug("add byte[" + boost::lexical_cast<std::string>(n + 7) + "] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[n + 7]));
270
            data_set.Append(this->buffer_[n + 7]);
271
            data_set.Append(this->buffer_[n + 7]);
271
        }
272
        }
272
       
273
        /*
273
        /*for (unsigned int n = 0; n < this->buffer_.GetSize(); n++)
274
        for (unsigned int n = 0; n < data_set.GetSize(); n++)
274
        {
275
        {
275
            LOG.Debug("byte[" + boost::lexical_cast<std::string>(n) + "] = " + boost::lexical_cast<std::string>((unsigned int)this->buffer_[n]));
276
            LOG.Debug("byte[" + boost::lexical_cast<std::string>(n) + "] = " + boost::lexical_cast<std::string>((unsigned int)data_set[n]));
276
        }*/
277
        }
277
       
278
        */
278
        type::Bitset databits(data_set);
279
        type::Bitset databits(data_set);
279
       
280
       
280
       
281
        /*
281
        std::string temp = "";
282
        std::string temp = "";
-
 
283
        std::string temp2 = "";
282
        for (unsigned int n = 0; n < databits.GetCount(); n ++)
284
        for (unsigned int n = 0; n < databits.GetCount(); n ++)
283
        {
285
        {
284
            temp += boost::lexical_cast<std::string>(databits.Get(n));
-
 
285
        }
-
 
286
       
286
           
-
 
287
            temp2 += boost::lexical_cast<std::string>((unsigned int)databits.bytes_[n/8]);
-
 
288
            temp += boost::lexical_cast<std::string>(databits.Get(n));
-
 
289
        }
-
 
290
       
287
        //LOG.Debug("databits = " + temp);
291
        LOG.Debug("databits1 = " + temp);
-
 
292
        LOG.Debug("databits2 = " + temp2);*/
288
       
293
       
289
        xml::Node::NodeList variable_nodes;
294
        xml::Node::NodeList variable_nodes;
290
       
295
       
291
        if (class_name == "nmt")
296
        if (class_name == "nmt")
292
        {
297
        {
293
            variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name);
298
            variable_nodes = Protocol::Instance()->GetNMTCommandVariables(command_name);
294
        }
299
        }
295
        else
300
        else
296
        {
301
        {
297
            variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name);
302
            variable_nodes = Protocol::Instance()->GetCommandVariables(command_name, module_name);
298
        }
303
        }
299
       
304
       
300
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
305
        for (unsigned int n = 0; n < variable_nodes.size(); n++)
301
        {
306
        {
302
            unsigned int start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
307
            unsigned int start_bit = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("start_bit"));
303
            unsigned int bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
308
            unsigned int bit_length = boost::lexical_cast<unsigned int>(variable_nodes[n].GetAttributeValue("bit_length"));
304
            std::string type = variable_nodes[n].GetAttributeValue("type");
309
            std::string type = variable_nodes[n].GetAttributeValue("type");
305
           
310
           
306
            std::string value = boost::lexical_cast<std::string>(databits.Read(start_bit, bit_length));
311
            //LOG.Debug("name=" + variable_nodes[n].GetAttributeValue("name"));
-
 
312
            //LOG.Debug("type=" + type);
307
 
313
           
-
 
314
            std::string value = "";
-
 
315
           
308
            if (type == "enum")
316
            if (type == "int")
-
 
317
            {
-
 
318
                unsigned long raw_bit_value = databits.Read(start_bit, bit_length);
-
 
319
                int raw_value = 0;
-
 
320
               
-
 
321
                memcpy(&raw_value, &raw_bit_value, sizeof(raw_value));
-
 
322
                value = boost::lexical_cast<std::string>(raw_value);
-
 
323
            }
-
 
324
            else if (type == "float")
-
 
325
            {
-
 
326
                float raw_value = 0;
-
 
327
                bool negative = false;;
-
 
328
               
-
 
329
                if (databits.Get(start_bit))
-
 
330
                {
-
 
331
                    negative = true;
-
 
332
                }
-
 
333
               
-
 
334
                for (int c = bit_length-1; c > 0; c--)
-
 
335
                {
-
 
336
                    if (databits.Get(start_bit + c) != negative)
-
 
337
                    {
-
 
338
                        raw_value += pow(2.0f, (int)(bit_length-1-c));
-
 
339
                    }
-
 
340
                }
-
 
341
               
-
 
342
                raw_value /= 64.0f;
-
 
343
               
-
 
344
                if (negative)
-
 
345
                {
-
 
346
                    raw_value = -raw_value;
-
 
347
                }
-
 
348
               
-
 
349
                value = boost::lexical_cast<std::string>(raw_value);
-
 
350
                //LOG.Debug("float:value="+value);
-
 
351
            }
-
 
352
            else if (type == "ascii")
-
 
353
            {
-
 
354
                char c = 0;
-
 
355
               
-
 
356
                for (unsigned int p = 0; p < bit_length; p += 8)
-
 
357
                {
-
 
358
                    unsigned long raw_bit_value = databits.Read(start_bit + p, 8);
-
 
359
                    memcpy(&c, &raw_bit_value, sizeof(c));
-
 
360
                    value += c;
-
 
361
                }
-
 
362
            }
-
 
363
            else if (type == "hexstring")
-
 
364
            {
-
 
365
                for (unsigned int p = 0; p < bit_length; p += 4)
-
 
366
                {
-
 
367
                    unsigned long raw_bit_value = databits.Read(start_bit + p, 4);
-
 
368
                   
-
 
369
                    //value += boost::lexical_cast<std::string>(raw_bit_value);
-
 
370
                }
-
 
371
            }
-
 
372
            else if (type == "enum")
309
            {
373
            {
-
 
374
                value = boost::lexical_cast<std::string>(databits.Read(start_bit, bit_length));
310
                value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name");
375
                value = variable_nodes[n].SelectChild("id", value).GetAttributeValue("name");
311
            }
376
            }
-
 
377
            else// if (type == "uint")
312
           
378
            {
-
 
379
                unsigned long raw_bit_value = databits.Read(start_bit, bit_length);
313
            // TODO handle all datatypes!
380
                unsigned int raw_value = 0;
-
 
381
               
-
 
382
                memcpy(&raw_value, &raw_bit_value, sizeof(raw_value));
-
 
383
                value = boost::lexical_cast<std::string>(raw_value);
-
 
384
            }
314
           
385
           
315
            //LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ",bit_length=" + boost::lexical_cast<std::string>(bit_length));
386
            //LOG.Debug("start_bit=" + boost::lexical_cast<std::string>(start_bit) + ",bit_length=" + boost::lexical_cast<std::string>(bit_length));
316
           
387
           
317
            payload->SetVariable(variable_nodes[n].GetAttributeValue("name"), value);
388
            payload->SetVariable(variable_nodes[n].GetAttributeValue("name"), value);
318
            //LOG.Debug("Variable:" + variable_nodes[n].GetAttributeValue("name") + " = " + value);
389
            //LOG.Debug("Variable:" + variable_nodes[n].GetAttributeValue("name") + " = " + value);
319
        }
390
        }
320
       
391
       
321
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
392
        broker::Manager::Instance()->Post(broker::Message::Pointer(new broker::Message(broker::Message::CAN_MESSAGE, broker::Message::PayloadPointer(payload), this)));
322
    }
393
    }
323
    catch (std::runtime_error& e)
394
    catch (std::runtime_error& e)
324
    {
395
    {
-
 
396
        std::string bytestring;
-
 
397
       
-
 
398
        for (unsigned int n = 0; n < this->buffer_.GetSize(); n++)
-
 
399
        {
-
 
400
            bytestring += boost::lexical_cast<std::string>((unsigned int)this->buffer_[n]) + ",";
-
 
401
        }
-
 
402
       
-
 
403
        LOG.Debug("Bytes: " + bytestring);
-
 
404
       
325
        LOG.Error("Malformed message received, " + std::string(e.what()));
405
        LOG.Error("Malformed message received, " + std::string(e.what()));
326
    }
406
    }
327
   
407
   
328
    this->buffer_.Clear();
408
    this->buffer_.Clear();
329
}
409
}