OCStackResult InProcServerWrapper::unregisterResource(const OCResourceHandle& resourceHandle) { auto cLock = m_csdkLock.lock(); OCStackResult result = OC_STACK_ERROR; if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCDeleteResource(resourceHandle); if(result == OC_STACK_OK) { std::lock_guard<std::mutex> lock(OC::details::serverWrapperLock); OC::details::resourceUriMap.erase(resourceHandle); } else { throw OCException(OC::Exception::RESOURCE_UNREG_FAILED, result); } } else { result = OC_STACK_ERROR; } return result; }
OCStackResult InProcServerWrapper::deleteResourceFromRD(const std::string& host, OCConnectivityType connectivityType, ResourceHandles& resourceHandles, DeleteResourceCallback& callback, OCQualityOfService qos) { ServerCallbackContext::DeleteContext* ctx = new ServerCallbackContext::DeleteContext(callback); OCCallbackData cbdata( static_cast<void*>(ctx), deleteResourceFromRDCallback, [](void* c) {delete static_cast<ServerCallbackContext::DeleteContext*>(c);} ); auto cLock = m_csdkLock.lock(); OCStackResult result = OC_STACK_ERROR; if (cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCRDDelete(host.c_str(), connectivityType, &resourceHandles[0], resourceHandles.size(), &cbdata, qos); } if (OC_STACK_OK != result) { throw OCException(OC::Exception::PUBLISH_RESOURCE_FAILED, result); } return result; }
void OCSecureResource::validateSecureResource() { if (!devPtr) { throw OCException("Incomplete secure resource", OC_STACK_RESOURCE_ERROR); } }
OCStackResult result_guard(const OCStackResult r) { std::ostringstream os; switch(r) { default: os << "result_guard(): unhandled exception: " << OCException::reason(r); throw OCException(os.str(), r); /* Exceptional conditions: */ case OC_STACK_NO_MEMORY: case OC_STACK_COMM_ERROR: case OC_STACK_NOTIMPL: case OC_STACK_INVALID_URI: case OC_STACK_INVALID_QUERY: case OC_STACK_INVALID_IP: case OC_STACK_INVALID_PORT: case OC_STACK_INVALID_CALLBACK: case OC_STACK_INVALID_METHOD: case OC_STACK_INVALID_PARAM: case OC_STACK_INVALID_OBSERVE_PARAM: os << "result_guard(): " << r << ": " << OCException::reason(r); throw OCException(os.str(), r); /* Non-exceptional failures or success: */ case OC_STACK_OK: case OC_STACK_NO_RESOURCE: case OC_STACK_RESOURCE_ERROR: case OC_STACK_SLOW_RESOURCE: case OC_STACK_NO_OBSERVERS: case OC_STACK_OBSERVER_NOT_FOUND: #ifdef WITH_PRESENCE case OC_STACK_PRESENCE_STOPPED: case OC_STACK_PRESENCE_TIMEOUT: case OC_STACK_PRESENCE_DO_NOT_HANDLE: #endif break; } return r; }
OCStackResult InProcServerWrapper::stopPresence() { auto cLock = m_csdkLock.lock(); OCStackResult result = OC_STACK_ERROR; if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCStopPresence(); } if(result != OC_STACK_OK) { throw OCException(OC::Exception::END_PRESENCE_FAILED, result); } return result; }
OCStackResult InProcServerWrapper::bindTypeToResource(const OCResourceHandle& resourceHandle, const std::string& resourceTypeName) { auto cLock = m_csdkLock.lock(); OCStackResult result; if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCBindResourceTypeToResource(resourceHandle, resourceTypeName.c_str()); } else { result = OC_STACK_ERROR; } if (result != OC_STACK_OK) { throw OCException(OC::Exception::BIND_TYPE_FAILED, result); } return result; }
OCStackResult InProcServerWrapper::sendResponse( const std::shared_ptr<OCResourceResponse> pResponse) { auto cLock = m_csdkLock.lock(); OCStackResult result = OC_STACK_ERROR; if(!pResponse) { result = OC_STACK_MALFORMED_RESPONSE; throw OCException(OC::Exception::STR_NULL_RESPONSE, OC_STACK_MALFORMED_RESPONSE); } else { OCEntityHandlerResponse response; // OCRepPayload* payLoad = pResponse->getPayload(); HeaderOptions serverHeaderOptions = pResponse->getHeaderOptions(); response.requestHandle = pResponse->getRequestHandle(); response.resourceHandle = pResponse->getResourceHandle(); response.ehResult = pResponse->getResponseResult(); response.payload = reinterpret_cast<OCPayload*>(pResponse->getPayload()); response.persistentBufferFlag = 0; response.numSendVendorSpecificHeaderOptions = serverHeaderOptions.size(); int i = 0; for (auto it=serverHeaderOptions.begin(); it != serverHeaderOptions.end(); ++it) { response.sendVendorSpecificHeaderOptions[i].protocolID = OC_COAP_ID; response.sendVendorSpecificHeaderOptions[i].optionID = static_cast<uint16_t>(it->getOptionID()); response.sendVendorSpecificHeaderOptions[i].optionLength = (it->getOptionData()).length() + 1; std::string optionData = it->getOptionData(); std::copy(optionData.begin(), optionData.end(), response.sendVendorSpecificHeaderOptions[i].optionData); response.sendVendorSpecificHeaderOptions[i].optionData[it->getOptionData().length()] = '\0'; i++; } if(OC_EH_RESOURCE_CREATED == response.ehResult) { pResponse->getNewResourceUri().copy(response.resourceUri, sizeof (response.resourceUri) - 1); response.resourceUri[pResponse->getNewResourceUri().length()] = '\0'; } if(cLock) { std::lock_guard<std::recursive_mutex> lock(*cLock); result = OCDoResponse(&response); } else { OICFree(response.payload); result = OC_STACK_ERROR; } if(result != OC_STACK_OK) { oclog() << "Error sending response\n"; } return result; } }