Skip to content

Commit 724816c

Browse files
authored
Merge pull request #126 from ARMmbed/reset_on_continous_error
Reset device if it gets numerous connection errors
2 parents a8506d2 + 899f72a commit 724816c

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

main.cpp

Lines changed: 20 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,13 @@ 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+
error_count = 0;
95104
}
96105

97106
void client_unregistered(void)
@@ -112,6 +121,15 @@ void factory_reset(void*)
112121
void client_error(int err)
113122
{
114123
printf("client_error(%d) -> %s\n", err, cloud_client->error_description());
124+
if (err == MbedCloudClient::ConnectNetworkError ||
125+
err == MbedCloudClient::ConnectDnsResolvingFailed ||
126+
err == MbedCloudClient::ConnectSecureConnectionFailed) {
127+
if(++error_count == MAX_ERROR_COUNT) {
128+
printf("Max error count %d reached, rebooting.\n\n", MAX_ERROR_COUNT);
129+
ThisThread::sleep_for(1*1000);
130+
NVIC_SystemReset();
131+
}
132+
}
115133
}
116134

117135
void update_progress(uint32_t progress, uint32_t total)
@@ -240,6 +258,8 @@ int main(void)
240258
cloud_client = new MbedCloudClient(client_registered, client_unregistered, client_error);
241259
#endif // MBED_CLOUD_CLIENT_SUPPORT_UPDATE
242260

261+
cloud_client->on_registration_updated(client_registration_updated);
262+
243263
cloud_client->add_objects(m2m_obj_list);
244264
cloud_client->setup(network);
245265

0 commit comments

Comments
 (0)