Skip to content

Commit 285e238

Browse files
committed
rtc: shell: support device node labels
Added support for using node labels instead of only full node name Signed-off-by: Yishai Jaffe <[email protected]>
1 parent 8780d91 commit 285e238

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

drivers/rtc/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ config RTC_CALIBRATION
3838
config RTC_SHELL
3939
bool "RTC Shell commands"
4040
depends on SHELL
41+
imply DEVICE_DT_METADATA
4142
help
4243
RTC Shell commands
4344

drivers/rtc/rtc_shell.c

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,37 @@ static char *strptime(const char *s, const char *format, struct tm *tm_time)
146146
}
147147
}
148148

149+
/* Look up a device by some human-readable string identifier. We
150+
* always search among device names. If the feature is available, we
151+
* search by node label as well.
152+
*/
153+
static const struct device *get_rtc_device(char *id)
154+
{
155+
const struct device *dev;
156+
157+
dev = device_get_binding(id);
158+
if (dev != NULL) {
159+
return dev;
160+
}
161+
162+
#ifdef CONFIG_DEVICE_DT_METADATA
163+
dev = device_get_by_dt_nodelabel(id);
164+
if (dev != NULL) {
165+
return dev;
166+
}
167+
#endif /* CONFIG_DEVICE_DT_METADATA */
168+
169+
return NULL;
170+
}
171+
149172
static int cmd_set(const struct shell *sh, size_t argc, char **argv)
150173
{
151-
const struct device *dev = device_get_binding(argv[1]);
174+
const struct device *dev = get_rtc_device(argv[1]);
175+
176+
if (dev == NULL) {
177+
shell_error(sh, "unknown RTC device %s", argv[1]);
178+
return -EINVAL;
179+
}
152180

153181
if (!device_is_ready(dev)) {
154182
shell_error(sh, "device %s not ready", argv[1]);
@@ -191,7 +219,12 @@ static int cmd_set(const struct shell *sh, size_t argc, char **argv)
191219

192220
static int cmd_get(const struct shell *sh, size_t argc, char **argv)
193221
{
194-
const struct device *dev = device_get_binding(argv[1]);
222+
const struct device *dev = get_rtc_device(argv[1]);
223+
224+
if (dev == NULL) {
225+
shell_error(sh, "unknown RTC device %s", argv[1]);
226+
return -EINVAL;
227+
}
195228

196229
if (!device_is_ready(dev)) {
197230
shell_error(sh, "device %s not ready", argv[1]);

0 commit comments

Comments
 (0)