From: jyong2.kim Date: Thu, 16 Jul 2015 08:43:06 +0000 (+0900) Subject: Refactoring Notification Manager using Resource-manipulation API. X-Git-Tag: 0.9.2-RC1^2~27 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d262468cda1393fade284435c67bd2903600c6de;p=contrib%2Fiotivity.git Refactoring Notification Manager using Resource-manipulation API. This commit is draft of Resource Hosting. ignore to build manager in service/SConscript. Change-Id: Iee707c5432da8c05246551328f131b575d6f206a Signed-off-by: jyong2.kim Reviewed-on: https://gerrit.iotivity.org/gerrit/1691 Reviewed-by: Uze Choi Tested-by: Uze Choi --- diff --git a/service/SConscript b/service/SConscript index 656a198..40f9c7d 100644 --- a/service/SConscript +++ b/service/SConscript @@ -39,7 +39,7 @@ if target_os not in ['arduino','darwin','ios']: SConscript('protocol-plugin/SConscript') # Build notification manager project - SConscript('notification-manager/SConscript') + #SConscript('notification-manager/SConscript') # Build resource-manipulation project #SConscript('resource-manipulation/SConscript') diff --git a/service/notification-manager/NotificationManager/src/HostingObject.cpp b/service/notification-manager/NotificationManager/src/HostingObject.cpp new file mode 100644 index 0000000..46f7f2c --- /dev/null +++ b/service/notification-manager/NotificationManager/src/HostingObject.cpp @@ -0,0 +1,220 @@ +//****************************************************************** +// +// Copyright 2015 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "HostingObject.h" + +namespace OIC +{ +namespace Service +{ + +HostingObject::RemoteObjectPtr HostingObject::getRemoteResource() const +{ + return remoteObject; +} + +void HostingObject::initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB) +{ + mirroredServer = nullptr; + remoteState = ResourceState::NOT_WATCHING; + + remoteObject = rResource; + + pStateChangedCB = std::bind(&HostingObject::stateChangedCB, this, + std::placeholders::_1, remoteObject); + pDataUpdateCB = std::bind(&HostingObject::dataChangedCB, this, + std::placeholders::_1, remoteObject); + pDestroyCB = destroyCB; + + pSetRequestHandler = std::bind(&HostingObject::setRequestHandler, this, + std::placeholders::_1, std::placeholders::_2); + + try + { + remoteObject->startWatching(pStateChangedCB); + remoteObject->startCaching(pDataUpdateCB); + }catch(InvalidParameterException &e) + { + std::cout << e.what() << std::endl; + } +} + +void HostingObject::destroyHostingObject() +{ + pDestroyCB(); +} + +void HostingObject::stateChangedCB(ResourceState state, RemoteObjectPtr rObject) +{ + remoteState = state; + + switch (state) + { + case ResourceState::ALIVE: + { + if(rObject->isCaching() == false) + { + try + { + rObject->startCaching(pDataUpdateCB); + }catch(InvalidParameterException &e) + { + std::cout << e.what() << std::endl; + } + } + break; + } + case ResourceState::LOST_SIGNAL: + case ResourceState::DESTROYED: + { + if(rObject->isCaching() == true) + { + try + { + rObject->stopCaching(); + }catch(InvalidParameterException &e) + { + std::cout << e.what() << std::endl; + } + } + if(rObject->isWatching() == true) + { + try + { + std::cout << "stopWatching\n"; +// rObject->stopWatching(); + }catch(InvalidParameterException &e) + { + std::cout << e.what() << std::endl; + } + } + std::cout << "delete Mirrored Server\n"; + mirroredServer = nullptr; + std::cout << "delete Mirrored Server Done\n"; + destroyHostingObject(); + break; + } + default: + // not support of state + break; + } +} + +void HostingObject::dataChangedCB(const ResourceAttributes & attributes, RemoteObjectPtr rObject) +{ + if(attributes.empty()) + { + return; + } + + if(mirroredServer == nullptr) + { + try + { + mirroredServer = createMirroredServer(rObject); + }catch(...) + { + mirroredServer = nullptr; + return; + } + } + + ResourceAttributes rData; + { + ResourceObject::LockGuard guard(mirroredServer); + rData = mirroredServer->getAttributes(); + } + if(rData.empty() || rData != attributes) + { + { + ResourceObject::LockGuard guard(mirroredServer); + for(auto it = rData.begin(); ; ++it) + { + if(it == rData.end()) + { + break; + } + mirroredServer->removeAttribute(it->key()); + } + + for(auto it = attributes.begin();; ++it) + { + if(it == attributes.end()) + { + break; + } + mirroredServer->setAttribute(it->key(), it->value()); + } + } + } +} + +HostingObject::ResourceObjectPtr HostingObject::createMirroredServer(RemoteObjectPtr rObject) +{ + ResourceObjectPtr retResource = nullptr; + if(rObject != nullptr) + { + std::string fulluri = rObject->getUri(); + std::string uri = fulluri.substr(0, fulluri.size()-8); + std::vector types = rObject->getTypes(); + std::vector interfaces = rObject->getInterfaces(); + try + { + std::string type = types.begin()->c_str(); + std::string interface = interfaces.begin()->c_str(); + retResource = ResourceObject::Builder(uri, type, interface). + setDiscoverable(true).setObservable(true).build(); + // TODO need to bind types and interfaces + retResource->setAutoNotifyPolicy(ResourceObject::AutoNotifyPolicy::UPDATED); + retResource->setSetRequestHandler(pSetRequestHandler); + }catch(PlatformException &e) + { + // TODO logging and throw + throw; + } + } + else + { + //TODO logging and throw + throw PlatformException(OC_STACK_ERROR); + } + + return retResource; +} + +PrimitiveSetResponse HostingObject::setRequestHandler(const PrimitiveRequest & primitiveRequest, + ResourceAttributes & resourceAttibutes) +{ + try + { + RequestObject newRequest; + newRequest.invokeRequest(remoteObject, RequestObject::RequestMethod::Setter, + primitiveRequest, resourceAttibutes); + }catch(...) + { + //TODO It is failed that setRequest send to remoteObject from mirrored server. + throw; + } + + return PrimitiveSetResponse::create(resourceAttibutes); +} + +} /* namespace Service */ +} /* namespace OIC */ diff --git a/service/notification-manager/NotificationManager/src/HostingObject.h b/service/notification-manager/NotificationManager/src/HostingObject.h new file mode 100644 index 0000000..661ef10 --- /dev/null +++ b/service/notification-manager/NotificationManager/src/HostingObject.h @@ -0,0 +1,86 @@ +//****************************************************************** +// +// Copyright 2015 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef RH_HOSTINGOBJECT_H_ +#define RH_HOSTINGOBJECT_H_ + +#include "ResourceClient.h" +#include "ResourceObject.h" +#include "RequestObject.h" +#include "ResourceBroker.h" +#include "ResourceCacheManager.h" +#include "PrimitiveResource.h" + +namespace OIC +{ +namespace Service +{ + +class HostingObject +{ +private: + using ResourceObjectPtr = std::shared_ptr; + using RemoteObjectPtr = std::shared_ptr; + using RequestObjectPtr = std::shared_ptr; + using PrimiteveResourcePtr = std::shared_ptr; + + using BrokerCallback = std::function; + using CacheCallback = std::function; + using DestroyedCallback = std::function; + + using SetRequestHandler = std::function; + +public: + HostingObject() = default; + ~HostingObject() = default; + + void initializeHostingObject(RemoteObjectPtr rResource, DestroyedCallback destroyCB); + + RemoteObjectPtr getRemoteResource() const; + +private: + RemoteObjectPtr remoteObject; + ResourceObjectPtr mirroredServer; + + ResourceState remoteState; + + BrokerCallback pStateChangedCB; + CacheCallback pDataUpdateCB; + DestroyedCallback pDestroyCB; + + SetRequestHandler pSetRequestHandler; + + ResourceObjectPtr createMirroredServer(RemoteObjectPtr rObject); + + void stateChangedCB(ResourceState state, RemoteObjectPtr rObject); + void dataChangedCB(const ResourceAttributes & attributes, RemoteObjectPtr rObject); + + PrimitiveSetResponse setRequestHandler( + const PrimitiveRequest & request, ResourceAttributes & attributes); + + void destroyHostingObject(); + +}; + +} /* namespace Service */ +} /* namespace OIC */ + +#endif /* RH_HOSTINGOBJECT_H_ */ diff --git a/service/notification-manager/NotificationManager/src/RequestObject.cpp b/service/notification-manager/NotificationManager/src/RequestObject.cpp new file mode 100644 index 0000000..3591a24 --- /dev/null +++ b/service/notification-manager/NotificationManager/src/RequestObject.cpp @@ -0,0 +1,62 @@ +//****************************************************************** +// +// Copyright 2015 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "RequestObject.h" + +namespace OIC +{ +namespace Service +{ + +void RequestObject::invokeRequest(RemoteObjectPtr remoteObject, RequestMethod method, + const PrimitiveRequest & pRequest, ResourceAttributes & resourceAttibutes) +{ + try + { + switch (method) + { + case RequestMethod::Setter: + remoteObject->setRemoteAttributes(resourceAttibutes, + std::bind(&RequestObject::setRequestCB, this, + std::placeholders::_1, resourceAttibutes)); + break; + case RequestMethod::Getter: + case RequestMethod::Delete: + default: + // unknown type of method. + break; + } + }catch(...) + { + throw; + } +} + +void RequestObject::setRequestCB(const ResourceAttributes & returnedAttributes, + ResourceAttributes & putAttibutes) +{ + if(putAttibutes != returnedAttributes) + { + // TODO fail set attributes + } +} + +} /* namespace Service */ +} /* namespace OIC */ diff --git a/service/notification-manager/NotificationManager/src/RequestObject.h b/service/notification-manager/NotificationManager/src/RequestObject.h new file mode 100644 index 0000000..65ec23d --- /dev/null +++ b/service/notification-manager/NotificationManager/src/RequestObject.h @@ -0,0 +1,58 @@ +//****************************************************************** +// +// Copyright 2015 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef RH_REQUESTOBJECT_H_ +#define RH_REQUESTOBJECT_H_ + +#include "ResourceClient.h" +#include "ResourceObject.h" + +namespace OIC +{ +namespace Service +{ + +class RequestObject +{ +public: + using RemoteObjectPtr = std::shared_ptr; + + enum class RequestMethod + { + Getter = 0, + Setter, + Delete + }; + + RequestObject() = default; + ~RequestObject() = default; + + void invokeRequest(RemoteObjectPtr remoteObject, RequestMethod method, + const PrimitiveRequest & pRequest, ResourceAttributes & resourceAttibutes); + +private: + void setRequestCB(const ResourceAttributes & returnedAttributes, + ResourceAttributes & putAttibutes); +}; + +} /* namespace Service */ +} /* namespace OIC */ + +#endif /* RH_REQUESTOBJECT_H_ */ diff --git a/service/notification-manager/NotificationManager/src/ResourceHosting.cpp b/service/notification-manager/NotificationManager/src/ResourceHosting.cpp new file mode 100755 index 0000000..61a318c --- /dev/null +++ b/service/notification-manager/NotificationManager/src/ResourceHosting.cpp @@ -0,0 +1,235 @@ +//****************************************************************** +// +// Copyright 2015 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "ResourceHosting.h" + +#include "PresenceSubscriber.h" +#include "OCPlatform.h" + +namespace OIC +{ +namespace Service +{ + +#define HOSTING_TAG "/hosting" +#define HOSTING_TAG_SIZE ((size_t)8) + +ResourceHosting * ResourceHosting::s_instance(nullptr); +std::mutex ResourceHosting::s_mutexForCreation; + +ResourceHosting * ResourceHosting::getInstance() +{ + if (!s_instance) + { + s_mutexForCreation.lock(); + if (!s_instance) + { + s_instance = new ResourceHosting(); + s_instance->initializeResourceHosting(); + } + s_mutexForCreation.unlock(); + } + return s_instance; +} + +void ResourceHosting::startHosting() +{ + try + { + requestMulticastPresence(); + requestMulticastDiscovery(); + }catch(PlatformException &e) + { + throw; + }catch(InvalidParameterException &e) + { + throw; + } +} + +void ResourceHosting::stopHosting() +{ + // TODO clear list hostingObjectList + if(presenceHandle.isSubscribing()) + { + presenceHandle.unsubscribe(); + } + for(auto it : hostingObjectList) + { + it.reset(); + } +} + +void ResourceHosting::initializeResourceHosting() +{ + pPresenceCB = std::bind(&ResourceHosting::presenceHandler, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3); + pDiscoveryCB = std::bind(&ResourceHosting::discoverHandler, this, + std::placeholders::_1); + + discoveryManager = DiscoveryManager::getInstance(); +} + +void ResourceHosting::requestMulticastPresence() +{ + try + { + presenceHandle = PresenceSubscriber(std::string("coap://") + OC_MULTICAST_PREFIX, + OCConnectivityType::CT_DEFAULT, pPresenceCB); + }catch(...) + { + throw; + } +} + +void ResourceHosting::presenceHandler(OCStackResult ret, const unsigned int seq, + const std::string & address) +{ + switch(ret) + { + case OC_STACK_OK: + case OC_STACK_CONTINUE: + case OC_STACK_RESOURCE_CREATED: + { + // TODO start discovery + requestDiscovery(address); + break; + } + + case OC_STACK_RESOURCE_DELETED: + case OC_STACK_COMM_ERROR: + case OC_STACK_TIMEOUT: + case OC_STACK_PRESENCE_STOPPED: + case OC_STACK_PRESENCE_TIMEOUT: + case OC_STACK_PRESENCE_DO_NOT_HANDLE: + case OC_STACK_ERROR: + // TODO presence error + break; + + case OC_STACK_INVALID_URI: + case OC_STACK_INVALID_QUERY: + case OC_STACK_INVALID_IP: + case OC_STACK_INVALID_PORT: + case OC_STACK_INVALID_CALLBACK: + case OC_STACK_INVALID_METHOD: + case OC_STACK_INVALID_PARAM: + case OC_STACK_INVALID_OBSERVE_PARAM: + case OC_STACK_NO_MEMORY: + case OC_STACK_ADAPTER_NOT_ENABLED: + case OC_STACK_NOTIMPL: + case OC_STACK_NO_RESOURCE: + case OC_STACK_RESOURCE_ERROR: + case OC_STACK_SLOW_RESOURCE: + case OC_STACK_DUPLICATE_REQUEST: + case OC_STACK_NO_OBSERVERS: + case OC_STACK_OBSERVER_NOT_FOUND: + case OC_STACK_INVALID_OPTION: + case OC_STACK_VIRTUAL_DO_NOT_HANDLE: + case OC_STACK_MALFORMED_RESPONSE: + case OC_STACK_PERSISTENT_BUFFER_REQUIRED: + case OC_STACK_INVALID_REQUEST_HANDLE: + case OC_STACK_INVALID_DEVICE_INFO: + case OC_STACK_INVALID_JSON: + break; + default: + // TODO unknown presence result + break; + } +} + +void ResourceHosting::requestMulticastDiscovery() +{ + requestDiscovery(); +} +void ResourceHosting::requestDiscovery(std::string address) +{ + std::string host = address; + std::string uri = OC_MULTICAST_DISCOVERY_URI + std::string("?rt=Resource.Hosting"); + OCConnectivityType type = OCConnectivityType::CT_DEFAULT; + discoveryManager->discoverResource(host, uri, type, pDiscoveryCB); +} + +void ResourceHosting::discoverHandler(RemoteObjectPtr remoteResource) +{ + std::string discoverdUri = remoteResource->getUri(); + if(discoverdUri.compare( + discoverdUri.size()-HOSTING_TAG_SIZE, HOSTING_TAG_SIZE, HOSTING_TAG) != 0) + { + return; + } + + HostingObjectPtr foundHostingObject = findRemoteResource(remoteResource); + if(foundHostingObject == nullptr) + { + foundHostingObject.reset(new HostingObject()); + foundHostingObject->initializeHostingObject(remoteResource, + std::bind(&ResourceHosting::destroyedHostingObject, this, foundHostingObject)); + hostingObjectList.push_back(foundHostingObject); + } + else + { + // this resource registered + } +} + +ResourceHosting::HostingObjectPtr ResourceHosting::findRemoteResource( + RemoteObjectPtr remoteResource) +{ + HostingObjectPtr retObject = nullptr; + + for(auto it : hostingObjectList) + { + RemoteObjectPtr inListPtr = it->getRemoteResource(); + if(inListPtr != nullptr && isSameRemoteResource(inListPtr, remoteResource)) + { + retObject = it; + } + } + + return retObject; +} + +bool ResourceHosting::isSameRemoteResource( + RemoteObjectPtr remoteResource_1, RemoteObjectPtr remoteResource_2) +{ + bool ret = false; + if(remoteResource_1->getAddress() == remoteResource_2->getAddress() && +// remoteResource_1->getID() == remoteResource_2->getID() && + remoteResource_1->getUri() == remoteResource_2->getUri()) + { + ret = true; + } + return ret; +} + +void ResourceHosting::destroyedHostingObject(HostingObjectPtr destroyedPtr) +{ + std::list::iterator foundObjectIter + = std::find(hostingObjectList.begin(), hostingObjectList.end(), destroyedPtr); + + if(foundObjectIter != hostingObjectList.end()) + { + std::cout << "destroy hosting object.\n"; + hostingObjectList.erase(foundObjectIter); + } +} + +} /* namespace Service */ +} /* namespace OIC */ diff --git a/service/notification-manager/NotificationManager/src/ResourceHosting.h b/service/notification-manager/NotificationManager/src/ResourceHosting.h new file mode 100644 index 0000000..73822a8 --- /dev/null +++ b/service/notification-manager/NotificationManager/src/ResourceHosting.h @@ -0,0 +1,102 @@ +//****************************************************************** +// +// Copyright 2015 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#ifndef RH_RESOURCEHOSTING_H_ +#define RH_RESOURCEHOSTING_H_ + +#include +#include +#include +#include +#include +#include +#include + +#include "octypes.h" +#include "ResourceClient.h" +#include "PresenceSubscriber.h" +#include "HostingObject.h" +#include "PrimitiveResource.h" + +namespace OIC +{ +namespace Service +{ + +class ResourceHosting +{ +private: + using HostingObjectPtr = std::shared_ptr; + using RemoteObjectPtr = std::shared_ptr; + using PrimiteveResourcePtr = std::shared_ptr; + + using SubscribeCallback + = std::function; + using DiscoveryCallback + = std::function)>; + using DestroyedCallback + = std::function; + +public: + void startHosting(); + void stopHosting(); + + static ResourceHosting * getInstance(); + +private: + ResourceHosting() = default; + ~ResourceHosting() = default; + + ResourceHosting(const ResourceHosting&) = delete; + ResourceHosting(ResourceHosting&&) = delete; + ResourceHosting& operator=(const ResourceHosting&) const = delete; + ResourceHosting& operator=(ResourceHosting&&) const = delete; + + static ResourceHosting * s_instance; + static std::mutex s_mutexForCreation; + + std::list hostingObjectList; + + DiscoveryManager * discoveryManager; + PresenceSubscriber presenceHandle; + + SubscribeCallback pPresenceCB; + DiscoveryCallback pDiscoveryCB; + + void initializeResourceHosting(); + + void requestMulticastPresence(); + void requestMulticastDiscovery(); + void requestDiscovery(std::string address = std::string()); + + void presenceHandler(OCStackResult ret, const unsigned int seq, const std::string & address); + void discoverHandler(RemoteObjectPtr remoteResource); + + HostingObjectPtr findRemoteResource(RemoteObjectPtr remoteResource); + bool isSameRemoteResource(RemoteObjectPtr remoteResource_1, RemoteObjectPtr remoteResource_2); + + void destroyedHostingObject(HostingObjectPtr destroyedPtr); + +}; + +} /* namespace Service */ +} /* namespace OIC */ + +#endif /* RH_RESOURCEHOSTING_H_ */ diff --git a/service/notification-manager/NotificationManager/src/hosting.c b/service/notification-manager/NotificationManager/src/hosting.c deleted file mode 100644 index 059cf75..0000000 --- a/service/notification-manager/NotificationManager/src/hosting.c +++ /dev/null @@ -1,1545 +0,0 @@ -//****************************************************************** -// -// Copyright 2015 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. -// -//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - -// Hosting Header -#include "hosting.h" -#include "virtualResource.h" - -// External Lib -#include "cJSON.h" - -/* - * internal function & static variable - */ -/////////////////////////////////////////////////////////////////////////////////////////////////// -static MirrorResourceList *s_mirrorResourceList = NULL; -static RequestHandleList *s_requestHandleList = NULL; - -#define OIC_COORDINATING_FLAG "/hosting" -#define OIC_STRING_MAX_VALUE 100 - -#define OC_DEFAULT_ADDRESS "224.0.1.187" -#define OC_WELL_KNOWN_COORDINATING_QUERY "coap://224.0.1.187:5683/oc/core?rt=Resource.Hosting" -#define OC_COORDINATING_QUERY "/oc/core?rt=Resource.Hosting" -#define DEFAULT_CONTEXT_VALUE 0x99 - -/* - * Presence Func for hosting - */ - -/** - * - * request presence for coordinating - * - * @param[in] originResourceAddr - pointer of address string of original resource - * - * @return - * OC_STACK_OK - * OC_STACK_ERROR - */ -OCStackResult requestPresence(char *originResourceAddr); - -/** - * - * callback function that call when response of presence request received - * - * @param[in] originResourceAddr - pointer of address string of original resource - * - * @return - * OC_STACK_OK - * OC_STACK_ERROR - */ -OCStackApplicationResult requestPresenceCB(void *context, OCDoHandle handle, - OCClientResponse *clientResponse); - -/** - * - * build mirror resource list by clientResponse - * - * @param[in] handle - not using... - * @param[in] clientResponse - client response that mirror resources are stored - * - * @return - * pointer of result MirrorResourceList - */ -MirrorResourceList *buildMirrorResourceList(OCDoHandle handle, OCClientResponse *clientResponse); - -/** - * - * build mirror resource by JSON payload - * - * @param[in] ocArray_sub - pointer of json payload string - * - * @return - * pointer of result MirrorResource - */ -MirrorResource *buildMirrorResource(cJSON *ocArray_sub); - -/** - * - * This method is used when setting queryUri, registering callback function and starting OCDoResource() Function in order to find Coordinatee Candidate - * - * @brief discover coordinatee candidate - * - * @return - * OC_STACK_OK - no errors - * OC_STACK_INVALID_CALLBACK - invalid callback function pointer - * OC_STACK_INVALID_METHOD - invalid resource method - * OC_STACK_INVALID_URI - invalid required or reference URI - * OC_STACK_INVALID_QUERY - number of resource types specified for filtering presence - * notifications exceeds @ref MAX_PRESENCE_FILTERS. - * OC_STACK_ERROR - otherwise error(initialized value) - */ -int requestCoordinateeCandidateDiscovery(char *address); - -/** - * - * This method is used to add a coordinator resource callback method in mirrorResourceList when host resource discovered. - * - * @param[in] context - * Context for callback method - * @param[in] handle - * Handle to an @ref OCDoResource invocation. - * @param[in] clientResponse - * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API. - * - * @brief callback for receiving response of discoverCoordinateeCandidate() - * - * @return - * PRINT("Callback Context for DISCOVER query recvd successfully") - context is DEFAULT_CONTEXT_VALUE - * call the buildMirrorResource() method - clientResponse is not NULL && clientResponse->result is OC_STACK_OK - * OC_STACK_KEEP_TRANSACTION - otherwise case - */ -OCStackApplicationResult requestCoordinateeCandidateDiscoveryCB(void *context, OCDoHandle handle, - OCClientResponse *clientResponse); - -/** - * - * This method is used when setting queryUri, registering callback function and starting OCDoResource() Function in order to request resource coordination - * - * @brief - * - * @param[in] mirrorResource - * mirrorResource for using in order to request resource coordination - * - * @return - * OC_STACK_OK - no errors - * OC_STACK_INVALID_CALLBACK - invalid callback function pointer - * OC_STACK_INVALID_METHOD - invalid resource method - * OC_STACK_INVALID_URI - invalid required or reference URI - * OC_STACK_INVALID_QUERY - number of resource types specified for filtering presence - * notifications exceeds @ref MAX_PRESENCE_FILTERS. - * OC_STACK_ERROR - otherwise error(initialized value) - */ -OCStackResult requestResourceObservation(MirrorResource *mirrorResource); - -/** - * - * This method is used to handle callback of requestCoordination method. - * - * @param[in] context - * Context for callback method - * @param[in] handle - * Handle to update mirror resource and check errorResponse - * @param[in] clientResponse - * Response from queries to remote servers. Queries are made by calling the @ref OCDoResource API. - * - * @brief callback when receiving response of coordinating requestion. - * - * @todo diverge return value - * - * @return - * - * OC_STACK_KEEP_TRANSACTION - otherwise case - */ -OCStackApplicationResult requestResourceObservationCB(void *context, OCDoHandle handle, - OCClientResponse *clientResponse); - -/** - * - * This method is used to check resource validation and delete resource if it is not exist(not alive). - * - * @brief check mirror resource is alive - * - * @param[in] requestHandle - * Handle to check mirror resource - * - * @return - * - * OC_STACK_DELETE_TRANSACTION - otherwise case - */ -OCStackApplicationResult checkResourceValidation(OCDoHandle requestHandle); - -/** - * - * register Mirror resource in the base resource list - * - * @param[in] requestHandle - * Handle to check mirror resource - * - * @return - * OC_STACK_OK - * OC_STACK_ERROR - */ -OCStackResult registerMirrorResource(MirrorResource *node); - -/** - * - * update resource - * - * @param[in] sourceHandle - handle of source resource - * @param[in] payload - pointer of json payload string that update items stored - * - * @return - * pointer of mirror resource. return NULL if there is any error. - */ -MirrorResource *updateMirrorResource(OCDoHandle sourceHandle, const char *payload); - -/** - * - * build response payload - * - * @param[in] ehRequest - pointer of handler of entity handler request that to be responded - * - * @return - * OC_STACK_OK - * OC_STACK_ERROR - */ -char *buildResponsePayload (OCEntityHandlerRequest *ehRequest); - -/** - * - * handle "Get" request - * - * @param[in] ehRequest - pointer of handler of entity handler request - * @param[out] payload - pointer of payload to be responded - * @param[in] maxPayloadSize - size of payload - * - * @return - * OC_EH_OK - success to copy response payload - * OC_EH_ERROR - error to copy response payload - */ -OCEntityHandlerResult handleGetRequest (OCEntityHandlerRequest *ehRequest, - char *payload, uint16_t maxPayloadSize); - -/** - * - * handle request for non-existing resource - * - * @param[in] ehRequest - pointer of handler of entity handler request - * @param[out] payload - pointer of payload to be responded - * @param[in] maxPayloadSize - size of payload - * - * @return - * OC_EH_RESOURCE_DELETED - resource deleted - */ -OCEntityHandlerResult handleNonExistingResourceRequest(OCEntityHandlerRequest *ehRequest, - char *payload, uint16_t maxPayloadSize); - -/** - * - * callback function that called when source resource changed - * - * @param[in] flag - entity handler flag - * @param[in] entityHandlerRequest - pointer of entity handler request - * - * @return - * OC_EH_OK - * OC_EH_ERROR - */ -OCEntityHandlerResult resourceEntityHandlerCB (OCEntityHandlerFlag flag, - OCEntityHandlerRequest *entityHandlerRequest, - void *callbackParam); - -/** - * - * request that address is alive - * - * @param[in] address - pointer of address string - * - * @return - * OC_STACK_OK - * OC_STACK_ERROR - */ -OCStackResult requestIsAlive(const char *address); - -/** - * - * get string value of OCStackResult code - * - * @param[in] result - OCStringResult code - * - * @return - * pointer of result string value - */ -const char *getResultString(OCStackResult result); - -OCStackResult requestQuery(RequestHandle *request, OCMethod method, - const char *queryAddress, const char *queryUri); -OCStackApplicationResult requestQueryCB(void *context, OCDoHandle handle, - OCClientResponse *clientResponse); -OCEntityHandlerResponse buildEntityHandlerResponse(OCEntityHandlerRequest *entityHandlerRequest, - const char *clientPayload); -OCEntityHandlerResult handleRequestPayload (OCEntityHandlerRequest *entityHandlerRequest, - char *payload, uint16_t maxPayloadSize); - -/* - * for Lite Device Side - */ - -/** - * - * register resource as coordinatable - * - * @param[in] handle - resource handle - * @param[in] resourceTypeName - resource type name - * @param[in] resourceInterfaceName - resource interface name - * @param[in] resourceUri - resource URI - * @param[in] entityHandler - entity handler - * @param[in] resourceProperties - resource properties - * - * @return - * pointer of result string value - */ -OCStackResult registerResourceAsCoordinatable(OCResourceHandle *handle, - const char *resourceTypeName, const char *resourceInterfaceName, - const char *resourceUri, OCEntityHandler entityHandler, uint8_t resourceProperties); - -OCStackResult registerResourceAsCoordinatable(OCResourceHandle *handle, - const char *resourceTypeName, - const char *resourceInterfaceName, - const char *resourceUri, - OCEntityHandler entityHandler, - uint8_t resourceProperties) -{ - OCStackResult ret = OC_STACK_OK; - size_t coordinateUriLen = sizeof(char) * (strlen(resourceUri) + - strlen(OIC_COORDINATING_FLAG) + 1); - char *coordinatingURI = (char *)malloc(coordinateUriLen); - if(coordinatingURI == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "memory alloc fail : coordinatingURI"); - return OC_STACK_NO_MEMORY; - } - - snprintf(coordinatingURI, coordinateUriLen,"%s%s", resourceUri, OIC_COORDINATING_FLAG); - - OC_LOG_V(DEBUG, HOSTING_TAG, "requiredUri+coordinatingFlag = %s", coordinatingURI); - - ret = OCCreateResource(handle, resourceTypeName, resourceInterfaceName, - coordinatingURI, entityHandler, NULL, resourceProperties); - free(coordinatingURI); - return ret; -} - -/* - * for Hosting Device Side - */ -OCStackResult OICStartCoordinate() -{ - OCStackResult ret = OC_STACK_ERROR; - if (OCInit((char *) NULL, 0, OC_CLIENT_SERVER) != OC_STACK_OK) - { - OC_LOG(ERROR, HOSTING_TAG, PCF("OCStack init ERROR")); - } - else - { - s_mirrorResourceList = createMirrorResourceList(); - s_requestHandleList = createRequestHandleList(); - ret = requestPresence(OC_MULTICAST_PREFIX); - } - - return ret; -} - -OCStackResult OICStopCoordinate() -{ - OCStackResult result = OC_STACK_ERROR; - - if (OCStop() == OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "OCStack Stop OK"); - } - - result = destroyMirrorResourceList(s_mirrorResourceList); - s_mirrorResourceList = NULL; - if(result != OC_STACK_OK) - { - return OC_STACK_ERROR; - } - - return result; -} - -int requestCoordinateeCandidateDiscovery(char *sourceResourceAddress) -{ - OCStackResult result; - OCCallbackData cbData; - OCDoHandle handle; - - /* Start a discovery query*/ - char queryUri[OIC_STRING_MAX_VALUE] = { '\0' }; - if (sourceResourceAddress == NULL) - { - strncpy(queryUri, OC_WELL_KNOWN_COORDINATING_QUERY, sizeof(queryUri)); - } - else - { - snprintf(queryUri, sizeof(queryUri), "coap://%s%s", - sourceResourceAddress , OC_COORDINATING_QUERY); - } - - cbData.cb = requestCoordinateeCandidateDiscoveryCB; - cbData.context = (void *)DEFAULT_CONTEXT_VALUE; - cbData.cd = NULL; - - result = OCDoResource(&handle, OC_REST_GET, queryUri, NULL, 0, - OC_TRANSPORT, OC_LOW_QOS, &cbData, NULL, 0); - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "OCStack resource error"); - } - else - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Host Resource Finding..."); - } - return result; -} - -OCStackResult requestPresence(char *sourceResourceAddress) -{ - OCStackResult result = OC_STACK_ERROR; - OCCallbackData cbData; - OCDoHandle handle; - - if (sourceResourceAddress == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "SourceResourceAddress is not available."); - return result; - } - - cbData.cb = requestPresenceCB; - cbData.context = (void *)DEFAULT_CONTEXT_VALUE; - cbData.cd = NULL; - - char queryUri[OIC_STRING_MAX_VALUE] = { '\0' }; - snprintf(queryUri, sizeof(queryUri), "coap://%s%s", sourceResourceAddress , OC_RSRVD_PRESENCE_URI); - OC_LOG_V(DEBUG, HOSTING_TAG, "initializePresenceForCoordinating Query : %s", queryUri); - - result = OCDoResource(&handle, OC_REST_PRESENCE, queryUri, 0, 0, - OC_TRANSPORT, OC_LOW_QOS, &cbData, NULL, 0); - - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "initializePresenceForCoordinating error"); - result = OC_STACK_ERROR; - } - else - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Success initializePresenceForCoordinating"); - } - - return result; -} - -OCStackApplicationResult requestPresenceCB(void *context, OCDoHandle handle, - OCClientResponse *clientResponse) -{ - char address[OIC_STRING_MAX_VALUE] = { '\0' }; - - if (context == (void *) DEFAULT_CONTEXT_VALUE) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "\tCallback Context for presence CB recv successfully"); - } - if (clientResponse) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "\tStackResult: %s", getResultString(clientResponse->result)); - OC_LOG_V(DEBUG, HOSTING_TAG, "\tStackResult: %d", clientResponse->result); - //OC_LOG_V(DEBUG, HOSTING_TAG, - // "\tPresence Device =============> Presence %s @ %s:%d", - // clientResponse->resJSONPayload, - // clientResponse->devAddr.addr, - // clientResponse->devAddr.port); - - snprintf(address, sizeof(address), "%s:%d", - clientResponse->devAddr.addr, - clientResponse->devAddr.port); - if (clientResponse->result == OC_STACK_OK) - { - requestCoordinateeCandidateDiscovery(address); - } - if (clientResponse->result == OC_STACK_PRESENCE_STOPPED - || clientResponse->result == OC_STACK_PRESENCE_TIMEOUT - || clientResponse->result == OC_STACK_PRESENCE_DO_NOT_HANDLE) - { - requestIsAlive(address); - } - - } - return OC_STACK_KEEP_TRANSACTION; -} - -OCStackApplicationResult requestCoordinateeCandidateDiscoveryCB(void *ctx, OCDoHandle handle, - OCClientResponse *clientResponse) -{ - OC_LOG(DEBUG, HOSTING_TAG, "Found Host Resource"); - OCStackResult ret = OC_STACK_DELETE_TRANSACTION; - - if (ctx == (void *) DEFAULT_CONTEXT_VALUE) - { - OC_LOG(DEBUG, HOSTING_TAG, "Callback Context for DISCOVER query recvd successfully"); - } - if (clientResponse && clientResponse->result == OC_STACK_OK) - { - MirrorResourceList *vList = buildMirrorResourceList(handle, clientResponse); - if (vList != NULL) - { - - if (vList->headerNode == NULL) - { - OC_LOG(DEBUG, HOSTING_TAG, "This Discover Response is empty"); - destroyMirrorResourceList(vList); - return ret; - } - - // register All of VirtualResource - while (vList->headerNode) - { - MirrorResource *mirrorResource = vList->headerNode; - ret = ejectMirrorResource(vList, mirrorResource); - mirrorResource->next = NULL; - OC_LOG_V(DEBUG, HOSTING_TAG, - "register virtual resource uri : %s", mirrorResource->uri); - if (ret != OC_STACK_OK) - { - continue; - } - - ret = registerMirrorResource(mirrorResource); - if (ret != OC_STACK_OK) - { - continue; - } - - ret = insertMirrorResource(s_mirrorResourceList, mirrorResource); - if (ret != OC_STACK_OK) - { - OCDeleteResource(mirrorResource->resourceHandle[OIC_MIRROR_HANDLE]); - continue; - } - printMirrorResourceList(s_mirrorResourceList); - - ret = requestResourceObservation(mirrorResource); - if (ret != OC_STACK_OK) - { - OCDeleteResource(mirrorResource->resourceHandle[OIC_MIRROR_HANDLE]); - deleteMirrorResourceFromList(s_mirrorResourceList, mirrorResource); - continue; - } - } - destroyMirrorResourceList(vList); - if (ret != OC_STACK_OK) - { - return ret; - } - } - ret = OC_STACK_KEEP_TRANSACTION; - } - return ret; -} - -MirrorResourceList *buildMirrorResourceList(OCDoHandle handle, OCClientResponse *clientResponse) -{ - - cJSON *discoveryJson = cJSON_CreateObject(); - //discoveryJson = cJSON_Parse((char *)clientResponse->resJSONPayload); - - cJSON *ocArray = cJSON_GetObjectItem(discoveryJson, "oc"); - char *ocArray_str = cJSON_PrintUnformatted(ocArray); - - if ( strstr(ocArray_str, "[{}") == ocArray_str ) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "invalid payload : %s", ocArray_str); - cJSON_Delete(discoveryJson); - return NULL; - } - - MirrorResourceList *retList = createMirrorResourceList(); - - char sourceaddr[OIC_STRING_MAX_VALUE] = {'\0'}; - snprintf(sourceaddr, sizeof(sourceaddr), "%s:%d", - clientResponse->devAddr.addr, - clientResponse->devAddr.port); - - //OC_LOG_V(DEBUG, HOSTING_TAG, "Host Device =============> Discovered %s @ %s", - // clientResponse->resJSONPayload, sourceaddr); - - int arraySize = cJSON_GetArraySize(ocArray); - for (int i = 0; i < arraySize; ++i) - { - cJSON *ocArray_sub = cJSON_GetArrayItem(ocArray, i); - MirrorResource *mirrorResource = buildMirrorResource(ocArray_sub); - - if (mirrorResource == NULL) - { - continue; - } - mirrorResource->address[OIC_SOURCE_ADDRESS] = - (char *)malloc(sizeof(char) * OIC_STRING_MAX_VALUE); - if(mirrorResource->address[OIC_SOURCE_ADDRESS] == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "memory alloc fail : mirrorResource address_source"); - destroyMirrorResource(mirrorResource); - continue; - } - snprintf(mirrorResource->address[OIC_SOURCE_ADDRESS], - sizeof(char) * OIC_STRING_MAX_VALUE, "%s", sourceaddr); - - mirrorResource->address[OIC_MIRROR_ADDRESS] = - (char *)malloc(sizeof(char) * OIC_STRING_MAX_VALUE); - if(mirrorResource->address[OIC_MIRROR_ADDRESS] == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "memory alloc fail : mirrorResource address_mirror"); - destroyMirrorResource(mirrorResource); - continue; - } - snprintf(mirrorResource->address[OIC_MIRROR_ADDRESS], - sizeof(char) * OIC_STRING_MAX_VALUE, "0.0.0.0:00"); - - if (OC_STACK_OK != insertMirrorResource(retList, mirrorResource)) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "buildVirtualResourceList : insert resource fail"); - destroyMirrorResource(mirrorResource); - } - } - - cJSON_Delete(discoveryJson); - return retList; -} - -MirrorResource *buildMirrorResource(cJSON *ocArray_sub) -{ - MirrorResource *mirrorResource = NULL; - const char *curValuestring = cJSON_GetObjectItem(ocArray_sub, "href")->valuestring; - - if ( strstr(curValuestring, OIC_COORDINATING_FLAG) ) - { - mirrorResource = createMirrorResource(); - if(mirrorResource == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "memory alloc fail for mirrorResource"); - goto RET_ERROR; - } - - mirrorResource->uri = (char *)malloc(sizeof(char) * OIC_STRING_MAX_VALUE); - if(mirrorResource->uri == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "memory alloc fail for mirrorResource uri"); - goto RET_ERROR; - } - strncpy(mirrorResource->uri, curValuestring, strlen(curValuestring) - strlen(OIC_COORDINATING_FLAG)); - mirrorResource->uri[strlen(curValuestring) - strlen(OIC_COORDINATING_FLAG)] = '\0'; - OC_LOG_V(DEBUG, HOSTING_TAG, "VirtualResource URI : %s", mirrorResource->uri); - - cJSON *inArray_sub = cJSON_GetObjectItem(ocArray_sub, "prop"); - - cJSON *tmpJSON = NULL; - int sizetemp = 0; - - tmpJSON = cJSON_GetObjectItem(inArray_sub, "rt"); - sizetemp = cJSON_GetArraySize(tmpJSON); - mirrorResource->prop.countResourceType = sizetemp; - mirrorResource->prop.resourceType = (char **)malloc(sizeof(char *)*sizetemp); - if (mirrorResource->prop.resourceType == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, - "memory alloc fail for mirrorResource number of resourceType"); - goto RET_ERROR; - } - else - { - for (int k = 0; k < sizetemp; ++k) - { - mirrorResource->prop.resourceType[k] = - (char *)malloc(sizeof(char) * OIC_STRING_MAX_VALUE); - if (mirrorResource->prop.resourceType[k] == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, - "memory alloc fail for mirrorResource resourceType[n]"); - goto RET_ERROR; - } - memset(mirrorResource->prop.resourceType[k], '\0', OIC_STRING_MAX_VALUE); - strncpy(mirrorResource->prop.resourceType[k], - cJSON_GetArrayItem(tmpJSON, k)->valuestring, - sizeof(char) * OIC_STRING_MAX_VALUE); - } - } - - tmpJSON = cJSON_GetObjectItem(inArray_sub, "if"); - sizetemp = cJSON_GetArraySize(tmpJSON); - mirrorResource->prop.countInterface = sizetemp; - mirrorResource->prop.resourceInterfaceName = (char **)malloc(sizeof(char *)*sizetemp); - if (mirrorResource->prop.resourceInterfaceName == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, - "memory alloc fail for mirrorResource number of resourceInterfaceName"); - goto RET_ERROR; - } - - for (int k = 0; k < sizetemp; ++k) - { - mirrorResource->prop.resourceInterfaceName[k] = - (char *)malloc(sizeof(char) * OIC_STRING_MAX_VALUE); - if (mirrorResource->prop.resourceInterfaceName[k] == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, - "memory alloc fail for mirrorResource resourceInterfaceName[n]"); - goto RET_ERROR; - } - - memset(mirrorResource->prop.resourceInterfaceName[k], '\0', OIC_STRING_MAX_VALUE); - strncpy(mirrorResource->prop.resourceInterfaceName[k], - cJSON_GetArrayItem(tmpJSON, k)->valuestring, - sizeof(char) * OIC_STRING_MAX_VALUE); - } - } - - return mirrorResource; - -RET_ERROR: - destroyMirrorResource(mirrorResource); - return NULL; -} - -OCStackResult registerMirrorResource(MirrorResource *mirrorResource) -{ - OCStackResult result = OC_STACK_ERROR; - - MirrorResource *foundMirrorResource = findMirrorResourceUsingAddressAndURI(s_mirrorResourceList, - mirrorResource->address[OIC_MIRROR_ADDRESS], OIC_MIRROR_ADDRESS, mirrorResource->uri); - if (foundMirrorResource != NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Already registered resource"); - goto RETURN_ERR; - } - - result = OCCreateResource(&(mirrorResource->resourceHandle[OIC_MIRROR_HANDLE]), - mirrorResource->prop.resourceType[0], - mirrorResource->prop.resourceInterfaceName[0], - mirrorResource->uri, - resourceEntityHandlerCB, - NULL, - OC_DISCOVERABLE | OC_OBSERVABLE); - - OC_LOG_V(DEBUG, HOSTING_TAG, "created mirror resource Handle : %u",mirrorResource->resourceHandle[OIC_MIRROR_HANDLE]); - - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "error return = %s", getResultString(result)); - mirrorResource->next = NULL; - destroyMirrorResource(mirrorResource); - return result; - } - - if (mirrorResource->prop.countResourceType > 1) - { - int i = 0; - for (i = 1; i < mirrorResource->prop.countResourceType; ++i) - { - result = OCBindResourceTypeToResource( - mirrorResource->resourceHandle[OIC_MIRROR_HANDLE], - mirrorResource->prop.resourceType[i]); - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Virtual Resource Registration Fail : BindResourceType"); - goto RETURN_ERR; - } - } - } - - if (mirrorResource->prop.countInterface > 1) - { - int i = 0; - for (i = 1; i < mirrorResource->prop.countInterface; ++i) - { - result = OCBindResourceInterfaceToResource( - mirrorResource->resourceHandle[OIC_MIRROR_HANDLE], - mirrorResource->prop.resourceInterfaceName[i]); - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, - "Virtual Resource Registration Fail : BindResourceInterfaceName"); - goto RETURN_ERR; - } - } - } - - OC_LOG_V(DEBUG, HOSTING_TAG, "Mirror Resource Registration Success"); - OC_LOG_V(DEBUG, HOSTING_TAG, "Mirror Resource uri : %s", mirrorResource->uri); - OC_LOG_V(DEBUG, HOSTING_TAG, "Mirror Resource source address : %s", - mirrorResource->address[OIC_SOURCE_ADDRESS]); - OC_LOG_V(DEBUG, HOSTING_TAG, "Mirror Resource virtual address : %s", - mirrorResource->address[OIC_MIRROR_ADDRESS]); - return result; - -RETURN_ERR: - OCDeleteResource(mirrorResource->resourceHandle[OIC_MIRROR_HANDLE]); - mirrorResource->next = NULL; - destroyMirrorResource(mirrorResource); - - return result; -} - -OCStackResult requestResourceObservation(MirrorResource *mirrorResource) -{ - OCStackResult result; - OCCallbackData cbData; - - cbData.cb = requestResourceObservationCB; - cbData.context = (void *)DEFAULT_CONTEXT_VALUE; - cbData.cd = NULL; - - char query[OIC_STRING_MAX_VALUE] = {'\0'}; - snprintf(query, sizeof(query), "coap://%s%s%s", mirrorResource->address[OIC_SOURCE_ADDRESS], mirrorResource->uri, - OIC_COORDINATING_FLAG); - - result = OCDoResource(&mirrorResource->resourceHandle[OIC_REQUEST_HANDLE], OC_REST_OBSERVE, query, - 0, NULL, OC_TRANSPORT, - OC_HIGH_QOS, &cbData, NULL, 0); - - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "OCDoResource returns error %s with method %d", - getResultString(result), OC_REST_OBSERVE); - } - - return result; -} - -OCStackApplicationResult requestResourceObservationCB(void *context, OCDoHandle handle, - OCClientResponse *clientResponse) -{ - OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION; - - if (context == (void *)DEFAULT_CONTEXT_VALUE) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Callback Context for OBS query recvd successfully"); - } - - if (clientResponse && clientResponse->result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "observeCB result error = %s", - getResultString(clientResponse->result)); - return checkResourceValidation(handle); - } - - if (clientResponse && clientResponse->result == OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, - "<=============Callback Context for OBSERVE notification recvd successfully"); - OC_LOG_V(DEBUG, HOSTING_TAG, "SEQUENCE NUMBER: %d", clientResponse->sequenceNumber); - //OC_LOG_V(DEBUG, HOSTING_TAG, "JSON = %s =============> Obs Response", - // clientResponse->resJSONPayload); - - MirrorResource *foundMirrorResource = NULL;//updateMirrorResource(handle, clientResponse->resJSONPayload); - if (foundMirrorResource == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Cannot found Mirror Resource : Fail"); - return ret; - } - - if ( OC_STACK_OK != OCNotifyAllObservers(foundMirrorResource->resourceHandle[OIC_MIRROR_HANDLE], - OC_HIGH_QOS) ) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Notify Mirror Resource's Subscriber : Fail"); - } - else - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Notify Mirror Resource's Subscriber : Success"); - } - - if (clientResponse->sequenceNumber == OC_OBSERVE_REGISTER) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "This also serves as a registration confirmation"); - } - else if (clientResponse->sequenceNumber == OC_OBSERVE_DEREGISTER) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "This also serves as a deregistration confirmation"); - return ret; - } - else if (clientResponse->sequenceNumber == OC_OBSERVE_NO_OPTION) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "This also tells you that registration/deregistration failed"); - return ret; - } - ret = OC_STACK_KEEP_TRANSACTION; - } - return ret; -} - -OCStackApplicationResult checkResourceValidation(OCDoHandle handle) -{ - OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION; - - RequestHandle *foundRequestHandle = findRequestHandle(s_requestHandleList, handle, - OIC_REQUEST_BY_COORDINATOR); - - if (foundRequestHandle == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Not found any request."); - return ret; - } - - if (foundRequestHandle->isAliveCheck) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "This response is Alive Check : Expired resource"); - OCDeleteResource(foundRequestHandle->requestHandle[OIC_REQUEST_BY_CLIENT]); - } - deleteRequestHandleFromList(s_requestHandleList, foundRequestHandle); - return ret; -} - -MirrorResource *updateMirrorResource(OCDoHandle handle, const char *payload) -{ - MirrorResource *foundMirrorResource = findMirrorResourceUsingHandle( - s_mirrorResourceList, handle, OIC_REQUEST_HANDLE); - - if (!foundMirrorResource) - { - // TODO - OC_LOG_V(DEBUG, HOSTING_TAG, "Cannot found Mirror Resource. In updateMirrorResource"); - return NULL; - } - - cJSON *repData = NULL; - cJSON *observeJson = cJSON_Parse(payload); - - if (observeJson) - { - cJSON *ocArray = cJSON_GetObjectItem(observeJson, "oc"); - cJSON *ocArray_sub = cJSON_GetArrayItem(ocArray, 0); - cJSON *tempData = cJSON_GetObjectItem(ocArray_sub, "rep"); - char *temp = cJSON_PrintUnformatted(tempData); - - repData = cJSON_Parse(temp); - if (temp != NULL) - { - free(temp); - } - cJSON_Delete(observeJson); - } - else - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Mirror resource payload is not correct"); - return NULL; - } - - if (foundMirrorResource->rep) - { - cJSON_Delete(foundMirrorResource->rep); - foundMirrorResource->rep = NULL; - } - foundMirrorResource->rep = repData; - - cJSON *json = cJSON_CreateObject(); - - char nodeData[OIC_STRING_MAX_VALUE] = {'\0'}; - snprintf(nodeData, sizeof(nodeData), "%s", foundMirrorResource->uri); - cJSON_AddStringToObject(json, "href", nodeData); - - cJSON *nodeRep = cJSON_Parse(cJSON_PrintUnformatted(foundMirrorResource->rep)); - cJSON_AddItemToObject(json, "rep", nodeRep); - OC_LOG_V(DEBUG, HOSTING_TAG, "It will notify resource : %s", cJSON_PrintUnformatted(json)); - - cJSON_Delete(json); - - return foundMirrorResource; -} - -char *buildResponsePayload (OCEntityHandlerRequest *entityHandlerRequest) -{ - MirrorResource *mirrorResource = findMirrorResourceUsingHandle(s_mirrorResourceList, - entityHandlerRequest->resource, OIC_MIRROR_HANDLE); - if (!mirrorResource) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Cannot found Mirror Resource. In buildResponsePayload()"); - OC_LOG_V(DEBUG, HOSTING_TAG, "Mirror Resource's Handle : %x.", entityHandlerRequest->resource); - return NULL; - } - - if (entityHandlerRequest->method == OC_REST_PUT) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "oc_rest_put"); - if (mirrorResource->rep) - { - cJSON_Delete(mirrorResource->rep); - mirrorResource->rep = NULL; - } - mirrorResource->rep = cJSON_CreateObject(); - //mirrorResource->rep = cJSON_Parse(entityHandlerRequest->reqJSONPayload); - } - - OC_LOG_V(DEBUG, HOSTING_TAG, "node's uri : %s", mirrorResource->uri); - OC_LOG_V(DEBUG, HOSTING_TAG, "node's source address : %s", mirrorResource->address[0]); - OC_LOG_V(DEBUG, HOSTING_TAG, "node's mirror address : %s", mirrorResource->address[1]); - OC_LOG_V(DEBUG, HOSTING_TAG, "node's rep : %s", cJSON_PrintUnformatted(mirrorResource->rep)); - - cJSON *jsonObject = cJSON_CreateObject(); - - char uriString[OIC_STRING_MAX_VALUE] = {'\0'}; - snprintf(uriString, sizeof(uriString), "%s", mirrorResource->uri); - cJSON_AddStringToObject(jsonObject, "href", uriString); - - cJSON *itemRep = cJSON_Parse(cJSON_PrintUnformatted(mirrorResource->rep)); - cJSON_AddItemToObject(jsonObject, "rep", itemRep); - OC_LOG_V(DEBUG, HOSTING_TAG, "Will response resource : %s", cJSON_PrintUnformatted(jsonObject)); - - char *jsonResponse = cJSON_Print(jsonObject); - cJSON_Delete(jsonObject); - - return jsonResponse; -} - -OCEntityHandlerResult -resourceEntityHandlerCB (OCEntityHandlerFlag entifyHandlerFlag, - OCEntityHandlerRequest *entityHandlerRequest, - void* callbackParam) -{ - OC_LOG_V(DEBUG, HOSTING_TAG, "Inside device default entity handler - flags: 0x%x", - entifyHandlerFlag); - - OCEntityHandlerResult entityHandlerResult = OC_EH_OK; - OCEntityHandlerResponse entityHandlerResponse; - char payload[MAX_RESPONSE_LENGTH] = {0}; - - // Validate pointer - if (!entityHandlerRequest) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Invalid request pointer"); - return OC_EH_ERROR; - } - - // Initialize certain response fields - entityHandlerResponse.numSendVendorSpecificHeaderOptions = 0; - memset(entityHandlerResponse.sendVendorSpecificHeaderOptions, 0, - sizeof entityHandlerResponse.sendVendorSpecificHeaderOptions); - memset(entityHandlerResponse.resourceUri, 0, sizeof entityHandlerResponse.resourceUri); - - if (entifyHandlerFlag & OC_REQUEST_FLAG) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Flag includes OC_REQUEST_FLAG"); - if (entityHandlerRequest->resource == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received request from client to a non-existing resource"); - entityHandlerResult = handleNonExistingResourceRequest(entityHandlerRequest, payload, - sizeof(payload) - 1); - } - else if (OC_REST_GET == entityHandlerRequest->method) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received OC_REST_GET from client"); - entityHandlerResult = handleGetRequest (entityHandlerRequest, payload, sizeof(payload) - 1); - } - else if (OC_REST_PUT == entityHandlerRequest->method || - OC_REST_DELETE == entityHandlerRequest->method ) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received OC_REST_PUT/DELETE from client"); - - RequestHandle *request = createRequestHandle(); - - request->requestHandle[OIC_REQUEST_BY_CLIENT] = entityHandlerRequest; - request->resourceHandle = entityHandlerRequest->resource; - request->method = entityHandlerRequest->method; - request->entityRequestHandle = entityHandlerRequest->requestHandle; - - OCStackResult result = insertRequestHandle(s_requestHandleList, request); - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Insert request list : fail2(%d)", result); - return result; - } - - MirrorResource *mirrorResource = findMirrorResourceUsingHandle(s_mirrorResourceList, - entityHandlerRequest->resource, OIC_MIRROR_HANDLE); - if (mirrorResource == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Not found requested resource"); - return OC_EH_ERROR; - } - - result = requestQuery(request, - entityHandlerRequest->method, mirrorResource->address[OIC_SOURCE_ADDRESS], - mirrorResource->uri); - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Request query failed"); - deleteRequestHandleFromList(s_requestHandleList, request); - } - return OC_EH_OK; - } - else - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received unsupported method %d from client", - entityHandlerRequest->method); - entityHandlerResult = OC_EH_ERROR; - } - - // If the result isn't an error or forbidden, send response - if (!((entityHandlerResult == OC_EH_ERROR) || (entityHandlerResult == OC_EH_FORBIDDEN))) - { - // Format the response. Note this requires some info about the request - entityHandlerResponse.requestHandle = entityHandlerRequest->requestHandle; - entityHandlerResponse.resourceHandle = entityHandlerRequest->resource; - entityHandlerResponse.ehResult = entityHandlerResult; - //entityHandlerResponse.payload = (char *)payload; - //entityHandlerResponse.payloadSize = strlen(payload); - // Indicate that response is NOT in a persistent buffer - entityHandlerResponse.persistentBufferFlag = 0; - - // Handle vendor specific options - if (entityHandlerRequest->rcvdVendorSpecificHeaderOptions && - entityHandlerRequest->numRcvdVendorSpecificHeaderOptions) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received vendor specific options"); - OCHeaderOption *receivedVenderSpecificHeaderOptions = - entityHandlerRequest->rcvdVendorSpecificHeaderOptions; - for ( int i = 0; i < entityHandlerRequest->numRcvdVendorSpecificHeaderOptions; i++) - { - if (((OCHeaderOption)receivedVenderSpecificHeaderOptions[i]).protocolID == OC_COAP_ID) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received option with OC_COAP_ID and ID %u with", - ((OCHeaderOption)receivedVenderSpecificHeaderOptions[i]).optionID ); - } - } - OCHeaderOption *sendVenderSpecificHeaderOptions = - entityHandlerResponse.sendVendorSpecificHeaderOptions; - uint8_t option2[] = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30}; - uint8_t option3[] = {31, 32, 33, 34, 35, 36, 37, 38, 39, 40}; - sendVenderSpecificHeaderOptions[0].protocolID = OC_COAP_ID; - sendVenderSpecificHeaderOptions[0].optionID = 2248; - memcpy(sendVenderSpecificHeaderOptions[0].optionData, option2, sizeof(option2)); - sendVenderSpecificHeaderOptions[0].optionLength = 10; - sendVenderSpecificHeaderOptions[1].protocolID = OC_COAP_ID; - sendVenderSpecificHeaderOptions[1].optionID = 2600; - memcpy(sendVenderSpecificHeaderOptions[1].optionData, option3, sizeof(option3)); - sendVenderSpecificHeaderOptions[1].optionLength = 10; - entityHandlerResponse.numSendVendorSpecificHeaderOptions = 2; - } - - // Send the response - if (OCDoResponse(&entityHandlerResponse) != OC_STACK_OK) - { - OC_LOG(ERROR, HOSTING_TAG, "Error sending response"); - entityHandlerResult = OC_EH_ERROR; - } - } - } - if (entifyHandlerFlag & OC_OBSERVE_FLAG) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Flag includes OC_OBSERVE_FLAG"); - if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received OC_OBSERVE_REGISTER from client"); - } - else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Received OC_OBSERVE_DEREGISTER from client"); - } - } - - return entityHandlerResult; -} -OCEntityHandlerResult -handleGetRequest (OCEntityHandlerRequest *entityHandlerRequest, - char *payload, uint16_t maxPayloadSize) -{ - OC_LOG_V(DEBUG, HOSTING_TAG, "ProcessGetRequest in...."); - - OCEntityHandlerResult entityHandlerResult = OC_EH_ERROR; - char *responsePayload = buildResponsePayload(entityHandlerRequest); - if(!responsePayload) - { - return entityHandlerResult; - } - - if (maxPayloadSize > strlen ((char *)responsePayload)) - { - strncpy(payload, responsePayload, strlen((char *)responsePayload)); - entityHandlerResult = OC_EH_OK; - } - else - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Response buffer: %d bytes is too small", maxPayloadSize); - } - - free(responsePayload); - - return entityHandlerResult; -} -OCEntityHandlerResult -handleNonExistingResourceRequest(OCEntityHandlerRequest *entityHandlerRequest, - char *payload, uint16_t maxPayloadSize) -{ - OC_LOG_V(INFO, HOSTING_TAG, "Executing %s ", __func__); - - char responsePayload[OIC_STRING_MAX_VALUE] = {'\0'}; - strncpy(responsePayload, "{App determines payload: The resource does not exist.}", - sizeof(responsePayload)); - - if ( (entityHandlerRequest != NULL) && - (maxPayloadSize > strlen ((char *)responsePayload)) ) - { - strncpy((char *)payload, responsePayload, strlen((char *)responsePayload)); - } - else - { - OC_LOG_V (INFO, HOSTING_TAG, "Response buffer: %d bytes is too small", - maxPayloadSize); - } - - return OC_EH_RESOURCE_DELETED; -} - -OCStackResult requestIsAlive(const char *address) -{ - MirrorResourceList *requestMirrorResourceList = findMirrorResourceListUsingAddress( - s_mirrorResourceList, address, OIC_SOURCE_ADDRESS); - - if (requestMirrorResourceList == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Cannot found any mirror resource1"); - return OC_STACK_ERROR; - } - - if (requestMirrorResourceList->headerNode == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Cannot found any mirror resource2"); - destroyMirrorResourceList(requestMirrorResourceList); - return OC_STACK_ERROR; - } - - MirrorResource *mirrorResource = requestMirrorResourceList->headerNode; - while (mirrorResource) - { - RequestHandle *requestAlive = createRequestHandle(); - requestAlive->isAliveCheck = 1; - requestAlive->requestHandle[OIC_REQUEST_BY_CLIENT] = - mirrorResource->resourceHandle[OIC_MIRROR_HANDLE]; - - OCStackResult result = insertRequestHandle(s_requestHandleList, requestAlive); - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Insert request list : fail3"); - destroyRequestHandle(requestAlive); - mirrorResource = mirrorResource->next; - continue; - } - - result = requestQuery(requestAlive, OC_REST_GET, address, mirrorResource->uri); - if (result != OC_STACK_OK) - { - deleteRequestHandleFromList(s_requestHandleList, requestAlive); - } - mirrorResource = mirrorResource->next; - } - destroyMirrorResourceList(requestMirrorResourceList); - - return OC_STACK_OK; -} - -const char *getResultString(OCStackResult result) -{ - switch (result) - { - case OC_STACK_OK: - return "OC_STACK_OK"; - case OC_STACK_RESOURCE_CREATED: - return "OC_STACK_RESOURCE_CREATED"; - case OC_STACK_RESOURCE_DELETED: - return "OC_STACK_RESOURCE_DELETED"; - case OC_STACK_INVALID_URI: - return "OC_STACK_INVALID_URI"; - case OC_STACK_INVALID_QUERY: - return "OC_STACK_INVALID_QUERY"; - case OC_STACK_INVALID_IP: - return "OC_STACK_INVALID_IP"; - case OC_STACK_INVALID_PORT: - return "OC_STACK_INVALID_PORT"; - case OC_STACK_INVALID_CALLBACK: - return "OC_STACK_INVALID_CALLBACK"; - case OC_STACK_INVALID_METHOD: - return "OC_STACK_INVALID_METHOD"; - case OC_STACK_NO_MEMORY: - return "OC_STACK_NO_MEMORY"; - case OC_STACK_COMM_ERROR: - return "OC_STACK_COMM_ERROR"; - case OC_STACK_INVALID_PARAM: - return "OC_STACK_INVALID_PARAM"; - case OC_STACK_NOTIMPL: - return "OC_STACK_NOTIMPL"; - case OC_STACK_NO_RESOURCE: - return "OC_STACK_NO_RESOURCE"; - case OC_STACK_RESOURCE_ERROR: - return "OC_STACK_RESOURCE_ERROR"; - case OC_STACK_SLOW_RESOURCE: - return "OC_STACK_SLOW_RESOURCE"; - case OC_STACK_NO_OBSERVERS: - return "OC_STACK_NO_OBSERVERS"; - case OC_STACK_VIRTUAL_DO_NOT_HANDLE: - return "OC_STACK_VIRTUAL_DO_NOT_HANDLE"; - case OC_STACK_PRESENCE_STOPPED: - return "OC_STACK_PRESENCE_STOPPED"; - case OC_STACK_PRESENCE_TIMEOUT: - return "OC_STACK_PRESENCE_TIMEOUT"; - case OC_STACK_PRESENCE_DO_NOT_HANDLE: - return "OC_STACK_PRESENCE_DO_NOT_HANDLE"; - case OC_STACK_ERROR: - return "OC_STACK_ERROR"; - default: - return "UNKNOWN"; - } -} - -void getJsonArrayPair(cJSON *tempData) -{ - int countofrep = cJSON_GetArraySize(tempData); - OC_LOG_V(DEBUG, HOSTING_TAG, - "//////////////////////////////////////////////////////////////////////////"); - OC_LOG_V(DEBUG, HOSTING_TAG, "//Test"); - OC_LOG_V(DEBUG, HOSTING_TAG, "rep Size : %d", countofrep); - - for (int i = 0; i < countofrep; ++i) - { - cJSON *arrayJSON = cJSON_GetArrayItem(tempData, i); - OC_LOG_V(DEBUG, HOSTING_TAG, "rep#%d's name : %s", i, arrayJSON->string); - - switch (arrayJSON->type) - { - case cJSON_False: - case cJSON_True: - OC_LOG_V(DEBUG, HOSTING_TAG, "rep#%d's value : %d", i, arrayJSON->valueint); - break; - case cJSON_Number: - OC_LOG_V(DEBUG, HOSTING_TAG, "rep#%d's value : %f", i, arrayJSON->valuedouble); - break; - case cJSON_String: - OC_LOG_V(DEBUG, HOSTING_TAG, "rep#%d's value : %s", i, arrayJSON->valuestring); - break; - case cJSON_NULL: - default: - OC_LOG_V(DEBUG, HOSTING_TAG, "rep#%d's value : NULL", i); - break; - } - } - OC_LOG_V(DEBUG, HOSTING_TAG, - "//////////////////////////////////////////////////////////////////////////"); -} - -OCStackResult requestQuery(RequestHandle *request, OCMethod method, - const char *queryAddress, const char *queryUri) -{ - - OCStackResult result = OC_STACK_ERROR; - OCCallbackData cbData; - - /* Start a discovery query*/ - char queryFullUri[OIC_STRING_MAX_VALUE] = {'\0'}; - if (queryAddress == NULL) - { - return result; - } - else - { - snprintf(queryFullUri, sizeof(queryFullUri) ,"coap://%s%s%s", queryAddress , queryUri, OIC_COORDINATING_FLAG); - } - - cbData.cb = requestQueryCB; - cbData.context = (void *)DEFAULT_CONTEXT_VALUE; - cbData.cd = NULL; - - if(method == OC_REST_PUT) - { - char payload[OIC_STRING_MAX_VALUE] = {'\0'}; - //snprintf(payload , sizeof(payload), "%s" , - // ((OCEntityHandlerRequest*)request->requestHandle[OIC_REQUEST_BY_CLIENT])->reqJSONPayload); - - //result = OCDoResource(&request->requestHandle[OIC_REQUEST_BY_COORDINATOR], - // method, queryFullUri, NULL, payload, OC_TRANSPORT, OC_LOW_QOS, &cbData, NULL, 0); - } - else - { - result = OCDoResource(&request->requestHandle[OIC_REQUEST_BY_COORDINATOR], - method, queryFullUri, NULL, 0, OC_TRANSPORT, OC_LOW_QOS, &cbData, NULL, 0); - } - - if (result != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "OCStack resource error"); - } - - return result; -} - -OCStackApplicationResult requestQueryCB(void *context, OCDoHandle handle, - OCClientResponse *clientResponse) -{ - OCStackApplicationResult ret = OC_STACK_DELETE_TRANSACTION; - - if (context == (void *) DEFAULT_CONTEXT_VALUE) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Callback Context for Request query recvd successfully"); - } - - if (clientResponse && clientResponse->result != OC_STACK_OK && clientResponse->result != OC_STACK_RESOURCE_DELETED) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "requestQueryCB result error = %s", - getResultString(clientResponse->result)); - return checkResourceValidation(handle); - } - - if (clientResponse && (clientResponse->result == OC_STACK_OK || clientResponse->result == OC_STACK_RESOURCE_DELETED)) - { - RequestHandle *request = findRequestHandle(s_requestHandleList, handle, OIC_REQUEST_BY_COORDINATOR); - if (request == NULL) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Not found Any request"); - return ret; - } - if (request->isAliveCheck == 1) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "This response is Alive Check : Keep resource"); - } - else - { - //OC_LOG_V(DEBUG, HOSTING_TAG, "requestCB's payload: %s", clientResponse->resJSONPayload); - OCEntityHandlerRequest *entityHandler = (OCEntityHandlerRequest *)( - request->requestHandle[OIC_REQUEST_BY_CLIENT]); - OC_LOG_V(DEBUG, HOSTING_TAG, "requested resource handle : %u", entityHandler->resource - ); - - entityHandler->resource = request->resourceHandle; - entityHandler->method = request->method; - entityHandler->requestHandle = request->entityRequestHandle; - - OCEntityHandlerResponse response; //= buildEntityHandlerResponse( - //entityHandler, clientResponse->resJSONPayload); - if (OCDoResponse(&response) != OC_STACK_OK) - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Error sending response"); - deleteRequestHandleFromList(s_requestHandleList, request); - return ret; - } - if (entityHandler->method == OC_REST_DELETE) - { - OCDeleteResource(entityHandler->resource); - } - } - deleteRequestHandleFromList(s_requestHandleList, request); - ret = OC_STACK_KEEP_TRANSACTION; - } - - return ret; -} - -OCEntityHandlerResponse buildEntityHandlerResponse(OCEntityHandlerRequest *entityHandlerRequest, - const char *clientPayload) -{ - OC_LOG_V(DEBUG, HOSTING_TAG, "enter buildEntityHandlerResponse"); - OCEntityHandlerResponse response; - memset(&response, 0, sizeof(response)); - OCEntityHandlerResult entityHandlerResult = OC_EH_OK; - char payload[MAX_RESPONSE_LENGTH] = {'\0'}; - - // Initialize certain response fields - response.numSendVendorSpecificHeaderOptions = 0; - memset(response.sendVendorSpecificHeaderOptions, 0, - sizeof response.sendVendorSpecificHeaderOptions); - memset(response.resourceUri, 0, sizeof response.resourceUri); - - char *temp = NULL; - if(entityHandlerRequest->method == OC_REST_PUT) - { - cJSON *observeJson = cJSON_CreateObject(); - observeJson = cJSON_Parse(clientPayload); - - cJSON *ocArray = cJSON_GetObjectItem(observeJson, "oc"); - cJSON *ocArray_sub = cJSON_GetArrayItem(ocArray, 0); - - cJSON *tempData = cJSON_GetObjectItem(ocArray_sub, "rep"); - temp = cJSON_PrintUnformatted(tempData); - - cJSON_Delete(observeJson); - - //entityHandlerRequest->reqJSONPayload = temp; - } - entityHandlerResult = handleRequestPayload(entityHandlerRequest, payload, sizeof(payload) - 1); - - // Format the response. Note this requires some info about the request - response.requestHandle = entityHandlerRequest->requestHandle; - response.resourceHandle = entityHandlerRequest->resource; - response.ehResult = entityHandlerResult; - - //response.payload = (char *)payload; - //response.payloadSize = strlen(payload); - // Indicate that response is NOT in a persistent buffer - response.persistentBufferFlag = 0; - - if(entityHandlerRequest->method == OC_REST_PUT){ - if(temp){ - free(temp); - } - } - - return response; -} - -OCEntityHandlerResult handleRequestPayload (OCEntityHandlerRequest *entityHandlerRequest, - char *payload, uint16_t maxPayloadSize) -{ - OC_LOG_V(DEBUG, HOSTING_TAG, "enter handleRequestPayload"); - OCEntityHandlerResult entityHandlerResult = OC_EH_ERROR; - - if (entityHandlerRequest->method == OC_REST_DELETE) - { - memset(payload, '\0', sizeof(char) * (maxPayloadSize + 1)); - OC_LOG_V(DEBUG, HOSTING_TAG, "DELETE"); - return OC_EH_RESOURCE_DELETED; - } - - char *responsePayload = buildResponsePayload(entityHandlerRequest); - if(!responsePayload) - { - return entityHandlerResult; - } - - if (maxPayloadSize > strlen ((char *)responsePayload)) - { - strncpy(payload, responsePayload, strlen ((char *)responsePayload)); - entityHandlerResult = OC_EH_OK; - } - else - { - OC_LOG_V(DEBUG, HOSTING_TAG, "Response buffer: %d bytes is too small", maxPayloadSize); - entityHandlerResult = OC_EH_ERROR; - } - - free(responsePayload); - - return entityHandlerResult; -} diff --git a/service/notification-manager/NotificationManager/src/hosting.cpp b/service/notification-manager/NotificationManager/src/hosting.cpp new file mode 100644 index 0000000..b3a8323 --- /dev/null +++ b/service/notification-manager/NotificationManager/src/hosting.cpp @@ -0,0 +1,61 @@ +//****************************************************************** +// +// Copyright 2015 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. +// +//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + +#include "hosting.h" + +// Standard API +#include +#include +#include +#include + +#include "octypes.h" +#include "logger.h" +#include "ResourceHosting.h" + +#define OC_TRANSPORT CT_ADAPTER_IP +#define HOSTING_TAG PCF("Hosting") + +namespace +{ + OIC::Service::ResourceHosting * rhInstance = OIC::Service::ResourceHosting::getInstance(); +} + +OCStackResult OICStartCoordinate() +{ + OCStackResult retResult = OC_STACK_OK; + try + { + rhInstance->startHosting(); + }catch(...) + { + retResult = OC_STACK_ERROR; + } + + return retResult; +} + +OCStackResult OICStopCoordinate() +{ + OCStackResult retResult = OC_STACK_OK; + rhInstance->stopHosting(); + + return retResult; +} diff --git a/service/notification-manager/NotificationManager/src/requestHandler.c b/service/notification-manager/NotificationManager/src/requestHandler.c deleted file mode 100644 index 8508d9d..0000000 --- a/service/notification-manager/NotificationManager/src/requestHandler.c +++ /dev/null @@ -1,208 +0,0 @@ -//****************************************************************** -// -// Copyright 2015 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. -// -//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - -#include "virtualResource.h" - -RequestHandleList *createRequestHandleList() -{ - RequestHandleList *requestHandleList = (RequestHandleList*)malloc(sizeof(RequestHandleList)); - if(!requestHandleList) - { - OC_LOG(DEBUG, RH_TAG,"Request Handle List Creation Fail."); - } - else - { - requestHandleList->headerNode = NULL; - requestHandleList->tailNode = NULL; - } - - return requestHandleList; -} - -RequestHandle *createRequestHandle() -{ - RequestHandle *requestHandle = (RequestHandle*)malloc(sizeof(RequestHandle)); - if(!requestHandle) - { - OC_LOG(DEBUG, RH_TAG,"Request Handle Creation Fail."); - } - else - { - requestHandle->requestHandle[OIC_REQUEST_BY_CLIENT] = NULL; - requestHandle->requestHandle[OIC_REQUEST_BY_COORDINATOR] = NULL; - requestHandle->resourceHandle = NULL; - - requestHandle->isAliveCheck = 0; - - requestHandle->next = NULL; - } - - return requestHandle; - -} -OCStackResult insertRequestHandle(RequestHandleList *requestHandleList, - RequestHandle *requestHandle) -{ - if(requestHandleList == NULL || requestHandle == NULL) - { - return OC_STACK_INVALID_PARAM; - } - - if(requestHandleList->headerNode == NULL) - { - requestHandleList->headerNode = requestHandle; - requestHandleList->tailNode = requestHandle; - } - else - { - requestHandleList->tailNode->next = requestHandle; - requestHandleList->tailNode = requestHandle; - } - - return OC_STACK_OK; -} - -OCStackResult deleteRequestHandleFromList(RequestHandleList *requestHandleList, - RequestHandle *requestHandle) -{ - if(requestHandleList == NULL || requestHandle == NULL) - { - OC_LOG(DEBUG, RH_TAG,"Delete Request Handle : invalid parameter."); - return OC_STACK_INVALID_PARAM; - } - if(requestHandleList->headerNode == NULL) - { - OC_LOG(DEBUG, RH_TAG,"Delete Request Handle : Empty Request Handle List."); - return OC_STACK_ERROR; - } - - if(requestHandle == requestHandleList->headerNode) - { - requestHandleList->headerNode = requestHandleList->headerNode->next; - requestHandle->next = NULL; - return destroyRequestHandle(requestHandle); - } - - RequestHandle *preNode = requestHandleList->headerNode; - RequestHandle *curNode = preNode->next; - while(curNode != NULL) - { - if(curNode == requestHandle) - { - if(curNode == requestHandleList->tailNode) - { - requestHandleList->tailNode = preNode; - preNode->next = NULL; - } - else - { - preNode->next = curNode->next; - } - requestHandle->next = NULL; - return destroyRequestHandle(requestHandle); - } - preNode = curNode; - curNode = curNode->next; - } - - return OC_STACK_ERROR; -} - -OCStackResult destroyRequestHandle(RequestHandle *requestHandle) -{ - if(requestHandle) - { - if(requestHandle->next) - { -// destroyRequestHandle(requestHandle->next); - requestHandle->next = NULL; - } - - requestHandle->requestHandle[OIC_REQUEST_BY_CLIENT] = NULL; - requestHandle->requestHandle[OIC_REQUEST_BY_COORDINATOR] = NULL; - - requestHandle->isAliveCheck = 0; - - free(requestHandle); - } - else - { - return OC_STACK_INVALID_PARAM; - } - - return OC_STACK_OK; -} - -OCStackResult destroyRequestHandleList(RequestHandleList *requestHandleList){ - if(requestHandleList) - { - while(requestHandleList->headerNode) - { - deleteRequestHandleFromList(requestHandleList,requestHandleList->headerNode); - } - free(requestHandleList); - } - else{ - return OC_STACK_INVALID_PARAM; - } - - return OC_STACK_OK; -} - -RequestHandle *findRequestHandle(RequestHandleList *requestHandleList, - OCDoHandle handle, OICResourceCoordinatorParamType paramType) -{ - if(requestHandleList == NULL || handle == NULL) - { - OC_LOG(DEBUG, RH_TAG,"Find Virtual Resource : invalid parameter."); - return NULL; - } - if(requestHandleList->headerNode == NULL) - { - OC_LOG(DEBUG, RH_TAG,"Find Virtual Resource : Empty Virtual Resource List."); - return NULL; - } - - if(paramType == OIC_REQUEST_BY_CLIENT) - { - RequestHandle *tempRequestHandle = requestHandleList->headerNode; - while(tempRequestHandle != NULL) - { - if((OCEntityHandlerRequest*)tempRequestHandle->requestHandle[paramType] == handle) - { - return tempRequestHandle; - } - tempRequestHandle = tempRequestHandle->next; - } - } - else - { - RequestHandle *tempRequestHandle = requestHandleList->headerNode; - while(tempRequestHandle != NULL) - { - if((OCDoHandle)tempRequestHandle->requestHandle[paramType] == handle) - { - return tempRequestHandle; - } - tempRequestHandle = tempRequestHandle->next; - } - } - return NULL; -} diff --git a/service/notification-manager/NotificationManager/src/virtualResource.c b/service/notification-manager/NotificationManager/src/virtualResource.c deleted file mode 100644 index 9dae0b2..0000000 --- a/service/notification-manager/NotificationManager/src/virtualResource.c +++ /dev/null @@ -1,406 +0,0 @@ -//****************************************************************** -// -// Copyright 2015 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. -// -//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - -#include "virtualResource.h" - - -MirrorResourceList *createMirrorResourceList() -{ - MirrorResourceList *mirrorResourceList = (MirrorResourceList *)malloc(sizeof(MirrorResourceList)); - if (!mirrorResourceList) - { - OC_LOG_V(DEBUG, VR_TAG,"Virtual Resource List Creation Fail."); - } - else - { - mirrorResourceList->headerNode = NULL; - mirrorResourceList->tailNode = NULL; - } - - return mirrorResourceList; -} - -MirrorResource *createMirrorResource() -{ - MirrorResource *mirrorResource = (MirrorResource*)malloc(sizeof(MirrorResource)); - if (!mirrorResource) - { - OC_LOG_V(DEBUG, VR_TAG,"Virtual Resource List Creation Fail."); - } - else - { - mirrorResource->resourceHandle[OIC_REQUEST_HANDLE] = NULL; - mirrorResource->resourceHandle[OIC_MIRROR_HANDLE] = NULL; - mirrorResource->address[OIC_SOURCE_ADDRESS] = NULL; - mirrorResource->address[OIC_MIRROR_ADDRESS] = NULL; - - mirrorResource->rep = NULL; - mirrorResource->uri = NULL; - mirrorResource->next = NULL; - - mirrorResource->prop.countResourceType = 0; - mirrorResource->prop.resourceType = NULL; - mirrorResource->prop.countInterface = 0; - mirrorResource->prop.resourceInterfaceName = NULL; - } - - return mirrorResource; -} - -OCStackResult destroyMirrorResourceList(MirrorResourceList *mirrorResourceList) -{ - OC_LOG_V(DEBUG, VR_TAG,"enter destroyVirtualResourceList"); - if(mirrorResourceList) - { - while (mirrorResourceList->headerNode) - { - deleteMirrorResourceFromList(mirrorResourceList, mirrorResourceList->headerNode); - } - - free(mirrorResourceList); - } - else - { - return OC_STACK_INVALID_PARAM; - } - - return OC_STACK_OK; -} - -OCStackResult destroyMirrorResource(MirrorResource *mirrorResource) -{ - OC_LOG_V(DEBUG, VR_TAG,"enter destroy virtual resource."); - if(mirrorResource) - { - if(mirrorResource->next) - { - mirrorResource->next = NULL; - } - if (mirrorResource->rep) - { - cJSON_Delete(mirrorResource->rep); - } - if (mirrorResource->uri) - { - free(mirrorResource->uri); - } - if (mirrorResource->address[OIC_SOURCE_ADDRESS]) - { - free(mirrorResource->address[OIC_SOURCE_ADDRESS]); - } - if (mirrorResource->address[OIC_MIRROR_ADDRESS]) - { - free(mirrorResource->address[OIC_MIRROR_ADDRESS]); - } - if (mirrorResource->prop.resourceType) - { - int i = 0; - for (i = 0; i < mirrorResource->prop.countResourceType; ++i) - { - free(mirrorResource->prop.resourceType[i]); - mirrorResource->prop.resourceType[i] = NULL; - } - free(mirrorResource->prop.resourceType); - mirrorResource->prop.countResourceType = 0; - } - if (mirrorResource->prop.resourceInterfaceName) - { - int i = 0; - for (i = 0; i < mirrorResource->prop.countInterface; ++i) - { - free(mirrorResource->prop.resourceInterfaceName[i]); - mirrorResource->prop.resourceInterfaceName[i] = NULL; - } - free(mirrorResource->prop.resourceInterfaceName); - mirrorResource->prop.countInterface = 0; - } - free(mirrorResource); - } - else - { - return OC_STACK_INVALID_PARAM; - } - - return OC_STACK_OK; -} - -OCStackResult insertMirrorResource(MirrorResourceList *mirrorResourceList, - MirrorResource *mirrorResource) -{ - if (mirrorResourceList == NULL || mirrorResource == NULL) - { - return OC_STACK_INVALID_PARAM; - } - - if (mirrorResourceList->headerNode == NULL) - { - mirrorResourceList->headerNode = mirrorResource; - mirrorResourceList->tailNode = mirrorResource; - } - else - { - mirrorResourceList->tailNode->next = mirrorResource; - mirrorResourceList->tailNode = mirrorResource; - } - - return OC_STACK_OK; -} - -MirrorResource *findMirrorResourceUsingAddressAndURI(MirrorResourceList *mirrorResourceList, - const char *address, OICResourceCoordinatorParamType paramType, const char *uri) -{ - if (mirrorResourceList == NULL || address == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Find Virtual Resource : invalid parameter."); - return NULL; - } - if (mirrorResourceList->headerNode == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Find Virtual Resource : Empty Virtual Resource List."); - return NULL; - } - - MirrorResource *tempMirrorResource = mirrorResourceList->headerNode; - while (tempMirrorResource != NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"node's uri = %s", tempMirrorResource->uri); - if (strcmp(tempMirrorResource->address[paramType], address) == 0) // if(It is Same) - { - if (strcmp(tempMirrorResource->uri, uri) == 0) // if(It is Same) - { - return tempMirrorResource; - } - } - tempMirrorResource = tempMirrorResource->next; - } - - return NULL; -} - -MirrorResource *findMirrorResourceUsingHandle(MirrorResourceList *mirrorResourceList, - OCResourceHandle handle, OICResourceCoordinatorParamType paramType) -{ - if (mirrorResourceList == NULL || handle == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Find Virtual Resource : invalid parameter."); - return NULL; - } - if (mirrorResourceList->headerNode == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Find Virtual Resource : Empty Virtual Resource List."); - return NULL; - } - - MirrorResource *tempMirrorResource = mirrorResourceList->headerNode; - while (tempMirrorResource != NULL) - { - if (tempMirrorResource->resourceHandle[paramType] == handle) - { - return tempMirrorResource; - } - tempMirrorResource = tempMirrorResource->next; - } - - return NULL; -} - -OCStackResult deleteMirrorResourceFromList(MirrorResourceList *mirrorResourceList, - MirrorResource *mirrorResource) -{ - - OC_LOG_V(DEBUG, VR_TAG,"enter delete virtual resource."); - - if (mirrorResourceList == NULL || mirrorResource == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Delete Virtual Resource : invalid parameter."); - return OC_STACK_INVALID_PARAM; - } - if (mirrorResourceList->headerNode == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Delete Virtual Resource : Empty Virtual Resource List."); - return OC_STACK_ERROR; - } - - if (mirrorResource == mirrorResourceList->headerNode) - { - mirrorResourceList->headerNode = mirrorResourceList->headerNode->next; - mirrorResource->next = NULL; - return destroyMirrorResource(mirrorResource); - } - - MirrorResource *preNode = mirrorResourceList->headerNode; - MirrorResource *curNode = preNode->next; - while (curNode != NULL) - { - if (curNode == mirrorResource) - { - if (curNode == mirrorResourceList->tailNode) - { - mirrorResourceList->tailNode = preNode; - preNode->next = NULL; - } - else - { - preNode->next = curNode->next; - } - mirrorResource->next = NULL; - return destroyMirrorResource(mirrorResource); - } - preNode = curNode; - curNode = curNode->next; - } - - return OC_STACK_ERROR; -} - -OCStackResult ejectMirrorResource(MirrorResourceList *mirrorResourceList, - MirrorResource *mirrorResource) -{ - if (mirrorResourceList == NULL || mirrorResource == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Eject Virtual Resource : invalid parameter."); - return OC_STACK_INVALID_PARAM; - } - if (mirrorResourceList->headerNode == NULL) - { - OC_LOG_V(DEBUG, VR_TAG, "Eject Virtual Resource : Empty Virtual Resource List."); - return OC_STACK_ERROR; - } - - if (mirrorResource == mirrorResourceList->headerNode) - { - mirrorResourceList->headerNode = mirrorResourceList->headerNode->next; - return OC_STACK_OK; - } - - MirrorResource *preNode = mirrorResourceList->headerNode; - MirrorResource *curNode = preNode->next; - while (curNode != NULL) - { - if (curNode == mirrorResource) - { - if(curNode == mirrorResourceList->headerNode) - { - mirrorResourceList->headerNode = NULL; - mirrorResourceList->tailNode = NULL; - } - else if(curNode == mirrorResourceList->tailNode) - { - mirrorResourceList->tailNode = preNode; - } - else - { - preNode->next = curNode->next; - } - return OC_STACK_OK; - } - preNode = curNode; - curNode = curNode->next; - } - - return OC_STACK_ERROR; - -} - -MirrorResource *cloneMirrorResource(MirrorResource *sourceMirrorResource) -{ - MirrorResource *clonedMirrorResource = createMirrorResource(); - - int sizeofstr = 0; - int i = 0; - - clonedMirrorResource->rep = cJSON_Parse(cJSON_PrintUnformatted(sourceMirrorResource->rep)); - - sizeofstr = strlen(sourceMirrorResource->uri) + 1; - clonedMirrorResource->uri = (char *)malloc(sizeof(char) * sizeofstr); - memset(clonedMirrorResource->uri, '\0', sizeofstr); - strcpy(clonedMirrorResource->uri, sourceMirrorResource->uri); - - for (i = OIC_SOURCE_ADDRESS; i < OIC_MIRROR_ADDRESS; ++i) - { - sizeofstr = strlen(sourceMirrorResource->address[i]) + 1; - clonedMirrorResource->address[i] = (char *)malloc(sizeof(char) * sizeofstr); - memset(clonedMirrorResource->address[i], '\0', sizeofstr); - strcpy(clonedMirrorResource->address[i], sourceMirrorResource->address[i]); - } - - return clonedMirrorResource; -} - -MirrorResourceList *findMirrorResourceListUsingAddress(MirrorResourceList *mirrorResourceList, - const char *address, OICResourceCoordinatorParamType paramType) -{ - if (mirrorResourceList == NULL || address == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Find Virtual Resource List : invalid parameter."); - return NULL; - } - if (mirrorResourceList->headerNode == NULL) - { - OC_LOG_V(DEBUG, VR_TAG,"Find Virtual Resource List : Empty Virtual Resource List."); - return NULL; - } - - MirrorResource *tempNode = mirrorResourceList->headerNode; - while (tempNode != NULL) - { - OC_LOG_V(DEBUG, VR_TAG, "uri = %s", tempNode->uri); - tempNode = tempNode->next; - } - - - MirrorResourceList *resultMirrorResourceList = createMirrorResourceList(); - MirrorResource *mirrorResource = mirrorResourceList->headerNode; - while (mirrorResource != NULL) - { - if (strcmp(mirrorResource->address[paramType], address) == 0) // if(It is Same) - { - insertMirrorResource(resultMirrorResourceList, cloneMirrorResource(mirrorResource)); - } - mirrorResource = mirrorResource->next; - } - - return resultMirrorResourceList; -} - - -OCStackResult printMirrorResourceList(MirrorResourceList *mirrorResourceList) -{ - if (mirrorResourceList == NULL) - { - OC_LOG_V(DEBUG, VR_TAG, "print Virtual Resource list : invalid parameter."); - return OC_STACK_INVALID_PARAM; - } - if (mirrorResourceList->headerNode == NULL) - { - OC_LOG_V(DEBUG, VR_TAG, "print Virtual Resource list : Empty Virtual Resource List."); - return OC_STACK_INVALID_PARAM; - } - OC_LOG_V(DEBUG, VR_TAG, "=============================================================="); - MirrorResource *mirrorResource = mirrorResourceList->headerNode; - while (mirrorResource != NULL) - { - OC_LOG_V(DEBUG, VR_TAG, "uri = %s", mirrorResource->uri); - mirrorResource = mirrorResource->next; - } - OC_LOG_V(DEBUG, VR_TAG, "=============================================================="); - - return OC_STACK_OK; -} diff --git a/service/notification-manager/NotificationManager/src/virtualResource.h b/service/notification-manager/NotificationManager/src/virtualResource.h deleted file mode 100644 index 44885ab..0000000 --- a/service/notification-manager/NotificationManager/src/virtualResource.h +++ /dev/null @@ -1,302 +0,0 @@ -//****************************************************************** -// -// Copyright 2015 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. -// -//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - -#ifndef _VIRTUAL_RESOURCE_H_ -#define _VIRTUAL_RESOURCE_H_ - -#include -#include -#include - -#include "ocstack.h" -#include "logger.h" - -#include "cJSON.h" -#define TAG PCF("MirrorResource") -//----------------------------------------------------------------------------- -// Definition of Constant -//----------------------------------------------------------------------------- -#define VR_TAG "__NM__" -#define RH_TAG "__RM__" - -//----------------------------------------------------------------------------- -// Typedefs -//----------------------------------------------------------------------------- - -/** -* Resource Coordinating Parameter Type -*/ -typedef enum -{ - /* - * for mirrorResourceHandle - */ - OIC_SOURCE_ADDRESS = 0, - OIC_MIRROR_ADDRESS = 1, - OIC_REQUEST_HANDLE = 0, - OIC_MIRROR_HANDLE = 1, - - /* - * for requestHandle - */ - OIC_REQUEST_BY_CLIENT = 0, - OIC_REQUEST_BY_COORDINATOR = 1, - - OIC_NONE = 255 -} OICResourceCoordinatorParamType; - -/** -* Property of Mirror Resource -*/ -typedef struct MirrorResourceProperty -{ - int countResourceType; - int countInterface; - char **resourceType; - char **resourceInterfaceName; -} MirrorResourceProperty; - -/** -* Mirror Resource Object -*/ -typedef struct MirrorResource -{ - OCDoHandle resourceHandle[2]; // OIC_REQUEST_HANDLE = 0, OIC_VIRTUAL_HANDLE = 1 - char *address[2]; // OIC_SOURCE_ADDRESS = 0, OIC_VIRTUAL_ADDRESS = 1 - cJSON *rep; - char *uri; - MirrorResourceProperty prop; - - struct MirrorResource *next; - - /* - * for multiple resource - */ -// virtualRepresentation rep2; - -} MirrorResource; - -/** -* Mirror Resource List -*/ -typedef struct MirrorResourceList -{ - struct MirrorResource *headerNode; - struct MirrorResource *tailNode; -} MirrorResourceList; - -/** -* Request Object -*/ -typedef struct RequestHandle -{ - void *requestHandle[2]; // OIC_REQUEST_BY_CLIENT = 0, OIC_REQUEST_BY_COORDINATOR = 1 - OCResourceHandle resourceHandle; - OCRequestHandle entityRequestHandle; - - OCMethod method; - - unsigned char isAliveCheck; - - struct RequestHandle *next; -} RequestHandle; - -/** -* Request Object List -*/ -typedef struct RequestHandleList -{ - struct RequestHandle *headerNode; - struct RequestHandle *tailNode; -} RequestHandleList; - -//----------------------------------------------------------------------------- -// Function prototypes for mirrorResourceHandle -//----------------------------------------------------------------------------- - -/** -* Create an empty mirror resource list -* -* @return -* pointer to empty Mirror resource list -*/ -MirrorResourceList *createMirrorResourceList(); - -/** -* Create an empty mirror resource. -* -* @return -* pointer to empty mirror resource -*/ -MirrorResource *createMirrorResource(); - -/** -* Insert the mirror resource in the mirror resource list -* -* @param mirrorResourceList - pointer to the target mirror resource list that mirror resource will be inserted -* @param mirrorResource - pointer to mirror resource list to be inserted -* @return -* OIC_HOSTING_INVALID_PARAM - if list or vResource is null -* OIC_HOSTING_OK - successfully inserted -*/ -OCStackResult insertMirrorResource(MirrorResourceList *mirrorResourceList, - MirrorResource *mirrorResource); - -/** -* Delete the mirror resource from the mirror resource list -* -* @param mirrorResourceList - pointer to the target mirror resource list that mirror resource will be deleted -* @param mirrorResource - pointer to mirror resource list to be deleted -* @return -* OIC_HOSTING_INVALID_PARAM - if mirrorResourceList or mirrorResource is null -* OIC_HOSTING_ERROR - mirror resource delete process error -* OIC_HOSTING_OK - successfully deleted -*/ -OCStackResult deleteMirrorResourceFromList(MirrorResourceList *mirrorResourceList, - MirrorResource *mirrorResource); - -/** -* Destroy mirror resource -* -* @param mirrorResource - pointer to mirror resource to be destroyed -* @return -* OIC_HOSTING_INVALID_PARAM - if mirrorResource is null -* OIC_HOSTING_OK - successfully destroyed -*/ -OCStackResult destroyMirrorResource(MirrorResource *mirrorResource); - -/** -* Destroy mirror resource list -* -* @param mirrorResourceList - pointer to mirror resource list to be destroyed -* @return -* OIC_HOSTING_INVALID_PARAM - if mirrorResourceList is null -* OIC_HOSTING_OK - successfully destroyed -*/ -OCStackResult destroyMirrorResourceList(MirrorResourceList *mirrorResourceList); - -/** -* Find mirror resource using handle -* -* @param mirrorResourceList - pointer to the target mirror resource list that mirror resource will be found -* @param handle - handle value to be found -* @param paramType - handle type to be found -* -* NOTE: every parameter(handle and type) will be compared -* -* @return -* pointer to the found mirror resource -* NULL - mirror resource not found -*/ -MirrorResource *findMirrorResourceUsingHandle(MirrorResourceList *mirrorResourceList, - OCResourceHandle handle, OICResourceCoordinatorParamType paramType); - -/* - * find virtual resource using address function 사용하는지 확인 필요. - */ -//virtualResource *findvResourceUsingAddress(virtualResourceList *list, -// const char *Address, OICResourceHostingParamType type, const char *subData); - -/** -* Find mirror resource using Address and URI -* -* @param mirrorResourceList - pointer to the target mirror resource list that mirror resource will be found -* @param address - pointer to address to be found -* @param paramType - address type to be found -* @param uri - pointer to uri to be found -* -* NOTE: every parameter(address, type and uri) will be compared -* -* @return -* pointer to the found mirror resource -* NULL - invalid input parameter or mirror resource not found -*/ -MirrorResource *findMirrorResourceUsingAddressAndURI(MirrorResourceList *mirrorResourceList, - const char *address, OICResourceCoordinatorParamType paramType, const char *uri); - -/** -* Find mirror resource list using Address and Hosting Parameter Type -* -* @param mirrorResourceList - pointer to the target mirror resource list that mirror resource will be found -* @param address - pointer to address to be found -* @param paramType - address type to be found -* -* NOTE: every parameter(address, type and uri) will be compared -* -* @return -* pointer to the found mirror resource -* NULL - invalid input parameter or mirror resource list not found -*/ -MirrorResourceList *findMirrorResourceListUsingAddress(MirrorResourceList *mirrorResourceList, - const char *address, OICResourceCoordinatorParamType paramType); - -/** -* Copy mirror resource -* -* @param sourceMirrorResource - pointer to source mirror resource -* -* @return -* pointer to the copied mirror resource -*/ -MirrorResource *cloneMirrorResource(MirrorResource *sourceMirrorResource); - -/** -* Eject mirror resource from mirror resource list -* -* @param mirrorResourceList - pointer to the target mirror resource list that mirror resource will be found -* @param mirrorResource - pointer to mirror resource to be ejected -* -* @return -* OIC_HOSTING_INVALID_PARAM - mirrorResourceList or mirrorResource is null -* OIC_HOSTING_OK - ejected successfully -* OIC_HOSTING_ERROR - cannot found mirror resource from mirrorResourceList same with mirrorResource -*/ -OCStackResult ejectMirrorResource(MirrorResourceList *mirrorResourceList, - MirrorResource *mirrorResource); - -/** -* Print mirror resources from mirror resource list -* -* @param mirrorResourceList - pointer to the mirror resource list that mirror resource will be printed -* -* @return -* OIC_HOSTING_INVALID_PARAM - mirrorResourceList is null or mirrorResourceList is empty -* OIC_HOSTING_OK - print successfully -*/ -OCStackResult printMirrorResourceList(MirrorResourceList *mirrorResourceList); - -//----------------------------------------------------------------------------- -// Function prototypes for RequestHandle -//----------------------------------------------------------------------------- -RequestHandleList *createRequestHandleList(); -RequestHandle *createRequestHandle(); -OCStackResult insertRequestHandle(RequestHandleList *requestHandleList, - RequestHandle *requestHandle); - -OCStackResult deleteRequestHandleFromList(RequestHandleList *requestHandleList, - RequestHandle *requestHandle); -OCStackResult destroyRequestHandle(RequestHandle *requestHandle); - -OCStackResult destroyRequestHandleList(RequestHandleList *requestHandleList); - -RequestHandle *findRequestHandle(RequestHandleList *requestHandleList, - OCDoHandle handle, OICResourceCoordinatorParamType paramType); - -#endif //_MIRROR_RESOURCE_H_ diff --git a/service/notification-manager/SampleApp/linux/notificationManager/main.c b/service/notification-manager/SampleApp/linux/notificationManager/main.cpp similarity index 92% rename from service/notification-manager/SampleApp/linux/notificationManager/main.c rename to service/notification-manager/SampleApp/linux/notificationManager/main.cpp index 5814107..4f4ebe0 100644 --- a/service/notification-manager/SampleApp/linux/notificationManager/main.c +++ b/service/notification-manager/SampleApp/linux/notificationManager/main.cpp @@ -46,13 +46,6 @@ int main() signal(SIGINT, handleSigInt); while (!g_quitFlag) { - if (OCProcess() != OC_STACK_OK) - { - OICStopCoordinate(); - printf("OCStack process error\n"); - return 0; - } - sleep(2); }