void foundResource(std::shared_ptr< OCResource > resource) { std::lock_guard<std::mutex> lock(resourceLock); if(g_resource) { std::cout << "Found another resource, ignoring"<<std::endl; return; } std::string resourceURI; std::string hostAddress; try { cout << "FOUND Resource" << endl; if (resource) { string resourceURI = resource->uri(); cout << resourceURI << endl; cout << "HOST :: " << resource->host() << endl; if (resourceURI == "/core/a/collection") { g_resource = resource; resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet); } printf("HOST :: %s\n", resource->host().c_str()); } } catch (std::exception& e) { std::cerr << "Exception in foundResource: "<< e.what() << std::endl; } }
void foundResource(std::shared_ptr<OCResource> resource) { std::string resourceURI; std::string hostAddress; cout << "FOUND RESOURCE" << endl; try { if (resource) { resourceURI = resource->uri(); hostAddress = resource->host(); if (resourceURI == "/core/a/collection") { g_resource = resource; // g_resource->get("", DEFAULT_INTERFACE, QueryParamsMap(), onGet); cout << "FOUND " << resourceURI << endl; // printf("\tHOST :: %s\n", resource->host().c_str()); } else if (resourceURI == "/core/bookmark") { resource->observe(ObserveType::Observe, QueryParamsMap(), &onObserve); } } } catch (std::exception& e) { std::cout << "Exception: " << e.what() << std::endl; } }
void foundResource(std::shared_ptr< OCResource > resource) { std::string resourceURI; std::string hostAddress; try { if(resource) { // if(resource->uri().find("/a/NM/TempHumSensor/virtual") != std::string::npos) if(resource->uri().find("/a/NM/TempHumSensor") != std::string::npos) { std::cout << std::endl; std::cout << "========================================================" << std::endl; std::cout << "DISCOVERED Resource(Consumer):" << std::endl; resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; startObserve(resource); } } else { std::cout << "Resource is invalid" << std::endl; } } catch(std::exception& e) { } }
void printResourceInformation(std::shared_ptr< OC::OCResource > resource) { std::lock_guard<std::mutex> lock(g_iotivity_utility_print_mutex); try { std::cout << "------------------------------------------------------" << std::endl; std::cout << "DISCOVERED Resource:" << std::endl; std::cout << "\tResource address: " << resource->host() << std::endl; std::cout << "\tResource uri: " << resource->uri() << std::endl; std::cout << "\tList of resource interfaces: " << std::endl; for (auto &resourceInterface : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterface << std::endl; } std::cout << "\tList of resource types: " << std::endl; for (auto &resourceType : resource->getResourceTypes()) { std::cout << "\t\t" << resourceType << std::endl; } } catch (std::exception &e) { std::cerr << "Caught exception in printResourceInformation: " << e.what() << std::endl; } }
void printResourceCompact(std::shared_ptr< OC::OCResource > resource) { std::unique_lock<std::mutex> lock(g_iotivity_utility_print_mutex); try { resource->get(OC::QueryParamsMap(), &on_iotivity_utility_get); g_iotivity_utility_condition_variable.wait(lock); std::cout << "\t" << resource->host() << resource->uri() << std::endl // uri is links.href << "\tresourceTypes "; for (auto rt : resource->getResourceTypes()) { std::cout << rt << " "; } std::cout << std::endl; std::cout << "\tresourceInterfaces "; for (auto ri : resource->getResourceInterfaces()) { std::cout << ri << " "; } std::cout << std::endl; std:: cout << "\tsid " << resource->sid() << std::endl; } catch (std::exception &e) { std::cerr << "Caught exception in printResourceInformation: " << e.what() << std::endl; } }
// Callback to found resources static void foundResource(std::shared_ptr< OCResource > resource) { try { if(resource) { DBG("DISCOVERED Resource:"); DBG("\tURI of the resource: %s" , resource->uri().c_str()); DBG("\tHost address of the resource: %s" , resource->host().c_str()); if(resource->uri().find("/a/NM") != string::npos) { g_curResource = resource; onfound(); } } else { ERR("Resource is invalid"); } } catch(std::exception& e) { } }
// Callback to found resources void foundResource(std::shared_ptr<OCResource> resource) { std::lock_guard<std::mutex> lock(curResourceLock); if(curResource) { std::cout << "Found another resource, ignoring"<<std::endl; return; } std::string resourceURI; std::string hostAddress; try { // Do some operations with resource object. if(resource) { std::cout<<"DISCOVERED Resource:"<<std::endl; // Get the resource URI resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; // Get the resource host address hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; // Get the resource types std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } if(resourceURI == "/a/garage") { curResource = resource; // Call a local function which will internally invoke // get API on the resource pointer getLightRepresentation(resource); } } else { // Resource is invalid std::cout << "Resource is invalid" << std::endl; } } catch(std::exception& e) { std::cerr << "Exception in foundResource: "<< e.what()<<std::endl; } }
void foundResource(std::shared_ptr<OCResource> resource) { std::cout << "In foundResource\n"; std::string resourceURI; std::string hostAddress; try { if(discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end()) { std::cout << "Found resource " << resource->uniqueIdentifier() << " for the first time on server with ID: "<< resource->sid()<<std::endl; discoveredResources[resource->uniqueIdentifier()] = resource; } else { std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl; } if(curResource) { std::cout << "Found another resource, ignoring"<<std::endl; return; } if(resource) { std::cout<<"DISCOVERED Resource:"<<std::endl; resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } if(resourceURI == "/a/rvi") { curResource = resource; dlog_print(DLOG_INFO, LOG_TAG, "RVI resource detected"); ocfinitialized = 1; } } else { std::cout << "Resource is invalid" << std::endl; } } catch(std::exception& e) { std::cerr << "Exception in foundResource: "<< e.what() << std::endl; } std::cout << "-----------------------Exit foundResource\n"; }
void CResourceFinder::onResourceFound(std::shared_ptr< OC::OCResource > resource) { if (resource) { std::string path = resource->host() + resource->uri(); if (m_mapResourceHandler.find(path) != m_mapResourceHandler.end()) return; intptr_t *pMessage = new intptr_t [2]; pMessage[0] = RESOURCE_DISCOVER_REQUESTPROFILE; pMessage[1] = reinterpret_cast<intptr_t> (new std::shared_ptr<OC::OCResource>(resource)); m_pTasker->addTask(this, pMessage); } }
void RemoteEnrollee::onDeviceDiscovered(std::shared_ptr<OC::OCResource> resource) { OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onDeviceDiscovered"); std::string resourceURI; std::string hostAddress; std::string hostDeviceID; try { if(resource) { if(!(resource->connectivityType() & CT_ADAPTER_TCP)) { // Get the resource URI resourceURI = resource->uri(); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "URI of the resource: %s", resourceURI.c_str()); // Get the resource host address hostAddress = resource->host(); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "Host address of the resource: %s", hostAddress.c_str()); hostDeviceID = resource->sid(); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "Host DeviceID of the resource: %s", hostDeviceID.c_str()); if(!m_deviceId.empty() && m_deviceId == hostDeviceID) { OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_TAG, "Find matched CloudResource"); m_ocResource = resource; m_discoveryResponse = true; m_cond.notify_all(); } } } } catch(std::exception& e) { OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "Exception in foundResource: %s", e.what()); } }
void onfound() { if(g_curResource->uri().find("/a/NM/TempHumSensor/virtual") != string::npos) { oicappData *ad = g_oicFindAd; std::string tmpuri = PREFIX_URI + g_curResource->uri(); std::string tmphost = PREFIX_HOST + g_curResource->host(); DBG("OnFound Resource..."); DBG("Resource Uri : %s" , tmpuri.c_str()); DBG("Resource Host: %s" , tmphost.c_str()); _gl_update_item(ad , tmphost.c_str() , ad->itemConsumerHost); _gl_update_item(ad , tmpuri.c_str() , ad->itemConsumerUri); } }
void printResourceInfo(std::shared_ptr<OCResource> resource) { std::cout << "Found Resource: "<<std::endl; std::cout << "\tHost: "<< resource->host()<<std::endl; std::cout << "\tURI: "<< resource->uri()<<std::endl; // Get the resource types std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } }
void RemoteEnrolleeResource::onDeviceDiscovered(std::shared_ptr<OC::OCResource> resource) { OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onDeviceDiscovered"); std::string resourceURI; std::string hostAddress; try { if(resource) { // Get the resource URI resourceURI = resource->uri(); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "URI of the resource: %s", resourceURI.c_str()); // Get the resource host address hostAddress = resource->host(); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Host address of the resource: %s", hostAddress.c_str()); /* * Easysetup is always performed with a single Enrollee device and * in a private network (SoftAP or BLE), so the assumption is that * only the intended device will respond for the discovery. * With the above assumption the below two statements are written. */ m_ocResource = resource; m_discoveryResponse = true; } else { OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Resource is invalid"); } } catch(std::exception& e) { OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Exception in foundResource: %s", e.what()); } }
void foundResource(std::shared_ptr<OCResource> resource) { std::cout << "In foundResource" << std::endl; if(resource && resource->uri() == "/q/foo") { { std::lock_guard<std::mutex> lock(m_resourceLock); if(m_resource) { return; } m_resource = resource; } std::cout << "Found Resource: "<<std::endl; std::cout << "\tHost: "<< resource->host()<<std::endl; std::cout << "\tURI: "<< resource->uri()<<std::endl; // Get the resource types std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } std::cout<<"Doing a get on q/foo."<<std::endl; resource->get(QueryParamsMap(), GetCallback(std::bind(&ClientWorker::getResourceInfo, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))); } }
// Callback to found resources void foundResource(std::shared_ptr<OCResource> resource) { std::string resourceURI; std::string hostAddress; try { // Do some operations with resource object. if(resource) { std::lock_guard<std::mutex> lk(resourceLock); if(discoveredResources.find(resource) == discoveredResources.end()) { std::cout << "Found resource " << resource->uniqueIdentifier() << " for the first time on server with ID: "<< resource->sid()<<std::endl; discoveredResources.insert(resource); } else { std::cout<<"Found resource "<< resource->uniqueIdentifier() << " again!"<<std::endl; } if(curResource) { std::cout << "Found another resource, ignoring"<<std::endl; return; } std::cout<<"DISCOVERED Resource:"<<std::endl; // Get the resource URI resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; // Get the resource host address hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; // Get the resource types std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } if(resourceURI == "/a/light") { curResource = resource; sleep(1); // Call a local function which will internally invoke get // API on the resource pointer getLightRepresentation(resource); } } else { // Resource is invalid std::cout << "Resource is invalid" << std::endl; } } catch(std::exception& e) { std::cerr << "Exception in foundResource: "<< e.what() <<std::endl; } }
void GroupManager::onFoundResource(std::shared_ptr< OCResource > resource, int waitsec) { std::string resourceURI; std::string hostAddress; try { // Do some operations with resource object. if (resource) { std::cout << "DISCOVERED Resource:" << std::endl; // Get the resource URI resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; // Get the resource host address hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; // Get the resource types std::cout << "\tList of resource types: " << std::endl; hostAddress.append(resourceURI); for (auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; if (std::find(allFoundResourceTypes.begin(), allFoundResourceTypes.end(), resourceTypes) == allFoundResourceTypes.end()) { allFoundResourceTypes.push_back(resourceTypes); } rtForResourceList[resourceTypes][hostAddress] = resource; } // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; for (auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } if (waitsec == -1) { findPreparedRequest(candidateRequest); } } else { // Resource is invalid std::cout << "Resource is invalid" << std::endl; } } catch (std::exception& e) { //log(e.what()); } }
// Callback to found resources void foundResource(std::shared_ptr<OCResource> resource) { std::cout << "In foundResource\n"; std::string resourceURI = resource->uri(); std::string hostAddress; try { { std::lock_guard<std::mutex> lock(curResourceLock); if (discoveredResources.find(resource->uniqueIdentifier()) == discoveredResources.end()) { std::cout << "Found resource " << resource->uniqueIdentifier() << " for the first time on server with ID: " << resource->sid() << std::endl; discoveredResources[resource->uniqueIdentifier()] = resource; if (resourceURI.find("/discomfortIndex") != std::string::npos) { std::cout << "discomfortIndex found !!! " << std::endl; DISensorResource = resource; OCRepresentation rep; rep.setValue("humidity", std::string("30")); rep.setValue("temperature", std::string("27")); resource->put(rep, QueryParamsMap(), &onPutForDISensor); } } else { std::cout << "Found resource " << resource->uniqueIdentifier() << " again!" << std::endl; } if (curResource) { std::cout << "Found another resource, ignoring" << std::endl; return; } } // Do some operations with resource object. if (resource) { std::cout << "DISCOVERED Resource:" << std::endl; // Get the resource URI resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; // Get the resource host address hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; // Get the resource types std::cout << "\tList of resource types: " << std::endl; for (auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; if (resourceTypes == "oic.r.light") { curResource = resource; // Call a local function which will internally invoke get API on the resource pointer getLightRepresentation(resource); } } // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; for (auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } } else { // Resource is invalid std::cout << "Resource is invalid" << std::endl; } } catch (std::exception &e) { std::cerr << "Exception in foundResource: " << e.what() << std::endl; } }
// Callback to found resources void SensorResource::foundResource(std::shared_ptr<OCResource> resource) { std::string resourceURI; std::string hostAddress; std::lock_guard<std::mutex> lock(m_resourceLock); try { if(resource) { std::cout<<"DISCOVERED Resource:"<<std::endl; resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } GetCallback g (std::bind(&SensorResource::onGet, this, PH::_1, PH::_2, PH::_3)); ObserveCallback o (std::bind(&SensorResource::onObserve, this, PH::_1, PH::_2, PH::_3, PH::_4)); if (resourceURI == "/sensor/gas") { std::cout << "\tFound Gas Sensor." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); m_gas.s_resource = resource; m_gas.s_active = true; } else if (resourceURI == "/a/fan") { std::cout << "\tFound Fan Device." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); QueryParamsMap params; resource->get(params, g); m_fan.s_resource = resource; m_fan.s_active = true; } else if (resourceURI == "/intel/chainable_led_edison") { std::cout << "\tFound Edison Led Device." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); QueryParamsMap params; resource->get(params, g); m_led.s_resource = resource; m_led.s_active = true; } else if (resourceURI == "/sensor/pri") { std::cout << "\tFound Pri Device." << std::endl; resource->observe(ObserveType::Observe, QueryParamsMap(), o); m_pri.s_resource = resource; m_pri.s_active = true; } else { std::cout << "Resource unknown." << std::endl; } } else { std::cout << "Resource is invalid" << std::endl; } } catch(std::exception& e) { //log(e.what()); } }
void foundDevice(std::shared_ptr<OCResource> resource) { using namespace OC::OCPlatform; if(resource && resource->uri() == "/device") { std::cout << "Discovered a device object"<<std::endl; std::cout << "\tHost: "<<resource->host()<<std::endl; std::cout << "\tURI: "<<resource->uri() <<std::endl; } // we have now found a resource, so lets create a few resource objects // for the other resources that we KNOW are associated with the intel.fridge // server, and query them. std::vector<std::string> lightTypes = {"intel.fridge.light"}; std::vector<std::string> ifaces = {DEFAULT_INTERFACE}; OCResource::Ptr light = constructResourceObject(resource->host(), "/light", m_connectivityType, false, lightTypes, ifaces); if(!light) { std::cout << "Error: Light Resource Object construction returned null\n"; return; } std::vector<std::string> doorTypes = {"intel.fridge.door"}; OCResource::Ptr leftdoor = constructResourceObject(resource->host(), "/door/left", m_connectivityType, false, doorTypes, ifaces); if(!leftdoor) { std::cout << "Error: Left Door Resource Object construction returned null\n"; return; } OCResource::Ptr rightdoor = constructResourceObject(resource->host(), "/door/right", m_connectivityType, false, doorTypes, ifaces); if(!rightdoor) { std::cout << "Error: Right Door Resource Object construction returned null\n"; return; } OCResource::Ptr randomdoor = constructResourceObject(resource->host(), "/door/random", m_connectivityType, false, doorTypes, ifaces); if(!randomdoor) { std::cout << "Error: Random Door Resource Object construction returned null\n"; return; } // Set header options with API version and token HeaderOptions headerOptions; try { // Set API version and client token HeaderOption::OCHeaderOption apiVersion(API_VERSION, "v.1.0"); HeaderOption::OCHeaderOption clientToken(TOKEN, "21ae43gf"); headerOptions.push_back(apiVersion); headerOptions.push_back(clientToken); } catch(OCException& e) { std::cout << "Error creating HeaderOption: " << e.what() << std::endl; } // Setting header options will send above options in all requests // Header options are set per resource. // Below, header options are set only for device resource resource->setHeaderOptions(headerOptions); ++m_callsMade; resource->get(QueryParamsMap(), GetCallback( std::bind(&ClientFridge::getResponse, this, "Device", PH::_1, PH::_2, PH::_3, resource, 0) )); ++m_callsMade; light->get(QueryParamsMap(), GetCallback( std::bind(&ClientFridge::getResponse, this, "Fridge Light", PH::_1, PH::_2, PH::_3, light, 1) )); ++m_callsMade; leftdoor->get(QueryParamsMap(), GetCallback( std::bind(&ClientFridge::getResponse, this, "Left Door", PH::_1, PH::_2, PH::_3, leftdoor, 2) )); ++m_callsMade; rightdoor->get(QueryParamsMap(), GetCallback( std::bind(&ClientFridge::getResponse, this, "Right Door", PH::_1, PH::_2, PH::_3, rightdoor, 3) )); ++m_callsMade; randomdoor->get(QueryParamsMap(), GetCallback( std::bind(&ClientFridge::getResponse, this, "Random Door", PH::_1, PH::_2, PH::_3, randomdoor, 4) )); ++m_callsMade; resource->deleteResource(DeleteCallback( std::bind(&ClientFridge::deleteResponse, this, "Device", PH::_1, PH::_2, resource, 0) )); }
void resourceFindCallback::foundResource(std::shared_ptr<OCResource> resource) { resourceCallbackMutex.lock(); Dart_CObject* result = new Dart_CObject; // Indicate invocation m_callbackInvoked = true; try { // Check for a null resource if (resource == NULL) { // Return a boolean here to indicate no resources found Dart_CObject* servicePortObject = m_message->value.as_array.values[EXT_SERVICE_PORT]; Dart_Port reply_port_id = servicePortObject->value.as_send_port.id; result->type = Dart_CObject_kBool; result->value.as_bool = false; Dart_PostCObject(reply_port_id, result); resourceCallbackMutex.unlock(); #ifdef DEBUG std::cout << "<<< foundResource - returned invalid result" << std::endl; #endif } else { #ifdef DEBUG //std::cout << "<<< foundResource - resource id is " << resource->uniqueIdentifier() << std::endl; #endif // Build and return the result for a found resource // Host Dart_CObject* retHost = new Dart_CObject; std::string host = resource->host(); retHost->type = Dart_CObject_kString; retHost->value.as_string = const_cast<char*> (host.c_str()); // Uri Dart_CObject* retUri = new Dart_CObject; std::string uri = resource->uri(); retUri->type = Dart_CObject_kString; retUri->value.as_string = const_cast<char*> (uri.c_str()); // Unique id Dart_CObject* retUid = new Dart_CObject; retUid->type = Dart_CObject_kString; retUid->value.as_string = const_cast<char*> (uri.c_str()); // We don't know how many resource and interface types a resource has so // use dynamic memory allocation, not Dart as we have no zone in this callback. // Resource types Dart_CObject* retResourceTypes = new Dart_CObject; Dart_CObject* resTemp; std::vector<std::string> resourceTypes = resource->getResourceTypes(); long unsigned int resourceTypesLen = resourceTypes.size(); Dart_CObject** resourceTemp = new Dart_CObject*[resourceTypesLen]; int i = 0; for (std::vector<std::string>::iterator it = resourceTypes.begin(); it != resourceTypes.end(); ++it) { resTemp = new Dart_CObject; resTemp->type = Dart_CObject_kString; resTemp->value.as_string = const_cast<char*> ((*it).c_str()); resourceTemp[i] = resTemp; i++; } retResourceTypes->type = Dart_CObject_kArray; retResourceTypes->value.as_array.values = resourceTemp; retResourceTypes->value.as_array.length = resourceTypesLen; // Interface types Dart_CObject* retInterfaceTypes = new Dart_CObject; Dart_CObject* intTemp; std::vector<std::string> intTypes = resource->getResourceInterfaces(); long unsigned intTypesLen = intTypes.size(); Dart_CObject** interfaceTemp = new Dart_CObject*[intTypesLen]; i = 0; for (std::vector<std::string>::iterator it = intTypes.begin(); it != intTypes.end(); ++it) { intTemp = new Dart_CObject; intTemp->type = Dart_CObject_kString; intTemp->value.as_string = const_cast<char*> ((*it).c_str()); interfaceTemp[i] = intTemp; i++; } retInterfaceTypes->type = Dart_CObject_kArray; retInterfaceTypes->value.as_array.values = interfaceTemp; retInterfaceTypes->value.as_array.length = intTypesLen; // Observable Dart_CObject* retObservable = new Dart_CObject; bool observable = resource->isObservable(); retObservable->type = Dart_CObject_kBool; retObservable->value.as_bool = observable; // Create a proxy object from the resource so we can see this outside // of this handler. OCResource::Ptr resourcePtr = OCPlatform::constructResourceObject(resource->host(), resource->uri(), resource->connectivityType(), resource->isObservable(), resource->getResourceTypes(), resource->getResourceInterfaces()); // The proxy object is only supports put/get/observe functionality so get as // much resource data as we can here and return it to create a resource class. // The pointer Dart_CObject* retPtr = new Dart_CObject; retPtr->type = Dart_CObject_kInt64; retPtr->value.as_int64 = reinterpret_cast<int64_t> (resourcePtr.get()); // Return it all Dart_CObject** temp = new Dart_CObject*[PLATFORM_FIND_RESOURCES_RET_PARAMS]; temp[0] = retPtr; temp[1] = retUid; temp[2] = retUri; temp[3] = retHost; temp[4] = retResourceTypes; temp[5] = retInterfaceTypes; temp[6] = retObservable; result->type = Dart_CObject_kArray; result->value.as_array.values = temp; result->value.as_array.length = PLATFORM_FIND_RESOURCES_RET_PARAMS; if (m_resListCount <= (MAX_DISCOVERABLE_RESOURCES - 1)) { m_resList[m_resListCount] = result; ++m_resListCount; } #ifdef DEBUG std::cout << "<<< foundResource - returned valid result id is " << resource->uniqueIdentifier() << std::endl; std::cout << "<<< foundResource - returned valid result uri is " << resource->uri() << std::endl; #endif } resourceCallbackMutex.unlock(); } catch (std::exception& e) { std::cout << "Exception in foundResource: " << e.what() << std::endl; } }
// Callback to found resources void foundResource(std::shared_ptr<OCResource> resource) { std::lock_guard<std::mutex> lock(resourceLock); if(curResource) { std::cout << "Found another resource, ignoring"<<std::endl; return; } std::string resourceURI; std::string hostAddress; try { // Do some operations with resource object. if(resource) { std::cout<<"DISCOVERED Resource:"<<std::endl; // Get the resource URI resourceURI = resource->uri(); std::cout << "\tURI of the resource: " << resourceURI << std::endl; // Get the resource host address hostAddress = resource->host(); std::cout << "\tHost address of the resource: " << hostAddress << std::endl; // Get the resource types std::cout << "\tList of resource types: " << std::endl; for(auto &resourceTypes : resource->getResourceTypes()) { std::cout << "\t\t" << resourceTypes << std::endl; } // Get the resource interfaces std::cout << "\tList of resource interfaces: " << std::endl; for(auto &resourceInterfaces : resource->getResourceInterfaces()) { std::cout << "\t\t" << resourceInterfaces << std::endl; } if(resourceURI == "/a/light") { OCStackResult result = OC_STACK_OK; curResource = resource; OCPlatform::OCPresenceHandle presenceHandle = nullptr; if(TEST_CASE == TEST_UNICAST_PRESENCE_NORMAL) { result = OCPlatform::subscribePresence(presenceHandle, hostAddress, connectivityType, &presenceHandler); if(result == OC_STACK_OK) { std::cout<< "Subscribed to unicast address: " << hostAddress << std::endl; } else { std::cout<< "Failed to subscribe to unicast address:" << hostAddress << std::endl; } } if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTER || TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS) { result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.light", connectivityType, &presenceHandler); if(result == OC_STACK_OK) { std::cout<< "Subscribed to unicast address: " << hostAddress; } else { std::cout<< "Failed to subscribe to unicast address: " << hostAddress; } std::cout << " with resource type \"core.light\"." << std::endl; } if(TEST_CASE == TEST_UNICAST_PRESENCE_WITH_FILTERS) { result = OCPlatform::subscribePresence(presenceHandle, hostAddress, "core.fan", connectivityType, &presenceHandler); if(result == OC_STACK_OK) { std::cout<< "Subscribed to unicast address: " << hostAddress; } else { std::cout<< "Failed to subscribe to unicast address: " << hostAddress; } std::cout << " with resource type \"core.fan\"." << std::endl; } } } else { // Resource is invalid std::cout << "Resource is invalid" << std::endl; } } catch(std::exception& e) { //log(e.what()); } }