3535
3636namespace py = pybind11;
3737
38+ std::string config_backup;
39+ std::string cmd_backup;
40+ std::shared_ptr<CNNHostPipeline> gl_result = nullptr ;
41+
42+ static volatile std::atomic<int > wdog_keep;
43+
44+ bool deinit_device ();
45+ bool init_device (
46+ const std::string &device_cmd_file
47+ );
48+ std::shared_ptr<CNNHostPipeline> create_pipeline (
49+ const std::string &config_json_str
50+ );
51+
52+ static int wdog_thread_alive = 1 ;
53+ void wdog_thread (int & wd_timeout_ms)
54+ {
55+ std::cout << " watchdog started " << wd_timeout_ms << std::endl;
56+ while (wdog_thread_alive)
57+ {
58+ wdog_keep = 0 ;
59+ std::this_thread::sleep_for (std::chrono::milliseconds (wd_timeout_ms));
60+ if (wdog_keep == 0 && wdog_thread_alive == 1 )
61+ {
62+ std::cout << " watchdog triggered " << std::endl;
63+ deinit_device ();
64+ bool init = init_device (cmd_backup);
65+ if (!init)
66+ {
67+ exit (1 );
68+ }
69+ create_pipeline (config_backup);
70+ }
71+ }
72+
73+ }
74+
75+ static std::thread wd_thread;
76+ static int wd_timeout_ms = 1000 ;
77+ int wdog_start (void )
78+ {
79+ static int once = 1 ;
80+ if (once)
81+ {
82+ wdog_thread_alive = 1 ;
83+ wd_thread = std::thread (wdog_thread, std::ref (wd_timeout_ms));
84+ once = 0 ;
85+ }
86+ return 0 ;
87+ }
88+ int wdog_stop (void )
89+ {
90+ wdog_thread_alive = 0 ;
91+ wd_thread.join ();
92+
93+ return 0 ;
94+ }
95+
96+ // todo
97+ extern " C" {
98+ void wdog_keepalive (void )
99+ {
100+ wdog_keep = 1 ;
101+ }
102+
103+ };
38104
39105// TODO: REMOVE, IT'S TEMPORARY (for test only)
40106static XLinkGlobalHandler_t g_xlink_global_handler =
@@ -75,6 +141,7 @@ bool init_device(
75141 const std::string &device_cmd_file
76142)
77143{
144+ cmd_backup = device_cmd_file;
78145 bool result = false ;
79146 std::string error_msg;
80147
@@ -101,6 +168,7 @@ bool init_device(
101168 break ;
102169 }
103170
171+ wdog_start ();
104172
105173 // config_d2h
106174 {
@@ -236,11 +304,11 @@ std::unique_ptr<CNNHostPipeline> create_pipeline_TEST(
236304}
237305
238306
239- std::unique_ptr <CNNHostPipeline> create_pipeline (
307+ std::shared_ptr <CNNHostPipeline> create_pipeline (
240308 const std::string &config_json_str
241309)
242310{
243- std::unique_ptr<CNNHostPipeline> result ;
311+ config_backup = config_json_str ;
244312
245313 bool init_ok = false ;
246314 do
@@ -511,16 +579,17 @@ std::unique_ptr<CNNHostPipeline> create_pipeline(
511579
512580
513581 // pipeline
514- result = std::unique_ptr<CNNHostPipeline>(new CNNHostPipeline (tensors_info));
582+ if (gl_result == nullptr )
583+ gl_result = std::shared_ptr<CNNHostPipeline>(new CNNHostPipeline (tensors_info));
515584
516585 for (const std::string &stream_name : pipeline_device_streams)
517586 {
518587 std::cout << " Host stream start:" << stream_name << " \n " ;
519588
520589 if (g_xlink->openStreamInThreadAndNotifyObservers (c_streams_myriad_to_pc.at (stream_name)))
521590 {
522- result ->makeStreamPublic (stream_name);
523- result ->observe (*g_xlink.get (), c_streams_myriad_to_pc.at (stream_name));
591+ gl_result ->makeStreamPublic (stream_name);
592+ gl_result ->observe (*g_xlink.get (), c_streams_myriad_to_pc.at (stream_name));
524593 }
525594 else
526595 {
@@ -552,14 +621,14 @@ std::unique_ptr<CNNHostPipeline> create_pipeline(
552621
553622 if (add_disparity_post_processing_color)
554623 {
555- result ->makeStreamPublic (stream_out_color_name);
556- result ->observe (*g_disparity_post_proc.get (), c_streams_myriad_to_pc.at (stream_out_color_name));
624+ gl_result ->makeStreamPublic (stream_out_color_name);
625+ gl_result ->observe (*g_disparity_post_proc.get (), c_streams_myriad_to_pc.at (stream_out_color_name));
557626 }
558627
559628 if (add_disparity_post_processing_mm)
560629 {
561- result ->makeStreamPublic (stream_out_mm_name);
562- result ->observe (*g_disparity_post_proc.get (), c_streams_myriad_to_pc.at (stream_out_mm_name));
630+ gl_result ->makeStreamPublic (stream_out_mm_name);
631+ gl_result ->observe (*g_disparity_post_proc.get (), c_streams_myriad_to_pc.at (stream_out_mm_name));
563632 }
564633 }
565634 else
@@ -570,7 +639,7 @@ std::unique_ptr<CNNHostPipeline> create_pipeline(
570639 }
571640 }
572641
573- if (!result ->setHostCalcDepthConfigs (
642+ if (!gl_result ->setHostCalcDepthConfigs (
574643 config.depth .type ,
575644 config.depth .padding_factor ,
576645 config.board_config .left_fov_deg ,
@@ -589,10 +658,10 @@ std::unique_ptr<CNNHostPipeline> create_pipeline(
589658
590659 if (!init_ok)
591660 {
592- result = nullptr ;
661+ gl_result = nullptr ;
593662 }
594663
595- return result ;
664+ return gl_result ;
596665}
597666
598667
@@ -772,6 +841,7 @@ PYBIND11_MODULE(depthai, m)
772841
773842 // module destructor
774843 auto cleanup_callback = []() {
844+ wdog_stop ();
775845 deinit_device ();
776846 };
777847
0 commit comments