1 /*
2 * Copyright (c) 2016, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements the PAN ID Query Server.
32 */
33
34 #include "panid_query_server.hpp"
35
36 #include "coap/coap_message.hpp"
37 #include "common/code_utils.hpp"
38 #include "common/debug.hpp"
39 #include "common/instance.hpp"
40 #include "common/locator_getters.hpp"
41 #include "common/logging.hpp"
42 #include "meshcop/meshcop.hpp"
43 #include "meshcop/meshcop_tlvs.hpp"
44 #include "thread/thread_netif.hpp"
45 #include "thread/uri_paths.hpp"
46
47 namespace ot {
48
PanIdQueryServer(Instance & aInstance)49 PanIdQueryServer::PanIdQueryServer(Instance &aInstance)
50 : InstanceLocator(aInstance)
51 , mChannelMask(0)
52 , mPanId(Mac::kPanIdBroadcast)
53 , mTimer(aInstance, PanIdQueryServer::HandleTimer)
54 , mPanIdQuery(UriPath::kPanIdQuery, &PanIdQueryServer::HandleQuery, this)
55 {
56 Get<Tmf::Agent>().AddResource(mPanIdQuery);
57 }
58
HandleQuery(void * aContext,otMessage * aMessage,const otMessageInfo * aMessageInfo)59 void PanIdQueryServer::HandleQuery(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
60 {
61 static_cast<PanIdQueryServer *>(aContext)->HandleQuery(*static_cast<Coap::Message *>(aMessage),
62 *static_cast<const Ip6::MessageInfo *>(aMessageInfo));
63 }
64
HandleQuery(Coap::Message & aMessage,const Ip6::MessageInfo & aMessageInfo)65 void PanIdQueryServer::HandleQuery(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
66 {
67 uint16_t panId;
68 Ip6::MessageInfo responseInfo(aMessageInfo);
69 uint32_t mask;
70
71 VerifyOrExit(aMessage.IsPostRequest());
72 VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0);
73
74 SuccessOrExit(Tlv::Find<MeshCoP::PanIdTlv>(aMessage, panId));
75
76 mChannelMask = mask;
77 mCommissioner = aMessageInfo.GetPeerAddr();
78 mPanId = panId;
79 mTimer.Start(kScanDelay);
80
81 if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast())
82 {
83 SuccessOrExit(Get<Tmf::Agent>().SendEmptyAck(aMessage, responseInfo));
84 otLogInfoMeshCoP("sent panid query response");
85 }
86
87 exit:
88 return;
89 }
90
HandleScanResult(Mac::ActiveScanResult * aScanResult,void * aContext)91 void PanIdQueryServer::HandleScanResult(Mac::ActiveScanResult *aScanResult, void *aContext)
92 {
93 static_cast<PanIdQueryServer *>(aContext)->HandleScanResult(aScanResult);
94 }
95
HandleScanResult(Mac::ActiveScanResult * aScanResult)96 void PanIdQueryServer::HandleScanResult(Mac::ActiveScanResult *aScanResult)
97 {
98 if (aScanResult != nullptr)
99 {
100 if (aScanResult->mPanId == mPanId)
101 {
102 mChannelMask |= 1 << aScanResult->mChannel;
103 }
104 }
105 else if (mChannelMask != 0)
106 {
107 SendConflict();
108 }
109 }
110
SendConflict(void)111 void PanIdQueryServer::SendConflict(void)
112 {
113 Error error = kErrorNone;
114 MeshCoP::ChannelMaskTlv channelMask;
115 Ip6::MessageInfo messageInfo;
116 Coap::Message * message;
117
118 VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get<Tmf::Agent>())) != nullptr, error = kErrorNoBufs);
119
120 SuccessOrExit(error = message->InitAsConfirmablePost(UriPath::kPanIdConflict));
121 SuccessOrExit(error = message->SetPayloadMarker());
122
123 channelMask.Init();
124 channelMask.SetChannelMask(mChannelMask);
125 SuccessOrExit(error = channelMask.AppendTo(*message));
126
127 SuccessOrExit(error = Tlv::Append<MeshCoP::PanIdTlv>(*message, mPanId));
128
129 messageInfo.SetSockAddr(Get<Mle::MleRouter>().GetMeshLocal16());
130 messageInfo.SetPeerAddr(mCommissioner);
131 messageInfo.SetPeerPort(Tmf::kUdpPort);
132 SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, messageInfo));
133
134 otLogInfoMeshCoP("sent panid conflict");
135
136 exit:
137 FreeMessageOnError(message, error);
138 MeshCoP::LogError("send panid conflict", error);
139 }
140
HandleTimer(Timer & aTimer)141 void PanIdQueryServer::HandleTimer(Timer &aTimer)
142 {
143 aTimer.Get<PanIdQueryServer>().HandleTimer();
144 }
145
HandleTimer(void)146 void PanIdQueryServer::HandleTimer(void)
147 {
148 IgnoreError(Get<Mac::Mac>().ActiveScan(mChannelMask, 0, HandleScanResult, this));
149 mChannelMask = 0;
150 }
151
152 } // namespace ot
153