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_CALLBACK, "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");
319 ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listener cannot be null");
325 ThrowOcException(OC_STACK_INVALID_PARAM, "deviceID can not be null");
328 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
331 LOGD("OcCloudProvisioning_getAclIdByDevice, No native object, creating now");
332 cloud = Create_native_object(env, thiz);
335 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_getAclIdByDevice,"
336 "Can not Create Native object");
341 const char *str = env->GetStringUTFChars(jdeviceId, NULL);
342 if (!str || env->ExceptionCheck())
344 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_getAclIdByDevice");
347 std::string deviceId(str);
348 env->ReleaseStringUTFChars(jdeviceId, str);
352 OCStackResult result = cloud->getAclIdByDevice(env, deviceId, jListener);
353 if (OC_STACK_OK != result)
355 ThrowOcException(result, "OcCloudProvisioning_getAclIdByDevice");
359 catch (OCException& e)
361 LOGE("%s", e.reason().c_str());
362 ThrowOcException(e.code(), e.reason().c_str());
368 * Class: org_iotivity_base_OcCloudProvisioning
369 * Method: getIndividualAclInfo
370 * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcCloudProvisioning/GetIndividualAclInfoListener;)V
372 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_getIndividualAclInfo
373 (JNIEnv *env, jobject thiz, jstring jaclID, jobject jListener)
375 LOGD("OcCloudProvisioning_getIndividualAclInfo");
378 ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listener cannot be null");
383 ThrowOcException(OC_STACK_INVALID_PARAM, "aclID cannot be null");
387 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
390 LOGD("OcCloudProvisioning_getIndividualAclInfo, No native object, creating now");
391 cloud = Create_native_object(env, thiz);
394 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_getIndividualAclInfo,"
395 "Can not Create Native object");
400 const char *str = env->GetStringUTFChars(jaclID, NULL);
401 std::string aclID(str);
402 env->ReleaseStringUTFChars(jaclID, str);
406 OCStackResult result = cloud->getIndividualAclInfo(env, jListener, aclID);
407 if (OC_STACK_OK != result)
409 ThrowOcException(result, "OcCloudProvisioning_getIndividualAclInf");
413 catch (OCException& e)
415 LOGE("%s", e.reason().c_str());
416 ThrowOcException(e.code(), e.reason().c_str());
422 * Class: org_iotivity_base_OcCloudProvisioning
424 * Signature: (Lorg/iotivity/base/OcCloudProvisioning/GetCRLListener;)V
426 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_getCRL
427 (JNIEnv *env, jobject thiz, jobject jListener)
429 LOGD("OcCloudProvisioning_getCRL");
432 ThrowOcException(OC_STACK_INVALID_CALLBACK, "Listener cannot be null");
436 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
439 LOGD("OcCloudProvisioning_getCRL, No native object, creating now");
440 cloud = Create_native_object(env, thiz);
443 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_getCRL,"
444 "Can not Create Native object");
451 OCStackResult result = cloud->getCRL(env, jListener);
452 if (OC_STACK_OK != result)
454 ThrowOcException(result, "OcCloudProvisioning_requestCertificate");
458 catch (OCException& e)
460 LOGE("%s", e.reason().c_str());
461 ThrowOcException(e.code(), e.reason().c_str());
467 * Class: org_iotivity_base_OcCloudProvisioning
469 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/util/ArrayList;Lorg/iotivity/base/OcCloudProvisioning/PostCRLListener;)V
471 JNIEXPORT void JNICALL Java_org_iotivity_base_OcCloudProvisioning_postCRL0
472 (JNIEnv *env , jobject thiz, jstring jthisUpdate, jstring jnextUpdate, jstring jcrl,
473 jobjectArray jserialnumArray, jobject jListener)
475 LOGD("OcCloudProvisioning_postCRL0");
478 ThrowOcException(OC_STACK_INVALID_CALLBACK,"Listener can not be null");
481 if (!jthisUpdate || !jnextUpdate || !jserialnumArray)
483 ThrowOcException(OC_STACK_INVALID_PARAM, " Invalid parameter (NULL param)");
487 JniOcCloudProvisioning *cloud = JniOcCloudProvisioning::getJniOcCloudProvisioningPtr(env, thiz);
490 LOGD("OcCloudProvisioning_getCRL, No native object, creating now");
491 cloud = Create_native_object(env, thiz);
494 ThrowOcException(OC_STACK_ERROR, "OcCloudProvisioning_PostCRL0,"
495 "Can not Create Native object");
500 const char *str = env->GetStringUTFChars(jthisUpdate, NULL);
501 if (!str || env->ExceptionCheck())
503 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
506 std::string thisUpdate(str);
507 env->ReleaseStringUTFChars(jthisUpdate, str);
509 str = env->GetStringUTFChars(jnextUpdate, NULL);
510 if (!str || env->ExceptionCheck())
512 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
515 std::string nextUpdate(str);
516 env->ReleaseStringUTFChars(jnextUpdate, str);
518 OCByteString *crl = NULL;
521 str = env->GetStringUTFChars(jcrl, NULL);
522 if (!str || env->ExceptionCheck())
524 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
527 crl = (OCByteString*)OICCalloc(1, sizeof(OCByteString));
528 crl->len = (size_t)(strlen(str));
529 crl->bytes = (uint8_t*)OICCalloc(crl->len, sizeof(uint8_t));
531 for (size_t i = 0 ; i < crl->len; i++)
533 crl->bytes[i] = (uint8_t)str[i];
537 jsize len = env->GetArrayLength(jserialnumArray);
539 stringArray_t *serialNumArray = (stringArray_t*)OICCalloc(1, sizeof(stringArray_t));
540 serialNumArray->array = (char**)OICCalloc(len, sizeof(char*));
541 serialNumArray->length = len;
543 for (jsize i = 0; i < len; ++i)
545 jstring jStr = (jstring)env->GetObjectArrayElement(jserialnumArray, i);
548 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
551 serialNumArray->array[i] = (char*)env->GetStringUTFChars(jStr, nullptr);
552 if (env->ExceptionCheck())
554 ThrowOcException(OC_STACK_ERROR,"OcCloudProvisioning_PostCRL0");
557 env->DeleteLocalRef(jStr);
562 OCStackResult result = cloud->postCRL(env, thisUpdate, nextUpdate, crl,
563 serialNumArray, jListener);
564 if (OC_STACK_OK != result)
566 ThrowOcException(result, "OcCloudProvisioning_PostCRL0");
570 catch (OCException& e)
572 LOGE("%s", e.reason().c_str());
573 ThrowOcException(e.code(), e.reason().c_str());