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