bde7daf76109079bbc2fe900a935295c346754ec
[platform/upstream/connectedhomeip.git] / src / app / tests / integration / chip_im_responder.cpp
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  *    @file
20  *      This file implements a chip-im-responder, for the
21  *      CHIP Interaction Data Model Protocol.
22  *
23  *      Currently it provides simple command handler with sample cluster and command
24  *
25  */
26
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>
33
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>
40
41 namespace chip {
42 namespace app {
43
44 void DispatchSingleClusterCommand(chip::ClusterId aClusterId, chip::CommandId aCommandId, chip::EndpointId aEndPointId,
45                                   chip::TLV::TLVReader & aReader, Command * apCommandObj)
46 {
47     CHIP_ERROR err = CHIP_NO_ERROR;
48
49     if (aClusterId != kTestClusterId || aCommandId != kTestCommandId || aEndPointId != kTestEndPointId)
50     {
51         return;
52     }
53
54     if (aReader.GetLength() != 0)
55     {
56         chip::TLV::Debug::Dump(aReader, TLVPrettyPrinter);
57     }
58
59     chip::app::Command::CommandParams commandParams = { kTestEndPointId, // Endpoint
60                                                         kTestGroupId,    // GroupId
61                                                         kTestClusterId,  // ClusterId
62                                                         kTestCommandId,  // CommandId
63                                                         (chip::app::Command::kCommandPathFlag_EndpointIdValid) };
64
65     // Add command data here
66
67     uint8_t effectIdentifier = 1; // Dying light
68     uint8_t effectVariant    = 1;
69
70     chip::TLV::TLVType dummyType = chip::TLV::kTLVType_NotSpecified;
71
72     chip::TLV::TLVWriter writer = apCommandObj->CreateCommandDataElementTLVWriter();
73
74     printf("responder constructing response");
75     err = writer.StartContainer(chip::TLV::AnonymousTag, chip::TLV::kTLVType_Structure, dummyType);
76     SuccessOrExit(err);
77
78     err = writer.Put(chip::TLV::ContextTag(1), effectIdentifier);
79     SuccessOrExit(err);
80
81     err = writer.Put(chip::TLV::ContextTag(2), effectVariant);
82     SuccessOrExit(err);
83
84     err = writer.EndContainer(dummyType);
85     SuccessOrExit(err);
86
87     err = writer.Finalize();
88     SuccessOrExit(err);
89
90     err = apCommandObj->AddCommand(commandParams);
91     SuccessOrExit(err);
92
93 exit:
94     return;
95 }
96 } // namespace app
97 } // namespace chip
98
99 namespace {
100
101 // The CommandHandler object
102 chip::TransportMgr<chip::Transport::UDP> gTransportManager;
103 chip::SecureSessionMgr gSessionManager;
104 chip::SecurePairingUsingTestSecret gTestPairing;
105
106 } // namespace
107
108 int main(int argc, char * argv[])
109 {
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);
115
116     VerifyOrExit(adminInfo != nullptr, err = CHIP_ERROR_NO_MEMORY);
117
118     InitializeChip();
119
120     err = gTransportManager.Init(
121         chip::Transport::UdpListenParameters(&chip::DeviceLayer::InetLayer).SetAddressType(chip::Inet::kIPAddressType_IPv4));
122     SuccessOrExit(err);
123
124     err = gSessionManager.Init(chip::kTestDeviceNodeId, &chip::DeviceLayer::SystemLayer, &gTransportManager, &admins);
125     SuccessOrExit(err);
126
127     err = gExchangeManager.Init(chip::kTestDeviceNodeId, &gTransportManager, &gSessionManager);
128     SuccessOrExit(err);
129
130     err = chip::app::InteractionModelEngine::GetInstance()->Init(&gExchangeManager);
131     SuccessOrExit(err);
132
133     err = gSessionManager.NewPairing(peer, chip::kTestControllerNodeId, &gTestPairing,
134                                      chip::SecureSessionMgr::PairingDirection::kResponder, gAdminId);
135     SuccessOrExit(err);
136
137     printf("Listening for IM requests...\n");
138
139     chip::DeviceLayer::PlatformMgr().RunEventLoop();
140
141 exit:
142
143     if (err != CHIP_NO_ERROR)
144     {
145         printf("CommandHandler failed, err:%s\n", chip::ErrorStr(err));
146         exit(EXIT_FAILURE);
147     }
148
149     chip::app::InteractionModelEngine::GetInstance()->Shutdown();
150
151     ShutdownChip();
152
153     return EXIT_SUCCESS;
154 }