libnx  v4.2.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).
151 void* hidbusGetSharedmemAddr(void);
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  */
160 Result hidbusGetBusHandle(HidbusBusHandle *handle, bool *flag, HidNpadIdType id, HidbusBusType bus_type);
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 
HidbusBusType
HidbusBusType
BusType.
Definition: hidbus.h:14
HidbusJoyDisableSixAxisPollingDataAccessorEntryData::sampling_number
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:57
HidbusJoyEnableSixAxisPollingDataAccessorEntryData::out_size
u8 out_size
Size of data.
Definition: hidbus.h:69
HidbusStatusManagerEntryCommon::is_connected
u8 is_connected
IsConnected.
Definition: hidbus.h:114
HidbusJoyPollingReceivedData
JoyPollingReceivedData.
Definition: hidbus.h:37
HidbusJoyDisableSixAxisPollingDataAccessor::hdr
HidbusDataAccessorHeader hdr
HidbusDataAccessorHeader
Definition: hidbus.h:96
HidbusStatusManagerEntryCommon::polling_mode
u32 polling_mode
HidbusJoyPollingMode
Definition: hidbus.h:121
hidbusGetServiceSession
Result hidbusGetServiceSession(Service *srv_out)
Gets the Service object for the actual hidbus service session. This object must be closed by the user...
HidbusJoyButtonOnlyPollingDataAccessorEntry
HidbusJoyButtonOnlyPollingDataAccessorEntry.
Definition: hidbus.h:89
u8
uint8_t u8
8-bit unsigned integer.
Definition: types.h:19
HidbusJoyDisableSixAxisPollingDataAccessorEntry
HidbusJoyDisableSixAxisPollingDataAccessorEntry.
Definition: hidbus.h:61
HidbusStatusManagerEntryCommon::reserved
u8 reserved
Reserved.
Definition: hidbus.h:120
HidbusBusHandle::is_valid
u8 is_valid
IsValid.
Definition: hidbus.h:33
HidbusJoyDisableSixAxisPollingDataAccessorEntryData
HidbusJoyDisableSixAxisPollingDataAccessorEntryData.
Definition: hidbus.h:53
HidbusJoyButtonOnlyPollingDataAccessorEntryData::sampling_number
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:85
HidbusJoyEnableSixAxisPollingDataAccessorEntry::data
HidbusJoyEnableSixAxisPollingDataAccessorEntryData data
HidbusJoyEnableSixAxisPollingDataAccessorEntryData
Definition: hidbus.h:77
s32
int32_t s32
32-bit signed integer.
Definition: types.h:27
HidbusJoyEnableSixAxisPollingDataAccessorEntry::sampling_number
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:76
HidbusJoyPollingReceivedData::sampling_number
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:40
HidbusDataAccessorHeader
HidbusDataAccessorHeader.
Definition: hidbus.h:44
HidbusJoyDisableSixAxisPollingDataAccessor
HidbusJoyDisableSixAxisPollingDataAccessor.
Definition: hidbus.h:95
HidbusBusType_LeftJoyRail
@ HidbusBusType_LeftJoyRail
LeftJoyRail.
Definition: hidbus.h:15
HidbusBusHandle
BusHandle.
Definition: hidbus.h:28
HidbusJoyEnableSixAxisPollingDataAccessorEntry
HidbusJoyEnableSixAxisPollingDataAccessorEntry.
Definition: hidbus.h:75
HidbusStatusManagerEntry
HidbusStatusManagerEntry.
Definition: hidbus.h:131
HidbusJoyEnableSixAxisPollingDataAccessor::hdr
HidbusDataAccessorHeader hdr
HidbusDataAccessorHeader
Definition: hidbus.h:102
HidbusJoyEnableSixAxisPollingDataAccessorEntryData
HidbusJoyEnableSixAxisPollingDataAccessorEntryData.
Definition: hidbus.h:67
HidNpadIdType
HidNpadIdType
HID controller IDs.
Definition: hid.h:214
HidbusBusType_RightLarkRail
@ HidbusBusType_RightLarkRail
[6.0.0+] RightLarkRail (for microphone).
Definition: hidbus.h:17
HidbusJoyButtonOnlyPollingDataAccessorEntry::sampling_number
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:90
HidbusJoyPollingMode_SixAxisSensorDisable
@ HidbusJoyPollingMode_SixAxisSensorDisable
SixAxisSensorDisable.
Definition: hidbus.h:22
HidbusJoyButtonOnlyPollingDataAccessor::hdr
HidbusDataAccessorHeader hdr
HidbusDataAccessorHeader
Definition: hidbus.h:108
HidbusStatusManagerEntryCommon::is_connected_result
Result is_connected_result
IsConnectedResult.
Definition: hidbus.h:116
HidbusBusHandle::abstracted_pad_id
u32 abstracted_pad_id
AbstractedPadId.
Definition: hidbus.h:29
u32
uint32_t u32
32-bit unsigned integer.
Definition: types.h:21
HidbusJoyDisableSixAxisPollingDataAccessorEntry::data
HidbusJoyDisableSixAxisPollingDataAccessorEntryData data
HidbusJoyDisableSixAxisPollingDataAccessorEntryData
Definition: hidbus.h:63
HidbusStatusManagerEntryV5::common
HidbusStatusManagerEntryCommon common
HidbusStatusManagerEntryCommon
Definition: hidbus.h:126
HidbusBusHandle::player_number
u8 player_number
PlayerNumber.
Definition: hidbus.h:31
u64
uint64_t u64
64-bit unsigned integer.
Definition: types.h:22
hidbusGetJoyPollingReceivedData
Result hidbusGetJoyPollingReceivedData(HidbusBusHandle handle, HidbusJoyPollingReceivedData *recv_data, s32 count)
GetJoyPollingReceivedData.
HidbusStatusManagerEntry::common
HidbusStatusManagerEntryCommon common
HidbusStatusManagerEntryCommon
Definition: hidbus.h:132
HidbusStatusManagerEntryCommon::is_in_focus
u8 is_in_focus
Flag indicating whether this entry is valid.
Definition: hidbus.h:118
HidbusStatusManager
StatusManager.
Definition: hidbus.h:142
Result
u32 Result
Function error code result type.
Definition: types.h:44
HidbusBusHandle::bus_type_id
u8 bus_type_id
BusTypeId.
Definition: hidbus.h:32
HidbusStatusManagerEntryCommon::is_polling_mode
u8 is_polling_mode
Flag indicating whether polling is enabled (hidbusEnableJoyPollingReceiveMode).
Definition: hidbus.h:119
HidbusStatusManagerEntryV5
HidbusStatusManagerEntry on 5.x.
Definition: hidbus.h:125
hidbusEnableExternalDevice
Result hidbusEnableExternalDevice(HidbusBusHandle handle, bool flag, u32 device_id)
EnableExternalDevice.
hidbusInitialize
Result hidbusInitialize(HidbusBusHandle handle)
Initialize.
HidbusJoyPollingMode_ButtonOnly
@ HidbusJoyPollingMode_ButtonOnly
[6.0.0+] ButtonOnly
Definition: hidbus.h:24
HidbusJoyPollingReceivedData::out_size
u64 out_size
Size of data.
Definition: hidbus.h:39
HidbusJoyButtonOnlyPollingDataAccessorEntryData::out_size
u8 out_size
Size of data.
Definition: hidbus.h:83
HidbusStatusManagerEntryCommon
Common data for HidbusStatusManagerEntry*.
Definition: hidbus.h:113
hidbusEnableJoyPollingReceiveMode
Result hidbusEnableJoyPollingReceiveMode(HidbusBusHandle handle, const void *inbuf, size_t inbuf_size, void *workbuf, size_t workbuf_size, HidbusJoyPollingMode polling_mode)
EnableJoyPollingReceiveMode.
HidbusDataAccessorHeader::res
Result res
Result.
Definition: hidbus.h:45
HidbusBusHandle::internal_index
u8 internal_index
InternalIndex.
Definition: hidbus.h:30
HidbusStatusManagerV5
StatusManager on 5.x.
Definition: hidbus.h:137
HidbusJoyButtonOnlyPollingDataAccessorEntryData
HidbusJoyButtonOnlyPollingDataAccessorEntryData.
Definition: hidbus.h:81
HidbusJoyDisableSixAxisPollingDataAccessorEntryData::out_size
u8 out_size
Size of data.
Definition: hidbus.h:55
hidbusDisableJoyPollingReceiveMode
Result hidbusDisableJoyPollingReceiveMode(HidbusBusHandle handle)
DisableJoyPollingReceiveMode.
HidbusBusType_RightJoyRail
@ HidbusBusType_RightJoyRail
RightJoyRail.
Definition: hidbus.h:16
HidbusDataAccessorHeader::total_entries
u64 total_entries
Total entries.
Definition: hidbus.h:49
hidbusGetSharedmemAddr
void * hidbusGetSharedmemAddr(void)
Gets the SharedMemory addr (HidbusStatusManagerV5 on 5.x, otherwise HidbusStatusManager)....
HidbusJoyButtonOnlyPollingDataAccessorEntry::data
HidbusJoyButtonOnlyPollingDataAccessorEntryData data
HidbusJoyEnableSixAxisPollingDataAccessorEntryData
Definition: hidbus.h:91
Service
Service object structure.
Definition: service.h:13
hidbusSendAndReceive
Result hidbusSendAndReceive(HidbusBusHandle handle, const void *inbuf, size_t inbuf_size, void *outbuf, size_t outbuf_size, u64 *out_size)
SendAndReceive.
HidbusJoyPollingMode_SixAxisSensorEnable
@ HidbusJoyPollingMode_SixAxisSensorEnable
JoyEnableSixAxisPollingData.
Definition: hidbus.h:23
hidbusGetBusHandle
Result hidbusGetBusHandle(HidbusBusHandle *handle, bool *flag, HidNpadIdType id, HidbusBusType bus_type)
GetBusHandle.
HidbusStatusManagerEntryCommon::is_enabled
u8 is_enabled
Flag indicating whether a device is enabled (hidbusEnableExternalDevice).
Definition: hidbus.h:117
HidbusJoyPollingMode
HidbusJoyPollingMode
JoyPollingMode.
Definition: hidbus.h:21
HidbusDataAccessorHeader::pad
u32 pad
Padding.
Definition: hidbus.h:46
HidbusJoyEnableSixAxisPollingDataAccessor
HidbusJoyEnableSixAxisPollingDataAccessor.
Definition: hidbus.h:101
hidbusFinalize
Result hidbusFinalize(HidbusBusHandle handle)
Finalize.
HidbusJoyDisableSixAxisPollingDataAccessorEntry::sampling_number
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:62
HidbusJoyDisableSixAxisPollingDataAccessorEntryData::pad
u8 pad
Padding.
Definition: hidbus.h:56
HidbusJoyEnableSixAxisPollingDataAccessorEntryData::sampling_number
u64 sampling_number
SamplingNumber.
Definition: hidbus.h:71
HidbusJoyButtonOnlyPollingDataAccessor
HidbusJoyButtonOnlyPollingDataAccessor.
Definition: hidbus.h:107
HidbusDataAccessorHeader::latest_entry
u64 latest_entry
Latest entry.
Definition: hidbus.h:48