└── src ├── CMakeLists.txt ├── ch_9.h ├── candle_ctrl_req.h ├── candle_defs.h ├── candle_ctrl_req.c ├── candle.h ├── LICENSE └── candle.c /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(candle_api) 4 | 5 | add_library(candle_api SHARED 6 | candle.c 7 | candle_ctrl_req.c 8 | ) 9 | 10 | add_definitions(-DCANDLE_API_LIBRARY -DUNICODE) 11 | set_target_properties(candle_api PROPERTIES PREFIX "") 12 | 13 | target_link_libraries(candle_api 14 | SetupApi 15 | winusb 16 | Ole32 17 | ) 18 | 19 | -------------------------------------------------------------------------------- /src/ch_9.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016 Hubert Denkmair 4 | 5 | This file is part of the candle windows API. 6 | 7 | This library is free software: you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation, either 10 | version 3 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library. If not, see . 19 | 20 | */ 21 | 22 | #pragma once 23 | 24 | #define USB_DIR_OUT 0 /* to device */ 25 | #define USB_DIR_IN 0x80 /* to host */ 26 | 27 | #define USB_TYPE_MASK (0x03 << 5) 28 | #define USB_TYPE_STANDARD (0x00 << 5) 29 | #define USB_TYPE_CLASS (0x01 << 5) 30 | #define USB_TYPE_VENDOR (0x02 << 5) 31 | #define USB_TYPE_RESERVED (0x03 << 5) 32 | 33 | #define USB_RECIP_MASK 0x1f 34 | #define USB_RECIP_DEVICE 0x00 35 | #define USB_RECIP_INTERFACE 0x01 36 | #define USB_RECIP_ENDPOINT 0x02 37 | #define USB_RECIP_OTHER 0x03 38 | -------------------------------------------------------------------------------- /src/candle_ctrl_req.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016 Hubert Denkmair 4 | 5 | This file is part of the candle windows API. 6 | 7 | This library is free software: you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation, either 10 | version 3 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library. If not, see . 19 | 20 | */ 21 | 22 | #pragma once 23 | 24 | #include "candle_defs.h" 25 | 26 | enum { 27 | CANDLE_DEVMODE_RESET = 0, 28 | CANDLE_DEVMODE_START = 1 29 | }; 30 | 31 | bool candle_ctrl_set_host_format(candle_device_t *dev); 32 | bool candle_ctrl_set_timestamp_mode(candle_device_t *dev, bool enable_timestamps); 33 | bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags); 34 | bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf); 35 | bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data); 36 | bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data); 37 | bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp); 38 | 39 | -------------------------------------------------------------------------------- /src/candle_defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016 Hubert Denkmair 4 | 5 | This file is part of the candle windows API. 6 | 7 | This library is free software: you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation, either 10 | version 3 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library. If not, see . 19 | 20 | */ 21 | 22 | #pragma once 23 | 24 | #include 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #undef __CRT__NO_INLINE 33 | #include 34 | #define __CRT__NO_INLINE 35 | 36 | #include "candle.h" 37 | 38 | #define CANDLE_MAX_DEVICES 32 39 | #define CANDLE_URB_COUNT 30 40 | 41 | #pragma pack(push,1) 42 | 43 | typedef struct { 44 | uint32_t byte_order; 45 | } candle_host_config_t; 46 | 47 | typedef struct { 48 | uint8_t reserved1; 49 | uint8_t reserved2; 50 | uint8_t reserved3; 51 | uint8_t icount; 52 | uint32_t sw_version; 53 | uint32_t hw_version; 54 | } candle_device_config_t; 55 | 56 | typedef struct { 57 | uint32_t mode; 58 | uint32_t flags; 59 | } candle_device_mode_t; 60 | 61 | #pragma pack(pop) 62 | 63 | 64 | typedef struct { 65 | OVERLAPPED ovl; 66 | uint8_t buf[64]; 67 | } canlde_rx_urb; 68 | 69 | typedef struct { 70 | wchar_t path[256]; 71 | candle_devstate_t state; 72 | candle_err_t last_error; 73 | 74 | HANDLE deviceHandle; 75 | WINUSB_INTERFACE_HANDLE winUSBHandle; 76 | UCHAR interfaceNumber; 77 | UCHAR bulkInPipe; 78 | UCHAR bulkOutPipe; 79 | 80 | candle_device_config_t dconf; 81 | candle_capability_t bt_const; 82 | canlde_rx_urb rxurbs[CANDLE_URB_COUNT]; 83 | HANDLE rxevents[CANDLE_URB_COUNT]; 84 | } candle_device_t; 85 | 86 | typedef struct { 87 | uint8_t num_devices; 88 | candle_err_t last_error; 89 | candle_device_t dev[CANDLE_MAX_DEVICES]; 90 | } candle_list_t; 91 | -------------------------------------------------------------------------------- /src/candle_ctrl_req.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016 Hubert Denkmair 4 | 5 | This file is part of the candle windows API. 6 | 7 | This library is free software: you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation, either 10 | version 3 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library. If not, see . 19 | 20 | */ 21 | 22 | #include "candle_ctrl_req.h" 23 | #include "ch_9.h" 24 | 25 | enum { 26 | CANDLE_BREQ_HOST_FORMAT = 0, 27 | CANDLE_BREQ_BITTIMING, 28 | CANDLE_BREQ_MODE, 29 | CANDLE_BREQ_BERR, 30 | CANDLE_BREQ_BT_CONST, 31 | CANDLE_BREQ_DEVICE_CONFIG, 32 | CANDLE_TIMESTAMP_GET = 0x40, 33 | CANDLE_TIMESTAMP_ENABLE = 0x41, 34 | }; 35 | 36 | 37 | static bool usb_control_msg(WINUSB_INTERFACE_HANDLE hnd, uint8_t request, uint8_t requesttype, uint16_t value, uint16_t index, void *data, uint16_t size) 38 | { 39 | WINUSB_SETUP_PACKET packet; 40 | memset(&packet, 0, sizeof(packet)); 41 | 42 | packet.Request = request; 43 | packet.RequestType = requesttype; 44 | packet.Value = value; 45 | packet.Index = index; 46 | packet.Length = size; 47 | 48 | unsigned long bytes_sent = 0; 49 | return WinUsb_ControlTransfer(hnd, packet, (uint8_t*)data, size, &bytes_sent, 0); 50 | } 51 | 52 | bool candle_ctrl_set_host_format(candle_device_t *dev) 53 | { 54 | candle_host_config_t hconf; 55 | hconf.byte_order = 0x0000beef; 56 | 57 | bool rc = usb_control_msg( 58 | dev->winUSBHandle, 59 | CANDLE_BREQ_HOST_FORMAT, 60 | USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 61 | 1, 62 | dev->interfaceNumber, 63 | &hconf, 64 | sizeof(hconf) 65 | ); 66 | 67 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_HOST_FORMAT; 68 | return rc; 69 | } 70 | 71 | bool candle_ctrl_set_timestamp_mode(candle_device_t *dev, bool enable_timestamps) 72 | { 73 | uint32_t ts_config = enable_timestamps ? 1 : 0; 74 | 75 | bool rc = usb_control_msg( 76 | dev->winUSBHandle, 77 | CANDLE_TIMESTAMP_ENABLE, 78 | USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 79 | 1, 80 | ts_config, 81 | NULL, 82 | 0 83 | ); 84 | 85 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_TIMESTAMP_MODE; 86 | return rc; 87 | } 88 | 89 | bool candle_ctrl_set_device_mode(candle_device_t *dev, uint8_t channel, uint32_t mode, uint32_t flags) 90 | { 91 | candle_device_mode_t dm; 92 | dm.mode = mode; 93 | dm.flags = flags; 94 | 95 | bool rc = usb_control_msg( 96 | dev->winUSBHandle, 97 | CANDLE_BREQ_MODE, 98 | USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 99 | channel, 100 | dev->interfaceNumber, 101 | &dm, 102 | sizeof(dm) 103 | ); 104 | 105 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_DEVICE_MODE; 106 | return rc; 107 | } 108 | 109 | 110 | bool candle_ctrl_get_config(candle_device_t *dev, candle_device_config_t *dconf) 111 | { 112 | bool rc = usb_control_msg( 113 | dev->winUSBHandle, 114 | CANDLE_BREQ_DEVICE_CONFIG, 115 | USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 116 | 1, 117 | dev->interfaceNumber, 118 | dconf, 119 | sizeof(*dconf) 120 | ); 121 | 122 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_DEVICE_INFO; 123 | return rc; 124 | } 125 | 126 | bool candle_ctrl_get_timestamp(candle_device_t *dev, uint32_t *current_timestamp) 127 | { 128 | bool rc = usb_control_msg( 129 | dev->winUSBHandle, 130 | CANDLE_TIMESTAMP_GET, 131 | USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 132 | 1, 133 | dev->interfaceNumber, 134 | current_timestamp, 135 | sizeof(*current_timestamp) 136 | ); 137 | 138 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_TIMESTAMP; 139 | return rc; 140 | } 141 | 142 | bool candle_ctrl_get_capability(candle_device_t *dev, uint8_t channel, candle_capability_t *data) 143 | { 144 | bool rc = usb_control_msg( 145 | dev->winUSBHandle, 146 | CANDLE_BREQ_BT_CONST, 147 | USB_DIR_IN|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 148 | channel, 149 | 0, 150 | data, 151 | sizeof(*data) 152 | ); 153 | 154 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_GET_BITTIMING_CONST; 155 | return rc; 156 | } 157 | 158 | bool candle_ctrl_set_bittiming(candle_device_t *dev, uint8_t channel, candle_bittiming_t *data) 159 | { 160 | bool rc = usb_control_msg( 161 | dev->winUSBHandle, 162 | CANDLE_BREQ_BITTIMING, 163 | USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_INTERFACE, 164 | channel, 165 | 0, 166 | data, 167 | sizeof(*data) 168 | ); 169 | 170 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SET_BITTIMING; 171 | return rc; 172 | } 173 | -------------------------------------------------------------------------------- /src/candle.h: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016 Hubert Denkmair 4 | 5 | This file is part of the candle windows API. 6 | 7 | This library is free software: you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation, either 10 | version 3 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library. If not, see . 19 | 20 | */ 21 | 22 | #pragma once 23 | #include 24 | #include 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | typedef void* candle_list_handle; 31 | typedef void* candle_handle; 32 | 33 | typedef enum { 34 | CANDLE_DEVSTATE_AVAIL, 35 | CANDLE_DEVSTATE_INUSE 36 | } candle_devstate_t; 37 | 38 | typedef enum { 39 | CANDLE_FRAMETYPE_UNKNOWN, 40 | CANDLE_FRAMETYPE_RECEIVE, 41 | CANDLE_FRAMETYPE_ECHO, 42 | CANDLE_FRAMETYPE_ERROR, 43 | CANDLE_FRAMETYPE_TIMESTAMP_OVFL 44 | } candle_frametype_t; 45 | 46 | typedef enum { 47 | CANDLE_MODE_NORMAL = 0x00, 48 | CANDLE_MODE_LISTEN_ONLY = 0x01, 49 | CANDLE_MODE_LOOP_BACK = 0x02, 50 | CANDLE_MODE_TRIPLE_SAMPLE = 0x04, 51 | CANDLE_MODE_ONE_SHOT = 0x08 52 | } candle_mode_t; 53 | 54 | typedef enum { 55 | CANDLE_ERR_OK = 0, 56 | CANDLE_ERR_CREATE_FILE = 1, 57 | CANDLE_ERR_WINUSB_INITIALIZE = 2, 58 | CANDLE_ERR_QUERY_INTERFACE = 3, 59 | CANDLE_ERR_QUERY_PIPE = 4, 60 | CANDLE_ERR_PARSE_IF_DESCR = 5, 61 | CANDLE_ERR_SET_HOST_FORMAT = 6, 62 | CANDLE_ERR_GET_DEVICE_INFO = 7, 63 | CANDLE_ERR_GET_BITTIMING_CONST = 8, 64 | CANDLE_ERR_PREPARE_READ = 9, 65 | CANDLE_ERR_SET_DEVICE_MODE = 10, 66 | CANDLE_ERR_SET_BITTIMING = 11, 67 | CANDLE_ERR_BITRATE_FCLK = 12, 68 | CANDLE_ERR_BITRATE_UNSUPPORTED = 13, 69 | CANDLE_ERR_SEND_FRAME = 14, 70 | CANDLE_ERR_READ_TIMEOUT = 15, 71 | CANDLE_ERR_READ_WAIT = 16, 72 | CANDLE_ERR_READ_RESULT = 17, 73 | CANDLE_ERR_READ_SIZE = 18, 74 | CANDLE_ERR_SETUPDI_IF_DETAILS = 19, 75 | CANDLE_ERR_SETUPDI_IF_DETAILS2 = 20, 76 | CANDLE_ERR_MALLOC = 21, 77 | CANDLE_ERR_PATH_LEN = 22, 78 | CANDLE_ERR_CLSID = 23, 79 | CANDLE_ERR_GET_DEVICES = 24, 80 | CANDLE_ERR_SETUPDI_IF_ENUM = 25, 81 | CANDLE_ERR_SET_TIMESTAMP_MODE = 26, 82 | CANDLE_ERR_DEV_OUT_OF_RANGE = 27, 83 | CANDLE_ERR_GET_TIMESTAMP = 28, 84 | CANDLE_ERR_SET_PIPE_RAW_IO = 29, 85 | } candle_err_t; 86 | 87 | #pragma pack(push,1) 88 | 89 | typedef struct { 90 | uint32_t echo_id; 91 | uint32_t can_id; 92 | uint8_t can_dlc; 93 | uint8_t channel; 94 | uint8_t flags; 95 | uint8_t reserved; 96 | uint8_t data[8]; 97 | uint32_t timestamp_us; 98 | } candle_frame_t; 99 | 100 | typedef struct { 101 | uint32_t feature; 102 | uint32_t fclk_can; 103 | uint32_t tseg1_min; 104 | uint32_t tseg1_max; 105 | uint32_t tseg2_min; 106 | uint32_t tseg2_max; 107 | uint32_t sjw_max; 108 | uint32_t brp_min; 109 | uint32_t brp_max; 110 | uint32_t brp_inc; 111 | } candle_capability_t; 112 | 113 | typedef struct { 114 | uint32_t prop_seg; 115 | uint32_t phase_seg1; 116 | uint32_t phase_seg2; 117 | uint32_t sjw; 118 | uint32_t brp; 119 | } candle_bittiming_t; 120 | 121 | #pragma pack(pop) 122 | 123 | 124 | #ifdef CANDLE_API_LIBRARY 125 | #define DLL __declspec(dllexport) 126 | #else 127 | #define DLL __declspec(dllimport) 128 | #endif 129 | 130 | DLL bool __stdcall candle_list_scan(candle_list_handle *list); 131 | DLL bool __stdcall candle_list_free(candle_list_handle list); 132 | DLL bool __stdcall candle_list_length(candle_list_handle list, uint8_t *len); 133 | 134 | DLL bool __stdcall candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev); 135 | DLL bool __stdcall candle_dev_get_state(candle_handle hdev, candle_devstate_t *state); 136 | DLL wchar_t* __stdcall candle_dev_get_path(candle_handle hdev); 137 | DLL bool __stdcall candle_dev_open(candle_handle hdev); 138 | DLL bool __stdcall candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us); 139 | DLL bool __stdcall candle_dev_close(candle_handle hdev); 140 | DLL bool __stdcall candle_dev_free(candle_handle hdev); 141 | 142 | DLL bool __stdcall candle_channel_count(candle_handle hdev, uint8_t *num_channels); 143 | DLL bool __stdcall candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap); 144 | DLL bool __stdcall candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data); 145 | DLL bool __stdcall candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate); 146 | DLL bool __stdcall candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags); 147 | DLL bool __stdcall candle_channel_stop(candle_handle hdev, uint8_t ch); 148 | 149 | DLL bool __stdcall candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame); 150 | DLL bool __stdcall candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms); 151 | 152 | DLL candle_frametype_t __stdcall candle_frame_type(candle_frame_t *frame); 153 | DLL uint32_t __stdcall candle_frame_id(candle_frame_t *frame); 154 | DLL bool __stdcall candle_frame_is_extended_id(candle_frame_t *frame); 155 | DLL bool __stdcall candle_frame_is_rtr(candle_frame_t *frame); 156 | DLL uint8_t __stdcall candle_frame_dlc(candle_frame_t *frame); 157 | DLL uint8_t* __stdcall candle_frame_data(candle_frame_t *frame); 158 | DLL uint32_t __stdcall candle_frame_timestamp_us(candle_frame_t *frame); 159 | 160 | DLL candle_err_t __stdcall candle_dev_last_error(candle_handle hdev); 161 | 162 | #ifdef __cplusplus 163 | } 164 | #endif 165 | -------------------------------------------------------------------------------- /src/LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | 3 | Version 3, 29 June 2007 4 | 5 | Copyright © 2007 Free Software Foundation, Inc. 6 | 7 | Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. 8 | 9 | This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 10 | 0. Additional Definitions. 11 | 12 | As used herein, “this License” refers to version 3 of the GNU Lesser General Public License, and the “GNU GPL” refers to version 3 of the GNU General Public License. 13 | 14 | “The Library” refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. 15 | 16 | An “Application” is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. 17 | 18 | A “Combined Work” is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the “Linked Version”. 19 | 20 | The “Minimal Corresponding Source” for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. 21 | 22 | The “Corresponding Application Code” for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 23 | 1. Exception to Section 3 of the GNU GPL. 24 | 25 | You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 26 | 2. Conveying Modified Versions. 27 | 28 | If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: 29 | 30 | a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or 31 | b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 32 | 33 | 3. Object Code Incorporating Material from Library Header Files. 34 | 35 | The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: 36 | 37 | a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. 38 | b) Accompany the object code with a copy of the GNU GPL and this license document. 39 | 40 | 4. Combined Works. 41 | 42 | You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: 43 | 44 | a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. 45 | b) Accompany the Combined Work with a copy of the GNU GPL and this license document. 46 | c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. 47 | d) Do one of the following: 48 | 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 49 | 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. 50 | e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 51 | 52 | 5. Combined Libraries. 53 | 54 | You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: 55 | 56 | a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. 57 | b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 58 | 59 | 6. Revised Versions of the GNU Lesser General Public License. 60 | 61 | The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. 62 | 63 | Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License “or any later version” applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. 64 | 65 | If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. 66 | 67 | -------------------------------------------------------------------------------- /src/candle.c: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Copyright (c) 2016 Hubert Denkmair 4 | 5 | This file is part of the candle windows API. 6 | 7 | This library is free software: you can redistribute it and/or 8 | modify it under the terms of the GNU Lesser General Public 9 | License as published by the Free Software Foundation, either 10 | version 3 of the License, or (at your option) any later version. 11 | 12 | This library is distributed in the hope that it will be useful, 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 | Lesser General Public License for more details. 16 | 17 | You should have received a copy of the GNU Lesser General Public 18 | License along with this library. If not, see . 19 | 20 | */ 21 | 22 | #include "candle.h" 23 | #include 24 | 25 | #include "candle_defs.h" 26 | #include "candle_ctrl_req.h" 27 | #include "ch_9.h" 28 | 29 | static bool candle_read_di(HDEVINFO hdi, SP_DEVICE_INTERFACE_DATA interfaceData, candle_device_t *dev) 30 | { 31 | /* get required length first (this call always fails with an error) */ 32 | ULONG requiredLength=0; 33 | SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, NULL, 0, &requiredLength, NULL); 34 | if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) { 35 | dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS; 36 | return false; 37 | } 38 | 39 | PSP_DEVICE_INTERFACE_DETAIL_DATA detail_data = 40 | (PSP_DEVICE_INTERFACE_DETAIL_DATA) LocalAlloc(LMEM_FIXED, requiredLength); 41 | 42 | if (detail_data != NULL) { 43 | detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA); 44 | } else { 45 | dev->last_error = CANDLE_ERR_MALLOC; 46 | return false; 47 | } 48 | 49 | bool retval = true; 50 | ULONG length = requiredLength; 51 | if (!SetupDiGetDeviceInterfaceDetail(hdi, &interfaceData, detail_data, length, &requiredLength, NULL) ) { 52 | dev->last_error = CANDLE_ERR_SETUPDI_IF_DETAILS2; 53 | retval = false; 54 | } else if (FAILED(StringCchCopy(dev->path, sizeof(dev->path), detail_data->DevicePath))) { 55 | dev->last_error = CANDLE_ERR_PATH_LEN; 56 | retval = false; 57 | } 58 | 59 | LocalFree(detail_data); 60 | 61 | if (!retval) { 62 | return false; 63 | } 64 | 65 | /* try to open to read device infos and see if it is avail */ 66 | if (candle_dev_open(dev)) { 67 | dev->state = CANDLE_DEVSTATE_AVAIL; 68 | candle_dev_close(dev); 69 | } else { 70 | dev->state = CANDLE_DEVSTATE_INUSE; 71 | } 72 | 73 | dev->last_error = CANDLE_ERR_OK; 74 | return true; 75 | } 76 | 77 | bool __stdcall candle_list_scan(candle_list_handle *list) 78 | { 79 | if (list==NULL) { 80 | return false; 81 | } 82 | 83 | candle_list_t *l = (candle_list_t *)calloc(1, sizeof(candle_list_t)); 84 | *list = l; 85 | if (l==NULL) { 86 | return false; 87 | } 88 | 89 | GUID guid; 90 | if (CLSIDFromString(L"{c15b4308-04d3-11e6-b3ea-6057189e6443}", &guid) != NOERROR) { 91 | l->last_error = CANDLE_ERR_CLSID; 92 | return false; 93 | } 94 | 95 | HDEVINFO hdi = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); 96 | if (hdi == INVALID_HANDLE_VALUE) { 97 | l->last_error = CANDLE_ERR_GET_DEVICES; 98 | return false; 99 | } 100 | 101 | bool rv = false; 102 | for (unsigned i=0; idev[i])) { 110 | l->last_error = l->dev[i].last_error; 111 | rv = false; 112 | break; 113 | } 114 | 115 | } else { 116 | 117 | DWORD err = GetLastError(); 118 | if (err==ERROR_NO_MORE_ITEMS) { 119 | l->num_devices = i; 120 | l->last_error = CANDLE_ERR_OK; 121 | rv = true; 122 | } else { 123 | l->last_error = CANDLE_ERR_SETUPDI_IF_ENUM; 124 | rv = false; 125 | } 126 | break; 127 | 128 | } 129 | 130 | } 131 | 132 | SetupDiDestroyDeviceInfoList(hdi); 133 | 134 | return rv; 135 | 136 | } 137 | 138 | DLL bool __stdcall candle_list_free(candle_list_handle list) 139 | { 140 | free(list); 141 | return true; 142 | } 143 | 144 | DLL bool __stdcall candle_list_length(candle_list_handle list, uint8_t *len) 145 | { 146 | candle_list_t *l = (candle_list_t *)list; 147 | *len = l->num_devices; 148 | return true; 149 | } 150 | 151 | DLL bool __stdcall candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev) 152 | { 153 | candle_list_t *l = (candle_list_t *)list; 154 | if (l==NULL) { 155 | return false; 156 | } 157 | 158 | if (dev_num >= CANDLE_MAX_DEVICES) { 159 | l->last_error = CANDLE_ERR_DEV_OUT_OF_RANGE; 160 | return false; 161 | } 162 | 163 | candle_device_t *dev = calloc(1, sizeof(candle_device_t)); 164 | *hdev = dev; 165 | if (dev==NULL) { 166 | l->last_error = CANDLE_ERR_MALLOC; 167 | return false; 168 | } 169 | 170 | memcpy(dev, &l->dev[dev_num], sizeof(candle_device_t)); 171 | l->last_error = CANDLE_ERR_OK; 172 | dev->last_error = CANDLE_ERR_OK; 173 | return true; 174 | } 175 | 176 | 177 | DLL bool __stdcall candle_dev_get_state(candle_handle hdev, candle_devstate_t *state) 178 | { 179 | if (hdev==NULL) { 180 | return false; 181 | } else { 182 | candle_device_t *dev = (candle_device_t*)hdev; 183 | *state = dev->state; 184 | return true; 185 | } 186 | } 187 | 188 | DLL wchar_t* __stdcall candle_dev_get_path(candle_handle hdev) 189 | { 190 | if (hdev==NULL) { 191 | return NULL; 192 | } else { 193 | candle_device_t *dev = (candle_device_t*)hdev; 194 | return dev->path; 195 | } 196 | } 197 | 198 | static bool candle_dev_interal_open(candle_handle hdev) 199 | { 200 | candle_device_t *dev = (candle_device_t*)hdev; 201 | 202 | memset(dev->rxevents, 0, sizeof(dev->rxevents)); 203 | memset(dev->rxurbs, 0, sizeof(dev->rxurbs)); 204 | 205 | dev->deviceHandle = CreateFile( 206 | dev->path, 207 | GENERIC_WRITE | GENERIC_READ, 208 | FILE_SHARE_WRITE | FILE_SHARE_READ, 209 | NULL, 210 | OPEN_EXISTING, 211 | FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 212 | NULL 213 | ); 214 | 215 | if (dev->deviceHandle == INVALID_HANDLE_VALUE) { 216 | dev->last_error = CANDLE_ERR_CREATE_FILE; 217 | return false; 218 | } 219 | 220 | if (!WinUsb_Initialize(dev->deviceHandle, &dev->winUSBHandle)) { 221 | dev->last_error = CANDLE_ERR_WINUSB_INITIALIZE; 222 | goto close_handle; 223 | } 224 | 225 | USB_INTERFACE_DESCRIPTOR ifaceDescriptor; 226 | if (!WinUsb_QueryInterfaceSettings(dev->winUSBHandle, 0, &ifaceDescriptor)) { 227 | dev->last_error = CANDLE_ERR_QUERY_INTERFACE; 228 | goto winusb_free; 229 | } 230 | 231 | dev->interfaceNumber = ifaceDescriptor.bInterfaceNumber; 232 | unsigned pipes_found = 0; 233 | 234 | for (uint8_t i=0; iwinUSBHandle, 0, i, &pipeInfo)) { 238 | dev->last_error = CANDLE_ERR_QUERY_PIPE; 239 | goto winusb_free; 240 | } 241 | 242 | if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_IN(pipeInfo.PipeId)) { 243 | dev->bulkInPipe = pipeInfo.PipeId; 244 | pipes_found++; 245 | } else if (pipeInfo.PipeType == UsbdPipeTypeBulk && USB_ENDPOINT_DIRECTION_OUT(pipeInfo.PipeId)) { 246 | dev->bulkOutPipe = pipeInfo.PipeId; 247 | pipes_found++; 248 | } else { 249 | dev->last_error = CANDLE_ERR_PARSE_IF_DESCR; 250 | goto winusb_free; 251 | } 252 | 253 | } 254 | 255 | if (pipes_found != 2) { 256 | dev->last_error = CANDLE_ERR_PARSE_IF_DESCR; 257 | goto winusb_free; 258 | } 259 | 260 | char use_raw_io = 1; 261 | if (!WinUsb_SetPipePolicy(dev->winUSBHandle, dev->bulkInPipe, RAW_IO, sizeof(use_raw_io), &use_raw_io)) { 262 | dev->last_error = CANDLE_ERR_SET_PIPE_RAW_IO; 263 | goto winusb_free; 264 | } 265 | 266 | if (!candle_ctrl_set_host_format(dev)) { 267 | goto winusb_free; 268 | } 269 | 270 | if (!candle_ctrl_set_timestamp_mode(dev, true)) { 271 | goto winusb_free; 272 | } 273 | 274 | if (!candle_ctrl_get_config(dev, &dev->dconf)) { 275 | goto winusb_free; 276 | } 277 | 278 | if (!candle_ctrl_get_capability(dev, 0, &dev->bt_const)) { 279 | dev->last_error = CANDLE_ERR_GET_BITTIMING_CONST; 280 | goto winusb_free; 281 | } 282 | 283 | dev->last_error = CANDLE_ERR_OK; 284 | return true; 285 | 286 | winusb_free: 287 | WinUsb_Free(dev->winUSBHandle); 288 | 289 | close_handle: 290 | CloseHandle(dev->deviceHandle); 291 | return false; 292 | 293 | } 294 | 295 | static bool candle_prepare_read(candle_device_t *dev, unsigned urb_num) 296 | { 297 | bool rc = WinUsb_ReadPipe( 298 | dev->winUSBHandle, 299 | dev->bulkInPipe, 300 | dev->rxurbs[urb_num].buf, 301 | sizeof(dev->rxurbs[urb_num].buf), 302 | NULL, 303 | &dev->rxurbs[urb_num].ovl 304 | ); 305 | 306 | if (rc || (GetLastError()!=ERROR_IO_PENDING)) { 307 | dev->last_error = CANDLE_ERR_PREPARE_READ; 308 | return false; 309 | } else { 310 | dev->last_error = CANDLE_ERR_OK; 311 | return true; 312 | } 313 | } 314 | 315 | static bool candle_close_rxurbs(candle_device_t *dev) 316 | { 317 | for (unsigned i=0; irxevents[i] != NULL) { 319 | CloseHandle(dev->rxevents[i]); 320 | } 321 | } 322 | return true; 323 | } 324 | 325 | 326 | DLL bool __stdcall candle_dev_open(candle_handle hdev) 327 | { 328 | candle_device_t *dev = (candle_device_t*)hdev; 329 | 330 | if (candle_dev_interal_open(dev)) { 331 | for (unsigned i=0; irxevents[i] = ev; 334 | dev->rxurbs[i].ovl.hEvent = ev; 335 | if (!candle_prepare_read(dev, i)) { 336 | candle_close_rxurbs(dev); 337 | return false; // keep last_error from prepare_read call 338 | } 339 | } 340 | dev->last_error = CANDLE_ERR_OK; 341 | return true; 342 | } else { 343 | return false; // keep last_error from open_device call 344 | } 345 | 346 | } 347 | 348 | DLL bool __stdcall candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us) 349 | { 350 | return candle_ctrl_get_timestamp(hdev, timestamp_us); 351 | } 352 | 353 | DLL bool __stdcall candle_dev_close(candle_handle hdev) 354 | { 355 | candle_device_t *dev = (candle_device_t*)hdev; 356 | 357 | candle_close_rxurbs(dev); 358 | 359 | WinUsb_Free(dev->winUSBHandle); 360 | dev->winUSBHandle = NULL; 361 | CloseHandle(dev->deviceHandle); 362 | dev->deviceHandle = NULL; 363 | 364 | dev->last_error = CANDLE_ERR_OK; 365 | return true; 366 | } 367 | 368 | DLL bool __stdcall candle_dev_free(candle_handle hdev) 369 | { 370 | free(hdev); 371 | return true; 372 | } 373 | 374 | DLL candle_err_t __stdcall candle_dev_last_error(candle_handle hdev) 375 | { 376 | candle_device_t *dev = (candle_device_t*)hdev; 377 | return dev->last_error; 378 | } 379 | 380 | DLL bool __stdcall candle_channel_count(candle_handle hdev, uint8_t *num_channels) 381 | { 382 | // TODO check if info was already read from device; try to do so; throw error... 383 | candle_device_t *dev = (candle_device_t*)hdev; 384 | *num_channels = dev->dconf.icount+1; 385 | return true; 386 | } 387 | 388 | DLL bool __stdcall candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap) 389 | { 390 | // TODO check if info was already read from device; try to do so; throw error... 391 | candle_device_t *dev = (candle_device_t*)hdev; 392 | memcpy(cap, &dev->bt_const, sizeof(candle_capability_t)); 393 | return true; 394 | } 395 | 396 | DLL bool __stdcall candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data) 397 | { 398 | // TODO ensure device is open, check channel count.. 399 | candle_device_t *dev = (candle_device_t*)hdev; 400 | return candle_ctrl_set_bittiming(dev, ch, data); 401 | } 402 | 403 | DLL bool __stdcall candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate) 404 | { 405 | // TODO ensure device is open, check channel count.. 406 | candle_device_t *dev = (candle_device_t*)hdev; 407 | 408 | if (dev->bt_const.fclk_can != 48000000) { 409 | /* this function only works for the candleLight base clock of 48MHz */ 410 | dev->last_error = CANDLE_ERR_BITRATE_FCLK; 411 | return false; 412 | } 413 | 414 | candle_bittiming_t t; 415 | t.prop_seg = 1; 416 | t.sjw = 1; 417 | t.phase_seg1 = 13 - t.prop_seg; 418 | t.phase_seg2 = 2; 419 | 420 | switch (bitrate) { 421 | case 10000: 422 | t.brp = 300; 423 | break; 424 | 425 | case 20000: 426 | t.brp = 150; 427 | break; 428 | 429 | case 50000: 430 | t.brp = 60; 431 | break; 432 | 433 | case 83333: 434 | t.brp = 36; 435 | break; 436 | 437 | case 100000: 438 | t.brp = 30; 439 | break; 440 | 441 | case 125000: 442 | t.brp = 24; 443 | break; 444 | 445 | case 250000: 446 | t.brp = 12; 447 | break; 448 | 449 | case 500000: 450 | t.brp = 6; 451 | break; 452 | 453 | case 800000: 454 | t.brp = 4; 455 | t.phase_seg1 = 12 - t.prop_seg; 456 | t.phase_seg2 = 2; 457 | break; 458 | 459 | case 1000000: 460 | t.brp = 3; 461 | break; 462 | 463 | default: 464 | dev->last_error = CANDLE_ERR_BITRATE_UNSUPPORTED; 465 | return false; 466 | } 467 | 468 | return candle_ctrl_set_bittiming(dev, ch, &t); 469 | } 470 | 471 | DLL bool __stdcall candle_channel_start(candle_handle hdev, uint8_t ch, uint32_t flags) 472 | { 473 | // TODO ensure device is open, check channel count.. 474 | candle_device_t *dev = (candle_device_t*)hdev; 475 | return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_START, flags); 476 | } 477 | 478 | DLL bool __stdcall candle_channel_stop(candle_handle hdev, uint8_t ch) 479 | { 480 | // TODO ensure device is open, check channel count.. 481 | candle_device_t *dev = (candle_device_t*)hdev; 482 | return candle_ctrl_set_device_mode(dev, ch, CANDLE_DEVMODE_RESET, 0); 483 | } 484 | 485 | DLL bool __stdcall candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame) 486 | { 487 | // TODO ensure device is open, check channel count.. 488 | candle_device_t *dev = (candle_device_t*)hdev; 489 | 490 | unsigned long bytes_sent = 0; 491 | 492 | frame->echo_id = 0; 493 | frame->channel = ch; 494 | 495 | bool rc = WinUsb_WritePipe( 496 | dev->winUSBHandle, 497 | dev->bulkOutPipe, 498 | (uint8_t*)frame, 499 | sizeof(*frame), 500 | &bytes_sent, 501 | 0 502 | ); 503 | 504 | dev->last_error = rc ? CANDLE_ERR_OK : CANDLE_ERR_SEND_FRAME; 505 | return rc; 506 | 507 | } 508 | 509 | DLL bool __stdcall candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms) 510 | { 511 | // TODO ensure device is open.. 512 | candle_device_t *dev = (candle_device_t*)hdev; 513 | 514 | DWORD wait_result = WaitForMultipleObjects(CANDLE_URB_COUNT, dev->rxevents, false, timeout_ms); 515 | if (wait_result == WAIT_TIMEOUT) { 516 | dev->last_error = CANDLE_ERR_READ_TIMEOUT; 517 | return false; 518 | } 519 | 520 | if ( (wait_result < WAIT_OBJECT_0) || (wait_result >= WAIT_OBJECT_0 + CANDLE_URB_COUNT) ) { 521 | dev->last_error = CANDLE_ERR_READ_WAIT; 522 | return false; 523 | } 524 | 525 | DWORD urb_num = wait_result - WAIT_OBJECT_0; 526 | DWORD bytes_transfered; 527 | 528 | if (!WinUsb_GetOverlappedResult(dev->winUSBHandle, &dev->rxurbs[urb_num].ovl, &bytes_transfered, false)) { 529 | candle_prepare_read(dev, urb_num); 530 | dev->last_error = CANDLE_ERR_READ_RESULT; 531 | return false; 532 | } 533 | 534 | if (bytes_transfered != sizeof(*frame)) { 535 | candle_prepare_read(dev, urb_num); 536 | dev->last_error = CANDLE_ERR_READ_SIZE; 537 | return false; 538 | } 539 | 540 | memcpy(frame, dev->rxurbs[urb_num].buf, sizeof(*frame)); 541 | 542 | return candle_prepare_read(dev, urb_num); 543 | } 544 | 545 | DLL candle_frametype_t __stdcall candle_frame_type(candle_frame_t *frame) 546 | { 547 | if (frame->echo_id != 0xFFFFFFFF) { 548 | return CANDLE_FRAMETYPE_ECHO; 549 | }; 550 | 551 | if (frame->can_id & 0x20000000) { 552 | return CANDLE_FRAMETYPE_ERROR; 553 | } 554 | 555 | return CANDLE_FRAMETYPE_RECEIVE; 556 | } 557 | 558 | DLL uint32_t __stdcall candle_frame_id(candle_frame_t *frame) 559 | { 560 | return frame->can_id & 0x1FFFFFFF; 561 | } 562 | 563 | DLL bool __stdcall candle_frame_is_extended_id(candle_frame_t *frame) 564 | { 565 | return (frame->can_id & 0x80000000) != 0; 566 | } 567 | 568 | DLL bool __stdcall candle_frame_is_rtr(candle_frame_t *frame) 569 | { 570 | return (frame->can_id & 0x40000000) != 0; 571 | } 572 | 573 | DLL uint8_t __stdcall candle_frame_dlc(candle_frame_t *frame) 574 | { 575 | return frame->can_dlc; 576 | } 577 | 578 | DLL uint8_t* __stdcall candle_frame_data(candle_frame_t *frame) 579 | { 580 | return frame->data; 581 | } 582 | 583 | DLL uint32_t __stdcall candle_frame_timestamp_us(candle_frame_t *frame) 584 | { 585 | return frame->timestamp_us; 586 | } 587 | --------------------------------------------------------------------------------