diff options
author | Jeff Hamilton <jham@android.com> | 2011-06-29 22:01:44 -0500 |
---|---|---|
committer | Jeff Hamilton <jham@android.com> | 2011-08-01 23:26:58 -0500 |
commit | 4a61d3b45e81c0070538f94747a70a49c78f12fa (patch) | |
tree | 4939ec54a0e6527b942126023a393f9c0f85ad35 | |
parent | 5ce39aec169a275e814286372256277819593829 (diff) | |
download | packages_apps_nfc-4a61d3b45e81c0070538f94747a70a49c78f12fa.zip packages_apps_nfc-4a61d3b45e81c0070538f94747a70a49c78f12fa.tar.gz packages_apps_nfc-4a61d3b45e81c0070538f94747a70a49c78f12fa.tar.bz2 |
Remove the binder interface for LLCP.
Change-Id: I8d5fd546ecb07f005322eb5f173975dff7820439
-rw-r--r-- | src/com/android/nfc/DeviceHost.java | 42 | ||||
-rw-r--r-- | src/com/android/nfc/LlcpException.java | 39 | ||||
-rwxr-xr-x | src/com/android/nfc/NfcService.java | 829 | ||||
-rwxr-xr-x | src/com/android/nfc/ndefpush/NdefPushClient.java | 12 | ||||
-rwxr-xr-x | src/com/android/nfc/ndefpush/NdefPushServer.java | 39 | ||||
-rwxr-xr-x | src/com/android/nfc/nxp/NativeLlcpServiceSocket.java | 35 | ||||
-rwxr-xr-x | src/com/android/nfc/nxp/NativeLlcpSocket.java | 65 | ||||
-rwxr-xr-x | src/com/android/nfc/nxp/NativeNfcManager.java | 57 | ||||
-rw-r--r-- | src/com/android/nfc/snep/SnepClient.java | 14 | ||||
-rw-r--r-- | src/com/android/nfc/snep/SnepMessenger.java | 10 | ||||
-rw-r--r-- | src/com/android/nfc/snep/SnepServer.java | 58 | ||||
-rw-r--r-- | tests/src/com/android/nfc/MockLlcpSocket.java | 46 | ||||
-rw-r--r-- | tests/src/com/android/nfc/snep/SnepBasicTests.java | 20 |
13 files changed, 332 insertions, 934 deletions
diff --git a/src/com/android/nfc/DeviceHost.java b/src/com/android/nfc/DeviceHost.java index 9693446..d1e7064 100644 --- a/src/com/android/nfc/DeviceHost.java +++ b/src/com/android/nfc/DeviceHost.java @@ -23,6 +23,8 @@ import com.android.nfc.nxp.NativeLlcpSocket; import android.nfc.NdefMessage; import android.os.Bundle; +import java.io.IOException; + public interface DeviceHost { public interface DeviceHostListener { public void onRemoteEndpointDiscovered(TagEndpoint tag); @@ -120,6 +122,34 @@ public interface DeviceHost { public byte[] getGeneralBytes(); } + public interface LlcpSocket { + public void connectToSap(int sap) throws IOException; + + public void connectToService(String serviceName) throws IOException; + + public void close() throws IOException; + + public void send(byte[] data) throws IOException; + + public int receive(byte[] recvBuff) throws IOException; + + public int getRemoteMiu(); + + public int getRemoteRw(); + + public int getLocalSap(); + + public int getLocalMiu(); + + public int getLocalRw(); + } + + public interface LlcpServerSocket { + public LlcpSocket accept() throws IOException, LlcpException; + + public void close() throws IOException; + } + public boolean initialize(); public boolean deinitialize(); @@ -136,13 +166,11 @@ public interface DeviceHost { public int doGetLastError(); - public NativeLlcpConnectionlessSocket doCreateLlcpConnectionlessSocket(int nSap); + public LlcpServerSocket createLlcpServerSocket(int nSap, String sn, int miu, + int rw, int linearBufferLength) throws LlcpException; - public NativeLlcpServiceSocket doCreateLlcpServiceSocket(int nSap, String sn, int miu, - int rw, int linearBufferLength); - - public NativeLlcpSocket doCreateLlcpSocket(int sap, int miu, int rw, - int linearBufferLength); + public LlcpSocket createLlcpSocket(int sap, int miu, int rw, + int linearBufferLength) throws LlcpException; public boolean doCheckLlcp(); @@ -153,4 +181,6 @@ public interface DeviceHost { public boolean setTimeout(int technology, int timeout); public int getTimeout(int technology); + + public void doAbort(); } diff --git a/src/com/android/nfc/LlcpException.java b/src/com/android/nfc/LlcpException.java new file mode 100644 index 0000000..9757087 --- /dev/null +++ b/src/com/android/nfc/LlcpException.java @@ -0,0 +1,39 @@ +/* + * 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. + */ + +package com.android.nfc; + +import android.nfc.ErrorCodes; + +/** + * Generic exception thrown in case something unexpected happened during an + * LLCP communication. + */ +public class LlcpException extends Exception { + /** + * Constructs a new LlcpException with the current stack trace and the + * specified detail message. + * + * @param s the detail message for this exception. + */ + public LlcpException(String s) { + super(s); + } + + public LlcpException(int error) { + super(ErrorCodes.asString(error)); + } +} diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java index f4714c9..7e5f8d2 100755 --- a/src/com/android/nfc/NfcService.java +++ b/src/com/android/nfc/NfcService.java @@ -16,12 +16,11 @@ package com.android.nfc; -import com.android.internal.nfc.LlcpServiceSocket; -import com.android.internal.nfc.LlcpSocket; import com.android.nfc.DeviceHost.DeviceHostListener; +import com.android.nfc.DeviceHost.LlcpServerSocket; +import com.android.nfc.DeviceHost.LlcpSocket; import com.android.nfc.DeviceHost.NfcDepEndpoint; import com.android.nfc.DeviceHost.TagEndpoint; -import com.android.nfc.nxp.NativeLlcpConnectionlessSocket; import com.android.nfc.nxp.NativeLlcpServiceSocket; import com.android.nfc.nxp.NativeLlcpSocket; import com.android.nfc.nxp.NativeNfcManager; @@ -45,16 +44,10 @@ import android.media.SoundPool; import android.net.Uri; import android.nfc.ErrorCodes; import android.nfc.FormatException; -import android.nfc.ILlcpConnectionlessSocket; -import android.nfc.ILlcpServiceSocket; -import android.nfc.ILlcpSocket; import android.nfc.INdefPushCallback; import android.nfc.INfcAdapter; import android.nfc.INfcAdapterExtras; import android.nfc.INfcTag; -import android.nfc.IP2pInitiator; -import android.nfc.IP2pTarget; -import android.nfc.LlcpPacket; import android.nfc.NdefMessage; import android.nfc.NfcAdapter; import android.nfc.Tag; @@ -174,13 +167,12 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt // fields below are used in multiple threads and protected by synchronized(this) private final HashMap<Integer, Object> mObjectMap = new HashMap<Integer, Object>(); - private final HashMap<Integer, Object> mSocketMap = new HashMap<Integer, Object>(); private boolean mScreenUnlocked; private HashSet<String> mSePackages = new HashSet<String>(); // fields below are final after onCreate() Context mContext; - private NativeNfcManager mDeviceHost; + private DeviceHost mDeviceHost; private SharedPreferences mPrefs; private SharedPreferences.Editor mPrefsEditor; private PowerManager.WakeLock mWakeLock; @@ -190,6 +182,9 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt int mEndSound; int mErrorSound; SoundPool mSoundPool; // playback synchronized on this + TagService mNfcTagService; + NfcAdapterService mNfcAdapter; + NfcAdapterExtrasService mExtrasService; private NfcDispatcher mNfcDispatcher; private KeyguardManager mKeyguard; @@ -279,6 +274,9 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt public void onCreate() { super.onCreate(); + mNfcTagService = new TagService(); + mNfcAdapter = new NfcAdapterService(); + Log.i(TAG, "Starting NFC service"); sService = this; @@ -290,7 +288,6 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt mContext = this; mDeviceHost = new NativeNfcManager(this, this); - mDeviceHost.initializeNativeStructure(); mP2pManager = new NdefP2pManager(this, this); mNfcDispatcher = new NfcDispatcher(this, mP2pManager); @@ -399,7 +396,7 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt Log.wtf(TAG, "NFC service is under attack!"); } - private final INfcAdapter.Stub mNfcAdapter = new INfcAdapter.Stub() { + final class NfcAdapterService extends INfcAdapter.Stub { /** Protected by "this" */ NdefMessage mLocalMessage = null; @@ -539,160 +536,12 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt } @Override - public int createLlcpConnectionlessSocket(int sap) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* Check SAP is not already used */ - - /* Store the socket handle */ - int sockeHandle = mGeneratedSocketHandle; - NativeLlcpConnectionlessSocket socket; - - socket = mDeviceHost.doCreateLlcpConnectionlessSocket(sap); - if (socket != null) { - synchronized(NfcService.this) { - /* update socket handle generation */ - mGeneratedSocketHandle++; - - /* Add the socket into the socket map */ - mSocketMap.put(mGeneratedSocketHandle, socket); - return mGeneratedSocketHandle; - } - } else { - /* Get Error Status */ - int errorStatus = mDeviceHost.doGetLastError(); - - switch (errorStatus) { - case ErrorCodes.ERROR_BUFFER_TO_SMALL: - return ErrorCodes.ERROR_BUFFER_TO_SMALL; - case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES: - return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES; - default: - return ErrorCodes.ERROR_SOCKET_CREATION; - } - } - } - - @Override - public int createLlcpServiceSocket(int sap, String sn, int miu, int rw, int linearBufferLength) - throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - NativeLlcpServiceSocket socket; - - socket = mDeviceHost.doCreateLlcpServiceSocket(sap, sn, miu, rw, linearBufferLength); - if (socket != null) { - synchronized(NfcService.this) { - /* update socket handle generation */ - mGeneratedSocketHandle++; - - /* Add the socket into the socket map */ - mSocketMap.put(mGeneratedSocketHandle, socket); - return mGeneratedSocketHandle; - } - } else { - /* Get Error Status */ - int errorStatus = mDeviceHost.doGetLastError(); - - switch (errorStatus) { - case ErrorCodes.ERROR_BUFFER_TO_SMALL: - return ErrorCodes.ERROR_BUFFER_TO_SMALL; - case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES: - return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES; - default: - return ErrorCodes.ERROR_SOCKET_CREATION; - } - } - } - - @Override - public int createLlcpSocket(int sap, int miu, int rw, int linearBufferLength) - throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - if (DBG) Log.d(TAG, "creating llcp socket"); - NativeLlcpSocket socket; - - socket = mDeviceHost.doCreateLlcpSocket(sap, miu, rw, linearBufferLength); - - if (socket != null) { - synchronized(NfcService.this) { - /* update socket handle generation */ - mGeneratedSocketHandle++; - - /* Add the socket into the socket map */ - mSocketMap.put(mGeneratedSocketHandle, socket); - return mGeneratedSocketHandle; - } - } else { - /* Get Error Status */ - int errorStatus = mDeviceHost.doGetLastError(); - - Log.d(TAG, "failed to create llcp socket: " + ErrorCodes.asString(errorStatus)); - - switch (errorStatus) { - case ErrorCodes.ERROR_BUFFER_TO_SMALL: - return ErrorCodes.ERROR_BUFFER_TO_SMALL; - case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES: - return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES; - default: - return ErrorCodes.ERROR_SOCKET_CREATION; - } - } - } - - @Override - public ILlcpConnectionlessSocket getLlcpConnectionlessInterface() throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - return mLlcpConnectionlessSocketService; - } - - @Override - public ILlcpSocket getLlcpInterface() throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - return mLlcpSocket; - } - - @Override - public ILlcpServiceSocket getLlcpServiceInterface() throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - return mLlcpServerSocketService; - } - - @Override public INfcTag getNfcTagInterface() throws RemoteException { mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); return mNfcTagService; } @Override - public IP2pInitiator getP2pInitiatorInterface() throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - return mP2pInitiatorService; - } - - @Override - public IP2pTarget getP2pTargetInterface() throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - return mP2pTargetService; - } - - @Override public INfcAdapterExtras getNfcAdapterExtrasInterface() { NfcService.enforceNfceeAdminPerm(mContext); return mExtrasService; @@ -704,416 +553,7 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt } }; - private final ILlcpSocket mLlcpSocket = new ILlcpSocket.Stub() { - - private NativeLlcpSocket findSocket(int nativeHandle) { - Object socket = NfcService.this.findSocket(nativeHandle); - if (!(socket instanceof NativeLlcpSocket)) { - return null; - } - return (NativeLlcpSocket) socket; - } - - @Override - public int close(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - socket.doClose(); - /* Remove the socket closed from the hmap */ - removeSocket(nativeHandle); - return ErrorCodes.SUCCESS; - } else { - return ErrorCodes.ERROR_IO; - } - } - - @Override - public int connect(int nativeHandle, int sap) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - boolean isSuccess = false; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - isSuccess = socket.doConnect(sap); - if (isSuccess) { - return ErrorCodes.SUCCESS; - } else { - return ErrorCodes.ERROR_IO; - } - } else { - return ErrorCodes.ERROR_IO; - } - - } - - @Override - public int connectByName(int nativeHandle, String sn) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - boolean isSuccess = false; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - isSuccess = socket.doConnectBy(sn); - if (isSuccess) { - return ErrorCodes.SUCCESS; - } else { - return ErrorCodes.ERROR_IO; - } - } else { - return ErrorCodes.ERROR_IO; - } - - } - - @Override - public int getLocalSap(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - return socket.getSap(); - } else { - return 0; - } - } - - @Override - public int getLocalSocketMiu(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - return socket.getMiu(); - } else { - return 0; - } - } - - @Override - public int getLocalSocketRw(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - return socket.getRw(); - } else { - return 0; - } - } - - @Override - public int getRemoteSocketMiu(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - if (socket.doGetRemoteSocketMiu() != 0) { - return socket.doGetRemoteSocketMiu(); - } else { - return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED; - } - } else { - return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED; - } - } - - @Override - public int getRemoteSocketRw(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - if (socket.doGetRemoteSocketRw() != 0) { - return socket.doGetRemoteSocketRw(); - } else { - return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED; - } - } else { - return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED; - } - } - - @Override - public int receive(int nativeHandle, byte[] receiveBuffer) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - return socket.doReceive(receiveBuffer); - } else { - return 0; - } - } - - @Override - public int send(int nativeHandle, byte[] data) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpSocket socket = null; - boolean isSuccess = false; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - isSuccess = socket.doSend(data); - if (isSuccess) { - return ErrorCodes.SUCCESS; - } else { - return ErrorCodes.ERROR_IO; - } - } else { - return ErrorCodes.ERROR_IO; - } - } - }; - - private final ILlcpServiceSocket mLlcpServerSocketService = new ILlcpServiceSocket.Stub() { - - private NativeLlcpServiceSocket findSocket(int nativeHandle) { - Object socket = NfcService.this.findSocket(nativeHandle); - if (!(socket instanceof NativeLlcpServiceSocket)) { - return null; - } - return (NativeLlcpServiceSocket) socket; - } - - @Override - public int accept(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpServiceSocket socket = null; - NativeLlcpSocket clientSocket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - clientSocket = socket.doAccept(socket.getMiu(), - socket.getRw(), socket.getLinearBufferLength()); - if (clientSocket != null) { - /* Add the socket into the socket map */ - synchronized(this) { - mGeneratedSocketHandle++; - mSocketMap.put(mGeneratedSocketHandle, clientSocket); - return mGeneratedSocketHandle; - } - } else { - return ErrorCodes.ERROR_IO; - } - } else { - return ErrorCodes.ERROR_IO; - } - } - - @Override - public void close(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpServiceSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - socket.doClose(); - synchronized (this) { - /* Remove the socket closed from the hmap */ - removeSocket(nativeHandle); - } - } - } - }; - - private final ILlcpConnectionlessSocket mLlcpConnectionlessSocketService = new ILlcpConnectionlessSocket.Stub() { - - private NativeLlcpConnectionlessSocket findSocket(int nativeHandle) { - Object socket = NfcService.this.findSocket(nativeHandle); - if (!(socket instanceof NativeLlcpConnectionlessSocket)) { - return null; - } - return (NativeLlcpConnectionlessSocket) socket; - } - - @Override - public void close(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpConnectionlessSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - socket.doClose(); - /* Remove the socket closed from the hmap */ - removeSocket(nativeHandle); - } - } - - @Override - public int getSap(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpConnectionlessSocket socket = null; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - return socket.getSap(); - } else { - return 0; - } - } - - @Override - public LlcpPacket receiveFrom(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpConnectionlessSocket socket = null; - LlcpPacket packet; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return null; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - packet = socket.doReceiveFrom(socket.getLinkMiu()); - if (packet != null) { - return packet; - } - return null; - } else { - return null; - } - } - - @Override - public int sendTo(int nativeHandle, LlcpPacket packet) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NativeLlcpConnectionlessSocket socket = null; - boolean isSuccess = false; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the socket in the hmap */ - socket = findSocket(nativeHandle); - if (socket != null) { - isSuccess = socket.doSendTo(packet.getRemoteSap(), packet.getDataBuffer()); - if (isSuccess) { - return ErrorCodes.SUCCESS; - } else { - return ErrorCodes.ERROR_IO; - } - } else { - return ErrorCodes.ERROR_IO; - } - } - }; - - private final INfcTag mNfcTagService = new INfcTag.Stub() { + final class TagService extends INfcTag.Stub { @Override public int close(int nativeHandle) throws RemoteException { @@ -1431,7 +871,7 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt NdefMessage[] msgs = tag.findAndReadNdef(); // Build a new Tag object to return Tag newTag = new Tag(tag.getUid(), tag.getTechList(), - tag.getTechExtras(), tag.getHandle(), mNfcTagService); + tag.getTechExtras(), tag.getHandle(), this); return newTag; } return null; @@ -1463,206 +903,6 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt } }; - private final IP2pInitiator mP2pInitiatorService = new IP2pInitiator.Stub() { - - @Override - public byte[] getGeneralBytes(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return null; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - byte[] buff = device.getGeneralBytes(); - if (buff == null) - return null; - return buff; - } - return null; - } - - @Override - public int getMode(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - return device.getMode(); - } - return ErrorCodes.ERROR_INVALID_PARAM; - } - - @Override - public byte[] receive(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return null; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - byte[] buff = device.receive(); - if (buff == null) { - return null; - } - return buff; - } - /* Restart polling loop for notification */ - applyRouting(); - return null; - } - - @Override - public boolean send(int nativeHandle, byte[] data) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - boolean isSuccess = false; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return isSuccess; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - isSuccess = device.send(data); - } - return isSuccess; - } - }; - - private final IP2pTarget mP2pTargetService = new IP2pTarget.Stub() { - - @Override - public int connect(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - if (device.connect()) { - return ErrorCodes.SUCCESS; - } - } - return ErrorCodes.ERROR_CONNECT; - } - - @Override - public boolean disconnect(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - boolean isSuccess = false; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return isSuccess; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - if (isSuccess = device.disconnect()) { - /* remove the device from the hmap */ - unregisterObject(nativeHandle); - /* Restart polling loop for notification */ - applyRouting(); - } - } - return isSuccess; - - } - - @Override - public byte[] getGeneralBytes(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return null; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - byte[] buff = device.getGeneralBytes(); - if (buff == null) - return null; - return buff; - } - return null; - } - - @Override - public int getMode(int nativeHandle) throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return ErrorCodes.ERROR_NOT_INITIALIZED; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - return device.getMode(); - } - return ErrorCodes.ERROR_INVALID_PARAM; - } - - @Override - public byte[] transceive(int nativeHandle, byte[] data) - throws RemoteException { - mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR); - - NfcDepEndpoint device; - - // Check if NFC is enabled - if (!mIsNfcEnabled) { - return null; - } - - /* find the device in the hmap */ - device = (NfcDepEndpoint) findObject(nativeHandle); - if (device != null) { - return device.transceive(data); - } - return null; - } - }; - private void _nfcEeClose(boolean checkPid, int callingPid) throws IOException { // Blocks until a pending open() or transceive() times out. //TODO: This is incorrect behavior - the close should interrupt pending @@ -1687,7 +927,7 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt } } - private INfcAdapterExtras mExtrasService = new INfcAdapterExtras.Stub() { + final class NfcAdapterExtrasService extends INfcAdapterExtras.Stub { private Bundle writeNoException() { Bundle p = new Bundle(); p.putInt("e", 0); @@ -2093,7 +1333,6 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt // Clear tables mObjectMap.clear(); - mSocketMap.clear(); // Reset variables mIsNfcEnabled = false; @@ -2118,46 +1357,16 @@ public class NfcService extends Application implements DeviceHostListener, P2pSt mObjectMap.remove(handle); } - private synchronized Object findSocket(int key) { - if (mSocketMap == null) { - return null; - } - return mSocketMap.get(key); - } - - private void removeSocket(int key) { - mSocketMap.remove(key); - } - /** For use by code in this process */ - public LlcpSocket createLlcpSocket(int sap, int miu, int rw, int linearBufferLength) { - try { - int handle = mNfcAdapter.createLlcpSocket(sap, miu, rw, linearBufferLength); - if (ErrorCodes.isError(handle)) { - Log.e(TAG, "unable to create socket: " + ErrorCodes.asString(handle)); - return null; - } - return new LlcpSocket(mLlcpSocket, handle); - } catch (RemoteException e) { - // This will never happen since the code is calling into it's own process - throw new IllegalStateException("unable to talk to myself", e); - } + public LlcpSocket createLlcpSocket(int sap, int miu, int rw, int linearBufferLength) + throws IOException, LlcpException { + return mDeviceHost.createLlcpSocket(sap, miu, rw, linearBufferLength); } /** For use by code in this process */ - public LlcpServiceSocket createLlcpServiceSocket(int sap, String sn, int miu, int rw, - int linearBufferLength) { - try { - int handle = mNfcAdapter.createLlcpServiceSocket(sap, sn, miu, rw, linearBufferLength); - if (ErrorCodes.isError(handle)) { - Log.e(TAG, "unable to create socket: " + ErrorCodes.asString(handle)); - return null; - } - return new LlcpServiceSocket(mLlcpServerSocketService, mLlcpSocket, handle); - } catch (RemoteException e) { - // This will never happen since the code is calling into it's own process - throw new IllegalStateException("unable to talk to myself", e); - } + public LlcpServerSocket createLlcpServerSocket(int sap, String sn, int miu, int rw, + int linearBufferLength) throws IOException, LlcpException { + return mDeviceHost.createLlcpServerSocket(sap, sn, miu, rw, linearBufferLength); } public void sendMockNdefTag(NdefMessage msg) { diff --git a/src/com/android/nfc/ndefpush/NdefPushClient.java b/src/com/android/nfc/ndefpush/NdefPushClient.java index 1bb1bfe..e48b0af 100755 --- a/src/com/android/nfc/ndefpush/NdefPushClient.java +++ b/src/com/android/nfc/ndefpush/NdefPushClient.java @@ -16,13 +16,13 @@ package com.android.nfc.ndefpush; +import com.android.nfc.DeviceHost.LlcpSocket; +import com.android.nfc.LlcpException; +import com.android.nfc.NfcService; + import android.nfc.NdefMessage; import android.util.Log; -import com.android.internal.nfc.LlcpException; -import com.android.internal.nfc.LlcpSocket; -import com.android.nfc.NfcService; - import java.io.IOException; import java.util.Arrays; @@ -52,9 +52,9 @@ public class NdefPushClient { throw new IOException("Could not connect to socket."); } if (DBG) Log.d(TAG, "about to connect to service " + NdefPushServer.SERVICE_NAME); - sock.connect(NdefPushServer.SERVICE_NAME); + sock.connectToService(NdefPushServer.SERVICE_NAME); - remoteMiu = sock.getRemoteSocketMiu(); + remoteMiu = sock.getRemoteMiu(); if (DBG) Log.d(TAG, "about to send a " + buffer.length + " byte message"); while (offset < buffer.length) { int length = Math.min(buffer.length - offset, remoteMiu); diff --git a/src/com/android/nfc/ndefpush/NdefPushServer.java b/src/com/android/nfc/ndefpush/NdefPushServer.java index c8728e9..2f4d6f4 100755 --- a/src/com/android/nfc/ndefpush/NdefPushServer.java +++ b/src/com/android/nfc/ndefpush/NdefPushServer.java @@ -16,9 +16,9 @@ package com.android.nfc.ndefpush; -import com.android.internal.nfc.LlcpException; -import com.android.internal.nfc.LlcpServiceSocket; -import com.android.internal.nfc.LlcpSocket; +import com.android.nfc.DeviceHost.LlcpServerSocket; +import com.android.nfc.DeviceHost.LlcpSocket; +import com.android.nfc.LlcpException; import com.android.nfc.NfcService; import android.nfc.FormatException; @@ -30,14 +30,15 @@ import java.io.IOException; /** * A simple server that accepts NDEF messages pushed to it over an LLCP connection. Those messages - * are typically set on the client side by using {@link NfcAdapter#setLocalNdefMessage}. + * are typically set on the client side by using {@link NfcAdapter#enableForegroundNdefPush}. */ public class NdefPushServer { private static final String TAG = "NdefPushServer"; private static final boolean DBG = true; private static final int MIU = 248; - private static int mSap; + + int mSap; static final String SERVICE_NAME = "com.android.npp"; @@ -109,20 +110,20 @@ public class NdefPushServer { /** Server class, used to listen for incoming connection request */ class ServerThread extends Thread { boolean mRunning = true; - LlcpServiceSocket mServerSocket; + LlcpServerSocket mServerSocket; @Override public void run() { while (mRunning) { if (DBG) Log.d(TAG, "about create LLCP service socket"); - mServerSocket = mService.createLlcpServiceSocket(mSap, SERVICE_NAME, - MIU, 1, 1024); - if (mServerSocket == null) { - if (DBG) Log.d(TAG, "failed to create LLCP service socket"); - return; - } - if (DBG) Log.d(TAG, "created LLCP service socket"); try { + mServerSocket = mService.createLlcpServerSocket(mSap, SERVICE_NAME, + MIU, 1, 1024); + if (mServerSocket == null) { + if (DBG) Log.d(TAG, "failed to create LLCP service socket"); + return; + } + if (DBG) Log.d(TAG, "created LLCP service socket"); while (mRunning) { if (DBG) Log.d(TAG, "about to accept"); LlcpSocket communicationSocket = mServerSocket.accept(); @@ -139,7 +140,11 @@ public class NdefPushServer { } finally { if (mServerSocket != null) { if (DBG) Log.d(TAG, "about to close"); - mServerSocket.close(); + try { + mServerSocket.close(); + } catch (IOException e) { + // ignore + } mServerSocket = null; } } @@ -149,7 +154,11 @@ public class NdefPushServer { public void shutdown() { mRunning = false; if (mServerSocket != null) { - mServerSocket.close(); + try { + mServerSocket.close(); + } catch (IOException e) { + // ignore + } mServerSocket = null; } } diff --git a/src/com/android/nfc/nxp/NativeLlcpServiceSocket.java b/src/com/android/nfc/nxp/NativeLlcpServiceSocket.java index 2eb8ab7..531afd8 100755 --- a/src/com/android/nfc/nxp/NativeLlcpServiceSocket.java +++ b/src/com/android/nfc/nxp/NativeLlcpServiceSocket.java @@ -16,11 +16,16 @@ package com.android.nfc.nxp; +import com.android.nfc.DeviceHost; +import com.android.nfc.DeviceHost.LlcpSocket; + +import java.io.IOException; + /** * LlcpServiceSocket represents a LLCP Service to be used in a * Connection-oriented communication */ -public class NativeLlcpServiceSocket { +public class NativeLlcpServiceSocket implements DeviceHost.LlcpServerSocket { private int mHandle; private int mLocalMiu; private int mLocalRw; @@ -30,23 +35,19 @@ public class NativeLlcpServiceSocket { public NativeLlcpServiceSocket(){ } - public native NativeLlcpSocket doAccept(int miu, int rw, int linearBufferLength); - - public native boolean doClose(); - - public int getHandle(){ - return mHandle; - } - - public int getRw(){ - return mLocalRw; - } - - public int getMiu(){ - return mLocalMiu; + private native NativeLlcpSocket doAccept(int miu, int rw, int linearBufferLength); + @Override + public LlcpSocket accept() throws IOException { + LlcpSocket socket = doAccept(mLocalMiu, mLocalRw, mLocalLinearBufferLength); + if (socket == null) throw new IOException(); + return socket; } - public int getLinearBufferLength(){ - return mLocalLinearBufferLength; + private native boolean doClose(); + @Override + public void close() throws IOException { + if (!doClose()) { + throw new IOException(); + } } } diff --git a/src/com/android/nfc/nxp/NativeLlcpSocket.java b/src/com/android/nfc/nxp/NativeLlcpSocket.java index 4132c3a..84d9dd3 100755 --- a/src/com/android/nfc/nxp/NativeLlcpSocket.java +++ b/src/com/android/nfc/nxp/NativeLlcpSocket.java @@ -16,11 +16,15 @@ package com.android.nfc.nxp; +import com.android.nfc.DeviceHost; + +import java.io.IOException; + /** * LlcpClientSocket represents a LLCP Connection-Oriented client to be used in a * connection-oriented communication */ -public class NativeLlcpSocket { +public class NativeLlcpSocket implements DeviceHost.LlcpSocket { private int mHandle; private int mSap; private int mLocalMiu; @@ -28,33 +32,64 @@ public class NativeLlcpSocket { public NativeLlcpSocket(){ } - public native boolean doConnect(int nSap); + private native boolean doConnect(int nSap); + @Override + public void connectToSap(int sap) throws IOException { + if (!doConnect(sap)) { + throw new IOException(); + } + } - public native boolean doConnectBy(String sn); + private native boolean doConnectBy(String sn); + @Override + public void connectToService(String serviceName) throws IOException { + if (!doConnectBy(serviceName)) { + throw new IOException(); + } + } - public native boolean doClose(); + private native boolean doClose(); + @Override + public void close() throws IOException { + if (!doClose()) { + throw new IOException(); + } + } - public native boolean doSend(byte[] data); + private native boolean doSend(byte[] data); + @Override + public void send(byte[] data) throws IOException { + if (!doSend(data)) { + throw new IOException(); + } + } - public native int doReceive(byte[] recvBuff); + private native int doReceive(byte[] recvBuff); + @Override + public int receive(byte[] recvBuff) throws IOException { + return doReceive(recvBuff); + } - public native int doGetRemoteSocketMiu(); + private native int doGetRemoteSocketMiu(); + @Override + public int getRemoteMiu() { return doGetRemoteSocketMiu(); } - public native int doGetRemoteSocketRw(); + private native int doGetRemoteSocketRw(); + @Override + public int getRemoteRw() { return doGetRemoteSocketRw(); } - public int getSap(){ + @Override + public int getLocalSap(){ return mSap; } - public int getMiu(){ + @Override + public int getLocalMiu(){ return mLocalMiu; } - public int getRw(){ + @Override + public int getLocalRw(){ return mLocalRw; } - - public int getHandle(){ - return mHandle; - } } diff --git a/src/com/android/nfc/nxp/NativeNfcManager.java b/src/com/android/nfc/nxp/NativeNfcManager.java index 8a59370..bce9fa9 100755 --- a/src/com/android/nfc/nxp/NativeNfcManager.java +++ b/src/com/android/nfc/nxp/NativeNfcManager.java @@ -17,15 +17,20 @@ package com.android.nfc.nxp; import com.android.nfc.DeviceHost; +import com.android.nfc.LlcpException; import android.annotation.SdkConstant; import android.annotation.SdkConstant.SdkConstantType; import android.content.Context; +import android.nfc.ErrorCodes; +import android.util.Log; /** * Native interface to the NFC Manager functions */ public class NativeNfcManager implements DeviceHost { + private static final String TAG = "NativeNfcManager"; + static { System.loadLibrary("nfc_jni"); } @@ -40,6 +45,7 @@ public class NativeNfcManager implements DeviceHost { public NativeNfcManager(Context context, DeviceHostListener listener) { mListener = listener; + initializeNativeStructure(); } public native boolean initializeNativeStructure(); @@ -68,16 +74,55 @@ public class NativeNfcManager implements DeviceHost { @Override public native int doGetLastError(); - @Override - public native NativeLlcpConnectionlessSocket doCreateLlcpConnectionlessSocket(int nSap); + private native NativeLlcpConnectionlessSocket doCreateLlcpConnectionlessSocket(int nSap); - @Override - public native NativeLlcpServiceSocket doCreateLlcpServiceSocket(int nSap, String sn, int miu, + private native NativeLlcpServiceSocket doCreateLlcpServiceSocket(int nSap, String sn, int miu, int rw, int linearBufferLength); - @Override - public native NativeLlcpSocket doCreateLlcpSocket(int sap, int miu, int rw, + public LlcpServerSocket createLlcpServerSocket(int nSap, String sn, int miu, + int rw, int linearBufferLength) throws LlcpException { + LlcpServerSocket socket = doCreateLlcpServiceSocket(nSap, sn, miu, rw, linearBufferLength); + if (socket != null) { + return socket; + } else { + /* Get Error Status */ + int error = doGetLastError(); + + Log.d(TAG, "failed to create llcp socket: " + ErrorCodes.asString(error)); + + switch (error) { + case ErrorCodes.ERROR_BUFFER_TO_SMALL: + case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES: + throw new LlcpException(error); + default: + throw new LlcpException(ErrorCodes.ERROR_SOCKET_CREATION); + } + } + } + + private native NativeLlcpSocket doCreateLlcpSocket(int sap, int miu, int rw, int linearBufferLength); + @Override + public LlcpSocket createLlcpSocket(int sap, int miu, int rw, + int linearBufferLength) throws LlcpException { + LlcpSocket socket = doCreateLlcpSocket(sap, miu, rw, linearBufferLength); + if (socket != null) { + return socket; + } else { + /* Get Error Status */ + int error = doGetLastError(); + + Log.d(TAG, "failed to create llcp socket: " + ErrorCodes.asString(error)); + + switch (error) { + case ErrorCodes.ERROR_BUFFER_TO_SMALL: + case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES: + throw new LlcpException(error); + default: + throw new LlcpException(ErrorCodes.ERROR_SOCKET_CREATION); + } + } + } @Override public native boolean doCheckLlcp(); diff --git a/src/com/android/nfc/snep/SnepClient.java b/src/com/android/nfc/snep/SnepClient.java index 44c9f30..18e1b2c 100644 --- a/src/com/android/nfc/snep/SnepClient.java +++ b/src/com/android/nfc/snep/SnepClient.java @@ -16,15 +16,15 @@ package com.android.nfc.snep; -import java.io.IOException; - -import com.android.internal.nfc.LlcpException; -import com.android.internal.nfc.LlcpSocket; +import com.android.nfc.DeviceHost.LlcpSocket; +import com.android.nfc.LlcpException; import com.android.nfc.NfcService; import android.nfc.NdefMessage; import android.util.Log; +import java.io.IOException; + public final class SnepClient { private static final String TAG = "SnepClient"; private static final boolean DBG = false; @@ -128,12 +128,12 @@ public final class SnepClient { } if (mPort == -1) { if (DBG) Log.d(TAG, "about to connect to service " + mServiceName); - socket.connect(mServiceName); + socket.connectToService(mServiceName); } else { if (DBG) Log.d(TAG, "about to connect to port " + mPort); - socket.connect(mPort); + socket.connectToSap(mPort); } - int miu = socket.getRemoteSocketMiu(); + int miu = socket.getRemoteMiu(); int fragmentLength = (mFragmentLength == -1) ? miu : Math.min(miu, mFragmentLength); messenger = new SnepMessenger(true, socket, fragmentLength); } catch (LlcpException e) { diff --git a/src/com/android/nfc/snep/SnepMessenger.java b/src/com/android/nfc/snep/SnepMessenger.java index a7fd339..97ac1f0 100644 --- a/src/com/android/nfc/snep/SnepMessenger.java +++ b/src/com/android/nfc/snep/SnepMessenger.java @@ -16,17 +16,17 @@ package com.android.nfc.snep; +import com.android.nfc.DeviceHost.LlcpSocket; + +import android.nfc.FormatException; +import android.util.Log; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import java.util.Arrays; -import com.android.internal.nfc.LlcpSocket; - -import android.nfc.FormatException; -import android.util.Log; - public class SnepMessenger { private static final String TAG = "SnepMessager"; private static final boolean DBG = false; diff --git a/src/com/android/nfc/snep/SnepServer.java b/src/com/android/nfc/snep/SnepServer.java index 74bc320..84bb673 100644 --- a/src/com/android/nfc/snep/SnepServer.java +++ b/src/com/android/nfc/snep/SnepServer.java @@ -16,9 +16,9 @@ package com.android.nfc.snep; -import com.android.internal.nfc.LlcpException; -import com.android.internal.nfc.LlcpServiceSocket; -import com.android.internal.nfc.LlcpSocket; +import com.android.nfc.DeviceHost.LlcpServerSocket; +import com.android.nfc.DeviceHost.LlcpSocket; +import com.android.nfc.LlcpException; import com.android.nfc.NfcService; import android.nfc.NdefMessage; @@ -29,7 +29,7 @@ import java.io.IOException; /** * A simple server that accepts NDEF messages pushed to it over an LLCP connection. Those messages - * are typically set on the client side by using {@link NfcAdapter#setLocalNdefMessage}. + * are typically set on the client side by using {@link NfcAdapter#enableForegroundNdefPush}. */ public final class SnepServer { private static final String TAG = "SnepServer"; @@ -40,14 +40,14 @@ public final class SnepServer { public static final String DEFAULT_SERVICE_NAME = "urn:nfc:sn:snep"; - private final Callback mCallback; - private final String mServiceName; - private final int mServiceSap; - private final int mFragmentLength; + final Callback mCallback; + final String mServiceName; + final int mServiceSap; + final int mFragmentLength; /** Protected by 'this', null when stopped, non-null when running */ - private ServerThread mServerThread = null; - private boolean mServerRunning = false; + ServerThread mServerThread = null; + boolean mServerRunning = false; public interface Callback { public SnepMessage doPut(NdefMessage msg); @@ -154,7 +154,7 @@ public final class SnepServer { /** Server class, used to listen for incoming connection request */ class ServerThread extends Thread { private boolean mThreadRunning = true; - LlcpServiceSocket mServerSocket; + LlcpServerSocket mServerSocket; @Override public void run() { @@ -165,22 +165,22 @@ public final class SnepServer { while (threadRunning) { if (DBG) Log.d(TAG, "about create LLCP service socket"); - synchronized (SnepServer.this) { - mServerSocket = NfcService.getInstance().createLlcpServiceSocket(mServiceSap, - mServiceName, MIU, 1, 1024); - } - if (mServerSocket == null) { - if (DBG) Log.d(TAG, "failed to create LLCP service socket"); - return; - } - if (DBG) Log.d(TAG, "created LLCP service socket"); try { synchronized (SnepServer.this) { + mServerSocket = NfcService.getInstance().createLlcpServerSocket(mServiceSap, + mServiceName, MIU, 1, 1024); + } + if (mServerSocket == null) { + if (DBG) Log.d(TAG, "failed to create LLCP service socket"); + return; + } + if (DBG) Log.d(TAG, "created LLCP service socket"); + synchronized (SnepServer.this) { threadRunning = mThreadRunning; } while (threadRunning) { - LlcpServiceSocket serverSocket; + LlcpServerSocket serverSocket; synchronized (SnepServer.this) { serverSocket = mServerSocket; } @@ -193,7 +193,7 @@ public final class SnepServer { LlcpSocket communicationSocket = serverSocket.accept(); if (DBG) Log.d(TAG, "accept returned " + communicationSocket); if (communicationSocket != null) { - int miu = communicationSocket.getRemoteSocketMiu(); + int miu = communicationSocket.getRemoteMiu(); int fragmentLength = (mFragmentLength == -1) ? miu : Math.min(miu, mFragmentLength); new ConnectionThread(communicationSocket, fragmentLength).start(); @@ -212,7 +212,11 @@ public final class SnepServer { synchronized (SnepServer.this) { if (mServerSocket != null) { if (DBG) Log.d(TAG, "about to close"); - mServerSocket.close(); + try { + mServerSocket.close(); + } catch (IOException e) { + // ignore + } mServerSocket = null; } } @@ -224,11 +228,15 @@ public final class SnepServer { } } - private void shutdown() { + public void shutdown() { synchronized (SnepServer.this) { mThreadRunning = false; if (mServerSocket != null) { - mServerSocket.close(); + try { + mServerSocket.close(); + } catch (IOException e) { + // ignore + } mServerSocket = null; } } diff --git a/tests/src/com/android/nfc/MockLlcpSocket.java b/tests/src/com/android/nfc/MockLlcpSocket.java index 4265111..95279a7 100644 --- a/tests/src/com/android/nfc/MockLlcpSocket.java +++ b/tests/src/com/android/nfc/MockLlcpSocket.java @@ -16,26 +16,18 @@ package com.android.nfc; +import android.util.Log; + import java.io.IOException; -import java.nio.ByteBuffer; import java.util.LinkedList; import java.util.List; -import android.util.Log; - -import com.android.internal.nfc.LlcpException; -import com.android.internal.nfc.LlcpSocket; - -public class MockLlcpSocket extends LlcpSocket { +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; - public MockLlcpSocket() { - super(null, -1); - } - @Override public void close() throws IOException { mClosed = true; @@ -43,7 +35,7 @@ public class MockLlcpSocket extends LlcpSocket { } @Override - public void connect(int sap) throws IOException, LlcpException { + public void connectToSap(int sap) throws IOException { throw new UnsupportedOperationException("Use MockLlcpSocket.bind(client, server)"); } @@ -80,4 +72,34 @@ public class MockLlcpSocket extends LlcpSocket { 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(); + } }
\ No newline at end of file diff --git a/tests/src/com/android/nfc/snep/SnepBasicTests.java b/tests/src/com/android/nfc/snep/SnepBasicTests.java index bec570d..32b5e64 100644 --- a/tests/src/com/android/nfc/snep/SnepBasicTests.java +++ b/tests/src/com/android/nfc/snep/SnepBasicTests.java @@ -16,17 +16,14 @@ package com.android.nfc.snep; -import java.io.IOException; - import com.android.nfc.MockLlcpSocket; -import com.android.nfc.snep.SnepMessage; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.test.AndroidTestCase; import android.util.Log; -import com.android.internal.nfc.LlcpSocket; +import java.io.IOException; /** * Tests the SNEP cleint/server interfaces using a mock LLCP socket. @@ -45,6 +42,7 @@ public class SnepBasicTests extends AndroidTestCase { final SnepMessenger server = new SnepMessenger(false, serverSocket, MIU); new Thread() { + @Override public void run() { try { SnepServer.handleRequest(server, mCallback); @@ -75,6 +73,7 @@ public class SnepBasicTests extends AndroidTestCase { final SnepMessenger server = new SnepMessenger(false, serverSocket, MIU); new Thread() { + @Override public void run() { try { SnepServer.handleRequest(server, mCallback); @@ -105,6 +104,7 @@ public class SnepBasicTests extends AndroidTestCase { final SnepMessenger server = new SnepMessenger(false, serverSocket, MIU); new Thread() { + @Override public void run() { try { SnepServer.handleRequest(server, mCallback); @@ -135,6 +135,7 @@ public class SnepBasicTests extends AndroidTestCase { final SnepMessenger server = new SnepMessenger(false, serverSocket, MIU); new Thread() { + @Override public void run() { try { SnepServer.handleRequest(server, mCallback); @@ -165,6 +166,7 @@ public class SnepBasicTests extends AndroidTestCase { final SnepMessenger server = new SnepMessenger(false, serverSocket, MIU); new Thread() { + @Override public void run() { try { SnepServer.handleRequest(server, mCallback); @@ -195,6 +197,7 @@ public class SnepBasicTests extends AndroidTestCase { final SnepMessenger server = new SnepMessenger(false, serverSocket, MIU); new Thread() { + @Override public void run() { try { SnepServer.handleRequest(server, mCallback); @@ -228,6 +231,7 @@ public class SnepBasicTests extends AndroidTestCase { final SnepMessenger server = new SnepMessenger(false, serverSocket, MIU); new Thread() { + @Override public void run() { try { SnepServer.handleRequest(server, mCallback); @@ -254,13 +258,13 @@ public class SnepBasicTests extends AndroidTestCase { assertEquals(SnepMessage.RESPONSE_SUCCESS, response.getField()); } - private NdefMessage getSmallNdef() { + NdefMessage getSmallNdef() { NdefRecord rec = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI, new byte[0], "http://android.com".getBytes()); return new NdefMessage(new NdefRecord[] { rec }); } - private NdefMessage getNdef(int size) { + NdefMessage getNdef(int size) { StringBuffer string = new StringBuffer(size); for (int i = 0; i < size; i++) { string.append('A' + (i % 26)); @@ -270,10 +274,6 @@ public class SnepBasicTests extends AndroidTestCase { return new NdefMessage(new NdefRecord[] { rec }); } - private class SnepMessageContainer { - SnepMessage message; - } - /** * A SNEP Server implementation that accepts PUT requests for all ndef * messages and responds to GET requests with acceptable length greater |