Subversion Repositories HomeAutomation

Rev

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

Rev 2066 Rev 2118
Line 20... Line 20...
20
 
20
 
21
#include "Protocol.h"
21
#include "Protocol.h"
22
 
22
 
23
#include <stdexcept>
23
#include <stdexcept>
24
#include <string.h>
24
#include <string.h>
25
 
-
 
-
 
25
#include <stdint.h>
26
#include <boost/lexical_cast.hpp>
26
#include <boost/lexical_cast.hpp>
27
 
27
 
28
#include "common/log.h"
28
#include "common/log.h"
29
 
29
 
30
namespace atom {
30
namespace atom {
31
namespace can {
31
namespace can {
32
 
32
 
33
Protocol::Pointer Protocol::instance_;
33
Protocol::Pointer Protocol::instance_;
34
 
34
 
35
static const std::string log_module_ = "can::protocol";
35
static const std::string log_module_ = "can::protocol";
36
 
36
 
37
Protocol::Protocol() : LOG("can::Protocol")
37
Protocol::Protocol() : LOG("can::Protocol")
38
{
38
{
39
}
39
}
40
 
40
 
41
Protocol::~Protocol()
41
Protocol::~Protocol()
42
{
42
{
43
}
43
}
44
 
44
 
45
void Protocol::Create()
45
void Protocol::Create()
46
{
46
{
47
  Protocol::instance_ = Protocol::Pointer(new Protocol());
47
  Protocol::instance_ = Protocol::Pointer(new Protocol());
48
}
48
}
49
 
49
 
50
Protocol::Pointer Protocol::Instance()
50
Protocol::Pointer Protocol::Instance()
51
{
51
{
52
  return Protocol::instance_;
52
  return Protocol::instance_;
53
}
53
}
54
 
54
 
55
void Protocol::Delete()
55
void Protocol::Delete()
56
{
56
{
57
  Protocol::instance_.reset();
57
  Protocol::instance_.reset();
58
}
58
}
59
 
59
 
60
bool Protocol::Load(std::string file_path)
60
bool Protocol::Load(std::string file_path)
61
{
61
{
62
  try
62
  try
63
  {
63
  {
64
    this->root_node_.LoadFile(file_path);
64
    this->root_node_.LoadFile(file_path);
65
  }
65
  }
66
  catch (std::runtime_error& e)
66
  catch (std::runtime_error& e)
67
  {
67
  {
68
    LOG.Error(e.what());
68
    LOG.Error(e.what());
69
    return false;
69
    return false;
70
  }
70
  }
71
 
71
 
72
  LOG.Info("Protocol loaded successfully.");
72
  LOG.Info("Protocol loaded successfully.");
Line 141... Line 141...
141
std::string Protocol::LookupNMTCommandName(unsigned int command_id)
141
std::string Protocol::LookupNMTCommandName(unsigned int command_id)
142
{
142
{
143
    try
143
    try
144
    {
144
    {
145
        return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
145
        return this->root_node_.FindChild("nmt_messages").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
146
    }
146
    }
147
    catch (std::runtime_error& e)
147
    catch (std::runtime_error& e)
148
    {
148
    {
149
        LOG.Error(e.what());
149
        LOG.Error(e.what());
150
        throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id));
150
        throw std::runtime_error("No such nmt command found, id = " + boost::lexical_cast<std::string>(command_id));
151
    }
151
    }
152
}
152
}
153
 
153
 
154
unsigned int Protocol::ResolveNMTCommandId(std::string command_name)
154
unsigned int Protocol::ResolveNMTCommandId(std::string command_name)
155
{
155
{
156
    try
156
    try
157
    {
157
    {
158
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id"));
158
        return boost::lexical_cast<unsigned int >(this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name).GetAttributeValue("id"));
159
    }
159
    }
-
 
160
    catch (std::runtime_error& e)
-
 
161
    {
-
 
162
        LOG.Error(e.what());
-
 
163
        throw std::runtime_error("No such nmt command found, name = " + command_name);
-
 
164
    }
-
 
165
}
-
 
166
 
-
 
167
std::string Protocol::GetDefineId(std::string name, std::string group)
-
 
168
{
-
 
169
    try
-
 
170
    {
-
 
171
        return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id");
-
 
172
    }
160
    catch (std::runtime_error& e)
173
    catch (std::runtime_error& e)
161
    {
174
    {
162
        LOG.Error(e.what());
-
 
163
        throw std::runtime_error("No such nmt command found, name = " + command_name);
-
 
164
    }
-
 
165
}
-
 
166
 
-
 
167
std::string Protocol::GetDefineId(std::string name, std::string group)
-
 
168
{
-
 
169
    try
-
 
170
    {
-
 
171
        return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id");
-
 
172
    }
-
 
173
    catch (std::runtime_error& e)
-
 
174
    {
-
 
175
        LOG.Error(e.what());
175
        LOG.Error(e.what());
176
        throw std::runtime_error("No such define found, name = " + name + ", group = " + group);
176
        throw std::runtime_error("No such define found, name = " + name + ", group = " + group);
177
    }
177
    }
178
}
178
}
179
 
179
 
180
std::string Protocol::LookupDirectionFlag(unsigned int direction_flag)
180
std::string Protocol::LookupDirectionFlag(unsigned int direction_flag)
181
{
181
{
182
    try
182
    try
183
    {
183
    {
184
        return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name");
184
        return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name");
185
    }
185
    }
186
    catch (std::runtime_error& e)
186
    catch (std::runtime_error& e)
187
    {
187
    {
188
        LOG.Error(e.what());
188
        LOG.Error(e.what());
189
        throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag));
189
        throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag));
190
    }
190
    }
191
}
191
}
192
 
192
 
193
unsigned int Protocol::ResolveDirectionFlag(std::string direction_name)
193
unsigned int Protocol::ResolveDirectionFlag(std::string direction_name)
194
{
194
{
195
    try
195
    try
196
    {
196
    {
197
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id"));
197
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id"));
198
    }
198
    }
199
    catch (std::runtime_error& e)
199
    catch (std::runtime_error& e)
200
    {
200
    {
201
        LOG.Error(e.what());
201
        LOG.Error(e.what());
202
        throw std::runtime_error("No such direction flag found, direction_name = " + direction_name);
202
        throw std::runtime_error("No such direction flag found, direction_name = " + direction_name);
203
    }
203
    }
204
}
204
}
205
 
205
 
206
std::string Protocol::LookupModuleName(unsigned int module_id)
206
std::string Protocol::LookupModuleName(unsigned int module_id)
207
{
207
{
208
    try
208
    try
209
    {
209
    {
210
        return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name");
210
        return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name");
211
    }
211
    }
212
    catch (std::runtime_error& e)
212
    catch (std::runtime_error& e)
213
    {
213
    {
214
        LOG.Error(e.what());
214
        LOG.Error(e.what());
Line 219... Line 219...
219
unsigned int Protocol::ResolveModuleId(std::string module_name)
219
unsigned int Protocol::ResolveModuleId(std::string module_name)
220
{
220
{
221
    if (module_name == "")
221
    if (module_name == "")
222
    {
222
    {
223
        return 0;
223
        return 0;
224
    }
224
    }
225
 
225
 
226
    try
226
    try
227
    {
227
    {
228
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id"));
228
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("modules").SelectChild("name", module_name).GetAttributeValue("id"));
229
    }
229
    }
230
    catch (std::runtime_error& e)
230
    catch (std::runtime_error& e)
231
    {
231
    {
232
        LOG.Error(e.what());
232
        LOG.Error(e.what());
233
        throw std::runtime_error("No such module name found, " + module_name);
233
        throw std::runtime_error("No such module name found, " + module_name);
234
    }
234
    }
235
}
235
}
236
 
236
 
237
xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name)
237
xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name)
238
{
238
{
239
    xml::Node node;
239
    xml::Node node;
240
 
240
 
241
    try
241
    try
242
    {
242
    {
243
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name);
243
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name);
244
 
244
 
245
        try
245
        try
246
        {
246
        {
247
            return node.FindChild("variables").GetChildren();
247
            return node.FindChild("variables").GetChildren();
248
        }
248
        }
249
        catch (std::runtime_error& e)
249
        catch (std::runtime_error& e)
250
        {
250
        {
251
            return xml::Node::NodeList();
251
            return xml::Node::NodeList();
252
        }
252
        }
253
    }
253
    }
254
    catch (std::runtime_error& e)
254
    catch (std::runtime_error& e)
255
    {
255
    {
256
    }
256
    }
257
 
257
 
258
 
258
 
259
    try
259
    try
260
    {
260
    {
261
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name);
261
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name);
262
 
262
 
263
        try
263
        try
264
        {
264
        {
265
            return node.FindChild("variables").GetChildren();
265
            return node.FindChild("variables").GetChildren();
266
        }
266
        }
267
        catch (std::runtime_error& e)
267
        catch (std::runtime_error& e)
268
        {
268
        {
269
            return xml::Node::NodeList();
269
            return xml::Node::NodeList();
270
        }
270
        }
271
    }
271
    }
272
    catch (std::runtime_error& e)
272
    catch (std::runtime_error& e)
273
    {
273
    {
274
        LOG.Error(e.what());
274
        LOG.Error(e.what());
275
        throw std::runtime_error("Could not find variables for command, name = " + command_name + ", module = " + module_name);
275
        throw std::runtime_error("Could not find variables for command, name = " + command_name + ", module = " + module_name);
276
    }
276
    }
277
 
277
 
Line 350... Line 350...
350
    bitset.Write(start_bit, bit_length, raw_bit_value);
350
    bitset.Write(start_bit, bit_length, raw_bit_value);
351
}
351
}
352
 
352
 
353
std::string Protocol::DecodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
353
std::string Protocol::DecodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
354
{
354
{
355
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
355
    uint64_t raw_bit_value = bitset.Read(start_bit, bit_length);
356
 
356
 
357
    //LOG.Debug("DecodeUint: raw_bit_value=" + boost::lexical_cast<std::string>(raw_bit_value));
357
    //LOG.Debug("DecodeUint: raw_bit_value=" + boost::lexical_cast<std::string>(raw_bit_value));
358
 
358
 
359
    return boost::lexical_cast<std::string>((unsigned long)raw_bit_value);
359
    return boost::lexical_cast<std::string>((uint64_t)raw_bit_value);
360
}
360
}
361
 
361
 
362
void Protocol::EncodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
362
void Protocol::EncodeUint(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
363
{
363
{
364
    unsigned long raw_bit_value = boost::lexical_cast<unsigned long>(value);
364
    uint64_t raw_bit_value = boost::lexical_cast<uint64_t>(value);
365
 
365
 
366
    bitset.Write(start_bit, bit_length, raw_bit_value);
366
    bitset.Write(start_bit, bit_length, raw_bit_value);
367
}
367
}
368
 
368
 
369
std::string Protocol::DecodeFloat(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
369
std::string Protocol::DecodeFloat(common::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)