├── CMakeLists.txt ├── CMakeModules └── FindHIDAPI.cmake ├── LICENSE ├── README.md └── main.c /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.18) 2 | 3 | project(sixaxispairer VERSION 1.1.0 LANGUAGES C) 4 | 5 | set(CMAKE_C_STANDARD 11) 6 | set(CMAKE_C_STANDARD_REQUIRED ON) 7 | 8 | set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules") 9 | find_package(hidapi REQUIRED) 10 | 11 | set(SOURCES 12 | main.c 13 | ) 14 | 15 | # Mac USB linking. 16 | if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") 17 | find_library(IOKIT NAMES IOKit) 18 | set (PLATFORM_LIBS 19 | ${IOKIT} 20 | ) 21 | endif() 22 | 23 | add_executable(${PROJECT_NAME} ${SOURCES}) 24 | target_include_directories(${PROJECT_NAME} PRIVATE ${hidapi_INCLUDE_DIRS}) 25 | target_link_libraries(${PROJECT_NAME} hidapi::hidapi ${PLATFORM_LIBS}) 26 | install(TARGETS ${PROJECT_NAME} DESTINATION bin) 27 | -------------------------------------------------------------------------------- /CMakeModules/FindHIDAPI.cmake: -------------------------------------------------------------------------------- 1 | # Try to find hidapi library 2 | # Once done will define: 3 | # 4 | # hidapi_FOUND 5 | # hidapi::hidapi 6 | # hidapi_INCLUDE_DIRS 7 | 8 | find_path(hidapi_INCLUDE_DIR 9 | NAMES hidapi.h 10 | PATH_SUFFIXES hidapi 11 | ) 12 | mark_as_advanced(FORCE hidapi_INCLUDE_DIR) 13 | 14 | find_library(hidapi_LIBRARY 15 | NAMES hidapi 16 | ) 17 | mark_as_advanced(FORCE hidapi_LIBRARY) 18 | 19 | include(FindPackageHandleStandardArgs) 20 | FIND_PACKAGE_HANDLE_STANDARD_ARGS(hidapi DEFAULT_MSG 21 | hidapi_INCLUDE_DIR 22 | hidapi_LIBRARY 23 | ) 24 | 25 | if(hidapi_FOUND) 26 | set(hidapi_INCLUDE_DIRS ${hidapi_INCLUDE_DIR}) 27 | if (NOT TARGET hidapi::hidapi AND EXISTS "${hidapi_LIBRARY}" AND EXISTS "${hidapi_INCLUDE_DIR}") 28 | add_library(hidapi::hidapi UNKNOWN IMPORTED) 29 | set_target_properties(hidapi::hidapi PROPERTIES 30 | INTERFACE_INCLUDE_DIRECTORIES ${hidapi_INCLUDE_DIR} 31 | IMPORTED_LOCATION ${hidapi_LIBRARY} 32 | ) 33 | endif() 34 | endif() 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 John Schember 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # About 2 | 3 | A tool for viewing and setting the bluetooth address 4 | a sixaxis controller is currently paired with. 5 | 6 | 7 | # Dependencies 8 | 9 | HID API (https://github.com/libusb/hidapi) 10 | 11 | 12 | # Supported Platforms 13 | 14 | * Windows 15 | * Mac 16 | * Linux 17 | 18 | 19 | # Building 20 | 21 | $ mkdir build 22 | $ cd build 23 | $ cmake .. 24 | $ make 25 | $ ./bin/sixaxispairer 26 | $ ./bin/sixaxispairer xx:xx:xx:xx:xx:xx 27 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2014 John Schember 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to deal 5 | * in the Software without restriction, including without limitation the rights 6 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | * copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | * THE SOFTWARE 20 | */ 21 | 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | static const unsigned short VENDOR = 0x054c; 29 | static const unsigned short PRODUCT[] = { 30 | 0x0268, /* sixaxis */ 31 | 0x042f /* move motion */ 32 | }; 33 | /* 0xf5 == (0x03f5 & ~(3 << 8)) 34 | * 0x03f5 == (0xf5 | (3 << 8)) 35 | * HIDAPI will automatically add (3 << 8 to the report id. 36 | * Other tools for setting the report id use hid libraries which 37 | * don't automatically do this. */ 38 | static const unsigned char MAC_REPORT_ID = 0xf5; 39 | 40 | static unsigned char char_to_nible(char c) 41 | { 42 | if (c >= '0' && c <= '9') 43 | return c - '0'; 44 | if (c >= 'a' && c <= 'f') 45 | return c - 'a' + 10; 46 | if (c >= 'A' && c <= 'F') 47 | return c - 'A' + 10; 48 | return 255; 49 | } 50 | 51 | /* return 1 on success, 0 on failure. */ 52 | static int mac_to_bytes(const char *in, size_t in_len, unsigned char *out, size_t out_len) 53 | { 54 | const char *p; 55 | size_t i=0; 56 | 57 | if (in == NULL || out == NULL || in_len == 0 || out_len == 0) 58 | return 0; 59 | 60 | memset(out, 0, out_len); 61 | p = in; 62 | while (p+1 < in+in_len && i < out_len) { 63 | if (*p == ':') { 64 | p++; 65 | continue; 66 | } 67 | 68 | if (!isxdigit(*p) || !isxdigit(*(p+1))) { 69 | return 0; 70 | } 71 | 72 | out[i] = (char_to_nible(*p) << 4) | (char_to_nible(*(p+1) & 0xff)); 73 | i++; 74 | p += 2; 75 | } 76 | 77 | if (p < in+in_len) 78 | return 0; 79 | return 1; 80 | } 81 | 82 | static void pair_device(hid_device *dev, const char *mac, size_t mac_len) 83 | { 84 | unsigned char buf[8]; 85 | int ret; 86 | 87 | memset(buf, 0, sizeof(buf)); 88 | buf[0] = MAC_REPORT_ID; 89 | buf[1] = 0x0; 90 | if ((mac_len != 12 && mac_len != 17) || !mac_to_bytes(mac, mac_len, buf+2, sizeof(buf)-2)) { 91 | printf("Invalid mac\n"); 92 | return; 93 | } 94 | 95 | ret = hid_send_feature_report(dev, buf, sizeof(buf)); 96 | if (ret == -1) { 97 | printf("Failed to set mac\n"); 98 | } 99 | } 100 | 101 | static void show_pairing(hid_device *dev) 102 | { 103 | unsigned char buf[8]; 104 | int ret; 105 | 106 | memset(buf, 0, sizeof(buf)); 107 | buf[0] = MAC_REPORT_ID; 108 | buf[1] = 0x0; 109 | 110 | ret = hid_get_feature_report(dev, buf, sizeof(buf)); 111 | if (ret < 8) { 112 | printf("Read error\n"); 113 | return; 114 | } 115 | printf("%02x:%02x:%02x:%02x:%02x:%02x\n", buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]); 116 | } 117 | 118 | int main(int argc, char **argv) 119 | { 120 | hid_device *dev; 121 | 122 | if ((argc != 1 && argc != 2) || 123 | (argc == 2 && (strncmp(argv[1], "-h", 2) == 0 || strncmp(argv[1], "--help", 6) == 0))) 124 | { 125 | printf("usage:\n\t%s [mac]\n", argv[0]); 126 | return 0; 127 | } 128 | 129 | for (size_t i = 0; i < sizeof(PRODUCT) / sizeof(*PRODUCT); i++) { 130 | dev = hid_open(VENDOR, PRODUCT[i], NULL); 131 | if (dev != NULL) { 132 | break; 133 | } 134 | } 135 | if (dev == NULL) { 136 | fprintf(stderr, "Could not find SixAxis controller\n"); 137 | return 0; 138 | } 139 | 140 | if (argc == 2) { 141 | pair_device(dev, argv[1], strlen(argv[1])); 142 | } else { 143 | show_pairing(dev); 144 | } 145 | 146 | hid_close(dev); 147 | return 0; 148 | } 149 | --------------------------------------------------------------------------------