/* * Copyright (C) 2014 Timo Kokkonen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ #include #include #include #include #include "contour-next-protocol.h" #include "utils.h" #define OUT_EP (4 | LIBUSB_ENDPOINT_OUT) #define IN_EP (3 | LIBUSB_ENDPOINT_IN) static int send_msg(libusb_device_handle *dev_handle, unsigned char *msg) { int ret, actual; trace(2, "Sending message "); print_hex(2, msg, MSG_LEN); ret = libusb_interrupt_transfer(dev_handle, OUT_EP, msg, MSG_LEN, &actual, 0); if (ret || actual != MSG_LEN) { trace(0, "Error sending msg: %d, actual %d\n", ret, actual); return -1; } return 0; } static int read_msg(libusb_device_handle *dev_handle, unsigned char *msg) { int ret, actual; ret = libusb_interrupt_transfer(dev_handle, IN_EP , msg, MSG_LEN, &actual, 0); if (ret || actual != MSG_LEN) { trace(0, "Error reading message: %d, actual %d\n", ret, actual); return -1; } return 0; } static int send_init_msg(libusb_device_handle *dev_handle, unsigned char b0, unsigned char b1, unsigned char b2, int read_msgs) { unsigned char msg[MSG_LEN]; int ret, i; bzero(msg, sizeof(msg)); msg[3] = b0; msg[4] = b1; msg[5] = b2; ret = send_msg(dev_handle, msg); if (ret) return ret; for (i = 0; i < read_msgs; i++) { ret = read_msg(dev_handle, msg); if (ret) return ret; trace(1, "Reading intermediate msg %d of %d\n", i, read_msgs); print_hex(3, msg, sizeof(msg)); print_ascii(2, msg, sizeof(msg)); } return 0; } struct contour_next_init_sequence { unsigned char b0, b1, b2; int response_msgs; }; static int send_init_messages(libusb_device_handle *dev_handle, struct contour_next_init_sequence *seq, int messages) { int i, ret; for (i = 0; i < messages; i++) { if (!trace_level) trace(0, "\rInitializing %d/%d...", i, messages); ret = send_init_msg(dev_handle, seq[i].b0, seq[i].b1, seq[i].b2, seq[i].response_msgs); if (ret) return ret; } return 0; } static struct contour_next_init_sequence seq[] = { { 0x01, 0x04, 0x00, 6, }, { 0x01, 0x06, 0x00, 5, }, { 0x01, 0x15, 0x00, 5, }, { 0x01, 0x15, 0x00, 5, }, { 0x01, 0x15, 0x00, 5, }, { 0x01, 0x15, 0x00, 5, }, { 0x01, 0x15, 0x00, 5, }, { 0x01, 0x15, 0x00, 2, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x41, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x43, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x44, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x47, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x49, 0x7C, 1, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x4D, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x50, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x53, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x54, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x56, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x57, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x58, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x02, 0x52, 0x7C, 1, }, { 0x02, 0x5A, 0x7C, 2, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x15, 0x00, 1, }, { 0x01, 0x05, 0x00, 1, }, { 0x01, 0x04, 0x00, 1, }, { 0x01, 0x04, 0x00, 6, }, { 0x01, 0x06, 0x00, 5, }, { 0x01, 0x06, 0x00, 1, }, }; static libusb_device_handle *dev_handle; int contour_next_initialize(void) { libusb_device **devs; int ret; ret = libusb_init(NULL); if (ret) { printf("Failed to init libusb: %d\n", ret); return 1; } ret = libusb_get_device_list(NULL, &devs); if (ret < 0) { printf("Failed to get device list: %d\n", ret); return 1; } dev_handle = libusb_open_device_with_vid_pid(NULL, 0x1a79, 0x7410); if (dev_handle == NULL) { printf("Failed to open device\n"); return 1; } ret = libusb_kernel_driver_active(dev_handle, 0); if (ret == 1) { printf("Kernel Driver Active\n"); ret = libusb_detach_kernel_driver(dev_handle, 0); if (ret == 0) printf("Kernel Driver Detached!\n"); } ret = libusb_claim_interface(dev_handle, 0); if (ret) { printf("Failed to claim the interface: %d\n", ret); return 1; } send_init_messages(dev_handle, seq, sizeof(seq)/sizeof(seq[0])); return 0; while (1) { unsigned char msg[MSG_LEN]; bzero(msg, sizeof(msg)); msg[3] = 0x01; msg[4] = 0x06; msg[5] = 0x00; ret = send_msg(dev_handle, msg); if (ret) return ret; ret = read_msg(dev_handle, msg); if (ret) return ret; print_ascii(0, msg, sizeof(msg)); } return 0; } int contour_next_read_entry(unsigned char *msg) { int ret; bzero(msg, MSG_LEN); msg[3] = 0x01; msg[4] = 0x06; msg[5] = 0x00; ret = send_msg(dev_handle, msg); if (ret) return ret; ret = read_msg(dev_handle, msg); if (ret) return ret; return datalen(msg); }