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)
    {
    }
}
// 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)
    {
    }
}
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;
    }
}
void SensorRule::sensorAdded(std::shared_ptr< OC::OCResource > sensor)
{
    if (objectName().toStdString() != sensor->uri())
        return;

    m_resource = sensor;
}
Example #6
0
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;
    }
}
Example #7
0
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 onPutForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
                      const int eCode)
{
    void onGetForDISensor(const HeaderOptions & headerOptions, const OCRepresentation & rep,
                          const int eCode);

    (void)headerOptions;
    (void)rep;
    try
    {
        if (eCode == OC_STACK_OK)
        {
            std::cout << "PUT request was successful" << std::endl;

            QueryParamsMap test;
            std::cout << "Sending request to: " << DISensorResource->uri() << std::endl;
            DISensorResource->get(test, &onGetForDISensor);
        }
        else
        {
            std::cout << "onPut Response error: " << eCode << std::endl;
        }
    }
    catch (std::exception &e)
    {
        std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
    }
}
void onGetForDISensor(const HeaderOptions &headerOptions, const OCRepresentation &rep,
                      const int eCode)
{
    (void)headerOptions;
    try
    {
        if (eCode == OC_STACK_OK)
        {
            std::cout << "GET request was successful" << std::endl;
            std::cout << "Resource URI: " << DISensorResource->uri() << std::endl;

            std::cout << "Payload: " << rep.getPayload() << std::endl;

            std::cout << "\tdiscomfortIndex: " << rep.getValue<std::string>("discomfortIndex") << std::endl;
        }
        else
        {
            std::cout << "onGET Response error: " << eCode << std::endl;
        }
    }
    catch (std::exception &e)
    {
        std::cout << "Exception: " << e.what() << " in onPut" << std::endl;
    }
}
Example #10
0
// 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;
    }
}
Example #11
0
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 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);
    }

}
Example #13
0
void foundResource1(std::shared_ptr<OCResource> resource)
{
    std::cout << "In foundResource1:" << std::endl;
    if(resource && resource->uri() == "/q/foo1")
    {
        printResourceInfo(resource);
    }
    else
    {
        std::cout << "foundResource1: Ignoring the resource which doesn't have uri /q/foo1\n";
    }
}
// Local function to get representation of light resource
void getLightRepresentation(std::shared_ptr<OCResource> resource)
{
    if (resource)
    {
        std::cout << "Getting Light Representation..." << std::endl;
        // Invoke resource's get API with the callback parameter

        QueryParamsMap test;
        std::cout << "Sending request to: " << resource->uri() << std::endl;
        resource->get(test, &onGet);
    }
}
Example #15
0
    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)));
        }
    }
Example #16
0
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);
    }
}
// Local function to put a different state for this resource
void putLightRepresentation(std::shared_ptr<OCResource> resource)
{
    if (resource)
    {
        OCRepresentation rep;

        std::cout << "Putting light representation..." << std::endl;

        mylight.m_on_off = true;

        std::cout << "Sending request to: " << resource->uri() << std::endl;
        rep.setValue("on-off", mylight.m_on_off);

        // Invoke resource's put API with rep, query map and the callback parameter

        resource->put(rep, QueryParamsMap(), &onPut);
    }
}
Example #18
0
        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());
            }
        }
Example #19
0
void foundResource2(std::shared_ptr<OCResource> resource)
{
    std::cout << "In foundResource2:" << std::endl;

    if(resource && resource->uri() == "/q/foo2")
    {
        printResourceInfo(resource);

        std::cout<<"Doing a get on q/foo."<<std::endl;

        resource->get(QueryParamsMap(),
            GetCallback(std::bind(getResourceInfo, resource,
            std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)));
    }
    else
    {
        std::cout << "foundResource2: Ignoring the resource which doesn't have uri /q/foo2\n";
    }
}
Example #20
0
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());
            }
        }
Example #22
0
int main(int argc, char* argv[])
{
    std::string str_steps;

    try
    {
        //**************************************************************
        // STEP 0
        PlatformConfig cfg
        { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };

        OCPlatform::Configure(cfg);
        //g_thingsmanager = new ThingsManager();
        g_thingsConf = new ThingsConfiguration();
        g_thingsMnt = new ThingsMaintenance();
        g_groupmanager = new GroupManager();

        //**************************************************************

        while (true)
        {
            pthread_mutex_lock(&mutex_lock);
            if (isWaiting > 0)
            {
                pthread_mutex_unlock(&mutex_lock);
                continue;
            }

            isWaiting = 0;
            pthread_mutex_unlock(&mutex_lock);

            cout << endl << endl << "(0) Quit" << std::endl;
            cout << "(1) Find all resources(URI: /oic/con, /oic/mnt, /factoryset)" << std::endl;
            cout << "(2) Find all groups" << std::endl;
            cout << "(3) Get a Configuration resource" << std::endl;
            cout << "(4) Update a device name attribute value" << std::endl;
            cout << "(5) FactoryReset (for the group)" << std::endl;
            cout << "(6) Reboot (for the group)" << std::endl;
            cout << "(10) Show Configuration Units" << std::endl;

            try
            {
                std::getline (std::cin, str_steps);

                if(str_steps == "")
                {
                    continue;
                }
                else
                {
                    g_Steps = std::stoi(str_steps);
                }
            }
            catch(std::invalid_argument&)
            {
                std::cout << "Please put a digit, not string" << std::endl;
                continue;
            }

            if (g_Steps == 0)
            {
                break;
            }
            else if (g_Steps == 1)
            {
                std::vector< std::string > types;

                // For Registering a collection resource for configuration resources
                if (configurationCollectionHandle == NULL)
                {
                    string resourceURI = "/core/a/configuration/resourceset";
                    string resourceTypeName = "core.configuration.resourceset";
                    string resourceInterface = BATCH_INTERFACE;

                    OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
                        resourceTypeName, resourceInterface, NULL,
                        //&entityHandler, // entityHandler
                        OC_DISCOVERABLE);

                    OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
                    OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
                        DEFAULT_INTERFACE);
                }

                // For Registering a collection resource for maintenance resources
                if (maintenanceCollectionHandle == NULL)
                {
                    string resourceURI = "/core/a/maintenance/resourceset";
                    string resourceTypeName = "core.maintenance.resourceset";
                    string resourceInterface = BATCH_INTERFACE;

                    OCPlatform::registerResource(maintenanceCollectionHandle, resourceURI,
                        resourceTypeName, resourceInterface, NULL,
                        //&entityHandler, // entityHandler
                        OC_DISCOVERABLE);

                    OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, GROUP_INTERFACE);
                    OCPlatform::bindInterfaceToResource(maintenanceCollectionHandle, DEFAULT_INTERFACE);
                }

                // For Registering a collection resource for set resources
                if (setCollectionHandle == NULL)
                {
                    string resourceURI = "/core/a/factoryset/resourceset";
                    string resourceTypeName = "core.factoryset.resourceset";
                    string resourceInterface = BATCH_INTERFACE;

                    OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
                        resourceInterface, NULL,
                        //&entityHandler, // entityHandler
                        OC_DISCOVERABLE);

                    OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
                    OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
                }

                types.push_back("oic.wk.con");
                types.push_back("oic.wk.mnt");
                types.push_back("factoryset");

                std::cout << "Finding Configuration Resource... " << std::endl;
                std::cout << "Finding Maintenance Resource... " << std::endl;
                std::cout << "Finding Set Resource... " << std::endl;

                g_groupmanager->findCandidateResources(types, &onFoundCandidateResource, 5);

                pthread_mutex_lock(&mutex_lock);
                isWaiting = 1;
                pthread_mutex_unlock(&mutex_lock);

                thread t(&timeCheck, 5);
                t.join();       // After 5 seconds, isWaiting value will be 0.
            }
            else if (g_Steps == 2) // make a group with found things
            {
                std::vector< std::string > types;
                types.push_back("core.configuration.resourceset");
                types.push_back("core.maintenance.resourceset");
                types.push_back("core.factoryset.resourceset");

                g_groupmanager->findCandidateResources(types, &onFoundCollectionResource, 5);

                std::cout << "Finding Collection resource... " << std::endl;

                pthread_mutex_lock(&mutex_lock);
                isWaiting = 1;
                pthread_mutex_unlock(&mutex_lock);

                thread t(&timeCheck, 5);
                t.join();       // After 5 seconds, isWaiting value will be 0.
            }
            else if (g_Steps == 3)
            {
                // get a value

                ConfigurationName name = "all";

                std::cout << "For example, get configuration resources's value" << std::endl;

                std::vector< ConfigurationName > configurations;

                configurations.push_back(name);

                if (g_thingsConf->getConfigurations(g_configurationCollection, configurations, &onGet)
                        != OC_STACK_ERROR)
                {
                    pthread_mutex_lock(&mutex_lock);
                    isWaiting = 0;
                    pthread_mutex_unlock(&mutex_lock);
                }
            }
            else if (g_Steps == 4)
            {
                ConfigurationName name = "n";
                ConfigurationValue value = "OIC Device";

                if(g_configurationCollection == NULL)
                {
                    std::cout<<"Note that you first create a group to use this command." << std::endl;
                    continue;
                }

                std::cout << "For example, change a device name" << std::endl;
                std::cout << g_configurationCollection->uri() << std::endl;

                std::map< ConfigurationName, ConfigurationValue > configurations;

                configurations.insert(std::make_pair(name, value));

                if (g_thingsConf->updateConfigurations(g_configurationCollection, configurations,
                        &onUpdate) != OC_STACK_ERROR)
                {
                    pthread_mutex_lock(&mutex_lock);
                    isWaiting = 0;
                    pthread_mutex_unlock(&mutex_lock);
                }
            }
            else if (g_Steps == 5)
            {
                // factory reset
                if(g_maintenanceCollection == NULL)
                {
                    std::cout<<"Note that you first create a group to use this command." << std::endl;
                    continue;
                }

                if (g_thingsMnt->factoryReset(g_maintenanceCollection, &onFactoryReset)
                        != OC_STACK_ERROR)
                {
                    pthread_mutex_lock(&mutex_lock);
                    isWaiting = 0;
                    pthread_mutex_unlock(&mutex_lock);
                }
            }
            else if (g_Steps == 6)
            {
                // reboot
                if(g_maintenanceCollection == NULL)
                {
                    std::cout<<"Note that you first create a group to use this command." << std::endl;
                    continue;
                }

                if (g_thingsMnt->reboot(g_maintenanceCollection, &onReboot) != OC_STACK_ERROR)
                {
                    pthread_mutex_lock(&mutex_lock);
                    isWaiting = 0;
                    pthread_mutex_unlock(&mutex_lock);
                }
            }
            else if (g_Steps == 10)
            {
                std::cout << g_thingsConf->getListOfSupportedConfigurationUnits() << std::endl;

            }
        }
    }catch (OCException e)
    {
        std::cout << "Exception in main: " << e.what();
    }

    return 0;
}
Example #23
0
// 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;
    }
}
Example #24
0
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;
    }
}
Example #26
0
// 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());
	}
}
Example #27
0
    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)
                ));
    }
Example #28
0
// 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());
    }
}
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;
    }

}