| /* |
| * |
| * Copyright (c) 2021 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 <platform/CHIPDeviceLayer.h> |
| #include <platform/PlatformManager.h> |
| |
| #include "af.h" |
| #include "gen/attribute-id.h" |
| #include "gen/cluster-id.h" |
| #include <app/chip-zcl-zpro-codec.h> |
| #include <app/util/af-types.h> |
| #include <app/util/attribute-storage.h> |
| #include <app/util/util.h> |
| #include <core/CHIPError.h> |
| #include <setup_payload/QRCodeSetupPayloadGenerator.h> |
| #include <setup_payload/SetupPayload.h> |
| #include <support/CHIPMem.h> |
| #include <support/ErrorMacros.h> |
| #include <support/ErrorStr.h> |
| #include <support/RandUtils.h> |
| |
| #include "LightingManager.h" |
| #include "Options.h" |
| #include "Server.h" |
| |
| #include <cassert> |
| #include <iostream> |
| |
| using namespace chip; |
| using namespace chip::Inet; |
| using namespace chip::Transport; |
| using namespace chip::DeviceLayer; |
| |
| void emberAfPostAttributeChangeCallback(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId, uint8_t mask, |
| uint16_t manufacturerCode, uint8_t type, uint8_t size, uint8_t * value) |
| { |
| if (clusterId != ZCL_ON_OFF_CLUSTER_ID) |
| { |
| ChipLogProgress(Zcl, "Unknown cluster ID: %d", clusterId); |
| return; |
| } |
| |
| if (attributeId != ZCL_ON_OFF_ATTRIBUTE_ID) |
| { |
| ChipLogProgress(Zcl, "Unknown attribute ID: %d", attributeId); |
| return; |
| } |
| |
| if (*value) |
| { |
| LightingMgr().InitiateAction(LightingManager::ON_ACTION); |
| } |
| else |
| { |
| LightingMgr().InitiateAction(LightingManager::OFF_ACTION); |
| } |
| } |
| |
| /** @brief OnOff Cluster Init |
| * |
| * This function is called when a specific cluster is initialized. It gives the |
| * application an opportunity to take care of cluster initialization procedures. |
| * It is called exactly once for each endpoint where cluster is present. |
| * |
| * @param endpoint Ver.: always |
| * |
| * TODO Issue #3841 |
| * emberAfOnOffClusterInitCallback happens before the stack initialize the cluster |
| * attributes to the default value. |
| * The logic here expects something similar to the deprecated Plugins callback |
| * emberAfPluginOnOffClusterServerPostInitCallback. |
| * |
| */ |
| void emberAfOnOffClusterInitCallback(EndpointId endpoint) |
| { |
| // TODO: implement any additional Cluster Server init actions |
| } |
| |
| namespace { |
| void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg) |
| { |
| (void) arg; |
| if (event->Type == chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished) |
| { |
| ChipLogProgress(DeviceLayer, "Receive kCHIPoBLEConnectionEstablished"); |
| } |
| } |
| |
| CHIP_ERROR PrintQRCodeContent() |
| { |
| CHIP_ERROR err = CHIP_NO_ERROR; |
| // If we do not have a discriminator, generate one |
| chip::SetupPayload payload; |
| uint32_t setUpPINCode; |
| uint16_t setUpDiscriminator; |
| uint16_t vendorId; |
| uint16_t productId; |
| std::string result; |
| |
| err = ConfigurationMgr().GetSetupPinCode(setUpPINCode); |
| SuccessOrExit(err); |
| |
| err = ConfigurationMgr().GetSetupDiscriminator(setUpDiscriminator); |
| SuccessOrExit(err); |
| |
| err = ConfigurationMgr().GetVendorId(vendorId); |
| SuccessOrExit(err); |
| |
| err = ConfigurationMgr().GetProductId(productId); |
| SuccessOrExit(err); |
| |
| payload.version = 0; |
| payload.vendorID = vendorId; |
| payload.productID = productId; |
| payload.setUpPINCode = setUpPINCode; |
| payload.discriminator = setUpDiscriminator; |
| |
| // Wrap it so SuccessOrExit can work |
| { |
| chip::QRCodeSetupPayloadGenerator generator(payload); |
| err = generator.payloadBase41Representation(result); |
| SuccessOrExit(err); |
| } |
| |
| std::cout << "SetupPINCode: [" << setUpPINCode << "]" << std::endl; |
| // There might be whitespace in setup QRCode, add brackets to make it clearer. |
| std::cout << "SetupQRCode: [" << result << "]" << std::endl; |
| |
| exit: |
| if (err != CHIP_NO_ERROR) |
| { |
| std::cerr << "Failed to generate QR Code: " << ErrorStr(err) << std::endl; |
| } |
| return err; |
| } |
| } // namespace |
| |
| int main(int argc, char * argv[]) |
| { |
| CHIP_ERROR err = CHIP_NO_ERROR; |
| |
| err = chip::Platform::MemoryInit(); |
| SuccessOrExit(err); |
| |
| err = ParseArguments(argc, argv); |
| SuccessOrExit(err); |
| |
| err = chip::DeviceLayer::PlatformMgr().InitChipStack(); |
| SuccessOrExit(err); |
| |
| err = PrintQRCodeContent(); |
| SuccessOrExit(err); |
| |
| chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0); |
| |
| chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(nullptr); // Use default device name (CHIP-XXXX) |
| |
| #if CONFIG_NETWORK_LAYER_BLE |
| chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(LinuxDeviceOptions::GetInstance().mBleDevice, false); |
| #endif |
| |
| chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true); |
| |
| LightingMgr().Init(); |
| |
| // Init ZCL Data Model and CHIP App Server |
| InitServer(); |
| |
| chip::DeviceLayer::PlatformMgr().RunEventLoop(); |
| |
| exit: |
| if (err != CHIP_NO_ERROR) |
| { |
| std::cerr << "Failed to run Linux Bridge App: " << ErrorStr(err) << std::endl; |
| // End the program with non zero error code to indicate a error. |
| return 1; |
| } |
| return 0; |
| } |