Ejemplo n.º 1
0
    // Note that resourceName, resource, and getId are all bound via the std::bind mechanism.
    // it is possible to attach ANY arbitrary data to do whatever you would like here.  It may,
    // however be a better fit to wrap each call in an object so a fuller context (and additional
    // requests) can be easily made inside of a simple context
    void getResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
                const OCRepresentation rep, const int eCode, OCResource::Ptr resource, int getId)
    {
        std::cout << "Got a response from get from the " << resourceName << std::endl;
        std::cout << "Get ID is "<<getId<<" and resource URI is " << resource->uri() << std::endl;

        printHeaderOptions(headerOptions);

        std::cout << "The Attribute Data is: "<<std::endl;

        switch(getId)
        {
            case 0:
                {
                    // Get on device
                    std::string name;
                    rep.getValue("device_name", name);
                    std::cout << "Name of device: "<< name << std::endl;
                    break;
                }
            case 1:
                {
                    bool isOn = false;
                    rep.getValue("on",isOn);
                    std::cout<<"The fridge light is "<< ((isOn)?"":"not ") <<"on"<<std::endl;
                }
                break;
            case 2:
            case 3:
                {
                    bool isOpen = false;
                    std::string side;
                    rep.getValue("open", isOpen);
                    rep.getValue("side", side);
                    std::cout << "Door is "<<isOpen<<" and is on the "<<side<<std::endl;
                }
                break;
            case 4:
                {
                    // Get on random resource called.
                    std::string name;
                    rep.getValue("device_name", name);
                    std::cout << "Name of fridge: "<< name << std::endl;
                    break;
                }
        }
        ++m_callbackCount;

        if(m_callbackCount == m_callsMade)
        {
            m_cv.notify_all();
        }
    }
Ejemplo n.º 2
0
    //Callback function to handle response for deleteResource call.
    void deleteResponse(const std::string& resourceName, const HeaderOptions& headerOptions,
                const int eCode, OCResource::Ptr resource, int deleteId)
    {
        std::cout << "Got a response from delete from the "<< resourceName << std::endl;
        std::cout << "Delete ID is "<<deleteId<<" and resource URI is "<<resource->uri()<<std::endl;
        printHeaderOptions(headerOptions);

        ++m_callbackCount;

        if(m_callbackCount == m_callsMade)
        {
            m_cv.notify_all();
        }
    }
Ejemplo n.º 3
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;
    }

}
Ejemplo n.º 4
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)
                ));
    }