Subversion Repositories HomeAutomation

Rev

Rev 1766 | Rev 2066 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

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