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 "oicresourcedirectory.h"
22 #include "rdpayload.h"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
27 #include "ocpayload.h"
28 #include "rdpayload.h"
29 #include "ocresource.h"
30 #include "payload_logging.h"
32 #define TAG "OIC_RI_RESOURCE_DIRECTORY"
35 OCStackResult OCRDPublish(const char *host, OCConnectivityType connectivityType,
36 OCResourceHandle *resourceHandles, uint8_t nHandles,
37 OCCallbackData *cbData, OCQualityOfService qos)
39 // Validate input parameters.
40 if (!host || !cbData || !cbData->cb)
42 return OC_STACK_INVALID_CALLBACK;
45 // Get Device ID from stack.
46 const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
48 return OCRDPublishWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
52 OCStackResult OCRDPublishWithDeviceId(const char *host, const unsigned char *id,
53 OCConnectivityType connectivityType,
54 OCResourceHandle *resourceHandles, uint8_t nHandles,
55 OCCallbackData *cbData, OCQualityOfService qos)
57 // Validate input parameters.
58 if (!host || !cbData || !cbData->cb || !id)
60 return OC_STACK_INVALID_CALLBACK;
63 OIC_LOG_V(DEBUG, TAG, "Publish Resource to RD with device id [%s]", id);
65 OCResourceHandle *pubResHandle = resourceHandles;
66 uint8_t nPubResHandles = nHandles;
68 // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
69 if (!pubResHandle && !nPubResHandles)
71 OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
73 // get "/oic/d" and "/oic/p" resource handle from stack.
74 defaultResHandles[0] = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
75 defaultResHandles[1] = OCGetResourceHandleAtUri(OC_RSRVD_PLATFORM_URI);
77 for (uint8_t j = 0; j < OIC_RD_DEFAULT_RESOURCE; j++)
79 if (defaultResHandles[j])
81 OIC_LOG_V(DEBUG, TAG, "Add virtual resource(%s) to resource handle list",
82 OCGetResourceUri(defaultResHandles[j]));
86 pubResHandle = defaultResHandles;
87 nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
90 char targetUri[MAX_URI_LENGTH] = { 0 };
91 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?rt=%s", host,
92 OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
93 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
95 OCPayload *rdPayload = (OCPayload *) OCRDPublishPayloadCreate(id, pubResHandle, nPubResHandles,
99 OIC_LOG_V(ERROR, TAG, "Failed to create RD Payload");
100 return OC_STACK_NO_MEMORY;
103 OIC_LOG(DEBUG, TAG, "Create RD payload successfully");
105 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
106 connectivityType, qos, cbData, NULL, 0);
109 OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
110 OCResourceHandle *resourceHandles, uint8_t nHandles,
111 OCCallbackData *cbData, OCQualityOfService qos)
113 // Validate input parameters
114 if (!host || !cbData || !cbData->cb)
116 return OC_STACK_INVALID_CALLBACK;
119 const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
121 return OCRDDeleteWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
125 OCStackResult OCRDDeleteWithDeviceId(const char *host, const unsigned char *id,
126 OCConnectivityType connectivityType,
127 OCResourceHandle *resourceHandles, uint8_t nHandles,
128 OCCallbackData *cbData, OCQualityOfService qos)
130 // Validate input parameters
131 if (!host || !cbData || !cbData->cb || !id)
133 return OC_STACK_INVALID_CALLBACK;
136 OIC_LOG_V(DEBUG, TAG, "Delete Resource to RD with device id [%s]", id);
138 char targetUri[MAX_URI_LENGTH] = { 0 };
139 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
141 char queryParam[MAX_URI_LENGTH] = { 0 };
142 for (uint8_t j = 0; j < nHandles; j++)
144 OCResource *handle = (OCResource *) resourceHandles[j];
145 snprintf(queryParam, MAX_URI_LENGTH, "&ins=%d", handle->ins);
148 OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
149 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
151 return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
152 qos, cbData, NULL, 0);