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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
|
/*
Aseba - an event-based framework for distributed robot control
Copyright (C) 2007--2016:
Stephane Magnenat <stephane at magnenat dot net>
(http://stephane.magnenat.net)
and other contributors, see authors.txt for details
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "msg.h"
#include "endian.h"
#include "../utils/utils.h"
#include <typeinfo>
#include <iostream>
#include <iomanip>
#include <map>
#include <iterator>
#include <utility>
#include <dashel/dashel.h>
using namespace std;
namespace Aseba
{
using namespace Dashel;
//! Static class that fills a table of known messages types in its constructor
class MessageTypesInitializer
{
public:
//! Constructor, register all known messages types
MessageTypesInitializer()
{
registerMessageType<BootloaderDescription>(ASEBA_MESSAGE_BOOTLOADER_DESCRIPTION);
registerMessageType<BootloaderDataRead>(ASEBA_MESSAGE_BOOTLOADER_PAGE_DATA_READ);
registerMessageType<BootloaderAck>(ASEBA_MESSAGE_BOOTLOADER_ACK);
registerMessageType<ListNodes>(ASEBA_MESSAGE_LIST_NODES);
registerMessageType<NodePresent>(ASEBA_MESSAGE_NODE_PRESENT);
registerMessageType<GetDescription>(ASEBA_MESSAGE_GET_DESCRIPTION);
registerMessageType<GetNodeDescription>(ASEBA_MESSAGE_GET_NODE_DESCRIPTION);
registerMessageType<Description>(ASEBA_MESSAGE_DESCRIPTION);
registerMessageType<NamedVariableDescription>(ASEBA_MESSAGE_NAMED_VARIABLE_DESCRIPTION);
registerMessageType<LocalEventDescription>(ASEBA_MESSAGE_LOCAL_EVENT_DESCRIPTION);
registerMessageType<NativeFunctionDescription>(ASEBA_MESSAGE_NATIVE_FUNCTION_DESCRIPTION);
registerMessageType<Disconnected>(ASEBA_MESSAGE_DISCONNECTED);
registerMessageType<Variables>(ASEBA_MESSAGE_VARIABLES);
registerMessageType<ArrayAccessOutOfBounds>(ASEBA_MESSAGE_ARRAY_ACCESS_OUT_OF_BOUNDS);
registerMessageType<DivisionByZero>(ASEBA_MESSAGE_DIVISION_BY_ZERO);
registerMessageType<EventExecutionKilled>(ASEBA_MESSAGE_EVENT_EXECUTION_KILLED);
registerMessageType<NodeSpecificError>(ASEBA_MESSAGE_NODE_SPECIFIC_ERROR);
registerMessageType<ExecutionStateChanged>(ASEBA_MESSAGE_EXECUTION_STATE_CHANGED);
registerMessageType<BreakpointSetResult>(ASEBA_MESSAGE_BREAKPOINT_SET_RESULT);
registerMessageType<BootloaderReset>(ASEBA_MESSAGE_BOOTLOADER_RESET);
registerMessageType<BootloaderReadPage>(ASEBA_MESSAGE_BOOTLOADER_READ_PAGE);
registerMessageType<BootloaderWritePage>(ASEBA_MESSAGE_BOOTLOADER_WRITE_PAGE);
registerMessageType<BootloaderPageDataWrite>(ASEBA_MESSAGE_BOOTLOADER_PAGE_DATA_WRITE);
registerMessageType<SetBytecode>(ASEBA_MESSAGE_SET_BYTECODE);
registerMessageType<Reset>(ASEBA_MESSAGE_RESET);
registerMessageType<Run>(ASEBA_MESSAGE_RUN);
registerMessageType<Pause>(ASEBA_MESSAGE_PAUSE);
registerMessageType<Step>(ASEBA_MESSAGE_STEP);
registerMessageType<Stop>(ASEBA_MESSAGE_STOP);
registerMessageType<GetExecutionState>(ASEBA_MESSAGE_GET_EXECUTION_STATE);
registerMessageType<BreakpointSet>(ASEBA_MESSAGE_BREAKPOINT_SET);
registerMessageType<BreakpointClear>(ASEBA_MESSAGE_BREAKPOINT_CLEAR);
registerMessageType<BreakpointClearAll>(ASEBA_MESSAGE_BREAKPOINT_CLEAR_ALL);
registerMessageType<GetVariables>(ASEBA_MESSAGE_GET_VARIABLES);
registerMessageType<SetVariables>(ASEBA_MESSAGE_SET_VARIABLES);
registerMessageType<WriteBytecode>(ASEBA_MESSAGE_WRITE_BYTECODE);
registerMessageType<Reboot>(ASEBA_MESSAGE_REBOOT);
registerMessageType<Sleep>(ASEBA_MESSAGE_SUSPEND_TO_RAM);
}
//! Register a message type by storing a pointer to its constructor
template<typename Sub>
void registerMessageType(uint16_t type)
{
messagesTypes[type] = &Creator<Sub>;
}
//! Create an instance of a registered message type
Message *createMessage(uint16_t type)
{
if (messagesTypes.find(type) == messagesTypes.end())
return new UserMessage;
else
return messagesTypes[type]();
}
//! Print the list of registered messages types to stream
void dumpKnownMessagesTypes(wostream &stream) const
{
stream << hex << showbase;
for (const auto& messageTypeKV: messagesTypes)
stream << "\t" << setw(4) << messageTypeKV.first << "\n";
stream << dec << noshowbase;
}
protected:
//! Pointer to constructor of class Message
using CreatorFunc = Message *(*)();
map<uint16_t, CreatorFunc> messagesTypes; //!< table of known messages types
//! Create a new message of type Sub
template<typename Sub>
static Message* Creator()
{
return new Sub();
}
} messageTypesInitializer; //!< static instance, used only to have its constructor called on startup
//
//! Compare all data members
bool operator ==(const TargetDescription::NamedVariable &lhs, const TargetDescription::NamedVariable &rhs)
{
return lhs.size == rhs.size && lhs.name == rhs.name;
}
//! Compare all data members
bool operator ==(const TargetDescription::LocalEvent &lhs, const TargetDescription::LocalEvent &rhs)
{
return lhs.name == rhs.name && lhs.description == rhs.description;
}
//! Compare all data members
bool operator ==(const TargetDescription::NativeFunctionParameter &lhs, const TargetDescription::NativeFunctionParameter &rhs)
{
return lhs.size == rhs.size && lhs.name == rhs.name;
}
//! Compare all data members
bool operator ==(const TargetDescription::NativeFunction &lhs, const TargetDescription::NativeFunction &rhs)
{
return lhs.name == rhs.name && lhs.description == rhs.description && lhs.parameters == rhs.parameters;
}
//
void Message::serialize(Stream* stream) const
{
SerializationBuffer buffer;
serializeSpecific(buffer);
auto len = static_cast<uint16_t>(buffer.rawData.size());
if (len > ASEBA_MAX_EVENT_ARG_SIZE)
{
cerr << "Message::serialize() : fatal error: message size exceeds maximum packet size.\n";
cerr << "message payload size: " << len << ", maximum packet payload size (excluding type): " << ASEBA_MAX_EVENT_ARG_SIZE << ", message type: " << hex << showbase << type << dec << noshowbase;
cerr << endl;
terminate();
}
uint16_t t;
swapEndian(len);
stream->write(&len, 2);
t = swapEndianCopy(source);
stream->write(&t, 2);
t = swapEndianCopy(type);
stream->write(&t, 2);
if (buffer.rawData.size())
stream->write(&buffer.rawData[0], buffer.rawData.size());
}
Message *Message::receive(Stream* stream)
{
// read header
uint16_t len, source, type;
stream->read(&len, 2);
swapEndian(len);
stream->read(&source, 2);
swapEndian(source);
stream->read(&type, 2);
swapEndian(type);
// read content
SerializationBuffer buffer;
buffer.rawData.resize(len);
if (len)
stream->read(&buffer.rawData[0], len);
// deserialize message
return create(source, type, buffer);
}
Message *Message::create(uint16_t source, uint16_t type, SerializationBuffer& buffer)
{
// create message
Message *message = messageTypesInitializer.createMessage(type);
// prepare message
message->source = source;
message->type = type;
// deserialize it
message->deserializeSpecific(buffer);
if (buffer.readPos != buffer.rawData.size())
{
cerr << "Message::create() : fatal error: message not fully deserialized.\n";
cerr << "type: " << type << ", readPos: " << buffer.readPos << ", rawData size: " << buffer.rawData.size() << endl;
buffer.dump(wcerr);
terminate();
}
return message;
}
Message* Message::clone() const
{
// create message
Message *message = messageTypesInitializer.createMessage(type);
// fill headers
message->source = source;
message->type = type;
// copy content through the serialization buffer
SerializationBuffer content;
serializeSpecific(content);
message->deserializeSpecific(content);
return message;
}
void Message::dump(wostream &stream) const
{
stream << hex << setw(4) << setfill(wchar_t('0')) << type << " ";
stream << dec << setfill(wchar_t(' ')) << *this << " from ";
stream << source << " : ";
dumpSpecific(stream);
}
bool operator ==(const Message &lhs, const Message &rhs)
{
return
lhs.source == rhs.source &&
lhs.type == rhs.type
;
}
template<typename T>
void Message::SerializationBuffer::add(const T& val)
{
const size_t pos = rawData.size();
rawData.reserve(pos + sizeof(T));
const T swappedVal(swapEndianCopy(val));
const auto *ptr = reinterpret_cast<const uint8_t *>(&swappedVal);
copy(ptr, ptr + sizeof(T), back_inserter(rawData));
}
template<>
void Message::SerializationBuffer::add(const string& val)
{
if (val.length() > 255)
{
cerr << "Message::SerializationBuffer::add<string>() : fatal error, string length exceeds 255 characters.\n";
cerr << "string size: " << val.length();
cerr << endl;
dump(wcerr);
terminate();
}
add(static_cast<uint8_t>(val.length()));
for (const uint8_t c: val)
add(c);
}
template<typename T>
T Message::SerializationBuffer::get()
{
if (readPos + sizeof(T) > rawData.size())
{
cerr << "Message::SerializationBuffer::get<" << typeid(T).name() << ">() : fatal error: attempt to overread.\n";
cerr << "readPos: " << readPos << ", rawData size: " << rawData.size() << ", element size: " << sizeof(T);
cerr << endl;
dump(wcerr);
terminate();
}
size_t pos = readPos;
readPos += sizeof(T);
T val;
auto *ptr = reinterpret_cast<uint8_t *>(&val);
copy(rawData.cbegin() + pos, rawData.cbegin() + pos + sizeof(T), ptr);
swapEndian(val);
return val;
}
template<>
string Message::SerializationBuffer::get()
{
string s;
size_t len = get<uint8_t>();
s.resize(len);
for (auto& c: s)
c = get<uint8_t>();
return s;
}
void Message::SerializationBuffer::dump(std::wostream &stream) const
{
for (const auto word: rawData)
stream << unsigned(word) << " (" << word << "), ";
stream << endl;
}
//
UserMessage::UserMessage(uint16_t type, const int16_t* data, const size_t length):
Message(type)
{
this->data.resize(length);
copy(data, data+length, this->data.begin());
}
void UserMessage::serializeSpecific(SerializationBuffer& buffer) const
{
for (const auto word: data)
buffer.add(word);
}
void UserMessage::deserializeSpecific(SerializationBuffer& buffer)
{
if (buffer.rawData.size() % 2 != 0)
{
cerr << "UserMessage::deserializeSpecific(SerializationBuffer& buffer) : fatal error: odd size.\n";
cerr << "message size: " << buffer.rawData.size() << ", message type: " << type;
cerr << endl;
terminate();
}
data.resize(buffer.rawData.size() / 2);
for (auto& word: data)
word = buffer.get<int16_t>();
}
void UserMessage::dumpSpecific(wostream &stream) const
{
stream << dec << "user message of size " << data.size() << " : ";
for (const auto word: data)
stream << setw(4) << word << " ";
stream << dec << setfill(wchar_t(' '));
}
bool operator ==(const UserMessage &lhs, const UserMessage &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.data == rhs.data
;
}
//
void CmdMessage::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(dest);
}
void CmdMessage::deserializeSpecific(SerializationBuffer& buffer)
{
dest = buffer.get<uint16_t>();
}
void CmdMessage::dumpSpecific(wostream &stream) const
{
stream << "dest " << dest << " ";
}
bool operator ==(const CmdMessage &lhs, const CmdMessage &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.dest == rhs.dest
;
}
//
void BootloaderDescription::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(pageSize);
buffer.add(pagesStart);
buffer.add(pagesCount);
}
void BootloaderDescription::deserializeSpecific(SerializationBuffer& buffer)
{
pageSize = buffer.get<uint16_t>();
pagesStart = buffer.get<uint16_t>();
pagesCount = buffer.get<uint16_t>();
}
void BootloaderDescription::dumpSpecific(wostream &stream) const
{
stream << pagesCount << " pages of size " << pageSize << " starting at page " << pagesStart;
}
bool operator ==(const BootloaderDescription &lhs, const BootloaderDescription &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.pageSize == rhs.pageSize &&
lhs.pagesStart == rhs.pagesStart &&
lhs.pagesCount == rhs.pagesCount
;
}
//
void BootloaderDataRead::serializeSpecific(SerializationBuffer& buffer) const
{
for (const auto word: data)
buffer.add(word);
}
void BootloaderDataRead::deserializeSpecific(SerializationBuffer& buffer)
{
for (auto& word: data)
word = buffer.get<uint8_t>();
}
void BootloaderDataRead::dumpSpecific(wostream &stream) const
{
stream << hex << setfill(wchar_t('0'));
for (const auto word: data)
stream << setw(2) << unsigned(word) << " ";
stream << dec << setfill(wchar_t(' '));
}
bool operator ==(const BootloaderDataRead &lhs, const BootloaderDataRead &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.data == rhs.data
;
}
//
void BootloaderAck::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(static_cast<uint16_t>(errorCode));
if (errorCode == ErrorCode::PROGRAMMING_FAILED)
buffer.add(errorAddress);
}
void BootloaderAck::deserializeSpecific(SerializationBuffer& buffer)
{
errorCode = static_cast<ErrorCode>(buffer.get<uint16_t>());
if (errorCode == ErrorCode::PROGRAMMING_FAILED)
errorAddress = buffer.get<uint16_t>();
}
void BootloaderAck::dumpSpecific(wostream &stream) const
{
switch (errorCode)
{
case ErrorCode::SUCCESS: stream << "success"; break;
case ErrorCode::INVALID_FRAME_SIZE: stream << "error, invalid frame size"; break;
case ErrorCode::PROGRAMMING_FAILED: stream << "error, programming failed at low address " << hex << showbase << errorAddress; break;
case ErrorCode::NOT_PROGRAMMING: stream << "error, not programming"; break;
default: stream << "unknown error"; break;
}
stream << dec << noshowbase;
}
bool operator ==(const BootloaderAck &lhs, const BootloaderAck &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.errorCode == rhs.errorCode &&
lhs.errorAddress == rhs.errorAddress
;
}
//
void ListNodes::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(version);
}
void ListNodes::deserializeSpecific(SerializationBuffer& buffer)
{
version = buffer.get<uint16_t>();
}
void ListNodes::dumpSpecific(std::wostream &stream) const
{
stream << "protocol version " << version;
}
bool operator ==(const ListNodes &lhs, const ListNodes &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.version == rhs.version
;
}
//
void NodePresent::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(version);
}
void NodePresent::deserializeSpecific(SerializationBuffer& buffer)
{
version = buffer.get<uint16_t>();
}
void NodePresent::dumpSpecific(std::wostream &stream) const
{
stream << "protocol version " << version;
}
bool operator ==(const NodePresent &lhs, const NodePresent &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.version == rhs.version
;
}
//
void GetDescription::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(version);
}
void GetDescription::deserializeSpecific(SerializationBuffer& buffer)
{
version = buffer.get<uint16_t>();
}
void GetDescription::dumpSpecific(std::wostream &stream) const
{
stream << "protocol version " << version;
}
bool operator ==(const GetDescription &lhs, const GetDescription &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.version == rhs.version
;
}
//
void GetNodeDescription::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(version);
}
void GetNodeDescription::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
version = buffer.get<uint16_t>();
}
void GetNodeDescription::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << "protocol version " << version;
}
bool operator ==(const GetNodeDescription &lhs, const GetNodeDescription &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.version == rhs.version
;
}
//
void Description::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(WStringToUTF8(name));
buffer.add(static_cast<uint16_t>(protocolVersion));
buffer.add(static_cast<uint16_t>(bytecodeSize));
buffer.add(static_cast<uint16_t>(stackSize));
buffer.add(static_cast<uint16_t>(variablesSize));
buffer.add(static_cast<uint16_t>(namedVariables.size()));
// named variables are sent separately
buffer.add(static_cast<uint16_t>(localEvents.size()));
// local events are sent separately
buffer.add(static_cast<uint16_t>(nativeFunctions.size()));
// native functions are sent separately
}
void Description::deserializeSpecific(SerializationBuffer& buffer)
{
name = UTF8ToWString(buffer.get<string>());
protocolVersion = buffer.get<uint16_t>();
bytecodeSize = buffer.get<uint16_t>();
stackSize = buffer.get<uint16_t>();
variablesSize = buffer.get<uint16_t>();
namedVariables.resize(buffer.get<uint16_t>());
// named variables are received separately
localEvents.resize(buffer.get<uint16_t>());
// local events are received separately
nativeFunctions.resize(buffer.get<uint16_t>());
// native functions are received separately
}
void Description::dumpSpecific(wostream &stream) const
{
stream << "Node " << name << " using protocol version " << protocolVersion << "\n";
stream << "bytecode: " << bytecodeSize << ", stack: " << stackSize << ", variables: " << variablesSize;
stream << "\nnamed variables: " << namedVariables.size() << "\n";
// named variables are available separately
stream << "\nlocal events: " << localEvents.size() << "\n";
// local events are available separately
stream << "native functions: " << nativeFunctions.size();
// native functions are available separately
}
bool operator ==(const Description &lhs, const Description &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.name == rhs.name &&
lhs.protocolVersion == rhs.protocolVersion &&
lhs.bytecodeSize == rhs.bytecodeSize &&
lhs.stackSize == rhs.stackSize &&
lhs.variablesSize == rhs.variablesSize &&
lhs.namedVariables == rhs.namedVariables &&
lhs.localEvents == rhs.localEvents &&
lhs.nativeFunctions == rhs.nativeFunctions
;
}
//
void NamedVariableDescription::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(static_cast<int16_t>(size));
buffer.add(WStringToUTF8(name));
}
void NamedVariableDescription::deserializeSpecific(SerializationBuffer& buffer)
{
size = buffer.get<uint16_t>();
name = UTF8ToWString(buffer.get<string>());
}
void NamedVariableDescription::dumpSpecific(std::wostream &stream) const
{
stream << name << " of size " << size;
}
bool operator ==(const NamedVariableDescription &lhs, const NamedVariableDescription &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.size == rhs.size &&
lhs.name == rhs.name
;
}
//
void LocalEventDescription::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(WStringToUTF8(name));
buffer.add(WStringToUTF8(description));
}
void LocalEventDescription::deserializeSpecific(SerializationBuffer& buffer)
{
name = UTF8ToWString(buffer.get<string>());
description = UTF8ToWString(buffer.get<string>());
}
void LocalEventDescription::dumpSpecific(std::wostream &stream) const
{
stream << name << " : " << description;
}
bool operator ==(const LocalEventDescription &lhs, const LocalEventDescription &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.name == rhs.name &&
lhs.description == rhs.description
;
}
//
void NativeFunctionDescription::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(WStringToUTF8(name));
buffer.add(WStringToUTF8(description));
buffer.add(static_cast<uint16_t>(parameters.size()));
for (const auto& parameter: parameters)
{
buffer.add(static_cast<int16_t>(parameter.size));
buffer.add(WStringToUTF8(parameter.name));
}
}
void NativeFunctionDescription::deserializeSpecific(SerializationBuffer& buffer)
{
name = UTF8ToWString(buffer.get<string>());
description = UTF8ToWString(buffer.get<string>());
parameters.resize(buffer.get<uint16_t>());
for (auto& parameter: parameters)
{
parameter.size = buffer.get<int16_t>();
parameter.name = UTF8ToWString(buffer.get<string>());
}
}
void NativeFunctionDescription::dumpSpecific(std::wostream &stream) const
{
stream << name << " (";
for (size_t j = 0; j < parameters.size(); j++)
{
stream << parameters[j].name;
stream << "<" << parameters[j].size << ">";
if (j + 1 < parameters.size())
stream << ", ";
}
stream << ") : " << description;
}
bool operator ==(const NativeFunctionDescription &lhs, const NativeFunctionDescription &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.name == rhs.name &&
lhs.description == rhs.description &&
lhs.parameters == rhs.parameters
;
}
//
bool operator ==(const Disconnected &lhs, const Disconnected &rhs)
{
return static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs);
}
//
void Variables::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(start);
for (const auto variable: variables)
buffer.add(variable);
}
void Variables::deserializeSpecific(SerializationBuffer& buffer)
{
start = buffer.get<uint16_t>();
variables.resize((buffer.rawData.size() - buffer.readPos) / 2);
for (auto& variable: variables)
variable = buffer.get<int16_t>();
}
void Variables::dumpSpecific(wostream &stream) const
{
stream << "start " << start << ", variables vector of size " << variables.size();
/*for (size_t i = 0; i < variables.size(); i++)
stream << "\n " << i << " : " << variables[i];
stream << "\n";*/
}
bool operator ==(const Variables &lhs, const Variables &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.start == rhs.start &&
lhs.variables == rhs.variables
;
}
//
void ArrayAccessOutOfBounds::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(pc);
buffer.add(size);
buffer.add(index);
}
void ArrayAccessOutOfBounds::deserializeSpecific(SerializationBuffer& buffer)
{
pc = buffer.get<uint16_t>();
size = buffer.get<uint16_t>();
index = buffer.get<uint16_t>();
}
void ArrayAccessOutOfBounds::dumpSpecific(wostream &stream) const
{
stream << "pc " << pc << ", size " << size << ", index " << index ;
}
bool operator ==(const ArrayAccessOutOfBounds &lhs, const ArrayAccessOutOfBounds &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.pc == rhs.pc &&
lhs.size == rhs.size &&
lhs.index == rhs.index
;
}
//
void DivisionByZero::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(pc);
}
void DivisionByZero::deserializeSpecific(SerializationBuffer& buffer)
{
pc = buffer.get<uint16_t>();
}
void DivisionByZero::dumpSpecific(wostream &stream) const
{
stream << "pc " << pc;
}
bool operator ==(const DivisionByZero &lhs, const DivisionByZero &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.pc == rhs.pc
;
}
//
void EventExecutionKilled::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(pc);
}
void EventExecutionKilled::deserializeSpecific(SerializationBuffer& buffer)
{
pc = buffer.get<uint16_t>();
}
void EventExecutionKilled::dumpSpecific(wostream &stream) const
{
stream << "pc " << pc;
}
bool operator ==(const EventExecutionKilled &lhs, const EventExecutionKilled &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.pc == rhs.pc
;
}
//
void NodeSpecificError::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(pc);
buffer.add(WStringToUTF8(message));
}
void NodeSpecificError::deserializeSpecific(SerializationBuffer& buffer)
{
pc = buffer.get<uint16_t>();
message = UTF8ToWString(buffer.get<std::string>());
}
void NodeSpecificError::dumpSpecific(wostream &stream) const
{
stream << "pc " << pc << " " << message;
}
bool operator ==(const NodeSpecificError &lhs, const NodeSpecificError &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.pc == rhs.pc &&
lhs.message == rhs.message
;
}
//
void ExecutionStateChanged::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(pc);
buffer.add(flags);
}
void ExecutionStateChanged::deserializeSpecific(SerializationBuffer& buffer)
{
pc = buffer.get<uint16_t>();
flags = buffer.get<uint16_t>();
}
void ExecutionStateChanged::dumpSpecific(wostream &stream) const
{
stream << "pc " << pc << ", flags ";
stream << hex << showbase << setw(4) << flags;
stream << dec << noshowbase;
}
bool operator ==(const ExecutionStateChanged &lhs, const ExecutionStateChanged &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.pc == rhs.pc &&
lhs.flags == rhs.flags
;
}
//
void BreakpointSetResult::serializeSpecific(SerializationBuffer& buffer) const
{
buffer.add(pc);
buffer.add(success);
}
void BreakpointSetResult::deserializeSpecific(SerializationBuffer& buffer)
{
pc = buffer.get<uint16_t>();
success = buffer.get<uint16_t>();
}
void BreakpointSetResult::dumpSpecific(wostream &stream) const
{
stream << "pc " << pc << ", success " << success;
}
bool operator ==(const BreakpointSetResult &lhs, const BreakpointSetResult &rhs)
{
return
static_cast<const Message&>(lhs) == static_cast<const Message&>(rhs) &&
lhs.pc == rhs.pc &&
lhs.success == rhs.success
;
}
//
bool operator ==(const BootloaderReset &lhs, const BootloaderReset &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
void BootloaderReadPage::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(pageNumber);
}
void BootloaderReadPage::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
pageNumber = buffer.get<uint16_t>();
}
void BootloaderReadPage::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << "page " << pageNumber;
}
bool operator ==(const BootloaderReadPage &lhs, const BootloaderReadPage &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.pageNumber == rhs.pageNumber
;
}
//
void BootloaderWritePage::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(pageNumber);
}
void BootloaderWritePage::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
pageNumber = buffer.get<uint16_t>();
}
void BootloaderWritePage::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << "page " << pageNumber;
}
bool operator ==(const BootloaderWritePage &lhs, const BootloaderWritePage &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.pageNumber == rhs.pageNumber
;
}
//
void BootloaderPageDataWrite::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
for (const auto word: data)
buffer.add(word);
}
void BootloaderPageDataWrite::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
for (auto& word: data)
word = buffer.get<uint8_t>();
}
void BootloaderPageDataWrite::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << hex << setfill(wchar_t('0'));
for (const auto word: data)
stream << setw(2) << unsigned(word) << " ";
stream << dec << setfill(wchar_t(' '));
}
bool operator ==(const BootloaderPageDataWrite &lhs, const BootloaderPageDataWrite &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.data == rhs.data
;
}
//
void SetBytecode::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(start);
for (const auto word: bytecode)
buffer.add(word);
}
void SetBytecode::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
start = buffer.get<uint16_t>();
bytecode.resize((buffer.rawData.size() - buffer.readPos) / 2);
for (auto& word: bytecode)
word = buffer.get<uint16_t>();
}
void SetBytecode::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << bytecode.size() << " words of bytecode of starting at " << start;
}
bool operator ==(const SetBytecode &lhs, const SetBytecode &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.start == rhs.start &&
lhs.bytecode == rhs.bytecode
;
}
void sendBytecode(Dashel::Stream* stream, uint16_t dest, const std::vector<uint16_t>& bytecode)
{
const unsigned bytecodePayloadSize = ASEBA_MAX_EVENT_ARG_COUNT-2;
unsigned bytecodeStart = 0;
unsigned bytecodeCount = bytecode.size();
while (bytecodeCount > bytecodePayloadSize)
{
SetBytecode setBytecodeMessage(dest, bytecodeStart);
setBytecodeMessage.bytecode.resize(bytecodePayloadSize);
copy(bytecode.begin()+bytecodeStart, bytecode.begin()+bytecodeStart+bytecodePayloadSize, setBytecodeMessage.bytecode.begin());
setBytecodeMessage.serialize(stream);
bytecodeStart += bytecodePayloadSize;
bytecodeCount -= bytecodePayloadSize;
}
{
SetBytecode setBytecodeMessage(dest, bytecodeStart);
setBytecodeMessage.bytecode.resize(bytecodeCount);
copy(bytecode.begin()+bytecodeStart, bytecode.end(), setBytecodeMessage.bytecode.begin());
setBytecodeMessage.serialize(stream);
}
}
void sendBytecode(std::vector<std::unique_ptr<Message>>& messagesVector, uint16_t dest, const std::vector<uint16_t>& bytecode)
{
const unsigned bytecodePayloadSize = ASEBA_MAX_EVENT_ARG_COUNT-2;
unsigned bytecodeStart = 0;
unsigned bytecodeCount = bytecode.size();
while (bytecodeCount > bytecodePayloadSize)
{
auto setBytecodeMessage = make_unique<SetBytecode>(dest, bytecodeStart);
setBytecodeMessage->bytecode.resize(bytecodePayloadSize);
copy(bytecode.begin()+bytecodeStart, bytecode.begin()+bytecodeStart+bytecodePayloadSize, setBytecodeMessage->bytecode.begin());
messagesVector.push_back(move(setBytecodeMessage));
bytecodeStart += bytecodePayloadSize;
bytecodeCount -= bytecodePayloadSize;
}
{
auto setBytecodeMessage = make_unique<SetBytecode>(dest, bytecodeStart);
setBytecodeMessage->bytecode.resize(bytecodeCount);
copy(bytecode.begin()+bytecodeStart, bytecode.end(), setBytecodeMessage->bytecode.begin());
messagesVector.push_back(move(setBytecodeMessage));
}
}
//
bool operator ==(const Reset &lhs, const Reset &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
bool operator ==(const Run &lhs, const Run &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
bool operator ==(const Pause &lhs, const Pause &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
bool operator ==(const Step &lhs, const Step &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
bool operator ==(const Stop &lhs, const Stop &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
bool operator ==(const GetExecutionState &lhs, const GetExecutionState &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
void BreakpointSet::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(pc);
}
void BreakpointSet::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
pc = buffer.get<uint16_t>();
}
void BreakpointSet::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << "pc " << pc;
}
bool operator ==(const BreakpointSet &lhs, const BreakpointSet &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.pc == rhs.pc
;
}
//
void BreakpointClear::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(pc);
}
void BreakpointClear::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
pc = buffer.get<uint16_t>();
}
void BreakpointClear::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << "pc " << pc;
}
bool operator ==(const BreakpointClear &lhs, const BreakpointClear &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.pc == rhs.pc
;
}
//
bool operator ==(const BreakpointClearAll &lhs, const BreakpointClearAll &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
GetVariables::GetVariables(uint16_t dest, uint16_t start, uint16_t length) :
CmdMessage(ASEBA_MESSAGE_GET_VARIABLES, dest),
start(start),
length(length)
{
}
void GetVariables::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(start);
buffer.add(length);
}
void GetVariables::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
start = buffer.get<uint16_t>();
length = buffer.get<uint16_t>();
}
void GetVariables::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << "start " << start << ", length " << length;
}
bool operator ==(const GetVariables &lhs, const GetVariables &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.start == rhs.start &&
lhs.length == rhs.length
;
}
//
SetVariables::SetVariables(uint16_t dest, uint16_t start, VariablesDataVector variables) :
CmdMessage(ASEBA_MESSAGE_SET_VARIABLES, dest),
start(start),
variables(std::move(variables))
{
}
void SetVariables::serializeSpecific(SerializationBuffer& buffer) const
{
CmdMessage::serializeSpecific(buffer);
buffer.add(start);
for (const auto variable: variables)
buffer.add(variable);
}
void SetVariables::deserializeSpecific(SerializationBuffer& buffer)
{
CmdMessage::deserializeSpecific(buffer);
start = buffer.get<uint16_t>();
variables.resize((buffer.rawData.size() - buffer.readPos) / 2);
for (auto& variable: variables)
variable = buffer.get<int16_t>();
}
void SetVariables::dumpSpecific(wostream &stream) const
{
CmdMessage::dumpSpecific(stream);
stream << "start " << start << ", variables vector of size " << variables.size();
}
bool operator ==(const SetVariables &lhs, const SetVariables &rhs)
{
return
static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs) &&
lhs.start == rhs.start &&
lhs.variables == rhs.variables
;
}
//
bool operator ==(const WriteBytecode &lhs, const WriteBytecode &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
bool operator ==(const Reboot &lhs, const Reboot &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
//
bool operator ==(const Sleep &lhs, const Sleep &rhs)
{
return static_cast<const CmdMessage&>(lhs) == static_cast<const CmdMessage&>(rhs);
}
} // namespace Aseba
|