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

Commit 6cfc490c authored by Robert Quattlebaum's avatar Robert Quattlebaum
Browse files

lowpan: AIDL refactor to no longer use property design pattern

After doing a considerable amount of thinking on the subject,
I decided that attempting to directly port wpantund's property-based
API over to Binder was a mistake, so this commit contains the required
changes to abandon that approach and go with a more traditional
Binder interface.

Bug: b/63708348
Change-Id: I685cd066fabe8470ef4bb456aea7d3bb515c3969
Test: Compiled and ran unit tests, manually confirmed lowpanctl
      and lowpan-service (with related changes) appeared to be
      working properly.
parent 865fdc75
Loading
Loading
Loading
Loading
+63 −98
Original line number Diff line number Diff line
@@ -16,103 +16,39 @@

package android.net.lowpan;

import android.net.IpPrefix;
import android.net.lowpan.ILowpanEnergyScanCallback;
import android.net.lowpan.ILowpanInterfaceListener;
import android.net.lowpan.ILowpanNetScanCallback;
import android.net.lowpan.ILowpanEnergyScanCallback;
import android.os.PersistableBundle;
import android.net.IpPrefix;
import android.net.lowpan.LowpanBeaconInfo;
import android.net.lowpan.LowpanChannelInfo;
import android.net.lowpan.LowpanCredential;
import android.net.lowpan.LowpanIdentity;
import android.net.lowpan.LowpanProvision;

/** {@hide} */
interface ILowpanInterface {

    //////////////////////////////////////////////////////////////////////////
    // Permission String Constants

    /* These are here for the sake of C++ interface implementations. */
    // These are here for the sake of C++ interface implementations.

    const String PERM_ACCESS_LOWPAN_STATE    = "android.permission.ACCESS_LOWPAN_STATE";
    const String PERM_CHANGE_LOWPAN_STATE    = "android.permission.CHANGE_LOWPAN_STATE";
    const String PERM_READ_LOWPAN_CREDENTIAL = "android.permission.READ_LOWPAN_CREDENTIAL";

    //////////////////////////////////////////////////////////////////////////
    // Property Key Constants

    /** Type: Boolean */
    const String KEY_INTERFACE_ENABLED      = "android.net.lowpan.property.INTERFACE_ENABLED";

    /** Type: Boolean */
    const String KEY_INTERFACE_UP           = "android.net.lowpan.property.INTERFACE_UP";

    /** Type: Boolean */
    const String KEY_INTERFACE_COMMISSIONED = "android.net.lowpan.property.INTERFACE_COMMISSIONED";

    /** Type: Boolean */
    const String KEY_INTERFACE_CONNECTED    = "android.net.lowpan.property.INTERFACE_CONNECTED";

    /** Type: String */
    const String KEY_INTERFACE_STATE        = "android.net.lowpan.property.INTERFACE_STATE";

    /** Type: String */
    const String KEY_NETWORK_NAME             = "android.net.lowpan.property.NETWORK_NAME";

    /** Type: Integer */
    const String KEY_NETWORK_TYPE             = "android.net.lowpan.property.NETWORK_TYPE";

    /** Type: Integer */
    const String KEY_NETWORK_PANID            = "android.net.lowpan.property.NETWORK_PANID";

    /** Type: byte[] */
    const String KEY_NETWORK_XPANID           = "android.net.lowpan.property.NETWORK_XPANID";

    /** Type: String */
    const String KEY_NETWORK_ROLE             = "android.net.lowpan.property.NETWORK_ROLE";

    /** Type: byte[] */
    const String KEY_NETWORK_MASTER_KEY       = "android.net.lowpan.property.NETWORK_MASTER_KEY";

    /** Type: Integer */
    const String KEY_NETWORK_MASTER_KEY_INDEX
        = "android.net.lowpan.property.NETWORK_MASTER_KEY_INDEX";

    /** Type: int[] */
    const String KEY_SUPPORTED_CHANNELS = "android.net.lowpan.property.SUPPORTED_CHANNELS";

    /** Type: Integer */
    const String KEY_CHANNEL            = "android.net.lowpan.property.CHANNEL";

    /** Type: int[] */
    /**
     * Channel mask key.
     * Used for setting a channel mask when starting a scan.
     * Type: int[]
     * */
    const String KEY_CHANNEL_MASK       = "android.net.lowpan.property.CHANNEL_MASK";

    /** Type: Integer */
    /**
     * Max Transmit Power Key.
     * Used for setting the maximum transmit power when starting a network scan.
     * Type: Integer
     * */
    const String KEY_MAX_TX_POWER       = "android.net.lowpan.property.MAX_TX_POWER";

    /** Type: Integer */
    const String KEY_RSSI               = "android.net.lowpan.property.RSSI";

    /** Type: Integer */
    const String KEY_LQI                = "android.net.lowpan.property.LQI";

    /** Type: byte[] */
    const String KEY_BEACON_ADDRESS     = "android.net.lowpan.property.BEACON_ORIGIN_ADDRESS";

    /** Type: Boolean */
    const String KEY_BEACON_CAN_ASSIST  = "android.net.lowpan.property.BEACON_CAN_ASSIST";

    /** Type: String */
    const String DRIVER_VERSION         = "android.net.lowpan.property.DRIVER_VERSION";

    /** Type: String */
    const String NCP_VERSION            = "android.net.lowpan.property.NCP_VERSION";

    /** Type: byte[]
     * @hide */
    const String KEY_EXTENDED_ADDRESS = "android.net.lowpan.property.EXTENDED_ADDRESS";

    /** Type: byte[]
     * @hide */
    const String KEY_MAC_ADDRESS      = "android.net.lowpan.property.MAC_ADDRESS";

    //////////////////////////////////////////////////////////////////////////
    // Interface States

    const String STATE_OFFLINE = "offline";
@@ -121,16 +57,26 @@ interface ILowpanInterface {
    const String STATE_ATTACHED = "attached";
    const String STATE_FAULT = "fault";

    //////////////////////////////////////////////////////////////////////////
    // Device Roles

    const String ROLE_END_DEVICE = "end-device";
    const String ROLE_ROUTER = "router";
    const String ROLE_SLEEPY_END_DEVICE = "sleepy-end-device";
    const String ROLE_SLEEPY_ROUTER = "sleepy-router";
    const String ROLE_UNKNOWN = "unknown";
    const String ROLE_LEADER = "leader";
    const String ROLE_COORDINATOR = "coordinator";
    const String ROLE_DETACHED = "detached";

    const String NETWORK_TYPE_UNKNOWN = "unknown";

    /**
     * Network type for Thread 1.x networks.
     *
     * @see android.net.lowpan.LowpanIdentity#getType
     * @see #getLowpanIdentity
     */
    const String NETWORK_TYPE_THREAD_V1 = "org.threadgroup.thread.v1";

    //////////////////////////////////////////////////////////////////////////
    // Service-Specific Error Code Constants

    const int ERROR_UNSPECIFIED = 1;
@@ -149,25 +95,49 @@ interface ILowpanInterface {
    const int ERROR_JOIN_FAILED_AT_AUTH = 14;
    const int ERROR_FORM_FAILED_AT_SCAN = 15;

    //////////////////////////////////////////////////////////////////////////
    // Methods

    @utf8InCpp String getName();

    void join(in Map parameters);
    void form(in Map parameters);
    @utf8InCpp String getNcpVersion();
    @utf8InCpp String getDriverVersion();
    LowpanChannelInfo[] getSupportedChannels();
    @utf8InCpp String[] getSupportedNetworkTypes();
    byte[] getMacAddress();

    boolean isEnabled();
    void setEnabled(boolean enabled);

    boolean isUp();
    boolean isCommissioned();
    boolean isConnected();
    @utf8InCpp String getState();

    @utf8InCpp String getRole();
    @utf8InCpp String getPartitionId();
    byte[] getExtendedAddress();

    LowpanIdentity getLowpanIdentity();
    LowpanCredential getLowpanCredential();

    @utf8InCpp String[] getLinkAddresses();
    IpPrefix[] getLinkNetworks();

    void join(in LowpanProvision provision);
    void form(in LowpanProvision provision);
    void attach(in LowpanProvision provision);
    void leave();
    void reset();

    void startCommissioningSession(in LowpanBeaconInfo beaconInfo);
    void closeCommissioningSession();
    oneway void sendToCommissioner(in byte[] packet);

    void beginLowPower();
    void pollForData();
    oneway void pollForData();

    oneway void onHostWake();

    @utf8InCpp String[] getPropertyKeys();
    Map getProperties(in @utf8InCpp String[] keys);
    void setProperties(in Map properties);

    void addListener(ILowpanInterfaceListener listener);
    oneway void removeListener(ILowpanInterfaceListener listener);

@@ -177,14 +147,9 @@ interface ILowpanInterface {
    void startEnergyScan(in Map properties, ILowpanEnergyScanCallback listener);
    oneway void stopEnergyScan();

    String[] copyLinkAddresses();
    IpPrefix[] copyLinkNetworks();

    void addOnMeshPrefix(in IpPrefix prefix, int flags);
    oneway void removeOnMeshPrefix(in IpPrefix prefix);

    void addExternalRoute(in IpPrefix prefix, int flags);
    oneway void removeExternalRoute(in IpPrefix prefix);

    @utf8InCpp String getPropertyAsString(@utf8InCpp String key);
}
+20 −5
Original line number Diff line number Diff line
@@ -17,14 +17,29 @@
package android.net.lowpan;

import android.net.IpPrefix;
import android.net.lowpan.LowpanIdentity;

/** {@hide} */
interface ILowpanInterfaceListener {
    oneway void onPropertiesChanged(in Map properties);
    oneway void onEnabledChanged(boolean value);

    oneway void onLinkNetworkAdded(in IpPrefix prefix);
    oneway void onLinkNetworkRemoved(in IpPrefix prefix);
    oneway void onConnectedChanged(boolean value);

    oneway void onLinkAddressAdded(in String address);
    oneway void onLinkAddressRemoved(in String address);
    oneway void onUpChanged(boolean value);

    oneway void onRoleChanged(@utf8InCpp String value);

    oneway void onStateChanged(@utf8InCpp String value);

    oneway void onLowpanIdentityChanged(in LowpanIdentity value);

    oneway void onLinkNetworkAdded(in IpPrefix value);

    oneway void onLinkNetworkRemoved(in IpPrefix value);

    oneway void onLinkAddressAdded(@utf8InCpp String value);

    oneway void onLinkAddressRemoved(@utf8InCpp String value);

    oneway void onReceiveFromCommissioner(in byte[] packet);
}
+4 −2
Original line number Diff line number Diff line
/*
 * Copyright (C) 2016 The Android Open Source Project
 * 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.
@@ -16,8 +16,10 @@

package android.net.lowpan;

import android.net.lowpan.LowpanBeaconInfo;

/** {@hide} */
interface ILowpanNetScanCallback {
    oneway void onNetScanBeacon(in Map parameters);
    oneway void onNetScanBeacon(in LowpanBeaconInfo beacon);
    oneway void onNetScanFinished();
}
+169 −19
Original line number Diff line number Diff line
@@ -18,9 +18,11 @@ package android.net.lowpan;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.Network;
import android.net.IpPrefix;
import android.os.DeadObjectException;
import android.os.Handler;
import java.net.InetSocketAddress;
import android.os.Looper;
import android.os.RemoteException;

/**
 * Commissioning Session.
@@ -31,8 +33,15 @@ import java.net.InetSocketAddress;
 * @hide
 */
// @SystemApi
public abstract class LowpanCommissioningSession {
    public LowpanCommissioningSession() {}
public class LowpanCommissioningSession {

    private final ILowpanInterface mBinder;
    private final LowpanBeaconInfo mBeaconInfo;
    private final ILowpanInterfaceListener mInternalCallback = new InternalCallback();
    private final Looper mLooper;
    private Handler mHandler;
    private Callback mCallback = null;
    private volatile boolean mIsClosed = false;

    /**
     * Callback base class for {@link LowpanCommissioningSession}
@@ -40,34 +49,175 @@ public abstract class LowpanCommissioningSession {
     * @hide
     */
    // @SystemApi
    public static abstract class Callback {
    public abstract static class Callback {
        public void onReceiveFromCommissioner(@NonNull byte[] packet) {};

        public void onClosed() {};
    }

    private class InternalCallback extends ILowpanInterfaceListener.Stub {
        @Override
        public void onStateChanged(String value) {
            if (!mIsClosed) {
                switch (value) {
                    case ILowpanInterface.STATE_OFFLINE:
                    case ILowpanInterface.STATE_FAULT:
                        synchronized (LowpanCommissioningSession.this) {
                            lockedCleanup();
                        }
                }
            }
        }

        @Override
        public void onReceiveFromCommissioner(byte[] packet) {
            mHandler.post(
                    () -> {
                        synchronized (LowpanCommissioningSession.this) {
                            if (!mIsClosed && (mCallback != null)) {
                                mCallback.onReceiveFromCommissioner(packet);
                            }
                        }
                    });
        }

        // We ignore all other callbacks.
        @Override
        public void onEnabledChanged(boolean value) {}

        @Override
        public void onConnectedChanged(boolean value) {}

        @Override
        public void onUpChanged(boolean value) {}

        @Override
        public void onRoleChanged(String value) {}

        @Override
        public void onLowpanIdentityChanged(LowpanIdentity value) {}

        @Override
        public void onLinkNetworkAdded(IpPrefix value) {}

        @Override
        public void onLinkNetworkRemoved(IpPrefix value) {}

        @Override
        public void onLinkAddressAdded(String value) {}

        @Override
        public void onLinkAddressRemoved(String value) {}
    }

    LowpanCommissioningSession(
            ILowpanInterface binder, LowpanBeaconInfo beaconInfo, Looper looper) {
        mBinder = binder;
        mBeaconInfo = beaconInfo;
        mLooper = looper;

        if (mLooper != null) {
            mHandler = new Handler(mLooper);
        } else {
            mHandler = new Handler();
        }

        try {
            mBinder.addListener(mInternalCallback);

        } catch (RemoteException x) {
            throw x.rethrowAsRuntimeException();
        }
    }

    private void lockedCleanup() {
        // Note: this method is only called from synchronized contexts.

        if (!mIsClosed) {
            try {
                mBinder.removeListener(mInternalCallback);

            } catch (DeadObjectException x) {
                /* We don't care if we receive a DOE at this point.
                 * DOE is as good as success as far as we are concerned.
                 */

            } catch (RemoteException x) {
                throw x.rethrowAsRuntimeException();
            }

            if (mCallback != null) {
                mHandler.post(() -> mCallback.onClosed());
            }
        }

        mCallback = null;
        mIsClosed = true;
    }

    /** TODO: doc */
    @NonNull
    public abstract LowpanBeaconInfo getBeaconInfo();
    public LowpanBeaconInfo getBeaconInfo() {
        return mBeaconInfo;
    }

    /** TODO: doc */
    public abstract void sendToCommissioner(@NonNull byte[] packet);
    public void sendToCommissioner(@NonNull byte[] packet) {
        if (!mIsClosed) {
            try {
                mBinder.sendToCommissioner(packet);

            } catch (DeadObjectException x) {
                /* This method is a best-effort delivery.
                 * We don't care if we receive a DOE at this point.
                 */

            } catch (RemoteException x) {
                throw x.rethrowAsRuntimeException();
            }
        }
    }

    /** TODO: doc */
    public abstract void setCallback(@Nullable Callback cb, @Nullable Handler handler);
    public synchronized void setCallback(@Nullable Callback cb, @Nullable Handler handler) {
        if (!mIsClosed) {
            /* This class can be created with or without a default looper.
             * Also, this method can be called with or without a specific
             * handler. If a handler is specified, it is to always be used.
             * Otherwise, if there was a Looper specified when this object
             * was created, we create a new handle based on that looper.
             * Otherwise we just create a default handler object. Since we
             * don't really know how the previous handler was created, we
             * end up always replacing it here. This isn't a huge problem
             * because this method should be called infrequently.
             */
            if (handler != null) {
                mHandler = handler;
            } else if (mLooper != null) {
                mHandler = new Handler(mLooper);
            } else {
                mHandler = new Handler();
            }
            mCallback = cb;
        }
    }

    /** TODO: doc */
    public abstract void close();
    public synchronized void close() {
        if (!mIsClosed) {
            try {
                mBinder.closeCommissioningSession();

    /**
     * This method is largely for Nest Weave, as an alternative to {@link #sendToCommissioner()}
     * and @{link Callback#onReceiveFromCommissioner()}.
     *
     * <p>When used with the Network instance obtained from getNetwork(), the caller can use the
     * given InetSocketAddress to communicate with the commissioner using a UDP (or, under certain
     * circumstances, TCP) socket.
                lockedCleanup();

            } catch (DeadObjectException x) {
                /* We don't care if we receive a DOE at this point.
                 * DOE is as good as success as far as we are concerned.
                 */
    public abstract @Nullable InetSocketAddress getInetSocketAddress();

    public abstract @Nullable Network getNetwork();
            } catch (RemoteException x) {
                throw x.rethrowAsRuntimeException();
            }
        }
    }
}
+211 −248

File changed.

Preview size limit exceeded, changes collapsed.

Loading