這篇文章用來分析NFC學習——NFC Enable 過程分析(一) 中setp 3-2:開啟一些循環監聽的線程服務。處理方法enableDisable().
code路徑:packages/apps/nfc/src/com/android/nfc/P2pLinkManager.java,具體看enableDisable()中的處理code:
[html] view plain copy
- public void enableDisable(boolean sendEnable, boolean receiveEnable) {
- synchronized (this) {
- if (!mIsReceiveEnabled && receiveEnable) {
- //setp1:啟動SnepServer
- mDefaultSnepServer.start();
- //setp2:啟動NdefPushServer
- mNdefPushServer.start();
- if (mEchoServer != null) {
- //setp3:啟動EchoServer
- mHandler.sendEmptyMessage(MSG_START_ECHOSERVER);
- }
- }
- }
- }
Setp1:啟動SnepServer,此Server的作用是接收NDEF消息,并把它推送給LLCP(Logical Link Control Protocol).啟動SnepServer最後轉到其内部類ServerThread去處理,ServerThread繼承Thread,具體分析它的run方法。
code路徑:packages/apps/nfc/src/com/android/nfc/snep/SnepServer.java
[html] view plain copy
- public void run() {
- ........
- while (threadRunning) {
- synchronized (SnepServer.this) {
- //setp1-1:建立一個伺服器端Socket連接配接
- mServerSocket = NfcService.getInstance().createLlcpServerSocket(mServiceSap,
- mServiceName, MIU, 1, 1024);
- }
- ......
- //接收Socket請求
- LlcpSocket communicationSocket = serverSocket.accept();
- if (communicationSocket != null) {
- //setp1-2:miu 是什麼,如何擷取來的
- int miu = communicationSocket.getRemoteMiu();
- int fragmentLength = (mFragmentLength == -1) ?
- miu : Math.min(miu, mFragmentLength);
- //setp1-3: 啟動線程處理SnepMessenger
- new ConnectionThread(communicationSocket, fragmentLength).start();
- }
- ......
- }
- }
Setp1-1:建立伺服器端mServerSocket中參數如下:
mServiceSap:socket 端口,預設是4;
mServiceName:從命名可以看出,它是socket名稱
MIU:全稱是Maximum information Unit ,LLCP中資料單元中消息最大的長度(不知道是不是按byte算的),
1:
1024:是buffer的長度,至于它的作用是什麼,贊時還不知道。
mServerSocket的建立過程:通過NfcService調用createLlcpServerSocket(),而NfcService直接傳回的DeviceHost.createLlcpServerSocket()的調用,DeviceHost僅僅隻是一個接口,DeviceHost.createLlcpServerSocket()具體實作在NativeNfcManager.createLlcpServerSocket().NativeNfcManager則直接調用JNI方法com_android_nfc_NfcManager_doCreateLlcpServiceSocket()來實作其建立功能,其中的參數為了書寫友善都省略了。
[html] view plain copy
- static jobject com_android_nfc_NfcManager_doCreateLlcpServiceSocket(JNIEnv *e, jobject o,
- jint nSap, jstring sn, jint miu, jint rw, jint linearBufferLength)
- {
- ......
- ret = phLibNfc_Llcp_Socket(phFriNfc_LlcpTransport_eConnectionOriented,
- &sOptions,
- &sWorkingBuffer,
- &hLlcpSocket,
- nfc_jni_llcp_transport_socket_err_callback,
- (void*)nat);
- ......
- if(nfc_jni_cache_object(e,"com/android/nfc/nxp/NativeLlcpServiceSocket",&(serviceSocket)) == -1)
- {
- ALOGE("Llcp Socket object creation error");
- goto error;
- }
- ......
- return serviceSocket;
- }
附上一張思路圖,就清晰點
Setp1-2:miu如何得來的,看下面分析圖
看到這張圖,可能隻是對函數的調用過程有個清晰的了解,但是miu到底是如何擷取到的呢??這次我們從函數的調用最後來分析,即phFriNfc_LlcpTransport_Connection.c這個檔案的函數。
[html] view plain copy
- NFCSTATUS phFriNfc_LlcpTransport_ConnectionOriented_SocketGetRemoteOptions(
- phFriNfc_LlcpTransport_Socket_t* pLlcpSocket,
- phLibNfc_Llcp_sSocketOptions_t* psRemoteOptions)
- {
- NFCSTATUS status = NFCSTATUS_SUCCESS;
- psRemoteOptions->miu = pLlcpSocket->remoteMIU;
- psRemoteOptions->rw = pLlcpSocket->remoteRW;
- return status;
- }
上面code可以發現phFriNfc_LlcpTransport_Socket_t 的remoteMIU 直接賦給phLibNfc_Llcp_sSocketOptions_t的miu.這就是我們要找的答案。phFriNfc_LlcpTransport_Socket_t 和phLibNfc_Llcp_sSocketOptions_t具體是什麼就不做研究了。
setp1-3: 啟動線程處理SnepMessenger,從新開啟一個Thread處理SnepMessage,處理code 還是SnepServer.java
[html] view plain copy
- static boolean handleRequest(SnepMessenger messenger, Callback callback) throws IOException {
- SnepMessage request;
- ......
- //從SnepMessenger中取出SnepMessage
- request = messenger.getMessage();
- if (((request.getVersion() & 0xF0) >> 4) != SnepMessage.VERSION_MAJOR) {
- messenger.sendMessage(SnepMessage.getMessage(
- SnepMessage.RESPONSE_UNSUPPORTED_VERSION));
- } else if (request.getField() == SnepMessage.REQUEST_GET) {
- messenger.sendMessage(callback.doGet(request.getAcceptableLength(),
- request.getNdefMessage()));
- } else if (request.getField() == SnepMessage.REQUEST_PUT) {
- if (DBG) Log.d(TAG, "putting message " + request.toString());
- //發送SnepMessage,SnepMessage來源于Callback的實作
- messenger.sendMessage(callback.doPut(request.getNdefMessage()));
- } else {
- if (DBG) Log.d(TAG, "Unknown request (" + request.getField() +")");
- messenger.sendMessage(SnepMessage.getMessage(
- SnepMessage.RESPONSE_BAD_REQUEST));
- }
- return true;
- }
上面code涉及到CallBack一個回調接口,它實作在P2pLinkManager.java中
[html] view plain copy
- final SnepServer.Callback mDefaultSnepCallback = new SnepServer.Callback() {
- @Override
- public SnepMessage doPut(NdefMessage msg) {
- onReceiveComplete(msg);
- return SnepMessage.getMessage(SnepMessage.RESPONSE_SUCCESS);
- }
- @Override
- public SnepMessage doGet(int acceptableLength, NdefMessage msg) {
- NdefMessage response = mHandoverManager.tryHandoverRequest(msg);
- if (response != null) {
- onReceiveHandover();
- return SnepMessage.getSuccessResponse(response);
- } else {
- return SnepMessage.getMessage(SnepMessage.RESPONSE_NOT_FOUND);
- }
- }
- };
setp2:啟動NdefPushServer,setp3:啟動EchoServer的過程和setp1:啟動SnepServer過程類似,具體可以參照setp1,在此就不做詳細分析。
以上分析中涉及到SnepServer,NdefPushServer,EchoServer。看下這三個檔案中對自己的注釋說明。。
SnepServer: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 NfcAdapter.enableForegroundNdefPush.通過LLCP接收NDEF 消息,并把消息通過NfcAdapter.enableForegroundNdefPush設定到用戶端。
NdefPushServer:同SnepServer。
EchoServer:EchoServer is an implementation of the echo server that is used in the nfcpy LLCP test suite. Enabling the EchoServer allows to test Android NFC devices against nfcpy,這是code中給的注釋說明,但不知到nfcpy是什麼東西,故不做翻譯了。