1 /*
2  * Copyright (c) 2024 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief USB support message types and structure
10  */
11 
12 #ifndef ZEPHYR_INCLUDE_USBD_MSG_H_
13 #define ZEPHYR_INCLUDE_USBD_MSG_H_
14 
15 #include <stdint.h>
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 /**
22  * @defgroup usbd_msg_api USB device core API
23  * @ingroup usb
24  * @since 3.7
25  * @version 0.1.0
26  * @{
27  */
28 
29 /**
30  * @brief USB device support message types
31  *
32  * The first set of message types map to event types from the UDC driver API.
33  */
34 enum usbd_msg_type {
35 	/** VBUS ready message (optional) */
36 	USBD_MSG_VBUS_READY,
37 	/** VBUS removed message (optional) */
38 	USBD_MSG_VBUS_REMOVED,
39 	/** Device resume message */
40 	USBD_MSG_RESUME,
41 	/** Device suspended message */
42 	USBD_MSG_SUSPEND,
43 	/** Bus reset detected */
44 	USBD_MSG_RESET,
45 	/** Device changed configuration */
46 	USBD_MSG_CONFIGURATION,
47 	/** Non-correctable UDC error message  */
48 	USBD_MSG_UDC_ERROR,
49 	/** Unrecoverable device stack error message  */
50 	USBD_MSG_STACK_ERROR,
51 	/** CDC ACM Line Coding update */
52 	USBD_MSG_CDC_ACM_LINE_CODING,
53 	/** CDC ACM Line State update */
54 	USBD_MSG_CDC_ACM_CONTROL_LINE_STATE,
55 	/** Maximum number of message types */
56 	USBD_MSG_MAX_NUMBER,
57 };
58 
59 /**
60  * @cond INTERNAL_HIDDEN
61  */
62 static const char *const usbd_msg_type_list[] = {
63 	"VBUS ready",
64 	"VBUS removed",
65 	"Device resumed",
66 	"Device suspended",
67 	"Bus reset",
68 	"New device configuration",
69 	"Controller error",
70 	"Stack error",
71 	"CDC ACM line coding",
72 	"CDC ACM control line state",
73 };
74 
75 BUILD_ASSERT(ARRAY_SIZE(usbd_msg_type_list) == USBD_MSG_MAX_NUMBER,
76 	     "Number of entries in usbd_msg_type_list is not equal to USBD_MSG_MAX_NUMBER");
77 /** @endcond */
78 
79 /**
80  * @brief USB device message
81  */
82 struct usbd_msg {
83 	/** Message type */
84 	enum usbd_msg_type type;
85 	/** Message status, value or data */
86 	union {
87 		int status;
88 		const struct device *dev;
89 	};
90 };
91 
92 /**
93  * @brief Returns the message type as a constant string
94  *
95  * @param[in] type USBD message type
96  *
97  * @return Message type as a constant string
98  */
usbd_msg_type_string(const enum usbd_msg_type type)99 static inline const char *usbd_msg_type_string(const enum usbd_msg_type type)
100 {
101 	if (type >= 0 && type < USBD_MSG_MAX_NUMBER) {
102 		return usbd_msg_type_list[type];
103 	}
104 
105 	return "?";
106 }
107 
108 /**
109  * @}
110  */
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* ZEPHYR_INCLUDE_USBD_MSG_H_ */
117