| 1 | /*************************************************************************** |
| 2 | ftdi_stream.c - description |
| 3 | ------------------- |
| 4 | copyright : (C) 2009 Micah Dowty 2010 Uwe Bonnes |
| 5 | email : [email protected] |
| 6 | SPDX-License-Identifier: (LGPL-2.1-only AND MIT) |
| 7 | ***************************************************************************/ |
| 8 | |
| 9 | /*************************************************************************** |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU Lesser General Public License * |
| 13 | * version 2.1 as published by the Free Software Foundation; * |
| 14 | * * |
| 15 | ***************************************************************************/ |
| 16 | |
| 17 | /* Adapted from |
| 18 | * fastftdi.c - A minimal FTDI FT232H interface for which supports bit-bang |
| 19 | * mode, but focuses on very high-performance support for |
| 20 | * synchronous FIFO mode. Requires libusb-1.0 |
| 21 | * |
| 22 | * Copyright (C) 2009 Micah Dowty |
| 23 | * |
| 24 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 25 | * of this software and associated documentation files (the "Software"), to deal |
| 26 | * in the Software without restriction, including without limitation the rights |
| 27 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 28 | * copies of the Software, and to permit persons to whom the Software is |
| 29 | * furnished to do so, subject to the following conditions: |
| 30 | * |
| 31 | * The above copyright notice and this permission notice shall be included in |
| 32 | * all copies or substantial portions of the Software. |
| 33 | * |
| 34 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 35 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 36 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 37 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 38 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 39 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 40 | * THE SOFTWARE. |
| 41 | */ |
| 42 | |
| 43 | #include <stdlib.h> |
| 44 | #include <stdio.h> |
| 45 | #ifndef _WIN32 |
| 46 | #include <sys/time.h> |
| 47 | #endif |
| 48 | #include <libusb.h> |
| 49 | |
| 50 | #include "ftdi.h"cxx . . $ `<file>/src/ftdi.h`/ |
| 51 | |
| 52 | typedef structcxx . . $ $anonymous_type_d49159d0632c6ba7_0# |
| 53 | { |
| 54 | FTDIStreamCallbackcxx . . $ FTDIStreamCallback# *callbackcxx . . $ $anonymous_type_d49159d0632c6ba7_0#callback.; |
| 55 | void *userdatacxx . . $ $anonymous_type_d49159d0632c6ba7_0#userdata.; |
| 56 | int packetsizecxx . . $ $anonymous_type_d49159d0632c6ba7_0#packetsize.; |
| 57 | int activitycxx . . $ $anonymous_type_d49159d0632c6ba7_0#activity.; |
| 58 | int resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result.; |
| 59 | FTDIProgressInfocxx . . $ FTDIProgressInfo# progresscxx . . $ $anonymous_type_d49159d0632c6ba7_0#progress.; |
| 60 | } FTDIStreamStatecxx . . $ FTDIStreamState#; |
| 61 | |
| 62 | /* Handle callbacks |
| 63 | * |
| 64 | * With Exit request, free memory and release the transfer |
| 65 | * |
| 66 | * state->result is only set when some error happens |
| 67 | */ |
| 68 | static void LIBUSB_CALLcxx . . $ `<external>/8540661316442904143/libusb.h:122:9`! |
| 69 | ftdi_readstream_cbcxx . . $ ftdi_readstream_cb(6aa7c12d948ffba5).(struct libusb_transfercxx . . $ libusb_transfer# *transfer) |
| 70 | { |
| 71 | FTDIStreamStatecxx . . $ FTDIStreamState# *state = transfer->user_datacxx . . $ libusb_transfer#user_data.; |
| 72 | int packet_size = state->packetsizecxx . . $ $anonymous_type_d49159d0632c6ba7_0#packetsize.; |
| 73 | |
| 74 | state->activitycxx . . $ $anonymous_type_d49159d0632c6ba7_0#activity.++; |
| 75 | if (transfer->statuscxx . . $ libusb_transfer#status. == LIBUSB_TRANSFER_COMPLETEDcxx . . $ LIBUSB_TRANSFER_COMPLETED.) |
| 76 | { |
| 77 | int i; |
| 78 | uint8_tcxx . . $ uint8_t# *ptr = transfer->buffercxx . . $ libusb_transfer#buffer.; |
| 79 | int length = transfer->actual_lengthcxx . . $ libusb_transfer#actual_length.; |
| 80 | int numPackets = (length + packet_size - 1) / packet_size; |
| 81 | int res = 0; |
| 82 | |
| 83 | for (i = 0; i < numPackets; i++) |
| 84 | { |
| 85 | int payloadLen; |
| 86 | int packetLen = length; |
| 87 | |
| 88 | if (packetLen > packet_size) |
| 89 | packetLen = packet_size; |
| 90 | |
| 91 | payloadLen = packetLen - 2; |
| 92 | state->progresscxx . . $ $anonymous_type_d49159d0632c6ba7_0#progress..currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..totalBytescxx . . $ size_and_time#totalBytes. += payloadLen; |
| 93 | |
| 94 | res = state->callbackcxx . . $ $anonymous_type_d49159d0632c6ba7_0#callback.(ptr + 2, payloadLen, |
| 95 | NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, state->userdatacxx . . $ $anonymous_type_d49159d0632c6ba7_0#userdata.); |
| 96 | |
| 97 | ptr += packetLen; |
| 98 | length -= packetLen; |
| 99 | } |
| 100 | if (res) |
| 101 | { |
| 102 | freecxx . . $ free(5d22bdacc48458e8).(transfer->buffercxx . . $ libusb_transfer#buffer.); |
| 103 | libusb_free_transfercxx . . $ libusb_free_transfer(6aa7c12d948ffba5).(transfer); |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | transfer->statuscxx . . $ libusb_transfer#status. = -1; |
| 108 | state->resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result. = libusb_submit_transfercxx . . $ libusb_submit_transfer(d1d6c59b35b7dbb7).(transfer); |
| 109 | } |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "unknown status %d\n",transfer->statuscxx . . $ libusb_transfer#status.); |
| 114 | state->resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result. = LIBUSB_ERROR_IOcxx . . $ LIBUSB_ERROR_IO.; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | Helper function to calculate (unix) time differences |
| 120 | |
| 121 | \param a timeval |
| 122 | \param b timeval |
| 123 | */ |
| 124 | static double |
| 125 | TimevalDiffcxx . . $ TimevalDiff(50254d9293d29250).(const struct timevalcxx . . $ timeval# *a, const struct timevalcxx . . $ timeval# *b) |
| 126 | { |
| 127 | return (a->tv_seccxx . . $ timeval#tv_sec. - b->tv_seccxx . . $ timeval#tv_sec.) + 1e-6 * (a->tv_useccxx . . $ timeval#tv_usec. - b->tv_useccxx . . $ timeval#tv_usec.); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | Streaming reading of data from the device |
| 132 | |
| 133 | Use asynchronous transfers in libusb-1.0 for high-performance |
| 134 | streaming of data from a device interface back to the PC. This |
| 135 | function continuously transfers data until either an error occurs |
| 136 | or the callback returns a nonzero value. This function returns |
| 137 | a libusb error code or the callback's return value. |
| 138 | |
| 139 | For every contiguous block of received data, the callback will |
| 140 | be invoked. |
| 141 | |
| 142 | \param ftdi pointer to ftdi_context |
| 143 | \param callback to user supplied function for one block of data |
| 144 | \param userdata |
| 145 | \param packetsPerTransfer number of packets per transfer |
| 146 | \param numTransfers Number of transfers per callback |
| 147 | |
| 148 | */ |
| 149 | |
| 150 | int |
| 151 | ftdi_readstreamcxx . . $ ftdi_readstream(6585c5a6b0d5e143).(struct ftdi_contextcxx . . $ ftdi_context# *ftdi, |
| 152 | FTDIStreamCallbackcxx . . $ FTDIStreamCallback# *callback, void *userdata, |
| 153 | int packetsPerTransfer, int numTransfers) |
| 154 | { |
| 155 | struct libusb_transfercxx . . $ libusb_transfer# **transfers; |
| 156 | FTDIStreamStatecxx . . $ FTDIStreamState# state = { callback, userdata, ftdi->max_packet_sizecxx . . $ ftdi_context#max_packet_size., 1 }; |
| 157 | int bufferSize = packetsPerTransfer * ftdi->max_packet_sizecxx . . $ ftdi_context#max_packet_size.; |
| 158 | int xferIndex; |
| 159 | int err = 0; |
| 160 | |
| 161 | /* Only FT2232H and FT232H know about the synchronous FIFO Mode*/ |
| 162 | if ((ftdi->typecxx . . $ ftdi_context#type. != TYPE_2232Hcxx . . $ TYPE_2232H.) && (ftdi->typecxx . . $ ftdi_context#type. != TYPE_232Hcxx . . $ TYPE_232H.)) |
| 163 | { |
| 164 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Device doesn't support synchronous FIFO mode\n"); |
| 165 | return 1; |
| 166 | } |
| 167 | |
| 168 | /* We don't know in what state we are, switch to reset*/ |
| 169 | if (ftdi_set_bitmodecxx . . $ ftdi_set_bitmode(fd17119b3c08153d).(ftdi, 0xff, BITMODE_RESETcxx . . $ BITMODE_RESET.) < 0) |
| 170 | { |
| 171 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't reset mode\n"); |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | /* Purge anything remaining in the buffers*/ |
| 176 | if (ftdi_tcioflushcxx . . $ ftdi_tcioflush(e7991dd2c21df3a2).(ftdi) < 0) |
| 177 | { |
| 178 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't flush FIFOs & buffers\n"); |
| 179 | return 1; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Set up all transfers |
| 184 | */ |
| 185 | |
| 186 | transfers = calloccxx . . $ calloc(63a7bf2941bfcd7a).(numTransfers, sizeof *transfers); |
| 187 | if (!transfers) |
| 188 | { |
| 189 | err = LIBUSB_ERROR_NO_MEMcxx . . $ LIBUSB_ERROR_NO_MEM.; |
| 190 | goto cleanup; |
| 191 | } |
| 192 | |
| 193 | for (xferIndex = 0; xferIndex < numTransfers; xferIndex++) |
| 194 | { |
| 195 | struct libusb_transfercxx . . $ libusb_transfer# *transfer; |
| 196 | |
| 197 | transfer = libusb_alloc_transfercxx . . $ libusb_alloc_transfer(681d497ec4df4030).(0); |
| 198 | transfers[xferIndex] = transfer; |
| 199 | if (!transfer) |
| 200 | { |
| 201 | err = LIBUSB_ERROR_NO_MEMcxx . . $ LIBUSB_ERROR_NO_MEM.; |
| 202 | goto cleanup; |
| 203 | } |
| 204 | |
| 205 | libusb_fill_bulk_transfercxx . . $ libusb_fill_bulk_transfer(8af723ba29f5478e).(transfer, ftdi->usb_devcxx . . $ ftdi_context#usb_dev., ftdi->out_epcxx . . $ ftdi_context#out_ep., |
| 206 | malloccxx . . $ malloc(bfcfa4d6b7f7ef64).(bufferSize), bufferSize, |
| 207 | ftdi_readstream_cbcxx . . $ ftdi_readstream_cb(6aa7c12d948ffba5)., |
| 208 | &state, 0); |
| 209 | |
| 210 | if (!transfer->buffercxx . . $ libusb_transfer#buffer.) |
| 211 | { |
| 212 | err = LIBUSB_ERROR_NO_MEMcxx . . $ LIBUSB_ERROR_NO_MEM.; |
| 213 | goto cleanup; |
| 214 | } |
| 215 | |
| 216 | transfer->statuscxx . . $ libusb_transfer#status. = -1; |
| 217 | err = libusb_submit_transfercxx . . $ libusb_submit_transfer(d1d6c59b35b7dbb7).(transfer); |
| 218 | if (err) |
| 219 | goto cleanup; |
| 220 | } |
| 221 | |
| 222 | /* Start the transfers only when everything has been set up. |
| 223 | * Otherwise the transfers start stuttering and the PC not |
| 224 | * fetching data for several to several ten milliseconds |
| 225 | * and we skip blocks |
| 226 | */ |
| 227 | if (ftdi_set_bitmodecxx . . $ ftdi_set_bitmode(fd17119b3c08153d).(ftdi, 0xff, BITMODE_SYNCFFcxx . . $ BITMODE_SYNCFF.) < 0) |
| 228 | { |
| 229 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!,"Can't set synchronous fifo mode: %s\n", |
| 230 | ftdi_get_error_stringcxx . . $ ftdi_get_error_string(318567b992c3484e).(ftdi)); |
| 231 | goto cleanup; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Run the transfers, and periodically assess progress. |
| 236 | */ |
| 237 | |
| 238 | gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&state.progresscxx . . $ $anonymous_type_d49159d0632c6ba7_0#progress..firstcxx . . $ $anonymous_type_5ca5afed695a70e1_0#first..timecxx . . $ size_and_time#time., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 239 | |
| 240 | do |
| 241 | { |
| 242 | FTDIProgressInfocxx . . $ FTDIProgressInfo# *progress = &state.progresscxx . . $ $anonymous_type_d49159d0632c6ba7_0#progress.; |
| 243 | const double progressInterval = 1.0; |
| 244 | struct timevalcxx . . $ timeval# timeout = { ftdi->usb_read_timeoutcxx . . $ ftdi_context#usb_read_timeout. / 1000, |
| 245 | (ftdi->usb_read_timeoutcxx . . $ ftdi_context#usb_read_timeout. % 1000) * 1000 }; |
| 246 | struct timevalcxx . . $ timeval# now; |
| 247 | |
| 248 | int xfer_err = libusb_handle_events_timeoutcxx . . $ libusb_handle_events_timeout(c69f988ddeb7cffb).(ftdi->usb_ctxcxx . . $ ftdi_context#usb_ctx., &timeout); |
| 249 | if (xfer_err == LIBUSB_ERROR_INTERRUPTEDcxx . . $ LIBUSB_ERROR_INTERRUPTED.) |
| 250 | /* restart interrupted events */ |
| 251 | xfer_err = libusb_handle_events_timeoutcxx . . $ libusb_handle_events_timeout(c69f988ddeb7cffb).(ftdi->usb_ctxcxx . . $ ftdi_context#usb_ctx., &timeout); |
| 252 | if (!state.resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result.) |
| 253 | { |
| 254 | state.resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result. = xfer_err; |
| 255 | } |
| 256 | if (state.activitycxx . . $ $anonymous_type_d49159d0632c6ba7_0#activity. == 0) |
| 257 | state.resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result. = 1; |
| 258 | else |
| 259 | state.activitycxx . . $ $anonymous_type_d49159d0632c6ba7_0#activity. = 0; |
| 260 | |
| 261 | // If enough time has elapsed, update the progress |
| 262 | gettimeofdaycxx . . $ gettimeofday(ee8865a94200b3d6).(&now, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 263 | if (TimevalDiffcxx . . $ TimevalDiff(50254d9293d29250).(&now, &progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..timecxx . . $ size_and_time#time.) >= progressInterval) |
| 264 | { |
| 265 | progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..timecxx . . $ size_and_time#time. = now; |
| 266 | progress->totalTimecxx . . $ $anonymous_type_5ca5afed695a70e1_0#totalTime. = TimevalDiffcxx . . $ TimevalDiff(50254d9293d29250).(&progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..timecxx . . $ size_and_time#time., |
| 267 | &progress->firstcxx . . $ $anonymous_type_5ca5afed695a70e1_0#first..timecxx . . $ size_and_time#time.); |
| 268 | |
| 269 | if (progress->prevcxx . . $ $anonymous_type_5ca5afed695a70e1_0#prev..totalBytescxx . . $ size_and_time#totalBytes.) |
| 270 | { |
| 271 | // We have enough information to calculate rates |
| 272 | |
| 273 | double currentTime; |
| 274 | |
| 275 | currentTime = TimevalDiffcxx . . $ TimevalDiff(50254d9293d29250).(&progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..timecxx . . $ size_and_time#time., |
| 276 | &progress->prevcxx . . $ $anonymous_type_5ca5afed695a70e1_0#prev..timecxx . . $ size_and_time#time.); |
| 277 | |
| 278 | progress->totalRatecxx . . $ $anonymous_type_5ca5afed695a70e1_0#totalRate. = |
| 279 | progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..totalBytescxx . . $ size_and_time#totalBytes. /progress->totalTimecxx . . $ $anonymous_type_5ca5afed695a70e1_0#totalTime.; |
| 280 | progress->currentRatecxx . . $ $anonymous_type_5ca5afed695a70e1_0#currentRate. = |
| 281 | (progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current..totalBytescxx . . $ size_and_time#totalBytes. - |
| 282 | progress->prevcxx . . $ $anonymous_type_5ca5afed695a70e1_0#prev..totalBytescxx . . $ size_and_time#totalBytes.) / currentTime; |
| 283 | } |
| 284 | |
| 285 | state.callbackcxx . . $ $anonymous_type_d49159d0632c6ba7_0#callback.(NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, progress, state.userdatacxx . . $ $anonymous_type_d49159d0632c6ba7_0#userdata.); |
| 286 | progress->prevcxx . . $ $anonymous_type_5ca5afed695a70e1_0#prev. = progress->currentcxx . . $ $anonymous_type_5ca5afed695a70e1_0#current.; |
| 287 | |
| 288 | } |
| 289 | } while (!state.resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result.); |
| 290 | |
| 291 | /* |
| 292 | * Cancel any outstanding transfers, and free memory. |
| 293 | */ |
| 294 | |
| 295 | cleanup: |
| 296 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "cleanup\n"); |
| 297 | if (transfers) |
| 298 | freecxx . . $ free(5d22bdacc48458e8).(transfers); |
| 299 | if (err) |
| 300 | return err; |
| 301 | else |
| 302 | return state.resultcxx . . $ $anonymous_type_d49159d0632c6ba7_0#result.; |
| 303 | } |
| 304 |