summaryrefslogtreecommitdiffstats
path: root/core/java/com/trustedlogic/trustednfc/android/P2pInitiator.java
blob: 0f28ae0131d86d6645577f8db890c2ef4c614509 (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
/*
 * Copyright (C) 2010 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.
 */

/**
 * File            : P2PInitiator.java
 * Original-Author : Trusted Logic S.A. (Daniel Tomas)
 */

package com.trustedlogic.trustednfc.android;

import java.io.IOException;

import com.trustedlogic.trustednfc.android.internal.ErrorCodes;

import android.os.RemoteException;
import android.util.Log;

/**
 * P2pInitiator represents the initiator in an NFC-IP1 peer-to-peer
 * communication.
 * 
 * @see P2pTarget
 * @since AA02.01
 * @hide
 */
public class P2pInitiator extends P2pDevice {

    private static final String TAG = "P2pInitiator";

	/**
     * The entry point for P2P tag operations.
     * @hide
     */
	private IP2pInitiator mService;
	
    /**
     * Internal constructor for the P2pInitiator class.
     * 
     * @param handle The handle returned by the NFC service and used to identify
     * 				 the tag in subsequent calls.
     * 
     * @hide
     */
    P2pInitiator(IP2pInitiator service, int handle) {
        this.mService = service;
        this.mHandle = handle;
    }	

    /**
     * Receives data from a P2pInitiator.
     * 
     * @return data sent by the P2pInitiator.
     * @throws IOException if the target has been lost or if the connection has
     *             been closed.
     */
    public byte[] receive() throws IOException {
        try {
        	byte[] result = mService.receive(mHandle);
        	if (result == null) {
        		throw new IOException("Tag has been lost");
        	}
            return result;
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in receive(): ", e);
            return null;
        }
    }

    /**
     * Sends data to a P2pInitiator.
     * 
     * @param data data to be sent to the P2pInitiator.
     * @throws IOException if the target has been lost or if the connection has
     *             been closed.
     */
    public void send(byte[] data) throws IOException {
        try {
        	boolean isSuccess = mService.send(mHandle, data);
        	if (!isSuccess) {
        		throw new IOException("Tag has been lost");
        	}
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in send(): ", e);
        }
    }

    @Override
    public byte[] getGeneralBytes() {
        try {
            return mService.getGeneralBytes(mHandle);
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in getGeneralBytes(): ", e);
            return null;
        }
    }

    @Override
    public int getMode() {
        return P2pDevice.MODE_P2P_INITIATOR;
    }

}