Esempio n. 1
0
void ObjectItem::do_instantiate(const string& name)
{
    auto_release_ptr<ObjectInstance> object_instance(
        ObjectInstanceFactory::create(
            name.c_str(),
            ParamArray(),
            m_entity->get_name(),
            Transformd::identity(),
            StringDictionary()));

    m_parent_item->add_item(object_instance.get());
    m_parent.object_instances().insert(object_instance);

    m_parent.bump_version_id();
    m_project_builder.notify_project_modification();
}
void ObjectCollectionItem::insert_objects(const string& path) const
{
    const string base_object_name =
        bf::path(path).replace_extension().filename().string();

    ParamArray params;
    params.insert("filename", path);

    SearchPaths search_paths;
    MeshObjectArray mesh_objects;

    if (!MeshObjectReader().read(
            search_paths,
            base_object_name.c_str(),
            params,
            mesh_objects))
        return;

    for (size_t i = 0; i < mesh_objects.size(); ++i)
    {
        MeshObject* object = mesh_objects[i];

        m_parent_item->add_item(object);

        m_parent.objects().insert(auto_release_ptr<Object>(object));

        const string object_instance_name = string(object->get_name()) + "_inst";

        auto_release_ptr<ObjectInstance> object_instance(
            ObjectInstanceFactory::create(
                object_instance_name.c_str(),
                ParamArray(),
                object->get_name(),
                Transformd::identity(),
                StringDictionary()));

        m_parent_item->add_item(object_instance.get());

        m_parent.object_instances().insert(object_instance);
    }

    if (!mesh_objects.empty())
    {
        m_parent.bump_version_id();
        m_editor_context.m_project_builder.slot_notify_project_modification();
    }
}
Esempio n. 3
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;
}
Esempio n. 4
0
M2MServer::M2MServer()
: M2MObject(M2M_SERVER_ID)
{
    M2MObject::create_object_instance();

    _server_instance = object_instance();

    if(_server_instance) {

        M2MResource* res = _server_instance->create_dynamic_resource(SERVER_SHORT_SERVER_ID,
                                                                     OMA_RESOURCE_TYPE,
                                                                     M2MResourceInstance::INTEGER,
                                                                     true);
        if(res) {
            res->set_operation(M2MBase::GET_PUT_ALLOWED);
        }
        res = _server_instance->create_dynamic_resource(SERVER_LIFETIME,
                                                        OMA_RESOURCE_TYPE,
                                                        M2MResourceInstance::INTEGER,
                                                        true);
        if(res) {
            res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
        }
        res = _server_instance->create_dynamic_resource(SERVER_NOTIFICATION_STORAGE,
                                                        OMA_RESOURCE_TYPE,
                                                        M2MResourceInstance::BOOLEAN,
                                                        true);
        if(res) {
            res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
        }
        res = _server_instance->create_dynamic_resource(SERVER_BINDING,
                                                        OMA_RESOURCE_TYPE,
                                                        M2MResourceInstance::STRING,
                                                        true);
        if(res) {
            res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);
        }
        res = _server_instance->create_dynamic_resource(SERVER_REGISTRATION_UPDATE,
                                                        OMA_RESOURCE_TYPE,
                                                        M2MResourceInstance::OPAQUE,
                                                        false);
        if(res) {
          res->set_operation(M2MBase::POST_ALLOWED);
        }
    }
}
Esempio n. 5
0
sn_coap_hdr_s* M2MObject::handle_post_request(nsdl_s *nsdl,
                                              sn_coap_hdr_s *received_coap_header,
                                              M2MObservationHandler *observation_handler,
                                              bool &execute_value_updated)
{
    tr_debug("M2MObject::handle_post_request()");    
    sn_coap_msg_code_e msg_code = COAP_MSG_CODE_RESPONSE_CHANGED; // 2.04
    // process the POST if we have registered a callback for it    
    sn_coap_hdr_s *coap_response = sn_nsdl_build_response(nsdl,
                                      received_coap_header,
                                      msg_code);

    if(received_coap_header) {
        if ((operation() & SN_GRS_POST_ALLOWED) != 0) {
            if(received_coap_header->payload_ptr) {
                tr_debug("M2MObject::handle_post_request() - Update Object with new values");
                uint16_t coap_content_type = 0;
                bool content_type_present = false;
                if(received_coap_header->content_type_ptr) {
                    content_type_present = true;
                    if(coap_response) {
                        coap_response->content_type_ptr = (uint8_t*)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(received_coap_header->content_type_ptr)
                if(!content_type_present &&
                   M2MBase::coap_content_type() == COAP_CONTENT_OMA_TLV_TYPE) {
                    coap_content_type = COAP_CONTENT_OMA_TLV_TYPE;
                }

                tr_debug("M2MObject::handle_post_request() - Request Content-Type %d", coap_content_type);

                if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type) {
                    uint16_t instance_id = 0;
                    // Check next free instance id
                    for(instance_id = 0; instance_id <= _max_instance_count; instance_id++) {
                        if(NULL == object_instance(instance_id)) {
                            break;
                        }
                        if(instance_id == _max_instance_count) {
                            msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
                            break;
                        }
                    }
                    if(COAP_MSG_CODE_RESPONSE_CHANGED == msg_code) {
                        M2MTLVDeserializer *deserializer = new M2MTLVDeserializer();
                        bool is_obj_instance = false;
                        bool obj_instance_exists = false;
                        if (deserializer) {
                            is_obj_instance = deserializer->is_object_instance(received_coap_header->payload_ptr);
                            if (is_obj_instance) {
                                instance_id = deserializer->instance_id(received_coap_header->payload_ptr);
                                tr_debug("M2MObject::handle_post_request() - instance id in TLV: %d", instance_id);
                                // Check if instance id already exists
                                if (object_instance(instance_id)){
                                    obj_instance_exists = true;
                                }
                            }
                        }
                        if (!obj_instance_exists) {
                            M2MObjectInstance *obj_instance = create_object_instance(instance_id);
                            if(obj_instance) {
                                obj_instance->set_operation(M2MBase::GET_PUT_ALLOWED);
                            }

                            if(deserializer) {
                                String obj_name = "";
                                M2MTLVDeserializer::Error error = M2MTLVDeserializer::None;
                                if(is_obj_instance) {
                                    tr_debug("M2MObject::handle_post_request() - TLV data contains ObjectInstance");
                                    error = deserializer->deserialise_object_instances(received_coap_header->payload_ptr,
                                                                               received_coap_header->payload_len,
                                                                               *this,
                                                                               M2MTLVDeserializer::Post);
                                } else if(deserializer->is_resource(received_coap_header->payload_ptr) ||
                                          deserializer->is_multiple_resource(received_coap_header->payload_ptr)) {
                                    tr_debug("M2MObject::handle_post_request() - TLV data contains Resources");
                                    error = deserializer->deserialize_resources(received_coap_header->payload_ptr,
                                                                                received_coap_header->payload_len,
                                                                                *obj_instance,
                                                                                M2MTLVDeserializer::Post);
                                } else {
                                    error = M2MTLVDeserializer::NotValid;
                                }
                                switch(error) {
                                    case M2MTLVDeserializer::None:
                                        if(observation_handler) {
                                            execute_value_updated = true;
                                        }
                                        coap_response->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
                                        if (coap_response->options_list_ptr) {
                                            memset(coap_response->options_list_ptr, 0, sizeof(sn_coap_options_list_s));

                                            obj_name = M2MBase::name();
                                            obj_name.push_back('/');
                                            obj_name.append_int(instance_id);

                                            coap_response->options_list_ptr->location_path_len = obj_name.length();
                                            if (coap_response->options_list_ptr->location_path_len != 0) {
                                                coap_response->options_list_ptr->location_path_ptr =
                                                        (uint8_t*)malloc(coap_response->options_list_ptr->location_path_len);
                                                if (coap_response->options_list_ptr->location_path_ptr) {
                                                    memcpy(coap_response->options_list_ptr->location_path_ptr,
                                                           obj_name.c_str(),
                                                           coap_response->options_list_ptr->location_path_len);
                                                }
                                            }
                                        }
                                        msg_code = COAP_MSG_CODE_RESPONSE_CREATED;
                                        break;
                                    case M2MTLVDeserializer::NotAllowed:
                                        msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED;
                                        break;
                                    case M2MTLVDeserializer::NotValid:
                                        msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
                                        break;
                                    case M2MTLVDeserializer::NotFound:
                                        msg_code = COAP_MSG_CODE_RESPONSE_NOT_FOUND;
                                        break;
                                }                                
                            }
                        } else {
                            tr_debug("M2MObject::handle_post_request() - COAP_MSG_CODE_RESPONSE_BAD_REQUEST");
                            msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
                        }
                        delete deserializer;
                    }
                } else {
                    msg_code =COAP_MSG_CODE_RESPONSE_UNSUPPORTED_CONTENT_FORMAT;
                } // if(COAP_CONTENT_OMA_TLV_TYPE == coap_content_type)
            } else {
                tr_error("M2MObject::handle_post_request - COAP_MSG_CODE_RESPONSE_BAD_REQUEST - Missing Payload");
                msg_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST; //
            }
        } else { // if ((object->operation() & SN_GRS_POST_ALLOWED) != 0)
            tr_error("M2MObject::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
            msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.05
        }
    } else { //if(received_coap_header)
        tr_error("M2MObject::handle_post_request - COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED");
        msg_code = COAP_MSG_CODE_RESPONSE_METHOD_NOT_ALLOWED; // 4.05
    }

    if(coap_response) {
        coap_response->msg_code = msg_code;
    }
    return coap_response;
}