[tuxdroid-svn] r1037 - software_suite_v2/middleware/tuxdriver/branches/hid_transition/src
Status: Beta
Brought to you by:
ks156
From: remi <c2m...@c2...> - 2008-04-30 13:40:03
|
Author: remi Date: 2008-04-30 11:59:44 +0200 (Wed, 30 Apr 2008) New Revision: 1037 Added: software_suite_v2/middleware/tuxdriver/branches/hid_transition/src/tux_hid_unix software_suite_v2/middleware/tuxdriver/branches/hid_transition/src/tux_hid_unix.c Log: added HID interface module for linux (work in progress) Added: software_suite_v2/middleware/tuxdriver/branches/hid_transition/src/tux_hid_unix =================================================================== (Binary files differ) Property changes on: software_suite_v2/middleware/tuxdriver/branches/hid_transition/src/tux_hid_unix ___________________________________________________________________ Name: svn:executable + * Name: svn:mime-type + application/octet-stream Added: software_suite_v2/middleware/tuxdriver/branches/hid_transition/src/tux_hid_unix.c =================================================================== --- software_suite_v2/middleware/tuxdriver/branches/hid_transition/src/tux_hid_unix.c (rev 0) +++ software_suite_v2/middleware/tuxdriver/branches/hid_transition/src/tux_hid_unix.c 2008-04-30 09:59:44 UTC (rev 1037) @@ -0,0 +1,189 @@ +/* + * Tux Droid - Hid interface (only for unix) + * Copyright (C) 2008 C2ME Sa + * + * 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. + */ + +#ifndef WIN32 + +#include <stdlib.h> +#include <stdio.h> +#include <sys/ioctl.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <asm/types.h> +#include <fcntl.h> +#include <unistd.h> +#include <linux/hiddev.h> + +#include <string.h> +#include <dirent.h> + +#include "tux_hid_unix.h" +#include "tux_misc.h" + +static int tux_device_hdl = -1; +static char tux_device_path[256] = ""; +struct hiddev_usage_ref uref_out; +struct hiddev_report_info rinfo_out; + +static void +showReports(int fd, unsigned report_type) +{ + struct hiddev_report_info rinfo; + struct hiddev_field_info finfo; + struct hiddev_usage_ref uref; + int i, j, ret; + + rinfo.report_type = report_type; + rinfo.report_id = HID_REPORT_ID_FIRST; + ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo); + + // Get Reports + while (ret >= 0) + { + // Copy the output report + if( report_type == HID_REPORT_TYPE_OUTPUT ) { + memcpy(&rinfo_out, &rinfo, sizeof(rinfo)); + } + + // Get Fields + for (i = 0; i < rinfo.num_fields; i++) + { + finfo.report_type = rinfo.report_type; + finfo.report_id = rinfo.report_id; + finfo.field_index = i; + ioctl(fd, HIDIOCGFIELDINFO, &finfo); + + // Get usages + for (j = 0; j < finfo.maxusage; j++) + { + uref.report_type = finfo.report_type; + uref.report_id = finfo.report_id; + uref.field_index = i; + uref.usage_index = j; + ioctl(fd, HIDIOCGUCODE, &uref); + ioctl(fd, HIDIOCGUSAGE, &uref); + + if(uref.report_type == HID_REPORT_TYPE_OUTPUT && j==0) { + memcpy(&uref_out, &uref, sizeof(uref)); + } + } + } + rinfo.report_id |= HID_REPORT_ID_NEXT; + ret = ioctl(fd, HIDIOCGREPORTINFO, &rinfo); + } +} + +static bool +find_dongle_from_path(char *path, int vendor_id, int product_id) +{ + DIR* dir; + struct dirent *dinfo; + int fd = -1; + char device_path[256] = ""; + struct hiddev_devinfo device_info; + int err; + + /* Normal path to scan is /dev/usb */ + dir = opendir(path); + if (dir != NULL) + { + while ((dinfo = readdir(dir)) != NULL) + { + if (strncmp(dinfo->d_name, "hiddev", 6) == 0) + { + sprintf(device_path, "%s/%s", path, dinfo->d_name); + + if ((fd = open(device_path, O_RDONLY)) >= 0) + { + err = ioctl(fd, HIDIOCGDEVINFO, &device_info); + if ((device_info.vendor == vendor_id) && + ((device_info.product & 0xFFFF) == product_id)) + { + sprintf(tux_device_path, "%s", device_path); + tux_device_hdl = fd; + + closedir(dir); + return true; + } + else + { + close(fd); + } + } + } + } + + closedir(dir); + } + + return false; +} + +bool LIBLOCAL +tux_hid_capture(int vendor_id, int product_id) +{ + /* Normal path to scan is /dev/usb */ + if (find_dongle_from_path("/dev/usb", vendor_id, product_id)) + { + return true; + } + + /* Other possible path to scan is /dev/usb */ + if (find_dongle_from_path("/dev", vendor_id, product_id)) + { + return true; + } + + /* dongle not found */ + return false; +} + +int +main(int argc, char **argv) +{ + unsigned char go_am[] = "\x00\x31\x0A\x00\x00"; + int i; + + if (tux_hid_capture(0x03EB, 0xFF07)) + { + printf("Dongle was found [%s]\n", tux_device_path); + + showReports(tux_device_hdl, HID_REPORT_TYPE_INPUT); + showReports(tux_device_hdl, HID_REPORT_TYPE_OUTPUT); + + for(i = 0; i < 5; i++) { + uref_out.usage_index = i; + uref_out.value = go_am[i]; + ioctl(tux_device_hdl,HIDIOCSUSAGE, &uref_out); + } + ioctl(tux_device_hdl,HIDIOCSREPORT,&rinfo_out); + + sleep(5); + close(tux_device_hdl); + return 0; + } + else + { + printf("Dongle was not found !\n"); + return -1; + } +} + +#endif /* Not WIN32 */ + |