forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformManagerImpl.cpp
180 lines (163 loc) · 6.67 KB
/
PlatformManagerImpl.cpp
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
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2018 Nest Labs, Inc.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* Provides an implementation of the PlatformManager object
* for the ESP32 platform.
*/
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <crypto/CHIPCryptoPAL.h>
#include <platform/ESP32/DiagnosticDataProviderImpl.h>
#include <platform/ESP32/ESP32Utils.h>
#include <platform/ESP32/SystemTimeSupport.h>
#include <platform/PlatformManager.h>
#include <platform/internal/GenericPlatformManagerImpl_FreeRTOS.ipp>
#include "esp_event.h"
#include "esp_heap_caps_init.h"
#include "esp_log.h"
#include "esp_netif.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
#include "spi_flash_mmap.h"
#else
#include "esp_spi_flash.h"
#endif
#include "esp_system.h"
#include "esp_wifi.h"
namespace chip {
namespace DeviceLayer {
namespace Internal {
extern CHIP_ERROR InitLwIPCoreLock(void);
}
PlatformManagerImpl PlatformManagerImpl::sInstance;
static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen)
{
esp_fill_random(output, len);
*olen = len;
return 0;
}
CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
{
// Arrange for CHIP-encapsulated ESP32 errors to be translated to text
Internal::ESP32Utils::RegisterESP32ErrorFormatter();
// Make sure the LwIP core lock has been initialized
ReturnErrorOnFailure(Internal::InitLwIPCoreLock());
// Arrange for the ESP event loop to deliver events into the CHIP Device layer.
esp_err_t err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
if (err != ESP_OK)
{
return Internal::ESP32Utils::MapError(err);
}
mStartTime = System::SystemClock().GetMonotonicTimestamp();
ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16));
// Call _InitChipStack() on the generic implementation base class
// to finish the initialization process.
ReturnErrorOnFailure(Internal::GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>::_InitChipStack());
ReturnErrorOnFailure(System::Clock::InitClock_RealTime());
return CHIP_NO_ERROR;
}
void PlatformManagerImpl::_Shutdown()
{
uint32_t totalOperationalHours = 0;
if (ConfigurationMgr().GetTotalOperationalHours(totalOperationalHours) == CHIP_NO_ERROR)
{
ConfigurationMgr().StoreTotalOperationalHours(totalOperationalHours);
}
else
{
ChipLogError(DeviceLayer, "Failed to get total operational hours of the Node");
}
Internal::GenericPlatformManagerImpl_FreeRTOS<PlatformManagerImpl>::_Shutdown();
esp_event_loop_delete_default();
}
void PlatformManagerImpl::HandleESPSystemEvent(void * arg, esp_event_base_t eventBase, int32_t eventId, void * eventData)
{
ChipDeviceEvent event;
memset(&event, 0, sizeof(event));
event.Type = DeviceEventType::kESPSystemEvent;
event.Platform.ESPSystemEvent.Base = eventBase;
event.Platform.ESPSystemEvent.Id = eventId;
if (eventBase == IP_EVENT)
{
switch (eventId)
{
case IP_EVENT_STA_GOT_IP:
memcpy(&event.Platform.ESPSystemEvent.Data.IpGotIp, eventData, sizeof(event.Platform.ESPSystemEvent.Data.IpGotIp));
break;
case IP_EVENT_GOT_IP6:
memcpy(&event.Platform.ESPSystemEvent.Data.IpGotIp6, eventData, sizeof(event.Platform.ESPSystemEvent.Data.IpGotIp6));
break;
case IP_EVENT_AP_STAIPASSIGNED:
memcpy(&event.Platform.ESPSystemEvent.Data.IpApStaIpAssigned, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.IpApStaIpAssigned));
break;
default:
break;
}
}
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
else if (eventBase == WIFI_EVENT)
{
switch (eventId)
{
case WIFI_EVENT_SCAN_DONE:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiStaScanDone, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiStaScanDone));
break;
case WIFI_EVENT_STA_CONNECTED:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiStaConnected, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiStaConnected));
break;
case WIFI_EVENT_STA_DISCONNECTED:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiStaDisconnected, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiStaDisconnected));
break;
case WIFI_EVENT_STA_AUTHMODE_CHANGE:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiStaAuthModeChange, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiStaAuthModeChange));
break;
case WIFI_EVENT_STA_WPS_ER_PIN:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiStaWpsErPin, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiStaWpsErPin));
break;
case WIFI_EVENT_STA_WPS_ER_FAILED:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiStaWpsErFailed, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiStaWpsErFailed));
break;
case WIFI_EVENT_AP_STACONNECTED:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiApStaConnected, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiApStaConnected));
break;
case WIFI_EVENT_AP_STADISCONNECTED:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiApStaDisconnected, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiApStaDisconnected));
break;
case WIFI_EVENT_AP_PROBEREQRECVED:
memcpy(&event.Platform.ESPSystemEvent.Data.WiFiApProbeReqRecved, eventData,
sizeof(event.Platform.ESPSystemEvent.Data.WiFiApProbeReqRecved));
break;
default:
break;
}
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
sInstance.PostEventOrDie(&event);
}
} // namespace DeviceLayer
} // namespace chip