[tuxdroid-svn] r5906 - in software_suite_v3/smart-core/smart-lib: . c c/hid c/hid/trunk c/hid/trunk
Status: Beta
Brought to you by:
ks156
From: remi <svn...@ky...> - 2009-11-20 12:57:49
|
Author: remi Date: 2009-11-20 13:57:28 +0100 (Fri, 20 Nov 2009) New Revision: 5906 Added: software_suite_v3/smart-core/smart-lib/c/ software_suite_v3/smart-core/smart-lib/c/hid/ software_suite_v3/smart-core/smart-lib/c/hid/branches/ software_suite_v3/smart-core/smart-lib/c/hid/tags/ software_suite_v3/smart-core/smart-lib/c/hid/trunk/ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.c software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.h software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.c software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.h software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/libhid.c software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/misc.h software_suite_v3/smart-core/smart-lib/c/hid/trunk/unix/ software_suite_v3/smart-core/smart-lib/c/hid/trunk/unix/Makefile software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/ software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/Makefile software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/compile.bat software_suite_v3/smart-core/smart-lib/c/hid/trunk/wrappers/ software_suite_v3/smart-core/smart-lib/c/hid/trunk/wrappers/LibHID.py Log: * Added a shared library to control hid devices Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.c =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.c (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.c 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,265 @@ +/* + * Kysoh - Hid interface (only for linux) + * Copyright (C) 2009 Kysoh S.A. <in...@ky...> + * Orininaly written by Rémi Jocaille + * + * 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 + */ + +/* $Id: hid_unix.c 20 2009-11-13 11:36:57Z paul $ */ + +#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 "hid_unix.h" + +static int device_hdl = -1; +static char dev_path[256] = ""; +static struct hiddev_usage_ref uref_out; +static struct hiddev_report_info rinfo_out; + +/** + * \brief Search the HID dongle in a "dev" path. + * \param path Path how to search. + * \param vendor_id Dongle vendor ID. + * \param product_id Dongle product ID. + * \return true or false + */ +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(dev_path, "%s", device_path); + device_hdl = fd; + + closedir(dir); + + return true; + } + else + { + close(fd); + } + } + } + } + + closedir(dir); + } + + return false; +} + +/** + * \brief Check if the dongle is still plugged. + * \return true or false. + */ +static bool +check_device_still_plugged(void) +{ + FILE *fp; + + if (device_hdl == -1) + { + return false; + } + else + { + fp = fopen(dev_path, "r"); + if (fp) + { + fclose(fp); + return true; + } + else + { + return false; + } + } +} + +/** + * \brief Capture the HID dongle. + * \param vendor_id Dongle vendor ID. + * \param product_id Dongle product ID. + * \return true or false. + */ +LIBLOCAL bool +device_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 */ + if (find_dongle_from_path("/dev", vendor_id, product_id)) + { + return true; + } + + /* dongle not found */ + return false; +} + +/** + * \brief Release the access to the HID dongle. + */ +LIBLOCAL void +device_hid_release(void) +{ + if (device_hdl != -1) + { + close(device_hdl); + device_hdl = -1; + } +} + +/** + * \brief Write data to the HID dongle. + * \param size Data size. + * \param buffer Data to write. + * \return The write success. + */ +LIBLOCAL bool +device_hid_write(int size, const unsigned char *buffer) +{ + int i; + int err; + + if (!check_device_still_plugged()) + { + return false; + } + + rinfo_out.report_type = HID_REPORT_TYPE_OUTPUT; + rinfo_out.report_id = HID_REPORT_ID_FIRST; + + err = ioctl(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(device_hdl,HIDIOCSUSAGE, &uref_out); + } + + err = ioctl(device_hdl,HIDIOCSREPORT,&rinfo_out); + + return true; +} + +/** + * \brief Read data from the HID dongle. + * \param size Data size. + * \param buffer Data buffer. + * \return The read success. + */ +LIBLOCAL bool +device_hid_read(int size, char *buffer) +{ + int i; + int err; + + if (!check_device_still_plugged()) + { + return false; + } + + rinfo_out.report_type = HID_REPORT_TYPE_INPUT; + rinfo_out.report_id = HID_REPORT_ID_FIRST; + + err = ioctl(device_hdl, HIDIOCGREPORTINFO, &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(device_hdl, HIDIOCGUCODE, &uref_out); + if (err < 0) + { + return false; + } + + err = ioctl(device_hdl, HIDIOCGUSAGE, &uref_out); + if (err < 0) + { + return false; + } + + buffer[i] = uref_out.value; + } + + return true; +} + +/** + * \brief Check that the hid device is still connected. + * \return true or false. + */ +LIBLOCAL bool +device_hid_still_connected(void) +{ + return check_device_still_plugged(); +} + +#endif /* Not WIN32 */ Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.h =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.h (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_unix.h 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,50 @@ +/* + * Kysoh - Hid interface (only for linux) + * Copyright (C) 2009 Kysoh S.A. <in...@ky...> + * Orininaly written by Rémi Jocaille + * + * 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 + */ + +/* $Id: hid_unix.h 17 2009-11-13 11:31:15Z paul $ */ + +/** + * \file hid_unix.h + * \brief Tux HID header. + * \author rem...@c2... + * \ingroup hid_interface + */ + +#ifndef WIN32 + +#ifndef _TUX_HID_H_ +#define _TUX_HID_H_ + +#include <stdbool.h> +#include "misc.h" + +/** \brief HID USB timout */ +#define HID_RW_TIMEOUT 1000 + +extern bool device_hid_capture(int vendor_id, int product_id); +extern void device_hid_release(void); +extern bool device_hid_write(int size, const unsigned char *buffer); +extern bool device_hid_read(int size, char *buffer); +extern bool device_hid_still_connected(void); + +#endif /* _TUX_HID_H_ */ + +#endif /* Not WIN32 */ + Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.c =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.c (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.c 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,267 @@ +/* + * Kysoh - Hid interface (only for windows) + * Copyright (C) 2009 Kysoh S.A. <in...@ky...> + * + * 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. + */ + +/** + * \file hid_win32.c + * \brief HID functions. + * \author rem...@c2... + * \ingroup hid_interface + */ + +#ifdef WIN32 + +#include <windows.h> +#include <hidsdi.h> +#include <setupapi.h> +#include <dbt.h> +#include <stdbool.h> +#include <stdio.h> +#include "hid_win32.h" + +static char device_symbolic_name[256] = ""; +static HANDLE tux_device_hdl = NULL; +static COMMTIMEOUTS timeout; + +/** + * \brief Capture the HID dongle. + * \param vendor_id Dongle vendor ID. + * \param product_id Dongle product ID. + * \return true or false. + */ +LIBLOCAL bool +device_hid_capture(int vendor_id, int product_id) +{ + GUID hid_guid; + HANDLE h_dev_info; + SP_DEVICE_INTERFACE_DATA dev_info_data; + PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = NULL; + int member_index = 0; + bool last_device = false; + long result; + unsigned long length = 0; + ULONG required; + HANDLE device_hdl = NULL; + HIDD_ATTRIBUTES attributes; + bool tux_found = false; + + if (tux_device_hdl != NULL) + { + return false; + } + + HidD_GetHidGuid(&hid_guid); + + h_dev_info = SetupDiGetClassDevs(&hid_guid, + NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); + + dev_info_data.cbSize = sizeof(dev_info_data); + + member_index = 0; + + do + { + result = SetupDiEnumDeviceInterfaces(h_dev_info, 0, + &hid_guid, member_index, &dev_info_data); + + if (result != 0) + { + result = SetupDiGetDeviceInterfaceDetail(h_dev_info, + &dev_info_data, NULL, 0, &length, NULL); + + detail_data = (PSP_DEVICE_INTERFACE_DETAIL_DATA)malloc(length); + detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); + + result = SetupDiGetDeviceInterfaceDetail(h_dev_info, + &dev_info_data, detail_data, length, &required, NULL); + + device_hdl = CreateFile(detail_data->DevicePath, 0, + FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL, + OPEN_EXISTING, 0, NULL); + + attributes.Size = sizeof(attributes); + result = HidD_GetAttributes(device_hdl, &attributes); + + if ((attributes.VendorID == vendor_id) && + (attributes.ProductID == product_id)) + { + sprintf(device_symbolic_name, "%s", detail_data->DevicePath); + + CloseHandle(device_hdl); + + tux_device_hdl = CreateFile(detail_data->DevicePath, + GENERIC_WRITE|GENERIC_READ, + FILE_SHARE_READ|FILE_SHARE_WRITE, + (LPSECURITY_ATTRIBUTES)NULL, + OPEN_EXISTING, 0, NULL); + + timeout.ReadTotalTimeoutConstant = HID_RW_TIMEOUT; + timeout.WriteTotalTimeoutConstant = HID_RW_TIMEOUT; + SetCommTimeouts(tux_device_hdl, &timeout); + + tux_found = true; + + break; + } + + CloseHandle(device_hdl); + free(detail_data); + } + else + { + last_device = true; + } + + member_index++; + + } + while (last_device == false); + + SetupDiDestroyDeviceInfoList(h_dev_info); + + if (tux_found) + { + return true; + } + else + { + return false; + } +} + +/** + * \brief Release the access to the HID dongle. + */ +LIBLOCAL void +device_hid_release(void) +{ + if (tux_device_hdl != NULL) + { + CloseHandle(tux_device_hdl); + tux_device_hdl = NULL; + } +} + +/** + * \brief Write data to the HID dongle. + * \param size Data size. + * \param buffer Data to write. + * \return The write success. + */ +LIBLOCAL bool +device_hid_write(int size, const unsigned char *buffer) +{ + long wrt_count; + char report[REPORT_SIZE_OUT + 1] = { [0 ... REPORT_SIZE_OUT] = 0 }; + long result; + + if (size > REPORT_SIZE_OUT) + { + return false; + } + + if (tux_device_hdl == NULL) + { + return false; + } + + report[0] = 0; + memcpy(&report[1], buffer, size); + + result = WriteFile(tux_device_hdl, report, REPORT_SIZE_OUT + 1, &wrt_count, + NULL); + + if (!result) + { + return false; + } + else + { + return true; + } +} + +/** + * \brief Read data from the HID dongle. + * \param size Data size. + * \param buffer Data buffer. + * \return The read success. + */ +LIBLOCAL bool +device_hid_read(int size, unsigned char *buffer) +{ + long rd_count; + char report[REPORT_SIZE_IN + 1]; + long result; + + if (size > REPORT_SIZE_IN) + { + return false; + } + + if (tux_device_hdl == NULL) + { + return false; + } + + result = ReadFile(tux_device_hdl, report, REPORT_SIZE_IN + 1, &rd_count, + NULL); + + memcpy(buffer, &report[1], size); + + if (!result) + { + return false; + } + else + { + return true; + } +} + +/** + * \brief Check that the hid device is still connected. + * \return true or false. + */ +LIBLOCAL bool +device_hid_still_connected(void) +{ + long result; + HIDD_ATTRIBUTES attributes; + + if (tux_device_hdl == NULL) + { + return false; + } + else + { + attributes.Size = sizeof(attributes); + result = HidD_GetAttributes(tux_device_hdl, &attributes); + if (result != 0) + { + return true; + } + else + { + return false; + } + } +} + +#endif /* WIN32 */ Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.h =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.h (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/hid_win32.h 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,49 @@ +/* + * Kysoh - Hid interface (only for windows) + * Copyright (C) 2009 Kysoh S.A. <in...@ky...> + * + * 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. + */ + +/** + * \file hid_win32.h + * \brief Tux HID header. + * \author rem...@c2... + * \ingroup hid_interface + */ + +#ifdef WIN32 + +#ifndef _HID_H_ +#define _HID_H_ +#include <stdbool.h> +#include "misc.h" +/** \brief HID USB timout */ +#define HID_RW_TIMEOUT 1000 +/** \brief HID input report size */ +#define REPORT_SIZE_IN 64 +/** \brief HID output report size */ +#define REPORT_SIZE_OUT 64 + +extern bool device_hid_capture(int vendor_id, int product_id); +extern void device_hid_release(void); +extern bool device_hid_write(int size, const unsigned char *buffer); +extern bool device_hid_read(int size, unsigned char *buffer); +extern bool device_hid_still_connected(void); + +#endif /* _HID_H_ */ + +#endif /* WIN32 */ Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/libhid.c =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/libhid.c (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/libhid.c 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,85 @@ +/* + * Kysoh - Hid interface + * Copyright (C) 2009 Kysoh S.A. <in...@ky...> + * Orininaly written by Rémi Jocaille + * + * 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 <stdlib.h> +#include <stdio.h> +#include <stdint.h> +#include <stdbool.h> +#include "misc.h" +#ifndef WIN32 +# include "hid_unix.h" +#else +# include "hid_win32.h" +#endif + +/** + * \brief Capture the HID device. + * \param vendorId Device vendor ID. + * \param productId Device product ID. + * \return true or false. + */ +LIBEXPORT bool +LIBHID_Capture(int vendorId, int productId) +{ + return device_hid_capture(vendorId, productId); +} + +/** + * \brief Release the access to the HID dongle. + */ +LIBEXPORT void +LIBHID_Release(void) +{ + device_hid_release(); +} + +/** + * \brief Write data to the HID device. + * \param size Data size. + * \param buffer Data to write. + * \return The write success. + */ +LIBEXPORT bool +LIBHID_Write(int size, const unsigned char *buffer) +{ + return device_hid_write(size, buffer); +} + +/** + * \brief Read data from the HID device. + * \param size Data size. + * \param buffer Data buffer. + * \return The read success. + */ +LIBEXPORT bool +LIBHID_Read(int size, char *buffer) +{ + return device_hid_read(size, buffer); +} + +/** + * \brief Check that the HID device is still connected. + * \return true or false. + */ +LIBEXPORT bool +LIBHID_StillConnected(void) +{ + return device_hid_still_connected(); +} Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/misc.h =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/misc.h (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/src/misc.h 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,30 @@ +/* + * Kysoh - Hid interface + * Copyright (C) 2009 Kysoh S.A. <in...@ky...> + * Orininaly written by Rémi Jocaille + * + * 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 + */ + +#ifdef WIN32 +# include <windows.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 Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/unix/Makefile =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/unix/Makefile (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/unix/Makefile 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,33 @@ +PROJECT = libhid +CC = gcc +OBJ_DIR = ./obj +SRC_DIR = ../src +OUTPUT_DIR = ./ +TARGET = libhid.so +C_INCLUDE_DIRS = -I"/usr/local/include" +C_PREPROC = +CFLAGS = -pipe -std=gnu99 -Wall -g0 -O2 -fPIC +RC_INCLUDE_DIRS = +RC_PREPROC = +RCFLAGS = +LIB_DIRS = +LIBS = -lm +LDFLAGS = -pipe -shared + +SRC_OBJS = \ + $(OBJ_DIR)/hid_unix.o \ + $(OBJ_DIR)/libhid.o + +.PHONY: make clean + +make: + -@if [ ! -d "$(OBJ_DIR)" ]; then mkdir "$(OBJ_DIR)"; fi + -@if [ ! -d "$(OUTPUT_DIR)" ]; then mkdir "$(OUTPUT_DIR)"; fi + $(CC) -c $(CFLAGS) $(SRC_DIR)/hid_unix.c $(C_INCLUDE_DIRS) -o $(OBJ_DIR)/hid_unix.o + $(CC) -c $(CFLAGS) $(SRC_DIR)/libhid.c $(C_INCLUDE_DIRS) -o $(OBJ_DIR)/libhid.o + $(CC) -o "$(OUTPUT_DIR)$(TARGET)" $(SRC_OBJS) $(LIB_DIRS) $(LIBS) $(LDFLAGS) + -@ar rcs $(OUTPUT_DIR)/libhid.a $(SRC_OBJS) + -@rm -fR $(OBJ_DIR)/*.o + +clean: + -@rm -fR $(OBJ_DIR)/*.o $(OBJ_DIR) $(PROJECT).a $(PROJECT).so Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/Makefile =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/Makefile (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/Makefile 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,37 @@ +# +# You can use this makefile in the MSYS environement with MinGW installed. +# This soft need the windows DDK. +# Please change the "C_INCLUDE_DIRS" variable with you DDK path. +# +PROJECT = libhid +CC = gcc +OBJ_DIR = ./obj +SRC_DIR = ../src +OUTPUT_DIR = ./ +TARGET = libhid.dll +C_INCLUDE_DIRS = -I C:/MinGWStudio/MinGW/include/ddk +C_PREPROC = +CFLAGS = -pipe -Wall -g2 -O0 +RC_INCLUDE_DIRS = +RC_PREPROC = +RCFLAGS = +LIB_DIRS = +LIBS = -lwinmm -lhid -lsetupapi -lhidparse +LDFLAGS = -pipe -shared -Wl,--output-def,"$(OUTPUT_DIR)\libhid.def",--out-implib,"$(OUTPUT_DIR)\libhid.a" -s + +SRC_OBJS = \ + $(OBJ_DIR)/hid_win32.o \ + $(OBJ_DIR)/libhid.o + +.PHONY: make clean + +make: + -@if [ ! -d "$(OBJ_DIR)" ]; then mkdir "$(OBJ_DIR)"; fi + -@if [ ! -d "$(OUTPUT_DIR)" ]; then mkdir "$(OUTPUT_DIR)"; fi + $(CC) -c $(CFLAGS) $(SRC_DIR)/hid_win32.c $(C_INCLUDE_DIRS) -o $(OBJ_DIR)/hid_win32.o + $(CC) -c $(CFLAGS) $(SRC_DIR)/libhid.c $(C_INCLUDE_DIRS) -o $(OBJ_DIR)/libhid.o + $(CC) -o "$(OUTPUT_DIR)\$(TARGET)" $(SRC_OBJS) $(LIB_DIRS) $(LIBS) $(LDFLAGS) + -@rm -fR $(OBJ_DIR)/*.o + +clean: + -@rm -fR $(OBJ_DIR)/*.o Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/compile.bat =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/compile.bat (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/win32/compile.bat 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,3 @@ +set PATH=C:\MinGWStudio\MinGW\bin;%PATH% +make.exe -f Makefile +cmd \ No newline at end of file Added: software_suite_v3/smart-core/smart-lib/c/hid/trunk/wrappers/LibHID.py =================================================================== --- software_suite_v3/smart-core/smart-lib/c/hid/trunk/wrappers/LibHID.py (rev 0) +++ software_suite_v3/smart-core/smart-lib/c/hid/trunk/wrappers/LibHID.py 2009-11-20 12:57:28 UTC (rev 5906) @@ -0,0 +1,189 @@ +#!/usr/bin/python + +# Copyright (C) 2009 Kysoh S.A. <in...@ky...> +# Distributed under the terms of the GNU General Public License +# http://www.gnu.org/copyleft/gpl.html + +import os +import sys +import time +from ctypes import * + +# ============================================================================== +# MPatern ctype object. +# ============================================================================== +class MPatern(Structure): + """MPatern ctype object. + """ + _fields_ = [("array", c_byte * 64)] + +# ============================================================================== +# LibHID Object. +# ============================================================================== +class LibHID(object): + """LibHID Object. + """ + + # -------------------------------------------------------------------------- + # Constructor. + # -------------------------------------------------------------------------- + def __init__(self, vendorId, productId): + """Constructor. + @param vendorId: Device vendor Id. + @param productId: Device product Id. + """ + self.__vendorId = vendorId + self.__productId = productId + libraryPath = "" + mPath, mFile = os.path.split(__file__) + if os.name == 'nt': + libraryPath = os.path.join(mPath, "libhid.dll") + else: + libraryPath = os.path.join(mPath, "libhid.so") + self.lib = None + if os.path.isfile(libraryPath): + try: + self.lib = CDLL(libraryPath) + except: + pass + else: + pass + + # -------------------------------------------------------------------------- + # Capture the device. + # -------------------------------------------------------------------------- + def capture(self): + """Capture the device. + """ + if self.lib == None: + return False + else: + ret = self.lib.LIBHID_Capture(c_int(self.__vendorId), c_int(self.__productId)) + if ret == 0: + return False + else: + return True + + # -------------------------------------------------------------------------- + # Release the device. + # -------------------------------------------------------------------------- + def release(self): + """Release the device. + """ + if self.lib == None: + return + else: + self.lib.LIBHID_Release() + + # -------------------------------------------------------------------------- + # Read data from the device. + # -------------------------------------------------------------------------- + def read(self, data): + """Read data from the device. + @param data: Data to read as List (max length is 64) + @return: True or False. + """ + if self.lib == None: + return False + else: + size = len(data) + if size > 64: + return False + dSize = 64 - len(data) + data = data + [0] * dSize + cData = self.__listToMPatern(data) + ret = self.lib.LIBHID_Read(c_int(size), pointer(cData)) + if ret == 0: + return False + else: + for i in range(len(data)): + data[i] = cData.array[i] + return True + + # -------------------------------------------------------------------------- + # Write data to the device. + # -------------------------------------------------------------------------- + def write(self, data): + """Write data to the device. + @param data: Data to write as List (max length is 64) + @return: True or False. + """ + if self.lib == None: + return False + else: + size = len(data) + if size > 64: + return False + dSize = 64 - len(data) + data = data + [0] * dSize + cData = self.__listToMPatern(data) + ret = self.lib.LIBHID_Write(c_int(size), pointer(cData)) + if ret == 0: + return False + else: + return True + + # -------------------------------------------------------------------------- + # Check if the device is connected. + # -------------------------------------------------------------------------- + def isConnected(self): + """Check if the device is connected. + @return: True or False. + """ + if self.lib == None: + return False + else: + ret = self.lib.LIBHID_StillConnected() + if ret == 0: + return False + else: + return True + + # -------------------------------------------------------------------------- + # Wait for device connected. + # -------------------------------------------------------------------------- + def waitForConnected(self, timeout = 10.0): + """Wait for device connected. + @param timeout: Maximal time to wait (default is 10 secondes). + @return: True or False. + """ + if self.lib == None: + return False + else: + if self.isConnected(): + return True + cycles = (int)(timeout * 4) + for i in range(cycles): + if self.capture(): + return True + time.sleep(0.25) + return False + + # -------------------------------------------------------------------------- + # Wait for device disconnected. + # -------------------------------------------------------------------------- + def waitForDisconnected(self, timeout = 10.0): + """Wait for device disconnected. + @param timeout: Maximal time to wait (default is 10 secondes). + @return: True or False. + """ + if self.lib == None: + return True + else: + cycles = (int)(timeout * 4) + for i in range(cycles): + if not self.isConnected(): + return True + time.sleep(0.25) + return False + + # -------------------------------------------------------------------------- + # Convert a list to cType char array. + # -------------------------------------------------------------------------- + def __listToMPatern(self, charList): + """Convert a list to cType char array. + """ + t_char = tuple(charList) + self.p = MPatern() + self.p.array = (c_byte * len(t_char))(*t_char) + return self.p |