Skip to content

Commit 19fd572

Browse files
committed
Reset device if it gets numerous connection errors
1 parent a8506d2 commit 19fd572

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

main.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
static MbedCloudClient *cloud_client;
3030
static bool cloud_client_running = true;
3131
static NetworkInterface *network = NULL;
32+
static int error_count = 0;
3233

3334
// Fake entropy needed for non-TRNG boards. Suitable only for demo devices.
3435
const uint8_t MBED_CLOUD_DEV_ENTROPY[] = { 0xf6, 0xd6, 0xc0, 0x09, 0x9e, 0x6e, 0xf2, 0x37, 0xdc, 0x29, 0x88, 0xf1, 0x57, 0x32, 0x7d, 0xde, 0xac, 0xb3, 0x99, 0x8c, 0xb9, 0x11, 0x35, 0x18, 0xeb, 0x48, 0x29, 0x03, 0x6a, 0x94, 0x6d, 0xe8, 0x40, 0xc0, 0x28, 0xcc, 0xe4, 0x04, 0xc3, 0x1f, 0x4b, 0xc2, 0xe0, 0x68, 0xa0, 0x93, 0xe6, 0x3a };
36+
const int MAX_ERROR_COUNT = 5;
3537

3638
static M2MResource* m2m_get_res;
3739
static M2MResource* m2m_put_res;
@@ -92,6 +94,14 @@ void client_registered(void)
9294
{
9395
printf("Client registered.\n");
9496
print_client_ids();
97+
error_count = 0;
98+
}
99+
100+
void client_registration_updated(void)
101+
{
102+
printf("Client registration updated.\n");
103+
print_client_ids();
104+
error_count = 0;
95105
}
96106

97107
void client_unregistered(void)
@@ -112,6 +122,15 @@ void factory_reset(void*)
112122
void client_error(int err)
113123
{
114124
printf("client_error(%d) -> %s\n", err, cloud_client->error_description());
125+
if (err == MbedCloudClient::ConnectNetworkError ||
126+
err == MbedCloudClient::ConnectDnsResolvingFailed ||
127+
err == MbedCloudClient::ConnectSecureConnectionFailed) {
128+
if(++error_count == MAX_ERROR_COUNT) {
129+
printf("Max error count %d reached, rebooting.\n\n", MAX_ERROR_COUNT);
130+
ThisThread::sleep_for(1*1000);
131+
NVIC_SystemReset();
132+
}
133+
}
115134
}
116135

117136
void update_progress(uint32_t progress, uint32_t total)
@@ -240,6 +259,8 @@ int main(void)
240259
cloud_client = new MbedCloudClient(client_registered, client_unregistered, client_error);
241260
#endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE
242261

262+
cloud_client->on_registration_updated(client_registration_updated);
263+
243264
cloud_client->add_objects(m2m_obj_list);
244265
cloud_client->setup(network);
245266

0 commit comments

Comments
 (0)