aboutsummaryrefslogtreecommitdiff
path: root/src/android/java/com/android/server/thread/openthread/testing/FakeOtDaemon.java
blob: ce50eba964a1181fd19d65d682a47701f6e4184b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 *    Copyright (c) 2023, The OpenThread Authors.
 *    All rights reserved.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are met:
 *    1. Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *    3. Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGE.
 */

package com.android.server.thread.openthread.testing;

import static com.android.server.thread.openthread.IOtDaemon.OT_STATE_DISABLED;
import static com.android.server.thread.openthread.IOtDaemon.OT_STATE_ENABLED;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.net.thread.ChannelMaxPower;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;

import com.android.server.thread.openthread.BackboneRouterState;
import com.android.server.thread.openthread.BorderRouterConfigurationParcel;
import com.android.server.thread.openthread.IChannelMasksReceiver;
import com.android.server.thread.openthread.INsdPublisher;
import com.android.server.thread.openthread.IOtDaemon;
import com.android.server.thread.openthread.IOtDaemonCallback;
import com.android.server.thread.openthread.IOtStatusReceiver;
import com.android.server.thread.openthread.MeshcopTxtAttributes;
import com.android.server.thread.openthread.OtDaemonState;

import java.time.Duration;
import java.util.ArrayList;
import java.util.NoSuchElementException;

/** A fake implementation of the {@link IOtDaemon} AIDL API for testing. */
public final class FakeOtDaemon extends IOtDaemon.Stub {
    /** The typical Thread network join / attach delay is around 8 seconds. */
    public static final Duration JOIN_DELAY = Duration.ofSeconds(8);

    static final int OT_DEVICE_ROLE_DISABLED = 0;
    static final int OT_DEVICE_ROLE_DETACHED = 1;
    static final int OT_DEVICE_ROLE_CHILD = 2;
    static final int OT_DEVICE_ROLE_ROUTER = 3;
    static final int OT_DEVICE_ROLE_LEADER = 4;
    static final int OT_ERROR_NONE = 0;

    private static final long PROACTIVE_LISTENER_ID = -1;

    private final Handler mHandler;
    private OtDaemonState mState;
    private BackboneRouterState mBbrState;
    private boolean mIsInitialized = false;
    private int mThreadEnabled = OT_STATE_DISABLED;
    private int mChannelMasksReceiverOtError = OT_ERROR_NONE;
    private int mSupportedChannelMask = 0x07FFF800; // from channel 11 to 26
    private int mPreferredChannelMask = 0;

    @Nullable private DeathRecipient mDeathRecipient;
    @Nullable private ParcelFileDescriptor mTunFd;
    @Nullable private INsdPublisher mNsdPublisher;
    @Nullable private MeshcopTxtAttributes mOverriddenMeshcopTxts;
    @Nullable private IOtDaemonCallback mCallback;
    @Nullable private Long mCallbackListenerId;
    @Nullable private RemoteException mJoinException;
    @Nullable private String mCountryCode;

    public FakeOtDaemon(Handler handler) {
        mHandler = handler;
        resetStates();
    }

    private void resetStates() {
        mState = new OtDaemonState();
        mState.isInterfaceUp = false;
        mState.partitionId = -1;
        mState.deviceRole = OT_DEVICE_ROLE_DISABLED;
        mState.activeDatasetTlvs = new byte[0];
        mState.pendingDatasetTlvs = new byte[0];
        mBbrState = new BackboneRouterState();
        mBbrState.multicastForwardingEnabled = false;
        mBbrState.listeningAddresses = new ArrayList<>();

        mTunFd = null;
        mThreadEnabled = OT_STATE_DISABLED;
        mNsdPublisher = null;
        mIsInitialized = false;

        mCallback = null;
        mCallbackListenerId = null;
    }

    @Override
    public IBinder asBinder() {
        return this;
    }

    @Override
    public void linkToDeath(DeathRecipient recipient, int flags) {
        if (mDeathRecipient != null && recipient != null) {
            throw new IllegalStateException("IOtDaemon death recipient is already linked!");
        }

        mDeathRecipient = recipient;
    }

    @Override
    public boolean unlinkToDeath(@NonNull DeathRecipient recipient, int flags) {
        if (mDeathRecipient == null || recipient != mDeathRecipient) {
            throw new NoSuchElementException("recipient is not linked! " + recipient);
        }

        mDeathRecipient = null;
        return true;
    }

    @Nullable
    public DeathRecipient getDeathRecipient() {
        return mDeathRecipient;
    }

    @Override
    public void initialize(
            ParcelFileDescriptor tunFd,
            boolean enabled,
            INsdPublisher nsdPublisher,
            MeshcopTxtAttributes overriddenMeshcopTxts,
            IOtDaemonCallback callback,
            String countryCode)
            throws RemoteException {
        mIsInitialized = true;
        mTunFd = tunFd;
        mThreadEnabled = enabled ? OT_STATE_ENABLED : OT_STATE_DISABLED;
        mNsdPublisher = nsdPublisher;
        mCountryCode = countryCode;

        mOverriddenMeshcopTxts = new MeshcopTxtAttributes();
        mOverriddenMeshcopTxts.vendorOui = overriddenMeshcopTxts.vendorOui.clone();
        mOverriddenMeshcopTxts.vendorName = overriddenMeshcopTxts.vendorName;
        mOverriddenMeshcopTxts.modelName = overriddenMeshcopTxts.modelName;

        registerStateCallback(callback, PROACTIVE_LISTENER_ID);
    }

    /** Returns {@code true} if {@link initialize} has been called to initialize this object. */
    public boolean isInitialized() {
        return mIsInitialized;
    }

    @Override
    public void terminate() throws RemoteException {
        mHandler.post(
                () -> {
                    resetStates();
                    if (mDeathRecipient != null) {
                        mDeathRecipient.binderDied();
                        mDeathRecipient = null;
                    }
                });
    }

    public int getEnabledState() {
        return mThreadEnabled;
    }

    public OtDaemonState getState() {
        return makeCopy(mState);
    }

    public BackboneRouterState getBackboneRouterState() {
        return makeCopy(mBbrState);
    }

    /**
     * Returns the Thread TUN interface FD sent to OT daemon or {@code null} if {@link initialize}
     * is never called.
     */
    @Nullable
    public ParcelFileDescriptor getTunFd() {
        return mTunFd;
    }

    /**
     * Returns the INsdPublisher sent to OT daemon or {@code null} if {@link #initialize} is never
     * called.
     */
    @Nullable
    public INsdPublisher getNsdPublisher() {
        return mNsdPublisher;
    }

    /**
     * Returns the overridden MeshCoP TXT attributes that is to OT daemon or {@code null} if {@link
     * #initialize} is never called.
     */
    @Nullable
    public MeshcopTxtAttributes getOverriddenMeshcopTxtAttributes() {
        return mOverriddenMeshcopTxts;
    }

    @Nullable
    public IOtDaemonCallback getCallback() {
        return mCallback;
    }

    @Override
    public void setThreadEnabled(boolean enabled, IOtStatusReceiver receiver) {
        mHandler.post(
                () -> {
                    mThreadEnabled = enabled ? OT_STATE_ENABLED : OT_STATE_DISABLED;
                    try {
                        receiver.onSuccess();
                    } catch (RemoteException e) {
                        throw new AssertionError(e);
                    }
                });
    }

    @Override
    public void registerStateCallback(IOtDaemonCallback callback, long listenerId)
            throws RemoteException {
        mCallback = callback;
        mCallbackListenerId = listenerId;

        mHandler.post(() -> onStateChanged(mState, mCallbackListenerId));
        mHandler.post(() -> onBackboneRouterStateChanged(mBbrState));
    }

    @Nullable
    public IOtDaemonCallback getStateCallback() {
        return mCallback;
    }

    /**
     * Returns the country code sent to OT daemon or {@code null} if {@link #initialize} is never
     * called.
     */
    @Nullable
    public String getCountryCode() {
        return mCountryCode;
    }

    @Override
    public void join(byte[] activeDataset, IOtStatusReceiver receiver) throws RemoteException {
        if (mJoinException != null) {
            throw mJoinException;
        }

        mHandler.post(
                () -> {
                    mState.isInterfaceUp = true;
                    mState.deviceRole = OT_DEVICE_ROLE_DETACHED;
                    onStateChanged(mState, PROACTIVE_LISTENER_ID);
                });

        mHandler.postDelayed(
                () -> {
                    mState.deviceRole = OT_DEVICE_ROLE_LEADER;
                    mState.activeDatasetTlvs = activeDataset.clone();
                    mBbrState.multicastForwardingEnabled = true;

                    onStateChanged(mState, PROACTIVE_LISTENER_ID);
                    onBackboneRouterStateChanged(mBbrState);
                    try {
                        receiver.onSuccess();
                    } catch (RemoteException e) {
                        throw new AssertionError(e);
                    }
                },
                JOIN_DELAY.toMillis());
    }

    private OtDaemonState makeCopy(OtDaemonState state) {
        OtDaemonState copyState = new OtDaemonState();
        copyState.isInterfaceUp = state.isInterfaceUp;
        copyState.deviceRole = state.deviceRole;
        copyState.partitionId = state.partitionId;
        copyState.activeDatasetTlvs = state.activeDatasetTlvs.clone();
        copyState.pendingDatasetTlvs = state.pendingDatasetTlvs.clone();
        return copyState;
    }

    private BackboneRouterState makeCopy(BackboneRouterState state) {
        BackboneRouterState copyState = new BackboneRouterState();
        copyState.multicastForwardingEnabled = state.multicastForwardingEnabled;
        copyState.listeningAddresses = new ArrayList<>(state.listeningAddresses);
        return copyState;
    }

    private void onStateChanged(OtDaemonState state, long listenerId) {
        try {
            // Make a copy of state so that clients won't keep a direct reference to it
            OtDaemonState copyState = makeCopy(state);

            mCallback.onStateChanged(copyState, listenerId);
        } catch (RemoteException e) {
            throw new AssertionError(e);
        }
    }

    private void onBackboneRouterStateChanged(BackboneRouterState state) {
        try {
            // Make a copy of state so that clients won't keep a direct reference to it
            BackboneRouterState copyState = makeCopy(state);

            mCallback.onBackboneRouterStateChanged(copyState);
        } catch (RemoteException e) {
            throw new AssertionError(e);
        }
    }

    /** Sets the {@link RemoteException} which will be thrown from {@link #join}. */
    public void setJoinException(RemoteException exception) {
        mJoinException = exception;
    }

    @Override
    public void leave(IOtStatusReceiver receiver) throws RemoteException {
        throw new UnsupportedOperationException("FakeOtDaemon#leave is not implemented!");
    }

    @Override
    public void configureBorderRouter(
            BorderRouterConfigurationParcel config, IOtStatusReceiver receiver)
            throws RemoteException {
        throw new UnsupportedOperationException(
                "FakeOtDaemon#configureBorderRouter is not implemented!");
    }

    @Override
    public void scheduleMigration(byte[] pendingDataset, IOtStatusReceiver receiver)
            throws RemoteException {
        throw new UnsupportedOperationException(
                "FakeOtDaemon#scheduleMigration is not implemented!");
    }

    @Override
    public void setCountryCode(String countryCode, IOtStatusReceiver receiver)
            throws RemoteException {
        throw new UnsupportedOperationException("FakeOtDaemon#setCountryCode is not implemented!");
    }

    @Override
    public void getChannelMasks(IChannelMasksReceiver receiver) throws RemoteException {
        mHandler.post(
                () -> {
                    try {
                        if (mChannelMasksReceiverOtError == OT_ERROR_NONE) {
                            receiver.onSuccess(mSupportedChannelMask, mPreferredChannelMask);
                        } else {
                            receiver.onError(
                                    mChannelMasksReceiverOtError, "Get channel masks failed");
                        }
                    } catch (RemoteException e) {
                        throw new AssertionError(e);
                    }
                });
    }

    public void setChannelMasks(int supportedChannelMask, int preferredChannelMask) {
        mSupportedChannelMask = supportedChannelMask;
        mPreferredChannelMask = preferredChannelMask;
    }

    public void setChannelMasksReceiverOtError(int otError) {
        mChannelMasksReceiverOtError = otError;
    }

    @Override
    public void setChannelMaxPowers(ChannelMaxPower[] channelMaxPowers, IOtStatusReceiver receiver)
            throws RemoteException {
        throw new UnsupportedOperationException(
                "FakeOtDaemon#setChannelTargetPowers is not implemented!");
    }
}