@ -14,10 +14,10 @@
# define DEFAULT_TIMEOUT 1000
# define SC_ HID _EVENT_QUEUE_MAX 64
# define SC_ AOA _EVENT_QUEUE_MAX 64
static void
sc_hid_event_log ( const struct sc_hid_event * event ) {
sc_hid_event_log ( uint16_t accessory_id , const struct sc_hid_event * event ) {
// HID Event: [00] FF FF FF FF...
assert ( event - > size ) ;
unsigned buffer_size = event - > size * 3 + 1 ;
@ -29,7 +29,7 @@ sc_hid_event_log(const struct sc_hid_event *event) {
for ( unsigned i = 0 ; i < event - > size ; + + i ) {
snprintf ( buffer + i * 3 , 4 , " %02x " , event - > data [ i ] ) ;
}
LOGV ( " HID Event: [%d]%s " , event- > accessory_id, buffer ) ;
LOGV ( " HID Event: [%d]%s " , accessory_id, buffer ) ;
free ( buffer ) ;
}
@ -38,7 +38,7 @@ sc_aoa_init(struct sc_aoa *aoa, struct sc_usb *usb,
struct sc_acksync * acksync ) {
sc_vecdeque_init ( & aoa - > queue ) ;
if ( ! sc_vecdeque_reserve ( & aoa - > queue , SC_ HID _EVENT_QUEUE_MAX) ) {
if ( ! sc_vecdeque_reserve ( & aoa - > queue , SC_ AOA _EVENT_QUEUE_MAX) ) {
return false ;
}
@ -150,13 +150,14 @@ sc_aoa_setup_hid(struct sc_aoa *aoa, uint16_t accessory_id,
}
static bool
sc_aoa_send_hid_event ( struct sc_aoa * aoa , const struct sc_hid_event * event ) {
sc_aoa_send_hid_event ( struct sc_aoa * aoa , uint16_t accessory_id ,
const struct sc_hid_event * event ) {
uint8_t request_type = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR ;
uint8_t request = ACCESSORY_SEND_HID_EVENT ;
// <https://source.android.com/devices/accessories/aoa2.html#hid-support>
// value (arg0): accessory assigned ID for the HID device
// index (arg1): 0 (unused)
uint16_t value = event- > accessory_id;
uint16_t value = accessory_id;
uint16_t index = 0 ;
unsigned char * data = ( uint8_t * ) event - > data ; // discard const
uint16_t length = event - > size ;
@ -173,7 +174,7 @@ sc_aoa_send_hid_event(struct sc_aoa *aoa, const struct sc_hid_event *event) {
}
bool
sc_aoa_unregister_hid ( struct sc_aoa * aoa , const uint16_t accessory_id ) {
sc_aoa_unregister_hid ( struct sc_aoa * aoa , uint16_t accessory_id ) {
uint8_t request_type = LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR ;
uint8_t request = ACCESSORY_UNREGISTER_HID ;
// <https://source.android.com/devices/accessories/aoa2.html#hid-support>
@ -196,16 +197,25 @@ sc_aoa_unregister_hid(struct sc_aoa *aoa, const uint16_t accessory_id) {
}
bool
sc_aoa_push_hid_event ( struct sc_aoa * aoa , const struct sc_hid_event * event ) {
sc_aoa_push_hid_event_with_ack_to_wait ( struct sc_aoa * aoa ,
uint16_t accessory_id ,
const struct sc_hid_event * event ,
uint64_t ack_to_wait ) {
if ( sc_get_log_level ( ) < = SC_LOG_LEVEL_VERBOSE ) {
sc_hid_event_log ( event ) ;
sc_hid_event_log ( accessory_id, event) ;
}
sc_mutex_lock ( & aoa - > mutex ) ;
bool full = sc_vecdeque_is_full ( & aoa - > queue ) ;
if ( ! full ) {
bool was_empty = sc_vecdeque_is_empty ( & aoa - > queue ) ;
sc_vecdeque_push_noresize ( & aoa - > queue , * event ) ;
struct sc_aoa_event * aoa_event =
sc_vecdeque_push_hole_noresize ( & aoa - > queue ) ;
aoa_event - > hid = * event ;
aoa_event - > accessory_id = accessory_id ;
aoa_event - > ack_to_wait = ack_to_wait ;
if ( was_empty ) {
sc_cond_signal ( & aoa - > event_cond ) ;
}
@ -233,7 +243,7 @@ run_aoa_thread(void *data) {
}
assert ( ! sc_vecdeque_is_empty ( & aoa - > queue ) ) ;
struct sc_ hid _event event = sc_vecdeque_pop ( & aoa - > queue ) ;
struct sc_ aoa _event event = sc_vecdeque_pop ( & aoa - > queue ) ;
uint64_t ack_to_wait = event . ack_to_wait ;
sc_mutex_unlock ( & aoa - > mutex ) ;
@ -259,7 +269,7 @@ run_aoa_thread(void *data) {
}
}
bool ok = sc_aoa_send_hid_event ( aoa , & event ) ;
bool ok = sc_aoa_send_hid_event ( aoa , event . accessory_id , & event . hid ) ;
if ( ! ok ) {
LOGW ( " Could not send HID event to USB device " ) ;
}