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/locator_getters.hpp"
41 #include "common/log.hpp"
42 #include "instance/instance.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)
57 {
58 }
59 
60 template <>
HandleTmf(Coap::Message & aMessage,const Ip6::MessageInfo & aMessageInfo)61 void PanIdQueryServer::HandleTmf<kUriPanIdQuery>(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
62 {
63     uint16_t panId;
64     uint32_t mask;
65 
66     VerifyOrExit(aMessage.IsPostRequest());
67     SuccessOrExit(MeshCoP::ChannelMaskTlv::FindIn(aMessage, mask));
68 
69     SuccessOrExit(Tlv::Find<MeshCoP::PanIdTlv>(aMessage, panId));
70 
71     mChannelMask  = mask;
72     mCommissioner = aMessageInfo.GetPeerAddr();
73     mPanId        = panId;
74     mTimer.Start(kScanDelay);
75 
76     if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast())
77     {
78         SuccessOrExit(Get<Tmf::Agent>().SendEmptyAck(aMessage, aMessageInfo));
79         LogInfo("Sent %s ack", UriToString<kUriPanIdQuery>());
80     }
81 
82 exit:
83     return;
84 }
85 
HandleScanResult(Mac::ActiveScanResult * aScanResult,void * aContext)86 void PanIdQueryServer::HandleScanResult(Mac::ActiveScanResult *aScanResult, void *aContext)
87 {
88     static_cast<PanIdQueryServer *>(aContext)->HandleScanResult(aScanResult);
89 }
90 
HandleScanResult(Mac::ActiveScanResult * aScanResult)91 void PanIdQueryServer::HandleScanResult(Mac::ActiveScanResult *aScanResult)
92 {
93     if (aScanResult != nullptr)
94     {
95         if (aScanResult->mPanId == mPanId)
96         {
97             mChannelMask |= 1 << aScanResult->mChannel;
98         }
99     }
100     else if (mChannelMask != 0)
101     {
102         SendConflict();
103     }
104 }
105 
SendConflict(void)106 void PanIdQueryServer::SendConflict(void)
107 {
108     Error            error = kErrorNone;
109     Tmf::MessageInfo messageInfo(GetInstance());
110     Coap::Message   *message;
111 
112     message = Get<Tmf::Agent>().NewPriorityConfirmablePostMessage(kUriPanIdConflict);
113     VerifyOrExit(message != nullptr, error = kErrorNoBufs);
114 
115     SuccessOrExit(error = MeshCoP::ChannelMaskTlv::AppendTo(*message, mChannelMask));
116 
117     SuccessOrExit(error = Tlv::Append<MeshCoP::PanIdTlv>(*message, mPanId));
118 
119     messageInfo.SetSockAddrToRlocPeerAddrTo(mCommissioner);
120 
121     SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, messageInfo));
122 
123     LogInfo("Sent %s", UriToString<kUriPanIdConflict>());
124 
125 exit:
126     FreeMessageOnError(message, error);
127     LogWarnOnError(error, "send panid conflict");
128 }
129 
HandleTimer(void)130 void PanIdQueryServer::HandleTimer(void)
131 {
132     IgnoreError(Get<Mac::Mac>().ActiveScan(mChannelMask, 0, HandleScanResult, this));
133     mChannelMask = 0;
134 }
135 
136 } // namespace ot
137