1 //******************************************************************
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "iotivity_config.h"
21 #include "oicresourcedirectory.h"
23 #include "rdpayload.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
28 #include "ocpayload.h"
29 #include "rdpayload.h"
30 #include "ocresource.h"
31 #include "payload_logging.h"
33 #define TAG "OIC_RI_RESOURCE_DIRECTORY"
36 OCStackResult OCRDPublish(const char *host, OCConnectivityType connectivityType,
37 OCResourceHandle *resourceHandles, uint8_t nHandles,
38 OCCallbackData *cbData, OCQualityOfService qos)
40 // Validate input parameters.
41 if (!host || !cbData || !cbData->cb)
43 return OC_STACK_INVALID_CALLBACK;
46 // Get Device ID from stack.
47 const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
49 return OCRDPublishWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
53 OCStackResult OCRDPublishWithDeviceId(const char *host, const unsigned char *id,
54 OCConnectivityType connectivityType,
55 OCResourceHandle *resourceHandles, uint8_t nHandles,
56 OCCallbackData *cbData, OCQualityOfService qos)
58 // Validate input parameters.
59 if (!host || !cbData || !cbData->cb || !id)
61 return OC_STACK_INVALID_CALLBACK;
64 OIC_LOG_V(DEBUG, TAG, "Publish Resource to RD with device id [%s]", id);
66 OCResourceHandle *pubResHandle = resourceHandles;
67 uint8_t nPubResHandles = nHandles;
69 // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
70 if (!pubResHandle && !nPubResHandles)
72 OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
74 // get "/oic/d" and "/oic/p" resource handle from stack.
75 defaultResHandles[0] = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
76 defaultResHandles[1] = OCGetResourceHandleAtUri(OC_RSRVD_PLATFORM_URI);
78 for (uint8_t j = 0; j < OIC_RD_DEFAULT_RESOURCE; j++)
80 if (defaultResHandles[j])
82 OIC_LOG_V(DEBUG, TAG, "Add virtual resource(%s) to resource handle list",
83 OCGetResourceUri(defaultResHandles[j]));
87 pubResHandle = defaultResHandles;
88 nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
91 char targetUri[MAX_URI_LENGTH] = { 0 };
92 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?rt=%s", host,
93 OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
94 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
96 OCPayload *rdPayload = (OCPayload *) OCRDPublishPayloadCreate(id, pubResHandle, nPubResHandles,
100 OIC_LOG_V(ERROR, TAG, "Failed to create RD Payload");
101 return OC_STACK_NO_MEMORY;
104 OIC_LOG(DEBUG, TAG, "Create RD payload successfully");
106 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
107 connectivityType, qos, cbData, NULL, 0);
110 OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
111 OCResourceHandle *resourceHandles, uint8_t nHandles,
112 OCCallbackData *cbData, OCQualityOfService qos)
114 // Validate input parameters
115 if (!host || !cbData || !cbData->cb)
117 return OC_STACK_INVALID_CALLBACK;
120 const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
122 return OCRDDeleteWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
126 OCStackResult OCRDDeleteWithDeviceId(const char *host, const unsigned char *id,
127 OCConnectivityType connectivityType,
128 OCResourceHandle *resourceHandles, uint8_t nHandles,
129 OCCallbackData *cbData, OCQualityOfService qos)
131 // Validate input parameters
132 if (!host || !cbData || !cbData->cb || !id)
134 return OC_STACK_INVALID_CALLBACK;
137 OIC_LOG_V(DEBUG, TAG, "Delete Resource to RD with device id [%s]", id);
139 char targetUri[MAX_URI_LENGTH] = { 0 };
140 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
142 char queryParam[MAX_URI_LENGTH] = { 0 };
143 for (uint8_t j = 0; j < nHandles; j++)
145 OCResource *handle = (OCResource *) resourceHandles[j];
146 snprintf(queryParam, MAX_URI_LENGTH, "&ins=%d", handle->ins);
149 OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
150 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
152 return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
153 qos, cbData, NULL, 0);