This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
/
aws_wifi_connect_task.c
188 lines (159 loc) · 5.6 KB
/
aws_wifi_connect_task.c
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
/*
* FreeRTOS V202007.00
* Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
#include "iot_config.h"
/**
* @brief The demo task is used to connect to the list of provisioned WIFI networks in priority order.
* The task also handles WIFI disconnection and reconnect to the provisioned APs upto a configured number of retries.
*/
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "aws_demo_config.h"
#include "aws_wifi_connect_task.h"
#include "iot_network_manager_private.h"
#include "iot_ble_wifi_provisioning.h"
#define wifiConnectTASK_NAME "WiFiConnectTask"
/**
* @brief Delay in milliseconds between connecting to the provisioned WiFi networks.
*/
#define wifiConnectDELAY_SECONDS ( 1 )
/**
* @brief Task for WiFi Reconnection. The task loops through the provisioned wifi networks in priority order
* until retry limit is reached or a network is connected. Task gets triggered on every WiFi disconnection.
*/
static void prvWiFiConnectTask( void * pvParams );
/**
* @brief WIFI network state change callback.
* The callback is used to trigger reconnection to the provisioned WiFi networks on a network disconnect.
*/
static void prvWiFiNetworkStateChangeCallback( uint32_t ulNetworkType,
AwsIotNetworkState_t xNetworkState,
void * pvContext );
/**
* @brief Semaphore used to trigger reconnection to WiFi networks.
*/
static SemaphoreHandle_t xWiFiConnectLock = NULL;
static TaskHandle_t xWifiConnectionTask = NULL;
static IotNetworkManagerSubscription_t xHandle = IOT_NETWORK_MANAGER_SUBSCRIPTION_INITIALIZER;
static void prvWiFiNetworkStateChangeCallback( uint32_t ulNetworkType,
AwsIotNetworkState_t xNetworkState,
void * pvContext )
{
if( xNetworkState == eNetworkStateDisabled )
{
xSemaphoreGive( xWiFiConnectLock );
}
}
void prvWiFiConnectTask( void * pvParams )
{
uint16_t ulNumNetworks, ulNetworkIndex;
TickType_t xWifiConnectionDelay = pdMS_TO_TICKS( wifiConnectDELAY_SECONDS * 1000 );
BaseType_t xWiFiConnected;
for( ; ; )
{
if( xSemaphoreTake( xWiFiConnectLock, portMAX_DELAY ) == pdTRUE )
{
xWiFiConnected = pdFALSE;
while( xWiFiConnected == pdFALSE )
{
ulNumNetworks = IotBleWifiProv_GetNumNetworks();
if( ulNumNetworks > 0 )
{
for( ulNetworkIndex = 0; ulNetworkIndex < ulNumNetworks; ulNetworkIndex++ )
{
if( WIFI_IsConnected() == pdFALSE )
{
xWiFiConnected = IotBleWifiProv_Connect( ulNetworkIndex );
}
else
{
xWiFiConnected = pdTRUE;
}
if( xWiFiConnected == pdTRUE )
{
break;
}
vTaskDelay( xWifiConnectionDelay );
}
}
else
{
break;
}
}
}
}
vTaskDelete( NULL );
}
BaseType_t xWiFiConnectTaskInitialize( void )
{
BaseType_t xRet = pdTRUE;
xWiFiConnectLock = xSemaphoreCreateBinary();
if( xWiFiConnectLock != NULL )
{
xRet = xSemaphoreGive( xWiFiConnectLock );
}
else
{
xRet = pdFALSE;
}
if( xRet == pdTRUE )
{
xRet = AwsIotNetworkManager_SubscribeForStateChange(
AWSIOT_NETWORK_TYPE_WIFI,
prvWiFiNetworkStateChangeCallback,
NULL,
&xHandle );
}
if( xRet == pdTRUE )
{
xRet = xTaskCreate(
prvWiFiConnectTask,
wifiConnectTASK_NAME,
democonfigDEMO_STACKSIZE,
NULL,
democonfigDEMO_PRIORITY,
&xWifiConnectionTask );
}
return xRet;
}
void vWiFiConnectTaskDestroy( void )
{
if( xHandle != NULL )
{
AwsIotNetworkManager_RemoveSubscription( xHandle );
xHandle = NULL;
}
if( xWifiConnectionTask != NULL )
{
vTaskDelete( xWifiConnectionTask );
xWifiConnectionTask = NULL;
}
if( xWiFiConnectLock != NULL )
{
vSemaphoreDelete( xWiFiConnectLock );
xWiFiConnectLock = NULL;
}
}