DSView/libsigrok4DSL/lib_main.c

742 lines
17 KiB
C
Raw Normal View History

2022-07-15 11:57:50 +08:00
/*
* This file is part of the DSView project.
* DSView is based on PulseView.
*
* Copyright (C) 2022 DreamSourceLab <support@dreamsourcelab.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libsigrok-internal.h"
2022-07-20 11:39:02 +08:00
#include "log.h"
2022-08-08 15:59:50 +08:00
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
2022-07-15 11:57:50 +08:00
2022-08-01 10:40:29 +08:00
#ifdef _WIN32
#include <windows.h>
#define _sleep(m) Sleep((m))
#else
#include <unistd.h>
#define _sleep(m) usleep((m) * 1000)
#endif
#undef LOG_PREFIX
#define LOG_PREFIX "lib_main: "
char DS_RES_PATH[500] = {0};
2022-08-08 15:59:50 +08:00
struct sr_lib_context
{
libsigrok_event_callback_t event_callback;
struct sr_context *sr_ctx;
GList* device_list; // All device instance, sr_dev_inst* type
pthread_mutex_t mutext;
GThread *hotplug_thread;
int lib_exit_flag;
int attach_event_flag;
int detach_event_flag;
int is_waitting_reconnect;
int check_reconnect_times;
struct libusb_device *attach_device_handle;
struct libusb_device *detach_device_handle;
2022-08-10 16:29:15 +08:00
struct sr_device_info current_device_info;
struct sr_dev_inst *current_device_instance;
2022-08-08 15:59:50 +08:00
};
2022-08-01 10:40:29 +08:00
static void hotplug_event_listen_callback(struct libusb_context *ctx, struct libusb_device *dev, int event);
static void usb_hotplug_process_proc();
2022-08-08 15:59:50 +08:00
static void destroy_device_instance(struct sr_dev_inst *dev);
2022-08-10 16:29:15 +08:00
static void close_device_instance(struct sr_dev_inst *dev);
static int open_device_instance(struct sr_dev_inst *dev);
2022-08-08 15:59:50 +08:00
static struct sr_lib_context lib_ctx = {
.event_callback = NULL,
.sr_ctx = NULL,
.device_list = NULL,
.hotplug_thread = NULL,
.lib_exit_flag = 0,
.attach_event_flag = 0,
.detach_event_flag = 0,
.is_waitting_reconnect = 0,
.check_reconnect_times = 0,
.attach_device_handle = NULL,
.detach_device_handle = NULL,
2022-08-10 16:29:15 +08:00
.current_device_info = {
.handle = 0,
2022-08-08 15:59:50 +08:00
.name[0] = '\0',
.is_current = 0,
.dev_type = DEV_TYPE_UNKOWN,
},
2022-08-10 16:29:15 +08:00
.current_device_instance = NULL,
2022-08-08 15:59:50 +08:00
};
2022-07-20 11:39:02 +08:00
/**
* Must call first
*/
2022-07-15 11:57:50 +08:00
SR_API int sr_lib_init()
2022-08-08 15:59:50 +08:00
{
2022-07-20 11:39:02 +08:00
int ret = 0;
struct sr_dev_driver **drivers = NULL;
struct sr_dev_driver **dr = NULL;
2022-08-08 15:59:50 +08:00
if (lib_ctx.sr_ctx != NULL){
2022-07-28 16:57:15 +08:00
return SR_ERR_HAVE_DONE;
}
2022-08-08 15:59:50 +08:00
sr_log_init(); //try init log
sr_info("Init %s.", SR_LIB_NAME);
ret = sr_init(&lib_ctx.sr_ctx);
2022-07-20 11:39:02 +08:00
if (ret != SR_OK){
return ret;
}
2022-08-08 15:59:50 +08:00
lib_ctx.lib_exit_flag = 0;
2022-07-20 11:39:02 +08:00
// Initialise all libsigrok drivers
drivers = sr_driver_list();
for (dr = drivers; *dr; dr++) {
2022-08-08 15:59:50 +08:00
if (sr_driver_init(lib_ctx.sr_ctx, *dr) != SR_OK) {
2022-07-20 11:39:02 +08:00
sr_err("Failed to initialize driver '%s'", (*dr)->name);
return SR_ERR;
}
}
2022-08-08 15:59:50 +08:00
pthread_mutex_init(&lib_ctx.mutext, NULL); //init locker
2022-07-20 11:39:02 +08:00
2022-08-08 15:59:50 +08:00
lib_ctx.sr_ctx->hotplug_tv.tv_sec = 0;
lib_ctx.sr_ctx->hotplug_tv.tv_usec = 0;
2022-08-01 10:40:29 +08:00
2022-08-08 15:59:50 +08:00
sr_listen_hotplug(lib_ctx.sr_ctx, hotplug_event_listen_callback);
2022-08-01 10:40:29 +08:00
/** Start usb hotplug thread */
2022-08-08 15:59:50 +08:00
lib_ctx.hotplug_thread = g_thread_new("hotplug_proc", usb_hotplug_process_proc, NULL);
2022-08-01 10:40:29 +08:00
2022-07-20 11:39:02 +08:00
return SR_OK;
2022-07-15 11:57:50 +08:00
}
2022-07-20 11:39:02 +08:00
/**
* Free all resource before program exits
*/
2022-07-15 11:57:50 +08:00
SR_API int sr_lib_exit()
2022-08-08 15:59:50 +08:00
{
GSList *l;
2022-08-01 10:40:29 +08:00
2022-08-08 15:59:50 +08:00
if (lib_ctx.sr_ctx == NULL){
2022-07-28 16:57:15 +08:00
return SR_ERR_HAVE_DONE;
2022-07-20 11:39:02 +08:00
}
2022-07-28 16:57:15 +08:00
2022-08-08 15:59:50 +08:00
sr_info("Uninit %s.", SR_LIB_NAME);
sr_close_hotplug(lib_ctx.sr_ctx);
2022-08-01 10:40:29 +08:00
2022-08-08 15:59:50 +08:00
lib_ctx.lib_exit_flag = 1; //all thread to exit
2022-08-01 10:40:29 +08:00
2022-08-08 15:59:50 +08:00
if (lib_ctx.hotplug_thread != NULL){
g_thread_join(lib_ctx.hotplug_thread);
lib_ctx.hotplug_thread = NULL;
2022-08-01 10:40:29 +08:00
}
2022-07-28 16:57:15 +08:00
// Release all device
2022-08-08 15:59:50 +08:00
for (l = lib_ctx.device_list; l; l = l->next){
destroy_device_instance((struct sr_dev_inst*)l->data);
2022-07-20 11:39:02 +08:00
}
2022-08-08 15:59:50 +08:00
g_safe_free_list(lib_ctx.device_list);
pthread_mutex_destroy(&lib_ctx.mutext); //uninit locker
2022-07-20 11:39:02 +08:00
2022-08-08 15:59:50 +08:00
if (sr_exit(lib_ctx.sr_ctx) != SR_OK){
2022-07-28 16:57:15 +08:00
sr_err("%s", "call sr_exit error");
}
2022-08-08 15:59:50 +08:00
lib_ctx.sr_ctx = NULL;
sr_log_uninit(); //try uninit log
2022-07-20 11:39:02 +08:00
2022-07-15 11:57:50 +08:00
return SR_OK;
}
2022-07-20 11:39:02 +08:00
/**
* Set the firmware binary file directory
*/
SR_API void sr_set_firmware_resource_dir(const char *dir)
2022-07-15 11:57:50 +08:00
{
if (dir){
strcpy(DS_RES_PATH, dir);
int len = strlen(DS_RES_PATH);
if (DS_RES_PATH[len-1] != '/'){
DS_RES_PATH[len] = '/';
DS_RES_PATH[len + 1] = 0;
}
2022-08-08 15:59:50 +08:00
2022-08-10 16:29:15 +08:00
sr_info("Firmware resource path:\"%s\"", DS_RES_PATH);
2022-07-15 11:57:50 +08:00
}
}
/**
2022-07-20 11:39:02 +08:00
* Set event callback, event type see enum libsigrok_event_type
2022-07-15 11:57:50 +08:00
*/
2022-08-08 15:59:50 +08:00
SR_API void sr_set_event_callback(libsigrok_event_callback_t cb)
{
lib_ctx.event_callback = cb;
}
/**
* Get the device list, if the field _handle is 0, the list visited to end.
* User need call free() to release the buffer. If the list is empty, the out_list is null.
*/
SR_API int sr_device_get_list(struct sr_device_info** out_list, int *out_count)
{
int num;
struct sr_device_info *array = NULL;
struct sr_device_info *p = NULL;
GList *l;
struct sr_dev_inst *dev;
if (out_list == NULL){
return SR_ERR_ARG;
}
*out_list = NULL;
pthread_mutex_lock(&lib_ctx.mutext);
num = g_slist_length(lib_ctx.device_list);
if (num == 0){
pthread_mutex_unlock(&lib_ctx.mutext);
return SR_OK;
}
array = (struct sr_device_info*)malloc(sizeof(struct sr_device_info) * (num+1));
if (array == NULL){
pthread_mutex_unlock(&lib_ctx.mutext);
return SR_ERR_MALLOC;
}
p = array;
for (l=lib_ctx.device_list; l; l = l->next){
dev = l->data;
p->handle = dev->handle;
strncpy(p->name, dev->name, sizeof(dev->name) - 1);
p->dev_type = dev->dev_type;
2022-08-10 16:29:15 +08:00
p->is_current = (dev == lib_ctx.current_device_instance);
2022-08-08 15:59:50 +08:00
p++;
}
p->handle = 0; //is the end
p->name[0] = '\0';
if (out_count){
*out_count = num;
}
pthread_mutex_unlock(&lib_ctx.mutext);
*out_list = array;
return SR_OK;
}
2022-08-10 16:29:15 +08:00
/**
* Active a device, if success, it will trigs the event of SR_EV_CURRENT_DEVICE_CHANGED.
*/
SR_API int sr_device_select(sr_device_handle handle)
{
GList *l;
struct sr_dev_inst *dev;
int bFind = 0;
int ret;
if (handle == NULL){
return SR_ERR_ARG;
}
ret = SR_OK;
sr_info("%s", "Begin set current device.");
pthread_mutex_lock(&lib_ctx.mutext);
if (lib_ctx.current_device_instance != NULL) {
sr_info("Close the previous device \"%s\"", lib_ctx.current_device_instance->name);
close_device_instance(lib_ctx.current_device_instance);
lib_ctx.current_device_instance = NULL;
lib_ctx.current_device_info.handle = NULL;
lib_ctx.current_device_info.name[0] = '\0';
}
// To open the new.
for (l = lib_ctx.device_list; l; l = l->next){
dev = l->data;
if (dev->handle == handle){
bFind = 1;
if (dev->dev_type == DEV_TYPE_USB && DS_RES_PATH[0] == '\0'){
sr_err("%s", "Please call sr_set_firmware_resource_dir() to set the firmware resource path.");
}
sr_info("Switch \"%s\" to current device.", dev->name);
if (open_device_instance(dev) == SR_OK)
{
lib_ctx.current_device_info.handle = dev->handle;
lib_ctx.current_device_info.dev_type = dev->dev_type;
lib_ctx.current_device_info.is_current = 1;
strncpy(lib_ctx.current_device_info.name, dev->name, sizeof(lib_ctx.current_device_info.name) - 1);
lib_ctx.current_device_instance = dev;
}
else{
sr_err("%s", "Open device error!");
ret = SR_ERR_CALL_STATUS;
}
break;
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
sr_info("%s", "End of setting current device.");
if (!bFind){
sr_err("sr_device_select() error, can't find the device.");
return SR_ERR_CALL_STATUS;
}
return ret;
}
/**
* Active a device, if success, it will trigs the event of SR_EV_CURRENT_DEVICE_CHANGED.
* @index is -1, will select the last one.
*/
SR_API int sr_device_select_by_index(int index)
{
GList *l;
struct sr_dev_inst *dev;
sr_device_handle handle = NULL;
sr_device_handle lst_handle = NULL;
int i=0;
pthread_mutex_lock(&lib_ctx.mutext);
// Get index
for (l = lib_ctx.device_list; l; l = l->next){
dev = l->data;
lst_handle = dev->handle;
if (index == i){
handle = dev->handle;
break;
}
++i;
}
pthread_mutex_unlock(&lib_ctx.mutext);
if (index == -1){
handle = lst_handle; // Get the last one.
}
if (handle == NULL){
sr_err("%s", "sr_device_select_by_index(), index is error!");
return SR_ERR_CALL_STATUS;
}
return sr_device_select(handle);
}
2022-08-08 15:59:50 +08:00
/**-------------------internal function ---------------*/
/**
* Check whether the USB device is in the device list.
*/
SR_PRIV int sr_usb_device_is_exists(libusb_device *usb_dev)
2022-07-15 11:57:50 +08:00
{
2022-08-08 15:59:50 +08:00
GList *l;
struct sr_dev_inst *dev;
struct sr_usb_dev_inst *usb_dev_info;
int bFind = 0;
if (usb_dev == NULL){
sr_err("%s", "sr_usb_device_is_exists(), @usb_dev is null.");
return 0;
}
pthread_mutex_lock(&lib_ctx.mutext);
for (l = lib_ctx.device_list; l; l = l->next){
dev = l->data;
usb_dev_info = dev->conn;
if (dev->dev_type == DEV_TYPE_USB
&& usb_dev_info != NULL
&& usb_dev_info->usb_dev == usb_dev){
bFind = 1;
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
return bFind;
2022-08-01 10:40:29 +08:00
}
2022-08-08 15:59:50 +08:00
/**
* Remove one device from the list, and destory it.
* User need to call sr_device_get_list() to get the new list.
*/
SR_API int sr_remove_device(sr_device_handle handle)
{
GList *l;
struct sr_dev_inst *dev;
int bFind = 0;
if (handle == NULL){
return SR_ERR_ARG;
}
pthread_mutex_lock(&lib_ctx.mutext);
for (l = lib_ctx.device_list; l; l = l->next){
dev = l->data;
if (dev->handle == handle){
lib_ctx.device_list = g_slist_remove(lib_ctx.device_list, l->data);
destroy_device_instance(dev);
bFind = 1;
2022-08-10 16:29:15 +08:00
if (dev == lib_ctx.current_device_instance){
lib_ctx.current_device_instance = NULL;
}
2022-08-08 15:59:50 +08:00
break;
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
if (bFind == 0){
return SR_ERR_CALL_STATUS;
}
return SR_OK;
}
/**
* Get the current device info.
* If the current device is not exists, the handle filed will be set null.
*/
SR_API int sr_get_current_device_info(struct sr_device_info *info)
{
if (info == NULL){
return SR_ERR_ARG;
}
info->handle = NULL;
info->name[0] = '\0';
info->is_current = 0;
info->dev_type = DEV_TYPE_UNKOWN;
2022-08-10 16:29:15 +08:00
if (lib_ctx.current_device_info.handle != NULL){
info->handle = lib_ctx.current_device_info.handle;
strncpy(info->name, lib_ctx.current_device_info.name, sizeof(info->name));
2022-08-08 15:59:50 +08:00
info->is_current = 1;
2022-08-10 16:29:15 +08:00
info->dev_type = lib_ctx.current_device_info.dev_type;
2022-08-08 15:59:50 +08:00
}
return SR_OK;
}
2022-08-01 10:40:29 +08:00
/**-------------------private function ---------------*/
2022-08-08 15:59:50 +08:00
static int update_device_handle(struct libusb_device *old_dev, struct libusb_device *new_dev)
2022-08-01 10:40:29 +08:00
{
2022-08-08 15:59:50 +08:00
GList *l;
2022-08-10 16:29:15 +08:00
struct sr_dev_inst *dev;
2022-08-08 15:59:50 +08:00
struct sr_usb_dev_inst *usb_dev_info;
uint8_t bus;
2022-08-10 16:29:15 +08:00
uint8_t address;
int bFind = 0;
2022-08-08 15:59:50 +08:00
pthread_mutex_lock(&lib_ctx.mutext);
2022-08-01 10:40:29 +08:00
2022-08-08 15:59:50 +08:00
for (l = lib_ctx.device_list; l; l = l->next){
dev = l->data;
usb_dev_info = dev->conn;
if (dev->dev_type == DEV_TYPE_USB
&& usb_dev_info != NULL
&& usb_dev_info->usb_dev == old_dev){
2022-08-10 16:29:15 +08:00
// Release the old device and the resource.
if (dev == lib_ctx.current_device_instance){
sr_info("%s", "Release the old device's resource.");
close_device_instance(dev);
}
2022-08-08 15:59:50 +08:00
bus = libusb_get_bus_number(new_dev);
address = libusb_get_device_address(new_dev);
2022-08-10 16:29:15 +08:00
usb_dev_info->usb_dev = new_dev;
usb_dev_info->bus = bus;
usb_dev_info->address = address;
dev->handle = new_dev;
bFind = 1;
2022-08-08 15:59:50 +08:00
2022-08-10 16:29:15 +08:00
// Reopen the device.
if (dev == lib_ctx.current_device_instance){
sr_info("%s", "Reopen the current device.");
open_device_instance(dev);
}
2022-08-08 15:59:50 +08:00
break;
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
2022-08-10 16:29:15 +08:00
if (bFind){
return SR_OK;
}
return SR_ERR;
2022-08-01 10:40:29 +08:00
}
static void hotplug_event_listen_callback(struct libusb_context *ctx, struct libusb_device *dev, int event)
{
2022-08-08 15:59:50 +08:00
int bDone = 0;
2022-08-01 10:40:29 +08:00
2022-08-08 15:59:50 +08:00
if (dev == NULL){
sr_err("%s", "hotplug_event_listen_callback(), @dev is null.");
return;
}
if (event == USB_EV_HOTPLUG_ATTACH){
2022-08-10 16:29:15 +08:00
sr_info("One device attached,handle:%p", dev);
2022-08-08 15:59:50 +08:00
if (lib_ctx.is_waitting_reconnect){
if (lib_ctx.attach_device_handle != NULL){
sr_err("One attached device haven't processed complete,handle:%p",
lib_ctx.attach_device_handle);
}
if (lib_ctx.detach_device_handle == NULL){
sr_err("%s", "The detached device handle is null, but the status is waitting for reconnect.");
}
else{
2022-08-10 16:29:15 +08:00
if (update_device_handle(lib_ctx.detach_device_handle, dev) != SR_OK){
2022-08-08 15:59:50 +08:00
bDone = 1;
sr_info("%s", "One device loose contact, but it reconnect success.");
}
else{
sr_err("Update device handle error! can't find the old.");
}
lib_ctx.detach_device_handle = NULL;
}
}
if (bDone == 0){
lib_ctx.attach_event_flag = 1; // Is a new device attched.
lib_ctx.attach_device_handle = dev;
2022-08-01 10:40:29 +08:00
}
2022-08-08 15:59:50 +08:00
lib_ctx.is_waitting_reconnect = 0;
2022-08-01 10:40:29 +08:00
}
else if (event == USB_EV_HOTPLUG_DETTACH){
2022-08-08 15:59:50 +08:00
sr_info("One device detached,handle:%p", dev);
if (lib_ctx.detach_device_handle != NULL){
sr_err("One detached device haven't processed complete,handle:%p",
lib_ctx.detach_device_handle);
}
2022-08-10 16:29:15 +08:00
if (lib_ctx.current_device_info.handle != NULL && lib_ctx.current_device_info.dev_type == DEV_TYPE_USB){
}
2022-08-08 15:59:50 +08:00
/**
* Begin to wait the device reconnect, if timeout, will process the detach event.
*/
lib_ctx.is_waitting_reconnect = 1;
lib_ctx.check_reconnect_times = 0;
lib_ctx.detach_device_handle = dev;
2022-08-01 10:40:29 +08:00
}
else{
2022-08-08 15:59:50 +08:00
sr_err("%s", "Unknown usb device event");
2022-07-20 11:39:02 +08:00
}
}
2022-07-28 16:57:15 +08:00
2022-08-01 10:40:29 +08:00
static void process_attach_event()
{
2022-08-08 15:59:50 +08:00
struct sr_dev_driver **drivers;
GList *dev_list;
GSList *l;
GList *cur_list;
struct sr_dev_driver *dr;
int num = 0;
2022-08-10 16:29:15 +08:00
sr_info("%s", "Process device attach event.");
2022-08-08 15:59:50 +08:00
if (lib_ctx.attach_device_handle == NULL){
sr_err("%s", "The attached device handle is null.");
return;
}
drivers = sr_driver_list();
while (*drivers)
{
dr = *drivers;
if (dr->driver_type == DRIVER_TYPE_HARDWARE)
{
dev_list = dr->scan(NULL);
if (dev_list != NULL){
sr_info("Get new device list by driver \"%s\"", dr->name);
pthread_mutex_lock(&lib_ctx.mutext);
cur_list = lib_ctx.device_list;
for (l= dev_list; l; l = l->next){
cur_list = g_slist_append(cur_list, l->data);
num++;
}
lib_ctx.device_list = cur_list;
pthread_mutex_unlock(&lib_ctx.mutext);
g_slist_free(dev_list);
}
}
drivers++;
}
if (lib_ctx.event_callback != NULL && num > 0){
// Tell user one new device attched, and the list is updated.
lib_ctx.event_callback(SR_EV_NEW_DEVICE_ATTACH);
}
lib_ctx.attach_device_handle = NULL;
2022-08-01 10:40:29 +08:00
}
static void process_detach_event()
{
2022-08-08 15:59:50 +08:00
libusb_device *ev_dev;
int ev = SR_EV_INACTIVE_DEVICE_DETACH;
GList *l;
struct sr_dev_inst *dev;
struct sr_usb_dev_inst *usb_dev_info;
struct sr_dev_driver *driver_ins;
2022-08-01 10:40:29 +08:00
sr_info("%s", "Process device detach event.");
2022-08-08 15:59:50 +08:00
ev_dev = lib_ctx.detach_device_handle;
if (ev_dev == NULL){
sr_err("%s", "The detached device handle is null.");
return;
}
lib_ctx.detach_device_handle = NULL;
pthread_mutex_lock(&lib_ctx.mutext);
for (l = lib_ctx.device_list; l; l = l->next){
dev = l->data;
usb_dev_info = dev->conn;
if (dev->dev_type == DEV_TYPE_USB
&& usb_dev_info != NULL
&& usb_dev_info->usb_dev == ev_dev){
//Found the device, and remove it.
lib_ctx.device_list = g_slist_remove(lib_ctx.device_list, l->data);
destroy_device_instance(dev);
break;
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
2022-08-10 16:29:15 +08:00
if (ev_dev == lib_ctx.current_device_info.handle)
2022-08-08 15:59:50 +08:00
ev = SR_EV_CURRENT_DEVICE_DETACH;
if (lib_ctx.event_callback != NULL){
//Tell user one new device detached, and the list is updated.
lib_ctx.event_callback(ev);
}
2022-08-01 10:40:29 +08:00
}
static void usb_hotplug_process_proc()
{
sr_info("%s", "Hotplug thread start!");
2022-08-08 15:59:50 +08:00
while (!lib_ctx.lib_exit_flag)
2022-08-01 10:40:29 +08:00
{
2022-08-08 15:59:50 +08:00
sr_hotplug_wait_timout(lib_ctx.sr_ctx);
2022-08-01 10:40:29 +08:00
2022-08-08 15:59:50 +08:00
if (lib_ctx.attach_event_flag){
2022-08-01 10:40:29 +08:00
process_attach_event();
2022-08-08 15:59:50 +08:00
lib_ctx.attach_event_flag = 0;
2022-08-01 10:40:29 +08:00
}
2022-08-08 15:59:50 +08:00
if (lib_ctx.detach_event_flag){
2022-08-01 10:40:29 +08:00
process_detach_event();
2022-08-08 15:59:50 +08:00
lib_ctx.detach_event_flag = 0;
2022-08-01 10:40:29 +08:00
}
_sleep(100);
2022-08-08 15:59:50 +08:00
if (lib_ctx.is_waitting_reconnect){
lib_ctx.check_reconnect_times++;
2022-08-01 10:40:29 +08:00
// 500ms
2022-08-08 15:59:50 +08:00
if (lib_ctx.check_reconnect_times == 5){
2022-08-01 10:40:29 +08:00
//Device loose contact,wait for it reconnection timeout.
2022-08-08 15:59:50 +08:00
lib_ctx.detach_event_flag = 1; // use detach event
lib_ctx.is_waitting_reconnect = 0;
2022-08-01 10:40:29 +08:00
}
}
}
sr_info("%s", "Hotplug thread end!");
2022-08-08 15:59:50 +08:00
}
static void destroy_device_instance(struct sr_dev_inst *dev)
{
if (dev == NULL || dev->driver == NULL){
sr_err("%s", "destroy_device_instance() argument error.");
return;
}
struct sr_dev_driver *driver_ins;
driver_ins = dev->driver;
if (driver_ins->dev_destroy)
driver_ins->dev_destroy(dev);
else if (driver_ins->dev_close)
driver_ins->dev_close(dev);
}
2022-08-10 16:29:15 +08:00
static void close_device_instance(struct sr_dev_inst *dev)
{
if (dev == NULL || dev->driver == NULL){
sr_err("%s", "close_device_instance() argument error.");
return;
}
struct sr_dev_driver *driver_ins;
driver_ins = dev->driver;
if (driver_ins->dev_close)
driver_ins->dev_close(dev);
}
static int open_device_instance(struct sr_dev_inst *dev)
{
if (dev == NULL || dev->driver == NULL){
sr_err("%s", "open_device_instance() argument error.");
return SR_ERR_ARG;
}
struct sr_dev_driver *driver_ins;
driver_ins = dev->driver;
if (driver_ins->dev_open){
driver_ins->dev_open(dev);
return SR_OK;
}
return SR_ERR_CALL_STATUS;
}