void createResources() { light.state = false; OCResourceHandle fan; OCStackResult res = OCCreateResource(&fan, "core.fan", "oc.mi.def", "/a/fan", OCEntityHandlerFanCb, OC_DISCOVERABLE|OC_OBSERVABLE); OC_LOG_V(INFO, TAG, "Created fan resource with result: %s", getResult(res)); OCResourceHandle light; res = OCCreateResource(&light, "core.light", "oc.mi.def", "/a/light", OCEntityHandlerLightCb, OC_DISCOVERABLE|OC_OBSERVABLE); OC_LOG_V(INFO, TAG, "Created light resource with result: %s", getResult(res)); OCResourceHandle room; if(TEST == TEST_APP_COLL_EH) { res = OCCreateResource(&room, "core.room", "oc.mi.b", "/a/room", OCEntityHandlerRoomCb, OC_DISCOVERABLE); } else { res = OCCreateResource(&room, "core.room", "oc.mi.b", "/a/room", NULL, OC_DISCOVERABLE); } OC_LOG_V(INFO, TAG, "Created room resource with result: %s", getResult(res)); OCBindResourceInterfaceToResource(room, "oc.mi.ll"); OCBindResourceInterfaceToResource(room, "oc.mi.def"); res = OCBindResource(room, light); OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res)); res = OCBindResource(room, fan); OC_LOG_V(INFO, TAG, "OC Bind Contained Resource to resource: %s", getResult(res)); }
int add_interface_type(things_resource_s *resource, char *interface) { if (NULL != resource && NULL != interface) { if (NULL != resource->resource_handle) { iotivity_api_lock(); OCStackResult res = OCBindResourceInterfaceToResource(resource->resource_handle, interface); iotivity_api_unlock(); if (OC_STACK_OK == res) { return OC_STACK_OK; } } } return OC_STACK_ERROR; }
struct things_resource_s *create_collection_resource(struct things_server_builder_s *builder, char *uri, char *type) { things_resource_s *res = NULL; OCResourceHandle hd = NULL; uint8_t rsc_properties = OC_DISCOVERABLE | OC_OBSERVABLE; if (builder->handler == NULL) { THINGS_LOG_E(TAG, "Handler for serverbuilder is not registered"); return res; } #ifdef __SECURED__ rsc_properties |= OC_SECURE; #endif //#ifdef __SECURED__ iotivity_api_lock(); OCStackResult ret = OCCreateResource(&hd, type, OIC_INTERFACE_LINKEDLIST, uri, builder->handler, NULL, rsc_properties); if (ret != OC_STACK_OK) { THINGS_LOG_V(TAG, "Resource Creation Failed - ret = %d, %s", ret, uri); iotivity_api_unlock(); return NULL; } OCBindResourceTypeToResource(hd, OIC_RTYPE_COLLECTION_WK); OCBindResourceInterfaceToResource(hd, OIC_INTERFACE_BATCH); iotivity_api_unlock(); res = things_create_resource_inst(NULL, hd, NULL, NULL); if (NULL == res) { THINGS_LOG_E(TAG, "things_create_resource_inst is failed"); return res; } res->res_type = things_strdup(type); builder->gres_arr[builder->res_num++] = res; THINGS_LOG_V(TAG, "Created hd [%x], prop [0x%X] uri : %s", res->resource_handle, rsc_properties, uri); return res; }
OCStackResult InProcServerWrapper::bindInterfaceToResource( const OCResourceHandle& resourceHandle, const std::string& resourceInterfaceName) { auto cLock = m_csdkLock.lock(); OCStackResult result; if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCBindResourceInterfaceToResource(resourceHandle, resourceInterfaceName.c_str()); } else { result = OC_STACK_ERROR; } if (result != OC_STACK_OK) { throw OCException(OC::Exception::BIND_INTERFACE_FAILED, result); } return result; }
/** * @brief addInterface Adds and bind a interface to a resource * * @param resource The resource to bind a type to * @param interface Type to be bound and added * * @return OC_STACK_OK if successfully bound */ OCStackResult addInterface(OCBaseResourceT *resource, OCResourceInterface *interface) { OCResourceInterface *current = resource->interface; // Loop through the linked list of ind the tail element while(current->next != NULL) { current = current->next; } current->next = (OCResourceInterface*)malloc(sizeof(OCResourceInterface)); if(current->next == NULL) {printNoMemoryMsg(); return OC_STACK_NO_MEMORY;} memcpy(current->next, interface, sizeof(OCResourceInterface)); OCStackResult result = OCBindResourceInterfaceToResource(resource->handle, interface->name); //OIC_LOG_V(INFO, TAG, "Result of type binding: %s", getOCStackResult(result)); OIC_LOG_V(INFO, TAG, "Interface added: %s", interface->name); free(interface); return result; }
/** * @brief Initializes and creates a resource * * @param uri Path and name of the resource * @param interface Resource interfaces * @param type Resource types * @param properties Byte of allowed properties of the resources * @param OCIOhandler Callback called when a PUT request is received * * @return Pointer to the created resource */ OCBaseResourceT * createResource(char* uri, OCResourceType* type, OCResourceInterface* interface, uint8_t properties, OCIOHandler OCIOhandler) { // Create the resource OIC_LOG_V(DEBUG, TAG, "Creating resource with uri: %s\n", uri); OCBaseResourceT *resource = (OCBaseResourceT*)malloc(sizeof(OCBaseResourceT)); if(resource == NULL) {printNoMemoryMsg(); return NULL;} resource->uri = uri; resource->type = (OCResourceType*)malloc(sizeof(OCResourceType)); if(resource->type == NULL) {printNoMemoryMsg(); return NULL;} memcpy(resource->type, type, sizeof(OCResourceType)); resource->interface = (OCResourceInterface*)malloc(sizeof(OCResourceInterface)); if(resource->interface == NULL) {printNoMemoryMsg(); return NULL;} memcpy(resource->interface, interface, sizeof(OCResourceInterface)); resource->resourceProperties = properties; resource->OCIOhandler = OCIOhandler; resource->underObservation = false; resource->attribute = NULL; resource->next = NULL; OCStackResult res = OCCreateResource(&resource->handle, resource->type->resourcetypename, resource->interface->name, resource->uri, OCEntityHandlerCb, resource, resource->resourceProperties); OIC_LOG_V(DEBUG, TAG, "Created resource with OCStackResult: %s", res); // Add types OCResourceType *currentType = resource->type->next; while(currentType != NULL) { OIC_LOG_V(DEBUG, TAG, "Adding type in createResource: %s", currentType->resourcetypename); if(OCBindResourceTypeToResource(resource->handle, currentType->resourcetypename) != OC_STACK_OK) { OIC_LOG_V(ERROR, TAG, "Unable to bind type to resource: %s", currentType->resourcetypename); } currentType = currentType->next; } // Add interfaces OCResourceInterface *currentInterface = resource->interface->next; while(currentInterface != NULL) { OIC_LOG_V(DEBUG, TAG, "Adding interface in createResource: %s", currentInterface->name); if(OCBindResourceInterfaceToResource(resource->handle, currentInterface->name) != OC_STACK_OK) { OIC_LOG_V(ERROR, TAG, "Unable to bind interface to resource: %s", currentInterface->name); } currentInterface = currentInterface->next; } // Add the resource to the list of available resources pushResorcetoList(&resource); return resource; }