|
| 1 | +#ifndef TINYLINK_MESSAGETYPE_H |
| 2 | +#define TINYLINK_MESSAGETYPE_H |
| 3 | + |
| 4 | +#include <stdint.h> |
| 5 | + |
| 6 | +namespace tinylink { |
| 7 | + |
| 8 | + // Wire message type identifiers used in TinyLink frames. |
| 9 | + // Stored as single bytes on the wire (ASCII codes). |
| 10 | + enum class MessageType : uint8_t { |
| 11 | + Data = 'D', |
| 12 | + Debug = 'g', |
| 13 | + Cmd = 'C', // renamed from Req -> Cmd; new outgoing frames use 'C' |
| 14 | + Done = 'K' |
| 15 | + }; |
| 16 | + |
| 17 | + // Convert a wire byte to MessageType. Returns true on success. |
| 18 | + // Accept both legacy 'R' (Req) and new 'C' (Cmd) for migration. |
| 19 | + inline bool message_type_from_wire(uint8_t b, MessageType &out) { |
| 20 | + switch (b) { |
| 21 | + case static_cast<uint8_t>(MessageType::Data): out = MessageType::Data; return true; |
| 22 | + case static_cast<uint8_t>(MessageType::Debug): out = MessageType::Debug; return true; |
| 23 | + case static_cast<uint8_t>(MessageType::Cmd): out = MessageType::Cmd; return true; |
| 24 | + case 'R': /* legacy: Req */ out = MessageType::Cmd; return true; |
| 25 | + case static_cast<uint8_t>(MessageType::Done): out = MessageType::Done; return true; |
| 26 | + default: return false; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + // Small helper to get the wire byte for a MessageType. |
| 31 | + // Emits the new 'C' value for commands. |
| 32 | + inline uint8_t message_type_to_wire(MessageType t) { |
| 33 | + return static_cast<uint8_t>(t); |
| 34 | + } |
| 35 | + |
| 36 | + // to-string helper for diagnostics. |
| 37 | + inline const char* message_type_to_string(MessageType t) { |
| 38 | + switch (t) { |
| 39 | + case MessageType::Data: return "Data"; |
| 40 | + case MessageType::Debug: return "Debug"; |
| 41 | + case MessageType::Cmd: return "Cmd"; |
| 42 | + case MessageType::Done: return "Done"; |
| 43 | + default: return "Unknown"; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | +} // namespace tinylink |
| 48 | + |
| 49 | +#endif // TINYLINK_MESSAGETYPE_H |
0 commit comments