Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 80aca1e3 authored by Robert Quattlebaum's avatar Robert Quattlebaum
Browse files

lowpan: Make various data classes Parcelable

This change updates several of the data classes in
`android.net.lowpan` to be parcelable. This allows them to be used
directly for inter-process communication.

Bug: b/63707448 b/63708348
Change-Id: Ib5e8cad153534948ff4b43a2fda82f3db250839e
Test: Confirmed with unit tests from change id
      I41d590b1e77dc41873c4b9e9bf1b7f1bf859f74e
parent ccd0a151
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.lowpan;

parcelable LowpanBeaconInfo cpp_header "android/net/lowpan/LowpanBeaconInfo.h";
+128 −44
Original line number Diff line number Diff line
@@ -16,9 +16,12 @@

package android.net.lowpan;

import android.os.Parcel;
import android.os.Parcelable;
import com.android.internal.util.HexDump;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.TreeSet;

/**
@@ -27,70 +30,93 @@ import java.util.TreeSet;
 * @hide
 */
// @SystemApi
public class LowpanBeaconInfo extends LowpanIdentity {
public class LowpanBeaconInfo implements Parcelable {
    public static final int UNKNOWN_RSSI = Integer.MAX_VALUE;
    public static final int UNKNOWN_LQI = 0;

    private int mRssi = UNKNOWN;
    private int mLqi = UNKNOWN;
    private LowpanIdentity mIdentity;
    private int mRssi = UNKNOWN_RSSI;
    private int mLqi = UNKNOWN_LQI;
    private byte[] mBeaconAddress = null;
    private final TreeSet<Integer> mFlags = new TreeSet<>();

    public static final int FLAG_CAN_ASSIST = 1;

    static class Builder extends LowpanIdentity.Builder {
        private final LowpanBeaconInfo identity = new LowpanBeaconInfo();
    /** @hide */
    public static class Builder {
        final LowpanIdentity.Builder mIdentityBuilder = new LowpanIdentity.Builder();
        final LowpanBeaconInfo mBeaconInfo = new LowpanBeaconInfo();

        public Builder setRssi(int x) {
            identity.mRssi = x;
        public Builder setLowpanIdentity(LowpanIdentity x) {
            mIdentityBuilder.setLowpanIdentity(x);
            return this;
        }

        public Builder setLqi(int x) {
            identity.mLqi = x;
        public Builder setName(String x) {
            mIdentityBuilder.setName(x);
            return this;
        }

        public Builder setBeaconAddress(byte x[]) {
            identity.mBeaconAddress = x.clone();
        public Builder setXpanid(byte x[]) {
            mIdentityBuilder.setXpanid(x);
            return this;
        }

        public Builder setFlag(int x) {
            identity.mFlags.add(x);
        public Builder setPanid(int x) {
            mIdentityBuilder.setPanid(x);
            return this;
        }

        public Builder setFlags(Collection<Integer> x) {
            identity.mFlags.addAll(x);
        public Builder setChannel(int x) {
            mIdentityBuilder.setChannel(x);
            return this;
        }

        /** @hide */
        Builder updateFromMap(Map map) {
            if (map.containsKey(LowpanProperties.KEY_RSSI.getName())) {
                setRssi(LowpanProperties.KEY_RSSI.getFromMap(map));
        public Builder setType(String x) {
            mIdentityBuilder.setType(x);
            return this;
        }
            if (map.containsKey(LowpanProperties.KEY_LQI.getName())) {
                setLqi(LowpanProperties.KEY_LQI.getFromMap(map));

        public Builder setRssi(int x) {
            mBeaconInfo.mRssi = x;
            return this;
        }
            if (map.containsKey(LowpanProperties.KEY_BEACON_ADDRESS.getName())) {
                setBeaconAddress(LowpanProperties.KEY_BEACON_ADDRESS.getFromMap(map));

        public Builder setLqi(int x) {
            mBeaconInfo.mLqi = x;
            return this;
        }
            identity.mFlags.clear();
            if (map.containsKey(LowpanProperties.KEY_BEACON_CAN_ASSIST.getName())
                    && LowpanProperties.KEY_BEACON_CAN_ASSIST.getFromMap(map).booleanValue()) {
                setFlag(FLAG_CAN_ASSIST);

        public Builder setBeaconAddress(byte x[]) {
            mBeaconInfo.mBeaconAddress = (x != null ? x.clone() : null);
            return this;
        }
            super.updateFromMap(map);

        public Builder setFlag(int x) {
            mBeaconInfo.mFlags.add(x);
            return this;
        }

        public Builder setFlags(Collection<Integer> x) {
            mBeaconInfo.mFlags.addAll(x);
            return this;
        }

        public LowpanBeaconInfo build() {
            return identity;
            mBeaconInfo.mIdentity = mIdentityBuilder.build();
            if (mBeaconInfo.mBeaconAddress == null) {
                mBeaconInfo.mBeaconAddress = new byte[0];
            }
            return mBeaconInfo;
        }
    }

    private LowpanBeaconInfo() {}

    public LowpanIdentity getLowpanIdentity() {
        return mIdentity;
    }

    public int getRssi() {
        return mRssi;
    }
@@ -111,26 +137,21 @@ public class LowpanBeaconInfo extends LowpanIdentity {
        return mFlags.contains(flag);
    }

    @Override
    void addToMap(Map<String, Object> parameters) {
        super.addToMap(parameters);
    }

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();

        sb.append(super.toString());
        sb.append(mIdentity.toString());

        if (mRssi != UNKNOWN) {
            sb.append(", RSSI: ").append(mRssi);
        if (mRssi != UNKNOWN_RSSI) {
            sb.append(", RSSI:").append(mRssi).append("dBm");
        }

        if (mLqi != UNKNOWN) {
        if (mLqi != UNKNOWN_LQI) {
            sb.append(", LQI:").append(mLqi);
        }

        if (mBeaconAddress != null) {
        if (mBeaconAddress.length > 0) {
            sb.append(", BeaconAddress:").append(HexDump.toHexString(mBeaconAddress));
        }

@@ -147,4 +168,67 @@ public class LowpanBeaconInfo extends LowpanIdentity {

        return sb.toString();
    }

    @Override
    public int hashCode() {
        return Objects.hash(mIdentity, mRssi, mLqi, Arrays.hashCode(mBeaconAddress), mFlags);
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof LowpanBeaconInfo)) {
            return false;
        }
        LowpanBeaconInfo rhs = (LowpanBeaconInfo) obj;
        return mIdentity.equals(rhs.mIdentity)
                && Arrays.equals(mBeaconAddress, rhs.mBeaconAddress)
                && mRssi == rhs.mRssi
                && mLqi == rhs.mLqi
                && mFlags.equals(rhs.mFlags);
    }

    /** Implement the Parcelable interface. */
    @Override
    public int describeContents() {
        return 0;
    }

    /** Implement the Parcelable interface. */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        mIdentity.writeToParcel(dest, flags);
        dest.writeInt(mRssi);
        dest.writeInt(mLqi);
        dest.writeByteArray(mBeaconAddress);

        dest.writeInt(mFlags.size());
        for (Integer val : mFlags) {
            dest.writeInt(val);
        }
    }

    /** Implement the Parcelable interface. */
    public static final Creator<LowpanBeaconInfo> CREATOR =
            new Creator<LowpanBeaconInfo>() {
                public LowpanBeaconInfo createFromParcel(Parcel in) {
                    Builder builder = new Builder();

                    builder.setLowpanIdentity(LowpanIdentity.CREATOR.createFromParcel(in));

                    builder.setRssi(in.readInt());
                    builder.setLqi(in.readInt());

                    builder.setBeaconAddress(in.createByteArray());

                    for (int i = in.readInt(); i > 0; i--) {
                        builder.setFlag(in.readInt());
                    }

                    return builder.build();
                }

                public LowpanBeaconInfo[] newArray(int size) {
                    return new LowpanBeaconInfo[size];
                }
            };
}
+19 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.lowpan;

parcelable LowpanChannelInfo cpp_header "android/net/lowpan/LowpanChannelInfo.h";
+145 −13
Original line number Diff line number Diff line
@@ -16,22 +16,66 @@

package android.net.lowpan;

/** Provides detailed information about a given channel. */
import android.os.Parcel;
import android.os.Parcelable;
import java.util.Objects;

/**
 * Provides detailed information about a given channel.
 *
 * @hide
 */
// @SystemApi
public class LowpanChannelInfo {
public class LowpanChannelInfo implements Parcelable {

    public static final int UNKNOWN_POWER = Integer.MAX_VALUE;
    public static final float UNKNOWN_FREQUENCY = 0.0f;
    public static final float UNKNOWN_BANDWIDTH = 0.0f;

    // Instance Variables

    private String mName = null;
    private int mIndex = 0;
    private boolean mIsMaskedByRegulatoryDomain = false;
    private float mSpectrumCenterFrequency = 0.0f;
    private float mSpectrumBandwidth = 0.0f;
    private String mName = null;
    private float mSpectrumCenterFrequency = UNKNOWN_FREQUENCY;
    private float mSpectrumBandwidth = UNKNOWN_BANDWIDTH;
    private int mMaxTransmitPower = UNKNOWN_POWER;
    private boolean mIsMaskedByRegulatoryDomain = false;

    /** @hide */
    public static LowpanChannelInfo getChannelInfoForIeee802154Page0(int index) {
        LowpanChannelInfo info = new LowpanChannelInfo();

        if (index < 0) {
            info = null;

        } else if (index == 0) {
            info.mSpectrumCenterFrequency = 868300000.0f;
            info.mSpectrumBandwidth = 600000.0f;

    // Public Getters and Setters
        } else if (index < 11) {
            info.mSpectrumCenterFrequency = 906000000.0f - (2000000.0f * 1) + 2000000.0f * (index);
            info.mSpectrumBandwidth = 0; // Unknown

        } else if (index < 26) {
            info.mSpectrumCenterFrequency =
                    2405000000.0f - (5000000.0f * 11) + 5000000.0f * (index);
            info.mSpectrumBandwidth = 2000000.0f;

        } else {
            info = null;
        }

        info.mName = Integer.toString(index);

        return info;
    }

    private LowpanChannelInfo() {}

    private LowpanChannelInfo(int index, String name, float cf, float bw) {
        mIndex = index;
        mName = name;
        mSpectrumCenterFrequency = cf;
        mSpectrumBandwidth = bw;
    }

    public String getName() {
        return mName;
@@ -63,22 +107,110 @@ public class LowpanChannelInfo {

        sb.append("Channel ").append(mIndex);

        if (mName != null) {
        if (mName != null && !mName.equals(Integer.toString(mIndex))) {
            sb.append(" (").append(mName).append(")");
        }

        if (mSpectrumCenterFrequency > 0.0f) {
            sb.append(", SpectrumCenterFrequency: ").append(mSpectrumCenterFrequency).append("Hz");
            if (mSpectrumCenterFrequency > 1000000000.0f) {
                sb.append(", SpectrumCenterFrequency: ")
                        .append(mSpectrumCenterFrequency / 1000000000.0f)
                        .append("GHz");
            } else if (mSpectrumCenterFrequency > 1000000.0f) {
                sb.append(", SpectrumCenterFrequency: ")
                        .append(mSpectrumCenterFrequency / 1000000.0f)
                        .append("MHz");
            } else {
                sb.append(", SpectrumCenterFrequency: ")
                        .append(mSpectrumCenterFrequency / 1000.0f)
                        .append("kHz");
            }
        }

        if (mSpectrumBandwidth > 0.0f) {
            sb.append(", SpectrumBandwidth: ").append(mSpectrumBandwidth).append("Hz");
            if (mSpectrumBandwidth > 1000000000.0f) {
                sb.append(", SpectrumBandwidth: ")
                        .append(mSpectrumBandwidth / 1000000000.0f)
                        .append("GHz");
            } else if (mSpectrumBandwidth > 1000000.0f) {
                sb.append(", SpectrumBandwidth: ")
                        .append(mSpectrumBandwidth / 1000000.0f)
                        .append("MHz");
            } else {
                sb.append(", SpectrumBandwidth: ")
                        .append(mSpectrumBandwidth / 1000.0f)
                        .append("kHz");
            }
        }

        if (mMaxTransmitPower != UNKNOWN_POWER) {
            sb.append(", MaxTransmitPower: ").append(mMaxTransmitPower);
            sb.append(", MaxTransmitPower: ").append(mMaxTransmitPower).append("dBm");
        }

        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof LowpanChannelInfo)) {
            return false;
        }
        LowpanChannelInfo rhs = (LowpanChannelInfo) obj;
        return Objects.equals(mName, rhs.mName)
                && mIndex == rhs.mIndex
                && mIsMaskedByRegulatoryDomain == rhs.mIsMaskedByRegulatoryDomain
                && mSpectrumCenterFrequency == rhs.mSpectrumCenterFrequency
                && mSpectrumBandwidth == rhs.mSpectrumBandwidth
                && mMaxTransmitPower == rhs.mMaxTransmitPower;
    }

    @Override
    public int hashCode() {
        return Objects.hash(
                mName,
                mIndex,
                mIsMaskedByRegulatoryDomain,
                mSpectrumCenterFrequency,
                mSpectrumBandwidth,
                mMaxTransmitPower);
    }

    /** Implement the Parcelable interface. */
    @Override
    public int describeContents() {
        return 0;
    }

    /** Implement the Parcelable interface. */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mIndex);
        dest.writeString(mName);
        dest.writeFloat(mSpectrumCenterFrequency);
        dest.writeFloat(mSpectrumBandwidth);
        dest.writeInt(mMaxTransmitPower);
        dest.writeBoolean(mIsMaskedByRegulatoryDomain);
    }

    /** Implement the Parcelable interface. */
    public static final Creator<LowpanChannelInfo> CREATOR =
            new Creator<LowpanChannelInfo>() {

                public LowpanChannelInfo createFromParcel(Parcel in) {
                    LowpanChannelInfo info = new LowpanChannelInfo();

                    info.mIndex = in.readInt();
                    info.mName = in.readString();
                    info.mSpectrumCenterFrequency = in.readFloat();
                    info.mSpectrumBandwidth = in.readFloat();
                    info.mMaxTransmitPower = in.readInt();
                    info.mIsMaskedByRegulatoryDomain = in.readBoolean();

                    return info;
                }

                public LowpanChannelInfo[] newArray(int size) {
                    return new LowpanChannelInfo[size];
                }
            };
}
+19 −0
Original line number Diff line number Diff line
/**
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.lowpan;

parcelable LowpanCredential cpp_header "android/net/lowpan/LowpanCredential.h";
Loading