Пример #1
0
void M2MResource::notification_update()
{
    tr_debug("M2MResource::notification_update()");
    M2MReportHandler *report_handler = M2MBase::report_handler();
    if(report_handler && is_observable()) {
        report_handler->set_notification_trigger();
    }
}
Пример #2
0
void pp_variable_assignment(FILE *outfile,
                            const_variable_assignment assignment,
                            const_variable_list variables,
                            const_values_set_list domains)
{
    const_variable var = variables->arr[assignment->var];

    if (is_health(var)) {
        fprintf(outfile, "health ");
    }
    if (is_observable(var)) {
        fprintf(outfile, "observable ");
    }
    switch (var->tag) {
        case TAGbool_variable:
            assert(TAGbool_variable_assignment == assignment->tag);
            fprintf(outfile, "bool ");
            pp_variable_assignment_short(outfile,
                                         assignment,
                                         variables,
                                         domains);
            fprintf(outfile, "\n");
            pp_variable_attribute_value_list(outfile, var, var->attributes, domains, to_bool_variable_assignment(assignment)->value ? 1 : 0);
            break;
        case TAGint_variable:
            assert(TAGint_variable_assignment == assignment->tag);
            fprintf(outfile, "int ");
            pp_variable_assignment_short(outfile, assignment, variables, domains);
            fprintf(outfile, "\n");
/* TODO: Print the attributes. */
            break;
        case TAGfloat_variable:
            assert(TAGfloat_variable_assignment == assignment->tag);
            fprintf(outfile, "float ");
            pp_variable_assignment_short(outfile, assignment, variables, domains);
            fprintf(outfile, "\n");
/* TODO: Print the attributes. */
            break;
        case TAGenum_variable:
            assert(TAGenum_variable_assignment == assignment->tag);
            assert(NULL != domains);
            fprintf(outfile, "enum ");
            pp_variable_assignment_short(outfile, assignment, variables, domains);
            fprintf(outfile, "\n");
            pp_variable_attribute_value_list(outfile, var, var->attributes, domains, to_enum_variable_assignment(assignment)->value);
            break;
        default:
            assert(0);
    }
}
Пример #3
0
sn_coap_hdr_s* M2MResource::handle_get_request(nsdl_s *nsdl,
                                               sn_coap_hdr_s *received_coap_header,
                                               M2MObservationHandler *observation_handler)
{
    tr_debug("M2MResource::handle_get_request()");
    sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
    sn_coap_hdr_s * coap_response = NULL;
    if(_has_multiple_instances) {
        coap_response = sn_nsdl_build_response(nsdl,
                                               received_coap_header,
                                               msg_code);
        if(received_coap_header) {
            // process the GET if we have registered a callback for it
            if ((operation() & SN_GRS_GET_ALLOWED) != 0) {
                if(coap_response) {
                    uint16_t coap_content_type = 0;
                    bool content_type_present = false;
                    if(received_coap_header->content_type_ptr){
                        content_type_present = true;
                        coap_response->content_type_ptr = alloc_copy(received_coap_header->content_type_ptr,
                                                                     received_coap_header->content_type_len);
                        if(coap_response->content_type_ptr) {
                            coap_response->content_type_len = received_coap_header->content_type_len;
                            for(uint8_t i = 0; i < coap_response->content_type_len; i++) {
                                coap_content_type = (coap_content_type << 8) +
                                        (coap_response->content_type_ptr[i] & 0xFF);
                            }
                        }
                    }

                    if(!content_type_present &&
                       M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE) {
                        coap_content_type = COAP_CONTENT_OMA_TLV_TYPE;
                    }

                    tr_debug("M2MResource::handle_get_request() - Request Content-Type %d", coap_content_type);
                    if (!coap_response->content_type_ptr) {
                        coap_response->content_type_ptr =
                                m2m::String::convert_integer_to_array(coap_content_type,
                                    coap_response->content_type_len);
                        if (coap_response->content_type_ptr) {
                            set_coap_content_type(coap_content_type);
                        }
                    }
                    uint8_t *data = NULL;
                    uint32_t data_length = 0;
                    // fill in the CoAP response payload
                    if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) {
                        M2MTLVSerializer *serializer = new M2MTLVSerializer();
                        if(serializer) {
                            data = serializer->serialize(this, data_length);
                            delete serializer;
                        }
                    } else {
                        msg_code = COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT; // Content format not supported
                    }

                    coap_response->payload_len = data_length;
                    coap_response->payload_ptr = data;

                    coap_response->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
                    memset(coap_response->options_list_ptr, 0, sizeof(sn_coap_options_list_s));

                    coap_response->options_list_ptr->max_age_ptr =
                            m2m::String::convert_integer_to_array(max_age(),
                                coap_response->options_list_ptr->max_age_len);

                    if(received_coap_header->token_ptr) {
                        tr_debug("M2MResource::handle_get_request - Sets Observation Token to resource");
                        set_observation_token(received_coap_header->token_ptr,
                                              received_coap_header->token_len);
                    }

                    if(received_coap_header->options_list_ptr) {
                        if(received_coap_header->options_list_ptr->observe) {
                            if (is_observable()) {
                                uint32_t number = 0;
                                uint8_t observe_option = 0;
                                if(received_coap_header->options_list_ptr->observe_ptr) {
                                    observe_option = *received_coap_header->options_list_ptr->observe_ptr;
                                }
                                if(START_OBSERVATION == observe_option) {
                                    tr_debug("M2MResource::handle_get_request - Starts Observation");
                                    // If the observe length is 0 means register for observation.
                                    if(received_coap_header->options_list_ptr->observe_len != 0) {
                                        for(int i=0;i < received_coap_header->options_list_ptr->observe_len; i++) {
                                            number = (*(received_coap_header->options_list_ptr->observe_ptr + i) & 0xff) <<
                                                     8*(received_coap_header->options_list_ptr->observe_len- 1 - i);
                                            }
                                    }
                                    // If the observe value is 0 means register for observation.
                                    if(number == 0) {
                                        tr_debug("M2MResource::handle_get_request - Put Resource under Observation");
                                        M2MResourceInstanceList::const_iterator it;
                                        it = _resource_instance_list.begin();
                                        for (; it!=_resource_instance_list.end(); it++ ) {
                                            tr_debug("M2MResource::handle_get_request - set_resource_observer");
                                            (*it)->set_resource_observer(this);
                                            (*it)->add_observation_level(M2MBase::R_Attribute);
                                        }
                                        set_under_observation(true,observation_handler);
                                        M2MBase::add_observation_level(M2MBase::R_Attribute);
                                        coap_response->options_list_ptr->observe_ptr =
                                                m2m::String::convert_integer_to_array(observation_number(),
                                                      coap_response->options_list_ptr->observe_len);
                                    }
                                } else if (STOP_OBSERVATION == observe_option) {
                                    tr_debug("M2MResource::handle_get_request - Stops Observation");
                                    set_under_observation(false,NULL);
                                    M2MBase::remove_observation_level(M2MBase::R_Attribute);
                                    M2MResourceInstanceList::const_iterator it;
                                    it = _resource_instance_list.begin();
                                    for (; it!=_resource_instance_list.end(); it++ ) {
                                        (*it)->set_resource_observer(NULL);
                                    }
                                }
                                msg_code = COAP_MSG_CODE_RESPONSE_CONTENT;
                            }
                            else {
                                msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
                            }
                        }
                    }
                }
            } else {
                tr_error("M2MResource::handle_get_request - Return COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
                // Operation is not allowed.
                msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
            }
        }
        if(coap_response) {
            coap_response->msg_code = msg_code;
        }
    } else {
        coap_response = M2MResourceInstance::handle_get_request(nsdl,
                            received_coap_header,
                            observation_handler);
    }
    return coap_response;
}