3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * This file implements a chip-im-responder, for the
21 * CHIP Interaction Data Model Protocol.
23 * Currently it provides simple command handler with sample cluster and command
27 #include "app/InteractionModelEngine.h"
28 #include <app/CommandHandler.h>
29 #include <app/CommandSender.h>
30 #include <app/tests/integration/common.h>
31 #include <core/CHIPCore.h>
32 #include <platform/CHIPDeviceLayer.h>
34 #include "InteractionModelEngine.h"
35 #include <support/ErrorStr.h>
36 #include <system/SystemPacketBuffer.h>
37 #include <transport/PASESession.h>
38 #include <transport/SecureSessionMgr.h>
39 #include <transport/raw/UDP.h>
44 void DispatchSingleClusterCommand(chip::ClusterId aClusterId, chip::CommandId aCommandId, chip::EndpointId aEndPointId,
45 chip::TLV::TLVReader & aReader, Command * apCommandObj)
47 CHIP_ERROR err = CHIP_NO_ERROR;
49 if (aClusterId != kTestClusterId || aCommandId != kTestCommandId || aEndPointId != kTestEndPointId)
54 if (aReader.GetLength() != 0)
56 chip::TLV::Debug::Dump(aReader, TLVPrettyPrinter);
59 chip::app::Command::CommandParams commandParams = { kTestEndPointId, // Endpoint
60 kTestGroupId, // GroupId
61 kTestClusterId, // ClusterId
62 kTestCommandId, // CommandId
63 (chip::app::Command::kCommandPathFlag_EndpointIdValid) };
65 // Add command data here
67 uint8_t effectIdentifier = 1; // Dying light
68 uint8_t effectVariant = 1;
70 chip::TLV::TLVType dummyType = chip::TLV::kTLVType_NotSpecified;
72 chip::TLV::TLVWriter writer = apCommandObj->CreateCommandDataElementTLVWriter();
74 printf("responder constructing response");
75 err = writer.StartContainer(chip::TLV::AnonymousTag, chip::TLV::kTLVType_Structure, dummyType);
78 err = writer.Put(chip::TLV::ContextTag(1), effectIdentifier);
81 err = writer.Put(chip::TLV::ContextTag(2), effectVariant);
84 err = writer.EndContainer(dummyType);
87 err = writer.Finalize();
90 err = apCommandObj->AddCommand(commandParams);
101 // The CommandHandler object
102 chip::TransportMgr<chip::Transport::UDP> gTransportManager;
103 chip::SecureSessionMgr gSessionManager;
104 chip::SecurePairingUsingTestSecret gTestPairing;
108 int main(int argc, char * argv[])
110 CHIP_ERROR err = CHIP_NO_ERROR;
111 chip::Optional<chip::Transport::PeerAddress> peer(chip::Transport::Type::kUndefined);
112 const chip::Transport::AdminId gAdminId = 0;
113 chip::Transport::AdminPairingTable admins;
114 chip::Transport::AdminPairingInfo * adminInfo = admins.AssignAdminId(gAdminId, chip::kTestDeviceNodeId);
116 VerifyOrExit(adminInfo != nullptr, err = CHIP_ERROR_NO_MEMORY);
120 err = gTransportManager.Init(
121 chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::kIPAddressType_IPv4));
124 err = gSessionManager.Init(chip::kTestDeviceNodeId, &chip::DeviceLayer::SystemLayer, &gTransportManager, &admins);
127 err = gExchangeManager.Init(chip::kTestDeviceNodeId, &gTransportManager, &gSessionManager);
130 err = chip::app::InteractionModelEngine::GetInstance()->Init(&gExchangeManager);
133 err = gSessionManager.NewPairing(peer, chip::kTestControllerNodeId, &gTestPairing,
134 chip::SecureSessionMgr::PairingDirection::kResponder, gAdminId);
137 printf("Listening for IM requests...\n");
139 chip::DeviceLayer::PlatformMgr().RunEventLoop();
143 if (err != CHIP_NO_ERROR)
145 printf("CommandHandler failed, err:%s\n", chip::ErrorStr(err));
149 chip::app::InteractionModelEngine::GetInstance()->Shutdown();