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
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2025-present Eclipse ThreadX Contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
#include <stdio.h>
#include "nx_azure_iot_hub_client.h"
#ifndef SAMPLE_MAX_EXPONENTIAL_BACKOFF_IN_SEC
#define SAMPLE_MAX_EXPONENTIAL_BACKOFF_IN_SEC (10 * 60)
#endif /* SAMPLE_MAX_EXPONENTIAL_BACKOFF_IN_SEC */
#ifndef SAMPLE_INITIAL_EXPONENTIAL_BACKOFF_IN_SEC
#define SAMPLE_INITIAL_EXPONENTIAL_BACKOFF_IN_SEC (3)
#endif /* SAMPLE_INITIAL_EXPONENTIAL_BACKOFF_IN_SEC */
#ifndef SAMPLE_MAX_EXPONENTIAL_BACKOFF_JITTER_PERCENT
#define SAMPLE_MAX_EXPONENTIAL_BACKOFF_JITTER_PERCENT (60)
#endif /* SAMPLE_MAX_EXPONENTIAL_BACKOFF_JITTER_PERCENT */
VOID sample_connection_monitor(NX_IP *ip_ptr, NX_AZURE_IOT_HUB_CLIENT *iothub_client_ptr, UINT sample_status,
UINT (*iothub_init)(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr));
static UINT exponential_retry_count;
static UINT iothub_init_count = 0;
static UINT exponential_backoff_with_jitter()
{
double jitter_percent = (SAMPLE_MAX_EXPONENTIAL_BACKOFF_JITTER_PERCENT / 100.0) * (rand() / ((double)RAND_MAX));
UINT base_delay = SAMPLE_MAX_EXPONENTIAL_BACKOFF_IN_SEC;
uint64_t delay;
if (exponential_retry_count < (sizeof(UINT) * 8))
{
delay = (uint64_t)((1 << exponential_retry_count) * SAMPLE_INITIAL_EXPONENTIAL_BACKOFF_IN_SEC);
if (delay <= (UINT)(-1))
{
base_delay = (UINT)delay;
}
}
if (base_delay > SAMPLE_MAX_EXPONENTIAL_BACKOFF_IN_SEC)
{
base_delay = SAMPLE_MAX_EXPONENTIAL_BACKOFF_IN_SEC;
}
else
{
exponential_retry_count++;
}
return((UINT)(base_delay * (1 + jitter_percent)) * NX_IP_PERIODIC_RATE) ;
}
static VOID exponential_backoff_reset()
{
exponential_retry_count = 0;
}
/**
*
* Connection state
*
*
* +--------------+ +--------------+ +--------------+
* | | INIT | | NETWORK | |
* | | SUCCESS | | GOOD | |
* | INIT | | NETWORK | | CONNECT |
* | +-------------> CHECK +---------------------> +---------+
* | | | | | | |
* | | | | | | |
* +------^-------+ +------^-------+ +------+-------+ |
* | | | |
* | | CONNECT FAIL | |
* | | +--------------------------+ |
* | | | CONNECTED |
* | RECONNECT | | |
* | | | |
* | | +------v-------+ +--------------+ |
* | REINITIALIZE | | | | | |
* | +---+ | DISCONNECT | | |
* | | DISCONNECTED | | CONNECTED | |
* | | <--------------+ <------+
* +--------------------------------+ | | |
* | | | |
* +--------------+ +---^------+---+
* | |(TELEMETRY |
* | | C2D |
* | | COMMAND | METHOD |
* +------+ PROPERTIES | DEVICE TWIN)
*
*
*
*/
VOID sample_connection_monitor(NX_IP *ip_ptr, NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr, UINT connection_status,
UINT (*iothub_init)(NX_AZURE_IOT_HUB_CLIENT *hub_client_ptr))
{
UINT loop = NX_TRUE;
ULONG gateway_address;
/* Check parameters. */
if ((ip_ptr == NX_NULL) || (hub_client_ptr == NX_NULL) || (iothub_init == NX_NULL))
{
return;
}
/* Check if connected. */
if (connection_status == NX_SUCCESS)
{
/* Reset the exponential. */
exponential_backoff_reset();
}
else
{
/* Disconnect. */
if (connection_status != NX_AZURE_IOT_NOT_INITIALIZED)
{
nx_azure_iot_hub_client_disconnect(hub_client_ptr);
}
/* Recover. */
while (loop)
{
switch (connection_status)
{
/* Something bad has happened with client state, we need to re-initialize it. */
case NX_DNS_QUERY_FAILED :
case NXD_MQTT_ERROR_BAD_USERNAME_PASSWORD :
case NXD_MQTT_ERROR_NOT_AUTHORIZED :
{
/* Deinitialize iot hub client. */
nx_azure_iot_hub_client_deinitialize(hub_client_ptr);
}
/* fallthrough */
case NX_AZURE_IOT_NOT_INITIALIZED:
{
if (iothub_init_count++)
{
printf("Re-initializing iothub connection, after backoff\r\n");
tx_thread_sleep(exponential_backoff_with_jitter());
}
/* Initialize iot hub. */
if (iothub_init(hub_client_ptr))
{
connection_status = NX_AZURE_IOT_NOT_INITIALIZED;
}
else
{
/* Wait for network. */
while (nx_ip_gateway_address_get(ip_ptr, &gateway_address))
{
tx_thread_sleep(NX_IP_PERIODIC_RATE);
}
/* Connect to iot hub. */
connection_status = nx_azure_iot_hub_client_connect(hub_client_ptr, NX_FALSE, NX_WAIT_FOREVER);
}
}
break;
case NX_AZURE_IOT_SAS_TOKEN_EXPIRED:
{
printf("SAS token expired\r\n");
}
/* fallthrough */
default :
{
printf("Reconnecting iothub, after backoff\r\n");
tx_thread_sleep(exponential_backoff_with_jitter());
/* Wait for network. */
while (nx_ip_gateway_address_get(ip_ptr, &gateway_address))
{
tx_thread_sleep(NX_IP_PERIODIC_RATE);
}
connection_status = nx_azure_iot_hub_client_connect(hub_client_ptr, NX_FALSE, NX_WAIT_FOREVER);
}
break;
}
/* Check status. */
if (connection_status == NX_SUCCESS)
{
return;
}
}
}
}
|