RD updates
[platform/upstream/iotivity.git] / resource / csdk / resource-directory / src / rd_client.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 a
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 "rd_client.h"
21
22 #include "oic_malloc.h"
23 #include "oic_string.h"
24 #include "octypes.h"
25 #include "ocstack.h"
26 #include "ocpayload.h"
27 #include "payload_logging.h"
28
29 #define TAG "RD_CLIENT"
30
31 #ifdef RD_CLIENT
32
33 OCStackResult OCRDDiscover(OCConnectivityType connectivityType, OCCallbackData *cbBiasFactor,
34                            OCQualityOfService qos)
35 {
36     if (!cbBiasFactor || !cbBiasFactor->cb)
37     {
38         OIC_LOG(DEBUG, TAG, "No callback function specified.");
39         return OC_STACK_INVALID_CALLBACK;
40     }
41
42     /* Start a discovery query*/
43     char queryUri[MAX_URI_LENGTH] = { '\0' };
44     snprintf(queryUri, MAX_URI_LENGTH, "coap://%s%s", OC_MULTICAST_PREFIX, OC_RSRVD_RD_URI);
45     OIC_LOG_V(DEBUG, TAG, "Querying RD: %s\n", queryUri);
46
47     return OCDoResource(NULL, OC_REST_DISCOVER, queryUri, NULL, NULL, connectivityType, qos,
48                         cbBiasFactor, NULL, 0);
49 }
50
51 OCStackResult OCRDPublish(const char *host, OCConnectivityType connectivityType,
52                           OCResourceHandle *resourceHandles, uint8_t nHandles,
53                           OCCallbackData *cbData, OCQualityOfService qos)
54 {
55     // Validate input parameters.
56     if (!host)
57     {
58         return OC_STACK_INVALID_IP;
59     }
60
61     if (!cbData || !cbData->cb)
62     {
63         return OC_STACK_INVALID_CALLBACK;
64     }
65
66     // Get Device ID from stack.
67     const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
68
69     return OCRDPublishWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
70                                    cbData, qos);
71 }
72
73 OCStackResult OCRDPublishWithDeviceId(const char *host, const unsigned char *id,
74                                       OCConnectivityType connectivityType,
75                                       OCResourceHandle *resourceHandles, uint8_t nHandles,
76                                       OCCallbackData *cbData, OCQualityOfService qos)
77 {
78     // Validate input parameters.
79     if (!host || !cbData || !cbData->cb || !id)
80     {
81         return OC_STACK_INVALID_CALLBACK;
82     }
83
84     OIC_LOG_V(DEBUG, TAG, "Publish Resource to RD with device id [%s]", id);
85
86     OCResourceHandle *pubResHandle = resourceHandles;
87     uint8_t nPubResHandles = nHandles;
88
89     // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
90     if (!pubResHandle && !nPubResHandles)
91     {
92         OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
93
94         // get "/oic/d" and "/oic/p" resource handle from stack.
95         defaultResHandles[0] = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
96         defaultResHandles[1] = OCGetResourceHandleAtUri(OC_RSRVD_PLATFORM_URI);
97
98         for (uint8_t j = 0; j < OIC_RD_DEFAULT_RESOURCE; j++)
99         {
100             if (defaultResHandles[j])
101             {
102                 OIC_LOG_V(DEBUG, TAG, "Add virtual resource(%s) to resource handle list",
103                           OCGetResourceUri(defaultResHandles[j]));
104             }
105         }
106
107         pubResHandle = defaultResHandles;
108         nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
109     }
110
111     char targetUri[MAX_URI_LENGTH] = { 0 };
112     snprintf(targetUri, MAX_URI_LENGTH, "%s%s?rt=%s", host,
113              OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
114     OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
115
116     OCRepPayload *rdPayload =  (OCRepPayload *)OCRepPayloadCreate();
117     if (!rdPayload)
118     {
119         return OC_STACK_NO_MEMORY;
120     }
121
122     const char *deviceId = OCGetServerInstanceIDString();
123     if (deviceId)
124     {
125         OCRepPayloadSetPropString(rdPayload, OC_RSRVD_DEVICE_ID, deviceId);
126     }
127     OCRepPayloadSetPropInt(rdPayload, OC_RSRVD_DEVICE_TTL, OIC_RD_PUBLISH_TTL);
128
129     const OCRepPayload *linkArr[nPubResHandles];
130     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {nPubResHandles, 0, 0};
131
132     for (uint8_t j = 0; j < nPubResHandles; j++)
133     {
134         OCResourceHandle handle = pubResHandle[j];
135         if (handle)
136         {
137             OCRepPayload *link = OCRepPayloadCreate();
138
139             const char *uri = OCGetResourceUri(handle);
140             if (uri)
141             {
142                 OCRepPayloadSetPropString(link, OC_RSRVD_HREF, uri);
143             }
144
145             uint8_t numElement = 0;
146             if (OC_STACK_OK == OCGetNumberOfResourceTypes(handle, &numElement))
147             {
148                 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
149                 char **rt = (char **)OICMalloc(sizeof(char *) * numElement);
150                 for (uint8_t i = 0; i < numElement; ++i)
151                 {
152                     const char *value = OCGetResourceTypeName(handle, i);
153                     OIC_LOG_V(DEBUG, TAG, "value: %s", value);
154                     rt[i] = OICStrdup(value);
155                 }
156                 OCRepPayloadSetStringArray(link, OC_RSRVD_RESOURCE_TYPE, (const char **)rt, rtDim);
157             }
158
159             numElement = 0;
160             if (OC_STACK_OK == OCGetNumberOfResourceInterfaces(handle, &numElement))
161             {
162                 size_t ifDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
163                 char **itf = (char **)OICMalloc(sizeof(char *) * numElement);
164                 for (uint8_t i = 0; i < numElement; ++i)
165                 {
166                     const char *value = OCGetResourceInterfaceName(handle, i);
167                     OIC_LOG_V(DEBUG, TAG, "value: %s", value);
168                     itf[i] = OICStrdup(value);
169                 }
170                 OCRepPayloadSetStringArray(link, OC_RSRVD_INTERFACE, (const char **)itf, ifDim);
171             }
172
173             uint8_t ins = 0;
174             if (OCGetResourceIns(handle, &ins))
175             {
176                 OCRepPayloadSetPropInt(link, OC_RSRVD_INS, ins);
177             }
178
179             size_t mtDim[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
180             char **mediaType = (char **)OICMalloc(sizeof(char *) * 1);
181             mediaType[0] = OICStrdup(DEFAULT_MESSAGE_TYPE);
182             OCRepPayloadSetStringArray(link, OC_RSRVD_MEDIA_TYPE, (const char **)mediaType,
183             mtDim);
184
185             OCResourceProperty p = OCGetResourceProperties(handle);
186             p = (OCResourceProperty) ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE));
187             OCRepPayload *policy = OCRepPayloadCreate();
188             OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, p);
189             OCRepPayloadSetPropObjectAsOwner(link, OC_RSRVD_POLICY, policy);
190
191             linkArr[j] = link;
192         }
193     }
194
195     OCRepPayloadSetPropObjectArray(rdPayload, OC_RSRVD_LINKS, linkArr, dimensions);
196     OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
197
198     if (OC_STACK_OK == OCStopMulticastServer())
199     {
200         OIC_LOG_V(DEBUG, TAG, "Stopped receiving the multicast traffic.");
201     }
202     else
203     {
204         OIC_LOG_V(DEBUG, TAG, "Failed stopping the multicast traffic.");
205     }
206     return OCDoResource(NULL, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
207                         connectivityType, qos, cbData, NULL, 0);
208 }
209
210 OCStackResult OCRDDelete(const char *host, OCConnectivityType connectivityType,
211                          OCResourceHandle *resourceHandles, uint8_t nHandles,
212                          OCCallbackData *cbData, OCQualityOfService qos)
213 {
214     // Validate input parameters
215     if (!host)
216     {
217         return OC_STACK_INVALID_IP;
218     }
219
220     if (!cbData || !cbData->cb)
221     {
222         return OC_STACK_INVALID_CALLBACK;
223     }
224
225     const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
226
227     return OCRDDeleteWithDeviceId(host, id, connectivityType, resourceHandles, nHandles,
228                                   cbData, qos);
229 }
230
231 OCStackResult OCRDDeleteWithDeviceId(const char *host, const unsigned char *id,
232                                      OCConnectivityType connectivityType,
233                                      OCResourceHandle *resourceHandles, uint8_t nHandles,
234                                      OCCallbackData *cbData, OCQualityOfService qos)
235 {
236     // Validate input parameters
237     if (!host || !cbData || !cbData->cb || !id)
238     {
239         return OC_STACK_INVALID_CALLBACK;
240     }
241
242     OIC_LOG_V(DEBUG, TAG, "Delete Resource to RD with device id [%s]", id);
243
244     char targetUri[MAX_URI_LENGTH] = { 0 };
245     snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
246
247     char queryParam[MAX_URI_LENGTH] = { 0 };
248     for (uint8_t j = 0; j < nHandles; j++)
249     {
250         OCResource *handle = (OCResource *) resourceHandles[j];
251         uint8_t ins = 0;
252         OCGetResourceIns(handle, &ins);
253         snprintf(queryParam, MAX_URI_LENGTH, "&ins=%u", ins);
254     }
255
256     OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
257     OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
258
259     return OCDoResource(NULL, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
260                         qos, cbData, NULL, 0);
261 }
262
263 #endif