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;
170 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
171 reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
172 // loop to ensure valid construction of all resources
174 for(auto resource : container.Resources())
176 std::thread exec(context->callback, resource);
180 catch (std::exception &e)
182 oclog() << "Exception in listCallback, ignoring response: "
183 << e.what() << std::flush;
187 return OC_STACK_KEEP_TRANSACTION;
190 OCStackApplicationResult listenErrorCallback(void* ctx, OCDoHandle /*handle*/,
191 OCClientResponse* clientResponse)
193 if (!ctx || !clientResponse)
195 return OC_STACK_KEEP_TRANSACTION;
198 ClientCallbackContext::ListenErrorContext* context =
199 static_cast<ClientCallbackContext::ListenErrorContext*>(ctx);
202 return OC_STACK_KEEP_TRANSACTION;
205 OCStackResult result = clientResponse->result;
206 if (result == OC_STACK_OK)
208 if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY)
210 oclog() << "listenCallback(): clientResponse payload was null or the wrong type"
212 return OC_STACK_KEEP_TRANSACTION;
215 auto clientWrapper = context->clientWrapper.lock();
219 oclog() << "listenCallback(): failed to get a shared_ptr to the client wrapper"
221 return OC_STACK_KEEP_TRANSACTION;
224 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
225 reinterpret_cast<OCDiscoveryPayload*>(clientResponse->payload));
226 // loop to ensure valid construction of all resources
227 for (auto resource : container.Resources())
229 std::thread exec(context->callback, resource);
232 return OC_STACK_KEEP_TRANSACTION;
235 std::string resourceURI = clientResponse->resourceUri;
236 std::thread exec(context->errorCallback, resourceURI, result);
238 return OC_STACK_KEEP_TRANSACTION;
241 OCStackResult InProcClientWrapper::ListenForResource(
242 const std::string& serviceUrl,
243 const std::string& resourceType,
244 OCConnectivityType connectivityType,
245 FindCallback& callback, QualityOfService QoS)
249 return OC_STACK_INVALID_PARAM;
252 OCStackResult result;
253 ostringstream resourceUri;
254 resourceUri << serviceUrl << resourceType;
256 ClientCallbackContext::ListenContext* context =
257 new ClientCallbackContext::ListenContext(callback, shared_from_this());
258 OCCallbackData cbdata;
259 cbdata.context = static_cast<void*>(context),
260 cbdata.cb = listenCallback;
261 cbdata.cd = [](void* c){delete (ClientCallbackContext::ListenContext*)c;};
263 auto cLock = m_csdkLock.lock();
266 std::lock_guard<std::recursive_mutex> lock(*cLock);
267 result = OCDoResource(nullptr, OC_REST_DISCOVER,
268 resourceUri.str().c_str(),
269 nullptr, nullptr, connectivityType,
270 static_cast<OCQualityOfService>(QoS),
277 result = OC_STACK_ERROR;
282 OCStackResult InProcClientWrapper::ListenErrorForResource(
283 const std::string& serviceUrl,
284 const std::string& resourceType,
285 OCConnectivityType connectivityType,
286 FindCallback& callback, FindErrorCallback& errorCallback,
287 QualityOfService QoS)
291 return OC_STACK_INVALID_PARAM;
294 ostringstream resourceUri;
295 resourceUri << serviceUrl << resourceType;
297 ClientCallbackContext::ListenErrorContext* context =
298 new ClientCallbackContext::ListenErrorContext(callback, errorCallback,
302 return OC_STACK_ERROR;
305 OCCallbackData cbdata(
306 static_cast<void*>(context),
308 [](void* c){delete static_cast<ClientCallbackContext::ListenErrorContext*>(c);}
311 OCStackResult result;
312 auto cLock = m_csdkLock.lock();
315 std::lock_guard<std::recursive_mutex> lock(*cLock);
316 result = OCDoResource(nullptr, OC_REST_DISCOVER,
317 resourceUri.str().c_str(),
318 nullptr, nullptr, connectivityType,
319 static_cast<OCQualityOfService>(QoS),
326 result = OC_STACK_ERROR;
331 OCStackApplicationResult listenMQCallback(void* ctx, OCDoHandle /*handle*/,
332 OCClientResponse* clientResponse)
334 ClientCallbackContext::MQTopicContext* context =
335 static_cast<ClientCallbackContext::MQTopicContext*>(ctx);
337 if (!clientResponse || !context)
339 return OC_STACK_DELETE_TRANSACTION;
342 std::string resourceURI = clientResponse->resourceUri;
343 if (clientResponse->result != OC_STACK_OK)
345 oclog() << "listenMQCallback(): failed to create resource. clientResponse: "
346 << clientResponse->result
349 std::thread exec(context->callback, clientResponse->result,
350 resourceURI, nullptr);
353 return OC_STACK_DELETE_TRANSACTION;
356 auto clientWrapper = context->clientWrapper.lock();
359 oclog() << "listenMQCallback(): failed to get a shared_ptr to the client wrapper"
361 return OC_STACK_DELETE_TRANSACTION;
366 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
367 (OCRepPayload *) clientResponse->payload);
369 // loop to ensure valid construction of all resources
370 for (auto resource : container.Resources())
372 std::thread exec(context->callback, clientResponse->result,
373 resourceURI, resource);
377 catch (std::exception &e)
379 oclog() << "Exception in listCallback, ignoring response: "
380 << e.what() << std::flush;
383 return OC_STACK_DELETE_TRANSACTION;
386 OCStackResult InProcClientWrapper::ListenForMQTopic(const OCDevAddr& devAddr,
387 const std::string& resourceUri,
388 const QueryParamsMap& queryParams,
389 const HeaderOptions& headerOptions,
390 MQTopicCallback& callback,
391 QualityOfService QoS)
393 oclog() << "ListenForMQTopic()" << std::flush;
397 return OC_STACK_INVALID_PARAM;
400 ClientCallbackContext::MQTopicContext* context =
401 new ClientCallbackContext::MQTopicContext(callback, shared_from_this());
402 OCCallbackData cbdata;
403 cbdata.context = static_cast<void*>(context),
404 cbdata.cb = listenMQCallback;
405 cbdata.cd = [](void* c){delete (ClientCallbackContext::MQTopicContext*)c;};
407 std::string uri = assembleSetResourceUri(resourceUri, queryParams);
409 OCStackResult result = OC_STACK_ERROR;
410 auto cLock = m_csdkLock.lock();
413 std::lock_guard<std::recursive_mutex> lock(*cLock);
414 OCHeaderOption options[MAX_HEADER_OPTIONS];
415 result = OCDoResource(
416 nullptr, OC_REST_GET,
420 static_cast<OCQualityOfService>(QoS),
422 assembleHeaderOptions(options, headerOptions),
423 headerOptions.size());
434 OCStackApplicationResult listenDeviceCallback(void* ctx,
435 OCDoHandle /*handle*/,
436 OCClientResponse* clientResponse)
438 ClientCallbackContext::DeviceListenContext* context =
439 static_cast<ClientCallbackContext::DeviceListenContext*>(ctx);
443 OCRepresentation rep = parseGetSetCallback(clientResponse);
444 std::thread exec(context->callback, rep);
447 catch(OC::OCException& e)
449 oclog() <<"Exception in listenDeviceCallback, ignoring response: "
450 <<e.what() <<std::flush;
453 return OC_STACK_KEEP_TRANSACTION;
456 OCStackResult InProcClientWrapper::ListenForDevice(
457 const std::string& serviceUrl,
458 const std::string& deviceURI,
459 OCConnectivityType connectivityType,
460 FindDeviceCallback& callback,
461 QualityOfService QoS)
465 return OC_STACK_INVALID_PARAM;
467 OCStackResult result;
468 ostringstream deviceUri;
469 deviceUri << serviceUrl << deviceURI;
471 ClientCallbackContext::DeviceListenContext* context =
472 new ClientCallbackContext::DeviceListenContext(callback, shared_from_this());
473 OCCallbackData cbdata;
475 cbdata.context = static_cast<void*>(context),
476 cbdata.cb = listenDeviceCallback;
477 cbdata.cd = [](void* c){delete (ClientCallbackContext::DeviceListenContext*)c;};
479 auto cLock = m_csdkLock.lock();
482 std::lock_guard<std::recursive_mutex> lock(*cLock);
483 result = OCDoResource(nullptr, OC_REST_DISCOVER,
484 deviceUri.str().c_str(),
485 nullptr, nullptr, connectivityType,
486 static_cast<OCQualityOfService>(QoS),
493 result = OC_STACK_ERROR;
498 void parseServerHeaderOptions(OCClientResponse* clientResponse,
499 HeaderOptions& serverHeaderOptions)
503 // Parse header options from server
505 std::string optionData;
507 for(int i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
509 optionID = clientResponse->rcvdVendorSpecificHeaderOptions[i].optionID;
510 optionData = reinterpret_cast<const char*>
511 (clientResponse->rcvdVendorSpecificHeaderOptions[i].optionData);
512 HeaderOption::OCHeaderOption headerOption(optionID, optionData);
513 serverHeaderOptions.push_back(headerOption);
518 // clientResponse is invalid
519 // TODO check proper logging
520 std::cout << " Invalid response " << std::endl;
525 OCStackApplicationResult createMQTopicCallback(void* ctx, OCDoHandle /*handle*/,
526 OCClientResponse* clientResponse)
528 ClientCallbackContext::MQTopicContext* context =
529 static_cast<ClientCallbackContext::MQTopicContext*>(ctx);
530 HeaderOptions serverHeaderOptions;
532 if (!clientResponse || !context)
534 return OC_STACK_DELETE_TRANSACTION;
537 std::string createdUri;
538 bool isLocationOption = false;
539 OCStackResult result = clientResponse->result;
540 if (OC_STACK_OK == result ||
541 OC_STACK_RESOURCE_CREATED == result)
543 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
545 for (auto headerOption : serverHeaderOptions)
547 if (HeaderOption::LOCATION_PATH_OPTION_ID == headerOption.getOptionID())
550 createdUri += headerOption.getOptionData();
551 if (!isLocationOption)
553 isLocationOption = true;
559 if (!isLocationOption)
561 createdUri = std::string(clientResponse->resourceUri);
564 auto clientWrapper = context->clientWrapper.lock();
568 oclog() << "createMQTopicCallback(): failed to get a shared_ptr to the client wrapper"
570 return OC_STACK_DELETE_TRANSACTION;
575 if (OC_STACK_OK == result ||
576 OC_STACK_RESOURCE_CREATED == result)
578 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
580 for (auto resource : container.Resources())
582 std::thread exec(context->callback, result,
590 std::thread exec(context->callback, result,
596 catch (std::exception &e)
598 oclog() << "Exception in createMQTopicCallback, ignoring response: "
599 << e.what() << std::flush;
601 return OC_STACK_DELETE_TRANSACTION;
604 OCStackResult InProcClientWrapper::PutMQTopicRepresentation(
605 const OCDevAddr& devAddr,
606 const std::string& uri,
607 const OCRepresentation& rep,
608 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
609 MQTopicCallback& callback, QualityOfService QoS)
613 return OC_STACK_INVALID_PARAM;
615 OCStackResult result;
616 ClientCallbackContext::MQTopicContext* ctx =
617 new ClientCallbackContext::MQTopicContext(callback, shared_from_this());
618 OCCallbackData cbdata;
619 cbdata.context = static_cast<void*>(ctx),
620 cbdata.cb = createMQTopicCallback;
621 cbdata.cd = [](void* c){delete (ClientCallbackContext::MQTopicContext*)c;};
623 std::string url = assembleSetResourceUri(uri, queryParams);
625 auto cLock = m_csdkLock.lock();
629 std::lock_guard<std::recursive_mutex> lock(*cLock);
630 OCHeaderOption options[MAX_HEADER_OPTIONS];
632 result = OCDoResource(nullptr, OC_REST_PUT,
633 url.c_str(), &devAddr,
634 assembleSetResourcePayload(rep),
636 static_cast<OCQualityOfService>(QoS),
638 assembleHeaderOptions(options, headerOptions),
639 headerOptions.size());
644 result = OC_STACK_ERROR;
650 OCStackApplicationResult getResourceCallback(void* ctx,
651 OCDoHandle /*handle*/,
652 OCClientResponse* clientResponse)
654 ClientCallbackContext::GetContext* context =
655 static_cast<ClientCallbackContext::GetContext*>(ctx);
657 OCRepresentation rep;
658 HeaderOptions serverHeaderOptions;
659 OCStackResult result = clientResponse->result;
661 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
664 rep = parseGetSetCallback(clientResponse);
666 catch(OC::OCException& e)
671 std::thread exec(context->callback, serverHeaderOptions, rep, result);
673 return OC_STACK_DELETE_TRANSACTION;
676 OCStackResult InProcClientWrapper::GetResourceRepresentation(
677 const OCDevAddr& devAddr,
678 const std::string& resourceUri,
679 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
680 OCConnectivityType connectivityType,
681 GetCallback& callback, QualityOfService QoS)
685 return OC_STACK_INVALID_PARAM;
687 OCStackResult result;
688 ClientCallbackContext::GetContext* ctx =
689 new ClientCallbackContext::GetContext(callback);
690 OCCallbackData cbdata;
691 cbdata.context = static_cast<void*>(ctx),
692 cbdata.cb = getResourceCallback;
693 cbdata.cd = [](void* c){delete (ClientCallbackContext::GetContext*)c;};
696 std::string uri = assembleSetResourceUri(resourceUri, queryParams);
698 auto cLock = m_csdkLock.lock();
702 std::lock_guard<std::recursive_mutex> lock(*cLock);
703 OCHeaderOption options[MAX_HEADER_OPTIONS];
705 result = OCDoResource(
706 nullptr, OC_REST_GET,
710 static_cast<OCQualityOfService>(QoS),
712 assembleHeaderOptions(options, headerOptions),
713 headerOptions.size());
718 result = OC_STACK_ERROR;
724 OCStackApplicationResult setResourceCallback(void* ctx,
725 OCDoHandle /*handle*/,
726 OCClientResponse* clientResponse)
728 ClientCallbackContext::SetContext* context =
729 static_cast<ClientCallbackContext::SetContext*>(ctx);
730 OCRepresentation attrs;
731 HeaderOptions serverHeaderOptions;
733 OCStackResult result = clientResponse->result;
735 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
738 attrs = parseGetSetCallback(clientResponse);
740 catch(OC::OCException& e)
745 std::thread exec(context->callback, serverHeaderOptions, attrs, result);
747 return OC_STACK_DELETE_TRANSACTION;
750 std::string InProcClientWrapper::assembleSetResourceUri(std::string uri,
751 const QueryParamsMap& queryParams)
755 if (uri.back() == '/')
757 uri.resize(uri.size() - 1);
761 ostringstream paramsList;
762 if (queryParams.size() > 0)
767 for (auto& param : queryParams)
769 paramsList << param.first <<'='<<param.second<<';';
772 std::string queryString = paramsList.str();
774 if (queryString.empty())
779 if (queryString.back() == ';')
781 queryString.resize(queryString.size() - 1);
784 std::string ret = uri + queryString;
788 std::string InProcClientWrapper::assembleSetResourceUri(std::string uri,
789 const QueryParamsList& queryParams)
793 if (uri.back() == '/')
795 uri.resize(uri.size() - 1);
799 ostringstream paramsList;
800 if (queryParams.size() > 0)
805 for (auto& param : queryParams)
807 for (auto& paramList : param.second)
809 paramsList << param.first << '=' << paramList << ';';
813 std::string queryString = paramsList.str();
815 if (queryString.empty())
820 if (queryString.back() == ';')
822 queryString.resize(queryString.size() - 1);
825 std::string ret = uri + queryString;
829 OCPayload* InProcClientWrapper::assembleSetResourcePayload(const OCRepresentation& rep)
831 MessageContainer ocInfo;
832 ocInfo.addRepresentation(rep);
833 for(const OCRepresentation& r : rep.getChildren())
835 ocInfo.addRepresentation(r);
838 return reinterpret_cast<OCPayload*>(ocInfo.getPayload());
841 OCStackResult InProcClientWrapper::PostResourceRepresentation(
842 const OCDevAddr& devAddr,
843 const std::string& uri,
844 const OCRepresentation& rep,
845 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
846 OCConnectivityType connectivityType,
847 PostCallback& callback, QualityOfService QoS)
851 return OC_STACK_INVALID_PARAM;
853 OCStackResult result;
854 ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext(callback);
855 OCCallbackData cbdata;
856 cbdata.context = static_cast<void*>(ctx),
857 cbdata.cb = setResourceCallback;
858 cbdata.cd = [](void* c){delete (ClientCallbackContext::SetContext*)c;};
861 std::string url = assembleSetResourceUri(uri, queryParams);
863 auto cLock = m_csdkLock.lock();
867 std::lock_guard<std::recursive_mutex> lock(*cLock);
868 OCHeaderOption options[MAX_HEADER_OPTIONS];
870 result = OCDoResource(nullptr, OC_REST_POST,
871 url.c_str(), &devAddr,
872 assembleSetResourcePayload(rep),
874 static_cast<OCQualityOfService>(QoS),
876 assembleHeaderOptions(options, headerOptions),
877 headerOptions.size());
882 result = OC_STACK_ERROR;
888 OCStackResult InProcClientWrapper::PutResourceRepresentation(
889 const OCDevAddr& devAddr,
890 const std::string& uri,
891 const OCRepresentation& rep,
892 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
893 PutCallback& callback, QualityOfService QoS)
897 return OC_STACK_INVALID_PARAM;
899 OCStackResult result;
900 ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext(callback);
901 OCCallbackData cbdata;
902 cbdata.context = static_cast<void*>(ctx),
903 cbdata.cb = setResourceCallback;
904 cbdata.cd = [](void* c){delete (ClientCallbackContext::SetContext*)c;};
907 std::string url = assembleSetResourceUri(uri, queryParams).c_str();
909 auto cLock = m_csdkLock.lock();
913 std::lock_guard<std::recursive_mutex> lock(*cLock);
915 OCHeaderOption options[MAX_HEADER_OPTIONS];
917 result = OCDoResource(&handle, OC_REST_PUT,
918 url.c_str(), &devAddr,
919 assembleSetResourcePayload(rep),
921 static_cast<OCQualityOfService>(QoS),
923 assembleHeaderOptions(options, headerOptions),
924 headerOptions.size());
929 result = OC_STACK_ERROR;
935 OCStackApplicationResult deleteResourceCallback(void* ctx,
936 OCDoHandle /*handle*/,
937 OCClientResponse* clientResponse)
939 ClientCallbackContext::DeleteContext* context =
940 static_cast<ClientCallbackContext::DeleteContext*>(ctx);
941 HeaderOptions serverHeaderOptions;
943 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
945 std::thread exec(context->callback, serverHeaderOptions, clientResponse->result);
947 return OC_STACK_DELETE_TRANSACTION;
950 OCStackResult InProcClientWrapper::DeleteResource(
951 const OCDevAddr& devAddr,
952 const std::string& uri,
953 const HeaderOptions& headerOptions,
954 OCConnectivityType connectivityType,
955 DeleteCallback& callback,
956 QualityOfService /*QoS*/)
960 return OC_STACK_INVALID_PARAM;
962 OCStackResult result;
963 ClientCallbackContext::DeleteContext* ctx =
964 new ClientCallbackContext::DeleteContext(callback);
965 OCCallbackData cbdata;
966 cbdata.context = static_cast<void*>(ctx),
967 cbdata.cb = deleteResourceCallback;
968 cbdata.cd = [](void* c){delete (ClientCallbackContext::DeleteContext*)c;};
971 auto cLock = m_csdkLock.lock();
975 OCHeaderOption options[MAX_HEADER_OPTIONS];
977 std::lock_guard<std::recursive_mutex> lock(*cLock);
979 result = OCDoResource(nullptr, OC_REST_DELETE,
980 uri.c_str(), &devAddr,
983 static_cast<OCQualityOfService>(m_cfg.QoS),
985 assembleHeaderOptions(options, headerOptions),
986 headerOptions.size());
991 result = OC_STACK_ERROR;
997 OCStackApplicationResult observeResourceCallback(void* ctx,
998 OCDoHandle /*handle*/,
999 OCClientResponse* clientResponse)
1001 ClientCallbackContext::ObserveContext* context =
1002 static_cast<ClientCallbackContext::ObserveContext*>(ctx);
1003 OCRepresentation attrs;
1004 HeaderOptions serverHeaderOptions;
1005 uint32_t sequenceNumber = clientResponse->sequenceNumber;
1006 OCStackResult result = clientResponse->result;
1008 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
1011 attrs = parseGetSetCallback(clientResponse);
1013 catch(OC::OCException& e)
1018 std::thread exec(context->callback, serverHeaderOptions, attrs,
1019 result, sequenceNumber);
1021 if (sequenceNumber == MAX_SEQUENCE_NUMBER + 1)
1023 return OC_STACK_DELETE_TRANSACTION;
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 << OC_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;