Home | History | Annotate | Download | only in libandroid_net_lowpan
      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 "LowpanCredential"
     18 
     19 #include <android/net/lowpan/LowpanCredential.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::LowpanCredential;
     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 LowpanCredential::LowpanCredential() : mMasterKeyIndex(UNSPECIFIED_MASTER_KEY_INDEX) { }
     50 
     51 status_t LowpanCredential::initMasterKey(LowpanCredential& out, const uint8_t* masterKeyBytes, int masterKeyLen, int masterKeyIndex)
     52 {
     53     if (masterKeyLen < 0) {
     54         return BAD_INDEX;
     55     } else if (masterKeyLen > MASTER_KEY_MAX_SIZE) {
     56         return BAD_INDEX;
     57     } else if (masterKeyBytes == NULL) {
     58         return BAD_VALUE;
     59     }
     60 
     61     out.mMasterKey.clear();
     62     out.mMasterKey.insert(out.mMasterKey.end(), masterKeyBytes, masterKeyBytes + masterKeyLen);
     63     out.mMasterKeyIndex = masterKeyIndex;
     64 
     65     return NO_ERROR;
     66 }
     67 
     68 status_t LowpanCredential::initMasterKey(LowpanCredential& out, const uint8_t* masterKeyBytes, int masterKeyLen)
     69 {
     70     return LowpanCredential::initMasterKey(out, masterKeyBytes, masterKeyLen, 0);
     71 }
     72 
     73 status_t LowpanCredential::initMasterKey(LowpanCredential& out, const std::vector<uint8_t>& masterKey, int masterKeyIndex)
     74 {
     75     return LowpanCredential::initMasterKey(out, &masterKey.front(), masterKey.size(), masterKeyIndex);
     76 }
     77 
     78 status_t LowpanCredential::initMasterKey(LowpanCredential& out, const std::vector<uint8_t>& masterKey)
     79 {
     80     return LowpanCredential::initMasterKey(out, masterKey, 0);
     81 }
     82 
     83 bool LowpanCredential::isMasterKey() const {
     84     return mMasterKey.size() > 0;
     85 }
     86 
     87 bool LowpanCredential::getMasterKey(std::vector<uint8_t>* masterKey) const {
     88     if (isMasterKey()) {
     89         *masterKey = mMasterKey;
     90         return true;
     91     }
     92     return false;
     93 }
     94 
     95 bool LowpanCredential::getMasterKey(const uint8_t** masterKey, int* masterKeyLen) const {
     96     if (isMasterKey()) {
     97         if (masterKey) {
     98             *masterKey = &mMasterKey.front();
     99         }
    100         if (masterKeyLen) {
    101             *masterKeyLen = mMasterKey.size();
    102         }
    103         return true;
    104     }
    105     return false;
    106 }
    107 
    108 int LowpanCredential::getMasterKeyIndex() const {
    109     return mMasterKeyIndex;
    110 }
    111 
    112 status_t LowpanCredential::writeToParcel(Parcel* parcel) const {
    113     /*
    114      * Keep implementation in sync with writeToParcel() in
    115      * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanCredential.java.
    116      */
    117     RETURN_IF_FAILED(parcel->writeByteVector(mMasterKey));
    118     RETURN_IF_FAILED(parcel->writeInt32(mMasterKeyIndex));
    119     return NO_ERROR;
    120 }
    121 
    122 status_t LowpanCredential::readFromParcel(const Parcel* parcel) {
    123     /*
    124      * Keep implementation in sync with readFromParcel() in
    125      * frameworks/base/lowpan/java/android/net/android/net/lowpan/LowpanCredential.java.
    126      */
    127     RETURN_IF_FAILED(parcel->readByteVector(&mMasterKey));
    128     RETURN_IF_FAILED(parcel->readInt32(&mMasterKeyIndex));
    129     return NO_ERROR;
    130 }
    131 
    132 bool LowpanCredential::operator==(const LowpanCredential& rhs)
    133 {
    134     if (mMasterKey != rhs.mMasterKey) {
    135         return false;
    136     }
    137 
    138     if (mMasterKeyIndex != rhs.mMasterKeyIndex) {
    139         return false;
    140     }
    141 
    142     return true;
    143 }
    144 
    145 }  // namespace lowpan
    146 
    147 }  // namespace net
    148 
    149 }  // namespace android
    150