summaryrefslogtreecommitdiffstats
path: root/core/java/com/trustedlogic/trustednfc/android/LlcpServiceSocket.java
blob: a152ecb1440b68bab07edd163f6198258e7b5196 (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
/*
 * 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            : LLCPServerSocket.java
 * Original-Author : Trusted Logic S.A. (Daniel Tomas)
 * Created         : 18-02-2010
 */

package com.trustedlogic.trustednfc.android;

import java.io.IOException;

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

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

/**
 * LlcpServiceSocket represents a LLCP Service to be used in a
 * Connection-oriented communication
 * 
 * @since AA02.01
 * @hide
 */
public class LlcpServiceSocket {

	private static final String TAG = "LlcpServiceSocket";

	/**
	 * The handle returned by the NFC service and used to identify the LLCP
	 * Service socket in every call of this class.
	 * 
	 * @hide
	 */
	protected int mHandle;

	/**
	 * The entry point for LLCP Service socket operations.
	 * 
	 * @hide
	 */
	protected ILlcpServiceSocket mService;
	
    private ILlcpSocket mLlcpSocketService;

	static LlcpException convertErrorToLlcpException(int errorCode) {
		return convertErrorToLlcpException(errorCode, null);
	}

	static LlcpException convertErrorToLlcpException(int errorCode,
			String message) {
		if (message == null) {
			message = "";
		} else {
			message = " (" + message + ")";
		}

		switch (errorCode) {
		case ErrorCodes.ERROR_SOCKET_CREATION:
			return new LlcpException(
					"Error during the creation of an Llcp socket" + message);
		case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES:
			return new LlcpException("Not enough ressources are available"
					+ message);
		default:
			return new LlcpException("Unkown error code " + errorCode + message);
		}
	}

	/**
	 * Internal constructor for the LlcpServiceSocket class.
	 * 
	 * @param service
	 *            The entry point to the Nfc Service for LlcpServiceSocket
	 *            class.
	 * @param handle
	 *            The handle returned by the NFC service and used to identify
	 *            the socket in subsequent calls.
	 * @hide
	 */
	LlcpServiceSocket(ILlcpServiceSocket service, ILlcpSocket socketService, int handle) {
		this.mService = service;
		this.mHandle = handle;
		this.mLlcpSocketService = socketService;
	}

	/**
	 * Wait for incomming connection request from a LLCP client and accept this
	 * request
	 * 
	 * @return socket object to be used to communicate with a LLCP client
	 * 
	 * @throws IOException
	 *             if the llcp link is lost or deactivated
	 * @throws LlcpException
	 *             if not enough ressources are available
	 * 
	 * @see LlcpSocket
	 * @since AA02.01
	 */
	public LlcpSocket accept() throws IOException, LlcpException {

		try {
			int handle = mService.accept(mHandle);
			// Handle potential errors
			if (ErrorCodes.isError(handle)) {
				if (handle == ErrorCodes.ERROR_IO) {
					throw new IOException();
				} else {
					throw convertErrorToLlcpException(handle);
				}
			}
	        
			// Build the public LlcpSocket object
			return new LlcpSocket(mLlcpSocketService, handle);
		} catch (RemoteException e) {
			Log.e(TAG, "RemoteException in accept(): ", e);
			return null;
		}

	}

	/**
	 * Set the timeout for the accept request
	 * 
	 * @param timeout
	 *            value of the timeout for the accept request
	 * @since AA02.01
	 */
	public void setAcceptTimeout(int timeout) {
		try {
			mService.setAcceptTimeout(mHandle, timeout);
		} catch (RemoteException e) {
			Log.e(TAG, "RemoteException in setAcceptTimeout(): ", e);
		}
	}

	/**
	 * Get the timeout value of the accept request
	 * 
	 * @return mTimeout
	 * @since AA02.01
	 */
	public int getAcceptTimeout() {
		try {
			return mService.getAcceptTimeout(mHandle);
		} catch (RemoteException e) {
			Log.e(TAG, "RemoteException in getAcceptTimeout(): ", e);
			return 0;
		}
	}

	/**
	 * Close the created Llcp Service socket
	 * 
	 * @since AA02.01
	 */
	public void close() {
		try {
			mService.close(mHandle);
		} catch (RemoteException e) {
			Log.e(TAG, "RemoteException in close(): ", e);
		}
	}

}