Skip to content

Commit 1afbb97

Browse files
author
szabi-luxonis
committed
implement watchdog in cpp backend
1 parent 5451496 commit 1afbb97

3 files changed

Lines changed: 89 additions & 21 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Microsoft Visual Studio Code
22
.project
33
.vscode/
4-
4+
build/*
55
# Auto-generated
66
shared/version.hpp

host/py_module/py_bindings.cpp

Lines changed: 82 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,72 @@
3535

3636
namespace 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)
40106
static 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

shared/xlink/xlink_wrapper.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
#include "xlink_wrapper.hpp"
1212

1313
// FIXME use some header
14-
#if !defined(__PC__)
1514
extern "C" void wdog_keepalive(void);
16-
#endif
15+
1716

1817
XLinkWrapper::XLinkWrapper(
1918
bool be_verbose
@@ -218,7 +217,7 @@ uint32_t XLinkWrapper::openReadAndCloseStream(
218217
memcpy(&stl_container[0], packet->data, packet->length);
219218

220219
result = packet->length;
221-
220+
wdog_keepalive();
222221
// release data
223222
status = XLinkReleaseData(stream_id);
224223
if (status != X_LINK_SUCCESS)
@@ -276,7 +275,7 @@ uint32_t XLinkWrapper::openReadAndCloseStream(
276275
uint32_t copy_sz = std::min(buffer_size, packet->length);
277276
memcpy(buffer, packet->data, copy_sz);
278277
result = copy_sz;
279-
278+
wdog_keepalive();
280279
// release data
281280
status = XLinkReleaseData(stream_id);
282281
if (status != X_LINK_SUCCESS)
@@ -511,9 +510,8 @@ bool XLinkWrapper::writeToStream(
511510
printf("!!! XLink write successful: %s (%d)\n", stream.name, int(write_data_size));
512511
#endif
513512

514-
#if !defined(__PC__)
515-
wdog_keepalive();
516-
#endif
513+
wdog_keepalive();
514+
517515
}
518516

519517
return status == X_LINK_SUCCESS;
@@ -615,7 +613,7 @@ void XLinkWrapper::openAndReadDataThreadFunc(
615613
// printf ("Stream id #%d | Name %10s | Packet size: %8u | No.: %4u\n",
616614
// stream_id, stream_info.name, packet->length, packet_counter);
617615
// }
618-
616+
wdog_keepalive();
619617
notifyObservers(stream_info, data);
620618

621619
packet_counter += 1;

0 commit comments

Comments
 (0)