Example #1
0
kaa_error_t kaa_platform_protocol_serialize_client_sync(kaa_platform_protocol_t *self
        , const kaa_serialize_info_t *info
        , char **buffer
        , size_t *buffer_size)
{
    KAA_RETURN_IF_NIL4(self, info, buffer, buffer_size, KAA_ERR_BADPARAM);
    KAA_RETURN_IF_NIL3(info->allocator, info->services, info->services_count, KAA_ERR_BADDATA);

    KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Serializing client sync...");

    *buffer_size = 0;
    kaa_error_t error = kaa_client_sync_get_size(self, info->services, info->services_count, buffer_size);
    KAA_RETURN_IF_ERR(error)

    KAA_LOG_DEBUG(self->logger, KAA_ERR_NONE, "Going to request sync buffer (size %zu)", *buffer_size);

    *buffer = info->allocator(info->allocator_context, *buffer_size);
    if (*buffer) {
        self->request_id++;
        error = kaa_client_sync_serialize(self, info->services, info->services_count, *buffer, buffer_size);
    } else {
        error = KAA_ERR_WRITE_FAILED;
    }

    if (error) {
        self->request_id--;
    } else {
        KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Client sync successfully serialized");
    }

    return error;
}
Example #2
0
/*
 * Initializes Kaa SDK.
 */
kaa_error_t kaa_sdk_init()
{
    kaa_error_t error_code = kaa_init(&kaa_context_);
    if (error_code) {
        printf("Error during kaa context creation %d", error_code);
        return error_code;
    }

    error_code = kaa_log_collector_init();
    if (error_code) {
        KAA_LOG_ERROR(kaa_context_->logger, error_code, "Failed to init Kaa log collector %d", error_code);
        return error_code;
    }

    KAA_LOG_TRACE(kaa_context_->logger, KAA_ERR_NONE, "Adding transport channels");

    error_code = kaa_tcp_channel_create(&operations_channel
                                      , kaa_context_->logger
                                      , OPERATIONS_SERVICES
                                      , OPERATIONS_SERVICES_COUNT);
    KAA_RETURN_IF_ERR(error_code);

    error_code = kaa_tcp_channel_create(&bootstrap_channel
                                      , kaa_context_->logger
                                      , BOOTSTRAP_SERVICE
                                      , BOOTSTRAP_SERVICE_COUNT);
    KAA_RETURN_IF_ERR(error_code);

    error_code = kaa_channel_manager_add_transport_channel(kaa_context_->channel_manager
                                                         , &bootstrap_channel
                                                         , NULL);
    KAA_RETURN_IF_ERR(error_code);

    error_code = kaa_channel_manager_add_transport_channel(kaa_context_->channel_manager
                                                         , &operations_channel
                                                         , NULL);
    KAA_RETURN_IF_ERR(error_code);

    KAA_LOG_TRACE(kaa_context_->logger, KAA_ERR_NONE, "Kaa SDK started");
    return KAA_ERR_NONE;
}
Example #3
0
kaa_error_t kaa_meta_data_request_serialize(kaa_status_t *status, kaa_platform_message_writer_t* writer, uint32_t request_id)
{
    KAA_RETURN_IF_NIL2(status, writer, KAA_ERR_BADPARAM);

    uint32_t options = TIMEOUT_VALUE | PUBLIC_KEY_HASH_VALUE | PROFILE_HASH_VALUE | APP_TOKEN_VALUE;

    size_t payload_length = 0;
    kaa_error_t err_code = kaa_meta_data_request_get_size(&payload_length);
    KAA_RETURN_IF_ERR(err_code);
    payload_length -= KAA_EXTENSION_HEADER_SIZE;

    err_code = kaa_platform_message_write_extension_header(writer
               , KAA_META_DATA_EXTENSION_TYPE
               , options
               , payload_length);
    KAA_RETURN_IF_ERR(err_code);

    uint32_t request_id_network = KAA_HTONL(request_id);
    err_code = kaa_platform_message_write(writer, &request_id_network, sizeof(uint32_t));
    KAA_RETURN_IF_ERR(err_code);

    uint32_t timeout = KAA_HTONL(KAA_SYNC_TIMEOUT);
    err_code = kaa_platform_message_write(writer, &timeout, sizeof(timeout));
    KAA_RETURN_IF_ERR(err_code);

    err_code = kaa_platform_message_write_aligned(writer, status->endpoint_public_key_hash, SHA_1_DIGEST_LENGTH);
    KAA_RETURN_IF_ERR(err_code);

    err_code = kaa_platform_message_write_aligned(writer, status->profile_hash, SHA_1_DIGEST_LENGTH);
    KAA_RETURN_IF_ERR(err_code);

    err_code = kaa_platform_message_write_aligned(writer, KAA_SDK_TOKEN, KAA_SDK_TOKEN_LENGTH);

    return err_code;
}
Example #4
0
kaa_error_t kaa_profile_request_serialize(kaa_profile_manager_t *self, kaa_platform_message_writer_t *writer)
{
    KAA_RETURN_IF_NIL2(self, writer, KAA_ERR_BADPARAM);

    KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Going to compile profile client sync");

    kaa_error_t error_code = kaa_platform_message_write_extension_header(writer
                                                                       , KAA_EXTENSION_PROFILE
                                                                       , 0
                                                                       , self->extension_data->payload_size);
    KAA_RETURN_IF_ERR(error_code);

    uint32_t network_order_32 = KAA_HTONL(0);
#if KAA_PROFILE_SCHEMA_VERSION > 0
    if (resync_is_required(self)) {
        network_order_32 = KAA_HTONL(self->profile_body.size);
        error_code = kaa_platform_message_write(writer, &network_order_32, sizeof(uint32_t));
        KAA_RETURN_IF_ERR(error_code);
        if (self->profile_body.size) {
            KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Writing profile body (size %u)...", self->profile_body.size);
            error_code = kaa_platform_message_write_aligned(writer, self->profile_body.buffer, self->profile_body.size);
            if (error_code) {
                KAA_LOG_ERROR(self->logger, error_code, "Failed to write profile body");
                return error_code;
            }
        }
    } else {
        error_code = kaa_platform_message_write(writer, &network_order_32, sizeof(uint32_t));
        KAA_RETURN_IF_ERR(error_code);
    }
#else
    error_code = kaa_platform_message_write(writer, &network_order_32, sizeof(uint32_t));
    KAA_RETURN_IF_ERR(error_code);
#endif

    uint16_t network_order_16 = 0;
    uint16_t field_number_with_reserved = 0;

    if (!self->status->is_registered) {
        field_number_with_reserved = KAA_HTONS(PUB_KEY_VALUE << 8);
        error_code = kaa_platform_message_write(writer
                                             , &field_number_with_reserved
                                             , sizeof(uint16_t));
        KAA_RETURN_IF_ERR(error_code);

        network_order_16 = KAA_HTONS(self->extension_data->public_key.size);
        error_code = kaa_platform_message_write(writer, &network_order_16, sizeof(uint16_t));
        KAA_RETURN_IF_ERR(error_code);

        KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Writing public key (size %u)...", self->extension_data->public_key.size);
        error_code = kaa_platform_message_write_aligned(writer
                                                     , (char*)self->extension_data->public_key.buffer
                                                     , self->extension_data->public_key.size);
        if (error_code) {
            KAA_LOG_ERROR(self->logger, error_code, "Failed to write public key");
            return error_code;
        }
    }

    if (self->extension_data->access_token.buffer) {
        field_number_with_reserved = KAA_HTONS(ACCESS_TOKEN_VALUE << 8);
        error_code = kaa_platform_message_write(writer
                                             , &field_number_with_reserved
                                             , sizeof(uint16_t));
        KAA_RETURN_IF_ERR(error_code);

        network_order_16 = KAA_HTONS(self->extension_data->access_token.size);
        error_code = kaa_platform_message_write(writer, &network_order_16, sizeof(uint16_t));
        KAA_RETURN_IF_ERR(error_code);

        KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Writing access token (size %u)...", self->extension_data->access_token.size);
        error_code = kaa_platform_message_write_aligned(writer
                                                     , (char*)self->extension_data->access_token.buffer
                                                     , self->extension_data->access_token.size);
        if (error_code) {
            KAA_LOG_ERROR(self->logger, error_code, "Failed to write access token");
            return error_code;
        }
    }

    return error_code;
}
Example #5
0
kaa_error_t kaa_platform_protocol_process_server_sync(kaa_platform_protocol_t *self
        , const char *buffer
        , size_t buffer_size)
{
    KAA_RETURN_IF_NIL3(self, buffer, buffer_size, KAA_ERR_BADPARAM);

    KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Processing server sync...");

    kaa_platform_message_reader_t *reader = NULL;
    kaa_error_t error_code = kaa_platform_message_reader_create(&reader, buffer, buffer_size);
    KAA_RETURN_IF_ERR(error_code);

    uint32_t protocol_id = 0;
    uint16_t protocol_version = 0;
    uint16_t extension_count = 0;

    error_code = kaa_platform_message_header_read(reader, &protocol_id, &protocol_version, &extension_count);
    KAA_RETURN_IF_ERR(error_code);

    if (protocol_id != KAA_PLATFORM_PROTOCOL_ID) {
        KAA_LOG_ERROR(self->logger, KAA_ERR_BAD_PROTOCOL_ID, "Unsupported protocol ID %x", protocol_id);
        return KAA_ERR_BAD_PROTOCOL_ID;
    }
    if (protocol_version != KAA_PLATFORM_PROTOCOL_VERSION) {
        KAA_LOG_ERROR(self->logger, KAA_ERR_BAD_PROTOCOL_VERSION, "Unsupported protocol version %u", protocol_version);
        return KAA_ERR_BAD_PROTOCOL_VERSION;
    }

    uint32_t request_id = 0;
    uint8_t extension_type = 0;
    uint32_t extension_options = 0;
    uint32_t extension_length = 0;

    while (!error_code && kaa_platform_message_is_buffer_large_enough(reader, KAA_PROTOCOL_MESSAGE_HEADER_SIZE)) {

        error_code = kaa_platform_message_read_extension_header(reader
                     , &extension_type
                     , &extension_options
                     , &extension_length);
        KAA_RETURN_IF_ERR(error_code);

        switch (extension_type) {
        case KAA_BOOTSTRAP_EXTENSION_TYPE: {
            error_code = kaa_bootstrap_manager_handle_server_sync(self->kaa_context->bootstrap_manager
                         , reader
                         , extension_options
                         , extension_length);
            break;
        }
        case KAA_META_DATA_EXTENSION_TYPE: {
            error_code = kaa_platform_message_read(reader, &request_id, sizeof(uint32_t));
            request_id = KAA_NTOHL(request_id);
            break;
        }
        case KAA_PROFILE_EXTENSION_TYPE: {
            error_code = kaa_profile_handle_server_sync(self->kaa_context->profile_manager
                         , reader
                         , extension_options
                         , extension_length);
            break;
        }
        case KAA_USER_EXTENSION_TYPE: {
            error_code = kaa_user_handle_server_sync(self->kaa_context->user_manager
                         , reader
                         , extension_options
                         , extension_length);
            break;
        }
#ifndef KAA_DISABLE_FEATURE_LOGGING
        case KAA_LOGGING_EXTENSION_TYPE: {
            error_code = kaa_logging_handle_server_sync(self->kaa_context->log_collector
                         , reader
                         , extension_options
                         , extension_length);
            break;
        }
#endif
#ifndef KAA_DISABLE_FEATURE_EVENTS
        case KAA_EVENT_EXTENSION_TYPE: {
            error_code = kaa_event_handle_server_sync(self->kaa_context->event_manager
                         , reader
                         , extension_options
                         , extension_length
                         , request_id);
            break;
        }
#endif
#ifndef KAA_DISABLE_FEATURE_CONFIGURATION
        case KAA_CONFIGURATION_EXTENSION_TYPE: {
            error_code = kaa_configuration_manager_handle_server_sync(self->kaa_context->configuration_manager
                         , reader
                         , extension_options
                         , extension_length);
            break;
        }
#endif
#ifndef KAA_DISABLE_FEATURE_NOTIFICATION
        case KAA_NOTIFICATION_EXTENSION_TYPE: {
            error_code = kaa_notification_manager_handle_server_sync(self->kaa_context->notification_manager
                         , reader
                         , extension_length);
            break;
        }
#endif
        default:
            KAA_LOG_WARN(self->logger, KAA_ERR_UNSUPPORTED,
                         "Unsupported extension received (type = %u)", extension_type);
            break;
        }
    }

    kaa_platform_message_reader_destroy(reader);

    if (!error_code) {
        error_code = kaa_status_save(self->status);
        KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Server sync successfully processed");
    } else {
        KAA_LOG_ERROR(self->logger, error_code,
                      "Server sync is corrupted. Failed to read extension with type %u", extension_type);
    }

    return error_code;
}
Example #6
0
static kaa_error_t kaa_client_sync_serialize(kaa_platform_protocol_t *self
        , const kaa_service_t services[]
        , size_t services_count
        , char* buffer
        , size_t *size)
{
    kaa_platform_message_writer_t *writer = NULL;
    kaa_error_t error_code = kaa_platform_message_writer_create(&writer, buffer, *size);
    KAA_RETURN_IF_ERR(error_code);

    uint16_t total_services_count = services_count + 1 /* Meta extension */;

    error_code = kaa_platform_message_header_write(writer, KAA_PLATFORM_PROTOCOL_ID, KAA_PLATFORM_PROTOCOL_VERSION);
    if (error_code) {
        KAA_LOG_ERROR(self->logger, error_code, "Failed to write the client sync header");
        return error_code;
    }
    char *extension_count_p = writer->current;
    writer->current += KAA_PROTOCOL_EXTENSIONS_COUNT_SIZE;

    error_code = kaa_meta_data_request_serialize(self->status, writer, self->request_id);

    while (!error_code && services_count--) {
        switch (services[services_count]) {
        case KAA_SERVICE_BOOTSTRAP: {
            error_code = kaa_channel_manager_bootstrap_request_serialize(self->kaa_context->channel_manager
                         , writer);
            if (error_code)
                KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the bootstrap extension");
            break;
        }
        case KAA_SERVICE_PROFILE: {
            bool need_resync = false;
            error_code = kaa_profile_need_profile_resync(self->kaa_context->profile_manager
                         , &need_resync);
            if (!error_code) {
                if (need_resync) {
                    error_code = kaa_profile_request_serialize(self->kaa_context->profile_manager, writer);
                    if (error_code)
                        KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the profile extension");
                } else {
                    --total_services_count;
                }
            } else {
                KAA_LOG_ERROR(self->logger, error_code, "Failed to read profile's 'need_resync' flag");
            }
            break;
        }
        case KAA_SERVICE_USER: {
            error_code = kaa_user_request_serialize(self->kaa_context->user_manager, writer);
            if (error_code)
                KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the user extension");
            break;
        }
        case KAA_SERVICE_EVENT: {
#ifndef KAA_DISABLE_FEATURE_EVENTS
            error_code = kaa_event_request_serialize(self->kaa_context->event_manager, self->request_id, writer);
            if (error_code)
                KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the event extension");
#else
            --total_services_count;
#endif
            break;
        }
        case KAA_SERVICE_LOGGING: {
#ifndef KAA_DISABLE_FEATURE_LOGGING
            bool need_resync = false;
            error_code = kaa_logging_need_logging_resync(self->kaa_context->log_collector, &need_resync);
            if (!error_code) {
                if (need_resync) {
                    error_code = kaa_logging_request_serialize(self->kaa_context->log_collector, writer);
                    if (error_code)
                        KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the logging extension");
                } else {
                    --total_services_count;
                }
            } else {
                KAA_LOG_ERROR(self->logger, error_code, "Failed to read logging's 'need_resync' flag");
            }
#else
            --total_services_count;
#endif
            break;
        }
        case KAA_SERVICE_CONFIGURATION: {
#ifndef KAA_DISABLE_FEATURE_CONFIGURATION
            error_code = kaa_configuration_manager_request_serialize(self->kaa_context->configuration_manager, writer);
            if (error_code)
                KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the configuration extension");
#else
            --total_services_count;
#endif
            break;
        }
        case KAA_SERVICE_NOTIFICATION: {
#ifndef KAA_DISABLE_FEATURE_NOTIFICATION
            error_code = kaa_notification_manager_request_serialize(self->kaa_context->notification_manager, writer);
            if (error_code)
                KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the configuration extension");
#else
            --total_services_count;
#endif
            break;
        }
        default:
            break;
        }
    }
    *(uint16_t *) extension_count_p = KAA_HTONS(total_services_count);
    *size = writer->current - writer->begin;
    kaa_platform_message_writer_destroy(writer);

    return error_code;
}
Example #7
0
kaa_error_t kaa_client_create(kaa_client_t **kaa_client, kaa_client_props_t *props)
{
    KAA_RETURN_IF_NIL2(kaa_client, props, KAA_ERR_BADPARAM);

    kaa_error_t error_code = KAA_ERR_NONE;

    error_code = kaa_init_security_stuff();
    KAA_RETURN_IF_ERR(error_code);

    kaa_client_t *self = sndc_mem_calloc(1,sizeof(kaa_client_t));
    KAA_RETURN_IF_NIL(self,KAA_ERR_NOMEM);

    self->thread_name = sndc_mem_strdup("Kaa-Client-Thread");
    self->operate = true;
    self->max_update_time = props->max_update_time;

    print_mem_stat(self);
    sndc_printf("Initializing Kaa SDK...\n");
    sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);

    error_code = kaa_init(&self->kaa_context);
    if (error_code) {
        sndc_printf("Error during Kaa context creation %d\n", error_code);
        sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
        goto error;
    }

    KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa framework initialized.");

    print_mem_stat(self);
    error_code = kaa_log_collector_init(self);
    if (error_code) {
        sndc_printf("Failed to init Kaa log collector %d\n", error_code);
        sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
        goto error;
    }

    KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Kaa log collector initialized.");

    *kaa_client = self;

    /* initialize semaphore */
    sndc_sem_init(&self->start_semophore, 0);
    sndc_sem_init(&self->logging_semophore, 0);

    int status = sndc_thrd_create(
            self->thread_name,
            thread_run_fn,
            (uintptr_t)self,
            SNDC_THRD_PRIORITY_DEFAULT,
            THREAD_STACKSIZE_DMR_START); //Defined 4K-8 bytes of stack

    switch (status) {
        case 0:

            break;
        case -EINVAL:
            error_code = KAA_ERR_BADPARAM;
            break;
        case -ENOMEM:
            error_code = KAA_ERR_NOMEM;
            break;
        default:
            error_code = KAA_ERR_BADDATA;
            break;
    }

    return error_code;

error:

    sndc_printf("Kaa initialization failed. error_code %d\n", error_code);
    sndc_thrd_delay(TRACE_DELAY * SNDC_MILLISECOND);
    kaa_client_destroy(self);

    return error_code;
}