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