📄 fusb_darwin.cc
字号:
/* -*- c++ -*- *//* * Copyright 2006 Free Software Foundation, Inc. * * This file is part of GNU Radio. * * GNU Radio 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 3, or (at your option) * any later version. * * GNU Radio 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 GNU Radio; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, * Boston, MA 02110-1301, USA. */#ifdef HAVE_CONFIG_H#include "config.h"#endif// tell mld_threads to NOT use omni_threads,// but rather Darwin's pthreads#define _USE_OMNI_THREADS_#define DO_DEBUG 0#include <usb.h>#include "fusb.h"#include "fusb_darwin.h"#include "darwin_libusb.h"static const int USB_TIMEOUT = 100; // in millisecondsstatic const UInt8 NUM_QUEUE_ITEMS = 20;fusb_devhandle_darwin::fusb_devhandle_darwin (usb_dev_handle* udh) : fusb_devhandle (udh){ // that's it}fusb_devhandle_darwin::~fusb_devhandle_darwin (){ // nop}fusb_ephandle*fusb_devhandle_darwin::make_ephandle (int endpoint, bool input_p, int block_size, int nblocks){ return new fusb_ephandle_darwin (this, endpoint, input_p, block_size, nblocks);}// ----------------------------------------------------------------fusb_ephandle_darwin::fusb_ephandle_darwin (fusb_devhandle_darwin* dh, int endpoint, bool input_p, int block_size, int nblocks) : fusb_ephandle (endpoint, input_p, block_size, nblocks), d_devhandle (dh), d_pipeRef (0), d_transferType (0), d_interfaceRef (0), d_interface (0), d_queue (0), d_buffer (0), d_bufLenBytes (0){ d_bufLenBytes = fusb_sysconfig::max_block_size();// create circular buffer d_buffer = new circular_buffer<char> (NUM_QUEUE_ITEMS * d_bufLenBytes, !d_input_p, d_input_p);// create the queue d_queue = new circular_linked_list <s_buffer_ptr> (NUM_QUEUE_ITEMS); d_queue->iterate_start (); s_node_ptr l_node = d_queue->iterate_next (); while (l_node) { l_node->both (new s_both<s_buffer_ptr> (l_node, this)); s_buffer_ptr l_buf = new s_buffer (d_bufLenBytes); l_node->object (l_buf); l_node = d_queue->iterate_next (); l_buf = NULL; } d_readRunning = new mld_mutex (); d_runThreadRunning = new mld_mutex (); d_runBlock = new mld_condition (); d_readBlock = new mld_condition ();}fusb_ephandle_darwin::~fusb_ephandle_darwin (){ stop (); d_queue->iterate_start (); s_node_ptr l_node = d_queue->iterate_next (); while (l_node) { s_both_ptr l_both = l_node->both (); delete l_both; l_both = NULL; l_node->both (NULL); s_buffer_ptr l_buf = l_node->object (); delete l_buf; l_buf = NULL; l_node->object (NULL); l_node = d_queue->iterate_next (); } delete d_queue; d_queue = NULL; delete d_buffer; d_buffer = NULL; delete d_readRunning; d_readRunning = NULL; delete d_runThreadRunning; d_runThreadRunning = NULL; delete d_runBlock; d_runBlock = NULL; delete d_readBlock; d_readBlock = NULL;}boolfusb_ephandle_darwin::start (){ UInt8 direction, number, interval; UInt16 maxPacketSize;// reset circular buffer d_buffer->reset ();// reset the queue d_queue->num_used (0); d_queue->iterate_start (); s_node_ptr l_node = d_queue->iterate_next (); while (l_node) { l_node->both()->set (l_node, this); l_node->object()->reset (); l_node->set_available (); l_node = d_queue->iterate_next (); } d_pipeRef = d_transferType = 0; usb_dev_handle* dev = d_devhandle->get_usb_dev_handle (); if (! dev) USB_ERROR_STR (false, -ENXIO, "fusb_ephandle_darwin::start: " "null device"); darwin_dev_handle* device = (darwin_dev_handle*) dev->impl_info; if (! device) USB_ERROR_STR (false, -ENOENT, "fusb_ephandle_darwin::start: " "device not initialized"); if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::start: " "dev = %p, device = %p\n", dev, device); d_interfaceRef = device->interface; if (! d_interfaceRef) USB_ERROR_STR (false, -EACCES, "fusb_ephandle_darwin::start: " "interface used without being claimed"); d_interface = *d_interfaceRef;// get read or write pipe info (depends on "d_input_p") if (usb_debug > 3) fprintf (stderr, "fusb_ephandle_darwin::start " "d_endpoint = %d, d_input_p = %s\n", d_endpoint, d_input_p ? "TRUE" : "FALSE"); int l_endpoint = (d_input_p ? USB_ENDPOINT_IN : USB_ENDPOINT_OUT); int pipeRef = ep_to_pipeRef (device, d_endpoint | l_endpoint); if (pipeRef < 0) USB_ERROR_STR (false, -EINVAL, "fusb_ephandle_darwin::start " " invalid pipeRef.\n"); d_pipeRef = pipeRef; d_interface->GetPipeProperties (d_interfaceRef, d_pipeRef, &direction, &number, &d_transferType, &maxPacketSize, &interval); if (usb_debug == 3) fprintf (stderr, "fusb_ephandle_darwin::start: %s: ep = 0x%02x, " "pipeRef = %d, d_i = %p, d_iR = %p, if_dir = %d, if_# = %d, " "if_int = %d, if_maxPS = %d\n", d_input_p ? "read" : "write", d_endpoint, d_pipeRef, d_interface, d_interfaceRef, direction, number, interval, maxPacketSize); // set global start boolean d_started = true; // lock the runBlock mutex, before creating the run thread. // this guarantees that we can control execution between these 2 threads d_runBlock->mutex ()->lock (); // create the run thread, which allows OSX to process I/O separately d_runThread = new mld_thread (run_thread, this); // wait until the run thread (and possibky read thread) are -really- // going; this will unlock the mutex before waiting for a signal () d_runBlock->wait (); if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::start: %s started.\n", d_input_p ? "read" : "write"); return (true);}voidfusb_ephandle_darwin::run_thread (void* arg){ fusb_ephandle_darwin* This = static_cast<fusb_ephandle_darwin*>(arg); // lock the run thread running mutex; if ::stop() is called, it will // first abort() the pipe then wait for the run thread to finish, // via a lock() on this mutex mld_mutex_ptr l_runThreadRunning = This->d_runThreadRunning; l_runThreadRunning->lock (); mld_mutex_ptr l_readRunning = This->d_readRunning; mld_condition_ptr l_readBlock = This->d_readBlock; mld_mutex_ptr l_readBlock_mutex = l_readBlock->mutex (); bool l_input_p = This->d_input_p; if (usb_debug) fprintf (stderr, "fusb_ephandle_darwin::run_thread: " "starting for %s.\n", l_input_p ? "read" : "write"); usb_interface_t** l_interfaceRef = This->d_interfaceRef; usb_interface_t* l_interface = This->d_interface; CFRunLoopSourceRef l_cfSource;// create async run loop l_interface->CreateInterfaceAsyncEventSource (l_interfaceRef, &l_cfSource); CFRunLoopAddSource (CFRunLoopGetCurrent (), l_cfSource, kCFRunLoopDefaultMode);// get run loop reference, to allow other threads to stop This->d_CFRunLoopRef = CFRunLoopGetCurrent (); mld_thread_ptr l_rwThread = NULL; if (l_input_p) { // lock the readBlock mutex, before creating the read thread. // this guarantees that we can control execution between these 2 threads l_readBlock_mutex->lock (); // create the read thread, which just issues all of the starting // async read commands, then returns l_rwThread = new mld_thread (read_thread, arg); // wait until the the read thread is -really- going; this will // unlock the read block mutex before waiting for a signal () l_readBlock->wait (); } // now signal the run condition to release and finish ::start(). // lock the runBlock mutex first; this will force waiting until the // ->wait() command is issued in ::start() mld_mutex_ptr l_run_block_mutex = This->d_runBlock->mutex (); l_run_block_mutex->lock (); // now that the lock is in place, signal the parent thread that // things are running This->d_runBlock->signal (); // release the run_block mutex, just in case l_run_block_mutex->unlock (); // run the loop CFRunLoopRun (); if (l_input_p) { // wait for read_thread () to finish, if needed l_readRunning->lock (); l_readRunning->unlock (); } // remove run loop stuff CFRunLoopRemoveSource (CFRunLoopGetCurrent (), l_cfSource, kCFRunLoopDefaultMode);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -