Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

## Purpose
- This repository is a small C11 library for talking to Mitsubishi Outlander PHEV remote WiFi systems.
- Core code lives in `src/`, public headers in `include/`, and Unity tests in `test/`.
- The build is CMake-based; `cJSON` and Unity are fetched via FetchContent. `msg-core` sources are vendored directly in `src/`.
- Core code lives in `src/phev/` and `src/msg/`, public headers in `include/phev/` and `include/msg/`, and greatest tests in `test/`.
- The build is CMake-based; `cJSON` and greatest are fetched via FetchContent. `msg-core` sources are vendored in `src/msg/`.

## Repository Layout
- `src/`: library implementation files — both phev-specific (`phev.c`, `phev_core.c`, `phev_service.c`, `phev_pipe.c`, etc.) and vendored msg-core (`msg_core.c`, `msg_pipe.c`, `msg_utils.c`, etc.).
- `include/`: installed public headers such as `phev.h`, `phev_core.h`, `phev_service.h`, `msg_core.h`, `msg_pipe.h`.
- `test/`: Unity-based test sources, per-suite `run_*.c` runners, and `test/CMakeLists.txt`.
- `CMakeLists.txt`: root build definition for the static library and optional tests.
- `src/msg/`: vendored msg-core library sources (`msg_core.c`, `msg_pipe.c`, `msg_utils.c`, etc.).
- `src/phev/`: phev-specific library sources (`phev.c`, `phev_core.c`, `phev_service.c`, `phev_pipe.c`, etc.).
- `include/msg/`: msg-core public headers (`msg_core.h`, `msg_pipe.h`, `msg_utils.h`, `logger.h`, etc.).
- `include/phev/`: phev public headers (`phev.h`, `phev_core.h`, `phev_service.h`, `phev_pipe.h`, etc.).
- `test/`: greatest-based test sources and `test/CMakeLists.txt`.
- `CMakeLists.txt`: root build definition for two static libraries (`msg_core` + `phev`) and optional tests.
- `CMakePresets.json`: standardized build presets (dev, release, ci).
- `Dockerfile`: reproducible build that uses the `ci` preset and runs `ctest`.
- `.github/workflows/dockerimage.yml`: GitHub Actions CI that builds and tests natively with cmake presets.
Expand All @@ -19,7 +21,7 @@
## Dependencies
- Build/runtime: `cJSON` (fetched via FetchContent, v1.7.18).
- Test: greatest (fetched via FetchContent, v1.5.0).
- msg-core sources are vendored directly in `src/` and `include/` (originally from github.com/papawattu/msg-core).
- msg-core sources are vendored in `src/msg/` and `include/msg/` (originally from github.com/papawattu/msg-core).

## Build Commands

Expand Down Expand Up @@ -69,20 +71,21 @@ docker build -t phevcore . && docker run --rm phevcore
```

## Test Suites
CTest registers 6 per-suite executables, each with its own `run_*.c` runner:
- `test_phev_core` (52 tests)
- `test_phev_pipe` (18 tests)
- `test_phev_service` (56 tests)
CTest registers 7 suites, each a standalone greatest executable:
- `test_phev_core` (82 tests)
- `test_phev_pipe` (31 tests)
- `test_phev_service` (70 tests)
- `test_phev_model` (8 tests)
- `test_phev` (2 tests)
- `test_phev_register` (14 tests)
- `test_phev_config` (12 tests)

A legacy monolithic `test_runner.c` also exists but is not wired into the CMake build.
Total: 219 tests. 35 are SKIPped (pre-existing bugs from previously-unwired test functions).

## Single-Test Guidance
- There is no built-in per-test-name CLI filter in the current Unity runners.
- If you need one specific Unity case, the least invasive approach is to temporarily comment out unrelated `RUN_TEST(...)` lines in the relevant `test/run_*.c` file, build, run, then restore.
- If you need repeatable focused execution, add a dedicated temporary runner in `test/` rather than reshaping production code.
- greatest supports `-t <test_name>` CLI filtering, e.g. `./build/test/test_phev_core -t test_phev_core_simpleRequestMessage`.
- You can also run a single suite within a multi-suite executable using `-s <suite_name>`.
- For listing all tests: `./build/test/test_phev_core -l`.

## Lint / Static Analysis
- A `.clang-format` file documents the project's formatting conventions (4-space indent, Allman braces, middle pointer alignment).
Expand All @@ -102,13 +105,14 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev

## Language and Build Conventions
- Target language is C11: `set(CMAKE_C_STANDARD 11)`.
- The main artifact is a static library named `phev`.
- Two static library targets: `msg_core` (vendored messaging framework) and `phev` (links `msg_core` + `cjson`).
- Dead transport backends (`msg_gcp_mqtt`, `msg_mqtt_paho`) are gated behind `BUILD_TRANSPORT_BACKENDS` (OFF by default).
- Tests are only added when `BUILD_TESTS` is enabled.
- Public headers are intended for installation from `include/`.
- Public headers are installed from `include/msg/` and `include/phev/`.

## Import / Include Style
- Put standard library headers first, then project headers, then external library headers if needed.
- Use quoted includes for project headers, for example `#include "phev_core.h"`.
- Use quoted includes with subdirectory prefixes for project headers: `#include "phev/phev_core.h"`, `#include "msg/msg_core.h"`.
- Use angle brackets for standard headers such as `<stdlib.h>` and `<stdint.h>`.
- Keep include blocks compact; do not alphabetize aggressively if existing local grouping is clearer.
- Many headers define `_GNU_SOURCE` guards at the top; preserve that pattern where GNU extensions are required.
Expand All @@ -127,7 +131,7 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev
- Types use `_t` suffixes, for example `phevCtx_t`, `phevMessage_t`, `phevServiceCtx_t`.
- Constants and protocol/register macros use upper snake case, for example `KO_WF_H_LAMP_CONT_SP`.
- Local log tags are usually `const static char *TAG` or `APP_TAG`.
- Test functions use `test_...` naming and are invoked explicitly with `RUN_TEST(...)`.
- Test functions use `test_...` naming and are registered via greatest `TEST` macros and `SUITE` wiring.

## Types and Data Handling
- Use fixed-width integer types from `<stdint.h>` for protocol data.
Expand Down Expand Up @@ -157,10 +161,11 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev
- Prefer repository logging macros over raw `printf`, except in existing buffer-dump helpers or tests.

## Testing Conventions
- Tests are integration-heavy and often exercise real message encoding/decoding paths.
- Each test suite has a dedicated `test/run_*.c` runner that `#include`s the corresponding `test_*.c` file and wires `RUN_TEST(...)` entries.
- Add new test functions to the relevant `test/test_*.c` file and wire them into `RUN_TEST(...)` in the matching `test/run_*.c` runner.
- Follow existing assertion style with Unity macros such as `TEST_ASSERT_EQUAL`, `TEST_ASSERT_NOT_NULL`, and `TEST_ASSERT_EQUAL_HEX8_ARRAY`.
- Tests use the greatest framework with `TEST`, `SUITE`, and `GREATEST_MAIN_DEFS()`/`GREATEST_MAIN_BEGIN()`/`GREATEST_MAIN_END()` macros.
- Each test suite is a standalone `.c` file in `test/` with its own `main()`.
- Add new test functions to the relevant `test/test_*.c` file and wire them into the appropriate `SUITE()` callback, then register the suite in `main()` via `RUN_SUITE()`.
- Follow existing assertion style with greatest macros such as `ASSERT_EQ`, `ASSERT`, `ASSERT_EQ_FMT`, and `ASSERT_MEM_EQ`.
- Tests are wired into CMake via the `phev_add_test(name source)` helper in `test/CMakeLists.txt`.

## Editing Guidance For Agents
- Keep changes narrow and consistent with surrounding style; this is not a heavily normalized codebase.
Expand All @@ -172,5 +177,4 @@ cmake --preset dev && cmake --build --preset dev && ctest --preset dev
## Known Quirks To Respect
- Some code intentionally uses duplicated patterns, manual memory management, and verbose logging; preserve behavior first, elegance second.
- There are existing rough edges and probable bugs in the codebase; avoid opportunistic rewrites unless required for the task at hand.
- 75 of 220 defined test functions are not wired into any runner (34%). See `TODO.md` for the Phase 2 plan to address this.
- `test_phev_config.c` (12 tests) and `test_phev_controller.c` (5 active tests, needs CMock) are orphaned with no runner.
- 35 of 219 test functions are SKIPped due to pre-existing bugs (never wired in the old Unity runners). See `TODO.md` for details.
110 changes: 72 additions & 38 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,48 @@ set(CJSON_BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)

FetchContent_MakeAvailable(cjson)

# ---------- library ----------
# ---------- msg_core library (vendored from github.com/papawattu/msg-core) ----------
add_library(msg_core STATIC
src/msg/msg_core.c
src/msg/msg_mqtt.c
src/msg/msg_pipe.c
src/msg/msg_pipe_splitter.c
src/msg/msg_tcpip.c
src/msg/msg_utils.c
)

target_include_directories(msg_core
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)

if(WIN32)
target_link_libraries(msg_core PUBLIC mswsock advapi32 ws2_32)
endif()

# ---------- optional dead transport backends ----------
option(BUILD_TRANSPORT_BACKENDS "Build msg_gcp_mqtt and msg_mqtt_paho (requires external deps)" OFF)

if(BUILD_TRANSPORT_BACKENDS)
target_sources(msg_core PRIVATE
src/msg/msg_gcp_mqtt.c
src/msg/msg_mqtt_paho.c
)
endif()

# ---------- phev library ----------
add_library(phev STATIC
# msg-core sources (merged from github.com/papawattu/msg-core)
src/msg_core.c
src/msg_mqtt.c
src/msg_pipe.c
src/msg_pipe_splitter.c
src/msg_tcpip.c
src/msg_utils.c
# phev sources
src/phev.c
src/phev_config.c
src/phev_core.c
src/phev_model.c
src/phev_pipe.c
src/phev_register.c
src/phev_service.c
src/phev_tcpip.c
src/phev/phev.c
src/phev/phev_config.c
src/phev/phev_core.c
src/phev/phev_model.c
src/phev/phev_pipe.c
src/phev/phev_register.c
src/phev/phev_service.c
src/phev/phev_tcpip.c
)

target_include_directories(phev
Expand All @@ -53,13 +77,9 @@ target_include_directories(phev
)

target_link_libraries(phev
PUBLIC cjson
PUBLIC msg_core cjson
)

if(WIN32)
target_link_libraries(phev PUBLIC mswsock advapi32 ws2_32)
endif()

# ---------- tests ----------
option(BUILD_TESTS "Build the test binaries" OFF)

Expand All @@ -76,25 +96,39 @@ endif()

# ---------- install ----------
install(
TARGETS phev
TARGETS msg_core phev
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib
)

install(FILES
include/phev.h
include/phev_config.h
include/phev_core.h
include/phev_model.h
include/phev_pipe.h
include/phev_register.h
include/phev_service.h
include/phev_tcpip.h
include/msg_core.h
include/msg_pipe.h
include/msg_pipe_splitter.h
include/msg_tcpip.h
include/msg_utils.h
include/logger.h
DESTINATION include
include/phev/phev.h
include/phev/phev_config.h
include/phev/phev_core.h
include/phev/phev_model.h
include/phev/phev_pipe.h
include/phev/phev_register.h
include/phev/phev_service.h
include/phev/phev_tcpip.h
DESTINATION include/phev
)

install(FILES
include/msg/msg_core.h
include/msg/msg_pipe.h
include/msg/msg_pipe_splitter.h
include/msg/msg_tcpip.h
include/msg/msg_utils.h
include/msg/msg_mqtt.h
include/msg/logger.h
DESTINATION include/msg
)

if(BUILD_TRANSPORT_BACKENDS)
install(FILES
include/msg/msg_gcp_mqtt.h
include/msg/msg_mqtt_paho.h
include/msg/config.h
DESTINATION include/msg
)
endif()
16 changes: 9 additions & 7 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@ project layout. Each phase is one PR.

## Phase 3 — Directory restructure

- [ ] Move `src/` into `src/msg/` + `src/phev/`
- [ ] Move `include/` into `include/msg/` + `include/phev/`
- [ ] Update all `#include` directives
- [ ] Split CMake into two targets: `msg_core` (static) + `phev` (static, links `msg_core`)
- [ ] Move tests into `tests/msg/` + `tests/phev/`
- [ ] Gate dead transport backends (`msg_gcp_mqtt`, `msg_mqtt_paho`) behind CMake options
- [ ] Update Dockerfile, install rules, and CI
- [x] Move `src/` into `src/msg/` + `src/phev/`
- [x] Move `include/` into `include/msg/` + `include/phev/`
- [x] Update all `#include` directives (79 directives across 24 files)
- [x] Split CMake into two targets: `msg_core` (static) + `phev` (static, links `msg_core`)
- [x] Gate dead transport backends (`msg_gcp_mqtt`, `msg_mqtt_paho`) behind `BUILD_TRANSPORT_BACKENDS` option
- [x] Update install rules for new directory layout
- [x] Tests remain in `test/` (no msg-layer tests exist; all 7 suites test phev)
- [x] Dockerfile and CI already use presets — no changes needed
- [x] Update `AGENTS.md` to reflect restructure
File renamed without changes.
2 changes: 1 addition & 1 deletion include/logger.h → include/msg/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdint.h>
#include <stdio.h>
#include <ctype.h>
#include "msg_core.h"
#include "msg/msg_core.h"


#define LOG_NONE 0
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions include/msg_gcp_mqtt.h → include/msg/msg_gcp_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ typedef void *QueueHandle_t;
//#ifndef TEST
//#include "mqtt_client.h"
//#endif
#include "msg_core.h"
#include "msg_mqtt.h"
#include "config.h"
#include "msg/msg_core.h"
#include "msg/msg_mqtt.h"
#include "msg/config.h"

#define MSG_GCP_OK 0
#define MSG_GCP_FAIL -1
Expand Down
2 changes: 1 addition & 1 deletion include/msg_mqtt.h → include/msg/msg_mqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdbool.h>

#include "msg_core.h"
#include "msg/msg_core.h"

typedef void * mqtt_client_handle_t;
typedef char * topic_t;
Expand Down
2 changes: 1 addition & 1 deletion include/msg_mqtt_paho.h → include/msg/msg_mqtt_paho.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _MSG_MQTT_PAHO_H_
#define _MSG_MQTT_PAHO_H_
#include "MQTTClient.h"
#include "msg_core.h"
#include "msg/msg_core.h"


#define DEFAULT_INCOMING_TOPIC "incoming_topic"
Expand Down
2 changes: 1 addition & 1 deletion include/msg_pipe.h → include/msg/msg_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <stdlib.h>
#include <stdbool.h>
#include "msg_core.h"
#include "msg/msg_core.h"

#define MAX_TRANSFORMERS 16
#define MAX_MESSAGES 100
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _MSG_PIPE_SPLITTER_H_
#define _MSG_PIPE_SPLITTER_H_
#include "msg_core.h"
#include "msg_pipe.h"
#include "msg/msg_core.h"
#include "msg/msg_pipe.h"

messageBundle_t * msg_pipe_splitter(msg_pipe_ctx_t *ctx, msg_pipe_chain_t * chain, message_t *message);
message_t * msg_pipe_splitter_aggregrator(messageBundle_t * messages);
Expand Down
2 changes: 1 addition & 1 deletion include/msg_tcpip.h → include/msg/msg_tcpip.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _MSG_TCPIP_H_
#define _MSG_TCPIP_H_

#include "msg_core.h"
#include "msg/msg_core.h"

#define TCPIP_CLIENT_READ_BUF_SIZE 2048
//#define OK 0
Expand Down
4 changes: 2 additions & 2 deletions include/msg_utils.h → include/msg/msg_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "msg_core.h"
#include "logger.h"
#include "msg/msg_core.h"
#include "msg/logger.h"
#ifdef __XTENSA__
#include "esp_system.h"
#endif
Expand Down
6 changes: 3 additions & 3 deletions include/phev.h → include/phev/phev.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

#include <stdint.h>
#include <stdbool.h>
#include "msg_core.h"
#include "phev_service.h"
#include "phev_pipe.h"
#include "msg/msg_core.h"
#include "phev/phev_service.h"
#include "phev/phev_pipe.h"

#define KO_WF_CONNECT_INFO_GS_SP 1
#define KO_WF_REG_DISP_SP 16
Expand Down
2 changes: 1 addition & 1 deletion include/phev_config.h → include/phev/phev_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include "phev_core.h"
#include "phev/phev_core.h"
#include "cJSON.h"
#ifndef BUILD_NUMBER
#define BUILD_NUMBER 1
Expand Down
2 changes: 1 addition & 1 deletion include/phev_core.h → include/phev/phev_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "msg_core.h"
#include "msg/msg_core.h"
#define PHEV_OK 0

#define REQUEST_TYPE 0
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions include/phev_pipe.h → include/phev/phev_pipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include <time.h>
#include <stdint.h>
#include <stdbool.h>
#include "msg_core.h"
#include "msg_pipe.h"
#include "phev_core.h"
#include "msg/msg_core.h"
#include "msg/msg_pipe.h"
#include "phev/phev_core.h"
#define PHEV_PIPE_MAX_EVENT_HANDLERS 10
#define PHEV_PIPE_MAX_UPDATE_CALLBACKS 10
#ifndef PHEV_CONNECT_WAIT_TIME
Expand Down
Loading
Loading