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.
18 #include "RendezvousServer.h"
20 #include "SessionManager.h"
21 #include <core/CHIPError.h>
22 #include <support/CodeUtils.h>
23 #include <transport/SecureSessionMgr.h>
25 #if CHIP_ENABLE_OPENTHREAD
26 #include <platform/ThreadStackManager.h>
28 #include <lib/mdns/DiscoveryManager.h>
30 using namespace ::chip::Inet;
31 using namespace ::chip::Transport;
32 using namespace ::chip::DeviceLayer;
36 RendezvousServer::RendezvousServer() : mRendezvousSession(this) {}
38 CHIP_ERROR RendezvousServer::Init(const RendezvousParameters & params, TransportMgrBase * transportMgr)
40 return mRendezvousSession.Init(params, transportMgr);
43 void RendezvousServer::OnRendezvousError(CHIP_ERROR err)
45 ChipLogProgress(AppServer, "OnRendezvousError: %s", ErrorStr(err));
48 void RendezvousServer::OnRendezvousConnectionOpened()
50 ChipLogProgress(AppServer, "OnRendezvousConnectionOpened");
53 void RendezvousServer::OnRendezvousConnectionClosed()
55 ChipLogProgress(AppServer, "OnRendezvousConnectionClosed");
58 void RendezvousServer::OnRendezvousMessageReceived(const PacketHeader & packetHeader, const PeerAddress & peerAddress,
59 System::PacketBufferHandle buffer)
62 void RendezvousServer::OnRendezvousComplete()
64 ChipLogProgress(AppServer, "Device completed Rendezvous process");
65 if (mRendezvousSession.GetLocalNodeId().HasValue() && mRendezvousSession.GetRemoteNodeId().HasValue())
67 SessionManager().NewPairing(Optional<Transport::PeerAddress>{}, mRendezvousSession.GetRemoteNodeId().Value(),
68 &mRendezvousSession.GetPairingSession());
72 ChipLogError(AppServer, "Commissioner did not assign a node ID to the device!!!");
76 void RendezvousServer::OnRendezvousStatusUpdate(Status status, CHIP_ERROR err)
78 VerifyOrExit(err == CHIP_NO_ERROR, ChipLogError(AppServer, "OnRendezvousStatusUpdate: %s", chip::ErrorStr(err)));
82 case RendezvousSessionDelegate::SecurePairingSuccess:
83 ChipLogProgress(AppServer, "Device completed SPAKE2+ handshake");
84 if (mDelegate != nullptr)
86 mDelegate->OnRendezvousStarted();
90 case RendezvousSessionDelegate::SecurePairingFailed:
91 ChipLogProgress(AppServer, "Failed in SPAKE2+ handshake");
92 if (mDelegate != nullptr)
94 mDelegate->OnRendezvousStopped();
98 case RendezvousSessionDelegate::NetworkProvisioningSuccess:
99 ChipLogProgress(AppServer, "Device was assigned network credentials");
100 chip::Mdns::DiscoveryManager::GetInstance().StartPublishDevice();
101 if (mDelegate != nullptr)
103 mDelegate->OnRendezvousStopped();
107 case RendezvousSessionDelegate::NetworkProvisioningFailed:
108 ChipLogProgress(AppServer, "Failed in network provisioning");
109 if (mDelegate != nullptr)
111 mDelegate->OnRendezvousStopped();