1 /*
2 * Copyright (c) 2020, 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 OpenThread Link Metrics API.
32 */
33
34 #include "openthread-core-config.h"
35
36 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE
37
38 #include "instance/instance.hpp"
39
40 using namespace ot;
41
otLinkMetricsQuery(otInstance * aInstance,const otIp6Address * aDestination,uint8_t aSeriesId,const otLinkMetrics * aLinkMetricsFlags,otLinkMetricsReportCallback aCallback,void * aCallbackContext)42 otError otLinkMetricsQuery(otInstance *aInstance,
43 const otIp6Address *aDestination,
44 uint8_t aSeriesId,
45 const otLinkMetrics *aLinkMetricsFlags,
46 otLinkMetricsReportCallback aCallback,
47 void *aCallbackContext)
48 {
49 AsCoreType(aInstance).Get<LinkMetrics::Initiator>().SetReportCallback(aCallback, aCallbackContext);
50
51 return AsCoreType(aInstance).Get<LinkMetrics::Initiator>().Query(AsCoreType(aDestination), aSeriesId,
52 AsCoreTypePtr(aLinkMetricsFlags));
53 }
54
otLinkMetricsConfigForwardTrackingSeries(otInstance * aInstance,const otIp6Address * aDestination,uint8_t aSeriesId,const otLinkMetricsSeriesFlags aSeriesFlags,const otLinkMetrics * aLinkMetricsFlags,otLinkMetricsMgmtResponseCallback aCallback,void * aCallbackContext)55 otError otLinkMetricsConfigForwardTrackingSeries(otInstance *aInstance,
56 const otIp6Address *aDestination,
57 uint8_t aSeriesId,
58 const otLinkMetricsSeriesFlags aSeriesFlags,
59 const otLinkMetrics *aLinkMetricsFlags,
60 otLinkMetricsMgmtResponseCallback aCallback,
61 void *aCallbackContext)
62 {
63 LinkMetrics::Initiator &initiator = AsCoreType(aInstance).Get<LinkMetrics::Initiator>();
64
65 initiator.SetMgmtResponseCallback(aCallback, aCallbackContext);
66
67 return initiator.SendMgmtRequestForwardTrackingSeries(AsCoreType(aDestination), aSeriesId,
68 AsCoreType(&aSeriesFlags), AsCoreTypePtr(aLinkMetricsFlags));
69 }
70
otLinkMetricsConfigEnhAckProbing(otInstance * aInstance,const otIp6Address * aDestination,otLinkMetricsEnhAckFlags aEnhAckFlags,const otLinkMetrics * aLinkMetricsFlags,otLinkMetricsMgmtResponseCallback aCallback,void * aCallbackContext,otLinkMetricsEnhAckProbingIeReportCallback aEnhAckCallback,void * aEnhAckCallbackContext)71 otError otLinkMetricsConfigEnhAckProbing(otInstance *aInstance,
72 const otIp6Address *aDestination,
73 otLinkMetricsEnhAckFlags aEnhAckFlags,
74 const otLinkMetrics *aLinkMetricsFlags,
75 otLinkMetricsMgmtResponseCallback aCallback,
76 void *aCallbackContext,
77 otLinkMetricsEnhAckProbingIeReportCallback aEnhAckCallback,
78 void *aEnhAckCallbackContext)
79 {
80 LinkMetrics::Initiator &initiator = AsCoreType(aInstance).Get<LinkMetrics::Initiator>();
81
82 initiator.SetMgmtResponseCallback(aCallback, aCallbackContext);
83 initiator.SetEnhAckProbingCallback(aEnhAckCallback, aEnhAckCallbackContext);
84
85 return initiator.SendMgmtRequestEnhAckProbing(AsCoreType(aDestination), MapEnum(aEnhAckFlags),
86 AsCoreTypePtr(aLinkMetricsFlags));
87 }
88
otLinkMetricsSendLinkProbe(otInstance * aInstance,const otIp6Address * aDestination,uint8_t aSeriesId,uint8_t aLength)89 otError otLinkMetricsSendLinkProbe(otInstance *aInstance,
90 const otIp6Address *aDestination,
91 uint8_t aSeriesId,
92 uint8_t aLength)
93 {
94 LinkMetrics::Initiator &initiator = AsCoreType(aInstance).Get<LinkMetrics::Initiator>();
95
96 return initiator.SendLinkProbe(AsCoreType(aDestination), aSeriesId, aLength);
97 }
98
99 #if OPENTHREAD_CONFIG_LINK_METRICS_MANAGER_ENABLE
otLinkMetricsManagerIsEnabled(otInstance * aInstance)100 bool otLinkMetricsManagerIsEnabled(otInstance *aInstance)
101 {
102 return AsCoreType(aInstance).Get<Utils::LinkMetricsManager>().IsEnabled();
103 }
104
otLinkMetricsManagerSetEnabled(otInstance * aInstance,bool aEnable)105 void otLinkMetricsManagerSetEnabled(otInstance *aInstance, bool aEnable)
106 {
107 AsCoreType(aInstance).Get<Utils::LinkMetricsManager>().SetEnabled(aEnable);
108 }
109
otLinkMetricsManagerGetMetricsValueByExtAddr(otInstance * aInstance,const otExtAddress * aExtAddress,otLinkMetricsValues * aLinkMetricsValues)110 otError otLinkMetricsManagerGetMetricsValueByExtAddr(otInstance *aInstance,
111 const otExtAddress *aExtAddress,
112 otLinkMetricsValues *aLinkMetricsValues)
113 {
114 otError error = OT_ERROR_NONE;
115
116 VerifyOrExit(aExtAddress != nullptr && aLinkMetricsValues != nullptr, error = OT_ERROR_INVALID_ARGS);
117
118 error = AsCoreType(aInstance).Get<Utils::LinkMetricsManager>().GetLinkMetricsValueByExtAddr(
119 AsCoreType(aExtAddress), AsCoreType(aLinkMetricsValues));
120 exit:
121 return error;
122 }
123 #endif
124
125 #endif // OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE
126