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