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