2
0
forked from Ivasoft/DSView

Code refactoring 10

This commit is contained in:
dreamsourcelabTAI
2022-08-12 13:25:50 +08:00
parent 76aeeed8ae
commit 4185d823d2
6 changed files with 57 additions and 38 deletions

View File

@@ -30,8 +30,6 @@
#undef LOG_PREFIX
#define LOG_PREFIX "dsl: "
extern struct ds_trigger *trigger;
static const int32_t probeOptions[] = {
SR_CONF_PROBE_COUPLING,
SR_CONF_PROBE_VDIV,

View File

@@ -1000,20 +1000,6 @@ static int hw_dev_acquisition_start(struct sr_dev_inst *sdi,
devc->stop = FALSE;
devc->samples_not_sent = 0;
/*
* trigger setting
*/
// if (!trigger->trigger_en || sdi->mode != LOGIC) {
// devc->trigger_stage = 0;
// } else {
// devc->trigger_mask = ds_trigger_get_mask0(TriggerStages);
// devc->trigger_value = ds_trigger_get_value0(TriggerStages);
// devc->trigger_edge = ds_trigger_get_edge0(TriggerStages);
// if (devc->trigger_edge != 0)
// devc->trigger_stage = 2;
// else
// devc->trigger_stage = 1;
// }
devc->trigger_stage = 0;
/*

View File

@@ -113,7 +113,7 @@ SR_API int ds_lib_init()
}
lib_ctx.lib_exit_flag = 0;
//init trigger
// Init trigger.
ds_trigger_init();
// Initialise all libsigrok drivers
@@ -171,14 +171,16 @@ SR_API int ds_lib_exit()
pthread_mutex_destroy(&lib_ctx.mutext); //uninit locker
//uninit trigger
ds_trigger_destroy();
// Uninit trigger.
ds_trigger_destroy();
if (sr_exit(lib_ctx.sr_ctx) != SR_OK){
sr_err("%s", "call sr_exit error");
}
lib_ctx.sr_ctx = NULL;
sr_log_uninit(); //try uninit log
return SR_OK;
@@ -461,12 +463,12 @@ SR_API int ds_device_start_collect()
sr_err("%s", "Is collecting.");
return SR_ERR_CALL_STATUS;
}
if (lib_ctx.current_device_instance == NULL){
if (di == NULL){
sr_err("%s", "Please set a current device first.");
return SR_ERR_CALL_STATUS;
}
if (lib_ctx.current_device_instance->status != SR_ST_ACTIVE){
sr_err("The device cannot be used, status:%d", SR_ST_ACTIVE);
if (di->status == SR_ST_INITIALIZING){
sr_err("Error!The device is initializing.");
return SR_ERR_CALL_STATUS;
}
if (ds_channel_is_enabled() == 0){
@@ -480,9 +482,10 @@ SR_API int ds_device_start_collect()
lib_ctx.collect_stop_flag = 0;
sr_session_new(); //create new session
// Create new session.
sr_session_new();
ret = open_device_instance(lib_ctx.current_device_instance); //open device
ret = open_device_instance(di); //open device
if (ret != SR_OK){
sr_err("%s", "Open device error!");
return ret;
@@ -531,17 +534,23 @@ END:
*/
SR_API int ds_device_stop_collect()
{
if (lib_ctx.collect_thread != NULL){
sr_session_destroy();
struct sr_dev_inst *di;
di = lib_ctx.current_device_instance;
if (lib_ctx.collect_thread != NULL){
lib_ctx.collect_stop_flag = 1;
// Stop current session.
sr_session_stop();
g_thread_join(lib_ctx.collect_thread); //Wait the collect thread ends.
lib_ctx.collect_thread = NULL;
if (lib_ctx.current_device_instance != NULL){
close_device_instance(lib_ctx.current_device_instance);
}
close_device_instance(di);
// Destroy current session.
sr_session_destroy();
}
return SR_OK;

View File

@@ -57,6 +57,7 @@
/** global variable */
extern char DS_RES_PATH[500];
extern struct ds_trigger *trigger;
typedef void (*hotplug_event_callback)(struct libusb_context *ctx, struct libusb_device *dev, int event);

View File

@@ -1073,7 +1073,7 @@ enum sr_config_option_id{
};
/** Device instance status. */
enum {
enum sr_device_status {
/** The device instance was not found. */
SR_ST_NOT_FOUND = 10000,
/** The device instance was found, but is still booting. */

View File

@@ -29,6 +29,10 @@
#undef LOG_PREFIX
#define LOG_PREFIX "session: "
/* There can only be one session at a time. */
/* 'session' is not static, it's used elsewhere (via 'extern'). */
static struct sr_session *session = NULL;
/**
* @file
@@ -60,10 +64,6 @@ struct datafeed_callback {
void *cb_data;
};
/* There can only be one session at a time. */
/* 'session' is not static, it's used elsewhere (via 'extern'). */
struct sr_session *session;
/**
* Create a new session.
*
@@ -74,8 +74,14 @@ struct sr_session *session;
*/
SR_API struct sr_session *sr_session_new(void)
{
if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
sr_err("Session malloc failed.");
if (session != NULL){
sr_info("%s", "Destroy the old session.");
sr_session_destroy(); // Destory the old.
}
session = g_try_malloc0(sizeof(struct sr_session));
if (session == NULL) {
sr_err("%s", "Session malloc failed.");
return NULL;
}
@@ -96,7 +102,7 @@ SR_API struct sr_session *sr_session_new(void)
*/
SR_API int sr_session_destroy(void)
{
if (!session) {
if (session == NULL) {
sr_detail("%s: session was NULL", __func__);
return SR_ERR_BUG;
}
@@ -143,6 +149,11 @@ static int sr_session_iteration(gboolean block)
unsigned int i;
int ret;
if (session == NULL){
sr_err("%s", "sr_session_iteration(), session is null.");
return SR_ERR_CALL_STATUS;
}
ret = g_poll(session->pollfds, session->num_sources,
block ? session->source_timeout : 0);
for (i = 0; i < session->num_sources; i++) {
@@ -183,7 +194,7 @@ static int sr_session_iteration(gboolean block)
*/
SR_API int sr_session_run(void)
{
if (!session) {
if (session == NULL) {
sr_err("%s: session was NULL; a session must be "
"created first, before running it.", __func__);
return SR_ERR_BUG;
@@ -262,6 +273,11 @@ static void datafeed_dump(const struct sr_datafeed_packet *packet)
const struct sr_datafeed_dso *dso;
const struct sr_datafeed_analog *analog;
if (packet == NULL){
sr_err("%s", "datafeed_dump() Error! packet is null.");
return;
}
switch (packet->type) {
case SR_DF_HEADER:
sr_dbg("bus: Received SR_DF_HEADER packet.");
@@ -327,6 +343,10 @@ static int _sr_session_source_add(GPollFD *pollfd, int timeout,
sr_err("%s: cb was NULL", __func__);
return SR_ERR_ARG;
}
if (session == NULL){
sr_err("%s", "_sr_session_source_add(), session is null.");
return SR_ERR_CALL_STATUS;
}
/* Note: cb_data can be NULL, that's not a bug. */
@@ -445,6 +465,11 @@ static int _sr_session_source_remove(gintptr poll_object)
GPollFD *new_pollfds;
unsigned int old;
if (session == NULL){
sr_err("%s", "_sr_session_source_remove(), session is null.");
return SR_ERR_CALL_STATUS;
}
if (!session->sources || !session->num_sources) {
sr_err("%s: sources was NULL", __func__);
return SR_ERR_BUG;