Subversion Repositories HomeAutomation

Rev

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

Rev 1605 Rev 1607
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
 
27
 
28
namespace atom {
28
namespace atom {
29
namespace can {
29
namespace can {
30
 
30
 
31
Protocol::Pointer Protocol::instance_;
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
}
39
}
40
 
40
 
41
void Protocol::Create()
41
void Protocol::Create()
42
{
42
{
43
    Protocol::instance_ = Protocol::Pointer(new Protocol());
43
    Protocol::instance_ = Protocol::Pointer(new Protocol());
44
}
44
}
45
 
45
 
46
Protocol::Pointer Protocol::Instance()
46
Protocol::Pointer Protocol::Instance()
47
{
47
{
48
    return Protocol::instance_;
48
    return Protocol::instance_;
49
}
49
}
50
 
50
 
51
void Protocol::Delete()
51
void Protocol::Delete()
52
{
52
{
53
    Protocol::instance_.reset();
53
    Protocol::instance_.reset();
54
}
54
}
55
   
55
   
56
bool Protocol::Load(std::string filename)
56
bool Protocol::Load(std::string filename)
57
{
57
{
58
    try
58
    try
59
    {
59
    {
60
        this->root_node_.LoadFile(filename);
60
        this->root_node_.LoadFile(filename);
61
    }
61
    }
62
    catch (std::runtime_error& e)
62
    catch (std::runtime_error& e)
63
    {
63
    {
64
        LOG.Error(e.what());
64
        LOG.Error(e.what());
65
        return false;
65
        return false;
66
    }
66
    }
67
   
67
   
68
    LOG.Info("Protocol loaded successfully.");
68
    LOG.Info("Protocol loaded successfully.");
69
    return true;
69
    return true;
70
}
70
}
71
 
71
 
72
std::string Protocol::LookupClassName(unsigned int class_id)
72
std::string Protocol::LookupClassName(unsigned int class_id)
73
{
73
{
74
    try
74
    try
75
    {
75
    {
76
        return this->root_node_.FindChild("classes").SelectChild("id", boost::lexical_cast<std::string>(class_id)).GetAttributeValue("name");
76
        return this->root_node_.FindChild("classes").SelectChild("id", boost::lexical_cast<std::string>(class_id)).GetAttributeValue("name");
77
    }
77
    }
78
    catch (std::runtime_error& e)
78
    catch (std::runtime_error& e)
79
    {
79
    {
80
        LOG.Error(e.what());
80
        LOG.Error(e.what());
81
        throw std::runtime_error("No such class_id found, " + boost::lexical_cast<std::string>(class_id));
81
        throw std::runtime_error("No such class_id found, " + boost::lexical_cast<std::string>(class_id));
82
    }
82
    }
83
}
83
}
84
 
84
 
85
unsigned int Protocol::ResolveClassId(std::string class_name)
85
unsigned int Protocol::ResolveClassId(std::string class_name)
86
{
86
{
87
    try
87
    try
88
    {
88
    {
89
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("classes").SelectChild("name", class_name).GetAttributeValue("id"));
89
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("classes").SelectChild("name", class_name).GetAttributeValue("id"));
90
    }
90
    }
91
    catch (std::runtime_error& e)
91
    catch (std::runtime_error& e)
92
    {
92
    {
93
        LOG.Error(e.what());
93
        LOG.Error(e.what());
94
        throw std::runtime_error("No such class_name found, " + class_name);
94
        throw std::runtime_error("No such class_name found, " + class_name);
95
    }
95
    }
96
}
96
}
97
 
97
 
98
std::string Protocol::LookupCommandName(unsigned int command_id, std::string module_name)
98
std::string Protocol::LookupCommandName(unsigned int command_id, std::string module_name)
99
{
99
{
100
    try
100
    try
101
    {
101
    {
102
        if (command_id >= 128)
102
        if (command_id >= 128)
103
        {
103
        {
104
            return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id), "module", module_name).GetAttributeValue("name");
104
            return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id), "module", module_name).GetAttributeValue("name");
105
        }
105
        }
106
   
106
   
107
        return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
107
        return this->root_node_.FindChild("commands").SelectChild("id", boost::lexical_cast<std::string>(command_id)).GetAttributeValue("name");
108
    }
108
    }
109
    catch (std::runtime_error& e)
109
    catch (std::runtime_error& e)
110
    {
110
    {
111
        LOG.Error(e.what());
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);
112
        throw std::runtime_error("No such command found, id = " + boost::lexical_cast<std::string>(command_id) + ", module = " + module_name);
113
    }
113
    }
114
}
114
}
115
 
115
 
116
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)
117
{
117
{
118
    try
118
    try
119
    {
119
    {
120
        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"));
121
    }
121
    }
122
    catch (std::runtime_error& e)
122
    catch (std::runtime_error& e)
123
    {
123
    {
124
    }
124
    }
125
   
125
   
126
    try
126
    try
127
    {
127
    {
128
        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"));
129
    }
129
    }
130
    catch (std::runtime_error& e)
130
    catch (std::runtime_error& e)
131
    {
131
    {
132
        LOG.Error(e.what());
132
        LOG.Error(e.what());
133
        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);
134
    }
134
    }
135
}
135
}
136
 
136
 
137
std::string Protocol::LookupNMTCommandName(unsigned int command_id)
137
std::string Protocol::LookupNMTCommandName(unsigned int command_id)
138
{
138
{
139
    try
139
    try
140
    {
140
    {
141
        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");
142
    }
142
    }
143
    catch (std::runtime_error& e)
143
    catch (std::runtime_error& e)
144
    {
144
    {
145
        LOG.Error(e.what());
145
        LOG.Error(e.what());
146
        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));
147
    }
147
    }
148
}
148
}
149
 
149
 
150
unsigned int Protocol::ResolveNMTCommandId(std::string command_name)
150
unsigned int Protocol::ResolveNMTCommandId(std::string command_name)
151
{
151
{
152
    try
152
    try
153
    {
153
    {
154
        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"));
155
    }
155
    }
156
    catch (std::runtime_error& e)
156
    catch (std::runtime_error& e)
157
    {
157
    {
158
        LOG.Error(e.what());
158
        LOG.Error(e.what());
159
        throw std::runtime_error("No such nmt command found, name = " + command_name);
159
        throw std::runtime_error("No such nmt command found, name = " + command_name);
160
    }
160
    }
161
}
161
}
162
 
162
 
163
std::string Protocol::GetDefineId(std::string name, std::string group)
163
std::string Protocol::GetDefineId(std::string name, std::string group)
164
{
164
{
165
    try
165
    try
166
    {
166
    {
167
        return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id");
167
        return this->root_node_.FindChild("defines").SelectChild("name", name, "group", group).GetAttributeValue("id");
168
    }
168
    }
169
    catch (std::runtime_error& e)
169
    catch (std::runtime_error& e)
170
    {
170
    {
171
        LOG.Error(e.what());
171
        LOG.Error(e.what());
172
        throw std::runtime_error("No such define found, name = " + name + ", group = " + group);
172
        throw std::runtime_error("No such define found, name = " + name + ", group = " + group);
173
    }
173
    }
174
}
174
}
175
 
175
 
176
std::string Protocol::LookupDirectionFlag(unsigned int direction_flag)
176
std::string Protocol::LookupDirectionFlag(unsigned int direction_flag)
177
{
177
{
178
    try
178
    try
179
    {
179
    {
180
        return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name");
180
        return this->root_node_.FindChild("defines").SelectChild("id", boost::lexical_cast<std::string>(direction_flag), "group", "DirectionFlag").GetAttributeValue("name");
181
    }
181
    }
182
    catch (std::runtime_error& e)
182
    catch (std::runtime_error& e)
183
    {
183
    {
184
        LOG.Error(e.what());
184
        LOG.Error(e.what());
185
        throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag));
185
        throw std::runtime_error("No such direction flag found, direction_flag = " + boost::lexical_cast<std::string>(direction_flag));
186
    }
186
    }
187
}
187
}
188
 
188
 
189
unsigned int Protocol::ResolveDirectionFlag(std::string direction_name)
189
unsigned int Protocol::ResolveDirectionFlag(std::string direction_name)
190
{
190
{
191
    try
191
    try
192
    {
192
    {
193
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id"));
193
        return boost::lexical_cast<unsigned int>(this->root_node_.FindChild("defines").SelectChild("name", direction_name, "group", "DirectionFlag").GetAttributeValue("id"));
194
    }
194
    }
195
    catch (std::runtime_error& e)
195
    catch (std::runtime_error& e)
196
    {
196
    {
197
        LOG.Error(e.what());
197
        LOG.Error(e.what());
198
        throw std::runtime_error("No such direction flag found, direction_name = " + direction_name);
198
        throw std::runtime_error("No such direction flag found, direction_name = " + direction_name);
199
    }
199
    }
200
}
200
}
201
 
201
 
202
std::string Protocol::LookupModuleName(unsigned int module_id)
202
std::string Protocol::LookupModuleName(unsigned int module_id)
203
{
203
{
204
    if (module_id == 0)
204
    if (module_id == 0)
205
    {
205
    {
206
        return "";
206
        return "";
207
    }
207
    }
208
   
208
   
209
    try
209
    try
210
    {
210
    {
211
        return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name");
211
        return this->root_node_.FindChild("modules").SelectChild("id", boost::lexical_cast<std::string>(module_id)).GetAttributeValue("name");
212
    }
212
    }
213
    catch (std::runtime_error& e)
213
    catch (std::runtime_error& e)
214
    {
214
    {
215
        LOG.Error(e.what());
215
        LOG.Error(e.what());
216
        throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id));
216
        throw std::runtime_error("No such module id found, " + boost::lexical_cast<std::string>(module_id));
217
    }
217
    }
218
}
218
}
219
 
219
 
220
unsigned int Protocol::ResolveModuleId(std::string module_name)
220
unsigned int Protocol::ResolveModuleId(std::string module_name)
221
{
221
{
222
    if (module_name == "")
222
    if (module_name == "")
223
    {
223
    {
224
        return 0;
224
        return 0;
225
    }
225
    }
226
   
226
   
227
    try
227
    try
228
    {
228
    {
229
        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"));
230
    }
230
    }
231
    catch (std::runtime_error& e)
231
    catch (std::runtime_error& e)
232
    {
232
    {
233
        LOG.Error(e.what());
233
        LOG.Error(e.what());
234
        throw std::runtime_error("No such module name found, " + module_name);
234
        throw std::runtime_error("No such module name found, " + module_name);
235
    }
235
    }
236
}
236
}
237
 
237
 
238
xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name)
238
xml::Node::NodeList Protocol::GetCommandVariables(std::string command_name, std::string module_name)
239
{
239
{
240
    xml::Node node;
240
    xml::Node node;
241
   
241
   
242
    try
242
    try
243
    {
243
    {
244
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name);
244
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name, "module", module_name);
245
       
245
       
246
        try
246
        try
247
        {
247
        {
248
            return node.FindChild("variables").GetChildren();
248
            return node.FindChild("variables").GetChildren();
249
        }
249
        }
250
        catch (std::runtime_error& e)
250
        catch (std::runtime_error& e)
251
        {
251
        {
252
            return xml::Node::NodeList();
252
            return xml::Node::NodeList();
253
        }
253
        }
254
    }
254
    }
255
    catch (std::runtime_error& e)
255
    catch (std::runtime_error& e)
256
    {
256
    {
257
    }
257
    }
258
 
258
 
259
   
259
   
260
    try
260
    try
261
    {
261
    {
262
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name);
262
        node = this->root_node_.FindChild("commands").SelectChild("name", command_name);
263
       
263
       
264
        try
264
        try
265
        {
265
        {
266
            return node.FindChild("variables").GetChildren();
266
            return node.FindChild("variables").GetChildren();
267
        }
267
        }
268
        catch (std::runtime_error& e)
268
        catch (std::runtime_error& e)
269
        {
269
        {
270
            return xml::Node::NodeList();
270
            return xml::Node::NodeList();
271
        }
271
        }
272
    }
272
    }
273
    catch (std::runtime_error& e)
273
    catch (std::runtime_error& e)
274
    {
274
    {
275
        LOG.Error(e.what());
275
        LOG.Error(e.what());
276
        throw std::runtime_error("Could not find variables for command, name = " + command_name + ", module = " + module_name);
276
        throw std::runtime_error("Could not find variables for command, name = " + command_name + ", module = " + module_name);
277
    }
277
    }
278
   
278
   
279
    return xml::Node::NodeList();
279
    return xml::Node::NodeList();
280
}
280
}
281
 
281
 
282
xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name)
282
xml::Node::NodeList Protocol::GetNMTCommandVariables(std::string command_name)
283
{
283
{
284
    xml::Node node;
284
    xml::Node node;
285
   
285
   
286
    try
286
    try
287
    {
287
    {
288
        node = this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name);
288
        node = this->root_node_.FindChild("nmt_messages").SelectChild("name", command_name);
289
       
289
       
290
        try
290
        try
291
        {
291
        {
292
            return node.FindChild("variables").GetChildren();
292
            return node.FindChild("variables").GetChildren();
293
        }
293
        }
294
        catch (std::runtime_error& e)
294
        catch (std::runtime_error& e)
295
        {
295
        {
296
            return xml::Node::NodeList();
296
            return xml::Node::NodeList();
297
        }
297
        }
298
    }
298
    }
299
    catch (std::runtime_error& e)
299
    catch (std::runtime_error& e)
300
    {
300
    {
301
        LOG.Error(e.what());
301
        LOG.Error(e.what());
302
        throw std::runtime_error("Could not find variables for nmt command, name = " + command_name);
302
        throw std::runtime_error("Could not find variables for nmt command, name = " + command_name);
303
    }
303
    }
304
   
304
   
305
    return xml::Node::NodeList();
305
    return xml::Node::NodeList();
306
}
306
}
307
 
307
 
308
std::string Protocol::DecodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
308
std::string Protocol::DecodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
309
{
309
{
310
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
310
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
311
    int raw_value = 0;
311
    int raw_value = 0;
312
   
312
   
313
    memcpy(&raw_value, &raw_bit_value, sizeof(raw_value));
313
    memcpy(&raw_value, &raw_bit_value, sizeof(raw_value));
314
   
314
   
315
    return boost::lexical_cast<std::string>(raw_value);
315
    return boost::lexical_cast<std::string>(raw_value);
316
}
316
}
317
 
317
 
318
void Protocol::EncodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
318
void Protocol::EncodeInt(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
319
{
319
{
320
    unsigned long raw_bit_value = 0;
320
    unsigned long raw_bit_value = 0;
321
    int raw_value = boost::lexical_cast<int>(value);
321
    int raw_value = boost::lexical_cast<int>(value);
322
   
322
   
323
    memcpy(&raw_bit_value, &raw_value, sizeof(raw_value));
323
    memcpy(&raw_bit_value, &raw_value, sizeof(raw_value));
324
   
324
   
325
    bitset.Write(start_bit, bit_length, raw_bit_value);
325
    bitset.Write(start_bit, bit_length, raw_bit_value);
326
}
326
}
327
 
327
 
328
std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
328
std::string Protocol::DecodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
329
{
329
{
330
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
330
    unsigned long raw_bit_value = bitset.Read(start_bit, bit_length);
-
 
331
   
-
 
332
    //LOG.Debug("DecodeUint: raw_bit_value=" + boost::lexical_cast<std::string>(raw_bit_value));
331
   
333
   
332
    return boost::lexical_cast<std::string>((unsigned int)raw_bit_value);
334
    return boost::lexical_cast<std::string>((unsigned int)raw_bit_value);
333
}
335
}
334
 
336
 
335
void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
337
void Protocol::EncodeUint(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
336
{
338
{
337
    unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value);
339
    unsigned long raw_bit_value = boost::lexical_cast<unsigned int>(value);
338
   
340
   
339
    bitset.Write(start_bit, bit_length, raw_bit_value);
341
    bitset.Write(start_bit, bit_length, raw_bit_value);
340
}
342
}
341
 
343
 
342
std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
344
std::string Protocol::DecodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
343
{
345
{
344
    float float_value = boost::lexical_cast<float>(this->DecodeInt(bitset, start_bit, bit_length)) / 64.0f;
346
    float float_value = boost::lexical_cast<float>(this->DecodeInt(bitset, start_bit, bit_length)) / 64.0f;
345
   
347
   
346
    return boost::lexical_cast<std::string>(float_value);
348
    return boost::lexical_cast<std::string>(float_value);
347
}
349
}
348
 
350
 
349
void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
351
void Protocol::EncodeFloat(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
350
{
352
{
351
    int int_value = boost::lexical_cast<float>(value) * 64.0f;
353
    int int_value = boost::lexical_cast<float>(value) * 64.0f;
352
   
354
   
353
    this->EncodeInt(bitset, start_bit, bit_length, boost::lexical_cast<std::string>(int_value));
355
    this->EncodeInt(bitset, start_bit, bit_length, boost::lexical_cast<std::string>(int_value));
354
}
356
}
355
 
357
 
356
std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
358
std::string Protocol::DecodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
357
{
359
{
358
    std::string value;
360
    std::string value;
359
   
361
   
360
    for (unsigned int n = 0; n < bit_length; n += 8)
362
    for (unsigned int n = 0; n < bit_length; n += 8)
361
    {
363
    {
362
        unsigned long raw_value = bitset.Read(start_bit + n, 8);
364
        unsigned long raw_value = bitset.Read(start_bit + n, 8);
363
        value += (char)raw_value;
365
        value += (char)raw_value;
364
    }
366
    }
365
   
367
   
366
    return value;
368
    return value;
367
}
369
}
368
 
370
 
369
void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
371
void Protocol::EncodeAscii(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
370
{
372
{
-
 
373
    //LOG.Debug("EncodeAscii: " + value + ", length =" + boost::lexical_cast<std::string>(value.size()) + ", bit_length=" + boost::lexical_cast<std::string>(bit_length));
-
 
374
   
371
    for (unsigned int n = 0; n < bit_length; n += 8)
375
    for (unsigned int n = 0; n < bit_length; n += 8)
372
    {
376
    {
-
 
377
        if (n / 8 > value.size())
-
 
378
        {
-
 
379
            break;
-
 
380
        }
-
 
381
       
373
        bitset.Write(start_bit + n, 8, value[n / 8]);
382
        bitset.Write(start_bit + n, 8, value[n / 8]);
374
    }
383
    }
375
}
384
}
376
 
385
 
377
std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
386
std::string Protocol::DecodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length)
378
{
387
{
379
    std::string value;
388
    std::string value;
380
    char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
389
    char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
381
   
390
   
382
    for (unsigned int n = 0; n < bit_length; n += 4)
391
    for (unsigned int n = 0; n < bit_length; n += 4)
383
    {
392
    {
384
        unsigned long raw_bit_value = bitset.Read(start_bit + n, 4);
393
        unsigned long raw_bit_value = bitset.Read(start_bit + n, 4);
385
       
394
       
386
        if (raw_bit_value >= sizeof(hex))
395
        if (raw_bit_value >= sizeof(hex))
387
        {
396
        {
388
            value += '-';
397
            value += '-';
389
        }
398
        }
390
        else
399
        else
391
        {
400
        {
392
            value += hex[raw_bit_value];
401
            value += hex[raw_bit_value];
393
        }
402
        }
394
    }
403
    }
395
   
404
   
396
    return value;
405
    return value;
397
}
406
}
398
 
407
 
399
void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
408
void Protocol::EncodeHexstring(type::Bitset& bitset, unsigned int start_bit, unsigned int bit_length, std::string value)
400
{
409
{
401
    int ascii_value;
410
    int ascii_value;
402
   
411
   
403
    for (unsigned int n = 0; n < bit_length; n += 4)
412
    for (unsigned int n = 0; n < bit_length; n += 4)
404
    {
413
    {
405
        ascii_value = boost::lexical_cast<int>((int)std::toupper(value[n / 4]));
414
        ascii_value = boost::lexical_cast<int>((int)std::toupper(value[n / 4]));
406
       
415
       
407
        if (48 <= ascii_value && ascii_value <= 57)
416
        if (48 <= ascii_value && ascii_value <= 57)
408
        {
417
        {
409
            bitset.Write(start_bit + n, 4, ascii_value - 48);
418
            bitset.Write(start_bit + n, 4, ascii_value - 48);
410
        }
419
        }
411
        else if (65 <= ascii_value && ascii_value <= 70)
420
        else if (65 <= ascii_value && ascii_value <= 70)
412
        {
421
        {
413
            bitset.Write(start_bit + n, 4, ascii_value - 65);
422
            bitset.Write(start_bit + n, 4, ascii_value - 65);
414
        }
423
        }
415
        else
424
        else
416
        {
425
        {
417
            throw std::runtime_error("This is not an hex string, " + value);
426
            throw std::runtime_error("This is not an hex string, " + value);
418
        }
427
        }
419
    }
428
    }
420
}
429
}
421
 
430
 
422
}; // namespace can
431
}; // namespace can
423
}; // namespace atom
432
}; // namespace atom
424
 
433