The Assimilation Project
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
frame.c
Go to the documentation of this file.
1 
23 #include <frame.h>
24 #include <generic_tlv_min.h>
25 #include <memory.h>
26 
28 FSTATIC gsize _frame_dataspace(const Frame* f);
29 FSTATIC gboolean _frame_default_isvalid(const Frame *, gconstpointer, gconstpointer);
30 FSTATIC void _frame_setvalue(Frame *, gpointer, guint32, GDestroyNotify valnotify);
31 FSTATIC void _frame_updatedata(Frame *, gpointer, gconstpointer, FrameSet*);
32 FSTATIC void _frame_dump(const Frame *, const char * prefix);
33 FSTATIC gchar* _frame_toString(gconstpointer aself);
34 
36 
41 
43 FSTATIC void
45 {
46  Frame* self = CASTTOCLASS(Frame, obj);
47  DEBUGMSG5("%s: Finalizing Frame at 0x%p", __FUNCTION__, self);
48  if (self->value && self->valuefinalize) {
49  self->valuefinalize(self->value);
50  }
51  memset(self, 0x00, sizeof(Frame));
52  _assimobj_finalize(obj);
53 }
55 FSTATIC void
57 {
58  if (value) {
59  DEBUGMSG4("%s: freeing Frame value at 0x%p", __FUNCTION__, value);
60  FREE(value);
61  }
62 }
63 
65 FSTATIC gsize
67 {
68  g_return_val_if_fail(f != NULL, 0);
69  return (gsize)(FRAME_INITSIZE + f->length);
70 }
71 
73 FSTATIC gboolean
75  gconstpointer tlvptr,
76  gconstpointer pktend)
77 {
78  (void)self;
79  (void)tlvptr;
80  (void)pktend;
81  return TRUE;
82 }
83 
85 FSTATIC gchar*
86 _frame_toString(gconstpointer aself)
87 {
88  const Frame* self = CASTTOCONSTCLASS(Frame, aself);
89  return g_strdup_printf("Frame(frametype=%d, length=%d, address=%p)"
90  , self->type, self->length, self);
91 }
92 
93 
95 FSTATIC void
97  gpointer value,
98  guint32 length,
99  GDestroyNotify valnotify)
100 {
101  if (self->value && self->valuefinalize) {
102  self->valuefinalize(self);
103  }
104  self->value = value;
105  self->length = length;
106  self->valuefinalize = valnotify;
107 }
108 
110 FSTATIC void
112  gpointer tlvptr,
113  gconstpointer pktend,
114  FrameSet* fs)
115 {
116  (void)fs;
117  // set_generic_tlv_value does pretty exhaustive error checking.
118  set_generic_tlv_value(tlvptr, self->value, self->length, pktend);
119 }
120 
123 Frame*
124 frame_new(guint16 frame_type,
125  gsize framesize)
126 {
127  AssimObj * newobj;
128  Frame * newframe = NULL;
129 
130  BINDDEBUG(Frame);
131  if (framesize < sizeof(Frame)) {
132  framesize = sizeof(Frame);
133  }
134  newobj = assimobj_new(framesize);
135  if (framesize == sizeof(Frame)) {
136  DEBUGMSG5("%s: Constructing New Frame at 0x%p", __FUNCTION__, newobj);
137  }
138  if (newobj != NULL) {
139  newframe = NEWSUBCLASS(Frame, newobj);
141  newobj->toString = _frame_toString;
142  newframe->type = frame_type;
143  newframe->length = 0;
144  newframe->value = NULL;
145  newframe->dataspace = _frame_dataspace;
146  newframe->isvalid = _frame_default_isvalid;
147  newframe->setvalue = _frame_setvalue;
148  newframe->updatedata = _frame_updatedata;
149  newframe->dump = _frame_dump;
150  newframe->valuefinalize = NULL;
151  }
152  return newframe;
153 }
156 Frame*
157 frame_tlvconstructor(gconstpointer tlvstart,
158  gconstpointer pktend,
159  gpointer* ignorednewpkt,
160  gpointer* ignoredpktend)
161 {
162  guint16 frametype = get_generic_tlv_type(tlvstart, pktend);
163  guint32 framelength = get_generic_tlv_len(tlvstart, pktend);
164  const guint8* framevalue = get_generic_tlv_value(tlvstart, pktend);
165  Frame * ret = frame_new(frametype, 0);
166  (void)ignorednewpkt; (void)ignoredpktend;
167  g_return_val_if_fail(ret != NULL, NULL);
168 
169  ret->length = framelength;
170  if (framelength > 0) {
171  ret->setvalue(ret, g_memdup(framevalue, framelength), framelength
173  }else{
174  ret->value = 0;
175  }
176  return ret;
177 }
180 void
181 _frame_dump(const Frame * f,
182  const char * prefix)
183 {
184  g_debug("%s%s: type = %d, length = %d dataspace=%zd",
185  prefix,
187  f->type,
188  f->length,
189  f->dataspace(f));
190 }
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:66
GDestroyNotify valuefinalize
method for finalizing value
Definition: frame.h:57
FSTATIC void _frame_default_finalize(AssimObj *self)
Finalize a Frame.
Definition: frame.c:44
#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 DEBUGMSG5(...)
Definition: proj_classes.h:93
FSTATIC void _frame_setvalue(Frame *, gpointer, guint32, GDestroyNotify valnotify)
'setvalue' Frame class member function.
Definition: frame.c:96
#define FSTATIC
Definition: projectcommon.h:31
gpointer value
Frame Value (pointer)
Definition: frame.h:47
void(* dump)(const Frame *self, const char *prefix)
member function for dumping Frame
Definition: frame.h:56
Frame * frame_tlvconstructor(gconstpointer tlvstart, gconstpointer pktend, gpointer *ignorednewpkt, gpointer *ignoredpktend)
Given marshalled data corresponding to a Frame (basic binary frame), return that corresponding Frame ...
Definition: frame.c:157
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:111
FSTATIC gchar * _frame_toString(gconstpointer aself)
Frame class 'toString' operation - convert a basic Frame into a string
Definition: frame.c:86
Frame * frame_new(guint16 frame_type, gsize framesize)
Construct a new frame - allowing for "derived" frame types...
Definition: frame.c:124
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)
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:56
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:45
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:74
#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:181
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 NEWSUBCLASS(Cclass, obj)
Definition: proj_classes.h:67
struct _Frame Frame
Definition: frame.h:33