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