diff --git a/tools/bfutil/src/Makefile b/tools/bfutil/src/Makefile deleted file mode 100644 index 31ffb41d..00000000 --- a/tools/bfutil/src/Makefile +++ /dev/null @@ -1,56 +0,0 @@ -CC = gcc -LD = gcc -CFLAGS += -O0 -g3 -Wall -std=gnu99 -LIBS += -lusb-1.0 -OBJDIR = _build - - - -ifeq ($(OS),Windows_NT) - LIBS += -Llibusb-1.0 - BINFILE = bfutil.exe - BINDIR := win32 - COPYDLL = cp libusb-1.0/libusb-1.0.dll $(BINDIR)/libusb-1.0.dll -else - BINFILE = bfutil - COPYDLL = - - OS := $(shell uname -s) - ifeq ($(OS),Linux) - ifeq ($(MBIT),32) - BINDIR :=linux32 - CFLAGS +=-m32 - else - LBITS := $(shell getconf LONG_BIT) - ifeq ($(LBITS),64) - BINDIR := linux64 - else - BINDIR := linux32 - endif - endif - endif - ifeq ($(OS),Darwin) - BINDIR := macos - endif -endif - -BINDIR := ../binaries/$(BINDIR) -$(info $(BINDIR)) - -all: clean $(OBJDIR) bfutil - -.PHONY: $(OBJDIR) -$(OBJDIR): - @mkdir -p $(OBJDIR) - @mkdir -p $(BINDIR) - -$(OBJDIR)/%.o: %.c - $(CC) -c -o $@ $< $(CFLAGS) - -bfutil: $(OBJDIR)/bfutil.o - $(LD) -o $(BINDIR)/$(BINFILE) $^ $(CFLAGS) $(LIBS) - $(COPYDLL) - -clean: - @rm -rf $(OBJDIR) - @rm -f $(BINDIR)/$(BINFILE) diff --git a/tools/bfutil/src/bfutil.c b/tools/bfutil/src/bfutil.c deleted file mode 100644 index 23dd2c8b..00000000 --- a/tools/bfutil/src/bfutil.c +++ /dev/null @@ -1,313 +0,0 @@ -/**************************************************************************/ -/*! - @file bfutil.c (Bluefruit Util) - @author hathach (tinyusb.org) - - @section LICENSE - - Software License Agreement (BSD License) - - Copyright (c) 2018, Adafruit Industries (adafruit.com) - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - 3. Neither the name of the copyright holders nor the - names of its contributors may be used to endorse or promote products - derived from this software without specific prior written permission. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY - EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -/**************************************************************************/ - -#include -#include -#include -#include - -#include -#include - -#include "libusb-1.0/libusb.h" - -#if defined(_WIN32) - -#include -static inline void sleep_ms(int ms) -{ - Sleep(ms); -} - - -#else - -#include -static inline void sleep_ms(int ms) -{ - usleep(ms*1000); -} - -#endif - -/*------------------------------------------------------------------*/ -/* MACRO TYPEDEF CONSTANT ENUM - *------------------------------------------------------------------*/ -#define ASSERT(x) assert( (x) == LIBUSB_SUCCESS ) - -enum -{ - RESET_DELAY = 1000, // 1 second delay - RESET_MSC_DELAY = 2000, // MSC available require more time for USB to enumerate -}; - -enum -{ - ARDUINO_ADDR = 0x080C0000, - FEATHERLIB_ADDR = 0x08010000 -}; - -enum -{ - USB_VID = 0xcafe, - USB_PID = 0x4011, - - USB_PID_MSC = 0x8010, - USB_DFU_PID = 0x0008, -}; - -// SDEP Command Type -enum -{ - SDEP_MSGTYPE_COMMAND = 0x10, - SDEP_MSGTYPE_RESPONSE = 0x20, - SDEP_MSGTYPE_ALERT = 0x40, - SDEP_MSGTYPE_ERROR = 0x80, -}; - -// SDEP Command Constant -enum -{ - // General Purpose - SDEP_CMD_RESET = 0x0001, // HW reset - SDEP_CMD_FACTORYRESET = 0x0002, // Factory reset - SDEP_CMD_DFU = 0x0003, // Enter DFU mode - SDEP_CMD_INFO = 0x0004, // System information - SDEP_CMD_NVM_RESET = 0x0005, // Reset DCT - SDEP_CMD_ERROR_STRING = 0x0006, // Get descriptive error string - SDEP_CMD_COMMAND_STRING = 0x0007, // Get descriptive SDEP command string - SDEP_CMD_SFLASH_ERASEALL = 0x0008, // Erase SPI flash memory - - // DEBUG Commands - SDEP_CMD_STACKDUMP = 0x0300, // Dump the stack - SDEP_CMD_STACKSIZE = 0x0301, // Get stack size - SDEP_CMD_HEAPDUMP = 0x0302, // Dump the heap - SDEP_CMD_HEAPSIZE = 0x0303, // Get heap size - SDEP_CMD_THREADLIST_PRINT = 0x0304, // Get thread information - SDEP_CMD_THREAD_INFO = 0x0305, // Get thread info - SDEP_CMD_ARDUINO_HEAPSIZE = 0x0306, // Get Arduino heap size -}; - - -/*------------------------------------------------------------------*/ -/* VARIABLE DECLARATION - *------------------------------------------------------------------*/ -libusb_device_handle* udev = NULL; - -bool _dfu_mode = false; -bool _msc_enbled = false; - -/*------------------------------------------------------------------*/ -/* FUNCTION DECLARATION - *------------------------------------------------------------------*/ -void print_help(void); -void sdep_syscmd(uint16_t cmd); - -void arduino_upgrade(char* dfu_util, char* binfile); -void featherlib_upgrade(char* dfu_util, char* binfile); - -struct { - const char* command; - uint16_t sdep_cmdid; - void (*func) (char*, char*); -}cmd_table[] = -{ - { "arduino_upgrade" , 0 , arduino_upgrade }, - { "enter_dfu" , SDEP_CMD_DFU , NULL }, - { "erase_spiflash" , SDEP_CMD_SFLASH_ERASEALL , NULL }, - { "factory_reset" , SDEP_CMD_FACTORYRESET , NULL }, - { "featherlib_upgrade" , 0 , featherlib_upgrade }, - { "info" , SDEP_CMD_INFO , NULL }, - { "nvm_reset" , SDEP_CMD_NVM_RESET , NULL }, - { "reboot" , SDEP_CMD_RESET , NULL }, -}; - -/*------------------------------------------------------------------*/ -/* IMPLEMENTATION - *------------------------------------------------------------------*/ -int main(int argc, char *argv[]) -{ - char* command = argv[1]; - - if ( argc <= 1 || (!strcmp("--help", command)) ) - { - print_help(); - return 0; - } - - ASSERT( libusb_init(NULL) ); - - // Open device, try with all PIDs - udev = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_PID); - - if (!udev) - { - udev = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_PID_MSC); - if (udev) _msc_enbled = true; - } - - if (!udev) - { - udev = libusb_open_device_with_vid_pid(NULL, USB_VID, USB_DFU_PID); - if(udev) _dfu_mode = true; - } - - if (!udev) - { - printf("Bluefruit Feather Board is not detected !!!\n"); - return 0; - } - - printf("Bluefruit Feather Board opened !!!\n"); - return 0; - - for(int i=0; i C/C++ -> General). - - Copy the relevant .lib file from MS32\ or MS64\ and add 'libusb-1.0.lib' to - your 'Additional Dependencies' (Configuration Properties -> Linker -> Input) - Also make sure that the directory where libusb-1.0.lib resides is added to - 'Additional Library Directories' (Configuration Properties -> Linker - -> General) - - If you use the static version of the libusb library, make sure that - 'Runtime Library' is set to 'Multi-threaded DLL (/MD)' (Configuration - Properties -> C/C++ -> Code Generation). - NB: If your application requires /MT (Multi-threaded/libCMT), you need to - recompile a static libusb 1.0 library from source. - - Compile and run your application. If you use the DLL version of libusb-1.0, - remember that you need to have a copy of the DLL either in the runtime - directory or in system32 - -o WDK/DDK: - - The following is an example of a sources files that you can use to compile - a libusb 1.0 based console application. In this sample ..\libusb\ is the - directory where you would have copied libusb.h as well as the relevant - libusb-1.0.lib - - TARGETNAME=your_app - TARGETTYPE=PROGRAM - USE_MSVCRT=1 - UMTYPE=console - INCLUDES=..\libusb;$(DDK_INC_PATH) - TARGETLIBS=..\libusb\libusb-1.0.lib - SOURCES=your_app.c - - - Note that if you plan to use libCMT instead of MSVCRT (USE_LIBCMT=1 instead - of USE_MSVCRT=1), you will need to recompile libusb to use libCMT. This can - easily be achieved, in the DDK environment, by running 'ddk_build /MT' - -o MinGW/cygwin - - Copy libusb.h, from include/libusb-1.0/ to your default include directory, - and copy the MinGW32/ or MinGW64/ .a files to your default library directory. - Or, if you don't want to use the default locations, make sure that you feed - the relevant -I and -L options to the compiler. - - Add the '-lusb-1.0' linker option when compiling. - -o Additional information: - - The libusb 1.0 API documentation can be accessed at: - http://api.libusb.info - - For some libusb samples (including source), please have a look in examples/ - - For additional information on the libusb 1.0 Windows backend please visit: - http://windows.libusb.info - - The MinGW and MS generated DLLs are fully interchangeable, provided that you - use the import libs provided or generate one from the .def also provided. - - If you find any issue, please visit http://libusb.info/ and check the - Support section diff --git a/tools/bfutil/src/libusb-1.0/libusb-1.0.dll b/tools/bfutil/src/libusb-1.0/libusb-1.0.dll deleted file mode 100644 index a9803c1e..00000000 Binary files a/tools/bfutil/src/libusb-1.0/libusb-1.0.dll and /dev/null differ diff --git a/tools/bfutil/src/libusb-1.0/libusb-1.0.dll.a b/tools/bfutil/src/libusb-1.0/libusb-1.0.dll.a deleted file mode 100644 index fc6c5ebc..00000000 Binary files a/tools/bfutil/src/libusb-1.0/libusb-1.0.dll.a and /dev/null differ diff --git a/tools/bfutil/src/libusb-1.0/libusb.h b/tools/bfutil/src/libusb-1.0/libusb.h deleted file mode 100644 index f73e31cc..00000000 --- a/tools/bfutil/src/libusb-1.0/libusb.h +++ /dev/null @@ -1,2008 +0,0 @@ -/* - * Public libusb header file - * Copyright © 2001 Johannes Erdfelt - * Copyright © 2007-2008 Daniel Drake - * Copyright © 2012 Pete Batard - * Copyright © 2012 Nathan Hjelm - * For more information, please visit: http://libusb.info - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. - * - * This library 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef LIBUSB_H -#define LIBUSB_H - -#ifdef _MSC_VER -/* on MS environments, the inline keyword is available in C++ only */ -#if !defined(__cplusplus) -#define inline __inline -#endif -/* ssize_t is also not available (copy/paste from MinGW) */ -#ifndef _SSIZE_T_DEFINED -#define _SSIZE_T_DEFINED -#undef ssize_t -#ifdef _WIN64 - typedef __int64 ssize_t; -#else - typedef int ssize_t; -#endif /* _WIN64 */ -#endif /* _SSIZE_T_DEFINED */ -#endif /* _MSC_VER */ - -/* stdint.h is not available on older MSVC */ -#if defined(_MSC_VER) && (_MSC_VER < 1600) && (!defined(_STDINT)) && (!defined(_STDINT_H)) -typedef unsigned __int8 uint8_t; -typedef unsigned __int16 uint16_t; -typedef unsigned __int32 uint32_t; -#else -#include -#endif - -#if !defined(_WIN32_WCE) -#include -#endif - -#if defined(__linux) || defined(__APPLE__) || defined(__CYGWIN__) || defined(__HAIKU__) -#include -#endif - -#include -#include - -/* 'interface' might be defined as a macro on Windows, so we need to - * undefine it so as not to break the current libusb API, because - * libusb_config_descriptor has an 'interface' member - * As this can be problematic if you include windows.h after libusb.h - * in your sources, we force windows.h to be included first. */ -#if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) -#include -#if defined(interface) -#undef interface -#endif -#if !defined(__CYGWIN__) -#include -#endif -#endif - -#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) -#define LIBUSB_DEPRECATED_FOR(f) \ - __attribute__((deprecated("Use " #f " instead"))) -#else -#define LIBUSB_DEPRECATED_FOR(f) -#endif /* __GNUC__ */ - -/** \def LIBUSB_CALL - * \ingroup libusb_misc - * libusb's Windows calling convention. - * - * Under Windows, the selection of available compilers and configurations - * means that, unlike other platforms, there is not one true calling - * convention (calling convention: the manner in which parameters are - * passed to functions in the generated assembly code). - * - * Matching the Windows API itself, libusb uses the WINAPI convention (which - * translates to the stdcall convention) and guarantees that the - * library is compiled in this way. The public header file also includes - * appropriate annotations so that your own software will use the right - * convention, even if another convention is being used by default within - * your codebase. - * - * The one consideration that you must apply in your software is to mark - * all functions which you use as libusb callbacks with this LIBUSB_CALL - * annotation, so that they too get compiled for the correct calling - * convention. - * - * On non-Windows operating systems, this macro is defined as nothing. This - * means that you can apply it to your code without worrying about - * cross-platform compatibility. - */ -/* LIBUSB_CALL must be defined on both definition and declaration of libusb - * functions. You'd think that declaration would be enough, but cygwin will - * complain about conflicting types unless both are marked this way. - * The placement of this macro is important too; it must appear after the - * return type, before the function name. See internal documentation for - * API_EXPORTED. - */ -#if defined(_WIN32) || defined(__CYGWIN__) || defined(_WIN32_WCE) -#define LIBUSB_CALL WINAPI -#else -#define LIBUSB_CALL -#endif - -/** \def LIBUSB_API_VERSION - * \ingroup libusb_misc - * libusb's API version. - * - * Since version 1.0.13, to help with feature detection, libusb defines - * a LIBUSB_API_VERSION macro that gets increased every time there is a - * significant change to the API, such as the introduction of a new call, - * the definition of a new macro/enum member, or any other element that - * libusb applications may want to detect at compilation time. - * - * The macro is typically used in an application as follows: - * \code - * #if defined(LIBUSB_API_VERSION) && (LIBUSB_API_VERSION >= 0x01001234) - * // Use one of the newer features from the libusb API - * #endif - * \endcode - * - * Internally, LIBUSB_API_VERSION is defined as follows: - * (libusb major << 24) | (libusb minor << 16) | (16 bit incremental) - */ -#define LIBUSB_API_VERSION 0x01000105 - -/* The following is kept for compatibility, but will be deprecated in the future */ -#define LIBUSBX_API_VERSION LIBUSB_API_VERSION - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * \ingroup libusb_misc - * Convert a 16-bit value from host-endian to little-endian format. On - * little endian systems, this function does nothing. On big endian systems, - * the bytes are swapped. - * \param x the host-endian value to convert - * \returns the value in little-endian byte order - */ -static inline uint16_t libusb_cpu_to_le16(const uint16_t x) -{ - union { - uint8_t b8[2]; - uint16_t b16; - } _tmp; - _tmp.b8[1] = (uint8_t) (x >> 8); - _tmp.b8[0] = (uint8_t) (x & 0xff); - return _tmp.b16; -} - -/** \def libusb_le16_to_cpu - * \ingroup libusb_misc - * Convert a 16-bit value from little-endian to host-endian format. On - * little endian systems, this function does nothing. On big endian systems, - * the bytes are swapped. - * \param x the little-endian value to convert - * \returns the value in host-endian byte order - */ -#define libusb_le16_to_cpu libusb_cpu_to_le16 - -/* standard USB stuff */ - -/** \ingroup libusb_desc - * Device and/or Interface Class codes */ -enum libusb_class_code { - /** In the context of a \ref libusb_device_descriptor "device descriptor", - * this bDeviceClass value indicates that each interface specifies its - * own class information and all interfaces operate independently. - */ - LIBUSB_CLASS_PER_INTERFACE = 0, - - /** Audio class */ - LIBUSB_CLASS_AUDIO = 1, - - /** Communications class */ - LIBUSB_CLASS_COMM = 2, - - /** Human Interface Device class */ - LIBUSB_CLASS_HID = 3, - - /** Physical */ - LIBUSB_CLASS_PHYSICAL = 5, - - /** Printer class */ - LIBUSB_CLASS_PRINTER = 7, - - /** Image class */ - LIBUSB_CLASS_PTP = 6, /* legacy name from libusb-0.1 usb.h */ - LIBUSB_CLASS_IMAGE = 6, - - /** Mass storage class */ - LIBUSB_CLASS_MASS_STORAGE = 8, - - /** Hub class */ - LIBUSB_CLASS_HUB = 9, - - /** Data class */ - LIBUSB_CLASS_DATA = 10, - - /** Smart Card */ - LIBUSB_CLASS_SMART_CARD = 0x0b, - - /** Content Security */ - LIBUSB_CLASS_CONTENT_SECURITY = 0x0d, - - /** Video */ - LIBUSB_CLASS_VIDEO = 0x0e, - - /** Personal Healthcare */ - LIBUSB_CLASS_PERSONAL_HEALTHCARE = 0x0f, - - /** Diagnostic Device */ - LIBUSB_CLASS_DIAGNOSTIC_DEVICE = 0xdc, - - /** Wireless class */ - LIBUSB_CLASS_WIRELESS = 0xe0, - - /** Application class */ - LIBUSB_CLASS_APPLICATION = 0xfe, - - /** Class is vendor-specific */ - LIBUSB_CLASS_VENDOR_SPEC = 0xff -}; - -/** \ingroup libusb_desc - * Descriptor types as defined by the USB specification. */ -enum libusb_descriptor_type { - /** Device descriptor. See libusb_device_descriptor. */ - LIBUSB_DT_DEVICE = 0x01, - - /** Configuration descriptor. See libusb_config_descriptor. */ - LIBUSB_DT_CONFIG = 0x02, - - /** String descriptor */ - LIBUSB_DT_STRING = 0x03, - - /** Interface descriptor. See libusb_interface_descriptor. */ - LIBUSB_DT_INTERFACE = 0x04, - - /** Endpoint descriptor. See libusb_endpoint_descriptor. */ - LIBUSB_DT_ENDPOINT = 0x05, - - /** BOS descriptor */ - LIBUSB_DT_BOS = 0x0f, - - /** Device Capability descriptor */ - LIBUSB_DT_DEVICE_CAPABILITY = 0x10, - - /** HID descriptor */ - LIBUSB_DT_HID = 0x21, - - /** HID report descriptor */ - LIBUSB_DT_REPORT = 0x22, - - /** Physical descriptor */ - LIBUSB_DT_PHYSICAL = 0x23, - - /** Hub descriptor */ - LIBUSB_DT_HUB = 0x29, - - /** SuperSpeed Hub descriptor */ - LIBUSB_DT_SUPERSPEED_HUB = 0x2a, - - /** SuperSpeed Endpoint Companion descriptor */ - LIBUSB_DT_SS_ENDPOINT_COMPANION = 0x30 -}; - -/* Descriptor sizes per descriptor type */ -#define LIBUSB_DT_DEVICE_SIZE 18 -#define LIBUSB_DT_CONFIG_SIZE 9 -#define LIBUSB_DT_INTERFACE_SIZE 9 -#define LIBUSB_DT_ENDPOINT_SIZE 7 -#define LIBUSB_DT_ENDPOINT_AUDIO_SIZE 9 /* Audio extension */ -#define LIBUSB_DT_HUB_NONVAR_SIZE 7 -#define LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE 6 -#define LIBUSB_DT_BOS_SIZE 5 -#define LIBUSB_DT_DEVICE_CAPABILITY_SIZE 3 - -/* BOS descriptor sizes */ -#define LIBUSB_BT_USB_2_0_EXTENSION_SIZE 7 -#define LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE 10 -#define LIBUSB_BT_CONTAINER_ID_SIZE 20 - -/* We unwrap the BOS => define its max size */ -#define LIBUSB_DT_BOS_MAX_SIZE ((LIBUSB_DT_BOS_SIZE) +\ - (LIBUSB_BT_USB_2_0_EXTENSION_SIZE) +\ - (LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) +\ - (LIBUSB_BT_CONTAINER_ID_SIZE)) - -#define LIBUSB_ENDPOINT_ADDRESS_MASK 0x0f /* in bEndpointAddress */ -#define LIBUSB_ENDPOINT_DIR_MASK 0x80 - -/** \ingroup libusb_desc - * Endpoint direction. Values for bit 7 of the - * \ref libusb_endpoint_descriptor::bEndpointAddress "endpoint address" scheme. - */ -enum libusb_endpoint_direction { - /** In: device-to-host */ - LIBUSB_ENDPOINT_IN = 0x80, - - /** Out: host-to-device */ - LIBUSB_ENDPOINT_OUT = 0x00 -}; - -#define LIBUSB_TRANSFER_TYPE_MASK 0x03 /* in bmAttributes */ - -/** \ingroup libusb_desc - * Endpoint transfer type. Values for bits 0:1 of the - * \ref libusb_endpoint_descriptor::bmAttributes "endpoint attributes" field. - */ -enum libusb_transfer_type { - /** Control endpoint */ - LIBUSB_TRANSFER_TYPE_CONTROL = 0, - - /** Isochronous endpoint */ - LIBUSB_TRANSFER_TYPE_ISOCHRONOUS = 1, - - /** Bulk endpoint */ - LIBUSB_TRANSFER_TYPE_BULK = 2, - - /** Interrupt endpoint */ - LIBUSB_TRANSFER_TYPE_INTERRUPT = 3, - - /** Stream endpoint */ - LIBUSB_TRANSFER_TYPE_BULK_STREAM = 4, -}; - -/** \ingroup libusb_misc - * Standard requests, as defined in table 9-5 of the USB 3.0 specifications */ -enum libusb_standard_request { - /** Request status of the specific recipient */ - LIBUSB_REQUEST_GET_STATUS = 0x00, - - /** Clear or disable a specific feature */ - LIBUSB_REQUEST_CLEAR_FEATURE = 0x01, - - /* 0x02 is reserved */ - - /** Set or enable a specific feature */ - LIBUSB_REQUEST_SET_FEATURE = 0x03, - - /* 0x04 is reserved */ - - /** Set device address for all future accesses */ - LIBUSB_REQUEST_SET_ADDRESS = 0x05, - - /** Get the specified descriptor */ - LIBUSB_REQUEST_GET_DESCRIPTOR = 0x06, - - /** Used to update existing descriptors or add new descriptors */ - LIBUSB_REQUEST_SET_DESCRIPTOR = 0x07, - - /** Get the current device configuration value */ - LIBUSB_REQUEST_GET_CONFIGURATION = 0x08, - - /** Set device configuration */ - LIBUSB_REQUEST_SET_CONFIGURATION = 0x09, - - /** Return the selected alternate setting for the specified interface */ - LIBUSB_REQUEST_GET_INTERFACE = 0x0A, - - /** Select an alternate interface for the specified interface */ - LIBUSB_REQUEST_SET_INTERFACE = 0x0B, - - /** Set then report an endpoint's synchronization frame */ - LIBUSB_REQUEST_SYNCH_FRAME = 0x0C, - - /** Sets both the U1 and U2 Exit Latency */ - LIBUSB_REQUEST_SET_SEL = 0x30, - - /** Delay from the time a host transmits a packet to the time it is - * received by the device. */ - LIBUSB_SET_ISOCH_DELAY = 0x31, -}; - -/** \ingroup libusb_misc - * Request type bits of the - * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control - * transfers. */ -enum libusb_request_type { - /** Standard */ - LIBUSB_REQUEST_TYPE_STANDARD = (0x00 << 5), - - /** Class */ - LIBUSB_REQUEST_TYPE_CLASS = (0x01 << 5), - - /** Vendor */ - LIBUSB_REQUEST_TYPE_VENDOR = (0x02 << 5), - - /** Reserved */ - LIBUSB_REQUEST_TYPE_RESERVED = (0x03 << 5) -}; - -/** \ingroup libusb_misc - * Recipient bits of the - * \ref libusb_control_setup::bmRequestType "bmRequestType" field in control - * transfers. Values 4 through 31 are reserved. */ -enum libusb_request_recipient { - /** Device */ - LIBUSB_RECIPIENT_DEVICE = 0x00, - - /** Interface */ - LIBUSB_RECIPIENT_INTERFACE = 0x01, - - /** Endpoint */ - LIBUSB_RECIPIENT_ENDPOINT = 0x02, - - /** Other */ - LIBUSB_RECIPIENT_OTHER = 0x03, -}; - -#define LIBUSB_ISO_SYNC_TYPE_MASK 0x0C - -/** \ingroup libusb_desc - * Synchronization type for isochronous endpoints. Values for bits 2:3 of the - * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in - * libusb_endpoint_descriptor. - */ -enum libusb_iso_sync_type { - /** No synchronization */ - LIBUSB_ISO_SYNC_TYPE_NONE = 0, - - /** Asynchronous */ - LIBUSB_ISO_SYNC_TYPE_ASYNC = 1, - - /** Adaptive */ - LIBUSB_ISO_SYNC_TYPE_ADAPTIVE = 2, - - /** Synchronous */ - LIBUSB_ISO_SYNC_TYPE_SYNC = 3 -}; - -#define LIBUSB_ISO_USAGE_TYPE_MASK 0x30 - -/** \ingroup libusb_desc - * Usage type for isochronous endpoints. Values for bits 4:5 of the - * \ref libusb_endpoint_descriptor::bmAttributes "bmAttributes" field in - * libusb_endpoint_descriptor. - */ -enum libusb_iso_usage_type { - /** Data endpoint */ - LIBUSB_ISO_USAGE_TYPE_DATA = 0, - - /** Feedback endpoint */ - LIBUSB_ISO_USAGE_TYPE_FEEDBACK = 1, - - /** Implicit feedback Data endpoint */ - LIBUSB_ISO_USAGE_TYPE_IMPLICIT = 2, -}; - -/** \ingroup libusb_desc - * A structure representing the standard USB device descriptor. This - * descriptor is documented in section 9.6.1 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_device_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE LIBUSB_DT_DEVICE in this - * context. */ - uint8_t bDescriptorType; - - /** USB specification release number in binary-coded decimal. A value of - * 0x0200 indicates USB 2.0, 0x0110 indicates USB 1.1, etc. */ - uint16_t bcdUSB; - - /** USB-IF class code for the device. See \ref libusb_class_code. */ - uint8_t bDeviceClass; - - /** USB-IF subclass code for the device, qualified by the bDeviceClass - * value */ - uint8_t bDeviceSubClass; - - /** USB-IF protocol code for the device, qualified by the bDeviceClass and - * bDeviceSubClass values */ - uint8_t bDeviceProtocol; - - /** Maximum packet size for endpoint 0 */ - uint8_t bMaxPacketSize0; - - /** USB-IF vendor ID */ - uint16_t idVendor; - - /** USB-IF product ID */ - uint16_t idProduct; - - /** Device release number in binary-coded decimal */ - uint16_t bcdDevice; - - /** Index of string descriptor describing manufacturer */ - uint8_t iManufacturer; - - /** Index of string descriptor describing product */ - uint8_t iProduct; - - /** Index of string descriptor containing device serial number */ - uint8_t iSerialNumber; - - /** Number of possible configurations */ - uint8_t bNumConfigurations; -}; - -/** \ingroup libusb_desc - * A structure representing the standard USB endpoint descriptor. This - * descriptor is documented in section 9.6.6 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_endpoint_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_ENDPOINT LIBUSB_DT_ENDPOINT in - * this context. */ - uint8_t bDescriptorType; - - /** The address of the endpoint described by this descriptor. Bits 0:3 are - * the endpoint number. Bits 4:6 are reserved. Bit 7 indicates direction, - * see \ref libusb_endpoint_direction. - */ - uint8_t bEndpointAddress; - - /** Attributes which apply to the endpoint when it is configured using - * the bConfigurationValue. Bits 0:1 determine the transfer type and - * correspond to \ref libusb_transfer_type. Bits 2:3 are only used for - * isochronous endpoints and correspond to \ref libusb_iso_sync_type. - * Bits 4:5 are also only used for isochronous endpoints and correspond to - * \ref libusb_iso_usage_type. Bits 6:7 are reserved. - */ - uint8_t bmAttributes; - - /** Maximum packet size this endpoint is capable of sending/receiving. */ - uint16_t wMaxPacketSize; - - /** Interval for polling endpoint for data transfers. */ - uint8_t bInterval; - - /** For audio devices only: the rate at which synchronization feedback - * is provided. */ - uint8_t bRefresh; - - /** For audio devices only: the address if the synch endpoint */ - uint8_t bSynchAddress; - - /** Extra descriptors. If libusb encounters unknown endpoint descriptors, - * it will store them here, should you wish to parse them. */ - const unsigned char *extra; - - /** Length of the extra descriptors, in bytes. */ - int extra_length; -}; - -/** \ingroup libusb_desc - * A structure representing the standard USB interface descriptor. This - * descriptor is documented in section 9.6.5 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_interface_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_INTERFACE LIBUSB_DT_INTERFACE - * in this context. */ - uint8_t bDescriptorType; - - /** Number of this interface */ - uint8_t bInterfaceNumber; - - /** Value used to select this alternate setting for this interface */ - uint8_t bAlternateSetting; - - /** Number of endpoints used by this interface (excluding the control - * endpoint). */ - uint8_t bNumEndpoints; - - /** USB-IF class code for this interface. See \ref libusb_class_code. */ - uint8_t bInterfaceClass; - - /** USB-IF subclass code for this interface, qualified by the - * bInterfaceClass value */ - uint8_t bInterfaceSubClass; - - /** USB-IF protocol code for this interface, qualified by the - * bInterfaceClass and bInterfaceSubClass values */ - uint8_t bInterfaceProtocol; - - /** Index of string descriptor describing this interface */ - uint8_t iInterface; - - /** Array of endpoint descriptors. This length of this array is determined - * by the bNumEndpoints field. */ - const struct libusb_endpoint_descriptor *endpoint; - - /** Extra descriptors. If libusb encounters unknown interface descriptors, - * it will store them here, should you wish to parse them. */ - const unsigned char *extra; - - /** Length of the extra descriptors, in bytes. */ - int extra_length; -}; - -/** \ingroup libusb_desc - * A collection of alternate settings for a particular USB interface. - */ -struct libusb_interface { - /** Array of interface descriptors. The length of this array is determined - * by the num_altsetting field. */ - const struct libusb_interface_descriptor *altsetting; - - /** The number of alternate settings that belong to this interface */ - int num_altsetting; -}; - -/** \ingroup libusb_desc - * A structure representing the standard USB configuration descriptor. This - * descriptor is documented in section 9.6.3 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_config_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_CONFIG LIBUSB_DT_CONFIG - * in this context. */ - uint8_t bDescriptorType; - - /** Total length of data returned for this configuration */ - uint16_t wTotalLength; - - /** Number of interfaces supported by this configuration */ - uint8_t bNumInterfaces; - - /** Identifier value for this configuration */ - uint8_t bConfigurationValue; - - /** Index of string descriptor describing this configuration */ - uint8_t iConfiguration; - - /** Configuration characteristics */ - uint8_t bmAttributes; - - /** Maximum power consumption of the USB device from this bus in this - * configuration when the device is fully operation. Expressed in units - * of 2 mA when the device is operating in high-speed mode and in units - * of 8 mA when the device is operating in super-speed mode. */ - uint8_t MaxPower; - - /** Array of interfaces supported by this configuration. The length of - * this array is determined by the bNumInterfaces field. */ - const struct libusb_interface *interface; - - /** Extra descriptors. If libusb encounters unknown configuration - * descriptors, it will store them here, should you wish to parse them. */ - const unsigned char *extra; - - /** Length of the extra descriptors, in bytes. */ - int extra_length; -}; - -/** \ingroup libusb_desc - * A structure representing the superspeed endpoint companion - * descriptor. This descriptor is documented in section 9.6.7 of - * the USB 3.0 specification. All multiple-byte fields are represented in - * host-endian format. - */ -struct libusb_ss_endpoint_companion_descriptor { - - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_SS_ENDPOINT_COMPANION in - * this context. */ - uint8_t bDescriptorType; - - - /** The maximum number of packets the endpoint can send or - * receive as part of a burst. */ - uint8_t bMaxBurst; - - /** In bulk EP: bits 4:0 represents the maximum number of - * streams the EP supports. In isochronous EP: bits 1:0 - * represents the Mult - a zero based value that determines - * the maximum number of packets within a service interval */ - uint8_t bmAttributes; - - /** The total number of bytes this EP will transfer every - * service interval. valid only for periodic EPs. */ - uint16_t wBytesPerInterval; -}; - -/** \ingroup libusb_desc - * A generic representation of a BOS Device Capability descriptor. It is - * advised to check bDevCapabilityType and call the matching - * libusb_get_*_descriptor function to get a structure fully matching the type. - */ -struct libusb_bos_dev_capability_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY - * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ - uint8_t bDescriptorType; - /** Device Capability type */ - uint8_t bDevCapabilityType; - /** Device Capability data (bLength - 3 bytes) */ - uint8_t dev_capability_data -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) - [] /* valid C99 code */ -#else - [0] /* non-standard, but usually working code */ -#endif - ; -}; - -/** \ingroup libusb_desc - * A structure representing the Binary Device Object Store (BOS) descriptor. - * This descriptor is documented in section 9.6.2 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_bos_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_BOS LIBUSB_DT_BOS - * in this context. */ - uint8_t bDescriptorType; - - /** Length of this descriptor and all of its sub descriptors */ - uint16_t wTotalLength; - - /** The number of separate device capability descriptors in - * the BOS */ - uint8_t bNumDeviceCaps; - - /** bNumDeviceCap Device Capability Descriptors */ - struct libusb_bos_dev_capability_descriptor *dev_capability -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) - [] /* valid C99 code */ -#else - [0] /* non-standard, but usually working code */ -#endif - ; -}; - -/** \ingroup libusb_desc - * A structure representing the USB 2.0 Extension descriptor - * This descriptor is documented in section 9.6.2.1 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_usb_2_0_extension_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY - * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ - uint8_t bDescriptorType; - - /** Capability type. Will have value - * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION - * LIBUSB_BT_USB_2_0_EXTENSION in this context. */ - uint8_t bDevCapabilityType; - - /** Bitmap encoding of supported device level features. - * A value of one in a bit location indicates a feature is - * supported; a value of zero indicates it is not supported. - * See \ref libusb_usb_2_0_extension_attributes. */ - uint32_t bmAttributes; -}; - -/** \ingroup libusb_desc - * A structure representing the SuperSpeed USB Device Capability descriptor - * This descriptor is documented in section 9.6.2.2 of the USB 3.0 specification. - * All multiple-byte fields are represented in host-endian format. - */ -struct libusb_ss_usb_device_capability_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY - * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ - uint8_t bDescriptorType; - - /** Capability type. Will have value - * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY - * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY in this context. */ - uint8_t bDevCapabilityType; - - /** Bitmap encoding of supported device level features. - * A value of one in a bit location indicates a feature is - * supported; a value of zero indicates it is not supported. - * See \ref libusb_ss_usb_device_capability_attributes. */ - uint8_t bmAttributes; - - /** Bitmap encoding of the speed supported by this device when - * operating in SuperSpeed mode. See \ref libusb_supported_speed. */ - uint16_t wSpeedSupported; - - /** The lowest speed at which all the functionality supported - * by the device is available to the user. For example if the - * device supports all its functionality when connected at - * full speed and above then it sets this value to 1. */ - uint8_t bFunctionalitySupport; - - /** U1 Device Exit Latency. */ - uint8_t bU1DevExitLat; - - /** U2 Device Exit Latency. */ - uint16_t bU2DevExitLat; -}; - -/** \ingroup libusb_desc - * A structure representing the Container ID descriptor. - * This descriptor is documented in section 9.6.2.3 of the USB 3.0 specification. - * All multiple-byte fields, except UUIDs, are represented in host-endian format. - */ -struct libusb_container_id_descriptor { - /** Size of this descriptor (in bytes) */ - uint8_t bLength; - - /** Descriptor type. Will have value - * \ref libusb_descriptor_type::LIBUSB_DT_DEVICE_CAPABILITY - * LIBUSB_DT_DEVICE_CAPABILITY in this context. */ - uint8_t bDescriptorType; - - /** Capability type. Will have value - * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID - * LIBUSB_BT_CONTAINER_ID in this context. */ - uint8_t bDevCapabilityType; - - /** Reserved field */ - uint8_t bReserved; - - /** 128 bit UUID */ - uint8_t ContainerID[16]; -}; - -/** \ingroup libusb_asyncio - * Setup packet for control transfers. */ -struct libusb_control_setup { - /** Request type. Bits 0:4 determine recipient, see - * \ref libusb_request_recipient. Bits 5:6 determine type, see - * \ref libusb_request_type. Bit 7 determines data transfer direction, see - * \ref libusb_endpoint_direction. - */ - uint8_t bmRequestType; - - /** Request. If the type bits of bmRequestType are equal to - * \ref libusb_request_type::LIBUSB_REQUEST_TYPE_STANDARD - * "LIBUSB_REQUEST_TYPE_STANDARD" then this field refers to - * \ref libusb_standard_request. For other cases, use of this field is - * application-specific. */ - uint8_t bRequest; - - /** Value. Varies according to request */ - uint16_t wValue; - - /** Index. Varies according to request, typically used to pass an index - * or offset */ - uint16_t wIndex; - - /** Number of bytes to transfer */ - uint16_t wLength; -}; - -#define LIBUSB_CONTROL_SETUP_SIZE (sizeof(struct libusb_control_setup)) - -/* libusb */ - -struct libusb_context; -struct libusb_device; -struct libusb_device_handle; - -/** \ingroup libusb_lib - * Structure providing the version of the libusb runtime - */ -struct libusb_version { - /** Library major version. */ - const uint16_t major; - - /** Library minor version. */ - const uint16_t minor; - - /** Library micro version. */ - const uint16_t micro; - - /** Library nano version. */ - const uint16_t nano; - - /** Library release candidate suffix string, e.g. "-rc4". */ - const char *rc; - - /** For ABI compatibility only. */ - const char* describe; -}; - -/** \ingroup libusb_lib - * Structure representing a libusb session. The concept of individual libusb - * sessions allows for your program to use two libraries (or dynamically - * load two modules) which both independently use libusb. This will prevent - * interference between the individual libusb users - for example - * libusb_set_debug() will not affect the other user of the library, and - * libusb_exit() will not destroy resources that the other user is still - * using. - * - * Sessions are created by libusb_init() and destroyed through libusb_exit(). - * If your application is guaranteed to only ever include a single libusb - * user (i.e. you), you do not have to worry about contexts: pass NULL in - * every function call where a context is required. The default context - * will be used. - * - * For more information, see \ref libusb_contexts. - */ -typedef struct libusb_context libusb_context; - -/** \ingroup libusb_dev - * Structure representing a USB device detected on the system. This is an - * opaque type for which you are only ever provided with a pointer, usually - * originating from libusb_get_device_list(). - * - * Certain operations can be performed on a device, but in order to do any - * I/O you will have to first obtain a device handle using libusb_open(). - * - * Devices are reference counted with libusb_ref_device() and - * libusb_unref_device(), and are freed when the reference count reaches 0. - * New devices presented by libusb_get_device_list() have a reference count of - * 1, and libusb_free_device_list() can optionally decrease the reference count - * on all devices in the list. libusb_open() adds another reference which is - * later destroyed by libusb_close(). - */ -typedef struct libusb_device libusb_device; - - -/** \ingroup libusb_dev - * Structure representing a handle on a USB device. This is an opaque type for - * which you are only ever provided with a pointer, usually originating from - * libusb_open(). - * - * A device handle is used to perform I/O and other operations. When finished - * with a device handle, you should call libusb_close(). - */ -typedef struct libusb_device_handle libusb_device_handle; - -/** \ingroup libusb_dev - * Speed codes. Indicates the speed at which the device is operating. - */ -enum libusb_speed { - /** The OS doesn't report or know the device speed. */ - LIBUSB_SPEED_UNKNOWN = 0, - - /** The device is operating at low speed (1.5MBit/s). */ - LIBUSB_SPEED_LOW = 1, - - /** The device is operating at full speed (12MBit/s). */ - LIBUSB_SPEED_FULL = 2, - - /** The device is operating at high speed (480MBit/s). */ - LIBUSB_SPEED_HIGH = 3, - - /** The device is operating at super speed (5000MBit/s). */ - LIBUSB_SPEED_SUPER = 4, -}; - -/** \ingroup libusb_dev - * Supported speeds (wSpeedSupported) bitfield. Indicates what - * speeds the device supports. - */ -enum libusb_supported_speed { - /** Low speed operation supported (1.5MBit/s). */ - LIBUSB_LOW_SPEED_OPERATION = 1, - - /** Full speed operation supported (12MBit/s). */ - LIBUSB_FULL_SPEED_OPERATION = 2, - - /** High speed operation supported (480MBit/s). */ - LIBUSB_HIGH_SPEED_OPERATION = 4, - - /** Superspeed operation supported (5000MBit/s). */ - LIBUSB_SUPER_SPEED_OPERATION = 8, -}; - -/** \ingroup libusb_dev - * Masks for the bits of the - * \ref libusb_usb_2_0_extension_descriptor::bmAttributes "bmAttributes" field - * of the USB 2.0 Extension descriptor. - */ -enum libusb_usb_2_0_extension_attributes { - /** Supports Link Power Management (LPM) */ - LIBUSB_BM_LPM_SUPPORT = 2, -}; - -/** \ingroup libusb_dev - * Masks for the bits of the - * \ref libusb_ss_usb_device_capability_descriptor::bmAttributes "bmAttributes" field - * field of the SuperSpeed USB Device Capability descriptor. - */ -enum libusb_ss_usb_device_capability_attributes { - /** Supports Latency Tolerance Messages (LTM) */ - LIBUSB_BM_LTM_SUPPORT = 2, -}; - -/** \ingroup libusb_dev - * USB capability types - */ -enum libusb_bos_type { - /** Wireless USB device capability */ - LIBUSB_BT_WIRELESS_USB_DEVICE_CAPABILITY = 1, - - /** USB 2.0 extensions */ - LIBUSB_BT_USB_2_0_EXTENSION = 2, - - /** SuperSpeed USB device capability */ - LIBUSB_BT_SS_USB_DEVICE_CAPABILITY = 3, - - /** Container ID type */ - LIBUSB_BT_CONTAINER_ID = 4, -}; - -/** \ingroup libusb_misc - * Error codes. Most libusb functions return 0 on success or one of these - * codes on failure. - * You can call libusb_error_name() to retrieve a string representation of an - * error code or libusb_strerror() to get an end-user suitable description of - * an error code. - */ -enum libusb_error { - /** Success (no error) */ - LIBUSB_SUCCESS = 0, - - /** Input/output error */ - LIBUSB_ERROR_IO = -1, - - /** Invalid parameter */ - LIBUSB_ERROR_INVALID_PARAM = -2, - - /** Access denied (insufficient permissions) */ - LIBUSB_ERROR_ACCESS = -3, - - /** No such device (it may have been disconnected) */ - LIBUSB_ERROR_NO_DEVICE = -4, - - /** Entity not found */ - LIBUSB_ERROR_NOT_FOUND = -5, - - /** Resource busy */ - LIBUSB_ERROR_BUSY = -6, - - /** Operation timed out */ - LIBUSB_ERROR_TIMEOUT = -7, - - /** Overflow */ - LIBUSB_ERROR_OVERFLOW = -8, - - /** Pipe error */ - LIBUSB_ERROR_PIPE = -9, - - /** System call interrupted (perhaps due to signal) */ - LIBUSB_ERROR_INTERRUPTED = -10, - - /** Insufficient memory */ - LIBUSB_ERROR_NO_MEM = -11, - - /** Operation not supported or unimplemented on this platform */ - LIBUSB_ERROR_NOT_SUPPORTED = -12, - - /* NB: Remember to update LIBUSB_ERROR_COUNT below as well as the - message strings in strerror.c when adding new error codes here. */ - - /** Other error */ - LIBUSB_ERROR_OTHER = -99, -}; - -/* Total number of error codes in enum libusb_error */ -#define LIBUSB_ERROR_COUNT 14 - -/** \ingroup libusb_asyncio - * Transfer status codes */ -enum libusb_transfer_status { - /** Transfer completed without error. Note that this does not indicate - * that the entire amount of requested data was transferred. */ - LIBUSB_TRANSFER_COMPLETED, - - /** Transfer failed */ - LIBUSB_TRANSFER_ERROR, - - /** Transfer timed out */ - LIBUSB_TRANSFER_TIMED_OUT, - - /** Transfer was cancelled */ - LIBUSB_TRANSFER_CANCELLED, - - /** For bulk/interrupt endpoints: halt condition detected (endpoint - * stalled). For control endpoints: control request not supported. */ - LIBUSB_TRANSFER_STALL, - - /** Device was disconnected */ - LIBUSB_TRANSFER_NO_DEVICE, - - /** Device sent more data than requested */ - LIBUSB_TRANSFER_OVERFLOW, - - /* NB! Remember to update libusb_error_name() - when adding new status codes here. */ -}; - -/** \ingroup libusb_asyncio - * libusb_transfer.flags values */ -enum libusb_transfer_flags { - /** Report short frames as errors */ - LIBUSB_TRANSFER_SHORT_NOT_OK = 1<<0, - - /** Automatically free() transfer buffer during libusb_free_transfer(). - * Note that buffers allocated with libusb_dev_mem_alloc() should not - * be attempted freed in this way, since free() is not an appropriate - * way to release such memory. */ - LIBUSB_TRANSFER_FREE_BUFFER = 1<<1, - - /** Automatically call libusb_free_transfer() after callback returns. - * If this flag is set, it is illegal to call libusb_free_transfer() - * from your transfer callback, as this will result in a double-free - * when this flag is acted upon. */ - LIBUSB_TRANSFER_FREE_TRANSFER = 1<<2, - - /** Terminate transfers that are a multiple of the endpoint's - * wMaxPacketSize with an extra zero length packet. This is useful - * when a device protocol mandates that each logical request is - * terminated by an incomplete packet (i.e. the logical requests are - * not separated by other means). - * - * This flag only affects host-to-device transfers to bulk and interrupt - * endpoints. In other situations, it is ignored. - * - * This flag only affects transfers with a length that is a multiple of - * the endpoint's wMaxPacketSize. On transfers of other lengths, this - * flag has no effect. Therefore, if you are working with a device that - * needs a ZLP whenever the end of the logical request falls on a packet - * boundary, then it is sensible to set this flag on every - * transfer (you do not have to worry about only setting it on transfers - * that end on the boundary). - * - * This flag is currently only supported on Linux. - * On other systems, libusb_submit_transfer() will return - * LIBUSB_ERROR_NOT_SUPPORTED for every transfer where this flag is set. - * - * Available since libusb-1.0.9. - */ - LIBUSB_TRANSFER_ADD_ZERO_PACKET = 1 << 3, -}; - -/** \ingroup libusb_asyncio - * Isochronous packet descriptor. */ -struct libusb_iso_packet_descriptor { - /** Length of data to request in this packet */ - unsigned int length; - - /** Amount of data that was actually transferred */ - unsigned int actual_length; - - /** Status code for this packet */ - enum libusb_transfer_status status; -}; - -struct libusb_transfer; - -/** \ingroup libusb_asyncio - * Asynchronous transfer callback function type. When submitting asynchronous - * transfers, you pass a pointer to a callback function of this type via the - * \ref libusb_transfer::callback "callback" member of the libusb_transfer - * structure. libusb will call this function later, when the transfer has - * completed or failed. See \ref libusb_asyncio for more information. - * \param transfer The libusb_transfer struct the callback function is being - * notified about. - */ -typedef void (LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer); - -/** \ingroup libusb_asyncio - * The generic USB transfer structure. The user populates this structure and - * then submits it in order to request a transfer. After the transfer has - * completed, the library populates the transfer with the results and passes - * it back to the user. - */ -struct libusb_transfer { - /** Handle of the device that this transfer will be submitted to */ - libusb_device_handle *dev_handle; - - /** A bitwise OR combination of \ref libusb_transfer_flags. */ - uint8_t flags; - - /** Address of the endpoint where this transfer will be sent. */ - unsigned char endpoint; - - /** Type of the endpoint from \ref libusb_transfer_type */ - unsigned char type; - - /** Timeout for this transfer in millseconds. A value of 0 indicates no - * timeout. */ - unsigned int timeout; - - /** The status of the transfer. Read-only, and only for use within - * transfer callback function. - * - * If this is an isochronous transfer, this field may read COMPLETED even - * if there were errors in the frames. Use the - * \ref libusb_iso_packet_descriptor::status "status" field in each packet - * to determine if errors occurred. */ - enum libusb_transfer_status status; - - /** Length of the data buffer */ - int length; - - /** Actual length of data that was transferred. Read-only, and only for - * use within transfer callback function. Not valid for isochronous - * endpoint transfers. */ - int actual_length; - - /** Callback function. This will be invoked when the transfer completes, - * fails, or is cancelled. */ - libusb_transfer_cb_fn callback; - - /** User context data to pass to the callback function. */ - void *user_data; - - /** Data buffer */ - unsigned char *buffer; - - /** Number of isochronous packets. Only used for I/O with isochronous - * endpoints. */ - int num_iso_packets; - - /** Isochronous packet descriptors, for isochronous transfers only. */ - struct libusb_iso_packet_descriptor iso_packet_desc -#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) - [] /* valid C99 code */ -#else - [0] /* non-standard, but usually working code */ -#endif - ; -}; - -/** \ingroup libusb_misc - * Capabilities supported by an instance of libusb on the current running - * platform. Test if the loaded library supports a given capability by calling - * \ref libusb_has_capability(). - */ -enum libusb_capability { - /** The libusb_has_capability() API is available. */ - LIBUSB_CAP_HAS_CAPABILITY = 0x0000, - /** Hotplug support is available on this platform. */ - LIBUSB_CAP_HAS_HOTPLUG = 0x0001, - /** The library can access HID devices without requiring user intervention. - * Note that before being able to actually access an HID device, you may - * still have to call additional libusb functions such as - * \ref libusb_detach_kernel_driver(). */ - LIBUSB_CAP_HAS_HID_ACCESS = 0x0100, - /** The library supports detaching of the default USB driver, using - * \ref libusb_detach_kernel_driver(), if one is set by the OS kernel */ - LIBUSB_CAP_SUPPORTS_DETACH_KERNEL_DRIVER = 0x0101 -}; - -/** \ingroup libusb_lib - * Log message levels. - * - LIBUSB_LOG_LEVEL_NONE (0) : no messages ever printed by the library (default) - * - LIBUSB_LOG_LEVEL_ERROR (1) : error messages are printed to stderr - * - LIBUSB_LOG_LEVEL_WARNING (2) : warning and error messages are printed to stderr - * - LIBUSB_LOG_LEVEL_INFO (3) : informational messages are printed to stdout, warning - * and error messages are printed to stderr - * - LIBUSB_LOG_LEVEL_DEBUG (4) : debug and informational messages are printed to stdout, - * warnings and errors to stderr - */ -enum libusb_log_level { - LIBUSB_LOG_LEVEL_NONE = 0, - LIBUSB_LOG_LEVEL_ERROR, - LIBUSB_LOG_LEVEL_WARNING, - LIBUSB_LOG_LEVEL_INFO, - LIBUSB_LOG_LEVEL_DEBUG, -}; - -int LIBUSB_CALL libusb_init(libusb_context **ctx); -void LIBUSB_CALL libusb_exit(libusb_context *ctx); -void LIBUSB_CALL libusb_set_debug(libusb_context *ctx, int level); -const struct libusb_version * LIBUSB_CALL libusb_get_version(void); -int LIBUSB_CALL libusb_has_capability(uint32_t capability); -const char * LIBUSB_CALL libusb_error_name(int errcode); -int LIBUSB_CALL libusb_setlocale(const char *locale); -const char * LIBUSB_CALL libusb_strerror(enum libusb_error errcode); - -ssize_t LIBUSB_CALL libusb_get_device_list(libusb_context *ctx, - libusb_device ***list); -void LIBUSB_CALL libusb_free_device_list(libusb_device **list, - int unref_devices); -libusb_device * LIBUSB_CALL libusb_ref_device(libusb_device *dev); -void LIBUSB_CALL libusb_unref_device(libusb_device *dev); - -int LIBUSB_CALL libusb_get_configuration(libusb_device_handle *dev, - int *config); -int LIBUSB_CALL libusb_get_device_descriptor(libusb_device *dev, - struct libusb_device_descriptor *desc); -int LIBUSB_CALL libusb_get_active_config_descriptor(libusb_device *dev, - struct libusb_config_descriptor **config); -int LIBUSB_CALL libusb_get_config_descriptor(libusb_device *dev, - uint8_t config_index, struct libusb_config_descriptor **config); -int LIBUSB_CALL libusb_get_config_descriptor_by_value(libusb_device *dev, - uint8_t bConfigurationValue, struct libusb_config_descriptor **config); -void LIBUSB_CALL libusb_free_config_descriptor( - struct libusb_config_descriptor *config); -int LIBUSB_CALL libusb_get_ss_endpoint_companion_descriptor( - struct libusb_context *ctx, - const struct libusb_endpoint_descriptor *endpoint, - struct libusb_ss_endpoint_companion_descriptor **ep_comp); -void LIBUSB_CALL libusb_free_ss_endpoint_companion_descriptor( - struct libusb_ss_endpoint_companion_descriptor *ep_comp); -int LIBUSB_CALL libusb_get_bos_descriptor(libusb_device_handle *dev_handle, - struct libusb_bos_descriptor **bos); -void LIBUSB_CALL libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos); -int LIBUSB_CALL libusb_get_usb_2_0_extension_descriptor( - struct libusb_context *ctx, - struct libusb_bos_dev_capability_descriptor *dev_cap, - struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension); -void LIBUSB_CALL libusb_free_usb_2_0_extension_descriptor( - struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension); -int LIBUSB_CALL libusb_get_ss_usb_device_capability_descriptor( - struct libusb_context *ctx, - struct libusb_bos_dev_capability_descriptor *dev_cap, - struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap); -void LIBUSB_CALL libusb_free_ss_usb_device_capability_descriptor( - struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap); -int LIBUSB_CALL libusb_get_container_id_descriptor(struct libusb_context *ctx, - struct libusb_bos_dev_capability_descriptor *dev_cap, - struct libusb_container_id_descriptor **container_id); -void LIBUSB_CALL libusb_free_container_id_descriptor( - struct libusb_container_id_descriptor *container_id); -uint8_t LIBUSB_CALL libusb_get_bus_number(libusb_device *dev); -uint8_t LIBUSB_CALL libusb_get_port_number(libusb_device *dev); -int LIBUSB_CALL libusb_get_port_numbers(libusb_device *dev, uint8_t* port_numbers, int port_numbers_len); -LIBUSB_DEPRECATED_FOR(libusb_get_port_numbers) -int LIBUSB_CALL libusb_get_port_path(libusb_context *ctx, libusb_device *dev, uint8_t* path, uint8_t path_length); -libusb_device * LIBUSB_CALL libusb_get_parent(libusb_device *dev); -uint8_t LIBUSB_CALL libusb_get_device_address(libusb_device *dev); -int LIBUSB_CALL libusb_get_device_speed(libusb_device *dev); -int LIBUSB_CALL libusb_get_max_packet_size(libusb_device *dev, - unsigned char endpoint); -int LIBUSB_CALL libusb_get_max_iso_packet_size(libusb_device *dev, - unsigned char endpoint); - -int LIBUSB_CALL libusb_open(libusb_device *dev, libusb_device_handle **dev_handle); -void LIBUSB_CALL libusb_close(libusb_device_handle *dev_handle); -libusb_device * LIBUSB_CALL libusb_get_device(libusb_device_handle *dev_handle); - -int LIBUSB_CALL libusb_set_configuration(libusb_device_handle *dev_handle, - int configuration); -int LIBUSB_CALL libusb_claim_interface(libusb_device_handle *dev_handle, - int interface_number); -int LIBUSB_CALL libusb_release_interface(libusb_device_handle *dev_handle, - int interface_number); - -libusb_device_handle * LIBUSB_CALL libusb_open_device_with_vid_pid( - libusb_context *ctx, uint16_t vendor_id, uint16_t product_id); - -int LIBUSB_CALL libusb_set_interface_alt_setting(libusb_device_handle *dev_handle, - int interface_number, int alternate_setting); -int LIBUSB_CALL libusb_clear_halt(libusb_device_handle *dev_handle, - unsigned char endpoint); -int LIBUSB_CALL libusb_reset_device(libusb_device_handle *dev_handle); - -int LIBUSB_CALL libusb_alloc_streams(libusb_device_handle *dev_handle, - uint32_t num_streams, unsigned char *endpoints, int num_endpoints); -int LIBUSB_CALL libusb_free_streams(libusb_device_handle *dev_handle, - unsigned char *endpoints, int num_endpoints); - -unsigned char * LIBUSB_CALL libusb_dev_mem_alloc(libusb_device_handle *dev_handle, - size_t length); -int LIBUSB_CALL libusb_dev_mem_free(libusb_device_handle *dev_handle, - unsigned char *buffer, size_t length); - -int LIBUSB_CALL libusb_kernel_driver_active(libusb_device_handle *dev_handle, - int interface_number); -int LIBUSB_CALL libusb_detach_kernel_driver(libusb_device_handle *dev_handle, - int interface_number); -int LIBUSB_CALL libusb_attach_kernel_driver(libusb_device_handle *dev_handle, - int interface_number); -int LIBUSB_CALL libusb_set_auto_detach_kernel_driver( - libusb_device_handle *dev_handle, int enable); - -/* async I/O */ - -/** \ingroup libusb_asyncio - * Get the data section of a control transfer. This convenience function is here - * to remind you that the data does not start until 8 bytes into the actual - * buffer, as the setup packet comes first. - * - * Calling this function only makes sense from a transfer callback function, - * or situations where you have already allocated a suitably sized buffer at - * transfer->buffer. - * - * \param transfer a transfer - * \returns pointer to the first byte of the data section - */ -static inline unsigned char *libusb_control_transfer_get_data( - struct libusb_transfer *transfer) -{ - return transfer->buffer + LIBUSB_CONTROL_SETUP_SIZE; -} - -/** \ingroup libusb_asyncio - * Get the control setup packet of a control transfer. This convenience - * function is here to remind you that the control setup occupies the first - * 8 bytes of the transfer data buffer. - * - * Calling this function only makes sense from a transfer callback function, - * or situations where you have already allocated a suitably sized buffer at - * transfer->buffer. - * - * \param transfer a transfer - * \returns a casted pointer to the start of the transfer data buffer - */ -static inline struct libusb_control_setup *libusb_control_transfer_get_setup( - struct libusb_transfer *transfer) -{ - return (struct libusb_control_setup *)(void *) transfer->buffer; -} - -/** \ingroup libusb_asyncio - * Helper function to populate the setup packet (first 8 bytes of the data - * buffer) for a control transfer. The wIndex, wValue and wLength values should - * be given in host-endian byte order. - * - * \param buffer buffer to output the setup packet into - * This pointer must be aligned to at least 2 bytes boundary. - * \param bmRequestType see the - * \ref libusb_control_setup::bmRequestType "bmRequestType" field of - * \ref libusb_control_setup - * \param bRequest see the - * \ref libusb_control_setup::bRequest "bRequest" field of - * \ref libusb_control_setup - * \param wValue see the - * \ref libusb_control_setup::wValue "wValue" field of - * \ref libusb_control_setup - * \param wIndex see the - * \ref libusb_control_setup::wIndex "wIndex" field of - * \ref libusb_control_setup - * \param wLength see the - * \ref libusb_control_setup::wLength "wLength" field of - * \ref libusb_control_setup - */ -static inline void libusb_fill_control_setup(unsigned char *buffer, - uint8_t bmRequestType, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, - uint16_t wLength) -{ - struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer; - setup->bmRequestType = bmRequestType; - setup->bRequest = bRequest; - setup->wValue = libusb_cpu_to_le16(wValue); - setup->wIndex = libusb_cpu_to_le16(wIndex); - setup->wLength = libusb_cpu_to_le16(wLength); -} - -struct libusb_transfer * LIBUSB_CALL libusb_alloc_transfer(int iso_packets); -int LIBUSB_CALL libusb_submit_transfer(struct libusb_transfer *transfer); -int LIBUSB_CALL libusb_cancel_transfer(struct libusb_transfer *transfer); -void LIBUSB_CALL libusb_free_transfer(struct libusb_transfer *transfer); -void LIBUSB_CALL libusb_transfer_set_stream_id( - struct libusb_transfer *transfer, uint32_t stream_id); -uint32_t LIBUSB_CALL libusb_transfer_get_stream_id( - struct libusb_transfer *transfer); - -/** \ingroup libusb_asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for a control transfer. - * - * If you pass a transfer buffer to this function, the first 8 bytes will - * be interpreted as a control setup packet, and the wLength field will be - * used to automatically populate the \ref libusb_transfer::length "length" - * field of the transfer. Therefore the recommended approach is: - * -# Allocate a suitably sized data buffer (including space for control setup) - * -# Call libusb_fill_control_setup() - * -# If this is a host-to-device transfer with a data stage, put the data - * in place after the setup packet - * -# Call this function - * -# Call libusb_submit_transfer() - * - * It is also legal to pass a NULL buffer to this function, in which case this - * function will not attempt to populate the length field. Remember that you - * must then populate the buffer and length fields later. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param buffer data buffer. If provided, this function will interpret the - * first 8 bytes as a setup packet and infer the transfer length from that. - * This pointer must be aligned to at least 2 bytes boundary. - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_control_transfer( - struct libusb_transfer *transfer, libusb_device_handle *dev_handle, - unsigned char *buffer, libusb_transfer_cb_fn callback, void *user_data, - unsigned int timeout) -{ - struct libusb_control_setup *setup = (struct libusb_control_setup *)(void *) buffer; - transfer->dev_handle = dev_handle; - transfer->endpoint = 0; - transfer->type = LIBUSB_TRANSFER_TYPE_CONTROL; - transfer->timeout = timeout; - transfer->buffer = buffer; - if (setup) - transfer->length = (int) (LIBUSB_CONTROL_SETUP_SIZE - + libusb_le16_to_cpu(setup->wLength)); - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup libusb_asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for a bulk transfer. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param endpoint address of the endpoint where this transfer will be sent - * \param buffer data buffer - * \param length length of data buffer - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_bulk_transfer(struct libusb_transfer *transfer, - libusb_device_handle *dev_handle, unsigned char endpoint, - unsigned char *buffer, int length, libusb_transfer_cb_fn callback, - void *user_data, unsigned int timeout) -{ - transfer->dev_handle = dev_handle; - transfer->endpoint = endpoint; - transfer->type = LIBUSB_TRANSFER_TYPE_BULK; - transfer->timeout = timeout; - transfer->buffer = buffer; - transfer->length = length; - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup libusb_asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for a bulk transfer using bulk streams. - * - * Since version 1.0.19, \ref LIBUSB_API_VERSION >= 0x01000103 - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param endpoint address of the endpoint where this transfer will be sent - * \param stream_id bulk stream id for this transfer - * \param buffer data buffer - * \param length length of data buffer - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_bulk_stream_transfer( - struct libusb_transfer *transfer, libusb_device_handle *dev_handle, - unsigned char endpoint, uint32_t stream_id, - unsigned char *buffer, int length, libusb_transfer_cb_fn callback, - void *user_data, unsigned int timeout) -{ - libusb_fill_bulk_transfer(transfer, dev_handle, endpoint, buffer, - length, callback, user_data, timeout); - transfer->type = LIBUSB_TRANSFER_TYPE_BULK_STREAM; - libusb_transfer_set_stream_id(transfer, stream_id); -} - -/** \ingroup libusb_asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for an interrupt transfer. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param endpoint address of the endpoint where this transfer will be sent - * \param buffer data buffer - * \param length length of data buffer - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_interrupt_transfer( - struct libusb_transfer *transfer, libusb_device_handle *dev_handle, - unsigned char endpoint, unsigned char *buffer, int length, - libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) -{ - transfer->dev_handle = dev_handle; - transfer->endpoint = endpoint; - transfer->type = LIBUSB_TRANSFER_TYPE_INTERRUPT; - transfer->timeout = timeout; - transfer->buffer = buffer; - transfer->length = length; - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup libusb_asyncio - * Helper function to populate the required \ref libusb_transfer fields - * for an isochronous transfer. - * - * \param transfer the transfer to populate - * \param dev_handle handle of the device that will handle the transfer - * \param endpoint address of the endpoint where this transfer will be sent - * \param buffer data buffer - * \param length length of data buffer - * \param num_iso_packets the number of isochronous packets - * \param callback callback function to be invoked on transfer completion - * \param user_data user data to pass to callback function - * \param timeout timeout for the transfer in milliseconds - */ -static inline void libusb_fill_iso_transfer(struct libusb_transfer *transfer, - libusb_device_handle *dev_handle, unsigned char endpoint, - unsigned char *buffer, int length, int num_iso_packets, - libusb_transfer_cb_fn callback, void *user_data, unsigned int timeout) -{ - transfer->dev_handle = dev_handle; - transfer->endpoint = endpoint; - transfer->type = LIBUSB_TRANSFER_TYPE_ISOCHRONOUS; - transfer->timeout = timeout; - transfer->buffer = buffer; - transfer->length = length; - transfer->num_iso_packets = num_iso_packets; - transfer->user_data = user_data; - transfer->callback = callback; -} - -/** \ingroup libusb_asyncio - * Convenience function to set the length of all packets in an isochronous - * transfer, based on the num_iso_packets field in the transfer structure. - * - * \param transfer a transfer - * \param length the length to set in each isochronous packet descriptor - * \see libusb_get_max_packet_size() - */ -static inline void libusb_set_iso_packet_lengths( - struct libusb_transfer *transfer, unsigned int length) -{ - int i; - for (i = 0; i < transfer->num_iso_packets; i++) - transfer->iso_packet_desc[i].length = length; -} - -/** \ingroup libusb_asyncio - * Convenience function to locate the position of an isochronous packet - * within the buffer of an isochronous transfer. - * - * This is a thorough function which loops through all preceding packets, - * accumulating their lengths to find the position of the specified packet. - * Typically you will assign equal lengths to each packet in the transfer, - * and hence the above method is sub-optimal. You may wish to use - * libusb_get_iso_packet_buffer_simple() instead. - * - * \param transfer a transfer - * \param packet the packet to return the address of - * \returns the base address of the packet buffer inside the transfer buffer, - * or NULL if the packet does not exist. - * \see libusb_get_iso_packet_buffer_simple() - */ -static inline unsigned char *libusb_get_iso_packet_buffer( - struct libusb_transfer *transfer, unsigned int packet) -{ - int i; - size_t offset = 0; - int _packet; - - /* oops..slight bug in the API. packet is an unsigned int, but we use - * signed integers almost everywhere else. range-check and convert to - * signed to avoid compiler warnings. FIXME for libusb-2. */ - if (packet > INT_MAX) - return NULL; - _packet = (int) packet; - - if (_packet >= transfer->num_iso_packets) - return NULL; - - for (i = 0; i < _packet; i++) - offset += transfer->iso_packet_desc[i].length; - - return transfer->buffer + offset; -} - -/** \ingroup libusb_asyncio - * Convenience function to locate the position of an isochronous packet - * within the buffer of an isochronous transfer, for transfers where each - * packet is of identical size. - * - * This function relies on the assumption that every packet within the transfer - * is of identical size to the first packet. Calculating the location of - * the packet buffer is then just a simple calculation: - * buffer + (packet_size * packet) - * - * Do not use this function on transfers other than those that have identical - * packet lengths for each packet. - * - * \param transfer a transfer - * \param packet the packet to return the address of - * \returns the base address of the packet buffer inside the transfer buffer, - * or NULL if the packet does not exist. - * \see libusb_get_iso_packet_buffer() - */ -static inline unsigned char *libusb_get_iso_packet_buffer_simple( - struct libusb_transfer *transfer, unsigned int packet) -{ - int _packet; - - /* oops..slight bug in the API. packet is an unsigned int, but we use - * signed integers almost everywhere else. range-check and convert to - * signed to avoid compiler warnings. FIXME for libusb-2. */ - if (packet > INT_MAX) - return NULL; - _packet = (int) packet; - - if (_packet >= transfer->num_iso_packets) - return NULL; - - return transfer->buffer + ((int) transfer->iso_packet_desc[0].length * _packet); -} - -/* sync I/O */ - -int LIBUSB_CALL libusb_control_transfer(libusb_device_handle *dev_handle, - uint8_t request_type, uint8_t bRequest, uint16_t wValue, uint16_t wIndex, - unsigned char *data, uint16_t wLength, unsigned int timeout); - -int LIBUSB_CALL libusb_bulk_transfer(libusb_device_handle *dev_handle, - unsigned char endpoint, unsigned char *data, int length, - int *actual_length, unsigned int timeout); - -int LIBUSB_CALL libusb_interrupt_transfer(libusb_device_handle *dev_handle, - unsigned char endpoint, unsigned char *data, int length, - int *actual_length, unsigned int timeout); - -/** \ingroup libusb_desc - * Retrieve a descriptor from the default control pipe. - * This is a convenience function which formulates the appropriate control - * message to retrieve the descriptor. - * - * \param dev_handle a device handle - * \param desc_type the descriptor type, see \ref libusb_descriptor_type - * \param desc_index the index of the descriptor to retrieve - * \param data output buffer for descriptor - * \param length size of data buffer - * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure - */ -static inline int libusb_get_descriptor(libusb_device_handle *dev_handle, - uint8_t desc_type, uint8_t desc_index, unsigned char *data, int length) -{ - return libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN, - LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t) ((desc_type << 8) | desc_index), - 0, data, (uint16_t) length, 1000); -} - -/** \ingroup libusb_desc - * Retrieve a descriptor from a device. - * This is a convenience function which formulates the appropriate control - * message to retrieve the descriptor. The string returned is Unicode, as - * detailed in the USB specifications. - * - * \param dev_handle a device handle - * \param desc_index the index of the descriptor to retrieve - * \param langid the language ID for the string descriptor - * \param data output buffer for descriptor - * \param length size of data buffer - * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure - * \see libusb_get_string_descriptor_ascii() - */ -static inline int libusb_get_string_descriptor(libusb_device_handle *dev_handle, - uint8_t desc_index, uint16_t langid, unsigned char *data, int length) -{ - return libusb_control_transfer(dev_handle, LIBUSB_ENDPOINT_IN, - LIBUSB_REQUEST_GET_DESCRIPTOR, (uint16_t)((LIBUSB_DT_STRING << 8) | desc_index), - langid, data, (uint16_t) length, 1000); -} - -int LIBUSB_CALL libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle, - uint8_t desc_index, unsigned char *data, int length); - -/* polling and timeouts */ - -int LIBUSB_CALL libusb_try_lock_events(libusb_context *ctx); -void LIBUSB_CALL libusb_lock_events(libusb_context *ctx); -void LIBUSB_CALL libusb_unlock_events(libusb_context *ctx); -int LIBUSB_CALL libusb_event_handling_ok(libusb_context *ctx); -int LIBUSB_CALL libusb_event_handler_active(libusb_context *ctx); -void LIBUSB_CALL libusb_interrupt_event_handler(libusb_context *ctx); -void LIBUSB_CALL libusb_lock_event_waiters(libusb_context *ctx); -void LIBUSB_CALL libusb_unlock_event_waiters(libusb_context *ctx); -int LIBUSB_CALL libusb_wait_for_event(libusb_context *ctx, struct timeval *tv); - -int LIBUSB_CALL libusb_handle_events_timeout(libusb_context *ctx, - struct timeval *tv); -int LIBUSB_CALL libusb_handle_events_timeout_completed(libusb_context *ctx, - struct timeval *tv, int *completed); -int LIBUSB_CALL libusb_handle_events(libusb_context *ctx); -int LIBUSB_CALL libusb_handle_events_completed(libusb_context *ctx, int *completed); -int LIBUSB_CALL libusb_handle_events_locked(libusb_context *ctx, - struct timeval *tv); -int LIBUSB_CALL libusb_pollfds_handle_timeouts(libusb_context *ctx); -int LIBUSB_CALL libusb_get_next_timeout(libusb_context *ctx, - struct timeval *tv); - -/** \ingroup libusb_poll - * File descriptor for polling - */ -struct libusb_pollfd { - /** Numeric file descriptor */ - int fd; - - /** Event flags to poll for from . POLLIN indicates that you - * should monitor this file descriptor for becoming ready to read from, - * and POLLOUT indicates that you should monitor this file descriptor for - * nonblocking write readiness. */ - short events; -}; - -/** \ingroup libusb_poll - * Callback function, invoked when a new file descriptor should be added - * to the set of file descriptors monitored for events. - * \param fd the new file descriptor - * \param events events to monitor for, see \ref libusb_pollfd for a - * description - * \param user_data User data pointer specified in - * libusb_set_pollfd_notifiers() call - * \see libusb_set_pollfd_notifiers() - */ -typedef void (LIBUSB_CALL *libusb_pollfd_added_cb)(int fd, short events, - void *user_data); - -/** \ingroup libusb_poll - * Callback function, invoked when a file descriptor should be removed from - * the set of file descriptors being monitored for events. After returning - * from this callback, do not use that file descriptor again. - * \param fd the file descriptor to stop monitoring - * \param user_data User data pointer specified in - * libusb_set_pollfd_notifiers() call - * \see libusb_set_pollfd_notifiers() - */ -typedef void (LIBUSB_CALL *libusb_pollfd_removed_cb)(int fd, void *user_data); - -const struct libusb_pollfd ** LIBUSB_CALL libusb_get_pollfds( - libusb_context *ctx); -void LIBUSB_CALL libusb_free_pollfds(const struct libusb_pollfd **pollfds); -void LIBUSB_CALL libusb_set_pollfd_notifiers(libusb_context *ctx, - libusb_pollfd_added_cb added_cb, libusb_pollfd_removed_cb removed_cb, - void *user_data); - -/** \ingroup libusb_hotplug - * Callback handle. - * - * Callbacks handles are generated by libusb_hotplug_register_callback() - * and can be used to deregister callbacks. Callback handles are unique - * per libusb_context and it is safe to call libusb_hotplug_deregister_callback() - * on an already deregisted callback. - * - * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 - * - * For more information, see \ref libusb_hotplug. - */ -typedef int libusb_hotplug_callback_handle; - -/** \ingroup libusb_hotplug - * - * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 - * - * Flags for hotplug events */ -typedef enum { - /** Default value when not using any flags. */ - LIBUSB_HOTPLUG_NO_FLAGS = 0, - - /** Arm the callback and fire it for all matching currently attached devices. */ - LIBUSB_HOTPLUG_ENUMERATE = 1<<0, -} libusb_hotplug_flag; - -/** \ingroup libusb_hotplug - * - * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 - * - * Hotplug events */ -typedef enum { - /** A device has been plugged in and is ready to use */ - LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED = 0x01, - - /** A device has left and is no longer available. - * It is the user's responsibility to call libusb_close on any handle associated with a disconnected device. - * It is safe to call libusb_get_device_descriptor on a device that has left */ - LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT = 0x02, -} libusb_hotplug_event; - -/** \ingroup libusb_hotplug - * Wildcard matching for hotplug events */ -#define LIBUSB_HOTPLUG_MATCH_ANY -1 - -/** \ingroup libusb_hotplug - * Hotplug callback function type. When requesting hotplug event notifications, - * you pass a pointer to a callback function of this type. - * - * This callback may be called by an internal event thread and as such it is - * recommended the callback do minimal processing before returning. - * - * libusb will call this function later, when a matching event had happened on - * a matching device. See \ref libusb_hotplug for more information. - * - * It is safe to call either libusb_hotplug_register_callback() or - * libusb_hotplug_deregister_callback() from within a callback function. - * - * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 - * - * \param ctx context of this notification - * \param device libusb_device this event occurred on - * \param event event that occurred - * \param user_data user data provided when this callback was registered - * \returns bool whether this callback is finished processing events. - * returning 1 will cause this callback to be deregistered - */ -typedef int (LIBUSB_CALL *libusb_hotplug_callback_fn)(libusb_context *ctx, - libusb_device *device, - libusb_hotplug_event event, - void *user_data); - -/** \ingroup libusb_hotplug - * Register a hotplug callback function - * - * Register a callback with the libusb_context. The callback will fire - * when a matching event occurs on a matching device. The callback is - * armed until either it is deregistered with libusb_hotplug_deregister_callback() - * or the supplied callback returns 1 to indicate it is finished processing events. - * - * If the \ref LIBUSB_HOTPLUG_ENUMERATE is passed the callback will be - * called with a \ref LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED for all devices - * already plugged into the machine. Note that libusb modifies its internal - * device list from a separate thread, while calling hotplug callbacks from - * libusb_handle_events(), so it is possible for a device to already be present - * on, or removed from, its internal device list, while the hotplug callbacks - * still need to be dispatched. This means that when using \ref - * LIBUSB_HOTPLUG_ENUMERATE, your callback may be called twice for the arrival - * of the same device, once from libusb_hotplug_register_callback() and once - * from libusb_handle_events(); and/or your callback may be called for the - * removal of a device for which an arrived call was never made. - * - * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 - * - * \param[in] ctx context to register this callback with - * \param[in] events bitwise or of events that will trigger this callback. See \ref - * libusb_hotplug_event - * \param[in] flags hotplug callback flags. See \ref libusb_hotplug_flag - * \param[in] vendor_id the vendor id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY - * \param[in] product_id the product id to match or \ref LIBUSB_HOTPLUG_MATCH_ANY - * \param[in] dev_class the device class to match or \ref LIBUSB_HOTPLUG_MATCH_ANY - * \param[in] cb_fn the function to be invoked on a matching event/device - * \param[in] user_data user data to pass to the callback function - * \param[out] callback_handle pointer to store the handle of the allocated callback (can be NULL) - * \returns LIBUSB_SUCCESS on success LIBUSB_ERROR code on failure - */ -int LIBUSB_CALL libusb_hotplug_register_callback(libusb_context *ctx, - libusb_hotplug_event events, - libusb_hotplug_flag flags, - int vendor_id, int product_id, - int dev_class, - libusb_hotplug_callback_fn cb_fn, - void *user_data, - libusb_hotplug_callback_handle *callback_handle); - -/** \ingroup libusb_hotplug - * Deregisters a hotplug callback. - * - * Deregister a callback from a libusb_context. This function is safe to call from within - * a hotplug callback. - * - * Since version 1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102 - * - * \param[in] ctx context this callback is registered with - * \param[in] callback_handle the handle of the callback to deregister - */ -void LIBUSB_CALL libusb_hotplug_deregister_callback(libusb_context *ctx, - libusb_hotplug_callback_handle callback_handle); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/tools/bootutil/Makefile b/tools/bootutil/Makefile deleted file mode 100644 index 24c67ed8..00000000 --- a/tools/bootutil/Makefile +++ /dev/null @@ -1,26 +0,0 @@ -TARGET = bootutil -CC = gcc -CFLAGS = -g -Wall -ifneq ($(OS),Windows_NT) # ncurses support limited to *NIX for now -LIBS = -lncurses -endif - -.PHONY: default all clean - -default: $(TARGET) -all: default - -OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) -HEADERS = $(wildcard *.h) - -%.o: %.c $(HEADERS) - $(CC) $(CFLAGS) -c $< -o $@ - -.PRECIOUS: $(TARGET) $(OBJECTS) - -$(TARGET): $(OBJECTS) - $(CC) $(OBJECTS) -Wall -o $@ $(LIBS) - -clean: - -rm -f *.o - -rm -f $(TARGET) diff --git a/tools/bootutil/binaries/linux/bootutil b/tools/bootutil/binaries/linux/bootutil deleted file mode 100755 index 975c202b..00000000 Binary files a/tools/bootutil/binaries/linux/bootutil and /dev/null differ diff --git a/tools/bootutil/binaries/osx/bootutil b/tools/bootutil/binaries/osx/bootutil deleted file mode 100755 index ea8e8b82..00000000 Binary files a/tools/bootutil/binaries/osx/bootutil and /dev/null differ diff --git a/tools/bootutil/binaries/windows/bootutil.exe b/tools/bootutil/binaries/windows/bootutil.exe deleted file mode 100644 index 6735fa9a..00000000 Binary files a/tools/bootutil/binaries/windows/bootutil.exe and /dev/null differ diff --git a/tools/bootutil/commands.c b/tools/bootutil/commands.c deleted file mode 100644 index fce74fdd..00000000 --- a/tools/bootutil/commands.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include -#include "commands.h" -#include "serialport.h" -#include "infohelper.h" - -extern int g_bootutil_gui_mode; /* Defined in main.c */ - -/* -static int -commands_stol(char *param_val, long min, long max, long *output, uint8_t b) -{ - char *endptr; - long lval; - - if ((b != 10) && (b != 16)) { - return EINVAL; - } - - lval = strtol(param_val, &endptr, b); - if (param_val != '\0' && *endptr == '\0' && - lval >= min && lval <= max) { - *output = lval; - } else { - return EINVAL; - } - - return 0; -} -*/ - -static int -commands_err_unknown_arg(char *arg_name) -{ - LOGERR("unknown argument \"%s\"\n", - arg_name); - return EINVAL; -} - -int -commands_cmd_reset(void) -{ - /* Can't log data with -g option since stdout use kills ncurses */ - if (g_bootutil_gui_mode == 0) { - LOGINFO("attempting to reset (toggling DTR)\n"); - } - - /* Toggle the DTR line to provoke a reset */ - serialport_set_dtr(1); - serialport_set_dtr(0); - - return 0; -} - -int -commands_parse(char *argv) -{ - int i = 0; - - while(argv[i]) { - argv[i] = tolower(argv[i]); - i++; - } - - if (strcmp(argv, "reset") == 0) { - return commands_cmd_reset(); - } else { - return commands_err_unknown_arg(argv); - } -} diff --git a/tools/bootutil/commands.h b/tools/bootutil/commands.h deleted file mode 100644 index 4e1f4018..00000000 --- a/tools/bootutil/commands.h +++ /dev/null @@ -1,7 +0,0 @@ -#ifndef __COMMANDS_H__ -#define __COMMANDS_H__ - -int commands_cmd_reset(void); -int commands_parse(char *argv); - -#endif diff --git a/tools/bootutil/gui.c b/tools/bootutil/gui.c deleted file mode 100644 index 6b7306a8..00000000 --- a/tools/bootutil/gui.c +++ /dev/null @@ -1,334 +0,0 @@ -#ifndef _WIN32 - -#include -#include -#include -#include -#include "infohelper.h" -#include "commands.h" - -enum gui_action { - GUI_ACTION_ABOUT = 0, - GUI_ACTION_RESET, - GUI_ACTION_EXIT, - GUI_ACTION_NONE = 0xFF, -}; - -struct gui_frame_cfg { - int x; - int y; - int w; - int h; - int titlebar; - int colorpair; -}; - -/* Advanced prototypes for command handler functions */ -int gui_handler_about(void); -int gui_handler_reset(void); - -static const struct { - const char *name; - const char shortcut; - const enum gui_action action; - int (*handler)(void); -} gui_menu_items[] = { - { "About", '?', GUI_ACTION_ABOUT, gui_handler_about }, - { "Reset Device", 'r', GUI_ACTION_RESET, gui_handler_reset }, - { "Exit", 'X', GUI_ACTION_EXIT, NULL }, - { NULL, '\0', GUI_ACTION_NONE, NULL } -}; - -static volatile int g_gui_menu_item_count = 0; - -int -gui_handler_about(void) { - printw("About Adafruit nRF52 bootutil!\n"); - return 0; -} - -int -gui_handler_reset(void) { - printw("Resetting the nRF52 (toggling DTR)\n"); - commands_cmd_reset(); - return 0; -} - -static void -gui_config(void) -{ - /* Disable line buffering (to avoid waiting for enter key) */ - raw(); - /* Enable color functionality */ - start_color(); - /* Enable F1, F2, etc. */ - keypad(stdscr, TRUE); - /* Dont echo in getch (avoids garbage output on special keys) */ - noecho(); - /* Setup color pairs */ - init_pair(1, COLOR_CYAN, COLOR_BLACK); - init_pair(2, COLOR_BLACK, COLOR_BLACK); - init_pair(3, COLOR_WHITE, COLOR_BLUE); - /* Hide the cursor by default */ - curs_set(0); -} - -static void -gui_draw_frame(WINDOW *win, const struct gui_frame_cfg *cfg) -{ - int i, hpad; - - if ((cfg->x < 0) || (cfg->y < 0)) { - return; - } - - if ((!cfg->w) || (!cfg->h)) { - return; - } - - /* Check if we need hpadding for the titlebar */ - hpad = cfg->titlebar ? 2 : 0; - - /* Set the frame colors */ - if (cfg->colorpair > 0) { - attron(COLOR_PAIR(cfg->colorpair)); - } - - /* Top bar */ - wmove(win, cfg->y, cfg->x); - addch(ACS_ULCORNER); - for (i=0;iw;i++) { - addch(ACS_HLINE); - } - addch(ACS_URCORNER); - - /* Title section if requested */ - if (cfg->titlebar) { - wmove(win, cfg->y+1, cfg->x); - addch(ACS_VLINE); - wmove(win, cfg->y+1, cfg->x+cfg->w+1); - addch(ACS_VLINE); - wmove(win, cfg->y+2, cfg->x); - addch(ACS_LTEE); - for (i=0;iw;i++) { - addch(ACS_HLINE); - } - addch(ACS_RTEE); - } - - /* Middle lines */ - for (i=0;ih;i++) { - wmove(win, cfg->y+i+hpad+1, cfg->x); - addch(ACS_VLINE); - wmove(win, cfg->y+i+hpad+1, cfg->x+cfg->w+1); - addch(ACS_VLINE); - } - - /* Bottom bar */ - wmove(win,cfg-> y+cfg->h+hpad+1, cfg->x); - addch(ACS_LLCORNER); - for (i=0;iw;i++) { - addch(ACS_HLINE); - } - addch(ACS_LRCORNER); - - /* Clear the frame colors */ - if (cfg->colorpair > 0) { - attroff(COLOR_PAIR(cfg->colorpair)); - } - - wrefresh(win); -} - -static void -gui_menu_print(WINDOW *win, int highlight) -{ - int rows, cols; - int x, y, i; - - /* Get the window dimenstions */ - getmaxyx(stdscr, rows, cols); - - /* Draw some borders */ - struct gui_frame_cfg cfg = { - .x = 0, - .y = 0, - .w = cols-2, - .h = rows-2, - .titlebar = false, - .colorpair = 0 - }; - gui_draw_frame(win, &cfg); - - //box(win, 0, 0); - - /* Add the title */ - char *title = "Adafruit nRF52 Bootloader Utility"; - attron(COLOR_PAIR(1)); - attron(A_BOLD); - mvprintw(2, (cols-strlen(title))/2, "%s", title); - attroff(A_BOLD); - attroff(COLOR_PAIR(1)); - - /* Set the menu item start position */ - y = 4; - x = (cols-strlen(title))/2; - wmove(win, y, x); - - if (highlight > g_gui_menu_item_count) { - highlight = 0; - } - - for(i = 0; i < g_gui_menu_item_count; ++i) { - /* Print the menu item name */ - if(highlight == i + 1) { - wattron(win, A_REVERSE); - mvwprintw(win, y, x, "%s", gui_menu_items[i].name); - wattroff(win, A_REVERSE); - } else { - mvwprintw(win, y, x, "%s", gui_menu_items[i].name); - } - /* Print the shortcut */ - if (gui_menu_items[i].shortcut != '\0') { - printw(" ["); - attron(A_BOLD); - printw("%c", gui_menu_items[i].shortcut); - attroff(A_BOLD); - printw("]"); - } - ++y; - } - - /* Change the cursor to displat any debug output at a safe location*/ - wmove(win, 5 + g_gui_menu_item_count, x); - wrefresh(win); -} - -int -gui_init(const char *ttyname) -{ - int i; - int ch; - int selected_menu_item; - int execute_menu_item; - int rows, cols; - WINDOW * mainwin; - - /* Init ncurses window */ - if ( (mainwin = initscr()) == NULL ) { - LOGERR("error initialising ncurses.\n"); - exit(EXIT_FAILURE); - } - - /* Setup the core window properties */ - gui_config(); - - /* Count the total menu items */ - g_gui_menu_item_count = -1; - while (gui_menu_items[++g_gui_menu_item_count].action != GUI_ACTION_NONE) { - } - - if (g_gui_menu_item_count < 1) { - LOGERR("No menu items defined\n"); - return EXIT_FAILURE; - } - - /* Get the window dimenstions */ - getmaxyx(stdscr, rows, cols); - - /* Show the TTY/Serial port */ - attron(COLOR_PAIR(2)); - attron(A_BOLD); - mvprintw(rows-4, (cols-strlen(ttyname))/2, "%s", ttyname); - attroff(A_BOLD); - attroff(COLOR_PAIR(2)); - - /* Help message on navigation */ - char *dataentry = "Use the arrow or shortcut keys to continue, 'X' to exit"; - attron(COLOR_PAIR(1)); - attron(A_BOLD); - mvprintw(rows-3, (cols-strlen(dataentry))/2, "%s", dataentry); - attroff(A_BOLD); - attroff(COLOR_PAIR(1)); - - /* Render the menu */ - selected_menu_item = 1; - gui_menu_print(mainwin, selected_menu_item); - - /* Check input until we get an exit request */ - while(1) { - ch = getch(); - execute_menu_item = -1; - switch(ch) { - case KEY_F(1): - break; - case KEY_LEFT: - selected_menu_item--; - break; - case KEY_RIGHT: - selected_menu_item++; - break; - case KEY_UP: - selected_menu_item--; - break; - case KEY_DOWN: - selected_menu_item++; - break; - case 10: /* ENTER */ - execute_menu_item = selected_menu_item-1; - break; - case 'Z': - goto cleanup; - break; - default: - /* Check for hotkeys */ - for (i=0;i g_gui_menu_item_count) { - selected_menu_item = g_gui_menu_item_count; - } - - /* Re-render the menu with the new menu item */ - gui_menu_print(mainwin, selected_menu_item); - - /* Update the display */ - refresh(); - } - -cleanup: - /* Clean up after ourselves */ - delwin(mainwin); - endwin(); - refresh(); - - return EXIT_SUCCESS; -} - -#endif /* _WIN32 */ diff --git a/tools/bootutil/gui.h b/tools/bootutil/gui.h deleted file mode 100644 index eb9f856d..00000000 --- a/tools/bootutil/gui.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __GUI_H__ -#define __GUI_H__ - -#ifndef _WIN32 - -int gui_init(const char *ttyname); - -#endif /* _WIN32 */ - -#endif diff --git a/tools/bootutil/infohelper.c b/tools/bootutil/infohelper.c deleted file mode 100644 index f89a03f5..00000000 --- a/tools/bootutil/infohelper.c +++ /dev/null @@ -1,92 +0,0 @@ -/********************************************************************************** - ********************************************************************************** - *** - *** infohelper.c - *** - implementation of a crude verbosity-controllabe info system - *** - *** Copyright (C) 2014 Christian Klippel - *** - *** 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., - *** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - *** - **/ - -#include -#include -#include "infohelper.h" - -static char infolevel = 0; - -void infohelper_set_infolevel(char lvl) -{ - infolevel = lvl; -} - -void infohelper_increase_infolevel(void) -{ - if(infolevel < 20) - { - infolevel++; - } -} - -void infohelper_set_argverbosity(int num_args, char **arg_ptr) -{ - char *cur_arg; - - while(num_args--) - { - cur_arg = arg_ptr[num_args]; - - if(*cur_arg++ == '-') - { - if(*cur_arg == 'q') - { - infolevel = 0; - return; - } - else while(*cur_arg++ == 'v') - { - infolevel++; - } - } - } -} - -void infohelper_output(int loglevel, const char* format, ...) -{ - if (infolevel < loglevel) - return; - const char* log_level_names[] = {"Error: ", "Warning: ", "", "\t", "\t\t"}; - const int log_level_names_count = sizeof(log_level_names) / sizeof(const char*); - if (loglevel >= log_level_names_count) - loglevel = log_level_names_count - 1; - - va_list v; - printf("%s", log_level_names[loglevel]); - va_start(v, format); - vprintf(format, v); - va_end(v); - printf("\n"); -} - -void infohelper_output_plain(int loglevel, const char* format, ...) -{ - if (infolevel < loglevel) - return; - va_list v; - va_start(v, format); - vprintf(format, v); - va_end(v); -} diff --git a/tools/bootutil/infohelper.h b/tools/bootutil/infohelper.h deleted file mode 100644 index 6abcc613..00000000 --- a/tools/bootutil/infohelper.h +++ /dev/null @@ -1,60 +0,0 @@ -/********************************************************************************** - ********************************************************************************** - *** - *** infohelper.h - *** - defines and prototypes for a crude verbositiy-controllabe info output - *** - *** Copyright (C) 2014 Christian Klippel - *** - *** 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., - *** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - *** - **/ - -#ifndef INFOHELPER_H -#define INFOHELPER_H - - -/* -** set verbositiy level -** 0 = only error messages -** 1 = warnings -** 2 = information messages (default) -** 3 = debugging info -** 4 = extra debugging info -*/ -void infohelper_set_infolevel(char lvl); - -/* -** increase verbositylevel by 1 -*/ -void infohelper_increase_infolevel(void); - -/* -** set verbosity level according to arguments given -*/ -void infohelper_set_argverbosity(int num_args, char **arg_ptr); - -void infohelper_output(int loglevel, const char* format, ...); -void infohelper_output_plain(int loglevel, const char* format, ...); - -#define LOGERR(...) infohelper_output(0, __VA_ARGS__) -#define LOGWARN(...) infohelper_output(1, __VA_ARGS__) -#define LOGINFO(...) infohelper_output(2, __VA_ARGS__) -#define LOGDEBUG(...) infohelper_output(3, __VA_ARGS__) -#define LOGVERBOSE(...) infohelper_output(4, __VA_ARGS__) - -#define INFO(...) infohelper_output_plain(0, __VA_ARGS__) - -#endif diff --git a/tools/bootutil/main.c b/tools/bootutil/main.c deleted file mode 100644 index 245c019f..00000000 --- a/tools/bootutil/main.c +++ /dev/null @@ -1,227 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "serialport.h" -#include "infohelper.h" -#include "commands.h" -#ifndef _WIN32 -#include "gui.h" -#endif - -#define BOOTUTIL_VER_MAJOR (0U) -#define BOOTUTIL_VER_MINOR (0U) -#define BOOTUTIL_VER_REVISION (1U) - -volatile int g_bootutil_serport_open = 0; -volatile int g_bootutil_gui_mode = 0; - -static int -bootutil_err_too_many_instances(char *arg_name) -{ - LOGERR("multiple instances of \"%s\"\n", - arg_name); - return EINVAL; -} - -static int -bootutil_err_missing_arg(char *arg_name) -{ - LOGERR("missing argument \"%s\"\n", - arg_name); - return EINVAL; -} - -static int -bootutil_err_missing_value(char *arg_name) -{ - LOGERR("missing value for argument \"%s\"\n", - arg_name); - return EINVAL; -} - -static void -bootutil_help(int rc) -{ - printf("Bootloader utility for the Adafruit nRF52 board family.\n"); - printf("Version: %u.%u.%u\n", BOOTUTIL_VER_MAJOR, - BOOTUTIL_VER_MINOR, - BOOTUTIL_VER_REVISION); - printf("\n"); -#ifdef _WIN32 - printf("Usage: bootutil.exe -d [-c ] [-ghrv]\n"); -#else - printf("Usage: bootutil -d [-c ] [-hrv]\n"); -#endif - printf("\n"); - printf(" -d : serial/tty port name (mandatory)\n"); - printf(" -c : execute the specified command\n"); - printf(" reset : perform a system reset (toggle DTR)\n"); -#ifndef _WIN32 - printf(" -g : show GUI\n"); -#endif - printf(" -h : show help\n"); - printf(" -r : perform a system reset (toggle DTR)\n"); - printf(" -v : verbose output\n"); - printf("\n"); -#ifdef _WIN32 - printf("Ex: $ bootutil.exe -d COM14 -r\n"); -#else - printf("Ex: $ bootutil -d /dev/tty.SLAB_USBtoUART -r\n"); -#endif - - exit(rc); -} - -static int -bootutil_serport_open(char *argv) -{ - int serport; - - serport = serialport_open(argv, 115200); - if (serport != 0) { - g_bootutil_serport_open = 1; - } - - return 0; -} - -static int -bootutil_serport_close(void) -{ - int rc; - - rc = serialport_close(); - g_bootutil_serport_open = 0; - - return rc; -} - -int -main(int argc, char **argv) -{ - int ch; - int rc = -1; - int ttyflag = 0; - int rstflag = 0; - int cmdflag = 0; - int vrbflag = 0; - char cmd[256]; - char tty[PATH_MAX + NAME_MAX]; - - memset(cmd, '\0', sizeof(cmd)); - memset(tty, '\0', sizeof(cmd)); - - /* Display help menu on zero params or '?' */ - if (argc == 1) { - bootutil_help(0); - } - if ((argc == 2) && (strcmp(argv[1], "?") == 0)) { - bootutil_help(0); - } - - /* Parse input params */ - while ((ch = getopt(argc, argv, "d:c:ghrv")) != -1) { - switch (ch) { - case 'd': - /* Make sure we have a valid command (isn't another param) */ - if (optarg[0] == '-') { - rc = bootutil_err_missing_value("-d"); - goto error; - } - ttyflag = 1; - strncpy(tty, optarg, sizeof(tty)-1); - break; - case 'c': - /* Check if we've already received a command */ - if (cmdflag == 1) { - exit(bootutil_err_too_many_instances("-c")); - } - /* Make sure we have a valid command (isn't another param) */ - if (optarg[0] == '-') { - rc = bootutil_err_missing_value("-c"); - goto error; - } - cmdflag = 1; - strncpy(cmd, optarg, sizeof(cmd)-1); - break; -#ifndef _WIN32 - case 'g': - g_bootutil_gui_mode = 1; - break; -#endif - case 'r': - rstflag = 1; - break; - case 'v': - vrbflag = 1; - break; - case 'h': - case '?': - bootutil_help(0); - break; - } - } - - /* Make sure we have something to do first */ - if (!rstflag && !cmdflag && !g_bootutil_gui_mode) { - LOGERR("no action requested\n"); - goto error; - } - - /* Set logger level to verbose if requested */ - if (vrbflag == 1) { - infohelper_set_infolevel(2); - } - - /* Try to open the serial port */ - if (!ttyflag) { - rc = bootutil_err_missing_arg("-d"); - goto error; - } else { - bootutil_serport_open(tty); - /* Make sure the serial port was actually opened */ - if (g_bootutil_serport_open != 1) { - rc = EINVAL; - goto error; - } - } - - /* Check if a system reset was requested (perform this first) */ - if (rstflag) { - rc = commands_cmd_reset(); - if (rc != 0) { - goto error; - } - } - - /* Command handler */ - if (cmdflag) { - /* Try to parse the supplied command */ - rc = commands_parse(cmd); - if (rc != 0) { - goto error; - } - } - -#ifndef _WIN32 - /* GUI handler */ - if (g_bootutil_gui_mode) { - rc = gui_init(tty); - if (rc != 0) { - goto error; - } - } -#endif - - /* Display the return code of any unhandled errors */ - if (rc != 0) { - LOGERR("rc = 0x%04X (%u): %s\n", rc, rc, strerror(rc)); - } - -error: - bootutil_serport_close(); - exit(rc); -} diff --git a/tools/bootutil/serialport.c b/tools/bootutil/serialport.c deleted file mode 100644 index b8069d2c..00000000 --- a/tools/bootutil/serialport.c +++ /dev/null @@ -1,661 +0,0 @@ -/********************************************************************************** - ********************************************************************************** - *** - *** serialport.c - *** - low level functions to access the serial port - *** - *** Copyright (C) 2014 Christian Klippel - *** - *** 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., - *** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - *** - **/ - -#include -#include -#include -#include - -#if defined (_WIN32) -#include -#else -#include -#include -#include -#include -#include -#include -#endif - -#include "serialport.h" -#include "infohelper.h" - -#if defined (_WIN32) -static HANDLE sPort = NULL; -static DCB sDCB; -static COMMTIMEOUTS sTIMEOUTS; -#else -static int serial_port = -1; -static struct termios term; -static unsigned int timeout; -#endif - - -#ifdef _WIN32 - -#ifndef CBR_230400 -#define CBR_230400 230400 -#endif -#ifndef CBR_460800 -#define CBR_460800 460800 -#endif -#ifndef CBR_512000 -#define CBR_512000 512000 -#endif -#ifndef CBR_921600 -#define CBR_921600 921600 -#endif - - -void serialport_setbaudrate(unsigned int baudrate) -{ - DWORD br = 0; - switch(baudrate) - { - case 9600: br = CBR_9600; break; - case 14400: br = CBR_14400; break; - case 19200: br = CBR_19200; break; - case 38400: br = CBR_38400; break; - case 56000: br = CBR_56000; break; - case 57600: br = CBR_57600; break; - case 115200: br = CBR_115200; break; - case 128000: br = CBR_128000; break; - case 230400: br = CBR_230400; break; - case 256000: br = CBR_256000; break; - case 512000: br = CBR_512000; break; - case 460800: br = CBR_460800; break; - case 921600: br = CBR_921600; break; - } - if (br == 0) - { - LOGWARN("unsupported baud rate: %d, using 115200", baudrate); - br = CBR_115200; - } - - memset(&sDCB, 0, sizeof(DCB)); - BuildCommDCB("baud=9600 parity=N data=8 stop=1",&sDCB); - sDCB.DCBlength = sizeof(DCB); - sDCB.BaudRate = br; - sDCB.fBinary = TRUE; - sDCB.fParity = FALSE; -// sDCB.fOutxCtsFlow = TRUE; - sDCB.fDtrControl = DTR_CONTROL_DISABLE; - sDCB.fDsrSensitivity= FALSE; - sDCB.fRtsControl = RTS_CONTROL_DISABLE; - sDCB.ByteSize = 8; - sDCB.StopBits = ONESTOPBIT; - sDCB.fAbortOnError = FALSE; - sDCB.fOutX = FALSE; - sDCB.fInX = FALSE; - if (!SetCommState(sPort, &sDCB)) - { - LOGDEBUG("SetCommState call failed"); - } -} - -int serialport_open(const char *device, unsigned int baudrate) -{ - char portName[40]; - sprintf(portName,"\\\\.\\%s", device); - sPort = CreateFile(portName, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - - if (sPort == INVALID_HANDLE_VALUE) - { - LOGERR("Failed to open %s", device); - return 0; - } - - SetupComm(sPort, 256, 256); - serialport_setbaudrate(baudrate); - serialport_set_timeout(1000); - return 1; -} - -void serialport_set_timeout(unsigned int timeout) -{ - LOGDEBUG("setting serial port timeouts to %d ms", timeout); - sTIMEOUTS.ReadIntervalTimeout = 0; - sTIMEOUTS.ReadTotalTimeoutConstant = timeout; - sTIMEOUTS.ReadTotalTimeoutMultiplier = 0; - sTIMEOUTS.WriteTotalTimeoutConstant = timeout; - sTIMEOUTS.WriteTotalTimeoutMultiplier = 0; - if (!SetCommTimeouts(sPort,&sTIMEOUTS)) - { - LOGDEBUG("SetCommTimeouts call failed"); - } - SetCommMask(sPort, EV_TXEMPTY); -} - -unsigned serialport_get_timeout() -{ - return sTIMEOUTS.ReadTotalTimeoutConstant; -} - -unsigned serialport_read(unsigned char* data, unsigned int size) -{ - unsigned long cb; - ReadFile(sPort, data, size, &cb, NULL); - if (cb != size) - { - LOGDEBUG("read %d, requested %d", cb, size); - } - return (unsigned) cb; -} - -unsigned serialport_write(const unsigned char* data, unsigned int size) -{ - unsigned long cb; - WriteFile(sPort, data, size, &cb, NULL); - if (cb != size) - { - LOGDEBUG("wrote %d, requested %d", cb, size); - } - return (unsigned) cb; -} - -void serialport_flush(void) -{ - unsigned char tmp[512]; - if(sPort) - { - LOGDEBUG("flush start"); - unsigned old_timeout = sTIMEOUTS.ReadTotalTimeoutConstant; - serialport_set_timeout(1); - ClearCommError(sPort, NULL, NULL); - PurgeComm(sPort, PURGE_TXCLEAR|PURGE_RXCLEAR); - unsigned long cb; - int result; - do { - result = ReadFile(sPort, tmp, 512, &cb, NULL); - LOGVERBOSE("flushed %lu bytes", cb); - } while(cb && result == 0); - serialport_set_timeout(old_timeout); - LOGDEBUG("flush complete"); - } -} - -void serialport_drain(void) -{ - if(sPort) - { - FlushFileBuffers(sPort); - } - -} - -int serialport_close(void) -{ - if(!sPort) - return 0; - serialport_drain(); - serialport_flush(); - CloseHandle(sPort); - sPort = NULL; - return 1; -} - -void serialport_set_dtr(unsigned char val) -{ - if(sPort) - { - EscapeCommFunction(sPort,((val)?SETDTR:CLRDTR)); - } -} - -void serialport_set_rts(unsigned char val) -{ - if(sPort) - { - EscapeCommFunction(sPort,((val)?SETRTS:CLRRTS)); - } -} - -void serialport_send_break() -{ - if (sPort) - { - EscapeCommFunction(sPort, SETBREAK); - Sleep(250); - EscapeCommFunction(sPort, CLRBREAK); - } -} - - -#else - -void serialport_set_baudrate(unsigned int baudrate) -{ - switch(baudrate) - { - case 2400: - cfsetispeed(&term,B2400); - cfsetospeed(&term,B2400); - break; - - case 4800: - cfsetispeed(&term,B4800); - cfsetospeed(&term,B4800); - break; - - case 9600: - cfsetispeed(&term,B9600); - cfsetospeed(&term,B9600); - break; - - case 19200: - cfsetispeed(&term,B19200); - cfsetospeed(&term,B19200); - break; - - case 38400: - cfsetispeed(&term,B38400); - cfsetospeed(&term,B38400); - break; - - case 57600: - cfsetispeed(&term,B57600); - cfsetospeed(&term,B57600); - break; - - case 115200: - cfsetispeed(&term,B115200); - cfsetospeed(&term,B115200); - break; - - case 230400: - cfsetispeed(&term,B230400); - cfsetospeed(&term,B230400); - break; -#ifndef __APPLE__ - case 460800: - cfsetispeed(&term,B460800); - cfsetospeed(&term,B460800); - break; - - case 921600: - cfsetispeed(&term,B921600); - cfsetospeed(&term,B921600); - break; -#endif - default: - LOGWARN("serialport_set_baudrate: baud rate %d may not work", baudrate); - cfsetispeed(&term,baudrate); - cfsetospeed(&term,baudrate); - break; - } -} - -void serialport_set_timeout(unsigned int t) -{ - if(t != timeout) - { - LOGDEBUG("setting timeout %i", t); - timeout = t; - } -} - -unsigned serialport_get_timeout() -{ - return timeout; -} - -int serialport_open(const char *device, unsigned int baudrate) -{ - LOGINFO("opening port %s at %d", device, baudrate); - int flags = O_RDWR | O_NOCTTY; -#ifdef __APPLE__ - flags |= O_NONBLOCK; -#endif - serial_port = open(device, flags); - - if(serial_port<0) - { - LOGERR("cannot access %s\n",device); - return 0; - } - -#ifdef __APPLE__ - flags = fcntl(serial_port, F_GETFL, 0); - fcntl(serial_port, F_SETFL, flags & (~O_NONBLOCK)); -#endif - - serialport_set_dtr(0); - - LOGDEBUG("tcgetattr"); - tcgetattr(serial_port,&term); - - serialport_set_baudrate(baudrate); - - term.c_cflag = (term.c_cflag & ~CSIZE) | CS8; - term.c_cflag |= CLOCAL | CREAD; - - term.c_cflag &= ~(PARENB | PARODD); - term.c_cflag &= ~CSTOPB; - - term.c_iflag = IGNBRK; - - term.c_iflag &= ~(IXON | IXOFF); - - term.c_lflag = 0; - - term.c_oflag = 0; - - - term.c_cc[VMIN]=0; - term.c_cc[VTIME]=1; - timeout = 100; - - - - LOGDEBUG("tcsetattr"); - if (tcsetattr(serial_port, TCSANOW, &term)!=0) - { - LOGERR("setattr stage 1 failed"); - return 0; - } - - - if (tcgetattr(serial_port, &term)!=0) - { - LOGERR("getattr failed"); - return 0; - } - - term.c_cflag &= ~CRTSCTS; - - if (tcsetattr(serial_port, TCSANOW, &term)!=0) - { - LOGERR("setattr stage 2 failed"); - return 0; - } - LOGDEBUG("serial open"); - return serial_port; -} - -unsigned serialport_read(unsigned char* data, unsigned int size) -{ - struct timeval tv0, tv1; - gettimeofday(&tv0, NULL); - unsigned n = 0; - unsigned time_spent = 0; - do - { - unsigned cb = read(serial_port, data + n, size - n); - n += cb; - gettimeofday(&tv1, NULL); - time_spent = (tv1.tv_sec - tv0.tv_sec) * 1000 + tv1.tv_usec / 1000 - tv0.tv_usec / 1000; - } while (n < size && time_spent < timeout); - return n; -} - -unsigned serialport_write(const unsigned char* data, unsigned int size) -{ - return write(serial_port, data, size); -} - -void serialport_flush(void) -{ - static unsigned char b; - - if(serial_port) - { - tcdrain(serial_port); - while(read(serial_port, &b, 1) > 0); - } -} - -void serialport_drain(void) -{ - if(serial_port) - { - tcdrain(serial_port); - } -} - -int serialport_close(void) -{ - if(serial_port) - { - tcdrain(serial_port); - tcflush(serial_port, TCIOFLUSH); - close(serial_port); - return 1; - } - else - { - return 0; - } -} - -void serialport_set_dtr(unsigned char val) -{ - int mcs; - - if(serial_port) - { - ioctl (serial_port, TIOCMGET, &mcs); - - if(val) - { - mcs |= TIOCM_DTR; - ioctl (serial_port, TIOCMSET, &mcs); - } - else - { - mcs &= ~TIOCM_DTR; - ioctl (serial_port, TIOCMSET, &mcs); - } - } -} - -void serialport_set_rts(unsigned char val) -{ - int mcs; - - if(serial_port) - { - ioctl (serial_port, TIOCMGET, &mcs); - - if(val) - { - mcs |= TIOCM_RTS; - ioctl (serial_port, TIOCMSET, &mcs); - } - else - { - mcs &= ~TIOCM_RTS; - ioctl (serial_port, TIOCMSET, &mcs); - } - } -} - -void serialport_send_break() -{ - tcsendbreak(serial_port, 0); -} - - - -#endif -static unsigned char subst_C0[2] = { 0xDB, 0xDC }; -static unsigned char subst_DB[2] = { 0xDB, 0xDD }; - -#define STATIC_SLIP_BUF_SIZE 4096 -int serialport_send_slip_static(unsigned char *data, unsigned int size) -{ - unsigned char buf[STATIC_SLIP_BUF_SIZE]; - unsigned out_pos = 0; - for (int i = 0; i < size; ++i) - { - unsigned char cur = data[i]; - if (cur == 0xC0) - { - buf[out_pos++] = subst_C0[0]; - buf[out_pos++] = subst_C0[1]; - } - else if (cur == 0xDB) - { - buf[out_pos++] = subst_DB[0]; - buf[out_pos++] = subst_DB[1]; - } - else - buf[out_pos++] = cur; - } - - if(serialport_write(buf, out_pos) != out_pos) - { - LOGERR("failed sending %d bytes", out_pos); - return 0; - } - serialport_drain(); - return size; -} - -int serialport_send_slip(unsigned char *data, unsigned int size) -{ - unsigned int sent; - unsigned char cur_byte; - - if (size < STATIC_SLIP_BUF_SIZE / 2) - return serialport_send_slip_static(data, size); - - sent = 0; - - while(sent != size) - { - cur_byte = *data++; - if(cur_byte == 0xC0) - { - if(serialport_write(subst_C0, 2) != 2) - { - LOGERR("failed substituting 0xC0"); - return 0; - } - } - else if(cur_byte == 0xDB) - { - if(serialport_write(subst_DB, 2) != 2) - { - LOGERR("failed substituting 0xDB"); - return 0; - } - } - else - { - if(serialport_write(&cur_byte, 1) != 1) - { - LOGERR("failed sending byte %i", sent); - return 0; - } - } - sent++; - } - - serialport_drain(); - - return sent; -} - -int serialport_receive_slip(unsigned char *data, unsigned int size) -{ - unsigned int received; - unsigned char cur_byte; - - received = 0; - - while(received != size) - { - if(serialport_read(&cur_byte, 1) != 1) - { - LOGERR("failed reading byte"); - return 0; - } - - if(cur_byte == 0xDB) - { - if(serialport_read(&cur_byte, 1) != 1) - { - LOGERR("failed reading byte for unslip"); - return 0; - } - - if(cur_byte == 0xDC) - { - *data++ = 0xC0; - } - else if(cur_byte == 0xDD) - { - *data++ = 0xDB; - } - else - { - LOGERR("unslip sequence wrong"); - return 0; - } - } - else - { - *data++ = cur_byte; - } - - received++; - } - - return received; -} - -int serialport_send_C0(void) -{ - unsigned char b; - - b = 0xC0; - - if(serialport_write(&b, 1) != 1) - { - LOGERR("failed sending 0xC0"); - return 0; - } - serialport_drain(); - return 1; -} - -int serialport_receive_C0(void) -{ - unsigned char b; - - b = 0x00; - - if(serialport_read(&b, 1) != 1) - { - return 0; - } - - if(b != 0xC0) - { - LOGDEBUG("serialport_receive_C0: %02X instead of C0", b); - return 0; - } - - return 1; -} diff --git a/tools/bootutil/serialport.h b/tools/bootutil/serialport.h deleted file mode 100644 index 168b1857..00000000 --- a/tools/bootutil/serialport.h +++ /dev/null @@ -1,46 +0,0 @@ -/********************************************************************************** - ********************************************************************************** - *** - *** serialport.h - *** - include file for serialport.c - *** - *** Copyright (C) 2014 Christian Klippel - *** - *** 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., - *** 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - *** - **/ - - -#ifndef SERIALPORT_H -#define SERIALPORT_H - -int serialport_open(const char *dev, unsigned int baudrate); -int serialport_close(void); -void serialport_set_dtr(unsigned char val); -void serialport_set_rts(unsigned char val); -void serialport_send_break(); -void serialport_set_timeout(unsigned int timeout); -unsigned serialport_get_timeout(); -void serialport_drain(void); -void serialport_flush(void); -unsigned serialport_read(unsigned char* data, unsigned int size); -unsigned serialport_write(const unsigned char* data, unsigned int size); - -int serialport_send_slip(unsigned char *data, unsigned int size); -int serialport_receive_slip(unsigned char *data, unsigned int size); -int serialport_send_C0(void); -int serialport_receive_C0(void); - -#endif