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 OCResourceHandle *pubResHandle = resourceHandles;
46 uint8_t nPubResHandles = nHandles;
48 // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
49 if (!pubResHandle && !nPubResHandles)
51 OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
53 // get "/oic/d" resource handle from stack.
54 defaultResHandles[0] = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
55 // get "/oic/p" resource handle from stack.
56 defaultResHandles[1] = OCGetResourceHandleAtUri(OC_RSRVD_PLATFORM_URI);
58 pubResHandle = defaultResHandles;
59 nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
62 char targetUri[MAX_URI_LENGTH] = { 0 };
63 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?rt=%s", host,
64 OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
65 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
67 OCPayload *rdPayload = (OCPayload *) OCRDPublishPayloadCreate(pubResHandle, nPubResHandles,
71 OIC_LOG_V(ERROR, TAG, "Failed to create RD Payload");
72 return OC_STACK_NO_MEMORY;
75 OIC_LOG(DEBUG, TAG, "Create RD payload successfully");
77 return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
78 connectivityType, qos, cbData, NULL, 0);
81 OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
82 OCResourceHandle resourceHandles[], uint8_t nHandles,
83 OCCallbackData *cbData, OCQualityOfService qos)
85 // Validate input parameters
86 if (!host || !cbData || !cbData->cb)
88 return OC_STACK_INVALID_CALLBACK;
91 const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
93 char targetUri[MAX_URI_LENGTH] = { 0 };
94 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
96 char queryParam[MAX_URI_LENGTH] = { 0 };
97 for (uint8_t j = 0; j < nHandles; j++)
99 OCResource *handle = (OCResource *) resourceHandles[j];
100 snprintf(queryParam, MAX_URI_LENGTH, "&ins=%d", handle->ins);
103 OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
104 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
106 return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
107 qos, cbData, NULL, 0);