4c81fc5009cc04f88750896ee2c434b87222ae3e
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcSecureResource.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 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 "JniOcSecureResource.h"
24 #include "JniSecureUtils.h"
25 #include "aclresource.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
28 namespace PH = std::placeholders;
29
30 JniOcSecureResource::JniOcSecureResource(std::shared_ptr<OCSecureResource> device)
31     : m_sharedSecureResource(device)
32 {}
33
34 JniOcSecureResource::~JniOcSecureResource()
35 {
36     LOGD("~JniOcSecureResource()");
37     m_sharedSecureResource = nullptr;
38 }
39
40 std::string JniOcSecureResource::getIpAddr()
41 {
42     return m_sharedSecureResource->getDevAddr();
43 }
44
45 std::string JniOcSecureResource::getDeviceID()
46 {
47     return m_sharedSecureResource->getDeviceID();
48 }
49
50 int JniOcSecureResource::getDeviceStatus()
51 {
52     return m_sharedSecureResource->getDeviceStatus();
53 }
54
55 bool JniOcSecureResource::getOwnedStatus()
56 {
57     return m_sharedSecureResource->getOwnedStatus();
58 }
59
60 OCSecureResource* JniOcSecureResource::getDevicePtr()
61 {
62     return m_sharedSecureResource.get();
63 }
64
65 JniOcSecureResource* JniOcSecureResource::getJniOcSecureResourcePtr(JNIEnv *env, jobject thiz)
66 {
67     JniOcSecureResource *secureResource = GetHandle<JniOcSecureResource>(env, thiz);
68     if (env->ExceptionCheck())
69     {
70         LOGE("Failed to get native handle from OcSecureResource");
71     }
72     if (!secureResource)
73     {
74         ThrowOcException(JNI_NO_NATIVE_POINTER, "");
75     }
76     return secureResource;
77 }
78
79 JniProvisionResultListner* JniOcSecureResource::AddProvisionResultListener(JNIEnv* env,
80         jobject jListener)
81 {
82     JniProvisionResultListner *resultListener = NULL;
83     resultMapLock.lock();
84
85     for (auto it = resultMap.begin(); it != resultMap.end(); ++it)
86     {
87         if (env->IsSameObject(jListener, it->first))
88         {
89             auto refPair = it->second;
90             resultListener = refPair.first;
91             refPair.second++;
92             it->second = refPair;
93             resultMap.insert(*it);
94             LOGD("Provision resultListener: ref. count incremented");
95             break;
96         }
97     }
98     if (!resultListener)
99     {
100         resultListener = new JniProvisionResultListner(env, jListener,
101             RemoveCallback(std::bind(&JniOcSecureResource::RemoveProvisionResultListener,
102                     this, PH::_1, PH::_2)));
103         jobject jgListener = env->NewGlobalRef(jListener);
104
105         resultMap.insert(std::pair < jobject, std::pair < JniProvisionResultListner*,
106                 int >> (jgListener, std::pair<JniProvisionResultListner*, int>(resultListener, 1)));
107         LOGD("Provision resultListener: new listener");
108     }
109     resultMapLock.unlock();
110     return resultListener;
111 }
112
113 void JniOcSecureResource::RemoveProvisionResultListener(JNIEnv* env, jobject jListener)
114 {
115     resultMapLock.lock();
116
117     for (auto it = resultMap.begin(); it != resultMap.end(); ++it)
118     {
119         if (env->IsSameObject(jListener, it->first))
120         {
121             auto refPair = it->second;
122             if (refPair.second > 1)
123             {
124                 refPair.second--;
125                 it->second = refPair;
126                 resultMap.insert(*it);
127                 LOGI("Provision resultListener: ref. count decremented");
128             }
129             else
130             {
131                 env->DeleteGlobalRef(it->first);
132                 JniProvisionResultListner* listener = refPair.first;
133                 delete listener;
134                 resultMap.erase(it);
135                 LOGI("Provision resultListener removed");
136             }
137             break;
138         }
139     }
140     resultMapLock.unlock();
141 }
142
143 OCStackResult JniOcSecureResource::doOwnershipTransfer(JNIEnv* env, jobject jListener)
144 {
145     JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener);
146
147     ResultCallBack resultCallback = [resultListener](PMResultList_t *result, int hasError)
148     {
149         resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::OWNERSHIPTRANSFER);
150     };
151
152     return m_sharedSecureResource->doOwnershipTransfer(resultCallback);
153 }
154
155 OCStackResult JniOcSecureResource::getLinkedDevices(JNIEnv *env, UuidList_t &uuidList)
156 {
157     return m_sharedSecureResource->getLinkedDevices(uuidList);
158 }
159
160 OCStackResult JniOcSecureResource::removeDevice(JNIEnv* env, jint timeout, jobject jListener)
161 {
162     JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener);
163
164     ResultCallBack resultCallback = [resultListener](PMResultList_t *result, int hasError)
165     {
166         resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::REMOVEDEVICE);
167     };
168
169     return m_sharedSecureResource->removeDevice((int)timeout, resultCallback);
170 }
171
172 OCStackResult JniOcSecureResource::unlinkDevices(JNIEnv* env, jobject _device2, jobject jListener)
173 {
174     JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener);
175
176     ResultCallBack resultCallback = [resultListener](PMResultList_t *result, int hasError)
177     {
178         resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::UNLINKDEVICES);
179     };
180
181     JniOcSecureResource *device2 = JniOcSecureResource::getJniOcSecureResourcePtr(env, _device2);
182     if (!device2)
183     {
184         return OC_STACK_ERROR;
185     }
186
187     return m_sharedSecureResource->unlinkDevices(*device2->getDevicePtr(), resultCallback);
188 }
189
190 OCStackResult JniOcSecureResource::provisionCredentials(JNIEnv* env, jint type, jint keySize,
191         jobject _device2, jobject jListener)
192 {
193     JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener);
194
195     ResultCallBack resultCallback = [resultListener](PMResultList_t *result, int hasError)
196     {
197         resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::PROVISIONCREDENTIALS);
198     };
199
200     JniOcSecureResource *device2 = JniOcSecureResource::getJniOcSecureResourcePtr(env, _device2);
201     if (!device2)
202     {
203         return OC_STACK_ERROR;
204     }
205
206     Credential cred((OicSecCredType_t)type, keySize);
207
208     return m_sharedSecureResource->provisionCredentials(cred, *device2->getDevicePtr(),
209             resultCallback);
210 }
211
212 OCStackResult JniOcSecureResource::provisionACL(JNIEnv* env, jobject _acl, jobject jListener)
213 {
214     OCStackResult ret;
215     JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener);
216     OicSecAcl_t *acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
217
218     if (!acl)
219     {
220         return OC_STACK_NO_MEMORY;
221     }
222
223     if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, _acl, acl))
224     {
225         DeleteACLList(acl);
226         return OC_STACK_ERROR;
227     }
228
229     ResultCallBack resultCallback = [acl, resultListener](PMResultList_t *result, int hasError)
230     {
231         DeleteACLList(acl);
232         resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::PROVISIONACL);
233     };
234     ret = m_sharedSecureResource->provisionACL(acl, resultCallback);
235
236     if (ret != OC_STACK_OK)
237     {
238         DeleteACLList(acl);
239     }
240     return ret;
241 }
242
243 OCStackResult JniOcSecureResource::provisionDirectPairing(JNIEnv* env, jobjectArray jpdacls,
244         jobject jListener, std::string pin, std::vector<int> prms, int edp)
245 {
246     OCStackResult ret;
247     JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener);
248
249     jsize len = env->GetArrayLength(jpdacls);
250
251     OicSecPconf_t *pconf = nullptr;
252     OicSecPdAcl_t *head = nullptr;
253
254     for (jsize i = 0; i < len; ++i)
255     {
256         OicSecPdAcl_t *pdacl = new OicSecPdAcl_t;
257         if (!pdacl)
258         {
259             return OC_STACK_NO_MEMORY;
260         }
261
262         memset(pdacl, 0, sizeof(OicSecPdAcl_t));
263         pdacl->next = nullptr;
264
265         jobject jpdacl  = env->GetObjectArrayElement(jpdacls, i);
266
267         if (OC_STACK_OK != JniSecureUtils::convertJavaPdACLToOCAcl(env, jpdacl, pdacl))
268         {
269             delete pdacl;
270             return OC_STACK_ERROR;
271         }
272
273         pdacl->next = head;
274         head = pdacl;
275     }
276
277     pconf = new OicSecPconf_t;
278     memset(pconf, 0, sizeof(OicSecPconf_t));
279     pconf->edp = edp;
280     pconf->prmLen = prms.size();
281     pconf->prm = new OicSecPrm_t[pconf->prmLen];
282     pconf->pddevLen = 0;
283
284     for (int i = 0 ; i < prms.size(); i++)
285         pconf->prm[i] = (OicSecPrm_t)prms[i];
286
287     memcpy(pconf->pin.val, pin.c_str(), DP_PIN_LENGTH);
288     pconf->pdacls = head;
289
290     ResultCallBack resultCallback = [head, resultListener, pconf, prms]
291         (PMResultList_t *result, int hasError)
292         {
293             OicSecPdAcl_t *tmp1, *tmp2;
294             tmp1 = head;
295             while (tmp1)
296             {
297                 tmp2 = tmp1->next;
298                 delete tmp1;
299                 tmp1 = tmp2;
300             }
301
302             delete pconf->prm;
303             delete pconf;
304             resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::PROVISIONDIRECTPAIRING);
305         };
306
307     ret = m_sharedSecureResource->provisionDirectPairing(pconf, resultCallback);
308
309     if (ret != OC_STACK_OK)
310     {
311         OicSecPdAcl_t *tmp1, *tmp2;
312         tmp1 = head;
313         while (tmp1)
314         {
315             tmp2 = tmp1->next;
316             delete tmp1;
317             tmp1 = tmp2;
318         }
319
320         delete pconf->prm;
321         delete pconf;
322     }
323     return ret;
324 }
325
326 OCStackResult JniOcSecureResource::provisionPairwiseDevices(JNIEnv* env, jint type, jint keySize,
327         jobject _acl1, jobject _device2, jobject _acl2, jobject jListener)
328 {
329     OCStackResult ret;
330     if(!jListener)
331     {
332         return OC_STACK_INVALID_CALLBACK;
333     }
334     JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener);
335     JniOcSecureResource *device2 = JniOcSecureResource::getJniOcSecureResourcePtr(env, _device2);
336     if (!device2)
337     {
338         return OC_STACK_ERROR;
339     }
340
341     Credential cred((OicSecCredType_t)type, keySize);
342
343     OicSecAcl_t *acl1 = nullptr;
344     OicSecAcl_t *acl2 = nullptr;
345
346     if (_acl1)
347     {
348         acl1 = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
349         if (!acl1)
350         {
351             return OC_STACK_NO_MEMORY;
352         }
353
354         if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, _acl1, acl1))
355         {
356             DeleteACLList(acl1);
357             return OC_STACK_ERROR;
358         }
359     }
360
361     if (_acl2)
362     {
363         acl2 = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t));
364         if (!acl2)
365         {
366             DeleteACLList(acl1);
367             return OC_STACK_NO_MEMORY;
368         }
369
370         if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, _acl2, acl2))
371         {
372             DeleteACLList(acl2);
373             return OC_STACK_ERROR;
374         }
375     }
376
377     ResultCallBack resultCallback = [acl1, acl2, resultListener](PMResultList_t *result,
378             int hasError)
379     {
380         DeleteACLList(acl1);
381         DeleteACLList(acl2);
382         resultListener->ProvisionResultCallback(result, hasError,
383                 ListenerFunc::PROVISIONPAIRWISEDEVICES);
384     };
385
386
387     ret = m_sharedSecureResource->provisionPairwiseDevices(cred, acl1,
388             *device2->getDevicePtr(), acl2, resultCallback);
389     if (ret != OC_STACK_OK)
390     {
391         DeleteACLList(acl1);
392         DeleteACLList(acl2);
393     }
394     return ret;
395 }
396
397 /*
398  * Class:     org_iotivity_base_OcSecureResource
399  * Method:    doOwnershipTransfer
400  * Signature: (Lorg/iotivity/base/OcSecureResource/doOwnershipTransferListener;)V
401  */
402 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_doOwnershipTransfer
403 (JNIEnv *env, jobject thiz, jobject jListener)
404 {
405     LOGD("OcSecureResource_doOwnershipTransfer");
406     if (!jListener)
407     {
408         ThrowOcException(OC_STACK_INVALID_CALLBACK, "provisionResultListener cannot be null");
409         return;
410     }
411
412     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
413     if (!secureResource)
414     {
415         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
416         return;
417     }
418
419     try
420     {
421         OCStackResult result = secureResource->doOwnershipTransfer(env, jListener);
422         if (OC_STACK_OK != result)
423         {
424             ThrowOcException(result, "OcSecureResource_doOwnershipTransfer");
425             return;
426         }
427     }
428     catch (OCException& e)
429     {
430         LOGE("%s", e.reason().c_str());
431         ThrowOcException(e.code(), e.reason().c_str());
432     }
433 }
434
435 /*
436  * Class:     org_iotivity_base_OcSecureResource
437  * Method:    removeDevice
438  * Signature: (ILorg/iotivity/base/OcSecureResource/removeDevice;)V
439  */
440 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_removeDevice
441 (JNIEnv *env, jobject thiz, jint timeout, jobject jListener)
442 {
443     LOGD("OcSecureResource_removeDevice");
444     if (timeout < 0)
445     {
446         ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
447         return;
448     }
449
450     if (!jListener)
451     {
452         ThrowOcException(OC_STACK_INVALID_CALLBACK, "provisionResultListener cannot be null");
453         return;
454     }
455
456     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
457     if (!secureResource)
458     {
459         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
460         return;
461     }
462
463     try
464     {
465         OCStackResult result = secureResource->removeDevice(env, timeout, jListener);
466         if (OC_STACK_OK != result)
467         {
468             ThrowOcException(result, "OcSecureResource_removeDevice");
469             return;
470         }
471     }
472     catch (OCException& e)
473     {
474         LOGE("%s", e.reason().c_str());
475         ThrowOcException(e.code(), e.reason().c_str());
476     }
477 }
478
479 /*
480  * Class:     org_iotivity_base_OcSecureResource
481  * Method:    unlinkDevices
482  * Signature: (Lorg/iotivity/base/OcSecureResource/unlinkDevices;)V
483  */
484 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_unlinkDevices
485 (JNIEnv *env, jobject thiz, jobject device2, jobject jListener)
486 {
487     LOGD("OcSecureResource_unlinkDevices");
488     if (!jListener)
489     {
490         ThrowOcException(OC_STACK_INVALID_CALLBACK, "provisionResultListener cannot be null");
491         return;
492     }
493     if (!device2)
494     {
495         ThrowOcException(OC_STACK_INVALID_PARAM, "device2 cannot be null");
496         return;
497     }
498
499     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
500     if (!secureResource)
501     {
502         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
503         return;
504     }
505
506     try
507     {
508         OCStackResult result = secureResource->unlinkDevices(env, device2, jListener);
509         if (OC_STACK_OK != result)
510         {
511             ThrowOcException(result, "OcSecureResource_unlinkDevices");
512             return;
513         }
514     }
515     catch (OCException& e)
516     {
517         LOGE("%s", e.reason().c_str());
518         ThrowOcException(e.code(), e.reason().c_str());
519     }
520 }
521
522 /*
523  * Class:     org_iotivity_base_OcSecureResource
524  * Method:    provisionCredentials1
525  * Signature: (Lorg/iotivity/base/OcSecureResource/provisionCredentials;)V
526  */
527 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionCredentials1
528 (JNIEnv *env, jobject thiz, jint type, jint keySize, jobject device2, jobject jListener)
529 {
530     LOGD("OcSecureResource_provisionCredentials");
531     if (!jListener)
532     {
533         ThrowOcException(OC_STACK_INVALID_CALLBACK, "provisionResultListener cannot be null");
534         return;
535     }
536     if (!device2)
537     {
538         ThrowOcException(OC_STACK_INVALID_PARAM, "device2 cannot be null");
539         return;
540     }
541
542     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
543     if (!secureResource)
544     {
545         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
546         return;
547     }
548
549     try
550     {
551         OCStackResult result = secureResource->provisionCredentials(env, type, keySize, device2,
552                 jListener);
553         if (OC_STACK_OK != result)
554         {
555             ThrowOcException(result, "OcSecureResource_provisionCredentials");
556             return;
557         }
558     }
559     catch (OCException& e)
560     {
561         LOGE("%s", e.reason().c_str());
562         ThrowOcException(e.code(), e.reason().c_str());
563     }
564 }
565
566 /*
567  * Class:     org_iotivity_base_OcSecureResource
568  * Method:    provisionACL
569  * Signature: (Lorg/iotivity/base/OcSecureResource/provisionACL;)V
570  */
571 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionACL
572 (JNIEnv *env, jobject thiz, jobject acl, jobject jListener)
573 {
574     LOGD("OcSecureResource_provisionACL");
575     if (!jListener)
576     {
577         ThrowOcException(OC_STACK_INVALID_CALLBACK, "provisionResultListener cannot be null");
578         return;
579     }
580     if (!acl)
581     {
582         ThrowOcException(OC_STACK_INVALID_PARAM, "acl cannot be null");
583         return;
584     }
585
586     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
587     if (!secureResource)
588     {
589         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
590         return;
591     }
592
593     try
594     {
595         OCStackResult result = secureResource->provisionACL(env, acl, jListener);
596         if (OC_STACK_OK != result)
597         {
598             ThrowOcException(result, "OcSecureResource_provisionACL");
599             return;
600         }
601     }
602     catch (OCException& e)
603     {
604         LOGE("%s", e.reason().c_str());
605         ThrowOcException(e.code(), e.reason().c_str());
606     }
607 }
608
609 /*
610  * Class:     org_iotivity_base_OcSecureResource
611  * Method:    provisionPairwiseDevices1
612  * Signature: (Lorg/iotivity/base/OcSecureResource/provisionPairwiseDevices;)V
613  */
614 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionPairwiseDevices1
615 (JNIEnv *env, jobject thiz, jint type, jint keySize, jobject acl1, jobject device2,
616         jobject acl2, jobject jListener)
617 {
618     LOGD("OcSecureResource_provisionPairwiseDevices");
619     if (!jListener)
620     {
621         ThrowOcException(OC_STACK_INVALID_CALLBACK, "Invalid Callback");
622         return;
623     }
624     if (!device2)
625     {
626         ThrowOcException(OC_STACK_INVALID_PARAM, "device2 cannot be null");
627         return;
628     }
629
630     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
631     if (!secureResource)
632     {
633         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
634         return;
635     }
636
637     try
638     {
639         OCStackResult result = secureResource->provisionPairwiseDevices(env, type, keySize,
640                 acl1, device2, acl2, jListener);
641         if (OC_STACK_OK != result)
642         {
643             ThrowOcException(result, "OcSecureResource_provisionPairwiseDevices");
644             return;
645         }
646     }
647     catch (OCException& e)
648     {
649         LOGE("%s", e.reason().c_str());
650         ThrowOcException(e.code(), e.reason().c_str());
651     }
652 }
653
654 /*
655  * Class:     org_iotivity_base_OcSecureResource
656  * Method:    provisionDirectPairing
657  * Signature: (Ljava/lang/String;[Lorg/iotivity/base/OicSecPdAcl;ILorg/iotivity/base/OcSecureResource/ProvisionDirectPairingListener;I)V
658  */
659 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionDirectPairing
660 (JNIEnv *env, jobject thiz, jstring jpin, jobjectArray pdacls, jintArray jprmType,
661     jint jedp, jobject jListener)
662 {
663     LOGD("OcSecureResource_provisionDirectPairing");
664     if (!jListener)
665     {
666         ThrowOcException(OC_STACK_INVALID_CALLBACK, "Invalid Callback");
667         return;
668     }
669     if (!pdacls || !jpin || ! jprmType)
670     {
671         ThrowOcException(OC_STACK_INVALID_PARAM, "Invalid Parameters");
672         return;
673     }
674     std::string pin = env->GetStringUTFChars(jpin, nullptr);
675
676     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
677     if (!secureResource)
678     {
679         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
680         return;
681     }
682
683     const jsize len = env->GetArrayLength(jprmType);
684     jint* ints = env->GetIntArrayElements(jprmType, nullptr);
685     std::vector<int> value;
686     for (jsize i = 0; i < len; ++i)
687     {
688         value.push_back(static_cast<int>(ints[i]));
689     }
690     env->ReleaseIntArrayElements(jprmType, ints, JNI_ABORT);
691
692     try
693     {
694         OCStackResult result = secureResource->provisionDirectPairing(env, pdacls, jListener,
695                 pin, value, static_cast<int>(jedp));
696         if (OC_STACK_OK != result)
697         {
698             ThrowOcException(result, "OcSecureResource_provisionDirectPairing");
699             return;
700         }
701     }
702     catch (OCException& e)
703     {
704         LOGE("%s", e.reason().c_str());
705         ThrowOcException(e.code(), e.reason().c_str());
706     }
707 }
708
709 /*
710  * Class:     org_iotivity_base_OcSecureResource
711  * Method:    getLinkedDevices
712  * Signature: ()Ljava/util/List;
713  */
714 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcSecureResource_getLinkedDevices
715 (JNIEnv *env, jobject thiz)
716 {
717     LOGD("OcSecureResource_getLinkedDevices");
718     UuidList_t uuidList;
719
720     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
721     if (!secureResource)
722     {
723         ThrowOcException(OC_STACK_ERROR, "getJniOcSecureResourcePtr failed");
724         return nullptr;
725     }
726
727     try
728     {
729         OCStackResult result = secureResource->getLinkedDevices(env, uuidList);
730         if (OC_STACK_OK != result)
731         {
732             ThrowOcException(result, "OcSecureResource_getLinkedDevices");
733             return nullptr;
734         }
735         return JniSecureUtils::convertUUIDVectorToJavaStrList(env, uuidList);
736     }
737     catch (OCException& e)
738     {
739         LOGE("%s", e.reason().c_str());
740         ThrowOcException(e.code(), e.reason().c_str());
741         return nullptr;
742     }
743 }
744
745 /*
746  * Class:     org_iotivity_base_OcSecureResource
747  * Method:    getIpAddr
748  * Signature: ()Ljava/lang/String;
749  */
750 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcSecureResource_getIpAddr
751   (JNIEnv *env, jobject thiz)
752 {
753     LOGD("OcSecureResource_getIpAddr");
754     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
755     if (!secureResource)
756     {
757         LOGD("getJniOcSecureResourcePtr failed");
758         return nullptr;
759     }
760
761     return env->NewStringUTF(secureResource->getIpAddr().c_str());
762 }
763
764 /*
765  * Class:     org_iotivity_base_OcSecureResource
766  * Method:    getDeviceID
767  * Signature: ()Ljava/lang/String;
768  */
769 JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcSecureResource_getDeviceID
770   (JNIEnv *env , jobject thiz)
771 {
772     LOGD("OcSecureResource_getDeviceID");
773     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
774     if (!secureResource)
775     {
776         LOGD("getJniOcSecureResourcePtr failed");
777         return nullptr;
778     }
779
780     return env->NewStringUTF(secureResource->getDeviceID().c_str());
781 }
782
783 /*
784  * Class:     org_iotivity_base_OcSecureResource
785  * Method:    deviceStatus
786  * Signature: ()I
787  */
788 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcSecureResource_deviceStatus
789   (JNIEnv *env, jobject thiz)
790 {
791     LOGD("OcSecureResource_deviceStatus");
792     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
793     if (!secureResource)
794     {
795         LOGD("getJniOcSecureResourcePtr failed");
796         return -1;
797     }
798
799     return secureResource->getDeviceStatus();
800 }
801
802 /*
803  * Class:     org_iotivity_base_OcSecureResource
804  * Method:    ownedStatus
805  * Signature: ()I
806  */
807 JNIEXPORT jint JNICALL Java_org_iotivity_base_OcSecureResource_ownedStatus
808   (JNIEnv *env, jobject thiz)
809 {
810     LOGD("OcSecureResource_ownedStatus");
811     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
812     if (!secureResource)
813     {
814         LOGD("getJniOcSecureResourcePtr failed");
815         return -1;
816     }
817
818     return secureResource->getOwnedStatus();
819 }
820
821 /*
822  * Class:     org_iotivity_base_OcSecureResource
823  * Method:    dispose
824  * Signature: ()V
825  */
826 JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_dispose
827   (JNIEnv *env, jobject thiz)
828 {
829     LOGD("OcSecureResource_dispose");
830     JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz);
831     delete secureResource;
832 }