From 82da3a6acd1aba361fe863cbbe72396a972f8da1 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Tue, 30 Nov 2021 14:17:47 +0800 Subject: [PATCH] make zip file in memory, function update --- DSView/pv/config/appconfig.cpp | 10 + DSView/pv/config/appconfig.h | 4 +- DSView/pv/device/device.cpp | 15 +- DSView/pv/device/devinst.cpp | 3 +- DSView/pv/device/sessionfile.cpp | 25 +- DSView/pv/devicemanager.cpp | 3 +- DSView/pv/dialogs/storeprogress.cpp | 6 +- DSView/pv/dialogs/storeprogress.h | 3 +- DSView/pv/interface/icallbacks.h | 6 + DSView/pv/mainwindow.cpp | 72 +++--- DSView/pv/mainwindow.h | 9 +- DSView/pv/sigsession.cpp | 8 +- DSView/pv/storesession.cpp | 382 ++++++++++++++++------------ DSView/pv/storesession.h | 18 +- libsigrok4DSL/dsdevice.c | 2 +- libsigrok4DSL/proto.h | 3 + libsigrok4DSL/session.c | 11 + qtpro/DSView.pro | 1 + qtpro/DSView.pro.user | 2 +- 19 files changed, 333 insertions(+), 250 deletions(-) diff --git a/DSView/pv/config/appconfig.cpp b/DSView/pv/config/appconfig.cpp index 920300fe..75fae622 100644 --- a/DSView/pv/config/appconfig.cpp +++ b/DSView/pv/config/appconfig.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #define MAX_PROTOCOL_FORMAT_LIST 15 @@ -314,3 +315,12 @@ QString GetResourceDir(){ qDebug() << "dir is not exists:" << dir.absolutePath(); assert(false); } + +QString GetUserDataDir() +{ + #if QT_VERSION >= 0x050400 + return QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); + #else + return QStandardPaths::writableLocation(QStandardPaths::DataLocation); + #endif +} diff --git a/DSView/pv/config/appconfig.h b/DSView/pv/config/appconfig.h index f9cdb6a8..250e1868 100644 --- a/DSView/pv/config/appconfig.h +++ b/DSView/pv/config/appconfig.h @@ -33,12 +33,10 @@ //--------------------api--- QString GetDirectoryName(QString path); - QString GetIconPath(); - QString GetAppDataDir(); - QString GetResourceDir(); +QString GetUserDataDir(); //------------------class diff --git a/DSView/pv/device/device.cpp b/DSView/pv/device/device.cpp index 1f6c0bdb..4e056b45 100755 --- a/DSView/pv/device/device.cpp +++ b/DSView/pv/device/device.cpp @@ -33,8 +33,6 @@ Device::Device(sr_dev_inst *sdi) : _sdi(sdi) { assert(_sdi); - void *p = this; - (void)p; } Device::~Device() @@ -52,9 +50,8 @@ void Device::use(SigSession *owner) DevInst::use(owner); sr_session_new(); - - assert(_sdi); sr_dev_open(_sdi); + _usable = (_sdi->status == SR_ST_ACTIVE); if (sr_session_dev_add(_sdi) != SR_OK) throw QString(tr("Failed to use device.")); @@ -74,15 +71,6 @@ QString Device::format_device_title() { ostringstream s; - assert(_sdi); - -// if (_sdi->vendor && _sdi->vendor[0]) { -// s << _sdi->vendor; -// if ((_sdi->model && _sdi->model[0]) || -// (_sdi->version && _sdi->version[0])) -// s << ' '; -// } - if (_sdi->model && _sdi->model[0]) { s << _sdi->model; if (_sdi->version && _sdi->version[0]) @@ -97,7 +85,6 @@ QString Device::format_device_title() bool Device::is_trigger_enabled() { - assert(_sdi); for (const GSList *l = _sdi->channels; l; l = l->next) { const sr_channel *const p = (const sr_channel *)l->data; assert(p); diff --git a/DSView/pv/device/devinst.cpp b/DSView/pv/device/devinst.cpp index f4b03485..92c4894f 100755 --- a/DSView/pv/device/devinst.cpp +++ b/DSView/pv/device/devinst.cpp @@ -195,7 +195,8 @@ bool DevInst::is_trigger_enabled() void DevInst::start() { if (sr_session_start() != SR_OK) - throw tr("Failed to start session."); + throw tr("Failed to start session."); + //assert(false); } void DevInst::run() diff --git a/DSView/pv/device/sessionfile.cpp b/DSView/pv/device/sessionfile.cpp index 6d3f53d2..3873fa22 100755 --- a/DSView/pv/device/sessionfile.cpp +++ b/DSView/pv/device/sessionfile.cpp @@ -25,9 +25,9 @@ namespace pv { namespace device { SessionFile::SessionFile(QString path) : - File(path), - _sdi(NULL) + File(path) { + _sdi = NULL; } sr_dev_inst* SessionFile::dev_inst() @@ -38,6 +38,9 @@ sr_dev_inst* SessionFile::dev_inst() void SessionFile::use(SigSession *owner) { assert(!_sdi); + if (_sdi){ + //return; + } if (sr_session_load(_path.toUtf8().data()) != SR_OK) throw tr("Failed to open file.\n"); @@ -58,15 +61,15 @@ void SessionFile::use(SigSession *owner) } void SessionFile::release() -{ - if (!_owner || !_sdi) - return; - - File::release(); - sr_dev_close(_sdi); - sr_dev_clear(_sdi->driver); - sr_session_destroy(); - _sdi = NULL; +{ + if (_owner != NULL && _sdi != NULL) + { + File::release(); + sr_dev_close(_sdi); + sr_dev_clear(_sdi->driver); + sr_session_destroy(); + _sdi = NULL; + } } } // device diff --git a/DSView/pv/devicemanager.cpp b/DSView/pv/devicemanager.cpp index 448272ef..f148ee11 100755 --- a/DSView/pv/devicemanager.cpp +++ b/DSView/pv/devicemanager.cpp @@ -102,8 +102,7 @@ void DeviceManager::driver_scan( // 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) { + if ((*i)->dev_inst() && (*i)->dev_inst()->driver == driver) { (*i)->release(); i = _devices.erase(i); } else { diff --git a/DSView/pv/dialogs/storeprogress.cpp b/DSView/pv/dialogs/storeprogress.cpp index 168a4c75..28fa1571 100755 --- a/DSView/pv/dialogs/storeprogress.cpp +++ b/DSView/pv/dialogs/storeprogress.cpp @@ -158,18 +158,20 @@ void StoreProgress::timeout() _store_session.session()->set_saving(false); save_done(); close(); + delete this; + } else { QTimer::singleShot(100, this, SLOT(timeout())); } } -void StoreProgress::save_run(QString session_file) +void StoreProgress::save_run(ISessionDataGetter *getter) { _isExport = false; setTitle(tr("Saving...")); QString file = _store_session.MakeSaveFile(false); _fileLab->setText(file); - _store_session._sessionFile = session_file; + _store_session._sessionDataGetter = getter; show(); } diff --git a/DSView/pv/dialogs/storeprogress.h b/DSView/pv/dialogs/storeprogress.h index f0475b5d..54a5b548 100755 --- a/DSView/pv/dialogs/storeprogress.h +++ b/DSView/pv/dialogs/storeprogress.h @@ -25,6 +25,7 @@ #include #include "../storesession.h" #include "../dialogs/dsdialog.h" +#include "../interface/icallbacks.h" class QLineEdit; class QCheckBox; @@ -59,7 +60,7 @@ signals: void save_done(); public slots: - void save_run(QString session_file); + void save_run(ISessionDataGetter *getter); void export_run(); private slots: diff --git a/DSView/pv/interface/icallbacks.h b/DSView/pv/interface/icallbacks.h index 1d1627c3..0fca6f66 100644 --- a/DSView/pv/interface/icallbacks.h +++ b/DSView/pv/interface/icallbacks.h @@ -34,4 +34,10 @@ public: }; +class ISessionDataGetter +{ +public: + virtual bool genSessionData(std::string &str) = 0; +}; + #endif diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index cbd65ecd..70caa8f4 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -913,25 +913,11 @@ void MainWindow::on_save() using pv::dialogs::StoreProgress; SigSession *_session = _control->GetSession(); - _session->set_saving(true); - QString session_file; - QDir dir; - #if QT_VERSION >= 0x050400 - QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - #else - QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); - #endif - if(dir.mkpath(path)) { - dir.cd(path); - - session_file = dir.absolutePath() + "/DSView-session-XXXXXX"; - on_store_session(session_file); - } StoreProgress *dlg = new StoreProgress(_session, this); connect(dlg, SIGNAL(save_done()), this, SLOT(device_detach_post())); - dlg->save_run(session_file); + dlg->save_run(this); } void MainWindow::on_export() @@ -1139,21 +1125,9 @@ bool MainWindow::load_session_json(QJsonDocument json, bool file_dev, bool bDeco return true; } - -bool MainWindow::on_store_session(QString name) -{ - QFile sessionFile(name); - if (!sessionFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - qDebug("Warning: Couldn't open session file to write!"); - return false; - } - QTextStream outStream(&sessionFile); - app::set_utf8(outStream); - - //outStream.setGenerateByteOrderMark(true); // UTF-8 without BOM - - AppConfig &app = AppConfig::Instance(); +bool MainWindow::gen_session_json(QJsonArray &array){ SigSession *_session = _control->GetSession(); + AppConfig &app = AppConfig::Instance(); GVariant *gvar_opts; GVariant *gvar; @@ -1168,6 +1142,7 @@ bool MainWindow::on_store_session(QString name) if ((sr_config_list(sdi->driver, sdi, NULL, SR_CONF_DEVICE_SESSIONS, &gvar_opts) != SR_OK)) return false; /* Driver supports no device instance sessions. */ + const int *const options = (const int32_t *)g_variant_get_fixed_array( gvar_opts, &num_opts, sizeof(int32_t)); for (unsigned int i = 0; i < num_opts; i++) { @@ -1231,23 +1206,52 @@ bool MainWindow::on_store_session(QString name) if (_session->get_device()->dev_inst()->mode == LOGIC) { sessionVar["trigger"] = _trigger_widget->get_session(); } - - + StoreSession ss(_session); - sessionVar["decoder"] = ss.json_decoders(); - + QJsonArray decodeJson; + ss.json_decoders(decodeJson); + sessionVar["decoder"] = decodeJson; if (_session->get_device()->dev_inst()->mode == DSO) { sessionVar["measure"] = _view->get_viewstatus()->get_session(); + } + + array.push_back(sessionVar); + return true; +} + +bool MainWindow::on_store_session(QString name) +{ + QFile sessionFile(name); + if (!sessionFile.open(QIODevice::WriteOnly | QIODevice::Text)) { + qDebug("Warning: Couldn't open session file to write!"); + return false; } - QJsonDocument sessionDoc(sessionVar); + QTextStream outStream(&sessionFile); + app::set_utf8(outStream); + + QJsonArray jsonArray; + if (!gen_session_json(jsonArray)) + return false; + QJsonDocument sessionDoc(jsonArray); //sessionFile.write(QString::fromUtf8(sessionDoc.toJson())); outStream << QString::fromUtf8(sessionDoc.toJson()); sessionFile.close(); return true; } +bool MainWindow::genSessionData(std::string &str) +{ + QJsonArray jsonArray; + if (!gen_session_json(jsonArray)) + return false; + QJsonDocument sessionDoc(jsonArray); + QString data = QString::fromUtf8(sessionDoc.toJson()); + str.append(data.toLatin1().data()); + return true; +} + void MainWindow::restore_dock() { // default dockwidget size diff --git a/DSView/pv/mainwindow.h b/DSView/pv/mainwindow.h index 6e284bb8..047024ad 100755 --- a/DSView/pv/mainwindow.h +++ b/DSView/pv/mainwindow.h @@ -73,7 +73,7 @@ using namespace pv::device; //The mainwindow,referenced by MainFrame //TODO: create graph view,toolbar,and show device list -class MainWindow : public QMainWindow, public ISessionCallback, public IMainForm +class MainWindow : public QMainWindow, public ISessionCallback, public IMainForm, ISessionDataGetter { Q_OBJECT @@ -146,6 +146,10 @@ public: public: void session_save(); + //ISessionDataGetter +private: + bool genSessionData(std::string &str); + //ISessionCallback private: void show_error(QString error); @@ -173,6 +177,9 @@ private: void receive_header(); void data_received(); + //------private + bool gen_session_json(QJsonArray &array); + private: AppControl *_control; bool _hot_detach; diff --git a/DSView/pv/sigsession.cpp b/DSView/pv/sigsession.cpp index 2816eddb..0806c8ba 100755 --- a/DSView/pv/sigsession.cpp +++ b/DSView/pv/sigsession.cpp @@ -418,6 +418,8 @@ void SigSession::start_capture(bool instant) } assert(_dev_inst->dev_inst()); + qDebug()<<"start capture, device title:"<<_dev_inst->format_device_title(); + if (!_dev_inst->is_usable()) { _error = Hw_err; _callback->session_error(); @@ -431,7 +433,6 @@ void SigSession::start_capture(bool instant) // reset measure of dso signal for(auto &s : _signals) { - assert(s); view::DsoSignal *dsoSig = NULL; if ((dsoSig = dynamic_cast(s))) dsoSig->set_mValid(false); @@ -465,6 +466,10 @@ void SigSession::start_capture(bool instant) if (_sampling_thread.joinable()){ _sampling_thread.join(); } + + if (sr_check_session_start_before() != 0){ + assert(false); + } _sampling_thread = std::thread(&SigSession::sample_thread_proc, this, _dev_inst); } @@ -483,6 +488,7 @@ void SigSession::sample_thread_proc(DevInst *dev_inst) receive_data(0); set_capture_state(Running); + //session loop dev_inst->run(); set_capture_state(Stopped); diff --git a/DSView/pv/storesession.cpp b/DSView/pv/storesession.cpp index b65b36ed..33a4b829 100755 --- a/DSView/pv/storesession.cpp +++ b/DSView/pv/storesession.cpp @@ -49,10 +49,11 @@ #include #include #include - + #include #include "config/appconfig.h" #include "dsvdef.h" + namespace pv { @@ -64,6 +65,7 @@ StoreSession::StoreSession(SigSession *session) : _has_error(false), _canceled(false) { + _sessionDataGetter = NULL; } StoreSession::~StoreSession() @@ -117,12 +119,8 @@ QList StoreSession::getSuportedExportFormats(){ } bool StoreSession::save_start() -{ - if (_sessionFile == "") - { - _error = tr("No set session file name."); - return false; - } +{ + assert(_sessionDataGetter); std::set type_set; for(auto &sig : _session->get_signals()) { @@ -151,11 +149,7 @@ bool StoreSession::save_start() if (snapshot->empty()) { _error = tr("No data to save."); return false; - } - - QString meta_file = meta_gen(snapshot); - - QString decoders_file = decoders_gen(); + } /* if (meta_file == NULL) { @@ -174,13 +168,37 @@ bool StoreSession::save_start() } */ - //make zip file - if (meta_file != "" && m_zipDoc.CreateNew(_file_name.toUtf8().data(), false)) - { - if (!m_zipDoc.AddFromFile(meta_file.toUtf8().data(), "header") - || !m_zipDoc.AddFromFile(decoders_file.toUtf8().data(), "decoders") - || !m_zipDoc.AddFromFile(_sessionFile.toUtf8().data(), "session")) - { + std::string meta_data; + std::string decoder_data; + std::string session_data; + + meta_gen(snapshot, meta_data); + decoders_gen(decoder_data); + _sessionDataGetter->genSessionData(session_data); + + if (meta_data.empty()) { + _error = tr("Generate temp file data failed."); + QFile::remove(_file_name); + return false; + } + if (decoder_data.empty()){ + _error = tr("Generate decoder file data failed."); + QFile::remove(_file_name); + return false; + } + if (session_data.empty()){ + _error = tr("Generate session file data failed."); + QFile::remove(_file_name); + return false; + } + + std::string _filename = getFileName(_file_name); + if (m_zipDoc.CreateNew(_filename.c_str(), false)) + { + if ( !m_zipDoc.AddFromBuffer("header", meta_data.c_str(), meta_data.size()) + || !m_zipDoc.AddFromBuffer("decoders", decoder_data.c_str(), decoder_data.size()) + || !m_zipDoc.AddFromBuffer("session", session_data.c_str(), session_data.size()) + ){ _has_error = true; _error = m_zipDoc.GetError(); } @@ -191,9 +209,11 @@ bool StoreSession::save_start() return !_has_error; } } + else{ + _error = tr("Generate zip file failed."); + } QFile::remove(_file_name); - //_error.clear(); return false; } @@ -339,55 +359,35 @@ void StoreSession::save_proc(data::Snapshot *snapshot) } } -QString StoreSession::meta_gen(data::Snapshot *snapshot) +bool StoreSession::meta_gen(data::Snapshot *snapshot, std::string &str) { GSList *l; - GVariant *gvar; - FILE *meta = NULL; + GVariant *gvar; struct sr_channel *probe; int probecnt; char *s; struct sr_status status; - QString metafile; + const sr_dev_inst *sdi = NULL; + char meta[300] = {0}; + + sdi = _session->get_device()->dev_inst(); + + sprintf(meta, "%s", "[version]\n"); str += meta; + sprintf(meta, "version = %d\n", File_Version); str += meta; + sprintf(meta, "%s", "[header]\n"); str += meta; - /* init "metadata" */ - QDir dir; - #if QT_VERSION >= 0x050400 - QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - #else - QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); - #endif - if(dir.mkpath(path)) { - dir.cd(path); - metafile = dir.absolutePath() + "/DSView-meta-XXXXXX"; - } else { - return NULL; - } - - const sr_dev_inst *sdi = _session->get_device()->dev_inst(); - meta = fopen(metafile.toUtf8().data(), "wb"); - if (meta == NULL) { - qDebug() << "Failed to create temp meta file."; - return NULL; - } - - fprintf(meta, "[version]\n"); - fprintf(meta, "version = %d\n", File_Version); - - /* metadata */ - fprintf(meta, "[header]\n"); if (sdi->driver) { - fprintf(meta, "driver = %s\n", sdi->driver->name); - fprintf(meta, "device mode = %d\n", sdi->mode); + sprintf(meta, "driver = %s\n", sdi->driver->name); str += meta; + sprintf(meta, "device mode = %d\n", sdi->mode); str += meta; } /* metadata */ - fprintf(meta, "capturefile = data\n"); - fprintf(meta, "total samples = %" PRIu64 "\n", snapshot->get_sample_count()); + sprintf(meta, "capturefile = data\n"); str += meta; + sprintf(meta, "total samples = %" PRIu64 "\n", snapshot->get_sample_count()); str += meta; if (sdi->mode != LOGIC) { - fprintf(meta, "total probes = %d\n", snapshot->get_channel_num()); - fprintf(meta, "total blocks = %d\n", snapshot->get_block_num()); + sprintf(meta, "total probes = %d\n", snapshot->get_channel_num()); str += meta; + sprintf(meta, "total blocks = %d\n", snapshot->get_block_num()); str += meta; } data::LogicSnapshot *logic_snapshot = NULL; @@ -398,140 +398,198 @@ QString StoreSession::meta_gen(data::Snapshot *snapshot) if (probe->enabled && logic_snapshot->has_data(probe->index)) to_save_probes++; } - fprintf(meta, "total probes = %d\n", to_save_probes); - fprintf(meta, "total blocks = %d\n", logic_snapshot->get_block_num()); + sprintf(meta, "total probes = %d\n", to_save_probes); str += meta; + sprintf(meta, "total blocks = %d\n", logic_snapshot->get_block_num()); str += meta; } s = sr_samplerate_string(_session->cur_snap_samplerate()); - fprintf(meta, "samplerate = %s\n", s); + + sprintf(meta, "samplerate = %s\n", s); str += meta; if (sdi->mode == DSO) { gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_TIMEBASE); if (gvar != NULL) { uint64_t tmp_u64 = g_variant_get_uint64(gvar); - fprintf(meta, "hDiv = %" PRIu64 "\n", tmp_u64); + sprintf(meta, "hDiv = %" PRIu64 "\n", tmp_u64); str += meta; g_variant_unref(gvar); } gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_MAX_TIMEBASE); if (gvar != NULL) { uint64_t tmp_u64 = g_variant_get_uint64(gvar); - fprintf(meta, "hDiv max = %" PRIu64 "\n", tmp_u64); + sprintf(meta, "hDiv max = %" PRIu64 "\n", tmp_u64); str += meta; g_variant_unref(gvar); } gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_MIN_TIMEBASE); if (gvar != NULL) { uint64_t tmp_u64 = g_variant_get_uint64(gvar); - fprintf(meta, "hDiv min = %" PRIu64 "\n", tmp_u64); + sprintf(meta, "hDiv min = %" PRIu64 "\n", tmp_u64); str += meta; g_variant_unref(gvar); } gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_UNIT_BITS); if (gvar != NULL) { uint8_t tmp_u8 = g_variant_get_byte(gvar); - fprintf(meta, "bits = %d\n", tmp_u8); + sprintf(meta, "bits = %d\n", tmp_u8); str += meta; g_variant_unref(gvar); } gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_REF_MIN); if (gvar != NULL) { uint32_t tmp_u32 = g_variant_get_uint32(gvar); - fprintf(meta, "ref min = %d\n", tmp_u32); + sprintf(meta, "ref min = %d\n", tmp_u32); str += meta; g_variant_unref(gvar); } gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_REF_MAX); if (gvar != NULL) { uint32_t tmp_u32 = g_variant_get_uint32(gvar); - fprintf(meta, "ref max = %d\n", tmp_u32); + sprintf(meta, "ref max = %d\n", tmp_u32); str += meta; g_variant_unref(gvar); } } else if (sdi->mode == LOGIC) { - fprintf(meta, "trigger time = %lld\n", _session->get_session_time().toMSecsSinceEpoch()); + sprintf(meta, "trigger time = %lld\n", _session->get_session_time().toMSecsSinceEpoch()); str += meta; } else if (sdi->mode == ANALOG) { data::AnalogSnapshot *analog_snapshot = NULL; if ((analog_snapshot = dynamic_cast(snapshot))) { uint8_t tmp_u8 = analog_snapshot->get_unit_bytes(); - fprintf(meta, "bits = %d\n", tmp_u8*8); + sprintf(meta, "bits = %d\n", tmp_u8*8); str += meta; } gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_REF_MIN); if (gvar != NULL) { uint32_t tmp_u32 = g_variant_get_uint32(gvar); - fprintf(meta, "ref min = %d\n", tmp_u32); + sprintf(meta, "ref min = %d\n", tmp_u32); str += meta; g_variant_unref(gvar); } gvar = _session->get_device()->get_config(NULL, NULL, SR_CONF_REF_MAX); if (gvar != NULL) { uint32_t tmp_u32 = g_variant_get_uint32(gvar); - fprintf(meta, "ref max = %d\n", tmp_u32); + sprintf(meta, "ref max = %d\n", tmp_u32); str += meta; g_variant_unref(gvar); } } - fprintf(meta, "trigger pos = %" PRIu64 "\n", _session->get_trigger_pos()); + sprintf(meta, "trigger pos = %" PRIu64 "\n", _session->get_trigger_pos()); str += meta; + + probecnt = 0; - probecnt = 0; for (l = sdi->channels; l; l = l->next) { + probe = (struct sr_channel *)l->data; - if (snapshot->has_data(probe->index)) { - if (sdi->mode == LOGIC && !probe->enabled) - continue; + if (!snapshot->has_data(probe->index)) + continue; + if (sdi->mode == LOGIC && !probe->enabled) + continue; - if (probe->name) - fprintf(meta, "probe%d = %s\n", (sdi->mode == LOGIC) ? probe->index : probecnt, probe->name); - if (probe->trigger) - fprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger); - if (sdi->mode == DSO) { - fprintf(meta, " enable%d = %d\n", probecnt, probe->enabled); - fprintf(meta, " coupling%d = %d\n", probecnt, probe->coupling); - fprintf(meta, " vDiv%d = %" PRIu64 "\n", probecnt, probe->vdiv); - fprintf(meta, " vFactor%d = %" PRIu64 "\n", probecnt, probe->vfactor); - fprintf(meta, " vOffset%d = %d\n", probecnt, probe->hw_offset); - fprintf(meta, " vTrig%d = %d\n", probecnt, probe->trig_value); - if (sr_status_get(sdi, &status, false) == SR_OK) { - if (probe->index == 0) { - fprintf(meta, " period%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_tlen); - fprintf(meta, " pcnt%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_cnt); - fprintf(meta, " max%d = %d\n", probecnt, status.ch0_max); - fprintf(meta, " min%d = %d\n", probecnt, status.ch0_min); - fprintf(meta, " plen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_plen); - fprintf(meta, " llen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_llen); - fprintf(meta, " level%d = %d\n", probecnt, status.ch0_level_valid); - fprintf(meta, " plevel%d = %d\n", probecnt, status.ch0_plevel); - fprintf(meta, " low%d = %" PRIu32 "\n", probecnt, status.ch0_low_level); - fprintf(meta, " high%d = %" PRIu32 "\n", probecnt, status.ch0_high_level); - fprintf(meta, " rlen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_rlen); - fprintf(meta, " flen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_flen); - fprintf(meta, " rms%d = %" PRIu64 "\n", probecnt, status.ch0_acc_square); - fprintf(meta, " mean%d = %" PRIu32 "\n", probecnt, status.ch0_acc_mean); - } else { - fprintf(meta, " period%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_tlen); - fprintf(meta, " pcnt%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_cnt); - fprintf(meta, " max%d = %d\n", probecnt, status.ch1_max); - fprintf(meta, " min%d = %d\n", probecnt, status.ch1_min); - fprintf(meta, " plen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_plen); - fprintf(meta, " llen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_llen); - fprintf(meta, " level%d = %d\n", probecnt, status.ch1_level_valid); - fprintf(meta, " plevel%d = %d\n", probecnt, status.ch1_plevel); - fprintf(meta, " low%d = %" PRIu32 "\n", probecnt, status.ch1_low_level); - fprintf(meta, " high%d = %" PRIu32 "\n", probecnt, status.ch1_high_level); - fprintf(meta, " rlen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_rlen); - fprintf(meta, " flen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_flen); - fprintf(meta, " rms%d = %" PRIu64 "\n", probecnt, status.ch1_acc_square); - fprintf(meta, " mean%d = %" PRIu32 "\n", probecnt, status.ch1_acc_mean); - } - } - } else if (sdi->mode == ANALOG) { - fprintf(meta, " enable%d = %d\n", probecnt, probe->enabled); - fprintf(meta, " coupling%d = %d\n", probecnt, probe->coupling); - fprintf(meta, " vDiv%d = %" PRIu64 "\n", probecnt, probe->vdiv); - fprintf(meta, " vOffset%d = %d\n", probecnt, probe->hw_offset); - fprintf(meta, " mapUnit%d = %s\n", probecnt, probe->map_unit); - fprintf(meta, " mapMax%d = %lf\n", probecnt, probe->map_max); - fprintf(meta, " mapMin%d = %lf\n", probecnt, probe->map_min); - } - probecnt++; + if (probe->name) + { + int sigdex = (sdi->mode == LOGIC) ? probe->index : probecnt; + sprintf(meta, "probe%d = %s\n", sigdex, probe->name); + str += meta; } - } - fclose(meta); + if (probe->trigger){ + sprintf(meta, " trigger%d = %s\n", probecnt, probe->trigger); + str += meta; + } - return metafile; + if (sdi->mode == DSO) + { + sprintf(meta, " enable%d = %d\n", probecnt, probe->enabled); + str += meta; + sprintf(meta, " coupling%d = %d\n", probecnt, probe->coupling); + str += meta; + sprintf(meta, " vDiv%d = %" PRIu64 "\n", probecnt, probe->vdiv); + str += meta; + sprintf(meta, " vFactor%d = %" PRIu64 "\n", probecnt, probe->vfactor); + str += meta; + sprintf(meta, " vOffset%d = %d\n", probecnt, probe->hw_offset); + str += meta; + sprintf(meta, " vTrig%d = %d\n", probecnt, probe->trig_value); + str += meta; + + if (sr_status_get(sdi, &status, false) == SR_OK) + { + if (probe->index == 0) + { + sprintf(meta, " period%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_tlen); + str += meta; + sprintf(meta, " pcnt%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_cnt); + str += meta; + sprintf(meta, " max%d = %d\n", probecnt, status.ch0_max); + str += meta; + sprintf(meta, " min%d = %d\n", probecnt, status.ch0_min); + str += meta; + sprintf(meta, " plen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_plen); + str += meta; + sprintf(meta, " llen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_llen); + str += meta; + sprintf(meta, " level%d = %d\n", probecnt, status.ch0_level_valid); + str += meta; + sprintf(meta, " plevel%d = %d\n", probecnt, status.ch0_plevel); + str += meta; + sprintf(meta, " low%d = %" PRIu32 "\n", probecnt, status.ch0_low_level); + str += meta; + sprintf(meta, " high%d = %" PRIu32 "\n", probecnt, status.ch0_high_level); + str += meta; + sprintf(meta, " rlen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_rlen); + str += meta; + sprintf(meta, " flen%d = %" PRIu32 "\n", probecnt, status.ch0_cyc_flen); + str += meta; + sprintf(meta, " rms%d = %" PRIu64 "\n", probecnt, status.ch0_acc_square); + str += meta; + sprintf(meta, " mean%d = %" PRIu32 "\n", probecnt, status.ch0_acc_mean); + str += meta; + } + else + { + sprintf(meta, " period%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_tlen); + str += meta; + sprintf(meta, " pcnt%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_cnt); + str += meta; + sprintf(meta, " max%d = %d\n", probecnt, status.ch1_max); + str += meta; + sprintf(meta, " min%d = %d\n", probecnt, status.ch1_min); + str += meta; + sprintf(meta, " plen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_plen); + str += meta; + sprintf(meta, " llen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_llen); + str += meta; + sprintf(meta, " level%d = %d\n", probecnt, status.ch1_level_valid); + str += meta; + sprintf(meta, " plevel%d = %d\n", probecnt, status.ch1_plevel); + str += meta; + sprintf(meta, " low%d = %" PRIu32 "\n", probecnt, status.ch1_low_level); + str += meta; + sprintf(meta, " high%d = %" PRIu32 "\n", probecnt, status.ch1_high_level); + str += meta; + sprintf(meta, " rlen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_rlen); + str += meta; + sprintf(meta, " flen%d = %" PRIu32 "\n", probecnt, status.ch1_cyc_flen); + str += meta; + sprintf(meta, " rms%d = %" PRIu64 "\n", probecnt, status.ch1_acc_square); + str += meta; + sprintf(meta, " mean%d = %" PRIu32 "\n", probecnt, status.ch1_acc_mean); + str += meta; + } + } + } + else if (sdi->mode == ANALOG) + { + sprintf(meta, " enable%d = %d\n", probecnt, probe->enabled); + str += meta; + sprintf(meta, " coupling%d = %d\n", probecnt, probe->coupling); + str += meta; + sprintf(meta, " vDiv%d = %" PRIu64 "\n", probecnt, probe->vdiv); + str += meta; + sprintf(meta, " vOffset%d = %d\n", probecnt, probe->hw_offset); + str += meta; + sprintf(meta, " mapUnit%d = %s\n", probecnt, probe->map_unit); + str += meta; + sprintf(meta, " mapMax%d = %lf\n", probecnt, probe->map_max); + str += meta; + sprintf(meta, " mapMin%d = %lf\n", probecnt, probe->map_min); + str += meta; + } + probecnt++; + } + + return true; } //export as csv file @@ -824,43 +882,19 @@ void StoreSession::export_proc(data::Snapshot *snapshot) } -QString StoreSession::decoders_gen() -{ - QDir dir; - #if QT_VERSION >= 0x050400 - QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); - #else - QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); - #endif - if(dir.mkpath(path)) { - dir.cd(path); - - QString file_name = dir.absolutePath() + "/DSView-decoders-XXXXXX"; - QFile sessionFile(file_name); - if (!sessionFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - qDebug("Warning: Couldn't open session file to write!"); - return NULL; - } - QTextStream outStream(&sessionFile); - app::set_utf8(outStream); - //outStream.setGenerateByteOrderMark(true); // UTF-8 without BOM - - QJsonArray dec_array = json_decoders(); - QJsonDocument sessionDoc(dec_array); - outStream << QString::fromUtf8(sessionDoc.toJson()); - sessionFile.close(); - - return file_name; - } else { - return NULL; - } - +bool StoreSession::decoders_gen(std::string &str) +{ + QJsonArray dec_array; + if (!json_decoders(dec_array)) + return false; + QJsonDocument sessionDoc(dec_array); + QString data = QString::fromUtf8(sessionDoc.toJson()); + str.append(data.toLatin1().data()); + return true; } -QJsonArray StoreSession::json_decoders() -{ - QJsonArray dec_array; - +bool StoreSession::json_decoders(QJsonArray &array) +{ for(auto &t : _session->get_decode_signals()) { QJsonObject dec_obj; QJsonArray stack_array; @@ -913,7 +947,6 @@ QJsonArray StoreSession::json_decoders() } } - if (have_probes) { dec_obj["id"] = QJsonValue::fromVariant(d->id); dec_obj["channel"] = ch_array; @@ -936,9 +969,10 @@ QJsonArray StoreSession::json_decoders() } dec_obj["show"] = show_obj; - dec_array.push_back(dec_obj); + array.push_back(dec_obj); } - return dec_array; + + return true; } bool StoreSession::load_decoders(dock::ProtocolDock *widget, QJsonArray dec_array) @@ -1300,4 +1334,14 @@ void StoreSession::MakeChunkName(char *chunk_name, int chunk_num, int index, int } } +std::string StoreSession::getFileName(QString fileName) +{ +#if defined(_WIN32) + QTextCodec *code = QTextCodec::codecForName("GB2312"); + return code->fromUnicode(fileName).data(); +#else + return _file_name.toUtf8().toStdString(); +#endif +} + } // pv diff --git a/DSView/pv/storesession.h b/DSView/pv/storesession.h index 99db9f12..882653e9 100755 --- a/DSView/pv/storesession.h +++ b/DSView/pv/storesession.h @@ -24,12 +24,12 @@ #include #include -#include - +#include #include - #include +#include "interface/icallbacks.h" + #include "ZipMaker.h" namespace pv { @@ -72,14 +72,13 @@ public: private: void save_proc(pv::data::Snapshot *snapshot); - QString meta_gen(data::Snapshot *snapshot); - void export_proc(pv::data::Snapshot *snapshot); - - QString decoders_gen(); + bool meta_gen(data::Snapshot *snapshot, std::string &str); + void export_proc(pv::data::Snapshot *snapshot); + bool decoders_gen(std::string &str); public: - QJsonArray json_decoders(); + bool json_decoders(QJsonArray &array); bool load_decoders(dock::ProtocolDock *widget, QJsonArray dec_array); QString MakeSaveFile(bool bDlg); QString MakeExportFile(bool bDlg); @@ -94,12 +93,13 @@ private: QList getSuportedExportFormats(); double get_integer(GVariant * var); void MakeChunkName(char *chunk_name, int chunk_num, int index, int type, int version); + std:: string getFileName(QString fileName); signals: void progress_updated(); public: - QString _sessionFile; + ISessionDataGetter *_sessionDataGetter; private: QString _file_name; diff --git a/libsigrok4DSL/dsdevice.c b/libsigrok4DSL/dsdevice.c index bf0c6ce9..213a91db 100755 --- a/libsigrok4DSL/dsdevice.c +++ b/libsigrok4DSL/dsdevice.c @@ -360,7 +360,7 @@ SR_API int sr_dev_open(struct sr_dev_inst *sdi) SR_API int sr_dev_close(struct sr_dev_inst *sdi) { int ret; - + if (!sdi || !sdi->driver || !sdi->driver->dev_close) return SR_ERR; diff --git a/libsigrok4DSL/proto.h b/libsigrok4DSL/proto.h index 0ebd0d26..88aaec0e 100755 --- a/libsigrok4DSL/proto.h +++ b/libsigrok4DSL/proto.h @@ -117,6 +117,9 @@ SR_API int sr_session_source_remove(int fd); SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd); SR_API int sr_session_source_remove_channel(GIOChannel *channel); +//0:ok, 1:error +SR_API int sr_check_session_start_before(); + /*--- input/input.c ---------------------------------------------------------*/ SR_API struct sr_input_format **sr_input_list(void); diff --git a/libsigrok4DSL/session.c b/libsigrok4DSL/session.c index 74ca9996..24786c07 100755 --- a/libsigrok4DSL/session.c +++ b/libsigrok4DSL/session.c @@ -383,6 +383,17 @@ SR_API int sr_session_start(void) return ret; } +/* +* check session if be created +*/ +int sr_check_session_start_before(){ + + if (!session || !session->devs) { + return 1; + } + return 0; +} + /** * Run the session. * diff --git a/qtpro/DSView.pro b/qtpro/DSView.pro index 57b0ae30..c3540636 100644 --- a/qtpro/DSView.pro +++ b/qtpro/DSView.pro @@ -18,6 +18,7 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets #QMAKE_CFLAGS_ISYSTEM = -I + TARGET = DSView TEMPLATE = app diff --git a/qtpro/DSView.pro.user b/qtpro/DSView.pro.user index 1c801f1b..936f882c 100644 --- a/qtpro/DSView.pro.user +++ b/qtpro/DSView.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId