2 * //******************************************************************
4 * // Copyright 2016 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 "JniOcCloudProvisioning.h"
24 #include "oic_malloc.h"
26 namespace PH = std::placeholders;
28 JniOcCloudProvisioning::JniOcCloudProvisioning(std::shared_ptr<OCCloudProvisioning> p)
29 : m_sharedCloudObject(p)
32 JniOcCloudProvisioning::~JniOcCloudProvisioning()
34 LOGD("~JniOcCloudProvisioning");
35 m_sharedCloudObject = nullptr;
38 JniOcCloudResultListener* JniOcCloudProvisioning::AddCloudResultListener(JNIEnv* env,
41 JniOcCloudResultListener *resultListener = NULL;
44 for (auto it = resultMap.begin(); it != resultMap.end(); ++it)
46 if (env->IsSameObject(jListener, it->first))
48 auto refPair = it->second;
49 resultListener = refPair.first;
52 resultMap.insert(*it);
53 LOGD("Cloud Provision resultListener: ref. count incremented");
59 resultListener = new JniOcCloudResultListener(env, jListener,
60 RemoveCallback(std::bind(&JniOcCloudProvisioning::RemoveCloudResultListener,
61 this, PH::_1, PH::_2)));
62 jobject jgListener = env->NewGlobalRef(jListener);
64 resultMap.insert(std::pair < jobject, std::pair < JniOcCloudResultListener*,
65 int >> (jgListener, std::pair<JniOcCloudResultListener*, int>(resultListener, 1)));
66 LOGD("Cloud Provision resultListener: new listener");
68 resultMapLock.unlock();
69 return resultListener;
72 void JniOcCloudProvisioning::RemoveCloudResultListener(JNIEnv* env, jobject jListener)
76 for (auto it = resultMap.begin(); it != resultMap.end(); ++it)
78 if (env->IsSameObject(jListener, it->first))
80 auto refPair = it->second;
81 if (refPair.second > 1)
85 resultMap.insert(*it);
86 LOGI("Cloud Provision resultListener: ref. count decremented");
90 env->DeleteGlobalRef(it->first);
91 JniOcCloudResultListener* listener = refPair.first;
94 LOGI("Cloud Provision resultListener removed");
99 resultMapLock.unlock();
102 JniGetAclIdByDeviceListener* JniOcCloudProvisioning::AddGetAclByDeviceListener(JNIEnv* env,
105 JniGetAclIdByDeviceListener *resultListener = NULL;
106 aclresultMapLock.lock();
108 for (auto it = aclresultMap.begin(); it != aclresultMap.end(); ++it)
110 if (env->IsSameObject(jListener, it->first))
112 auto refPair = it->second;
113 resultListener = refPair.first;
115 it->second = refPair;
116 aclresultMap.insert(*it);
117 LOGD("GetACLByDeviceID Listener: ref. count incremented");
123 resultListener = new JniGetAclIdByDeviceListener(env, jListener,
124 RemoveCallback(std::bind(&JniOcCloudProvisioning::RemoveGetAclByDeviceIdListener,
125 this, PH::_1, PH::_2)));
126 jobject jgListener = env->NewGlobalRef(jListener);
128 aclresultMap.insert(std::pair < jobject, std::pair < JniGetAclIdByDeviceListener*,
129 int >> (jgListener, std::pair<JniGetAclIdByDeviceListener*, int>(resultListener, 1)));
130 LOGD("GetACLByDeviceID Listener: new listener");
132 aclresultMapLock.unlock();
133 return resultListener;
136 void JniOcCloudProvisioning::RemoveGetAclByDeviceIdListener(JNIEnv* env, jobject jListener)
138 aclresultMapLock.lock();
140 for (auto it = aclresultMap.begin(); it != aclresultMap.end(); ++it)
142 if (env->IsSameObject(jListener, it->first))
144 auto refPair = it->second;
145 if (refPair.second > 1)
148 it->second = refPair;
149 aclresultMap.insert(*it);
150 LOGI("GetACLByDeviceID Listener: ref. count decremented");
154 env->DeleteGlobalRef(it->first);
155 JniGetAclIdByDeviceListener* listener = refPair.first;
157 aclresultMap.erase(it);
158 LOGI("GetACLByDeviceID Listener removed");
163 aclresultMapLock.unlock();
165 JniOcCloudProvisioning * Create_native_object(JNIEnv *env, jobject thiz)
167 jstring jip = (jstring)env->CallObjectMethod(thiz, g_mid_OcCloudProvisioning_getIP);
168 if (!jip || env->ExceptionCheck())
172 const char *str = env->GetStringUTFChars(jip, NULL);
173 std::string ipaddress(str);
174 env->ReleaseStringUTFChars(jip, str);
176 jint port = env->CallIntMethod(thiz, g_mid_OcCloudProvisioning_getPort);
177 if (env->ExceptionCheck())
181 JniOcCloudProvisioning *cloud = new JniOcCloudProvisioning(
182 std::shared_ptr<OCCloudProvisioning>(new OCCloudProvisioning(ipaddress, (uint16_t)port)));
183 SetHandle<JniOcCloudProvisioning>(env, thiz, cloud);
187 JniOcCloudProvisioning* JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(JNIEnv *env, jobject thiz)
189 JniOcCloudProvisioning *cloud = GetHandle<JniOcCloudProvisioning>(env, thiz);
191 if (env->ExceptionCheck())
193 LOGE("Failed to get native handle from OcCloudProvisioning class");
199 OCStackResult JniOcCloudProvisioning::requestCertificate(JNIEnv* env, jobject jListener)
201 JniOcCloudResultListener *resultListener = AddCloudResultListener(env, jListener);
203 ResponseCallBack responseCallBack = [resultListener](OCStackResult result, void *data)
205 resultListener->CloudResultListenerCB(result, data, ListenerFunc::REQUEST_CERTIFICATE);
208 return m_sharedCloudObject->requestCertificate(responseCallBack);
211 OCStackResult JniOcCloudProvisioning::getIndividualAclInfo(JNIEnv* env, jobject jListener, std::string &aclID)
213 JniOcCloudResultListener *resultListener = AddCloudResultListener(env, jListener);
215 ResponseCallBack responseCallBack = [resultListener](OCStackResult result, void *data)
217 resultListener->CloudResultListenerCB(result, data, ListenerFunc::GET_ACLINFO);
220 return m_sharedCloudObject->getIndividualAclInfo(aclID, responseCallBack);
223 OCStackResult JniOcCloudProvisioning::getCRL(JNIEnv* env, jobject jListener)
225 JniOcCloudResultListener *resultListener = AddCloudResultListener(env, jListener);
227 ResponseCallBack responseCallBack = [resultListener](OCStackResult result, void *data)
229 resultListener->CloudResultListenerCB(result, data, ListenerFunc::GET_CRL);
232 return m_sharedCloudObject->getCRL(responseCallBack);
235 OCStackResult JniOcCloudProvisioning::postCRL(JNIEnv* env, const std::string& thisUpdate,
236 const std::string& nextUpdate, const OCByteString *crl,
237 const stringArray_t *serialNumbers, jobject jListener)
239 JniOcCloudResultListener *resultListener = AddCloudResultListener(env, jListener);
241 ResponseCallBack responseCallBack = [resultListener](OCStackResult result, void *data)
243 resultListener->CloudResultListenerCB(result, data, ListenerFunc::POST_CRL);
246 return m_sharedCloudObject->postCRL(thisUpdate, nextUpdate, crl, serialNumbers,
250 OCStackResult JniOcCloudProvisioning::getAclIdByDevice(JNIEnv* env, std::string deviceId,
253 JniGetAclIdByDeviceListener *resultListener = AddGetAclByDeviceListener(env, jListener);
255 AclIdResponseCallBack aclIdResponseCallBack = [resultListener](OCStackResult result,
258 resultListener->GetAclIdByDeviceListenerCB(result, aclId);
261 return m_sharedCloudObject->getAclIdByDevice(deviceId, aclIdResponseCallBack);
264 * Class: org_iotivity_base_OcCloudProvisioning
265 * Method: requestCertificate
266 * Signature: (Lorg/iotivity/base/OcCloudProvisioning/RequestCertificateListener;)V
268 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_requestCertificate
269 (JNIEnv* env, jobject thiz, jobject jListener)
271 LOGD("OcCloudProvisioning_requestCertificate");
274 ThrowOcException(OC_STACK_INVALID_PARAM, "Listener cannot be null");
278 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
281 LOGD("OcCloudProvisioning_requestCertificate, No native object, creating now");
282 cloud = Create_native_object(env, thiz);
285 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_requestCertificate,"
286 "Can not Create Native object");
293 OCStackResult result = cloud->requestCertificate(env, jListener);
294 if (OC_STACK_OK != result)
296 ThrowOcException(result, "OcCloudProvisioning_requestCertificate");
300 catch (OCException& e)
302 LOGE("%s", e.reason().c_str());
303 ThrowOcException(e.code(), e.reason().c_str());
309 * Class: org_iotivity_base_OcCloudProvisioning
310 * Method: getAclIdByDevice
311 * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcCloudProvisioning/GetAclIdByDeviceListener;)V
313 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_getAclIdByDevice
314 (JNIEnv *env, jobject thiz, jstring jdeviceId, jobject jListener)
316 LOGD("OcCloudProvisioning_getAclIdByDevice");
317 if (!jListener || !jdeviceId)
319 ThrowOcException(OC_STACK_INVALID_PARAM, "Listener and deviceID cannot be null");
323 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
326 LOGD("OcCloudProvisioning_getAclIdByDevice, No native object, creating now");
327 cloud = Create_native_object(env, thiz);
330 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_getAclIdByDevice,"
331 "Can not Create Native object");
336 const char *str = env->GetStringUTFChars(jdeviceId, NULL);
337 if (!str || env->ExceptionCheck())
339 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_getAclIdByDevice");
342 std::string deviceId(str);
343 env->ReleaseStringUTFChars(jdeviceId, str);
347 OCStackResult result = cloud->getAclIdByDevice(env, deviceId, jListener);
348 if (OC_STACK_OK != result)
350 ThrowOcException(result, "OcCloudProvisioning_getAclIdByDevice");
354 catch (OCException& e)
356 LOGE("%s", e.reason().c_str());
357 ThrowOcException(e.code(), e.reason().c_str());
363 * Class: org_iotivity_base_OcCloudProvisioning
364 * Method: getIndividualAclInfo
365 * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcCloudProvisioning/GetIndividualAclInfoListener;)V
367 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_getIndividualAclInfo
368 (JNIEnv *env, jobject thiz, jstring jaclID, jobject jListener)
370 LOGD("OcCloudProvisioning_getIndividualAclInfo");
371 if (!jListener || !jaclID)
373 ThrowOcException(OC_STACK_INVALID_PARAM, "Listener/aclID cannot be null");
377 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
380 LOGD("OcCloudProvisioning_getIndividualAclInfo, No native object, creating now");
381 cloud = Create_native_object(env, thiz);
384 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_getIndividualAclInfo,"
385 "Can not Create Native object");
390 const char *str = env->GetStringUTFChars(jaclID, NULL);
391 std::string aclID(str);
392 env->ReleaseStringUTFChars(jaclID, str);
396 OCStackResult result = cloud->getIndividualAclInfo(env, jListener, aclID);
397 if (OC_STACK_OK != result)
399 ThrowOcException(result, "OcCloudProvisioning_getIndividualAclInf");
403 catch (OCException& e)
405 LOGE("%s", e.reason().c_str());
406 ThrowOcException(e.code(), e.reason().c_str());
412 * Class: org_iotivity_base_OcCloudProvisioning
414 * Signature: (Lorg/iotivity/base/OcCloudProvisioning/GetCRLListener;)V
416 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_getCRL
417 (JNIEnv *env, jobject thiz, jobject jListener)
419 LOGD("OcCloudProvisioning_getCRL");
422 ThrowOcException(OC_STACK_INVALID_PARAM, "Listener cannot be null");
426 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
429 LOGD("OcCloudProvisioning_getCRL, No native object, creating now");
430 cloud = Create_native_object(env, thiz);
433 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_getCRL,"
434 "Can not Create Native object");
441 OCStackResult result = cloud->getCRL(env, jListener);
442 if (OC_STACK_OK != result)
444 ThrowOcException(result, "OcCloudProvisioning_requestCertificate");
448 catch (OCException& e)
450 LOGE("%s", e.reason().c_str());
451 ThrowOcException(e.code(), e.reason().c_str());
457 * Class: org_iotivity_base_OcCloudProvisioning
459 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/ArrayList;Lorg/iotivity/base/OcCloudProvisioning/PostCRLListener;)V
461 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_postCRL0
462 (JNIEnv *env , jobject thiz, jstring jthisUpdate, jstring jnextUpdate, jstring jcrl,
463 jobjectArray jserialnumArray, jobject jListener)
465 LOGD("OcCloudProvisioning_postCRL0");
466 if (!jListener || !jthisUpdate || !jnextUpdate || !jserialnumArray)
468 ThrowOcException(OC_STACK_INVALID_PARAM, " Invalid parameter (NULL param)");
472 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
475 LOGD("OcCloudProvisioning_getCRL, No native object, creating now");
476 cloud = Create_native_object(env, thiz);
479 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_PostCRL0,"
480 "Can not Create Native object");
485 const char *str = env->GetStringUTFChars(jthisUpdate, NULL);
486 if (!str || env->ExceptionCheck())
488 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
491 std::string thisUpdate(str);
492 env->ReleaseStringUTFChars(jthisUpdate, str);
494 str = env->GetStringUTFChars(jnextUpdate, NULL);
495 if (!str || env->ExceptionCheck())
497 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
500 std::string nextUpdate(str);
501 env->ReleaseStringUTFChars(jnextUpdate, str);
503 OCByteString *crl = NULL;
506 str = env->GetStringUTFChars(jcrl, NULL);
507 if (!str || env->ExceptionCheck())
509 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
512 crl = (OCByteString*)OICCalloc(1, sizeof(OCByteString));
513 crl->len = (size_t)(strlen(str));
514 crl->bytes = (uint8_t*)OICCalloc(crl->len, sizeof(uint8_t));
516 for (size_t i = 0 ; i < crl->len; i++)
518 crl->bytes[i] = (uint8_t)str[i];
522 jsize len = env->GetArrayLength(jserialnumArray);
524 stringArray_t *serialNumArray = (stringArray_t*)OICCalloc(1, sizeof(stringArray_t));
525 serialNumArray->array = (char**)OICCalloc(len, sizeof(char*));
526 serialNumArray->length = len;
528 for (jsize i = 0; i < len; ++i)
530 jstring jStr = (jstring)env->GetObjectArrayElement(jserialnumArray, i);
533 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
536 serialNumArray->array[i] = (char*)env->GetStringUTFChars(jStr, nullptr);
537 if (env->ExceptionCheck())
539 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
542 env->DeleteLocalRef(jStr);
547 OCStackResult result = cloud->postCRL(env, thisUpdate, nextUpdate, crl,
548 serialNumArray, jListener);
549 if (OC_STACK_OK != result)
551 ThrowOcException(result, "OcCloudProvisioning_PostCRL0");
555 catch (OCException& e)
557 LOGE("%s", e.reason().c_str());
558 ThrowOcException(e.code(), e.reason().c_str());