summaryrefslogtreecommitdiffstats
path: root/tests/src/com/android/nfc/MockLlcpSocket.java
blob: 95279a732d3d2558fb6540954e8824045c34e006 (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
/*
 * Copyright (C) 2011 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 com.android.nfc;

import android.util.Log;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class MockLlcpSocket implements DeviceHost.LlcpSocket {
    private static final String TAG = "mockLlcpSocket";
    private MockLlcpSocket mPairedSocket;
    private List<byte[]> mReceivedPackets = new LinkedList<byte[]>();
    private boolean mClosed = false;

    @Override
    public void close() throws IOException {
        mClosed = true;
        mPairedSocket.mClosed = true;
    }

    @Override
    public void connectToSap(int sap) throws IOException {
        throw new UnsupportedOperationException("Use MockLlcpSocket.bind(client, server)");
    }

    @Override
    public void send(byte[] data) throws IOException {
        if (mClosed || mPairedSocket == null) {
            throw new IOException("Socket not connected");
        }
        synchronized (mPairedSocket.mReceivedPackets) {
            Log.d(TAG, "sending packet");
            mPairedSocket.mReceivedPackets.add(data);
            mPairedSocket.mReceivedPackets.notify();
        }
    }

    @Override
    public int receive(byte[] receiveBuffer) throws IOException {
        synchronized (mReceivedPackets) {
            while (!mClosed && mReceivedPackets.size() == 0) {
                try {
                    mReceivedPackets.wait(1000);
                } catch (InterruptedException e) {}
            }
            if (mClosed) {
                throw new IOException("Socket closed.");
            }
            byte[] arr = mReceivedPackets.remove(0);
            System.arraycopy(arr, 0, receiveBuffer, 0, arr.length);
            return arr.length;
        }
    }

    public static void bind(MockLlcpSocket client, MockLlcpSocket server) {
        client.mPairedSocket = server;
        server.mPairedSocket = client;
    }

    @Override
    public void connectToService(String serviceName) throws IOException {
        throw new UnsupportedOperationException();
    }

    @Override
    public int getRemoteMiu() {
        throw new UnsupportedOperationException();
    }

    @Override
    public int getRemoteRw() {
        throw new UnsupportedOperationException();
    }

    @Override
    public int getLocalSap() {
        throw new UnsupportedOperationException();
    }

    @Override
    public int getLocalMiu() {
        throw new UnsupportedOperationException();
    }

    @Override
    public int getLocalRw() {
        throw new UnsupportedOperationException();
    }
}