├── README.md ├── LICENSE ├── driver.c ├── android_device.h └── android_device.c /README.md: -------------------------------------------------------------------------------- 1 | # A Simple Android Accessory using libusb 2 | 3 | Simple Android Acessory that I wrote. It shows you how to use the AOA 2.0 protocol to make enable USB Audio and HID Events. In its current form it just opens the default music app of the phone. 4 | 5 | ## Compiling. 6 | 7 | You must have libusb-1.0.0 installed. 8 | 9 | ```shell 10 | gcc -std=gnu99 -o driv android_device.c driver.c `pkg-config --libs --cflags libusb-1.0` 11 | ``` 12 | 13 | ## TODO 14 | 15 | I should probably add better error checking and possibly turn this into a real library. 16 | 17 | ## License 18 | 19 | MIT. -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Thomas Corley 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /driver.c: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Thomas Corley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #include 26 | 27 | #include "android_device.h" 28 | 29 | int main(int argc, char const *argv[]) 30 | { 31 | libusb_init(NULL); 32 | android_device dev; 33 | 34 | printf("Attempting to find device..."); 35 | if (!android_device_find_device(&dev)) 36 | { 37 | printf("Failure!...Exiting!\n"); 38 | return -1; 39 | } 40 | else 41 | printf("Success!\n"); 42 | if (dev.product_id != ACCESSORY_PID && dev.product_id != ACCESSORY_PID_ADB) 43 | { 44 | printf("Attempting to put device in accessory mode..."); 45 | if (!android_device_set_accesory_mode(&dev)) 46 | { 47 | printf("Failure...Exiting!\n"); 48 | return -1; 49 | } 50 | printf("Success!\n"); 51 | usleep(100000); 52 | } 53 | else 54 | printf("Device already in Accesory Mode!\n"); 55 | printf("Attempting to open music app...\n"); 56 | if (android_device_send_hid_event(&dev, KEYCODE_MUSIC)) 57 | printf("Success!\n"); 58 | else 59 | printf("Failure!\n"); 60 | return 0; 61 | } -------------------------------------------------------------------------------- /android_device.h: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Thomas Corley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #ifndef ANDROID_DEV_H 26 | #define ANDROID_DEV_H 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | 34 | 35 | #define GOOGLE_VID 0x18D1 36 | #define ACCESSORY_PID_ADB 0x2D02 37 | #define ACCESSORY_PID_ADB 0x2D03 38 | 39 | #define FIRST_REQUEST_TYPE 0xC0 40 | #define ANDROID_REQUEST_TYPE 0x40 41 | 42 | #define ACCESSORY_GET_PROTOCOL 51 43 | #define ACCESSORY_SET_AUDIO_MODE 58 44 | #define ACCESSORY_START 53 45 | #define ACCESSORY_UNREGISTER_HID 54 46 | #define ACCESSORY_UNREGISTER_HID 55 47 | #define ACCESSORY_SET_HID_REPORT_DESC 56 48 | #define ACCESSORY_SEND_HID_EVENT 57 49 | 50 | #define HID_EVENT_LEN 2 51 | #define CONSUMER_HID_DESC_LEN 23 52 | 53 | // Various keycodes that I have used. 54 | // More can be found here:https://source.android.com/devices/input/keyboard-devices.html 55 | static const unsigned char KEYCODE_MEDIA_PLAY_PAUSE[] = {0xCD, 0x00}; 56 | static const unsigned char KEYCODE_VOLUME_MUTE[] = {0xE2, 0x00}; 57 | static const unsigned char KEYCODE_VOLUME_UP[] = {0xE9, 0x00}; 58 | static const unsigned char KEYCODE_VOLUME_DOWN[] = {0xEA, 0x00}; 59 | static const unsigned char KEYCODE_MEDIA_NEXT[] = {0xB5, 0x00}; 60 | static const unsigned char KEYCODE_MEDIA_PREVIOUS[] = {0xB6, 0x00}; 61 | static const unsigned char KEYCODE_MEDIA_STOP[] = {0xB7, 0x00}; 62 | static const unsigned char KEYCODE_MUSIC[] = {0x83, 0x01}; 63 | static const unsigned char HID_EVENT_RELEASE[] = {0x00, 0x00}; 64 | 65 | static unsigned char consumer_hid_descriptor[CONSUMER_HID_DESC_LEN] = { 66 | 0x05, 0x0c, // Usage Page (Consumer Devices) 67 | 0x09, 0x01, // Usage (Consumer Control) 68 | 0xa1, 0x01, // Collection (Application) 69 | 0x19, 0x00, // Usage Minimum (0), 70 | 0x2A, 0x3C, 0x02, // Usage Maximum (0x23C), 71 | 0x15, 0x00, // Logical Minimum (0) 72 | 0x26, 0x3C, 0x02, // Logical Maximum (0x23C) 73 | 0x75, 0x10, // Report Size (10) 74 | 0x95, 0x01, // Report Count (1) 75 | 0x81, 0x00, // Input (Data,Array,Absolute) 76 | 0xC0 // End Collection 77 | }; 78 | 79 | typedef struct { 80 | uint16_t vendor_id; 81 | uint16_t product_id; 82 | struct libusb_device_handle* handle; 83 | } android_device; 84 | 85 | bool android_device_find_device(android_device *and_dev); 86 | bool android_device_set_accesory_mode(android_device *and_dev); 87 | bool android_device_send_hid_event(android_device *and_dev, const unsigned char *event); 88 | 89 | #endif -------------------------------------------------------------------------------- /android_device.c: -------------------------------------------------------------------------------- 1 | /* 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Thomas Corley 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | */ 24 | 25 | #include "android_device.h" 26 | //TODO: Create better error handling 27 | 28 | /* 29 | * This function tries to see if an android device is connected. 30 | * If a valid device exists it grabs a handle to the device and populates the passed struct 31 | * If no valid device is found the function returns false 32 | */ 33 | bool android_device_find_device(android_device * and_dev) 34 | { 35 | // Various variables that we need to declare 36 | struct libusb_device **list; 37 | struct libusb_device *dev; 38 | struct libusb_device_descriptor desc = {0}; 39 | // Gets every USB device that is connected 40 | ssize_t size = libusb_get_device_list (NULL, &list); 41 | // We are assuming that there is only one Android device connected. 42 | bool found_device = 0; 43 | for (int i = 0; i < size; i++) 44 | { 45 | dev = list[i]; 46 | int rc = libusb_get_device_descriptor(dev, &desc); 47 | if (desc.idVendor == GOOGLE_VID) 48 | { 49 | found_device = true; 50 | break; 51 | } 52 | } 53 | // Need to make sure that we can get a handle to the device 54 | int err = libusb_open(dev, &(and_dev->handle)); 55 | if (err || !found_device) 56 | return false; 57 | and_dev->vendor_id = desc.idVendor; 58 | and_dev->product_id = desc.idProduct; 59 | libusb_free_device_list(list, 1); 60 | return true; 61 | } 62 | 63 | /* 64 | * Attempts to put the device in accesory mode. 65 | * This allows the device to output usb audio and accept HID Commands 66 | * Returns true if the device was put in accesory mode 67 | * and we were able to get a handle to it. 68 | * Returns false if something went wrong. 69 | */ 70 | bool android_device_set_accesory_mode(android_device *and_dev) 71 | { 72 | unsigned char buf[2]; 73 | int response; 74 | // First thing that needs to be done is to check and see if the phone supports AOA 75 | response = libusb_control_transfer(and_dev->handle, FIRST_REQUEST_TYPE, 76 | ACCESSORY_GET_PROTOCOL, 0, 0, buf, 2, 0); 77 | if (response < 0 || (buf[1] << 8 | buf[0] < 2)) 78 | return false; 79 | // We need to add a short sleep or the next request might fail on some devices. 80 | usleep(1000); 81 | // Now we are requesting the ability to use usb audio 82 | response = libusb_control_transfer(and_dev->handle, ANDROID_REQUEST_TYPE, 83 | ACCESSORY_SET_AUDIO_MODE, 1, 0, NULL, 0, 0); 84 | if (response < 0) 85 | return false; 86 | // Request to put the device in accesory mode. 87 | // The PID will now change. 88 | response = libusb_control_transfer(and_dev->handle, ANDROID_REQUEST_TYPE, 89 | ACCESSORY_START, 0, 0, NULL, 0, 0); 90 | if (response < 0) 91 | return false; 92 | //Since the PID changed, we need to release our current handle 93 | // and try to find the new one 94 | if (and_dev->handle != NULL) 95 | libusb_release_interface (and_dev->handle, 0); 96 | // This request fails sometimes, so we attempt it multiple times. 97 | for (int i = 0; i < 5; i++) 98 | { 99 | response = android_device_find_device(and_dev); 100 | if (response && 101 | (and_dev->product_id == ACCESSORY_PID || and_dev->product_id == ACCESSORY_PID_ADB)) 102 | break; 103 | if (i == 4) 104 | return false; 105 | // sleep for a second. 106 | usleep(1000000); 107 | } 108 | libusb_claim_interface(and_dev->handle, 0); 109 | // Now that the accesory part is set up we need to set up HID support 110 | response = libusb_control_transfer(and_dev->handle, ANDROID_REQUEST_TYPE, 111 | ACCESSORY_REGISTER_HID, 1, CONSUMER_HID_DESC_LEN, NULL, 0, 0); 112 | if (response < 0) 113 | return false; 114 | 115 | response = libusb_control_transfer(and_dev->handle, ANDROID_REQUEST_TYPE, 116 | ACCESSORY_SET_HID_REPORT_DESC, 1, 0, consumer_hid_descriptor, CONSUMER_HID_DESC_LEN, 0); 117 | if (response < 0) 118 | return false; 119 | // Now everything is set up properly, we can send HID events and play usb audio. 120 | return true; 121 | } 122 | 123 | /* 124 | * Sends an HID event to the device. 125 | * Returns true on the event being sent succesfully 126 | * False when it is not sent sucessfully. 127 | */ 128 | bool android_device_send_hid_event(android_device *and_dev, const unsigned char *event) 129 | { 130 | int response = libusb_control_transfer(and_dev->handle, ANDROID_REQUEST_TYPE, 131 | ACCESSORY_SEND_HID_EVENT, 1, 0, (unsigned char*)event, HID_EVENT_LEN, 0); 132 | if (response < 0) 133 | return false; 134 | response = libusb_control_transfer(and_dev->handle, ANDROID_REQUEST_TYPE, 135 | ACCESSORY_SEND_HID_EVENT, 1, 0, (unsigned char *)HID_EVENT_RELEASE, HID_EVENT_LEN, 0); 136 | return !(response < 0); 137 | } --------------------------------------------------------------------------------