$search
00001 /* 00002 Aseba - an event-based framework for distributed robot control 00003 Copyright (C) 2007--2012: 00004 Stephane Magnenat <stephane at magnenat dot net> 00005 (http://stephane.magnenat.net) 00006 and other contributors, see authors.txt for details 00007 00008 This program is free software: you can redistribute it and/or modify 00009 it under the terms of the GNU Lesser General Public License as published 00010 by the Free Software Foundation, version 3 of the License. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 #ifndef ASEBA_MSG 00022 #define ASEBA_MSG 00023 00024 #include "../common/types.h" 00025 #include "../common/consts.h" 00026 #include "../compiler/compiler.h" 00027 #include <vector> 00028 #include <string> 00029 00030 namespace Dashel 00031 { 00032 class Stream; 00033 } 00034 00035 namespace Aseba 00036 { 00044 00046 class Message 00047 { 00048 public: 00049 uint16 source; 00050 uint16 type; 00051 00052 public: 00053 Message(uint16 type); 00054 virtual ~Message(); 00055 00056 void serialize(Dashel::Stream* stream); 00057 static Message *receive(Dashel::Stream* stream); 00058 void dump(std::wostream &stream) const; 00059 void dumpBuffer(std::wostream &stream) const; 00060 00061 protected: 00062 virtual void serializeSpecific() = 0; 00063 virtual void deserializeSpecific() = 0; 00064 virtual void dumpSpecific(std::wostream &stream) const = 0; 00065 virtual operator const char * () const { return "message super class"; } 00066 00067 protected: 00068 template<typename T> void add(const T& val); 00069 template<typename T> T get(); 00070 00071 protected: 00072 std::vector<uint8> rawData; 00073 size_t readPos; 00074 }; 00075 00077 class UserMessage : public Message 00078 { 00079 public: 00080 typedef std::vector<sint16> DataVector; 00081 DataVector data; 00082 00083 public: 00084 UserMessage() : Message(ASEBA_MESSAGE_INVALID) { } 00085 UserMessage(uint16 type, const DataVector& data = DataVector()) : Message(type), data(data) { } 00086 UserMessage(uint16 type, const sint16* data, const size_t length); 00087 00088 protected: 00089 virtual void serializeSpecific(); 00090 virtual void deserializeSpecific(); 00091 virtual void dumpSpecific(std::wostream &stream) const; 00092 virtual operator const char * () const { return "user message"; } 00093 }; 00094 00096 class BootloaderDescription : public Message 00097 { 00098 public: 00099 uint16 pageSize; 00100 uint16 pagesStart; 00101 uint16 pagesCount; 00102 00103 public: 00104 BootloaderDescription() : Message(ASEBA_MESSAGE_BOOTLOADER_DESCRIPTION) { } 00105 00106 protected: 00107 virtual void serializeSpecific(); 00108 virtual void deserializeSpecific(); 00109 virtual void dumpSpecific(std::wostream &stream) const; 00110 virtual operator const char * () const { return "bootloader description"; } 00111 }; 00112 00114 class BootloaderDataRead : public Message 00115 { 00116 public: 00117 uint8 data[4]; 00118 00119 public: 00120 BootloaderDataRead() : Message(ASEBA_MESSAGE_BOOTLOADER_PAGE_DATA_READ) { } 00121 00122 protected: 00123 virtual void serializeSpecific(); 00124 virtual void deserializeSpecific(); 00125 virtual void dumpSpecific(std::wostream &stream) const; 00126 virtual operator const char * () const { return "bootloader page data read"; } 00127 }; 00128 00130 class BootloaderAck : public Message 00131 { 00132 public: 00133 enum ErrorTypes 00134 { 00135 SUCCESS = 0, 00136 ERROR_INVALID_FRAME_SIZE, 00137 ERROR_PROGRAMMING_FAILED, 00138 ERROR_NOT_PROGRAMMING 00139 }; 00140 00141 public: 00142 uint16 errorCode; 00143 uint16 errorAddress; 00144 00145 public: 00146 BootloaderAck() : Message(ASEBA_MESSAGE_BOOTLOADER_ACK) { } 00147 00148 protected: 00149 virtual void serializeSpecific(); 00150 virtual void deserializeSpecific(); 00151 virtual void dumpSpecific(std::wostream &stream) const; 00152 virtual operator const char * () const { return "bootloader ack"; } 00153 }; 00154 00156 class GetDescription : public Message 00157 { 00158 public: 00159 uint16 version; 00160 00161 public: 00162 GetDescription() : Message(ASEBA_MESSAGE_GET_DESCRIPTION), version(ASEBA_PROTOCOL_VERSION) { } 00163 00164 protected: 00165 virtual void serializeSpecific(); 00166 virtual void deserializeSpecific(); 00167 virtual void dumpSpecific(std::wostream &) const; 00168 virtual operator const char * () const { return "presence"; } 00169 }; 00170 00172 class Description : public Message, public TargetDescription 00173 { 00174 public: 00175 Description() : Message(ASEBA_MESSAGE_DESCRIPTION) { } 00176 00177 protected: 00178 virtual void serializeSpecific(); 00179 virtual void deserializeSpecific(); 00180 virtual void dumpSpecific(std::wostream &stream) const; 00181 virtual operator const char * () const { return "description"; } 00182 }; 00183 00185 class NamedVariableDescription : public Message, public TargetDescription::NamedVariable 00186 { 00187 public: 00188 NamedVariableDescription() : Message(ASEBA_MESSAGE_NAMED_VARIABLE_DESCRIPTION) { } 00189 00190 protected: 00191 virtual void serializeSpecific(); 00192 virtual void deserializeSpecific(); 00193 virtual void dumpSpecific(std::wostream &stream) const; 00194 virtual operator const char * () const { return "named variable"; } 00195 }; 00196 00198 class LocalEventDescription : public Message, public TargetDescription::LocalEvent 00199 { 00200 public: 00201 LocalEventDescription() : Message(ASEBA_MESSAGE_LOCAL_EVENT_DESCRIPTION) { } 00202 00203 protected: 00204 virtual void serializeSpecific(); 00205 virtual void deserializeSpecific(); 00206 virtual void dumpSpecific(std::wostream &stream) const; 00207 virtual operator const char * () const { return "local event"; } 00208 }; 00209 00211 class NativeFunctionDescription : public Message, public TargetDescription::NativeFunction 00212 { 00213 public: 00214 NativeFunctionDescription() : Message(ASEBA_MESSAGE_NATIVE_FUNCTION_DESCRIPTION) { } 00215 00216 protected: 00217 virtual void serializeSpecific(); 00218 virtual void deserializeSpecific(); 00219 virtual void dumpSpecific(std::wostream &stream) const; 00220 virtual operator const char * () const { return "native function description"; } 00221 }; 00222 00224 class Disconnected : public Message 00225 { 00226 public: 00227 Disconnected() : Message(ASEBA_MESSAGE_DISCONNECTED) { } 00228 00229 protected: 00230 virtual void serializeSpecific() {} 00231 virtual void deserializeSpecific() {} 00232 virtual void dumpSpecific(std::wostream &) const {} 00233 virtual operator const char * () const { return "disconnected"; } 00234 }; 00235 00237 class Variables : public Message 00238 { 00239 public: 00240 uint16 start; 00241 std::vector<sint16> variables; 00242 00243 public: 00244 Variables() : Message(ASEBA_MESSAGE_VARIABLES) { } 00245 00246 protected: 00247 virtual void serializeSpecific(); 00248 virtual void deserializeSpecific(); 00249 virtual void dumpSpecific(std::wostream &stream) const; 00250 virtual operator const char * () const { return "variables"; } 00251 }; 00252 00254 class ArrayAccessOutOfBounds : public Message 00255 { 00256 public: 00257 uint16 pc; 00258 uint16 size; 00259 uint16 index; 00260 00261 public: 00262 ArrayAccessOutOfBounds() : Message(ASEBA_MESSAGE_ARRAY_ACCESS_OUT_OF_BOUNDS) { } 00263 00264 protected: 00265 virtual void serializeSpecific(); 00266 virtual void deserializeSpecific(); 00267 virtual void dumpSpecific(std::wostream &stream) const; 00268 virtual operator const char * () const { return "array access out of bounds"; } 00269 }; 00270 00272 class DivisionByZero : public Message 00273 { 00274 public: 00275 uint16 pc; 00276 00277 public: 00278 DivisionByZero() : Message(ASEBA_MESSAGE_DIVISION_BY_ZERO) { } 00279 00280 protected: 00281 virtual void serializeSpecific(); 00282 virtual void deserializeSpecific(); 00283 virtual void dumpSpecific(std::wostream &stream) const; 00284 virtual operator const char * () const { return "division by zero"; } 00285 }; 00286 00288 class EventExecutionKilled : public Message 00289 { 00290 public: 00291 uint16 pc; 00292 00293 public: 00294 EventExecutionKilled() : Message(ASEBA_MESSAGE_EVENT_EXECUTION_KILLED) { } 00295 00296 protected: 00297 virtual void serializeSpecific(); 00298 virtual void deserializeSpecific(); 00299 virtual void dumpSpecific(std::wostream &stream) const; 00300 virtual operator const char * () const { return "event execution killed"; } 00301 }; 00302 00304 class NodeSpecificError : public Message 00305 { 00306 public: 00307 uint16 pc; 00308 std::wstring message; 00309 00310 public: 00311 NodeSpecificError() : Message(ASEBA_MESSAGE_NODE_SPECIFIC_ERROR) { } 00312 00313 protected: 00314 virtual void serializeSpecific(); 00315 virtual void deserializeSpecific(); 00316 virtual void dumpSpecific(std::wostream &) const; 00317 virtual operator const char * () const { return "node specific error"; } 00318 }; 00319 00321 class ExecutionStateChanged : public Message 00322 { 00323 public: 00324 uint16 pc; 00325 uint16 flags; 00326 00327 public: 00328 ExecutionStateChanged() : Message(ASEBA_MESSAGE_EXECUTION_STATE_CHANGED) { } 00329 00330 protected: 00331 virtual void serializeSpecific(); 00332 virtual void deserializeSpecific(); 00333 virtual void dumpSpecific(std::wostream &stream) const; 00334 virtual operator const char * () const { return "execution state"; } 00335 }; 00336 00338 class BreakpointSetResult : public Message 00339 { 00340 public: 00341 uint16 pc; 00342 uint16 success; 00343 00344 public: 00345 BreakpointSetResult() : Message(ASEBA_MESSAGE_BREAKPOINT_SET_RESULT) { } 00346 00347 protected: 00348 virtual void serializeSpecific(); 00349 virtual void deserializeSpecific(); 00350 virtual void dumpSpecific(std::wostream &stream) const; 00351 virtual operator const char * () const { return "breakpoint set result"; } 00352 }; 00353 00355 class CmdMessage : public Message 00356 { 00357 public: 00358 uint16 dest; 00359 00360 public: 00361 CmdMessage(uint16 type, uint16 dest) : Message(type), dest(dest) { } 00362 00363 protected: 00364 virtual void serializeSpecific(); 00365 virtual void deserializeSpecific(); 00366 virtual void dumpSpecific(std::wostream &stream) const; 00367 virtual operator const char * () const { return "command message super class"; } 00368 }; 00369 00371 class BootloaderReset : public CmdMessage 00372 { 00373 public: 00374 BootloaderReset() : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_RESET, ASEBA_DEST_INVALID) { } 00375 BootloaderReset(uint16 dest) : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_RESET, dest) { } 00376 00377 protected: 00378 virtual operator const char * () const { return "bootloader reset"; } 00379 }; 00380 00382 class BootloaderReadPage : public CmdMessage 00383 { 00384 public: 00385 uint16 pageNumber; 00386 00387 public: 00388 BootloaderReadPage() : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_READ_PAGE, ASEBA_DEST_INVALID) { } 00389 BootloaderReadPage(uint16 dest) : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_READ_PAGE, dest) { } 00390 00391 protected: 00392 virtual void serializeSpecific(); 00393 virtual void deserializeSpecific(); 00394 virtual void dumpSpecific(std::wostream &stream) const; 00395 virtual operator const char * () const { return "bootloader read page"; } 00396 }; 00397 00399 class BootloaderWritePage : public CmdMessage 00400 { 00401 public: 00402 uint16 pageNumber; 00403 00404 public: 00405 BootloaderWritePage() : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_WRITE_PAGE, ASEBA_DEST_INVALID) { } 00406 BootloaderWritePage(uint16 dest) : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_WRITE_PAGE, dest) { } 00407 00408 protected: 00409 virtual void serializeSpecific(); 00410 virtual void deserializeSpecific(); 00411 virtual void dumpSpecific(std::wostream &stream) const; 00412 virtual operator const char * () const { return "bootloader write page"; } 00413 }; 00414 00416 class BootloaderPageDataWrite : public CmdMessage 00417 { 00418 public: 00419 uint8 data[4]; 00420 00421 public: 00422 BootloaderPageDataWrite() : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_PAGE_DATA_WRITE, ASEBA_DEST_INVALID) { } 00423 BootloaderPageDataWrite(uint16 dest) : CmdMessage(ASEBA_MESSAGE_BOOTLOADER_PAGE_DATA_WRITE, dest) { } 00424 00425 protected: 00426 virtual void serializeSpecific(); 00427 virtual void deserializeSpecific(); 00428 virtual void dumpSpecific(std::wostream &stream) const; 00429 virtual operator const char * () const { return "bootloader page data write"; } 00430 }; 00431 00433 class SetBytecode : public CmdMessage 00434 { 00435 public: 00436 uint16 start; 00437 std::vector<uint16> bytecode; 00438 00439 public: 00440 SetBytecode() : CmdMessage(ASEBA_MESSAGE_SET_BYTECODE, ASEBA_DEST_INVALID), start(0) { } 00441 SetBytecode(uint16 dest, uint16 start) : CmdMessage(ASEBA_MESSAGE_SET_BYTECODE, dest), start(start) { } 00442 00443 protected: 00444 virtual void serializeSpecific(); 00445 virtual void deserializeSpecific(); 00446 virtual void dumpSpecific(std::wostream &stream) const; 00447 virtual operator const char * () const { return "set bytecode"; } 00448 }; 00449 00451 void sendBytecode(Dashel::Stream* stream, uint16 dest, const std::vector<uint16>& bytecode); 00453 void sendBytecode(std::vector<Message*>& messagesVector, uint16 dest, const std::vector<uint16>& bytecode); 00454 00456 class Reset : public CmdMessage 00457 { 00458 public: 00459 Reset() : CmdMessage(ASEBA_MESSAGE_RESET, ASEBA_DEST_INVALID) { } 00460 Reset(uint16 dest) : CmdMessage(ASEBA_MESSAGE_RESET, dest) { } 00461 00462 protected: 00463 virtual operator const char * () const { return "reset"; } 00464 }; 00465 00467 class Run : public CmdMessage 00468 { 00469 public: 00470 Run() : CmdMessage(ASEBA_MESSAGE_RUN, ASEBA_DEST_INVALID) { } 00471 Run(uint16 dest) : CmdMessage(ASEBA_MESSAGE_RUN, dest) { } 00472 00473 protected: 00474 virtual operator const char * () const { return "run"; } 00475 }; 00476 00478 class Pause : public CmdMessage 00479 { 00480 public: 00481 Pause() : CmdMessage(ASEBA_MESSAGE_PAUSE, ASEBA_DEST_INVALID) { } 00482 Pause(uint16 dest) : CmdMessage(ASEBA_MESSAGE_PAUSE, dest) { } 00483 00484 protected: 00485 virtual operator const char * () const { return "pause"; } 00486 }; 00487 00489 class Step : public CmdMessage 00490 { 00491 public: 00492 Step() : CmdMessage(ASEBA_MESSAGE_STEP, ASEBA_DEST_INVALID) { } 00493 Step(uint16 dest) : CmdMessage(ASEBA_MESSAGE_STEP, dest) { } 00494 00495 protected: 00496 virtual operator const char * () const { return "step"; } 00497 }; 00498 00500 class Stop : public CmdMessage 00501 { 00502 public: 00503 Stop() : CmdMessage(ASEBA_MESSAGE_STOP, ASEBA_DEST_INVALID) { } 00504 Stop(uint16 dest) : CmdMessage(ASEBA_MESSAGE_STOP, dest) { } 00505 00506 protected: 00507 virtual operator const char * () const { return "stop"; } 00508 }; 00509 00511 class GetExecutionState : public CmdMessage 00512 { 00513 public: 00514 GetExecutionState() : CmdMessage(ASEBA_MESSAGE_GET_EXECUTION_STATE, ASEBA_DEST_INVALID) { } 00515 GetExecutionState(uint16 dest) : CmdMessage(ASEBA_MESSAGE_GET_EXECUTION_STATE, dest) { } 00516 00517 protected: 00518 virtual operator const char * () const { return "get execution state"; } 00519 }; 00520 00522 class BreakpointSet : public CmdMessage 00523 { 00524 public: 00525 uint16 pc; 00526 00527 public: 00528 BreakpointSet() : CmdMessage(ASEBA_MESSAGE_BREAKPOINT_SET, ASEBA_DEST_INVALID) { } 00529 BreakpointSet(uint16 dest) : CmdMessage(ASEBA_MESSAGE_BREAKPOINT_SET, dest) { } 00530 00531 protected: 00532 virtual void serializeSpecific(); 00533 virtual void deserializeSpecific(); 00534 virtual void dumpSpecific(std::wostream &stream) const; 00535 virtual operator const char * () const { return "breakpoint set"; } 00536 }; 00537 00539 class BreakpointClear : public CmdMessage 00540 { 00541 public: 00542 uint16 pc; 00543 00544 public: 00545 BreakpointClear() : CmdMessage(ASEBA_MESSAGE_BREAKPOINT_CLEAR, ASEBA_DEST_INVALID) { } 00546 BreakpointClear(uint16 dest) : CmdMessage(ASEBA_MESSAGE_BREAKPOINT_CLEAR, dest) { } 00547 00548 protected: 00549 virtual void serializeSpecific(); 00550 virtual void deserializeSpecific(); 00551 virtual void dumpSpecific(std::wostream &stream) const; 00552 virtual operator const char * () const { return "breakpoint clear"; } 00553 }; 00554 00556 class BreakpointClearAll : public CmdMessage 00557 { 00558 public: 00559 BreakpointClearAll() : CmdMessage(ASEBA_MESSAGE_BREAKPOINT_CLEAR_ALL, ASEBA_DEST_INVALID) { } 00560 BreakpointClearAll(uint16 dest) : CmdMessage(ASEBA_MESSAGE_BREAKPOINT_CLEAR_ALL, dest) { } 00561 00562 protected: 00563 virtual operator const char * () const { return "breakpoint clear all"; } 00564 }; 00565 00567 class GetVariables : public CmdMessage 00568 { 00569 public: 00570 uint16 start; 00571 uint16 length; 00572 00573 public: 00574 GetVariables() : CmdMessage(ASEBA_MESSAGE_GET_VARIABLES, ASEBA_DEST_INVALID) { } 00575 GetVariables(uint16 dest, uint16 start, uint16 length); 00576 00577 protected: 00578 virtual void serializeSpecific(); 00579 virtual void deserializeSpecific(); 00580 virtual void dumpSpecific(std::wostream &stream) const; 00581 virtual operator const char * () const { return "get variables"; } 00582 }; 00583 00585 class SetVariables : public CmdMessage 00586 { 00587 public: 00588 uint16 start; 00589 typedef std::vector<sint16> VariablesVector; 00590 VariablesVector variables; 00591 00592 public: 00593 SetVariables() : CmdMessage(ASEBA_MESSAGE_SET_VARIABLES, ASEBA_DEST_INVALID) { } 00594 SetVariables(uint16 dest, uint16 start, const VariablesVector& variables); 00595 00596 protected: 00597 virtual void serializeSpecific(); 00598 virtual void deserializeSpecific(); 00599 virtual void dumpSpecific(std::wostream &stream) const; 00600 virtual operator const char * () const { return "set variables"; } 00601 }; 00602 00604 class WriteBytecode : public CmdMessage 00605 { 00606 public: 00607 WriteBytecode() : CmdMessage(ASEBA_MESSAGE_WRITE_BYTECODE, ASEBA_DEST_INVALID) { } 00608 WriteBytecode(uint16 dest) : CmdMessage(ASEBA_MESSAGE_WRITE_BYTECODE, dest) { } 00609 00610 protected: 00611 virtual operator const char * () const { return "save bytecode"; } 00612 }; 00613 00615 class Reboot : public CmdMessage 00616 { 00617 public: 00618 Reboot() : CmdMessage(ASEBA_MESSAGE_REBOOT, ASEBA_DEST_INVALID) { } 00619 Reboot(uint16 dest) : CmdMessage(ASEBA_MESSAGE_REBOOT, dest) { } 00620 00621 protected: 00622 virtual operator const char * () const { return "reboot"; } 00623 }; 00624 00628 class Sleep : public CmdMessage 00629 { 00630 public: 00631 Sleep() : CmdMessage(ASEBA_MESSAGE_SUSPEND_TO_RAM, ASEBA_DEST_INVALID) { } 00632 Sleep(uint16 dest) : CmdMessage(ASEBA_MESSAGE_SUSPEND_TO_RAM, dest) { } 00633 00634 protected: 00635 virtual operator const char * () const { return "sleep"; } 00636 }; 00637 00639 } 00640 00641 #endif 00642