1 /*
2  * Copyright (c) 2025 Cirrus Logic, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief Haptics shell commands.
10  */
11 
12 #include <zephyr/shell/shell.h>
13 #include <zephyr/drivers/haptics.h>
14 #include <stdlib.h>
15 
16 #define HAPTICS_START_HELP SHELL_HELP("Start haptic output", "<device>")
17 #define HAPTICS_STOP_HELP  SHELL_HELP("Stop haptic output", "<device>")
18 
19 #define HAPTICS_ARGS_DEVICE 1
20 
cmd_start(const struct shell * sh,size_t argc,char ** argv)21 static int cmd_start(const struct shell *sh, size_t argc, char **argv)
22 {
23 	const struct device *dev;
24 	int error;
25 
26 	dev = shell_device_get_binding(argv[HAPTICS_ARGS_DEVICE]);
27 	if (dev == NULL) {
28 		shell_error(sh, "haptic device not found");
29 		return -EINVAL;
30 	}
31 
32 	error = haptics_start_output(dev);
33 	if (error < 0) {
34 		shell_error(sh, "failed to start haptic output (%d)", error);
35 	}
36 
37 	return error;
38 }
39 
cmd_stop(const struct shell * sh,size_t argc,char ** argv)40 static int cmd_stop(const struct shell *sh, size_t argc, char **argv)
41 {
42 	const struct device *dev;
43 	int error;
44 
45 	dev = shell_device_get_binding(argv[HAPTICS_ARGS_DEVICE]);
46 	if (dev == NULL) {
47 		shell_error(sh, "haptic device not found");
48 		return -EINVAL;
49 	}
50 
51 	error = haptics_stop_output(dev);
52 	if (error < 0) {
53 		shell_error(sh, "Failed to stop haptic output (%d)", error);
54 	}
55 
56 	return error;
57 }
58 
device_is_haptics(const struct device * dev)59 static bool device_is_haptics(const struct device *dev)
60 {
61 	return DEVICE_API_IS(haptics, dev);
62 }
63 
device_name_get(size_t idx,struct shell_static_entry * entry)64 static void device_name_get(size_t idx, struct shell_static_entry *entry)
65 {
66 	const struct device *dev = shell_device_filter(idx, device_is_haptics);
67 
68 	entry->syntax = (dev != NULL) ? dev->name : NULL;
69 	entry->handler = NULL;
70 	entry->help = NULL;
71 	entry->subcmd = NULL;
72 }
73 
74 SHELL_DYNAMIC_CMD_CREATE(dsub_device_name, device_name_get);
75 
76 SHELL_STATIC_SUBCMD_SET_CREATE(
77 	haptic_cmds, SHELL_CMD_ARG(start, &dsub_device_name, HAPTICS_START_HELP, cmd_start, 2, 0),
78 	SHELL_CMD_ARG(stop, &dsub_device_name, HAPTICS_STOP_HELP, cmd_stop, 2, 0),
79 	SHELL_SUBCMD_SET_END);
80 
81 SHELL_CMD_REGISTER(haptics, &haptic_cmds, "Haptic shell commands", NULL);
82