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/as_core_type.hpp"
38 #include "common/code_utils.hpp"
39 #include "common/debug.hpp"
40 #include "common/instance.hpp"
41 #include "common/locator_getters.hpp"
42 #include "common/log.hpp"
43 #include "meshcop/meshcop.hpp"
44 #include "meshcop/meshcop_tlvs.hpp"
45 #include "thread/thread_netif.hpp"
46 #include "thread/uri_paths.hpp"
47
48 namespace ot {
49
50 RegisterLogModule("MeshCoP");
51
PanIdQueryServer(Instance & aInstance)52 PanIdQueryServer::PanIdQueryServer(Instance &aInstance)
53 : InstanceLocator(aInstance)
54 , mChannelMask(0)
55 , mPanId(Mac::kPanIdBroadcast)
56 , mTimer(aInstance, PanIdQueryServer::HandleTimer)
57 , mPanIdQuery(UriPath::kPanIdQuery, &PanIdQueryServer::HandleQuery, this)
58 {
59 Get<Tmf::Agent>().AddResource(mPanIdQuery);
60 }
61
HandleQuery(void * aContext,otMessage * aMessage,const otMessageInfo * aMessageInfo)62 void PanIdQueryServer::HandleQuery(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
63 {
64 static_cast<PanIdQueryServer *>(aContext)->HandleQuery(AsCoapMessage(aMessage), AsCoreType(aMessageInfo));
65 }
66
HandleQuery(Coap::Message & aMessage,const Ip6::MessageInfo & aMessageInfo)67 void PanIdQueryServer::HandleQuery(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
68 {
69 uint16_t panId;
70 Ip6::MessageInfo responseInfo(aMessageInfo);
71 uint32_t mask;
72
73 VerifyOrExit(aMessage.IsPostRequest());
74 VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0);
75
76 SuccessOrExit(Tlv::Find<MeshCoP::PanIdTlv>(aMessage, panId));
77
78 mChannelMask = mask;
79 mCommissioner = aMessageInfo.GetPeerAddr();
80 mPanId = panId;
81 mTimer.Start(kScanDelay);
82
83 if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast())
84 {
85 SuccessOrExit(Get<Tmf::Agent>().SendEmptyAck(aMessage, responseInfo));
86 LogInfo("sent panid query response");
87 }
88
89 exit:
90 return;
91 }
92
HandleScanResult(Mac::ActiveScanResult * aScanResult,void * aContext)93 void PanIdQueryServer::HandleScanResult(Mac::ActiveScanResult *aScanResult, void *aContext)
94 {
95 static_cast<PanIdQueryServer *>(aContext)->HandleScanResult(aScanResult);
96 }
97
HandleScanResult(Mac::ActiveScanResult * aScanResult)98 void PanIdQueryServer::HandleScanResult(Mac::ActiveScanResult *aScanResult)
99 {
100 if (aScanResult != nullptr)
101 {
102 if (aScanResult->mPanId == mPanId)
103 {
104 mChannelMask |= 1 << aScanResult->mChannel;
105 }
106 }
107 else if (mChannelMask != 0)
108 {
109 SendConflict();
110 }
111 }
112
SendConflict(void)113 void PanIdQueryServer::SendConflict(void)
114 {
115 Error error = kErrorNone;
116 MeshCoP::ChannelMaskTlv channelMask;
117 Tmf::MessageInfo messageInfo(GetInstance());
118 Coap::Message * message;
119
120 message = Get<Tmf::Agent>().NewPriorityConfirmablePostMessage(UriPath::kPanIdConflict);
121 VerifyOrExit(message != nullptr, error = kErrorNoBufs);
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.SetSockAddrToRlocPeerAddrTo(mCommissioner);
130
131 SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, messageInfo));
132
133 LogInfo("sent panid conflict");
134
135 exit:
136 FreeMessageOnError(message, error);
137 MeshCoP::LogError("send panid conflict", error);
138 }
139
HandleTimer(Timer & aTimer)140 void PanIdQueryServer::HandleTimer(Timer &aTimer)
141 {
142 aTimer.Get<PanIdQueryServer>().HandleTimer();
143 }
144
HandleTimer(void)145 void PanIdQueryServer::HandleTimer(void)
146 {
147 IgnoreError(Get<Mac::Mac>().ActiveScan(mChannelMask, 0, HandleScanResult, this));
148 mChannelMask = 0;
149 }
150
151 } // namespace ot
152