From 33cb5dcc760b06c3ff82f26b2a2bf121fad5f9a0 Mon Sep 17 00:00:00 2001 From: Sashi Penta Date: Tue, 12 Aug 2014 11:16:48 -0700 Subject: [PATCH] Patch1: 1. Renamed all container stuff to collection stuff both in C/C++. 2. Simplified function names in C stack. 3. Implemented OCUnbindResource in C stack. 4. Implemented unbindResource(s) in C++ stack. Patch2: Incorporated Doug and Erich's review comments. Change-Id: I32d8f7248b65e2fee9a05ca52d22f7d247748984 --- OCLib/OCPlatform.cpp | 54 ++++++++++++++++++------- csdk/stack/include/ocstack.h | 32 ++++++++++----- csdk/stack/src/ocstack.c | 94 ++++++++++++++++++++++++++++++++++---------- include/OCPlatform.h | 56 +++++++++++++------------- 4 files changed, 164 insertions(+), 72 deletions(-) diff --git a/OCLib/OCPlatform.cpp b/OCLib/OCPlatform.cpp index 1394149..4bb7c9c 100644 --- a/OCLib/OCPlatform.cpp +++ b/OCLib/OCPlatform.cpp @@ -130,7 +130,7 @@ namespace OC try{ result = m_server->registerResource(resourceHandle, resourceURI, resourceTypeName, resourceInterface, entityHandler, resourceProperty); } - catch(std::exception e) // define our own expception. + catch(std::exception e) // TODO: define our own expception. { throw e; } @@ -139,28 +139,56 @@ namespace OC return result; } - // TODO: Implement - OCStackResult OCPlatform::unbindResource(OCResourceHandle containerHandle, OCResourceHandle resourceHandle) + OCStackResult OCPlatform::unbindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) { OCStackResult result = OC_STACK_OK; + + try { + result = OCUnBindResource(collectionHandle, resourceHandle); + } + catch(std::exception e) // TODO: define our own expception. + { + throw e; + } + return result; } - // TODO: Implement - OCStackResult OCPlatform::unbindResources(OCResourceHandle containerHandle, std::vector& resourceHandleList) + OCStackResult OCPlatform::unbindResources(const OCResourceHandle collectionHandle, const std::vector& resourceHandles) { OCStackResult result = OC_STACK_OK; + + try { + + for(const OCResourceHandle& handle : resourceHandles) + { + result = OCUnBindResource(collectionHandle, handle); + + if(result != OC_STACK_OK) + { + // TODO Should we unbind the previous successful ones? + // TODO should we return which are succesful + // Currently just returns with any failure + return result; + } + } + } + catch(std::exception e) // TODO : define our own exception + { + throw e; + } + return result; } - OCStackResult bindResource(const OCResourceHandle containerHandle, const OCResourceHandle resourceHandle) + OCStackResult bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle) { OCStackResult result = OC_STACK_OK; try { - result = OCBindContainedResourceToResource(containerHandle, resourceHandle); + result = OCBindResource(collectionHandle, resourceHandle); } - catch(std::exception e) + catch(std::exception e) // TODO : define our own exception { throw e; } @@ -168,17 +196,15 @@ namespace OC return result; } - OCStackResult bindResources(const OCResourceHandle containerHandle, const std::vector& resourceHandles) + OCStackResult bindResources(const OCResourceHandle collectionHandle, const std::vector& resourceHandles) { OCStackResult result = OC_STACK_OK; try { - std::vector::const_iterator it; - - for(it = resourceHandles.begin(); it != resourceHandles.end(); it++) + for(const OCResourceHandle& handle : resourceHandles) { - result = OCBindContainedResourceToResource(containerHandle, *it); + result = OCBindResource(collectionHandle, handle); if(result != OC_STACK_OK) { @@ -189,7 +215,7 @@ namespace OC } } } - catch(std::exception e) + catch(std::exception e) // TODO : define our own exception { throw e; } diff --git a/csdk/stack/include/ocstack.h b/csdk/stack/include/ocstack.h index 6167834..a0ff4fe 100644 --- a/csdk/stack/include/ocstack.h +++ b/csdk/stack/include/ocstack.h @@ -294,21 +294,35 @@ OCStackResult OCCreateResource(OCResourceHandle *handle, uint8_t resourceProperties); /** - * Add a resource to a container resource. + * Add a resource to a collection resource. * - * @param containerHandle - handle to the container resource - * @param addedResourceHandle - handle to resource to be added to the container resource + * @param collectionHandle - handle to the collection resource + * @param resourceHandle - handle to resource to be added to the collection resource * * @return * OC_STACK_OK - no errors * OC_STACK_ERROR - stack process error + * OC_STACK_INVALID_PARAM - invalid collectionhandle */ -OCStackResult OCBindContainedResourceToResource(OCResourceHandle containerHandle, OCResourceHandle addedResourceHandle); +OCStackResult OCBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle); + +/** + * Remove a resource from a collection resource. + * + * @param collectionHandle - handle to the collection resource + * @param resourceHandle - handle to resource to be removed from the collection resource + * + * @return + * OC_STACK_OK - no errors + * OC_STACK_ERROR - stack process error + * OC_STACK_INVALID_PARAM - invalid collectionhandle + */ +OCStackResult OCUnBindResource(OCResourceHandle collectionHandle, OCResourceHandle resourceHandle); /** * Bind a resourcetype to a resource. * - * @param handle - handle to the container resource + * @param handle - handle to the resource * @param resourceTypeName - name of resource type. Example: "core.led" * * @return @@ -320,7 +334,7 @@ OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle, /** * Bind a resource interface to a resource. * - * @param handle - handle to the container resource + * @param handle - handle to the resource * @param resourceInterfaceName - name of resource interface. Example: "core.rw" * * @return @@ -472,16 +486,16 @@ const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index); uint8_t OCGetResourceInterfaceAllowedMethods(OCResourceHandle handle, uint8_t index); /** - * Get name of resource interface of the resource. + * Get resource handle from the collection resource by index. * - * @param containerHandle - handle of container resource + * @param collectionHandle - handle of collection resource * @param index - index of contained resource, 0 to Count - 1 * * @return * handle to contained resource - if resource found * NULL - resource not found */ -OCResourceHandle OCGetContainedResourceHandle(OCResourceHandle containerHandle, uint8_t index); +OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle, uint8_t index); /** * Get the entity handler for a resource. diff --git a/csdk/stack/src/ocstack.c b/csdk/stack/src/ocstack.c index 8237f07..fb29584 100644 --- a/csdk/stack/src/ocstack.c +++ b/csdk/stack/src/ocstack.c @@ -523,35 +523,36 @@ exit: } /** - * Add a resource to a container resource. + * Add a resource to a collection resource. * - * @param containerHandle - handle to the container resource - * @param addedResourceHandle - handle to resource to be added to the container resource + * @param collectionHandle - handle to the collection resource + * @param resourceHandle - handle to resource to be added to the collection resource * * @return * OC_STACK_OK - no errors * OC_STACK_ERROR - stack process error + * OC_STACK_INVALID_PARAM - invalid collectionhandle */ -OCStackResult OCBindContainedResourceToResource( - OCResourceHandle containerHandle, OCResourceHandle addedResourceHandle) { +OCStackResult OCBindResource( + OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) { OCResource *resource; uint8_t i; - OC_LOG(INFO, TAG, PCF("Entering OCBindContainedResourceToResource")); + OC_LOG(INFO, TAG, PCF("Entering OCBindResource")); // Validate parameters - VERIFY_NON_NULL(containerHandle, ERROR, OC_STACK_ERROR); - VERIFY_NON_NULL(addedResourceHandle, ERROR, OC_STACK_ERROR); + VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR); + VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR); // Container cannot contain itself - if (containerHandle == addedResourceHandle) { - OC_LOG(ERROR, TAG, PCF("Added handle equals container handle")); + if (collectionHandle == resourceHandle) { + OC_LOG(ERROR, TAG, PCF("Added handle equals collection handle")); return OC_STACK_INVALID_PARAM; } // Use the handle to find the resource in the resource linked list - resource = findResource((OCResource *) containerHandle); + resource = findResource((OCResource *) collectionHandle); if (!resource) { - OC_LOG(ERROR, TAG, PCF("Resource not found")); + OC_LOG(ERROR, TAG, PCF("Collection handle not found")); return OC_STACK_INVALID_PARAM; } @@ -559,20 +560,71 @@ OCStackResult OCBindContainedResourceToResource( // If found, add it and return success for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) { if (!resource->rsrcResources[i]) { - resource->rsrcResources[i] = (OCResourceHandle) addedResourceHandle; + resource->rsrcResources[i] = (OCResourceHandle) resourceHandle; OC_LOG(INFO, TAG, PCF("resource bound")); return OC_STACK_OK; } } - // Unable to add addedResourceHandle, so return error + // Unable to add resourceHandle, so return error return OC_STACK_ERROR; } /** + * Remove a resource from a collection resource. + * + * @param collectionHandle - handle to the collection resource + * @param resourceHandle - handle to resource to be added to the collection resource + * + * @return + * OC_STACK_OK - no errors + * OC_STACK_ERROR - stack process error + * OC_STACK_INVALID_PARAM - invalid collectionHandle + */ +OCStackResult OCUnBindResource( + OCResourceHandle collectionHandle, OCResourceHandle resourceHandle) { + OCResource *resource; + uint8_t i; + + OC_LOG(INFO, TAG, PCF("Entering OCUnBindResource")); + + // Validate parameters + VERIFY_NON_NULL(collectionHandle, ERROR, OC_STACK_ERROR); + VERIFY_NON_NULL(resourceHandle, ERROR, OC_STACK_ERROR); + // Container cannot contain itself + if (collectionHandle == resourceHandle) { + OC_LOG(ERROR, TAG, PCF("removing handle equals collection handle")); + return OC_STACK_INVALID_PARAM; + } + + // Use the handle to find the resource in the resource linked list + resource = findResource((OCResource *) collectionHandle); + if (!resource) { + OC_LOG(ERROR, TAG, PCF("Collection handle not found")); + return OC_STACK_INVALID_PARAM; + } + + // Look for an open slot to add add the child resource. + // If found, add it and return success + for (i = 0; i < MAX_CONTAINED_RESOURCES; i++) { + if (resourceHandle == resource->rsrcResources[i]) { + resource->rsrcResources[i] = (OCResourceHandle) 0; + OC_LOG(INFO, TAG, PCF("resource unbound")); + return OC_STACK_OK; + } + } + + OC_LOG(INFO, TAG, PCF("resource not found in collection")); + + // Unable to add resourceHandle, so return error + return OC_STACK_ERROR; +} + + +/** * Bind a resourcetype to a resource. * - * @param handle - handle to the container resource + * @param handle - handle to the resource * @param resourceTypeName - name of resource type. Example: "core.led" * * @return @@ -635,7 +687,7 @@ OCStackResult OCBindResourceTypeToResource(OCResourceHandle handle, /** * Bind a resourceinterface to a resource. * - * @param handle - handle to the container resource + * @param handle - handle to the resource * @param resourceInterfaceName - name of resource interface. Example: "core.rw" * * @return @@ -916,16 +968,16 @@ const char *OCGetResourceInterfaceName(OCResourceHandle handle, uint8_t index) { } /** - * Get name of resource interface of the resource. + * Get resource handle from the collection resource by index. * - * @param containerHandle - handle of container resource + * @param collectionHandle - handle of collection resource * @param index - index of contained resource, 0 to Count - 1 * * @return - * handle to contained resource - if resource found + * handle to resource - if resource found * NULL - resource not found */ -OCResourceHandle OCGetContainedResourceHandle(OCResourceHandle containerHandle, +OCResourceHandle OCGetResourceHandleFromCollection(OCResourceHandle collectionHandle, uint8_t index) { OCResource *resource; @@ -935,7 +987,7 @@ OCResourceHandle OCGetContainedResourceHandle(OCResourceHandle containerHandle, return NULL; } - resource = findResource((OCResource *) containerHandle); + resource = findResource((OCResource *) collectionHandle); if (!resource) { return NULL; } diff --git a/include/OCPlatform.h b/include/OCPlatform.h index e8cbf9f..f1537d8 100644 --- a/include/OCPlatform.h +++ b/include/OCPlatform.h @@ -128,83 +128,83 @@ namespace OC uint8_t resourceProperty); /** - * Add a resource to a container resource. + * Add a resource to a collection resource. * - * @param containerHandle - handle to the container resource - * @param addedResourceHandle - handle to resource to be added to the container resource + * @param collectionHandle - handle to the collection resource + * @param addedResourceHandle - handle to resource to be added to the collection resource * * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
* NOTE: OCStackResult is defined in ocstack.h.
- * NOTE: bindResourceToContainer must be used only after the both container resource and - * resource to add under a container are created and respective handles obtained
+ * NOTE: bindResource must be used only after the both collection resource and + * resource to add under a collections are created and respective handles obtained
* Example:
* Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface, entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface, entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
- * Step 3: bindResourceToContainer(homeResourceHandle, kitchenResourceHandle);
+ * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);
* At the end of Step 3, resource "a/home" will contain a reference to "a/kitchen".
*/ - OCStackResult bindResource(const OCResourceHandle containerHandle, const OCResourceHandle resourceHandle); + OCStackResult bindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle); /** - * Add multiple resources to a container resource. + * Add multiple resources to a collection resource. * - * @param containerHandle - handle to the container resource - * @param addedResourceHandleList reference to list of resource handles to be added to the container resource + * @param collectionHandle - handle to the collection resource + * @param addedResourceHandleList reference to list of resource handles to be added to the collection resource * * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
* NOTE: OCStackResult is defined in ocstack.h.
- * NOTE: bindResourcesToContainer must be used only after the both container resource and - * list of resources to add under a container are created and respective handles obtained
+ * NOTE: bindResources must be used only after the both collection resource and + * list of resources to add under a collection are created and respective handles obtained
* Example:
* Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface, homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface, kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface, roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 4: std::vector rList; rList.push_back(kitchenResourceHandle); rList.push_back(roomResourceHandle);
- * Step 5: bindResourceToContainer(homeResourceHandle, rList);
+ * Step 5: bindResource(homeResourceHandle, rList);
* At the end of Step 5, resource "a/home" will contain a references to "a/kitchen" and "a/room"
*/ - OCStackResult bindResources(const OCResourceHandle containerHandle, const std::vector& addedResourceHandleList); + OCStackResult bindResources(const OCResourceHandle collectionHandle, const std::vector& addedResourceHandleList); /** - * Unbind a resource from a container resource. + * Unbind a resource from a collection resource. * - * @param containerHandle - handle to the container resource - * @param resourceHandle resource handle to be unbound from the container resource + * @param collectionHandle - handle to the collection resource + * @param resourceHandle resource handle to be unbound from the collection resource * * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
* NOTE: OCStackResult is defined in ocstack.h.
- * NOTE: unbindResource must be used only after the both container resource and - * resource to unbind from a container are created and respective handles obtained
+ * NOTE: unbindResource must be used only after the both collection resource and + * resource to unbind from a collection are created and respective handles obtained
* Example
* Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface, entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface, entityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
- * Step 3: bindResourceToContainer(homeResourceHandle, kitchenResourceHandle);
+ * Step 3: bindResource(homeResourceHandle, kitchenResourceHandle);
* Step 4: unbindResource(homeResourceHandle, kitchenResourceHandle);
* At the end of Step 4, resource "a/home" will no longer reference "a/kitchen".
*/ - OCStackResult unbindResource(OCResourceHandle containerHandle, OCResourceHandle resourceHandle); + OCStackResult unbindResource(const OCResourceHandle collectionHandle, const OCResourceHandle resourceHandle); /** - * Unbind resources from a container resource. + * Unbind resources from a collection resource. * - * @param containerHandle - handle to the container resource - * @param resourceHandleList List of resource handles to be unbound from the container resource + * @param collectionHandle - handle to the collection resource + * @param resourceHandleList List of resource handles to be unbound from the collection resource * * @return OCStackResult return value of this API. Returns OC_STACK_OK if success.
* * NOTE: OCStackResult is defined in ocstack.h.
- * NOTE: unbindResources must be used only after the both container resource and - * list of resources resource to unbind from a container are created and respective handles obtained.
+ * NOTE: unbindResources must be used only after the both collection resource and + * list of resources resource to unbind from a collection are created and respective handles obtained.
* Example
* Step 1: registerResource(homeResourceHandle, "a/home", "home", Link_Interface, homeEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 2: registerResource(kitchenResourceHandle, "a/kitchen", "kitchen", Link_Interface, kitchenEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 3: registerResource(roomResourceHandle, "a/room", "room", Link_Interface, roomEntityHandler, OC_DISCOVERABLE | OC_OBSERVABLE);
* Step 4: std::vector rList; rList.push_back(kitchenResourceHandle); rList.push_back(roomResourceHandle);
- * Step 5: bindResourceToContainer(homeResourceHandle, rList);
+ * Step 5: bindResource(homeResourceHandle, rList);
* Step 6: unbindResources(homeResourceHandle, rList);
* At the end of Step 6, resource "a/home" will no longer reference to "a/kitchen" and "a/room"
*/ - OCStackResult unbindResources(OCResourceHandle containerHandle, std::vector& resourceHandleList); + OCStackResult unbindResources(const OCResourceHandle collectionHandle, const std::vector& resourceHandleList); /** * Binds a type to a particular resource -- 2.7.4