From: abitha.s Date: Wed, 10 Aug 2016 13:34:24 +0000 (+0530) Subject: Modify Callback function in cpp wrapper for Notification Service. X-Git-Tag: 1.2.0+RC1~52^2~65 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6ed7ab915fc2c42fa13d5f63bc4302f3f4b4c9f5;p=platform%2Fupstream%2Fiotivity.git Modify Callback function in cpp wrapper for Notification Service. 1) changed Subscribe Accepted CB to Provider Changed CB. 2) Provider Changed CB is invoked when Provider status is changed. 3) enabled c++ wrapper Change-Id: If07c8c71d764bd87d57f2aa519a09525bd5ef286 Signed-off-by: abitha.s Reviewed-on: https://gerrit.iotivity.org/gerrit/10233 Tested-by: jenkins-iotivity Reviewed-by: Chihyun Cho Reviewed-by: Madan Lanka --- diff --git a/service/notification/SConscript b/service/notification/SConscript index 3879098..1553295 100755 --- a/service/notification/SConscript +++ b/service/notification/SConscript @@ -121,4 +121,4 @@ if target_os == 'android': SConscript('android/SConscript') # Go to build c++ wrapper -#SConscript('cpp-wrapper/SConscript') +SConscript('cpp-wrapper/SConscript') diff --git a/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp b/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp index 4d91aac..e4b5c1e 100755 --- a/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp +++ b/service/notification/android/notification-service/src/main/jni/consumer/JniNotificationConsumer.cpp @@ -133,7 +133,7 @@ void onDiscoverProvider(OIC::Service::NSProvider *provider) return ; } -void onSubscriptionAccepted(OIC::Service::NSProvider *provider) +void onProviderChanged(OIC::Service::NSProvider *provider,OIC::Service::NSResponse response) { LOGD ("ConsumerService_onSubscriptionAccepted"); @@ -187,6 +187,7 @@ void onSubscriptionAccepted(OIC::Service::NSProvider *provider) if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); return ; } + //TODO: Modify to call ProviderChanged Cb adding topic in Java jmethodID mid = env->GetMethodID( cls, "onSubscriptionAccepted", @@ -499,7 +500,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_service_ns_consumer_ConsumerService_nat OIC::Service::NSConsumerService::ConsumerConfig cfg; cfg.m_discoverCb = onDiscoverProvider; - cfg.m_acceptedCb = onSubscriptionAccepted; + cfg.m_changedCb = onProviderChanged; OIC::Service::NSConsumerService::getInstance()->Start(cfg); @@ -529,10 +530,10 @@ Java_org_iotivity_service_ns_consumer_ConsumerService_nativeEnableRemoteService if (!jServerAddress) { ThrowNSException(NS_ERROR, "EnableRemoteService server address NULL"); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } const char *serverAddress = env->GetStringUTFChars(jServerAddress, 0); - OIC::Service::Result res = + OIC::Service::NSResult res = OIC::Service::NSConsumerService::getInstance()->EnableRemoteService(std::string(serverAddress)); env->ReleaseStringUTFChars(jServerAddress, serverAddress); return (jint) res; diff --git a/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp b/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp index 595a0d9..6aa0977 100644 --- a/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp +++ b/service/notification/android/notification-service/src/main/jni/provider/JniNotificationProvider.cpp @@ -435,7 +435,7 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat { LOGE("Fail to set listeners"); ThrowNSException(NS_ERROR, "Listener cannot be null"); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } if (g_obj_subscriptionListener != NULL) @@ -457,8 +457,8 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat cfg.m_syncInfoCb = onSyncInfoListenerCb; cfg.policy = (bool) jPolicy; - OIC::Service::Result result = OIC::Service::NSProviderService::getInstance()->Start(cfg); - if (result != OIC::Service::Result::OK) + OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->Start(cfg); + if (result != OIC::Service::NSResult::OK) { LOGE("Fail to start NSProviderService"); @@ -473,8 +473,8 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat { LOGI("JNIProviderService: nativeStop - IN"); - OIC::Service::Result result = OIC::Service::NSProviderService::getInstance()->Stop(); - if (result != OIC::Service::Result::OK) + OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->Stop(); + if (result != OIC::Service::NSResult::OK) { LOGI("Fail to stop NSProvider service"); return (jint) result; @@ -497,17 +497,17 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_ProviderService_nat { LOGI("Fail to send notification - Message is null"); ThrowNSException(NS_ERROR, "Message cannot be null"); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } OIC::Service::NSMessage *nsMsg = getMessage(env, jMsg); if (nsMsg == nullptr) { ThrowNSException(NS_ERROR, "Message didn't have a field ID "); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } - OIC::Service::Result result = OIC::Service::NSProviderService::getInstance()->SendMessage(nsMsg); - if (result != OIC::Service::Result::OK) + OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->SendMessage(nsMsg); + if (result != OIC::Service::NSResult::OK) { LOGI("Fail to send NSProvider Message"); } @@ -533,14 +533,14 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeEnableRemoteService( if (!jstr) { ThrowNSException(NS_ERROR, "Server Address Can't be NULL"); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } const char *address = env->GetStringUTFChars( jstr, NULL); std::string servAddress(address); - OIC::Service::Result result = OIC::Service::NSProviderService::getInstance()->EnableRemoteService( + OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->EnableRemoteService( servAddress); - if (result != OIC::Service::Result::OK) + if (result != OIC::Service::NSResult::OK) { LOGE("Fail to Enable Remote Service"); } @@ -557,14 +557,14 @@ Java_org_iotivity_service_ns_provider_ProviderService_nativeDisableRemoteService if (!jstr) { ThrowNSException(NS_ERROR, "Server Address Can't be NULL"); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } const char *address = env->GetStringUTFChars( jstr, NULL); std::string servAddress(address); - OIC::Service::Result result = OIC::Service::NSProviderService::getInstance()->DisableRemoteService( + OIC::Service::NSResult result = OIC::Service::NSProviderService::getInstance()->DisableRemoteService( servAddress); - if (result != OIC::Service::Result::OK) + if (result != OIC::Service::NSResult::OK) { LOGE("Fail to Disable Remote Service"); } @@ -583,7 +583,7 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeAcce if (!consumerClass) { ThrowNSException(NS_ERROR, "Failed to Get ObjectClass for Consumer"); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } // Consumer ID @@ -592,7 +592,7 @@ JNIEXPORT jint JNICALL Java_org_iotivity_service_ns_provider_Consumer_nativeAcce { LOGE("Error: jfieldID for mConsumerId is null"); ThrowNSException(NS_ERROR, "ConsumerId not found"); - return (jint) OIC::Service::Result::ERROR; + return (jint) OIC::Service::NSResult::ERROR; } jstring jconId = (jstring)env->GetObjectField( jConsumer, fid_id); diff --git a/service/notification/cpp-wrapper/common/NSErrorCode.h b/service/notification/cpp-wrapper/common/NSUtils.h similarity index 71% rename from service/notification/cpp-wrapper/common/NSErrorCode.h rename to service/notification/cpp-wrapper/common/NSUtils.h index c337b10..dce760f 100755 --- a/service/notification/cpp-wrapper/common/NSErrorCode.h +++ b/service/notification/cpp-wrapper/common/NSUtils.h @@ -1,47 +1,55 @@ -//****************************************************************** -// -// Copyright 2016 Samsung Electronics All Rights Reserved. -// -//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - -/** - * @file - * - * This file contains Notification service Error Codes. - */ - -#ifndef _NS_ERROR_CODE_H_ -#define _NS_ERROR_CODE_H_ - - -namespace OIC -{ - namespace Service - { - /** Result - enumeration for NS service Result*/ - enum class Result - { - OK = 100, - ERROR = 200, - SUCCESS = 300, - FAIL = 400, - ALLOW = 500, - DENY = 600, - }; - } -} -#endif /* _NS_ERROR_CODE_H_ */ +//****************************************************************** +// +// Copyright 2016 Samsung Electronics All Rights Reserved. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +/** + * @file + * + * This file contains Notification service Utils. + */ + +#ifndef _NS_UTILS_H_ +#define _NS_UTILS_H_ + + +namespace OIC +{ + namespace Service + { + /** NSResult - enumeration for NS service Result*/ + enum class NSResult + { + OK = 100, + ERROR = 200, + SUCCESS = 300, + FAIL = 400, + }; + + /** + * NSResponse code of notification service + */ + enum class NSResponse + { + ALLOW = 1, + DENY = 2, + TOPIC = 3, + }; + } +} +#endif /* _NS_UTILS_H_ */ diff --git a/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h b/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h index e34fd69..2f6aa89 100755 --- a/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h +++ b/service/notification/cpp-wrapper/consumer/inc/NSConsumerService.h @@ -31,7 +31,7 @@ #include #include #include "NSProvider.h" -#include "NSErrorCode.h" +#include "NSUtils.h" namespace OIC @@ -53,10 +53,11 @@ namespace OIC typedef void (*ProviderDiscoveredCallback)(NSProvider *); /** - * Consumer uses this callback function on subscription accepted by provider - * @param[in] provider Provider who has the notification resource + * Invoked when the provider state is changed + * @param[in] provider Provider which has the notification resource + * @param[in] response Response which has the provider state */ - typedef void (* AcceptedCallback)(NSProvider *); + typedef void (* ProviderChangedCallback)(NSProvider *, NSResponse); /** * @struct ConsumerConfig @@ -67,8 +68,8 @@ namespace OIC { /** m_discoverCb - ProviderDiscoveredCallback callback listener.*/ ProviderDiscoveredCallback m_discoverCb; - /** m_acceptedCb - AcceptedCallback callback listener.*/ - AcceptedCallback m_acceptedCb; + /** m_changedCb - ProviderChangedCallback callback listener.*/ + ProviderChangedCallback m_changedCb; } ConsumerConfig; /** @@ -93,9 +94,9 @@ namespace OIC /** * Request to discover to remote address as parameter. * @param[in] server address combined with IP address and port number using delimiter : - * @return result code of Consumer Service + * @return NSResult code of Consumer Service */ - Result EnableRemoteService(const std::string &serverAddress); + NSResult EnableRemoteService(const std::string &serverAddress); /** * Request discovery manually diff --git a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp index 341fb92..5be4b61 100755 --- a/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp +++ b/service/notification/cpp-wrapper/consumer/src/NSConsumerService.cpp @@ -40,15 +40,25 @@ namespace OIC NS_LOG(DEBUG, "onNSProviderDiscovered - OUT"); } - void onNSAccepted(::NSProvider *provider) + void onNSProviderChanged(::NSProvider *provider, ::NSResponse response) { - NS_LOG(DEBUG, "onNSAccepted - IN"); + NS_LOG(DEBUG, "onNSProviderChanged - IN"); + NS_LOG_V(DEBUG, "provider Id : %s",provider->providerId); + NS_LOG_V(DEBUG, "response : %d",(int)response); + NSProvider *nsProvider = new NSProvider(provider); - NSConsumerService::getInstance()->getAcceptedProviders().push_back(nsProvider); - - if (NSConsumerService::getInstance()->getConsumerConfig().m_acceptedCb != NULL) - NSConsumerService::getInstance()->getConsumerConfig().m_acceptedCb(nsProvider); - NS_LOG(DEBUG, "onNSAccepted - OUT"); + if(response == NS_ALLOW) + { + NSConsumerService::getInstance()->getAcceptedProviders().push_back(nsProvider); + } + else if(response == NS_DENY) + { + NSConsumerService::getInstance()->getAcceptedProviders().remove(nsProvider); + } + if (NSConsumerService::getInstance()->getConsumerConfig().m_changedCb != NULL) + NSConsumerService::getInstance()->getConsumerConfig().m_changedCb( + nsProvider, (NSResponse) response); + NS_LOG(DEBUG, "onNSProviderChanged - OUT"); } void onNSMessageReceived(::NSMessage *message) @@ -94,7 +104,7 @@ namespace OIC NSConsumerService::NSConsumerService() { m_config.m_discoverCb = NULL; - m_config.m_acceptedCb = NULL; + m_config.m_changedCb = NULL; } NSConsumerService::~NSConsumerService() @@ -117,7 +127,7 @@ namespace OIC m_config = config; NSConsumerConfig nsConfig; nsConfig.discoverCb = onNSProviderDiscovered; - nsConfig.acceptedCb = onNSAccepted; + nsConfig.changedCb= onNSProviderChanged; nsConfig.messageCb = onNSMessageReceived; nsConfig.syncInfoCb = onNSSyncInfoReceived; @@ -134,12 +144,12 @@ namespace OIC return; } - Result NSConsumerService::EnableRemoteService(const std::string &serverAddress) + NSResult NSConsumerService::EnableRemoteService(const std::string &serverAddress) { NS_LOG(DEBUG, "EnableRemoteService - IN"); - Result result = Result::ERROR; + NSResult result = NSResult::ERROR; #ifdef WITH_CLOUD - result = (Result) NSConsumerEnableRemoteService(OICStrdup(serverAddress.c_str())); + result = (NSResult) NSConsumerEnableRemoteService(OICStrdup(serverAddress.c_str())); #else NS_LOG(ERROR, "Remote Services feature is not enabled in the Build"); #endif diff --git a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp index c2f8fea..fe2af59 100755 --- a/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp +++ b/service/notification/cpp-wrapper/examples/linux/notificationserviceconsumer.cpp @@ -54,11 +54,12 @@ void onDiscoverNotificationCb(OIC::Service::NSProvider *provider) std::cout << "startSubscribing" << std::endl; } -void onSubscriptionAcceptedCb(OIC::Service::NSProvider *provider) +void onProviderChangedCb(OIC::Service::NSProvider *provider,OIC::Service::NSResponse response) { std::cout << "Subscription accepted" << std::endl; std::cout << "subscribed provider Id : " << provider->getProviderId() << std::endl; - provider->setListener(onNotificationPostedCb, onNotificationSyncCb); + if(response == OIC::Service::NSResponse::ALLOW) + provider->setListener(onNotificationPostedCb, onNotificationSyncCb); } void *OCProcessThread(void *ptr) @@ -91,7 +92,7 @@ int main(void) NSConsumerService::ConsumerConfig cfg; cfg.m_discoverCb = onDiscoverNotificationCb; - cfg.m_acceptedCb = onSubscriptionAcceptedCb; + cfg.m_changedCb = onProviderChangedCb; pthread_create(&OCThread, NULL, OCProcessThread, NULL); diff --git a/service/notification/cpp-wrapper/provider/inc/NSProviderService.h b/service/notification/cpp-wrapper/provider/inc/NSProviderService.h index 9b64ec6..deed41f 100755 --- a/service/notification/cpp-wrapper/provider/inc/NSProviderService.h +++ b/service/notification/cpp-wrapper/provider/inc/NSProviderService.h @@ -31,7 +31,7 @@ #include "NSConsumer.h" #include "NSSyncInfo.h" #include "NSMessage.h" -#include "NSErrorCode.h" +#include "NSUtils.h" namespace OIC { @@ -103,34 +103,34 @@ namespace OIC * and onMessageSynchronized function listeners * @return :: result code of Provider Service */ - Result Start(ProviderConfig config); + NSResult Start(ProviderConfig config); /** * Terminate notification service for provider * @return :: result code of Provider Service */ - Result Stop(); + NSResult Stop(); /** * Request to publish resource to cloud server * @param[in] server address combined with IP address and port number using delimiter : * @return result code of Provider Service */ - Result EnableRemoteService(const std::string &serverAddress); + NSResult EnableRemoteService(const std::string &serverAddress); /** * Request to cancel remote service using cloud server * @param[in] server address combined with IP address and port number using delimiter : * @return result code of Provider Service */ - Result DisableRemoteService(const std::string &serverAddress); + NSResult DisableRemoteService(const std::string &serverAddress); /** * Send notification message to all subscribers * @param[in] msg Notification message including id, title, contentText * @return :: result code of Provider Service */ - Result SendMessage(NSMessage *msg); + NSResult SendMessage(NSMessage *msg); /** diff --git a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp index e4335a7..68ac038 100755 --- a/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp +++ b/service/notification/cpp-wrapper/provider/src/NSProviderService.cpp @@ -78,7 +78,7 @@ namespace OIC return &s_instance; } - Result NSProviderService::Start(NSProviderService::ProviderConfig config) + NSResult NSProviderService::Start(NSProviderService::ProviderConfig config) { NS_LOG(DEBUG, "Start - IN"); @@ -89,25 +89,25 @@ namespace OIC nsConfig.policy = config.policy; nsConfig.userInfo = OICStrdup(config.userInfo.c_str()); - Result result = (Result) NSStartProvider(nsConfig); + NSResult result = (NSResult) NSStartProvider(nsConfig); NS_LOG(DEBUG, "Start - OUT"); return result; } - Result NSProviderService::Stop() + NSResult NSProviderService::Stop() { NS_LOG(DEBUG, "Stop - IN"); - Result result = (Result) NSStopProvider(); + NSResult result = (NSResult) NSStopProvider(); NS_LOG(DEBUG, "Stop - OUT"); return result; } - Result NSProviderService::EnableRemoteService(const std::string &serverAddress) + NSResult NSProviderService::EnableRemoteService(const std::string &serverAddress) { NS_LOG(DEBUG, "EnableRemoteService - IN"); - Result result = Result::ERROR; + NSResult result = NSResult::ERROR; #ifdef WITH_CLOUD - result = (Result) NSProviderEnableRemoteService(OICStrdup(serverAddress.c_str())); + result = (NSResult) NSProviderEnableRemoteService(OICStrdup(serverAddress.c_str())); #else NS_LOG(ERROR, "Remote Services feature is not enabled in the Build"); #endif @@ -115,12 +115,12 @@ namespace OIC return result; } - Result NSProviderService::DisableRemoteService(const std::string &serverAddress) + NSResult NSProviderService::DisableRemoteService(const std::string &serverAddress) { NS_LOG(DEBUG, "DisableRemoteService - IN"); - Result result = Result::ERROR; + NSResult result = NSResult::ERROR; #ifdef WITH_CLOUD - result = (Result) NSProviderDisableRemoteService(OICStrdup(serverAddress.c_str())); + result = (NSResult) NSProviderDisableRemoteService(OICStrdup(serverAddress.c_str())); #else NS_LOG(ERROR, "Remote Services feature is not enabled in the Build"); #endif @@ -128,12 +128,12 @@ namespace OIC return result; } - Result NSProviderService::SendMessage(NSMessage *msg) + NSResult NSProviderService::SendMessage(NSMessage *msg) { NS_LOG(DEBUG, "SendMessage - IN"); - Result result = Result::ERROR; + NSResult result = NSResult::ERROR; if (msg != nullptr) - result = (Result) NSSendMessage(getNSMessage(msg)); + result = (NSResult) NSSendMessage(getNSMessage(msg)); else NS_LOG(DEBUG, "Empty Message"); NS_LOG(DEBUG, "SendMessage - OUT");