Skip to content

Commit 5bc4b03

Browse files
authored
update networking to support more than 3 DNS entry returns (#335)
* update networking to support more than 3 DNS entry returns * coding style fix
1 parent f56af27 commit 5bc4b03

File tree

7 files changed

+85
-53
lines changed

7 files changed

+85
-53
lines changed

MXChip/AZ3166/app/wwd_networking.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define NETX_TX_POOL_SIZE ((NETX_PACKET_SIZE + sizeof(NX_PACKET)) * NETX_TX_PACKET_COUNT)
2020
#define NETX_RX_POOL_SIZE ((NETX_PACKET_SIZE + sizeof(NX_PACKET)) * NETX_RX_PACKET_COUNT)
2121
#define NETX_ARP_CACHE_SIZE 512
22+
#define NETX_DNS_COUNT 6
2223

2324
#define NETX_IPV4_ADDRESS IP_ADDRESS(0, 0, 0, 0)
2425
#define NETX_IPV4_MASK IP_ADDRESS(255, 255, 255, 0)
@@ -128,8 +129,8 @@ static UINT dhcp_connect(void)
128129
static UINT dns_connect()
129130
{
130131
UINT status;
131-
ULONG dns_server_address[3] = {0};
132-
UINT dns_server_address_size = 12;
132+
ULONG dns_server_address[NETX_DNS_COUNT] = {0};
133+
UINT dns_server_address_size = sizeof(UINT) * NETX_DNS_COUNT;
133134

134135
printf("\r\nInitializing DNS client\r\n");
135136

@@ -141,21 +142,22 @@ static UINT dns_connect()
141142
return status;
142143
}
143144

144-
// Output DNS Server address
145-
print_address("DNS address 1", dns_server_address[0]);
146-
print_address("DNS address 2", dns_server_address[1]);
147-
148145
if ((status = nx_dns_server_remove_all(&nx_dns_client)))
149146
{
150147
printf("ERROR: nx_dns_server_remove_all (0x%08x)\r\n", status);
151148
return status;
152149
}
153150

154-
// Add an IPv4 server address to the Client list
155-
if ((status = nx_dns_server_add(&nx_dns_client, dns_server_address[0])))
151+
for (int i = 0; i < dns_server_address_size / sizeof(UINT); ++i)
156152
{
157-
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
158-
return status;
153+
print_address("DNS address", dns_server_address[i]);
154+
155+
// Add an IPv4 server address to the Client list
156+
if ((status = nx_dns_server_add(&nx_dns_client, dns_server_address[i])))
157+
{
158+
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
159+
return status;
160+
}
159161
}
160162

161163
printf("SUCCESS: DNS client initialized\r\n");
@@ -245,7 +247,6 @@ UINT wwd_network_init(CHAR* ssid, CHAR* password, WiFi_Mode mode)
245247
nx_packet_pool_delete(&nx_pool[0]);
246248
nx_packet_pool_delete(&nx_pool[1]);
247249
printf("ERROR: nx_tcp_enable (0x%08x)\r\n", status);
248-
return status;
249250
}
250251

251252
// Enable UDP traffic
@@ -312,6 +313,11 @@ UINT wwd_network_init(CHAR* ssid, CHAR* password, WiFi_Mode mode)
312313
else if ((status = sntp_init()))
313314
{
314315
printf("ERROR: Failed to init the SNTP client (0x%08x)\r\n", status);
316+
nx_dns_delete(&nx_dns_client);
317+
nx_dhcp_delete(&nx_dhcp_client);
318+
nx_ip_delete(&nx_ip);
319+
nx_packet_pool_delete(&nx_pool[0]);
320+
nx_packet_pool_delete(&nx_pool[1]);
315321
}
316322

317323
// Initialize TLS

Renesas/RX65N_Cloud_Kit/app/rx_networking.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@ UINT rx_network_init(CHAR* ssid, CHAR* password, WiFi_Mode mode)
234234
// Initialize the SNTP client
235235
else if ((status = sntp_init()))
236236
{
237+
nx_dns_delete(&nx_dns_client);
238+
nx_ip_delete(&nx_ip);
239+
nx_packet_pool_delete(&nx_pool);
237240
printf("ERROR: Failed to init the SNTP client (0x%08x)\r\n", status);
238241
}
239242

STMicroelectronics/B-L475E-IOT01A/app/stm_networking.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,25 @@ static UINT dns_connect()
163163
if ((status = nx_dns_server_remove_all(&nx_dns_client)))
164164
{
165165
printf("ERROR: nx_dns_server_remove_all (0x%08x)\r\n", status);
166-
return status;
167166
}
168167

169-
// Add an IPv4 server address to the Client list
170-
if ((status = nx_dns_server_add(
171-
&nx_dns_client, IP_ADDRESS(dns_address_1[0], dns_address_1[1], dns_address_1[2], dns_address_1[3]))))
168+
else if ((status = nx_dns_server_add(
169+
&nx_dns_client, IP_ADDRESS(dns_address_1[0], dns_address_1[1], dns_address_1[2], dns_address_1[3]))))
172170
{
173171
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
174-
return status;
175172
}
176173

177-
printf("SUCCESS: DNS client initialized\r\n");
174+
else if ((status = nx_dns_server_add(
175+
&nx_dns_client, IP_ADDRESS(dns_address_2[0], dns_address_2[1], dns_address_2[2], dns_address_2[3]))))
176+
{
177+
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
178+
}
179+
else
180+
{
181+
printf("SUCCESS: DNS client initialized\r\n");
182+
}
178183

179-
return NX_SUCCESS;
184+
return status;
180185
}
181186

182187
UINT stm_network_init(CHAR* ssid, CHAR* password, WiFi_Mode mode)
@@ -272,6 +277,9 @@ UINT stm_network_init(CHAR* ssid, CHAR* password, WiFi_Mode mode)
272277
// Initialize the SNTP client
273278
else if ((status = sntp_init()))
274279
{
280+
nx_dns_delete(&nx_dns_client);
281+
nx_ip_delete(&nx_ip);
282+
nx_packet_pool_delete(&nx_pool);
275283
printf("ERROR: Failed to init the SNTP client (0x%08x)\r\n", status);
276284
}
277285

STMicroelectronics/B-L4S5I-IOT01A/app/stm_networking.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,25 @@ static UINT dns_connect()
163163
if ((status = nx_dns_server_remove_all(&nx_dns_client)))
164164
{
165165
printf("ERROR: nx_dns_server_remove_all (0x%08x)\r\n", status);
166-
return status;
167166
}
168167

169-
// Add an IPv4 server address to the Client list
170-
if ((status = nx_dns_server_add(
171-
&nx_dns_client, IP_ADDRESS(dns_address_1[0], dns_address_1[1], dns_address_1[2], dns_address_1[3]))))
168+
else if ((status = nx_dns_server_add(
169+
&nx_dns_client, IP_ADDRESS(dns_address_1[0], dns_address_1[1], dns_address_1[2], dns_address_1[3]))))
172170
{
173171
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
174-
return status;
175172
}
176173

177-
printf("SUCCESS: DNS client initialized\r\n");
174+
else if ((status = nx_dns_server_add(
175+
&nx_dns_client, IP_ADDRESS(dns_address_2[0], dns_address_2[1], dns_address_2[2], dns_address_2[3]))))
176+
{
177+
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
178+
}
179+
else
180+
{
181+
printf("SUCCESS: DNS client initialized\r\n");
182+
}
178183

179-
return NX_SUCCESS;
184+
return status;
180185
}
181186

182187
UINT stm_network_init(CHAR* ssid, CHAR* password, WiFi_Mode mode)
@@ -272,6 +277,9 @@ UINT stm_network_init(CHAR* ssid, CHAR* password, WiFi_Mode mode)
272277
// Initialize the SNTP client
273278
else if ((status = sntp_init()))
274279
{
280+
nx_dns_delete(&nx_dns_client);
281+
nx_ip_delete(&nx_ip);
282+
nx_packet_pool_delete(&nx_pool);
275283
printf("ERROR: Failed to init the SNTP client (0x%08x)\r\n", status);
276284
}
277285

shared/src/azure_iot_connect.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ VOID connection_monitor(
143143
// Connect the network
144144
if (network_connect() != NX_SUCCESS)
145145
{
146-
// Failed, break out to try again next time
146+
// Failed, sleep and break out to try again next time
147+
tx_thread_sleep(5 * TX_TIMER_TICKS_PER_SECOND);
147148
break;
148149
}
149150

shared/src/azure_iot_nx_client.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
#define TELEMETRY_BUFFER_SIZE 256
3838
#define PROPERTIES_BUFFER_SIZE 128
3939

40-
// define static strings for content type and -encoding on message property bag
41-
static const UCHAR content_type_property[] = "$.ct";
40+
// define static strings for content type and -encoding on message property bag
41+
static const UCHAR content_type_property[] = "$.ct";
4242
static const UCHAR content_encoding_property[] = "$.ce";
43-
static const UCHAR content_type_json[] = "application%2Fjson";
44-
static const UCHAR content_encoding_utf8[] = "utf-8";
43+
static const UCHAR content_type_json[] = "application%2Fjson";
44+
static const UCHAR content_encoding_utf8[] = "utf-8";
4545

4646
static UCHAR telemetry_buffer[TELEMETRY_BUFFER_SIZE];
4747
static UCHAR properties_buffer[PROPERTIES_BUFFER_SIZE];
@@ -673,23 +673,27 @@ UINT azure_iot_nx_client_publish_telemetry(AZURE_IOT_NX_CONTEXT* context_ptr,
673673
}
674674

675675
// set the ContentType property on the message to "application/json" (url-encoded)
676-
status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr, content_type_property, sizeof( content_type_property )-1,
677-
content_type_json, sizeof( content_type_json )-1,
678-
NX_WAIT_FOREVER);
679-
if (status != NX_AZURE_IOT_SUCCESS)
680-
{
681-
printf("Error: cannot set ContentType message property (0x%08X)\r\n", status);
676+
if ((status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr,
677+
content_type_property,
678+
sizeof(content_type_property) - 1,
679+
content_type_json,
680+
sizeof(content_type_json) - 1,
681+
NX_WAIT_FOREVER)))
682+
{
683+
printf("Error: Cant set ContentType message property (0x%08X)\r\n", status);
682684
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
683685
return status;
684686
}
685-
687+
686688
// set the ContentEncoding property on the message to "utf-8"
687-
status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr, content_encoding_property, sizeof( content_encoding_property )-1,
688-
content_encoding_utf8, sizeof( content_encoding_utf8 )-1,
689-
NX_WAIT_FOREVER);
690-
if (status != NX_AZURE_IOT_SUCCESS)
691-
{
692-
printf("Error: Canot set ContentEncoding message property (0x%08X)\r\n", status);
689+
if ((status = nx_azure_iot_hub_client_telemetry_property_add(packet_ptr,
690+
content_encoding_property,
691+
sizeof(content_encoding_property) - 1,
692+
content_encoding_utf8,
693+
sizeof(content_encoding_utf8) - 1,
694+
NX_WAIT_FOREVER)))
695+
{
696+
printf("Error: Cant set ContentEncoding message property (0x%08X)\r\n", status);
693697
nx_azure_iot_hub_client_telemetry_message_delete(packet_ptr);
694698
return status;
695699
}

shared/src/networking.c

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define NETX_PACKET_SIZE 1536
1818
#define NETX_POOL_SIZE ((NETX_PACKET_SIZE + sizeof(NX_PACKET)) * NETX_PACKET_COUNT)
1919
#define NETX_ARP_CACHE_SIZE 512
20+
#define NETX_DNS_COUNT 6
2021

2122
#define NETX_IPV4_ADDRESS IP_ADDRESS(0, 0, 0, 0)
2223
#define NETX_IPV4_MASK IP_ADDRESS(255, 255, 255, 0)
@@ -100,8 +101,8 @@ static UINT dhcp_connect()
100101
static UINT dns_connect()
101102
{
102103
UINT status;
103-
ULONG dns_server_address[3] = {0};
104-
UINT dns_server_address_size = 12;
104+
ULONG dns_server_address[NETX_DNS_COUNT] = {0};
105+
UINT dns_server_address_size = sizeof(UINT) * NETX_DNS_COUNT;
105106

106107
printf("\r\nInitializing DNS client\r\n");
107108

@@ -113,21 +114,22 @@ static UINT dns_connect()
113114
return status;
114115
}
115116

116-
// Output DNS Server address
117-
print_address("DNS address 1", dns_server_address[0]);
118-
print_address("DNS address 2", dns_server_address[1]);
119-
120117
if ((status = nx_dns_server_remove_all(&nx_dns_client)))
121118
{
122119
printf("ERROR: nx_dns_server_remove_all (0x%08x)\r\n", status);
123120
return status;
124121
}
125122

126-
// Add an IPv4 server address to the Client list
127-
if ((status = nx_dns_server_add(&nx_dns_client, dns_server_address[0])))
123+
for (int i = 0; i < dns_server_address_size / sizeof(UINT); ++i)
128124
{
129-
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
130-
return status;
125+
print_address("DNS address", dns_server_address[i]);
126+
127+
// Add an IPv4 server address to the Client list
128+
if ((status = nx_dns_server_add(&nx_dns_client, dns_server_address[i])))
129+
{
130+
printf("ERROR: nx_dns_server_add (0x%08x)\r\n", status);
131+
return status;
132+
}
131133
}
132134

133135
printf("SUCCESS: DNS client initialized\r\n");

0 commit comments

Comments
 (0)