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