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 "LowpanProvision"
18 
19 #include <android/net/lowpan/LowpanProvision.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::LowpanProvision;
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     }
LowpanProvision(const LowpanIdentity & identity,const LowpanCredential & credential)48 LowpanProvision::LowpanProvision(const LowpanIdentity& identity, const LowpanCredential& credential)
49     : mIdentity(identity), mCredential(credential), mHasCredential(true)
50 {
51 }
52 
LowpanProvision(const LowpanIdentity & identity)53 LowpanProvision::LowpanProvision(const LowpanIdentity& identity)
54     : mIdentity(identity), mHasCredential(false)
55 {
56 }
57 
getLowpanIdentity() const58 const LowpanIdentity* LowpanProvision::getLowpanIdentity() const {
59     return &mIdentity;
60 }
61 
getLowpanCredential() const62 const LowpanCredential* LowpanProvision::getLowpanCredential() const {
63     return mHasCredential
64         ? &mCredential
65         : NULL;
66 }
67 
writeToParcel(Parcel * parcel) const68 status_t LowpanProvision::writeToParcel(Parcel* parcel) const {
69     /*
70      * Keep implementation in sync with writeToParcel() in
71      * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanProvision.java.
72      */
73 
74     RETURN_IF_FAILED(mIdentity.writeToParcel(parcel));
75     RETURN_IF_FAILED(parcel->writeBool(mHasCredential));
76 
77     if (mHasCredential) {
78         RETURN_IF_FAILED(mCredential.writeToParcel(parcel));
79     }
80 
81     return NO_ERROR;
82 }
83 
readFromParcel(const Parcel * parcel)84 status_t LowpanProvision::readFromParcel(const Parcel* parcel) {
85     /*
86      * Keep implementation in sync with readFromParcel() in
87      * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanProvision.java.
88      */
89 
90     RETURN_IF_FAILED(mIdentity.readFromParcel(parcel));
91     RETURN_IF_FAILED(parcel->readBool(&mHasCredential));
92 
93     if (mHasCredential) {
94         RETURN_IF_FAILED(mCredential.readFromParcel(parcel));
95     }
96 
97     return NO_ERROR;
98 }
99 
operator ==(const LowpanProvision & rhs)100 bool LowpanProvision::operator==(const LowpanProvision& rhs)
101 {
102     if (mIdentity != rhs.mIdentity) {
103         return false;
104     }
105 
106     if (mHasCredential != rhs.mHasCredential) {
107         return false;
108     }
109 
110     if (mHasCredential && mCredential != rhs.mCredential) {
111         return false;
112     }
113 
114     return true;
115 }
116 
117 }  // namespace lowpan
118 
119 }  // namespace net
120 
121 }  // namespace android
122