diff --git a/DSView/pv/appcontrol.cpp b/DSView/pv/appcontrol.cpp index 444d68ba..618876ce 100644 --- a/DSView/pv/appcontrol.cpp +++ b/DSView/pv/appcontrol.cpp @@ -87,7 +87,7 @@ bool AppControl::Init() QString dir = GetDecodeScriptDir(); strcpy(path, dir.toUtf8().data()); - dsv_info("decode script files directory: \"%s\"", dir.toUtf8().data()); + dsv_info("Decode script files directory:\"%s\"", dir.toUtf8().data()); // Initialise libsigrokdecode if (srd_init(path) != SRD_OK) diff --git a/DSView/pv/mainwindow.cpp b/DSView/pv/mainwindow.cpp index c1019715..627331e5 100644 --- a/DSView/pv/mainwindow.cpp +++ b/DSView/pv/mainwindow.cpp @@ -313,8 +313,10 @@ namespace pv { try { - if (strncmp(_device_agent->name().toUtf8(), "virtual", 7)) + if (_device_agent->is_hardware()){ session_save(); + } + _session->set_file(file_name); } catch (QString e) @@ -414,8 +416,7 @@ namespace pv } void MainWindow::session_save() - { - QDir dir; + { #if QT_VERSION >= 0x050400 QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); #else @@ -428,28 +429,40 @@ namespace pv return; } - AppConfig &app = AppConfig::Instance(); + AppConfig &app = AppConfig::Instance(); - if (dir.mkpath(path)) - { - dir.cd(path); - QString driver_name = _device_agent->name(); - QString mode_name = QString::number(_device_agent->get_work_mode()); - QString lang_name = ".ses" + QString::number(app._frameOptions.language); - QString file_name = dir.absolutePath() + "/" + - driver_name + mode_name + - lang_name + ".dsc"; - if (strncmp(driver_name.toUtf8(), "virtual", 7) && - !file_name.isEmpty()) - { - on_store_session(file_name); - } + if (_device_agent->is_hardware()){ + QString sessionFile = genSessionFileName(); + on_store_session(sessionFile); } app._frameOptions.windowState = saveState(); app.SaveFrame(); } + QString MainWindow::genSessionFileName() + { +#if QT_VERSION >= 0x050400 + QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); +#else + QString path = QStandardPaths::writableLocation(QStandardPaths::DataLocation); +#endif + + AppConfig &app = AppConfig::Instance(); + + QDir dir(path); + if (dir.exists() == false){ + dir.mkpath(path); + } + + QString driver_name = _device_agent->driver_name(); + QString mode_name = QString::number(_device_agent->get_work_mode()); + QString lang_name = ".ses" + QString::number(app._frameOptions.language); + QString file_name = dir.absolutePath() + "/" + driver_name + mode_name + lang_name + ".dsc"; + + return file_name; + } + void MainWindow::closeEvent(QCloseEvent *event) { // not used, refer to closeEvent of mainFrame @@ -565,17 +578,29 @@ namespace pv bool MainWindow::on_load_session(QString name) { - QFile sessionFile(name); + if (name == ""){ + dsv_err("%s", "Session file name is empty."); + assert(false); + } + + dsv_info("Load session file: \"%s\"", name.toLocal8Bit().data()); + + QFile sf(name); bool bDone; - if (!sessionFile.open(QIODevice::ReadOnly)) - { - dsv_warn("%s", "Warning: Couldn't open session file!"); + if (!sf.exists()){ + dsv_warn("Warning: session file is not exists: \"%s\"", name.toLocal8Bit().data()); return false; } - QString sessionData = QString::fromUtf8(sessionFile.readAll()); - QJsonDocument sessionDoc = QJsonDocument::fromJson(sessionData.toUtf8()); + if (!sf.open(QIODevice::ReadOnly)) + { + dsv_warn("%s", "Warning: Couldn't open session file to load!"); + return false; + } + + QString sdata = QString::fromUtf8(sf.readAll()); + QJsonDocument sessionDoc = QJsonDocument::fromJson(sdata.toUtf8()); _protocol_widget->del_all_protocol(); return load_session_json(sessionDoc, bDone); @@ -920,6 +945,13 @@ namespace pv bool MainWindow::on_store_session(QString name) { + if (name == ""){ + dsv_err("%s", "Session file name is empty."); + assert(false); + } + + dsv_info("Store session file: \"%s\"", name.toLocal8Bit().data()); + QFile sessionFile(name); if (!sessionFile.open(QIODevice::WriteOnly | QIODevice::Text)) { @@ -931,8 +963,10 @@ namespace pv encoding::set_utf8(outStream); QJsonObject sessionVar; - if (!gen_session_json(sessionVar)) + if (!gen_session_json(sessionVar)){ return false; + } + QJsonDocument sessionDoc(sessionVar); outStream << QString::fromUtf8(sessionDoc.toJson()); sessionFile.close(); @@ -943,7 +977,10 @@ namespace pv { QJsonObject sessionVar; if (!gen_session_json(sessionVar)) + { return false; + } + QJsonDocument sessionDoc(sessionVar); QString data = QString::fromUtf8(sessionDoc.toJson()); str.append(data.toLocal8Bit().data()); @@ -1378,38 +1415,20 @@ namespace pv void MainWindow::load_device_config() { int lang = AppConfig::Instance()._frameOptions.language; - QString name = _device_agent->name(); int mode = _device_agent->get_work_mode(); if (_device_agent->is_hardware()) - { - -#if QT_VERSION >= 0x050400 - QDir dir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); -#else - QDir dir(QStandardPaths::writableLocation(QStandardPaths::DataLocation)); -#endif - if (dir.exists()) - { - QString str = dir.absolutePath() + "/"; - QString lang_name = ".ses" + QString::number(lang); - QString ses_name = str + - name + - QString::number(mode) + - lang_name + ".dsc"; - on_load_session(ses_name); - } + { + QString sessionFile = genSessionFileName(); + on_load_session(sessionFile); } - else + else if (_device_agent->is_demo()) { QDir dir(GetResourceDir()); if (dir.exists()) { QString str = dir.absolutePath() + "/"; - QString ses_name = str + - name + - QString::number(mode) + - ".dsc"; + QString ses_name = str + _device_agent->driver_name() + QString::number(mode) + ".dsc"; if (QFileInfo(ses_name).exists()) on_load_session(ses_name); } diff --git a/DSView/pv/mainwindow.h b/DSView/pv/mainwindow.h index abb24783..72b16bee 100644 --- a/DSView/pv/mainwindow.h +++ b/DSView/pv/mainwindow.h @@ -94,7 +94,8 @@ private: void setup_ui(); void retranslateUi(); bool eventFilter(QObject *object, QEvent *event); - bool load_session_json(QJsonDocument json, bool &haveDecoder); + bool load_session_json(QJsonDocument json, bool &haveDecoder); + QString genSessionFileName(); public slots: void switchTheme(QString style); diff --git a/DSView/pv/sigsession.cpp b/DSView/pv/sigsession.cpp index 12d5a0e6..914fc365 100644 --- a/DSView/pv/sigsession.cpp +++ b/DSView/pv/sigsession.cpp @@ -203,15 +203,15 @@ namespace pv _device_agent.update(); if (_device_agent.is_file()) - dsv_info("%s\"%s\"", "Switch to file: ", _device_agent.name().toUtf8().data()); + dsv_info("Switch to file \"%s\" done.", _device_agent.name().toUtf8().data()); else - dsv_info("%s\"%s\"", "Switch to device: ", _device_agent.name().toUtf8().data()); + dsv_info("Switch to device \"%s\" done.", _device_agent.name().toUtf8().data()); _device_status = ST_INIT; - RELEASE_ARRAY(_group_traces); - clear_all_decoder(); + + RELEASE_ARRAY(_group_traces); init_signals(); _cur_snap_samplerate = _device_agent.get_sample_rate(); diff --git a/DSView/pv/sigsession.h b/DSView/pv/sigsession.h index da923a9a..596d4887 100644 --- a/DSView/pv/sigsession.h +++ b/DSView/pv/sigsession.h @@ -162,8 +162,7 @@ public: std::vector& get_group_signals(); bool add_decoder(srd_decoder *const dec, bool silent, DecoderStatus *dstatus, - std::list &sub_decoders); - + std::list &sub_decoders); int get_trace_index_by_key_handel(void *handel); void remove_decoder(int index); void remove_decoder_by_key_handel(void *handel); diff --git a/DSView/pv/view/logicsignal.cpp b/DSView/pv/view/logicsignal.cpp index 5b517c70..0a6581cc 100644 --- a/DSView/pv/view/logicsignal.cpp +++ b/DSView/pv/view/logicsignal.cpp @@ -28,6 +28,7 @@ #include "../data/logicsnapshot.h" #include "view.h" #include "../dsvdef.h" +#include "../log.h" using namespace std; @@ -137,7 +138,9 @@ void LogicSignal::paint_mid(QPainter &p, int left, int right, QColor fore, QColo return; auto snapshot = const_cast(snapshots.front()); - if (snapshot->empty() || !snapshot->has_data(_probe->index)) + if (snapshot->empty()) + return; + if (!snapshot->has_data(_probe->index)) return; const int64_t last_sample = snapshot->get_sample_count() - 1; @@ -148,8 +151,10 @@ void LogicSignal::paint_mid(QPainter &p, int left, int right, QColor fore, QColo const double end = (offset + width + 1) * samples_per_pixel; const uint64_t end_index = min(max((int64_t)ceil(end), (int64_t)0), last_sample); const uint64_t start_index = max((uint64_t)floor(start), (uint64_t)0); + if (start_index > end_index) return; + width = min(width, (uint16_t)ceil((end_index + 1)/samples_per_pixel - offset)); const uint16_t max_togs = width / TogMaxScale; diff --git a/DSView/pv/view/viewport.cpp b/DSView/pv/view/viewport.cpp index db2f5400..652e9684 100644 --- a/DSView/pv/view/viewport.cpp +++ b/DSView/pv/view/viewport.cpp @@ -199,20 +199,21 @@ void Viewport::paintEvent(QPaintEvent *event) } void Viewport::paintSignals(QPainter &p, QColor fore, QColor back) -{ +{ std::vector traces; _view.get_traces(_type, traces); if (_view.session().get_device()->get_work_mode() == LOGIC) { - for(auto &t : traces) + for(auto t : traces) { assert(t); if (t->enabled()) t->paint_mid(p, 0, t->get_view_rect().right(), fore, back); } - } else { + } + else { if (_view.scale() != _curScale || _view.offset() != _curOffset || _view.get_signalHeight() != _curSignalHeight || @@ -225,21 +226,13 @@ void Viewport::paintSignals(QPainter &p, QColor fore, QColor back) pixmap.fill(Qt::transparent); QPainter dbp(&pixmap); - //dbp.begin(this); - for(auto &t : traces) + + for(auto t : traces) { assert(t); - /* - auto ptr = t->get(); - if (ptr->enabled()){ - ptr->paint_mid(dbp, 0, t->get_view_rect().right(), fore, back); - continue; - } - */ - if (t->enabled()) - t->paint_mid(dbp, 0, t->get_view_rect().right(), fore, back); + t->paint_mid(dbp, 0, t->get_view_rect().right(), fore, back); } _need_update = false; } diff --git a/libsigrok4DSL/dsdevice.c b/libsigrok4DSL/dsdevice.c index 8f168418..de19b5ac 100644 --- a/libsigrok4DSL/dsdevice.c +++ b/libsigrok4DSL/dsdevice.c @@ -46,7 +46,9 @@ SR_PRIV struct sr_channel *sr_channel_new(uint16_t index, int type, gboolean ena { struct sr_channel *probe; - if (!(probe = g_try_malloc0(sizeof(struct sr_channel)))) { + probe = g_try_malloc0(sizeof(struct sr_channel)); + + if (probe == NULL) { sr_err("Probe malloc failed."); return NULL; } diff --git a/libsigrok4DSL/hardware/DSL/dsl.c b/libsigrok4DSL/hardware/DSL/dsl.c index 8c2ae7ea..a89d7844 100644 --- a/libsigrok4DSL/hardware/DSL/dsl.c +++ b/libsigrok4DSL/hardware/DSL/dsl.c @@ -310,8 +310,8 @@ static int hw_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi) if (sdi->status == SR_ST_ACTIVE) { /* Device is already in use. */ - sr_info("Device is actived, can't to open, handle:%p", usb->usb_dev); - return SR_ERR; + sr_detail("The usb device is opened, handle:%p", usb->usb_dev); + return SR_OK; } if (sdi->status == SR_ST_INITIALIZING) { @@ -319,7 +319,7 @@ static int hw_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi) } dev_handel = usb->usb_dev; - sr_info("Open device instance, handle: %p", dev_handel); + sr_info("Open usb device instance, handle: %p", dev_handel); if (libusb_open(dev_handel, &usb->devhdl) != 0){ sr_err("Failed to open device: %s, handle:%p", diff --git a/libsigrok4DSL/hardware/DSL/dslogic.c b/libsigrok4DSL/hardware/DSL/dslogic.c index 245c7a6c..772e236b 100644 --- a/libsigrok4DSL/hardware/DSL/dslogic.c +++ b/libsigrok4DSL/hardware/DSL/dslogic.c @@ -1369,10 +1369,12 @@ static int dev_acquisition_start(struct sr_dev_inst *sdi, void *cb_data) /* Stop Previous GPIF acquisition */ wr_cmd.header.dest = DSL_CTL_STOP; wr_cmd.header.size = 0; + if ((ret = command_ctl_wr(usb->devhdl, wr_cmd)) != SR_OK) { sr_err("%s: Stop DSLogic acquisition failed!", __func__); return ret; - } else { + } + else { sr_info("%s: Stop Previous DSLogic acquisition!", __func__); } diff --git a/libsigrok4DSL/lib_main.c b/libsigrok4DSL/lib_main.c index 0d870703..8729c209 100644 --- a/libsigrok4DSL/lib_main.c +++ b/libsigrok4DSL/lib_main.c @@ -216,7 +216,7 @@ SR_API void ds_set_firmware_resource_dir(const char *dir) DS_RES_PATH[len + 1] = 0; } - sr_info("Firmware resource path:\"%s\"", DS_RES_PATH); + sr_info("Firmware resource directory:\"%s\"", DS_RES_PATH); } } @@ -310,7 +310,7 @@ SR_API int ds_active_device(ds_device_handle handle) } ret = SR_OK; - sr_info("%s", "Begin set current device."); + sr_info("%s", "Start activating device."); if (ds_is_collecting()) { @@ -348,7 +348,10 @@ SR_API int ds_active_device(ds_device_handle handle) sr_err("%s", "Please call ds_set_firmware_resource_dir() to set the firmware resource path."); } - sr_info("Switch \"%s\" to current device.", dev->name); + if (dev->dev_type == DEV_TYPE_FILELOG) + sr_info("virtual device name: \"%s\".", dev->name); + else + sr_info("device name: \"%s\".", dev->name); if (open_device_instance(dev) == SR_OK) { @@ -365,7 +368,7 @@ SR_API int ds_active_device(ds_device_handle handle) pthread_mutex_unlock(&lib_ctx.mutext); - sr_info("%s", "End of setting current device."); + sr_info("%s", "Activating device end."); if (!bFind) { diff --git a/libsigrok4DSL/session_driver.c b/libsigrok4DSL/session_driver.c index 504ddac4..168b29a7 100644 --- a/libsigrok4DSL/session_driver.c +++ b/libsigrok4DSL/session_driver.c @@ -435,28 +435,30 @@ static int dev_clear(void) static int dev_open(struct sr_dev_inst *sdi) { int ret; + struct session_vdev *vdev; - if (sdi->priv == NULL) - { - sdi->priv = g_try_malloc0(sizeof(struct session_vdev)); - if (sdi->priv == NULL) - { - sr_err("%s: sdi->priv malloc failed", __func__); - return SR_ERR_MALLOC; - } + assert(sdi); + + if (sdi->status == SR_ST_ACTIVE){ + // Is opened. + return SR_OK; } - struct session_vdev *vdev; + assert(sdi->priv == NULL); + + sdi->priv = g_try_malloc0(sizeof(struct session_vdev)); + if (sdi->priv == NULL) + { + sr_err("%s: sdi->priv malloc failed", __func__); + return SR_ERR_MALLOC; + } vdev = sdi->priv; + vdev->buf = g_try_malloc(CHUNKSIZE + sizeof(uint64_t)); if (vdev->buf == NULL) { - vdev->buf = g_try_malloc(CHUNKSIZE + sizeof(uint64_t)); - if (vdev->buf == NULL) - { - sr_err("%s: vdev->buf malloc failed", __func__); - return SR_ERR_MALLOC; - } + sr_err("%s: vdev->buf malloc failed", __func__); + return SR_ERR_MALLOC; } vdev->trig_pos = 0; @@ -473,6 +475,8 @@ static int dev_open(struct sr_dev_inst *sdi) vdev->mstatus.measure_valid = TRUE; vdev->archive = NULL; vdev->capfile = 0; + + sdi->status = SR_ST_ACTIVE; ret = sr_load_virtual_device_session(sdi); if (ret != SR_OK) @@ -494,9 +498,12 @@ static int dev_close(struct sr_dev_inst *sdi) g_safe_free(vdev->buf); g_safe_free(vdev->logic_buf); g_safe_free(sdi->priv); + + sdi->status = SR_ST_INACTIVE; + return SR_OK; } - return SR_OK; + return SR_ERR_CALL_STATUS; } static int dev_destroy(struct sr_dev_inst *sdi) @@ -1233,7 +1240,7 @@ SR_PRIV int sr_new_virtual_device(const char *filename, struct sr_dev_inst **out g_key_file_free(kf); g_free(metafile); - sdi = sr_dev_inst_new(mode, SR_ST_ACTIVE, NULL, NULL, NULL); + sdi = sr_dev_inst_new(mode, SR_ST_INACTIVE, NULL, NULL, NULL); sdi->driver = &session_driver; sdi->dev_type = DEV_TYPE_FILELOG; @@ -1456,9 +1463,6 @@ static int sr_load_virtual_device_session(struct sr_dev_inst *sdi) } else if (!strncmp(keys[j], "probe", 5)) { - if (!sdi) - continue; - enabled_probes++; tmp_u64 = strtoul(keys[j] + 5, NULL, 10); /* sr_session_save() */