00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <interfaces/SwitchInterface.h>
00025
00026 #include <core/exceptions/software.h>
00027
00028 #include <cstring>
00029 #include <cstdlib>
00030
00031 namespace fawkes {
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 SwitchInterface::SwitchInterface() : Interface()
00048 {
00049 data_size = sizeof(SwitchInterface_data_t);
00050 data_ptr = malloc(data_size);
00051 data = (SwitchInterface_data_t *)data_ptr;
00052 memset(data_ptr, 0, data_size);
00053 add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00054 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
00055 add_fieldinfo(IFT_FLOAT, "history", 1, &data->history);
00056 add_fieldinfo(IFT_UINT, "short_activations", 1, &data->short_activations);
00057 add_fieldinfo(IFT_UINT, "long_activations", 1, &data->long_activations);
00058 add_fieldinfo(IFT_UINT, "activation_count", 1, &data->activation_count);
00059 add_messageinfo("SetMessage");
00060 add_messageinfo("EnableSwitchMessage");
00061 add_messageinfo("DisableSwitchMessage");
00062 unsigned char tmp_hash[] = {0xcb, 0xd2, 0x25, 0x8e, 0x3e, 0x18, 0xd6, 0x3a, 0xe0, 0x12, 0x73, 0x27, 0x26, 0x75, 0x95, 0x56};
00063 set_hash(tmp_hash);
00064 }
00065
00066
00067 SwitchInterface::~SwitchInterface()
00068 {
00069 free(data_ptr);
00070 }
00071
00072
00073
00074
00075
00076
00077
00078 bool
00079 SwitchInterface::is_enabled() const
00080 {
00081 return data->enabled;
00082 }
00083
00084
00085
00086
00087
00088 size_t
00089 SwitchInterface::maxlenof_enabled() const
00090 {
00091 return 1;
00092 }
00093
00094
00095
00096
00097
00098
00099
00100 void
00101 SwitchInterface::set_enabled(const bool new_enabled)
00102 {
00103 data->enabled = new_enabled;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 float
00115 SwitchInterface::value() const
00116 {
00117 return data->value;
00118 }
00119
00120
00121
00122
00123
00124 size_t
00125 SwitchInterface::maxlenof_value() const
00126 {
00127 return 1;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 void
00139 SwitchInterface::set_value(const float new_value)
00140 {
00141 data->value = new_value;
00142 }
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 float
00156 SwitchInterface::history() const
00157 {
00158 return data->history;
00159 }
00160
00161
00162
00163
00164
00165 size_t
00166 SwitchInterface::maxlenof_history() const
00167 {
00168 return 1;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 void
00183 SwitchInterface::set_history(const float new_history)
00184 {
00185 data->history = new_history;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194
00195 unsigned int
00196 SwitchInterface::short_activations() const
00197 {
00198 return data->short_activations;
00199 }
00200
00201
00202
00203
00204
00205 size_t
00206 SwitchInterface::maxlenof_short_activations() const
00207 {
00208 return 1;
00209 }
00210
00211
00212
00213
00214
00215
00216
00217
00218 void
00219 SwitchInterface::set_short_activations(const unsigned int new_short_activations)
00220 {
00221 data->short_activations = new_short_activations;
00222 }
00223
00224
00225
00226
00227
00228
00229
00230
00231 unsigned int
00232 SwitchInterface::long_activations() const
00233 {
00234 return data->long_activations;
00235 }
00236
00237
00238
00239
00240
00241 size_t
00242 SwitchInterface::maxlenof_long_activations() const
00243 {
00244 return 1;
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254 void
00255 SwitchInterface::set_long_activations(const unsigned int new_long_activations)
00256 {
00257 data->long_activations = new_long_activations;
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267 unsigned int
00268 SwitchInterface::activation_count() const
00269 {
00270 return data->activation_count;
00271 }
00272
00273
00274
00275
00276
00277 size_t
00278 SwitchInterface::maxlenof_activation_count() const
00279 {
00280 return 1;
00281 }
00282
00283
00284
00285
00286
00287
00288
00289
00290 void
00291 SwitchInterface::set_activation_count(const unsigned int new_activation_count)
00292 {
00293 data->activation_count = new_activation_count;
00294 }
00295
00296
00297 Message *
00298 SwitchInterface::create_message(const char *type) const
00299 {
00300 if ( strncmp("SetMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00301 return new SetMessage();
00302 } else if ( strncmp("EnableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00303 return new EnableSwitchMessage();
00304 } else if ( strncmp("DisableSwitchMessage", type, __INTERFACE_MESSAGE_TYPE_SIZE) == 0 ) {
00305 return new DisableSwitchMessage();
00306 } else {
00307 throw UnknownTypeException("The given type '%s' does not match any known "
00308 "message type for this interface type.", type);
00309 }
00310 }
00311
00312
00313
00314
00315
00316 void
00317 SwitchInterface::copy_values(const Interface *other)
00318 {
00319 const SwitchInterface *oi = dynamic_cast<const SwitchInterface *>(other);
00320 if (oi == NULL) {
00321 throw TypeMismatchException("Can only copy values from interface of same type (%s vs. %s)",
00322 type(), other->type());
00323 }
00324 memcpy(data, oi->data, sizeof(SwitchInterface_data_t));
00325 }
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339 SwitchInterface::SetMessage::SetMessage(const bool ini_enabled, const float ini_value) : Message("SetMessage")
00340 {
00341 data_size = sizeof(SetMessage_data_t);
00342 data_ptr = malloc(data_size);
00343 memset(data_ptr, 0, data_size);
00344 data = (SetMessage_data_t *)data_ptr;
00345 data->enabled = ini_enabled;
00346 data->value = ini_value;
00347 add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00348 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
00349 }
00350
00351 SwitchInterface::SetMessage::SetMessage() : Message("SetMessage")
00352 {
00353 data_size = sizeof(SetMessage_data_t);
00354 data_ptr = malloc(data_size);
00355 memset(data_ptr, 0, data_size);
00356 data = (SetMessage_data_t *)data_ptr;
00357 add_fieldinfo(IFT_BOOL, "enabled", 1, &data->enabled);
00358 add_fieldinfo(IFT_FLOAT, "value", 1, &data->value);
00359 }
00360
00361
00362 SwitchInterface::SetMessage::~SetMessage()
00363 {
00364 free(data_ptr);
00365 }
00366
00367
00368
00369
00370 SwitchInterface::SetMessage::SetMessage(const SetMessage *m) : Message("SetMessage")
00371 {
00372 data_size = m->data_size;
00373 data_ptr = malloc(data_size);
00374 memcpy(data_ptr, m->data_ptr, data_size);
00375 data = (SetMessage_data_t *)data_ptr;
00376 }
00377
00378
00379
00380
00381
00382
00383
00384
00385 bool
00386 SwitchInterface::SetMessage::is_enabled() const
00387 {
00388 return data->enabled;
00389 }
00390
00391
00392
00393
00394
00395 size_t
00396 SwitchInterface::SetMessage::maxlenof_enabled() const
00397 {
00398 return 1;
00399 }
00400
00401
00402
00403
00404
00405
00406
00407 void
00408 SwitchInterface::SetMessage::set_enabled(const bool new_enabled)
00409 {
00410 data->enabled = new_enabled;
00411 }
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421 float
00422 SwitchInterface::SetMessage::value() const
00423 {
00424 return data->value;
00425 }
00426
00427
00428
00429
00430
00431 size_t
00432 SwitchInterface::SetMessage::maxlenof_value() const
00433 {
00434 return 1;
00435 }
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445 void
00446 SwitchInterface::SetMessage::set_value(const float new_value)
00447 {
00448 data->value = new_value;
00449 }
00450
00451
00452
00453
00454
00455
00456 Message *
00457 SwitchInterface::SetMessage::clone() const
00458 {
00459 return new SwitchInterface::SetMessage(this);
00460 }
00461
00462
00463
00464
00465
00466
00467
00468
00469 SwitchInterface::EnableSwitchMessage::EnableSwitchMessage() : Message("EnableSwitchMessage")
00470 {
00471 data_size = 0;
00472 data_ptr = NULL;
00473 }
00474
00475
00476 SwitchInterface::EnableSwitchMessage::~EnableSwitchMessage()
00477 {
00478 }
00479
00480
00481
00482
00483 SwitchInterface::EnableSwitchMessage::EnableSwitchMessage(const EnableSwitchMessage *m) : Message("EnableSwitchMessage")
00484 {
00485 data_size = 0;
00486 data_ptr = NULL;
00487 }
00488
00489
00490
00491
00492
00493
00494
00495 Message *
00496 SwitchInterface::EnableSwitchMessage::clone() const
00497 {
00498 return new SwitchInterface::EnableSwitchMessage(this);
00499 }
00500
00501
00502
00503
00504
00505
00506
00507
00508 SwitchInterface::DisableSwitchMessage::DisableSwitchMessage() : Message("DisableSwitchMessage")
00509 {
00510 data_size = 0;
00511 data_ptr = NULL;
00512 }
00513
00514
00515 SwitchInterface::DisableSwitchMessage::~DisableSwitchMessage()
00516 {
00517 }
00518
00519
00520
00521
00522 SwitchInterface::DisableSwitchMessage::DisableSwitchMessage(const DisableSwitchMessage *m) : Message("DisableSwitchMessage")
00523 {
00524 data_size = 0;
00525 data_ptr = NULL;
00526 }
00527
00528
00529
00530
00531
00532
00533
00534 Message *
00535 SwitchInterface::DisableSwitchMessage::clone() const
00536 {
00537 return new SwitchInterface::DisableSwitchMessage(this);
00538 }
00539
00540
00541
00542 bool
00543 SwitchInterface::message_valid(const Message *message) const
00544 {
00545 const SetMessage *m0 = dynamic_cast<const SetMessage *>(message);
00546 if ( m0 != NULL ) {
00547 return true;
00548 }
00549 const EnableSwitchMessage *m1 = dynamic_cast<const EnableSwitchMessage *>(message);
00550 if ( m1 != NULL ) {
00551 return true;
00552 }
00553 const DisableSwitchMessage *m2 = dynamic_cast<const DisableSwitchMessage *>(message);
00554 if ( m2 != NULL ) {
00555 return true;
00556 }
00557 return false;
00558 }
00559
00560
00561 EXPORT_INTERFACE(SwitchInterface)
00562
00563
00564
00565 }