无法使用KAA 0.10 C SDK推送日志数据

时间:2017-03-08 04:17:25

标签: kaa

我们使用KAA 0.10创建了一个本地云实例,并在添加了一些日志上传模式后生成了C Posix SDK。

我创建了一个示例程序,其中只创建一个KAA客户端并更新虚拟端点并启动KAA客户端,其中将调用一个虚拟回调函数,该函数正常工作而没有任何问题。

之后,我扩展了我的工作代码以上传一些事件的日志数据,例如每1秒或每5个日志计数,并在我的Linux系统上执行相同的代码,但我按照策略定义随机获取“failed_log_delivery_callback”。

请使用KAA 0.10 C SDK查找以下用于检查日志上传策略的代码。

/* Set of routines that handles log delivery events */
static void success_log_delivery_callback(void *context, const kaa_log_bucket_info_t *bucket)
{
    printf("+++++++++++++++++ success_log_delivery_callback called +++++++++++++++++++++++\n");

    printf("Bucket: %u is successfully delivered. Logs uploaded: %zu\n",
           bucket->bucket_id,
           bucket->log_count);
}

static void failed_log_delivery_callback(void *context, const kaa_log_bucket_info_t *bucket)
{
    printf("+++++++++++++++++ failed_log_delivery_callback called +++++++++++++++++++++++\n");

    printf("Log delivery of the bucket: %u is failed!\n", bucket->bucket_id);
}

static void timeout_log_delivery_callback(void *context, const kaa_log_bucket_info_t *bucket)
{
    printf("+++++++++++++++++ timeout_log_delivery_callback called +++++++++++++++++++++++\n");

    printf("Timeout reached for log delivery of the bucket: %u!\n", bucket->bucket_id);
}

/* Log delivery listener callbacks. Each callback called whenever something happen with a log bucket. */
kaa_log_delivery_listener_t log_listener = {
     .on_success = success_log_delivery_callback,   /* Called if log delivered successfully */
     .on_failed  = failed_log_delivery_callback,    /* Called if delivery failed */
     .on_timeout = timeout_log_delivery_callback,   /* Called if timeout occurs */
     .ctx        = NULL,                            /* Optional context */
};

kaa_union_t *kaa_logging_union_create(const char *data, uint8_t type, destroy_fn destroy)
{
    kaa_union_t *str;
    str = kaa_logging_union_string_or_null_branch_0_create();
    str->data = kaa_string_move_create(data,NULL);

    return str;
}

kaa_union_t *kaa_profile_union_create(const char *data, uint8_t type, destroy_fn destroy)
{
    kaa_union_t *str;
    str = kaa_logging_union_string_or_null_branch_0_create();
    str->data = kaa_string_move_create(data,NULL);

    return str;
}

static void dummy_function(void *context)
{
    kaa_error_t error_code = 0;

    printf("\n\n**************** Hello, I am a Kaa Application!!!!!!!!!!!!!!!!!!!!!!\n\n");

    kaa_logging_event_push_t *log_push;

    /* Log information. Populated when log is added via kaa_logging_add_record() */
    kaa_log_record_info_t log_info;

    /* kaa logging event push data will be created from here */
    log_push = kaa_logging_event_push_create();

    if(log_push == NULL)
    {
        printf("can not create log data\r\n");
        kaa_client_stop(kaa_client);
        return;
    }

    log_push->tag = kaa_logging_union_create("67891",0,NULL);
    if(log_push->tag == NULL)
    {
        printf("can not create log data\r\n");
        kaa_client_stop(kaa_client);
        return;
    }

    log_push->rid = kaa_logging_union_create("77:88:99:11:22:33",0,NULL);
    if(log_push->rid == NULL)
    {
        printf("can not create log data\r\n");
        kaa_client_stop(kaa_client);
        return;
    }

    log_push->var = kaa_logging_union_create("HIJKLMN",0,NULL);
    if(log_push->var == NULL)
    {
        printf("can not create log data\r\n");
        kaa_client_stop(kaa_client);
        return;
    }

    log_push->payload = kaa_string_move_create("Linux System KAA Message",NULL);
    if(log_push->payload == NULL)
    {
        printf("can not create log data\r\n");
        kaa_client_stop(kaa_client);
        return;
    }

    /* kaa logging push data will be added from here */
    error_code = kaa_logging_add_record(kaa_client_get_context(kaa_client)->log_collector, log_push, &log_info);
    if (error_code) 
    {
        printf("Failed to add log record, error code %d\n", error_code);
        kaa_client_stop(kaa_client);
        return;
    }

    printf("log message added :: count :: %d\r\n", kaa_log_count);

    /* kaa logging push data will be destroyed from here */
    log_push->destroy(log_push);

    kaa_log_count++;

    /* kaa client will be stopped from here */
    /*error_code = kaa_client_stop(kaa_client);
    if (error_code) 
    {
        printf("kaa_client_stop is failed to execute.....\n");
        return;
    }
    else
        printf("kaa_client_stop is executed successfully.....\n");  */
}

int main(void)
{
    kaa_error_t error_code = 0;
    int i = 0;
    void *log_storage_context = NULL, *log_upload_strategy_context = NULL;

    printf("*********** kaa_basic_main started **************\n");

    kaa_profile_t *profile = NULL;

    printf("Before kaa_client_create API...\n");

    /* kaa client will be created from here */
    error_code = kaa_client_create(&kaa_client, NULL);
    if (error_code) 
    {
        printf("kaa_client_create API is failed to execute...\n");
        return EXIT_FAILURE;
    }
    else
        printf("kaa_client_create API is called successfully...\n");

    sleep(2);

    kaa_endpoint_id_p buff = (kaa_endpoint_id_p)malloc(23);

    /* kaa profile manager endpoint id comes from here */
    error_code = kaa_profile_manager_get_endpoint_id(kaa_client_get_context(kaa_client)->profile_manager,buff);
    if (error_code) 
    {
        printf("kaa_profile_manager_get_endpoint_id is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
    {
        for(i = 0; i < 23; i++)
        {
            printf("kaa_profile_manager_get_endpoint_id[i] :: 0x%x\n", buff[i]);
        }


        printf("kaa_profile_manager_get_endpoint_id API is called successfully...\n");
    }

    free(buff);

    /* kaa profile will be created from here */
    profile = (kaa_profile_t *)kaa_profile_profile_create();

    printf("kaa_profile_profile_create API is called...\n");

    profile->id     = kaa_string_move_create(id,NULL);
    profile->key    = kaa_string_move_create((const char *)key,NULL);
    profile->current_firmware_version = kaa_string_move_create(f_version,NULL);
    profile->serial_number = kaa_string_move_create(ser_number,NULL);
    profile->health    = kaa_string_move_create(health,NULL);
    profile->pii    = kaa_string_move_create(pii,NULL);
    profile->info    = kaa_string_move_create(info,NULL);

    sleep(2);

    printf("before kaa_profile_manager_update_profile API is called...\n");

    /* kaa profile will be updated from here */
    error_code = kaa_profile_manager_update_profile(kaa_client_get_context(kaa_client)->profile_manager, profile);
    if (error_code) 
    {
        printf("kaa_profile_manager_update_profile is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
        printf("kaa_profile_manager_update_profile API is called successfully...\n");

    sleep(2);

    printf("before calling kaa_client_start.....\n");

    /* The internal memory log storage distributed with Kaa SDK */
    error_code = ext_unlimited_log_storage_create(&log_storage_context, kaa_client_get_context(kaa_client)->logger);
    if (error_code) 
    {
        printf("ext_unlimited_log_storage_create is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
        printf("ext_unlimited_log_storage_create is executed successfully.....\n");

    /* KAA Upload Strategy will be created with timeout */
    error_code = ext_log_upload_strategy_create(kaa_client_get_context(kaa_client), &log_upload_strategy_context, KAA_LOG_UPLOAD_BY_TIMEOUT_STRATEGY);
    if (error_code) 
    {
        printf("ext_log_upload_strategy_create is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
        printf("ext_log_upload_strategy_create is executed successfully.....\n");

    /* Strategy will upload logs every 1 seconds. */
    error_code = ext_log_upload_strategy_set_upload_timeout(log_upload_strategy_context, 1);
    if (error_code) 
    {
        printf("ext_log_upload_strategy_set_threshold_count is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
        printf("ext_log_upload_strategy_set_threshold_count is executed successfully.....\n");

    /* Specify log bucket size constraints */
    kaa_log_bucket_constraints_t bucket_sizes = {
         .max_bucket_size       = 512,  /* Bucket size in bytes */
         .max_bucket_log_count  = 5,    /* Maximum log count in one bucket */
    };

    /* Initialize the log storage and strategy (by default it is not set) */
    error_code = kaa_logging_init(kaa_client_get_context(kaa_client)->log_collector
                                  , log_storage_context
                                  , log_upload_strategy_context
                                  , &bucket_sizes);
    if (error_code) 
    {
        printf("kaa_logging_init is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
        printf("kaa_logging_init is executed successfully.....\n"); 

    /* Add listeners to a log collector */
    kaa_logging_set_listeners(kaa_client_get_context(kaa_client)->log_collector, &log_listener);
    if (error_code) 
    {
        printf("kaa_logging_set_listeners is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
        printf("kaa_logging_set_listeners is executed successfully.....\n"); 

    /* kaa client will be started from here */  
    error_code = kaa_client_start(kaa_client, dummy_function, (void *)kaa_client, 0);  
    if (error_code) 
    {
        printf("kaa_client_start is failed to execute.....\n");
        return EXIT_FAILURE;
    }
    else
        printf("kaa_client_start is executed successfully.....\n");

    printf("after calling kaa_client_start.....\n");

    sleep(2);

    /* kaa client will be destroyed from here */
    kaa_client_destroy(kaa_client);

    printf("after calling kaa_client_destroy.....\n");
    return EXIT_SUCCESS;
}

如果我需要更多细节,请告诉我。

0 个答案:

没有答案