code refactoring 3

This commit is contained in:
dreamsourcelabTAI 2022-07-20 11:39:02 +08:00
parent 693db0bb30
commit 718b41ee33
9 changed files with 225 additions and 59 deletions

View File

@ -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;

View File

@ -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

View File

@ -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++;

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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<var_cur_device_count; i++){
sr_free_device(var_device_array[i]);
var_device_array[i] = NULL;
}
// Clear all the drivers
drivers = sr_driver_list();
for (dr = drivers; *dr; dr++){
sr_dev_clear(*dr);
}
if (var_sr_context != NULL){
if (sr_exit(var_sr_context) != SR_OK)
sr_err("%s", "call sr_exit error");
var_sr_context = NULL;
}
return SR_OK;
}
void sr_set_firmware_resource_dir(const char *dir)
/**
* Set the firmware binary file directory
*/
SR_API void sr_set_firmware_resource_dir(const char *dir)
{
if (dir){
strcpy(DS_RES_PATH, dir);
@ -52,10 +120,27 @@ void sr_set_firmware_resource_dir(const char *dir)
}
}
SR_PRIV char* sr_get_firmware_res_path()
{
return DS_RES_PATH;
}
/**
* event type see enum libsigrok_event_type
* Set event callback, event type see enum libsigrok_event_type
*/
SR_API void sr_set_event_callback(libsigrok_event_callback_t *cb)
{
lib_event_callback = cb;
}
var_event_callback = cb;
}
/**
* 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()
{
if (var_sr_context == NULL){
sr_err("%s", "Must call 'sr_lib_init' first.");
return SR_ERR;
}
}

View File

@ -99,11 +99,6 @@ struct ds_trigger {
};
/*-------------------global variable----------------*/
// firmware binary file directory, endswith letter '/'
extern char DS_RES_PATH[500];
extern struct sr_context *lib_sr_context;
/*--- device.c --------------------------------------------------------------*/
SR_PRIV struct sr_channel *sr_channel_new(uint16_t index, int type,
@ -231,4 +226,7 @@ SR_PRIV int sr_usb_open(libusb_context *usb_ctx, struct sr_usb_dev_inst *usb);
SR_PRIV int sr_init(struct sr_context **ctx);
SR_PRIV int sr_exit(struct sr_context *ctx);
/*--- lib_main.c -------------------------------------------------*/
SR_PRIV char* sr_get_firmware_res_path();
#endif

View File

@ -22,7 +22,6 @@
#include <sys/time.h>
#include <stdio.h>
#include <sys/time.h>
#include <stdint.h>
#include <inttypes.h>
#include <glib.h>
@ -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