| /* |
| * |
| * Copyright (c) 2022 Project CHIP Authors |
| * 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. |
| */ |
| |
| #include "AppTask.h" |
| |
| #include "AppConfig.h" |
| #include "AppEvent.h" |
| #include "ButtonManager.h" |
| #include "LEDWidget.h" |
| #include "binding-handler.h" |
| #include <app/server/OnboardingCodesUtil.h> |
| #include <app/server/Server.h> |
| |
| #include "ThreadUtil.h" |
| |
| #include <app-common/zap-generated/attribute-id.h> |
| #include <app-common/zap-generated/attribute-type.h> |
| #include <app/util/attribute-storage.h> |
| |
| #include <credentials/DeviceAttestationCredsProvider.h> |
| #include <credentials/examples/DeviceAttestationCredsExample.h> |
| |
| #if CONFIG_CHIP_OTA_REQUESTOR |
| #include "OTAUtil.h" |
| #endif |
| |
| #include <zephyr/logging/log.h> |
| #include <zephyr/zephyr.h> |
| |
| LOG_MODULE_DECLARE(app); |
| |
| namespace { |
| |
| constexpr int kAppEventQueueSize = 10; |
| constexpr uint8_t kButtonPushEvent = 1; |
| constexpr uint8_t kButtonReleaseEvent = 0; |
| |
| K_MSGQ_DEFINE(sAppEventQueue, sizeof(AppEvent), kAppEventQueueSize, alignof(AppEvent)); |
| |
| LEDWidget sStatusLED; |
| |
| Button sFactoryResetButton; |
| Button sBleAdvStartButton; |
| |
| bool sIsThreadProvisioned = false; |
| bool sIsThreadEnabled = false; |
| bool sIsThreadAttached = false; |
| bool sHaveBLEConnections = false; |
| |
| } // namespace |
| |
| using namespace ::chip; |
| using namespace ::chip::Credentials; |
| using namespace ::chip::DeviceLayer; |
| using namespace ::chip::DeviceLayer::Internal; |
| |
| AppTask AppTask::sAppTask; |
| |
| constexpr EndpointId kNetworkCommissioningEndpointSecondary = 0xFFFE; |
| |
| CHIP_ERROR AppTask::Init() |
| { |
| CHIP_ERROR ret; |
| |
| LOG_INF("Current Software Version: %u, %s", CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION, |
| CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION_STRING); |
| |
| // Initialize status LED |
| LEDWidget::InitGpio(SYSTEM_STATE_LED_PORT); |
| sStatusLED.Init(SYSTEM_STATE_LED_PIN); |
| |
| UpdateStatusLED(); |
| |
| InitButtons(); |
| |
| // Init ZCL Data Model and start server |
| static chip::CommonCaseDeviceServerInitParams initParams; |
| (void) initParams.InitializeStaticResourcesBeforeServerInit(); |
| chip::Server::GetInstance().Init(initParams); |
| |
| // Initialize device attestation config |
| SetDeviceAttestationCredentialsProvider(Examples::GetExampleDACProvider()); |
| |
| #if CONFIG_CHIP_OTA_REQUESTOR |
| InitBasicOTARequestor(); |
| #endif |
| |
| // We only have network commissioning on endpoint 0. |
| // Set up a valid Network Commissioning cluster on endpoint 0 is done in |
| // src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.cpp |
| emberAfEndpointEnableDisable(kNetworkCommissioningEndpointSecondary, false); |
| |
| ConfigurationMgr().LogDeviceConfig(); |
| |
| // Configure Bindings |
| ret = InitBindingHandlers(); |
| if (ret != CHIP_NO_ERROR) |
| { |
| LOG_ERR("InitBindingHandlers() failed"); |
| return ret; |
| } |
| |
| PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); |
| |
| // Add CHIP event handler and start CHIP thread. |
| // Note that all the initialization code should happen prior to this point to avoid data races |
| // between the main and the CHIP threads. |
| PlatformMgr().AddEventHandler(ChipEventHandler, 0); |
| |
| ret = ConnectivityMgr().SetBLEDeviceName("TelinkMinApp"); |
| if (ret != CHIP_NO_ERROR) |
| { |
| LOG_ERR("Fail to set BLE device name"); |
| return ret; |
| } |
| |
| return CHIP_NO_ERROR; |
| } |
| |
| CHIP_ERROR AppTask::StartApp() |
| { |
| CHIP_ERROR err = Init(); |
| |
| if (err != CHIP_NO_ERROR) |
| { |
| LOG_ERR("AppTask.Init() failed"); |
| return err; |
| } |
| |
| AppEvent event = {}; |
| |
| while (true) |
| { |
| int ret = k_msgq_get(&sAppEventQueue, &event, K_MSEC(10)); |
| |
| while (!ret) |
| { |
| DispatchEvent(&event); |
| ret = k_msgq_get(&sAppEventQueue, &event, K_NO_WAIT); |
| } |
| |
| sStatusLED.Animate(); |
| } |
| } |
| |
| void AppTask::FactoryResetButtonEventHandler(void) |
| { |
| AppEvent event; |
| |
| event.Type = AppEvent::kEventType_Button; |
| event.ButtonEvent.Action = kButtonPushEvent; |
| event.Handler = FactoryResetHandler; |
| sAppTask.PostEvent(&event); |
| } |
| |
| void AppTask::FactoryResetHandler(AppEvent * aEvent) |
| { |
| LOG_INF("Factory Reset triggered."); |
| chip::Server::GetInstance().ScheduleFactoryReset(); |
| } |
| |
| void AppTask::StartBleAdvButtonEventHandler(void) |
| { |
| AppEvent event; |
| |
| event.Type = AppEvent::kEventType_Button; |
| event.ButtonEvent.Action = kButtonPushEvent; |
| event.Handler = StartBleAdvHandler; |
| sAppTask.PostEvent(&event); |
| } |
| |
| void AppTask::StartBleAdvHandler(AppEvent * aEvent) |
| { |
| LOG_INF("BLE advertising start button pressed"); |
| |
| // Don't allow on starting Matter service BLE advertising after Thread provisioning. |
| if (ConnectivityMgr().IsThreadProvisioned()) |
| { |
| LOG_INF("Matter service BLE advertising not started - device is commissioned to a Thread network."); |
| return; |
| } |
| |
| if (ConnectivityMgr().IsBLEAdvertisingEnabled()) |
| { |
| LOG_INF("BLE advertising is already enabled"); |
| return; |
| } |
| |
| if (chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow() != CHIP_NO_ERROR) |
| { |
| LOG_ERR("OpenBasicCommissioningWindow() failed"); |
| } |
| } |
| |
| void AppTask::UpdateStatusLED() |
| { |
| if (sIsThreadProvisioned && sIsThreadEnabled) |
| { |
| if (sIsThreadAttached) |
| { |
| sStatusLED.Blink(950, 50); |
| } |
| else |
| { |
| sStatusLED.Blink(100, 100); |
| } |
| } |
| else |
| { |
| sStatusLED.Blink(50, 950); |
| } |
| } |
| |
| void AppTask::ChipEventHandler(const ChipDeviceEvent * event, intptr_t /* arg */) |
| { |
| switch (event->Type) |
| { |
| case DeviceEventType::kCHIPoBLEAdvertisingChange: |
| sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0; |
| UpdateStatusLED(); |
| break; |
| case DeviceEventType::kThreadStateChange: |
| sIsThreadProvisioned = ConnectivityMgr().IsThreadProvisioned(); |
| sIsThreadEnabled = ConnectivityMgr().IsThreadEnabled(); |
| sIsThreadAttached = ConnectivityMgr().IsThreadAttached(); |
| UpdateStatusLED(); |
| break; |
| case DeviceEventType::kThreadConnectivityChange: |
| #if CONFIG_CHIP_OTA_REQUESTOR |
| if (event->ThreadConnectivityChange.Result == kConnectivity_Established) |
| { |
| InitBasicOTARequestor(); |
| } |
| #endif |
| break; |
| default: |
| break; |
| } |
| } |
| |
| void AppTask::PostEvent(AppEvent * aEvent) |
| { |
| if (!aEvent) |
| return; |
| if (k_msgq_put(&sAppEventQueue, aEvent, K_NO_WAIT) != 0) |
| { |
| LOG_INF("Failed to post event to app task event queue"); |
| } |
| } |
| |
| void AppTask::DispatchEvent(AppEvent * aEvent) |
| { |
| if (!aEvent) |
| return; |
| if (aEvent->Handler) |
| { |
| aEvent->Handler(aEvent); |
| } |
| else |
| { |
| LOG_INF("Event received with no handler. Dropping event."); |
| } |
| } |
| |
| void AppTask::InitButtons(void) |
| { |
| sFactoryResetButton.Configure(BUTTON_PORT, BUTTON_PIN_3, BUTTON_PIN_1, FactoryResetButtonEventHandler); |
| sBleAdvStartButton.Configure(BUTTON_PORT, BUTTON_PIN_4, BUTTON_PIN_2, StartBleAdvButtonEventHandler); |
| |
| ButtonManagerInst().AddButton(sFactoryResetButton); |
| ButtonManagerInst().AddButton(sBleAdvStartButton); |
| } |