* @brief Checks if an existing connection exists, and either uses it, or closes it and creates a new one * * A device - depending on the feature - needs different Endpoints/Connections * Instead of opening and keeping track of multiple connections, we close and open connections no demand * * * * @param existing_hid_path an existing connection path or NULL if none yet * @param device_handle
| 196 | * @return hid_device pointer if successful, or NULL (error in hid_error) |
| 197 | */ |
| 198 | static hid_device* dynamic_connect(char** existing_hid_path, hid_device* device_handle, |
| 199 | struct device* device, enum capabilities cap) |
| 200 | { |
| 201 | // Generate the path which is needed |
| 202 | char* hid_path = get_hid_path(device->idVendor, device->idProduct, |
| 203 | device->capability_details[cap].interface, device->capability_details[cap].usagepage, device->capability_details[cap].usageid); |
| 204 | |
| 205 | if (!hid_path) { |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | // A connection already exists |
| 210 | if (device_handle != NULL) { |
| 211 | // The connection which exists is the same we need, so simply return it |
| 212 | if (strcmp(*existing_hid_path, hid_path) == 0) { |
| 213 | return device_handle; |
| 214 | } else { // if its not the same, and already one connection is open, close it so we can make a new one |
| 215 | hid_close(device_handle); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | free(*existing_hid_path); |
| 220 | |
| 221 | device_handle = hid_open_path(hid_path); |
| 222 | if (device_handle == NULL) { |
| 223 | *existing_hid_path = NULL; |
| 224 | return NULL; |
| 225 | } |
| 226 | |
| 227 | hid_get_manufacturer_string(device_handle, device->device_hid_vendorname, sizeof(device->device_hid_vendorname) / sizeof(device->device_hid_vendorname[0])); |
| 228 | hid_get_product_string(device_handle, device->device_hid_productname, sizeof(device->device_hid_productname) / sizeof(device->device_hid_productname[0])); |
| 229 | |
| 230 | *existing_hid_path = hid_path; |
| 231 | return device_handle; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @brief Handle a requested feature |
no test coverage detected