char* minicut::read1s() { this->buffer=(unsigned char*)malloc(36*sizeof(unsigned char)); hid_read_timeout(minicut_device,this->buffer,sizeof(buffer),1000); return (char*)buffer; }
JNIEXPORT jint JNICALL Java_com_codeminders_hidapi_HIDDevice_readTimeout (JNIEnv *env, jobject self, jbyteArray data, jint milliseconds ) { hid_device *peer = getPeer(env, self); if(!peer) { throwIOException(env, peer); return 0; /* not an error, freed previously */ } jsize bufsize = env->GetArrayLength(data); jbyte *buf = env->GetByteArrayElements(data, NULL); int read = hid_read_timeout(peer, (unsigned char*) buf, bufsize, milliseconds); env->ReleaseByteArrayElements(data, buf, read==-1?JNI_ABORT:0); if(read == 0) /* time out */ { return 0; } else if(read == -1) { throwIOException(env, peer); return 0; /* not an error, freed previously */ } return read; }
int read_timeout(hid_device *device, unsigned char *data, size_t length, int milliseconds) { return hid_executor->await([=] { return hid_read_timeout(device, data, length, milliseconds); }); }
void RawHIDReadThread::run() { while(m_running) { //here we use a temporary buffer so we don't need to lock //the mutex while we are reading from the device // Want to read in regular chunks that match the packet size the device // is using. In this case it is 64 bytes (the interrupt packet limit) // although it would be nice if the device had a different report to // configure this unsigned char buffer[READ_SIZE] = {0}; int ret = hid_read_timeout(m_hid->m_handle, buffer, READ_SIZE, READ_TIMEOUT); if(ret > 0) //read some data { QMutexLocker lock(&m_readBufMtx); // Note: Preprocess the USB packets in this OS independent code // First byte is report ID, second byte is the number of valid bytes m_readBuffer.append((char *) &buffer[2], buffer[1]); emit m_hid->readyRead(); } else if(ret == 0) //nothing read { } else // < 0 => error { //TODO! make proper error handling, this only quick hack for unplug freeze m_running=false; } } }
void openDevice(hid_dev_desc * desc, std::atomic<bool> &shouldBeRunning) { trace("open device %p\n"); std::lock_guard<std::mutex> lock(guard); if (map.find(desc) != map.end()) // thread already polling device return; boost::thread * deviceThread = threads.create_thread( [=, &shouldBeRunning] { trace("start polling thread for %d\n", desc); while( true ) { unsigned char buf[256]; int res = hid_read_timeout( desc->device, buf, sizeof(buf), 250); if ( res > 0 ) { hid_parse_input_report( buf, res, desc ); } else if (res == -1) { trace("device thread interrupted \n"); hid_throw_readerror( desc ); trace("device thread closed device \n"); return; } } std::lock_guard<std::mutex> lock_(guard); auto it = map.find(desc); threads.remove_thread(it->second); map.erase(it); }); map.insert( std::make_pair(desc, deviceThread) ); }
int USB_Read() { if(DeviceHandle == NULL) return -1; return hid_read_timeout(DeviceHandle, InputBuffer, USB_MIN_PACKET_SIZE+1, 2000); }
bool PortalIO::CheckResponse (RWBlock *res, char expect) throw (int) { int b = hid_read_timeout(hPortalHandle, res->buf, rw_buf_size, TIMEOUT); if(b<0) throw 8; res->dwBytesTransferred = b; // this is here to debug the different responses from the portal. /* SkylanderIO *skio; skio = new SkylanderIO(); printf("<<<\n"); skio->fprinthex(stdout,res->buf, 0x21); delete skio; */ // found wireless USB but portal is not connected if (res->buf[0] == 'Z') throw 9; // Status says no skylander on portal if (res->buf[0] == 'Q' && res->buf[1] == 0) throw 11; return (res->buf[0] != expect); }
void vmarker::condisHandler(){ if(this->connected){ //CHECK IF DISCONNECTED unsigned char *data; if(hid_read_timeout(this->vmarkerdev,data,0,200)<0){ this->connected = false; qDebug("Vmarker Disconnected"); emit(Disconnect()); hid_close(this->vmarkerdev); } }else{ //SEARCH FOR VMARKER struct hid_device_info *devs,*cur_dev; int i = 0; for(i = 0; i<numPIDlist;i++){ devs = hid_enumerate(VID,PIDlist[i]); cur_dev = devs; while(cur_dev){ qDebug(cur_dev->path); if(cur_dev->interface_number == MIlist[i]){ this->vmarkerdev = hid_open_path(cur_dev->path); if(this->vmarkerdev){ this->productID = PIDlist[i]; this->connected = true; qDebug("Vmarker Connected"); emit (Connected()); } } cur_dev = cur_dev->next; } hid_free_enumeration(devs); } } }
bool vmarker::writereg(uint8_t reg, uint16_t *data){ if(this->connected){ unsigned char buf[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; buf[1] = 0x05; buf[2] = reg; buf[3] = *data >> 8; buf[4] = *data; if(hid_write(this->vmarkerdev,buf,9)<0) return false; if(hid_read_timeout(this->vmarkerdev,buf,9,200)<0) return false; return true; }else{
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 HIDDMXDevice::run() { while(m_running == true) { unsigned char buffer[35]; int size; size = hid_read_timeout(m_handle, buffer, 33, HID_DMX_READ_TIMEOUT); /** * Protocol: 33 bytes in buffer[33] * [0] = chunk, which is the offset by which the channel is calculated * from, the nth chunk starts at address n * 32 * [1]-[32] = channel values, where the nth value is the offset + n */ while(size > 0) { if(size == 33) { unsigned short startOff = buffer[0] * 32; if (buffer[0] < 16) { for (int i = 0; i < 32; i++) { unsigned short channel = startOff + i; unsigned char value = buffer[i + 1]; if ((unsigned char)m_dmx_in_cmp.at(channel) != value) { emit valueChanged(UINT_MAX, m_line, channel, value); m_dmx_in_cmp[channel] = value; } } } } size = hid_read_timeout(m_handle, buffer, 33, HID_DMX_READ_TIMEOUT); } } }
bool vmarker::readreg(uint8_t reg, uint16_t *data){ if(this->connected){ unsigned char buf[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; buf[1] = 0x04; buf[2] = reg; if(hid_write(this->vmarkerdev,buf,9)<0) return false; if(hid_read_timeout(this->vmarkerdev,buf,9,200)<0) return false; *data = (buf[1]<<8)+buf[2]; qDebug() << (uint16_t)buf[0] << " " << (uint16_t)buf[1] << " " << (uint16_t)buf[2] << " " << (uint16_t)buf[3] << " " << (uint16_t)buf[4] << " "; return true; }else{ return false; } }
int usbDialog::sendCMD(unsigned char *cmd) { int rc; rc = hid_write(dev, cmd, 1 + cmd[1]); dumpRawData(true, rc, cmd); rc = hid_read_timeout(dev, rawdata, sizeof(rawdata), 1000); dumpRawData(false, rc, rawdata); return rc; }
int ReadInput(glowproxy_device* handle, InputReport* reportBuffer) { //TODO: add last-report filtering if (handle == NULL) return -1; int result; unsigned char buffer[25]; memset(buffer, 0, sizeof(buffer)); buffer[0] = 0x00; result = hid_read_timeout(handle, buffer, 25, READWAIT_MS); if (result > 0) { memcpy(reportBuffer, &buffer, 25); } return result; }
std::size_t receive(unsigned char* buffer, std::size_t bufferSize) { const int size = hid_read_timeout(handle_, buffer, bufferSize, rxTimeout_); if (size < 0) { const int errorCode = hid_last_error_code(handle_); if (errorCode == ERROR_DEVICE_NOT_CONNECTED) { throw IOException("Interface not connected"); } else { throw IOException(("USB driver: Receive Error (error %d)", errorCode)); } } return size; }
int WiimoteHidapi::IORead(u8* buf) { int timeout = 200; // ms int result = hid_read_timeout(m_handle, buf + 1, MAX_PAYLOAD - 1, timeout); // TODO: If and once we use hidapi across plaforms, change our internal API to clean up this mess. if (result == -1) { ERROR_LOG(WIIMOTE, "Failed to read from %s.", m_device_path.c_str()); return 0; // error } if (result == 0) { return -1; // didn't read packet } buf[0] = WR_SET_REPORT | BT_INPUT; return result + 1; // number of bytes read }
char *zdpms_read_model(hid_device *dev, char *buf, size_t sz) { uint8_t msg[8], resp[32]; int result; if (dev == NULL || buf == NULL || sz <= 0) { errno = EINVAL; return NULL; } msg[0] = ZDPMS_GET; msg[1] = 0x2; /* 2-byte message */ msg[2] = 0x40; /* nope, still no idea what this is for. */ msg[3] = ZDPMS_MODEL_NUMBER; result = hid_write(dev, msg, 4); if (result < 4) { return NULL; } result = hid_read_timeout(dev, resp, 32, ZDPMS_TIMEOUT); if (result < 6 || resp[1] + 2 >= result || resp[0] != msg[0] || resp[2] != msg[2] || resp[3] != msg[3] || resp[resp[1]+1] != resp[0]) { return NULL; } if (resp[1] - 3 < sz) { /* Good news, we can fit the entire model name in the supplied buffer, * complete with a NULL terminator. */ memcpy(buf, resp + 4, resp[1] - 3); buf[resp[1] - 3] = '\0'; } else { /* Whoops, we can only fit in part of it. */ memcpy(buf, resp + 4, sz); } return buf; }
void HidReader::run() { m_stop = 0; unsigned char *data = new unsigned char[255]; while (m_stop == 0) { // Blocked polling: The only problem with this is that we can't close // the device until the block is released, which means the controller // has to send more data //result = hid_read_timeout(m_pHidDevice, data, 255, -1); // This relieves that at the cost of higher CPU usage since we only // block for a short while (500ms) int result = hid_read_timeout(m_pHidDevice, data, 255, 500); if (result > 0) { //qDebug() << "Read" << result << "bytes, pointer:" << data; QByteArray outData(reinterpret_cast<char*>(data), result); emit(incomingData(outData)); } } delete [] data; }
/* Set a specific ZDPMS configuration item. Currently-known configuration * commands only take a one-byte configuration value. */ int zdpms_set(hid_device *dev, uint8_t cmd, uint8_t val) { uint8_t msg[8], resp[8]; int result; if (dev == NULL) { return -1; } msg[0] = ZDPMS_SET; msg[1] = 0x03; /* 3-byte message */ msg[2] = 0x40; /* no idea what this is supposed to mean */ msg[3] = cmd; /* Command value */ msg[4] = val; result = hid_write(dev, msg, 5); if (result < 0) { return result; } if (result < 5) { return -1; } result = hid_read_timeout(dev, resp, 6, ZDPMS_TIMEOUT); if (result < 0) { return result; } if (result < 6 || resp[0] != msg[0] || resp[1] != 0x04 || resp[2] != msg[2] || resp[3] != msg[3] || resp[4] != msg[4] || resp[5] != resp[0]) { return -1; } return result; }
/** *** read_program() *** Read the program memory content **/ int read_program(hid_device* handle, unsigned char *program) { unsigned char hid_buf[FAN_SIZE]; int seen[FAN_STEPS], leds, servo, idx, res, count; memset(seen, 0, sizeof(seen)); hid_flush(handle); for(int j=0;j<100; j++) { res = hid_read_timeout(handle, hid_buf, sizeof(hid_buf), 100); // printf(".\n"); if ( res < 0 ) { return res; } idx = hid_buf[48]; leds = hid_buf[49]; servo = hid_buf[50]; // printf("%d %d %d %d\n", count, idx, leds, servo); if ( idx >=0 && idx <= FAN_STEPS ) { seen[idx] = 1; program[2*idx] = leds; program[2*idx+1] = servo; } count = 0; for(int i=0; i<FAN_STEPS; i++) count += seen[i]; if ( count == FAN_STEPS ) { return 0; } } }
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length) { return hid_read_timeout(dev, data, length, (dev->blocking)? -1: 0); }
int boca_hid_read_timeout(boca_hid_printer_t *self, unsigned char *buf, size_t byte_to_read, int milliseconds) { if (!self->device) return -1; int ret = hid_read_timeout(self->device, buf, byte_to_read, milliseconds); return ret; }
/** * Read boca status */ enum BOCA_STATUS boca_hid_status(boca_hid_printer_t *self) { enum BOCA_STATUS ret = BOCA_UNKNOWN; if (!self->device) return ret; unsigned char buf[2] = ""; int bytes_read = 0; bytes_read = hid_read_timeout(self->device, buf, sizeof(char), 200); if (bytes_read > 0) { int status = buf[0]; switch (status) { case 6: ret = BOCA_TICKET_ACK; break; case 9: ret = BOCA_INVALID_CHECKSUM; break; case 16: ret = BOCA_OUT_OF_PAPER; break; case 17: ret = BOCA_X_ON; break; case 18: ret = BOCA_POWER_ON; break; case 19: ret = BOCA_X_OFF; break; case 21: ret = BOCA_NAK; break; case 24: ret = BOCA_PAPER_JAM; break; case 25: ret = BOCA_ILLEGAL_DATA; break; case 26: ret = BOCA_POWERUP_PROBLEM; break; case 28: ret = BOCA_DOWNLOADING_ERROR; break; case 29: ret = BOCA_CUTTER_JAM; break; case 30: ret = BOCA_STUCK_TICKET; break; default: zsys_error("hid printer: unknown BOCA status [%d]", status); break; } if (self->verbose) { zsys_debug("hid printer: boca status is %d %s", ret, boca_status_display(ret)); } } if (bytes_read == -1) { zsys_error("hid printer: error reading hid printer, %ls", hid_error(self->device)); ret = BOCA_OFFLINE; } return ret; }
// TODO: Implement blocking int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length) { LOGV( "hid_read id=%d length=%u", device->nId, length ); return hid_read_timeout( device, data, length, 0 ); }
void mac_inkling_listener(hid_device *handle, char* ipAddress, int port) { // Some kind of handshaking. // Values obtained by sniffing the USB connection between SketchManager and the device. unsigned char usb_data[33]; memset (&usb_data, '\0', 33); memcpy (&usb_data, "\x80\x01\x03\x01\x02\x00\x00\x00", 8); int bytes = 0; bytes += hid_send_feature_report(handle, usb_data, 33); memcpy (&usb_data, "\x80\x01\x0a\x01\x01\x0b\x01\x00", 8); bytes += hid_send_feature_report(handle, usb_data, 33); memset (&usb_data, '\0', 33); bytes += hid_send_feature_report(handle, usb_data, 33); memcpy (&usb_data, "\x80\x01\x0b\x01\x00\x00\x00\x00", 8); bytes += hid_send_feature_report(handle, usb_data, 33); memcpy (&usb_data, "\x80\x01\x02\x01\x01\x00\x00\x00", 8); bytes += hid_send_feature_report(handle, usb_data, 33); memcpy (&usb_data, "\x80\x01\x0a\x01\x01\x02\x01\x00", 8); bytes += hid_send_feature_report(handle, usb_data, 33); memset (&usb_data, '\0', 33); bytes += hid_send_feature_report(handle, usb_data, 33); // Assume that the incorrect amount of bytes returned indicates a // handshake failure. if (bytes != 163) { puts ("Device handshake failed."); printf("bytes:%i\n", bytes); goto device_release; } //SET UP THE SOCKET TO BROADCAST DATA int sockfd = 0; struct sockaddr_in remoteAddr; sockfd = sizeof(remoteAddr); if((sockfd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) { puts("Socket failed to open"); return; } memset((char *) &remoteAddr, 0, sizeof(remoteAddr)); remoteAddr.sin_family = AF_INET; remoteAddr.sin_addr.s_addr = inet_addr(ipAddress); remoteAddr.sin_port = htons(port); if(inet_aton(ipAddress, &remoteAddr.sin_addr) == 0) { fprintf(stderr, "inet_aton() failed\n"); exit(1); } //THIS CAST MIGHT NOT BE VALID - If something goes wrong check here connect(sockfd, (struct sockaddr *)&remoteAddr, sizeof(remoteAddr)); int error = 0; socklen_t len = sizeof(error); int retval = getsockopt (sockfd, SOL_SOCKET, SO_ERROR, &error, &len); if(retval != 0){ printf("%s","error getting socket code"); } if(error != 0){ printf("%s", "socket error"); } //Listen for data while (1) { // Allocate some space for receiving data. unsigned char data[10]; memset (&data, '\0', 10); // Request coordinate and pressure data. //puts("reading bytes"); int bytesRead = hid_read_timeout(handle, data, 10, 1000); //printf("read:%i\n", bytesRead); if (bytesRead == -1) { puts ("Device disconnected."); goto device_release; } // Only process complete packets. if (bytesRead != 10) continue; // Only process "Pen" packets. if (data[0] != 0x02) continue; // The coordinate are segmented in 7 pieces of 1 byte. // An extra byte indicates which in which segment the pen is. // Thus, the right value can be obtained by this formula: // segment * 2^8 + x int x = data[2] * 256 + data[1]; int y = (data[4] * 256 + data[3]) * -1; int tilt_x = data[8]; int tilt_y = data[9]; // Same formula applies to pressure data. // segment * 2^8 + pressure // "segment" can vary between 0 and 3, giving us values // between 0 and 1024. int pressure = data[6]; pressure = pressure + 256 * data[7]; //Send the data over OSC dt_inkling_osc oscData; oscData.coordinate.x = x; oscData.coordinate.y = y; oscData.coordinate.pressure = pressure; oscData.tilt.x = tilt_x; oscData.tilt.y = tilt_y; sendInklingOSC(sockfd, oscData); } device_release: hid_close(handle); }
/* Read a ZDPMS sensor. */ float zdpms_read_sensor(hid_device *dev, uint8_t sensor) { uint8_t msg[8], resp[8]; int result; uint32_t val; unsigned int exponent; if (dev == NULL) { return NAN; } msg[0] = ZDPMS_GET; msg[1] = 0x2; /* 2-byte message */ msg[2] = 0x40; /* no idea what this is supposed to mean */ msg[3] = sensor; /* Sensor value */ result = hid_write(dev, msg, 4); if (result < 0) { return NAN; } if (result < 4) { return NAN; } result = hid_read_timeout(dev, resp, 7, ZDPMS_TIMEOUT); if (result < 7 || resp[0] != msg[0] || resp[1] != 5 || resp[2] != msg[2] || resp[3] != msg[3] || resp[6] != resp[0]) { return NAN; } /* The sensor value is encoded as little-endian quasi-IEEE 754, in * bytes 4 and 5. */ val = ((resp[5] & 7) << 8) | resp[4]; exponent = resp[5] >> 3; /* We are't sure yet if the mantissa really extends to 11 bits. Bit 11 * may belong to the exponent field instead... */ if (val > 1023) { fprintf(stderr, "WARNING: sensor mantissa %u is greater than 1023?\n", val); } /* We can turn this into a 32-bit fixed point representation by shifting * it to the left based on the exponent minus 16. However, if the * exponent is less than 16, I'm not sure what to do with it... */ if (exponent < 16) { fprintf(stderr, "WARNING: sensor exponent %u is less than 16?\n", exponent); } else { exponent -= 16; } val <<= exponent; /* And divide it by 65536.0 to turn it into an equivalent floating-point * representation. */ return (val / 65536.0); }
/** Method for getting the subtype ID from HID devices that use string IDs. */ bool tempered_type_hid_get_subtype_id_from_string( tempered_device* device, unsigned char* subtype_id ) { struct tempered_type_hid_subtype_from_string_data *subtype_data = (struct tempered_type_hid_subtype_from_string_data*) device->type->get_subtype_data; if ( subtype_data == NULL ) { // We don't have the necessary data, so pretend we got subtype 0. *subtype_id = 0; return true; } struct tempered_type_hid_query_result result; if ( !tempered_type_hid_query( device, &subtype_data->query, &result ) ) { return false; } int string_length = result.length; char subtype_string[64]; if ( result.length > 0 ) { memcpy(subtype_string, &result.data, result.length); } struct tempered_type_hid_query next_response_query = { .length = -1 }; for ( int i = 1 ; i < subtype_data->response_count ; i++ ) { if ( !tempered_type_hid_query( device, &next_response_query, &result ) ) { return false; } if ( string_length + result.length >= 64 ) { tempered_set_error( device, strdup( "The subtype string was too long." ) ); return false; } if ( result.length > 0 ) { memcpy(&(subtype_string[string_length]), &result.data, result.length); string_length += result.length; } } subtype_string[string_length] = '\0'; for ( int i = 0 ; subtype_data->subtype_strings[i] != NULL ; i++ ) { if ( strcmp(subtype_string, subtype_data->subtype_strings[i]) == 0 ) { // Found the subtype, use the array index as the subtype ID. *subtype_id = i; return true; } } int size = snprintf( NULL, 0, "Unknown device subtype string: %s", subtype_string ); // TODO: check that size >= 0 size++; char *error = malloc( size ); size = snprintf( error, size, "Unknown device subtype string: %s", subtype_string ); tempered_set_error( device, error ); return false; } bool tempered_type_hid_read_sensors( tempered_device* device ) { struct temper_subtype_hid *subtype = (struct temper_subtype_hid *) device->subtype; struct tempered_type_hid_device_data *device_data = (struct tempered_type_hid_device_data *) device->data; int i; for ( i = 0; i < subtype->sensor_group_count ; i++ ) { struct tempered_type_hid_sensor_group *group = &subtype->sensor_groups[i]; struct tempered_type_hid_query_result *group_data = &device_data->group_data[i]; if ( !group->read_sensors( device, group, group_data ) ) { return false; } } return true; } bool tempered_type_hid_read_sensor_group( tempered_device* device, struct tempered_type_hid_sensor_group* group, struct tempered_type_hid_query_result* group_data ) { return tempered_type_hid_query( device, &group->query, group_data ); } bool tempered_type_hid_query( tempered_device* device, struct tempered_type_hid_query* query, struct tempered_type_hid_query_result* result ) { struct tempered_type_hid_device_data *device_data = (struct tempered_type_hid_device_data *) device->data; hid_device *hid_dev = device_data->hid_dev; int size; if ( query->length >= 0 ) { size = hid_write( hid_dev, query->data, query->length ); if ( size <= 0 ) { size = snprintf( NULL, 0, "HID write failed: %ls", hid_error( hid_dev ) ); // TODO: check that size >= 0 size++; char *error = malloc( size ); size = snprintf( error, size, "HID write failed: %ls", hid_error( hid_dev ) ); tempered_set_error( device, error ); result->length = 0; return false; } } size = hid_read_timeout( hid_dev, result->data, sizeof( result->data ), 1000 ); if ( size < 0 ) { size = snprintf( NULL, 0, "Read of data from the sensor failed: %ls", hid_error( hid_dev ) ); // TODO: check that size >= 0 size++; char *error = malloc( size ); size = snprintf( error, size, "Read of data from the sensor failed: %ls", hid_error( hid_dev ) ); tempered_set_error( device, error ); result->length = 0; return false; } result->length = size; if ( size == 0 ) { tempered_set_error( device, strdup( "No data was read from the sensor (timeout)." ) ); return false; } return true; } int tempered_type_hid_get_sensor_count( tempered_device* device ) { struct temper_subtype_hid *subtype = (struct temper_subtype_hid *) device->subtype; int group_id, count = 0; for ( group_id = 0 ; group_id < subtype->sensor_group_count ; group_id++ ) { count += subtype->sensor_groups[group_id].sensor_count; } return count; } /** Get the group and sensor numbers for the given sensor ID. */ static bool tempered__type_hid__get_sensor_location( tempered_device* device, int sensor, int* group_num, int* sensor_num ) { struct temper_subtype_hid *subtype = (struct temper_subtype_hid *) device->subtype; int group_id, sensor_id = 0; for ( group_id = 0 ; group_id < subtype->sensor_group_count ; group_id++ ) { struct tempered_type_hid_sensor_group * group = &subtype->sensor_groups[group_id]; if ( sensor_id + group->sensor_count <= sensor ) { sensor_id += group->sensor_count; continue; } *group_num = group_id; *sensor_num = sensor - sensor_id; return true; } // The sensor ID is out of range despite the check in core.c tempered_set_error( device, strdup( "Sensor ID is out of range. This should never happen." ) ); return false; } int tempered_type_hid_get_sensor_type( tempered_device* device, int sensor ) { int group_id, sensor_id; if ( !tempered__type_hid__get_sensor_location( device, sensor, &group_id, &sensor_id ) ) { return TEMPERED_SENSOR_TYPE_NONE; } struct temper_subtype_hid *subtype = (struct temper_subtype_hid *) device->subtype; struct tempered_type_hid_sensor *hid_sensor = &subtype->sensor_groups[group_id].sensors[sensor_id]; int type = 0; if ( subtype->base.get_temperature != NULL && hid_sensor->get_temperature != NULL ) { type = type | TEMPERED_SENSOR_TYPE_TEMPERATURE; } if ( subtype->base.get_humidity != NULL && hid_sensor->get_humidity != NULL ) { type = type | TEMPERED_SENSOR_TYPE_HUMIDITY; } return type; }
///////////////////////////////////////////////////////////////////////////////////// // single-sample with ms wait ///////////////////////////////////////////////////////////////////////////////////// int waitForSample(Device *dev, UInt16 msec, UInt8 *buf, UInt16 maxLen ) { return hid_read_timeout(dev->hidapi_dev, buf, maxLen, msec ); }
int UT612ByteSource::run() { setupSIGINTHandler(); // Catch Ctrl-C // // Open the USB HID device and set up the UART // if (hid_init()) { return -1; } // Open the device using the VID, PID, // and optionally the Serial number. ////handle = hid_open(0x4d8, 0x3f, L"12345"); hid_device* handle = hid_open(0x10c4, 0xea80, NULL); if (!handle) { std::cerr << "ERROR: unable to connect to the UT612 LCR Meter\n" << "The most common problem is that the cp210x kernel module is interfering\n\n" << "If you installed this program using\n" << "sudo make install\n" << "then you should have a file /etc/modprobe.d/blacklist-cp210x.conf blacklisting the VID:PID of your LCR meter.\n"; if (_verbose) { printAllHidDevices(); std::cout << "\nTo be able to troubleshoot any problems, you need to provide the output of a script in the src folder:\n" << "cd src && ./collect_debug_info.sh\n" << "\n"; } return 1; } if (_verbose) { printHidDeviceInfo(handle); } setupUartForUT612(handle); enableUartForUT612(handle); // // Main loop // while (!killed) { uint8_t data[64]; // TODO: When timeout set to 0, non blocking read is used. When set to -1 blocking read is used. int read = hid_read_timeout(handle, data, sizeof(data), /*milliseconds*/ 1000 ); if (read == -1) { std::cerr << "\nERROR: hid_read_timeout returned -1" << std::endl; } else if (read == 0 && _verbose) { std::cerr << "\nERROR: timed out calling hid_read_timeout" << std::endl; } if (read > 1) { int numUartRxBytes = data[0]; if (numUartRxBytes != read-1) { std::cerr << "\nERROR: numUartRxBytes = " << numUartRxBytes << ", read=" << read << std::endl; } for (int i = 0; i < numUartRxBytes; i++) { notifyAllByteSinks(data[i+1]); } } } enableUartForUT612(handle, false); std::cout << "\nKilled by user" << std::endl; // // Clean up // hid_close(handle); hid_exit(); return 0; }
int touchmouse_process_events_timeout(touchmouse_device *dev, int milliseconds) { unsigned char data[256] = {}; int res; uint64_t deadline; if(milliseconds < 0) { deadline = (uint64_t)(-1); } else { deadline = mono_timer_nanos() + (milliseconds * 1000000); } uint64_t nanos = mono_timer_nanos(); if (nanos == 0 || deadline == 0) { TM_FATAL("touchmouse_process_events_timeout: timer function returned an error, erroring out since we have no timer\n"); return -1; } do { res = hid_read_timeout(dev->dev, data, 255, (deadline - nanos) / 1000000 ); if (res < 0 ) { TM_ERROR("hid_read() failed: %d - %ls\n", res, hid_error(dev->dev)); return -2; } else if (res > 0) { // Dump contents of transfer TM_SPEW("touchmouse_process_events_timeout: got report: %d bytes:", res); int j; for(j = 0; j < res; j++) { TM_SPEW(" %02X", data[j]); } TM_SPEW("\n"); // Interpret contents. report* r = (report*)data; // We only care about report ID 39 (0x27), which should be 32 bytes long if (res == 32 && r->report_id == 0x27) { TM_FLOOD("Timestamp: %02X\t%02X bytes:", r->timestamp, r->length - 1); int t; for(t = 0; t < r->length - 1; t++) { TM_FLOOD(" %02X", r->data[t]); } TM_FLOOD("\n"); // Reset the decoder if we've seen one timestamp already from earlier // transfers, and this one doesn't match. if (dev->buf_index != 0 && r->timestamp != dev->timestamp_in_progress) { TM_FLOOD("touchmouse_process_events_timeout: timestamps don't match: got %d, expected %d\n", r->timestamp, dev->timestamp_in_progress); reset_decoder(dev); // Reset decoder for next transfer } dev->timestamp_in_progress = r->timestamp; for(t = 0; t < r->length - 1; t++) { // We subtract one byte because the length includes the timestamp byte. int res; // Yes, we process the low nybble first. Embedded systems are funny like that. res = process_nybble(dev, r->data[t] & 0xf); if (res == DECODER_COMPLETE) { TM_SPEW("Frame completed, triggering callback\n"); dev->timestamp_last_completed = r->timestamp; touchmouse_callback_info cbinfo; cbinfo.userdata = dev->userdata; cbinfo.image = dev->image; cbinfo.timestamp = dev->timestamp_last_completed; dev->cb(&cbinfo); reset_decoder(dev); // Reset decoder for next transfer return 0; } if (res == DECODER_ERROR) { TM_ERROR("Caught error in decoder, aborting decode!\n"); reset_decoder(dev); return -1; } res = process_nybble(dev, (r->data[t] & 0xf0) >> 4); if (res == DECODER_COMPLETE) { TM_SPEW("Frame completed, triggering callback\n"); dev->timestamp_last_completed = r->timestamp; touchmouse_callback_info cbinfo; cbinfo.userdata = dev->userdata; cbinfo.image = dev->image; cbinfo.timestamp = dev->timestamp_last_completed; dev->cb(&cbinfo); reset_decoder(dev); // Reset decoder for next transfer return 0; } if (res == DECODER_ERROR) { TM_ERROR("Caught error in decoder, aborting decode!\n"); reset_decoder(dev); return -1; } } } }