ConnectivityManagerImpl_WiFi.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*
  2. *
  3. * Copyright (c) 2020-2021 Project CHIP Authors
  4. * Copyright (c) 2018 Nest Labs, Inc.
  5. * All rights reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. /* this file behaves like a config.h, comes first */
  20. #include <platform/internal/CHIPDeviceLayerInternal.h>
  21. #include <platform/CommissionableDataProvider.h>
  22. #include <platform/ConnectivityManager.h>
  23. #include <lib/support/CodeUtils.h>
  24. #include <lib/support/logging/CHIPLogging.h>
  25. #include <platform/DeviceInstanceInfoProvider.h>
  26. #include <platform/DiagnosticDataProvider.h>
  27. #include <platform/ESP32/ESP32Utils.h>
  28. #include <platform/ESP32/NetworkCommissioningDriver.h>
  29. #include <platform/ESP32/route_hook/ESP32RouteHook.h>
  30. #include <platform/internal/BLEManager.h>
  31. #include "esp_event.h"
  32. #include "esp_netif.h"
  33. #include "esp_wifi.h"
  34. #include <lwip/dns.h>
  35. #include <lwip/ip_addr.h>
  36. #include <lwip/nd6.h>
  37. #include <lwip/netif.h>
  38. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI
  39. using namespace ::chip;
  40. using namespace ::chip::Inet;
  41. using namespace ::chip::System;
  42. using namespace ::chip::TLV;
  43. using chip::DeviceLayer::Internal::ESP32Utils;
  44. namespace chip {
  45. namespace DeviceLayer {
  46. ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMode(void)
  47. {
  48. if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
  49. {
  50. wifi_mode_t curWiFiMode;
  51. mWiFiStationMode =
  52. (esp_wifi_get_mode(&curWiFiMode) == ESP_OK && (curWiFiMode == WIFI_MODE_APSTA || curWiFiMode == WIFI_MODE_STA))
  53. ? kWiFiStationMode_Enabled
  54. : kWiFiStationMode_Disabled;
  55. }
  56. return mWiFiStationMode;
  57. }
  58. bool ConnectivityManagerImpl::_IsWiFiStationEnabled(void)
  59. {
  60. return GetWiFiStationMode() == kWiFiStationMode_Enabled;
  61. }
  62. CHIP_ERROR ConnectivityManagerImpl::_SetWiFiStationMode(WiFiStationMode val)
  63. {
  64. CHIP_ERROR err = CHIP_NO_ERROR;
  65. VerifyOrExit(val != kWiFiStationMode_NotSupported, err = CHIP_ERROR_INVALID_ARGUMENT);
  66. if (val != kWiFiStationMode_ApplicationControlled)
  67. {
  68. bool autoConnect = (val == kWiFiStationMode_Enabled);
  69. err = Internal::ESP32Utils::SetAPMode(autoConnect);
  70. SuccessOrExit(err);
  71. DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
  72. }
  73. if (mWiFiStationMode != val)
  74. {
  75. ChipLogProgress(DeviceLayer, "WiFi station mode change: %s -> %s", WiFiStationModeToStr(mWiFiStationMode),
  76. WiFiStationModeToStr(val));
  77. }
  78. mWiFiStationMode = val;
  79. exit:
  80. return err;
  81. }
  82. bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void)
  83. {
  84. return Internal::ESP32Utils::IsStationProvisioned();
  85. }
  86. void ConnectivityManagerImpl::_ClearWiFiStationProvision(void)
  87. {
  88. if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
  89. {
  90. DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
  91. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  92. DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL);
  93. #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  94. }
  95. }
  96. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  97. CHIP_ERROR ConnectivityManagerImpl::_SetWiFiAPMode(WiFiAPMode val)
  98. {
  99. CHIP_ERROR err = CHIP_NO_ERROR;
  100. VerifyOrExit(val != kWiFiAPMode_NotSupported, err = CHIP_ERROR_INVALID_ARGUMENT);
  101. if (mWiFiAPMode != val)
  102. {
  103. ChipLogProgress(DeviceLayer, "WiFi AP mode change: %s -> %s", WiFiAPModeToStr(mWiFiAPMode), WiFiAPModeToStr(val));
  104. }
  105. mWiFiAPMode = val;
  106. DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL);
  107. exit:
  108. return err;
  109. }
  110. void ConnectivityManagerImpl::_DemandStartWiFiAP(void)
  111. {
  112. if (mWiFiAPMode == kWiFiAPMode_OnDemand || mWiFiAPMode == kWiFiAPMode_OnDemand_NoStationProvision)
  113. {
  114. mLastAPDemandTime = System::SystemClock().GetMonotonicTimestamp();
  115. DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL);
  116. }
  117. }
  118. void ConnectivityManagerImpl::_StopOnDemandWiFiAP(void)
  119. {
  120. if (mWiFiAPMode == kWiFiAPMode_OnDemand || mWiFiAPMode == kWiFiAPMode_OnDemand_NoStationProvision)
  121. {
  122. mLastAPDemandTime = System::Clock::kZero;
  123. DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL);
  124. }
  125. }
  126. void ConnectivityManagerImpl::_MaintainOnDemandWiFiAP(void)
  127. {
  128. if (mWiFiAPMode == kWiFiAPMode_OnDemand || mWiFiAPMode == kWiFiAPMode_OnDemand_NoStationProvision)
  129. {
  130. if (mWiFiAPState == kWiFiAPState_Activating || mWiFiAPState == kWiFiAPState_Active)
  131. {
  132. mLastAPDemandTime = System::SystemClock().GetMonotonicTimestamp();
  133. }
  134. }
  135. }
  136. void ConnectivityManagerImpl::_SetWiFiAPIdleTimeout(System::Clock::Timeout val)
  137. {
  138. mWiFiAPIdleTimeout = val;
  139. DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL);
  140. }
  141. #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  142. #define WIFI_BAND_2_4GHZ 2400
  143. #define WIFI_BAND_5_0GHZ 5000
  144. static uint16_t Map2400MHz(const uint8_t inChannel)
  145. {
  146. uint16_t frequency = 0;
  147. if (inChannel >= 1 && inChannel <= 13)
  148. {
  149. // Cast is OK because we definitely fit in 16 bits.
  150. frequency = static_cast<uint16_t>(2412 + ((inChannel - 1) * 5));
  151. }
  152. else if (inChannel == 14)
  153. {
  154. frequency = 2484;
  155. }
  156. return frequency;
  157. }
  158. static uint16_t Map5000MHz(const uint8_t inChannel)
  159. {
  160. uint16_t frequency = 0;
  161. switch (inChannel)
  162. {
  163. case 183:
  164. frequency = 4915;
  165. break;
  166. case 184:
  167. frequency = 4920;
  168. break;
  169. case 185:
  170. frequency = 4925;
  171. break;
  172. case 187:
  173. frequency = 4935;
  174. break;
  175. case 188:
  176. frequency = 4940;
  177. break;
  178. case 189:
  179. frequency = 4945;
  180. break;
  181. case 192:
  182. frequency = 4960;
  183. break;
  184. case 196:
  185. frequency = 4980;
  186. break;
  187. case 7:
  188. frequency = 5035;
  189. break;
  190. case 8:
  191. frequency = 5040;
  192. break;
  193. case 9:
  194. frequency = 5045;
  195. break;
  196. case 11:
  197. frequency = 5055;
  198. break;
  199. case 12:
  200. frequency = 5060;
  201. break;
  202. case 16:
  203. frequency = 5080;
  204. break;
  205. case 34:
  206. frequency = 5170;
  207. break;
  208. case 36:
  209. frequency = 5180;
  210. break;
  211. case 38:
  212. frequency = 5190;
  213. break;
  214. case 40:
  215. frequency = 5200;
  216. break;
  217. case 42:
  218. frequency = 5210;
  219. break;
  220. case 44:
  221. frequency = 5220;
  222. break;
  223. case 46:
  224. frequency = 5230;
  225. break;
  226. case 48:
  227. frequency = 5240;
  228. break;
  229. case 52:
  230. frequency = 5260;
  231. break;
  232. case 56:
  233. frequency = 5280;
  234. break;
  235. case 60:
  236. frequency = 5300;
  237. break;
  238. case 64:
  239. frequency = 5320;
  240. break;
  241. case 100:
  242. frequency = 5500;
  243. break;
  244. case 104:
  245. frequency = 5520;
  246. break;
  247. case 108:
  248. frequency = 5540;
  249. break;
  250. case 112:
  251. frequency = 5560;
  252. break;
  253. case 116:
  254. frequency = 5580;
  255. break;
  256. case 120:
  257. frequency = 5600;
  258. break;
  259. case 124:
  260. frequency = 5620;
  261. break;
  262. case 128:
  263. frequency = 5640;
  264. break;
  265. case 132:
  266. frequency = 5660;
  267. break;
  268. case 136:
  269. frequency = 5680;
  270. break;
  271. case 140:
  272. frequency = 5700;
  273. break;
  274. case 149:
  275. frequency = 5745;
  276. break;
  277. case 153:
  278. frequency = 5765;
  279. break;
  280. case 157:
  281. frequency = 5785;
  282. break;
  283. case 161:
  284. frequency = 5805;
  285. break;
  286. case 165:
  287. frequency = 5825;
  288. break;
  289. }
  290. return frequency;
  291. }
  292. static uint16_t MapFrequency(const uint16_t inBand, const uint8_t inChannel)
  293. {
  294. uint16_t frequency = 0;
  295. if (inBand == WIFI_BAND_2_4GHZ)
  296. {
  297. frequency = Map2400MHz(inChannel);
  298. }
  299. else if (inBand == WIFI_BAND_5_0GHZ)
  300. {
  301. frequency = Map5000MHz(inChannel);
  302. }
  303. return frequency;
  304. }
  305. CHIP_ERROR ConnectivityManagerImpl::_GetAndLogWiFiStatsCounters(void)
  306. {
  307. esp_err_t err;
  308. wifi_config_t wifiConfig;
  309. uint8_t primaryChannel;
  310. wifi_second_chan_t secondChannel;
  311. uint16_t freq;
  312. uint16_t bssid;
  313. IgnoreUnusedVariable(freq);
  314. IgnoreUnusedVariable(bssid);
  315. err = esp_wifi_get_config(WIFI_IF_STA, &wifiConfig);
  316. if (err != ESP_OK)
  317. {
  318. ChipLogError(DeviceLayer, "esp_wifi_get_config() failed: %s", esp_err_to_name(err));
  319. return ESP32Utils::MapError(err);
  320. }
  321. err = esp_wifi_get_channel(&primaryChannel, &secondChannel);
  322. if (err != ESP_OK)
  323. {
  324. ChipLogError(DeviceLayer, "esp_wifi_get_channel() failed: %s", esp_err_to_name(err));
  325. return ESP32Utils::MapError(err);
  326. }
  327. freq = MapFrequency(WIFI_BAND_2_4GHZ, primaryChannel);
  328. static_assert(std::is_same<std::remove_reference<decltype(wifiConfig.sta.bssid[5])>::type, uint8_t>::value,
  329. "Our bits are going to start overlapping");
  330. bssid = static_cast<uint16_t>((wifiConfig.sta.bssid[4] << 8) | wifiConfig.sta.bssid[5]);
  331. ChipLogProgress(DeviceLayer,
  332. "WiFi-Telemetry\n"
  333. "BSSID: %x\n"
  334. "freq: %d\n",
  335. bssid, freq);
  336. return CHIP_NO_ERROR;
  337. }
  338. CHIP_ERROR ConnectivityManagerImpl::InitWiFi()
  339. {
  340. mLastStationConnectFailTime = System::Clock::kZero;
  341. mWiFiStationMode = kWiFiStationMode_Disabled;
  342. mWiFiStationState = kWiFiStationState_NotConnected;
  343. mWiFiStationReconnectInterval = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL);
  344. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  345. mLastAPDemandTime = System::Clock::kZero;
  346. mWiFiAPMode = kWiFiAPMode_Disabled;
  347. mWiFiAPState = kWiFiAPState_NotActive;
  348. mWiFiAPIdleTimeout = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT);
  349. #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  350. mFlags.SetRaw(0);
  351. // TODO Initialize the Chip Addressing and Routing Module.
  352. // Ensure that ESP station mode is enabled.
  353. ReturnErrorOnFailure(Internal::ESP32Utils::EnableStationMode());
  354. // If there is no persistent station provision...
  355. if (!IsWiFiStationProvisioned())
  356. {
  357. // If the code has been compiled with a default WiFi station provision, configure that now.
  358. if (CONFIG_DEFAULT_WIFI_SSID[0] != 0)
  359. {
  360. ChipLogProgress(DeviceLayer, "Setting default WiFi station configuration (SSID: %s)", CONFIG_DEFAULT_WIFI_SSID);
  361. // Set a default station configuration.
  362. wifi_config_t wifiConfig;
  363. memset(&wifiConfig, 0, sizeof(wifiConfig));
  364. memcpy(wifiConfig.sta.ssid, CONFIG_DEFAULT_WIFI_SSID,
  365. std::min(sizeof(wifiConfig.sta.ssid), strlen(CONFIG_DEFAULT_WIFI_SSID)));
  366. memcpy(wifiConfig.sta.password, CONFIG_DEFAULT_WIFI_PASSWORD,
  367. std::min(sizeof(wifiConfig.sta.password), strlen(CONFIG_DEFAULT_WIFI_PASSWORD)));
  368. wifiConfig.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
  369. wifiConfig.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
  370. esp_err_t err = esp_wifi_set_config(WIFI_IF_STA, &wifiConfig);
  371. if (err != ESP_OK)
  372. {
  373. ChipLogError(DeviceLayer, "esp_wifi_set_config() failed: %s", esp_err_to_name(err));
  374. }
  375. // Enable WiFi station mode.
  376. ReturnErrorOnFailure(SetWiFiStationMode(kWiFiStationMode_Enabled));
  377. }
  378. // Otherwise, ensure WiFi station mode is disabled.
  379. else
  380. {
  381. ReturnErrorOnFailure(SetWiFiStationMode(kWiFiStationMode_Disabled));
  382. }
  383. }
  384. // Force AP mode off for now.
  385. ReturnErrorOnFailure(Internal::ESP32Utils::SetAPMode(false));
  386. // Queue work items to bootstrap the AP and station state machines once the Chip event loop is running.
  387. ReturnErrorOnFailure(DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL));
  388. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  389. ReturnErrorOnFailure(DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL));
  390. #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  391. return CHIP_NO_ERROR;
  392. }
  393. void ConnectivityManagerImpl::OnWiFiPlatformEvent(const ChipDeviceEvent * event)
  394. {
  395. // Handle ESP system events...
  396. if (event->Type == DeviceEventType::kESPSystemEvent)
  397. {
  398. if (event->Platform.ESPSystemEvent.Base == WIFI_EVENT)
  399. {
  400. switch (event->Platform.ESPSystemEvent.Id)
  401. {
  402. case WIFI_EVENT_SCAN_DONE:
  403. ChipLogProgress(DeviceLayer, "WIFI_EVENT_SCAN_DONE");
  404. NetworkCommissioning::ESPWiFiDriver::GetInstance().OnScanWiFiNetworkDone();
  405. break;
  406. case WIFI_EVENT_STA_START:
  407. ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_START");
  408. DriveStationState();
  409. break;
  410. case WIFI_EVENT_STA_CONNECTED:
  411. ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_CONNECTED");
  412. if (mWiFiStationState == kWiFiStationState_Connecting)
  413. {
  414. ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded);
  415. }
  416. DriveStationState();
  417. break;
  418. case WIFI_EVENT_STA_DISCONNECTED:
  419. ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_DISCONNECTED");
  420. NetworkCommissioning::ESPWiFiDriver::GetInstance().SetLastDisconnectReason(event);
  421. if (mWiFiStationState == kWiFiStationState_Connecting)
  422. {
  423. ChangeWiFiStationState(kWiFiStationState_Connecting_Failed);
  424. }
  425. DriveStationState();
  426. break;
  427. case WIFI_EVENT_STA_STOP:
  428. ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_STOP");
  429. DriveStationState();
  430. break;
  431. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  432. case WIFI_EVENT_AP_START:
  433. ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_START");
  434. ChangeWiFiAPState(kWiFiAPState_Active);
  435. DriveAPState();
  436. break;
  437. case WIFI_EVENT_AP_STOP:
  438. ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_STOP");
  439. ChangeWiFiAPState(kWiFiAPState_NotActive);
  440. DriveAPState();
  441. break;
  442. case WIFI_EVENT_AP_STACONNECTED:
  443. ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_STACONNECTED");
  444. MaintainOnDemandWiFiAP();
  445. break;
  446. #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  447. default:
  448. break;
  449. }
  450. }
  451. if (event->Platform.ESPSystemEvent.Base == IP_EVENT)
  452. {
  453. switch (event->Platform.ESPSystemEvent.Id)
  454. {
  455. case IP_EVENT_STA_GOT_IP:
  456. ChipLogProgress(DeviceLayer, "IP_EVENT_STA_GOT_IP");
  457. OnStationIPv4AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp);
  458. break;
  459. case IP_EVENT_STA_LOST_IP:
  460. ChipLogProgress(DeviceLayer, "IP_EVENT_STA_LOST_IP");
  461. OnStationIPv4AddressLost();
  462. break;
  463. case IP_EVENT_GOT_IP6:
  464. ChipLogProgress(DeviceLayer, "IP_EVENT_GOT_IP6");
  465. if (strcmp(esp_netif_get_ifkey(event->Platform.ESPSystemEvent.Data.IpGotIp6.esp_netif), "WIFI_STA_DEF") == 0)
  466. {
  467. OnStationIPv6AddressAvailable(event->Platform.ESPSystemEvent.Data.IpGotIp6);
  468. }
  469. break;
  470. default:
  471. break;
  472. }
  473. }
  474. }
  475. }
  476. void ConnectivityManagerImpl::_OnWiFiScanDone()
  477. {
  478. // Schedule a call to DriveStationState method in case a station connect attempt was
  479. // deferred because the scan was in progress.
  480. DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
  481. }
  482. void ConnectivityManagerImpl::_OnWiFiStationProvisionChange()
  483. {
  484. // Schedule a call to the DriveStationState method to adjust the station state as needed.
  485. DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
  486. }
  487. void ConnectivityManagerImpl::DriveStationState()
  488. {
  489. bool stationConnected;
  490. // Refresh the current station mode. Specifically, this reads the ESP auto_connect flag,
  491. // which determine whether the WiFi station mode is kWiFiStationMode_Enabled or
  492. // kWiFiStationMode_Disabled.
  493. GetWiFiStationMode();
  494. // If the station interface is NOT under application control...
  495. if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
  496. {
  497. // Ensure that the ESP WiFi layer is started.
  498. ReturnOnFailure(Internal::ESP32Utils::StartWiFiLayer());
  499. // Ensure that station mode is enabled in the ESP WiFi layer.
  500. ReturnOnFailure(Internal::ESP32Utils::EnableStationMode());
  501. }
  502. // Determine if the ESP WiFi layer thinks the station interface is currently connected.
  503. ReturnOnFailure(Internal::ESP32Utils::IsStationConnected(stationConnected));
  504. // If the station interface is currently connected ...
  505. if (stationConnected)
  506. {
  507. // Advance the station state to Connected if it was previously NotConnected or
  508. // a previously initiated connect attempt succeeded.
  509. if (mWiFiStationState == kWiFiStationState_NotConnected || mWiFiStationState == kWiFiStationState_Connecting_Succeeded)
  510. {
  511. ChangeWiFiStationState(kWiFiStationState_Connected);
  512. ChipLogProgress(DeviceLayer, "WiFi station interface connected");
  513. mLastStationConnectFailTime = System::Clock::kZero;
  514. OnStationConnected();
  515. }
  516. // If the WiFi station interface is no longer enabled, or no longer provisioned,
  517. // disconnect the station from the AP, unless the WiFi station mode is currently
  518. // under application control.
  519. if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled &&
  520. (mWiFiStationMode != kWiFiStationMode_Enabled || !IsWiFiStationProvisioned()))
  521. {
  522. ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface");
  523. esp_err_t err = esp_wifi_disconnect();
  524. if (err != ESP_OK)
  525. {
  526. ChipLogError(DeviceLayer, "esp_wifi_disconnect() failed: %s", esp_err_to_name(err));
  527. return;
  528. }
  529. ChangeWiFiStationState(kWiFiStationState_Disconnecting);
  530. }
  531. }
  532. // Otherwise the station interface is NOT connected to an AP, so...
  533. else
  534. {
  535. System::Clock::Timestamp now = System::SystemClock().GetMonotonicTimestamp();
  536. // Advance the station state to NotConnected if it was previously Connected or Disconnecting,
  537. // or if a previous initiated connect attempt failed.
  538. if (mWiFiStationState == kWiFiStationState_Connected || mWiFiStationState == kWiFiStationState_Disconnecting ||
  539. mWiFiStationState == kWiFiStationState_Connecting_Failed)
  540. {
  541. WiFiStationState prevState = mWiFiStationState;
  542. ChangeWiFiStationState(kWiFiStationState_NotConnected);
  543. if (prevState != kWiFiStationState_Connecting_Failed)
  544. {
  545. ChipLogProgress(DeviceLayer, "WiFi station interface disconnected");
  546. mLastStationConnectFailTime = System::Clock::kZero;
  547. OnStationDisconnected();
  548. }
  549. else
  550. {
  551. mLastStationConnectFailTime = now;
  552. }
  553. }
  554. // If the WiFi station interface is now enabled and provisioned (and by implication,
  555. // not presently under application control), AND the system is not in the process of
  556. // scanning, then...
  557. if (mWiFiStationMode == kWiFiStationMode_Enabled && IsWiFiStationProvisioned())
  558. {
  559. // Initiate a connection to the AP if we haven't done so before, or if enough
  560. // time has passed since the last attempt.
  561. if (mLastStationConnectFailTime == System::Clock::kZero ||
  562. now >= mLastStationConnectFailTime + mWiFiStationReconnectInterval)
  563. {
  564. ChipLogProgress(DeviceLayer, "Attempting to connect WiFi station interface");
  565. esp_err_t err = esp_wifi_connect();
  566. if (err != ESP_OK)
  567. {
  568. ChipLogError(DeviceLayer, "esp_wifi_connect() failed: %s", esp_err_to_name(err));
  569. return;
  570. }
  571. ChangeWiFiStationState(kWiFiStationState_Connecting);
  572. }
  573. // Otherwise arrange another connection attempt at a suitable point in the future.
  574. else
  575. {
  576. System::Clock::Timeout timeToNextConnect = (mLastStationConnectFailTime + mWiFiStationReconnectInterval) - now;
  577. ChipLogProgress(DeviceLayer, "Next WiFi station reconnect in %" PRIu32 " ms",
  578. System::Clock::Milliseconds32(timeToNextConnect).count());
  579. ReturnOnFailure(DeviceLayer::SystemLayer().StartTimer(timeToNextConnect, DriveStationState, NULL));
  580. }
  581. }
  582. }
  583. ChipLogProgress(DeviceLayer, "Done driving station state, nothing else to do...");
  584. // Kick-off any pending network scan that might have been deferred due to the activity
  585. // of the WiFi station.
  586. }
  587. void ConnectivityManagerImpl::OnStationConnected()
  588. {
  589. // Assign an IPv6 link local address to the station interface.
  590. esp_err_t err = esp_netif_create_ip6_linklocal(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
  591. if (err != ESP_OK)
  592. {
  593. ChipLogError(DeviceLayer, "esp_netif_create_ip6_linklocal() failed for WIFI_STA_DEF interface: %s", esp_err_to_name(err));
  594. }
  595. NetworkCommissioning::ESPWiFiDriver::GetInstance().OnConnectWiFiNetwork();
  596. // TODO Invoke WARM to perform actions that occur when the WiFi station interface comes up.
  597. // Alert other components of the new state.
  598. ChipDeviceEvent event;
  599. event.Type = DeviceEventType::kWiFiConnectivityChange;
  600. event.WiFiConnectivityChange.Result = kConnectivity_Established;
  601. PlatformMgr().PostEventOrDie(&event);
  602. WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetWiFiDiagnosticsDelegate();
  603. if (delegate)
  604. {
  605. delegate->OnConnectionStatusChanged(
  606. chip::to_underlying(chip::app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kConnected));
  607. }
  608. UpdateInternetConnectivityState();
  609. }
  610. void ConnectivityManagerImpl::OnStationDisconnected()
  611. {
  612. // TODO Invoke WARM to perform actions that occur when the WiFi station interface goes down.
  613. // Alert other components of the new state.
  614. ChipDeviceEvent event;
  615. event.Type = DeviceEventType::kWiFiConnectivityChange;
  616. event.WiFiConnectivityChange.Result = kConnectivity_Lost;
  617. PlatformMgr().PostEventOrDie(&event);
  618. WiFiDiagnosticsDelegate * delegate = GetDiagnosticDataProvider().GetWiFiDiagnosticsDelegate();
  619. uint16_t reason = NetworkCommissioning::ESPWiFiDriver::GetInstance().GetLastDisconnectReason();
  620. uint8_t associationFailureCause =
  621. chip::to_underlying(chip::app::Clusters::WiFiNetworkDiagnostics::AssociationFailureCauseEnum::kUnknown);
  622. switch (reason)
  623. {
  624. case WIFI_REASON_ASSOC_TOOMANY:
  625. case WIFI_REASON_NOT_ASSOCED:
  626. case WIFI_REASON_ASSOC_NOT_AUTHED:
  627. case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
  628. case WIFI_REASON_GROUP_CIPHER_INVALID:
  629. case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
  630. case WIFI_REASON_AKMP_INVALID:
  631. case WIFI_REASON_CIPHER_SUITE_REJECTED:
  632. case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
  633. associationFailureCause =
  634. chip::to_underlying(chip::app::Clusters::WiFiNetworkDiagnostics::AssociationFailureCauseEnum::kAssociationFailed);
  635. if (delegate)
  636. {
  637. delegate->OnAssociationFailureDetected(associationFailureCause, reason);
  638. }
  639. break;
  640. case WIFI_REASON_NOT_AUTHED:
  641. case WIFI_REASON_MIC_FAILURE:
  642. case WIFI_REASON_IE_IN_4WAY_DIFFERS:
  643. case WIFI_REASON_INVALID_RSN_IE_CAP:
  644. case WIFI_REASON_INVALID_PMKID:
  645. case WIFI_REASON_802_1X_AUTH_FAILED:
  646. associationFailureCause =
  647. chip::to_underlying(chip::app::Clusters::WiFiNetworkDiagnostics::AssociationFailureCauseEnum::kAuthenticationFailed);
  648. if (delegate)
  649. {
  650. delegate->OnAssociationFailureDetected(associationFailureCause, reason);
  651. }
  652. break;
  653. case WIFI_REASON_NO_AP_FOUND:
  654. associationFailureCause =
  655. chip::to_underlying(chip::app::Clusters::WiFiNetworkDiagnostics::AssociationFailureCauseEnum::kSsidNotFound);
  656. if (delegate)
  657. {
  658. delegate->OnAssociationFailureDetected(associationFailureCause, reason);
  659. }
  660. case WIFI_REASON_BEACON_TIMEOUT:
  661. case WIFI_REASON_AUTH_EXPIRE:
  662. case WIFI_REASON_AUTH_LEAVE:
  663. case WIFI_REASON_ASSOC_LEAVE:
  664. case WIFI_REASON_ASSOC_EXPIRE:
  665. break;
  666. default:
  667. if (delegate)
  668. {
  669. delegate->OnAssociationFailureDetected(associationFailureCause, reason);
  670. }
  671. break;
  672. }
  673. if (delegate)
  674. {
  675. delegate->OnDisconnectionDetected(reason);
  676. delegate->OnConnectionStatusChanged(
  677. chip::to_underlying(chip::app::Clusters::WiFiNetworkDiagnostics::ConnectionStatusEnum::kNotConnected));
  678. }
  679. UpdateInternetConnectivityState();
  680. }
  681. void ConnectivityManagerImpl::ChangeWiFiStationState(WiFiStationState newState)
  682. {
  683. if (mWiFiStationState != newState)
  684. {
  685. ChipLogProgress(DeviceLayer, "WiFi station state change: %s -> %s", WiFiStationStateToStr(mWiFiStationState),
  686. WiFiStationStateToStr(newState));
  687. mWiFiStationState = newState;
  688. SystemLayer().ScheduleLambda([]() { NetworkCommissioning::ESPWiFiDriver::GetInstance().OnNetworkStatusChange(); });
  689. }
  690. }
  691. void ConnectivityManagerImpl::DriveStationState(::chip::System::Layer * aLayer, void * aAppState)
  692. {
  693. sInstance.DriveStationState();
  694. }
  695. #if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  696. void ConnectivityManagerImpl::DriveAPState()
  697. {
  698. CHIP_ERROR err = CHIP_NO_ERROR;
  699. WiFiAPState targetState;
  700. bool espAPModeEnabled;
  701. // Determine if AP mode is currently enabled in the ESP WiFi layer.
  702. err = Internal::ESP32Utils::IsAPEnabled(espAPModeEnabled);
  703. SuccessOrExit(err);
  704. // Adjust the Connectivity Manager's AP state to match the state in the WiFi layer.
  705. if (espAPModeEnabled && (mWiFiAPState == kWiFiAPState_NotActive || mWiFiAPState == kWiFiAPState_Deactivating))
  706. {
  707. ChangeWiFiAPState(kWiFiAPState_Activating);
  708. }
  709. if (!espAPModeEnabled && (mWiFiAPState == kWiFiAPState_Active || mWiFiAPState == kWiFiAPState_Activating))
  710. {
  711. ChangeWiFiAPState(kWiFiAPState_Deactivating);
  712. }
  713. // If the AP interface is not under application control...
  714. if (mWiFiAPMode != kWiFiAPMode_ApplicationControlled)
  715. {
  716. // Ensure the ESP WiFi layer is started.
  717. err = Internal::ESP32Utils::StartWiFiLayer();
  718. SuccessOrExit(err);
  719. // Determine the target (desired) state for AP interface...
  720. // The target state is 'NotActive' if the application has expressly disabled the AP interface.
  721. if (mWiFiAPMode == kWiFiAPMode_Disabled)
  722. {
  723. targetState = kWiFiAPState_NotActive;
  724. }
  725. // The target state is 'Active' if the application has expressly enabled the AP interface.
  726. else if (mWiFiAPMode == kWiFiAPMode_Enabled)
  727. {
  728. targetState = kWiFiAPState_Active;
  729. }
  730. // The target state is 'Active' if the AP mode is 'On demand, when no station is available'
  731. // and the station interface is not provisioned or the application has disabled the station
  732. // interface.
  733. else if (mWiFiAPMode == kWiFiAPMode_OnDemand_NoStationProvision &&
  734. (!IsWiFiStationProvisioned() || GetWiFiStationMode() == kWiFiStationMode_Disabled))
  735. {
  736. targetState = kWiFiAPState_Active;
  737. }
  738. // The target state is 'Active' if the AP mode is one of the 'On demand' modes and there
  739. // has been demand for the AP within the idle timeout period.
  740. else if (mWiFiAPMode == kWiFiAPMode_OnDemand || mWiFiAPMode == kWiFiAPMode_OnDemand_NoStationProvision)
  741. {
  742. System::Clock::Timestamp now = System::SystemClock().GetMonotonicTimestamp();
  743. if (mLastAPDemandTime != System::Clock::kZero && now < (mLastAPDemandTime + mWiFiAPIdleTimeout))
  744. {
  745. targetState = kWiFiAPState_Active;
  746. // Compute the amount of idle time before the AP should be deactivated and
  747. // arm a timer to fire at that time.
  748. System::Clock::Timeout apTimeout = (mLastAPDemandTime + mWiFiAPIdleTimeout) - now;
  749. err = DeviceLayer::SystemLayer().StartTimer(apTimeout, DriveAPState, NULL);
  750. SuccessOrExit(err);
  751. ChipLogProgress(DeviceLayer, "Next WiFi AP timeout in %" PRIu32 " ms",
  752. System::Clock::Milliseconds32(apTimeout).count());
  753. }
  754. else
  755. {
  756. targetState = kWiFiAPState_NotActive;
  757. }
  758. }
  759. // Otherwise the target state is 'NotActive'.
  760. else
  761. {
  762. targetState = kWiFiAPState_NotActive;
  763. }
  764. // If the current AP state does not match the target state...
  765. if (mWiFiAPState != targetState)
  766. {
  767. // If the target state is 'Active' and the current state is NOT 'Activating', enable
  768. // and configure the AP interface, and then enter the 'Activating' state. Eventually
  769. // a SYSTEM_EVENT_AP_START event will be received from the ESP WiFi layer which will
  770. // cause the state to transition to 'Active'.
  771. if (targetState == kWiFiAPState_Active)
  772. {
  773. if (mWiFiAPState != kWiFiAPState_Activating)
  774. {
  775. err = Internal::ESP32Utils::SetAPMode(true);
  776. SuccessOrExit(err);
  777. err = ConfigureWiFiAP();
  778. SuccessOrExit(err);
  779. ChangeWiFiAPState(kWiFiAPState_Activating);
  780. }
  781. }
  782. // Otherwise, if the target state is 'NotActive' and the current state is not 'Deactivating',
  783. // disable the AP interface and enter the 'Deactivating' state. Later a SYSTEM_EVENT_AP_STOP
  784. // event will move the AP state to 'NotActive'.
  785. else
  786. {
  787. if (mWiFiAPState != kWiFiAPState_Deactivating)
  788. {
  789. err = Internal::ESP32Utils::SetAPMode(false);
  790. SuccessOrExit(err);
  791. ChangeWiFiAPState(kWiFiAPState_Deactivating);
  792. }
  793. }
  794. }
  795. }
  796. // If AP is active, but the interface doesn't have an IPv6 link-local
  797. // address, assign one now.
  798. if (mWiFiAPState == kWiFiAPState_Active && Internal::ESP32Utils::IsInterfaceUp("WIFI_AP_DEF") &&
  799. !Internal::ESP32Utils::HasIPv6LinkLocalAddress("WIFI_AP_DEF"))
  800. {
  801. esp_err_t error = esp_netif_create_ip6_linklocal(esp_netif_get_handle_from_ifkey("WIFI_AP_DEF"));
  802. if (error != ESP_OK)
  803. {
  804. ChipLogError(DeviceLayer, "esp_netif_create_ip6_linklocal() failed for WIFI_AP_DEF interface: %s",
  805. esp_err_to_name(error));
  806. goto exit;
  807. }
  808. }
  809. exit:
  810. if (err != CHIP_NO_ERROR && mWiFiAPMode != kWiFiAPMode_ApplicationControlled)
  811. {
  812. SetWiFiAPMode(kWiFiAPMode_Disabled);
  813. Internal::ESP32Utils::SetAPMode(false);
  814. }
  815. }
  816. CHIP_ERROR ConnectivityManagerImpl::ConfigureWiFiAP()
  817. {
  818. wifi_config_t wifiConfig;
  819. memset(&wifiConfig, 0, sizeof(wifiConfig));
  820. uint16_t discriminator;
  821. ReturnErrorOnFailure(GetCommissionableDataProvider()->GetSetupDiscriminator(discriminator));
  822. uint16_t vendorId;
  823. uint16_t productId;
  824. ReturnErrorOnFailure(GetDeviceInstanceInfoProvider()->GetVendorId(vendorId));
  825. ReturnErrorOnFailure(GetDeviceInstanceInfoProvider()->GetProductId(productId));
  826. snprintf((char *) wifiConfig.ap.ssid, sizeof(wifiConfig.ap.ssid), "%s%03X-%04X-%04X", CHIP_DEVICE_CONFIG_WIFI_AP_SSID_PREFIX,
  827. discriminator, vendorId, productId);
  828. wifiConfig.ap.channel = CHIP_DEVICE_CONFIG_WIFI_AP_CHANNEL;
  829. wifiConfig.ap.authmode = WIFI_AUTH_OPEN;
  830. wifiConfig.ap.max_connection = CHIP_DEVICE_CONFIG_WIFI_AP_MAX_STATIONS;
  831. wifiConfig.ap.beacon_interval = CHIP_DEVICE_CONFIG_WIFI_AP_BEACON_INTERVAL;
  832. ChipLogProgress(DeviceLayer, "Configuring WiFi AP: SSID %s, channel %u", wifiConfig.ap.ssid, wifiConfig.ap.channel);
  833. esp_err_t err = esp_wifi_set_config(WIFI_IF_AP, &wifiConfig);
  834. if (err != ESP_OK)
  835. {
  836. ChipLogError(DeviceLayer, "esp_wifi_set_config(WIFI_IF_AP) failed: %s", esp_err_to_name(err));
  837. return ESP32Utils::MapError(err);
  838. }
  839. return CHIP_NO_ERROR;
  840. }
  841. void ConnectivityManagerImpl::ChangeWiFiAPState(WiFiAPState newState)
  842. {
  843. if (mWiFiAPState != newState)
  844. {
  845. ChipLogProgress(DeviceLayer, "WiFi AP state change: %s -> %s", WiFiAPStateToStr(mWiFiAPState), WiFiAPStateToStr(newState));
  846. mWiFiAPState = newState;
  847. }
  848. }
  849. void ConnectivityManagerImpl::DriveAPState(::chip::System::Layer * aLayer, void * aAppState)
  850. {
  851. sInstance.DriveAPState();
  852. }
  853. #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
  854. void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
  855. {
  856. bool haveIPv4Conn = false;
  857. bool haveIPv6Conn = false;
  858. const bool hadIPv4Conn = mFlags.Has(ConnectivityFlags::kHaveIPv4InternetConnectivity);
  859. const bool hadIPv6Conn = mFlags.Has(ConnectivityFlags::kHaveIPv6InternetConnectivity);
  860. IPAddress addr;
  861. // If the WiFi station is currently in the connected state...
  862. if (mWiFiStationState == kWiFiStationState_Connected)
  863. {
  864. // Get the LwIP netif for the WiFi station interface.
  865. struct netif * netif = Internal::ESP32Utils::GetStationNetif();
  866. // If the WiFi station interface is up...
  867. if (netif != NULL && netif_is_up(netif) && netif_is_link_up(netif))
  868. {
  869. // Check if a DNS server is currently configured. If so...
  870. ip_addr_t dnsServerAddr = *dns_getserver(0);
  871. if (!ip_addr_isany_val(dnsServerAddr))
  872. {
  873. // If the station interface has been assigned an IPv4 address, and has
  874. // an IPv4 gateway, then presume that the device has IPv4 Internet
  875. // connectivity.
  876. if (!ip4_addr_isany_val(*netif_ip4_addr(netif)) && !ip4_addr_isany_val(*netif_ip4_gw(netif)))
  877. {
  878. haveIPv4Conn = true;
  879. esp_netif_ip_info_t ipInfo;
  880. if (esp_netif_get_ip_info(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"), &ipInfo) == ESP_OK)
  881. {
  882. char addrStr[INET_ADDRSTRLEN];
  883. // ToDo: change the code to using IPv6 address
  884. esp_ip4addr_ntoa(&ipInfo.ip, addrStr, sizeof(addrStr));
  885. IPAddress::FromString(addrStr, addr);
  886. }
  887. }
  888. // Search among the IPv6 addresses assigned to the interface for a Global Unicast
  889. // address (2000::/3) that is in the valid state. If such an address is found...
  890. for (uint8_t i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  891. {
  892. if (ip6_addr_isglobal(netif_ip6_addr(netif, i)) && ip6_addr_isvalid(netif_ip6_addr_state(netif, i)))
  893. {
  894. // Determine if there is a default IPv6 router that is currently reachable
  895. // via the station interface. If so, presume for now that the device has
  896. // IPv6 connectivity.
  897. struct netif * found_if = nd6_find_route(IP6_ADDR_ANY6);
  898. if (found_if && netif->num == found_if->num)
  899. {
  900. haveIPv6Conn = true;
  901. }
  902. }
  903. }
  904. }
  905. }
  906. }
  907. // If the internet connectivity state has changed...
  908. if (haveIPv4Conn != hadIPv4Conn || haveIPv6Conn != hadIPv6Conn)
  909. {
  910. // Update the current state.
  911. mFlags.Set(ConnectivityFlags::kHaveIPv4InternetConnectivity, haveIPv4Conn)
  912. .Set(ConnectivityFlags::kHaveIPv6InternetConnectivity, haveIPv6Conn);
  913. // Alert other components of the state change.
  914. ChipDeviceEvent event;
  915. event.Type = DeviceEventType::kInternetConnectivityChange;
  916. event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn);
  917. event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn);
  918. event.InternetConnectivityChange.ipAddress = addr;
  919. PlatformMgr().PostEventOrDie(&event);
  920. if (haveIPv4Conn != hadIPv4Conn)
  921. {
  922. ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv4", (haveIPv4Conn) ? "ESTABLISHED" : "LOST");
  923. }
  924. if (haveIPv6Conn != hadIPv6Conn)
  925. {
  926. ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv6", (haveIPv6Conn) ? "ESTABLISHED" : "LOST");
  927. }
  928. }
  929. }
  930. void ConnectivityManagerImpl::OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip)
  931. {
  932. #if CHIP_PROGRESS_LOGGING
  933. {
  934. ChipLogProgress(DeviceLayer, "IPv4 address %s on WiFi station interface: " IPSTR "/" IPSTR " gateway " IPSTR,
  935. (got_ip.ip_changed) ? "changed" : "ready", IP2STR(&got_ip.ip_info.ip), IP2STR(&got_ip.ip_info.netmask),
  936. IP2STR(&got_ip.ip_info.gw));
  937. }
  938. #endif // CHIP_PROGRESS_LOGGING
  939. UpdateInternetConnectivityState();
  940. ChipDeviceEvent event;
  941. event.Type = DeviceEventType::kInterfaceIpAddressChanged;
  942. event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV4_Assigned;
  943. PlatformMgr().PostEventOrDie(&event);
  944. }
  945. void ConnectivityManagerImpl::OnStationIPv4AddressLost(void)
  946. {
  947. ChipLogProgress(DeviceLayer, "IPv4 address lost on WiFi station interface");
  948. UpdateInternetConnectivityState();
  949. ChipDeviceEvent event;
  950. event.Type = DeviceEventType::kInterfaceIpAddressChanged;
  951. event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV4_Lost;
  952. PlatformMgr().PostEventOrDie(&event);
  953. }
  954. void ConnectivityManagerImpl::OnStationIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip)
  955. {
  956. #if CHIP_PROGRESS_LOGGING
  957. {
  958. ChipLogProgress(DeviceLayer, "IPv6 addr available. Ready on %s interface: " IPV6STR, esp_netif_get_ifkey(got_ip.esp_netif),
  959. IPV62STR(got_ip.ip6_info.ip));
  960. }
  961. #endif // CHIP_PROGRESS_LOGGING
  962. UpdateInternetConnectivityState();
  963. ChipDeviceEvent event;
  964. event.Type = DeviceEventType::kInterfaceIpAddressChanged;
  965. event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV6_Assigned;
  966. PlatformMgr().PostEventOrDie(&event);
  967. esp_route_hook_init(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"));
  968. }
  969. } // namespace DeviceLayer
  970. } // namespace chip
  971. #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI