Change script for apply upstream code
[platform/upstream/connectedhomeip.git] / examples / common / chip-app-server / RendezvousServer.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 #include "RendezvousServer.h"
19
20 #include "SessionManager.h"
21 #include <core/CHIPError.h>
22 #include <support/CodeUtils.h>
23 #include <transport/SecureSessionMgr.h>
24
25 #if CHIP_ENABLE_OPENTHREAD
26 #include <platform/ThreadStackManager.h>
27 #endif
28 #include <lib/mdns/DiscoveryManager.h>
29
30 using namespace ::chip::Inet;
31 using namespace ::chip::Transport;
32 using namespace ::chip::DeviceLayer;
33
34 namespace chip {
35
36 RendezvousServer::RendezvousServer() : mRendezvousSession(this) {}
37
38 CHIP_ERROR RendezvousServer::Init(const RendezvousParameters & params, TransportMgrBase * transportMgr)
39 {
40     return mRendezvousSession.Init(params, transportMgr);
41 }
42
43 void RendezvousServer::OnRendezvousError(CHIP_ERROR err)
44 {
45     ChipLogProgress(AppServer, "OnRendezvousError: %s", ErrorStr(err));
46 }
47
48 void RendezvousServer::OnRendezvousConnectionOpened()
49 {
50     ChipLogProgress(AppServer, "OnRendezvousConnectionOpened");
51 }
52
53 void RendezvousServer::OnRendezvousConnectionClosed()
54 {
55     ChipLogProgress(AppServer, "OnRendezvousConnectionClosed");
56 }
57
58 void RendezvousServer::OnRendezvousMessageReceived(const PacketHeader & packetHeader, const PeerAddress & peerAddress,
59                                                    System::PacketBufferHandle buffer)
60 {}
61
62 void RendezvousServer::OnRendezvousComplete()
63 {
64     ChipLogProgress(AppServer, "Device completed Rendezvous process");
65     if (mRendezvousSession.GetLocalNodeId().HasValue() && mRendezvousSession.GetRemoteNodeId().HasValue())
66     {
67         SessionManager().NewPairing(Optional<Transport::PeerAddress>{}, mRendezvousSession.GetRemoteNodeId().Value(),
68                                     &mRendezvousSession.GetPairingSession());
69     }
70     else
71     {
72         ChipLogError(AppServer, "Commissioner did not assign a node ID to the device!!!");
73     }
74 }
75
76 void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err)
77 {
78     VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(AppServer, "OnRendezvousStatusUpdate: %s", chip::ErrorStr(err)));
79
80     switch (status)
81     {
82     case RendezvousSessionDelegate::SecurePairingSuccess:
83         ChipLogProgress(AppServer, "Device completed SPAKE2+ handshake");
84         if (mDelegate != nullptr)
85         {
86             mDelegate->OnRendezvousStarted();
87         }
88         break;
89
90     case RendezvousSessionDelegate::SecurePairingFailed:
91         ChipLogProgress(AppServer, "Failed in SPAKE2+ handshake");
92         if (mDelegate != nullptr)
93         {
94             mDelegate->OnRendezvousStopped();
95         }
96         break;
97
98     case RendezvousSessionDelegate::NetworkProvisioningSuccess:
99         ChipLogProgress(AppServer, "Device was assigned network credentials");
100         chip::Mdns::DiscoveryManager::GetInstance().StartPublishDevice();
101         if (mDelegate != nullptr)
102         {
103             mDelegate->OnRendezvousStopped();
104         }
105         break;
106
107     case RendezvousSessionDelegate::NetworkProvisioningFailed:
108         ChipLogProgress(AppServer, "Failed in network provisioning");
109         if (mDelegate != nullptr)
110         {
111             mDelegate->OnRendezvousStopped();
112         }
113         break;
114
115     default:
116         break;
117     };
118
119 exit:
120     return;
121 }
122 } // namespace chip