Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcDirectPairDevice.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include "JniOcDirectPairDevice.h"
24 #include "OCDirectPairing.h"
25 #include "OCDirectPairing.h"
26 using namespace OC;
27 namespace PH = std::placeholders;
28
29 JniOcDirectPairDevice::JniOcDirectPairDevice(std::shared_ptr<OC::OCDirectPairing> directPairingDevice)
30     : m_sharedDirectPairDevice(directPairingDevice)
31 {
32 }
33
34 JniOcDirectPairDevice::~JniOcDirectPairDevice()
35 {
36     LOGD("~JniOcDirectPairDevice()");
37     m_sharedDirectPairDevice = nullptr;
38 }
39
40 std::string JniOcDirectPairDevice::getHost()
41 {
42    return m_sharedDirectPairDevice->getHost();
43 }
44
45 std::vector<OCPrm_t> JniOcDirectPairDevice::getPairingMethods()
46 {
47
48    return m_sharedDirectPairDevice->getPairingMethods();
49 }
50
51 OCConnectivityType JniOcDirectPairDevice::connectivityType()
52 {
53     return m_sharedDirectPairDevice->getConnType();
54 }
55
56 JniOcDirectPairDevice* JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(JNIEnv *env, jobject thiz)
57 {
58     JniOcDirectPairDevice *dpDev = GetHandle<JniOcDirectPairDevice>(env, thiz);
59     if (env->ExceptionCheck())
60     {
61         LOGE("Failed to get native handle from OcDirectPairingDevice");
62     }
63     if (!dpDev)
64     {
65         ThrowOcException(JNI_NO_NATIVE_POINTER, "");
66     }
67     return dpDev;
68 }
69 std::shared_ptr<OC::OCDirectPairing> JniOcDirectPairDevice::getPtr()
70 {
71     return m_sharedDirectPairDevice;
72 }
73
74 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcDirectPairDevice_getHost
75 (JNIEnv *env, jobject thiz)
76 {
77
78     LOGD("OcDirectPairing_getHost");
79
80     JniOcDirectPairDevice* device = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env,thiz);
81     if (!device)
82     {
83         return nullptr;
84     }
85
86     std::string dev  = device->getHost();
87     return env->NewStringUTF(dev.c_str());
88 }
89
90 JNIEXPORT jintArray JNICALL Java_org_iotivity_base_OcDirectPairDevice_getPairingMethods
91         (JNIEnv *env, jobject thiz)
92 {
93
94     LOGD("OcDirectPairing_getPairingMethods");
95
96     std::vector<jint> pairingMethodList;
97     JniOcDirectPairDevice* device = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env,thiz);
98     if (!device)
99     {
100         return nullptr;
101     }
102
103     std::vector<OCPrm_t> pairingMethods = device->getPairingMethods();
104     return JniOcDirectPairDevice::JconvertIntVectorToJavaList(env,pairingMethods);
105 }
106
107
108 jintArray JniOcDirectPairDevice::JconvertIntVectorToJavaList(JNIEnv *env, std::vector<OCPrm_t> &vector)
109 {
110
111     jsize len = static_cast<jsize>(vector.size());
112
113     jintArray intArray = env->NewIntArray(len);
114     if (!intArray)
115     {
116         return nullptr;
117     }
118
119     env->SetIntArrayRegion(intArray, (jsize)0, len, (const jint*)&vector[0]);
120
121     if (env->ExceptionCheck())
122     {
123         LOGE("ArrayIndexOutOfBoundsException in  JconvertIntVectorToJavaList");
124     }
125     return intArray;
126 }
127
128 /*
129 * Class:     org_iotivity_base_OcDirectPairDevice
130 * Method:    getConnectivityTypeN
131 * Signature: ()I
132 */
133 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcDirectPairDevice_getConnectivityTypeN
134 (JNIEnv *env, jobject thiz)
135 {
136     LOGD("OcDirectPairDevice_getConnectivityType");
137     JniOcDirectPairDevice* device = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env,thiz);
138     if (!device)
139     {
140         return -1;
141     }
142
143     OCConnectivityType connectivityType = device->connectivityType();
144     return static_cast<jint>(connectivityType);
145 }