1 //******************************************************************
3 // Copyright 2015 Intel Corporation.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "JniOcPlatform.h"
22 #include "OCPlatform.h"
23 #include "JniOcResource.h"
24 #include "JniOcResourceHandle.h"
25 #include "JniOcPresenceHandle.h"
26 #include "JniOcResourceResponse.h"
27 #include "JniOcSecurity.h"
28 #include "JniOcDirectPairDevice.h"
30 #include "ocpayload.h"
34 JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener)
36 JniOnResourceFoundListener *onResourceFoundListener = nullptr;
38 resourceFoundMapLock.lock();
40 for (auto it = onResourceFoundListenerMap.begin();
41 it != onResourceFoundListenerMap.end();
44 if (env->IsSameObject(jListener, it->first))
46 auto refPair = it->second;
47 onResourceFoundListener = refPair.first;
50 onResourceFoundListenerMap.insert(*it);
51 LOGD("OnResourceFoundListener: ref. count incremented");
56 if (!onResourceFoundListener)
58 onResourceFoundListener =
59 new JniOnResourceFoundListener(env, jListener, RemoveOnResourceFoundListener);
60 jobject jgListener = env->NewGlobalRef(jListener);
62 onResourceFoundListenerMap.insert(
64 std::pair<JniOnResourceFoundListener*, int >>(
66 std::pair<JniOnResourceFoundListener*, int>(onResourceFoundListener,
68 LOGD("OnResourceFoundListener: new listener");
70 resourceFoundMapLock.unlock();
71 return onResourceFoundListener;
74 void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener)
76 resourceFoundMapLock.lock();
78 for (auto it = onResourceFoundListenerMap.begin();
79 it != onResourceFoundListenerMap.end();
82 if (env->IsSameObject(jListener, it->first))
84 auto refPair = it->second;
85 if (refPair.second > 1)
89 onResourceFoundListenerMap.insert(*it);
90 LOGI("OnResourceFoundListener: ref. count decremented");
94 env->DeleteGlobalRef(it->first);
95 JniOnResourceFoundListener* listener = refPair.first;
97 onResourceFoundListenerMap.erase(it);
98 LOGI("OnResourceFoundListener removed");
103 resourceFoundMapLock.unlock();
106 JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener)
108 JniOnDeviceInfoListener *onDeviceInfoListener = nullptr;
110 deviceInfoMapLock.lock();
112 for (auto it = onDeviceInfoListenerMap.begin(); it != onDeviceInfoListenerMap.end(); ++it)
114 if (env->IsSameObject(jListener, it->first))
116 auto refPair = it->second;
117 onDeviceInfoListener = refPair.first;
119 it->second = refPair;
120 onDeviceInfoListenerMap.insert(*it);
121 LOGD("OnDeviceInfoListener: ref. count incremented");
126 if (!onDeviceInfoListener)
128 onDeviceInfoListener =
129 new JniOnDeviceInfoListener(env, jListener, RemoveOnDeviceInfoListener);
130 jobject jgListener = env->NewGlobalRef(jListener);
132 onDeviceInfoListenerMap.insert(
134 std::pair<JniOnDeviceInfoListener*, int>>(
136 std::pair<JniOnDeviceInfoListener*, int>(onDeviceInfoListener, 1)));
137 LOGI("OnDeviceInfoListener: new listener");
140 deviceInfoMapLock.unlock();
141 return onDeviceInfoListener;
144 void RemoveOnDeviceInfoListener(JNIEnv* env, jobject jListener)
146 deviceInfoMapLock.lock();
147 bool isFound = false;
148 for (auto it = onDeviceInfoListenerMap.begin(); it != onDeviceInfoListenerMap.end(); ++it)
150 if (env->IsSameObject(jListener, it->first))
152 auto refPair = it->second;
153 if (refPair.second > 1)
156 it->second = refPair;
157 onDeviceInfoListenerMap.insert(*it);
158 LOGI("OnDeviceInfoListener: ref. count decremented");
162 env->DeleteGlobalRef(it->first);
163 JniOnDeviceInfoListener* listener = refPair.first;
165 onDeviceInfoListenerMap.erase(it);
167 LOGI("OnDeviceInfoListener removed");
177 ThrowOcException(JNI_EXCEPTION, "OnDeviceInfoListenet not found");
179 deviceInfoMapLock.unlock();
182 JniOnPlatformInfoListener* AddOnPlatformInfoListener(JNIEnv* env, jobject jListener)
184 JniOnPlatformInfoListener *onPlatformInfoListener = nullptr;
186 platformInfoMapLock.lock();
188 for (auto it = onPlatformInfoListenerMap.begin(); it != onPlatformInfoListenerMap.end(); ++it)
190 if (env->IsSameObject(jListener, it->first))
192 auto refPair = it->second;
193 onPlatformInfoListener = refPair.first;
195 it->second = refPair;
196 onPlatformInfoListenerMap.insert(*it);
197 LOGD("OnPlatformInfoListener: ref. count incremented");
202 if (!onPlatformInfoListener)
204 onPlatformInfoListener =
205 new JniOnPlatformInfoListener(env, jListener, RemoveOnPlatformInfoListener);
206 jobject jgListener = env->NewGlobalRef(jListener);
208 onPlatformInfoListenerMap.insert(
209 std::pair<jobject, std::pair<JniOnPlatformInfoListener*, int>>(
211 std::pair<JniOnPlatformInfoListener*, int>(onPlatformInfoListener, 1)));
212 LOGI("OnPlatformInfoListener: new listener");
215 platformInfoMapLock.unlock();
216 return onPlatformInfoListener;
219 void RemoveOnPlatformInfoListener(JNIEnv* env, jobject jListener)
221 platformInfoMapLock.lock();
222 bool isFound = false;
223 for (auto it = onPlatformInfoListenerMap.begin(); it != onPlatformInfoListenerMap.end(); ++it)
225 if (env->IsSameObject(jListener, it->first))
227 auto refPair = it->second;
228 if (refPair.second > 1)
231 it->second = refPair;
232 onPlatformInfoListenerMap.insert(*it);
233 LOGI("OnPlatformInfoListener: ref. count decremented");
237 env->DeleteGlobalRef(it->first);
238 JniOnPlatformInfoListener* listener = refPair.first;
240 onPlatformInfoListenerMap.erase(it);
242 LOGI("OnPlatformInfoListener removed");
252 ThrowOcException(JNI_EXCEPTION, "OnPlatformInfoListenet not found");
254 platformInfoMapLock.unlock();
257 JniOnPresenceListener* AddOnPresenceListener(JNIEnv* env, jobject jListener)
259 JniOnPresenceListener *onPresenceListener = nullptr;
261 presenceMapLock.lock();
263 for (auto it = onPresenceListenerMap.begin(); it != onPresenceListenerMap.end(); ++it)
265 if (env->IsSameObject(jListener, it->first))
267 auto refPair = it->second;
268 onPresenceListener = refPair.first;
270 it->second = refPair;
271 onPresenceListenerMap.insert(*it);
272 LOGD("OnPresenceListener: ref. count incremented");
276 if (!onPresenceListener)
278 onPresenceListener = new JniOnPresenceListener(env, jListener, RemoveOnPresenceListener);
279 jobject jgListener = env->NewGlobalRef(jListener);
280 onPresenceListenerMap.insert(
281 std::pair<jobject, std::pair<JniOnPresenceListener*, int>>(
283 std::pair<JniOnPresenceListener*, int>(onPresenceListener, 1)));
284 LOGI("OnPresenceListener: new listener");
286 presenceMapLock.unlock();
287 return onPresenceListener;
290 void RemoveOnPresenceListener(JNIEnv* env, jobject jListener)
292 presenceMapLock.lock();
293 bool isFound = false;
294 for (auto it = onPresenceListenerMap.begin(); it != onPresenceListenerMap.end(); ++it)
296 if (env->IsSameObject(jListener, it->first))
298 auto refPair = it->second;
299 if (refPair.second > 1)
302 it->second = refPair;
303 onPresenceListenerMap.insert(*it);
304 LOGI("OnPresenceListener: ref. count decremented");
308 env->DeleteGlobalRef(it->first);
309 JniOnPresenceListener* listener = refPair.first;
311 onPresenceListenerMap.erase(it);
312 LOGI("OnPresenceListener is removed");
320 ThrowOcException(JNI_EXCEPTION, "OnPresenceListener not found");
322 presenceMapLock.unlock();
325 JniOnDPDevicesFoundListener* AddOnDPDevicesFoundListener(JNIEnv* env, jobject jListener)
327 JniOnDPDevicesFoundListener *onDPDeviceListener = nullptr;
329 dpDevicesFoundListenerMapLock.lock();
331 for (auto it = onDPDevicesFoundListenerMap.begin(); it !=
332 onDPDevicesFoundListenerMap.end(); ++it)
334 if (env->IsSameObject(jListener, it->first))
336 auto refPair = it->second;
337 onDPDeviceListener = refPair.first;
339 it->second = refPair;
340 onDPDevicesFoundListenerMap.insert(*it);
341 LOGD("onDPDeviceListener: ref. count incremented");
345 if (!onDPDeviceListener)
347 onDPDeviceListener = new JniOnDPDevicesFoundListener(env, jListener,
348 RemoveOnDPDevicesFoundListener);
349 jobject jgListener = env->NewGlobalRef(jListener);
350 onDPDevicesFoundListenerMap.insert(
351 std::pair<jobject, std::pair<JniOnDPDevicesFoundListener*, int>>(
353 std::pair<JniOnDPDevicesFoundListener*, int>(onDPDeviceListener, 1)));
354 LOGI("onDPDeviceListener: new listener");
356 dpDevicesFoundListenerMapLock.unlock();
357 return onDPDeviceListener;
360 void RemoveOnDPDevicesFoundListener(JNIEnv* env, jobject jListener)
362 dpDevicesFoundListenerMapLock.lock();
363 bool isFound = false;
364 for (auto it = onDPDevicesFoundListenerMap.begin(); it !=
365 onDPDevicesFoundListenerMap.end(); ++it)
367 if (env->IsSameObject(jListener, it->first))
369 auto refPair = it->second;
370 if (refPair.second > 1)
373 it->second = refPair;
374 onDPDevicesFoundListenerMap.insert(*it);
375 LOGI("onDPDeviceListener: ref. count decremented");
379 env->DeleteGlobalRef(it->first);
380 JniOnDPDevicesFoundListener* listener = refPair.first;
382 onDPDevicesFoundListenerMap.erase(it);
383 LOGI("onDPDeviceListener is removed");
391 ThrowOcException(JNI_EXCEPTION, "onDPDeviceListener not found");
393 dpDevicesFoundListenerMapLock.unlock();
396 JniOnDirectPairingListener* AddOnDirectPairingListener(JNIEnv* env, jobject jListener)
398 JniOnDirectPairingListener *onDirectPairingListener = nullptr;
400 directPairingListenerMapLock.lock();
402 for (auto it = directPairingListenerMap.begin(); it !=
403 directPairingListenerMap.end(); ++it)
405 if (env->IsSameObject(jListener, it->first))
407 auto refPair = it->second;
408 onDirectPairingListener = refPair.first;
410 it->second = refPair;
411 directPairingListenerMap.insert(*it);
412 LOGD("onDirectPairingListener: ref. count incremented");
416 if (!onDirectPairingListener)
418 onDirectPairingListener = new JniOnDirectPairingListener(env, jListener,
419 RemoveOnDirectPairingListener);
420 jobject jgListener = env->NewGlobalRef(jListener);
421 directPairingListenerMap.insert(
422 std::pair<jobject, std::pair<JniOnDirectPairingListener*, int>>(
424 std::pair<JniOnDirectPairingListener*, int>(onDirectPairingListener, 1)));
425 LOGI("onDirectPairingListener: new listener");
427 directPairingListenerMapLock.unlock();
428 return onDirectPairingListener;
431 void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener)
433 directPairingListenerMapLock.lock();
434 bool isFound = false;
435 for (auto it = directPairingListenerMap.begin(); it !=
436 directPairingListenerMap.end(); ++it)
438 if (env->IsSameObject(jListener, it->first))
440 auto refPair = it->second;
441 if (refPair.second > 1)
444 it->second = refPair;
445 directPairingListenerMap.insert(*it);
446 LOGI("onDirectPairingListener: ref. count decremented");
450 env->DeleteGlobalRef(it->first);
451 JniOnDirectPairingListener* listener = refPair.first;
453 directPairingListenerMap.erase(it);
454 LOGI("onDirectPairingListener is removed");
462 ThrowOcException(JNI_EXCEPTION, "onDirectPairingListener not found");
464 directPairingListenerMapLock.unlock();
468 * Class: org_iotivity_base_OcPlatform
470 * Signature: (IILjava/lang/String;II)V
472 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure
473 (JNIEnv *env, jclass clazz, jint jServiceType, jint jModeType, jstring jIpAddress, jint jPort,
474 jint jQOS, jstring jDbPath)
476 LOGI("OcPlatform_configure");
478 std::string ipAddress;
482 ipAddress = env->GetStringUTFChars(jIpAddress, nullptr);
486 dbfile = env->GetStringUTFChars(jDbPath, nullptr);
487 JniOcSecurity::StoreDbPath(dbfile);
492 port = static_cast<uint16_t>(jPort);
496 JniUtils::getServiceType(env, jServiceType),
497 JniUtils::getModeType(env, jModeType),
500 JniUtils::getQOS(env, static_cast<int>(jQOS)),
501 JniOcSecurity::getOCPersistentStorage()
503 OCPlatform::Configure(cfg);
507 * Class: org_iotivity_base_OcPlatform
508 * Method: notifyAllObservers0
509 * Signature: (Lorg/iotivity/base/OcResourceHandle;)V
511 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0
512 (JNIEnv *env, jclass clazz, jobject jResourceHandle)
514 LOGI("OcPlatform_notifyAllObservers");
515 if (!jResourceHandle)
517 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
521 JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(
522 env, jResourceHandle);
523 if (!jniOcResourceHandle) return;
527 OCStackResult result =
528 OCPlatform::notifyAllObservers(jniOcResourceHandle->getOCResourceHandle());
530 if (OC_STACK_OK != result)
532 ThrowOcException(result, "Failed to notify all observers");
536 catch (OCException& e)
538 LOGE("%s", e.reason().c_str());
539 ThrowOcException(e.code(), e.reason().c_str());
544 * Class: org_iotivity_base_OcPlatform
545 * Method: notifyAllObservers1
546 * Signature: (Lorg/iotivity/base/OcResourceHandle;I)V
548 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1
549 (JNIEnv *env, jclass clazz, jobject jResourceHandle, jint jQoS)
551 LOGI("OcPlatform_notifyAllObservers1");
553 if (!jResourceHandle)
555 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
559 JniOcResourceHandle* jniOcResourceHandle =
560 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
561 if (!jniOcResourceHandle)
567 OCStackResult result = OCPlatform::notifyAllObservers(
568 jniOcResourceHandle->getOCResourceHandle(),
569 JniUtils::getQOS(env, static_cast<int>(jQoS)));
571 if (OC_STACK_OK != result)
573 ThrowOcException(result, "Failed to notify all observers");
577 catch (OCException& e)
579 LOGE("%s", e.reason().c_str());
580 ThrowOcException(e.code(), e.reason().c_str());
585 * Class: org_iotivity_base_OcPlatform
586 * Method: notifyListOfObservers2
587 * Signature: (Lorg/iotivity/base/OcResourceHandle;[Ljava/lang/Byte;Lorg/iotivity/base/OcResourceResponse;)V
589 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers2(
592 jobject jResourceHandle,
593 jbyteArray jObservationIdArr,
594 jobject jResourceResponse)
596 LOGD("OcPlatform_notifyListOfObservers2");
597 if (!jResourceHandle)
599 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
602 if (!jObservationIdArr)
604 ThrowOcException(OC_STACK_INVALID_PARAM, "observationIdList cannot be null");
607 if (!jResourceResponse)
609 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null");
613 JniOcResourceHandle* jniOcResourceHandle =
614 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
615 if (!jniOcResourceHandle)
620 JniOcResourceResponse* jniOcResourceResponse =
621 JniOcResourceResponse::getJniOcResourceResponsePtr(env, jResourceResponse);
622 if (!jniOcResourceResponse)
627 int len = env->GetArrayLength(jObservationIdArr);
628 uint8_t* bArr = (uint8_t*)env->GetByteArrayElements(jObservationIdArr, 0);
630 ObservationIds observationIds;
631 for (int i = 0; i < len; ++i)
633 observationIds.push_back(bArr[i]);
636 env->ReleaseByteArrayElements(jObservationIdArr, (jbyte*)bArr, 0);
640 OCStackResult result = OCPlatform::notifyListOfObservers(
641 jniOcResourceHandle->getOCResourceHandle(),
643 jniOcResourceResponse->getOCResourceResponse());
645 if (OC_STACK_OK != result)
647 ThrowOcException(result, "Failed to notify all observers");
650 catch (OCException& e)
652 LOGE("%s", e.reason().c_str());
653 ThrowOcException(e.code(), e.reason().c_str());
658 * Class: org_iotivity_base_OcPlatform
659 * Method: notifyListOfObservers3
660 * Signature: (Lorg/iotivity/base/OcResourceHandle;[Ljava/lang/Byte;Lorg/iotivity/base/OcResourceResponse;I)V
662 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers3(
665 jobject jResourceHandle,
666 jbyteArray jObservationIdArr,
667 jobject jResourceResponse,
670 LOGD("OcPlatform_notifyListOfObservers3");
671 if (!jResourceHandle)
673 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
676 if (!jObservationIdArr)
678 ThrowOcException(OC_STACK_INVALID_PARAM, "observationIdList cannot be null");
681 if (!jResourceResponse)
683 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null");
687 JniOcResourceHandle* jniOcResourceHandle =
688 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
689 if (!jniOcResourceHandle)
694 JniOcResourceResponse* jniOcResourceResponse =
695 JniOcResourceResponse::getJniOcResourceResponsePtr(env, jResourceResponse);
696 if (!jniOcResourceResponse)
701 int len = env->GetArrayLength(jObservationIdArr);
702 uint8_t* bArr = (uint8_t*)env->GetByteArrayElements(jObservationIdArr, 0);
704 ObservationIds observationIds;
705 for (int i = 0; i < len; ++i)
707 observationIds.push_back(bArr[i]);
710 env->ReleaseByteArrayElements(jObservationIdArr, (jbyte*)bArr, 0);
714 OCStackResult result = OCPlatform::notifyListOfObservers(
715 jniOcResourceHandle->getOCResourceHandle(),
717 jniOcResourceResponse->getOCResourceResponse(),
718 JniUtils::getQOS(env, static_cast<int>(jQoS)));
720 if (OC_STACK_OK != result)
722 ThrowOcException(result, "Failed to notify all observers");
725 catch (OCException& e)
727 LOGE("%s", e.reason().c_str());
728 ThrowOcException(e.code(), e.reason().c_str());
733 * Class: org_iotivity_base_OcPlatform
734 * Method: findResource0
735 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;)V
737 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0(
741 jstring jResourceUri,
742 jint jConnectivityType,
745 LOGD("OcPlatform_findResource");
749 host = env->GetStringUTFChars(jHost, nullptr);
751 std::string resourceUri;
754 resourceUri = env->GetStringUTFChars(jResourceUri, nullptr);
758 ThrowOcException(OC_STACK_INVALID_PARAM, "onResourceFoundListener cannot be null");
762 JniOnResourceFoundListener *onResFoundListener = AddOnResourceFoundListener(env, jListener);
764 FindCallback findCallback = [onResFoundListener](std::shared_ptr<OCResource> resource)
766 onResFoundListener->foundResourceCallback(resource);
771 OCStackResult result = OCPlatform::findResource(
774 static_cast<OCConnectivityType>(jConnectivityType),
777 if (OC_STACK_OK != result)
779 ThrowOcException(result, "Find resource has failed");
783 catch (OCException& e)
785 LOGE("%s", e.reason().c_str());
786 ThrowOcException(e.code(), e.reason().c_str());
791 * Class: org_iotivity_base_OcPlatform
792 * Method: findResource1
793 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;I)V
795 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1(
799 jstring jResourceUri,
800 jint jConnectivityType,
804 LOGD("OcPlatform_findResource");
808 host = env->GetStringUTFChars(jHost, nullptr);
810 std::string resourceUri;
813 resourceUri = env->GetStringUTFChars(jResourceUri, nullptr);
817 ThrowOcException(OC_STACK_INVALID_PARAM, "onResourceFoundListener cannot be null");
820 JniOnResourceFoundListener *onResFoundListener = AddOnResourceFoundListener(env, jListener);
822 FindCallback findCallback = [onResFoundListener](std::shared_ptr<OCResource> resource)
824 onResFoundListener->foundResourceCallback(resource);
829 OCStackResult result = OCPlatform::findResource(
832 static_cast<OCConnectivityType>(jConnectivityType),
834 JniUtils::getQOS(env, static_cast<int>(jQoS)));
836 if (OC_STACK_OK != result)
838 ThrowOcException(result, "Find resource has failed");
842 catch (OCException& e)
844 LOGE("%s", e.reason().c_str());
845 ThrowOcException(e.code(), e.reason().c_str());
850 * Class: org_iotivity_base_OcPlatform
851 * Method: findDirectPairingDevices
852 * Signature: (ILorg/iotivity/base/OcPlatform/FindDirectPairingListener;)V
854 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findDirectPairingDevices
855 (JNIEnv * env, jclass clazz, jint jTimeout, jobject jListener)
857 LOGD("OcPlatform_findDirectPairingDevices");
861 ThrowOcException(OC_STACK_INVALID_PARAM, "onDPDevicesFoundListener cannot be null");
864 JniOnDPDevicesFoundListener *onDPDevsFoundListener = AddOnDPDevicesFoundListener(env,
867 GetDirectPairedCallback getDirectPairedCallback =
868 [onDPDevsFoundListener](PairedDevices pairingDevList)
870 onDPDevsFoundListener->directPairingDevicesCallback(pairingDevList,
871 DPFunc::FIND_DIRECT_PAIRED_DEV_LIST);
876 OCStackResult result = OCPlatform::findDirectPairingDevices(jTimeout,
877 getDirectPairedCallback);
878 if (OC_STACK_OK != result)
880 ThrowOcException(result, "OCPlatform::findDirectPairingDevices has failed");
884 catch (OCException& e)
886 LOGE("%s", e.reason().c_str());
887 ThrowOcException(e.code(), e.reason().c_str());
892 * Class: org_iotivity_base_OcPlatform
893 * Method: getDirectPairedDevices
894 * Signature: (Lorg/iotivity/base/OcDirectPairDevice/GetDirectPairedListener;)V
896 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDirectPairedDevices
897 (JNIEnv *env, jclass jclazz, jobject jListener)
899 LOGD("OcPlatform_getDirectPairedDevices");
903 ThrowOcException(OC_STACK_INVALID_PARAM, "getPairedDevList Callback cannot be null");
906 JniOnDPDevicesFoundListener *onGetPairedDevicesListener = AddOnDPDevicesFoundListener(env,
909 GetDirectPairedCallback getDirectPairedCallback =
910 [onGetPairedDevicesListener](PairedDevices pairedDevList)
912 onGetPairedDevicesListener->directPairingDevicesCallback(pairedDevList,
913 DPFunc::GET_PAIRED_DEV_LIST);
918 OCStackResult result = OCPlatform::getDirectPairedDevices(getDirectPairedCallback);
919 if (OC_STACK_OK != result)
921 ThrowOcException(result, "OcDirectPairDevice_getDirectPairedDevices");
925 catch (OCException& e)
927 LOGE("%s", e.reason().c_str());
928 ThrowOcException(e.code(), e.reason().c_str());
933 * Class: org_iotivity_base_OcPlatform
934 * Method: doDirectPairing
935 * Signature: (Lorg/iotivity/base/OcDirectPairDevice;Lorg/iotivity/base/OcPrmType;
936 * Ljava/lang/String;Lorg/iotivity/base/OcDirectPairDevice/DirectPairingListener;)V
938 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_doDirectPairing0
939 (JNIEnv *env, jclass clazz, jobject jpeer, jint jprmType, jstring jpin, jobject jListener)
941 LOGD("OcPlatform_doDirectPairing");
945 ThrowOcException(OC_STACK_INVALID_PARAM, "doDirectPairing Callback cannot be null");
950 ThrowOcException(OC_STACK_INVALID_PARAM, "Peer cannot be null");
954 JniOnDirectPairingListener *onDirectPairingListener = AddOnDirectPairingListener(env,
957 DirectPairingCallback DirectPairingCB =
958 [onDirectPairingListener](std::shared_ptr<OCDirectPairing> dpDev, OCStackResult result)
960 onDirectPairingListener->doDirectPairingCB(dpDev, result);
963 JniOcDirectPairDevice *dev = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env, jpeer);
969 std::string pin = env->GetStringUTFChars(jpin, 0);
973 OCStackResult result = OCPlatform::doDirectPairing(dev->getPtr(), (OCPrm_t)jprmType,
974 pin, DirectPairingCB);
975 if (OC_STACK_OK != result)
977 ThrowOcException(result, "OcPlatform_oDirectPairing");
981 catch (OCException& e)
983 LOGE("%s", e.reason().c_str());
984 ThrowOcException(e.code(), e.reason().c_str());
989 * Class: org_iotivity_base_OcPlatform
990 * Method: getDeviceInfo0
991 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;)V
993 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0(
997 jstring jResourceUri,
998 jint jConnectivityType,
1001 LOGD("OcPlatform_getDeviceInfo0");
1005 host = env->GetStringUTFChars(jHost, nullptr);
1007 std::string resourceUri;
1010 resourceUri = env->GetStringUTFChars(jResourceUri, nullptr);
1014 ThrowOcException(OC_STACK_INVALID_PARAM, "onDeviceFoundListener cannot be null");
1017 JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener);
1019 FindDeviceCallback findDeviceCallback =
1020 [onDeviceInfoListener](const OCRepresentation& ocRepresentation)
1022 onDeviceInfoListener->foundDeviceCallback(ocRepresentation);
1027 OCStackResult result = OCPlatform::getDeviceInfo(
1030 static_cast<OCConnectivityType>(jConnectivityType),
1031 findDeviceCallback);
1033 if (OC_STACK_OK != result)
1035 ThrowOcException(result, "Find device has failed");
1038 catch (OCException& e)
1040 LOGE("%s", e.reason().c_str());
1041 ThrowOcException(e.code(), e.reason().c_str());
1046 * Class: org_iotivity_base_OcPlatform
1047 * Method: getDeviceInfo1
1048 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;I)V
1050 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1(
1054 jstring jResourceUri,
1055 jint jConnectivityType,
1059 LOGD("OcPlatform_getDeviceInfo1");
1063 host = env->GetStringUTFChars(jHost, nullptr);
1065 std::string resourceUri;
1068 resourceUri = env->GetStringUTFChars(jResourceUri, nullptr);
1072 ThrowOcException(OC_STACK_INVALID_PARAM, "onDeviceFoundListener cannot be null");
1075 JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener);
1077 FindDeviceCallback findDeviceCallback =
1078 [onDeviceInfoListener](const OCRepresentation& ocRepresentation)
1080 onDeviceInfoListener->foundDeviceCallback(ocRepresentation);
1085 OCStackResult result = OCPlatform::getDeviceInfo(
1088 static_cast<OCConnectivityType>(jConnectivityType),
1090 JniUtils::getQOS(env, static_cast<int>(jQoS)));
1092 if (OC_STACK_OK != result)
1094 ThrowOcException(result, "Find device has failed");
1097 catch (OCException& e)
1099 LOGE("%s", e.reason().c_str());
1100 ThrowOcException(e.code(), e.reason().c_str());
1105 * Class: org_iotivity_base_OcPlatform
1106 * Method: getPlatformInfo0
1107 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPlatformFoundListener;)V
1109 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo0(
1113 jstring jResourceUri,
1114 jint jConnectivityType,
1117 LOGD("OcPlatform_getPlatformInfo0");
1121 host = env->GetStringUTFChars(jHost, nullptr);
1123 std::string resourceUri;
1126 resourceUri = env->GetStringUTFChars(jResourceUri, nullptr);
1130 ThrowOcException(OC_STACK_INVALID_PARAM, "onPlatformFoundListener cannot be null");
1133 JniOnPlatformInfoListener *onPlatformInfoListener = AddOnPlatformInfoListener(env, jListener);
1135 FindPlatformCallback findPlatformCallback =
1136 [onPlatformInfoListener](const OCRepresentation& ocRepresentation)
1138 onPlatformInfoListener->foundPlatformCallback(ocRepresentation);
1143 OCStackResult result = OCPlatform::getPlatformInfo(
1146 static_cast<OCConnectivityType>(jConnectivityType),
1147 findPlatformCallback);
1149 if (OC_STACK_OK != result)
1151 ThrowOcException(result, "Find platform has failed");
1154 catch (OCException& e)
1156 LOGE("%s", e.reason().c_str());
1157 ThrowOcException(e.code(), e.reason().c_str());
1162 * Class: org_iotivity_base_OcPlatform
1163 * Method: getPlatformInfo1
1164 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPlatformFoundListener;I)V
1166 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1(
1170 jstring jResourceUri,
1171 jint jConnectivityType,
1175 LOGD("OcPlatform_getPlatformInfo1");
1179 host = env->GetStringUTFChars(jHost, nullptr);
1181 std::string resourceUri;
1184 resourceUri = env->GetStringUTFChars(jResourceUri, nullptr);
1188 ThrowOcException(OC_STACK_INVALID_PARAM, "onPlatformFoundListener cannot be null");
1191 JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener);
1193 FindDeviceCallback findDeviceCallback =
1194 [onDeviceInfoListener](const OCRepresentation& ocRepresentation)
1196 onDeviceInfoListener->foundDeviceCallback(ocRepresentation);
1201 OCStackResult result = OCPlatform::getPlatformInfo(
1204 static_cast<OCConnectivityType>(jConnectivityType),
1206 JniUtils::getQOS(env, static_cast<int>(jQoS)));
1208 if (OC_STACK_OK != result)
1210 ThrowOcException(result, "Find platform has failed");
1213 catch (OCException& e)
1215 LOGE("%s", e.reason().c_str());
1216 ThrowOcException(e.code(), e.reason().c_str());
1221 * Class: org_iotivity_base_OcPlatform
1222 * Method: registerResource0
1223 * Signature: (Lorg/iotivity/base/OcResource;)Lorg/iotivity/base/OcResourceHandle;
1225 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0(
1226 JNIEnv *env, jclass clazz, jobject jResource)
1228 LOGD("OcPlatform_registerResource");
1231 ThrowOcException(OC_STACK_INVALID_PARAM, "Resource cannot be null");
1234 JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, jResource);
1235 if (!resource) return nullptr;
1237 OCResourceHandle resourceHandle;
1240 OCStackResult result = OCPlatform::registerResource(
1242 resource->getOCResource());
1244 if (OC_STACK_OK != result)
1246 ThrowOcException(result, "register resource");
1249 catch (OCException& e)
1251 LOGE("%s", e.reason().c_str());
1252 ThrowOcException(e.code(), e.reason().c_str());
1255 JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle);
1256 jlong handle = reinterpret_cast<jlong>(jniHandle);
1257 jobject jResourceHandle =
1258 env->NewObject(g_cls_OcResourceHandle, g_mid_OcResourceHandle_N_ctor, handle);
1260 if (!jResourceHandle)
1262 LOGE("Failed to create OcResourceHandle");
1265 return jResourceHandle;
1269 * Class: org_iotivity_base_OcPlatform
1270 * Method: registerResource1
1271 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcPlatform/EntityHandler;I)Lorg/iotivity/base/OcResourceHandle;
1273 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource1(
1276 jstring jResourceUri,
1277 jstring jResourceTypeName,
1278 jstring jResourceInterface,
1279 jobject jListener, jint jResourceProperty)
1281 LOGI("OcPlatform_registerResource1");
1282 std::string resourceUri;
1285 resourceUri = env->GetStringUTFChars(jResourceUri, nullptr);
1287 std::string resourceTypeName;
1288 if (jResourceTypeName)
1290 resourceTypeName = env->GetStringUTFChars(jResourceTypeName, nullptr);
1292 std::string resourceInterface;
1293 if (jResourceInterface)
1295 resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr);
1297 JniEntityHandler* entityHandler = NULL;
1298 EntityHandler handleEntityCallback = NULL;
1301 entityHandler = new JniEntityHandler(env, jListener);
1302 handleEntityCallback =
1303 [entityHandler](const std::shared_ptr<OCResourceRequest> request) -> OCEntityHandlerResult
1305 return entityHandler->handleEntity(request);
1309 OCResourceHandle resourceHandle;
1312 OCStackResult result = OCPlatform::registerResource(
1317 handleEntityCallback,
1318 static_cast<int>(jResourceProperty));
1320 if (OC_STACK_OK != result)
1322 delete entityHandler;
1323 ThrowOcException(result, "register resource");
1327 catch (OCException& e)
1329 LOGE("%s", e.reason().c_str());
1330 delete entityHandler;
1331 ThrowOcException(e.code(), e.reason().c_str());
1335 JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle);
1336 jlong handle = reinterpret_cast<jlong>(jniHandle);
1337 jobject jResourceHandle =
1338 env->NewObject(g_cls_OcResourceHandle, g_mid_OcResourceHandle_N_ctor, handle);
1339 if (!jResourceHandle)
1341 LOGE("Failed to create OcResourceHandle");
1345 return jResourceHandle;
1349 * Class: org_iotivity_base_OcPlatform
1350 * Method: registerDeviceInfo0
1351 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
1353 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0(
1356 jstring jDeviceName,
1357 jobjectArray jDeviceTypes)
1359 LOGI("OcPlatform_registerDeviceInfo");
1363 ThrowOcException(OC_STACK_INVALID_PARAM, "deviceName cannot be null");
1369 ThrowOcException(OC_STACK_INVALID_PARAM, "deviceTypes cannot be null");
1373 OCDeviceInfo deviceInfo;
1376 DuplicateString(&deviceInfo.deviceName, env->GetStringUTFChars(jDeviceName, nullptr));
1377 deviceInfo.types = NULL;
1379 jsize len = env->GetArrayLength(jDeviceTypes);
1380 for (jsize i = 0; i < len; ++i)
1382 jstring jStr = (jstring)env->GetObjectArrayElement(jDeviceTypes, i);
1385 ThrowOcException(OC_STACK_INVALID_PARAM, "device type cannot be null");
1389 OCResourcePayloadAddStringLL(&deviceInfo.types, env->GetStringUTFChars(jStr, nullptr));
1390 if (env->ExceptionCheck()) return;
1392 env->DeleteLocalRef(jStr);
1395 catch (std::exception &e)
1397 ThrowOcException(JNI_EXCEPTION, "Failed to construct device info");
1403 OCStackResult result = OCPlatform::registerDeviceInfo(deviceInfo);
1405 delete deviceInfo.deviceName;
1407 if (OC_STACK_OK != result)
1409 ThrowOcException(result, "Failed to register device info");
1413 catch (OCException& e)
1415 LOGE("%s", e.reason().c_str());
1416 ThrowOcException(e.code(), e.reason().c_str());
1421 * Class: org_iotivity_base_OcPlatform
1422 * Method: registerPlatformInfo0
1423 * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V
1425 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0(
1428 jstring jPlatformID,
1429 jstring jManufacturerName,
1430 jstring jManufacturerUrl,
1431 jstring jModelNumber,
1432 jstring jDateOfManufacture,
1433 jstring jPlatformVersion,
1434 jstring jOperatingSystemVersion,
1435 jstring jHardwareVersion,
1436 jstring jFirmwareVersion,
1437 jstring jSupportUrl,
1438 jstring jSystemTime)
1440 LOGI("OcPlatform_registerPlatformInfo");
1443 std::string platformID;
1444 std::string manufacturerName;
1445 std::string manufacturerUrl;
1446 std::string modelNumber;
1447 std::string dateOfManufacture;
1448 std::string platformVersion;
1449 std::string operatingSystemVersion;
1450 std::string hardwareVersion;
1451 std::string firmwareVersion;
1452 std::string supportUrl;
1453 std::string systemTime;
1457 platformID = env->GetStringUTFChars(jPlatformID, nullptr);
1459 if (jManufacturerName)
1461 manufacturerName = env->GetStringUTFChars(jManufacturerName, nullptr);
1463 if (jManufacturerUrl)
1465 manufacturerUrl = env->GetStringUTFChars(jManufacturerUrl, nullptr);
1469 modelNumber = env->GetStringUTFChars(jModelNumber, nullptr);
1471 if (jDateOfManufacture)
1473 dateOfManufacture = env->GetStringUTFChars(jDateOfManufacture, nullptr);
1475 if (jPlatformVersion)
1477 platformVersion = env->GetStringUTFChars(jPlatformVersion, nullptr);
1479 if (jOperatingSystemVersion)
1481 operatingSystemVersion = env->GetStringUTFChars(jOperatingSystemVersion, nullptr);
1483 if (jHardwareVersion)
1485 hardwareVersion = env->GetStringUTFChars(jHardwareVersion, nullptr);
1487 if (jFirmwareVersion)
1489 firmwareVersion = env->GetStringUTFChars(jFirmwareVersion, nullptr);
1493 supportUrl = env->GetStringUTFChars(jSupportUrl, nullptr);
1497 systemTime = env->GetStringUTFChars(jSystemTime, nullptr);
1500 OCPlatformInfo platformInfo;
1503 DuplicateString(&platformInfo.platformID, platformID);
1504 DuplicateString(&platformInfo.manufacturerName, manufacturerName);
1505 DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl);
1506 DuplicateString(&platformInfo.modelNumber, modelNumber);
1507 DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture);
1508 DuplicateString(&platformInfo.platformVersion, platformVersion);
1509 DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion);
1510 DuplicateString(&platformInfo.hardwareVersion, hardwareVersion);
1511 DuplicateString(&platformInfo.firmwareVersion, firmwareVersion);
1512 DuplicateString(&platformInfo.supportUrl, supportUrl);
1513 DuplicateString(&platformInfo.systemTime, systemTime);
1515 catch (std::exception &e)
1517 ThrowOcException(JNI_EXCEPTION, "Failed to construct platform info");
1521 // __android_log_print(ANDROID_LOG_INFO, "Rahul", "platformID = %s", platformID);
1524 OCStackResult result = OCPlatform::registerPlatformInfo(platformInfo);
1526 delete platformInfo.platformID;
1527 delete platformInfo.manufacturerName;
1528 delete platformInfo.manufacturerUrl;
1529 delete platformInfo.modelNumber;
1530 delete platformInfo.dateOfManufacture;
1531 delete platformInfo.platformVersion;
1532 delete platformInfo.operatingSystemVersion;
1533 delete platformInfo.hardwareVersion;
1534 delete platformInfo.firmwareVersion;
1535 delete platformInfo.supportUrl;
1536 delete platformInfo.systemTime;
1538 if (OC_STACK_OK != result)
1540 ThrowOcException(result, "Failed to register platform info");
1544 catch (OCException& e)
1546 LOGE("Error is due to %s", e.reason().c_str());
1547 ThrowOcException(e.code(), e.reason().c_str());
1552 * Class: org_iotivity_base_OcPlatform
1553 * Method: unregisterResource0
1554 * Signature: (Lorg/iotivity/base/OcResourceHandle;)V
1556 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unregisterResource0(
1557 JNIEnv *env, jclass clazz, jobject jResourceHandle)
1559 LOGI("OcPlatform_unregisterResource");
1560 if (!jResourceHandle)
1562 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
1565 JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(
1566 env, jResourceHandle);
1567 if (!jniOcResourceHandle)
1574 OCResourceHandle resHandle = jniOcResourceHandle->getOCResourceHandle();
1575 OCStackResult result = OCPlatform::unregisterResource(resHandle);
1576 if (OC_STACK_OK != result)
1578 ThrowOcException(result, "Failed to unregister resource");
1581 catch (OCException& e)
1583 LOGE("%s", e.reason().c_str());
1584 ThrowOcException(e.code(), e.reason().c_str());
1589 * Class: org_iotivity_base_OcPlatform
1590 * Method: bindResource0
1591 * Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V
1593 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0
1594 (JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobject jResourceHandle)
1596 LOGI("OcPlatform_bindResource");
1597 if (!jResourceCollectionHandle)
1599 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null");
1602 if (!jResourceHandle)
1604 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
1607 JniOcResourceHandle* jniOcResourceCollectionHandle =
1608 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceCollectionHandle);
1609 if (!jniOcResourceCollectionHandle) return;
1611 JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(
1612 env, jResourceHandle);
1613 if (!jniOcResourceHandle)
1620 OCStackResult result = OCPlatform::bindResource(
1621 jniOcResourceCollectionHandle->getOCResourceHandle(),
1622 jniOcResourceHandle->getOCResourceHandle()
1625 if (OC_STACK_OK != result)
1627 ThrowOcException(result, "Failed to bind resource");
1630 catch (OCException& e)
1632 LOGE("%s", e.reason().c_str());
1633 ThrowOcException(e.code(), e.reason().c_str());
1638 * Class: org_iotivity_base_OcPlatform
1639 * Method: bindResources0
1640 * Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V
1642 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResources0(
1645 jobject jResourceCollectionHandle,
1646 jobjectArray jResourceHandleArray)
1648 LOGI("OcPlatform_bindResources");
1650 if (!jResourceCollectionHandle)
1652 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null");
1655 if (!jResourceHandleArray)
1657 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null");
1661 JniOcResourceHandle* jniOcResourceCollectionHandle =
1662 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceCollectionHandle);
1663 if (!jniOcResourceCollectionHandle)
1668 std::vector<OCResourceHandle> resourceHandleList;
1669 int len = env->GetArrayLength(jResourceHandleArray);
1670 for (int i = 0; i < len; ++i)
1672 jobject jResourceHandle = env->GetObjectArrayElement(jResourceHandleArray, i);
1673 if (!jResourceHandle)
1675 ThrowOcException(JNI_EXCEPTION, "resource handle cannot be null");
1679 JniOcResourceHandle* jniOcResourceHandle =
1680 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
1681 if (!jniOcResourceHandle)
1686 resourceHandleList.push_back(
1687 jniOcResourceHandle->getOCResourceHandle());
1692 OCStackResult result = OCPlatform::bindResources(
1693 jniOcResourceCollectionHandle->getOCResourceHandle(),
1697 if (OC_STACK_OK != result)
1699 ThrowOcException(result, "Failed to bind resources");
1702 catch (OCException& e)
1704 LOGE("%s", e.reason().c_str());
1705 ThrowOcException(e.code(), e.reason().c_str());
1710 * Class: org_iotivity_base_OcPlatform
1711 * Method: unbindResource0
1712 * Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V
1714 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResource0(
1717 jobject jResourceCollectionHandle,
1718 jobject jResourceHandle)
1720 LOGI("OcPlatform_unbindResource");
1721 if (!jResourceCollectionHandle)
1723 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null");
1726 if (!jResourceHandle)
1728 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
1732 JniOcResourceHandle* jniOcResourceCollectionHandle =
1733 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceCollectionHandle);
1734 if (!jniOcResourceCollectionHandle)
1739 JniOcResourceHandle* jniOcResourceHandle =
1740 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
1741 if (!jniOcResourceHandle)
1748 OCStackResult result = OCPlatform::unbindResource(
1749 jniOcResourceCollectionHandle->getOCResourceHandle(),
1750 jniOcResourceHandle->getOCResourceHandle());
1752 if (OC_STACK_OK != result)
1754 ThrowOcException(result, "Failed to unbind resource");
1757 catch (OCException& e)
1759 LOGE("%s", e.reason().c_str());
1760 ThrowOcException(e.code(), e.reason().c_str());
1765 * Class: org_iotivity_base_OcPlatform
1766 * Method: unbindResources0
1767 * Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V
1769 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResources0(
1772 jobject jResourceCollectionHandle,
1773 jobjectArray jResourceHandleArray)
1775 LOGI("OcPlatform_unbindResources");
1776 if (!jResourceCollectionHandle)
1778 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null");
1781 if (!jResourceHandleArray)
1783 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null");
1787 JniOcResourceHandle* jniOcResourceCollectionHandle =
1788 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceCollectionHandle);
1789 if (!jniOcResourceCollectionHandle)
1794 std::vector<OCResourceHandle> resourceHandleList;
1795 int len = env->GetArrayLength(jResourceHandleArray);
1796 for (int i = 0; i < len; ++i)
1798 jobject jResourceHandle = env->GetObjectArrayElement(jResourceHandleArray, i);
1799 if (!jResourceHandle)
1801 ThrowOcException(JNI_EXCEPTION, "resource handle cannot be null");
1805 JniOcResourceHandle* jniOcResourceHandle =
1806 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
1807 if (!jniOcResourceHandle)
1812 resourceHandleList.push_back(
1813 jniOcResourceHandle->getOCResourceHandle());
1818 OCStackResult result = OCPlatform::unbindResources(
1819 jniOcResourceCollectionHandle->getOCResourceHandle(),
1823 if (OC_STACK_OK != result)
1825 ThrowOcException(result, "Failed to unbind resources");
1828 catch (OCException& e)
1830 LOGE("%s", e.reason().c_str());
1831 ThrowOcException(e.code(), e.reason().c_str());
1836 * Class: org_iotivity_base_OcPlatform
1837 * Method: bindTypeToResource0
1838 * Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V
1840 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0(
1843 jobject jResourceHandle,
1844 jstring jResourceTypeName)
1846 LOGI("OcPlatform_bindTypeToResource");
1847 if (!jResourceHandle)
1849 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
1852 std::string typeName;
1853 if (jResourceTypeName)
1855 typeName = env->GetStringUTFChars(jResourceTypeName, nullptr);
1858 JniOcResourceHandle* jniOcResourceHandle =
1859 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
1860 if (!jniOcResourceHandle) return;
1864 OCStackResult result = OCPlatform::bindTypeToResource(
1865 jniOcResourceHandle->getOCResourceHandle(),
1869 if (OC_STACK_OK != result)
1871 ThrowOcException(result, "Failed to bind type to resource");
1874 catch (OCException& e)
1876 LOGE("%s", e.reason().c_str());
1877 ThrowOcException(e.code(), e.reason().c_str());
1882 * Class: org_iotivity_base_OcPlatform
1883 * Method: bindInterfaceToResource0
1884 * Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V
1886 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource0
1887 (JNIEnv *env, jclass clazz, jobject jResourceHandle, jstring jResourceInterfaceName)
1889 LOGI("OcPlatform_bindInterfaceToResource");
1890 if (!jResourceHandle)
1892 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null");
1895 std::string interfaceName;
1896 if (jResourceInterfaceName)
1898 interfaceName = env->GetStringUTFChars(jResourceInterfaceName, nullptr);
1901 JniOcResourceHandle* jniOcResourceHandle =
1902 JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle);
1903 if (!jniOcResourceHandle)
1910 OCStackResult result = OCPlatform::bindInterfaceToResource(
1911 jniOcResourceHandle->getOCResourceHandle(),
1915 if (OC_STACK_OK != result)
1917 ThrowOcException(result, "Failed to bind interface to resource");
1920 catch (OCException& e)
1922 LOGE("%s", e.reason().c_str());
1923 ThrowOcException(e.code(), e.reason().c_str());
1928 * Class: org_iotivity_base_OcPlatform
1929 * Method: startPresence0
1932 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_startPresence0(
1933 JNIEnv *env, jclass clazz, jint ttl)
1935 LOGI("OcPlatform_startPresence");
1939 OCStackResult result = OCPlatform::startPresence((unsigned int)ttl);
1941 if (OC_STACK_OK != result)
1943 ThrowOcException(result, "Failed to start presence");
1946 catch (OCException& e)
1948 LOGE("%s", e.reason().c_str());
1949 ThrowOcException(e.code(), e.reason().c_str());
1954 * Class: org_iotivity_base_OcPlatform
1955 * Method: stopPresence0
1958 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stopPresence0(
1959 JNIEnv *env, jclass clazz)
1961 LOGI("OcPlatform_stopPresence");
1965 OCStackResult result = OCPlatform::stopPresence();
1967 if (OC_STACK_OK != result)
1969 ThrowOcException(result, "Failed to stop presence");
1972 catch (OCException& e)
1974 LOGE("%s", e.reason().c_str());
1975 ThrowOcException(e.code(), e.reason().c_str());
1980 * Class: org_iotivity_base_OcPlatform
1981 * Method: subscribePresence0
1982 * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle;
1984 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0(
1988 jint jConnectivityType,
1991 LOGD("OcPlatform_subscribePresence");
1995 host = env->GetStringUTFChars(jHost, nullptr);
1999 ThrowOcException(OC_STACK_INVALID_PARAM, "onPresenceListener cannot be null");
2003 JniOnPresenceListener *onPresenceListener = AddOnPresenceListener(env, jListener);
2005 SubscribeCallback subscribeCallback =
2006 [onPresenceListener](OCStackResult result,
2007 const unsigned int nonce,
2008 const std::string& hostAddress)
2010 onPresenceListener->onPresenceCallback(result, nonce, hostAddress);
2013 OCPlatform::OCPresenceHandle presenceHandle;
2016 OCStackResult result = OCPlatform::subscribePresence(
2019 static_cast<OCConnectivityType>(jConnectivityType),
2022 if (OC_STACK_OK != result)
2024 ThrowOcException(result, "subscribe presence has failed");
2027 catch (OCException& e)
2029 LOGE("%s", e.reason().c_str());
2030 ThrowOcException(e.code(), e.reason().c_str());
2034 JniOcPresenceHandle* jniPresenceHandle =
2035 new JniOcPresenceHandle(onPresenceListener, presenceHandle);
2036 jlong jhandle = reinterpret_cast<jlong>(jniPresenceHandle);
2037 jobject jPresenceHandle =
2038 env->NewObject(g_cls_OcPresenceHandle, g_mid_OcPresenceHandle_N_ctor, jhandle);
2039 if (!jPresenceHandle)
2041 LOGE("Failed to create OcPresenceHandle");
2042 delete jniPresenceHandle;
2044 return jPresenceHandle;
2048 * Class: org_iotivity_base_OcPlatform
2049 * Method: subscribePresence1
2050 * Signature: (Ljava/lang/String;Ljava/lang/String;I
2051 Lorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle;
2053 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1(
2057 jstring jResourceType,
2058 jint jConnectivityType,
2061 LOGD("OcPlatform_subscribePresence1");
2065 host = env->GetStringUTFChars(jHost, nullptr);
2067 std::string resourceType;
2070 resourceType = env->GetStringUTFChars(jResourceType, nullptr);
2074 ThrowOcException(OC_STACK_INVALID_PARAM, "onPresenceListener cannot be null");
2078 JniOnPresenceListener *onPresenceListener = AddOnPresenceListener(env, jListener);
2080 SubscribeCallback subscribeCallback = [onPresenceListener](OCStackResult result,
2081 const unsigned int nonce, const std::string& hostAddress)
2083 onPresenceListener->onPresenceCallback(result, nonce, hostAddress);
2086 OCPlatform::OCPresenceHandle presenceHandle;
2089 OCStackResult result = OCPlatform::subscribePresence(
2093 static_cast<OCConnectivityType>(jConnectivityType),
2096 if (OC_STACK_OK != result)
2098 ThrowOcException(result, "subscribe presence has failed");
2101 catch (OCException& e)
2103 LOGE("%s", e.reason().c_str());
2104 ThrowOcException(e.code(), e.reason().c_str());
2108 JniOcPresenceHandle* jniPresenceHandle =
2109 new JniOcPresenceHandle(onPresenceListener, presenceHandle);
2110 jlong jhandle = reinterpret_cast<jlong>(jniPresenceHandle);
2111 jobject jPresenceHandle =
2112 env->NewObject(g_cls_OcPresenceHandle, g_mid_OcPresenceHandle_N_ctor, jhandle);
2113 if (!jPresenceHandle)
2115 LOGE("Failed to create OcPresenceHandle");
2116 delete jniPresenceHandle;
2118 return jPresenceHandle;
2122 * Class: org_iotivity_base_OcPlatform
2123 * Method: unsubscribePresence0
2124 * Signature: (Lorg/iotivity/base/OcPresenceHandle;)V
2126 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0(
2127 JNIEnv *env, jclass clazz, jobject jPresenceHandle)
2129 LOGD("OcPlatform_unsubscribePresence");
2130 if (!jPresenceHandle)
2132 ThrowOcException(OC_STACK_INVALID_PARAM, "presenceHandle cannot be null");
2135 JniOcPresenceHandle* jniPresenceHandle =
2136 JniOcPresenceHandle::getJniOcPresenceHandlePtr(env, jPresenceHandle);
2137 if (!jniPresenceHandle)
2142 OCPresenceHandle presenceHandle = jniPresenceHandle->getOCPresenceHandle();
2146 OCStackResult result = OCPlatform::unsubscribePresence(presenceHandle);
2148 if (OC_STACK_OK != result)
2150 ThrowOcException(result, "unsubscribe presence has failed");
2153 jweak jwOnPresenceListener =
2154 jniPresenceHandle->getJniOnPresenceListener()->getJWListener();
2155 if (jwOnPresenceListener)
2157 RemoveOnPresenceListener(env, jwOnPresenceListener);
2160 catch (OCException& e)
2162 LOGE("%s", e.reason().c_str());
2163 ThrowOcException(e.code(), e.reason().c_str());
2168 * Class: org_iotivity_base_OcPlatform
2169 * Method: constructResourceObject0
2170 * Signature: (Ljava/lang/String;Ljava/lang/String;IZ[Ljava/lang/String;[Ljava/lang/String;)
2171 Lorg/iotivity/base/OcResource;
2173 JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructResourceObject0(
2178 jint jConnectivityType,
2179 jboolean jIsObservable,
2180 jobjectArray jResourceTypeArray,
2181 jobjectArray jInterfaceArray)
2183 LOGD("OcPlatform_constructResourceObject");
2187 host = env->GetStringUTFChars(jHost, nullptr);
2192 uri = env->GetStringUTFChars(jUri, nullptr);
2194 if (!jResourceTypeArray)
2196 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceTypeList cannot be null");
2199 if (!jInterfaceArray)
2201 ThrowOcException(OC_STACK_INVALID_PARAM, "interfaceList cannot be null");
2205 std::vector<std::string> resourceTypes;
2206 JniUtils::convertJavaStrArrToStrVector(env, jResourceTypeArray, resourceTypes);
2208 std::vector<std::string> interfaces;
2209 JniUtils::convertJavaStrArrToStrVector(env, jInterfaceArray, interfaces);
2211 std::shared_ptr<OCResource> resource = OCPlatform::constructResourceObject(
2214 static_cast<OCConnectivityType>(jConnectivityType),
2215 static_cast<bool>(jIsObservable),
2221 ThrowOcException(OC_STACK_ERROR, "Failed to create OCResource");
2225 JniOcResource *jniOcResource = new JniOcResource(resource);
2226 jlong handle = reinterpret_cast<jlong>(jniOcResource);
2228 jobject jResource = env->NewObject(g_cls_OcResource, g_mid_OcResource_ctor);
2231 delete jniOcResource;
2234 SetHandle<JniOcResource>(env, jResource, jniOcResource);
2235 if (env->ExceptionCheck())
2237 delete jniOcResource;
2244 * Class: org_iotivity_base_OcPlatform
2245 * Method: sendResponse0
2246 * Signature: (Lorg/iotivity/base/OcResourceResponse;)V
2248 JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0(
2249 JNIEnv *env, jclass clazz, jobject jResourceResponse)
2251 LOGD("OcPlatform_sendResponse");
2252 if (!jResourceResponse)
2254 ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null");
2258 JniOcResourceResponse *jniResponse =
2259 JniOcResourceResponse::getJniOcResourceResponsePtr(env, jResourceResponse);
2260 if (!jniResponse) return;
2264 OCStackResult result =
2265 OCPlatform::sendResponse(jniResponse->getOCResourceResponse());
2267 if (OC_STACK_OK != result)
2269 ThrowOcException(result, "failed to send response");
2272 catch (OCException& e)
2274 LOGE("%s", e.reason().c_str());
2275 ThrowOcException(e.code(), e.reason().c_str());