Fawkes API  Fawkes Development Version
HardwareModelsInterface.cpp
1 
2 /***************************************************************************
3  * HardwareModelsInterface.cpp - Fawkes BlackBoard Interface - HardwareModelsInterface
4  *
5  * Templated created: Thu Oct 12 10:49:19 2006
6  * Copyright 2019 Daniel Habering
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <interfaces/HardwareModelsInterface.h>
25 
26 #include <core/exceptions/software.h>
27 
28 #include <map>
29 #include <string>
30 #include <cstring>
31 #include <cstdlib>
32 
33 namespace fawkes {
34 
35 /** @class HardwareModelsInterface <interfaces/HardwareModelsInterface.h>
36  * HardwareModelsInterface Fawkes BlackBoard Interface.
37  *
38  Interface to inform the plugin about state changes in components
39 
40  * @ingroup FawkesInterfaces
41  */
42 
43 
44 
45 /** Constructor */
46 HardwareModelsInterface::HardwareModelsInterface() : Interface()
47 {
48  data_size = sizeof(HardwareModelsInterface_data_t);
49  data_ptr = malloc(data_size);
50  data = (HardwareModelsInterface_data_t *)data_ptr;
51  data_ts = (interface_data_ts_t *)data_ptr;
52  memset(data_ptr, 0, data_size);
53  add_fieldinfo(IFT_STRING, "error", 1024, data->error);
54  add_fieldinfo(IFT_BOOL, "busy", 1, &data->busy);
55  add_messageinfo("StateChangeMessage");
56  unsigned char tmp_hash[] = {0x55, 0xa2, 0xe2, 0xba, 0xd2, 0x11, 0xbc, 0x8, 0x8c, 0x21, 0xe, 0x9d, 0xaf, 0x39, 0x8f, 0xf2};
57  set_hash(tmp_hash);
58 }
59 
60 /** Destructor */
61 HardwareModelsInterface::~HardwareModelsInterface()
62 {
63  free(data_ptr);
64 }
65 /* Methods */
66 /** Get error value.
67  * Error state of the plugin
68  * @return error value
69  */
70 char *
71 HardwareModelsInterface::error() const
72 {
73  return data->error;
74 }
75 
76 /** Get maximum length of error value.
77  * @return length of error value, can be length of the array or number of
78  * maximum number of characters for a string
79  */
80 size_t
81 HardwareModelsInterface::maxlenof_error() const
82 {
83  return 1024;
84 }
85 
86 /** Set error value.
87  * Error state of the plugin
88  * @param new_error new error value
89  */
90 void
91 HardwareModelsInterface::set_error(const char * new_error)
92 {
93  data_changed |= change_field(data->error, new_error);
94 }
95 
96 /** Get busy value.
97  * Busy flag
98  * @return busy value
99  */
100 bool
101 HardwareModelsInterface::is_busy() const
102 {
103  return data->busy;
104 }
105 
106 /** Get maximum length of busy value.
107  * @return length of busy value, can be length of the array or number of
108  * maximum number of characters for a string
109  */
110 size_t
111 HardwareModelsInterface::maxlenof_busy() const
112 {
113  return 1;
114 }
115 
116 /** Set busy value.
117  * Busy flag
118  * @param new_busy new busy value
119  */
120 void
121 HardwareModelsInterface::set_busy(const bool new_busy)
122 {
123  data_changed |= change_field(data->busy, new_busy);
124 }
125 
126 /* =========== message create =========== */
127 Message *
128 HardwareModelsInterface::create_message(const char *type) const
129 {
130  if ( strncmp("StateChangeMessage", type, INTERFACE_MESSAGE_TYPE_SIZE_ - 1) == 0 ) {
131  return new StateChangeMessage();
132  } else {
133  throw UnknownTypeException("The given type '%s' does not match any known "
134  "message type for this interface type.", type);
135  }
136 }
137 
138 
139 /** Copy values from other interface.
140  * @param other other interface to copy values from
141  */
142 void
143 HardwareModelsInterface::copy_values(const Interface *other)
144 {
145  const HardwareModelsInterface *oi = dynamic_cast<const HardwareModelsInterface *>(other);
146  if (oi == NULL) {
147  throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
148  type(), other->type());
149  }
150  memcpy(data, oi->data, sizeof(HardwareModelsInterface_data_t));
151 }
152 
153 const char *
154 HardwareModelsInterface::enum_tostring(const char *enumtype, int val) const
155 {
156  throw UnknownTypeException("Unknown enum type %s", enumtype);
157 }
158 
159 /* =========== messages =========== */
160 /** @class HardwareModelsInterface::StateChangeMessage <interfaces/HardwareModelsInterface.h>
161  * StateChangeMessage Fawkes BlackBoard Interface Message.
162  *
163 
164  */
165 
166 
167 /** Constructor with initial values.
168  * @param ini_component initial value for component
169  * @param ini_transition initial value for transition
170  */
171 HardwareModelsInterface::StateChangeMessage::StateChangeMessage(const char * ini_component, const char * ini_transition) : Message("StateChangeMessage")
172 {
173  data_size = sizeof(StateChangeMessage_data_t);
174  data_ptr = malloc(data_size);
175  memset(data_ptr, 0, data_size);
176  data = (StateChangeMessage_data_t *)data_ptr;
178  strncpy(data->component, ini_component, 1024-1);
179  data->component[1024-1] = 0;
180  strncpy(data->transition, ini_transition, 1024-1);
181  data->transition[1024-1] = 0;
182  add_fieldinfo(IFT_STRING, "component", 1024, data->component);
183  add_fieldinfo(IFT_STRING, "transition", 1024, data->transition);
184 }
185 /** Constructor */
187 {
188  data_size = sizeof(StateChangeMessage_data_t);
189  data_ptr = malloc(data_size);
190  memset(data_ptr, 0, data_size);
191  data = (StateChangeMessage_data_t *)data_ptr;
193  add_fieldinfo(IFT_STRING, "component", 1024, data->component);
194  add_fieldinfo(IFT_STRING, "transition", 1024, data->transition);
195 }
196 
197 /** Destructor */
199 {
200  free(data_ptr);
201 }
202 
203 /** Copy constructor.
204  * @param m message to copy from
205  */
207 {
208  data_size = m->data_size;
209  data_ptr = malloc(data_size);
210  memcpy(data_ptr, m->data_ptr, data_size);
211  data = (StateChangeMessage_data_t *)data_ptr;
213 }
214 
215 /* Methods */
216 /** Get component value.
217  * The component that changed its state
218  * @return component value
219  */
220 char *
222 {
223  return data->component;
224 }
225 
226 /** Get maximum length of component value.
227  * @return length of component value, can be length of the array or number of
228  * maximum number of characters for a string
229  */
230 size_t
232 {
233  return 1024;
234 }
235 
236 /** Set component value.
237  * The component that changed its state
238  * @param new_component new component value
239  */
240 void
242 {
243  change_field(data->component, new_component);
244 }
245 
246 /** Get transition value.
247  * The transition executed by the component
248  * @return transition value
249  */
250 char *
252 {
253  return data->transition;
254 }
255 
256 /** Get maximum length of transition value.
257  * @return length of transition value, can be length of the array or number of
258  * maximum number of characters for a string
259  */
260 size_t
262 {
263  return 1024;
264 }
265 
266 /** Set transition value.
267  * The transition executed by the component
268  * @param new_transition new transition value
269  */
270 void
272 {
273  change_field(data->transition, new_transition);
274 }
275 
276 /** Clone this message.
277  * Produces a message of the same type as this message and copies the
278  * data to the new message.
279  * @return clone of this message
280  */
281 Message *
283 {
285 }
286 /** Check if message is valid and can be enqueued.
287  * @param message Message to check
288  * @return true if the message is valid, false otherwise.
289  */
290 bool
292 {
293  const StateChangeMessage *m0 = dynamic_cast<const StateChangeMessage *>(message);
294  if ( m0 != NULL ) {
295  return true;
296  }
297  return false;
298 }
299 
300 /// @cond INTERNALS
301 EXPORT_INTERFACE(HardwareModelsInterface)
302 /// @endcond
303 
304 
305 } // end namespace fawkes
StateChangeMessage Fawkes BlackBoard Interface Message.
virtual Message * clone() const
Clone this message.
void set_component(const char *new_component)
Set component value.
void set_transition(const char *new_transition)
Set transition value.
size_t maxlenof_transition() const
Get maximum length of transition value.
size_t maxlenof_component() const
Get maximum length of component value.
HardwareModelsInterface Fawkes BlackBoard Interface.
virtual bool message_valid(const Message *message) const
Check if message is valid and can be enqueued.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
const char * type() const
Get type of interface.
Definition: interface.cpp:643
void * data_ptr
Pointer to local memory storage.
Definition: interface.h:224
Base class for all messages passed through interfaces in Fawkes BlackBoard.
Definition: message.h:45
void add_fieldinfo(interface_fieldtype_t type, const char *name, size_t length, void *value, const char *enumtype=0, const interface_enum_map_t *enum_map=0)
Add an entry to the info list.
Definition: message.cpp:400
void * data_ptr
Pointer to memory that contains local data.
Definition: message.h:128
message_data_ts_t * data_ts
data timestamp aliasing pointer
Definition: message.h:138
unsigned int data_size
Size of memory needed to hold all data.
Definition: message.h:129
Fawkes library namespace.
@ IFT_STRING
string field
Definition: types.h:48
bool change_field(FieldT &field, const DataT &value)
Set a field and return whether it changed.
Definition: message.h:167
Timestamp data, must be present and first entries for each interface data structs!...
Definition: message.h:134