|
| 1 | +/* |
| 2 | +
|
| 3 | + Copyright (c) 2016 Hubert Denkmair <hubert@denkmair.de> |
| 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 <http://www.gnu.org/licenses/>. |
| 19 | +
|
| 20 | +*/ |
| 21 | + |
| 22 | +#pragma once |
| 23 | +#include <stdint.h> |
| 24 | +#include <stdbool.h> |
| 25 | + |
| 26 | +#ifdef USING_WINDOWS_ERR_DECODE |
| 27 | +#include <windows.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +// Use overlapped IO on TX as well as RX |
| 31 | +// #define USING_OVERLAPPED_IO_ON_TX |
| 32 | + |
| 33 | +#ifdef __cplusplus |
| 34 | +extern "C" { |
| 35 | +#endif |
| 36 | + |
| 37 | +typedef void* candle_list_handle; |
| 38 | +typedef void* candle_handle; |
| 39 | + |
| 40 | +typedef enum { |
| 41 | + CANDLE_DEVSTATE_AVAIL, |
| 42 | + CANDLE_DEVSTATE_INUSE |
| 43 | +} candle_devstate_t; |
| 44 | + |
| 45 | +typedef enum { |
| 46 | + CANDLE_FRAMETYPE_UNKNOWN, |
| 47 | + CANDLE_FRAMETYPE_RECEIVE, |
| 48 | + CANDLE_FRAMETYPE_ECHO, |
| 49 | + CANDLE_FRAMETYPE_ERROR, |
| 50 | + CANDLE_FRAMETYPE_TIMESTAMP_OVFL |
| 51 | +} candle_frametype_t; |
| 52 | + |
| 53 | +typedef enum { |
| 54 | + CANDLE_MODE_NORMAL = 0x00, |
| 55 | + CANDLE_MODE_LISTEN_ONLY = 0x01, |
| 56 | + CANDLE_MODE_LOOP_BACK = 0x02, |
| 57 | + CANDLE_MODE_TRIPLE_SAMPLE = 0x04, |
| 58 | + CANDLE_MODE_ONE_SHOT = 0x08, |
| 59 | + CANDLE_MODE_HW_TIMESTAMP = 0x10, |
| 60 | + CANDLE_MODE_PAD_PKTS_TO_MAX_PKT_SIZE = 0x80 |
| 61 | +} candle_device_mode_flags_t; |
| 62 | + |
| 63 | +typedef enum { |
| 64 | + CANDLE_FEATURE_NORMAL = 0x00, |
| 65 | + CANDLE_FEATURE_LISTEN_ONLY = 0x01, |
| 66 | + CANDLE_FEATURE_LOOP_BACK = 0x02, |
| 67 | + CANDLE_FEATURE_TRIPLE_SAMPLE = 0x04, |
| 68 | + CANDLE_FEATURE_ONE_SHOT = 0x08, |
| 69 | + CANDLE_FEATURE_HW_TIMESTAMP = 0x10, |
| 70 | + CANDLE_FEATURE_IDENTIFY = 0x20, |
| 71 | + CANDLE_FEATURE_USER_ID = 0x40, |
| 72 | + CANDLE_FEATURE_PAD_PKTS_TO_MAX_PKT_SIZE = 0x80 |
| 73 | +} candle_feature_t; |
| 74 | + |
| 75 | +typedef enum { |
| 76 | + CANDLE_ERR_OK = 0, |
| 77 | + CANDLE_ERR_CREATE_FILE = 1, |
| 78 | + CANDLE_ERR_WINUSB_INITIALIZE = 2, |
| 79 | + CANDLE_ERR_QUERY_INTERFACE = 3, |
| 80 | + CANDLE_ERR_QUERY_PIPE = 4, |
| 81 | + CANDLE_ERR_PARSE_IF_DESCR = 5, |
| 82 | + CANDLE_ERR_SET_HOST_FORMAT = 6, |
| 83 | + CANDLE_ERR_GET_DEVICE_INFO = 7, |
| 84 | + CANDLE_ERR_GET_BITTIMING_CONST = 8, |
| 85 | + CANDLE_ERR_PREPARE_READ = 9, |
| 86 | + CANDLE_ERR_SET_DEVICE_MODE = 10, |
| 87 | + CANDLE_ERR_SET_BITTIMING = 11, |
| 88 | + CANDLE_ERR_BITRATE_FCLK = 12, |
| 89 | + CANDLE_ERR_BITRATE_UNSUPPORTED = 13, |
| 90 | + CANDLE_ERR_SEND_FRAME = 14, |
| 91 | + CANDLE_ERR_READ_TIMEOUT = 15, |
| 92 | + CANDLE_ERR_READ_WAIT = 16, |
| 93 | + CANDLE_ERR_READ_RESULT = 17, |
| 94 | + CANDLE_ERR_READ_SIZE = 18, |
| 95 | + CANDLE_ERR_SETUPDI_IF_DETAILS = 19, |
| 96 | + CANDLE_ERR_SETUPDI_IF_DETAILS2 = 20, |
| 97 | + CANDLE_ERR_MALLOC = 21, |
| 98 | + CANDLE_ERR_PATH_LEN = 22, |
| 99 | + CANDLE_ERR_CLSID = 23, |
| 100 | + CANDLE_ERR_GET_DEVICES = 24, |
| 101 | + CANDLE_ERR_SETUPDI_IF_ENUM = 25, |
| 102 | + CANDLE_ERR_SET_TIMESTAMP_MODE = 26, |
| 103 | + CANDLE_ERR_DEV_OUT_OF_RANGE = 27, |
| 104 | + CANDLE_ERR_GET_TIMESTAMP = 28, |
| 105 | + CANDLE_ERR_SET_PIPE_RAW_IO = 29, |
| 106 | + CANDLE_ERR_DEVICE_FEATURE_UNAVAILABLE = 30, |
| 107 | + CANDLE_ERR_WRITE_WAIT = 31, |
| 108 | + CANDLE_ERR_WRITE_TIMEOUT_PRE = 32, |
| 109 | + CANDLE_ERR_WRITE_TIMEOUT = 33, |
| 110 | + CANDLE_ERR_WRITE_RESULT = 34, |
| 111 | + CANDLE_ERR_WRITE_SIZE = 35, |
| 112 | +} candle_err_t; |
| 113 | + |
| 114 | +#pragma pack(push,1) |
| 115 | + |
| 116 | +typedef struct { |
| 117 | + uint32_t echo_id; |
| 118 | + uint32_t can_id; |
| 119 | + uint8_t can_dlc; |
| 120 | + uint8_t channel; |
| 121 | + uint8_t flags; |
| 122 | + uint8_t reserved; |
| 123 | + uint8_t data[8]; |
| 124 | + uint32_t timestamp_us; |
| 125 | +} candle_frame_t; |
| 126 | + |
| 127 | +typedef struct { |
| 128 | + uint32_t feature; |
| 129 | + uint32_t fclk_can; |
| 130 | + uint32_t tseg1_min; |
| 131 | + uint32_t tseg1_max; |
| 132 | + uint32_t tseg2_min; |
| 133 | + uint32_t tseg2_max; |
| 134 | + uint32_t sjw_max; |
| 135 | + uint32_t brp_min; |
| 136 | + uint32_t brp_max; |
| 137 | + uint32_t brp_inc; |
| 138 | +} candle_capability_t; |
| 139 | + |
| 140 | +typedef struct { |
| 141 | + uint32_t prop_seg; |
| 142 | + uint32_t phase_seg1; |
| 143 | + uint32_t phase_seg2; |
| 144 | + uint32_t sjw; |
| 145 | + uint32_t brp; |
| 146 | +} candle_bittiming_t; |
| 147 | + |
| 148 | +#pragma pack(pop) |
| 149 | + |
| 150 | +// Static Lib |
| 151 | +#define DLL |
| 152 | +#define __stdcall |
| 153 | + |
| 154 | +DLL bool __stdcall candle_list_scan(candle_list_handle *list); |
| 155 | +DLL bool __stdcall candle_list_free(candle_list_handle list); |
| 156 | +DLL bool __stdcall candle_list_length(candle_list_handle list, uint8_t *len); |
| 157 | + |
| 158 | +DLL bool __stdcall candle_dev_get(candle_list_handle list, uint8_t dev_num, candle_handle *hdev); |
| 159 | +DLL bool __stdcall candle_dev_get_state(candle_handle hdev, candle_devstate_t *state); |
| 160 | +DLL wchar_t * __stdcall candle_dev_get_path(candle_handle hdev); |
| 161 | +DLL char * __stdcall candle_dev_get_name(candle_handle hdev); |
| 162 | +DLL bool __stdcall candle_dev_open(candle_handle hdev); |
| 163 | +DLL bool __stdcall candle_dev_get_timestamp_us(candle_handle hdev, uint32_t *timestamp_us); |
| 164 | +DLL bool __stdcall candle_dev_close(candle_handle hdev); |
| 165 | +DLL bool __stdcall candle_dev_free(candle_handle hdev); |
| 166 | + |
| 167 | +DLL bool __stdcall candle_channel_count(candle_handle hdev, uint8_t *num_channels); |
| 168 | +DLL bool __stdcall candle_channel_get_capabilities(candle_handle hdev, uint8_t ch, candle_capability_t *cap); |
| 169 | +DLL bool __stdcall candle_channel_set_timing(candle_handle hdev, uint8_t ch, candle_bittiming_t *data); |
| 170 | +DLL bool __stdcall candle_channel_set_bitrate(candle_handle hdev, uint8_t ch, uint32_t bitrate); |
| 171 | +DLL bool __stdcall candle_channel_start(candle_handle hdev, uint8_t ch, candle_device_mode_flags_t device_mode_flags); |
| 172 | +DLL bool __stdcall candle_channel_stop(candle_handle hdev, uint8_t ch); |
| 173 | + |
| 174 | +DLL bool __stdcall candle_frame_send(candle_handle hdev, uint8_t ch, candle_frame_t *frame,bool wait_send,uint32_t timeout_ms); |
| 175 | +DLL bool __stdcall candle_frame_read(candle_handle hdev, candle_frame_t *frame, uint32_t timeout_ms); |
| 176 | + |
| 177 | +DLL candle_frametype_t __stdcall candle_frame_type(candle_frame_t *frame); |
| 178 | +DLL uint32_t __stdcall candle_frame_id(candle_frame_t *frame); |
| 179 | +DLL bool __stdcall candle_frame_is_extended_id(candle_frame_t *frame); |
| 180 | +DLL bool __stdcall candle_frame_is_rtr(candle_frame_t *frame); |
| 181 | +DLL uint8_t __stdcall candle_frame_dlc(candle_frame_t *frame); |
| 182 | +DLL uint8_t * __stdcall candle_frame_data(candle_frame_t *frame); |
| 183 | +DLL uint32_t __stdcall candle_frame_timestamp_us(candle_frame_t *frame); |
| 184 | + |
| 185 | +DLL candle_err_t __stdcall candle_dev_last_error(candle_handle hdev); |
| 186 | +DLL const char * __stdcall candle_error_text(candle_err_t errnum); |
| 187 | +#ifdef USING_WINDOWS_ERR_DECODE |
| 188 | +DLL DWORD __stdcall candle_dev_last_windows_error(candle_handle hdev); |
| 189 | +DLL void __stdcall candle_windows_error_text(DWORD errnum, wchar_t* buffer, size_t buflen); |
| 190 | +#else |
| 191 | +#define DWORD int |
| 192 | +//int GetLastError() {return 0;} |
| 193 | +#endif |
| 194 | + |
| 195 | +// This is a convenience function to initialize a single device and start a channel on it |
| 196 | +DLL candle_err_t __stdcall candle_init_single_device(uint8_t device_num, uint8_t device_channel, uint32_t bitrate, candle_device_mode_flags_t device_mode_flags, candle_list_handle *plist, candle_handle *phdev); |
| 197 | + |
| 198 | +#ifdef __cplusplus |
| 199 | +} |
| 200 | +#endif |
0 commit comments