Ejemplo n.º 1
2
void app_start(int, char**) {

	// setup the EthernetInterface
	eth.init();	// DHCP
	eth.connect();
	
	// initialize the ipv4 tcp/ip stack
	lwipv4_socket_init();

	// setup the M2MInterface
	srand(time(NULL));
	uint16_t port = rand() % 65535 + 12345;
	srv = M2MInterfaceFactory::create_interface(observer,endpoint,type,
		lifetime,port,domain,M2MInterface::UDP,M2MInterface::LwIP_IPv4,context_address);

	// setup the Security object		
	sec = M2MInterfaceFactory::create_security(M2MSecurity::M2MServer);
	sec->set_resource_value(M2MSecurity::M2MServerUri,address);
	sec->set_resource_value(M2MSecurity::SecurityMode, M2MSecurity::NoSecurity);

	// setup the Device object
	dev = M2MInterfaceFactory::create_device();
	dev->create_resource(M2MDevice::Manufacturer,"Freescale");
	dev->create_resource(M2MDevice::DeviceType,"frdm-k64f");
	dev->create_resource(M2MDevice::ModelNumber,"M64FN1MOVLL12");
	dev->create_resource(M2MDevice::SerialNumber,"EB1524XXXX");

	// setup the sensor objects
	obj = M2MInterfaceFactory::create_object("loc");
	M2MObjectInstance* ins = obj->create_object_instance();
	M2MResource* resx = ins->create_dynamic_resource("x","accel",M2MResourceInstance::INTEGER,true);
	resx->set_operation(M2MBase::GET_PUT_ALLOWED);
	resx->set_value((const uint8_t*)"0",1);
	M2MResource* resy = ins->create_dynamic_resource("y","accel",M2MResourceInstance::INTEGER,true);
	resy->set_operation(M2MBase::GET_PUT_ALLOWED);
	resy->set_value((const uint8_t*)"0",1);

	
	// Assemble the list of objects to register
	M2MObjectList list;
	list.push_back(dev);
	list.push_back(obj);

	// setup registration event
	Ticker timer;
	timer.attach(&reg,&Registrar::update,20);

	// enable accelerometer
	printf("Initializied accelerometer\r\n");
	accel.enable();
	Ticker sampler;
	sampler.attach(sample,5);

	// schedule 
	FunctionPointer1<void, M2MObjectList> fp(&reg, &Registrar::setup);
	minar::Scheduler::postCallback(fp.bind(list));
	minar::Scheduler::postCallback(sample).period(minar::milliseconds(10000));
	minar::Scheduler::start();
}
Ejemplo n.º 2
0
bool M2MLWClient::create_object(const char *name,
                                bool new_instance,
                                uint8_t object_operation,
                                uint8_t object_instance_operation,
                                uint16_t object_instance_id,
                                bool object_observable,
                                bool object_instance_observable)
{
    bool success = false;
    M2MObjectInstance *inst = NULL;
    if(!_object) {
        _object = M2MInterfaceFactory::create_object(name);
        if(_object) {
            _object->set_operation(int_to_operation(object_operation));
            _object->set_observable(object_observable);
            inst = _object->create_object_instance(object_instance_id);
            if(inst) {
                success = true;
                inst->set_operation(int_to_operation(object_instance_operation));
                inst->set_observable(object_instance_observable);
            }
        }
    } else {
        if(new_instance) {
            inst = _object->create_object_instance(object_instance_id);
            if(inst) {
                success = true;
                inst->set_operation(int_to_operation(object_instance_operation));
                inst->set_observable(object_instance_observable);
            }
        }
    }
    return success;
}
Ejemplo n.º 3
0
    M2MObject* create_generic_object() {
        _object = M2MInterfaceFactory::create_object("Test");
        if(_object) {
            M2MObjectInstance* inst = _object->create_object_instance();
            if(inst) {
                    M2MResource* res = inst->create_dynamic_resource("D",
                                                                     "ResourceTest",
                                                                     M2MResourceInstance::INTEGER,
                                                                     true);
                    char buffer[20];
                    int size = sprintf(buffer,"%d",_value);
                    res->set_operation(M2MBase::GET_PUT_ALLOWED);
                    res->set_value((const uint8_t*)buffer,
                                   (const uint32_t)size);
                    _value++;

                    inst->create_static_resource("S",
                                                 "ResourceTest",
                                                 M2MResourceInstance::STRING,
                                                 STATIC_VALUE,
                                                 sizeof(STATIC_VALUE)-1);
            }
        }
        return _object;
    }
Ejemplo n.º 4
0
 M2MObject* create_led_object() {
     _sdw_led_object = M2MInterfaceFactory::create_object("311");
     
     if (_sdw_led_object) {
         M2MObjectInstance* inst = _sdw_led_object->create_object_instance();
         if (inst) {
             M2MResource* res = inst->create_dynamic_resource("5850",
                                                              "LED",
                                                              M2MResourceInstance::STRING,
                                                              false);
             if (res) {
                 char buffer[10] = "";
                 memset(buffer,0,10);
                 strcpy(buffer,"0");
                 if (__led == 1) strcpy(buffer,"1");
                 
                 // set the value of the LED 
                 res->set_operation(M2MBase::GET_PUT_ALLOWED);   // we allow GET and PUT of the LED
                 res->set_value((const uint8_t*)buffer,
                                (const uint32_t)strlen(buffer));
             }
         }
     }
     return _sdw_led_object;
 }
Ejemplo n.º 5
0
bool M2MLWClient::create_dynamic_resource_instance_string(const char *name,
                                                   bool observable,
                                                   bool multiple_instance,
                                                   uint16_t object_instance,
                                                   uint16_t resource_instance,
                                                   uint8_t resource_instance_operation)
{
    bool success = false;
    String name_string;
    if(name) {
        name_string += name;
    }
    if(_object) {
        M2MObjectInstance *inst = _object->object_instance(object_instance);
        if(inst) {
            M2MResourceInstance *res = inst->create_dynamic_resource_instance(name,"resource",
                                                                      M2MResourceInstance::STRING,
                                                                      observable,
                                                                      resource_instance);
            if( res) {
                success = true;
                res->set_operation(int_to_operation(resource_instance_operation));
            }
        }
    }
    return success;
}
Ejemplo n.º 6
0
bool M2MObject::remove_object_instance(uint16_t inst_id)
{
    tr_debug("M2MObject::remove_object_instance(inst_id %d)", inst_id);
    bool success = false;
    if(!_instance_list.empty()) {
        M2MObjectInstance* obj = NULL;
        M2MObjectInstanceList::const_iterator it;
        it = _instance_list.begin();
        int pos = 0;
        for ( ; it != _instance_list.end(); it++, pos++ ) {
            if((*it)->instance_id() == inst_id) {
                // Instance found and deleted.
                obj = *it;
                String obj_name = name();
                obj_name.push_back('/');
                obj_name.append_int(obj->instance_id());
                obj->remove_resource_from_coap(obj_name);
                _instance_list.erase(pos);
                delete obj;
                success = true;
                break;
            }
        }
    }
    return success;
}
Ejemplo n.º 7
0
bool M2MLWClient::create_static_resource_instance_string(const char *name,
                                                         const char *value,
                                                         bool multiple_instance,
                                                         uint16_t object_instance,
                                                         uint16_t resource_instance)
{
    bool success = false;
    String name_string;
    if(name) {
        name_string += name;
    }
    String value_string;
    if(value) {
        value_string += value;
    }
    if(_object) {
        M2MObjectInstance *inst = _object->object_instance(object_instance);
        if(inst) {
            if(inst->create_static_resource_instance(name,"resource",
                                                    M2MResourceInstance::STRING,
                                                    (const uint8_t*)value_string.c_str(),
                                                    value_string.size(),
                                                    resource_instance) != NULL) {
                success = true;
            }
        }
    }
    return success;
}
Ejemplo n.º 8
0
    bool create_generic_object() {
        bool success = false;
        _object = M2MInterfaceFactory::create_object("10");
        if(_object) {
            _object->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
            M2MObjectInstance* inst = _object->create_object_instance();
            if(inst) {
                inst->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
                inst->set_observable(false);
                char buffer[20];
                int size = sprintf(buffer,"%d",_value);

                inst->create_static_resource("0",
                                             "ResourceTest",
                                             M2MResourceInstance::INTEGER,
                                             STATIC_VALUE,
                                             sizeof(STATIC_VALUE)-1);

                M2MResourceInstance* instance = inst->create_dynamic_resource_instance("1",
                                                "ResourceTest",
                                                M2MResourceInstance::INTEGER,
                                                true,0);

                if(instance) {
                    instance->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
                    instance->set_value((const uint8_t*)buffer,
                                        (const uint32_t)size);
                    instance->set_execute_function(execute_callback(this,&MbedClient::execute_function));
                    _value++;
                }
            }
        }
        return success;
    }
Ejemplo n.º 9
0
 //Callback from mbed client stack if any value has changed
 // during PUT operation. Object and its type is passed in
 // the callback.
 void value_updated(M2MBase *base, M2MBase::BaseType type) {
     output.printf("\nUpdate Object name %s and Type %d\n", base->name().c_str(), type);
     
     // only the LED supports PUT so lets see if its this instance...
     M2MObjectInstance* inst = _sdw_led_object->object_instance();
     if (inst) {
         // should be the /311/0/5850 resource... check for it...
         M2MResource* res = inst->resource("5850");
         if (res != NULL && base == res) {
             // extract the LED value...
             char *new_led_value = (char *)(res->value());
             
             // DEBUG
             printf("\nLight Switch Resource Changed! [%s]\n",new_led_value);
             
             // Update the LED
             if (strcmp(new_led_value,"1") == 0) {
                 __led = 0;
             }
             else {
                 __led = 1;
             }
         }
     }
 }
Ejemplo n.º 10
0
bool M2MLWClient::set_resource_instance_value(const char *name,
                                              const char *value,
                                              uint16_t object_instance,
                                              uint16_t resource_instance)
{
    bool success = false;
    String name_string;
    String value_string;
    if(name) {
        name_string += name;
    }
    if(value) {
        value_string += value;
    }

    if(_object && name_string.length() > 0) {
        M2MObjectInstance *inst = _object->object_instance(object_instance);
        if(inst) {
            M2MResource *res = inst->resource(name_string);
            if (res) {
                M2MResourceInstance *res_inst = res->resource_instance(resource_instance);
                if(res_inst) {
                    if (res_inst->set_value((const uint8_t*)value_string.c_str(), value_string.size())) {
                        success = true;
                    }
                }
            }
        }
    }
    return success;
}
Ejemplo n.º 11
0
bool M2MLWClient::create_static_resource_instance_int(const char *name,
                                                      int32_t value,
                                                      bool multiple_instance,
                                                      uint16_t object_instance,
                                                      uint16_t resource_instance)
{
    bool success = false;
    String name_string;
    String value_string;

    if(name) {
        name_string += name;
    }

    char value_buffer[20];
    sprintf(value_buffer,"%ld",value);
    value_string += value_buffer;

    if(_object) {
        M2MObjectInstance *inst = _object->object_instance(object_instance);
        if(inst) {
            if(inst->create_static_resource_instance(name,"resource",
                                                    M2MResourceInstance::INTEGER,
                                                    (const uint8_t*)value_string.c_str(),
                                                    value_string.size(),
                                                    resource_instance) != NULL) {
                success = true;
            }
        }
    }
    return success;
}
Ejemplo n.º 12
0
    /*
     * When you press the button, we read the current value of the click counter
     * from mbed Device Connector, then up the value with one.
     */
    void handle_state_change(DoorIndicator::WarnStatus warnStatus) {
        M2MObjectInstance* inst = btn_object->object_instance();
        M2MResource* res = inst->resource("5501");

        printf("handle_state_change, new value of state is %d\r\n", (int) warnStatus);

        // serialize the value of counter as a string, and tell connector
        stringstream ss;
        ss << (int) warnStatus;
        std::string stringified = ss.str();
        res->set_value((uint8_t*)stringified.c_str(), stringified.length());
    }
Ejemplo n.º 13
0
void M2MLWClient::set_fw_execute_function()
{
    if(_firmware) {
        M2MObjectInstance *inst = _firmware->object_instance(0);
        if(inst) {
            M2MResource *res = inst->resource("2");
            if (res) {                
                res->set_execute_function(execute_callback(
                                              this,
                                              &M2MLWClient::fw_execute_function));
            }
        }
    }
}
Ejemplo n.º 14
0
    void update_resource() {
        if(_object) {
            M2MObjectInstance* inst = _object->object_instance();
            if(inst) {
                    M2MResource* res = inst->resource("D");

                    char buffer[20];
                    int size = sprintf(buffer,"%d",_value);
                    res->set_value((const uint8_t*)buffer,
                                   (const uint32_t)size);
                    _value++;
                }
        }
    }
Ejemplo n.º 15
0
M2MObjectInstance* M2MObject::create_object_instance(uint16_t instance_id)
{
    tr_debug("M2MObject::create_object_instance - id: %d", instance_id);
    M2MObjectInstance *instance = NULL;
    if(!object_instance(instance_id)) {
        instance = new M2MObjectInstance(this->name(),*this);
        if(instance) {
            instance->add_observation_level(observation_level());
            instance->set_instance_id(instance_id);
            if(M2MBase::name_id() != -1) {
                instance->set_coap_content_type(COAP_CONTENT_OMA_TLV_TYPE);
            }
            _instance_list.push_back(instance);
        }
    }
    return instance;
}
Ejemplo n.º 16
0
    void blink(void *argument) {
        // read the value of 'Pattern'
        status_ticker.detach();
        green_led = LED_OFF;

        M2MObjectInstance* inst = led_object->object_instance();
        M2MResource* res = inst->resource("5853");
        // Clear previous blink data
        blink_args->clear();

        // values in mbed Client are all buffers, and we need a vector of int's
        uint8_t* buffIn = NULL;
        uint32_t sizeIn;
        res->get_value(buffIn, sizeIn);

        // turn the buffer into a string, and initialize a vector<int> on the heap
        std::string s((char*)buffIn, sizeIn);
        free(buffIn);
        printf("led_execute_callback pattern=%s\n", s.c_str());

        // our pattern is something like 500:200:500, so parse that
        std::size_t found = s.find_first_of(":");
        while (found!=std::string::npos) {
            blink_args->blink_pattern.push_back(atoi((const char*)s.substr(0,found).c_str()));
            s = s.substr(found+1);
            found=s.find_first_of(":");
            if(found == std::string::npos) {
                blink_args->blink_pattern.push_back(atoi((const char*)s.c_str()));
            }
        }
        // check if POST contains payload
        if (argument) {
            M2MResource::M2MExecuteParameter* param = (M2MResource::M2MExecuteParameter*)argument;
            String object_name = param->get_argument_object_name();
            uint16_t object_instance_id = param->get_argument_object_instance_id();
            String resource_name = param->get_argument_resource_name();
            int payload_length = param->get_argument_value_length();
            uint8_t* payload = param->get_argument_value();
            printf("Resource: %s/%d/%s executed\n", object_name.c_str(), object_instance_id, resource_name.c_str());
            printf("Payload: %.*s\n", payload_length, payload);
        }
        // do_blink is called with the vector, and starting at -1
        blinky_thread.start(callback(this, &LedResource::do_blink));
    }
Ejemplo n.º 17
0
 void do_blink() {
     for (;;) {
         // blink the LED
         red_led = !red_led;
         // up the position, if we reached the end of the vector
         if (blink_args->position >= blink_args->blink_pattern.size()) {
             // send delayed response after blink is done
             M2MObjectInstance* inst = led_object->object_instance();
             M2MResource* led_res = inst->resource("5850");
             led_res->send_delayed_post_response();
             red_led = LED_OFF;
             status_ticker.attach_us(blinky, 250000);
             return;
         }
         // Wait requested time, then continue prosessing the blink pattern from next position.
         Thread::wait(blink_args->blink_pattern.at(blink_args->position));
         blink_args->position++;
     }
 }
Ejemplo n.º 18
0
 void update_resource() {
     if(_object) {
         M2MObjectInstance* inst = _object->object_instance();
         if(inst) {
             M2MResource* res = inst->resource("1");
             res = inst->resource("1");
             if(res) {
                 M2MResourceInstance *res_inst = res->resource_instance(0);
                 if(res_inst) {
                     char buffer1[20];
                     int size1 = sprintf(buffer1,"%d",_value);
                     res_inst->set_value((const uint8_t*)buffer1,
                                         (const uint32_t)size1);
                     _value++;
                 }
             }
         }
     }
 }
Ejemplo n.º 19
0
    /*
     * When you press the button, we read the current value of the click counter
     * from mbed Device Connector, then up the value with one.
     */
    void handle_button_click() {
        if (mbed_client.register_successful()) {
            M2MObjectInstance* inst = btn_object->object_instance();
            M2MResource* res = inst->resource("5501");

            // up counter
            counter++;
    #ifdef TARGET_K64F
            printf("handle_button_click, new value of counter is %d\n", counter);
    #else
            printf("simulate button_click, new value of counter is %d\n", counter);
    #endif
            // serialize the value of counter as a string, and tell connector
            char buffer[20];
            int size = sprintf(buffer,"%d",counter);
            res->set_value((uint8_t*)buffer, size);
        } else {
            printf("simulate button_click, device not registered\n");
        }
    }
Ejemplo n.º 20
0
// sample the accelerometer every second
static void sample(void) {
	char buffer[24];
	accel.getAxis(acc_raw);
	int len = 0;
	
	M2MObjectInstance* ins = obj->object_instance();

	// X
	len = snprintf(buffer,sizeof(buffer), "%d",acc_raw.x);
	printf("x: %s\r\n",buffer);
	if (! connected) return;
	M2MResource* resx = ins->resource("x");
	resx->set_value((uint8_t*)buffer,len);

	// Y
	len = snprintf(buffer,sizeof(buffer), "%d",acc_raw.y);
	printf("y: %s\r\n",buffer);
	if (! connected) return;
	M2MResource* resy = ins->resource("y");
	resy->set_value((uint8_t*)buffer,len);

}
void Test_M2MInterfaceImpl::test_register_object()
{
    M2MSecurity *sec = new M2MSecurity(M2MSecurity::M2MServer);
    m2msecurity_stub::int_value = 2;

    M2MObject *object = new M2MObject("test");
    M2MObjectInstance *ins = object->create_object_instance();
    ins->create_dynamic_resource("test","type",M2MResourceInstance::STRING,false,false);

    M2MObjectList list;
    list.push_back(object);

    String *val = new String("coaps://[2001:12:12:23::23:FF]:5685");
    m2msecurity_stub::string_value = val;
    m2mnsdlinterface_stub::bool_value = true;
    m2mconnectionhandler_stub::bool_value = true;
    impl->register_object(sec,list);
    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_REGISTER);

    impl->_register_ongoing = false;
    impl->_current_state = M2MInterfaceImpl::STATE_IDLE;
    impl->register_object(sec,list);
    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_REGISTER);

    delete val;
    val = new String("coaps://[10.45.3.83:5685");
    impl->_register_ongoing = false;
    impl->_current_state = M2MInterfaceImpl::STATE_IDLE;
    m2msecurity_stub::string_value = val;
    m2mnsdlinterface_stub::bool_value = true;
    m2mconnectionhandler_stub::bool_value = true;

    delete impl->_security;
    impl->_security = NULL;
    sec = new M2MSecurity(M2MSecurity::M2MServer);
    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_IDLE);
    CHECK(observer->error_occured == true);
    observer->error_occured = false;
    delete val;
    val = new String("coaps://10.45.3.83]:5685");

    impl->_register_ongoing = false;
    impl->_current_state = M2MInterfaceImpl::STATE_IDLE;

    m2msecurity_stub::string_value = val;
    m2mnsdlinterface_stub::bool_value = true;
    m2mconnectionhandler_stub::bool_value = true;

    delete impl->_security;
    impl->_security = NULL;
    sec = new M2MSecurity(M2MSecurity::M2MServer);
    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_IDLE);
    CHECK(observer->error_occured == true);

    observer->error_occured = false;
    delete val;
    val = new String("coaps://10.45.3.83:5685");
    delete impl->_security;
    impl->_security = NULL;
    sec = new M2MSecurity(M2MSecurity::M2MServer);

    impl->_register_ongoing = false;
    impl->_current_state = M2MInterfaceImpl::STATE_IDLE;

    m2msecurity_stub::string_value = val;
    m2mnsdlinterface_stub::bool_value = true;
    m2mconnectionhandler_stub::bool_value = true;

    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_REGISTER);

    observer->error_occured = false;
    delete val;
    val = new String("coap://10.45.3.83:5685");
    delete impl->_security;
    impl->_security = NULL;
    sec = new M2MSecurity(M2MSecurity::M2MServer);

    impl->_register_ongoing = false;
    impl->_current_state = M2MInterfaceImpl::STATE_IDLE;

    m2msecurity_stub::string_value = val;
    m2mnsdlinterface_stub::bool_value = true;
    m2mconnectionhandler_stub::bool_value = true;

    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_REGISTER);

    delete val;
    val = new String("coap://10.45.3.83:5685");
    impl->register_object(sec,list);
    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_REGISTER);


    impl->_current_state =  M2MInterfaceImpl::STATE_IDLE;
    m2mconnectionhandler_stub::bool_value = true;
    m2mnsdlinterface_stub::bool_value = false;

    delete impl->_security;
    impl->_security = NULL;
    sec = new M2MSecurity(M2MSecurity::M2MServer);
    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_IDLE);
    CHECK(observer->error_occured == true);


    impl->_current_state =  M2MInterfaceImpl::STATE_IDLE;
    m2mconnectionhandler_stub::bool_value = false;
    m2mnsdlinterface_stub::bool_value = true;

    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_IDLE);
    CHECK(observer->error_occured == true);


    impl->_register_ongoing = false;
    impl->_current_state =  M2MInterfaceImpl::STATE_BOOTSTRAP;
    m2mconnectionhandler_stub::bool_value = true;
    m2mnsdlinterface_stub::bool_value = true;

    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_BOOTSTRAP);
    CHECK(observer->error_occured == true);

    impl->_current_state =  M2MInterfaceImpl::STATE_IDLE;
    m2mconnectionhandler_stub::bool_value = true;
    m2mnsdlinterface_stub::bool_value = true;

    impl->register_object(sec,list);

    CHECK(impl->_current_state == M2MInterfaceImpl::STATE_IDLE);
    CHECK(observer->error_occured == true);

    impl->_current_state =  M2MInterfaceImpl::STATE_BOOTSTRAP;
    m2mconnectionhandler_stub::bool_value = true;
    m2mnsdlinterface_stub::bool_value = true;

    impl->register_object(sec,list);

    CHECK(observer->error_occured == true);


    delete val;
    val = NULL;

    list.clear();
    delete object;
    delete sec;
}