forked from m5stack/M5Tab5-UserDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
feat(rooms): add integration rooms provider #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| #include "rooms_provider.h" | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| static room_entity_t ENT_BAKERY_MAIN = { | ||
| .entity_id = "light.bakery_main", | ||
| .kind = ROOM_ENTITY_LIGHT, | ||
| .available = true, | ||
| .on = false, | ||
| .value = -1, | ||
| }; | ||
|
|
||
| static room_entity_t ENT_BEDROOM_MAIN = { | ||
| .entity_id = "light.bedroom_main", | ||
| .kind = ROOM_ENTITY_LIGHT, | ||
| .available = true, | ||
| .on = true, | ||
| .value = 75, | ||
| }; | ||
|
|
||
| static room_entity_t ENT_LIVING_MAIN = { | ||
| .entity_id = "light.living_main", | ||
| .kind = ROOM_ENTITY_LIGHT, | ||
| .available = true, | ||
| .on = false, | ||
| .value = -1, | ||
| }; | ||
|
|
||
| static room_entity_t* ROOM0_ENTS[] = {&ENT_BAKERY_MAIN}; | ||
| static room_entity_t* ROOM1_ENTS[] = {&ENT_BEDROOM_MAIN}; | ||
| static room_entity_t* ROOM2_ENTS[] = {&ENT_LIVING_MAIN}; | ||
|
|
||
| static room_t ROOMS[] = { | ||
| { | ||
| .room_id = "bakery", | ||
| .name = "Bakery", | ||
| .entities = (room_entity_t**)ROOM0_ENTS, | ||
| .entity_count = sizeof(ROOM0_ENTS) / sizeof(ROOM0_ENTS[0]), | ||
| .temp_c = 24, | ||
| .humidity = 48, | ||
| }, | ||
| { | ||
| .room_id = "bedroom", | ||
| .name = "Bedroom", | ||
| .entities = (room_entity_t**)ROOM1_ENTS, | ||
| .entity_count = sizeof(ROOM1_ENTS) / sizeof(ROOM1_ENTS[0]), | ||
| .temp_c = 23, | ||
| .humidity = 50, | ||
| }, | ||
| { | ||
| .room_id = "living", | ||
| .name = "Living Room", | ||
| .entities = (room_entity_t**)ROOM2_ENTS, | ||
| .entity_count = sizeof(ROOM2_ENTS) / sizeof(ROOM2_ENTS[0]), | ||
| .temp_c = 25, | ||
| .humidity = 45, | ||
| }, | ||
| }; | ||
|
|
||
| static rooms_state_t DEFAULT_STATE = { | ||
| .rooms = ROOMS, | ||
| .room_count = sizeof(ROOMS) / sizeof(ROOMS[0]), | ||
| }; | ||
|
|
||
| static const rooms_state_t* s_current_state = &DEFAULT_STATE; | ||
|
|
||
| const rooms_state_t* rooms_provider_get_state(void) | ||
| { | ||
| return s_current_state; | ||
| } | ||
|
|
||
| void rooms_provider_set_state(const rooms_state_t* state) | ||
| { | ||
| s_current_state = state; | ||
| } | ||
|
|
||
| void rooms_provider_reset_state(void) | ||
| { | ||
| s_current_state = &DEFAULT_STATE; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD | ||
| * | ||
| * SPDX-License-Identifier: MIT | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include "../ui/pages/ui_rooms_model.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" | ||
| { | ||
| #endif | ||
|
|
||
| /** | ||
| * @brief Retrieve the current rooms state snapshot. | ||
| * | ||
| * The pointer remains owned by the provider. Callers should treat the | ||
| * returned data as read-only and copy it if a longer lifetime is needed. | ||
| */ | ||
| const rooms_state_t* rooms_provider_get_state(void); | ||
|
|
||
| /** | ||
| * @brief Replace the active rooms state snapshot. | ||
| * | ||
| * Passing NULL clears the current snapshot so consumers can reset their | ||
| * views while awaiting new data. | ||
| */ | ||
| void rooms_provider_set_state(const rooms_state_t* state); | ||
|
|
||
| /** | ||
| * @brief Restore the provider's built-in mock snapshot. | ||
| */ | ||
| void rooms_provider_reset_state(void); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Avoid storing pointers to caller-owned room snapshots
The provider advertises that callers can treat the returned snapshot as provider-owned, but
rooms_provider_set_statesimply assignss_current_stateto the caller’s pointer without copying or retaining ownership. If an integration builds arooms_state_ton the stack (as the tests do) or frees/overwrites the structure after the setter returns, subsequent calls torooms_provider_get_statewill read dangling memory and the UI will dereference invalid entities. The provider should allocate or copy the snapshot it owns, or document and enforce that the passed state must outlive the provider.Useful? React with 👍 / 👎.