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::ListenContext* context =
333 static_cast<ClientCallbackContext::ListenContext*>(ctx);
337 return OC_STACK_KEEP_TRANSACTION;
340 if (clientResponse->result != OC_STACK_OK)
342 oclog() << "listenMQCallback(): failed to create resource. clientResponse: "
343 << clientResponse->result
346 return OC_STACK_KEEP_TRANSACTION;
349 auto clientWrapper = context->clientWrapper.lock();
352 oclog() << "listenMQCallback(): failed to get a shared_ptr to the client wrapper"
354 return OC_STACK_KEEP_TRANSACTION;
358 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
359 (OCRepPayload *) clientResponse->payload);
361 // loop to ensure valid construction of all resources
362 for (auto resource : container.Resources())
364 std::thread exec(context->callback, resource);
368 catch (std::exception &e){
369 oclog() << "Exception in listCallback, ignoring response: "
370 << e.what() << std::flush;
374 return OC_STACK_KEEP_TRANSACTION;
377 OCStackResult InProcClientWrapper::ListenForMQTopic(
378 const OCDevAddr& devAddr,
379 const std::string& resourceUri,
380 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
381 FindCallback& callback, QualityOfService QoS)
383 oclog() << "ListenForMQTopic()" << std::flush;
387 return OC_STACK_INVALID_PARAM;
390 ClientCallbackContext::ListenContext* context =
391 new ClientCallbackContext::ListenContext(callback, shared_from_this());
392 OCCallbackData cbdata;
393 cbdata.context = static_cast<void*>(context),
394 cbdata.cb = listenMQCallback;
395 cbdata.cd = [](void* c){delete (ClientCallbackContext::ListenContext*)c;};
397 std::string uri = assembleSetResourceUri(resourceUri, queryParams);
399 OCStackResult result = OC_STACK_ERROR;
400 auto cLock = m_csdkLock.lock();
403 std::lock_guard<std::recursive_mutex> lock(*cLock);
404 OCHeaderOption options[MAX_HEADER_OPTIONS];
405 result = OCDoResource(
406 nullptr, OC_REST_GET,
410 static_cast<OCQualityOfService>(QoS),
412 assembleHeaderOptions(options, headerOptions),
413 headerOptions.size());
424 OCStackApplicationResult listenDeviceCallback(void* ctx,
425 OCDoHandle /*handle*/,
426 OCClientResponse* clientResponse)
428 ClientCallbackContext::DeviceListenContext* context =
429 static_cast<ClientCallbackContext::DeviceListenContext*>(ctx);
433 OCRepresentation rep = parseGetSetCallback(clientResponse);
434 std::thread exec(context->callback, rep);
437 catch(OC::OCException& e)
439 oclog() <<"Exception in listenDeviceCallback, ignoring response: "
440 <<e.what() <<std::flush;
443 return OC_STACK_KEEP_TRANSACTION;
446 OCStackResult InProcClientWrapper::ListenForDevice(
447 const std::string& serviceUrl,
448 const std::string& deviceURI,
449 OCConnectivityType connectivityType,
450 FindDeviceCallback& callback,
451 QualityOfService QoS)
455 return OC_STACK_INVALID_PARAM;
457 OCStackResult result;
458 ostringstream deviceUri;
459 deviceUri << serviceUrl << deviceURI;
461 ClientCallbackContext::DeviceListenContext* context =
462 new ClientCallbackContext::DeviceListenContext(callback, shared_from_this());
463 OCCallbackData cbdata;
465 cbdata.context = static_cast<void*>(context),
466 cbdata.cb = listenDeviceCallback;
467 cbdata.cd = [](void* c){delete (ClientCallbackContext::DeviceListenContext*)c;};
469 auto cLock = m_csdkLock.lock();
472 std::lock_guard<std::recursive_mutex> lock(*cLock);
473 result = OCDoResource(nullptr, OC_REST_DISCOVER,
474 deviceUri.str().c_str(),
475 nullptr, nullptr, connectivityType,
476 static_cast<OCQualityOfService>(QoS),
483 result = OC_STACK_ERROR;
488 void parseServerHeaderOptions(OCClientResponse* clientResponse,
489 HeaderOptions& serverHeaderOptions)
493 // Parse header options from server
495 std::string optionData;
497 for(int i = 0; i < clientResponse->numRcvdVendorSpecificHeaderOptions; i++)
499 optionID = clientResponse->rcvdVendorSpecificHeaderOptions[i].optionID;
500 optionData = reinterpret_cast<const char*>
501 (clientResponse->rcvdVendorSpecificHeaderOptions[i].optionData);
502 HeaderOption::OCHeaderOption headerOption(optionID, optionData);
503 serverHeaderOptions.push_back(headerOption);
508 // clientResponse is invalid
509 // TODO check proper logging
510 std::cout << " Invalid response " << std::endl;
515 OCStackApplicationResult createMQTopicCallback(void* ctx, OCDoHandle /*handle*/,
516 OCClientResponse* clientResponse)
518 ClientCallbackContext::CreateMQTopicContext* context =
519 static_cast<ClientCallbackContext::CreateMQTopicContext*>(ctx);
520 OCRepresentation rep;
521 HeaderOptions serverHeaderOptions;
525 return OC_STACK_DELETE_TRANSACTION;
528 std::string createdUri;
529 OCStackResult result = clientResponse->result;
530 if (OC_STACK_OK == result ||
531 OC_STACK_RESOURCE_CREATED == result)
533 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
536 rep = parseGetSetCallback(clientResponse);
538 catch(OC::OCException& e)
543 bool isLocationOption = false;
544 for (auto headerOption : serverHeaderOptions)
546 if (HeaderOption::LOCATION_PATH_OPTION_ID == headerOption.getOptionID())
549 createdUri += headerOption.getOptionData();
550 if (!isLocationOption)
552 isLocationOption = true;
557 if (!isLocationOption)
559 createdUri = clientResponse->resourceUri;
563 auto clientWrapper = context->clientWrapper.lock();
567 oclog() << "createMQTopicCallback(): failed to get a shared_ptr to the client wrapper"
569 return OC_STACK_DELETE_TRANSACTION;
573 if (OC_STACK_OK == result ||
574 OC_STACK_RESOURCE_CREATED == result)
576 ListenOCContainer container(clientWrapper, clientResponse->devAddr,
578 for (auto resource : container.Resources())
580 std::thread exec(context->callback, serverHeaderOptions, rep, result, resource);
586 std::thread exec(context->callback, serverHeaderOptions, rep, result, nullptr);
590 catch (std::exception &e){
591 oclog() << "Exception in createMQTopicCallback, ignoring response: "
592 << e.what() << std::flush;
594 return OC_STACK_DELETE_TRANSACTION;
597 OCStackResult InProcClientWrapper::PutMQTopicRepresentation(
598 const OCDevAddr& devAddr,
599 const std::string& uri,
600 const OCRepresentation& rep,
601 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
602 MQCreateTopicCallback& callback, QualityOfService QoS)
606 return OC_STACK_INVALID_PARAM;
608 OCStackResult result;
609 ClientCallbackContext::CreateMQTopicContext* ctx =
610 new ClientCallbackContext::CreateMQTopicContext(callback, shared_from_this());
611 OCCallbackData cbdata;
612 cbdata.context = static_cast<void*>(ctx),
613 cbdata.cb = createMQTopicCallback;
614 cbdata.cd = [](void* c){delete (ClientCallbackContext::CreateMQTopicContext*)c;};
616 std::string url = assembleSetResourceUri(uri, queryParams);
618 auto cLock = m_csdkLock.lock();
622 std::lock_guard<std::recursive_mutex> lock(*cLock);
623 OCHeaderOption options[MAX_HEADER_OPTIONS];
625 result = OCDoResource(nullptr, OC_REST_PUT,
626 url.c_str(), &devAddr,
627 assembleSetResourcePayload(rep),
629 static_cast<OCQualityOfService>(QoS),
631 assembleHeaderOptions(options, headerOptions),
632 headerOptions.size());
637 result = OC_STACK_ERROR;
643 OCStackApplicationResult getResourceCallback(void* ctx,
644 OCDoHandle /*handle*/,
645 OCClientResponse* clientResponse)
647 ClientCallbackContext::GetContext* context =
648 static_cast<ClientCallbackContext::GetContext*>(ctx);
650 OCRepresentation rep;
651 HeaderOptions serverHeaderOptions;
652 OCStackResult result = clientResponse->result;
653 if (result == OC_STACK_OK)
655 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
658 rep = parseGetSetCallback(clientResponse);
660 catch(OC::OCException& e)
666 std::thread exec(context->callback, serverHeaderOptions, rep, result);
668 return OC_STACK_DELETE_TRANSACTION;
671 OCStackResult InProcClientWrapper::GetResourceRepresentation(
672 const OCDevAddr& devAddr,
673 const std::string& resourceUri,
674 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
675 GetCallback& callback, QualityOfService QoS)
679 return OC_STACK_INVALID_PARAM;
681 OCStackResult result;
682 ClientCallbackContext::GetContext* ctx =
683 new ClientCallbackContext::GetContext(callback);
684 OCCallbackData cbdata;
685 cbdata.context = static_cast<void*>(ctx),
686 cbdata.cb = getResourceCallback;
687 cbdata.cd = [](void* c){delete (ClientCallbackContext::GetContext*)c;};
690 std::string uri = assembleSetResourceUri(resourceUri, queryParams);
692 auto cLock = m_csdkLock.lock();
696 std::lock_guard<std::recursive_mutex> lock(*cLock);
697 OCHeaderOption options[MAX_HEADER_OPTIONS];
699 result = OCDoResource(
700 nullptr, OC_REST_GET,
704 static_cast<OCQualityOfService>(QoS),
706 assembleHeaderOptions(options, headerOptions),
707 headerOptions.size());
712 result = OC_STACK_ERROR;
718 OCStackApplicationResult setResourceCallback(void* ctx,
719 OCDoHandle /*handle*/,
720 OCClientResponse* clientResponse)
722 ClientCallbackContext::SetContext* context =
723 static_cast<ClientCallbackContext::SetContext*>(ctx);
724 OCRepresentation attrs;
725 HeaderOptions serverHeaderOptions;
727 OCStackResult result = clientResponse->result;
728 if (OC_STACK_OK == result ||
729 OC_STACK_RESOURCE_CREATED == result ||
730 OC_STACK_RESOURCE_DELETED == result ||
731 OC_STACK_RESOURCE_CHANGED == result)
733 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
736 attrs = parseGetSetCallback(clientResponse);
738 catch(OC::OCException& e)
744 std::thread exec(context->callback, serverHeaderOptions, attrs, result);
746 return OC_STACK_DELETE_TRANSACTION;
749 std::string InProcClientWrapper::assembleSetResourceUri(std::string uri,
750 const QueryParamsMap& queryParams)
754 if (uri.back() == '/')
756 uri.resize(uri.size() - 1);
760 ostringstream paramsList;
761 if (queryParams.size() > 0)
766 for (auto& param : queryParams)
768 paramsList << param.first <<'='<<param.second<<';';
771 std::string queryString = paramsList.str();
773 if (queryString.empty())
778 if (queryString.back() == ';')
780 queryString.resize(queryString.size() - 1);
783 std::string ret = uri + queryString;
787 std::string InProcClientWrapper::assembleSetResourceUri(std::string uri,
788 const QueryParamsList& queryParams)
792 if (uri.back() == '/')
794 uri.resize(uri.size() - 1);
798 ostringstream paramsList;
799 if (queryParams.size() > 0)
804 for (auto& param : queryParams)
806 for (auto& paramList : param.second)
808 paramsList << param.first << '=' << paramList;
809 if (paramList != param.second.back())
817 std::string queryString = paramsList.str();
819 if (queryString.empty())
824 if (queryString.back() == ';')
826 queryString.resize(queryString.size() - 1);
829 std::string ret = uri + queryString;
833 OCPayload* InProcClientWrapper::assembleSetResourcePayload(const OCRepresentation& rep)
835 MessageContainer ocInfo;
836 ocInfo.addRepresentation(rep);
837 for(const OCRepresentation& r : rep.getChildren())
839 ocInfo.addRepresentation(r);
842 return reinterpret_cast<OCPayload*>(ocInfo.getPayload());
845 OCStackResult InProcClientWrapper::PostResourceRepresentation(
846 const OCDevAddr& devAddr,
847 const std::string& uri,
848 const OCRepresentation& rep,
849 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
850 OCConnectivityType connectivityType,
851 PostCallback& callback, QualityOfService QoS)
855 return OC_STACK_INVALID_PARAM;
857 OCStackResult result;
858 ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext(callback);
859 OCCallbackData cbdata;
860 cbdata.context = static_cast<void*>(ctx),
861 cbdata.cb = setResourceCallback;
862 cbdata.cd = [](void* c){delete (ClientCallbackContext::SetContext*)c;};
865 std::string url = assembleSetResourceUri(uri, queryParams);
867 auto cLock = m_csdkLock.lock();
871 std::lock_guard<std::recursive_mutex> lock(*cLock);
872 OCHeaderOption options[MAX_HEADER_OPTIONS];
874 result = OCDoResource(nullptr, OC_REST_POST,
875 url.c_str(), &devAddr,
876 assembleSetResourcePayload(rep),
878 static_cast<OCQualityOfService>(QoS),
880 assembleHeaderOptions(options, headerOptions),
881 headerOptions.size());
886 result = OC_STACK_ERROR;
892 OCStackResult InProcClientWrapper::PutResourceRepresentation(
893 const OCDevAddr& devAddr,
894 const std::string& uri,
895 const OCRepresentation& rep,
896 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
897 PutCallback& callback, QualityOfService QoS)
901 return OC_STACK_INVALID_PARAM;
903 OCStackResult result;
904 ClientCallbackContext::SetContext* ctx = new ClientCallbackContext::SetContext(callback);
905 OCCallbackData cbdata;
906 cbdata.context = static_cast<void*>(ctx),
907 cbdata.cb = setResourceCallback;
908 cbdata.cd = [](void* c){delete (ClientCallbackContext::SetContext*)c;};
911 std::string url = assembleSetResourceUri(uri, queryParams).c_str();
913 auto cLock = m_csdkLock.lock();
917 std::lock_guard<std::recursive_mutex> lock(*cLock);
919 OCHeaderOption options[MAX_HEADER_OPTIONS];
921 result = OCDoResource(&handle, OC_REST_PUT,
922 url.c_str(), &devAddr,
923 assembleSetResourcePayload(rep),
925 static_cast<OCQualityOfService>(QoS),
927 assembleHeaderOptions(options, headerOptions),
928 headerOptions.size());
933 result = OC_STACK_ERROR;
939 OCStackApplicationResult deleteResourceCallback(void* ctx,
940 OCDoHandle /*handle*/,
941 OCClientResponse* clientResponse)
943 ClientCallbackContext::DeleteContext* context =
944 static_cast<ClientCallbackContext::DeleteContext*>(ctx);
945 HeaderOptions serverHeaderOptions;
947 if (clientResponse->result == OC_STACK_OK)
949 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
951 std::thread exec(context->callback, serverHeaderOptions, clientResponse->result);
953 return OC_STACK_DELETE_TRANSACTION;
956 OCStackResult InProcClientWrapper::DeleteResource(
957 const OCDevAddr& devAddr,
958 const std::string& uri,
959 const HeaderOptions& headerOptions,
960 DeleteCallback& callback,
961 QualityOfService /*QoS*/)
965 return OC_STACK_INVALID_PARAM;
967 OCStackResult result;
968 ClientCallbackContext::DeleteContext* ctx =
969 new ClientCallbackContext::DeleteContext(callback);
970 OCCallbackData cbdata;
971 cbdata.context = static_cast<void*>(ctx),
972 cbdata.cb = deleteResourceCallback;
973 cbdata.cd = [](void* c){delete (ClientCallbackContext::DeleteContext*)c;};
976 auto cLock = m_csdkLock.lock();
980 OCHeaderOption options[MAX_HEADER_OPTIONS];
982 std::lock_guard<std::recursive_mutex> lock(*cLock);
984 result = OCDoResource(nullptr, OC_REST_DELETE,
985 uri.c_str(), &devAddr,
988 static_cast<OCQualityOfService>(m_cfg.QoS),
990 assembleHeaderOptions(options, headerOptions),
991 headerOptions.size());
996 result = OC_STACK_ERROR;
1002 OCStackApplicationResult observeResourceCallback(void* ctx,
1003 OCDoHandle /*handle*/,
1004 OCClientResponse* clientResponse)
1006 ClientCallbackContext::ObserveContext* context =
1007 static_cast<ClientCallbackContext::ObserveContext*>(ctx);
1008 OCRepresentation attrs;
1009 HeaderOptions serverHeaderOptions;
1010 uint32_t sequenceNumber = clientResponse->sequenceNumber;
1011 OCStackResult result = clientResponse->result;
1012 if (clientResponse->result == OC_STACK_OK)
1014 parseServerHeaderOptions(clientResponse, serverHeaderOptions);
1017 attrs = parseGetSetCallback(clientResponse);
1019 catch(OC::OCException& e)
1024 std::thread exec(context->callback, serverHeaderOptions, attrs,
1025 result, sequenceNumber);
1028 return OC_STACK_KEEP_TRANSACTION;
1031 OCStackResult InProcClientWrapper::ObserveResource(ObserveType observeType, OCDoHandle* handle,
1032 const OCDevAddr& devAddr,
1033 const std::string& uri,
1034 const QueryParamsMap& queryParams, const HeaderOptions& headerOptions,
1035 ObserveCallback& callback, QualityOfService QoS)
1039 return OC_STACK_INVALID_PARAM;
1041 OCStackResult result;
1043 ClientCallbackContext::ObserveContext* ctx =
1044 new ClientCallbackContext::ObserveContext(callback);
1045 OCCallbackData cbdata;
1046 cbdata.context = static_cast<void*>(ctx),
1047 cbdata.cb = observeResourceCallback;
1048 cbdata.cd = [](void* c){delete (ClientCallbackContext::ObserveContext*)c;};
1052 if (observeType == ObserveType::Observe)
1054 method = OC_REST_OBSERVE;
1056 else if (observeType == ObserveType::ObserveAll)
1058 method = OC_REST_OBSERVE_ALL;
1062 method = OC_REST_OBSERVE_ALL;
1065 std::string url = assembleSetResourceUri(uri, queryParams).c_str();
1067 auto cLock = m_csdkLock.lock();
1071 std::lock_guard<std::recursive_mutex> lock(*cLock);
1072 OCHeaderOption options[MAX_HEADER_OPTIONS];
1074 result = OCDoResource(handle, method,
1075 url.c_str(), &devAddr,
1078 static_cast<OCQualityOfService>(QoS),
1080 assembleHeaderOptions(options, headerOptions),
1081 headerOptions.size());
1086 return OC_STACK_ERROR;
1092 OCStackResult InProcClientWrapper::CancelObserveResource(
1094 const std::string& /*host*/,
1095 const std::string& /*uri*/,
1096 const HeaderOptions& headerOptions,
1097 QualityOfService QoS)
1099 OCStackResult result;
1100 auto cLock = m_csdkLock.lock();
1104 std::lock_guard<std::recursive_mutex> lock(*cLock);
1105 OCHeaderOption options[MAX_HEADER_OPTIONS];
1107 result = OCCancel(handle,
1108 static_cast<OCQualityOfService>(QoS),
1109 assembleHeaderOptions(options, headerOptions),
1110 headerOptions.size());
1114 result = OC_STACK_ERROR;
1120 OCStackApplicationResult subscribePresenceCallback(void* ctx,
1121 OCDoHandle /*handle*/,
1122 OCClientResponse* clientResponse)
1124 ClientCallbackContext::SubscribePresenceContext* context =
1125 static_cast<ClientCallbackContext::SubscribePresenceContext*>(ctx);
1128 * This a hack while we rethink presence subscription.
1130 std::string url = clientResponse->devAddr.addr;
1132 std::thread exec(context->callback, clientResponse->result,
1133 clientResponse->sequenceNumber, url);
1137 return OC_STACK_KEEP_TRANSACTION;
1140 OCStackResult InProcClientWrapper::SubscribePresence(OCDoHandle* handle,
1141 const std::string& host, const std::string& resourceType,
1142 OCConnectivityType connectivityType, SubscribeCallback& presenceHandler)
1144 if (!presenceHandler)
1146 return OC_STACK_INVALID_PARAM;
1149 ClientCallbackContext::SubscribePresenceContext* ctx =
1150 new ClientCallbackContext::SubscribePresenceContext(presenceHandler);
1151 OCCallbackData cbdata;
1152 cbdata.context = static_cast<void*>(ctx),
1153 cbdata.cb = subscribePresenceCallback;
1154 cbdata.cd = [](void* c){delete (ClientCallbackContext::SubscribePresenceContext*)c;};
1157 auto cLock = m_csdkLock.lock();
1159 std::ostringstream os;
1160 os << host << OC_RSRVD_PRESENCE_URI;
1162 if (!resourceType.empty())
1164 os << "?rt=" << resourceType;
1170 return OC_STACK_ERROR;
1173 return OCDoResource(handle, OC_REST_PRESENCE,
1174 os.str().c_str(), nullptr,
1175 nullptr, connectivityType,
1176 OC_LOW_QOS, &cbdata, NULL, 0);
1179 OCStackResult InProcClientWrapper::UnsubscribePresence(OCDoHandle handle)
1181 OCStackResult result;
1182 auto cLock = m_csdkLock.lock();
1186 std::lock_guard<std::recursive_mutex> lock(*cLock);
1187 result = OCCancel(handle, OC_LOW_QOS, NULL, 0);
1191 result = OC_STACK_ERROR;
1198 OCStackResult InProcClientWrapper::SubscribeDevicePresence(OCDoHandle* handle,
1199 const std::string& host,
1200 const std::vector<std::string>& di,
1201 OCConnectivityType connectivityType,
1202 ObserveCallback& callback)
1206 return OC_STACK_INVALID_PARAM;
1208 OCStackResult result;
1210 ClientCallbackContext::ObserveContext* ctx =
1211 new ClientCallbackContext::ObserveContext(callback);
1212 OCCallbackData cbdata;
1213 cbdata.context = static_cast<void*>(ctx),
1214 cbdata.cb = observeResourceCallback;
1215 cbdata.cd = [](void* c){delete (ClientCallbackContext::ObserveContext*)c;};
1217 auto cLock = m_csdkLock.lock();
1221 std::lock_guard<std::recursive_mutex> lock(*cLock);
1223 std::ostringstream os;
1224 os << host << OCF_RSRVD_DEVICE_PRESENCE_URI;
1225 QueryParamsList queryParams({{OC_RSRVD_DEVICE_ID, di}});
1226 std::string url = assembleSetResourceUri(os.str(), queryParams);
1228 result = OCDoResource(handle, OC_REST_OBSERVE,
1229 url.c_str(), nullptr,
1230 nullptr, connectivityType,
1231 OC_LOW_QOS, &cbdata,
1237 result = OC_STACK_ERROR;
1244 OCStackResult InProcClientWrapper::GetDefaultQos(QualityOfService& qos)
1250 OCHeaderOption* InProcClientWrapper::assembleHeaderOptions(OCHeaderOption options[],
1251 const HeaderOptions& headerOptions)
1255 if ( headerOptions.size() == 0)
1260 for (auto it=headerOptions.begin(); it != headerOptions.end(); ++it)
1262 options[i] = OCHeaderOption();
1263 options[i].protocolID = OC_COAP_ID;
1264 options[i].optionID = it->getOptionID();
1265 options[i].optionLength = it->getOptionData().length() + 1;
1266 strcpy((char*)options[i].optionData, (it->getOptionData().c_str()));
1273 std::shared_ptr<OCDirectPairing> cloneDevice(const OCDPDev_t* dev)
1280 OCDPDev_t* result = new OCDPDev_t(*dev);
1281 result->prm = new OCPrm_t[dev->prmLen];
1282 memcpy(result->prm, dev->prm, sizeof(OCPrm_t)*dev->prmLen);
1283 return std::shared_ptr<OCDirectPairing>(new OCDirectPairing(result));
1286 void InProcClientWrapper::convert(const OCDPDev_t *list, PairedDevices& dpList)
1290 dpList.push_back(cloneDevice(list));
1295 OCStackResult InProcClientWrapper::FindDirectPairingDevices(unsigned short waittime,
1296 GetDirectPairedCallback& callback)
1298 if (!callback || 0 == waittime)
1300 return OC_STACK_INVALID_PARAM;
1303 OCStackResult result = OC_STACK_ERROR;
1304 const OCDPDev_t *list = nullptr;
1305 PairedDevices dpDeviceList;
1307 auto cLock = m_csdkLock.lock();
1311 std::lock_guard<std::recursive_mutex> lock(*cLock);
1313 list = OCDiscoverDirectPairingDevices(waittime);
1316 result = OC_STACK_NO_RESOURCE;
1317 oclog() << "findDirectPairingDevices(): No device found for direct pairing"
1321 convert(list, dpDeviceList);
1322 std::thread exec(callback, dpDeviceList);
1324 result = OC_STACK_OK;
1329 result = OC_STACK_ERROR;
1335 OCStackResult InProcClientWrapper::GetDirectPairedDevices(GetDirectPairedCallback& callback)
1339 return OC_STACK_INVALID_PARAM;
1342 OCStackResult result = OC_STACK_ERROR;
1343 const OCDPDev_t *list = nullptr;
1344 PairedDevices dpDeviceList;
1346 auto cLock = m_csdkLock.lock();
1350 std::lock_guard<std::recursive_mutex> lock(*cLock);
1352 list = OCGetDirectPairedDevices();
1355 result = OC_STACK_NO_RESOURCE;
1356 oclog() << "findDirectPairingDevices(): No device found for direct pairing"
1360 convert(list, dpDeviceList);
1361 std::thread exec(callback, dpDeviceList);
1363 result = OC_STACK_OK;
1368 result = OC_STACK_ERROR;
1374 void directPairingCallback(void *ctx, OCDPDev_t *peer,
1375 OCStackResult result)
1378 ClientCallbackContext::DirectPairingContext* context =
1379 static_cast<ClientCallbackContext::DirectPairingContext*>(ctx);
1381 std::thread exec(context->callback, cloneDevice(peer), result);
1385 OCStackResult InProcClientWrapper::DoDirectPairing(std::shared_ptr<OCDirectPairing> peer,
1386 const OCPrm_t& pmSel, const std::string& pinNumber, DirectPairingCallback& callback)
1388 if (!peer || !callback)
1390 oclog() << "Invalid parameters" << std::flush;
1391 return OC_STACK_INVALID_PARAM;
1394 OCStackResult result = OC_STACK_ERROR;
1395 ClientCallbackContext::DirectPairingContext* context =
1396 new ClientCallbackContext::DirectPairingContext(callback);
1398 auto cLock = m_csdkLock.lock();
1401 std::lock_guard<std::recursive_mutex> lock(*cLock);
1402 result = OCDoDirectPairing(static_cast<void*>(context), peer->getDev(),
1403 pmSel, const_cast<char*>(pinNumber.c_str()), directPairingCallback);
1408 result = OC_STACK_ERROR;