The Assimilation Project  based on Assimilation version 0.5.1427809010
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
frame.c
Go to the documentation of this file.
1 
23 #include <frame.h>
24 #include <generic_tlv_min.h>
25 #include <tlvhelper.h>
26 #include <memory.h>
27 
29 FSTATIC gsize _frame_dataspace(const Frame* f);
30 FSTATIC gboolean _frame_default_isvalid(const Frame *, gconstpointer, gconstpointer);
31 FSTATIC void _frame_setvalue(Frame *, gpointer, guint32, GDestroyNotify valnotify);
32 FSTATIC void _frame_updatedata(Frame *, gpointer, gconstpointer, FrameSet*);
33 FSTATIC void _frame_dump(const Frame *, const char * prefix);
34 FSTATIC gchar* _frame_toString(gconstpointer aself);
35 
37 
42 
44 FSTATIC void
46 {
47  Frame* self = CASTTOCLASS(Frame, obj);
48  DEBUGMSG5("%s: Finalizing Frame at 0x%p", __FUNCTION__, self);
49  if (self->value && self->valuefinalize) {
50  self->valuefinalize(self->value);
51  }
52  memset(self, 0x00, sizeof(Frame));
53  _assimobj_finalize(obj);
54 }
56 FSTATIC void
58 {
59  if (value) {
60  DEBUGMSG4("%s: freeing Frame value at 0x%p", __FUNCTION__, value);
61  FREE(value);
62  }
63 }
64 
66 FSTATIC gsize
68 {
69  g_return_val_if_fail(f != NULL, 0);
70  return (gsize)(FRAME_INITSIZE + f->length);
71 }
72 
74 FSTATIC gboolean
76  gconstpointer tlvptr,
77  gconstpointer pktend)
78 {
79  (void)self;
80  (void)tlvptr;
81  (void)pktend;
82  return TRUE;
83 }
84 
86 FSTATIC gchar*
87 _frame_toString(gconstpointer aself)
88 {
89  const Frame* self = CASTTOCONSTCLASS(Frame, aself);
90  return g_strdup_printf("Frame(frametype=%d, length=%d, address=%p)"
91  , self->type, self->length, self);
92 }
93 
94 
96 FSTATIC void
98  gpointer value,
99  guint32 length,
100  GDestroyNotify valnotify)
101 {
102  if (self->value && self->valuefinalize) {
103  self->valuefinalize(self);
104  }
105  self->value = value;
106  self->length = length;
107  self->valuefinalize = valnotify;
108 }
109 
111 FSTATIC void
113  gpointer tlvptr,
114  gconstpointer pktend,
115  FrameSet* fs)
116 {
117  (void)fs;
118  // set_generic_tlv_value does pretty exhaustive error checking.
119  set_generic_tlv_value(tlvptr, self->value, self->length, pktend);
120 }
121 
124 Frame*
125 frame_new(guint16 frame_type,
126  gsize framesize)
127 {
128  AssimObj * newobj;
129  Frame * newframe = NULL;
130 
131  BINDDEBUG(Frame);
132  if (framesize < sizeof(Frame)) {
133  framesize = sizeof(Frame);
134  }
135  newobj = assimobj_new(framesize);
136  if (framesize == sizeof(Frame)) {
137  DEBUGMSG5("%s: Constructing New Frame at 0x%p", __FUNCTION__, newobj);
138  }
139  if (newobj != NULL) {
140  newframe = NEWSUBCLASS(Frame, newobj);
142  newobj->toString = _frame_toString;
143  newframe->type = frame_type;
144  newframe->length = 0;
145  newframe->value = NULL;
146  newframe->dataspace = _frame_dataspace;
147  newframe->isvalid = _frame_default_isvalid;
148  newframe->setvalue = _frame_setvalue;
149  newframe->updatedata = _frame_updatedata;
150  newframe->dump = _frame_dump;
151  newframe->valuefinalize = NULL;
152  }
153  return newframe;
154 }
157 Frame*
158 frame_tlvconstructor(gpointer tlvstart,
159  gconstpointer pktend,
160  gpointer* ignorednewpkt,
161  gpointer* ignoredpktend)
162 {
163  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
164  guint32 framelength = get_generic_tlv_len(tlvstart, pktend);
165  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
166  Frame * ret = frame_new(frametype, 0);
167  (void)ignorednewpkt; (void)ignoredpktend;
168  g_return_val_if_fail(ret != NULL && frametype != TLV_BAD16 && framelength != TLV_BAD24, NULL);
169 
170  ret->length = framelength;
171  if (framelength > 0) {
172  ret->setvalue(ret, g_memdup(framevalue, framelength), framelength
174  }else{
175  ret->value = 0;
176  }
177  return ret;
178 }
181 void
182 _frame_dump(const Frame * f,
183  const char * prefix)
184 {
185  g_debug("%s%s: type = %d, length = %d dataspace=%zd",
186  prefix,
188  f->type,
189  f->length,
190  f->dataspace(f));
191 }
void _assimobj_finalize(AssimObj *self)
Definition: assimobj.c:61
#define FRAME_INITSIZE
Definition: frame.h:59
guint32 length
Frame Length.
Definition: frame.h:46
FSTATIC gsize _frame_dataspace(const Frame *f)
Return total space required to put this frame in a packet (marshalled size)
Definition: frame.c:67
GDestroyNotify valuefinalize
method for finalizing value
Definition: frame.h:57
FSTATIC void _frame_default_finalize(AssimObj *self)
Finalize a Frame.
Definition: frame.c:45
#define DEBUGMSG4(...)
Definition: proj_classes.h:92
This is the base Frame class object (in-memory TLV (type, length, value)) for every general component...
Definition: frame.h:43
#define TLV_BAD16
Definition: tlvhelper.h:30
#define DEBUGMSG5(...)
Definition: proj_classes.h:93
FSTATIC void _frame_setvalue(Frame *, gpointer, guint32, GDestroyNotify valnotify)
'setvalue' Frame class member function.
Definition: frame.c:97
#define FSTATIC
Definition: projectcommon.h:31
gpointer value
Frame Value (pointer)
Definition: frame.h:47
Frame * frame_tlvconstructor(gpointer tlvstart, gconstpointer pktend, gpointer *ignorednewpkt, gpointer *ignoredpktend)
Given marshalled data corresponding to a Frame (basic binary frame), return that corresponding Frame ...
Definition: frame.c:158
void(* dump)(const Frame *self, const char *prefix)
member function for dumping Frame
Definition: frame.h:56
guint16 get_generic_tlv_type(gconstpointer tlv_vp, gconstpointer pktend)
Return the 'Type' of the given TLV TLV entry (first two bytes)
#define BINDDEBUG(Cclass)
BINDDEBUG is for telling the class system where the debug variable for this class is - put it in the ...
Definition: proj_classes.h:82
#define __FUNCTION__
FSTATIC void _frame_updatedata(Frame *, gpointer, gconstpointer, FrameSet *)
Update packet data ('updatedata') Frame class member function.
Definition: frame.c:112
FSTATIC gchar * _frame_toString(gconstpointer aself)
Frame class 'toString' operation - convert a basic Frame into a string
Definition: frame.c:87
Frame * frame_new(guint16 frame_type, gsize framesize)
Construct a new frame - allowing for "derived" frame types...
Definition: frame.c:125
gboolean(* isvalid)(const Frame *self, gconstpointer tlvptr, gconstpointer pktend)
TRUE if TLV data looks valid...
Definition: frame.h:50
#define FREE(m)
Our interface to free.
Definition: projectcommon.h:29
Implements basic Frame class.
#define CASTTOCONSTCLASS(Cclass, obj)
Safely cast 'obj' to const C-class 'class' - verifying that it was registered as being of type class ...
Definition: proj_classes.h:71
void set_generic_tlv_value(gpointer tlv_vp, void *srcdata, guint32 srcsize, gconstpointer pktend)
Set the TLV data value to the given 'tlv_vp' pointer.
guint32 get_generic_tlv_len(gconstpointer tlv_vp, gconstpointer pktend)
Return the 'Length' of the given generic TLV entry (first 3 bytes after type) Note: return result is ...
TLV helper interfaces definitions.
AssimObj * assimobj_new(guint objsize)
Definition: assimobj.c:74
const char * proj_class_classname(gconstpointer object)
Return the class name of one of our managed objects.
Definition: proj_classes.c:324
FSTATIC void frame_default_valuefinalize(gpointer value)
Finalize a Frame.
Definition: frame.c:57
void(* _finalize)(AssimObj *)
Free object (private)
Definition: assimobj.h:55
guint16 type
Frame Type (see Individual TLV 'Frame' data types and layouts (by TLV type) - frameformats.h )
Definition: frame.h:45
Provides definitions for using our generic TLV capabilities.
gconstpointer get_generic_tlv_value(gconstpointer tlv_vp, gconstpointer pktend)
Return a const pointer to the 'Value' of the given generic TLV entry.
gsize(* dataspace)(const Frame *self)
How much space is needed to marshall this Frame?
Definition: frame.h:48
void(* updatedata)(Frame *self, gpointer tlvptr, gconstpointer pktend, FrameSet *fs)
Update packet data.
Definition: frame.h:49
FrameSet class - used for collecting Frames when not on the wire, and for marshalling/demarshalling t...
Definition: frameset.h:46
gchar *(* toString)(gconstpointer)
Produce malloc-ed string representation.
Definition: assimobj.h:58
FSTATIC gboolean _frame_default_isvalid(const Frame *, gconstpointer, gconstpointer)
Default Frame class 'isvalid' member function (always returns TRUE)
Definition: frame.c:75
#define DEBUGDECLARATIONS
Definition: proj_classes.h:79
FSTATIC void _frame_dump(const Frame *, const char *prefix)
Basic "dump a frame" member function - we use g_debug() for output.
Definition: frame.c:182
void(* setvalue)(Frame *self, gpointer value, guint32 length, GDestroyNotify valfinal)
member function for setting value
Definition: frame.h:52
#define CASTTOCLASS(Cclass, obj)
Safely cast 'obj' to C-class 'class' - verifying that it was registerd as being of type class ...
Definition: proj_classes.h:66
#define TLV_BAD24
Definition: tlvhelper.h:31
#define NEWSUBCLASS(Cclass, obj)
Definition: proj_classes.h:67
struct _Frame Frame
Definition: frame.h:33