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 a
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 "rd_client.h"
25 #include "oic_malloc.h"
26 #include "oic_string.h"
29 #include "ocpayload.h"
30 #include "payload_logging.h"
32 #define TAG "RD_CLIENT"
36 OCStackResult OCRDDiscover(OCDoHandle *handle, OCConnectivityType connectivityType,
37 OCCallbackData *cbBiasFactor, OCQualityOfService qos)
39 if (!cbBiasFactor || !cbBiasFactor->cb)
41 OIC_LOG(DEBUG, TAG, "No callback function specified.");
42 return OC_STACK_INVALID_CALLBACK;
45 /* Start a discovery query*/
46 char queryUri[MAX_URI_LENGTH] = { '\0' };
47 snprintf(queryUri, MAX_URI_LENGTH, "coap://%s%s", OC_MULTICAST_PREFIX, OC_RSRVD_RD_URI);
48 OIC_LOG_V(DEBUG, TAG, "Querying RD: %s\n", queryUri);
50 return OCDoResource(handle, OC_REST_DISCOVER, queryUri, NULL, NULL, connectivityType, qos,
51 cbBiasFactor, NULL, 0);
54 OCStackResult OCRDPublish(OCDoHandle *handle, const char *host,
55 OCConnectivityType connectivityType,
56 OCResourceHandle *resourceHandles, uint8_t nHandles,
57 OCCallbackData *cbData, OCQualityOfService qos)
59 // Validate input parameters.
62 return OC_STACK_INVALID_IP;
65 if (!cbData || !cbData->cb)
67 return OC_STACK_INVALID_CALLBACK;
70 // Get Device ID from stack.
71 const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
73 return OCRDPublishWithDeviceId(handle, host, id, connectivityType, resourceHandles,
74 nHandles, cbData, qos);
77 OCStackResult OCRDPublishWithDeviceId(OCDoHandle *handle, const char *host,
78 const unsigned char *id,
79 OCConnectivityType connectivityType,
80 OCResourceHandle *resourceHandles, uint8_t nHandles,
81 OCCallbackData *cbData, OCQualityOfService qos)
83 // Validate input parameters.
84 if (!host || !cbData || !cbData->cb || !id)
86 return OC_STACK_INVALID_CALLBACK;
89 OIC_LOG_V(DEBUG, TAG, "Publish Resource to RD with device id [%s]", id);
91 OCResourceHandle *pubResHandle = resourceHandles;
92 OCResourceHandle defaultResHandles[OIC_RD_DEFAULT_RESOURCE] = { 0 };
93 uint8_t nPubResHandles = nHandles;
95 // if resource handles is null, "/oic/p" and "/oic/d" resource will be published to RD.
98 // get "/oic/d" and "/oic/p" resource handle from stack.
99 defaultResHandles[0] = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI);
100 defaultResHandles[1] = OCGetResourceHandleAtUri(OC_RSRVD_PLATFORM_URI);
102 for (uint8_t j = 0; j < OIC_RD_DEFAULT_RESOURCE; j++)
104 if (defaultResHandles[j])
106 OIC_LOG_V(DEBUG, TAG, "Add virtual resource(%s) to resource handle list",
107 OCGetResourceUri(defaultResHandles[j]));
111 pubResHandle = defaultResHandles;
112 nPubResHandles = OIC_RD_DEFAULT_RESOURCE;
115 char targetUri[MAX_URI_LENGTH] = { 0 };
116 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?rt=%s", host,
117 OC_RSRVD_RD_URI, OC_RSRVD_RESOURCE_TYPE_RDPUBLISH);
118 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
120 OCRepPayload *rdPayload = (OCRepPayload *)OCRepPayloadCreate();
123 return OC_STACK_NO_MEMORY;
126 OCRepPayloadSetPropString(rdPayload, OC_RSRVD_DEVICE_ID, (const char *) id);
128 char *deviceName = NULL;
129 OCGetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, (void **) &deviceName);
132 OCRepPayloadSetPropString(rdPayload, OC_RSRVD_DEVICE_NAME, deviceName);
136 char *platformModelName = NULL;
137 OCGetPropertyValue(PAYLOAD_TYPE_PLATFORM, OC_RSRVD_MODEL_NUM, (void **) &platformModelName);
138 if (platformModelName)
140 OCRepPayloadSetPropString(rdPayload, OC_DATA_MODEL_NUMBER, platformModelName);
141 OICFree(platformModelName);
144 OCRepPayloadSetPropInt(rdPayload, OC_RSRVD_DEVICE_TTL, OIC_RD_PUBLISH_TTL);
146 OCRepPayload **linkArr = OICCalloc(nPubResHandles, sizeof(OCRepPayload *));
149 OCRepPayloadDestroy(rdPayload);
150 return OC_STACK_NO_MEMORY;
152 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {nPubResHandles, 0, 0};
154 for (uint8_t j = 0; j < nPubResHandles; j++)
156 OCResourceHandle handle = pubResHandle[j];
159 OCRepPayload *link = OCRepPayloadCreate();
161 const char *uri = OCGetResourceUri(handle);
164 OCRepPayloadSetPropString(link, OC_RSRVD_HREF, uri);
167 uint8_t numElement = 0;
168 if (OC_STACK_OK == OCGetNumberOfResourceTypes(handle, &numElement))
170 size_t rtDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
171 char **rt = (char **)OICMalloc(sizeof(char *) * numElement);
172 for (uint8_t i = 0; i < numElement; ++i)
174 const char *value = OCGetResourceTypeName(handle, i);
175 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
176 rt[i] = OICStrdup(value);
178 OCRepPayloadSetStringArrayAsOwner(link, OC_RSRVD_RESOURCE_TYPE, rt, rtDim);
182 if (OC_STACK_OK == OCGetNumberOfResourceInterfaces(handle, &numElement))
184 size_t ifDim[MAX_REP_ARRAY_DEPTH] = {numElement, 0, 0};
185 char **itf = (char **)OICMalloc(sizeof(char *) * numElement);
186 for (uint8_t i = 0; i < numElement; ++i)
188 const char *value = OCGetResourceInterfaceName(handle, i);
189 OIC_LOG_V(DEBUG, TAG, "value: %s", value);
190 itf[i] = OICStrdup(value);
192 OCRepPayloadSetStringArrayAsOwner(link, OC_RSRVD_INTERFACE, itf, ifDim);
196 if (OC_STACK_OK == OCGetResourceIns(handle, &ins))
198 OCRepPayloadSetPropInt(link, OC_RSRVD_INS, ins);
201 size_t mtDim[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
202 char **mediaType = (char **)OICMalloc(sizeof(char *) * 1);
205 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
207 for(uint8_t i = 0; i < nPubResHandles; i++)
209 OCRepPayloadDestroy(linkArr[i]);
212 OCRepPayloadDestroy(rdPayload);
213 return OC_STACK_NO_MEMORY;
216 mediaType[0] = OICStrdup(DEFAULT_MESSAGE_TYPE);
217 OCRepPayloadSetStringArrayAsOwner(link, OC_RSRVD_MEDIA_TYPE, mediaType, mtDim);
219 OCResourceProperty p = OCGetResourceProperties(handle);
220 p = (OCResourceProperty) ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE));
221 OCRepPayload *policy = OCRepPayloadCreate();
222 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, p);
223 OCRepPayloadSetPropObjectAsOwner(link, OC_RSRVD_POLICY, policy);
229 OCRepPayloadSetPropObjectArray(rdPayload, OC_RSRVD_LINKS, (const OCRepPayload **)linkArr, dimensions);
230 OIC_LOG_PAYLOAD(DEBUG, (OCPayload *) rdPayload);
232 for (uint8_t i = 0; i < nPubResHandles; i++)
234 OCRepPayloadDestroy(linkArr[i]);
238 return OCDoResource(handle, OC_REST_POST, targetUri, NULL, (OCPayload *)rdPayload,
239 connectivityType, qos, cbData, NULL, 0);
242 OCStackResult OCRDDelete(OCDoHandle *handle, const char *host,
243 OCConnectivityType connectivityType,
244 OCResourceHandle *resourceHandles, uint8_t nHandles,
245 OCCallbackData *cbData, OCQualityOfService qos)
247 // Validate input parameters
250 return OC_STACK_INVALID_IP;
253 if (!cbData || !cbData->cb)
255 return OC_STACK_INVALID_CALLBACK;
258 const unsigned char *id = (const unsigned char *) OCGetServerInstanceIDString();
260 return OCRDDeleteWithDeviceId(handle, host, id, connectivityType, resourceHandles,
261 nHandles, cbData, qos);
264 OCStackResult OCRDDeleteWithDeviceId(OCDoHandle *handle, const char *host,
265 const unsigned char *id,
266 OCConnectivityType connectivityType,
267 OCResourceHandle *resourceHandles, uint8_t nHandles,
268 OCCallbackData *cbData, OCQualityOfService qos)
270 // Validate input parameters
271 if (!host || !cbData || !cbData->cb || !id)
273 return OC_STACK_INVALID_CALLBACK;
276 OIC_LOG_V(DEBUG, TAG, "Delete Resource to RD with device id [%s]", id);
278 char targetUri[MAX_URI_LENGTH] = { 0 };
279 snprintf(targetUri, MAX_URI_LENGTH, "%s%s?di=%s", host, OC_RSRVD_RD_URI, id);
282 char queryParam[MAX_URI_LENGTH] = { 0 };
283 for (uint8_t j = 0; j < nHandles; j++)
285 OCResource *handle = (OCResource *) resourceHandles[j];
287 OCGetResourceIns(handle, &ins);
288 len += snprintf(queryParam + len, MAX_URI_LENGTH, "&ins=%" PRId64, ins);
289 OIC_LOG_V(DEBUG, TAG, "queryParam [%s]", queryParam);
292 OICStrcatPartial(targetUri, sizeof(targetUri), queryParam, strlen(queryParam));
293 OIC_LOG_V(DEBUG, TAG, "Target URI: %s", targetUri);
295 return OCDoResource(handle, OC_REST_DELETE, targetUri, NULL, NULL, connectivityType,
296 qos, cbData, NULL, 0);