X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fsrc%2FInProcClientWrapper.cpp;h=fcc96c37d4ba15f538b5b5dc0d12bb733d5ede21;hb=1cee1631595cac6a6394ac2e0b365c6dd5a42c68;hp=987c0f5727e655aacafd80a466b4cb243513c86d;hpb=8c01dff2c5bc5496f7dc1632c498943ec6ecb015;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/src/InProcClientWrapper.cpp b/resource/src/InProcClientWrapper.cpp index 987c0f5..fcc96c3 100644 --- a/resource/src/InProcClientWrapper.cpp +++ b/resource/src/InProcClientWrapper.cpp @@ -37,7 +37,7 @@ namespace OC // if the config type is server, we ought to never get called. If the config type // is both, we count on the server to run the thread and do the initialize - if(m_cfg.mode == ModeType::Client) + if (m_cfg.mode == ModeType::Client) { OCTransportFlags serverFlags = static_cast(m_cfg.serverConnectivity & CT_MASK_FLAGS); @@ -45,7 +45,7 @@ namespace OC static_cast(m_cfg.clientConnectivity & CT_MASK_FLAGS); OCStackResult result = OCInit1(OC_CLIENT, serverFlags, clientFlags); - if(OC_STACK_OK != result) + if (OC_STACK_OK != result) { throw InitializeException(OC::InitException::STACK_INIT_ERROR, result); } @@ -57,7 +57,7 @@ namespace OC InProcClientWrapper::~InProcClientWrapper() { - if(m_threadRun && m_listeningThread.joinable()) + if (m_threadRun && m_listeningThread.joinable()) { m_threadRun = false; m_listeningThread.join(); @@ -65,7 +65,7 @@ namespace OC // only stop if we are the ones who actually called 'init'. We are counting // on the server to do the stop. - if(m_cfg.mode == ModeType::Client) + if (m_cfg.mode == ModeType::Client) { OCStop(); } @@ -77,7 +77,7 @@ namespace OC { OCStackResult result; auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); result = OCProcess(); @@ -87,7 +87,7 @@ namespace OC result = OC_STACK_ERROR; } - if(result != OC_STACK_OK) + if (result != OC_STACK_OK) { // TODO: do something with result if failed? } @@ -99,7 +99,7 @@ namespace OC OCRepresentation parseGetSetCallback(OCClientResponse* clientResponse) { - if(clientResponse->payload == nullptr || + if (clientResponse->payload == nullptr || ( clientResponse->payload->type != PAYLOAD_TYPE_DEVICE && clientResponse->payload->type != PAYLOAD_TYPE_PLATFORM && @@ -116,13 +116,15 @@ namespace OC //OCPayloadDestroy(clientResponse->payload); std::vector::const_iterator it = oc.representations().begin(); - if(it == oc.representations().end()) + if (it == oc.representations().end()) { return OCRepresentation(); } // first one is considered the root, everything else is considered a child of this one. OCRepresentation root = *it; + root.setDevAddr(clientResponse->devAddr); + root.setUri(clientResponse->resourceUri); ++it; std::for_each(it, oc.representations().end(), @@ -132,13 +134,13 @@ namespace OC } - OCStackApplicationResult listenCallback(void* ctx, OCDoHandle handle, + OCStackApplicationResult listenCallback(void* ctx, OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::ListenContext* context = static_cast(ctx); - if(clientResponse->result != OC_STACK_OK) + if (clientResponse->result != OC_STACK_OK) { oclog() << "listenCallback(): failed to create resource. clientResponse: " << clientResponse->result @@ -147,7 +149,7 @@ namespace OC return OC_STACK_KEEP_TRANSACTION; } - if(!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY) + if (!clientResponse->payload || clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY) { oclog() << "listenCallback(): clientResponse payload was null or the wrong type" << std::flush; @@ -156,20 +158,26 @@ namespace OC auto clientWrapper = context->clientWrapper.lock(); - if(!clientWrapper) + if (!clientWrapper) { oclog() << "listenCallback(): failed to get a shared_ptr to the client wrapper" << std::flush; return OC_STACK_KEEP_TRANSACTION; } - ListenOCContainer container(clientWrapper, clientResponse->devAddr, - reinterpret_cast(clientResponse->payload)); - // loop to ensure valid construction of all resources - for(auto resource : container.Resources()) - { - std::thread exec(context->callback, resource); - exec.detach(); + try{ + ListenOCContainer container(clientWrapper, clientResponse->devAddr, + reinterpret_cast(clientResponse->payload)); + // loop to ensure valid construction of all resources + for(auto resource : container.Resources()) + { + std::thread exec(context->callback, resource); + exec.detach(); + } + } + catch (std::exception &e){ + oclog() << "Exception in listCallback, ignoring response: " + << e.what() << std::flush; } @@ -182,7 +190,7 @@ namespace OC OCConnectivityType connectivityType, FindCallback& callback, QualityOfService QoS) { - if(!callback) + if (!callback) { return OC_STACK_INVALID_PARAM; } @@ -200,7 +208,7 @@ namespace OC ); auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); result = OCDoResource(nullptr, OC_REST_DISCOVER, @@ -218,7 +226,8 @@ namespace OC return result; } - OCStackApplicationResult listenDeviceCallback(void* ctx, OCDoHandle handle, + OCStackApplicationResult listenDeviceCallback(void* ctx, + OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::DeviceListenContext* context = @@ -246,7 +255,7 @@ namespace OC FindDeviceCallback& callback, QualityOfService QoS) { - if(!callback) + if (!callback) { return OC_STACK_INVALID_PARAM; } @@ -263,7 +272,7 @@ namespace OC ); auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); result = OCDoResource(nullptr, OC_REST_DISCOVER, @@ -284,7 +293,7 @@ namespace OC void parseServerHeaderOptions(OCClientResponse* clientResponse, HeaderOptions& serverHeaderOptions) { - if(clientResponse) + if (clientResponse) { // Parse header options from server uint16_t optionID; @@ -307,7 +316,8 @@ namespace OC } } - OCStackApplicationResult getResourceCallback(void* ctx, OCDoHandle handle, + OCStackApplicationResult getResourceCallback(void* ctx, + OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::GetContext* context = @@ -316,7 +326,7 @@ namespace OC OCRepresentation rep; HeaderOptions serverHeaderOptions; OCStackResult result = clientResponse->result; - if(result == OC_STACK_OK) + if (result == OC_STACK_OK) { parseServerHeaderOptions(clientResponse, serverHeaderOptions); try @@ -340,7 +350,7 @@ namespace OC const QueryParamsMap& queryParams, const HeaderOptions& headerOptions, GetCallback& callback, QualityOfService QoS) { - if(!callback) + if (!callback) { return OC_STACK_INVALID_PARAM; } @@ -357,7 +367,7 @@ namespace OC auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); OCHeaderOption options[MAX_HEADER_OPTIONS]; @@ -381,7 +391,8 @@ namespace OC } - OCStackApplicationResult setResourceCallback(void* ctx, OCDoHandle handle, + OCStackApplicationResult setResourceCallback(void* ctx, + OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::SetContext* context = @@ -392,7 +403,8 @@ namespace OC OCStackResult result = clientResponse->result; if (OC_STACK_OK == result || OC_STACK_RESOURCE_CREATED == result || - OC_STACK_RESOURCE_DELETED == result) + OC_STACK_RESOURCE_DELETED == result || + OC_STACK_RESOURCE_CHANGED == result) { parseServerHeaderOptions(clientResponse, serverHeaderOptions); try @@ -413,13 +425,13 @@ namespace OC std::string InProcClientWrapper::assembleSetResourceUri(std::string uri, const QueryParamsMap& queryParams) { - if(uri.back() == '/') + if (uri.back() == '/') { uri.resize(uri.size()-1); } ostringstream paramsList; - if(queryParams.size() > 0) + if (queryParams.size() > 0) { paramsList << '?'; } @@ -430,7 +442,7 @@ namespace OC } std::string queryString = paramsList.str(); - if(queryString.back() == ';') + if (queryString.back() == ';') { queryString.resize(queryString.size() - 1); } @@ -443,6 +455,11 @@ namespace OC { MessageContainer ocInfo; ocInfo.addRepresentation(rep); + for(const OCRepresentation& r : rep.getChildren()) + { + ocInfo.addRepresentation(r); + } + return reinterpret_cast(ocInfo.getPayload()); } @@ -453,7 +470,7 @@ namespace OC const QueryParamsMap& queryParams, const HeaderOptions& headerOptions, PostCallback& callback, QualityOfService QoS) { - if(!callback) + if (!callback) { return OC_STACK_INVALID_PARAM; } @@ -469,7 +486,7 @@ namespace OC auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); OCHeaderOption options[MAX_HEADER_OPTIONS]; @@ -499,7 +516,7 @@ namespace OC const QueryParamsMap& queryParams, const HeaderOptions& headerOptions, PutCallback& callback, QualityOfService QoS) { - if(!callback) + if (!callback) { return OC_STACK_INVALID_PARAM; } @@ -515,7 +532,7 @@ namespace OC auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); OCDoHandle handle; @@ -539,14 +556,15 @@ namespace OC return result; } - OCStackApplicationResult deleteResourceCallback(void* ctx, OCDoHandle handle, + OCStackApplicationResult deleteResourceCallback(void* ctx, + OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::DeleteContext* context = static_cast(ctx); HeaderOptions serverHeaderOptions; - if(clientResponse->result == OC_STACK_OK) + if (clientResponse->result == OC_STACK_OK) { parseServerHeaderOptions(clientResponse, serverHeaderOptions); } @@ -558,9 +576,11 @@ namespace OC OCStackResult InProcClientWrapper::DeleteResource( const OCDevAddr& devAddr, const std::string& uri, - const HeaderOptions& headerOptions, DeleteCallback& callback, QualityOfService QoS) + const HeaderOptions& headerOptions, + DeleteCallback& callback, + QualityOfService /*QoS*/) { - if(!callback) + if (!callback) { return OC_STACK_INVALID_PARAM; } @@ -575,7 +595,7 @@ namespace OC auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { OCHeaderOption options[MAX_HEADER_OPTIONS]; @@ -599,7 +619,8 @@ namespace OC return result; } - OCStackApplicationResult observeResourceCallback(void* ctx, OCDoHandle handle, + OCStackApplicationResult observeResourceCallback(void* ctx, + OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::ObserveContext* context = @@ -608,7 +629,7 @@ namespace OC HeaderOptions serverHeaderOptions; uint32_t sequenceNumber = clientResponse->sequenceNumber; OCStackResult result = clientResponse->result; - if(clientResponse->result == OC_STACK_OK) + if (clientResponse->result == OC_STACK_OK) { parseServerHeaderOptions(clientResponse, serverHeaderOptions); try @@ -623,10 +644,7 @@ namespace OC std::thread exec(context->callback, serverHeaderOptions, attrs, result, sequenceNumber); exec.detach(); - if(sequenceNumber == OC_OBSERVE_DEREGISTER) - { - return OC_STACK_DELETE_TRANSACTION; - } + return OC_STACK_KEEP_TRANSACTION; } @@ -636,7 +654,7 @@ namespace OC const QueryParamsMap& queryParams, const HeaderOptions& headerOptions, ObserveCallback& callback, QualityOfService QoS) { - if(!callback) + if (!callback) { return OC_STACK_INVALID_PARAM; } @@ -668,7 +686,7 @@ namespace OC auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); OCHeaderOption options[MAX_HEADER_OPTIONS]; @@ -693,15 +711,15 @@ namespace OC OCStackResult InProcClientWrapper::CancelObserveResource( OCDoHandle handle, - const std::string& host, // unused - const std::string& uri, // unused + const std::string& /*host*/, + const std::string& /*uri*/, const HeaderOptions& headerOptions, QualityOfService QoS) { OCStackResult result; auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); OCHeaderOption options[MAX_HEADER_OPTIONS]; @@ -719,7 +737,8 @@ namespace OC return result; } - OCStackApplicationResult subscribePresenceCallback(void* ctx, OCDoHandle handle, + OCStackApplicationResult subscribePresenceCallback(void* ctx, + OCDoHandle /*handle*/, OCClientResponse* clientResponse) { ClientCallbackContext::SubscribePresenceContext* context = @@ -742,7 +761,7 @@ namespace OC const std::string& host, const std::string& resourceType, OCConnectivityType connectivityType, SubscribeCallback& presenceHandler) { - if(!presenceHandler) + if (!presenceHandler) { return OC_STACK_INVALID_PARAM; } @@ -761,12 +780,12 @@ namespace OC std::ostringstream os; os << host << OC_RSRVD_PRESENCE_URI; - if(!resourceType.empty()) + if (!resourceType.empty()) { os << "?rt=" << resourceType; } - if(!cLock) + if (!cLock) { delete ctx; return OC_STACK_ERROR; @@ -783,7 +802,7 @@ namespace OC OCStackResult result; auto cLock = m_csdkLock.lock(); - if(cLock) + if (cLock) { std::lock_guard lock(*cLock); result = OCCancel(handle, OC_LOW_QOS, NULL, 0); @@ -807,7 +826,7 @@ namespace OC { int i = 0; - if( headerOptions.size() == 0) + if ( headerOptions.size() == 0) { return nullptr; } @@ -823,4 +842,144 @@ namespace OC return options; } + + std::shared_ptr cloneDevice(const OCDPDev_t* dev) + { + if (!dev) + { + return nullptr; + } + + OCDPDev_t* result = new OCDPDev_t(*dev); + result->prm = new OCPrm_t[dev->prmLen]; + memcpy(result->prm, dev->prm, sizeof(OCPrm_t)*dev->prmLen); + return std::shared_ptr(new OCDirectPairing(result)); + } + + void InProcClientWrapper::convert(const OCDPDev_t *list, PairedDevices& dpList) + { + while(list) + { + dpList.push_back(cloneDevice(list)); + list = list->next; + } + } + + OCStackResult InProcClientWrapper::FindDirectPairingDevices(unsigned short waittime, + GetDirectPairedCallback& callback) + { + if (!callback || 0 == waittime) + { + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result = OC_STACK_ERROR; + const OCDPDev_t *list = nullptr; + PairedDevices dpDeviceList; + + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + + list = OCDiscoverDirectPairingDevices(waittime); + if (NULL == list) + { + result = OC_STACK_NO_RESOURCE; + oclog() << "findDirectPairingDevices(): No device found for direct pairing" + << std::flush; + } + else { + convert(list, dpDeviceList); + std::thread exec(callback, dpDeviceList); + exec.detach(); + result = OC_STACK_OK; + } + } + else + { + result = OC_STACK_ERROR; + } + + return result; + } + + OCStackResult InProcClientWrapper::GetDirectPairedDevices(GetDirectPairedCallback& callback) + { + if (!callback) + { + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result = OC_STACK_ERROR; + const OCDPDev_t *list = nullptr; + PairedDevices dpDeviceList; + + auto cLock = m_csdkLock.lock(); + + if (cLock) + { + std::lock_guard lock(*cLock); + + list = OCGetDirectPairedDevices(); + if (NULL == list) + { + result = OC_STACK_NO_RESOURCE; + oclog() << "findDirectPairingDevices(): No device found for direct pairing" + << std::flush; + } + else { + convert(list, dpDeviceList); + std::thread exec(callback, dpDeviceList); + exec.detach(); + result = OC_STACK_OK; + } + } + else + { + result = OC_STACK_ERROR; + } + + return result; + } + + void directPairingCallback(void *ctx, OCDPDev_t *peer, + OCStackResult result) + { + + ClientCallbackContext::DirectPairingContext* context = + static_cast(ctx); + + std::thread exec(context->callback, cloneDevice(peer), result); + exec.detach(); + } + + OCStackResult InProcClientWrapper::DoDirectPairing(std::shared_ptr peer, + const OCPrm_t& pmSel, const std::string& pinNumber, DirectPairingCallback& callback) + { + if (!peer || !callback) + { + oclog() << "Invalid parameters" << std::flush; + return OC_STACK_INVALID_PARAM; + } + + OCStackResult result = OC_STACK_ERROR; + ClientCallbackContext::DirectPairingContext* context = + new ClientCallbackContext::DirectPairingContext(callback); + + auto cLock = m_csdkLock.lock(); + if (cLock) + { + std::lock_guard lock(*cLock); + result = OCDoDirectPairing(static_cast(context), peer->getDev(), + pmSel, const_cast(pinNumber.c_str()), directPairingCallback); + } + else + { + delete context; + result = OC_STACK_ERROR; + } + return result; + } }