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 Client.
32 */
33
34 #include "energy_scan_client.hpp"
35
36 #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD
37
38 #include "coap/coap_message.hpp"
39 #include "common/as_core_type.hpp"
40 #include "common/code_utils.hpp"
41 #include "common/debug.hpp"
42 #include "common/encoding.hpp"
43 #include "common/instance.hpp"
44 #include "common/locator_getters.hpp"
45 #include "common/log.hpp"
46 #include "meshcop/meshcop.hpp"
47 #include "meshcop/meshcop_tlvs.hpp"
48 #include "thread/thread_netif.hpp"
49 #include "thread/uri_paths.hpp"
50
51 namespace ot {
52
53 RegisterLogModule("EnergyScanClnt");
54
EnergyScanClient(Instance & aInstance)55 EnergyScanClient::EnergyScanClient(Instance &aInstance)
56 : InstanceLocator(aInstance)
57 , mCallback(nullptr)
58 , mContext(nullptr)
59 , mEnergyScan(UriPath::kEnergyReport, &EnergyScanClient::HandleReport, this)
60 {
61 Get<Tmf::Agent>().AddResource(mEnergyScan);
62 }
63
SendQuery(uint32_t aChannelMask,uint8_t aCount,uint16_t aPeriod,uint16_t aScanDuration,const Ip6::Address & aAddress,otCommissionerEnergyReportCallback aCallback,void * aContext)64 Error EnergyScanClient::SendQuery(uint32_t aChannelMask,
65 uint8_t aCount,
66 uint16_t aPeriod,
67 uint16_t aScanDuration,
68 const Ip6::Address & aAddress,
69 otCommissionerEnergyReportCallback aCallback,
70 void * aContext)
71 {
72 Error error = kErrorNone;
73 MeshCoP::ChannelMaskTlv channelMask;
74 Tmf::MessageInfo messageInfo(GetInstance());
75 Coap::Message * message = nullptr;
76
77 VerifyOrExit(Get<MeshCoP::Commissioner>().IsActive(), error = kErrorInvalidState);
78 VerifyOrExit((message = Get<Tmf::Agent>().NewPriorityMessage()) != nullptr, error = kErrorNoBufs);
79
80 SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kEnergyScan));
81 SuccessOrExit(error = message->SetPayloadMarker());
82
83 SuccessOrExit(
84 error = Tlv::Append<MeshCoP::CommissionerSessionIdTlv>(*message, Get<MeshCoP::Commissioner>().GetSessionId()));
85
86 channelMask.Init();
87 channelMask.SetChannelMask(aChannelMask);
88 SuccessOrExit(error = channelMask.AppendTo(*message));
89
90 SuccessOrExit(error = Tlv::Append<MeshCoP::CountTlv>(*message, aCount));
91 SuccessOrExit(error = Tlv::Append<MeshCoP::PeriodTlv>(*message, aPeriod));
92 SuccessOrExit(error = Tlv::Append<MeshCoP::ScanDurationTlv>(*message, aScanDuration));
93
94 messageInfo.SetSockAddrToRlocPeerAddrTo(aAddress);
95 SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, messageInfo));
96
97 LogInfo("sent query");
98
99 mCallback = aCallback;
100 mContext = aContext;
101
102 exit:
103 FreeMessageOnError(message, error);
104 return error;
105 }
106
HandleReport(void * aContext,otMessage * aMessage,const otMessageInfo * aMessageInfo)107 void EnergyScanClient::HandleReport(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
108 {
109 static_cast<EnergyScanClient *>(aContext)->HandleReport(AsCoapMessage(aMessage), AsCoreType(aMessageInfo));
110 }
111
HandleReport(Coap::Message & aMessage,const Ip6::MessageInfo & aMessageInfo)112 void EnergyScanClient::HandleReport(Coap::Message &aMessage, const Ip6::MessageInfo &aMessageInfo)
113 {
114 uint32_t mask;
115
116 OT_TOOL_PACKED_BEGIN
117 struct
118 {
119 MeshCoP::EnergyListTlv tlv;
120 uint8_t list[OPENTHREAD_CONFIG_TMF_ENERGY_SCAN_MAX_RESULTS];
121 } OT_TOOL_PACKED_END energyList;
122
123 VerifyOrExit(aMessage.IsConfirmablePostRequest());
124
125 LogInfo("received report");
126
127 VerifyOrExit((mask = MeshCoP::ChannelMaskTlv::GetChannelMask(aMessage)) != 0);
128
129 SuccessOrExit(MeshCoP::Tlv::FindTlv(aMessage, MeshCoP::Tlv::kEnergyList, sizeof(energyList), energyList.tlv));
130 VerifyOrExit(energyList.tlv.IsValid());
131
132 if (mCallback != nullptr)
133 {
134 mCallback(mask, energyList.list, energyList.tlv.GetLength(), mContext);
135 }
136
137 SuccessOrExit(Get<Tmf::Agent>().SendEmptyAck(aMessage, aMessageInfo));
138
139 LogInfo("sent report response");
140
141 exit:
142 return;
143 }
144
145 } // namespace ot
146
147 #endif // OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD
148