1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker * Copyright (c) 2022, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker * All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker *
5*cfb92d14SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker * 1. Redistributions of source code must strain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker * 2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker * notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker * documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker * 3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker * names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker * derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker *
16*cfb92d14SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker */
28*cfb92d14SAndroid Build Coastguard Worker
29*cfb92d14SAndroid Build Coastguard Worker #include "power.hpp"
30*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
31*cfb92d14SAndroid Build Coastguard Worker #include "utils/parse_cmdline.hpp"
32*cfb92d14SAndroid Build Coastguard Worker
33*cfb92d14SAndroid Build Coastguard Worker namespace ot {
34*cfb92d14SAndroid Build Coastguard Worker namespace Power {
35*cfb92d14SAndroid Build Coastguard Worker
Set(const char * aDomain)36*cfb92d14SAndroid Build Coastguard Worker otError Domain::Set(const char *aDomain)
37*cfb92d14SAndroid Build Coastguard Worker {
38*cfb92d14SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
39*cfb92d14SAndroid Build Coastguard Worker uint16_t length = static_cast<uint16_t>(strlen(aDomain));
40*cfb92d14SAndroid Build Coastguard Worker
41*cfb92d14SAndroid Build Coastguard Worker VerifyOrExit(length <= kDomainSize, error = OT_ERROR_INVALID_ARGS);
42*cfb92d14SAndroid Build Coastguard Worker memcpy(m8, aDomain, length);
43*cfb92d14SAndroid Build Coastguard Worker m8[length] = '\0';
44*cfb92d14SAndroid Build Coastguard Worker
45*cfb92d14SAndroid Build Coastguard Worker exit:
46*cfb92d14SAndroid Build Coastguard Worker return error;
47*cfb92d14SAndroid Build Coastguard Worker }
48*cfb92d14SAndroid Build Coastguard Worker
FromString(char * aString)49*cfb92d14SAndroid Build Coastguard Worker otError TargetPower::FromString(char *aString)
50*cfb92d14SAndroid Build Coastguard Worker {
51*cfb92d14SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
52*cfb92d14SAndroid Build Coastguard Worker char *str;
53*cfb92d14SAndroid Build Coastguard Worker char *psave;
54*cfb92d14SAndroid Build Coastguard Worker
55*cfb92d14SAndroid Build Coastguard Worker VerifyOrExit((str = strtok_r(aString, ",", &psave)) != nullptr, error = OT_ERROR_PARSE);
56*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelStart));
57*cfb92d14SAndroid Build Coastguard Worker
58*cfb92d14SAndroid Build Coastguard Worker VerifyOrExit((str = strtok_r(nullptr, ",", &psave)) != nullptr, error = OT_ERROR_PARSE);
59*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelEnd));
60*cfb92d14SAndroid Build Coastguard Worker
61*cfb92d14SAndroid Build Coastguard Worker VerifyOrExit((str = strtok_r(nullptr, ",", &psave)) != nullptr, error = OT_ERROR_PARSE);
62*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = Utils::CmdLineParser::ParseAsInt16(str, mTargetPower));
63*cfb92d14SAndroid Build Coastguard Worker
64*cfb92d14SAndroid Build Coastguard Worker exit:
65*cfb92d14SAndroid Build Coastguard Worker return error;
66*cfb92d14SAndroid Build Coastguard Worker }
67*cfb92d14SAndroid Build Coastguard Worker
ToString(void) const68*cfb92d14SAndroid Build Coastguard Worker TargetPower::InfoString TargetPower::ToString(void) const
69*cfb92d14SAndroid Build Coastguard Worker {
70*cfb92d14SAndroid Build Coastguard Worker InfoString string;
71*cfb92d14SAndroid Build Coastguard Worker
72*cfb92d14SAndroid Build Coastguard Worker string.Append("%u,%u,%d", mChannelStart, mChannelEnd, mTargetPower);
73*cfb92d14SAndroid Build Coastguard Worker
74*cfb92d14SAndroid Build Coastguard Worker return string;
75*cfb92d14SAndroid Build Coastguard Worker }
76*cfb92d14SAndroid Build Coastguard Worker
Set(const char * aRawPowerSetting)77*cfb92d14SAndroid Build Coastguard Worker otError RawPowerSetting::Set(const char *aRawPowerSetting)
78*cfb92d14SAndroid Build Coastguard Worker {
79*cfb92d14SAndroid Build Coastguard Worker otError error;
80*cfb92d14SAndroid Build Coastguard Worker uint16_t length = sizeof(mData);
81*cfb92d14SAndroid Build Coastguard Worker
82*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = ot::Utils::CmdLineParser::ParseAsHexString(aRawPowerSetting, length, mData));
83*cfb92d14SAndroid Build Coastguard Worker mLength = static_cast<uint8_t>(length);
84*cfb92d14SAndroid Build Coastguard Worker
85*cfb92d14SAndroid Build Coastguard Worker exit:
86*cfb92d14SAndroid Build Coastguard Worker return error;
87*cfb92d14SAndroid Build Coastguard Worker }
88*cfb92d14SAndroid Build Coastguard Worker
ToString(void) const89*cfb92d14SAndroid Build Coastguard Worker RawPowerSetting::InfoString RawPowerSetting::ToString(void) const
90*cfb92d14SAndroid Build Coastguard Worker {
91*cfb92d14SAndroid Build Coastguard Worker InfoString string;
92*cfb92d14SAndroid Build Coastguard Worker
93*cfb92d14SAndroid Build Coastguard Worker string.AppendHexBytes(mData, mLength);
94*cfb92d14SAndroid Build Coastguard Worker
95*cfb92d14SAndroid Build Coastguard Worker return string;
96*cfb92d14SAndroid Build Coastguard Worker }
97*cfb92d14SAndroid Build Coastguard Worker
FromString(char * aString)98*cfb92d14SAndroid Build Coastguard Worker otError CalibratedPower::FromString(char *aString)
99*cfb92d14SAndroid Build Coastguard Worker {
100*cfb92d14SAndroid Build Coastguard Worker otError error = OT_ERROR_NONE;
101*cfb92d14SAndroid Build Coastguard Worker char *str;
102*cfb92d14SAndroid Build Coastguard Worker char *pSave;
103*cfb92d14SAndroid Build Coastguard Worker
104*cfb92d14SAndroid Build Coastguard Worker VerifyOrExit((str = strtok_r(aString, ",", &pSave)) != nullptr, error = OT_ERROR_PARSE);
105*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelStart));
106*cfb92d14SAndroid Build Coastguard Worker
107*cfb92d14SAndroid Build Coastguard Worker VerifyOrExit((str = strtok_r(nullptr, ",", &pSave)) != nullptr, error = OT_ERROR_PARSE);
108*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint8(str, mChannelEnd));
109*cfb92d14SAndroid Build Coastguard Worker
110*cfb92d14SAndroid Build Coastguard Worker VerifyOrExit((str = strtok_r(nullptr, ",", &pSave)) != nullptr, error = OT_ERROR_PARSE);
111*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = Utils::CmdLineParser::ParseAsInt16(str, mActualPower));
112*cfb92d14SAndroid Build Coastguard Worker SuccessOrExit(error = mRawPowerSetting.Set(pSave));
113*cfb92d14SAndroid Build Coastguard Worker
114*cfb92d14SAndroid Build Coastguard Worker exit:
115*cfb92d14SAndroid Build Coastguard Worker return error;
116*cfb92d14SAndroid Build Coastguard Worker }
117*cfb92d14SAndroid Build Coastguard Worker
ToString(void) const118*cfb92d14SAndroid Build Coastguard Worker CalibratedPower::InfoString CalibratedPower::ToString(void) const
119*cfb92d14SAndroid Build Coastguard Worker {
120*cfb92d14SAndroid Build Coastguard Worker InfoString string;
121*cfb92d14SAndroid Build Coastguard Worker
122*cfb92d14SAndroid Build Coastguard Worker string.Append("%u,%u,%d,%s", mChannelStart, mChannelEnd, mActualPower, mRawPowerSetting.ToString().AsCString());
123*cfb92d14SAndroid Build Coastguard Worker
124*cfb92d14SAndroid Build Coastguard Worker return string;
125*cfb92d14SAndroid Build Coastguard Worker }
126*cfb92d14SAndroid Build Coastguard Worker } // namespace Power
127*cfb92d14SAndroid Build Coastguard Worker } // namespace ot
128