/** *** Flush queue **/ void hid_flush(hid_device* handle) { unsigned char hid_buf[FAN_SIZE]; // Flush the report queue hid_set_nonblocking(handle, 1); while( hid_read(handle, hid_buf, sizeof(hid_buf) > 0)); hid_set_nonblocking(handle, 0); }
USING_PTYPES /** * */ void rp_init(hid_device* hid) { LPRINTF("Saitek ProPanels Plugin: rp_init\n"); uint8_t buf[4]; hid_set_nonblocking(hid, (int)true); hid_read(hid, buf, sizeof(buf)); hid_send_feature_report(hid, rp_blank_panel, sizeof(rp_blank_panel)); hid_set_nonblocking(hid, (int)false); }
int mcp_panel_open() { int res = 0; mcpHandle = hid_open(VENDOR_ID, MCP_PROD_ID, NULL); if (!mcpHandle) { XPLMDebugString("-> CP: mcp_driver.panel_open: unable to open device.\n"); return -1; } wchar_t wstr[MAX_STR]; res = hid_get_manufacturer_string(mcpHandle, wstr, MAX_STR); sprintf(tmp, "-> CP: mcp_driver.panel_open: Manufacturer String %ls\n", wstr); XPLMDebugString(tmp); hid_set_nonblocking(mcpHandle, 1); res = hid_read(mcpHandle, mcp_in_buf, MCP_IN_BUF_SIZE); if (res < 0) { sprintf(tmp, "-> CP: mcp_driver.panel_open: Error: %ls\n", hid_error(mcpHandle)); XPLMDebugString(tmp); } res = hid_send_feature_report(mcpHandle, mcp_all_on_panel, sizeof(mcp_all_on_panel)); res = hid_send_feature_report(mcpHandle, mcp_course_left, sizeof(mcp_course_left)); res = hid_send_feature_report(mcpHandle, mcp_course_right, sizeof(mcp_course_right)); res = hid_send_feature_report(mcpHandle, mcp_ias_mach, sizeof(mcp_ias_mach)); res = hid_send_feature_report(mcpHandle, mcp_heading, sizeof(mcp_heading)); res = hid_send_feature_report(mcpHandle, mcp_altitude, sizeof(mcp_altitude)); res = hid_send_feature_report(mcpHandle, mcp_vert_speed, sizeof(mcp_vert_speed)); if (res < 0) { sprintf(tmp, "-> CP: mcp_driver.panel_open: Error: %ls\n", hid_error(mcpHandle)); XPLMDebugString(tmp); } return 0; }
/************************************************************************//** * \brief Connects this instance to a Manta * \param connectionSerial The serial number of the manta to search for. * * If connectionSerial is left out or given as 0 then any connected Manta will * match. If a serial number is given then libmanta will attempt to connect to * that Manta. If no matching manta is found then Connect will throw a * MantaNotFoundException. If no exception is thrown then the connection can be * assumed to have been successful. * ****************************************************************************/ void MantaUSB::Connect(int connectionSerial) { #define SERIAL_STRING_SIZE 32 wchar_t serialString[SERIAL_STRING_SIZE]; if(IsConnected()) { return; } DebugPrint("%s-%d: Attempting to Connect to Manta %d...", __FILE__, __LINE__, connectionSerial); if(connectionSerial) { swprintf(serialString, SERIAL_STRING_SIZE, L"%d", connectionSerial); DeviceHandle = hid_open(VendorID, ProductID, serialString); } else { DeviceHandle = hid_open(VendorID, ProductID, NULL); } if(NULL == DeviceHandle) throw(MantaNotFoundException()); hid_get_serial_number_string(DeviceHandle, serialString, SERIAL_STRING_SIZE); SerialNumber = wcstol(serialString, NULL, 10); hid_set_nonblocking(DeviceHandle, 1); }
/* PsychHIDGetDeviceRecordPtrFromIndex() The inverse of PsychHIDGetIndexFromRecord() Accept the index from the list of device records and return a pointer to the indicated record. Externally the list is one-indexed. */ pRecDevice PsychHIDGetDeviceRecordPtrFromIndex(int deviceIndex) { int i; pRecDevice currentDevice=NULL; PsychHIDVerifyInit(); i=1; for(currentDevice=HIDGetFirstDevice(); currentDevice != NULL; currentDevice=HIDGetNextDevice(currentDevice)){ if(i==deviceIndex) { #if PSYCH_SYSTEM != PSYCH_OSX if (!currentDevice->interface) { currentDevice->interface = (void*) hid_open_path(currentDevice->transport); if (!currentDevice->interface) PsychErrorExitMsg(PsychError_system, "HIDLIB Failed to open USB device!"); // Set read ops on device to non-blocking: hid_set_nonblocking((hid_device*) currentDevice->interface, 1); } #endif return(currentDevice); } ++i; } PsychErrorExitMsg(PsychError_user, "Invalid device index specified. Has a device been unplugged? Try rebuilding the device list"); return(NULL); //make the compiler happy. }
SwitchPanel::SwitchPanel() { handle = hid_open(0x06a3, 0x0d67, NULL); oapiWriteLog("Device opened"); // Set the hid_read() function to be non-blocking. hid_set_nonblocking(handle, 1); }
int HidController::close() { if (!isOpen()) { qDebug() << "HID device" << getName() << "already closed"; return -1; } qDebug() << "Shutting down HID device" << getName(); // Stop the reading thread if (m_pReader == NULL) { qWarning() << "HidReader not present for" << getName() << "yet the device is open!"; } else { disconnect(m_pReader, SIGNAL(incomingData(QByteArray)), this, SLOT(receive(QByteArray))); m_pReader->stop(); hid_set_nonblocking(m_pHidDevice, 1); // Quit blocking if (debugging()) qDebug() << " Waiting on reader to finish"; m_pReader->wait(); delete m_pReader; m_pReader = NULL; } // Stop controller engine here to ensure it's done before the device is closed // incase it has any final parting messages stopEngine(); // Close device if (debugging()) { qDebug() << " Closing device"; } hid_close(m_pHidDevice); setOpen(false); return 0; }
void USBInterface::HIDClose() { if(HIDisOpen) { hid_close(DeviceHandle); hid_set_nonblocking(DeviceHandle,1); HIDisOpen= false; } }
int mcp_panel_read_non_blocking(unsigned char *buf) { int res = 0; if (mcpHandle) { hid_set_nonblocking(mcpHandle, 1); res = hid_read(mcpHandle, buf, MCP_IN_BUF_SIZE); if (res < 0) { sprintf(tmp, "-> CP: mcp_driver.panel_read_non_blocking: Error: %ls\n", hid_error(mcpHandle)); XPLMDebugString(tmp); } } return res; }
void HID_PnP::PollUSB() { buf[0] = 0x00; memset((void*)&buf[2], 0x00, sizeof(buf) - 2); if (isConnected == false) { device = hid_open(0x04d8, 0x003f, NULL); if (device) { isConnected = true; hid_set_nonblocking(device, true); timer->start(15); } } else { if(toggleLeds == true) { toggleLeds = false; buf[1] = 0x80; if (hid_write(device, buf, sizeof(buf)) == -1) { CloseDevice(); return; } buf[0] = 0x00; buf[1] = 0x37; memset((void*)&buf[2], 0x00, sizeof(buf) - 2); } if (hid_write(device, buf, sizeof(buf)) == -1) { CloseDevice(); return; } if(hid_read(device, buf, sizeof(buf)) == -1) { CloseDevice(); return; } if(buf[0] == 0x37) { potentiometerValue = (buf[2]<<8) + buf[1]; buf[1] = 0x81; } else if(buf[0] == 0x81) { pushbuttonStatus = (buf[1] == 0x00); buf[1] = 0x37; } } hid_comm_update(isConnected, pushbuttonStatus, potentiometerValue); }
int cb_panel_read_blocking(unsigned char *buf) { int res = 0; if (cbHandle) { hid_set_nonblocking(cbHandle, 0); res = hid_read_timeout(cbHandle, buf, CB_IN_BUF_SIZE, 100); if (res < 0) { sprintf(tmp, "-> CP: cb_driver.panel_read_blocking: Error: %ls\n", hid_error(cbHandle)); printf(tmp); } } return res; }
void TaquitoClient::connect(){ std::cerr << "connecting" << std::endl; if(USBHandle) disconnect(); USBHandle = hid_open(VENDOR_ID, DEVICE_ID, NULL); // attempt to open device if(USBHandle) { // if handle is set, then connection has been made hid_set_nonblocking(USBHandle, 1); // set hid_read() in non-blocking mode }else{ std::cerr << "connection failure" << std::endl; throw TaquitoError(); } }
// ***** Multi Panel Process ****** void process_multi_panel() { process_multi_menu(); // ******* Only do a read if something new to be read ******** hid_set_nonblocking(multihandle, 1); int multi_safety_cntr = 30; do{ multires = hid_read(multihandle, multibuf, sizeof(multibuf)); process_alt_switch(); process_vs_switch(); process_ias_switch(); process_hdg_switch(); process_crs_switch(); process_autothrottle_switch(); process_ap_master_switch(); process_hdg_button(); process_nav_button(); process_ias_button(); process_alt_button(); process_vs_button(); process_apr_button(); process_rev_button(); process_flaps_switch(); process_trim_wheel(); if(multires > 0){ process_multi_flash(); process_multi_blank_display(); process_multi_display(); hid_send_feature_report(multihandle, multiwbuf, sizeof(multiwbuf)); } --multi_safety_cntr; }while((multires > 0) && (multi_safety_cntr > 0)); process_multi_flash(); process_multi_blank_display(); process_multi_display(); // ******* Write on changes or timeout ******** if ((lastmultiseldis != multiseldis) || (lastbtnleds != btnleds) || (multinowrite > 50)) { mulres = hid_send_feature_report(multihandle, multiwbuf, sizeof(multiwbuf)); multinowrite = 1; lastmultiseldis = multiseldis; lastbtnleds = btnleds; }else{ multinowrite++; } return; }
//-------------------------------------------------------------- void ofxPowerMate::conecta(){ // Open the device using the VID, PID, // and optionally the Serial number. handle = hid_open(0x077d, 0x0410, NULL); // Read the Manufacturer String res = hid_get_manufacturer_string(handle, wstr, 6); printf("Manufacturer String: %ls\n", wstr); // Set the hid_read() function to be non-blocking. hid_set_nonblocking(handle, 1); }
Comm::ErrorCode Comm::open(void) { boot_device = hid_open(VID, PID, NULL); if(boot_device) { connected = true; hid_set_nonblocking(boot_device, true); qWarning("Device successfully connected to."); return Success; } qWarning("Unable to open device."); return NotConnected; }
int touchmouse_open(touchmouse_device **dev, touchmouse_device_info *dev_info) { touchmouse_device* t_dev = (touchmouse_device*)malloc(sizeof(touchmouse_device)); memset(t_dev, 0, sizeof(touchmouse_device)); char* path = ((struct hid_device_info**)dev_info->opaque)[0]->path; t_dev->dev = hid_open_path(path); if (!t_dev->dev) { TM_ERROR("hid_open() failed for device with path %s\n", path); free(t_dev); return -1; } hid_set_nonblocking(t_dev->dev, 1); // Enable nonblocking reads *dev = t_dev; return 0; }
JNIEXPORT void JNICALL Java_com_codeminders_hidapi_HIDDevice_disableBlocking (JNIEnv *env, jobject self) { hid_device *peer = getPeer(env, self); if(!peer) { throwIOException(env, peer); return; /* not an error, freed previously */ } int res = hid_set_nonblocking(peer, 1); if(res!=0) { throwIOException(env, peer); return; /* not an error, freed previously */ } }
int main(int argc, char* argv[]){ hid_device *handle; unsigned char resbuf[8]; if(argc < 3){ usage(); return 1; } host = argv[1]; port = atoi(argv[2]); printf("Opening ONTRAK device...\n"); handle = hid_open(VENDOR_ID, DEVICE_ID, NULL); hid_set_nonblocking(handle, 0); //blocking mode ftw printf("Device opened and set to blocking mode."); UdpTransmitSocket transmitSocket( IpEndpointName( host, port ) ); while(true){ send_ontrak_command(handle); int res = hid_read(handle, resbuf, 8); if(res <= 0){ printf("ERROR reading\n"); } else{ fprintf(stdout, "."); fflush(stdout); if(++dots > 80){ printf("\n"); dots = 0; } int state = atoi((const char*)resbuf+1); // printf("%d\n", state); if(state != last_state){ last_state = state; send_osc(transmitSocket, state); } } usleep(POLL_SLEEP_MS * 1000); } }
ambit_object_t * libambit_new(const ambit_device_info_t *device) { ambit_object_t *object = NULL; const ambit_known_device_t *known_device = NULL; const char *path = NULL; if (!device || !device->path) { LOG_ERROR("%s", strerror(EINVAL)); return NULL; } path = strdup (device->path); if (!path) return NULL; if (0 == device->access_status && device->is_supported) { // Note, this should never fail if device was properly received with libambit_enumerate known_device = libambit_device_support_find(device->vendor_id, device->product_id, device->model, device->fw_version); if (known_device != NULL) { object = calloc(1, sizeof(*object)); if (object) { object->handle = hid_open_path(path); memcpy(&object->device_info, device, sizeof(*device)); object->device_info.path = path; object->driver = known_device->driver; if (object->handle) { hid_set_nonblocking(object->handle, true); } // Initialize driver object->driver->init(object, known_device->driver_param); } } } if (!object) { free((char *) path); } return object; }
//----------------------------------------------------------------------------- bool HIDDevice::openDevice(const char* device_path) { // First fill out the device descriptor if (!HIDManager->GetDescriptorFromPath(device_path, &DevDesc)) { return false; } DeviceHandle = hid_open_path(device_path); if(!DeviceHandle) { LogError("Could not open %s error: %s", device_path, strerror(errno)); return false; } hid_set_nonblocking(DeviceHandle,0); HIDManager->DevManager->pThread->AddSelectFd(this, -1); //set this up to be polled return true; }
bool UsbHid::setNonBlockingRead() { return hidDevice != 0 && hid_set_nonblocking(hidDevice, 1) == 0; }
// Reconnects the device I/O for the first acceptable device // Called automatically by constructor, but userland code can // use it to reacquire a hotplugged device. void vrpn_HidInterface::reconnect() { // Enumerate all devices and pass each one to the acceptor to see if it is the // one that we want. struct hid_device_info *devs = hid_enumerate(0, 0); struct hid_device_info *loop = devs; bool found = false; const wchar_t *serial; const char * path; while ((loop != NULL) && !found) { vrpn_HIDDEVINFO device_info; device_info.vendor = loop->vendor_id; device_info.product = loop->product_id; device_info.serial_number = loop->serial_number; device_info.manufacturer_string = loop->manufacturer_string; device_info.product_string = loop->product_string; device_info.interface_number = loop->interface_number; //printf("XXX Found vendor %x, product %x\n", (unsigned)(loop->vendor_id), (unsigned)(loop->product_id)); if (_acceptor->accept(device_info)) { _vendor = loop->vendor_id; _product = loop->product_id; _interface = loop->interface_number; serial = loop->serial_number; path = loop->path; found = true; #ifdef VRPN_HID_DEBUGGING fprintf(stderr,"vrpn_HidInterface::reconnect(): Found %ls %ls (%04hx:%04hx) at path %s - will attempt to to open.\n", loop->manufacturer_string, loop->product_string, _vendor, _product, loop->path); #endif } loop = loop->next; } if (!found) { fprintf(stderr,"vrpn_HidInterface::reconnect(): Device not found\n"); return; } // Initialize the HID interface and open the device. _device = hid_open_path(path); if (_device == NULL) { fprintf(stderr,"vrpn_HidInterface::reconnect(): Could not open device\n"); #ifdef linux fprintf(stderr," (Did you remember to run as root?)\n"); #endif return; } // We cannot do this before the call to open because the serial number // is a pointer to a string down in there, which forms a race condition. // This will be a memory leak if the device fails to open. if (devs != NULL) { hid_free_enumeration(devs); devs = NULL; } // Set the device to non-blocking mode. if (hid_set_nonblocking(_device, 1) != 0) { fprintf(stderr,"vrpn_HidInterface::reconnect(): Could not set device to nonblocking\n"); return; } #ifdef VRPN_HID_DEBUGGING fprintf(stderr,"vrpn_HidInterface::reconnect(): Device successfully opened.\n"); #endif _working = true; }
static ohmd_device* open_device(ohmd_driver* driver, ohmd_device_desc* desc) { rift_priv* priv = ohmd_alloc(driver->ctx, sizeof(rift_priv)); if(!priv) goto cleanup; priv->base.ctx = driver->ctx; // Open the HID device priv->handle = hid_open_path(desc->path); if(!priv->handle) goto cleanup; if(hid_set_nonblocking(priv->handle, 1) == -1){ ohmd_set_error(driver->ctx, "failed to set non-blocking on device"); goto cleanup; } unsigned char buf[FEATURE_BUFFER_SIZE]; int size; // Read and decode the sensor range size = get_feature_report(priv, RIFT_CMD_RANGE, buf); decode_sensor_range(&priv->sensor_range, buf, size); dump_packet_sensor_range(&priv->sensor_range); // Read and decode display information size = get_feature_report(priv, RIFT_CMD_DISPLAY_INFO, buf); decode_sensor_display_info(&priv->display_info, buf, size); dump_packet_sensor_display_info(&priv->display_info); // Read and decode the sensor config size = get_feature_report(priv, RIFT_CMD_SENSOR_CONFIG, buf); decode_sensor_config(&priv->sensor_config, buf, size); dump_packet_sensor_config(&priv->sensor_config); // if the sensor has display info data, use HMD coordinate frame priv->coordinate_frame = priv->display_info.distortion_type != RIFT_DT_NONE ? RIFT_CF_HMD : RIFT_CF_SENSOR; // apply sensor config set_coordinate_frame(priv, priv->coordinate_frame); // set keep alive interval to n seconds pkt_keep_alive keep_alive = { 0, KEEP_ALIVE_VALUE }; size = encode_keep_alive(buf, &keep_alive); send_feature_report(priv, buf, size); // Update the time of the last keep alive we have sent. priv->last_keep_alive = ohmd_get_tick(); // update sensor settings with new keep alive value // (which will have been ignored in favor of the default 1000 ms one) size = get_feature_report(priv, RIFT_CMD_SENSOR_CONFIG, buf); decode_sensor_config(&priv->sensor_config, buf, size); dump_packet_sensor_config(&priv->sensor_config); // Set default device properties ohmd_set_default_device_properties(&priv->base.properties); // Set device properties priv->base.properties.hsize = priv->display_info.h_screen_size; priv->base.properties.vsize = priv->display_info.v_screen_size; priv->base.properties.hres = priv->display_info.h_resolution; priv->base.properties.vres = priv->display_info.v_resolution; priv->base.properties.lens_sep = priv->display_info.lens_separation; priv->base.properties.lens_vpos = priv->display_info.v_center; priv->base.properties.fov = DEG_TO_RAD(125.5144f); // TODO calculate. priv->base.properties.ratio = ((float)priv->display_info.h_resolution / (float)priv->display_info.v_resolution) / 2.0f; // calculate projection eye projection matrices from the device properties ohmd_calc_default_proj_matrices(&priv->base.properties); // set up device callbacks priv->base.update = update_device; priv->base.close = close_device; priv->base.getf = getf; // initialize sensor fusion ofusion_init(&priv->sensor_fusion); return &priv->base; cleanup: if(priv) free(priv); return NULL; }
int main(int argc, char* argv[]) { int res; unsigned char buf[256]; #define MAX_STR 255 wchar_t wstr[MAX_STR]; hid_device *handle; int i; #ifdef WIN32 UNREFERENCED_PARAMETER(argc); UNREFERENCED_PARAMETER(argv); #endif struct hid_device_info *devs, *cur_dev; if (hid_init()) return -1; devs = hid_enumerate(0x0, 0x0); cur_dev = devs; while (cur_dev) { printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number); printf("\n"); printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string); printf(" Product: %ls\n", cur_dev->product_string); printf(" Release: %hx\n", cur_dev->release_number); printf(" Interface: %d\n", cur_dev->interface_number); printf("\n"); cur_dev = cur_dev->next; } hid_free_enumeration(devs); // Set up the command buffer. memset(buf,0x00,sizeof(buf)); buf[0] = 0x01; buf[1] = 0x81; // Open the device using the VID, PID, // and optionally the Serial number. ////handle = hid_open(0x4d8, 0x3f, L"12345"); handle = hid_open(0x4d8, 0x3f, NULL); if (!handle) { printf("unable to open device\n"); return 1; } // Read the Manufacturer String wstr[0] = 0x0000; res = hid_get_manufacturer_string(handle, wstr, MAX_STR); if (res < 0) printf("Unable to read manufacturer string\n"); printf("Manufacturer String: %ls\n", wstr); // Read the Product String wstr[0] = 0x0000; res = hid_get_product_string(handle, wstr, MAX_STR); if (res < 0) printf("Unable to read product string\n"); printf("Product String: %ls\n", wstr); // Read the Serial Number String wstr[0] = 0x0000; res = hid_get_serial_number_string(handle, wstr, MAX_STR); if (res < 0) printf("Unable to read serial number string\n"); printf("Serial Number String: (%d) %ls", wstr[0], wstr); printf("\n"); // Read Indexed String 1 wstr[0] = 0x0000; res = hid_get_indexed_string(handle, 1, wstr, MAX_STR); if (res < 0) printf("Unable to read indexed string 1\n"); printf("Indexed String 1: %ls\n", wstr); // Set the hid_read() function to be non-blocking. hid_set_nonblocking(handle, 1); // Try to read from the device. There shoud be no // data here, but execution should not block. res = hid_read(handle, buf, 17); // Send a Feature Report to the device buf[0] = 0x2; buf[1] = 0xa0; buf[2] = 0x0a; buf[3] = 0x00; buf[4] = 0x00; res = hid_send_feature_report(handle, buf, 17); if (res < 0) { printf("Unable to send a feature report.\n"); } memset(buf,0,sizeof(buf)); // Read a Feature Report from the device buf[0] = 0x2; res = hid_get_feature_report(handle, buf, sizeof(buf)); if (res < 0) { printf("Unable to get a feature report.\n"); printf("%ls", hid_error(handle)); } else { // Print out the returned buffer. printf("Feature Report\n "); for (i = 0; i < res; i++) printf("%02hhx ", buf[i]); printf("\n"); } memset(buf,0,sizeof(buf)); // Toggle LED (cmd 0x80). The first byte is the report number (0x1). buf[0] = 0x1; buf[1] = 0x80; res = hid_write(handle, buf, 17); if (res < 0) { printf("Unable to write()\n"); printf("Error: %ls\n", hid_error(handle)); } // Request state (cmd 0x81). The first byte is the report number (0x1). buf[0] = 0x1; buf[1] = 0x81; hid_write(handle, buf, 17); if (res < 0) printf("Unable to write() (2)\n"); // Read requested state. hid_read() has been set to be // non-blocking by the call to hid_set_nonblocking() above. // This loop demonstrates the non-blocking nature of hid_read(). res = 0; while (res == 0) { res = hid_read(handle, buf, sizeof(buf)); if (res == 0) printf("waiting...\n"); if (res < 0) printf("Unable to read()\n"); #ifdef WIN32 Sleep(500); #else usleep(500*1000); #endif } printf("Data read:\n "); // Print out the returned buffer. for (i = 0; i < res; i++) printf("%02hhx ", buf[i]); printf("\n"); hid_close(handle); /* Free static HIDAPI objects. */ hid_exit(); #ifdef WIN32 system("pause"); #endif return 0; }
static ohmd_device* open_device(ohmd_driver* driver, ohmd_device_desc* desc) { rift_priv* priv = ohmd_alloc(driver->ctx, sizeof(rift_priv)); if(!priv) goto cleanup; priv->base.ctx = driver->ctx; // Open the HID device priv->handle = hid_open_path(desc->path); if(!priv->handle) { char* path = _hid_to_unix_path(desc->path); ohmd_set_error(driver->ctx, "Could not open %s. " "Check your rights.", path); free(path); goto cleanup; } if(hid_set_nonblocking(priv->handle, 1) == -1){ ohmd_set_error(driver->ctx, "failed to set non-blocking on device"); goto cleanup; } unsigned char buf[FEATURE_BUFFER_SIZE]; int size; // if the sensor has display info data, use HMD coordinate frame priv->coordinate_frame = priv->display_info.distortion_type != RIFT_DT_NONE ? RIFT_CF_HMD : RIFT_CF_SENSOR; // enable calibration SETFLAG(priv->sensor_config.flags, RIFT_SCF_USE_CALIBRATION, 1); SETFLAG(priv->sensor_config.flags, RIFT_SCF_AUTO_CALIBRATION, 1); // apply sensor config set_coordinate_frame(priv, priv->coordinate_frame); // set keep alive interval to n seconds pkt_keep_alive keep_alive = { 0, KEEP_ALIVE_VALUE }; size = dp_encode_keep_alive(buf, &keep_alive); send_feature_report(priv, buf, size); // Update the time of the last keep alive we have sent. priv->last_keep_alive = ohmd_get_tick(); // Set default device properties ohmd_set_default_device_properties(&priv->base.properties); // Set device properties //NOTE: These values are estimations, no one has taken one appart to check priv->base.properties.hsize = 0.1698f; priv->base.properties.vsize = 0.0936f; priv->base.properties.hres = 1920; priv->base.properties.vres = 1080; priv->base.properties.lens_sep = 0.0849f; priv->base.properties.lens_vpos = 0.0468f;; priv->base.properties.fov = DEG_TO_RAD(110.0); // TODO calculate. priv->base.properties.ratio = ((float)1920 / (float)1080) / 2.0f; // calculate projection eye projection matrices from the device properties ohmd_calc_default_proj_matrices(&priv->base.properties); // set up device callbacks priv->base.update = update_device; priv->base.close = close_device; priv->base.getf = getf; // initialize sensor fusion ofusion_init(&priv->sensor_fusion); return &priv->base; cleanup: if(priv) free(priv); return NULL; }
bool PSNaviController::open( const DeviceEnumerator *enumerator) { const ControllerDeviceEnumerator *pEnum = static_cast<const ControllerDeviceEnumerator *>(enumerator); const char *cur_dev_path= pEnum->get_path(); bool success= false; if (getIsOpen()) { SERVER_LOG_WARNING("PSNaviController::open") << "PSNavoController(" << cur_dev_path << ") already open. Ignoring request."; success= true; } else { char cur_dev_serial_number[256]; SERVER_LOG_INFO("PSNaviController::open") << "Opening PSNaviController(" << cur_dev_path << ")"; if (pEnum->get_serial_number(cur_dev_serial_number, sizeof(cur_dev_serial_number))) { SERVER_LOG_INFO("PSNaviController::open") << " with serial_number: " << cur_dev_serial_number; } else { cur_dev_serial_number[0]= '\0'; SERVER_LOG_INFO("PSNaviController::open") << " with EMPTY serial_number"; } HIDDetails.Device_path = cur_dev_path; #ifdef _WIN32 HIDDetails.Device_path_addr = HIDDetails.Device_path; HIDDetails.Device_path_addr.replace(HIDDetails.Device_path_addr.find("&col01#"), 7, "&col02#"); HIDDetails.Device_path_addr.replace(HIDDetails.Device_path_addr.find("&0000#"), 6, "&0001#"); HIDDetails.Handle_addr = hid_open_path(HIDDetails.Device_path_addr.c_str()); hid_set_nonblocking(HIDDetails.Handle_addr, 1); #endif HIDDetails.Handle = hid_open_path(HIDDetails.Device_path.c_str()); hid_set_nonblocking(HIDDetails.Handle, 1); IsBluetooth = (strlen(cur_dev_serial_number) > 0); if (getIsOpen()) // Controller was opened and has an index { // Get the bluetooth address #ifndef _WIN32 // On my Mac, getting the bt feature report when connected via // bt crashes the controller. So we simply copy the serial number. // It gets modified in getBTAddress. // TODO: Copy this over anyway even in Windows. Check getBTAddress // comments for handling windows serial_number. // Once done, we can remove the ifndef above. std::string mbs(cur_dev_serial_number); HIDDetails.Bt_addr = mbs; #endif if (getBTAddress(HIDDetails.Host_bt_addr, HIDDetails.Bt_addr)) { // Load the config file std::string btaddr = HIDDetails.Bt_addr; std::replace(btaddr.begin(), btaddr.end(), ':', '_'); cfg = PSNaviControllerConfig(btaddr); cfg.load(); // TODO: Other startup. success= true; } else { // If serial is still bad, maybe we have a disconnected // controller still showing up in hidapi SERVER_LOG_ERROR("PSNaviController::open") << "Failed to get bluetooth address of PSNaviController(" << cur_dev_path << ")"; success= false; } // Reset the polling sequence counter NextPollSequenceNumber= 0; } else { SERVER_LOG_ERROR("PSNaviController::open") << "Failed to open PSNaviController(" << cur_dev_path << ")"; success= false; } } return success; }
int main(int argc, char* argv[]) { int res; unsigned char buf[65]; #define MAX_STR 255 wchar_t wstr[MAX_STR]; hid_device *handle; int i; // Enumerate and print the HID devices on the system struct hid_device_info *devs, *cur_dev; devs = hid_enumerate(0x0, 0x0); cur_dev = devs; while (cur_dev) { printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number); printf("\n"); printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string); printf(" Product: %ls\n", cur_dev->product_string); printf("\n"); cur_dev = cur_dev->next; } hid_free_enumeration(devs); // Open the device using the VID, PID, // and optionally the Serial number. handle = hid_open(0x8021, 0x1941, NULL); // Read the Manufacturer String res = hid_get_manufacturer_string(handle, wstr, MAX_STR); printf("Manufacturer String: %ls\n", wstr); // Read the Product String res = hid_get_product_string(handle, wstr, MAX_STR); printf("Product String: %ls\n", wstr); // Read the Serial Number String res = hid_get_serial_number_string(handle, wstr, MAX_STR); printf("Serial Number String: %ls", wstr); printf("\n"); // Send a Feature Report to the device buf[0] = 0x2; // First byte is report number buf[1] = 0xa0; buf[2] = 0x0a; res = hid_send_feature_report(handle, buf, 17); // Read a Feature Report from the device buf[0] = 0x2; res = hid_get_feature_report(handle, buf, sizeof(buf)); // Print out the returned buffer. printf("Feature Report\n "); for (i = 0; i < res; i++) printf("%02hhx ", buf[i]); printf("\n"); // Set the hid_read() function to be non-blocking. hid_set_nonblocking(handle, 1); // Send an Output report to toggle the LED (cmd 0x80) buf[0] = 1; // First byte is report number buf[1] = 0x80; res = hid_write(handle, buf, 65); // Send an Output report to request the state (cmd 0x81) buf[1] = 0x81; hid_write(handle, buf, 65); // Read requested state res = hid_read(handle, buf, 65); if (res < 0) printf("Unable to read()\n"); // Print out the returned buffer. for (i = 0; i < res; i++) printf("buf[%d]: %d\n", i, buf[i]); return 0; }
int run(){ int res; unsigned char buf[128]; #define MAX_STR 255 wchar_t wstr[MAX_STR]; hid_device *handle; int fd; memset(buf,0x00,sizeof(buf)); handle = hid_open(VENDOR, PRODUCT, NULL); if (!handle) { printf("unable to open device\n"); return 1; } // Read the Manufacturer String wstr[0] = 0x0000; res = hid_get_manufacturer_string(handle, wstr, MAX_STR); if (res < 0) printf("Unable to read manufacturer string\n"); printf("Vendor: %ls\n", wstr); // Read the Product String wstr[0] = 0x0000; res = hid_get_product_string(handle, wstr, MAX_STR); if (res < 0) printf("Unable to read product string\n"); printf("Product: %ls\n", wstr); fd = uinput_connect(wstr); // Read the Serial Number String wstr[0] = 0x0000; res = hid_get_serial_number_string(handle, wstr, MAX_STR); if (res < 0) printf("Unable to read serial number string\n"); printf("Serial: %ls", wstr); printf("\n"); // Read Indexed String 1 wstr[0] = 0x0000; res = hid_get_indexed_string(handle, 1, wstr, MAX_STR); if (res < 0) printf("Unable to read indexed string 1\n"); printf("Indexed: %ls\n", wstr); // Set the hid_read() function to be non-blocking. hid_set_nonblocking(handle, 1); res = 0; while (1) { res = hid_read(handle, buf, sizeof(buf)); if (res>0){ touch(fd, buf, res); }else if (res<0){ break; } if (exitNow) break; usleep(10000); } hid_close(handle); /* Free static HIDAPI objects. */ hid_exit(); return 0; }
bool PSDualShock4Controller::open( const DeviceEnumerator *enumerator) { const ControllerDeviceEnumerator *pEnum = static_cast<const ControllerDeviceEnumerator *>(enumerator); const char *cur_dev_path = pEnum->get_path(); bool success = false; if (getIsOpen()) { SERVER_LOG_WARNING("PSDualShock4Controller::open") << "PSDualShock4Controller(" << cur_dev_path << ") already open. Ignoring request."; success = true; } else { char cur_dev_serial_number[256]; SERVER_LOG_INFO("PSDualShock4Controller::open") << "Opening PSDualShock4Controller(" << cur_dev_path << ")"; if (pEnum->get_serial_number(cur_dev_serial_number, sizeof(cur_dev_serial_number))) { SERVER_LOG_INFO("PSDualShock4Controller::open") << " with serial_number: " << cur_dev_serial_number; } else { cur_dev_serial_number[0] = '\0'; SERVER_LOG_INFO("PSDualShock4Controller::open") << " with EMPTY serial_number"; } // Attempt to open the controller HIDDetails.vendor_id = pEnum->get_vendor_id(); HIDDetails.product_id = pEnum->get_product_id(); HIDDetails.Device_path = cur_dev_path; HIDDetails.Handle = hid_open_path(HIDDetails.Device_path.c_str()); if (HIDDetails.Handle != nullptr) // Controller was opened and has an index { // Don't block on hid report requests hid_set_nonblocking(HIDDetails.Handle, 1); /* -USB or Bluetooth Device- On my Mac, using bluetooth, cur_dev->path = Bluetooth_054c_03d5_779732e8 cur_dev->serial_number = 00-06-f7-97-32-e8 On my Mac, using USB, cur_dev->path = USB_054c_03d5_14100000 cur_dev->serial_number = "" (not null, just empty) On my Windows 10 box (different controller), using bluetooth cur_dev->path = \\?\hid#{00001124-0000-1000-8000-00805f9b34fb}_vid&0002054c_pid&05c4#8&217a4584&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030} cur_dev->serial_number = 1c666d2c8deb Using USB cur_dev->path = \\?\hid#vid_054c&pid_03d5&col01#6&7773e57&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030} cur_dev->serial_number = (null) */ // Bluetooth connected if (strlen(cur_dev_serial_number) > 0) { IsBluetooth = true; // Convert the serial number into a normalized bluetooth address of the form: "xx:xx:xx:xx:xx:xx" char szNormalizedControllerAddress[18]; ServerUtility::bluetooth_cstr_address_normalize( cur_dev_serial_number, true, ':', szNormalizedControllerAddress, sizeof(szNormalizedControllerAddress)); // Save the controller address as a std::string HIDDetails.Bt_addr = std::string(szNormalizedControllerAddress); // Get the (possibly cached) bluetooth address of the first bluetooth adapter if (!bluetooth_get_host_address(HIDDetails.Host_bt_addr)) { HIDDetails.Host_bt_addr= "00:00:00:00:00:00"; } success = true; } // USB Connected else { IsBluetooth = false; // Fetch the bluetooth host and controller addresses via USB HID report request success = getBTAddressesViaUSB(HIDDetails.Host_bt_addr, HIDDetails.Bt_addr); if (!success) { // If serial is still bad, maybe we have a disconnected // controller still showing up in hidapi SERVER_LOG_ERROR("PSDualShock4Controller::open") << "Failed to get bluetooth address of PSDualShock4Controller(" << cur_dev_path << ")"; } } if (success) { // Build a unique name for the config file using bluetooth address of the controller char szConfigSuffix[18]; ServerUtility::bluetooth_cstr_address_normalize( HIDDetails.Bt_addr.c_str(), true, '_', szConfigSuffix, sizeof(szConfigSuffix)); std::string config_name("dualshock4_"); config_name += szConfigSuffix; // Load the config file cfg = PSDualShock4ControllerConfig(config_name); cfg.load(); // Save it back out again in case any defaults changed cfg.save(); } // Reset the polling sequence counter NextPollSequenceNumber = 0; // Write out the initial controller state if (success && IsBluetooth) { bWriteStateDirty= true; writeDataOut(); } } else { SERVER_LOG_ERROR("PSDualShock4Controller::open") << "Failed to open PSDualShock4Controller(" << cur_dev_path << ")"; success = false; } } return success; }
static ohmd_device* open_device(ohmd_driver* driver, ohmd_device_desc* desc) { rift_priv* priv = ohmd_alloc(driver->ctx, sizeof(rift_priv)); if(!priv) goto cleanup; priv->base.ctx = driver->ctx; // Open the HID device priv->handle = hid_open_path(desc->path); if(!priv->handle) goto cleanup; if(hid_set_nonblocking(priv->handle, 1) == -1){ ohmd_set_error(driver->ctx, "failed to set non-blocking on device"); goto cleanup; } unsigned char buf[FEATURE_BUFFER_SIZE]; int size; // Set default values. ohmd_set_default_device_properties(&priv->base); // Read and decode the sensor range size = get_feature_report(priv, RIFT_CMD_RANGE, buf); decode_sensor_range(&priv->sensor_range, buf, size); dump_packet_sensor_range(&priv->sensor_range); // Read and decode display information size = get_feature_report(priv, RIFT_CMD_DISPLAY_INFO, buf); decode_sensor_display_info(&priv->display_info, buf, size); dump_packet_sensor_display_info(&priv->display_info); // Read and decode the sensor config size = get_feature_report(priv, RIFT_CMD_SENSOR_CONFIG, buf); decode_sensor_config(&priv->sensor_config, buf, size); dump_packet_sensor_config(&priv->sensor_config); // if the sensor has display info data, use HMD coordinate frame priv->coordinate_frame = priv->display_info.distortion_type != RIFT_DT_NONE ? RIFT_CF_HMD : RIFT_CF_SENSOR; // apply sensor config set_coordinate_frame(priv, priv->coordinate_frame); // set keep alive interval to n seconds pkt_keep_alive keep_alive = { 0, KEEP_ALIVE_VALUE }; size = encode_keep_alive(buf, &keep_alive); send_feature_report(priv, buf, size); // Update the time of the last keep alive we have sent. priv->last_keep_alive = ohmd_get_tick(); // update sensor settings with new keep alive value // (which will have been ignored in favor of the default 1000 ms one) size = get_feature_report(priv, RIFT_CMD_SENSOR_CONFIG, buf); decode_sensor_config(&priv->sensor_config, buf, size); dump_packet_sensor_config(&priv->sensor_config); // Also sets the exported values. calc_derived_values(priv); priv->base.update = update_device; priv->base.close = close_device; priv->base.getf = getf; ofusion_init(&priv->sensor_fusion); return &priv->base; cleanup: if(priv) free(priv); return NULL; }