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 "LowpanBeaconInfo" 18 19 #include <android/net/lowpan/LowpanBeaconInfo.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::LowpanBeaconInfo; 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 49 LowpanBeaconInfo::Builder::Builder() { 50 } 51 52 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setName(const std::string& value) { 53 mIdentityBuilder.setName(value); 54 return *this; 55 } 56 57 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setType(const std::string& value) { 58 mIdentityBuilder.setType(value); 59 return *this; 60 } 61 62 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setType(const ::android::String16& value) { 63 mIdentityBuilder.setType(value); 64 return *this; 65 } 66 67 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setXpanid(const std::vector<uint8_t>& value) { 68 mIdentityBuilder.setXpanid(value); 69 return *this; 70 } 71 72 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setXpanid(const uint8_t* valuePtr, int32_t valueLen) { 73 mIdentityBuilder.setXpanid(valuePtr, valueLen); 74 return *this; 75 } 76 77 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setPanid(int32_t value) { 78 mIdentityBuilder.setPanid(value); 79 return *this; 80 } 81 82 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setChannel(int32_t value) { 83 mIdentityBuilder.setChannel(value); 84 return *this; 85 } 86 87 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setLowpanIdentity(const LowpanIdentity& value) { 88 mIdentityBuilder.setLowpanIdentity(value); 89 return *this; 90 } 91 92 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setRssi(int32_t value) { 93 mRssi = value; 94 return *this; 95 } 96 97 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setLqi(int32_t value) { 98 mLqi = value; 99 return *this; 100 } 101 102 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setBeaconAddress(const std::vector<uint8_t>& value) { 103 mBeaconAddress = value; 104 return *this; 105 } 106 107 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setBeaconAddress(const uint8_t* valuePtr, int32_t valueLen) { 108 mBeaconAddress.clear(); 109 mBeaconAddress.insert(mBeaconAddress.end(), valuePtr, valuePtr + valueLen); 110 return *this; 111 } 112 113 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::setFlag(int32_t value) { 114 mFlags.insert(value); 115 return *this; 116 } 117 118 LowpanBeaconInfo::Builder& LowpanBeaconInfo::Builder::clearFlag(int32_t value) { 119 mFlags.erase(value); 120 return *this; 121 } 122 123 LowpanBeaconInfo LowpanBeaconInfo::Builder::build(void) const { 124 return LowpanBeaconInfo(*this); 125 } 126 127 LowpanBeaconInfo::LowpanBeaconInfo(const LowpanBeaconInfo::Builder& builder) : 128 mIdentity(builder.mIdentityBuilder.build()), 129 mRssi(builder.mRssi), 130 mLqi(builder.mLqi), 131 mBeaconAddress(builder.mBeaconAddress), 132 mFlags(builder.mFlags) 133 { 134 } 135 136 status_t LowpanBeaconInfo::writeToParcel(Parcel* parcel) const { 137 /* 138 * Keep implementation in sync with writeToParcel() in 139 * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanBeaconInfo.java. 140 */ 141 142 RETURN_IF_FAILED(mIdentity.writeToParcel(parcel)); 143 RETURN_IF_FAILED(parcel->writeInt32(mRssi)); 144 RETURN_IF_FAILED(parcel->writeInt32(mLqi)); 145 RETURN_IF_FAILED(parcel->writeByteVector(mBeaconAddress)); 146 RETURN_IF_FAILED(parcel->writeInt32(mFlags.size())); 147 148 std::set<int32_t>::const_iterator iter; 149 std::set<int32_t>::const_iterator end = mFlags.end(); 150 151 for (iter = mFlags.begin(); iter != end; ++iter) { 152 RETURN_IF_FAILED(parcel->writeInt32(*iter)); 153 } 154 155 return NO_ERROR; 156 } 157 158 status_t LowpanBeaconInfo::readFromParcel(const Parcel* parcel) { 159 /* 160 * Keep implementation in sync with readFromParcel() in 161 * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanBeaconInfo.java. 162 */ 163 164 RETURN_IF_FAILED(mIdentity.readFromParcel(parcel)); 165 RETURN_IF_FAILED(parcel->readInt32(&mRssi)); 166 RETURN_IF_FAILED(parcel->readInt32(&mLqi)); 167 RETURN_IF_FAILED(parcel->readByteVector(&mBeaconAddress)); 168 169 int32_t flagCount = 0; 170 171 RETURN_IF_FAILED(parcel->readInt32(&flagCount)); 172 173 if (flagCount < 0) { 174 ALOGE("Bad flag count"); 175 return BAD_VALUE; 176 } 177 178 mFlags.clear(); 179 180 while (flagCount--) { 181 int32_t flag = 0; 182 RETURN_IF_FAILED(parcel->readInt32(&flag)); 183 mFlags.insert(flag); 184 } 185 186 return NO_ERROR; 187 } 188 189 bool LowpanBeaconInfo::operator==(const LowpanBeaconInfo& rhs) 190 { 191 if (mIdentity != rhs.mIdentity) { 192 return false; 193 } 194 195 if (mRssi != rhs.mRssi) { 196 return false; 197 } 198 199 if (mLqi != rhs.mLqi) { 200 return false; 201 } 202 203 if (mBeaconAddress != rhs.mBeaconAddress) { 204 return false; 205 } 206 207 if (mFlags != rhs.mFlags) { 208 return false; 209 } 210 211 return true; 212 } 213 214 } // namespace lowpan 215 216 } // namespace net 217 218 } // namespace android 219