2 * //******************************************************************
4 * // Copyright 2015 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "JniOcProvisioning.h"
24 #include "JniPinCheckListener.h"
25 #include "JniDisplayPinListener.h"
28 namespace PH = std::placeholders;
30 static JniPinCheckListener *jniPinListener = nullptr;
31 static JniDisplayPinListener *jniDisplayPinListener = nullptr;
33 void Callback(char *buf, size_t size)
37 jniPinListener->PinCallback(buf, size);
41 LOGE("jniPinListener is null");
45 void displayPinCB(char *pinBuf, size_t pinSize)
47 if (jniDisplayPinListener)
49 jniDisplayPinListener->displayPinCallback(pinBuf, pinSize);
53 LOGE("DisplayPinListener is null");
58 * Class: org_iotivity_base_OcProvisioning
59 * Method: ownershipTransferCDdata
62 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_ownershipTransferCBdata
63 (JNIEnv *env, jobject thiz, jint OxmType, jobject jListener)
65 LOGD("OcProvisioning_ownershipTransferCBdata");
66 OCStackResult result = OC_STACK_ERROR;
70 OTMCallbackData_t CBData = {0};
71 if (OIC_JUST_WORKS == (OicSecOxm_t)OxmType)
73 CBData.loadSecretCB = LoadSecretJustWorksCallback;
74 CBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
75 CBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
76 CBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
78 result = OCSecure::setOwnerTransferCallbackData((OicSecOxm_t)OxmType,
81 if (OIC_RANDOM_DEVICE_PIN == (OicSecOxm_t)OxmType)
85 delete jniPinListener;
86 jniPinListener = new JniPinCheckListener(env, jListener);
87 CBData.loadSecretCB = InputPinCodeCallback;
88 CBData.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
89 CBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
90 CBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
91 result = OCSecure::setOwnerTransferCallbackData((OicSecOxm_t)OxmType,
96 result = OC_STACK_INVALID_CALLBACK;
100 if (OC_STACK_OK != result)
102 ThrowOcException(result, "OcProvisioning_ownershipTransferCDdata");
106 catch (OCException& e)
108 LOGE("%s", e.reason().c_str());
109 ThrowOcException(e.code(), e.reason().c_str());
114 * Class: org_iotivity_base_OcProvisioning
115 * Method: discoverUnownedDevices
116 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
118 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverUnownedDevices1
119 (JNIEnv *env, jclass clazz, jint timeout)
121 LOGI("OcProvisioning_discoverUnownedDevices");
128 ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
131 OCStackResult result = OCSecure::discoverUnownedDevices((unsigned short)timeout, list);
133 if (OC_STACK_OK != result)
135 ThrowOcException(result, "Failed to discover Unowned devices");
139 return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
141 catch (OCException& e)
143 LOGE("%s", e.reason().c_str());
144 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
150 * Class: org_iotivity_base_OcProvisioning
151 * Method: provisionInit
152 * Signature: (Ljava/lang/String;)V
154 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_provisionInit
155 (JNIEnv *env, jclass calzz, jstring jdbPath)
157 LOGI("OcProvisioning_provisionInit");
162 ThrowOcException(OC_STACK_INVALID_PARAM, "SVR db path cannot be null");
168 dbpath = (char*)env->GetStringUTFChars(jdbPath, NULL);
169 OCStackResult result = OCSecure::provisionInit(env->GetStringUTFChars(jdbPath, NULL));
171 if (OC_STACK_OK != result)
173 env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
174 ThrowOcException(result, "Failed to Init Provisioning Manager");
177 env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
179 catch (OCException& e)
181 LOGE("%s", e.reason().c_str());
182 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
187 * Class: org_iotivity_base_OcProvisioning
188 * Method: discoverOwnedDevices
189 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
191 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverOwnedDevices1
192 (JNIEnv *env, jclass clazz , jint timeout)
194 LOGI("OcProvisioning_discoverOwnedDevices");
201 ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
204 OCStackResult result = OCSecure::discoverOwnedDevices((unsigned short)timeout, list);
205 if (OC_STACK_OK != result)
207 ThrowOcException(result, "Failed to discover Owned devices");
211 return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
213 catch (OCException& e)
215 LOGE("%s", e.reason().c_str());
216 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
222 * Class: org_iotivity_base_OcProvisioning
223 * Method: getDevicestatusLists
224 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
226 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceStatusList1
227 (JNIEnv *env, jclass clazz, jint timeout)
229 LOGI("OcProvisioning_getDeviceStatusList");
230 DeviceList_t ownedDevList, unownedDevList;
236 ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
239 OCStackResult result = OCSecure::getDevInfoFromNetwork((unsigned short)timeout,
240 ownedDevList, unownedDevList);
241 if (OC_STACK_OK != result)
243 ThrowOcException(result, "Failed to get Device Status List");
246 ownedDevList.insert(ownedDevList.end(), unownedDevList.begin(), unownedDevList.end());
247 return JniSecureUtils::convertDeviceVectorToJavaArray(env, ownedDevList);
249 catch (OCException& e)
251 LOGE("%s", e.reason().c_str());
252 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
258 * Class: org_iotivity_base_OcProvisioning
259 * Method: setDisplayPinListener
260 * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayPinListener;)V
262 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayPinListener
263 (JNIEnv *env, jclass thiz, jobject jListener)
266 LOGI("OcProvisioning_setDisplayPinListener");
270 ThrowOcException(OC_STACK_INVALID_CALLBACK, "displayPinListener can't be null");
273 delete jniDisplayPinListener;
274 jniDisplayPinListener = new JniDisplayPinListener(env, jListener);
278 OCStackResult result = OCSecure::setDisplayPinCB(displayPinCB);
279 if (OC_STACK_OK != result)
281 ThrowOcException(result, "Failed to set displayPinListener");
285 catch (OCException& e)
287 LOGE("%s", e.reason().c_str());
288 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());