1 //******************************************************************
3 // Copyright 2015 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 at
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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
26 #include "rd_client.h"
29 #include "OCRepresentation.h"
30 #include "OCResourceRequest.h"
31 #include "OCResourceResponse.h"
32 #include "OCPlatform.h"
33 #include "OCException.h"
34 #include "ocpayload.h"
39 OCRepresentation parseRDResponseCallback(OCClientResponse* clientResponse)
41 if (nullptr == clientResponse || nullptr == clientResponse->payload ||
42 PAYLOAD_TYPE_REPRESENTATION != clientResponse->payload->type)
44 return OCRepresentation();
48 oc.setPayload(clientResponse->payload);
50 std::vector<OCRepresentation>::const_iterator it = oc.representations().begin();
51 if (it == oc.representations().end())
53 return OCRepresentation();
56 // first one is considered the root, everything else is considered a child of this one.
57 OCRepresentation root = *it;
58 root.setDevAddr(clientResponse->devAddr);
59 root.setUri(clientResponse->resourceUri);
62 std::for_each(it, oc.representations().end(),
63 [&root](const OCRepresentation& repItr)
64 {root.addChild(repItr);});
68 OCStackApplicationResult publishResourceToRDCallback(void* ctx, OCDoHandle /*handle*/,
69 OCClientResponse* clientResponse)
71 ServerCallbackContext::PublishContext* context =
72 static_cast<ServerCallbackContext::PublishContext*>(ctx);
76 // Update resource unique id in stack.
79 if (clientResponse->payload)
81 OCRepPayload *rdPayload = (OCRepPayload *) clientResponse->payload;
82 OCRepPayload **links = NULL;
84 size_t dimensions[MAX_REP_ARRAY_DEPTH];
85 OCRepPayloadGetPropObjectArray(rdPayload, OC_RSRVD_LINKS, &links, dimensions);
86 for(size_t i = 0; i < dimensions[0]; i++)
89 OCRepPayloadGetPropString(links[i], OC_RSRVD_HREF, &uri);
90 OCResourceHandle handle = OCGetResourceHandleAtUri(uri);
92 OCRepPayloadGetPropInt(links[i], OC_RSRVD_INS, &ins);
93 OCBindResourceInsToResource(handle, ins);
96 OCRepresentation rep = parseRDResponseCallback(clientResponse);
97 std::thread exec(context->callback, rep, clientResponse->result);
101 catch (OC::OCException& e)
103 oclog() <<"Exception in publishResourceToRDCallback, ignoring response: "
104 <<e.what() <<std::flush;
107 return OC_STACK_KEEP_TRANSACTION;
110 OCStackResult RDClient::publishResourceToRD(const std::string& host,
111 OCConnectivityType connectivityType,
112 ResourceHandles& resourceHandles,
113 PublishResourceCallback callback)
115 return publishResourceToRD(host, connectivityType, resourceHandles, callback, static_cast<QualityOfService>(m_qos));
118 OCStackResult RDClient::publishResourceToRD(const std::string& host,
119 OCConnectivityType connectivityType,
120 PublishResourceCallback callback,
121 QualityOfService qos)
123 ResourceHandles resourceHandles;
124 return publishResourceToRD(host, connectivityType, resourceHandles, callback, qos);
127 OCStackResult RDClient::publishResourceToRD(const std::string& host,
128 OCConnectivityType connectivityType,
129 ResourceHandles& resourceHandles,
130 PublishResourceCallback callback,
131 QualityOfService qos)
133 ServerCallbackContext::PublishContext* ctx =
134 new ServerCallbackContext::PublishContext(callback);
135 OCCallbackData cbdata(
136 static_cast<void*>(ctx),
137 publishResourceToRDCallback,
139 {delete static_cast<ServerCallbackContext::PublishContext*>(c);}
142 auto cLock = m_csdkLock.lock();
143 OCStackResult result = OC_STACK_ERROR;
146 std::lock_guard<std::recursive_mutex> lock(*cLock);
147 result = OCRDPublish(nullptr, host.c_str(), connectivityType, &resourceHandles[0],
148 resourceHandles.size(), &cbdata, static_cast<OCQualityOfService>(qos));
151 if (OC_STACK_OK != result)
153 throw OCException(OC::Exception::PUBLISH_RESOURCE_FAILED, result);
158 OCStackApplicationResult deleteResourceFromRDCallback(void* ctx, OCDoHandle /*handle*/,
159 OCClientResponse* clientResponse)
161 ServerCallbackContext::DeleteContext* context =
162 static_cast<ServerCallbackContext::DeleteContext*>(ctx);
164 std::thread exec(context->callback, clientResponse->result);
166 return OC_STACK_DELETE_TRANSACTION;
169 OCStackResult RDClient::deleteResourceFromRD(const std::string& host,
170 OCConnectivityType connectivityType,
171 ResourceHandles& resourceHandles,
172 DeleteResourceCallback callback)
174 return deleteResourceFromRD(host, connectivityType, resourceHandles, callback, static_cast<QualityOfService>(m_qos));
178 OCStackResult RDClient::deleteResourceFromRD(const std::string& host,
179 OCConnectivityType connectivityType,
180 DeleteResourceCallback callback,
181 QualityOfService qos)
183 ResourceHandles resourceHandles;
184 return deleteResourceFromRD(host, connectivityType, resourceHandles, callback, qos);
187 OCStackResult RDClient::deleteResourceFromRD(const std::string& host,
188 OCConnectivityType connectivityType,
189 ResourceHandles& resourceHandles,
190 DeleteResourceCallback callback,
191 QualityOfService qos)
193 ServerCallbackContext::DeleteContext* ctx =
194 new ServerCallbackContext::DeleteContext(callback);
195 OCCallbackData cbdata(
196 static_cast<void*>(ctx),
197 deleteResourceFromRDCallback,
199 {delete static_cast<ServerCallbackContext::DeleteContext*>(c);}
202 auto cLock = m_csdkLock.lock();
203 OCStackResult result = OC_STACK_ERROR;
206 std::lock_guard<std::recursive_mutex> lock(*cLock);
207 result = OCRDDelete(nullptr, host.c_str(), connectivityType, &resourceHandles[0],
208 resourceHandles.size(), &cbdata, static_cast<OCQualityOfService>(qos));
211 if (OC_STACK_OK != result)
213 throw OCException(OC::Exception::PUBLISH_RESOURCE_FAILED, result);