Skip to content

Commit 803034f

Browse files
Copilotpetesramek
andcommitted
Rename MessageType::Req to MessageType::Cmd (emit 'C'), accept legacy 'R'
Co-authored-by: petesramek <2333452+petesramek@users.noreply.github.com> Agent-Logs-Url: https://github.com/petesramek/tiny-link/sessions/47ca4beb-1f07-4daf-9f99-6f7d3b0de914
1 parent d56c55c commit 803034f

7 files changed

Lines changed: 68 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2323
## [Unreleased]
2424

2525
### Added
26+
- `src/protocol/MessageType.h`: focused header with `MessageType` enum, `message_type_from_wire()`, `message_type_to_wire()`, and `message_type_to_string()` helpers.
27+
- Compatibility unit tests: `test_message_type_legacy_req_maps_to_cmd` and `test_message_type_cmd_wire_byte_is_C`.
28+
- Migration guide in README for `MessageType::Req``MessageType::Cmd`.
2629
- `CONTRIBUTING.md`, `CODE_OF_CONDUCT.md`, `SECURITY.md`, `CHANGELOG.md`, issue/PR templates, and `CITATION.cff` for OSS best practices.
2730

2831
### Changed
32+
- `MessageType::Req` (wire byte `'R'`) renamed to `MessageType::Cmd` (wire byte `'C'`). Parsers accept both `'R'` (legacy) and `'C'` (new) via `message_type_from_wire()`.
33+
- `src/TinyProtocol.h` now includes `src/protocol/MessageType.h` instead of defining the enum inline.
34+
- Examples updated to use `message_type_to_wire(MessageType::Cmd)`.
2935
- Payload size limit reduced from 240 bytes to 64 bytes to enforce micro-message design intent and eliminate a latent `_rawIdx` overflow bug on large payloads.
3036

3137
---

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,33 @@ Serial.print("Timeouts: "); Serial.println(stats.timeouts);
163163

164164
`src/`: Core protocol logic (`TinyLink.h`, `TinyProtocol.h`).
165165

166+
`src/protocol/`: Focused protocol headers (`MessageType.h`, etc.).
167+
166168
`src/adapters/`: Hardware-specific drivers (Arduino, Posix, Windows, etc.).
167169

168170
`examples/`: Ready-to-run duplex, callback, and health-monitoring demos.
169171

170172
`test/`: Native C++ unit tests using `TinyTestAdapter`.
171173

174+
## 🔄 Migration: `MessageType::Req``MessageType::Cmd`
175+
176+
`MessageType::Req` (wire byte `'R'`) has been renamed to `MessageType::Cmd` (wire byte `'C'`).
177+
New code should use `MessageType::Cmd` or the helper `message_type_to_wire(MessageType::Cmd)`.
178+
179+
Parsers automatically accept both `'R'` and `'C'` via `message_type_from_wire()` for backward
180+
compatibility with older firmware.
181+
182+
```cpp
183+
// New outgoing frames (prefer this):
184+
link.send(message_type_to_wire(MessageType::Cmd), msg); // emits 'C'
185+
186+
// Parsing accepts both legacy 'R' and new 'C':
187+
MessageType mt;
188+
if (message_type_from_wire(link.type(), mt) && mt == MessageType::Cmd) {
189+
// handles frames from old ('R') and new ('C') senders
190+
}
191+
```
192+
172193
## 📜 License
173194
This project is licensed under the MIT License.
174195

examples/Gateway_Handshake_Calback/ESPM3_Bridge.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void setup() {
2323

2424
// 2. Signal to the Tiny88 that we are ready to receive
2525
GatewayStatus msg = { 1, (uint32_t)millis() };
26-
statusLink.send(TYPE_REQ, msg);
26+
statusLink.send(message_type_to_wire(MessageType::Cmd), msg);
2727
}
2828

2929
void loop() {

examples/Gateway_Handshake_Polling/ESPM3_Bridge.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void setup() {
1919
// 3. Signal to Tiny88 that we are stable and ready for data
2020
TinyLink<GatewayStatus, TinyArduinoAdapter> statusLink(adapter);
2121
GatewayStatus msg = { 1, 0 };
22-
statusLink.send(TYPE_REQ, msg);
22+
statusLink.send(message_type_to_wire(MessageType::Cmd), msg);
2323
}
2424

2525
void loop() {

keywords.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ seq KEYWORD2
3434
#######################################
3535
TYPE_DATA LITERAL1
3636
TYPE_DEBUG LITERAL1
37-
TYPE_REQ LITERAL1
37+
TYPE_CMD LITERAL1
3838
TYPE_DONE LITERAL1

src/TinyProtocol.h

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,9 @@
88

99
#include <stdint.h>
1010

11-
namespace tinylink {
11+
#include "protocol/MessageType.h"
1212

13-
// ------------------------------------------------------------
14-
// Message Types (Wire Format)
15-
// ------------------------------------------------------------
16-
17-
/**
18-
* @brief Message type identifiers used in TinyLink frames.
19-
*
20-
* These are 1‑byte ASCII codes placed in pBuf[0].
21-
* Using enum class ensures type safety while allowing easy casting.
22-
*/
23-
enum class MessageType : uint8_t {
24-
Data = 'D', /**< Standard data payload */
25-
Debug = 'g', /**< Debugging / Logging information */
26-
Req = 'R', /**< Request for data / action */
27-
Done = 'K' /**< Acknowledgment of completion */
28-
};
13+
namespace tinylink {
2914

3015
// ------------------------------------------------------------
3116
// High-Level Status Codes

test/test_protocol.cpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,40 @@ void test_crc_endian_sensitivity(void) {
925925
TEST_ASSERT_EQUAL_UINT16(1, link.getStats().crcErrs);
926926
}
927927

928+
/**
929+
* @brief TEST: MessageType wire parsing — legacy 'R' backward compatibility.
930+
*
931+
* Verifies that message_type_from_wire() maps both the legacy wire byte 'R'
932+
* and the new wire byte 'C' to MessageType::Cmd, ensuring parsers accept
933+
* frames generated by older firmware.
934+
*/
935+
void test_message_type_legacy_req_maps_to_cmd(void) {
936+
tinylink::MessageType mt;
937+
938+
// Legacy 'R' (Req) must decode to Cmd
939+
bool ok_r = tinylink::message_type_from_wire('R', mt);
940+
TEST_ASSERT_TRUE_MESSAGE(ok_r, "message_type_from_wire should accept legacy 'R'");
941+
TEST_ASSERT_EQUAL_UINT8(static_cast<uint8_t>(tinylink::MessageType::Cmd),
942+
static_cast<uint8_t>(mt));
943+
944+
// New 'C' (Cmd) must also decode to Cmd
945+
bool ok_c = tinylink::message_type_from_wire('C', mt);
946+
TEST_ASSERT_TRUE_MESSAGE(ok_c, "message_type_from_wire should accept 'C'");
947+
TEST_ASSERT_EQUAL_UINT8(static_cast<uint8_t>(tinylink::MessageType::Cmd),
948+
static_cast<uint8_t>(mt));
949+
}
950+
951+
/**
952+
* @brief TEST: MessageType wire encoding — Cmd emits 'C', not 'R'.
953+
*
954+
* Verifies that message_type_to_wire(MessageType::Cmd) returns 'C' so
955+
* new code always sends the updated wire byte.
956+
*/
957+
void test_message_type_cmd_wire_byte_is_C(void) {
958+
uint8_t wire = tinylink::message_type_to_wire(tinylink::MessageType::Cmd);
959+
TEST_ASSERT_EQUAL_UINT8('C', wire);
960+
}
961+
928962
void register_protocol_tests(void) {
929963
// --- Core Protocol & Asynchronous Events ---
930964
RUN_TEST(test_cobs_loopback); /**< Basic encode/decode/verify pipeline */
@@ -997,5 +1031,6 @@ void register_protocol_tests(void) {
9971031
RUN_TEST(test_primitive_type_support);
9981032
RUN_TEST(test_truncated_cobs_rejection);
9991033
RUN_TEST(test_fletcher_overflow_stability);
1000-
RUN_TEST(test_crc_endian_sensitivity);
1034+
RUN_TEST(test_message_type_legacy_req_maps_to_cmd); /**< Legacy 'R' maps to MessageType::Cmd */
1035+
RUN_TEST(test_message_type_cmd_wire_byte_is_C); /**< MessageType::Cmd emits 'C' on the wire */
10011036
}

0 commit comments

Comments
 (0)