Beispiel #1
0
static void kaa_device_info_request(void *context
                           , kaa_remote_control_ecf_device_info_request_t *event
                           , kaa_endpoint_id_p source)
{
    (void)context;
    (void)source;

    demo_printf("Device info request received\r\n");
    kaa_remote_control_ecf_device_info_response_t *response = kaa_remote_control_ecf_device_info_response_create();

    response->device_name = kaa_string_copy_create(TARGET_DEVICE_NAME);
    response->model       = kaa_string_copy_create(TARGET_MODEL_NAME);
    response->gpio_status = kaa_list_create();
    for (int i = 0; i < target_gpio_led_get_count(); ++i) {
        gpio_port_t *gpio_led = target_get_gpio_port( i );
        if (gpio_led) {
            kaa_remote_control_ecf_gpio_status_t *gio_status = kaa_remote_control_ecf_gpio_status_create();
            gio_status->id = i;
            gio_status->status = gpio_led->state;
            gio_status->type = kaa_string_copy_create( gpio_led->id );
            kaa_list_push_back(response->gpio_status, (void*)gio_status);
        }
    }
    kaa_error_t err = kaa_event_manager_send_kaa_remote_control_ecf_device_info_response(kaa_client_get_context(kaa_client)->event_manager, response, NULL);

    response->destroy(response); // Destroying event that was successfully sent
    event->destroy(event);
}
Beispiel #2
0
void test_list_for_each()
{
    KAA_TRACE_IN(logger);

    kaa_list_t *list = kaa_list_create();
    ASSERT_NOT_NULL(list);

    int32_t *number1_ptr;
    int node_number = 4;
    for (int i = 0; i < node_number; ++i) {
        number1_ptr = (int32_t *)KAA_MALLOC(sizeof(int32_t *));
        ASSERT_NOT_NULL(number1_ptr);
        *number1_ptr = rand();
        kaa_list_push_back(list, number1_ptr);
    }

    int number2 = rand();
    kaa_list_for_each(kaa_list_begin(list), kaa_list_back(list), (process_data)&test_process_data, &number2);

    kaa_list_node_t *it = kaa_list_begin(list);
    while (it) {
        ASSERT_EQUAL(*(int32_t *)kaa_list_get_data(kaa_list_begin(list)), number2);
        it = kaa_list_next(it);
    }

    kaa_list_destroy(list, NULL);

    KAA_TRACE_OUT(logger);
}
Beispiel #3
0
static void test_list_sort()
{
    kaa_list_t *list = kaa_list_create();
    ASSERT_NOT_NULL(list);

    uint64_t node_number = 100;
    for (uint64_t i = 0; i < node_number; ++i) {
        test_list_node_t *node = KAA_MALLOC(sizeof(test_list_node_t));
        ASSERT_NOT_NULL(node);
        node->id = (uint64_t) rand();
        kaa_list_push_back(list, node);
    }

    ASSERT_EQUAL(kaa_list_get_size(list), node_number);

    kaa_list_sort(list,&test_kaa_list_predicate);
    kaa_list_node_t *it,*next;
    it = kaa_list_begin(list);
    next = kaa_list_next(it);
    while (it && next) {
        ASSERT_TRUE(((test_list_node_t*)kaa_list_get_data(it))->id <=
                ((test_list_node_t*)kaa_list_get_data(next))->id);
        it = next;
        next = kaa_list_next(it);
    }
    kaa_list_destroy(list, NULL);
}
Beispiel #4
0
static void test_array_get_size(void **state)
{
    (void)state;

    srand(time(NULL));
    ASSERT_EQUAL(kaa_array_get_size(NULL, NULL), 0);
    ASSERT_EQUAL(kaa_array_get_size(NULL, (get_size_fn)kaa_null_get_size), avro_long_get_size(0));

    const char *plain_str = "data";
    kaa_string_t *reference_kaa_str = kaa_string_copy_create(plain_str);
    size_t element_size = kaa_string_get_size(reference_kaa_str);
    kaa_string_destroy(reference_kaa_str);

    size_t array_size = 1 + rand() % 10;

    kaa_list_t *avro_array = kaa_list_create();
    size_t i = 0;
    for (i = 0; i < array_size; ++i) {
        kaa_list_push_back(avro_array, kaa_string_copy_create(plain_str));
    }

    size_t expected_size = avro_long_get_size(array_size)
                         + array_size * element_size
                         + avro_long_get_size(0);

    ASSERT_EQUAL(kaa_array_get_size(avro_array, &kaa_string_get_size), expected_size);

    kaa_list_destroy(avro_array, kaa_string_destroy);
}
Beispiel #5
0
static kaa_error_t remember_request(kaa_log_collector_t *self, uint16_t bucket_id)
{
    KAA_RETURN_IF_NIL(self, KAA_ERR_BADPARAM);

    timeout_info_t *info = (timeout_info_t *)KAA_MALLOC(sizeof(timeout_info_t));
    KAA_RETURN_IF_NIL(info, KAA_ERR_NOMEM);

    info->log_bucket_id = bucket_id;
    info->timeout = KAA_TIME() + (kaa_time_t)ext_log_upload_strategy_get_timeout(self->log_upload_strategy_context);

    kaa_list_node_t *it = kaa_list_push_back(self->timeouts, info);
    if (!it) {
        KAA_FREE(info);
        return KAA_ERR_NOMEM;
    }

    return KAA_ERR_NONE;
}
Beispiel #6
0
kaa_error_t ext_log_storage_add_log_record(void *context, kaa_log_record_t *record)
{
    mock_storage_context_t *self = context;

    test_log_record_t *test_rec = KAA_MALLOC(sizeof(*test_rec));
    if (!test_rec) {
        return KAA_ERR_NOMEM;
    }

    test_rec->rec = *record;
    test_rec->processed = false;

    kaa_list_push_back(self->logs, test_rec);

    self->record_count++;
    self->total_size += test_rec->rec.size;

    return KAA_ERR_NONE;
}
Beispiel #7
0
static void test_list_push_back()
{
    kaa_list_t *list = kaa_list_create();
    ASSERT_NOT_NULL(list);

    int32_t *number;
    int node_number = 2;
    for (int i = 0; i < node_number; ++i) {
        number = KAA_MALLOC(sizeof(int32_t *));
        ASSERT_NOT_NULL(number);
        *number = rand();
        kaa_list_push_back(list, number);
    }

    ASSERT_EQUAL(kaa_list_get_size(list), node_number);
    ASSERT_EQUAL((*(int32_t *)kaa_list_get_data(kaa_list_back(list))), *number);

    kaa_list_destroy(list, NULL);
}
Beispiel #8
0
static void test_array_serialize(void **state)
{
    (void)state;

    const char *plain_str = "data";
    kaa_string_t *reference_kaa_str = kaa_string_copy_create(plain_str);
    size_t element_size = kaa_string_get_size(reference_kaa_str);
    kaa_string_destroy(reference_kaa_str);

    size_t array_size = 1 + rand() % 10;
    size_t expected_size = avro_long_get_size(array_size)
                         + array_size * element_size
                         + avro_long_get_size(0);
    char manual_buffer[expected_size];
    avro_writer_t manual_avro_writer = avro_writer_memory(manual_buffer, expected_size);
    avro_binary_encoding.write_long(manual_avro_writer, array_size);

    kaa_list_t *avro_array = kaa_list_create();
    size_t i = 0;
    for (i = 0; i < array_size; ++i) {
        kaa_string_t *array_data = kaa_string_copy_create(plain_str);
        kaa_list_push_back(avro_array, array_data);
        avro_binary_encoding.write_string(manual_avro_writer, array_data->data);
    }

    avro_binary_encoding.write_long(manual_avro_writer, 0);

    size_t actual_size = kaa_array_get_size(avro_array, kaa_string_get_size);

    ASSERT_EQUAL(actual_size, expected_size);

    char auto_buffer[actual_size];
    avro_writer_t auto_avro_writer = avro_writer_memory(auto_buffer, actual_size);

    kaa_array_serialize(auto_avro_writer, avro_array, kaa_string_serialize);

    ASSERT_EQUAL(memcmp(auto_buffer, manual_buffer, expected_size), 0);

    kaa_list_destroy(avro_array, kaa_string_destroy);
    avro_writer_free(manual_avro_writer);
    avro_writer_free(auto_avro_writer);
}
Beispiel #9
0
static void test_array_deserialize_w_ctx(void **state)
{
    (void)state;

    const uint8_t plain_fixed[] = { 0x0, 0x1, 0x2, 0x3, 0x4 };
    size_t plain_fixed_size = sizeof(plain_fixed) / sizeof(char);

    size_t array_size = 1 + rand() % 10;

    kaa_list_t *avro_array1 = kaa_list_create();
    size_t i = 0;
    for (i = 0; i < array_size; ++i) {
        kaa_list_push_back(avro_array1, kaa_fixed_copy_create(plain_fixed, plain_fixed_size));
    }

    size_t buffer_size = kaa_array_get_size(avro_array1, kaa_fixed_get_size);
    char buffer[buffer_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, buffer_size);

    kaa_array_serialize(avro_writer, avro_array1, kaa_fixed_serialize);

    avro_reader_t avro_reader = avro_reader_memory(buffer, buffer_size);
    kaa_list_t *avro_array2 = kaa_array_deserialize_w_ctx(avro_reader, (deserialize_w_ctx_fn)kaa_fixed_deserialize, &plain_fixed_size);

    ASSERT_NOT_NULL(avro_array2);
    ASSERT_EQUAL(kaa_list_get_size(avro_array2), array_size);

    kaa_list_node_t *it = kaa_list_begin(avro_array2);
    while (it) {
        kaa_bytes_t *fixed = kaa_list_get_data(it);
        ASSERT_NOT_NULL(fixed);
        ASSERT_EQUAL((size_t)fixed->size, plain_fixed_size);
        ASSERT_EQUAL(memcmp(fixed->buffer, plain_fixed, plain_fixed_size), 0);
        it  = kaa_list_next(it);
    }

    kaa_list_destroy(avro_array2, kaa_fixed_destroy);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
    kaa_list_destroy(avro_array1, kaa_fixed_destroy);
}
Beispiel #10
0
static void test_list_hash()
{
    kaa_list_t *list = kaa_list_create();
    ASSERT_NOT_NULL(list);

    uint64_t node_number = 100;
    for (uint64_t i = 0; i < node_number; ++i) {
        test_list_node_t *node = KAA_MALLOC(sizeof(test_list_node_t));
        ASSERT_NOT_NULL(node);
        node->id = (uint64_t) node_number - i;
        kaa_list_push_back(list, node);
    }

    ASSERT_EQUAL(kaa_list_get_size(list), node_number);

    kaa_list_sort(list,&test_kaa_list_predicate);

    ASSERT_EQUAL(kaa_list_hash(list,&test_kaa_list_hash),-974344717);

    kaa_list_destroy(list, NULL);
}
Beispiel #11
0
static void kaa_demo_add_log_record(void *context)
{
    static size_t log_number = LOGS_TO_SEND_COUNT;

    kaa_logging_power_report_t *log_record = kaa_logging_power_report_create();
    if (!log_record) {
        printf("Failed to create log record, error code %d\n", KAA_ERR_NOMEM);
        return;
    }

    log_record->timestamp = time(NULL) * 1000; // expected in millis
    log_record->samples = kaa_list_create();

    size_t zone_id = 0;
    size_t panel_id = 0;
    for (zone_id = 0; zone_id < ZONE_COUNT; ++zone_id) {
        for (panel_id = 0; panel_id < PANEL_COUNT; ++panel_id) {
            kaa_logging_power_sample_t *sample = kaa_logging_power_sample_create();
            sample->zone_id = zone_id;
            sample->panel_id = panel_id;
            sample->power = get_random_double(MAX_PANEL_POWER);

            kaa_list_push_back(log_record->samples, sample);
        }
    }

    kaa_error_t error_code = kaa_logging_add_record(kaa_client_get_context((kaa_client_t *)context)->log_collector, log_record);
    if (error_code) {
        printf("Failed to add log record, error code %d\n", error_code);
    }

    log_record->destroy(log_record);

    if (!--log_number) {
        kaa_client_stop(kaa_client);
    }
}
Beispiel #12
0
static kaa_error_t remember_request(kaa_log_collector_t *self, uint16_t bucket_id, uint16_t count)
{
    // TODO(KAA-982): Use asserts
    if (!self) {
        return KAA_ERR_BADPARAM;
    }

    timeout_info_t *info = KAA_MALLOC(sizeof(*info));
    if (!info) {
        return KAA_ERR_NOMEM;
    }

    info->log_bucket_id = bucket_id;
    info->deadline = KAA_TIME() + (kaa_time_t)ext_log_upload_strategy_get_timeout(self->log_upload_strategy_context);
    info->log_count = count;

    kaa_list_node_t *it = kaa_list_push_back(self->timeouts, info);
    if (!it) {
        KAA_FREE(info);
        return KAA_ERR_NOMEM;
    }

    return KAA_ERR_NONE;
}
Beispiel #13
0
static void test_array_deserialize_wo_ctx(void **state)
{
    (void)state;

    size_t array_size = 1 + rand() % 10;

    kaa_list_t *avro_array1 = kaa_list_create();
    size_t i = 0;
    for (i = 0; i < array_size; ++i) {
        kaa_list_push_back(avro_array1, create_float());
    }

    size_t buffer_size = kaa_array_get_size(avro_array1, kaa_float_get_size);
    char buffer[buffer_size];
    avro_writer_t avro_writer = avro_writer_memory(buffer, buffer_size);

    kaa_array_serialize(avro_writer, avro_array1, kaa_float_serialize);

    avro_reader_t avro_reader = avro_reader_memory(buffer, buffer_size);
    kaa_list_t *avro_array2 = kaa_array_deserialize_wo_ctx(avro_reader, (deserialize_wo_ctx_fn)kaa_float_deserialize);

    ASSERT_NOT_NULL(avro_array2);
    ASSERT_EQUAL(kaa_list_get_size(avro_array2), array_size);

//    /*
//     * Deserialized array has a back order, so reverse the original.
//     */
//    kaa_list_t *reverse_avro_array = NULL;
//    kaa_list_t *it = avro_array1;
//    while (it) {
//        if (reverse_avro_array) {
//            reverse_avro_array = kaa_list_push_front(reverse_avro_array, kaa_list_get_data(it));
//        } else {
//            reverse_avro_array = kaa_list_create(kaa_list_get_data(it));
//        }
//        it  = kaa_list_next(it);
//    }
//
//    kaa_list_destroy(avro_array1, kaa_null_destroy);
//    avro_array1 = reverse_avro_array;

    /*
     * Compare origin and deserialized arrays.
     */
    kaa_list_node_t *it1 = kaa_list_begin(avro_array1);
    kaa_list_node_t *it2 = kaa_list_begin(avro_array2);
    while (it1 && it2) {
        float *float_value1 = kaa_list_get_data(it1);
        float *float_value2 = kaa_list_get_data(it2);

        ASSERT_NOT_NULL(float_value1);
        ASSERT_NOT_NULL(float_value2);
        ASSERT_EQUAL(*float_value2, *float_value1);

        it1  = kaa_list_next(it1);
        it2  = kaa_list_next(it2);
    }

    kaa_list_destroy(avro_array2, kaa_data_destroy);
    avro_reader_free(avro_reader);
    avro_writer_free(avro_writer);
    kaa_list_destroy(avro_array1, kaa_data_destroy);
}