Revert "Generate iotivity_config.h at build time"
[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     // Get Device ID from stack.
46     const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
47
48     return OCRDPublishWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
49                                    cbData, qos);
50 }
51
52 OCStackResult OCRDPublishWithDeviceId(const char *host, const unsigned char *id,
53                                       OCConnectivityType connectivityType,
54                                       OCResourceHandle *resourceHandles, uint8_t nHandles,
55                                       OCCallbackData *cbData, OCQualityOfService qos)
56 {
57     // Validate input parameters.
58     if (!host || !cbData || !cbData->cb || !id)
59     {
60         return OC_STACK_INVALID_CALLBACK;
61     }
62
63     OIC_LOG_V(DEBUG, TAG, "Publish Resource to RD with device id [%s]", id);
64
65     OCResourceHandle *pubResHandle = resourceHandles;
66     uint8_t nPubResHandles = nHandles;
67
68     // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
69     if (!pubResHandle && !nPubResHandles)
70     {
71         OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
72
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);
76
77         for (uint8_t j = 0; j < OIC_RD_DEFAULT_RESOURCE; j++)
78         {
79             if (defaultResHandles[j])
80             {
81                 OIC_LOG_V(DEBUG, TAG, "Add virtual resource(%s) to resource handle list",
82                           OCGetResourceUri(defaultResHandles[j]));
83             }
84         }
85
86         pubResHandle = defaultResHandles;
87         nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
88     }
89
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);
94
95     OCPayload *rdPayload = (OCPayload *) OCRDPublishPayloadCreate(id, pubResHandle, nPubResHandles,
96                                                                   OIC_RD_PUBLISH_TTL);
97     if (!rdPayload)
98     {
99         OIC_LOG_V(ERROR, TAG, "Failed to create RD Payload");
100         return OC_STACK_NO_MEMORY;
101     }
102
103     OIC_LOG(DEBUG, TAG, "Create RD payload successfully");
104
105     return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
106                         connectivityType, qos, cbData, NULL, 0);
107 }
108
109 OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
110                          OCResourceHandle *resourceHandles, uint8_t nHandles,
111                          OCCallbackData *cbData, OCQualityOfService qos)
112 {
113     // Validate input parameters
114     if (!host || !cbData || !cbData->cb)
115     {
116         return OC_STACK_INVALID_CALLBACK;
117     }
118
119     const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
120
121     return OCRDDeleteWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
122                                   cbData, qos);
123 }
124
125 OCStackResult OCRDDeleteWithDeviceId(const char *host, const unsigned char *id,
126                                      OCConnectivityType connectivityType,
127                                      OCResourceHandle *resourceHandles, uint8_t nHandles,
128                                      OCCallbackData *cbData, OCQualityOfService qos)
129 {
130     // Validate input parameters
131     if (!host || !cbData || !cbData->cb || !id)
132     {
133         return OC_STACK_INVALID_CALLBACK;
134     }
135
136     OIC_LOG_V(DEBUG, TAG, "Delete Resource to RD with device id [%s]", id);
137
138     char targetUri[MAX_URI_LENGTH] = { 0 };
139     snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
140
141     char queryParam[MAX_URI_LENGTH] = { 0 };
142     for (uint8_t j = 0; j < nHandles; j++)
143     {
144         OCResource *handle = (OCResource *) resourceHandles[j];
145         snprintf(queryParam, MAX_URI_LENGTH, "&ins=%d", handle->ins);
146     }
147
148     OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
149     OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
150
151     return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
152                         qos, cbData, NULL, 0);
153 }
154 #endif