-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_handling.h
1670 lines (1515 loc) · 52.3 KB
/
packet_handling.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
@file: packet_handling.h
@date: 28 Aug 2012
@author: Lukasz Forynski
@brief This is a small library to facilitate extracting
(parsing) and accessing the data formed in packets of
a fixed format. It was mainly meant in order to provide
an easy way for creating and using such packets.
In some way it is similar to google protocol buffers
(https://developers.google.com/protocol-buffers/),
but:
- the format / layout of buffer(s) can be created dynamically
- the actual data in the buffer does not contain any additional
information - this information is only held by the Packet object
- Packet object is really like a 'template', or a parser that facilitates
accessing various fields of the actual data.
- it can help with implementing various protocols
Example:
Example packet (Initialise Data Source-Packet I.D. 128
for the GPS protocol) (message sent over serial port):
Name Bytes Units
0 Packet ID 1
1 ECEF X 4 Meters
2 ECEF Y 4 Meters
3 ECEF Z 4 Meters
4 Clock Offset 4 Hz
5 Time of Week 4 Seconds
6 Week Number 2
7 Channels 1
8 Reset Config 1
Could be defined as follows:
@code
uint8_t* buffer = new uint8_t[25];
Packet gps_id_128(buffer, 25);
gps_id_128.add_field<uint8_t> ("Packet ID");
gps_id_128.add_field<uint32_t>("ECEF X");
gps_id_128.add_field<uint32_t>("ECEF Y");
gps_id_128.add_field<uint32_t>("ECEF Z");
gps_id_128.add_field<uint32_t>("Clock Offset");
gps_id_128.add_field<uint32_t>("Time of Week");
gps_id_128.add_field<uint16_t>("Week Number");
gps_id_128.add_field<uint8_t> ("Channels");
gps_id_128.add_field<uint8_t> ("Reset Config");
// from now on, these fields can be accessed either by the name:
gps_id_128.set_field("Time of Week", 0xffeb3fe3); // to set the field.
// or by id:
gps_id_128.set_field(7, 2); // id == 7 for channels
@endcode
Similarly for reading.
Endianess can be specified for the Packet, and also
char* and other pointer type fields are supported.
(Refer to code and documentation for more details).
Nibbles and bits will appear here soon too hopefully. :)
___________________________
Copyright (c) 2012 Lukasz Forynski <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sub-license, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#ifndef PACKET_HANDLING_H_
#define PACKET_HANDLING_H_
#include <stdint.h>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <utility>
#include <stdio.h>
#include <typeinfo>
#include <cxxabi.h>
#include <stdarg.h>
#include <cstring>
#include <exception>
#include <iostream>
#include <iomanip>
#include <algorithm>
#ifdef _MSC_VER
#define VSNPRINTF(dest, max_len, format, vargs) vsnprintf_s(dest, max_len, max_len, format, vargs)
#define __PRETTY_FUNCTION__ __FUNCTION__
#else
#define VSNPRINTF(dest, max_len, format, vargs) vsnprintf(dest, max_len, format, vargs)
#endif
/**
@brief Default exception to allow to print some useful error info.
*/
class GenericException: public std::exception
{
public:
/**
@brief Overloaded constructor. Use it as printf() for exceptions..
*/
GenericException(std::string format, ...)
{
if(format.length() > 0)
{
va_list vArgs;
va_start(vArgs, format);
VSNPRINTF(msg, MAX_MSG_LEN, format.c_str(), vArgs);
va_end(vArgs);
}
}
/**
@brief Overridden method to print exception info..
*/
virtual const char* what() const throw ()
{
return msg;
}
protected:
enum constants
{
MAX_MSG_LEN = 512
};
char msg[MAX_MSG_LEN];
};
/**
* @brief template overloaded function used to determine if parameter is a pointer.
*/
template <class P>
bool is_pointer_type(P*)
{
return true;
}
/**
* @brief template overloaded function used to determine if parameter is a pointer.
*/
template <class R>
bool is_pointer_type(R)
{
return false;
}
/**
@brief PacketBuffer class. This class will hold the data to be processed
(i.e. buffer) and abstract operations that are to be performed on that data.
*/
class PacketBuffer
{
public:
/**
@brief Constructor.
@param buffer_addr: pointer to the actual data buffer this Packet should use.
@param buffer_size: size of the data buffer.
@param big_endian: If set to true - all multi-byte fields will be read according to
big_endian rules. Little_endian used otherwise.
*/
explicit PacketBuffer(uint8_t* buffer_addr, uint32_t buffer_size, uint32_t big_endian = 0) :
max_len(buffer_size),
use_big_endian(big_endian),
buffer(buffer_addr)
{
}
/**
@brief Method for getting a pointer to this buffer.
(i.e. to be used for receiving / sending the data)
*/
uint8_t* get_buffer_addr()
{
return buffer;
}
/**
@brief sets a new buffer address
*/
void setup_buffer(uint8_t* new_buffer, uint32_t buffer_size, uint32_t big_endian = 0)
{
if (new_buffer != NULL && buffer_size > 0)
{
buffer = new_buffer;
max_len = buffer_size;
use_big_endian = big_endian;
}
else
{
throw GenericException(
"Packet::%s() parameter error(new_buffer: 0x%lx, buffer_size %d)",
__FUNCTION__, new_buffer, buffer_size);
}
}
/**
@brief returns maximum length for the buffer.
*/
uint32_t max_length()
{
return max_len;
}
protected:
/**
@brief Default constructor.
Hidden to allow only overloaded constructor to be used.
*/
PacketBuffer() :
max_len(0),
use_big_endian(0),
buffer(NULL)
{
}
public:
typedef uint32_t (PacketBuffer::*get_value)(uint32_t);
typedef uint32_t (PacketBuffer::*set_value)(uint32_t, uint32_t);
typedef uint8_t* (PacketBuffer::*get_ptr)(uint32_t);
/**
@brief Returns one byte data stored in the buffer at at specified offset.
@param offset: offset from the beginning of the buffer.
*/
uint32_t get_uint8(uint32_t offset)
{
return buffer[offset];
}
/**
@brief Returns two-byte data stored in the buffer at at specified offset.
@param offset: offset from the beginning of the buffer.
*/
uint32_t get_uint16(uint32_t offset)
{
uint32_t value = buffer[offset++];
if (use_big_endian)
{
value <<= 8;
value |= buffer[offset];
}
else
{
value |= (uint16_t) buffer[offset] << 8;
}
return value;
}
/**
@brief Returns four-byte data stored in the buffer at at specified offset.
@param offset: offset from the beginning of the buffer.
*/
uint32_t get_uint32(uint32_t offset)
{
uint32_t value;
if (use_big_endian)
{
value = get_uint16(offset + 2);
value |= (uint32_t) get_uint16(offset) << 16;
}
else
{
value = get_uint16(offset);
value |= (uint32_t) get_uint16(offset + 2) << 16;
}
return value;
}
/**
@brief Returns a pointer to the item at specified offset.
@param offset: offset from the beginning of the buffer.
*/
uint8_t* get_uint8_ptr(uint32_t offset)
{
return &buffer[offset];
}
/**
@brief Stores one byte in the buffer at at specified offset.
@param offset: offset from the beginning of the buffer.
*/
uint32_t set_uint8(uint32_t offset, uint32_t value)
{
buffer[offset] = value;
return 0;
}
/**
@brief Stores two bytes in the buffer at at specified offset.
@param offset: offset from the beginning of the buffer.
*/
uint32_t set_uint16(uint32_t offset, uint32_t value)
{
if (use_big_endian)
{
buffer[offset++] = (value >> 8) & 0xff;
buffer[offset] = value & 0xff;
}
else
{
buffer[offset++] = value & 0xff;
buffer[offset] = (value >> 8) & 0xff;
}
return 0;
}
/**
@brief Stores four bytes in the buffer at at specified offset.
@param offset: offset from the beginning of the buffer.
*/
uint32_t set_uint32(uint32_t offset, uint32_t value)
{
if (use_big_endian)
{
set_uint16(offset + 2, value & 0xffff);
set_uint16(offset, (value >> 16) & 0xffff);
}
else
{
set_uint16(offset, value & 0xffff);
set_uint16(offset + 2, (value >> 16) & 0xffff);
}
return 0;
}
/**
@brief Dummy get operation. All uninitialised fields will have their 'get_value'
pointers set to point at this method.
@param offset: offset from the beginning of the buffer.
*/
uint32_t dummy_get(uint32_t /*offset*/)
{
// assertion here perhaps?
return 0;
}
/**
@brief Dummy get operation. All uninitialised fields will have their 'get_pointer'
pointers set to point at this method.
@param offset: offset from the beginning of the buffer.
*/
uint8_t* dummy_ptr(uint32_t /*offset*/)
{
// assertion here perhaps?
return NULL;
}
/**
@brief Dummy set operation. All uninitialised fields will have their 'set_value'
pointers set to point at this method.
@param offset: offset from the beginning of the buffer.
*/
uint32_t dummy_set(uint32_t /*offset*/, uint32_t /*value*/)
{
//assertion here perhaps?
return 0;
}
protected:
uint32_t max_len;
uint32_t use_big_endian;
uint8_t* buffer;
};
/**
@brief PacketField class.
This class is meant to abstract Packet fields.
Each of the fields defined for Packet will know it's type,
offset within the buffer where its corresponding data is to be found
and operations to access this data.
*/
class PacketField
{
public:
/**
@brief Constructor.
@param starts_at_offset: offset from the beginning of the buffer for this field.
@param len: length of this field.
@param id: Id of the field. It will be used to identify the field within the Packet,
i.e. the order of field.
*/
PacketField() :
offset(0),
length(0),
field_id(0),
type(0),
pointer(false)
{
printf("\nPacketField() called!!\n");
get_val = &PacketBuffer::dummy_get;
set_val = &PacketBuffer::dummy_set;
get_ptr = &PacketBuffer::dummy_ptr;
}
PacketField(uint32_t starts_at_offset, uint32_t len, uint32_t id = -1) :
offset(starts_at_offset),
length(len),
field_id(id),
type(0),
pointer(false)
{
get_val = &PacketBuffer::dummy_get;
set_val = &PacketBuffer::dummy_set;
get_ptr = &PacketBuffer::dummy_ptr;
}
public:
/**
@brief Template method used to set-up (store) the type this field is defined for.
@param (template) T: type this field is representing.
*/
template<typename T>
void set_type_info()
{
type = const_cast<std::type_info*>(&typeid(T));
pointer = is_pointer_type(T());
}
/**
@brief Template method used to check if this field is of a specific type.
@param (template) T: reference type used for comparison.
*/
template<typename T>
bool is_my_guessed_type()
{
bool res = false;
if (type && type == &typeid(T))
{
res = true;
}
return res;
}
/**
@brief Returns name of the type (std::type_info::name()) this field represents.
*/
const char* type_name()
{
if (type)
{
return type->name();
}
return NULL;
}
bool is_pointer()
{
return pointer;
}
uint32_t offset;
uint32_t length;
uint32_t field_id;
std::type_info* type;
bool pointer;
PacketBuffer::get_value get_val;
PacketBuffer::set_value set_val;
PacketBuffer::get_ptr get_ptr;
};
/**
@brief Template method used to create a field of a specific type.
Since the set of operation requires more than compiler could do resolving macros,
this template is not going to be used, and specialisations will be provided for specified
types.
*/
template<typename T>
inline PacketField new_PacketField(uint32_t offset, uint32_t len, uint32_t field_id)
{
return PacketField(offset, len, field_id);
}
/**
@brief Specialisation of a template method for creating fields for <uint32_t> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<uint32_t>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<uint32_t>();
f.get_val = &PacketBuffer::get_uint32;
f.set_val = &PacketBuffer::set_uint32;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <int32_t> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<int32_t>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<int32_t>();
f.get_val = &PacketBuffer::get_uint32;
f.set_val = &PacketBuffer::set_uint32;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <uint16_t> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<uint16_t>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<uint16_t>();
f.get_val = &PacketBuffer::get_uint16;
f.set_val = &PacketBuffer::set_uint16;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <int16_t> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<int16_t>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<int16_t>();
f.get_val = &PacketBuffer::get_uint16;
f.set_val = &PacketBuffer::set_uint16;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <uint8_t> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<uint8_t>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<uint8_t>();
f.get_val = &PacketBuffer::get_uint8;
f.set_val = &PacketBuffer::set_uint8;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <int8_t> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<int8_t>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<int8_t>();
f.get_val = &PacketBuffer::get_uint8;
f.set_val = &PacketBuffer::set_uint8;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <uint8_t*> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<uint8_t*>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<uint8_t*>();
f.get_ptr = &PacketBuffer::get_uint8_ptr;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <int8_t*> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<int8_t*>(uint32_t offset, uint32_t len, uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<int8_t*>();
f.get_ptr = &PacketBuffer::get_uint8_ptr;
return f;
}
/**
@brief Specialisation of a template method for creating fields for <char*> type.
@param offset: Offset from the beginning of the Packet for this field.
*/
template<>
inline PacketField new_PacketField<char*>(uint32_t offset, uint32_t len,
uint32_t field_id)
{
PacketField f(offset, len, field_id);
f.set_type_info<char*>();
f.get_ptr = &PacketBuffer::get_uint8_ptr;
return f;
}
/**
@brief Packet class.
Objects of this class will hold all the information needed to access the data,
providing the interface to create and access fields.
Two main ways to access Packet fields are available:
1. By field name.
If field is accessed by name, get/set operations have O(log n)
complexity (lookup implemented with std::map).
2. By field id. The ID is the actual number of the field since the beggining of
the Packet (starting from 0).
Get/set operations have O(1) complexity (no lookup, direct indexing)
*/
class Packet
{
public:
/**
@brief Constructor.
@param buffer_addr: pointer to the actual buffer this Packet should be representing.
@param buffer_size: size of the buffer.
@param big_endian: endianness specification.
@throws: Can throw GenericException (from PacketBuffer) if buffer_addr is NULL.
*/
explicit Packet(void* buffer_addr, uint32_t buffer_size, uint32_t big_endian = 0) :
verbose_print(false),
msg_buffer((uint8_t*)buffer_addr, buffer_size, big_endian), // can throw exception..
cur_length(0),
packet_name("(no name)")
{
}
/**
@brief Constructor.
*/
explicit Packet() :
verbose_print(false),
msg_buffer(NULL, 0),
cur_length(0),
packet_name("(no name)")
{
}
/**
@brief Destructor.
*/
~Packet()
{
}
/**
@brief sets a new buffer to be used with this Packet
*/
void setup_buffer(void* new_buffer, uint32_t buffer_size, uint32_t big_endian = 0)
{
if (new_buffer != NULL && buffer_size > 0)
{
msg_buffer.setup_buffer((uint8_t*)new_buffer, buffer_size, big_endian);
}
else
{
throw GenericException(
"Packet(%s)::%s(): parameter error(new_buffer: 0x%lx, buffer_size %d)",
packet_name.c_str(), __FUNCTION__, new_buffer, buffer_size);
}
}
/**
@brief Interface for adding a named field.
@param (template) T: specify type of the field being added.
@param name: name of the field.
@param length: length of the field.
@throws: Can throw GenericException if following situations:
- field being added is of pointer type but length was not specified
- field being added has a zero-length name
- field of specified has already been defined.
- Packet has no more space for the field being added.
*/
template<typename T>
int add_field(const std::string& name, uint32_t length = 0)
{
uint32_t field_id = -1;
if (msg_buffer.get_buffer_addr() == NULL)
{
throw GenericException(
"Packet(%s)::%s(): buffer is not set. Should call Packet::setup_buffer() before this method..",
packet_name.c_str(), __FUNCTION__);
}
if (!is_pointer_type(T()))
{
if (length == 0)
{
length = sizeof(T);
}
}
else
{
if (length == 0)
{
throw GenericException(
"Packet(%s)::%s(): field \"%s\": length is needed for pointer-type field",
packet_name.c_str(), __FUNCTION__, name.c_str());
}
}
if (name.length() <= 0)
{
throw GenericException("Packet(%s)::%s(): field name is empty!",
packet_name.c_str(), __FUNCTION__);
}
if (cur_length + length <= msg_buffer.max_length())
{
field_id = fields_by_id.size();
PacketField f = new_PacketField<T>(cur_length, length, field_id);
std::pair<std::map<std::string, PacketField>::iterator, bool> res = fields.insert(make_pair(name, f));
if(res.second)
{
cur_length += length;
fields_by_id.push_back(f);
}
else
{
std::string msg;
if(fields.find(name) != fields.end())
{
msg = "- field already exists";
}
throw GenericException("Packet(%s)::%s(): Error while adding new field: \"%s\" (size: 0x%x) %s",
packet_name.c_str(), __FUNCTION__, name.c_str(), f.length, msg.c_str());
}
}
else
{
throw GenericException("Packet(%s)::%s(): Packet is too short to add new field: \"%s\" (size: 0x%x)",
packet_name.c_str(), __FUNCTION__, name.c_str(), length);
}
return field_id;
}
/**
@brief Updates the value of the field.
@param name: name of the field.
@param value: new value for the field.
@throws: Can throw GenericException if following situations:
- field does not exist in the Packet.
*/
void set_field(const std::string& name, uint32_t value)
{
std::map<std::string, PacketField>::iterator i = fields.find(name);
if (i != fields.end())
{
// if field is found, call the method it points to to access the data
PacketField& f = i->second;
(msg_buffer.*f.set_val)(f.offset, value);
}
else
{
throw GenericException("Packet(%s)::%s(): field \"%s\" not found",
packet_name.c_str(), __FUNCTION__, name.c_str());
}
}
/**
@brief Updates the value of the field.
@param field_id: id of the field.
@param value: new value for the field.
@throws: Can throw GenericException if following situations:
- field pointed by id does not exist in the Packet.
*/
void set_field(uint32_t field_id, uint32_t value)
{
if (field_id < fields_by_id.size())
{
// call the method it points to to access the data
PacketField& f = fields_by_id[field_id];
(msg_buffer.*f.set_val)(f.offset, value);
}
else
{
throw GenericException("Packet(%s)::%s(): field_index %d not found",
packet_name.c_str(), __FUNCTION__, field_id);
}
}
/**
@brief Updates the value of the field.
@param name: name of the field.
@param src: pointer to the source data the field should be updated from.
Note, that length is not needed, as the field has a fixed length,
and so there is no requirement for the null-termination etc.
@throws: Can throw GenericException if following situations:
- field does not exist in the Packet.
- field is not of pointer type.
@return: Returns the length of data (in bytes) that was copied to the Packet.
*/
template <typename T>
uint32_t set_field(const std::string& name, const T* src, int src_len=-1)
{
uint32_t length_written = 0;
std::map<std::string, PacketField>::iterator i = fields.find(name);
if (i != fields.end())
{
// if field is found, call the method it points to to access the data
PacketField& f = i->second;
if (!f.is_pointer())
{
throw GenericException(
"Packet(%s)::%s(): wrong method for non-pointer type field (%s).",
packet_name.c_str(), __FUNCTION__, name.c_str());
}
uint8_t* dst_ptr = msg_buffer.get_uint8_ptr(f.offset);
if(src_len > 0)
{
if(static_cast<uint32_t>(src_len) > f.length)
{
throw GenericException(
"Packet(%s)::%s(): specified src_len (%d) is invalid.",
packet_name.c_str(), __FUNCTION__, src_len);
}
}
else
{
src_len = f.length;
}
if (src != NULL)
{
memcpy(dst_ptr, src, src_len);
}
else
{
memset(dst_ptr, 0u, src_len);
}
length_written = src_len;
}
else
{
throw GenericException("Packet(%s)::%s(): field \"%s\" not found",
packet_name.c_str(), __FUNCTION__, name.c_str());
}
return length_written;
}
/**
@brief Updates the value of the field.
@param id: id of the field.
@param src: pointer to the source data the field should be updated from.
Note, that length is not needed, as the field has a fixed length,
and so there is no requirement for the null-termination etc.
@throws: Can throw GenericException if following situations:
- field does not exist in the Packet.
- field is not of pointer type.
@return: Returns the length of data (in bytes) that was copied to the Packet.
*/
template <class T>
uint32_t set_field(uint32_t field_id, const T* src)
{
uint32_t length_written = 0;
if (field_id < fields_by_id.size())
{
// call the method it points to to access the data
PacketField& f = fields_by_id[field_id];
if (!f.is_pointer())
{
throw GenericException("Packet(%s)::%s(): wrong method for non-pointer type field (%d).",
packet_name.c_str(), __FUNCTION__, field_id);
}
uint8_t* dst_ptr = msg_buffer.get_uint8_ptr(f.offset);
if (src != NULL)
{
memcpy(dst_ptr, src, f.length);
}
else
{
memset(dst_ptr, 0, f.length);
}
length_written = f.length;
}
else
{
throw GenericException("Packet(%s)::%s(): field_index %s not found",
packet_name.c_str(), __FUNCTION__, field_id);
}
return length_written;
}
/**
@brief Gets the value of the field from the Packet.
@param name: name of the field.
@throws: Can throw GenericException if following situations:
- field does not exist in the Packet.
- field is a pointer type (these should be accessed by other
overloads of this method.
@return: Returns the actual value for this field.
*/
uint32_t get_field(const std::string& name)
{
uint32_t val = 0;
std::map<std::string, PacketField>::iterator i = fields.find(name);
if (i != fields.end())
{
// if field is found, call the method it points to to access the data
PacketField& f = i->second;
if (f.is_pointer())
{
throw GenericException("Packet(%s)::%s(): wrong method for pointer type field (%s).",
packet_name.c_str(), __FUNCTION__, name.c_str());
}
val = (msg_buffer.*f.get_val)(f.offset);
}
else
{
throw GenericException("Packet(%s)::%s(): field \"%s\" not found",
packet_name.c_str(), __FUNCTION__, name.c_str());
}
return val;
}
/**
@brief Gets the value of the field from the Packet.
@param id: id of the field.
@throws: Can throw GenericException if following situations:
- field does not exist in the Packet.
- field is a pointer type (these should be accessed by other
overloads of this method.
@return: Returns the actual value for this field.
*/
uint32_t get_field(uint32_t field_id)
{
uint32_t val = 0;
if (field_id < fields_by_id.size())
{
// call the method it points to to access the data
PacketField& f = fields_by_id[field_id];
if (f.is_pointer())
{
throw GenericException("Packet(%s)::%s(): wrong method for pointer type field (%d).",
packet_name.c_str(), __FUNCTION__, field_id);
}
val = (msg_buffer.*f.get_val)(f.offset);
}
else
{
throw GenericException("Packet(%s)::%s(): field_index %s not found",
packet_name.c_str(), __FUNCTION__, field_id);
}
return val;
}
/**
@brief Copies the value of the field.
@param name: name of the field.
@param destination: pointer for the destination that the data should be copied.
If NULL - only a pointer to data will be returned.
(be careful not to modify the data past the boundaries!)
@throws: Can throw GenericException if following situations:
- field does not exist in the Packet.
- field is not of pointer type.
@return: Returns a pointer to the original data (if destination was NULL) or to the copied data otherwise.
*/
void* get_field(const std::string& name, void* destination)
{
uint8_t* src_ptr = NULL;
std::map<std::string, PacketField>::iterator i = fields.find(name);
if (i != fields.end())
{
// if field is found, call the method it points to to access the data
PacketField& f = i->second;
if (!f.is_pointer())
{
throw GenericException(
"Packet(%s)::%s(): wrong method for non-pointer type field (%s).",
packet_name.c_str(), __FUNCTION__, name.c_str());