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"
26 #include "oic_malloc.h"
27 #include "aclresource.h"
28 #include "oxmverifycommon.h"
29 #include "JniDisplayVerifyNumListener.h"
30 #include "JniConfirmNumListener.h"
33 namespace PH = std::placeholders;
35 static JniPinCheckListener *jniPinListener = nullptr;
36 static JniDisplayPinListener *jniDisplayPinListener = nullptr;
37 static JniDisplayVerifyNumListener *jniDisplayMutualVerifyNumListener = nullptr;
38 static JniConfirmNumListener *jniConfirmMutualVerifyNumListener = nullptr;
40 void Callback(char *buf, size_t size)
44 jniPinListener->PinCallback(buf, size);
48 LOGE("jniPinListener is null");
52 void displayPinCB(char *pinBuf, size_t pinSize)
54 if (jniDisplayPinListener)
56 jniDisplayPinListener->displayPinCallback(pinBuf, pinSize);
60 LOGE("DisplayPinListener is null");
64 OCStackResult displayMutualVerifNumCB(uint8_t *verifyNum)
68 if (jniDisplayMutualVerifyNumListener)
70 res = jniDisplayMutualVerifyNumListener->displayMutualVerifNumCallback(verifyNum);
75 LOGE("DisplayMutualVerifyNumListener is null");
80 OCStackResult confirmMutualVerifNumCB()
84 if (jniConfirmMutualVerifyNumListener)
86 res = jniConfirmMutualVerifyNumListener->confirmMutualVerifNumCallback();
91 LOGE("ConfirmMutualVerifyNumListener is null");
97 * Class: org_iotivity_base_OcProvisioning
98 * Method: ownershipTransferCDdata
101 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_ownershipTransferCBdata
102 (JNIEnv *env, jobject thiz, jint OxmType, jobject jListener)
104 LOGD("OcProvisioning_ownershipTransferCBdata");
105 OCStackResult result = OC_STACK_ERROR;
109 OTMCallbackData_t CBData = {0};
110 if (OIC_JUST_WORKS == (OicSecOxm_t)OxmType)
112 /*NO callback required for JUST_WORKS*/
113 result = OCSecure::setInputPinCallback(NULL);
115 if (OIC_RANDOM_DEVICE_PIN == (OicSecOxm_t)OxmType)
119 delete jniPinListener;
120 jniPinListener = new JniPinCheckListener(env, jListener);
121 result = OCSecure::setInputPinCallback(Callback);
125 result = OC_STACK_INVALID_CALLBACK;
129 if (OC_STACK_OK != result)
131 ThrowOcException(result, "OcProvisioning_ownershipTransferCDdata");
135 catch (OCException& e)
137 LOGE("%s", e.reason().c_str());
138 ThrowOcException(e.code(), e.reason().c_str());
143 * Class: org_iotivity_base_OcProvisioning
144 * Method: discoverUnownedDevices
145 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
147 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverUnownedDevices1
148 (JNIEnv *env, jclass clazz, jint timeout)
150 LOGI("OcProvisioning_discoverUnownedDevices");
157 ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
160 OCStackResult result = OCSecure::discoverUnownedDevices((unsigned short)timeout, list);
162 if (OC_STACK_OK != result)
164 ThrowOcException(result, "Failed to discover Unowned devices");
168 return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
170 catch (OCException& e)
172 LOGE("%s", e.reason().c_str());
173 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
179 * Class: org_iotivity_base_OcProvisioning
180 * Method: provisionInit
181 * Signature: (Ljava/lang/String;)V
183 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_provisionInit
184 (JNIEnv *env, jclass calzz, jstring jdbPath)
186 LOGI("OcProvisioning_provisionInit");
191 ThrowOcException(OC_STACK_INVALID_PARAM, "SVR db path cannot be null");
197 dbpath = (char*)env->GetStringUTFChars(jdbPath, NULL);
198 OCStackResult result = OCSecure::provisionInit(env->GetStringUTFChars(jdbPath, NULL));
200 if (OC_STACK_OK != result)
202 env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
203 ThrowOcException(result, "Failed to Init Provisioning Manager");
206 env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
208 catch (OCException& e)
210 LOGE("%s", e.reason().c_str());
211 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
216 * Class: org_iotivity_base_OcProvisioning
217 * Method: discoverOwnedDevices
218 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
220 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverOwnedDevices1
221 (JNIEnv *env, jclass clazz , jint timeout)
223 LOGI("OcProvisioning_discoverOwnedDevices");
230 ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
233 OCStackResult result = OCSecure::discoverOwnedDevices((unsigned short)timeout, list);
234 if (OC_STACK_OK != result)
236 ThrowOcException(result, "Failed to discover Owned devices");
240 return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
242 catch (OCException& e)
244 LOGE("%s", e.reason().c_str());
245 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
251 * Class: org_iotivity_base_OcProvisioning
252 * Method: getDevicestatusLists
253 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
255 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceStatusList1
256 (JNIEnv *env, jclass clazz, jint timeout)
258 LOGI("OcProvisioning_getDeviceStatusList");
259 DeviceList_t ownedDevList, unownedDevList;
265 ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
268 OCStackResult result = OCSecure::getDevInfoFromNetwork((unsigned short)timeout,
269 ownedDevList, unownedDevList);
270 if (OC_STACK_OK != result)
272 ThrowOcException(result, "Failed to get Device Status List");
275 ownedDevList.insert(ownedDevList.end(), unownedDevList.begin(), unownedDevList.end());
276 return JniSecureUtils::convertDeviceVectorToJavaArray(env, ownedDevList);
278 catch (OCException& e)
280 LOGE("%s", e.reason().c_str());
281 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
287 * Class: org_iotivity_base_OcProvisioning
288 * Method: setDisplayNumListener
289 * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayNumListener;)V
291 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayNumListener
292 (JNIEnv *env, jclass clazz, jobject jListener)
294 LOGI("OcProvisioning_setDisplayNumListener");
298 ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listner can't be null");
301 delete jniDisplayMutualVerifyNumListener;
302 jniDisplayMutualVerifyNumListener = new JniDisplayVerifyNumListener(env, jListener);
306 OCStackResult result = OCSecure::registerDisplayNumCallback(displayMutualVerifNumCB);
307 if (OC_STACK_OK != result)
309 ThrowOcException(result, "Failed to set Listner");
313 catch (OCException& e)
315 LOGE("%s", e.reason().c_str());
316 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
322 * Class: org_iotivity_base_OcProvisioning
323 * Method: unsetDisplayNumListener
326 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetDisplayNumListener
327 (JNIEnv * env, jclass clazz)
329 LOGI("OcProvisioning_unsetDisplayNumListener");
331 OCStackResult result = OCSecure::deregisterDisplayNumCallback();
333 if (OC_STACK_OK != result)
335 ThrowOcException(OC_STACK_INVALID_CALLBACK, "Failed to unset Listener");
342 * Class: org_iotivity_base_OcProvisioning
343 * Method: setPinType0
346 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setPinType0
347 (JNIEnv *env, jclass thiz, jint pinSize, jint pinType)
349 LOGI("OcProvisioning_setPinType0");
351 OCStackResult result = OC_STACK_ERROR;
354 result = OCSecure::setRandomPinPolicy((size_t)pinSize, (OicSecPinType_t)pinType);
355 if (OC_STACK_OK != result)
357 ThrowOcException(result, "Failed to set PinType");
360 catch (OCException& e)
362 LOGE("%s", e.reason().c_str());
363 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
369 * Class: org_iotivity_base_OcProvisioning
370 * Method: setConfirmNumListener
371 * Signature: (Lorg/iotivity/base/OcProvisioning/ConfirmNumListener;)V
373 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setConfirmNumListener
374 (JNIEnv *env, jclass clazz, jobject jListener)
376 LOGI("OcProvisioning_setConfirmNumListener");
380 ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listner can't be null");
383 delete jniConfirmMutualVerifyNumListener;
384 jniConfirmMutualVerifyNumListener = new JniConfirmNumListener(env, jListener);
388 OCStackResult result = OCSecure::registerUserConfirmCallback(confirmMutualVerifNumCB);
389 if (OC_STACK_OK != result)
391 ThrowOcException(result, "Failed to set Listner");
395 catch (OCException& e)
397 LOGE("%s", e.reason().c_str());
398 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
404 * Class: org_iotivity_base_OcProvisioning
405 * Method: unsetConfirmNumListener
408 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_unsetConfirmNumListener
409 (JNIEnv *env, jclass clazz)
411 LOGI("OcProvisioning_unsetConfirmNumListener");
413 OCStackResult result = OCSecure::deregisterUserConfirmCallback();
415 if (OC_STACK_OK != result)
417 ThrowOcException(OC_STACK_INVALID_CALLBACK, "Failed to unser Listener");
423 * Class: org_iotivity_base_OcProvisioning
424 * Method: setMVJustWorksOptions0
427 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setMVJustWorksOptions0
428 (JNIEnv *env, jclass clazz, jint options)
430 LOGI("OcProvisioning_setMVJustWorksOptions0");
432 OCStackResult result = OCSecure::setVerifyOptionMask((VerifyOptionBitmask_t)options);
434 if (OC_STACK_OK != result)
436 ThrowOcException(OC_STACK_INVALID_CALLBACK, "setMVJustWorksOptions Failed");
444 * Class: org_iotivity_base_OcProvisioning
445 * Method: setDisplayPinListener
446 * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayPinListener;)V
448 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayPinListener
449 (JNIEnv *env, jclass thiz, jobject jListener)
452 LOGI("OcProvisioning_setDisplayPinListener");
456 ThrowOcException(OC_STACK_INVALID_CALLBACK, "displayPinListener can't be null");
459 delete jniDisplayPinListener;
460 jniDisplayPinListener = new JniDisplayPinListener(env, jListener);
464 OCStackResult result = OCSecure::setDisplayPinCB(displayPinCB);
465 if (OC_STACK_OK != result)
467 ThrowOcException(result, "Failed to set displayPinListener");
471 catch (OCException& e)
473 LOGE("%s", e.reason().c_str());
474 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
478 * Class: org_iotivity_base_OcProvisioning
479 * Method: saveTrustCertChain1
480 * Signature: (Lorg/iotivity/base/OcProvisioning/provisionTrustCertChain1;)V
482 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_saveTrustCertChain1
483 (JNIEnv *env, jobject thiz, jbyteArray trustCertChain, jint encodingType)
485 LOGD("OcProvisioning_saveTrustCertChain1");
486 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
487 jbyte* trustCertChainBytes = env->GetByteArrayElements(trustCertChain, 0);
488 jsize arrayLength = env->GetArrayLength(trustCertChain);
489 uint16_t credId = -1;
490 unsigned char* trustedCertChar = new unsigned char[arrayLength];
493 env->GetByteArrayRegion (trustCertChain, 0, arrayLength, reinterpret_cast<jbyte*>(trustedCertChar));
494 OCStackResult result = OCSecure::saveTrustCertChain((uint8_t*)trustedCertChar, arrayLength,
495 (OicEncodingType_t)encodingType, &credId);
496 if (OC_STACK_OK != result)
498 ThrowOcException(result, "OcProvisioning_saveTrustCertChain1");
502 catch (OCException& e)
504 LOGE("%s", e.reason().c_str());
505 ThrowOcException(e.code(), e.reason().c_str());
509 ThrowOcException(OC_STACK_INVALID_PARAM, "WITH_TLS not enabled");
511 #endif // __WITH_DTLS__ || __WITH_TLS__
515 * Class: org_iotivity_base_OcProvisioning
516 * Method: setDeviceIdSeed1
517 * Signature: (Lorg/iotivity/base/OcProvisioning/provisionTrustCertChain1;)V
519 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_setDeviceIdSeed1
520 (JNIEnv *env, jobject thiz, jbyteArray seed)
522 LOGD("OcProvisioning_setDeviceIdSeed1");
523 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
524 jbyte* byteSeed = env->GetByteArrayElements(seed, 0);
525 jsize arrayLength = env->GetArrayLength(seed);
528 env->GetByteArrayRegion (seed, 0, arrayLength, byteSeed);
529 OCStackResult result = OCSecure::setDeviceIdSeed((uint8_t*)byteSeed, arrayLength);
530 if (OC_STACK_OK != result)
532 ThrowOcException(result, "OcProvisioning_setDeviceIdSeed");
536 catch (OCException& e)
538 LOGE("%s", e.reason().c_str());
539 ThrowOcException(e.code(), e.reason().c_str());
543 ThrowOcException(OC_STACK_INVALID_PARAM, "WITH_TLS not enabled");
545 #endif // __WITH_DTLS__ || __WITH_TLS__
549 * Class: org_iotivity_base_OcProvisioning
551 * Signature: (Ljava/lang/Object;)V
553 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_saveACL
554 (JNIEnv *env , jclass thiz, jobject jacl)
556 LOGD("OcProvisioning_saveACL");
560 ThrowOcException(OC_STACK_INVALID_PARAM, "acl can't be null");
563 OicSecAcl_t *acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
566 ThrowOcException(OC_STACK_NO_MEMORY, "acl allocation failed");
570 if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, jacl, acl))
573 ThrowOcException(OC_STACK_INVALID_PARAM, "Failed to convert Java acl to OC acl");
579 OCStackResult result = OCSecure::saveACL(acl);
580 if (OC_STACK_OK != result)
582 ThrowOcException(result, "OCSecure::saveACL Failed");
586 catch (OCException& e)
588 LOGE("%s", e.reason().c_str());
589 ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
594 * Class: org_iotivity_base_OcProvisioning
595 * Method: doSelfOwnershiptransfer
598 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_doSelfOwnershiptransfer
599 (JNIEnv *env, jclass thiz)
602 LOGD("OcProvisioning_doSelfOwnershiptransfer");
606 OCStackResult result = OCSecure::configSelfOwnership();
607 if (OC_STACK_OK != result)
609 ThrowOcException(result, "OCSecure::configSelfOwnership Failed");
613 catch (OCException& e)
615 LOGE("%s", e.reason().c_str());
616 ThrowOcException(e.code(), e.reason().c_str());