• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "thread/uri_paths.hpp"
47 
48 namespace ot {
49 
50 RegisterLogModule("EnergyScanSrv");
51 
EnergyScanServer(Instance & aInstance)52 EnergyScanServer::EnergyScanServer(Instance &aInstance)
53     : InstanceLocator(aInstance)
54     , mChannelMask(0)
55     , mChannelMaskCurrent(0)
56     , mPeriod(0)
57     , mScanDuration(0)
58     , mCount(0)
59     , mActive(false)
60     , mScanResultsLength(0)
61     , mTimer(aInstance, EnergyScanServer::HandleTimer)
62     , mEnergyScan(UriPath::kEnergyScan, &EnergyScanServer::HandleRequest, this)
63 {
64     Get<Tmf::Agent>().AddResource(mEnergyScan);
65 }
66 
HandleRequest(void * aContext,otMessage * aMessage,const otMessageInfo * aMessageInfo)67 void EnergyScanServer::HandleRequest(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
68 {
69     static_cast<EnergyScanServer *>(aContext)->HandleRequest(AsCoapMessage(aMessage), AsCoreType(aMessageInfo));
70 }
71 
HandleRequest(Coap::Message & aMessage,const Ip6::MessageInfo & aMessageInfo)72 void EnergyScanServer::HandleRequest(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
73 {
74     uint8_t          count;
75     uint16_t         period;
76     uint16_t         scanDuration;
77     Ip6::MessageInfo responseInfo(aMessageInfo);
78     uint32_t         mask;
79 
80     VerifyOrExit(aMessage.IsPostRequest());
81 
82     SuccessOrExit(Tlv::Find<MeshCoP::CountTlv>(aMessage, count));
83     SuccessOrExit(Tlv::Find<MeshCoP::PeriodTlv>(aMessage, period));
84     SuccessOrExit(Tlv::Find<MeshCoP::ScanDurationTlv>(aMessage, scanDuration));
85 
86     VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0);
87 
88     mChannelMask        = mask;
89     mChannelMaskCurrent = mChannelMask;
90     mCount              = count;
91     mPeriod             = period;
92     mScanDuration       = scanDuration;
93     mScanResultsLength  = 0;
94     mActive             = true;
95     mTimer.Start(kScanDelay);
96 
97     mCommissioner = aMessageInfo.GetPeerAddr();
98 
99     if (aMessage.IsConfirmable() && !aMessageInfo.GetSockAddr().IsMulticast())
100     {
101         SuccessOrExit(Get<Tmf::Agent>().SendEmptyAck(aMessage, responseInfo));
102         LogInfo("sent energy scan query response");
103     }
104 
105 exit:
106     return;
107 }
108 
HandleTimer(Timer & aTimer)109 void EnergyScanServer::HandleTimer(Timer &aTimer)
110 {
111     aTimer.Get<EnergyScanServer>().HandleTimer();
112 }
113 
HandleTimer(void)114 void EnergyScanServer::HandleTimer(void)
115 {
116     VerifyOrExit(mActive);
117 
118     if (mCount)
119     {
120         // grab the lowest channel to scan
121         uint32_t channelMask = mChannelMaskCurrent & ~(mChannelMaskCurrent - 1);
122         IgnoreError(Get<Mac::Mac>().EnergyScan(channelMask, mScanDuration, HandleScanResult, this));
123     }
124     else
125     {
126         SendReport();
127     }
128 
129 exit:
130     return;
131 }
132 
HandleScanResult(Mac::EnergyScanResult * aResult,void * aContext)133 void EnergyScanServer::HandleScanResult(Mac::EnergyScanResult *aResult, void *aContext)
134 {
135     static_cast<EnergyScanServer *>(aContext)->HandleScanResult(aResult);
136 }
137 
HandleScanResult(Mac::EnergyScanResult * aResult)138 void EnergyScanServer::HandleScanResult(Mac::EnergyScanResult *aResult)
139 {
140     VerifyOrExit(mActive);
141 
142     if (aResult)
143     {
144         VerifyOrExit(mScanResultsLength < OPENTHREAD_CONFIG_TMF_ENERGY_SCAN_MAX_RESULTS);
145         mScanResults[mScanResultsLength++] = aResult->mMaxRssi;
146     }
147     else
148     {
149         // clear the lowest channel to scan
150         mChannelMaskCurrent &= mChannelMaskCurrent - 1;
151 
152         if (mChannelMaskCurrent == 0)
153         {
154             mChannelMaskCurrent = mChannelMask;
155             mCount--;
156         }
157 
158         if (mCount)
159         {
160             mTimer.Start(mPeriod);
161         }
162         else
163         {
164             mTimer.Start(kReportDelay);
165         }
166     }
167 
168 exit:
169     return;
170 }
171 
SendReport(void)172 void EnergyScanServer::SendReport(void)
173 {
174     Error                   error = kErrorNone;
175     MeshCoP::ChannelMaskTlv channelMask;
176     MeshCoP::EnergyListTlv  energyList;
177     Tmf::MessageInfo        messageInfo(GetInstance());
178     Coap::Message *         message;
179 
180     message = Get<Tmf::Agent>().NewPriorityConfirmablePostMessage(UriPath::kEnergyReport);
181     VerifyOrExit(message != nullptr, error = kErrorNoBufs);
182 
183     channelMask.Init();
184     channelMask.SetChannelMask(mChannelMask);
185     SuccessOrExit(error = channelMask.AppendTo(*message));
186 
187     energyList.Init();
188     energyList.SetLength(mScanResultsLength);
189     SuccessOrExit(error = message->Append(energyList));
190     SuccessOrExit(error = message->AppendBytes(mScanResults, mScanResultsLength));
191 
192     messageInfo.SetSockAddrToRlocPeerAddrTo(mCommissioner);
193 
194     SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, messageInfo));
195 
196     LogInfo("sent scan results");
197 
198 exit:
199     FreeMessageOnError(message, error);
200     MeshCoP::LogError("send scan results", error);
201     mActive = false;
202 }
203 
HandleNotifierEvents(Events aEvents)204 void EnergyScanServer::HandleNotifierEvents(Events aEvents)
205 {
206     if (aEvents.Contains(kEventThreadNetdataChanged) && !mActive &&
207         Get<NetworkData::Leader>().GetCommissioningData() == nullptr)
208     {
209         mActive = false;
210         mTimer.Stop();
211     }
212 }
213 
214 } // namespace ot
215