RD client features in base layer
[platform/upstream/iotivity.git] / resource / csdk / stack / src / oicresourcedirectory.c
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 #include "oicresourcedirectory.h"
21
22 #include "rdpayload.h"
23 #include "oic_malloc.h"
24 #include "oic_string.h"
25 #include "octypes.h"
26 #include "ocstack.h"
27 #include "ocpayload.h"
28 #include "rdpayload.h"
29 #include "ocresource.h"
30 #include "payload_logging.h"
31
32 #define TAG "OIC_RI_RESOURCE_DIRECTORY"
33
34 #ifdef RD_CLIENT
35 OCStackResult OCRDPublish(const char *host, OCConnectivityType connectivityType,
36                           OCResourceHandle resourceHandles[], uint8_t nHandles,
37                           OCCallbackData *cbData, OCQualityOfService qos)
38 {
39     // Validate input parameters
40     if (!host || !cbData || !cbData->cb)
41     {
42         return OC_STACK_INVALID_CALLBACK;
43     }
44
45     OCResourceHandle *pubResHandle = resourceHandles;
46     uint8_t nPubResHandles = nHandles;
47
48     // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
49     if (!pubResHandle && !nPubResHandles)
50     {
51         OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
52
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);
57
58         pubResHandle = defaultResHandles;
59         nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
60     }
61
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);
66
67     OCPayload *rdPayload = (OCPayload *) OCRDPublishPayloadCreate(pubResHandle, nPubResHandles,
68                                                                   OIC_RD_PUBLISH_TTL);
69     if (!rdPayload)
70     {
71         OIC_LOG_V(ERROR, TAG, "Failed to create RD Payload");
72         return OC_STACK_NO_MEMORY;
73     }
74
75     OIC_LOG(DEBUG, TAG, "Create RD payload successfully");
76
77     return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
78                         connectivityType, qos, cbData, NULL, 0);
79 }
80
81 OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
82                          OCResourceHandle resourceHandles[], uint8_t nHandles,
83                          OCCallbackData *cbData, OCQualityOfService qos)
84 {
85     // Validate input parameters
86     if (!host || !cbData || !cbData->cb)
87     {
88         return OC_STACK_INVALID_CALLBACK;
89     }
90
91     const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
92
93     char targetUri[MAX_URI_LENGTH] = { 0 };
94     snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
95
96     char queryParam[MAX_URI_LENGTH] = { 0 };
97     for (uint8_t j = 0; j < nHandles; j++)
98     {
99         OCResource *handle = (OCResource *) resourceHandles[j];
100         snprintf(queryParam, MAX_URI_LENGTH, "&ins=%d", handle->ins);
101     }
102
103     OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
104     OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
105
106     return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
107                         qos, cbData, NULL, 0);
108 }
109 #endif