Fixes for build errors hit by some versions of Visual Studio
[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 "iotivity_config.h"
21 #include "oicresourcedirectory.h"
22
23 #include "rdpayload.h"
24 #include "oic_malloc.h"
25 #include "oic_string.h"
26 #include "octypes.h"
27 #include "ocstack.h"
28 #include "ocpayload.h"
29 #include "rdpayload.h"
30 #include "ocresource.h"
31 #include "payload_logging.h"
32
33 #define TAG "OIC_RI_RESOURCE_DIRECTORY"
34
35 #ifdef RD_CLIENT
36 OCStackResult OCRDPublish(const char *host, OCConnectivityType connectivityType,
37                           OCResourceHandle *resourceHandles, uint8_t nHandles,
38                           OCCallbackData *cbData, OCQualityOfService qos)
39 {
40     // Validate input parameters.
41     if (!host || !cbData || !cbData->cb)
42     {
43         return OC_STACK_INVALID_CALLBACK;
44     }
45
46     // Get Device ID from stack.
47     const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
48
49     return OCRDPublishWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
50                                    cbData, qos);
51 }
52
53 OCStackResult OCRDPublishWithDeviceId(const char *host, const unsigned char *id,
54                                       OCConnectivityType connectivityType,
55                                       OCResourceHandle *resourceHandles, uint8_t nHandles,
56                                       OCCallbackData *cbData, OCQualityOfService qos)
57 {
58     // Validate input parameters.
59     if (!host || !cbData || !cbData->cb || !id)
60     {
61         return OC_STACK_INVALID_CALLBACK;
62     }
63
64     OIC_LOG_V(DEBUG, TAG, "Publish Resource to RD with device id [%s]", id);
65
66     OCResourceHandle *pubResHandle = resourceHandles;
67     uint8_t nPubResHandles = nHandles;
68
69     // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
70     if (!pubResHandle && !nPubResHandles)
71     {
72         OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
73
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);
77
78         for (uint8_t j = 0; j < OIC_RD_DEFAULT_RESOURCE; j++)
79         {
80             if (defaultResHandles[j])
81             {
82                 OIC_LOG_V(DEBUG, TAG, "Add virtual resource(%s) to resource handle list",
83                           OCGetResourceUri(defaultResHandles[j]));
84             }
85         }
86
87         pubResHandle = defaultResHandles;
88         nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
89     }
90
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);
95
96     OCPayload *rdPayload = (OCPayload *) OCRDPublishPayloadCreate(id, pubResHandle, nPubResHandles,
97                                                                   OIC_RD_PUBLISH_TTL);
98     if (!rdPayload)
99     {
100         OIC_LOG_V(ERROR, TAG, "Failed to create RD Payload");
101         return OC_STACK_NO_MEMORY;
102     }
103
104     OIC_LOG(DEBUG, TAG, "Create RD payload successfully");
105
106     return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
107                         connectivityType, qos, cbData, NULL, 0);
108 }
109
110 OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
111                          OCResourceHandle *resourceHandles, uint8_t nHandles,
112                          OCCallbackData *cbData, OCQualityOfService qos)
113 {
114     // Validate input parameters
115     if (!host || !cbData || !cbData->cb)
116     {
117         return OC_STACK_INVALID_CALLBACK;
118     }
119
120     const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
121
122     return OCRDDeleteWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
123                                   cbData, qos);
124 }
125
126 OCStackResult OCRDDeleteWithDeviceId(const char *host, const unsigned char *id,
127                                      OCConnectivityType connectivityType,
128                                      OCResourceHandle *resourceHandles, uint8_t nHandles,
129                                      OCCallbackData *cbData, OCQualityOfService qos)
130 {
131     // Validate input parameters
132     if (!host || !cbData || !cbData->cb || !id)
133     {
134         return OC_STACK_INVALID_CALLBACK;
135     }
136
137     OIC_LOG_V(DEBUG, TAG, "Delete Resource to RD with device id [%s]", id);
138
139     char targetUri[MAX_URI_LENGTH] = { 0 };
140     snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
141
142     char queryParam[MAX_URI_LENGTH] = { 0 };
143     for (uint8_t j = 0; j < nHandles; j++)
144     {
145         OCResource *handle = (OCResource *) resourceHandles[j];
146         snprintf(queryParam, MAX_URI_LENGTH, "&ins=%d", handle->ins);
147     }
148
149     OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
150     OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
151
152     return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
153                         qos, cbData, NULL, 0);
154 }
155 #endif