Skip to content

Commit b4f39bc

Browse files
yongxu-wang15nordic-krch
authored andcommitted
[nrf fromtree] pm: states: add enum-string conversion helpers
Add two helper functions to convert power management states between enum and string: - pm_state_to_string() - pm_state_from_string() Signed-off-by: Yongxu Wang <[email protected]> (cherry picked from commit 8767cec)
1 parent eca0040 commit b4f39bc

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

include/zephyr/pm/state.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <zephyr/sys/util.h>
1111
#include <zephyr/devicetree.h>
12+
#include <errno.h>
1213

1314
#ifdef __cplusplus
1415
extern "C" {
@@ -382,6 +383,26 @@ uint8_t pm_state_cpu_get_all(uint8_t cpu, const struct pm_state_info **states);
382383
* @return Pointer to the power state structure or NULL if state is not found.
383384
*/
384385
const struct pm_state_info *pm_state_get(uint8_t cpu, enum pm_state state, uint8_t substate_id);
386+
387+
/**
388+
* @brief Convert a pm_state enum value to its string representation.
389+
*
390+
* @param state Power state.
391+
*
392+
* @return A constant string representing the state.
393+
*/
394+
const char *pm_state_to_str(enum pm_state state);
395+
396+
397+
/**
398+
* @brief Parse a string and convert it to a pm_state enum value.
399+
*
400+
* @param name Input string (e.g., "suspend-to-ram").
401+
* @param out Pointer to store the parsed pm_state value.
402+
*
403+
* @return 0 on success, -EINVAL if the string is invalid or NULL.
404+
*/
405+
int pm_state_from_str(const char *name, enum pm_state *out);
385406
/**
386407
* @}
387408
*/

subsys/pm/state.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,48 @@ const struct pm_state_info *pm_state_get(uint8_t cpu, enum pm_state state, uint8
107107

108108
return NULL;
109109
}
110+
111+
const char *pm_state_to_str(enum pm_state state)
112+
{
113+
switch (state) {
114+
case PM_STATE_ACTIVE:
115+
return "active";
116+
case PM_STATE_RUNTIME_IDLE:
117+
return "runtime-idle";
118+
case PM_STATE_SUSPEND_TO_IDLE:
119+
return "suspend-to-idle";
120+
case PM_STATE_STANDBY:
121+
return "standby";
122+
case PM_STATE_SUSPEND_TO_RAM:
123+
return "suspend-to-ram";
124+
case PM_STATE_SUSPEND_TO_DISK:
125+
return "suspend-to-disk";
126+
case PM_STATE_SOFT_OFF:
127+
return "soft-off";
128+
default:
129+
return "UNKNOWN";
130+
}
131+
}
132+
133+
int pm_state_from_str(const char *name, enum pm_state *out)
134+
{
135+
if (strcmp(name, "active") == 0) {
136+
*out = PM_STATE_ACTIVE;
137+
} else if (strcmp(name, "runtime-idle") == 0) {
138+
*out = PM_STATE_RUNTIME_IDLE;
139+
} else if (strcmp(name, "suspend-to-idle") == 0) {
140+
*out = PM_STATE_SUSPEND_TO_IDLE;
141+
} else if (strcmp(name, "standby") == 0) {
142+
*out = PM_STATE_STANDBY;
143+
} else if (strcmp(name, "suspend-to-ram") == 0) {
144+
*out = PM_STATE_SUSPEND_TO_RAM;
145+
} else if (strcmp(name, "suspend-to-disk") == 0) {
146+
*out = PM_STATE_SUSPEND_TO_DISK;
147+
} else if (strcmp(name, "soft-off") == 0) {
148+
*out = PM_STATE_SOFT_OFF;
149+
} else {
150+
return -EINVAL;
151+
}
152+
153+
return 0;
154+
}

0 commit comments

Comments
 (0)