xref: /aosp_15_r20/external/openthread/src/posix/platform/power.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1 /*
2  *  Copyright (c) 2022, 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 strain 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 #include "power.hpp"
30 #include "common/code_utils.hpp"
31 #include "utils/parse_cmdline.hpp"
32 
33 namespace ot {
34 namespace Power {
35 
Set(const char * aDomain)36 otError Domain::Set(const char *aDomain)
37 {
38     otError  error  = OT_ERROR_NONE;
39     uint16_t length = static_cast<uint16_t>(strlen(aDomain));
40 
41     VerifyOrExit(length <= kDomainSize, error = OT_ERROR_INVALID_ARGS);
42     memcpy(m8, aDomain, length);
43     m8[length] = '\0';
44 
45 exit:
46     return error;
47 }
48 
FromString(char * aString)49 otError TargetPower::FromString(char *aString)
50 {
51     otError error = OT_ERROR_NONE;
52     char   *str;
53     char   *psave;
54 
55     VerifyOrExit((str = strtok_r(aString, ",", &psave)) != nullptr, error = OT_ERROR_PARSE);
56     SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelStart));
57 
58     VerifyOrExit((str = strtok_r(nullptr, ",", &psave)) != nullptr, error = OT_ERROR_PARSE);
59     SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelEnd));
60 
61     VerifyOrExit((str = strtok_r(nullptr, ",", &psave)) != nullptr, error = OT_ERROR_PARSE);
62     SuccessOrExit(error = Utils::CmdLineParser::ParseAsInt16(str, mTargetPower));
63 
64 exit:
65     return error;
66 }
67 
ToString(void) const68 TargetPower::InfoString TargetPower::ToString(void) const
69 {
70     InfoString string;
71 
72     string.Append("%u,%u,%d", mChannelStart, mChannelEnd, mTargetPower);
73 
74     return string;
75 }
76 
Set(const char * aRawPowerSetting)77 otError RawPowerSetting::Set(const char *aRawPowerSetting)
78 {
79     otError  error;
80     uint16_t length = sizeof(mData);
81 
82     SuccessOrExit(error = ot::Utils::CmdLineParser::ParseAsHexString(aRawPowerSetting, length, mData));
83     mLength = static_cast<uint8_t>(length);
84 
85 exit:
86     return error;
87 }
88 
ToString(void) const89 RawPowerSetting::InfoString RawPowerSetting::ToString(void) const
90 {
91     InfoString string;
92 
93     string.AppendHexBytes(mData, mLength);
94 
95     return string;
96 }
97 
FromString(char * aString)98 otError CalibratedPower::FromString(char *aString)
99 {
100     otError error = OT_ERROR_NONE;
101     char   *str;
102     char   *pSave;
103 
104     VerifyOrExit((str = strtok_r(aString, ",", &pSave)) != nullptr, error = OT_ERROR_PARSE);
105     SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelStart));
106 
107     VerifyOrExit((str = strtok_r(nullptr, ",", &pSave)) != nullptr, error = OT_ERROR_PARSE);
108     SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelEnd));
109 
110     VerifyOrExit((str = strtok_r(nullptr, ",", &pSave)) != nullptr, error = OT_ERROR_PARSE);
111     SuccessOrExit(error = Utils::CmdLineParser::ParseAsInt16(str, mActualPower));
112     SuccessOrExit(error = mRawPowerSetting.Set(pSave));
113 
114 exit:
115     return error;
116 }
117 
ToString(void) const118 CalibratedPower::InfoString CalibratedPower::ToString(void) const
119 {
120     InfoString string;
121 
122     string.Append("%u,%u,%d,%s", mChannelStart, mChannelEnd, mActualPower, mRawPowerSetting.ToString().AsCString());
123 
124     return string;
125 }
126 } // namespace Power
127 } // namespace ot
128