QtHPConnect/hpusb.h

229 lines
5.6 KiB
C
Raw Normal View History

2019-02-10 14:32:15 +01:00
#ifndef HPUSB_H
#define HPUSB_H
2020-01-07 19:41:18 +01:00
#include <libusb-1.0/libusb.h>
2019-03-09 15:39:38 +01:00
#include <QObject>
2019-02-17 17:24:52 +01:00
#include <QByteArray>
2019-09-21 18:00:43 +02:00
#include <QMutex>
2019-02-10 14:43:00 +01:00
2019-02-12 21:48:35 +01:00
struct hp_Settings;
2019-02-20 23:08:14 +01:00
class hpCalcData;
2019-02-10 14:43:00 +01:00
//! USB Vendor ID of Hewlett-Packard.
#define USB_VID_HP (0x03F0)
//! USB Product ID of the Prime calculator in firmware revisions < 8151.
#define USB_PID_PRIME1 (0x0441)
//! USB Product ID of the Prime calculator in firmware revisions >= 8151.
#define USB_PID_PRIME2 (0x1541)
//! USB Product ID of the Prime calculator in firmware revisions >= 8151.
#define USB_PID_PRIME3 (0x2441)
//! Size of a raw HID packet for the Prime.
#define PRIME_RAW_HID_DATA_SIZE (64)
2019-02-17 17:24:52 +01:00
#define PRIME_RAW_DATA_SIZE (1024)
2019-02-10 14:43:00 +01:00
2019-02-17 17:24:52 +01:00
#define LEN_IN_BUFFER 1024*8
2019-02-10 14:43:00 +01:00
#define USB_ENDPOINT_IN (LIBUSB_ENDPOINT_IN | 1) /* endpoint address */
#define USB_ENDPOINT_OUT (LIBUSB_ENDPOINT_OUT | 2) /* endpoint address */
#define CMD_PRIME_CHECK_READY (0xFF)
2019-02-12 21:48:35 +01:00
#define CMD_PRIME_GET_SETTINGS (0xF9)
2019-02-10 14:43:00 +01:00
#define CMD_PRIME_GET_INFOS (0xFA)
#define CMD_PRIME_RECV_SCREEN (0xFC)
#define CMD_PRIME_RECV_BACKUP (0xF9)
#define CMD_PRIME_REQ_FILE (0xF8)
#define CMD_PRIME_RECV_FILE (0xF7)
#define CMD_PRIME_SEND_CHAT (0xF2)
#define CMD_PRIME_RECV_CHAT (0xF2)
#define CMD_PRIME_SEND_KEY (0xEC)
#define CMD_PRIME_SET_DATE_TIME (0xE7)
2019-02-17 17:24:52 +01:00
#define ENDPOINT_OUT (0x02)
#define ENDPOINT_IN (0x81)
#define HEADER_LEN (0xFD)
2019-02-20 23:08:14 +01:00
#define TIME_OUT (5000)
2019-02-17 17:24:52 +01:00
2019-02-10 14:43:00 +01:00
struct hp_Handle {
libusb_device_handle *usbhandle = nullptr;
libusb_device *usbdevice = nullptr;
2019-03-09 15:39:38 +01:00
2019-02-20 23:08:14 +01:00
hpCalcData * calc;
2019-02-10 14:43:00 +01:00
int dev_open=0;
};
2019-02-17 17:24:52 +01:00
struct usb_firstchunk {
uint8_t start1;
uint8_t start2;
uint8_t type;
2019-02-19 22:10:58 +01:00
int items;
2019-02-17 17:24:52 +01:00
int size;
};
struct usb_chunk {
uint8_t start1;
uint8_t chunk; //the chunk number
};
enum usb_header_type {
2019-02-20 23:08:14 +01:00
HP_HDR_FILE,
2019-02-17 17:24:52 +01:00
HP_HDR_CHUNK,
2019-02-20 23:08:14 +01:00
HP_HDR_INFO,
2019-02-21 19:51:56 +01:00
HP_HDR_PNG,
HP_HDR_UNKNOWN
2019-02-17 17:24:52 +01:00
};
2019-02-21 19:51:56 +01:00
enum hp_pkt_type {
HP_TP_SETTINGS=0,
HP_TP_FUNCTIONS=02,
HP_TP_LIST=03,
HP_TP_MATRIX=04,
2019-02-26 20:56:39 +01:00
HP_TP_NOTE=05,
2019-02-21 19:51:56 +01:00
HP_TP_PROG=06,
HP_TP_CUSTOM=11
};
2019-02-17 17:24:52 +01:00
struct usb_header {
usb_header_type type;
uint8_t cmd;
uint8_t typecode;
2019-02-19 22:10:58 +01:00
int items;
2019-02-20 23:08:14 +01:00
int pkt_size; //bytes in packet
int num_chunks;
2019-02-17 17:24:52 +01:00
int name_length;
2019-02-21 19:51:56 +01:00
uint16_t embedded_crc;
int pkt_type;
2019-02-20 23:08:14 +01:00
int chunk;
2019-02-21 19:51:56 +01:00
uint8_t header[HEADER_LEN];
2019-02-20 23:08:14 +01:00
int headerlen;
2019-02-17 17:24:52 +01:00
};
2019-02-10 14:43:00 +01:00
struct hp_cmd {
};
struct hp_pkt_in {
2019-02-18 21:15:04 +01:00
QByteArray array;
2019-02-17 17:24:52 +01:00
uint32_t size;
uint8_t cmd;
2019-02-20 23:08:14 +01:00
hpCalcData * calc;
usb_header_type type;
2019-02-21 19:51:56 +01:00
int pkt_type;
2019-02-10 14:43:00 +01:00
};
struct hp_pkt_out {
2019-02-17 17:24:52 +01:00
uint8_t * data;
2019-02-18 21:15:04 +01:00
QByteArray array= QByteArray(1024,0);
2019-02-17 17:24:52 +01:00
uint32_t size;
uint8_t cmd;
2019-02-10 14:43:00 +01:00
};
2019-02-17 17:24:52 +01:00
typedef enum {
// 5 is triggered periodically by the official connectivity kit. It returns something with a PNG header, but much smaller.
CALC_SCREENSHOT_FORMAT_FIRST = 8,
CALC_SCREENSHOT_FORMAT_PRIME_PNG_320x240x16 = 8,
CALC_SCREENSHOT_FORMAT_PRIME_PNG_320x240x4 = 9,
CALC_SCREENSHOT_FORMAT_PRIME_PNG_160x120x16 = 10,
CALC_SCREENSHOT_FORMAT_PRIME_PNG_160x120x4 = 11,
CALC_SCREENSHOT_FORMAT_LAST ///< Keep this one last
} hp_screenshot_format;
//! Structure defining a raw packet for the Prime, used at the lowest layer of the protocol implementation.
typedef struct
{
uint32_t size;
uint8_t data[PRIME_RAW_DATA_SIZE + 1];
} prime_raw_hid_pkt;
2019-03-09 15:39:38 +01:00
enum hp_hotplug {
HP_OPEN_DEVICE=1,
HP_CLOSE_DEVICE=2
};
2019-02-10 14:43:00 +01:00
struct hp_Information;
2019-03-09 15:39:38 +01:00
void cb_out(struct libusb_transfer *transfer);
void cb_in(struct libusb_transfer *transfer);
void sighandler(int signum);
2019-02-10 14:32:15 +01:00
2019-03-09 15:39:38 +01:00
class hpusb: public QObject
2019-02-10 14:32:15 +01:00
{
2019-03-09 15:39:38 +01:00
Q_OBJECT
private:
2019-02-10 14:43:00 +01:00
struct timespec t1, t2;
enum {
out_deinit,
out_release,
out
} exitflag;
2019-09-21 18:00:43 +02:00
libusb_context * ctx = nullptr;
2019-02-10 14:43:00 +01:00
2019-03-09 15:39:38 +01:00
hp_Handle hp_handle;
libusb_hotplug_callback_handle hp_callback_handle;
2019-02-10 14:43:00 +01:00
int lb_init=0;
// OUT-going transfers (OUT from host PC to USB-device)
2019-09-21 18:00:43 +02:00
struct libusb_transfer *transfer_out = nullptr;
2019-02-10 14:43:00 +01:00
// IN-coming transfers (IN to host PC from USB-device)
2019-09-21 18:00:43 +02:00
struct libusb_transfer *transfer_in = nullptr;
2019-02-10 14:43:00 +01:00
int do_exit = 0;
2019-09-21 18:00:43 +02:00
QMutex mutex;
2019-03-09 15:39:38 +01:00
public:
2019-02-10 14:43:00 +01:00
hpusb();
int hp_init();
int is_device(libusb_device *);
2019-02-12 22:35:42 +01:00
void dumpDevice(libusb_device * );
2019-02-10 14:43:00 +01:00
int hp_open(hp_Handle *);
2019-03-09 15:39:38 +01:00
int hp_close();
2019-02-10 14:43:00 +01:00
int submit_async_transfer(hp_Handle *, hp_pkt_in *, hp_pkt_out *);
2019-02-17 17:24:52 +01:00
int submit_sync_s_transfer(hp_Handle *, hp_pkt_out *);
int submit_sync_r_transfer(hp_Handle *, hp_pkt_in *);
int sync_s_recv_file(hp_Handle *);
int sync_r_recv_file(hp_Handle *);
2019-02-17 20:33:51 +01:00
int extract_header(uint8_t *, usb_header *);
2019-02-10 14:43:00 +01:00
int submit_callback();
int hp_close(hp_Handle * );
int hp_func();
2019-02-17 17:24:52 +01:00
int is_ready(hp_Handle *);
2019-02-20 23:08:14 +01:00
int load_info(hp_Handle *);
2019-02-17 17:24:52 +01:00
int lookfordouble (QByteArray , int );
2019-02-10 14:43:00 +01:00
int get_info( /*calc_infos * infos*/);
2019-02-20 23:08:14 +01:00
int get_settings(hp_Handle *);
2019-02-12 21:48:35 +01:00
int set_settings(hp_Handle * , hp_Settings set);
2019-02-20 23:08:14 +01:00
int get_screen_shot(hp_Handle *);
int send_screen_shot(hp_pkt_in *);
int send_info(hp_pkt_in *);
2019-02-21 19:51:56 +01:00
int send_file(hp_pkt_in *);
2019-02-20 23:08:14 +01:00
int data_dispatch(hp_pkt_in *);
2019-02-10 14:43:00 +01:00
int vpkt_send_experiments(hp_Handle * handle, int cmd);
// Function Prototypes:
void print_libusb_transfer(struct libusb_transfer *p_t);
friend void sighandler(int signum);
friend void cb_out(struct libusb_transfer *transfer);
friend void cb_in(struct libusb_transfer *transfer);
2019-03-09 15:39:38 +01:00
int hotplugcallback(struct libusb_context *ctx, struct libusb_device *dev,
libusb_hotplug_event event);
2019-02-10 14:43:00 +01:00
2019-03-09 15:39:38 +01:00
int eventHandler();
2019-02-10 14:43:00 +01:00
~hpusb();
2019-03-09 15:39:38 +01:00
signals:
void hotplug(int );
2019-02-10 14:32:15 +01:00
};
2019-02-10 14:43:00 +01:00
#endif // HPUSB_H