1 /* 2 * Copyright (C) 2012 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 17 /***************************************************************************** 18 ** 19 ** Name: Pn544Interop.cpp 20 ** 21 ** Description: Implement operations that provide compatibility with NXP 22 ** PN544 controller. Specifically facilitate peer-to-peer 23 ** operations with PN544 controller. 24 ** 25 *****************************************************************************/ 26 #include "OverrideLog.h" 27 #include "Pn544Interop.h" 28 #include "IntervalTimer.h" 29 #include "Mutex.h" 30 #include "NfcTag.h" 31 namespace android 32 { 33 extern void startStopPolling (bool isStartPolling); 34 } 35 36 37 /***************************************************************************** 38 ** 39 ** private variables and functions 40 ** 41 *****************************************************************************/ 42 43 44 static const int gIntervalTime = 1000; //millisecond between the check to restore polling 45 static IntervalTimer gTimer; 46 static Mutex gMutex; 47 static void pn544InteropStartPolling (union sigval); //callback function for interval timer 48 static bool gIsBusy = false; //is timer busy? 49 static bool gAbortNow = false; //stop timer during next callback 50 51 52 /******************************************************************************* 53 ** 54 ** Function: pn544InteropStopPolling 55 ** 56 ** Description: Stop polling to let NXP PN544 controller poll. 57 ** PN544 should activate in P2P mode. 58 ** 59 ** Returns: None 60 ** 61 *******************************************************************************/ 62 void pn544InteropStopPolling () 63 { 64 ALOGD ("%s: enter", __FUNCTION__); 65 gMutex.lock (); 66 gTimer.kill (); 67 android::startStopPolling (false); 68 gIsBusy = true; 69 gAbortNow = false; 70 gTimer.set (gIntervalTime, pn544InteropStartPolling); //after some time, start polling again 71 gMutex.unlock (); 72 ALOGD ("%s: exit", __FUNCTION__); 73 } 74 75 76 /******************************************************************************* 77 ** 78 ** Function: pn544InteropStartPolling 79 ** 80 ** Description: Start polling when activation state is idle. 81 ** sigval: Unused. 82 ** 83 ** Returns: None 84 ** 85 *******************************************************************************/ 86 void pn544InteropStartPolling (union sigval) 87 { 88 ALOGD ("%s: enter", __FUNCTION__); 89 gMutex.lock (); 90 NfcTag::ActivationState state = NfcTag::getInstance ().getActivationState (); 91 92 if (gAbortNow) 93 { 94 ALOGD ("%s: abort now", __FUNCTION__); 95 gIsBusy = false; 96 goto TheEnd; 97 } 98 99 if (state == NfcTag::Idle) 100 { 101 ALOGD ("%s: start polling", __FUNCTION__); 102 android::startStopPolling (true); 103 gIsBusy = false; 104 } 105 else 106 { 107 ALOGD ("%s: try again later", __FUNCTION__); 108 gTimer.set (gIntervalTime, pn544InteropStartPolling); //after some time, start polling again 109 } 110 111 TheEnd: 112 gMutex.unlock (); 113 ALOGD ("%s: exit", __FUNCTION__); 114 } 115 116 117 /******************************************************************************* 118 ** 119 ** Function: pn544InteropIsBusy 120 ** 121 ** Description: Is the code performing operations? 122 ** 123 ** Returns: True if the code is busy. 124 ** 125 *******************************************************************************/ 126 bool pn544InteropIsBusy () 127 { 128 bool isBusy = false; 129 gMutex.lock (); 130 isBusy = gIsBusy; 131 gMutex.unlock (); 132 ALOGD ("%s: %u", __FUNCTION__, isBusy); 133 return isBusy; 134 } 135 136 137 /******************************************************************************* 138 ** 139 ** Function: pn544InteropAbortNow 140 ** 141 ** Description: Request to abort all operations. 142 ** 143 ** Returns: None. 144 ** 145 *******************************************************************************/ 146 void pn544InteropAbortNow () 147 { 148 ALOGD ("%s", __FUNCTION__); 149 gMutex.lock (); 150 gAbortNow = true; 151 gMutex.unlock (); 152 } 153 154