1/*
2 * Copyright (C) 2011 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
17package com.android.nfc;
18
19import android.util.Log;
20
21import java.io.IOException;
22import java.util.LinkedList;
23import java.util.List;
24
25public class MockLlcpSocket implements DeviceHost.LlcpSocket {
26    private static final String TAG = "mockLlcpSocket";
27    private MockLlcpSocket mPairedSocket;
28    private List<byte[]> mReceivedPackets = new LinkedList<byte[]>();
29    private boolean mClosed = false;
30
31    @Override
32    public void close() throws IOException {
33        mClosed = true;
34        mPairedSocket.mClosed = true;
35    }
36
37    @Override
38    public void connectToSap(int sap) throws IOException {
39        throw new UnsupportedOperationException("Use MockLlcpSocket.bind(client, server)");
40    }
41
42    @Override
43    public void send(byte[] data) throws IOException {
44        if (mClosed || mPairedSocket == null) {
45            throw new IOException("Socket not connected");
46        }
47        synchronized (mPairedSocket.mReceivedPackets) {
48            Log.d(TAG, "sending packet");
49            mPairedSocket.mReceivedPackets.add(data);
50            mPairedSocket.mReceivedPackets.notify();
51        }
52    }
53
54    @Override
55    public int receive(byte[] receiveBuffer) throws IOException {
56        synchronized (mReceivedPackets) {
57            while (!mClosed && mReceivedPackets.size() == 0) {
58                try {
59                    mReceivedPackets.wait(1000);
60                } catch (InterruptedException e) {}
61            }
62            if (mClosed) {
63                throw new IOException("Socket closed.");
64            }
65            byte[] arr = mReceivedPackets.remove(0);
66            System.arraycopy(arr, 0, receiveBuffer, 0, arr.length);
67            return arr.length;
68        }
69    }
70
71    public static void bind(MockLlcpSocket client, MockLlcpSocket server) {
72        client.mPairedSocket = server;
73        server.mPairedSocket = client;
74    }
75
76    @Override
77    public void connectToService(String serviceName) throws IOException {
78        throw new UnsupportedOperationException();
79    }
80
81    @Override
82    public int getRemoteMiu() {
83        throw new UnsupportedOperationException();
84    }
85
86    @Override
87    public int getRemoteRw() {
88        throw new UnsupportedOperationException();
89    }
90
91    @Override
92    public int getLocalSap() {
93        throw new UnsupportedOperationException();
94    }
95
96    @Override
97    public int getLocalMiu() {
98        throw new UnsupportedOperationException();
99    }
100
101    @Override
102    public int getLocalRw() {
103        throw new UnsupportedOperationException();
104    }
105}