From 718b41ee33c98aa635ac70271c37622bb97d2724 Mon Sep 17 00:00:00 2001 From: dreamsourcelabTAI Date: Wed, 20 Jul 2022 11:39:02 +0800 Subject: [PATCH] code refactoring 3 --- DSView/main.cpp | 3 + DSView/pv/appcontrol.cpp | 2 +- DSView/pv/devicemanager.cpp | 3 +- libsigrok4DSL/hardware/DSL/dscope.c | 5 +- libsigrok4DSL/hardware/DSL/dsl.c | 5 +- libsigrok4DSL/hardware/DSL/dslogic.c | 10 +- libsigrok4DSL/lib_main.c | 109 ++++++++++++++++++--- libsigrok4DSL/libsigrok-internal.h | 8 +- libsigrok4DSL/libsigrok.h | 139 +++++++++++++++++++++------ 9 files changed, 225 insertions(+), 59 deletions(-) diff --git a/DSView/main.cpp b/DSView/main.cpp index 77fd30bd..f252778d 100644 --- a/DSView/main.cpp +++ b/DSView/main.cpp @@ -55,8 +55,11 @@ void usage() } +int main2(); + int main(int argc, char *argv[]) { + //return main2(); int ret = 0; const char *open_file = NULL; int logLevel = -1; diff --git a/DSView/pv/appcontrol.cpp b/DSView/pv/appcontrol.cpp index cd9c4d69..f4a19a4e 100644 --- a/DSView/pv/appcontrol.cpp +++ b/DSView/pv/appcontrol.cpp @@ -80,7 +80,7 @@ bool AppControl::Init() _session->set_sr_context(sr_ctx); QString resdir = GetResourceDir(); - sr_set_firmware_resource_dir(resdir.toUtf8().data()); + sr_set_firmware_resource_dir(resdir.toUtf8().data()); #ifdef _WIN32 //able run debug with qtcreator diff --git a/DSView/pv/devicemanager.cpp b/DSView/pv/devicemanager.cpp index 7c4107ad..7e10da14 100644 --- a/DSView/pv/devicemanager.cpp +++ b/DSView/pv/devicemanager.cpp @@ -102,7 +102,8 @@ void DeviceManager::driver_scan( auto i = _devices.begin(); while (i != _devices.end()) { if ((*i)->dev_inst() && (*i)->dev_inst()->driver == driver) { - (*i)->release(); + auto p = (*i); + p->release(); i = _devices.erase(i); } else { i++; diff --git a/libsigrok4DSL/hardware/DSL/dscope.c b/libsigrok4DSL/hardware/DSL/dscope.c index beaa1f35..92861b44 100644 --- a/libsigrok4DSL/hardware/DSL/dscope.c +++ b/libsigrok4DSL/hardware/DSL/dscope.c @@ -291,11 +291,12 @@ static GSList *scan(GSList *options) devices = g_slist_append(devices, sdi); } else { char *firmware; - if (!(firmware = g_try_malloc(strlen(DS_RES_PATH)+strlen(prof->firmware)+1))) { + char *res_path = sr_get_firmware_res_path(); + if (!(firmware = g_try_malloc(strlen(res_path)+strlen(prof->firmware)+1))) { sr_err("Firmware path malloc error!"); return NULL; } - strcpy(firmware, DS_RES_PATH); + strcpy(firmware, res_path); strcat(firmware, prof->firmware); if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, firmware) == SR_OK) diff --git a/libsigrok4DSL/hardware/DSL/dsl.c b/libsigrok4DSL/hardware/DSL/dsl.c index abbe1e83..326b1918 100644 --- a/libsigrok4DSL/hardware/DSL/dsl.c +++ b/libsigrok4DSL/hardware/DSL/dsl.c @@ -1902,11 +1902,12 @@ SR_PRIV int dsl_dev_open(struct sr_dev_driver *di, struct sr_dev_inst *sdi, gboo if (sdi->status == SR_ST_ACTIVE) { if (!(*fpga_done)) { char *fpga_bit; - if (!(fpga_bit = g_try_malloc(strlen(DS_RES_PATH)+strlen(devc->profile->fpga_bit33)+1))) { + char *res_path = sr_get_firmware_res_path(); + if (!(fpga_bit = g_try_malloc(strlen(res_path)+strlen(devc->profile->fpga_bit33)+1))) { sr_err("fpag_bit path malloc error!"); return SR_ERR_MALLOC; } - strcpy(fpga_bit, DS_RES_PATH); + strcpy(fpga_bit, res_path); switch(devc->th_level) { case SR_TH_3V3: strcat(fpga_bit, devc->profile->fpga_bit33); diff --git a/libsigrok4DSL/hardware/DSL/dslogic.c b/libsigrok4DSL/hardware/DSL/dslogic.c index 70a30df3..c2862ffd 100644 --- a/libsigrok4DSL/hardware/DSL/dslogic.c +++ b/libsigrok4DSL/hardware/DSL/dslogic.c @@ -361,11 +361,12 @@ static GSList *scan(GSList *options) devices = g_slist_append(devices, sdi); } else { char *firmware; - if (!(firmware = g_try_malloc(strlen(DS_RES_PATH)+strlen(prof->firmware)+1))) { + char *res_path = sr_get_firmware_res_path(); + if (!(firmware = g_try_malloc(strlen(res_path)+strlen(prof->firmware)+1))) { sr_err("Firmware path malloc error!"); return NULL; } - strcpy(firmware, DS_RES_PATH); + strcpy(firmware, res_path); strcat(firmware, prof->firmware); if (ezusb_upload_firmware(devlist[i], USB_CONFIGURATION, firmware) == SR_OK) @@ -988,11 +989,12 @@ static int config_set(int id, GVariant *data, struct sr_dev_inst *sdi, ret = SR_ERR; } char *fpga_bit; - if (!(fpga_bit = g_try_malloc(strlen(DS_RES_PATH)+strlen(devc->profile->fpga_bit33)+1))) { + char *res_path = sr_get_firmware_res_path(); + if (!(fpga_bit = g_try_malloc(strlen(res_path)+strlen(devc->profile->fpga_bit33)+1))) { sr_err("fpag_bit path malloc error!"); return SR_ERR_MALLOC; } - strcpy(fpga_bit, DS_RES_PATH); + strcpy(fpga_bit, res_path); switch(devc->th_level) { case SR_TH_3V3: strcat(fpga_bit, devc->profile->fpga_bit33); diff --git a/libsigrok4DSL/lib_main.c b/libsigrok4DSL/lib_main.c index 297e4dfd..184cff9e 100644 --- a/libsigrok4DSL/lib_main.c +++ b/libsigrok4DSL/lib_main.c @@ -20,26 +20,94 @@ */ #include "libsigrok-internal.h" +#include "log.h" -struct sr_context *lib_sr_context = NULL; -char DS_RES_PATH[500] = {0}; -libsigrok_event_callback_t *lib_event_callback = NULL; +struct device_all_info +{ + struct sr_device_info _base_info; +}; +#define SR_DEVICE_MAX_COUNT 100 + +static char DS_RES_PATH[500] = {0}; +static struct sr_context *var_sr_context = NULL; +static libsigrok_event_callback_t *var_event_callback = NULL; +static struct device_all_info* var_device_array[SR_DEVICE_MAX_COUNT] = {0}; +static int var_cur_device_count = 0; + + //----------------------------private function---------------- + + /** + * Free device resource + */ + static sr_free_device(struct device_all_info *dev) + { + if (dev){ + free(dev); + } + } + +/** + * Must call first + */ SR_API int sr_lib_init() { - return sr_init(&lib_sr_context); + int ret = 0; + struct sr_dev_driver **drivers = NULL; + struct sr_dev_driver **dr = NULL; + + ret = sr_init(&var_sr_context); + if (ret != SR_OK){ + return ret; + } + + // Initialise all libsigrok drivers + drivers = sr_driver_list(); + for (dr = drivers; *dr; dr++) { + if (sr_driver_init(var_sr_context, *dr) != SR_OK) { + sr_err("Failed to initialize driver '%s'", (*dr)->name); + return SR_ERR; + } + } + + return SR_OK; } +/** + * Free all resource before program exits + */ SR_API int sr_lib_exit() -{ - if (lib_sr_context != NULL){ - return sr_exit(lib_sr_context); - lib_sr_context = NULL; +{ + struct sr_dev_driver **drivers = NULL; + struct sr_dev_driver **dr = NULL; + int i = 0; + + // free all device + for (i=0; i #include -#include #include #include #include @@ -81,8 +80,6 @@ enum { */ }; -typedef int bool_t; - #define SR_MAX_PROBENAME_LEN 32 #define DS_MAX_ANALOG_PROBES_NUM 4 #define DS_MAX_DSO_PROBES_NUM 2 @@ -1279,6 +1276,9 @@ SR_API int sr_listen_hotplug(struct sr_context *ctx, hotplug_event_callback call SR_API int sr_close_hotplug(struct sr_context *ctx); SR_API void sr_hotplug_wait_timout(struct sr_context *ctx); +SR_API int sr_init(struct sr_context **ctx); +SR_API int sr_exit(struct sr_context *ctx); + /*--- device.c --------------------------------------------------------------*/ SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi, @@ -1418,47 +1418,122 @@ SR_API void sr_log_level(int level); /*---event define ---------------------------------------------*/ enum libsigrok_event_type { - EV_DEVICE_ATTACH = 0, + // A new device attachs, user need calls sr_device_scan_list to update new list, + // And call sr_device_get_list to get new list, the last one is new. + // User can call sr_device_select to swith a new device. + EV_DEVICE_ATTACH = 0, + // A device detachs, user need calls sr_device_scan_list to update new list, + // And call sr_device_get_list to get new list. + // User can call sr_device_select to swith a new device. + EV_DEVICE_DETACH = 1, + + // User can call sr_device_get_list to get new list. + EV_CURRENT_DEVICE_CHANGED = 2, }; -struct sr_device_handle; -typedef struct sr_device_handle sr_device_handle; - -struct sr_device_info -{ - sr_device_handle *_handle; - char _name[50]; - char _full_name[300]; - bool_t _is_active; //is the current device - bool_t _is_hardware; -}; - -/*---lib_main.c -----------------------------------------------*/ - -typedef void (*libsigrok_event_callback_t)(int event); - -SR_API int sr_lib_init(); - -SR_API int sr_lib_exit(); +typedef unsigned long long sr_device_handle; /** - * event type see enum libsigrok_event_type + * Device base info + */ +struct sr_device_info +{ + sr_device_handle _handle; + char _name[50]; + char _full_name[260]; + int _is_hardware; + int _is_current; //is actived +}; + +struct sr_task_progress +{ + int _progress; + int _is_end; +}; + +struct sr_store_extra_data +{ + char _name[50]; + char *_data; + int _data_length; +}; + + +/*---lib_main.c -----------------------------------------------*/ + +/** + * event see enum libsigrok_event_type + */ +typedef void (*libsigrok_event_callback_t)(int event); + +/** + * Must call first + */ +SR_API int sr_lib_init(); + +/** + * Free all resource before program exits + */ +SR_API int sr_lib_exit(); + +/** + * Set event callback, event type see enum libsigrok_event_type */ SR_API void sr_set_event_callback(libsigrok_event_callback_t *cb); /** - * Store current session data to file - */ -SR_API int sr_store_session_data(const char *file_path); - - - -/** - * firmware binary file directory + * Set the firmware binary file directory, + * User must call it to set the firmware resource directory */ SR_API void sr_set_firmware_resource_dir(const char *dir); +/** + * When device attached or detached, scan all devices to get the new list. + * The current list will be changed + */ +SR_API int sr_device_scan_list(); + +/** + * Get the device list, the last item is null. + * Call free to release buffer. If the list is empty, it returns null. + */ +SR_API struct sr_device_info* sr_device_get_list(int *out_count); + +/** + * Active a device, if success, it will trigs the event of EV_CURRENT_DEVICE_CHANGED. + * If the old actived device is hardware, maybe user need store the data first. + */ +SR_API int sr_device_select(sr_device_handle handle); + +/** + * Create a device from session file, it auto load the data. + */ +SR_API int sr_device_from_file(const char *file_path); + +/** + * Get current sample count + */ +SR_API uint64_t sr_sample_count(); + +/** + * Store current session data. + * @ext_data_array is the extra data. + */ +SR_API int sr_store_session_data(struct sr_task_progress *prog, const char *file_path, + struct sr_store_extra_data *ext_data_array, int ext_data_count); + +/** + * Start collect data + */ +SR_API int sr_device_start_collect(); + +/** + * Stop collect data + */ +SR_API int sr_device_start_collect(); + + #ifdef __cplusplus } #endif