[tuxdroid-svn] r1205 - firmware/tuxup/trunk
Status: Beta
Brought to you by:
ks156
From: Paul_R <c2m...@c2...> - 2008-05-28 12:05:03
|
Author: Paul_R Date: 2008-05-28 14:05:06 +0200 (Wed, 28 May 2008) New Revision: 1205 Added: firmware/tuxup/trunk/tux_hid_unix.c firmware/tuxup/trunk/tux_hid_unix.h firmware/tuxup/trunk/tux_misc.c firmware/tuxup/trunk/tux_misc.h Log: * Oups, forget to add some modules ... Added: firmware/tuxup/trunk/tux_hid_unix.c =================================================================== --- firmware/tuxup/trunk/tux_hid_unix.c (rev 0) +++ firmware/tuxup/trunk/tux_hid_unix.c 2008-05-28 12:05:06 UTC (rev 1205) @@ -0,0 +1,192 @@ +/* + * 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. + */ + + +#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] = ""; +static struct hiddev_usage_ref uref_out; +static struct hiddev_report_info rinfo_out; + +static bool +find_dongle_from_path(const 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; + + 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; +} + +void LIBLOCAL +tux_hid_release(void) +{ + if (tux_device_hdl != -1) + { + close(tux_device_hdl); + tux_device_hdl = -1; + } +} + +bool LIBLOCAL +tux_hid_write(int size, const unsigned char *buffer) +{ + int i; + int err; + + rinfo_out.report_type = HID_REPORT_TYPE_OUTPUT; + rinfo_out.report_id = HID_REPORT_ID_FIRST; + + err = ioctl(tux_device_hdl, HIDIOCGREPORTINFO, &rinfo_out); + if (err < 0) + { + return false; + } + + for(i = 0; i < size; i++) + { + uref_out.report_type = HID_REPORT_TYPE_OUTPUT; + uref_out.report_id = HID_REPORT_ID_FIRST; + uref_out.usage_index = i; + uref_out.value = (unsigned char)buffer[i]; + + err = ioctl(tux_device_hdl,HIDIOCSUSAGE, &uref_out); + if (err < 0) + { + return false; + } + } + + err = ioctl(tux_device_hdl,HIDIOCSREPORT,&rinfo_out); + if (err < 0) + { + return false; + } + + return true; +} + +bool LIBLOCAL +tux_hid_read(int size, unsigned char *buffer) +{ + int i; + int err; + + rinfo_out.report_type = HID_REPORT_TYPE_INPUT; + rinfo_out.report_id = HID_REPORT_ID_FIRST; + + err = ioctl(tux_device_hdl, HIDIOCGREPORT, &rinfo_out); + if (err < 0) + { + return false; + } + + for (i = 0; i < size; i++) + { + uref_out.report_type = HID_REPORT_TYPE_INPUT; + uref_out.report_id = HID_REPORT_ID_FIRST; + uref_out.usage_index = i; + + err = ioctl(tux_device_hdl, HIDIOCGUCODE, &uref_out); + if (err < 0) + { + return false; + } + + err = ioctl(tux_device_hdl, HIDIOCGUSAGE, &uref_out); + if (err < 0) + { + return false; + } + buffer[i] = uref_out.value; + } + return true; +} + Property changes on: firmware/tuxup/trunk/tux_hid_unix.c ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/tuxup/trunk/tux_hid_unix.h =================================================================== --- firmware/tuxup/trunk/tux_hid_unix.h (rev 0) +++ firmware/tuxup/trunk/tux_hid_unix.h 2008-05-28 12:05:06 UTC (rev 1205) @@ -0,0 +1,39 @@ +/* + * 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 + +#ifndef _TUX_HID_H_ +#define _TUX_HID_H_ + +#include <stdbool.h> +#include <stdio.h> + +#define HID_RW_TIMEOUT 1000 + +extern bool tux_hid_capture(int vendor_id, int product_id); +extern void tux_hid_release(void); +extern bool tux_hid_write(int size, const unsigned char *buffer); +extern bool tux_hid_read(int size, unsigned char *buffer); + +#endif /* _TUX_HID_H_ */ + +#endif /* Not WIN32 */ + Property changes on: firmware/tuxup/trunk/tux_hid_unix.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/tuxup/trunk/tux_misc.c =================================================================== --- firmware/tuxup/trunk/tux_misc.c (rev 0) +++ firmware/tuxup/trunk/tux_misc.c 2008-05-28 12:05:06 UTC (rev 1205) @@ -0,0 +1,211 @@ +/* + * Tux Droid - Misc + * 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. + */ + +#include <stdio.h> +#include <string.h> + +#include "tux_misc.h" + +#ifdef WIN32 +# include <time.h> +# include <windows.h> +#else +# include <sys/time.h> +#endif + +#ifdef WIN32 +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +# define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +# define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +/** + * + */ +static int +gettimeofday(struct timeval *tv, struct timezone *tz) +{ + FILETIME ft; + unsigned __int64 tmpres = 0; + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tmpres /= 10; /*convert into microseconds*/ + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + tz->tz_minuteswest = _timezone / 60; + tz->tz_dsttime = _daylight; + } + + return 0; +} +#endif /* WIN32 */ + +/** + * + */ +LIBEXPORT double +get_time(void) +{ + double result; + struct timeval tv; + struct timezone tz; + + gettimeofday(&tv, &tz); + result = ((double)tv.tv_usec / 1000000) + (double)tv.tv_sec; + + return result; +} + +LIBLOCAL bool +str_to_uint8(const char *str, unsigned char *dest) +{ + int r, val; + + r = sscanf(str, "%d", &val); + + if (r == 1) + { + if ((val >= 0) && (val <= 255)) + { + *dest = val; + return true; + } + } + + return false; +} + +LIBLOCAL bool +str_to_int8(const char *str, char *dest) +{ + int r, val; + + r = sscanf(str, "%d", &val); + + if (r == 1) + { + if ((val >= -128) && (val <= 127)) + { + *dest = val; + return true; + } + } + + return false; +} + +LIBLOCAL bool +str_to_int(const char *str, int *dest) +{ + int r, val; + + r = sscanf(str, "%d", &val); + + if (r == 1) + { + *dest = val; + return true; + } + + return false; +} + +LIBLOCAL bool +str_to_bool(const char *str, bool *dest) +{ + if (!strcmp(str, "True")) + { + *dest = true; + return true; + } + else + { + if (!strcmp(str, "False")) + { + *dest = false; + return true; + } + } + + return false; +} + +LIBLOCAL bool +str_to_float(const char *str, float *dest) +{ + int r; + float val; + + r = sscanf(str, "%f", &val); + + if (r == 1) + { + *dest = val; + return true; + } + + return false; +} + +LIBLOCAL bool +hex_to_uint8(const char *str, unsigned char *dest) +{ + int r; + int val; + + r = sscanf(str, "0x%2x", &val); + + if (r == 1) + { + if ((val >= 0) && (val <= 255)) + { + *dest = val; + return true; + } + } + + return false; +} Property changes on: firmware/tuxup/trunk/tux_misc.c ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native Added: firmware/tuxup/trunk/tux_misc.h =================================================================== --- firmware/tuxup/trunk/tux_misc.h (rev 0) +++ firmware/tuxup/trunk/tux_misc.h 2008-05-28 12:05:06 UTC (rev 1205) @@ -0,0 +1,57 @@ +/* + * Tux Droid - Misc + * 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 _TUX_MISC_H_ +#define _TUX_MISC_H_ + +#include <stdbool.h> +#include <stdint.h> + +#define FW_MAIN_LOOP_DELAY 0.004 + +#define TUX_VID 0x03eb // Atmel VID +#define TUX_PID 0xFF07 // Tux PID + +#ifdef WIN32 +# include <windows.h> +# include <mmsystem.h> +# define sleep(sec) Sleep(sec * 1000) +# define usleep(usec) Sleep(usec / 1000) +# define LIBEXPORT __declspec(dllexport) +# define LIBLOCAL +#else +# define LIBEXPORT __attribute__ ((visibility ("default"))) +# define LIBLOCAL __attribute__ ((visibility ("hidden"))) +#endif + + +/** + * Callback function prototype for simple event + */ +typedef void(*simple_callback_t)(void); + +extern double get_time(void); +extern bool str_to_uint8(const char *str, unsigned char *dest); +extern bool str_to_int8(const char *str, char *dest); +extern bool str_to_int(const char *str, int *dest); +extern bool str_to_bool(const char *str, bool *dest); +extern bool str_to_float(const char *str, float *dest); +extern bool hex_to_uint8(const char *str, unsigned char *dest); +#endif /* _TUX_MISC_H_ */ Property changes on: firmware/tuxup/trunk/tux_misc.h ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:keywords + Id Name: svn:eol-style + native |