From 6865ac687bccace724078eddaca5733ef08b73f3 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Mon, 15 Aug 2022 17:23:54 +0800 Subject: [PATCH] Code refactoring 12 --- DSView/pv/appcontrol.cpp | 50 +---- DSView/pv/appcontrol.h | 13 +- DSView/pv/devicemanager.cpp | 192 ------------------ DSView/pv/devicemanager.h | 93 --------- DSView/pv/interface/icallbacks.h | 3 + DSView/pv/mainwindow.cpp | 8 + DSView/pv/mainwindow.h | 1 + DSView/pv/sigsession.cpp | 194 +++++++------------ DSView/pv/sigsession.h | 62 +++--- DSView/pv/toolbars/samplingbar.cpp | 16 ++ DSView/pv/toolbars/samplingbar.h | 4 + libsigrok4DSL/backend.c | 5 +- libsigrok4DSL/dsdevice.c | 44 ----- libsigrok4DSL/hardware/DSL/dscope.c | 11 +- libsigrok4DSL/hardware/DSL/dslogic.c | 8 +- libsigrok4DSL/hardware/demo/demo.c | 12 +- libsigrok4DSL/hwdriver.c | 50 +---- libsigrok4DSL/lib_main.c | 279 +++++++++++++++++++-------- libsigrok4DSL/libsigrok-internal.h | 16 +- libsigrok4DSL/libsigrok.h | 102 +++++----- libsigrok4DSL/session.c | 14 +- libsigrok4DSL/session_driver.c | 3 +- libsigrok4DSL/session_file.c | 4 +- 23 files changed, 411 insertions(+), 773 deletions(-) delete mode 100644 DSView/pv/devicemanager.cpp delete mode 100644 DSView/pv/devicemanager.h diff --git a/DSView/pv/appcontrol.cpp b/DSView/pv/appcontrol.cpp index 0504e707..b34dfd1f 100644 --- a/DSView/pv/appcontrol.cpp +++ b/DSView/pv/appcontrol.cpp @@ -25,9 +25,7 @@ #include #include #include -#include - -#include "devicemanager.h" +#include #include "sigsession.h" #include "dsvdef.h" #include "config/appconfig.h" @@ -37,11 +35,8 @@ AppControl::AppControl() { - sr_ctx = NULL; - _topWindow = NULL; - - _device_manager = new pv::DeviceManager(); - _session = new pv::SigSession(_device_manager); + _topWindow = NULL; + _session = new pv::SigSession(); } AppControl::AppControl(AppControl &o) @@ -50,8 +45,7 @@ AppControl::AppControl(AppControl &o) } AppControl::~AppControl() -{ - DESTROY_OBJECT(_device_manager); +{ DESTROY_OBJECT(_session); } @@ -69,28 +63,10 @@ void AppControl::Destroy(){ } bool AppControl::Init() -{ - sr_log_set_context(dsv_log_context()); - srd_log_set_context(dsv_log_context()); - - // Initialise libsigrok - if (sr_init(&sr_ctx) != SR_OK) - { - m_error = "DSView run ERROR: libsigrok init failed."; - return false; - } - _session->set_sr_context(sr_ctx); +{ + _session->init(); - QString resdir = GetResourceDir(); - char res_path[256] = {0}; -#ifdef _WIN32 - QTextCodec *codec = QTextCodec::codecForName("System"); - QByteArray str_tmp = codec->fromUnicode(resdir); - strncpy(res_path, str_tmp.data(), sizeof(res_path) - 1); -#else - strncpy(res_path, resdir.toUtf8().data(), sizeof(res_path) - 1); -#endif - sr_set_firmware_resource_dir(res_path); + srd_log_set_context(dsv_log_context()); #if defined(_WIN32) && defined(DEBUG_INFO) //able run debug with qtcreator @@ -129,15 +105,13 @@ bool AppControl::Init() bool AppControl::Start() { - _session->Open(); - _device_manager->initAll(sr_ctx); + _session->Open(); return true; } void AppControl::Stop() { - _session->Close(); - _device_manager->UnInitAll(); + _session->Close(); } void AppControl::UnInit() @@ -145,11 +119,7 @@ void AppControl::UnInit() // Destroy libsigrokdecode srd_exit(); - if (sr_ctx) - { - sr_exit(sr_ctx); - sr_ctx = NULL; - } + _session->uninit(); } const char *AppControl::GetLastError() diff --git a/DSView/pv/appcontrol.h b/DSView/pv/appcontrol.h index 2cbb1754..189d0f6e 100644 --- a/DSView/pv/appcontrol.h +++ b/DSView/pv/appcontrol.h @@ -26,8 +26,7 @@ struct sr_context; class QWidget; -namespace pv{ - class DeviceManager; +namespace pv{ class SigSession; } @@ -55,11 +54,7 @@ public: inline pv::SigSession* GetSession(){ return _session; - } - - inline pv::DeviceManager& GetDeviceManager(){ - return *_device_manager; - } + } inline void SetTopWindow(QWidget *w){ _topWindow = w; @@ -75,9 +70,7 @@ public: std::string _open_file_name; private: - std::string m_error; - struct sr_context *sr_ctx; - pv::DeviceManager *_device_manager; + std::string m_error; pv::SigSession *_session; QWidget *_topWindow; }; diff --git a/DSView/pv/devicemanager.cpp b/DSView/pv/devicemanager.cpp deleted file mode 100644 index 7e10da14..00000000 --- a/DSView/pv/devicemanager.cpp +++ /dev/null @@ -1,192 +0,0 @@ -/* - * This file is part of the DSView project. - * DSView is based on PulseView. - * - * Copyright (C) 2012 Joel Holdsworth - * Copyright (C) 2013 DreamSourceLab - * - * 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 St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#include "devicemanager.h" -#include "device/devinst.h" -#include "device/device.h" -#include "sigsession.h" - -#include -#include -#include -#include - -#include -#include -#include "config/appconfig.h" - -using std::list; -using std::map; -using std::ostringstream; -using std::runtime_error; -using std::string; - -namespace pv { - -DeviceManager::DeviceManager() -{ - _sr_ctx = NULL; -} - -DeviceManager::DeviceManager(DeviceManager &o) -{ - (void)o; -} - -DeviceManager::~DeviceManager() -{ - -} - -void DeviceManager::initAll(struct sr_context *sr_ctx) -{ - _sr_ctx = sr_ctx; - init_drivers(); - scan_all_drivers(); -} - - void DeviceManager::UnInitAll() - { - release_devices(); - } - -void DeviceManager::add_device(DevInst *device) -{ - assert(device); - - auto it = std::find(_devices.begin(), _devices.end(), device); - if (it ==_devices.end()){ - _devices.push_front(device); - } -} - -void DeviceManager::del_device(DevInst *device) -{ - assert(device); - - auto it = std::find(_devices.begin(), _devices.end(), device); - if (it !=_devices.end()){ - _devices.erase(it); //remove from list - device->destroy(); - } -} - -void DeviceManager::driver_scan( - std::list &driver_devices, - struct sr_dev_driver *const driver, - GSList *const drvopts) -{ - assert(driver); - - // Remove any device instances from this driver from the device - // list. They will not be valid after the scan. - auto i = _devices.begin(); - while (i != _devices.end()) { - if ((*i)->dev_inst() && (*i)->dev_inst()->driver == driver) { - auto p = (*i); - p->release(); - i = _devices.erase(i); - } else { - i++; - } - } - - // Clear all the old device instances from this driver - sr_dev_clear(driver); - //release_driver(driver); - - // Check If DSL hardware driver - if (strncmp(driver->name, "virtual", 7)) { - QDir dir(GetResourceDir()); - if (!dir.exists()) - return; - } - - // Do the scan - GSList *const devices = sr_driver_scan(driver, drvopts); - - for (GSList *l = devices; l; l = l->next){ - Device *dev = new device::Device((sr_dev_inst*)l->data); //create new device - driver_devices.push_front(dev); - } - - g_slist_free(devices); - - // append the scanned devices to the main list - _devices.insert(_devices.end(), driver_devices.begin(), driver_devices.end()); -} - -void DeviceManager::init_drivers() -{ - // Initialise all libsigrok drivers - sr_dev_driver **const drivers = sr_driver_list(); - for (sr_dev_driver **driver = drivers; *driver; driver++) { - if (sr_driver_init(_sr_ctx, *driver) != SR_OK) { - throw runtime_error( - string("Failed to initialize driver ") + - string((*driver)->name)); - } - } -} - -void DeviceManager::release_devices() -{ - // Release all the used devices - for (DevInst *dev : _devices) { - dev->release(); - } - - // Clear all the drivers - sr_dev_driver **const drivers = sr_driver_list(); - for (sr_dev_driver **driver = drivers; *driver; driver++) - sr_dev_clear(*driver); -} - -void DeviceManager::scan_all_drivers() -{ - // Scan all drivers for all devices. - struct sr_dev_driver **const drivers = sr_driver_list(); - for (struct sr_dev_driver **driver = drivers; *driver; driver++){ - std::list driver_devices; - driver_scan(driver_devices, *driver); - } -} - -void DeviceManager::release_driver(struct sr_dev_driver *const driver) -{ - for (DevInst *dev : _devices) { - if(dev->dev_inst()->driver == driver) - dev->release(); - } - - // Clear all the old device instances from this driver - sr_dev_clear(driver); -} - -bool DeviceManager::compare_devices(DevInst *a, DevInst *b) -{ - assert(a); - assert(b); - return a->format_device_title().compare(b->format_device_title()) <= 0; -} - -} // namespace pv diff --git a/DSView/pv/devicemanager.h b/DSView/pv/devicemanager.h deleted file mode 100644 index c8b85b63..00000000 --- a/DSView/pv/devicemanager.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * This file is part of the DSView project. - * DSView is based on PulseView. - * - * Copyright (C) 2012 Joel Holdsworth - * Copyright (C) 2013 DreamSourceLab - * - * 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 St, Fifth Floor, Boston, MA 02110-1301 USA - */ - - -#ifndef DSVIEW_PV_DEVICEMANAGER_H -#define DSVIEW_PV_DEVICEMANAGER_H - -#include - -#include -#include -#include -#include -#include - -struct sr_context; -struct sr_dev_driver; -struct sr_dev_inst; - -namespace pv { - -class SigSession; - -namespace device { -class DevInst; -} - -using namespace pv::device; - -class DeviceManager -{ -private: - DeviceManager(DeviceManager &o); - -public: - DeviceManager(); - - ~DeviceManager(); - - inline const std::list& devices(){ - return _devices; - } - - void add_device(DevInst *device); - void del_device(DevInst *device); - - void driver_scan( - std::list &driver_devices, - struct sr_dev_driver *const driver=NULL, - GSList *const drvopts=NULL); - - void initAll(struct sr_context *sr_ctx); - - void UnInitAll(); - -private: - void init_drivers(); - - void release_devices(); - - void scan_all_drivers(); - - void release_driver(struct sr_dev_driver *const driver); - - static bool compare_devices(DevInst *a, DevInst *b); - -private: - struct sr_context* _sr_ctx; - std::list _devices; -}; - -} // namespace pv - -#endif // DSVIEW_PV_DEVICEMANAGER_H diff --git a/DSView/pv/interface/icallbacks.h b/DSView/pv/interface/icallbacks.h index c16f40b5..937ecdbb 100644 --- a/DSView/pv/interface/icallbacks.h +++ b/DSView/pv/interface/icallbacks.h @@ -23,6 +23,8 @@ #ifndef _I_CALLBACKS_ #define _I_CALLBACKS_ +struct ds_device_info; + class ISessionCallback { public: @@ -52,6 +54,7 @@ public: virtual void receive_header()=0; virtual void data_received()=0; + virtual void update_device_list(struct ds_device_info *array, int count, int select_index)=0; }; diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index d251b14f..f038d074 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -253,6 +253,7 @@ void MainWindow::setup_ui() connect(&_event, SIGNAL(data_updated()), this, SLOT(on_data_updated())); connect(&_event, SIGNAL(cur_snap_samplerate_changed()), this, SLOT(on_cur_snap_samplerate_changed())); connect(&_event, SIGNAL(receive_data_len(quint64)), this, SLOT(on_receive_data_len(quint64))); + connect(&_event, SIGNAL(update_device_list()), this, SLOT(on_device_list_changed())); //view @@ -1517,6 +1518,9 @@ void MainWindow::on_cur_snap_samplerate_changed() _measure_widget->cursor_update(); } + + /*------------------on event end-------*/ + void MainWindow::device_setted(){ _view->set_device(); } @@ -1598,4 +1602,8 @@ void MainWindow::data_received(){ } +void MainWindow::update_device_list(struct ds_device_info *array, int count, int select_index) +{ +} + } // namespace pv diff --git a/DSView/pv/mainwindow.h b/DSView/pv/mainwindow.h index 607be76e..de9153bf 100644 --- a/DSView/pv/mainwindow.h +++ b/DSView/pv/mainwindow.h @@ -173,6 +173,7 @@ private: void receive_data_len(quint64 len); void receive_header(); void data_received(); + void update_device_list(struct ds_device_info *array, int count, int select_index); //------private bool gen_session_json(QJsonObject &sessionVar); diff --git a/DSView/pv/sigsession.cpp b/DSView/pv/sigsession.cpp index 130020b7..c97f3f98 100644 --- a/DSView/pv/sigsession.cpp +++ b/DSView/pv/sigsession.cpp @@ -25,7 +25,6 @@ #include "sigsession.h" #include "mainwindow.h" -#include "devicemanager.h" #include "device/device.h" #include "device/file.h" @@ -60,15 +59,15 @@ #include "data/decode/decoderstatus.h" #include "dsvdef.h" #include "log.h" +#include "config/appconfig.h" namespace pv { // TODO: This should not be necessary SigSession* SigSession::_session = NULL; -SigSession::SigSession(DeviceManager *device_manager) -{ - _device_manager = device_manager; +SigSession::SigSession() +{ // TODO: This should not be necessary _session = this; _hot_attach = false; @@ -1217,124 +1216,12 @@ void SigSession::data_feed_in(const struct sr_dev_inst *sdi, } void SigSession::data_feed_callback(const struct sr_dev_inst *sdi, - const struct sr_datafeed_packet *packet, void *cb_data) -{ - (void) cb_data; + const struct sr_datafeed_packet *packet) +{ assert(_session); _session->data_feed_in(sdi, packet); } -/* - * hotplug function - */ -void SigSession::hotplug_callback(void *ctx, void *dev, int event, void *user_data) -{ - if (_session != NULL){ - _session->on_hotplug_event(ctx, dev, event, user_data); - } -} - -void SigSession::on_hotplug_event(void *ctx, void *dev, int event, void *user_data) -{ - (void)ctx; - (void)dev; - (void)user_data; - - if (USB_EV_HOTPLUG_ATTACH != event && USB_EV_HOTPLUG_DETTACH != event){ - dsv_err("%s%d", "Unhandled event", event); - return; - } - - if (USB_EV_HOTPLUG_ATTACH == event) - { - _hot_attach = true; - _is_device_reattach = _is_wait_reattch; - _is_wait_reattch = false; //cancel detach event - } - else if (USB_EV_HOTPLUG_DETTACH == event) - { - dsv_dbg("%s", "Device detached, will wait it reattach."); - _wait_reattch_times = 0; - _is_wait_reattch = true; //wait the device reattach. - _is_device_reattach = false; - } -} - -void SigSession::hotplug_proc() -{ - if (!_dev_inst) - return; - - dsv_info("%s", "Hotplug thread start!"); - - try { - while(_session && !_bHotplugStop) { - - sr_hotplug_wait_timout(_sr_ctx); - - if (_hot_attach) { - dsv_info("%s", "process event: DreamSourceLab hardware attached!"); - _callback->device_attach(); - _hot_attach = false; - } - if (_hot_detach) { - dsv_info("%s", "process event: DreamSourceLab hardware detached!"); - _callback->device_detach(); - _hot_detach = false; - } - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - - if (_is_wait_reattch){ - _wait_reattch_times++; - - // 500ms - if (_wait_reattch_times == 70) - { - dsv_dbg("%s", "Wait the device reattach time out for 500ms"); - _hot_detach = true; - _is_wait_reattch = false; - } - } - } - } catch(...) { - dsv_err("%s", "Interrupt exception for hotplug thread was thrown."); - } - - dsv_info("%s", "Hotplug thread exit!"); -} - -void SigSession::register_hotplug_callback() -{ - if (sr_listen_hotplug(_sr_ctx, hotplug_callback, NULL) != 0){ - dsv_err("%s", "Error creating a hotplug callback!"); - } -} - -void SigSession::deregister_hotplug_callback() -{ - sr_close_hotplug(_sr_ctx); -} - -void SigSession::start_hotplug_work() -{ - // Begin the session - _hot_attach = false; - _hot_detach = false; - - if (_hotplug_thread.joinable()){ - return; - } - _hotplug_thread = std::thread(&SigSession::hotplug_proc, this); -} - -void SigSession::stop_hotplug_work() -{ - _bHotplugStop = true; - if (_hotplug_thread.joinable()){ - _hotplug_thread.join(); - } -} - uint16_t SigSession::get_ch_num(int type) { uint16_t num_channels = 0; @@ -1445,7 +1332,7 @@ bool SigSession::add_decoder(srd_decoder *const dec, bool silent, DecoderStatus return ret; } catch(...) { - ds_debug("Starting a hotplug thread...\n"); + } return false; @@ -1838,7 +1725,7 @@ void SigSession::set_stop_scale(float scale) void SigSession::Open() { - register_hotplug_callback(); + } void SigSession::Close() @@ -1852,8 +1739,6 @@ void SigSession::set_stop_scale(float scale) clear_all_decoder(); //clear all decode task, and stop decode thread - ds_trigger_destroy(); - if (_dev_inst) { _dev_inst->release(); @@ -1861,10 +1746,6 @@ void SigSession::set_stop_scale(float scale) // TODO: This should not be necessary _session = NULL; - - stop_hotplug_work(); - - deregister_hotplug_callback(); } //append a decode task, and try create a thread @@ -2006,5 +1887,66 @@ void SigSession::set_stop_scale(float scale) _bDecodeRunning = false; } + void SigSession::device_lib_event_callback(int event) + { + struct ds_device_info *array = NULL; + int count = 0; + + if (_session == NULL){ + dsv_err("%s", "device_lib_event_callback() error, _session is null."); + return; + } + + if (ds_device_get_list(&array, &count) != SR_OK){ + dsv_err("%s", "Failed to get device list!"); + return; + } + + _session->_callback->update_device_list(array, count); + + if (array != NULL){ + free(array); + } + } + + bool SigSession::init() + { + ds_log_set_context(dsv_log_context()); + + ds_set_event_callback(device_lib_event_callback); + + ds_set_datafeed_callback(data_feed_callback); + + QString resdir = GetResourceDir(); + char res_path[256] = {0}; +#ifdef _WIN32 + QTextCodec *codec = QTextCodec::codecForName("System"); + QByteArray str_tmp = codec->fromUnicode(resdir); + strncpy(res_path, str_tmp.data(), sizeof(res_path) - 1); +#else + strncpy(res_path, resdir.toUtf8().data(), sizeof(res_path) - 1); +#endif + ds_set_firmware_resource_dir(res_path); + + if (ds_lib_init() != SR_OK) + { + dsv_err("%s", "DSView run ERROR: collect lib init failed."); + return false; + } + + return true; + } + + void SigSession::uninit() + { + this->Close(); + + ds_lib_exit(); + } + + int SigSession::get_device_work_mode() + { + return ds_get_selected_device_index() + } } // namespace pv diff --git a/DSView/pv/sigsession.h b/DSView/pv/sigsession.h index 860f6efa..85605820 100644 --- a/DSView/pv/sigsession.h +++ b/DSView/pv/sigsession.h @@ -122,7 +122,7 @@ private: SigSession(SigSession &o); public: - explicit SigSession(DeviceManager *device_manager); + explicit SigSession(); ~SigSession(); @@ -179,8 +179,7 @@ public: void init_signals(); void add_group(); void del_group(); - void start_hotplug_work(); - void stop_hotplug_work(); + uint16_t get_ch_num(int type); bool get_instant(); @@ -259,11 +258,26 @@ public: inline void decode_done(){ _callback->decode_done(); - } + } - inline void set_sr_context(struct sr_context *ctx){ - _sr_ctx = ctx; - } + bool init(); + + void uninit(); + + void reload(); + void refresh(int holdtime); + void start_capture(bool instant); + void stop_capture(); + void check_update(); + void set_repeating(bool repeat); + void set_map_zoom(int index); + void auto_end(); + + inline bool is_device_re_attach(){ + return _is_device_reattach; + } + + int get_device_work_mode(); private: inline void data_updated(){ @@ -278,11 +292,9 @@ private: _callback->receive_data_len(len); } -private: + void set_capture_state(capture_state state); - void register_hotplug_callback(); - void deregister_hotplug_callback(); - + void add_decode_task(view::DecodeTrace *trace); void remove_decode_task(view::DecodeTrace *trace); void clear_all_decode_task(int &runningDex); @@ -321,31 +333,12 @@ private: const struct sr_datafeed_packet *packet); static void data_feed_callback(const struct sr_dev_inst *sdi, - const struct sr_datafeed_packet *packet, void *cb_data); + const struct sr_datafeed_packet *packet); - // thread for hotplug - void hotplug_proc(); - - static void hotplug_callback(void *ctx, void *dev, int event, void *user_data); - - void on_hotplug_event(void *ctx, void *dev, int event, void *user_data); - -public: - void reload(); - void refresh(int holdtime); - void start_capture(bool instant); - void stop_capture(); - void check_update(); - void set_repeating(bool repeat); - void set_map_zoom(int index); - void auto_end(); - - inline bool is_device_re_attach(){ - return _is_device_reattach; - } + static void device_lib_event_callback(int event); + private: - DeviceManager *_device_manager; /** * The device instance that will be used in the next capture session. @@ -354,8 +347,7 @@ private: mutable std::mutex _sampling_mutex; mutable std::mutex _data_mutex; mutable std::mutex _decode_task_mutex; - - std::thread _hotplug_thread; + std::thread _sampling_thread; std::thread _decode_thread; diff --git a/DSView/pv/toolbars/samplingbar.cpp b/DSView/pv/toolbars/samplingbar.cpp index 520a471b..d19cfc85 100644 --- a/DSView/pv/toolbars/samplingbar.cpp +++ b/DSView/pv/toolbars/samplingbar.cpp @@ -68,6 +68,8 @@ SamplingBar::SamplingBar(SigSession *session, QWidget *parent) : _mode_button(this), _instant(false) { + _is_seting = false; + setMovable(false); setContentsMargins(0,0,0,0); layout()->setSpacing(0); @@ -1034,5 +1036,19 @@ void SamplingBar::on_mode() } } + void SamplingBar::update_device_list(struct ds_device_info *array, int count, int select_index) + { + if (array == NULL || count == 0){ + return; + } + _is_seting = true; + + for (int i=0; idev_list) { - sr_err("No dev_list in driver %d ('%s').", i, d); - errors++; - } + /* Note: config_get() is optional. */ if (!drivers[i]->config_set) { sr_err("No config_set in driver %d ('%s').", i, d); diff --git a/libsigrok4DSL/dsdevice.c b/libsigrok4DSL/dsdevice.c index 66cb4f90..f053eea4 100644 --- a/libsigrok4DSL/dsdevice.c +++ b/libsigrok4DSL/dsdevice.c @@ -315,50 +315,6 @@ SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial) g_free(serial->serialcomm); g_free(serial); } - -SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver) -{ - if (driver && driver->dev_list) - return driver->dev_list(); - else - return NULL; -} - -SR_API const GSList *sr_dev_mode_list(const struct sr_dev_inst *sdi) -{ - if (sdi && sdi->driver && sdi->driver->dev_mode_list) - return sdi->driver->dev_mode_list(sdi); - else - return NULL; -} - -SR_API int sr_dev_clear(const struct sr_dev_driver *driver) -{ - return SR_OK; -} - -SR_API int sr_dev_open(struct sr_dev_inst *sdi) -{ - int ret; - - if (!sdi || !sdi->driver || !sdi->driver->dev_open) - return SR_ERR; - - ret = sdi->driver->dev_open(sdi); - - return ret; -} - -SR_API int sr_dev_close(struct sr_dev_inst *sdi) -{ - int ret; - if (!sdi || !sdi->driver || !sdi->driver->dev_close) - return SR_ERR; - - ret = sdi->driver->dev_close(sdi); - - return ret; -} /** @} */ diff --git a/libsigrok4DSL/hardware/DSL/dscope.c b/libsigrok4DSL/hardware/DSL/dscope.c index 4de810f9..769860c2 100644 --- a/libsigrok4DSL/hardware/DSL/dscope.c +++ b/libsigrok4DSL/hardware/DSL/dscope.c @@ -283,12 +283,11 @@ static GSList *scan(GSList *options) address = libusb_get_device_address(device_handle); sr_info("Found a new device,handle:%p,bus:%d,address:%d", device_handle, bus, address); - devcnt = g_slist_length(drvc->instances); devc = DSCope_dev_new(prof); if (!devc) return NULL; - sdi = sr_dev_inst_new(channel_modes[devc->ch_mode].mode, devcnt, SR_ST_INITIALIZING, + sdi = sr_dev_inst_new(channel_modes[devc->ch_mode].mode, 0, SR_ST_INITIALIZING, prof->vendor, prof->model, prof->model_version); if (!sdi) { @@ -354,11 +353,6 @@ static GSList *scan(GSList *options) return devices; } -static GSList *dev_list(void) -{ - return ((struct drv_context *)(di->priv))->instances; -} - static const GSList *dev_mode_list(const struct sr_dev_inst *sdi) { return dsl_mode_list(sdi); @@ -2109,8 +2103,7 @@ SR_PRIV struct sr_dev_driver DSCope_driver_info = { .driver_type = DRIVER_TYPE_HARDWARE, .init = init, .cleanup = cleanup, - .scan = scan, - .dev_list = dev_list, + .scan = scan, .dev_mode_list = dev_mode_list, .config_get = config_get, .config_set = config_set, diff --git a/libsigrok4DSL/hardware/DSL/dslogic.c b/libsigrok4DSL/hardware/DSL/dslogic.c index 2b614579..f5607021 100644 --- a/libsigrok4DSL/hardware/DSL/dslogic.c +++ b/libsigrok4DSL/hardware/DSL/dslogic.c @@ -422,11 +422,6 @@ static GSList *scan(GSList *options) return devices; } -static GSList *dev_list(void) -{ - return ((struct drv_context *)(di->priv))->instances; -} - static const GSList *dev_mode_list(const struct sr_dev_inst *sdi) { return dsl_mode_list(sdi); @@ -1446,8 +1441,7 @@ SR_PRIV struct sr_dev_driver DSLogic_driver_info = { .driver_type = DRIVER_TYPE_HARDWARE, .init = init, .cleanup = cleanup, - .scan = scan, - .dev_list = dev_list, + .scan = scan, .dev_mode_list = dev_mode_list, .config_get = config_get, .config_set = config_set, diff --git a/libsigrok4DSL/hardware/demo/demo.c b/libsigrok4DSL/hardware/demo/demo.c index b82f1409..c5dc062a 100644 --- a/libsigrok4DSL/hardware/demo/demo.c +++ b/libsigrok4DSL/hardware/demo/demo.c @@ -167,19 +167,12 @@ static GSList *hw_scan(GSList *options) sdi->driver = di; sdi->dev_type = DEV_TYPE_DEMO; - devices = g_slist_append(devices, sdi); - drvc->instances = g_slist_append(drvc->instances, sdi); - + devices = g_slist_append(devices, sdi); setup_probes(sdi, channel_modes[devc->ch_mode].num); return devices; } -static GSList *hw_dev_list(void) -{ - return ((struct drv_context *)(di->priv))->instances; -} - static const GSList *hw_dev_mode_list(const struct sr_dev_inst *sdi) { struct demo_context *devc; @@ -1073,8 +1066,7 @@ SR_PRIV struct sr_dev_driver demo_driver_info = { .driver_type = DRIVER_TYPE_DEMO, .init = hw_init, .cleanup = hw_cleanup, - .scan = hw_scan, - .dev_list = hw_dev_list, + .scan = hw_scan, .dev_mode_list = hw_dev_mode_list, .config_get = config_get, .config_set = config_set, diff --git a/libsigrok4DSL/hwdriver.c b/libsigrok4DSL/hwdriver.c index 5c1ae180..44127103 100644 --- a/libsigrok4DSL/hwdriver.c +++ b/libsigrok4DSL/hwdriver.c @@ -148,7 +148,7 @@ static struct sr_dev_driver *drivers_list[] = { * * @return Pointer to the NULL-terminated list of hardware driver pointers. */ -SR_API struct sr_dev_driver **sr_driver_list(void) +SR_PRIV struct sr_dev_driver **sr_driver_list(void) { return drivers_list; @@ -159,7 +159,6 @@ SR_API struct sr_dev_driver **sr_driver_list(void) * * This usually involves memory allocations and variable initializations * within the driver, but _not_ scanning for attached devices. - * The API call sr_driver_scan() is used for that. * * @param ctx A libsigrok context object allocated by a previous call to * sr_init(). Must not be NULL. @@ -170,7 +169,7 @@ SR_API struct sr_dev_driver **sr_driver_list(void) * SR_ERR_BUG upon internal errors, or another negative error code * upon other errors. */ -SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) +SR_PRIV int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) { int ret; @@ -191,51 +190,6 @@ SR_API int sr_driver_init(struct sr_context *ctx, struct sr_dev_driver *driver) return ret; } -/** - * Tell a hardware driver to scan for devices. - * - * In addition to the detection, the devices that are found are also - * initialized automatically. On some devices, this involves a firmware upload, - * or other such measures. - * - * The order in which the system is scanned for devices is not specified. The - * caller should not assume or rely on any specific order. - * - * Before calling sr_driver_scan(), the user must have previously initialized - * the driver by calling sr_driver_init(). - * - * @param driver The driver that should scan. This must be a pointer to one of - * the entries returned by sr_driver_list(). Must not be NULL. - * @param options A list of 'struct sr_hwopt' options to pass to the driver's - * scanner. Can be NULL/empty. - * - * @return A GSList * of 'struct sr_dev_inst', or NULL if no devices were - * found (or errors were encountered). This list must be freed by the - * caller using g_slist_free(), but without freeing the data pointed - * to in the list. - */ -SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options) -{ - GSList *l; - - if (!driver) { - sr_err("Invalid driver, can't scan for devices."); - return NULL; - } - - if (!driver->priv) { - sr_err("Driver not initialized, can't scan for devices."); - return NULL; - } - - l = driver->scan(options); - - sr_detail("Scan of '%s' found %d devices.", driver->name, - g_slist_length(l)); - - return l; -} - /** @private */ SR_PRIV void sr_hw_cleanup_all(void) { diff --git a/libsigrok4DSL/lib_main.c b/libsigrok4DSL/lib_main.c index 77f89644..a7942c2d 100644 --- a/libsigrok4DSL/lib_main.c +++ b/libsigrok4DSL/lib_main.c @@ -52,11 +52,12 @@ struct sr_lib_context int check_reconnect_times; struct libusb_device *attach_device_handle; struct libusb_device *detach_device_handle; - struct ds_device_info current_device_info; - struct sr_dev_inst *current_device_instance; + struct ds_device_info actived_device_info; + struct sr_dev_inst *actived_device_instance; int collect_stop_flag; GThread *collect_thread; ds_datafeed_callback_t data_forward_callback; + int callback_thread_count; }; static void hotplug_event_listen_callback(struct libusb_context *ctx, struct libusb_device *dev, int event); @@ -65,6 +66,7 @@ static void destroy_device_instance(struct sr_dev_inst *dev); static void close_device_instance(struct sr_dev_inst *dev); static int open_device_instance(struct sr_dev_inst *dev); static void collect_run_proc(); +static void post_event_async(int event); static struct sr_lib_context lib_ctx = { .event_callback = NULL, @@ -78,16 +80,16 @@ static struct sr_lib_context lib_ctx = { .check_reconnect_times = 0, .attach_device_handle = NULL, .detach_device_handle = NULL, - .current_device_info = { + .actived_device_info = { .handle = 0, .name[0] = '\0', - .is_current = 0, .dev_type = DEV_TYPE_UNKOWN, }, - .current_device_instance = NULL, + .actived_device_instance = NULL, .collect_stop_flag = 0, .data_forward_callback = NULL, .collect_thread = NULL, + .callback_thread_count = 0, }; /** @@ -157,7 +159,7 @@ SR_API int ds_lib_exit() if (lib_ctx.collect_thread != NULL) { - ds_device_stop_collect(); // stop collect. + ds_stop_collect(); // stop collect. } sr_close_hotplug(lib_ctx.sr_ctx); @@ -224,7 +226,7 @@ SR_API void ds_set_event_callback(dslib_event_callback_t cb) /** * Set the data receive callback. */ -SR_API int ds_set_datafeed_callback(ds_datafeed_callback_t cb) +SR_API void ds_set_datafeed_callback(ds_datafeed_callback_t cb) { lib_ctx.data_forward_callback = cb; } @@ -233,7 +235,7 @@ SR_API int ds_set_datafeed_callback(ds_datafeed_callback_t cb) * Get the device list, if the field _handle is 0, the list visited to end. * User need call free() to release the buffer. If the list is empty, the out_list is null. */ -SR_API int ds_device_get_list(struct ds_device_info **out_list, int *out_count) +SR_API int ds_get_device_list(struct ds_device_info **out_list, int *out_count) { int num; struct ds_device_info *array = NULL; @@ -271,7 +273,6 @@ SR_API int ds_device_get_list(struct ds_device_info **out_list, int *out_count) p->handle = dev->handle; strncpy(p->name, dev->name, sizeof(dev->name) - 1); p->dev_type = dev->dev_type; - p->is_current = (dev == lib_ctx.current_device_instance); p++; } @@ -290,9 +291,9 @@ SR_API int ds_device_get_list(struct ds_device_info **out_list, int *out_count) } /** - * Active a device, if success, it will trigs the event of SR_EV_CURRENT_DEVICE_CHANGED. + * Active a device. */ -SR_API int ds_device_select(ds_device_handle handle) +SR_API int ds_active_device(ds_device_handle handle) { GList *l; struct sr_dev_inst *dev; @@ -315,15 +316,15 @@ SR_API int ds_device_select(ds_device_handle handle) pthread_mutex_lock(&lib_ctx.mutext); - if (lib_ctx.current_device_instance != NULL) + if (lib_ctx.actived_device_instance != NULL) { - sr_info("Close the previous device \"%s\"", lib_ctx.current_device_instance->name); + sr_info("Close the previous device \"%s\"", lib_ctx.actived_device_instance->name); - close_device_instance(lib_ctx.current_device_instance); - lib_ctx.current_device_instance = NULL; - lib_ctx.current_device_info.handle = NULL; - lib_ctx.current_device_info.name[0] = '\0'; + close_device_instance(lib_ctx.actived_device_instance); + lib_ctx.actived_device_instance = NULL; + lib_ctx.actived_device_info.handle = NULL; + lib_ctx.actived_device_info.name[0] = '\0'; } // To open the new. @@ -343,11 +344,10 @@ SR_API int ds_device_select(ds_device_handle handle) if (open_device_instance(dev) == SR_OK) { - lib_ctx.current_device_info.handle = dev->handle; - lib_ctx.current_device_info.dev_type = dev->dev_type; - lib_ctx.current_device_info.is_current = 1; - strncpy(lib_ctx.current_device_info.name, dev->name, sizeof(lib_ctx.current_device_info.name) - 1); - lib_ctx.current_device_instance = dev; + lib_ctx.actived_device_info.handle = dev->handle; + lib_ctx.actived_device_info.dev_type = dev->dev_type; + strncpy(lib_ctx.actived_device_info.name, dev->name, sizeof(lib_ctx.actived_device_info.name) - 1); + lib_ctx.actived_device_instance = dev; } else { @@ -364,7 +364,7 @@ SR_API int ds_device_select(ds_device_handle handle) if (!bFind) { - sr_err("ds_device_select() error, can't find the device."); + sr_err("ds_active_device() error, can't find the device."); return SR_ERR_CALL_STATUS; } @@ -372,10 +372,10 @@ SR_API int ds_device_select(ds_device_handle handle) } /** - * Active a device, if success, it will trigs the event of SR_EV_CURRENT_DEVICE_CHANGED. - * @index is -1, will select the last one. + * Active a device + * if @index is -1, will select the last one. */ -SR_API int ds_device_select_by_index(int index) +SR_API int ds_active_device_by_index(int index) { GList *l; struct sr_dev_inst *dev; @@ -407,28 +407,96 @@ SR_API int ds_device_select_by_index(int index) if (handle == NULL) { - sr_err("%s", "ds_device_select_by_index(), index is error!"); + sr_err("%s", "ds_active_device_by_index(), index is error!"); return SR_ERR_CALL_STATUS; } - return ds_device_select(handle); + return ds_active_device(handle); +} + +/** + * Get the selected device index. + */ +SR_API int ds_get_actived_device_index() +{ + int dex = -1; + GList *l; + int i = 0; + + if (lib_ctx.actived_device_instance == NULL){ + return -1; + } + + pthread_mutex_lock(&lib_ctx.mutext); + + for (l = lib_ctx.device_list; l; l = l->next) + { + if ((struct sr_dev_inst *)l->data == lib_ctx.actived_device_instance){ + dex = i; + break; + } + i++; + } + + pthread_mutex_unlock(&lib_ctx.mutext); + + return dex; +} + +/** + * Get the decive supports work mode, mode list: LOGIC、ANALOG、DSO + * return type see struct sr_dev_mode. + */ +SR_API const GSList *ds_get_device_mode_list(ds_device_handle handle) +{ + GList *l; + struct sr_dev_inst *dev; + struct sr_dev_inst *fd_dev; + fd_dev = NULL; + + pthread_mutex_lock(&lib_ctx.mutext); + + for (l = lib_ctx.device_list; l; l = l->next){ + dev = l->data; + if (dev->handle == handle){ + fd_dev = dev; + break; + } + } + + pthread_mutex_unlock(&lib_ctx.mutext); + + if (dev && dev->driver && dev->driver->dev_mode_list) + return dev->driver->dev_mode_list(dev); + + sr_err("%s", "ds_device_get_mode_list() error, can't find the device."); + + return NULL; } /** * Remove one device from the list, and destory it. - * User need to call ds_device_get_list() to get the new list. + * User need to call ds_get_device_list() to get the new list. */ SR_API int ds_remove_device(ds_device_handle handle) { GList *l; struct sr_dev_inst *dev; + struct sr_dev_inst *di; int bFind = 0; + + di = lib_ctx.actived_device_instance; if (handle == NULL) { return SR_ERR_ARG; } + if (di != NULL && di->handle == handle && ds_is_collecting()){ + sr_err("%s", "Device is collecting, can't remove it."); + return SR_ERR_CALL_STATUS; + } + pthread_mutex_lock(&lib_ctx.mutext); for (l = lib_ctx.device_list; l; l = l->next) @@ -438,14 +506,14 @@ SR_API int ds_remove_device(ds_device_handle handle) { lib_ctx.device_list = g_slist_remove(lib_ctx.device_list, l->data); - if (dev == lib_ctx.current_device_instance) + if (dev == lib_ctx.actived_device_instance) { - lib_ctx.current_device_instance = NULL; + lib_ctx.actived_device_instance = NULL; } - if (lib_ctx.current_device_info.handle == dev->handle) + if (lib_ctx.actived_device_info.handle == dev->handle) { - lib_ctx.current_device_info.handle = 0; - lib_ctx.current_device_info.name[0] = '\0'; + lib_ctx.actived_device_info.handle = 0; + lib_ctx.actived_device_info.name[0] = '\0'; } destroy_device_instance(dev); @@ -463,42 +531,56 @@ SR_API int ds_remove_device(ds_device_handle handle) } /** - * Get the current device info. - * If the current device is not exists, the handle filed will be set null. + * Get the actived device info. + * If the actived device is not exists, the handle filed will be set null. */ -SR_API int ds_get_current_device_info(struct ds_device_info *info) +SR_API int ds_get_actived_device_info(struct ds_device_info *fill_info) { - if (info == NULL) + if (fill_info == NULL) { return SR_ERR_ARG; } - info->handle = NULL; - info->name[0] = '\0'; - info->is_current = 0; - info->dev_type = DEV_TYPE_UNKOWN; + fill_info->handle = NULL; + fill_info->name[0] = '\0'; + fill_info->dev_type = DEV_TYPE_UNKOWN; - if (lib_ctx.current_device_info.handle != NULL) + if (lib_ctx.actived_device_info.handle != NULL) { - info->handle = lib_ctx.current_device_info.handle; - strncpy(info->name, lib_ctx.current_device_info.name, sizeof(info->name)); - info->is_current = 1; - info->dev_type = lib_ctx.current_device_info.dev_type; + fill_info->handle = lib_ctx.actived_device_info.handle; + strncpy(fill_info->name, lib_ctx.actived_device_info.name, sizeof(fill_info->name)); + fill_info->dev_type = lib_ctx.actived_device_info.dev_type; } return SR_OK; } +/** + * Get actived device work model. mode list:LOGIC、ANALOG、DSO + */ +SR_API int ds_get_actived_device_mode() +{ + int mode = UNKNOWN_DSL_MODE; + pthread_mutex_lock(&lib_ctx.mutext); + + if (lib_ctx.actived_device_instance != NULL){ + mode = lib_ctx.actived_device_instance->mode; + } + pthread_mutex_unlock(&lib_ctx.mutext); + + return mode; +} + /** * Start collect data */ -SR_API int ds_device_start_collect() +SR_API int ds_start_collect() { int ret; struct sr_dev_inst *di; - di = lib_ctx.current_device_instance; + di = lib_ctx.actived_device_instance; - sr_info("%s", "Start collection."); + sr_info("%s", "Start collect."); if (lib_ctx.collect_thread != NULL) { @@ -547,7 +629,7 @@ static void collect_run_proc() { int ret; struct sr_dev_inst *di; - di = lib_ctx.current_device_instance; + di = lib_ctx.actived_device_instance; sr_info("%s", "Collect thread start."); @@ -583,12 +665,12 @@ END: /** * Stop collect data */ -SR_API int ds_device_stop_collect() +SR_API int ds_stop_collect() { struct sr_dev_inst *di; - di = lib_ctx.current_device_instance; + di = lib_ctx.actived_device_instance; - sr_info("%s", "Stop collection."); + sr_info("%s", "Stop collect."); if (lib_ctx.collect_thread == NULL) { @@ -617,9 +699,9 @@ int ds_trigger_is_enabled() GSList *l; struct sr_channel *p; - if (lib_ctx.current_device_instance != NULL) + if (lib_ctx.actived_device_instance != NULL) { - for (l = lib_ctx.current_device_instance->channels; l; l = l->next) + for (l = lib_ctx.actived_device_instance->channels; l; l = l->next) { p = (struct sr_channel *)l->data; if (p->trigger && p->trigger[0] != '\0') @@ -637,9 +719,9 @@ int ds_channel_is_enabled() GSList *l; struct sr_channel *p; - if (lib_ctx.current_device_instance != NULL) + if (lib_ctx.actived_device_instance != NULL) { - for (l = lib_ctx.current_device_instance->channels; l; l = l->next) + for (l = lib_ctx.actived_device_instance->channels; l; l = l->next) { p = (struct sr_channel *)l->data; if (p->enabled) @@ -712,7 +794,7 @@ SR_PRIV int ds_data_forward(const struct sr_dev_inst *sdi, SR_PRIV int current_device_acquisition_stop() { struct sr_dev_inst *di; - di = lib_ctx.current_device_instance; + di = lib_ctx.actived_device_instance; if (di != NULL && di->driver && di->driver->dev_acquisition_stop) { return di->driver->dev_acquisition_stop(di, (void *)di); @@ -720,6 +802,17 @@ SR_PRIV int current_device_acquisition_stop() return SR_ERR; } +/** + * Check if the device is collecting. + */ +SR_API int ds_is_collecting() +{ + if (lib_ctx.collect_thread != NULL){ + return 1; + } + return 0; +} + /**--------------------internal function end------------------*/ /**-------------------private function ---------------*/ @@ -743,7 +836,7 @@ static int update_device_handle(struct libusb_device *old_dev, struct libusb_dev { // Release the old device and the resource. - if (dev == lib_ctx.current_device_instance) + if (dev == lib_ctx.actived_device_instance) { sr_info("%s", "Release the old device's resource."); close_device_instance(dev); @@ -758,7 +851,7 @@ static int update_device_handle(struct libusb_device *old_dev, struct libusb_dev bFind = 1; // Reopen the device. - if (dev == lib_ctx.current_device_instance) + if (dev == lib_ctx.actived_device_instance) { sr_info("%s", "Reopen the current device."); open_device_instance(dev); @@ -834,9 +927,6 @@ static void hotplug_event_listen_callback(struct libusb_context *ctx, struct lib lib_ctx.detach_device_handle); } - if (lib_ctx.current_device_info.handle != NULL && lib_ctx.current_device_info.dev_type == DEV_TYPE_USB) - { - } /** * Begin to wait the device reconnect, if timeout, will process the detach event. */ @@ -898,10 +988,10 @@ static void process_attach_event() drivers++; } - if (lib_ctx.event_callback != NULL && num > 0) - { - // Tell user one new device attched, and the list is updated. - lib_ctx.event_callback(DS_EV_NEW_DEVICE_ATTACH); + // Tell user one new device attched, and the list is updated. + + if (num > 0){ + post_event_async(DS_EV_NEW_DEVICE_ATTACH); } lib_ctx.attach_device_handle = NULL; @@ -938,23 +1028,20 @@ static void process_detach_event() // Found the device, and remove it. lib_ctx.device_list = g_slist_remove(lib_ctx.device_list, l->data); destroy_device_instance(dev); - if (dev == lib_ctx.current_device_instance) + if (dev == lib_ctx.actived_device_instance) { - lib_ctx.current_device_instance = NULL; + lib_ctx.actived_device_instance = NULL; } break; } } pthread_mutex_unlock(&lib_ctx.mutext); - if (ev_dev == lib_ctx.current_device_info.handle) + if (ev_dev == lib_ctx.actived_device_info.handle) ev = DS_EV_CURRENT_DEVICE_DETACH; - if (lib_ctx.event_callback != NULL) - { - // Tell user one new device detached, and the list is updated. - lib_ctx.event_callback(ev); - } + // Tell user a new device detached, and the list is updated. + post_event_async(ev); } static void usb_hotplug_process_proc() @@ -965,13 +1052,11 @@ static void usb_hotplug_process_proc() { sr_hotplug_wait_timout(lib_ctx.sr_ctx); - if (lib_ctx.attach_event_flag) - { + if (lib_ctx.attach_event_flag){ process_attach_event(); lib_ctx.attach_event_flag = 0; } - if (lib_ctx.detach_event_flag) - { + if (lib_ctx.detach_event_flag){ process_detach_event(); lib_ctx.detach_event_flag = 0; } @@ -981,10 +1066,8 @@ static void usb_hotplug_process_proc() if (lib_ctx.is_waitting_reconnect) { lib_ctx.check_reconnect_times++; - // 500ms - if (lib_ctx.check_reconnect_times == 5) - { + if (lib_ctx.check_reconnect_times == 5){ // Device loose contact,wait for it reconnection timeout. lib_ctx.detach_event_flag = 1; // use detach event lib_ctx.is_waitting_reconnect = 0; @@ -992,6 +1075,16 @@ static void usb_hotplug_process_proc() } } + if (lib_ctx.callback_thread_count > 0){ + sr_info("%d callback thread is actived, waiting all ends...", lib_ctx.callback_thread_count); + } + + // Wait all callback thread end. + while (lib_ctx.callback_thread_count > 0) + { + _sleep(100); + } + sr_info("%s", "Hotplug thread end!"); } @@ -1044,4 +1137,24 @@ static int open_device_instance(struct sr_dev_inst *dev) return SR_ERR_CALL_STATUS; } +static void post_event_proc(int event) +{ + if (lib_ctx.event_callback != NULL){ + lib_ctx.event_callback(event); + } + + pthread_mutex_lock(&lib_ctx.mutext); + lib_ctx.callback_thread_count--;; + pthread_mutex_unlock(&lib_ctx.mutext); +} + +static void post_event_async(int event) +{ + pthread_mutex_lock(&lib_ctx.mutext); + lib_ctx.callback_thread_count++; + pthread_mutex_unlock(&lib_ctx.mutext); + + g_thread_new("callback_thread", post_event_proc, event); +} + /**-------------------private function end---------------*/ \ No newline at end of file diff --git a/libsigrok4DSL/libsigrok-internal.h b/libsigrok4DSL/libsigrok-internal.h index dd4ea866..f42945cc 100644 --- a/libsigrok4DSL/libsigrok-internal.h +++ b/libsigrok4DSL/libsigrok-internal.h @@ -91,7 +91,6 @@ struct sr_dev_driver { int (*init) (struct sr_context *sr_ctx); int (*cleanup) (void); GSList *(*scan) (GSList *options); - GSList *(*dev_list) (void); const GSList *(*dev_mode_list) (const struct sr_dev_inst *sdi); int (*config_get) (int id, GVariant **data, @@ -340,12 +339,25 @@ SR_PRIV GSList *sr_usb_find(libusb_context *usb_ctx, const char *conn); /*--- backend.c -------------------------------------------------------------*/ SR_PRIV int sr_init(struct sr_context **ctx); -SR_PRIV int sr_exit(struct sr_context *ctx); +SR_PRIV int sr_exit(struct sr_context *ctx); SR_PRIV int sr_listen_hotplug(struct sr_context *ctx, hotplug_event_callback callback); SR_PRIV int sr_close_hotplug(struct sr_context *ctx); SR_PRIV void sr_hotplug_wait_timout(struct sr_context *ctx); +/**---------------driver ----*/ +SR_PRIV struct sr_dev_driver **sr_driver_list(void); +SR_PRIV int sr_driver_init(struct sr_context *ctx, + struct sr_dev_driver *driver); + +/*----- Session ------*/ +SR_PRIV int sr_session_run(void); +SR_PRIV int sr_session_stop(void); +SR_PRIV struct sr_session *sr_session_new(void); +SR_PRIV int sr_session_destroy(void); +/* Session setup */ +SR_PRIV int sr_session_load(const char *filename); + /*--- lib_main.c -------------------------------------------------*/ /** * Check whether the USB device is in the device list. diff --git a/libsigrok4DSL/libsigrok.h b/libsigrok4DSL/libsigrok.h index e02754f1..72bb2fe1 100644 --- a/libsigrok4DSL/libsigrok.h +++ b/libsigrok4DSL/libsigrok.h @@ -621,10 +621,11 @@ enum CHANNEL_TYPE { SR_CHANNEL_MATH, }; -enum OPERATION_MODE { +enum OPERATION_MODE { LOGIC = 0, DSO = 1, ANALOG = 2, + UNKNOWN_DSL_MODE = 99, }; struct sr_channel { @@ -1171,13 +1172,7 @@ struct ds_trigger_pos { * @file * * Header file containing API function prototypes. - */ - -/*--- backend.c -------------------------------------------------------------*/ - -//@event, 1:attach, 2:left -SR_PRIV int sr_init(struct sr_context **ctx); -SR_PRIV int sr_exit(struct sr_context *ctx); + */ /*--- device.c --------------------------------------------------------------*/ @@ -1187,18 +1182,11 @@ SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum, gboolean state); SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, uint16_t probenum, const char *trigger); -SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver); -SR_API const GSList *sr_dev_mode_list(const struct sr_dev_inst *sdi); -SR_API int sr_dev_clear(const struct sr_dev_driver *driver); -SR_API int sr_dev_open(struct sr_dev_inst *sdi); -SR_API int sr_dev_close(struct sr_dev_inst *sdi); + /*--- hwdriver.c ------------------------------------------------------------*/ -SR_API struct sr_dev_driver **sr_driver_list(void); -SR_API int sr_driver_init(struct sr_context *ctx, - struct sr_dev_driver *driver); -SR_API GSList *sr_driver_scan(struct sr_dev_driver *driver, GSList *options); + SR_API int sr_config_get(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel *ch, @@ -1218,22 +1206,6 @@ SR_API int sr_status_get(const struct sr_dev_inst *sdi, struct sr_status *status SR_API struct sr_config *sr_config_new(int key, GVariant *data); SR_API void sr_config_free(struct sr_config *src); -//SR_API void sr_test_usb_api(); - -/*--------------------session.c----------------*/ -typedef void (*sr_datafeed_callback_t)(const struct sr_dev_inst *sdi, - const struct sr_datafeed_packet *packet, void *cb_data); - - -/* Session setup */ -SR_API int sr_session_load(const char *filename); -SR_API struct sr_session *sr_session_new(void); -SR_API int sr_session_destroy(void); - -/* Session control */ -SR_API int sr_session_run(void); -SR_API int sr_session_stop(void); - /*--- input/input.c ---------------------------------------------------------*/ @@ -1296,22 +1268,18 @@ SR_API void ds_log_level(int level); /*---event define ---------------------------------------------*/ enum dslib_event_type { - // A new device attached, user need to call ds_device_get_list to get the list, + // A new device attached, user need to call ds_get_device_list to get the list, // the last one is new. - // User can call ds_device_select() to switch to the current device. + // User can call ds_active_device() to switch to the current device. DS_EV_NEW_DEVICE_ATTACH = 0, - // The current device detached, user need to call ds_device_get_list to get the list, - // and call ds_device_select() to switch to the current device. + // The current device detached, user need to call ds_get_device_list to get the list, + // and call ds_active_device() to switch to the current device. DS_EV_CURRENT_DEVICE_DETACH = 1, // A inactive device detached. - // User can call ds_device_get_list() to get the new list, and update the list view. + // User can call ds_get_device_list() to get the new list, and update the list view. DS_EV_INACTIVE_DEVICE_DETACH = 2, - - // The current device switch success. - // User can call ds_device_get_list() to get new list, and update the data view. - DS_EV_CURRENT_DEVICE_CHANGED = 3, }; typedef unsigned long long ds_device_handle; @@ -1324,7 +1292,6 @@ struct ds_device_info ds_device_handle handle; char name[50]; int dev_type; // enum sr_device_type - int is_current; //is actived }; struct ds_task_progress @@ -1381,7 +1348,7 @@ SR_API void ds_set_event_callback(dslib_event_callback_t cb); /** * Set the data receive callback. */ -SR_API int ds_set_datafeed_callback(ds_datafeed_callback_t cb); +SR_API void ds_set_datafeed_callback(ds_datafeed_callback_t cb); /** * Set the firmware binary file directory, @@ -1393,45 +1360,66 @@ SR_API void ds_set_firmware_resource_dir(const char *dir); * Get the device list, if the field _handle is 0, the list visited to end. * User need call free() to release the buffer. If the list is empty, the out_list is null. */ -SR_API int ds_device_get_list(struct ds_device_info** out_list, int *out_count); +SR_API int ds_get_device_list(struct ds_device_info** out_list, int *out_count); /** - * Active a device, if success, it will trigs the event of SR_EV_CURRENT_DEVICE_CHANGED. + * Active a device. */ -SR_API int ds_device_select(ds_device_handle handle); +SR_API int ds_active_device(ds_device_handle handle); /** - * Active a device, if success, it will trigs the event of SR_EV_CURRENT_DEVICE_CHANGED. - * @index is -1, will select the last one. + * Active a device, + * if @index is -1, will select the last one. */ -SR_API int ds_device_select_by_index(int index); +SR_API int ds_active_device_by_index(int index); /** - * Create a device from session file, it auto load the data. + * Get the selected device index. + */ +SR_API int ds_get_actived_device_index(); + +/** + * Create a device from session file, and will auto load the data. */ SR_API int ds_device_from_file(const char *file_path); +/** + * Get the decive supports work mode, mode list: LOGIC、ANALOG、DSO + * return type see struct sr_dev_mode. + */ +SR_API const GSList *ds_get_device_mode_list(ds_device_handle handle); + /** * Remove one device from the list, and destory it. - * User need to call ds_device_get_list() to get the new list. + * User need to call ds_get_device_list() to get the new list. */ SR_API int ds_remove_device(ds_device_handle handle); /** - * Get the current device info. - * If the current device is not exists, the handle filed will be set null. + * Get the actived device info. + * If the actived device is not exists, the handle filed will be set null. */ -SR_API int ds_get_current_device_info(struct ds_device_info *info); +SR_API int ds_get_actived_device_info(struct ds_device_info *fill_info); + +/** + * Get actived device work model. mode list:LOGIC、ANALOG、DSO + */ +SR_API int ds_get_actived_device_mode(); /** * Start collect data */ -SR_API int ds_device_start_collect(); +SR_API int ds_start_collect(); /** * Stop collect data */ -SR_API int ds_device_stop_collect(); +SR_API int ds_stop_collect(); + +/** + * Check if the device is collecting. + */ +SR_API int ds_is_collecting(); #ifdef __cplusplus diff --git a/libsigrok4DSL/session.c b/libsigrok4DSL/session.c index 217c9051..d75f5736 100644 --- a/libsigrok4DSL/session.c +++ b/libsigrok4DSL/session.c @@ -58,11 +58,7 @@ struct source { */ gintptr poll_object; }; - -struct datafeed_callback { - sr_datafeed_callback_t cb; - void *cb_data; -}; + /** * Create a new session. @@ -72,7 +68,7 @@ struct datafeed_callback { * * @return A pointer to the newly allocated session, or NULL upon errors. */ -SR_API struct sr_session *sr_session_new(void) +SR_PRIV struct sr_session *sr_session_new(void) { if (session != NULL){ sr_info("%s", "Destroy the old session."); @@ -100,7 +96,7 @@ SR_API struct sr_session *sr_session_new(void) * * @return SR_OK upon success, SR_ERR_BUG if no session exists. */ -SR_API int sr_session_destroy(void) +SR_PRIV int sr_session_destroy(void) { if (session == NULL) { sr_detail("%s: session was NULL", __func__); @@ -192,7 +188,7 @@ static int sr_session_iteration(gboolean block) * * @return SR_OK upon success, SR_ERR_BUG upon errors. */ -SR_API int sr_session_run(void) +SR_PRIV int sr_session_run(void) { if (session == NULL) { sr_err("%s: session was NULL; a session must be " @@ -247,7 +243,7 @@ SR_API int sr_session_run(void) * * @return SR_OK upon success, SR_ERR_BUG if no session exists. */ -SR_API int sr_session_stop(void) +SR_PRIV int sr_session_stop(void) { if (!session) { sr_err("%s: session was NULL", __func__); diff --git a/libsigrok4DSL/session_driver.c b/libsigrok4DSL/session_driver.c index 028c7edc..5c5cf746 100644 --- a/libsigrok4DSL/session_driver.c +++ b/libsigrok4DSL/session_driver.c @@ -1023,8 +1023,7 @@ SR_PRIV struct sr_dev_driver session_driver = { .driver_type = DRIVER_TYPE_FILE, .init = init, .cleanup = dev_clear, - .scan = NULL, - .dev_list = NULL, + .scan = NULL, .dev_mode_list = dev_mode_list, .config_get = config_get, .config_set = config_set, diff --git a/libsigrok4DSL/session_file.c b/libsigrok4DSL/session_file.c index fb386341..8d1ec67b 100644 --- a/libsigrok4DSL/session_file.c +++ b/libsigrok4DSL/session_file.c @@ -58,7 +58,7 @@ extern SR_PRIV struct sr_dev_driver session_driver; * SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon * other errors. */ -SR_API int sr_session_load(const char *filename) +SR_PRIV int sr_session_load(const char *filename) { GKeyFile *kf; GPtrArray *capturefiles; @@ -158,7 +158,7 @@ SR_API int sr_session_load(const char *filename) if (devcnt == 0) /* first device, init the driver */ sdi->driver->init(NULL); - sr_dev_open(sdi); + //sr_dev_open(sdi); //sr_session_dev_add(sdi); g_assert(0);