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 Energy Scan Server.
32 */
33
34 #include "energy_scan_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
47 namespace ot {
48
49 RegisterLogModule("EnergyScanSrv");
50
EnergyScanServer(Instance & aInstance)51 EnergyScanServer::EnergyScanServer(Instance &aInstance)
52 : InstanceLocator(aInstance)
53 , mChannelMask(0)
54 , mChannelMaskCurrent(0)
55 , mPeriod(0)
56 , mScanDuration(0)
57 , mCount(0)
58 , mReportMessage(nullptr)
59 , mTimer(aInstance)
60 {
61 }
62
63 template <>
HandleTmf(Coap::Message & aMessage,const Ip6::MessageInfo & aMessageInfo)64 void EnergyScanServer::HandleTmf<kUriEnergyScan>(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
65 {
66 uint8_t count;
67 uint16_t period;
68 uint16_t scanDuration;
69 uint32_t mask;
70 MeshCoP::Tlv tlv;
71 MeshCoP::ChannelMaskTlv channelMaskTlv;
72
73 VerifyOrExit(aMessage.IsPostRequest());
74
75 SuccessOrExit(Tlv::Find<MeshCoP::CountTlv>(aMessage, count));
76 SuccessOrExit(Tlv::Find<MeshCoP::PeriodTlv>(aMessage, period));
77 SuccessOrExit(Tlv::Find<MeshCoP::ScanDurationTlv>(aMessage, scanDuration));
78
79 VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0);
80
81 FreeMessage(mReportMessage);
82 mReportMessage = Get<Tmf::Agent>().NewPriorityConfirmablePostMessage(kUriEnergyReport);
83 VerifyOrExit(mReportMessage != nullptr);
84
85 channelMaskTlv.Init();
86 channelMaskTlv.SetChannelMask(mask);
87 SuccessOrExit(channelMaskTlv.AppendTo(*mReportMessage));
88
89 tlv.SetType(MeshCoP::Tlv::kEnergyList);
90 SuccessOrExit(mReportMessage->Append(tlv));
91
92 mNumScanResults = 0;
93 mChannelMask = mask;
94 mChannelMaskCurrent = mChannelMask;
95 mCount = count;
96 mPeriod = period;
97 mScanDuration = scanDuration;
98 mTimer.Start(kScanDelay);
99
100 mCommissioner = aMessageInfo.GetPeerAddr();
101
102 if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast())
103 {
104 SuccessOrExit(Get<Tmf::Agent>().SendEmptyAck(aMessage, aMessageInfo));
105 LogInfo("Sent %s ack", UriToString<kUriEnergyScan>());
106 }
107
108 exit:
109 return;
110 }
111
HandleTimer(void)112 void EnergyScanServer::HandleTimer(void)
113 {
114 VerifyOrExit(mReportMessage != nullptr);
115
116 if (mCount)
117 {
118 // grab the lowest channel to scan
119 uint32_t channelMask = mChannelMaskCurrent & ~(mChannelMaskCurrent - 1);
120
121 IgnoreError(Get<Mac::Mac>().EnergyScan(channelMask, mScanDuration, HandleScanResult, this));
122 }
123 else
124 {
125 SendReport();
126 }
127
128 exit:
129 return;
130 }
131
HandleScanResult(Mac::EnergyScanResult * aResult,void * aContext)132 void EnergyScanServer::HandleScanResult(Mac::EnergyScanResult *aResult, void *aContext)
133 {
134 static_cast<EnergyScanServer *>(aContext)->HandleScanResult(aResult);
135 }
136
HandleScanResult(Mac::EnergyScanResult * aResult)137 void EnergyScanServer::HandleScanResult(Mac::EnergyScanResult *aResult)
138 {
139 VerifyOrExit(mReportMessage != nullptr);
140
141 if (aResult)
142 {
143 if (mReportMessage->Append<int8_t>(aResult->mMaxRssi) != kErrorNone)
144 {
145 FreeMessage(mReportMessage);
146 mReportMessage = nullptr;
147 ExitNow();
148 }
149
150 mNumScanResults++;
151
152 if (mNumScanResults == NumericLimits<uint8_t>::kMax)
153 {
154 // If we reach the max length that fit in the Energy List
155 // TLV we send the current set of energy scan data.
156
157 mCount = 0;
158 mTimer.Start(kReportDelay);
159 }
160 }
161 else
162 {
163 // Clear the lowest channel to scan
164 mChannelMaskCurrent &= mChannelMaskCurrent - 1;
165
166 if (mChannelMaskCurrent == 0)
167 {
168 mChannelMaskCurrent = mChannelMask;
169 mCount--;
170 }
171
172 if (mCount)
173 {
174 mTimer.Start(mPeriod);
175 }
176 else
177 {
178 mTimer.Start(kReportDelay);
179 }
180 }
181
182 exit:
183 return;
184 }
185
SendReport(void)186 void EnergyScanServer::SendReport(void)
187 {
188 Error error = kErrorNone;
189 Tmf::MessageInfo messageInfo(GetInstance());
190 uint16_t offset;
191
192 // Update the Energy List TLV length in Report message
193 offset = mReportMessage->GetLength() - mNumScanResults - sizeof(uint8_t);
194 mReportMessage->Write(offset, mNumScanResults);
195
196 messageInfo.SetSockAddrToRlocPeerAddrTo(mCommissioner);
197
198 SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*mReportMessage, messageInfo));
199
200 LogInfo("Sent %s", UriToString<kUriEnergyReport>());
201
202 exit:
203 FreeMessageOnError(mReportMessage, error);
204 MeshCoP::LogError("send scan results", error);
205 mReportMessage = nullptr;
206 }
207
HandleNotifierEvents(Events aEvents)208 void EnergyScanServer::HandleNotifierEvents(Events aEvents)
209 {
210 if (aEvents.Contains(kEventThreadNetdataChanged) && (mReportMessage != nullptr) &&
211 Get<NetworkData::Leader>().GetCommissioningData() == nullptr)
212 {
213 mReportMessage->Free();
214 mReportMessage = nullptr;
215 mTimer.Stop();
216 }
217 }
218
219 } // namespace ot
220