示例#1
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;
             }
         }
     }
 }
示例#2
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;
}
示例#3
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++;
                 }
             }
         }
     }
 }
    /*
     * 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());
    }
示例#5
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);

}
示例#6
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++;
                }
        }
    }
示例#7
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));
            }
        }
    }
}
    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));
    }
 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++;
     }
 }
    /*
     * 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");
        }
    }