1 //******************************************************************
3 // Copyright 2014 Intel Mobile Communications GmbH 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 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "InProcClientWrapper.h"
24 #include "OCPlatform.h"
25 #include "OCResource.h"
26 #include "ocpayload.h"
27 #include <OCSerialization.h>
32 InProcClientWrapper::InProcClientWrapper(
33 std::weak_ptr<std::recursive_mutex> csdkLock, PlatformConfig cfg)
34 : m_threadRun(false), m_csdkLock(csdkLock),
37 // if the config type is server, we ought to never get called. If the config type
38 // is both, we count on the server to run the thread and do the initialize
40 if (m_cfg.mode == ModeType::Client)
42 OCTransportFlags serverFlags =
43 static_cast<OCTransportFlags>(m_cfg.serverConnectivity & CT_MASK_FLAGS);
44 OCTransportFlags clientFlags =
45 static_cast<OCTransportFlags>(m_cfg.clientConnectivity & CT_MASK_FLAGS);
46 OCStackResult result = OCInit1(OC_CLIENT, serverFlags, clientFlags);
48 if (OC_STACK_OK != result)
50 throw InitializeException(OC::InitException::STACK_INIT_ERROR, result);
54 m_listeningThread = std::thread(&InProcClientWrapper::listeningFunc, this);
58 InProcClientWrapper::~InProcClientWrapper()
60 if (m_threadRun && m_listeningThread.joinable())
63 m_listeningThread.join();
66 // only stop if we are the ones who actually called 'init'. We are counting
67 // on the server to do the stop.
68 if (m_cfg.mode == ModeType::Client)
74 void InProcClientWrapper::listeningFunc()
79 auto cLock = m_csdkLock.lock();
82 std::lock_guard<std::recursive_mutex> lock(*cLock);
87 result = OC_STACK_ERROR;
90 if (result != OC_STACK_OK)
92 // TODO: do something with result if failed?
95 // To minimize CPU utilization we may wish to do this with sleep
96 std::this_thread::sleep_for(std::chrono::milliseconds(10));
100 OCRepresentation parseGetSetCallback(OCClientResponse* clientResponse)
102 if (clientResponse->payload == nullptr ||
104 clientResponse->payload->type != PAYLOAD_TYPE_DEVICE &&
105 clientResponse->payload->type != PAYLOAD_TYPE_PLATFORM &&
106 clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION
110 //OCPayloadDestroy(clientResponse->payload);
111 return OCRepresentation();
115 oc.setPayload(clientResponse->payload);
116 //OCPayloadDestroy(clientResponse->payload);
118 std::vector<OCRepresentation>::const_iterator it = oc.representations().begin();
119 if (it == oc.representations().end())
121 return OCRepresentation();
124 // first one is considered the root, everything else is considered a child of this one.
125 OCRepresentation root = *it;
126 root.setDevAddr(clientResponse->devAddr);
127 root.setUri(clientResponse->resourceUri);
130 std::for_each(it, oc.representations().end(),
131 [&root](const OCRepresentation& repItr)
132 {root.addChild(repItr);});
137 OCStackApplicationResult listenCallback(void* ctx, OCDoHandle /*handle*/,
138 OCClientResponse* clientResponse)
140 ClientCallbackContext::ListenContext* context =
141 static_cast<ClientCallbackContext::ListenContext*>(ctx);
143 if (clientResponse->result != OC_STACK_OK)
145 oclog() << "listenCallback(): failed to create resource. clientResponse: "
146 << clientResponse->result
149 return OC_STACK_KEEP_TRANSACTION;
152 if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY)
154 oclog() << "listenCallback(): clientResponse payload was null or the wrong type"
156 return OC_STACK_KEEP_TRANSACTION;
159 auto clientWrapper = context->clientWrapper.lock();
163 oclog() << "listenCallback(): failed to get a shared_ptr to the client wrapper"
165 return OC_STACK_KEEP_TRANSACTION;
169 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
170 reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
171 // loop to ensure valid construction of all resources
173 for(auto resource : container.Resources())
175 std::thread exec(context->callback, resource);
179 catch (std::exception &e){
180 oclog() << "Exception in listCallback, ignoring response: "
181 << e.what() << std::flush;
185 return OC_STACK_KEEP_TRANSACTION;
188 OCStackApplicationResult listenErrorCallback(void* ctx, OCDoHandle /*handle*/,
189 OCClientResponse* clientResponse)
191 if (!ctx || !clientResponse)
193 return OC_STACK_KEEP_TRANSACTION;
196 ClientCallbackContext::ListenErrorContext* context =
197 static_cast<ClientCallbackContext::ListenErrorContext*>(ctx);
200 return OC_STACK_KEEP_TRANSACTION;
203 OCStackResult result = clientResponse->result;
204 if (result == OC_STACK_OK)
206 if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY)
208 oclog() << "listenCallback(): clientResponse payload was null or the wrong type"
210 return OC_STACK_KEEP_TRANSACTION;
213 auto clientWrapper = context->clientWrapper.lock();
217 oclog() << "listenCallback(): failed to get a shared_ptr to the client wrapper"
219 return OC_STACK_KEEP_TRANSACTION;
222 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
223 reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
224 // loop to ensure valid construction of all resources
225 for (auto resource : container.Resources())
227 std::thread exec(context->callback, resource);
230 return OC_STACK_KEEP_TRANSACTION;
233 std::string resourceURI = clientResponse->resourceUri;
234 std::thread exec(context->errorCallback, resourceURI, result);
236 return OC_STACK_DELETE_TRANSACTION;
239 OCStackResult InProcClientWrapper::ListenForResource(
240 const std::string& serviceUrl,
241 const std::string& resourceType,
242 OCConnectivityType connectivityType,
243 FindCallback& callback, QualityOfService QoS)
247 return OC_STACK_INVALID_PARAM;
250 OCStackResult result;
251 ostringstream resourceUri;
252 resourceUri << serviceUrl << resourceType;
254 ClientCallbackContext::ListenContext* context =
255 new ClientCallbackContext::ListenContext(callback, shared_from_this());
256 OCCallbackData cbdata;
257 cbdata.context = static_cast<void*>(context),
258 cbdata.cb = listenCallback;
259 cbdata.cd = [](void* c){delete (ClientCallbackContext::ListenContext*)c;};
261 auto cLock = m_csdkLock.lock();
264 std::lock_guard<std::recursive_mutex> lock(*cLock);
265 result = OCDoResource(nullptr, OC_REST_DISCOVER,
266 resourceUri.str().c_str(),
267 nullptr, nullptr, connectivityType,
268 static_cast<OCQualityOfService>(QoS),
275 result = OC_STACK_ERROR;
280 OCStackResult InProcClientWrapper::ListenErrorForResource(
281 const std::string& serviceUrl,
282 const std::string& resourceType,
283 OCConnectivityType connectivityType,
284 FindCallback& callback, FindErrorCallback& errorCallback,
285 QualityOfService QoS)
289 return OC_STACK_INVALID_PARAM;
292 ostringstream resourceUri;
293 resourceUri << serviceUrl << resourceType;
295 ClientCallbackContext::ListenErrorContext* context =
296 new ClientCallbackContext::ListenErrorContext(callback, errorCallback,
300 return OC_STACK_ERROR;
303 OCCallbackData cbdata(
304 static_cast<void*>(context),
306 [](void* c){delete static_cast<ClientCallbackContext::ListenErrorContext*>(c);}
309 OCStackResult result;
310 auto cLock = m_csdkLock.lock();
313 std::lock_guard<std::recursive_mutex> lock(*cLock);
314 result = OCDoResource(nullptr, OC_REST_DISCOVER,
315 resourceUri.str().c_str(),
316 nullptr, nullptr, connectivityType,
317 static_cast<OCQualityOfService>(QoS),
324 result = OC_STACK_ERROR;
329 OCStackApplicationResult listenMQCallback(void* ctx, OCDoHandle /*handle*/,
330 OCClientResponse* clientResponse)
332 ClientCallbackContext::MQTopicContext* context =
333 static_cast<ClientCallbackContext::MQTopicContext*>(ctx);
335 if (!clientResponse || !context)
337 return OC_STACK_DELETE_TRANSACTION;
340 if (clientResponse->result != OC_STACK_OK)
342 oclog() << "listenMQCallback(): failed to create resource. clientResponse: "
343 << clientResponse->result
346 std::thread exec(context->callback, clientResponse->result,
347 clientResponse->resourceUri, nullptr);
350 return OC_STACK_DELETE_TRANSACTION;
353 auto clientWrapper = context->clientWrapper.lock();
356 oclog() << "listenMQCallback(): failed to get a shared_ptr to the client wrapper"
358 return OC_STACK_DELETE_TRANSACTION;
362 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
363 (OCRepPayload *) clientResponse->payload);
365 // loop to ensure valid construction of all resources
366 for (auto resource : container.Resources())
368 std::thread exec(context->callback, clientResponse->result,
369 clientResponse->resourceUri, resource);
373 catch (std::exception &e){
374 oclog() << "Exception in listCallback, ignoring response: "
375 << e.what() << std::flush;
378 return OC_STACK_DELETE_TRANSACTION;
381 OCStackResult InProcClientWrapper::ListenForMQTopic(const OCDevAddr& devAddr,
382 const std::string& resourceUri,
383 const QueryParamsMap& queryParams,
384 const HeaderOptions& headerOptions,
385 MQTopicCallback& callback,
386 QualityOfService QoS)
388 oclog() << "ListenForMQTopic()" << std::flush;
392 return OC_STACK_INVALID_PARAM;
395 ClientCallbackContext::MQTopicContext* context =
396 new ClientCallbackContext::MQTopicContext(callback, shared_from_this());
397 OCCallbackData cbdata;
398 cbdata.context = static_cast<void*>(context),
399 cbdata.cb = listenMQCallback;
400 cbdata.cd = [](void* c){delete (ClientCallbackContext::MQTopicContext*)c;};
402 std::string uri = assembleSetResourceUri(resourceUri, queryParams);
404 OCStackResult result = OC_STACK_ERROR;
405 auto cLock = m_csdkLock.lock();
408 std::lock_guard<std::recursive_mutex> lock(*cLock);
409 OCHeaderOption options[MAX_HEADER_OPTIONS];
410 result = OCDoResource(
411 nullptr, OC_REST_GET,
415 static_cast<OCQualityOfService>(QoS),
417 assembleHeaderOptions(options, headerOptions),
418 headerOptions.size());
429 OCStackApplicationResult listenDeviceCallback(void* ctx,
430 OCDoHandle /*handle*/,
431 OCClientResponse* clientResponse)
433 ClientCallbackContext::DeviceListenContext* context =
434 static_cast<ClientCallbackContext::DeviceListenContext*>(ctx);
438 OCRepresentation rep = parseGetSetCallback(clientResponse);
439 std::thread exec(context->callback, rep);
442 catch(OC::OCException& e)
444 oclog() <<"Exception in listenDeviceCallback, ignoring response: "
445 <<e.what() <<std::flush;
448 return OC_STACK_KEEP_TRANSACTION;
451 OCStackResult InProcClientWrapper::ListenForDevice(
452 const std::string& serviceUrl,
453 const std::string& deviceURI,
454 OCConnectivityType connectivityType,
455 FindDeviceCallback& callback,
456 QualityOfService QoS)
460 return OC_STACK_INVALID_PARAM;
462 OCStackResult result;
463 ostringstream deviceUri;
464 deviceUri << serviceUrl << deviceURI;
466 ClientCallbackContext::DeviceListenContext* context =
467 new ClientCallbackContext::DeviceListenContext(callback, shared_from_this());
468 OCCallbackData cbdata;
470 cbdata.context = static_cast<void*>(context),
471 cbdata.cb = listenDeviceCallback;
472 cbdata.cd = [](void* c){delete (ClientCallbackContext::DeviceListenContext*)c;};
474 auto cLock = m_csdkLock.lock();
477 std::lock_guard<std::recursive_mutex> lock(*cLock);
478 result = OCDoResource(nullptr, OC_REST_DISCOVER,
479 deviceUri.str().c_str(),
480 nullptr, nullptr, connectivityType,
481 static_cast<OCQualityOfService>(QoS),
488 result = OC_STACK_ERROR;
493 void parseServerHeaderOptions(OCClientResponse* clientResponse,
494 HeaderOptions& serverHeaderOptions)
498 // Parse header options from server
500 std::string optionData;
502 for(int i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
504 optionID = clientResponse->rcvdVendorSpecificHeaderOptions[i].optionID;
505 optionData = reinterpret_cast<const char*>
506 (clientResponse->rcvdVendorSpecificHeaderOptions[i].optionData);
507 HeaderOption::OCHeaderOption headerOption(optionID, optionData);
508 serverHeaderOptions.push_back(headerOption);
513 // clientResponse is invalid
514 // TODO check proper logging
515 std::cout << " Invalid response " << std::endl;
520 OCStackApplicationResult createMQTopicCallback(void* ctx, OCDoHandle /*handle*/,
521 OCClientResponse* clientResponse)
523 ClientCallbackContext::MQTopicContext* context =
524 static_cast<ClientCallbackContext::MQTopicContext*>(ctx);
525 HeaderOptions serverHeaderOptions;
527 if (!clientResponse || !context)
529 return OC_STACK_DELETE_TRANSACTION;
532 std::string createdUri;
533 bool isLocationOption = false;
534 OCStackResult result = clientResponse->result;
535 if (OC_STACK_OK == result ||
536 OC_STACK_RESOURCE_CREATED == result)
538 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
540 for (auto headerOption : serverHeaderOptions)
542 if (HeaderOption::LOCATION_PATH_OPTION_ID == headerOption.getOptionID())
545 createdUri += headerOption.getOptionData();
546 if (!isLocationOption)
548 isLocationOption = true;
554 if (!isLocationOption)
556 createdUri = clientResponse->resourceUri;
559 auto clientWrapper = context->clientWrapper.lock();
563 oclog() << "createMQTopicCallback(): failed to get a shared_ptr to the client wrapper"
565 return OC_STACK_DELETE_TRANSACTION;
569 if (OC_STACK_OK == result ||
570 OC_STACK_RESOURCE_CREATED == result)
572 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
574 for (auto resource : container.Resources())
576 std::thread exec(context->callback, result, createdUri, resource);
582 std::thread exec(context->callback, result, createdUri, nullptr);
586 catch (std::exception &e){
587 oclog() << "Exception in createMQTopicCallback, ignoring response: "
588 << e.what() << std::flush;
590 return OC_STACK_DELETE_TRANSACTION;
593 OCStackResult InProcClientWrapper::PutMQTopicRepresentation(
594 const OCDevAddr& devAddr,
595 const std::string& uri,
596 const OCRepresentation& rep,
597 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
598 MQTopicCallback& callback, QualityOfService QoS)
602 return OC_STACK_INVALID_PARAM;
604 OCStackResult result;
605 ClientCallbackContext::MQTopicContext* ctx =
606 new ClientCallbackContext::MQTopicContext(callback, shared_from_this());
607 OCCallbackData cbdata;
608 cbdata.context = static_cast<void*>(ctx),
609 cbdata.cb = createMQTopicCallback;
610 cbdata.cd = [](void* c){delete (ClientCallbackContext::MQTopicContext*)c;};
612 std::string url = assembleSetResourceUri(uri, queryParams);
614 auto cLock = m_csdkLock.lock();
618 std::lock_guard<std::recursive_mutex> lock(*cLock);
619 OCHeaderOption options[MAX_HEADER_OPTIONS];
621 result = OCDoResource(nullptr, OC_REST_PUT,
622 url.c_str(), &devAddr,
623 assembleSetResourcePayload(rep),
625 static_cast<OCQualityOfService>(QoS),
627 assembleHeaderOptions(options, headerOptions),
628 headerOptions.size());
633 result = OC_STACK_ERROR;
639 OCStackApplicationResult getResourceCallback(void* ctx,
640 OCDoHandle /*handle*/,
641 OCClientResponse* clientResponse)
643 ClientCallbackContext::GetContext* context =
644 static_cast<ClientCallbackContext::GetContext*>(ctx);
646 OCRepresentation rep;
647 HeaderOptions serverHeaderOptions;
648 OCStackResult result = clientResponse->result;
649 if (result == OC_STACK_OK)
651 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
654 rep = parseGetSetCallback(clientResponse);
656 catch(OC::OCException& e)
662 std::thread exec(context->callback, serverHeaderOptions, rep, result);
664 return OC_STACK_DELETE_TRANSACTION;
667 OCStackResult InProcClientWrapper::GetResourceRepresentation(
668 const OCDevAddr& devAddr,
669 const std::string& resourceUri,
670 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
671 OCConnectivityType connectivityType,
672 GetCallback& callback, QualityOfService QoS)
676 return OC_STACK_INVALID_PARAM;
678 OCStackResult result;
679 ClientCallbackContext::GetContext* ctx =
680 new ClientCallbackContext::GetContext(callback);
681 OCCallbackData cbdata;
682 cbdata.context = static_cast<void*>(ctx),
683 cbdata.cb = getResourceCallback;
684 cbdata.cd = [](void* c){delete (ClientCallbackContext::GetContext*)c;};
687 std::string uri = assembleSetResourceUri(resourceUri, queryParams);
689 auto cLock = m_csdkLock.lock();
693 std::lock_guard<std::recursive_mutex> lock(*cLock);
694 OCHeaderOption options[MAX_HEADER_OPTIONS];
696 result = OCDoResource(
697 nullptr, OC_REST_GET,
701 static_cast<OCQualityOfService>(QoS),
703 assembleHeaderOptions(options, headerOptions),
704 headerOptions.size());
709 result = OC_STACK_ERROR;
715 OCStackApplicationResult setResourceCallback(void* ctx,
716 OCDoHandle /*handle*/,
717 OCClientResponse* clientResponse)
719 ClientCallbackContext::SetContext* context =
720 static_cast<ClientCallbackContext::SetContext*>(ctx);
721 OCRepresentation attrs;
722 HeaderOptions serverHeaderOptions;
724 OCStackResult result = clientResponse->result;
725 if (OC_STACK_OK == result ||
726 OC_STACK_RESOURCE_CREATED == result ||
727 OC_STACK_RESOURCE_DELETED == result ||
728 OC_STACK_RESOURCE_CHANGED == result)
730 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
733 attrs = parseGetSetCallback(clientResponse);
735 catch(OC::OCException& e)
741 std::thread exec(context->callback, serverHeaderOptions, attrs, result);
743 return OC_STACK_DELETE_TRANSACTION;
746 std::string InProcClientWrapper::assembleSetResourceUri(std::string uri,
747 const QueryParamsMap& queryParams)
751 if (uri.back() == '/')
753 uri.resize(uri.size() - 1);
757 ostringstream paramsList;
758 if (queryParams.size() > 0)
763 for (auto& param : queryParams)
765 paramsList << param.first <<'='<<param.second<<';';
768 std::string queryString = paramsList.str();
770 if (queryString.empty())
775 if (queryString.back() == ';')
777 queryString.resize(queryString.size() - 1);
780 std::string ret = uri + queryString;
784 std::string InProcClientWrapper::assembleSetResourceUri(std::string uri,
785 const QueryParamsList& queryParams)
789 if (uri.back() == '/')
791 uri.resize(uri.size() - 1);
795 ostringstream paramsList;
796 if (queryParams.size() > 0)
801 for (auto& param : queryParams)
803 for (auto& paramList : param.second)
805 paramsList << param.first << '=' << paramList;
806 if (paramList != param.second.back())
814 std::string queryString = paramsList.str();
816 if (queryString.empty())
821 if (queryString.back() == ';')
823 queryString.resize(queryString.size() - 1);
826 std::string ret = uri + queryString;
830 OCPayload* InProcClientWrapper::assembleSetResourcePayload(const OCRepresentation& rep)
832 MessageContainer ocInfo;
833 ocInfo.addRepresentation(rep);
834 for(const OCRepresentation& r : rep.getChildren())
836 ocInfo.addRepresentation(r);
839 return reinterpret_cast<OCPayload*>(ocInfo.getPayload());
842 OCStackResult InProcClientWrapper::PostResourceRepresentation(
843 const OCDevAddr& devAddr,
844 const std::string& uri,
845 const OCRepresentation& rep,
846 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
847 OCConnectivityType connectivityType,
848 PostCallback& callback, QualityOfService QoS)
852 return OC_STACK_INVALID_PARAM;
854 OCStackResult result;
855 ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext(callback);
856 OCCallbackData cbdata;
857 cbdata.context = static_cast<void*>(ctx),
858 cbdata.cb = setResourceCallback;
859 cbdata.cd = [](void* c){delete (ClientCallbackContext::SetContext*)c;};
862 std::string url = assembleSetResourceUri(uri, queryParams);
864 auto cLock = m_csdkLock.lock();
868 std::lock_guard<std::recursive_mutex> lock(*cLock);
869 OCHeaderOption options[MAX_HEADER_OPTIONS];
871 result = OCDoResource(nullptr, OC_REST_POST,
872 url.c_str(), &devAddr,
873 assembleSetResourcePayload(rep),
875 static_cast<OCQualityOfService>(QoS),
877 assembleHeaderOptions(options, headerOptions),
878 headerOptions.size());
883 result = OC_STACK_ERROR;
889 OCStackResult InProcClientWrapper::PutResourceRepresentation(
890 const OCDevAddr& devAddr,
891 const std::string& uri,
892 const OCRepresentation& rep,
893 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
894 PutCallback& callback, QualityOfService QoS)
898 return OC_STACK_INVALID_PARAM;
900 OCStackResult result;
901 ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext(callback);
902 OCCallbackData cbdata;
903 cbdata.context = static_cast<void*>(ctx),
904 cbdata.cb = setResourceCallback;
905 cbdata.cd = [](void* c){delete (ClientCallbackContext::SetContext*)c;};
908 std::string url = assembleSetResourceUri(uri, queryParams).c_str();
910 auto cLock = m_csdkLock.lock();
914 std::lock_guard<std::recursive_mutex> lock(*cLock);
916 OCHeaderOption options[MAX_HEADER_OPTIONS];
918 result = OCDoResource(&handle, OC_REST_PUT,
919 url.c_str(), &devAddr,
920 assembleSetResourcePayload(rep),
922 static_cast<OCQualityOfService>(QoS),
924 assembleHeaderOptions(options, headerOptions),
925 headerOptions.size());
930 result = OC_STACK_ERROR;
936 OCStackApplicationResult deleteResourceCallback(void* ctx,
937 OCDoHandle /*handle*/,
938 OCClientResponse* clientResponse)
940 ClientCallbackContext::DeleteContext* context =
941 static_cast<ClientCallbackContext::DeleteContext*>(ctx);
942 HeaderOptions serverHeaderOptions;
944 if (clientResponse->result == OC_STACK_OK)
946 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
948 std::thread exec(context->callback, serverHeaderOptions, clientResponse->result);
950 return OC_STACK_DELETE_TRANSACTION;
953 OCStackResult InProcClientWrapper::DeleteResource(
954 const OCDevAddr& devAddr,
955 const std::string& uri,
956 const HeaderOptions& headerOptions,
957 OCConnectivityType connectivityType,
958 DeleteCallback& callback,
959 QualityOfService /*QoS*/)
963 return OC_STACK_INVALID_PARAM;
965 OCStackResult result;
966 ClientCallbackContext::DeleteContext* ctx =
967 new ClientCallbackContext::DeleteContext(callback);
968 OCCallbackData cbdata;
969 cbdata.context = static_cast<void*>(ctx),
970 cbdata.cb = deleteResourceCallback;
971 cbdata.cd = [](void* c){delete (ClientCallbackContext::DeleteContext*)c;};
974 auto cLock = m_csdkLock.lock();
978 OCHeaderOption options[MAX_HEADER_OPTIONS];
980 std::lock_guard<std::recursive_mutex> lock(*cLock);
982 result = OCDoResource(nullptr, OC_REST_DELETE,
983 uri.c_str(), &devAddr,
986 static_cast<OCQualityOfService>(m_cfg.QoS),
988 assembleHeaderOptions(options, headerOptions),
989 headerOptions.size());
994 result = OC_STACK_ERROR;
1000 OCStackApplicationResult observeResourceCallback(void* ctx,
1001 OCDoHandle /*handle*/,
1002 OCClientResponse* clientResponse)
1004 ClientCallbackContext::ObserveContext* context =
1005 static_cast<ClientCallbackContext::ObserveContext*>(ctx);
1006 OCRepresentation attrs;
1007 HeaderOptions serverHeaderOptions;
1008 uint32_t sequenceNumber = clientResponse->sequenceNumber;
1009 OCStackResult result = clientResponse->result;
1010 if (clientResponse->result == OC_STACK_OK)
1012 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
1015 attrs = parseGetSetCallback(clientResponse);
1017 catch(OC::OCException& e)
1022 std::thread exec(context->callback, serverHeaderOptions, attrs,
1023 result, sequenceNumber);
1026 return OC_STACK_KEEP_TRANSACTION;
1029 OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle,
1030 const OCDevAddr& devAddr,
1031 const std::string& uri,
1032 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
1033 ObserveCallback& callback, QualityOfService QoS)
1037 return OC_STACK_INVALID_PARAM;
1039 OCStackResult result;
1041 ClientCallbackContext::ObserveContext* ctx =
1042 new ClientCallbackContext::ObserveContext(callback);
1043 OCCallbackData cbdata;
1044 cbdata.context = static_cast<void*>(ctx),
1045 cbdata.cb = observeResourceCallback;
1046 cbdata.cd = [](void* c){delete (ClientCallbackContext::ObserveContext*)c;};
1050 if (observeType == ObserveType::Observe)
1052 method = OC_REST_OBSERVE;
1054 else if (observeType == ObserveType::ObserveAll)
1056 method = OC_REST_OBSERVE_ALL;
1060 method = OC_REST_OBSERVE_ALL;
1063 std::string url = assembleSetResourceUri(uri, queryParams).c_str();
1065 auto cLock = m_csdkLock.lock();
1069 std::lock_guard<std::recursive_mutex> lock(*cLock);
1070 OCHeaderOption options[MAX_HEADER_OPTIONS];
1072 result = OCDoResource(handle, method,
1073 url.c_str(), &devAddr,
1076 static_cast<OCQualityOfService>(QoS),
1078 assembleHeaderOptions(options, headerOptions),
1079 headerOptions.size());
1084 return OC_STACK_ERROR;
1090 OCStackResult InProcClientWrapper::CancelObserveResource(
1092 const std::string& /*host*/,
1093 const std::string& /*uri*/,
1094 const HeaderOptions& headerOptions,
1095 QualityOfService QoS)
1097 OCStackResult result;
1098 auto cLock = m_csdkLock.lock();
1102 std::lock_guard<std::recursive_mutex> lock(*cLock);
1103 OCHeaderOption options[MAX_HEADER_OPTIONS];
1105 result = OCCancel(handle,
1106 static_cast<OCQualityOfService>(QoS),
1107 assembleHeaderOptions(options, headerOptions),
1108 headerOptions.size());
1112 result = OC_STACK_ERROR;
1118 OCStackApplicationResult subscribePresenceCallback(void* ctx,
1119 OCDoHandle /*handle*/,
1120 OCClientResponse* clientResponse)
1122 ClientCallbackContext::SubscribePresenceContext* context =
1123 static_cast<ClientCallbackContext::SubscribePresenceContext*>(ctx);
1126 * This a hack while we rethink presence subscription.
1128 std::string url = clientResponse->devAddr.addr;
1130 std::thread exec(context->callback, clientResponse->result,
1131 clientResponse->sequenceNumber, url);
1135 return OC_STACK_KEEP_TRANSACTION;
1138 OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle,
1139 const std::string& host, const std::string& resourceType,
1140 OCConnectivityType connectivityType, SubscribeCallback& presenceHandler)
1142 if (!presenceHandler)
1144 return OC_STACK_INVALID_PARAM;
1147 ClientCallbackContext::SubscribePresenceContext* ctx =
1148 new ClientCallbackContext::SubscribePresenceContext(presenceHandler);
1149 OCCallbackData cbdata;
1150 cbdata.context = static_cast<void*>(ctx),
1151 cbdata.cb = subscribePresenceCallback;
1152 cbdata.cd = [](void* c){delete (ClientCallbackContext::SubscribePresenceContext*)c;};
1155 auto cLock = m_csdkLock.lock();
1157 std::ostringstream os;
1158 os << host << OC_RSRVD_PRESENCE_URI;
1160 if (!resourceType.empty())
1162 os << "?rt=" << resourceType;
1168 return OC_STACK_ERROR;
1171 return OCDoResource(handle, OC_REST_PRESENCE,
1172 os.str().c_str(), nullptr,
1173 nullptr, connectivityType,
1174 OC_LOW_QOS, &cbdata, NULL, 0);
1177 OCStackResult InProcClientWrapper::UnsubscribePresence(OCDoHandle handle)
1179 OCStackResult result;
1180 auto cLock = m_csdkLock.lock();
1184 std::lock_guard<std::recursive_mutex> lock(*cLock);
1185 result = OCCancel(handle, OC_LOW_QOS, NULL, 0);
1189 result = OC_STACK_ERROR;
1196 OCStackResult InProcClientWrapper::SubscribeDevicePresence(OCDoHandle* handle,
1197 const std::string& host,
1198 const std::vector<std::string>& di,
1199 OCConnectivityType connectivityType,
1200 ObserveCallback& callback)
1204 return OC_STACK_INVALID_PARAM;
1206 OCStackResult result;
1208 ClientCallbackContext::ObserveContext* ctx =
1209 new ClientCallbackContext::ObserveContext(callback);
1210 OCCallbackData cbdata;
1211 cbdata.context = static_cast<void*>(ctx),
1212 cbdata.cb = observeResourceCallback;
1213 cbdata.cd = [](void* c){delete (ClientCallbackContext::ObserveContext*)c;};
1215 auto cLock = m_csdkLock.lock();
1219 std::lock_guard<std::recursive_mutex> lock(*cLock);
1221 std::ostringstream os;
1222 os << host << OCF_RSRVD_DEVICE_PRESENCE_URI;
1223 QueryParamsList queryParams({{OC_RSRVD_DEVICE_ID, di}});
1224 std::string url = assembleSetResourceUri(os.str(), queryParams);
1226 result = OCDoResource(handle, OC_REST_OBSERVE,
1227 url.c_str(), nullptr,
1228 nullptr, connectivityType,
1229 OC_LOW_QOS, &cbdata,
1235 result = OC_STACK_ERROR;
1242 OCStackResult InProcClientWrapper::GetDefaultQos(QualityOfService& qos)
1248 OCHeaderOption* InProcClientWrapper::assembleHeaderOptions(OCHeaderOption options[],
1249 const HeaderOptions& headerOptions)
1253 if ( headerOptions.size() == 0)
1258 for (auto it=headerOptions.begin(); it != headerOptions.end(); ++it)
1260 options[i] = OCHeaderOption();
1261 options[i].protocolID = OC_COAP_ID;
1262 options[i].optionID = it->getOptionID();
1263 options[i].optionLength = it->getOptionData().length() + 1;
1264 strcpy((char*)options[i].optionData, (it->getOptionData().c_str()));
1271 std::shared_ptr<OCDirectPairing> cloneDevice(const OCDPDev_t* dev)
1278 OCDPDev_t* result = new OCDPDev_t(*dev);
1279 result->prm = new OCPrm_t[dev->prmLen];
1280 memcpy(result->prm, dev->prm, sizeof(OCPrm_t)*dev->prmLen);
1281 return std::shared_ptr<OCDirectPairing>(new OCDirectPairing(result));
1284 void InProcClientWrapper::convert(const OCDPDev_t *list, PairedDevices& dpList)
1288 dpList.push_back(cloneDevice(list));
1293 OCStackResult InProcClientWrapper::FindDirectPairingDevices(unsigned short waittime,
1294 GetDirectPairedCallback& callback)
1296 if (!callback || 0 == waittime)
1298 return OC_STACK_INVALID_PARAM;
1301 OCStackResult result = OC_STACK_ERROR;
1302 const OCDPDev_t *list = nullptr;
1303 PairedDevices dpDeviceList;
1305 auto cLock = m_csdkLock.lock();
1309 std::lock_guard<std::recursive_mutex> lock(*cLock);
1311 list = OCDiscoverDirectPairingDevices(waittime);
1314 result = OC_STACK_NO_RESOURCE;
1315 oclog() << "findDirectPairingDevices(): No device found for direct pairing"
1319 convert(list, dpDeviceList);
1320 std::thread exec(callback, dpDeviceList);
1322 result = OC_STACK_OK;
1327 result = OC_STACK_ERROR;
1333 OCStackResult InProcClientWrapper::GetDirectPairedDevices(GetDirectPairedCallback& callback)
1337 return OC_STACK_INVALID_PARAM;
1340 OCStackResult result = OC_STACK_ERROR;
1341 const OCDPDev_t *list = nullptr;
1342 PairedDevices dpDeviceList;
1344 auto cLock = m_csdkLock.lock();
1348 std::lock_guard<std::recursive_mutex> lock(*cLock);
1350 list = OCGetDirectPairedDevices();
1353 result = OC_STACK_NO_RESOURCE;
1354 oclog() << "findDirectPairingDevices(): No device found for direct pairing"
1358 convert(list, dpDeviceList);
1359 std::thread exec(callback, dpDeviceList);
1361 result = OC_STACK_OK;
1366 result = OC_STACK_ERROR;
1372 void directPairingCallback(void *ctx, OCDPDev_t *peer,
1373 OCStackResult result)
1376 ClientCallbackContext::DirectPairingContext* context =
1377 static_cast<ClientCallbackContext::DirectPairingContext*>(ctx);
1379 std::thread exec(context->callback, cloneDevice(peer), result);
1383 OCStackResult InProcClientWrapper::DoDirectPairing(std::shared_ptr<OCDirectPairing> peer,
1384 const OCPrm_t& pmSel, const std::string& pinNumber, DirectPairingCallback& callback)
1386 if (!peer || !callback)
1388 oclog() << "Invalid parameters" << std::flush;
1389 return OC_STACK_INVALID_PARAM;
1392 OCStackResult result = OC_STACK_ERROR;
1393 ClientCallbackContext::DirectPairingContext* context =
1394 new ClientCallbackContext::DirectPairingContext(callback);
1396 auto cLock = m_csdkLock.lock();
1399 std::lock_guard<std::recursive_mutex> lock(*cLock);
1400 result = OCDoDirectPairing(static_cast<void*>(context), peer->getDev(),
1401 pmSel, const_cast<char*>(pinNumber.c_str()), directPairingCallback);
1406 result = OC_STACK_ERROR;