3 * Copyright (c) 2021 Project CHIP Authors
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 #include <platform/CHIPDeviceLayer.h>
20 #include <platform/PlatformManager.h>
23 #include "gen/attribute-id.h"
24 #include "gen/cluster-id.h"
25 #include <app/chip-zcl-zpro-codec.h>
26 #include <app/util/af-types.h>
27 #include <app/util/attribute-storage.h>
28 #include <app/util/util.h>
29 #include <core/CHIPError.h>
30 #include <setup_payload/QRCodeSetupPayloadGenerator.h>
31 #include <setup_payload/SetupPayload.h>
32 #include <support/CHIPMem.h>
33 #include <support/RandUtils.h>
35 #include "LightingManager.h"
43 using namespace chip::Inet;
44 using namespace chip::Transport;
45 using namespace chip::DeviceLayer;
47 void emberAfPostAttributeChangeCallback(EndpointId endpoint, ClusterId clusterId, AttributeId attributeId, uint8_t mask,
48 uint16_t manufacturerCode, uint8_t type, uint8_t size, uint8_t * value)
50 if (clusterId != ZCL_ON_OFF_CLUSTER_ID)
52 ChipLogProgress(Zcl, "Unknown cluster ID: %d", clusterId);
56 if (attributeId != ZCL_ON_OFF_ATTRIBUTE_ID)
58 ChipLogProgress(Zcl, "Unknown attribute ID: %d", attributeId);
64 LightingMgr().InitiateAction(LightingManager::ON_ACTION);
68 LightingMgr().InitiateAction(LightingManager::OFF_ACTION);
72 /** @brief OnOff Cluster Init
74 * This function is called when a specific cluster is initialized. It gives the
75 * application an opportunity to take care of cluster initialization procedures.
76 * It is called exactly once for each endpoint where cluster is present.
78 * @param endpoint Ver.: always
81 * emberAfOnOffClusterInitCallback happens before the stack initialize the cluster
82 * attributes to the default value.
83 * The logic here expects something similar to the deprecated Plugins callback
84 * emberAfPluginOnOffClusterServerPostInitCallback.
87 void emberAfOnOffClusterInitCallback(EndpointId endpoint)
89 // TODO: implement any additional Cluster Server init actions
93 void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
96 if (event->Type == chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished)
98 ChipLogProgress(DeviceLayer, "Receive kCHIPoBLEConnectionEstablished");
102 CHIP_ERROR PrintQRCodeContent()
104 CHIP_ERROR err = CHIP_NO_ERROR;
105 // If we do not have a discriminator, generate one
106 chip::SetupPayload payload;
107 uint32_t setUpPINCode;
108 uint16_t setUpDiscriminator;
113 err = ConfigurationMgr().GetSetupPinCode(setUpPINCode);
116 err = ConfigurationMgr().GetSetupDiscriminator(setUpDiscriminator);
119 err = ConfigurationMgr().GetVendorId(vendorId);
122 err = ConfigurationMgr().GetProductId(productId);
126 payload.vendorID = vendorId;
127 payload.productID = productId;
128 payload.setUpPINCode = setUpPINCode;
129 payload.discriminator = setUpDiscriminator;
131 // Wrap it so SuccessOrExit can work
133 chip::QRCodeSetupPayloadGenerator generator(payload);
134 err = generator.payloadBase41Representation(result);
138 std::cout << "SetupPINCode: [" << setUpPINCode << "]" << std::endl;
139 // There might be whitespace in setup QRCode, add brackets to make it clearer.
140 std::cout << "SetupQRCode: [" << result << "]" << std::endl;
143 if (err != CHIP_NO_ERROR)
145 std::cerr << "Failed to generate QR Code: " << ErrorStr(err) << std::endl;
151 int main(int argc, char * argv[])
153 CHIP_ERROR err = CHIP_NO_ERROR;
155 err = chip::Platform::MemoryInit();
158 err = ParseArguments(argc, argv);
161 err = chip::DeviceLayer::PlatformMgr().InitChipStack();
164 err = PrintQRCodeContent();
167 chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0);
169 chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(nullptr); // Use default device name (CHIP-XXXX)
171 #if CONFIG_NETWORK_LAYER_BLE
172 chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(LinuxDeviceOptions::GetInstance().mBleDevice, false);
175 chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true);
177 LightingMgr().Init();
179 // Init ZCL Data Model and CHIP App Server
182 chip::DeviceLayer::PlatformMgr().RunEventLoop();
185 if (err != CHIP_NO_ERROR)
187 std::cerr << "Failed to run Linux Bridge App: " << ErrorStr(err) << std::endl;
188 // End the program with non zero error code to indicate a error.