-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlib_main.c
1712 lines (1418 loc) · 35.5 KB
/
lib_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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"
#include "log.h"
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#define MAX_DEVCIE_LIST_LENGTH 20
// Wait the device reconnect for 500ms.
#define CHECK_DEV_RECONNECT_TIMES 5
#undef LOG_PREFIX
#define LOG_PREFIX "lib_main: "
// Have been encoding.
char DS_RES_PATH[500];
char DS_USR_PATH[500];
struct sr_lib_context
{
dslib_event_callback_t event_callback;
struct sr_context *sr_ctx;
GSList *device_list; // All device instance, sr_dev_inst* type
pthread_mutex_t mutext;
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;
struct sr_dev_inst *actived_device_instance;
GThread *hotplug_thread;
GThread *collect_thread;
ds_datafeed_callback_t data_forward_callback;
int callback_thread_count;
int is_delay_destory_actived_device;
int is_stop_by_detached;
int transaction_id;
int transaction_command;
int last_error;
};
static void hotplug_event_listen_callback(struct libusb_context *ctx, struct libusb_device *dev, int event);
static gpointer usb_hotplug_process_proc(gpointer data);
static void destroy_device_instance(struct sr_dev_inst *dev);
static void close_device_instance(struct sr_dev_inst *dev);
static int open_device_instance(struct sr_dev_inst *dev);
static gpointer collect_run_proc(gpointer data);
static void post_event_async(int event);
static void send_event(int event);
static void make_demo_device_to_list();
static void process_attach_event(int isEvent);
static struct libusb_device* get_new_attached_usb_device();
static struct libusb_device* get_new_detached_usb_device();
extern SR_PRIV int sr_dslogic_option_value_to_code2(const struct sr_dev_inst *sdi, int config_id, const char *value);
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,
.actived_device_instance = NULL,
.data_forward_callback = NULL,
.collect_thread = NULL,
.callback_thread_count = 0,
.is_delay_destory_actived_device = 0,
.is_stop_by_detached = 0,
.transaction_id = 0,
.transaction_command = DEV_TRANS_NONE,
.last_error = SR_OK,
};
/**
* Must call first
*/
SR_API int ds_lib_init()
{
int ret = 0;
struct sr_dev_driver **drivers = NULL;
struct sr_dev_driver **dr = NULL;
if (lib_ctx.sr_ctx != NULL)
{
return SR_ERR_HAVE_DONE;
}
sr_log_init(); // try init log
sr_info("Init %s.", SR_LIB_NAME);
ret = sr_init(&lib_ctx.sr_ctx);
if (ret != SR_OK)
{
return ret;
}
lib_ctx.lib_exit_flag = 0;
// Init trigger.
ds_trigger_init();
// Initialise all libsigrok drivers
drivers = sr_driver_list();
for (dr = drivers; *dr; dr++)
{
if (sr_driver_init(lib_ctx.sr_ctx, *dr) != SR_OK)
{
sr_err("Failed to initialize driver '%s'", (*dr)->name);
return SR_ERR;
}
}
pthread_mutex_init(&lib_ctx.mutext, NULL); // init locker
make_demo_device_to_list();
lib_ctx.sr_ctx->hotplug_tv.tv_sec = 0;
lib_ctx.sr_ctx->hotplug_tv.tv_usec = 0;
/**
* User can use custom hotplug function.
*/
lib_ctx.sr_ctx->listen_hotplug_ext = NULL;
lib_ctx.sr_ctx->close_hotplug_ext = NULL;
lib_ctx.sr_ctx->hotplug_wait_timout_ext = NULL;
#ifdef _WIN32
#ifdef HAVE_EXTERN_INIT
lib_extern_init(lib_ctx.sr_ctx);
#endif
#endif
// Scan the all hardware device.
sr_info("Scan all connected hardware device.");
process_attach_event(0);
sr_listen_hotplug(lib_ctx.sr_ctx, hotplug_event_listen_callback);
/** Start usb hotplug thread */
lib_ctx.hotplug_thread = g_thread_new("hotplug_proc", usb_hotplug_process_proc, (gpointer)0);
return SR_OK;
}
#ifndef _WIN32
SR_PRIV int lib_extern_init(struct sr_context *ctx){
(void)ctx;
return SR_OK;
}
#endif
/**
* Free all resource before program exits
*/
SR_API int ds_lib_exit()
{
GSList *l;
if (lib_ctx.sr_ctx == NULL)
{
return SR_ERR_HAVE_DONE;
}
sr_info("Uninit %s.", SR_LIB_NAME);
ds_release_actived_device();
sr_close_hotplug(lib_ctx.sr_ctx);
lib_ctx.lib_exit_flag = 1; // all thread to exit
if (lib_ctx.hotplug_thread != NULL)
{
g_thread_join(lib_ctx.hotplug_thread);
lib_ctx.hotplug_thread = NULL;
}
// The device is not in list.
if (lib_ctx.is_delay_destory_actived_device && lib_ctx.actived_device_instance != NULL)
{
sr_info("The current device is delayed for destruction, handle:%p", lib_ctx.actived_device_instance->handle);
destroy_device_instance(lib_ctx.actived_device_instance);
lib_ctx.actived_device_instance = NULL;
lib_ctx.is_delay_destory_actived_device = 0;
}
// Release all device
for (l = lib_ctx.device_list; l; l = l->next)
{
destroy_device_instance((struct sr_dev_inst *)l->data);
}
g_safe_free_list(lib_ctx.device_list);
pthread_mutex_destroy(&lib_ctx.mutext); // uninit locker
// Uninit trigger.
ds_trigger_destroy();
if (sr_exit(lib_ctx.sr_ctx) != SR_OK)
{
sr_err("call sr_exit error");
}
lib_ctx.sr_ctx = NULL;
sr_hw_cleanup_all();
sr_log_uninit(); // try uninit log
return SR_OK;
}
/**
* Set the firmware binary file directory
*/
SR_API void ds_set_firmware_resource_dir(const char *dir)
{
memset(DS_RES_PATH, 0, sizeof(DS_RES_PATH));
if (dir)
strcpy(DS_RES_PATH, dir);
}
/**
* Set user data directory.
*/
SR_API void ds_set_user_data_dir(const char *dir)
{
memset(DS_USR_PATH, 0, sizeof(DS_USR_PATH));
if (dir)
strcpy(DS_USR_PATH, dir);
}
/**
* Set event callback, event type see enum libsigrok_event_type
*/
SR_API void ds_set_event_callback(dslib_event_callback_t cb)
{
lib_ctx.event_callback = cb;
}
/**
* Set the data receive callback.
*/
SR_API void ds_set_datafeed_callback(ds_datafeed_callback_t cb)
{
lib_ctx.data_forward_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 ds_get_device_list(struct ds_device_base_info **out_list, int *out_count)
{
int num;
struct ds_device_base_info *p = NULL;
GSList *l;
struct sr_dev_inst *dev;
void *buf = NULL;
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;
}
buf = malloc(sizeof(struct ds_device_base_info) * (num + 1));
if (buf == NULL)
{
sr_err("%s,ERROR:failed to alloc memory.", __func__);
pthread_mutex_unlock(&lib_ctx.mutext);
return SR_ERR_MALLOC;
}
p = (struct ds_device_base_info*)buf;
for (l = lib_ctx.device_list; l; l = l->next)
{
dev = l->data;
p->handle = dev->handle;
strncpy(p->name, (const char*)dev->name, sizeof(p->name) - 1);
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 = (struct ds_device_base_info*)buf;
return SR_OK;
}
/**
* Active a device.
*/
SR_API int ds_active_device(ds_device_handle handle)
{
GSList *l;
struct sr_dev_inst *dev;
int bFind = 0;
int ret;
struct sr_dev_inst *old_dev;
lib_ctx.last_error = SR_OK;
if (handle == NULL_HANDLE)
{
return SR_ERR_ARG;
}
ret = SR_OK;
old_dev = NULL;
sr_info("Start activating device.");
if (ds_is_collecting())
{
sr_err("Error!the current device is collecting,can not switch to it.");
return SR_ERR_CALL_STATUS;
}
pthread_mutex_lock(&lib_ctx.mutext);
if (lib_ctx.actived_device_instance != NULL)
{
if (lib_ctx.is_delay_destory_actived_device)
{
sr_info("The current device is delayed for destruction, name:\"%s\", handle:%p",
lib_ctx.actived_device_instance->name,
lib_ctx.actived_device_instance->handle);
destroy_device_instance(lib_ctx.actived_device_instance);
lib_ctx.actived_device_instance = NULL;
lib_ctx.is_delay_destory_actived_device = 0;
}
else
{
sr_info("Close the previous device, name:\"%s\"",
lib_ctx.actived_device_instance->name);
close_device_instance(lib_ctx.actived_device_instance);
old_dev = lib_ctx.actived_device_instance;
}
}
// 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("Please call ds_set_firmware_resource_dir() to set the firmware resource path.");
}
if (dev->dev_type == DEV_TYPE_FILELOG)
sr_info("Activating virtual device name: \"%s\".", dev->name);
else
sr_info("Activating device name: \"%s\".", dev->name);
ret = open_device_instance(dev);
if (ret == SR_OK)
{
lib_ctx.actived_device_instance = dev;
dev->actived_times++;
}
else
{
// Failed to switch new device.
if(lib_ctx.device_list != NULL && old_dev == NULL){
old_dev = lib_ctx.device_list->data;
}
if (old_dev != NULL){
if (old_dev->dev_type == DEV_TYPE_USB){
sr_err("Open device error! Will switch to \"%s\", handle:%p.",
old_dev->name,
old_dev->handle);
}
else{
sr_err("Open device error! Will switch to \"%s\".",
old_dev->name);
}
lib_ctx.actived_device_instance = old_dev;
ret = open_device_instance(old_dev);
}
}
break;
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
sr_info("Activating device end.");
if (!bFind)
{
sr_err("Active device error, can't find the device.");
return SR_ERR_CALL_STATUS;
}
return ret;
}
/**
* Active a device
* if @index is -1, will select the last one.
*/
SR_API int ds_active_device_by_index(int index)
{
GSList *l;
struct sr_dev_inst *dev;
ds_device_handle handle = NULL_HANDLE;
ds_device_handle lst_handle = NULL_HANDLE;
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_HANDLE)
{
sr_err("ds_active_device_by_index(), index is error!");
return SR_ERR_CALL_STATUS;
}
return ds_active_device(handle);
}
/**
* Get the selected device index.
*/
SR_API int ds_get_actived_device_index()
{
int dex = -1;
GSList *l = NULL;
int i = 0;
(void)l;
if (lib_ctx.actived_device_instance == NULL)
{
return -1;
}
pthread_mutex_lock(&lib_ctx.mutext);
for (l = lib_ctx.device_list; l; l = l->next)
{
if ((struct sr_dev_inst *)l->data == lib_ctx.actived_device_instance)
{
dex = i;
break;
}
i++;
}
pthread_mutex_unlock(&lib_ctx.mutext);
return dex;
}
/**
* Detect whether the active device exists
*/
SR_API int ds_have_actived_device()
{
if (lib_ctx.actived_device_instance != NULL)
{
return 1;
}
return 0;
}
/**
* Create a device from session file, and append to the list.
*/
SR_API int ds_device_from_file(const char *file_path)
{
struct sr_dev_inst *dev;
int ret;
if (file_path == NULL || *file_path == '\0'){
sr_err("Error!File name is empty.");
return SR_ERR_ARG;
}
dev = NULL;
ret = sr_new_virtual_device(file_path, &dev);
if (ret != SR_OK){
return ret;
}
pthread_mutex_lock(&lib_ctx.mutext);
lib_ctx.device_list = g_slist_append(lib_ctx.device_list, dev);
pthread_mutex_unlock(&lib_ctx.mutext);
return SR_OK;
}
/**
* Get the decive supports work mode, mode list: LOGIC、ANALOG、DSO
* return type see struct sr_dev_mode.
*/
SR_API const GSList *ds_get_actived_device_mode_list()
{
struct sr_dev_inst *dev;
dev = lib_ctx.actived_device_instance;
if (dev == NULL)
{
sr_err("Have no actived device.");
}
if (dev->driver == NULL || dev->driver->dev_mode_list == NULL)
{
sr_err("Module not implemented.");
return NULL;
}
return dev->driver->dev_mode_list(dev);
}
/**
* Remove one device from the list, and destory it.
* User need to call ds_get_device_list() to get the new list.
*/
SR_API int ds_remove_device(ds_device_handle handle)
{
GSList *l;
struct sr_dev_inst *dev;
int bFind = 0;
if (handle == NULL_HANDLE)
return SR_ERR_ARG;
if (lib_ctx.actived_device_instance != NULL && lib_ctx.actived_device_instance->handle == handle && ds_is_collecting())
{
sr_err("Device is collecting, can't remove it.");
return SR_ERR_CALL_STATUS;
}
if (lib_ctx.actived_device_instance != NULL && lib_ctx.is_delay_destory_actived_device && lib_ctx.actived_device_instance->handle == handle)
{
sr_info("The current device is delayed for destruction, handle:%p", lib_ctx.actived_device_instance->handle);
destroy_device_instance(lib_ctx.actived_device_instance);
lib_ctx.actived_device_instance = NULL;
lib_ctx.is_delay_destory_actived_device = 0;
}
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);
if (dev == lib_ctx.actived_device_instance)
{
lib_ctx.actived_device_instance = NULL;
}
destroy_device_instance(dev);
bFind = 1;
break;
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
if (bFind == 0)
return SR_ERR_CALL_STATUS;
return SR_OK;
}
/**
* Get the actived device info.
* If the actived device is not exists, the handle filed will be set null.
*/
SR_API int ds_get_actived_device_info(struct ds_device_full_info *fill_info)
{
struct sr_dev_inst *dev;
struct ds_device_full_info *p;
int ret;
if (fill_info == NULL)
return SR_ERR_ARG;
ret = SR_ERR_CALL_STATUS;
p = fill_info;
p->handle = NULL_HANDLE;
p->name[0] = '\0';
p->path[0] = '\0';
p->driver_name[0] = '\0';
p->dev_type = DEV_TYPE_UNKOWN;
p->di = NULL;
p->actived_times = 0;
pthread_mutex_lock(&lib_ctx.mutext);
dev = lib_ctx.actived_device_instance;
if (dev != NULL)
{
p->handle = dev->handle;
p->dev_type = dev->dev_type;
p->di = dev;
p->actived_times = dev->actived_times;
strncpy(p->name, (const char*)dev->name, sizeof(p->name) - 1);
if (dev->driver && dev->driver->name)
{
strncpy(p->driver_name, dev->driver->name, sizeof(p->driver_name) - 1);
}
if ((dev->dev_type == DEV_TYPE_FILELOG || dev->dev_type == DEV_TYPE_DEMO) && dev->path != NULL){
strncpy(p->path, dev->path, sizeof(p->path) - 1);
}
ret = SR_OK;
}
pthread_mutex_unlock(&lib_ctx.mutext);
return ret;
}
/**
* Get actived device work model. mode list:LOGIC、ANALOG、DSO
*/
SR_API int ds_get_actived_device_mode()
{
int mode = UNKNOWN_DSL_MODE;
pthread_mutex_lock(&lib_ctx.mutext);
if (lib_ctx.actived_device_instance != NULL)
{
mode = lib_ctx.actived_device_instance->mode;
}
pthread_mutex_unlock(&lib_ctx.mutext);
return mode;
}
/**
* Start collect data
*/
SR_API int ds_start_collect()
{
int ret;
struct sr_dev_inst *di;
di = lib_ctx.actived_device_instance;
lib_ctx.last_error = SR_OK;
sr_info("Start collect.");
if (ds_is_collecting())
{
sr_err("Error,it's collecting!");
return SR_ERR_CALL_STATUS;
}
if (di == NULL)
{
sr_err("Please set a current device first.");
return SR_ERR_CALL_STATUS;
}
if (di->status == SR_ST_INITIALIZING)
{
sr_err("Error!The device is initializing.");
return SR_ERR_CALL_STATUS;
}
if (ds_channel_is_enabled() == 0)
{
sr_err("There have no useable channel, unable to collect.");
return SR_ERR_CALL_STATUS;
}
if (lib_ctx.data_forward_callback == NULL)
{
sr_err("Error! Data forwarding callback is not set, see \"ds_set_datafeed_callback()\".");
return SR_ERR_CALL_STATUS;
}
// Create new session.
sr_session_new();
if (di->status != SR_ST_ACTIVE)
{
ret = open_device_instance(di); // open device
if (ret != SR_OK)
{
sr_err("Open device error!");
return ret;
}
}
lib_ctx.collect_thread = g_thread_new("collect_proc", collect_run_proc, (gpointer)0);
return SR_OK;
}
static gpointer collect_run_proc(gpointer data)
{
(void)data;
int ret;
struct sr_dev_inst *di;
int bError;
bError = 0;
di = lib_ctx.actived_device_instance;
send_event(DS_EV_COLLECT_TASK_START);
sr_info("Collect thread start.");
if (di == NULL || di->driver == NULL || di->driver->dev_acquisition_start == NULL)
{
sr_err("The device cannot be used.");
bError = 1;
goto END;
}
ret = di->driver->dev_acquisition_start(di, (void *)di);
if (ret != SR_OK)
{
sr_err("Failed to start acquisition of device in "
"running session: %d",
ret);
bError = 1;
goto END;
}
send_event(DS_EV_DEVICE_RUNNING);
ret = sr_session_run();
send_event(DS_EV_DEVICE_STOPPED);
if (ret != SR_OK)
{
sr_err("Run session error!");
bError = 1;
goto END;
}
END:
sr_info("Collect thread end.");
lib_ctx.collect_thread = NULL;
if (bError)
send_event(DS_EV_COLLECT_TASK_END_BY_ERROR);
else if (lib_ctx.is_stop_by_detached)
post_event_async(DS_EV_COLLECT_TASK_END_BY_DETACHED);
else
send_event(DS_EV_COLLECT_TASK_END); // Normal end.
lib_ctx.is_stop_by_detached = 0;
return NULL;
}
/**
* Stop collect data, but not close the device.
*/
SR_API int ds_stop_collect()
{
sr_info("Stop collect.");
if (!ds_is_collecting())
{
sr_err("It's not collecting now.");
return SR_ERR_CALL_STATUS;
}
// Stop current session.
sr_session_stop();
// Wait the collect thread ends.
if (lib_ctx.collect_thread != NULL)
g_thread_join(lib_ctx.collect_thread);
lib_ctx.collect_thread = NULL;
return SR_OK;
}
/**
* Check if the device is collecting.
*/
SR_API int ds_is_collecting()
{
if (lib_ctx.collect_thread != NULL)
{
return 1;
}
return 0;
}
SR_API int ds_release_actived_device()
{
if (lib_ctx.actived_device_instance == NULL){
return SR_ERR_CALL_STATUS;
}
if (ds_is_collecting()){
ds_stop_collect();
}
if (lib_ctx.actived_device_instance->dev_type == DEV_TYPE_USB)
{
sr_info("Release current actived device. name:\"%s\", handle:%p",
lib_ctx.actived_device_instance->name,
lib_ctx.actived_device_instance->handle);
}
else
{
sr_info("Release current actived device. name:\"%s\"",
lib_ctx.actived_device_instance->name);
}
close_device_instance(lib_ctx.actived_device_instance);
// Destroy current session.
sr_session_destroy();
return SR_OK;
}
int ds_trigger_is_enabled()
{
GSList *l;
struct sr_channel *p;
int ret;
ret = 0;
pthread_mutex_lock(&lib_ctx.mutext);
if (lib_ctx.actived_device_instance != NULL)
{
for (l = lib_ctx.actived_device_instance->channels; l; l = l->next)
{
p = (struct sr_channel *)l->data;
if (p->trigger && p->trigger[0] != '\0')
{
ret = 1;
break;
}
}
}
pthread_mutex_unlock(&lib_ctx.mutext);
return ret;
}
SR_API int ds_trigger_reset()
{
return ds_trigger_init();
}
/**-------------------public end ---------------*/
/**-------------------config -------------------*/
SR_API int ds_get_actived_device_config(const struct sr_channel *ch,
const struct sr_channel_group *cg,
int key, GVariant **data)
{
if (lib_ctx.actived_device_instance == NULL)
{
sr_err("Have no actived device.");
return SR_ERR_CALL_STATUS;
}
return sr_config_get(lib_ctx.actived_device_instance->driver,
lib_ctx.actived_device_instance,
ch,
cg,
key,
data);
}
SR_API int ds_set_actived_device_config(const struct sr_channel *ch,
const struct sr_channel_group *cg,
int key, GVariant *data)
{
if (lib_ctx.actived_device_instance == NULL)
{
sr_err("Have no actived device.");
return SR_ERR_CALL_STATUS;
}
return sr_config_set(
lib_ctx.actived_device_instance,
(struct sr_channel*)ch,
(struct sr_channel_group*)cg,
key,
data);
}
SR_API int ds_get_actived_device_config_list(const struct sr_channel_group *cg,
int key, GVariant **data)
{
if (lib_ctx.actived_device_instance == NULL)
{
sr_err("Have no actived device.");
return SR_ERR_CALL_STATUS;
}
return sr_config_list(lib_ctx.actived_device_instance->driver,
lib_ctx.actived_device_instance,
cg,
key,
data);
}
SR_API const struct sr_config_info *ds_get_actived_device_config_info(int key)
{
if (lib_ctx.actived_device_instance == NULL)
{
sr_err("Have no actived device.");
return NULL;
}
return sr_config_info_get(key);
}
SR_API int ds_get_actived_device_status(struct sr_status *status, gboolean prg)
{
if (lib_ctx.actived_device_instance == NULL)