PageRenderTime 74ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/android/nfc/NfcService.java

https://github.com/Cafogen/android_packages_apps_Nfc
Java | 3101 lines | 2338 code | 465 blank | 298 comment | 533 complexity | fe76b2efa156b3871c8c0abb7ef7e4b7 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright (C) 2010 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. package com.android.nfc;
  17. import com.android.internal.nfc.LlcpServiceSocket;
  18. import com.android.internal.nfc.LlcpSocket;
  19. import com.android.nfc.RegisteredComponentCache.ComponentInfo;
  20. import com.android.nfc.ndefpush.NdefPushClient;
  21. import com.android.nfc.ndefpush.NdefPushServer;
  22. import com.android.nfc3.R;
  23. import android.app.Activity;
  24. import android.app.ActivityManagerNative;
  25. import android.app.Application;
  26. import android.app.IActivityManager;
  27. import android.app.PendingIntent;
  28. import android.app.PendingIntent.CanceledException;
  29. import android.app.StatusBarManager;
  30. import android.content.ActivityNotFoundException;
  31. import android.content.BroadcastReceiver;
  32. import android.content.ComponentName;
  33. import android.content.Context;
  34. import android.content.Intent;
  35. import android.content.IntentFilter;
  36. import android.content.SharedPreferences;
  37. import android.content.pm.PackageManager;
  38. import android.content.pm.ResolveInfo;
  39. import android.net.Uri;
  40. import android.nfc.ApduList;
  41. import android.nfc.ErrorCodes;
  42. import android.nfc.FormatException;
  43. import android.nfc.ILlcpConnectionlessSocket;
  44. import android.nfc.ILlcpServiceSocket;
  45. import android.nfc.ILlcpSocket;
  46. import android.nfc.INfcAdapter;
  47. import android.nfc.INfcAdapterExtras;
  48. import android.nfc.INfcTag;
  49. import android.nfc.IP2pInitiator;
  50. import android.nfc.IP2pTarget;
  51. import android.nfc.LlcpPacket;
  52. import android.nfc.NdefMessage;
  53. import android.nfc.NdefRecord;
  54. import android.nfc.NfcAdapter;
  55. import android.nfc.Tag;
  56. import android.nfc.TechListParcel;
  57. import android.nfc.TransceiveResult;
  58. import android.os.AsyncTask;
  59. import android.os.Bundle;
  60. import android.os.Handler;
  61. import android.os.IBinder;
  62. import android.os.Message;
  63. import android.os.PowerManager;
  64. import android.os.RemoteException;
  65. import android.os.ServiceManager;
  66. import android.util.Log;
  67. import java.io.ByteArrayOutputStream;
  68. import java.io.DataInputStream;
  69. import java.io.File;
  70. import java.io.FileInputStream;
  71. import java.io.FileNotFoundException;
  72. import java.io.FileOutputStream;
  73. import java.io.IOException;
  74. import java.nio.charset.Charsets;
  75. import java.util.ArrayList;
  76. import java.util.Arrays;
  77. import java.util.HashMap;
  78. import java.util.HashSet;
  79. import java.util.Iterator;
  80. import java.util.List;
  81. public class NfcService extends Application {
  82. private static final String ACTION_MASTER_CLEAR_NOTIFICATION = "android.intent.action.MASTER_CLEAR_NOTIFICATION";
  83. static final boolean DBG = false;
  84. private static final String MY_TAG_FILE_NAME = "mytag";
  85. private static final String SE_RESET_SCRIPT_FILE_NAME = "/system/etc/se-reset-script";
  86. private static final String NFC_CONTROLLER_FIRMWARE_FILE_NAME = "/system/lib/libpn544_fw.so";
  87. static {
  88. System.loadLibrary("nfc_jni");
  89. }
  90. /**
  91. * NFC Forum "URI Record Type Definition"
  92. *
  93. * This is a mapping of "URI Identifier Codes" to URI string prefixes,
  94. * per section 3.2.2 of the NFC Forum URI Record Type Definition document.
  95. */
  96. private static final String[] URI_PREFIX_MAP = new String[] {
  97. "", // 0x00
  98. "http://www.", // 0x01
  99. "https://www.", // 0x02
  100. "http://", // 0x03
  101. "https://", // 0x04
  102. "tel:", // 0x05
  103. "mailto:", // 0x06
  104. "ftp://anonymous:anonymous@", // 0x07
  105. "ftp://ftp.", // 0x08
  106. "ftps://", // 0x09
  107. "sftp://", // 0x0A
  108. "smb://", // 0x0B
  109. "nfs://", // 0x0C
  110. "ftp://", // 0x0D
  111. "dav://", // 0x0E
  112. "news:", // 0x0F
  113. "telnet://", // 0x10
  114. "imap:", // 0x11
  115. "rtsp://", // 0x12
  116. "urn:", // 0x13
  117. "pop:", // 0x14
  118. "sip:", // 0x15
  119. "sips:", // 0x16
  120. "tftp:", // 0x17
  121. "btspp://", // 0x18
  122. "btl2cap://", // 0x19
  123. "btgoep://", // 0x1A
  124. "tcpobex://", // 0x1B
  125. "irdaobex://", // 0x1C
  126. "file://", // 0x1D
  127. "urn:epc:id:", // 0x1E
  128. "urn:epc:tag:", // 0x1F
  129. "urn:epc:pat:", // 0x20
  130. "urn:epc:raw:", // 0x21
  131. "urn:epc:", // 0x22
  132. };
  133. public static final String SERVICE_NAME = "nfc";
  134. private static final String TAG = "NfcService";
  135. private static final String NFC_PERM = android.Manifest.permission.NFC;
  136. private static final String NFC_PERM_ERROR = "NFC permission required";
  137. private static final String ADMIN_PERM = android.Manifest.permission.WRITE_SECURE_SETTINGS;
  138. private static final String ADMIN_PERM_ERROR = "WRITE_SECURE_SETTINGS permission required";
  139. private static final String NFCEE_ADMIN_PERM = "com.android.nfc.permission.NFCEE_ADMIN";
  140. private static final String NFCEE_ADMIN_PERM_ERROR = "NFCEE_ADMIN permission required";
  141. private static final String PREF = "NfcServicePrefs";
  142. private static final String PREF_NFC_ON = "nfc_on";
  143. private static final boolean NFC_ON_DEFAULT = true;
  144. private static final String PREF_FIRST_BOOT = "first_boot";
  145. private static final String PREF_LLCP_LTO = "llcp_lto";
  146. private static final int LLCP_LTO_DEFAULT = 150;
  147. private static final int LLCP_LTO_MAX = 255;
  148. /** Maximum Information Unit */
  149. private static final String PREF_LLCP_MIU = "llcp_miu";
  150. private static final int LLCP_MIU_DEFAULT = 128;
  151. private static final int LLCP_MIU_MAX = 2176;
  152. /** Well Known Service List */
  153. private static final String PREF_LLCP_WKS = "llcp_wks";
  154. private static final int LLCP_WKS_DEFAULT = 1;
  155. private static final int LLCP_WKS_MAX = 15;
  156. private static final String PREF_LLCP_OPT = "llcp_opt";
  157. private static final int LLCP_OPT_DEFAULT = 0;
  158. private static final int LLCP_OPT_MAX = 3;
  159. private static final String PREF_DISCOVERY_A = "discovery_a";
  160. private static final boolean DISCOVERY_A_DEFAULT = true;
  161. private static final String PREF_DISCOVERY_B = "discovery_b";
  162. private static final boolean DISCOVERY_B_DEFAULT = true;
  163. private static final String PREF_DISCOVERY_F = "discovery_f";
  164. private static final boolean DISCOVERY_F_DEFAULT = true;
  165. private static final String PREF_DISCOVERY_15693 = "discovery_15693";
  166. private static final boolean DISCOVERY_15693_DEFAULT = true;
  167. private static final String PREF_DISCOVERY_NFCIP = "discovery_nfcip";
  168. private static final boolean DISCOVERY_NFCIP_DEFAULT = true;
  169. private static final String PREF_FIRMWARE_MODTIME = "firmware_modtime";
  170. private static final long FIRMWARE_MODTIME_DEFAULT = -1;
  171. /** NFC Reader Discovery mode for enableDiscovery() */
  172. private static final int DISCOVERY_MODE_READER = 0;
  173. private static final int PROPERTY_LLCP_LTO = 0;
  174. private static final String PROPERTY_LLCP_LTO_VALUE = "llcp.lto";
  175. private static final int PROPERTY_LLCP_MIU = 1;
  176. private static final String PROPERTY_LLCP_MIU_VALUE = "llcp.miu";
  177. private static final int PROPERTY_LLCP_WKS = 2;
  178. private static final String PROPERTY_LLCP_WKS_VALUE = "llcp.wks";
  179. private static final int PROPERTY_LLCP_OPT = 3;
  180. private static final String PROPERTY_LLCP_OPT_VALUE = "llcp.opt";
  181. private static final int PROPERTY_NFC_DISCOVERY_A = 4;
  182. private static final String PROPERTY_NFC_DISCOVERY_A_VALUE = "discovery.iso14443A";
  183. private static final int PROPERTY_NFC_DISCOVERY_B = 5;
  184. private static final String PROPERTY_NFC_DISCOVERY_B_VALUE = "discovery.iso14443B";
  185. private static final int PROPERTY_NFC_DISCOVERY_F = 6;
  186. private static final String PROPERTY_NFC_DISCOVERY_F_VALUE = "discovery.felica";
  187. private static final int PROPERTY_NFC_DISCOVERY_15693 = 7;
  188. private static final String PROPERTY_NFC_DISCOVERY_15693_VALUE = "discovery.iso15693";
  189. private static final int PROPERTY_NFC_DISCOVERY_NFCIP = 8;
  190. private static final String PROPERTY_NFC_DISCOVERY_NFCIP_VALUE = "discovery.nfcip";
  191. static final int MSG_NDEF_TAG = 0;
  192. static final int MSG_CARD_EMULATION = 1;
  193. static final int MSG_LLCP_LINK_ACTIVATION = 2;
  194. static final int MSG_LLCP_LINK_DEACTIVATED = 3;
  195. static final int MSG_TARGET_DESELECTED = 4;
  196. static final int MSG_SHOW_MY_TAG_ICON = 5;
  197. static final int MSG_HIDE_MY_TAG_ICON = 6;
  198. static final int MSG_MOCK_NDEF = 7;
  199. static final int MSG_SE_FIELD_ACTIVATED = 8;
  200. static final int MSG_SE_FIELD_DEACTIVATED = 9;
  201. static final int MSG_SE_APDU_RECEIVED = 10;
  202. static final int MSG_SE_EMV_CARD_REMOVAL = 11;
  203. static final int MSG_SE_MIFARE_ACCESS = 12;
  204. static final int STATUS_CODE_TARGET_LOST = 146;
  205. // Copied from com.android.nfc_extras to avoid library dependency
  206. // Must keep in sync with com.android.nfc_extras
  207. static final int ROUTE_OFF = 1;
  208. static final int ROUTE_ON_WHEN_SCREEN_ON = 2;
  209. public static final String ACTION_RF_FIELD_ON_DETECTED =
  210. "com.android.nfc_extras.action.RF_FIELD_ON_DETECTED";
  211. public static final String ACTION_RF_FIELD_OFF_DETECTED =
  212. "com.android.nfc_extras.action.RF_FIELD_OFF_DETECTED";
  213. public static final String ACTION_AID_SELECTED =
  214. "com.android.nfc_extras.action.AID_SELECTED";
  215. public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID";
  216. public static final String ACTION_APDU_RECEIVED =
  217. "com.android.nfc_extras.action.APDU_RECEIVED";
  218. public static final String EXTRA_APDU_BYTES =
  219. "com.android.nfc_extras.extra.APDU_BYTES";
  220. public static final String ACTION_EMV_CARD_REMOVAL =
  221. "com.android.nfc_extras.action.EMV_CARD_REMOVAL";
  222. public static final String ACTION_MIFARE_ACCESS_DETECTED =
  223. "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED";
  224. public static final String EXTRA_MIFARE_BLOCK =
  225. "com.android.nfc_extras.extra.MIFARE_BLOCK";
  226. // Locked on mNfcAdapter
  227. PendingIntent mDispatchOverrideIntent;
  228. IntentFilter[] mDispatchOverrideFilters;
  229. String[][] mDispatchOverrideTechLists;
  230. // TODO: none of these appear to be synchronized but are
  231. // read/written from different threads (notably Binder threads)...
  232. private int mGeneratedSocketHandle = 0;
  233. private volatile boolean mIsNfcEnabled = false;
  234. private boolean mIsDiscoveryOn = false;
  235. // NFC Execution Environment
  236. // fields below are protected by this
  237. private static final int SECURE_ELEMENT_ID = 11259375; //TODO: remove hard-coded value
  238. private NativeNfcSecureElement mSecureElement;
  239. private OpenSecureElement mOpenEe; // null when EE closed
  240. private int mEeRoutingState; // contactless interface routing
  241. // fields below are used in multiple threads and protected by synchronized(this)
  242. private final HashMap<Integer, Object> mObjectMap = new HashMap<Integer, Object>();
  243. private final HashMap<Integer, Object> mSocketMap = new HashMap<Integer, Object>();
  244. private boolean mScreenOn;
  245. private HashSet<String> mSePackages = new HashSet<String>();
  246. // fields below are final after onCreate()
  247. Context mContext;
  248. private NativeNfcManager mManager;
  249. private SharedPreferences mPrefs;
  250. private SharedPreferences.Editor mPrefsEditor;
  251. private PowerManager.WakeLock mWakeLock;
  252. private IActivityManager mIActivityManager;
  253. NdefPushClient mNdefPushClient;
  254. NdefPushServer mNdefPushServer;
  255. RegisteredComponentCache mTechListFilters;
  256. private static NfcService sService;
  257. public static void enforceAdminPerm(Context context) {
  258. int admin = context.checkCallingOrSelfPermission(ADMIN_PERM);
  259. int nfcee = context.checkCallingOrSelfPermission(NFCEE_ADMIN_PERM);
  260. if (admin != PackageManager.PERMISSION_GRANTED
  261. && nfcee != PackageManager.PERMISSION_GRANTED) {
  262. throw new SecurityException(ADMIN_PERM_ERROR);
  263. }
  264. }
  265. public static void enforceNfceeAdminPerm(Context context) {
  266. context.enforceCallingOrSelfPermission(NFCEE_ADMIN_PERM, NFCEE_ADMIN_PERM_ERROR);
  267. }
  268. public static NfcService getInstance() {
  269. return sService;
  270. }
  271. @Override
  272. public void onCreate() {
  273. super.onCreate();
  274. Log.i(TAG, "Starting NFC service");
  275. sService = this;
  276. mContext = this;
  277. mManager = new NativeNfcManager(mContext, this);
  278. mManager.initializeNativeStructure();
  279. mNdefPushClient = new NdefPushClient(this);
  280. mNdefPushServer = new NdefPushServer();
  281. mTechListFilters = new RegisteredComponentCache(this,
  282. NfcAdapter.ACTION_TECH_DISCOVERED, NfcAdapter.ACTION_TECH_DISCOVERED);
  283. mSecureElement = new NativeNfcSecureElement();
  284. mEeRoutingState = ROUTE_OFF;
  285. mPrefs = mContext.getSharedPreferences(PREF, Context.MODE_PRIVATE);
  286. mPrefsEditor = mPrefs.edit();
  287. mIsNfcEnabled = false; // real preference read later
  288. PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
  289. mScreenOn = pm.isScreenOn();
  290. mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "NfcService");
  291. mIActivityManager = ActivityManagerNative.getDefault();
  292. ServiceManager.addService(SERVICE_NAME, mNfcAdapter);
  293. IntentFilter filter = new IntentFilter(NativeNfcManager.INTERNAL_TARGET_DESELECTED_ACTION);
  294. filter.addAction(Intent.ACTION_SCREEN_OFF);
  295. filter.addAction(Intent.ACTION_SCREEN_ON);
  296. filter.addAction(ACTION_MASTER_CLEAR_NOTIFICATION);
  297. mContext.registerReceiver(mReceiver, filter);
  298. filter = new IntentFilter();
  299. filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
  300. filter.addDataScheme("package");
  301. mContext.registerReceiver(mReceiver, filter);
  302. Thread t = new Thread() {
  303. @Override
  304. public void run() {
  305. Log.d(TAG,"checking on firmware download");
  306. boolean nfc_on = mPrefs.getBoolean(PREF_NFC_ON, NFC_ON_DEFAULT);
  307. if (nfc_on) {
  308. Log.d(TAG,"NFC is on. Doing normal stuff");
  309. _enable(false, true);
  310. } else {
  311. Log.d(TAG,"NFC is off. Checking firmware version");
  312. _maybeUpdateFirmware();
  313. }
  314. resetSeOnFirstBoot();
  315. }
  316. };
  317. t.start();
  318. }
  319. @Override
  320. public void onTerminate() {
  321. super.onTerminate();
  322. // NFC application is persistent, it should not be destroyed by framework
  323. Log.wtf(TAG, "NFC service is under attack!");
  324. }
  325. private final INfcAdapter.Stub mNfcAdapter = new INfcAdapter.Stub() {
  326. /** Protected by "this" */
  327. NdefMessage mLocalMessage = null;
  328. @Override
  329. public boolean enable() throws RemoteException {
  330. NfcService.enforceAdminPerm(mContext);
  331. boolean isSuccess = false;
  332. boolean previouslyEnabled = isEnabled();
  333. if (!previouslyEnabled) {
  334. reset();
  335. isSuccess = _enable(previouslyEnabled, true);
  336. }
  337. return isSuccess;
  338. }
  339. @Override
  340. public boolean disable() throws RemoteException {
  341. boolean isSuccess = false;
  342. NfcService.enforceAdminPerm(mContext);
  343. boolean previouslyEnabled = isEnabled();
  344. if (DBG) Log.d(TAG, "Disabling NFC. previous=" + previouslyEnabled);
  345. if (previouslyEnabled) {
  346. isSuccess = _disable(previouslyEnabled, true);
  347. }
  348. return isSuccess;
  349. }
  350. @Override
  351. public void enableForegroundDispatch(ComponentName activity, PendingIntent intent,
  352. IntentFilter[] filters, TechListParcel techListsParcel) {
  353. // Permission check
  354. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  355. // Argument validation
  356. if (activity == null || intent == null) {
  357. throw new IllegalArgumentException();
  358. }
  359. // Validate the IntentFilters
  360. if (filters != null) {
  361. if (filters.length == 0) {
  362. filters = null;
  363. } else {
  364. for (IntentFilter filter : filters) {
  365. if (filter == null) {
  366. throw new IllegalArgumentException("null IntentFilter");
  367. }
  368. }
  369. }
  370. }
  371. // Validate the tech lists
  372. String[][] techLists = null;
  373. if (techListsParcel != null) {
  374. techLists = techListsParcel.getTechLists();
  375. }
  376. synchronized (this) {
  377. if (mDispatchOverrideIntent != null) {
  378. Log.e(TAG, "Replacing active dispatch overrides");
  379. }
  380. mDispatchOverrideIntent = intent;
  381. mDispatchOverrideFilters = filters;
  382. mDispatchOverrideTechLists = techLists;
  383. }
  384. }
  385. @Override
  386. public void disableForegroundDispatch(ComponentName activity) {
  387. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  388. synchronized (this) {
  389. if (mDispatchOverrideIntent == null) {
  390. Log.e(TAG, "No active foreground dispatching");
  391. }
  392. mDispatchOverrideIntent = null;
  393. mDispatchOverrideFilters = null;
  394. }
  395. }
  396. @Override
  397. public void enableForegroundNdefPush(ComponentName activity, NdefMessage msg) {
  398. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  399. if (activity == null || msg == null) {
  400. throw new IllegalArgumentException();
  401. }
  402. if (mNdefPushClient.setForegroundMessage(msg)) {
  403. Log.e(TAG, "Replacing active NDEF push message");
  404. }
  405. }
  406. @Override
  407. public void disableForegroundNdefPush(ComponentName activity) {
  408. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  409. if (!mNdefPushClient.setForegroundMessage(null)) {
  410. Log.e(TAG, "No active foreground NDEF push message");
  411. }
  412. }
  413. @Override
  414. public int createLlcpConnectionlessSocket(int sap) throws RemoteException {
  415. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  416. // Check if NFC is enabled
  417. if (!mIsNfcEnabled) {
  418. return ErrorCodes.ERROR_NOT_INITIALIZED;
  419. }
  420. /* Check SAP is not already used */
  421. /* Store the socket handle */
  422. int sockeHandle = mGeneratedSocketHandle;
  423. NativeLlcpConnectionlessSocket socket;
  424. socket = mManager.doCreateLlcpConnectionlessSocket(sap);
  425. if (socket != null) {
  426. synchronized(NfcService.this) {
  427. /* update socket handle generation */
  428. mGeneratedSocketHandle++;
  429. /* Add the socket into the socket map */
  430. mSocketMap.put(mGeneratedSocketHandle, socket);
  431. return mGeneratedSocketHandle;
  432. }
  433. } else {
  434. /* Get Error Status */
  435. int errorStatus = mManager.doGetLastError();
  436. switch (errorStatus) {
  437. case ErrorCodes.ERROR_BUFFER_TO_SMALL:
  438. return ErrorCodes.ERROR_BUFFER_TO_SMALL;
  439. case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES:
  440. return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES;
  441. default:
  442. return ErrorCodes.ERROR_SOCKET_CREATION;
  443. }
  444. }
  445. }
  446. @Override
  447. public int createLlcpServiceSocket(int sap, String sn, int miu, int rw, int linearBufferLength)
  448. throws RemoteException {
  449. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  450. // Check if NFC is enabled
  451. if (!mIsNfcEnabled) {
  452. return ErrorCodes.ERROR_NOT_INITIALIZED;
  453. }
  454. NativeLlcpServiceSocket socket;
  455. socket = mManager.doCreateLlcpServiceSocket(sap, sn, miu, rw, linearBufferLength);
  456. if (socket != null) {
  457. synchronized(NfcService.this) {
  458. /* update socket handle generation */
  459. mGeneratedSocketHandle++;
  460. /* Add the socket into the socket map */
  461. mSocketMap.put(mGeneratedSocketHandle, socket);
  462. return mGeneratedSocketHandle;
  463. }
  464. } else {
  465. /* Get Error Status */
  466. int errorStatus = mManager.doGetLastError();
  467. switch (errorStatus) {
  468. case ErrorCodes.ERROR_BUFFER_TO_SMALL:
  469. return ErrorCodes.ERROR_BUFFER_TO_SMALL;
  470. case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES:
  471. return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES;
  472. default:
  473. return ErrorCodes.ERROR_SOCKET_CREATION;
  474. }
  475. }
  476. }
  477. @Override
  478. public int createLlcpSocket(int sap, int miu, int rw, int linearBufferLength)
  479. throws RemoteException {
  480. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  481. // Check if NFC is enabled
  482. if (!mIsNfcEnabled) {
  483. return ErrorCodes.ERROR_NOT_INITIALIZED;
  484. }
  485. if (DBG) Log.d(TAG, "creating llcp socket");
  486. NativeLlcpSocket socket;
  487. socket = mManager.doCreateLlcpSocket(sap, miu, rw, linearBufferLength);
  488. if (socket != null) {
  489. synchronized(NfcService.this) {
  490. /* update socket handle generation */
  491. mGeneratedSocketHandle++;
  492. /* Add the socket into the socket map */
  493. mSocketMap.put(mGeneratedSocketHandle, socket);
  494. return mGeneratedSocketHandle;
  495. }
  496. } else {
  497. /* Get Error Status */
  498. int errorStatus = mManager.doGetLastError();
  499. Log.d(TAG, "failed to create llcp socket: " + ErrorCodes.asString(errorStatus));
  500. switch (errorStatus) {
  501. case ErrorCodes.ERROR_BUFFER_TO_SMALL:
  502. return ErrorCodes.ERROR_BUFFER_TO_SMALL;
  503. case ErrorCodes.ERROR_INSUFFICIENT_RESOURCES:
  504. return ErrorCodes.ERROR_INSUFFICIENT_RESOURCES;
  505. default:
  506. return ErrorCodes.ERROR_SOCKET_CREATION;
  507. }
  508. }
  509. }
  510. @Override
  511. public ILlcpConnectionlessSocket getLlcpConnectionlessInterface() throws RemoteException {
  512. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  513. return mLlcpConnectionlessSocketService;
  514. }
  515. @Override
  516. public ILlcpSocket getLlcpInterface() throws RemoteException {
  517. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  518. return mLlcpSocket;
  519. }
  520. @Override
  521. public ILlcpServiceSocket getLlcpServiceInterface() throws RemoteException {
  522. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  523. return mLlcpServerSocketService;
  524. }
  525. @Override
  526. public INfcTag getNfcTagInterface() throws RemoteException {
  527. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  528. return mNfcTagService;
  529. }
  530. @Override
  531. public IP2pInitiator getP2pInitiatorInterface() throws RemoteException {
  532. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  533. return mP2pInitiatorService;
  534. }
  535. @Override
  536. public IP2pTarget getP2pTargetInterface() throws RemoteException {
  537. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  538. return mP2pTargetService;
  539. }
  540. @Override
  541. public INfcAdapterExtras getNfcAdapterExtrasInterface() {
  542. NfcService.enforceNfceeAdminPerm(mContext);
  543. return mExtrasService;
  544. }
  545. @Override
  546. public String getProperties(String param) throws RemoteException {
  547. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  548. if (param == null) {
  549. return null;
  550. }
  551. if (param.equals(PROPERTY_LLCP_LTO_VALUE)) {
  552. return Integer.toString(mPrefs.getInt(PREF_LLCP_LTO, LLCP_LTO_DEFAULT));
  553. } else if (param.equals(PROPERTY_LLCP_MIU_VALUE)) {
  554. return Integer.toString(mPrefs.getInt(PREF_LLCP_MIU, LLCP_MIU_DEFAULT));
  555. } else if (param.equals(PROPERTY_LLCP_WKS_VALUE)) {
  556. return Integer.toString(mPrefs.getInt(PREF_LLCP_WKS, LLCP_WKS_DEFAULT));
  557. } else if (param.equals(PROPERTY_LLCP_OPT_VALUE)) {
  558. return Integer.toString(mPrefs.getInt(PREF_LLCP_OPT, LLCP_OPT_DEFAULT));
  559. } else if (param.equals(PROPERTY_NFC_DISCOVERY_A_VALUE)) {
  560. return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_A, DISCOVERY_A_DEFAULT));
  561. } else if (param.equals(PROPERTY_NFC_DISCOVERY_B_VALUE)) {
  562. return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_B, DISCOVERY_B_DEFAULT));
  563. } else if (param.equals(PROPERTY_NFC_DISCOVERY_F_VALUE)) {
  564. return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_F, DISCOVERY_F_DEFAULT));
  565. } else if (param.equals(PROPERTY_NFC_DISCOVERY_NFCIP_VALUE)) {
  566. return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_NFCIP, DISCOVERY_NFCIP_DEFAULT));
  567. } else if (param.equals(PROPERTY_NFC_DISCOVERY_15693_VALUE)) {
  568. return Boolean.toString(mPrefs.getBoolean(PREF_DISCOVERY_15693, DISCOVERY_15693_DEFAULT));
  569. } else {
  570. return "Unknown property";
  571. }
  572. }
  573. @Override
  574. public boolean isEnabled() throws RemoteException {
  575. return mIsNfcEnabled;
  576. }
  577. @Override
  578. public int setProperties(String param, String value) throws RemoteException {
  579. NfcService.enforceAdminPerm(mContext);
  580. if (isEnabled()) {
  581. return ErrorCodes.ERROR_NFC_ON;
  582. }
  583. int val;
  584. /* Check params validity */
  585. if (param == null || value == null) {
  586. return ErrorCodes.ERROR_INVALID_PARAM;
  587. }
  588. if (param.equals(PROPERTY_LLCP_LTO_VALUE)) {
  589. val = Integer.parseInt(value);
  590. /* Check params */
  591. if (val > LLCP_LTO_MAX)
  592. return ErrorCodes.ERROR_INVALID_PARAM;
  593. /* Store value */
  594. mPrefsEditor.putInt(PREF_LLCP_LTO, val);
  595. mPrefsEditor.apply();
  596. /* Update JNI */
  597. mManager.doSetProperties(PROPERTY_LLCP_LTO, val);
  598. } else if (param.equals(PROPERTY_LLCP_MIU_VALUE)) {
  599. val = Integer.parseInt(value);
  600. /* Check params */
  601. if ((val < LLCP_MIU_DEFAULT) || (val > LLCP_MIU_MAX))
  602. return ErrorCodes.ERROR_INVALID_PARAM;
  603. /* Store value */
  604. mPrefsEditor.putInt(PREF_LLCP_MIU, val);
  605. mPrefsEditor.apply();
  606. /* Update JNI */
  607. mManager.doSetProperties(PROPERTY_LLCP_MIU, val);
  608. } else if (param.equals(PROPERTY_LLCP_WKS_VALUE)) {
  609. val = Integer.parseInt(value);
  610. /* Check params */
  611. if (val > LLCP_WKS_MAX)
  612. return ErrorCodes.ERROR_INVALID_PARAM;
  613. /* Store value */
  614. mPrefsEditor.putInt(PREF_LLCP_WKS, val);
  615. mPrefsEditor.apply();
  616. /* Update JNI */
  617. mManager.doSetProperties(PROPERTY_LLCP_WKS, val);
  618. } else if (param.equals(PROPERTY_LLCP_OPT_VALUE)) {
  619. val = Integer.parseInt(value);
  620. /* Check params */
  621. if (val > LLCP_OPT_MAX)
  622. return ErrorCodes.ERROR_INVALID_PARAM;
  623. /* Store value */
  624. mPrefsEditor.putInt(PREF_LLCP_OPT, val);
  625. mPrefsEditor.apply();
  626. /* Update JNI */
  627. mManager.doSetProperties(PROPERTY_LLCP_OPT, val);
  628. } else if (param.equals(PROPERTY_NFC_DISCOVERY_A_VALUE)) {
  629. boolean b = Boolean.parseBoolean(value);
  630. /* Store value */
  631. mPrefsEditor.putBoolean(PREF_DISCOVERY_A, b);
  632. mPrefsEditor.apply();
  633. /* Update JNI */
  634. mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_A, b ? 1 : 0);
  635. } else if (param.equals(PROPERTY_NFC_DISCOVERY_B_VALUE)) {
  636. boolean b = Boolean.parseBoolean(value);
  637. /* Store value */
  638. mPrefsEditor.putBoolean(PREF_DISCOVERY_B, b);
  639. mPrefsEditor.apply();
  640. /* Update JNI */
  641. mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_B, b ? 1 : 0);
  642. } else if (param.equals(PROPERTY_NFC_DISCOVERY_F_VALUE)) {
  643. boolean b = Boolean.parseBoolean(value);
  644. /* Store value */
  645. mPrefsEditor.putBoolean(PREF_DISCOVERY_F, b);
  646. mPrefsEditor.apply();
  647. /* Update JNI */
  648. mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_F, b ? 1 : 0);
  649. } else if (param.equals(PROPERTY_NFC_DISCOVERY_15693_VALUE)) {
  650. boolean b = Boolean.parseBoolean(value);
  651. /* Store value */
  652. mPrefsEditor.putBoolean(PREF_DISCOVERY_15693, b);
  653. mPrefsEditor.apply();
  654. /* Update JNI */
  655. mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_15693, b ? 1 : 0);
  656. } else if (param.equals(PROPERTY_NFC_DISCOVERY_NFCIP_VALUE)) {
  657. boolean b = Boolean.parseBoolean(value);
  658. /* Store value */
  659. mPrefsEditor.putBoolean(PREF_DISCOVERY_NFCIP, b);
  660. mPrefsEditor.apply();
  661. /* Update JNI */
  662. mManager.doSetProperties(PROPERTY_NFC_DISCOVERY_NFCIP, b ? 1 : 0);
  663. } else {
  664. return ErrorCodes.ERROR_INVALID_PARAM;
  665. }
  666. return ErrorCodes.SUCCESS;
  667. }
  668. @Override
  669. public NdefMessage localGet() throws RemoteException {
  670. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  671. synchronized (this) {
  672. return mLocalMessage;
  673. }
  674. }
  675. @Override
  676. public void localSet(NdefMessage message) throws RemoteException {
  677. NfcService.enforceAdminPerm(mContext);
  678. synchronized (this) {
  679. mLocalMessage = message;
  680. Context context = NfcService.this.getApplicationContext();
  681. // Send a message to the UI thread to show or hide the icon so the requests are
  682. // serialized and the icon can't get out of sync with reality.
  683. if (message != null) {
  684. FileOutputStream out = null;
  685. try {
  686. out = context.openFileOutput(MY_TAG_FILE_NAME, Context.MODE_PRIVATE);
  687. byte[] bytes = message.toByteArray();
  688. if (bytes.length == 0) {
  689. Log.w(TAG, "Setting a empty mytag");
  690. }
  691. out.write(bytes);
  692. } catch (IOException e) {
  693. Log.e(TAG, "Could not write mytag file", e);
  694. } finally {
  695. try {
  696. if (out != null) {
  697. out.flush();
  698. out.close();
  699. }
  700. } catch (IOException e) {
  701. // Ignore
  702. }
  703. }
  704. // Only show the icon if NFC is enabled.
  705. if (mIsNfcEnabled) {
  706. sendMessage(MSG_SHOW_MY_TAG_ICON, null);
  707. }
  708. } else {
  709. context.deleteFile(MY_TAG_FILE_NAME);
  710. sendMessage(MSG_HIDE_MY_TAG_ICON, null);
  711. }
  712. }
  713. }
  714. };
  715. private final ILlcpSocket mLlcpSocket = new ILlcpSocket.Stub() {
  716. private NativeLlcpSocket findSocket(int nativeHandle) {
  717. Object socket = NfcService.this.findSocket(nativeHandle);
  718. if (!(socket instanceof NativeLlcpSocket)) {
  719. return null;
  720. }
  721. return (NativeLlcpSocket) socket;
  722. }
  723. @Override
  724. public int close(int nativeHandle) throws RemoteException {
  725. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  726. NativeLlcpSocket socket = null;
  727. // Check if NFC is enabled
  728. if (!mIsNfcEnabled) {
  729. return ErrorCodes.ERROR_NOT_INITIALIZED;
  730. }
  731. /* find the socket in the hmap */
  732. socket = findSocket(nativeHandle);
  733. if (socket != null) {
  734. socket.doClose();
  735. /* Remove the socket closed from the hmap */
  736. RemoveSocket(nativeHandle);
  737. return ErrorCodes.SUCCESS;
  738. } else {
  739. return ErrorCodes.ERROR_IO;
  740. }
  741. }
  742. @Override
  743. public int connect(int nativeHandle, int sap) throws RemoteException {
  744. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  745. NativeLlcpSocket socket = null;
  746. boolean isSuccess = false;
  747. // Check if NFC is enabled
  748. if (!mIsNfcEnabled) {
  749. return ErrorCodes.ERROR_NOT_INITIALIZED;
  750. }
  751. /* find the socket in the hmap */
  752. socket = findSocket(nativeHandle);
  753. if (socket != null) {
  754. isSuccess = socket.doConnect(sap);
  755. if (isSuccess) {
  756. return ErrorCodes.SUCCESS;
  757. } else {
  758. return ErrorCodes.ERROR_IO;
  759. }
  760. } else {
  761. return ErrorCodes.ERROR_IO;
  762. }
  763. }
  764. @Override
  765. public int connectByName(int nativeHandle, String sn) throws RemoteException {
  766. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  767. NativeLlcpSocket socket = null;
  768. boolean isSuccess = false;
  769. // Check if NFC is enabled
  770. if (!mIsNfcEnabled) {
  771. return ErrorCodes.ERROR_NOT_INITIALIZED;
  772. }
  773. /* find the socket in the hmap */
  774. socket = findSocket(nativeHandle);
  775. if (socket != null) {
  776. isSuccess = socket.doConnectBy(sn);
  777. if (isSuccess) {
  778. return ErrorCodes.SUCCESS;
  779. } else {
  780. return ErrorCodes.ERROR_IO;
  781. }
  782. } else {
  783. return ErrorCodes.ERROR_IO;
  784. }
  785. }
  786. @Override
  787. public int getLocalSap(int nativeHandle) throws RemoteException {
  788. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  789. NativeLlcpSocket socket = null;
  790. // Check if NFC is enabled
  791. if (!mIsNfcEnabled) {
  792. return ErrorCodes.ERROR_NOT_INITIALIZED;
  793. }
  794. /* find the socket in the hmap */
  795. socket = findSocket(nativeHandle);
  796. if (socket != null) {
  797. return socket.getSap();
  798. } else {
  799. return 0;
  800. }
  801. }
  802. @Override
  803. public int getLocalSocketMiu(int nativeHandle) throws RemoteException {
  804. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  805. NativeLlcpSocket socket = null;
  806. // Check if NFC is enabled
  807. if (!mIsNfcEnabled) {
  808. return ErrorCodes.ERROR_NOT_INITIALIZED;
  809. }
  810. /* find the socket in the hmap */
  811. socket = findSocket(nativeHandle);
  812. if (socket != null) {
  813. return socket.getMiu();
  814. } else {
  815. return 0;
  816. }
  817. }
  818. @Override
  819. public int getLocalSocketRw(int nativeHandle) throws RemoteException {
  820. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  821. NativeLlcpSocket socket = null;
  822. // Check if NFC is enabled
  823. if (!mIsNfcEnabled) {
  824. return ErrorCodes.ERROR_NOT_INITIALIZED;
  825. }
  826. /* find the socket in the hmap */
  827. socket = findSocket(nativeHandle);
  828. if (socket != null) {
  829. return socket.getRw();
  830. } else {
  831. return 0;
  832. }
  833. }
  834. @Override
  835. public int getRemoteSocketMiu(int nativeHandle) throws RemoteException {
  836. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  837. NativeLlcpSocket socket = null;
  838. // Check if NFC is enabled
  839. if (!mIsNfcEnabled) {
  840. return ErrorCodes.ERROR_NOT_INITIALIZED;
  841. }
  842. /* find the socket in the hmap */
  843. socket = findSocket(nativeHandle);
  844. if (socket != null) {
  845. if (socket.doGetRemoteSocketMiu() != 0) {
  846. return socket.doGetRemoteSocketMiu();
  847. } else {
  848. return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
  849. }
  850. } else {
  851. return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
  852. }
  853. }
  854. @Override
  855. public int getRemoteSocketRw(int nativeHandle) throws RemoteException {
  856. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  857. NativeLlcpSocket socket = null;
  858. // Check if NFC is enabled
  859. if (!mIsNfcEnabled) {
  860. return ErrorCodes.ERROR_NOT_INITIALIZED;
  861. }
  862. /* find the socket in the hmap */
  863. socket = findSocket(nativeHandle);
  864. if (socket != null) {
  865. if (socket.doGetRemoteSocketRw() != 0) {
  866. return socket.doGetRemoteSocketRw();
  867. } else {
  868. return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
  869. }
  870. } else {
  871. return ErrorCodes.ERROR_SOCKET_NOT_CONNECTED;
  872. }
  873. }
  874. @Override
  875. public int receive(int nativeHandle, byte[] receiveBuffer) throws RemoteException {
  876. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  877. NativeLlcpSocket socket = null;
  878. // Check if NFC is enabled
  879. if (!mIsNfcEnabled) {
  880. return ErrorCodes.ERROR_NOT_INITIALIZED;
  881. }
  882. /* find the socket in the hmap */
  883. socket = findSocket(nativeHandle);
  884. if (socket != null) {
  885. return socket.doReceive(receiveBuffer);
  886. } else {
  887. return 0;
  888. }
  889. }
  890. @Override
  891. public int send(int nativeHandle, byte[] data) throws RemoteException {
  892. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  893. NativeLlcpSocket socket = null;
  894. boolean isSuccess = false;
  895. // Check if NFC is enabled
  896. if (!mIsNfcEnabled) {
  897. return ErrorCodes.ERROR_NOT_INITIALIZED;
  898. }
  899. /* find the socket in the hmap */
  900. socket = findSocket(nativeHandle);
  901. if (socket != null) {
  902. isSuccess = socket.doSend(data);
  903. if (isSuccess) {
  904. return ErrorCodes.SUCCESS;
  905. } else {
  906. return ErrorCodes.ERROR_IO;
  907. }
  908. } else {
  909. return ErrorCodes.ERROR_IO;
  910. }
  911. }
  912. };
  913. private final ILlcpServiceSocket mLlcpServerSocketService = new ILlcpServiceSocket.Stub() {
  914. private NativeLlcpServiceSocket findSocket(int nativeHandle) {
  915. Object socket = NfcService.this.findSocket(nativeHandle);
  916. if (!(socket instanceof NativeLlcpServiceSocket)) {
  917. return null;
  918. }
  919. return (NativeLlcpServiceSocket) socket;
  920. }
  921. @Override
  922. public int accept(int nativeHandle) throws RemoteException {
  923. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  924. NativeLlcpServiceSocket socket = null;
  925. NativeLlcpSocket clientSocket = null;
  926. // Check if NFC is enabled
  927. if (!mIsNfcEnabled) {
  928. return ErrorCodes.ERROR_NOT_INITIALIZED;
  929. }
  930. /* find the socket in the hmap */
  931. socket = findSocket(nativeHandle);
  932. if (socket != null) {
  933. clientSocket = socket.doAccept(socket.getMiu(),
  934. socket.getRw(), socket.getLinearBufferLength());
  935. if (clientSocket != null) {
  936. /* Add the socket into the socket map */
  937. synchronized(this) {
  938. mGeneratedSocketHandle++;
  939. mSocketMap.put(mGeneratedSocketHandle, clientSocket);
  940. return mGeneratedSocketHandle;
  941. }
  942. } else {
  943. return ErrorCodes.ERROR_IO;
  944. }
  945. } else {
  946. return ErrorCodes.ERROR_IO;
  947. }
  948. }
  949. @Override
  950. public void close(int nativeHandle) throws RemoteException {
  951. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  952. NativeLlcpServiceSocket socket = null;
  953. // Check if NFC is enabled
  954. if (!mIsNfcEnabled) {
  955. return;
  956. }
  957. /* find the socket in the hmap */
  958. socket = findSocket(nativeHandle);
  959. if (socket != null) {
  960. socket.doClose();
  961. synchronized (this) {
  962. /* Remove the socket closed from the hmap */
  963. RemoveSocket(nativeHandle);
  964. }
  965. }
  966. }
  967. };
  968. private final ILlcpConnectionlessSocket mLlcpConnectionlessSocketService = new ILlcpConnectionlessSocket.Stub() {
  969. private NativeLlcpConnectionlessSocket findSocket(int nativeHandle) {
  970. Object socket = NfcService.this.findSocket(nativeHandle);
  971. if (!(socket instanceof NativeLlcpConnectionlessSocket)) {
  972. return null;
  973. }
  974. return (NativeLlcpConnectionlessSocket) socket;
  975. }
  976. @Override
  977. public void close(int nativeHandle) throws RemoteException {
  978. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  979. NativeLlcpConnectionlessSocket socket = null;
  980. // Check if NFC is enabled
  981. if (!mIsNfcEnabled) {
  982. return;
  983. }
  984. /* find the socket in the hmap */
  985. socket = findSocket(nativeHandle);
  986. if (socket != null) {
  987. socket.doClose();
  988. /* Remove the socket closed from the hmap */
  989. RemoveSocket(nativeHandle);
  990. }
  991. }
  992. @Override
  993. public int getSap(int nativeHandle) throws RemoteException {
  994. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  995. NativeLlcpConnectionlessSocket socket = null;
  996. // Check if NFC is enabled
  997. if (!mIsNfcEnabled) {
  998. return ErrorCodes.ERROR_NOT_INITIALIZED;
  999. }
  1000. /* find the socket in the hmap */
  1001. socket = findSocket(nativeHandle);
  1002. if (socket != null) {
  1003. return socket.getSap();
  1004. } else {
  1005. return 0;
  1006. }
  1007. }
  1008. @Override
  1009. public LlcpPacket receiveFrom(int nativeHandle) throws RemoteException {
  1010. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  1011. NativeLlcpConnectionlessSocket socket = null;
  1012. LlcpPacket packet;
  1013. // Check if NFC is enabled
  1014. if (!mIsNfcEnabled) {
  1015. return null;
  1016. }
  1017. /* find the socket in the hmap */
  1018. socket = findSocket(nativeHandle);
  1019. if (socket != null) {
  1020. packet = socket.doReceiveFrom(socket.getLinkMiu());
  1021. if (packet != null) {
  1022. return packet;
  1023. }
  1024. return null;
  1025. } else {
  1026. return null;
  1027. }
  1028. }
  1029. @Override
  1030. public int sendTo(int nativeHandle, LlcpPacket packet) throws RemoteException {
  1031. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  1032. NativeLlcpConnectionlessSocket socket = null;
  1033. boolean isSuccess = false;
  1034. // Check if NFC is enabled
  1035. if (!mIsNfcEnabled) {
  1036. return ErrorCodes.ERROR_NOT_INITIALIZED;
  1037. }
  1038. /* find the socket in the hmap */
  1039. socket = findSocket(nativeHandle);
  1040. if (socket != null) {
  1041. isSuccess = socket.doSendTo(packet.getRemoteSap(), packet.getDataBuffer());
  1042. if (isSuccess) {
  1043. return ErrorCodes.SUCCESS;
  1044. } else {
  1045. return ErrorCodes.ERROR_IO;
  1046. }
  1047. } else {
  1048. return ErrorCodes.ERROR_IO;
  1049. }
  1050. }
  1051. };
  1052. private final INfcTag mNfcTagService = new INfcTag.Stub() {
  1053. @Override
  1054. public int close(int nativeHandle) throws RemoteException {
  1055. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  1056. NativeNfcTag tag = null;
  1057. // Check if NFC is enabled
  1058. if (!mIsNfcEnabled) {
  1059. return ErrorCodes.ERROR_NOT_INITIALIZED;
  1060. }
  1061. /* find the tag in the hmap */
  1062. tag = (NativeNfcTag) findObject(nativeHandle);
  1063. if (tag != null) {
  1064. /* Remove the device from the hmap */
  1065. unregisterObject(nativeHandle);
  1066. tag.disconnect();
  1067. return ErrorCodes.SUCCESS;
  1068. }
  1069. /* Restart polling loop for notification */
  1070. applyRouting();
  1071. return ErrorCodes.ERROR_DISCONNECT;
  1072. }
  1073. @Override
  1074. public int connect(int nativeHandle, int technology) throws RemoteException {
  1075. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  1076. NativeNfcTag tag = null;
  1077. // Check if NFC is enabled
  1078. if (!mIsNfcEnabled) {
  1079. return ErrorCodes.ERROR_NOT_INITIALIZED;
  1080. }
  1081. /* find the tag in the hmap */
  1082. tag = (NativeNfcTag) findObject(nativeHandle);
  1083. if (tag == null) {
  1084. return ErrorCodes.ERROR_DISCONNECT;
  1085. }
  1086. // Note that on most tags, all technologies are behind a single
  1087. // handle. This means that the connect at the lower levels
  1088. // will do nothing, as the tag is already connected to that handle.
  1089. if (tag.connect(technology) == 0) {
  1090. return ErrorCodes.SUCCESS;
  1091. } else {
  1092. return ErrorCodes.ERROR_DISCONNECT;
  1093. }
  1094. }
  1095. @Override
  1096. public int reconnect(int nativeHandle) throws RemoteException {
  1097. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  1098. NativeNfcTag tag = null;
  1099. // Check if NFC is enabled
  1100. if (!mIsNfcEnabled) {
  1101. return ErrorCodes.ERROR_NOT_INITIALIZED;
  1102. }
  1103. /* find the tag in the hmap */
  1104. tag = (NativeNfcTag) findObject(nativeHandle);
  1105. if (tag != null) {
  1106. if (tag.reconnect() == 0) {
  1107. return ErrorCodes.SUCCESS;
  1108. } else {
  1109. return ErrorCodes.ERROR_DISCONNECT;
  1110. }
  1111. }
  1112. return ErrorCodes.ERROR_DISCONNECT;
  1113. }
  1114. @Override
  1115. public int[] getTechList(int nativeHandle) throws RemoteException {
  1116. mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
  1117. // Check if NFC is enabled
  1118. if (!mIsNfcEnabled) {
  1119. return null;
  1120. }
  1121. /* find the tag in the hmap */
  1122. NativeNfcTag tag = (NativeNfcTag) findObject(nativeHandle);
  1123. if (tag != null) {
  1124. return tag.getTechList();

Large files files are truncated, but you can click here to view the full file