Esempio n. 1
0
/** @deprecated Use kaa_extension_profile_init(). */
kaa_error_t kaa_profile_manager_create(kaa_profile_manager_t **profile_manager_p, kaa_status_t *status
        , kaa_channel_manager_t *channel_manager, kaa_logger_t *logger)
{
    if (!profile_manager_p || !channel_manager || !status) {
        return KAA_ERR_BADPARAM;
    }

    kaa_profile_manager_t *profile_manager = KAA_MALLOC(sizeof(kaa_profile_manager_t));
    if (!profile_manager) {
        return KAA_ERR_NOMEM;
    }

    /**
     * KAA_CALLOC is really needed.
     */
    profile_manager->extension_data = KAA_CALLOC(1, sizeof(kaa_profile_extension_data_t));
    if (!profile_manager->extension_data) {
        KAA_FREE(profile_manager);
        return KAA_ERR_NOMEM;
    }

    profile_manager->need_resync = true;

    profile_manager->profile_body.size = 0;
    profile_manager->profile_body.buffer = NULL;
    profile_manager->channel_manager = channel_manager;
    profile_manager->status = status;
    profile_manager->logger = logger;

    ext_calculate_sha_hash(NULL, 0, profile_manager->profile_hash);
    ext_copy_sha_hash(profile_manager->status->profile_hash, profile_manager->profile_hash);

    *profile_manager_p = profile_manager;
    return KAA_ERR_NONE;
}
Esempio n. 2
0
kaa_error_t kaa_profile_manager_update_profile(kaa_profile_manager_t *self, kaa_profile_t *profile_body)
{
#if KAA_PROFILE_SCHEMA_VERSION > 0
    KAA_RETURN_IF_NIL2(self, profile_body, KAA_ERR_BADPARAM);

    size_t serialized_profile_size = profile_body->get_size(profile_body);
    if (!serialized_profile_size) {
        KAA_LOG_ERROR(self->logger, KAA_ERR_BADDATA,
                      "Failed to update profile: serialize profile size is null. Maybe profile schema is empty");
        return KAA_ERR_BADDATA;
    }

    char *serialized_profile = (char *) KAA_MALLOC(serialized_profile_size * sizeof(char));
    KAA_RETURN_IF_NIL(serialized_profile, KAA_ERR_NOMEM);

    avro_writer_t writer = avro_writer_memory(serialized_profile, serialized_profile_size);
    if (!writer) {
        KAA_FREE(serialized_profile);
        return KAA_ERR_NOMEM;
    }
    profile_body->serialize(writer, profile_body);
    avro_writer_free(writer);

    kaa_digest new_hash;
    ext_calculate_sha_hash(serialized_profile, serialized_profile_size, new_hash);

    if (!memcmp(new_hash, self->status->profile_hash, SHA_1_DIGEST_LENGTH)) {
        self->need_resync = false;
        KAA_FREE(serialized_profile);
        return KAA_ERR_NONE;
    }

    KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Endpoint profile is updated");

    if (ext_copy_sha_hash(self->status->profile_hash, new_hash)) {
        KAA_FREE(serialized_profile);
        return KAA_ERR_BAD_STATE;
    }

    if (self->profile_body.size > 0) {
        KAA_FREE(self->profile_body.buffer);
        self->profile_body.buffer = NULL;
    }

    self->profile_body.buffer = (uint8_t*)serialized_profile;
    self->profile_body.size = serialized_profile_size;

    self->need_resync = true;

    kaa_transport_channel_interface_t *channel =
            kaa_channel_manager_get_transport_channel(self->channel_manager, profile_sync_services[0]);
    if (channel)
        channel->sync_handler(channel->context, profile_sync_services, 1);

#endif
    return KAA_ERR_NONE;
}
Esempio n. 3
0
void test_status_persistense(void)
{
    KAA_TRACE_IN(logger);

    kaa_status_t *status;
    kaa_error_t err_code = kaa_status_create(&status);

    ASSERT_NULL(status->endpoint_access_token);
    ASSERT_EQUAL(status->event_seq_n, 0);
    ASSERT_FALSE(status->is_attached);
    ASSERT_FALSE(status->is_registered);
    ASSERT_FALSE(status->is_updated);

    ASSERT_EQUAL(kaa_status_set_endpoint_access_token(status, "my_token"), KAA_ERR_NONE);
    ASSERT_EQUAL(ext_copy_sha_hash(status->endpoint_public_key_hash, test_ep_key_hash), KAA_ERR_NONE);
    ASSERT_EQUAL(ext_copy_sha_hash(status->profile_hash, test_profile_hash), KAA_ERR_NONE);
    status->is_attached = true;
    status->is_registered = true;
    status->is_updated = true;
    status->event_seq_n = 10;

    err_code = kaa_status_save(status);
    ASSERT_EQUAL(err_code, KAA_ERR_NONE);

    kaa_status_destroy(status);
    status = NULL;


    err_code = kaa_status_create(&status);

    ASSERT_NOT_NULL(status->endpoint_access_token);
    ASSERT_EQUAL(strcmp("my_token", status->endpoint_access_token), 0);

    ASSERT_EQUAL(status->event_seq_n, 10);
    ASSERT_TRUE(status->is_attached);
    ASSERT_TRUE(status->is_registered);
    ASSERT_TRUE(status->is_updated);

    ASSERT_EQUAL(memcmp(test_ep_key_hash, status->endpoint_public_key_hash, SHA_1_DIGEST_LENGTH), 0);
    ASSERT_EQUAL(memcmp(test_profile_hash, status->profile_hash, SHA_1_DIGEST_LENGTH), 0);

    kaa_status_destroy(status);
}
Esempio n. 4
0
kaa_error_t kaa_init(kaa_context_t **kaa_context_p)
{
    KAA_RETURN_IF_NIL(kaa_context_p, KAA_ERR_BADPARAM);

    // Initialize logger
    kaa_logger_t *logger = NULL;

    FILE *logfile = fopen("run.log", "w");

    kaa_error_t error = kaa_log_create(&logger, KAA_MAX_LOG_MESSAGE_LENGTH, KAA_MAX_LOG_LEVEL, logfile); // TODO: make log destination configurable
    if (error)
        return error;

    KAA_LOG_INFO(logger, KAA_ERR_NONE, "Kaa SDK version %s, commit hash %s", BUILD_VERSION, BUILD_COMMIT_HASH);

    // Initialize general Kaa context
    error = kaa_context_create(kaa_context_p, logger);
    if (error) {
        KAA_LOG_FATAL(logger, error, "Failed to create Kaa context");
        kaa_log_destroy(logger);
        *kaa_context_p = NULL;
        return error;
    }

    // Initialize endpoint identity
    char *pub_key_buffer = NULL;
    size_t pub_key_buffer_size = 0;
    bool need_deallocation = false;

    ext_get_endpoint_public_key(&pub_key_buffer, &pub_key_buffer_size, &need_deallocation);
    kaa_digest pub_key_hash;
    error = ext_calculate_sha_hash(pub_key_buffer, pub_key_buffer_size, pub_key_hash);

    if (need_deallocation && pub_key_buffer_size > 0) {
        KAA_FREE(pub_key_buffer);
    }

    if (error) {
        KAA_LOG_FATAL(logger, error, "Failed to calculate EP ID");
        kaa_context_destroy(*kaa_context_p);
        *kaa_context_p = NULL;
        kaa_log_destroy(logger);
        return error;
    }

    error = ext_copy_sha_hash((*kaa_context_p)->status->status_instance->endpoint_public_key_hash, pub_key_hash);
    if (error) {
        KAA_LOG_FATAL(logger, error, "Failed to set Endpoint public key");
        kaa_context_destroy(*kaa_context_p);
        *kaa_context_p = NULL;
        kaa_log_destroy(logger);
        return error;
    }
    return KAA_ERR_NONE;
}
Esempio n. 5
0
File: kaa.c Progetto: kaaproject/kaa
kaa_error_t kaa_init(kaa_context_t **kaa_context_p)
{
    KAA_RETURN_IF_NIL(kaa_context_p, KAA_ERR_BADPARAM);

    // Initialize logger
    kaa_logger_t *logger = NULL;
    kaa_error_t error = kaa_log_create(&logger, KAA_MAX_LOG_MESSAGE_LENGTH, KAA_MAX_LOG_LEVEL, NULL); // TODO: make log destination configurable
    if (error) {
        return error;
    }

    KAA_LOG_INFO(logger, KAA_ERR_NONE, "Kaa SDK version %s, commit hash %s", KAA_BUILD_VERSION, KAA_BUILD_COMMIT_HASH);

    // Initialize general Kaa context
    error = kaa_context_create(kaa_context_p, logger);
    if (error) {
        KAA_LOG_FATAL(logger, error, "Failed to create Kaa context");
        *kaa_context_p = NULL;
        return error;
    }

    // Initialize endpoint identity
    uint8_t *sha1 = NULL;
    size_t sha1_size = 0;

    error = kaa_init_keys();
    if (error) {
        KAA_LOG_ERROR(logger, error, "Failed to initialize keys");
        return error;
    }

    ext_get_sha1_public(&sha1, &sha1_size);
    ext_copy_sha_hash((*kaa_context_p)->status->status_instance->endpoint_public_key_hash, sha1);

    return kaa_status_set_updated((*kaa_context_p)->status->status_instance, true);
}
Esempio n. 6
0
kaa_error_t kaa_profile_manager_get_endpoint_id(kaa_profile_manager_t *self, kaa_endpoint_id_p result_id)
{
    KAA_RETURN_IF_NIL2(self, result_id, KAA_ERR_BADPARAM);
    return ext_copy_sha_hash((kaa_digest_p) result_id, self->status->endpoint_public_key_hash);
}
Esempio n. 7
0
void test_meta_extension_serialize(void)
{
    KAA_TRACE_IN(logger);

    size_t meta_extension_size;
    kaa_error_t error_code = kaa_meta_data_request_get_size(&meta_extension_size);
    char buffer[meta_extension_size];

    kaa_platform_message_writer_t *writer;
    error_code = kaa_platform_message_writer_create(&writer, buffer, meta_extension_size);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    uint32_t expected_timeout = KAA_SYNC_TIMEOUT;
    kaa_digest expected_public_key_hash = {0x74, 0xc7, 0x51, 0x43, 0x00, 0xf7, 0xb8, 0x21, 0x2c, 0xc3, 0x6b, 0xa5, 0x9c, 0xb4, 0x03, 0xef, 0xc2, 0x5c, 0x65, 0x6c};
    kaa_digest expected_profile_hash = {0xfa, 0x71, 0xb5, 0x02, 0xe7, 0xdf, 0x96, 0x86, 0x6c, 0xdc, 0xe1, 0x4a, 0x17, 0x35, 0x7f, 0xd9, 0xa8, 0xfb, 0x71, 0x09};

    error_code = ext_copy_sha_hash(status->endpoint_public_key_hash, expected_public_key_hash);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    error_code = ext_copy_sha_hash(status->profile_hash, expected_profile_hash);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    kaa_context_t *context = NULL;
    kaa_init(&context);
    kaa_platform_protocol_t *protocol = NULL;
    kaa_platform_protocol_create(&protocol, context, status);

    error_code = kaa_meta_data_request_serialize(protocol, writer, 1);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    kaa_deinit(context);

    kaa_platform_message_reader_t *reader;
    error_code = kaa_platform_message_reader_create(&reader, buffer, meta_extension_size);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    uint8_t extension_type;
    uint32_t extension_options;
    uint32_t extension_payload;

    error_code = kaa_platform_message_read_extension_header(
                    reader, &extension_type, &extension_options, &extension_payload);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    ASSERT_EQUAL(extension_type, KAA_META_DATA_EXTENSION_TYPE);
    ASSERT_EQUAL(extension_options, (TIMEOUT_VALUE | PUBLIC_KEY_HASH_VALUE | PROFILE_HASH_VALUE | APP_TOKEN_VALUE));
    ASSERT_EQUAL(extension_payload, meta_extension_size - KAA_EXTENSION_HEADER_SIZE);

    uint32_t request_id;
    uint32_t timeout;
    kaa_digest public_key_hash;
    kaa_digest profile_hash;
    char sdk_token[KAA_SDK_TOKEN_LENGTH];

    error_code = kaa_platform_message_read(reader, &request_id, sizeof(uint32_t));
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    ASSERT_EQUAL(KAA_NTOHL(request_id), 1);
    error_code = kaa_platform_message_read(reader, &timeout, sizeof(uint32_t));
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    timeout = KAA_NTOHL(timeout);
    ASSERT_EQUAL(expected_timeout, timeout);

    error_code = kaa_platform_message_read_aligned(reader, public_key_hash, SHA_1_DIGEST_LENGTH);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    error_code = (memcmp(public_key_hash, expected_public_key_hash, SHA_1_DIGEST_LENGTH) == 0 ? KAA_ERR_NONE : KAA_ERR_READ_FAILED);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    error_code = kaa_platform_message_read_aligned(reader, profile_hash, SHA_1_DIGEST_LENGTH);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    error_code = (memcmp(profile_hash, expected_profile_hash, SHA_1_DIGEST_LENGTH) == 0 ? KAA_ERR_NONE : KAA_ERR_READ_FAILED);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    error_code = kaa_platform_message_read_aligned(reader, sdk_token, KAA_SDK_TOKEN_LENGTH);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);
    error_code = (memcmp(sdk_token, KAA_SDK_TOKEN, KAA_SDK_TOKEN_LENGTH) == 0 ? KAA_ERR_NONE : KAA_ERR_READ_FAILED);
    ASSERT_EQUAL(error_code, KAA_ERR_NONE);

    kaa_platform_message_reader_destroy(reader);
    kaa_platform_message_writer_destroy(writer);
}