libnx  v4.7.0
hidbus.h
Go to the documentation of this file.
1 /**
2  * @file hidbus.h
3  * @brief hidbus service IPC wrapper, for using external devices attached to HID controllers. See also: https://switchbrew.org/wiki/HID_services#hidbus
4  * @note Only available on [5.0.0+].
5  * @author yellows8
6  */
7 #pragma once
8 #include "../types.h"
9 #include "../kernel/event.h"
10 #include "../services/hid.h"
11 #include "../sf/service.h"
12 
13 /// BusType
14 typedef enum {
15  HidbusBusType_LeftJoyRail = 0, ///< LeftJoyRail
16  HidbusBusType_RightJoyRail = 1, ///< RightJoyRail
17  HidbusBusType_RightLarkRail = 2, ///< [6.0.0+] RightLarkRail (for microphone).
19 
20 /// JoyPollingMode
21 typedef enum {
22  HidbusJoyPollingMode_SixAxisSensorDisable = 0, ///< SixAxisSensorDisable
23  HidbusJoyPollingMode_SixAxisSensorEnable = 1, ///< JoyEnableSixAxisPollingData
24  HidbusJoyPollingMode_ButtonOnly = 2, ///< [6.0.0+] ButtonOnly
26 
27 /// BusHandle
28 typedef struct {
29  u32 abstracted_pad_id; ///< AbstractedPadId
30  u8 internal_index; ///< InternalIndex
31  u8 player_number; ///< PlayerNumber
32  u8 bus_type_id; ///< BusTypeId
33  u8 is_valid; ///< IsValid
35 
36 /// JoyPollingReceivedData
37 typedef struct {
38  u8 data[0x30]; ///< Data.
39  u64 out_size; ///< Size of data.
40  u64 sampling_number; ///< SamplingNumber
42 
43 /// HidbusDataAccessorHeader
44 typedef struct {
45  Result res; ///< Result.
46  u32 pad; ///< Padding.
47  u8 unused[0x18]; ///< Initialized sysmodule-side, not used by sdknso.
48  u64 latest_entry; ///< Latest entry.
49  u64 total_entries; ///< Total entries.
51 
52 /// HidbusJoyDisableSixAxisPollingDataAccessorEntryData
53 typedef struct {
54  u8 data[0x26]; ///< Data.
55  u8 out_size; ///< Size of data.
56  u8 pad; ///< Padding.
57  u64 sampling_number; ///< SamplingNumber
59 
60 /// HidbusJoyDisableSixAxisPollingDataAccessorEntry
61 typedef struct {
62  u64 sampling_number; ///< SamplingNumber
63  HidbusJoyDisableSixAxisPollingDataAccessorEntryData data; ///< \ref HidbusJoyDisableSixAxisPollingDataAccessorEntryData
65 
66 /// HidbusJoyEnableSixAxisPollingDataAccessorEntryData
67 typedef struct {
68  u8 data[0x8]; ///< Data.
69  u8 out_size; ///< Size of data.
70  u8 pad[7]; ///< Padding.
71  u64 sampling_number; ///< SamplingNumber
73 
74 /// HidbusJoyEnableSixAxisPollingDataAccessorEntry
75 typedef struct {
76  u64 sampling_number; ///< SamplingNumber
77  HidbusJoyEnableSixAxisPollingDataAccessorEntryData data; ///< \ref HidbusJoyEnableSixAxisPollingDataAccessorEntryData
79 
80 /// HidbusJoyButtonOnlyPollingDataAccessorEntryData
81 typedef struct {
82  u8 data[0x2c]; ///< Data.
83  u8 out_size; ///< Size of data.
84  u8 pad[3]; ///< Padding.
85  u64 sampling_number; ///< SamplingNumber
87 
88 /// HidbusJoyButtonOnlyPollingDataAccessorEntry
89 typedef struct {
90  u64 sampling_number; ///< SamplingNumber
91  HidbusJoyButtonOnlyPollingDataAccessorEntryData data; ///< \ref HidbusJoyEnableSixAxisPollingDataAccessorEntryData
93 
94 /// HidbusJoyDisableSixAxisPollingDataAccessor
95 typedef struct {
96  HidbusDataAccessorHeader hdr; ///< \ref HidbusDataAccessorHeader
97  HidbusJoyDisableSixAxisPollingDataAccessorEntry entries[0xb]; ///< \ref HidbusJoyDisableSixAxisPollingDataAccessorEntry
99 
100 /// HidbusJoyEnableSixAxisPollingDataAccessor
101 typedef struct {
102  HidbusDataAccessorHeader hdr; ///< \ref HidbusDataAccessorHeader
103  HidbusJoyEnableSixAxisPollingDataAccessorEntry entries[0xb]; ///< \ref HidbusJoyEnableSixAxisPollingDataAccessorEntry
105 
106 /// HidbusJoyButtonOnlyPollingDataAccessor
107 typedef struct {
108  HidbusDataAccessorHeader hdr; ///< \ref HidbusDataAccessorHeader
109  HidbusJoyButtonOnlyPollingDataAccessorEntry entries[0xb]; ///< \ref HidbusJoyButtonOnlyPollingDataAccessorEntry
111 
112 /// Common data for HidbusStatusManagerEntry*.
113 typedef struct {
114  u8 is_connected; ///< IsConnected
115  u8 pad[3]; ///< Padding.
116  Result is_connected_result; ///< IsConnectedResult
117  u8 is_enabled; ///< Flag indicating whether a device is enabled (\ref hidbusEnableExternalDevice).
118  u8 is_in_focus; ///< Flag indicating whether this entry is valid.
119  u8 is_polling_mode; ///< Flag indicating whether polling is enabled (\ref hidbusEnableJoyPollingReceiveMode).
120  u8 reserved; ///< Reserved
121  u32 polling_mode; ///< \ref HidbusJoyPollingMode
123 
124 /// HidbusStatusManagerEntry on 5.x.
125 typedef struct {
126  HidbusStatusManagerEntryCommon common; ///< \ref HidbusStatusManagerEntryCommon
127  u8 unk_x10[0xf0]; ///< Ignored by official sw.
129 
130 /// HidbusStatusManagerEntry
131 typedef struct {
132  HidbusStatusManagerEntryCommon common; ///< \ref HidbusStatusManagerEntryCommon
133  u8 unk_x10[0x70]; ///< Ignored by official sw.
135 
136 /// StatusManager on 5.x.
137 typedef struct {
138  HidbusStatusManagerEntryV5 entries[0x10]; ///< \ref HidbusStatusManagerEntryV5
140 
141 /// StatusManager
142 typedef struct {
143  HidbusStatusManagerEntry entries[0x13]; ///< \ref HidbusStatusManagerEntry
144  u8 unused[0x680]; ///< Unused.
146 
147 /// Gets the Service object for the actual hidbus service session. This object must be closed by the user once finished using cmds with this.
149 
150 /// Gets the SharedMemory addr (\ref HidbusStatusManagerV5 on 5.x, otherwise \ref HidbusStatusManager). Only valid when at least one BusHandle is currently initialized (\ref hidbusInitialize).
152 
153 /**
154  * @brief GetBusHandle
155  * @param[out] handle \ref HidbusBusHandle
156  * @param[out] flag Output flag indicating whether the handle is valid.
157  * @param[in] id \ref HidNpadIdType
158  * @param[in] bus_type \ref HidbusBusType
159  */
161 
162 /**
163  * @brief Initialize
164  * @param[in] handle \ref HidbusBusHandle
165  */
167 
168 /**
169  * @brief Finalize
170  * @param[in] handle \ref HidbusBusHandle
171  */
173 
174 /**
175  * @brief EnableExternalDevice
176  * @note This uses \ref hidLaShowControllerFirmwareUpdate if needed.
177  * @param[in] handle \ref HidbusBusHandle
178  * @param[in] flag Whether to enable the device (true = enable, false = disable). When false, this will internally use \ref hidbusDisableJoyPollingReceiveMode if needed.
179  * @param[in] device_id ExternalDeviceId which must match the connected device. Only used when flag is set.
180  */
181 Result hidbusEnableExternalDevice(HidbusBusHandle handle, bool flag, u32 device_id);
182 
183 /**
184  * @brief SendAndReceive
185  * @param[in] handle \ref HidbusBusHandle
186  * @param[in] inbuf Input buffer, containing the command data.
187  * @param[in] inbuf_size Input buffer size, must be <0x26.
188  * @param[out] outbuf Output buffer, containing the command reply data.
189  * @param[in] outbuf_size Output buffer max size.
190  * @param[out] out_size Actual output size.
191  */
192 Result hidbusSendAndReceive(HidbusBusHandle handle, const void* inbuf, size_t inbuf_size, void* outbuf, size_t outbuf_size, u64 *out_size);
193 
194 /**
195  * @brief EnableJoyPollingReceiveMode
196  * @param[in] handle \ref HidbusBusHandle
197  * @param[in] inbuf Input buffer, containing the command data.
198  * @param[in] inbuf_size Input buffer size, must be <0x26.
199  * @param[out] workbuf TransferMemory buffer, must be 0x1000-byte aligned. This buffer must not be written to until after \ref hidbusDisableJoyPollingReceiveMode is used.
200  * @param[in] workbuf_size TransferMemory buffer size, must be 0x1000-byte aligned.
201  * @param[in] polling_mode \ref HidbusJoyPollingMode
202  */
203 Result hidbusEnableJoyPollingReceiveMode(HidbusBusHandle handle, const void* inbuf, size_t inbuf_size, void* workbuf, size_t workbuf_size, HidbusJoyPollingMode polling_mode);
204 
205 /**
206  * @brief DisableJoyPollingReceiveMode
207  * @note This can also be used via \ref hidbusEnableExternalDevice with flag=false.
208  * @param[in] handle \ref HidbusBusHandle
209  */
211 
212 /**
213  * @brief GetJoyPollingReceivedData
214  * @param[in] handle \ref HidbusBusHandle
215  * @param[out] recv_data Output array of \ref HidbusJoyPollingReceivedData.
216  * @param[in] count Total entries for the recv_data array. The maximum is 0xa. Official apps use range 0x1-0x9.
217  */
219 
HidNpadIdType
HID controller IDs.
Definition: hid.h:214
HidbusJoyPollingMode
JoyPollingMode.
Definition: hidbus.h:21
@ HidbusJoyPollingMode_SixAxisSensorEnable
JoyEnableSixAxisPollingData.
Definition: hidbus.h:23
@ HidbusJoyPollingMode_ButtonOnly
[6.0.0+] ButtonOnly
Definition: hidbus.h:24
@ HidbusJoyPollingMode_SixAxisSensorDisable
SixAxisSensorDisable.
Definition: hidbus.h:22
HidbusBusType
BusType.
Definition: hidbus.h:14
@ HidbusBusType_RightLarkRail
[6.0.0+] RightLarkRail (for microphone).
Definition: hidbus.h:17
@ HidbusBusType_LeftJoyRail
LeftJoyRail.
Definition: hidbus.h:15
@ HidbusBusType_RightJoyRail
RightJoyRail.
Definition: hidbus.h:16
Result hidbusFinalize(HidbusBusHandle handle)
Finalize.
Result hidbusGetServiceSession(Service *srv_out)
Gets the Service object for the actual hidbus service session. This object must be closed by the user...
Result hidbusGetJoyPollingReceivedData(HidbusBusHandle handle, HidbusJoyPollingReceivedData *recv_data, s32 count)
GetJoyPollingReceivedData.
Result hidbusSendAndReceive(HidbusBusHandle handle, const void *inbuf, size_t inbuf_size, void *outbuf, size_t outbuf_size, u64 *out_size)
SendAndReceive.
Result hidbusEnableJoyPollingReceiveMode(HidbusBusHandle handle, const void *inbuf, size_t inbuf_size, void *workbuf, size_t workbuf_size, HidbusJoyPollingMode polling_mode)
EnableJoyPollingReceiveMode.
Result hidbusGetBusHandle(HidbusBusHandle *handle, bool *flag, HidNpadIdType id, HidbusBusType bus_type)
GetBusHandle.
Result hidbusDisableJoyPollingReceiveMode(HidbusBusHandle handle)
DisableJoyPollingReceiveMode.
Result hidbusEnableExternalDevice(HidbusBusHandle handle, bool flag, u32 device_id)
EnableExternalDevice.
void * hidbusGetSharedmemAddr(void)
Gets the SharedMemory addr (HidbusStatusManagerV5 on 5.x, otherwise HidbusStatusManager)....
Result hidbusInitialize(HidbusBusHandle handle)
Initialize.
BusHandle.
Definition: hidbus.h:28
u8 player_number
PlayerNumber.
Definition: hidbus.h:31
u8 bus_type_id
BusTypeId.
Definition: hidbus.h:32
u8 is_valid
IsValid.
Definition: hidbus.h:33
u8 internal_index
InternalIndex.
Definition: hidbus.h:30
u32 abstracted_pad_id
AbstractedPadId.
Definition: hidbus.h:29
HidbusDataAccessorHeader.
Definition: hidbus.h:44
u64 total_entries
Total entries.
Definition: hidbus.h:49
u64 latest_entry
Latest entry.
Definition: hidbus.h:48
Result res
Result.
Definition: hidbus.h:45
u32 pad
Padding.
Definition: hidbus.h:46
HidbusJoyButtonOnlyPollingDataAccessorEntryData.
Definition: hidbus.h:81
u8 out_size
Size of data.
Definition: hidbus.h:83
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:85
HidbusJoyButtonOnlyPollingDataAccessorEntry.
Definition: hidbus.h:89
HidbusJoyButtonOnlyPollingDataAccessorEntryData data
HidbusJoyEnableSixAxisPollingDataAccessorEntryData
Definition: hidbus.h:91
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:90
HidbusJoyButtonOnlyPollingDataAccessor.
Definition: hidbus.h:107
HidbusDataAccessorHeader hdr
HidbusDataAccessorHeader
Definition: hidbus.h:108
HidbusJoyDisableSixAxisPollingDataAccessorEntryData.
Definition: hidbus.h:53
u8 out_size
Size of data.
Definition: hidbus.h:55
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:57
HidbusJoyDisableSixAxisPollingDataAccessorEntry.
Definition: hidbus.h:61
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:62
HidbusJoyDisableSixAxisPollingDataAccessorEntryData data
HidbusJoyDisableSixAxisPollingDataAccessorEntryData
Definition: hidbus.h:63
HidbusJoyDisableSixAxisPollingDataAccessor.
Definition: hidbus.h:95
HidbusDataAccessorHeader hdr
HidbusDataAccessorHeader
Definition: hidbus.h:96
HidbusJoyEnableSixAxisPollingDataAccessorEntryData.
Definition: hidbus.h:67
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:71
u8 out_size
Size of data.
Definition: hidbus.h:69
HidbusJoyEnableSixAxisPollingDataAccessorEntry.
Definition: hidbus.h:75
HidbusJoyEnableSixAxisPollingDataAccessorEntryData data
HidbusJoyEnableSixAxisPollingDataAccessorEntryData
Definition: hidbus.h:77
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:76
HidbusJoyEnableSixAxisPollingDataAccessor.
Definition: hidbus.h:101
HidbusDataAccessorHeader hdr
HidbusDataAccessorHeader
Definition: hidbus.h:102
JoyPollingReceivedData.
Definition: hidbus.h:37
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:40
u64 out_size
Size of data.
Definition: hidbus.h:39
Common data for HidbusStatusManagerEntry*.
Definition: hidbus.h:113
u8 is_enabled
Flag indicating whether a device is enabled (hidbusEnableExternalDevice).
Definition: hidbus.h:117
u32 polling_mode
HidbusJoyPollingMode
Definition: hidbus.h:121
u8 is_polling_mode
Flag indicating whether polling is enabled (hidbusEnableJoyPollingReceiveMode).
Definition: hidbus.h:119
u8 reserved
Reserved.
Definition: hidbus.h:120
u8 is_in_focus
Flag indicating whether this entry is valid.
Definition: hidbus.h:118
Result is_connected_result
IsConnectedResult.
Definition: hidbus.h:116
u8 is_connected
IsConnected.
Definition: hidbus.h:114
HidbusStatusManagerEntry on 5.x.
Definition: hidbus.h:125
HidbusStatusManagerEntryCommon common
HidbusStatusManagerEntryCommon
Definition: hidbus.h:126
HidbusStatusManagerEntry.
Definition: hidbus.h:131
HidbusStatusManagerEntryCommon common
HidbusStatusManagerEntryCommon
Definition: hidbus.h:132
StatusManager on 5.x.
Definition: hidbus.h:137
StatusManager.
Definition: hidbus.h:142
Service object structure.
Definition: service.h:14
uint64_t u64
64-bit unsigned integer.
Definition: types.h:22
uint8_t u8
8-bit unsigned integer.
Definition: types.h:19
u32 Result
Function error code result type.
Definition: types.h:44
int32_t s32
32-bit signed integer.
Definition: types.h:27
uint32_t u32
32-bit unsigned integer.
Definition: types.h:21