[tuxdroid-svn] r414 - in daemon/trunk: . libs
Status: Beta
Brought to you by:
ks156
From: neimad <c2m...@c2...> - 2007-06-23 16:31:05
|
Author: neimad Date: 2007-06-23 18:30:31 +0200 (Sat, 23 Jun 2007) New Revision: 414 Added: daemon/trunk/libs/USBDaemon_usb.c daemon/trunk/libs/USBDaemon_usb.h Modified: daemon/trunk/Makefile daemon/trunk/libs/USBDaemon_command_tux.c daemon/trunk/libs/USBDaemon_command_tux.h daemon/trunk/libs/USBDaemon_status_table.c daemon/trunk/libs/USBDaemon_tcp_server.c daemon/trunk/main.c Log: * Merged USBDaemon_usb_{enum,readWrite}.[ch] into USBDaemon_usb.[ch]. The naming scheme (both for the files and the functions) remains unchanged for the time being; this will happen in a future commit. Modified: daemon/trunk/Makefile =================================================================== --- daemon/trunk/Makefile 2007-06-23 15:27:46 UTC (rev 413) +++ daemon/trunk/Makefile 2007-06-23 16:30:31 UTC (rev 414) @@ -38,8 +38,7 @@ SRC_OBJS = \ $(OBJ_DIR)/main.o \ - $(OBJ_DIR)/USBDaemon_usb_enum.o \ - $(OBJ_DIR)/USBDaemon_usb_readWrite.o \ + $(OBJ_DIR)/USBDaemon_usb.o \ $(OBJ_DIR)/USBDaemon_status_table.o \ $(OBJ_DIR)/USBDaemon_pidfile.o \ $(OBJ_DIR)/USBDaemon_command_tux.o \ @@ -85,7 +84,7 @@ $(OBJ_DIR)/main.o: main.c \ libs/USBDaemon_globals.h \ -libs/USBDaemon_usb_enum.h \ +libs/USBDaemon_usb.h \ libs/USBDaemon_tcp_server.h \ libs/USBDaemon_status_table.h \ libs/USBDaemon_pidfile.h \ @@ -96,18 +95,12 @@ $(OBJ_DIR)/USBDaemon_log.o: libs/USBDaemon_log.c libs/USBDaemon_log.h $(compile_source) -$(OBJ_DIR)/USBDaemon_usb_enum.o: libs/USBDaemon_usb_enum.c \ -libs/USBDaemon_usb_enum.h \ +$(OBJ_DIR)/USBDaemon_usb.o: libs/USBDaemon_usb.c \ +libs/USBDaemon_usb.h \ libs/USBDaemon_globals.h \ -libs/USBDaemon_usb_readWrite.h \ libs/USBDaemon_pidfile.h \ -libs/USBDaemon_command_tux.h - $(compile_source) - -$(OBJ_DIR)/USBDaemon_usb_readWrite.o: libs/USBDaemon_usb_readWrite.c \ -libs/USBDaemon_usb_enum.h \ -libs/USBDaemon_globals.h \ libs/USBDaemon_status_table.h \ +libs/USBDaemon_command_tux.h \ tuxdefs/commands.h $(compile_source) @@ -125,9 +118,9 @@ $(OBJ_DIR)/USBDaemon_command_tux.o: libs/USBDaemon_command_tux.c \ libs/USBDaemon_status_table.h \ -tuxdefs/commands.h \ +tuxdefs/commands.h \ libs/USBDaemon_command_tux.h \ -libs/USBDaemon_usb_readWrite.h \ +libs/USBDaemon_usb.h \ libs/USBDaemon_globals.h \ libs/USBDaemon_pidfile.h \ libs/USBDaemon_tcp_server.h Modified: daemon/trunk/libs/USBDaemon_command_tux.c =================================================================== --- daemon/trunk/libs/USBDaemon_command_tux.c 2007-06-23 15:27:46 UTC (rev 413) +++ daemon/trunk/libs/USBDaemon_command_tux.c 2007-06-23 16:30:31 UTC (rev 414) @@ -24,9 +24,8 @@ #include <stdio.h> #include <usb.h> #include "USBDaemon_status_table.h" -#include "USBDaemon_usb_enum.h" +#include "USBDaemon_usb.h" #include "USBDaemon_command_tux.h" -#include "USBDaemon_usb_readWrite.h" #include "USBDaemon_globals.h" #include "USBDaemon_log.h" #include "../tuxdefs/commands.h" Modified: daemon/trunk/libs/USBDaemon_command_tux.h =================================================================== --- daemon/trunk/libs/USBDaemon_command_tux.h 2007-06-23 15:27:46 UTC (rev 413) +++ daemon/trunk/libs/USBDaemon_command_tux.h 2007-06-23 16:30:31 UTC (rev 414) @@ -26,7 +26,7 @@ /*_____________________ I N C L U D E S____________________________________ */ #include <stdint.h> -#include "USBDaemon_usb_enum.h" +#include "USBDaemon_usb.h" /*_____________________ D E F I N E S ______________________________________*/ Modified: daemon/trunk/libs/USBDaemon_status_table.c =================================================================== --- daemon/trunk/libs/USBDaemon_status_table.c 2007-06-23 15:27:46 UTC (rev 413) +++ daemon/trunk/libs/USBDaemon_status_table.c 2007-06-23 16:30:31 UTC (rev 414) @@ -24,7 +24,7 @@ /*_____________________ I N C L U D E S____________________________________ */ #include <stdio.h> #include "USBDaemon_status_table.h" -#include "USBDaemon_usb_enum.h" +#include "USBDaemon_usb.h" #include "USBDaemon_globals.h" #include "USBDaemon_log.h" #include "USBDaemon_tcp_server.h" Modified: daemon/trunk/libs/USBDaemon_tcp_server.c =================================================================== --- daemon/trunk/libs/USBDaemon_tcp_server.c 2007-06-23 15:27:46 UTC (rev 413) +++ daemon/trunk/libs/USBDaemon_tcp_server.c 2007-06-23 16:30:31 UTC (rev 414) @@ -34,8 +34,7 @@ #include "USBDaemon_command_tux.h" #include "USBDaemon_status_table.h" #include "USBDaemon_tcp_server.h" -#include "USBDaemon_usb_enum.h" -#include "USBDaemon_usb_readWrite.h" +#include "USBDaemon_usb.h" #include "USBDaemon_pidfile.h" #include "USBDaemon_log.h" Added: daemon/trunk/libs/USBDaemon_usb.c =================================================================== --- daemon/trunk/libs/USBDaemon_usb.c (rev 0) +++ daemon/trunk/libs/USBDaemon_usb.c 2007-06-23 16:30:31 UTC (rev 414) @@ -0,0 +1,264 @@ +/* + * Tux Droid - USB Daemon + * Copyright (C) 2007 C2ME Sa <rem...@c2...> + * + * 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, 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. + */ + +/* $Id$ */ + +#include <stdio.h> +#include <errno.h> + +#include "USBDaemon_usb.h" +#include "USBDaemon_globals.h" +#include "USBDaemon_log.h" +#include "USBDaemon_status_table.h" +#include "USBDaemon_tcp_server.h" + +usb_dev_handle *tux_handle = NULL; +struct usb_device *tux_device = NULL; +usb_connection_status_t usb_connection_status = USB_DISCONNECTED; + +/************************************************************************ */ + +/* usb_find_TuxDroid() */ + +/************************************************************************ */ +struct usb_device *usb_find_TuxDroid(void) +{ + struct usb_bus *bus; + struct usb_device *tux_dev; + + usb_init(); + usb_find_busses(); + usb_find_devices(); + + for (bus = usb_busses; bus; bus = bus->next) + for (tux_dev = bus->devices; tux_dev; tux_dev = tux_dev->next) + if (tux_dev->descriptor.idVendor == TUX_VID + && tux_dev->descriptor.idProduct == TUX_PID) + return tux_dev; + + return NULL; +} + +/************************************************************************ */ + +/* usb_open_TuxDroid() */ + +/************************************************************************ */ +static struct usb_dev_handle *usb_open_TuxDroid(struct usb_device *tux_dev) +{ + usb_dev_handle *tux_hdl; + int error; + + /* Open usb device */ + tux_hdl = usb_open(tux_dev); + + if (!tux_hdl) + { + log_error("Can't open device: %s (%d)", strerror(errno), errno); + return NULL; + } + + /* Claim device interface */ + error = usb_claim_interface(tux_hdl, TUX_INTERFACE); + if (error != 0) + { + /*usb_detach_kernel_driver_np(tux_hdl, TUX_INTERFACE); */ + error = usb_claim_interface(tux_hdl, TUX_INTERFACE); + if (error != 0) + { + log_error("Can't claim interface: %s (%d)", strerror(errno), errno); + return NULL; + } + } + return tux_hdl; +} + + +/** + * Close a Tux droid previously opened with usb_open_TuxDroid(). + */ +static void usb_close_TuxDroid(usb_dev_handle *tux_hdl) +{ + if (usb_release_interface(tux_hdl, TUX_INTERFACE) < 0) + log_warning("failed releasing USB interface: %s (%d)", + strerror(errno), errno); + + if (usb_close(tux_hdl) < 0) + log_warning("failed closing USB device: %s (%d)", + strerror(errno), errno); +} + +/** + * Capture a Tux droid. + * + * \return 1 if successful, 0 otherwise + */ +int usb_capture_TuxDroid(void) +{ + log_debug("Capturing USB device"); + + /* Find Tux */ + do + { + tux_device = usb_find_TuxDroid(); + if (tux_device == NULL) + log_error("Tux dongle not found"); + + sleep(1); + } + while (!tux_device); + + /* Old firmware should be discarded here */ + if (tux_device->descriptor.bcdDevice < 0x100) + { + log_error("Your dongle firmware is too old.\n" + "This version is not compatible with this daemon, please \ + update\n" + "the firmware to version 1.00 or better.\n" + "Check http://www.tuxisalive.com/documentation/how-to/\ + updating-the-firmware\nfor details."); + return 0; + } + + log_debug("Fux found on the USB bus"); + + /* Get Device handle */ + tux_handle = usb_open_TuxDroid(tux_device); + if (tux_handle == NULL) + { + log_error("You must load the daemon in root mode"); + return 0; + } + log_debug("Fux USB device opened"); + + usb_connection_status = USB_CONNECTED; + + return 1; +} + +/** + * Release a Tux droid previously captured. + */ +void usb_release_TuxDroid(void) +{ + log_debug("Releasing USB device"); + + if (tux_handle != NULL) + usb_close_TuxDroid(tux_handle); + + tux_handle = NULL; + tux_device = NULL; +} + +/** + * Get Tux droid's status. + * + * FIXME: the status of *what* exactly ? + * FIXME: "getting" something and not actually returning anything + * is weird; "updating" would be more correct. + */ +void usb_get_status_TuxDroid(void) +{ + const unsigned char cmd_send[5] = { 0x01, 0x01, 0x00, 0x00, 0x00 }; + unsigned char data[64] = { [0 ... 63] = 0 }; + unsigned char *d; + int idx; + int num_frames, i; + + if (usb_connection_status != USB_CONNECTED) + return; + + idx = usb_interrupt_write(tux_handle, TUX_WRITE_EP, (char *)cmd_send, + sizeof(cmd_send), TUX_WRITE_TIMEOUT); + if (idx <= 0) + return; + + idx = usb_interrupt_read(tux_handle, TUX_READ_EP, (char *)data, + sizeof(data), TUX_READ_TIMEOUT); + if (idx <= 0) + return; + + update_system_status_table(data); + + num_frames = data[3]; + + if (show_frames) + log_debug("RF_st: %.2x CMD_st: %.2x NB_frames : %d idx : %d", + RF_status, CMD_status, num_frames, idx); + + /* XXX Should assert in this case, I guess ? */ + if (num_frames >= 16) + return; + + d = data + 4; + for (i = 0; i < num_frames; i++) + { + if (show_raw_status) + log_debug("%.2x %.2x %.2x %.2x", d[0], d[1], d[2], d[3]); + + update_raw_status_table(d); + d += 4; + } +} + +/** + * Send data to Tux droid over usb. + * FIXME: the whole cmd_status_flag thing is clunky. + * + * @param[in] data Data to send + * + * @return Acknowledgement (ACK_xxx), may be success or error + */ +unsigned char usb_write_TuxDroid(unsigned char data[TUX_SEND_LENGTH]) +{ + int idx; + unsigned char counter; + unsigned char csf; + + if (usb_connection_status != USB_CONNECTED) + return ACK_CMD_DONGLE_NOT_PRESENT; + + /* Send data */ + cmd_status_flag = 1; + idx = usb_interrupt_write(tux_handle, TUX_WRITE_EP, (char *)data, + TUX_SEND_LENGTH, TUX_WRITE_TIMEOUT); + counter = 0; + csf = cmd_status_flag; + + while (csf) + { + usb_get_status_TuxDroid(); + csf = cmd_status_flag; + + counter++; + usleep(10000); + if (counter == 15) + { + return ACK_CMD_TIMEOUT; + } + } + + if (CMD_status == ACK_CMD_OK) + return ACK_CMD_OK; + else if (CMD_status == ACK_CMD_KO) + return ACK_CMD_KO; + else + return ACK_CMD_ERROR; +} Added: daemon/trunk/libs/USBDaemon_usb.h =================================================================== --- daemon/trunk/libs/USBDaemon_usb.h (rev 0) +++ daemon/trunk/libs/USBDaemon_usb.h 2007-06-23 16:30:31 UTC (rev 414) @@ -0,0 +1,77 @@ +/* + * Tux Droid - USB Daemon + * Copyright (C) 2007 C2ME Sa <rem...@c2...> + * + * 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, 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. + */ + +/* $Id$ */ + +#ifndef __USBDAEMON_USB_H__ +#define __USBDAEMON_USB_H__ + +#include <usb.h> + +#define USB_FRAME_SIZE 5 + +typedef enum +{ + USB_DISCONNECTED = 0, + USB_CONNECTED = 1 +} usb_connection_status_t; + +typedef enum +{ + USB_TUX_CMD = 0, + USB_DONGLE_CMD = 1, + USB_BOOTLOADER_CMD = 2, +} usb_command_header_t; + +typedef enum +{ + USB_TUX_CONNECTION_CMD = 0, + USB_STATUS_CMD = 1, + USB_AUDIO_CMD = 2, + USB_VERSION_CMD = 6, +} usb_dongle_commands_t; + +/* tux connection commands available on the usb dongle + * + * These are of the following type: + * USB_DONGLE_CMD, USB_TUX_CONNECTION_CMD + */ +typedef enum +{ + USB_TUX_CONNECTION_DISCONNECT = 1, + USB_TUX_CONNECTION_CONNECT = 2, + USB_TUX_CONNECTION_ID_REQUEST = 3, + USB_TUX_CONNECTION_ID_LOOKUP = 4, + USB_TUX_CONNECTION_CHANGE_ID = 5, + USB_TUX_CONNECTION_WAKEUP = 6, + USB_TUX_CONNECTION_WIRELESS_CHANNEL = 7, +} usb_tux_connection_t; + +extern usb_dev_handle *tux_handle; +extern usb_connection_status_t usb_connection_status; + +extern struct usb_device *usb_find_TuxDroid(void); +extern int usb_capture_TuxDroid(void); +extern void usb_release_TuxDroid(void); + +extern void usb_get_status_TuxDroid(void); +extern unsigned char usb_write_TuxDroid(unsigned char datas[]); + +#endif /* __USBDAEMON_USB_H__ */ Modified: daemon/trunk/main.c =================================================================== --- daemon/trunk/main.c 2007-06-23 15:27:46 UTC (rev 413) +++ daemon/trunk/main.c 2007-06-23 16:30:31 UTC (rev 414) @@ -37,7 +37,7 @@ #include "libs/USBDaemon_globals.h" #include "libs/USBDaemon_log.h" #include "libs/USBDaemon_pidfile.h" -#include "libs/USBDaemon_usb_enum.h" +#include "libs/USBDaemon_usb.h" #include "libs/USBDaemon_tcp_server.h" #include "libs/USBDaemon_status_table.h" #include "libs/USBDaemon_command_tux.h" |