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 #include "OverrideLog.h"
     17 #include "PeerToPeer.h"
     18 #include "JavaClassConstants.h"
     19 #include <ScopedPrimitiveArray.h>
     20 #include <ScopedUtfChars.h>
     21 
     22 
     23 namespace android
     24 {
     25 
     26 
     27 /*******************************************************************************
     28 **
     29 ** Function:        nativeLlcpSocket_doConnect
     30 **
     31 ** Description:     Establish a connection to the peer.
     32 **                  e: JVM environment.
     33 **                  o: Java object.
     34 **                  nSap: Service access point.
     35 **
     36 ** Returns:         True if ok.
     37 **
     38 *******************************************************************************/
     39 static jboolean nativeLlcpSocket_doConnect (JNIEnv* e, jobject o, jint nSap)
     40 {
     41     ALOGD ("%s: enter; sap=%d", __FUNCTION__, nSap);
     42 
     43     PeerToPeer::tJNI_HANDLE jniHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle(e, o);
     44     bool stat = PeerToPeer::getInstance().connectConnOriented (jniHandle, nSap);
     45 
     46     ALOGD ("%s: exit", __FUNCTION__);
     47     return stat ? JNI_TRUE : JNI_FALSE;
     48 }
     49 
     50 
     51 /*******************************************************************************
     52 **
     53 ** Function:        nativeLlcpSocket_doConnectBy
     54 **
     55 ** Description:     Establish a connection to the peer.
     56 **                  e: JVM environment.
     57 **                  o: Java object.
     58 **                  sn: Service name.
     59 **
     60 ** Returns:         True if ok.
     61 **
     62 *******************************************************************************/
     63 static jboolean nativeLlcpSocket_doConnectBy (JNIEnv* e, jobject o, jstring sn)
     64 {
     65     ALOGD ("%s: enter", __FUNCTION__);
     66 
     67     PeerToPeer::tJNI_HANDLE jniHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle(e, o);
     68 
     69     ScopedUtfChars serviceName(e, sn);
     70     if (serviceName.c_str() == NULL) {
     71         return JNI_FALSE;
     72     }
     73     bool stat = PeerToPeer::getInstance().connectConnOriented(jniHandle, serviceName.c_str());
     74 
     75     ALOGD ("%s: exit", __FUNCTION__);
     76     return stat ? JNI_TRUE : JNI_FALSE;
     77 }
     78 
     79 
     80 /*******************************************************************************
     81 **
     82 ** Function:        nativeLlcpSocket_doClose
     83 **
     84 ** Description:     Close socket.
     85 **                  e: JVM environment.
     86 **                  o: Java object.
     87 **
     88 ** Returns:         True if ok.
     89 **
     90 *******************************************************************************/
     91 static jboolean nativeLlcpSocket_doClose(JNIEnv *e, jobject o)
     92 {
     93     ALOGD ("%s: enter", __FUNCTION__);
     94 
     95     PeerToPeer::tJNI_HANDLE jniHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle(e, o);
     96     bool stat = PeerToPeer::getInstance().disconnectConnOriented (jniHandle);
     97 
     98     ALOGD ("%s: exit", __FUNCTION__);
     99     return JNI_TRUE; // TODO: stat?
    100 }
    101 
    102 
    103 /*******************************************************************************
    104 **
    105 ** Function:        nativeLlcpSocket_doSend
    106 **
    107 ** Description:     Send data to peer.
    108 **                  e: JVM environment.
    109 **                  o: Java object.
    110 **                  data: Buffer of data.
    111 **
    112 ** Returns:         True if sent ok.
    113 **
    114 *******************************************************************************/
    115 static jboolean nativeLlcpSocket_doSend (JNIEnv* e, jobject o, jbyteArray data)
    116 {
    117     ALOGD_IF ((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "%s: enter", __FUNCTION__);
    118 
    119     ScopedByteArrayRO bytes(e, data);
    120 
    121     PeerToPeer::tJNI_HANDLE jniHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle(e, o);
    122     UINT8* raw_ptr = const_cast<UINT8*>(reinterpret_cast<const UINT8*>(&bytes[0])); // TODO: API bug: send should take const*!
    123     bool stat = PeerToPeer::getInstance().send(jniHandle, raw_ptr, bytes.size());
    124 
    125     ALOGD_IF ((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "%s: exit", __FUNCTION__);
    126     return stat ? JNI_TRUE : JNI_FALSE;
    127 }
    128 
    129 
    130 /*******************************************************************************
    131 **
    132 ** Function:        nativeLlcpSocket_doReceive
    133 **
    134 ** Description:     Receive data from peer.
    135 **                  e: JVM environment.
    136 **                  o: Java object.
    137 **                  origBuffer: Buffer to put received data.
    138 **
    139 ** Returns:         Number of bytes received.
    140 **
    141 *******************************************************************************/
    142 static jint nativeLlcpSocket_doReceive(JNIEnv *e, jobject o, jbyteArray origBuffer)
    143 {
    144     ALOGD_IF ((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "%s: enter", __FUNCTION__);
    145 
    146     ScopedByteArrayRW bytes(e, origBuffer);
    147 
    148     PeerToPeer::tJNI_HANDLE jniHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle(e, o);
    149     uint16_t actualLen = 0;
    150     bool stat = PeerToPeer::getInstance().receive(jniHandle, reinterpret_cast<UINT8*>(&bytes[0]), bytes.size(), actualLen);
    151 
    152     jint retval = 0;
    153     if (stat && (actualLen>0)) {
    154         retval = actualLen;
    155     } else {
    156         retval = -1;
    157     }
    158 
    159     ALOGD_IF ((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "%s: exit; actual len=%d", __FUNCTION__, retval);
    160     return retval;
    161 }
    162 
    163 
    164 /*******************************************************************************
    165 **
    166 ** Function:        nativeLlcpSocket_doGetRemoteSocketMIU
    167 **
    168 ** Description:     Get peer's maximum information unit.
    169 **                  e: JVM environment.
    170 **                  o: Java object.
    171 **
    172 ** Returns:         Peer's maximum information unit.
    173 **
    174 *******************************************************************************/
    175 static jint nativeLlcpSocket_doGetRemoteSocketMIU (JNIEnv* e, jobject o)
    176 {
    177     ALOGD ("%s: enter", __FUNCTION__);
    178 
    179     PeerToPeer::tJNI_HANDLE jniHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle(e, o);
    180     jint miu = PeerToPeer::getInstance().getRemoteMaxInfoUnit(jniHandle);
    181 
    182     ALOGD ("%s: exit", __FUNCTION__);
    183     return miu;
    184 }
    185 
    186 
    187 /*******************************************************************************
    188 **
    189 ** Function:        nativeLlcpSocket_doGetRemoteSocketRW
    190 **
    191 ** Description:     Get peer's receive window size.
    192 **                  e: JVM environment.
    193 **                  o: Java object.
    194 **
    195 ** Returns:         Peer's receive window size.
    196 **
    197 *******************************************************************************/
    198 static jint nativeLlcpSocket_doGetRemoteSocketRW (JNIEnv* e, jobject o)
    199 {
    200     ALOGD ("%s: enter", __FUNCTION__);
    201 
    202     PeerToPeer::tJNI_HANDLE jniHandle = (PeerToPeer::tJNI_HANDLE) nfc_jni_get_nfc_socket_handle(e, o);
    203     jint rw = PeerToPeer::getInstance().getRemoteRecvWindow (jniHandle);
    204 
    205     ALOGD ("%s: exit", __FUNCTION__);
    206     return rw;
    207 }
    208 
    209 
    210 /*****************************************************************************
    211 **
    212 ** Description:     JNI functions
    213 **
    214 *****************************************************************************/
    215 static JNINativeMethod gMethods[] =
    216 {
    217     {"doConnect", "(I)Z", (void * ) nativeLlcpSocket_doConnect},
    218     {"doConnectBy", "(Ljava/lang/String;)Z", (void*) nativeLlcpSocket_doConnectBy},
    219     {"doClose", "()Z", (void *) nativeLlcpSocket_doClose},
    220     {"doSend", "([B)Z", (void *) nativeLlcpSocket_doSend},
    221     {"doReceive", "([B)I", (void *) nativeLlcpSocket_doReceive},
    222     {"doGetRemoteSocketMiu", "()I", (void *) nativeLlcpSocket_doGetRemoteSocketMIU},
    223     {"doGetRemoteSocketRw", "()I", (void *) nativeLlcpSocket_doGetRemoteSocketRW},
    224 };
    225 
    226 
    227 /*******************************************************************************
    228 **
    229 ** Function:        register_com_android_nfc_NativeLlcpSocket
    230 **
    231 ** Description:     Regisgter JNI functions with Java Virtual Machine.
    232 **                  e: Environment of JVM.
    233 **
    234 ** Returns:         Status of registration.
    235 **
    236 *******************************************************************************/
    237 int register_com_android_nfc_NativeLlcpSocket (JNIEnv* e)
    238 {
    239     return jniRegisterNativeMethods (e, gNativeLlcpSocketClassName, gMethods, NELEM(gMethods));
    240 }
    241 
    242 
    243 } //namespace android
    244