1 /*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "LowpanIdentity"
18
19 #include <android/net/lowpan/LowpanIdentity.h>
20
21 #include <binder/Parcel.h>
22 #include <log/log.h>
23 #include <utils/Errors.h>
24
25 using android::BAD_TYPE;
26 using android::BAD_VALUE;
27 using android::NO_ERROR;
28 using android::Parcel;
29 using android::status_t;
30 using android::UNEXPECTED_NULL;
31 using android::net::lowpan::LowpanIdentity;
32 using namespace ::android::binder;
33
34 namespace android {
35
36 namespace net {
37
38 namespace lowpan {
39
40 #define RETURN_IF_FAILED(calledOnce) \
41 { \
42 status_t returnStatus = calledOnce; \
43 if (returnStatus) { \
44 ALOGE("Failed at %s:%d (%s)", __FILE__, __LINE__, __func__); \
45 return returnStatus; \
46 } \
47 }
48
getName(std::string * value) const49 bool LowpanIdentity::getName(std::string* value) const {
50 if (value != NULL) {
51 *value = mName;
52 }
53 return true;
54 }
getType(std::string * value) const55 bool LowpanIdentity::getType(std::string* value) const {
56 if (value != NULL) {
57 *value = mType;
58 }
59 return true;
60 }
getXpanid(std::vector<uint8_t> * value) const61 bool LowpanIdentity::getXpanid(std::vector<uint8_t>* value) const {
62 if (value != NULL) {
63 *value = mXpanid;
64 }
65 return true;
66 }
getPanid(void) const67 int32_t LowpanIdentity::getPanid(void) const {
68 return mPanid;
69 }
getChannel(void) const70 int32_t LowpanIdentity::getChannel(void) const {
71 return mChannel;
72 }
73
Builder()74 LowpanIdentity::Builder::Builder() {
75 }
76
setName(const std::string & value)77 LowpanIdentity::Builder& LowpanIdentity::Builder::setName(const std::string& value) {
78 mIdentity.mName = value;
79 return *this;
80 }
81
setType(const std::string & value)82 LowpanIdentity::Builder& LowpanIdentity::Builder::setType(const std::string& value) {
83 mIdentity.mType = value;
84 return *this;
85 }
86
setType(const::android::String16 & value)87 LowpanIdentity::Builder& LowpanIdentity::Builder::setType(const ::android::String16& value) {
88 return setType(String8(value).string());
89 }
90
setXpanid(const std::vector<uint8_t> & value)91 LowpanIdentity::Builder& LowpanIdentity::Builder::setXpanid(const std::vector<uint8_t>& value) {
92 mIdentity.mXpanid = value;
93 return *this;
94 }
95
setXpanid(const uint8_t * valuePtr,int32_t valueLen)96 LowpanIdentity::Builder& LowpanIdentity::Builder::setXpanid(const uint8_t* valuePtr, int32_t valueLen) {
97 mIdentity.mXpanid.clear();
98 mIdentity.mXpanid.insert(mIdentity.mXpanid.end(), valuePtr, valuePtr + valueLen);
99 return *this;
100 }
101
setPanid(int32_t value)102 LowpanIdentity::Builder& LowpanIdentity::Builder::setPanid(int32_t value) {
103 mIdentity.mPanid = value;
104 return *this;
105 }
106
setChannel(int32_t value)107 LowpanIdentity::Builder& LowpanIdentity::Builder::setChannel(int32_t value) {
108 mIdentity.mChannel = value;
109 return *this;
110 }
111
setLowpanIdentity(const LowpanIdentity & value)112 LowpanIdentity::Builder& LowpanIdentity::Builder::setLowpanIdentity(const LowpanIdentity& value) {
113 mIdentity = value;
114 return *this;
115 }
116
build(void) const117 LowpanIdentity LowpanIdentity::Builder::build(void) const {
118 return mIdentity;
119 }
120
LowpanIdentity()121 LowpanIdentity::LowpanIdentity() : mPanid(UNSPECIFIED_PANID), mChannel(UNSPECIFIED_CHANNEL) {
122 }
123
writeToParcel(Parcel * parcel) const124 status_t LowpanIdentity::writeToParcel(Parcel* parcel) const {
125 /*
126 * Keep implementation in sync with writeToParcel() in
127 * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanIdentity.java.
128 */
129
130 std::vector<int8_t> rawName(mName.begin(), mName.end());
131
132 RETURN_IF_FAILED(parcel->writeByteVector(rawName));
133 RETURN_IF_FAILED(parcel->writeUtf8AsUtf16(mType));
134 RETURN_IF_FAILED(parcel->writeByteVector(mXpanid));
135 RETURN_IF_FAILED(parcel->writeInt32(mPanid));
136 RETURN_IF_FAILED(parcel->writeInt32(mChannel));
137 return NO_ERROR;
138 }
139
readFromParcel(const Parcel * parcel)140 status_t LowpanIdentity::readFromParcel(const Parcel* parcel) {
141 /*
142 * Keep implementation in sync with readFromParcel() in
143 * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanIdentity.java.
144 */
145
146 std::vector<int8_t> rawName;
147
148 RETURN_IF_FAILED(parcel->readByteVector(&rawName));
149
150 mName = std::string((const char*)&rawName.front(), rawName.size());
151
152 RETURN_IF_FAILED(parcel->readUtf8FromUtf16(&mType));
153 RETURN_IF_FAILED(parcel->readByteVector(&mXpanid));
154 RETURN_IF_FAILED(parcel->readInt32(&mPanid));
155 RETURN_IF_FAILED(parcel->readInt32(&mChannel));
156 return NO_ERROR;
157 }
158
operator ==(const LowpanIdentity & rhs)159 bool LowpanIdentity::operator==(const LowpanIdentity& rhs)
160 {
161 const LowpanIdentity& lhs = *this;
162
163 if (lhs.mName != rhs.mName) {
164 return false;
165 }
166
167 if (lhs.mType != rhs.mType) {
168 return false;
169 }
170
171 if (lhs.mXpanid != rhs.mXpanid) {
172 return false;
173 }
174
175 if (lhs.mPanid != rhs.mPanid) {
176 return false;
177 }
178
179 if (lhs.mChannel != rhs.mChannel) {
180 return false;
181 }
182 return true;
183 }
184
185 } // namespace lowpan
186
187 } // namespace net
188
189 } // namespace android
190