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/code_utils.hpp"
40 #include "common/debug.hpp"
41 #include "common/encoding.hpp"
42 #include "common/instance.hpp"
43 #include "common/locator_getters.hpp"
44 #include "common/logging.hpp"
45 #include "meshcop/meshcop.hpp"
46 #include "meshcop/meshcop_tlvs.hpp"
47 #include "thread/thread_netif.hpp"
48 #include "thread/uri_paths.hpp"
49
50 namespace ot {
51
EnergyScanClient(Instance & aInstance)52 EnergyScanClient::EnergyScanClient(Instance &aInstance)
53 : InstanceLocator(aInstance)
54 , mCallback(nullptr)
55 , mContext(nullptr)
56 , mEnergyScan(UriPath::kEnergyReport, &EnergyScanClient::HandleReport, this)
57 {
58 Get<Tmf::Agent>().AddResource(mEnergyScan);
59 }
60
SendQuery(uint32_t aChannelMask,uint8_t aCount,uint16_t aPeriod,uint16_t aScanDuration,const Ip6::Address & aAddress,otCommissionerEnergyReportCallback aCallback,void * aContext)61 Error EnergyScanClient::SendQuery(uint32_t aChannelMask,
62 uint8_t aCount,
63 uint16_t aPeriod,
64 uint16_t aScanDuration,
65 const Ip6::Address & aAddress,
66 otCommissionerEnergyReportCallback aCallback,
67 void * aContext)
68 {
69 Error error = kErrorNone;
70 MeshCoP::ChannelMaskTlv channelMask;
71 Ip6::MessageInfo messageInfo;
72 Coap::Message * message = nullptr;
73
74 VerifyOrExit(Get<MeshCoP::Commissioner>().IsActive(), error = kErrorInvalidState);
75 VerifyOrExit((message = MeshCoP::NewMeshCoPMessage(Get<Tmf::Agent>())) != nullptr, error = kErrorNoBufs);
76
77 SuccessOrExit(error = message->InitAsPost(aAddress, UriPath::kEnergyScan));
78 SuccessOrExit(error = message->SetPayloadMarker());
79
80 SuccessOrExit(
81 error = Tlv::Append<MeshCoP::CommissionerSessionIdTlv>(*message, Get<MeshCoP::Commissioner>().GetSessionId()));
82
83 channelMask.Init();
84 channelMask.SetChannelMask(aChannelMask);
85 SuccessOrExit(error = channelMask.AppendTo(*message));
86
87 SuccessOrExit(error = Tlv::Append<MeshCoP::CountTlv>(*message, aCount));
88 SuccessOrExit(error = Tlv::Append<MeshCoP::PeriodTlv>(*message, aPeriod));
89 SuccessOrExit(error = Tlv::Append<MeshCoP::ScanDurationTlv>(*message, aScanDuration));
90
91 messageInfo.SetSockAddr(Get<Mle::MleRouter>().GetMeshLocal16());
92 messageInfo.SetPeerAddr(aAddress);
93 messageInfo.SetPeerPort(Tmf::kUdpPort);
94 SuccessOrExit(error = Get<Tmf::Agent>().SendMessage(*message, messageInfo));
95
96 otLogInfoMeshCoP("sent energy scan query");
97
98 mCallback = aCallback;
99 mContext = aContext;
100
101 exit:
102 FreeMessageOnError(message, error);
103 return error;
104 }
105
HandleReport(void * aContext,otMessage * aMessage,const otMessageInfo * aMessageInfo)106 void EnergyScanClient::HandleReport(void *aContext, otMessage *aMessage, const otMessageInfo *aMessageInfo)
107 {
108 static_cast<EnergyScanClient *>(aContext)->HandleReport(*static_cast<Coap::Message *>(aMessage),
109 *static_cast<const Ip6::MessageInfo *>(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 otLogInfoMeshCoP("received energy scan 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 otLogInfoMeshCoP("sent energy scan report response");
140
141 exit:
142 return;
143 }
144
145 } // namespace ot
146
147 #endif // OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD
148