Home | History | Annotate | Download | only in jni
      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 #include "OverrideLog.h"
     18 #include "NfcJniUtil.h"
     19 #include "NfcAdaptation.h"
     20 #include "PeerToPeer.h"
     21 #include "JavaClassConstants.h"
     22 extern "C"
     23 {
     24     #include "nfa_api.h"
     25     #include "nfa_p2p_api.h"
     26 }
     27 
     28 
     29 namespace android
     30 {
     31 
     32 
     33 /*******************************************************************************
     34 **
     35 ** Function:        nativeLlcpServiceSocket_doAccept
     36 **
     37 ** Description:     Accept a connection request from a peer.
     38 **                  e: JVM environment.
     39 **                  o: Java object.
     40 **                  miu: Maximum information unit.
     41 **                  rw: Receive window.
     42 **                  linearBufferLength: Not used.
     43 **
     44 ** Returns:         LlcpSocket Java object.
     45 **
     46 *******************************************************************************/
     47 static jobject nativeLlcpServiceSocket_doAccept(JNIEnv *e, jobject o, jint miu, jint rw, jint linearBufferLength)
     48 {
     49     jobject     clientSocket = NULL;
     50     jclass      clsNativeLlcpSocket = NULL;
     51     jfieldID    f = 0;
     52     PeerToPeer::tJNI_HANDLE serverHandle; //handle of the local server
     53     bool        stat = false;
     54     PeerToPeer::tJNI_HANDLE connHandle = PeerToPeer::getInstance().getNewJniHandle ();
     55 
     56     ALOGD ("%s: enter", __FUNCTION__);
     57 
     58     serverHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle (e, o);
     59 
     60     stat = PeerToPeer::getInstance().accept (serverHandle, connHandle, miu, rw);
     61 
     62     if (! stat)
     63     {
     64         ALOGE ("%s: fail accept", __FUNCTION__);
     65         goto TheEnd;
     66     }
     67 
     68     /* Create new LlcpSocket object */
     69     if (nfc_jni_cache_object(e, gNativeLlcpSocketClassName, &(clientSocket)) == -1)
     70     {
     71         ALOGE ("%s: fail create NativeLlcpSocket", __FUNCTION__);
     72         goto TheEnd;
     73     }
     74 
     75     /* Get NativeConnectionOriented class object */
     76     clsNativeLlcpSocket = e->GetObjectClass (clientSocket);
     77     if (e->ExceptionCheck())
     78     {
     79         e->ExceptionClear();
     80         ALOGE ("%s: get class object error", __FUNCTION__);
     81         goto TheEnd;
     82     }
     83 
     84     /* Set socket handle */
     85     f = e->GetFieldID (clsNativeLlcpSocket, "mHandle", "I");
     86     e->SetIntField (clientSocket, f, (jint) connHandle);
     87 
     88     /* Set socket MIU */
     89     f = e->GetFieldID (clsNativeLlcpSocket, "mLocalMiu", "I");
     90     e->SetIntField (clientSocket, f, (jint)miu);
     91 
     92     /* Set socket RW */
     93     f = e->GetFieldID (clsNativeLlcpSocket, "mLocalRw", "I");
     94     e->SetIntField (clientSocket, f, (jint)rw);
     95 
     96 TheEnd:
     97     ALOGD ("%s: exit", __FUNCTION__);
     98     return clientSocket;
     99 }
    100 
    101 
    102 /*******************************************************************************
    103 **
    104 ** Function:        nativeLlcpServiceSocket_doClose
    105 **
    106 ** Description:     Close a server socket.
    107 **                  e: JVM environment.
    108 **                  o: Java object.
    109 **
    110 ** Returns:         True if ok.
    111 **
    112 *******************************************************************************/
    113 static jboolean nativeLlcpServiceSocket_doClose(JNIEnv *e, jobject o)
    114 {
    115     ALOGD ("%s: enter", __FUNCTION__);
    116     PeerToPeer::tJNI_HANDLE jniServerHandle = 0;
    117     bool stat = false;
    118 
    119     jniServerHandle = nfc_jni_get_nfc_socket_handle (e, o);
    120 
    121     stat = PeerToPeer::getInstance().deregisterServer (jniServerHandle);
    122 
    123     ALOGD ("%s: exit", __FUNCTION__);
    124     return JNI_TRUE;
    125 }
    126 
    127 
    128 /*****************************************************************************
    129 **
    130 ** Description:     JNI functions
    131 **
    132 *****************************************************************************/
    133 static JNINativeMethod gMethods[] =
    134 {
    135     {"doAccept", "(III)Lcom/android/nfc/dhimpl/NativeLlcpSocket;", (void*) nativeLlcpServiceSocket_doAccept},
    136     {"doClose", "()Z", (void*) nativeLlcpServiceSocket_doClose},
    137 };
    138 
    139 
    140 /*******************************************************************************
    141 **
    142 ** Function:        register_com_android_nfc_NativeLlcpServiceSocket
    143 **
    144 ** Description:     Regisgter JNI functions with Java Virtual Machine.
    145 **                  e: Environment of JVM.
    146 **
    147 ** Returns:         Status of registration.
    148 **
    149 *******************************************************************************/
    150 int register_com_android_nfc_NativeLlcpServiceSocket (JNIEnv* e)
    151 {
    152     return jniRegisterNativeMethods (e, gNativeLlcpServiceSocketClassName,
    153             gMethods, NELEM(gMethods));
    154 }
    155 
    156 
    157 } //namespace android
    158 
    159