Files
pumpitupdev_pumptools/src/main/io/piubtn/device.c
T
icex2 eafb2909d1 Appl new code style on whole code base
Use a similar code style to bemanitools though reduce indentation
to 2 spaces. Also some other tweaks to be more similar to google
style.
2021-03-19 15:52:34 +01:00

96 lines
1.9 KiB
C

#define LOG_MODULE "piubtn-drv-device"
#include "io/util/usb_.h"
#include "util/log.h"
#include "defs.h"
#include "device.h"
static void *piubtn_drv_device_handle;
bool piubtn_drv_device_open(void)
{
if (piubtn_drv_device_handle) {
log_warn("Device already opened");
return true;
}
piubtn_drv_device_handle = io_usb_open(
PIUBTN_DRV_VID, PIUBTN_DRV_PID, PIUBTN_DRV_CONFIG, PIUBTN_DRV_IFACE);
return piubtn_drv_device_handle;
}
bool piubtn_drv_device_read(uint8_t *buffer, uint8_t len)
{
int32_t res;
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (len < PIUBTN_DRV_BUFFER_SIZE) {
log_error("Read failed, buffer (%d) too small", len);
return false;
}
res = io_usb_control_transfer(
piubtn_drv_device_handle,
PIUBTN_DRV_USB_CTRL_TYPE_IN,
PIUBTN_DRV_USB_CTRL_REQUEST,
0,
0,
buffer,
PIUBTN_DRV_BUFFER_SIZE,
PIUBTN_DRV_USB_REQ_TIMEOUT);
if (res != PIUBTN_DRV_BUFFER_SIZE) {
log_error("Read failed: %d", res);
return false;
}
return true;
}
bool piubtn_drv_device_write(uint8_t *buffer, uint8_t len)
{
int32_t res;
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
return false;
}
if (len < PIUBTN_DRV_BUFFER_SIZE) {
log_error("Write failed, buffer (%d) too small", len);
return false;
}
res = io_usb_control_transfer(
piubtn_drv_device_handle,
PIUBTN_DRV_USB_CTRL_TYPE_OUT,
PIUBTN_DRV_USB_CTRL_REQUEST,
0,
0,
buffer,
PIUBTN_DRV_BUFFER_SIZE,
PIUBTN_DRV_USB_REQ_TIMEOUT);
if (res != PIUBTN_DRV_BUFFER_SIZE) {
log_error("Write failed: %d", res);
return false;
}
return true;
}
void piubtn_drv_device_close(void)
{
if (!piubtn_drv_device_handle) {
log_error("Device not opened");
}
io_usb_close(piubtn_drv_device_handle);
}