来自raspberry pi的Kaa C应用程序

时间:2017-02-05 00:52:02

标签: c kaa

我正在尝试在我的覆盆子pi上构建一个C应用程序,以便将日志发送到kaa服务器,我正在关注官方网站上的C文档。 我在使用java之前已经设法做到这一点,但现在我正在尝试使用相同的应用程序,但在C中。这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <kaa/kaa.h>
#include <kaa/platform/kaa_client.h>
#include <kaa/kaa_error.h>
#include <extensions/logging/kaa_logging.h>
#include <kaa/platform-impl/common/ext_log_upload_strategies.h>

#define LOG_UPLOAD_THRESHOLD 5

typedef struct {
    kaa_client_t *kaa_client;
    kaa_string_t * uuid;
    kaa_string_t * proximity;
    int32_t major;
    int32_t minor;
    int32_t measured_power;
    int32_t rssi;
    double accuracy;
} log_context;

static void sendLog(void *context)
{
    if (context == NULL) {
            return;
    }

    log_context * log_details_context = context;

    kaa_logging_beacon_t *log_record = kaa_logging_beacon_create();

    if (!log_record) {
            printf("Failed to create log record\r\n");
            kaa_client_stop(log_details_context->kaa_client);
            return;
    }

    log_record->uuid = log_details_context->uuid;
    log_record->major = log_details_context->major;
    log_record->minor = log_details_context->minor;
    log_record->measured_power = log_details_context->measured_power;
    log_record->rssi = log_details_context->rssi;
    log_record->accuracy = log_details_context->accuracy;
    log_record->proximity = log_details_context->proximity;

    kaa_error_t error = kaa_logging_add_record(
            kaa_client_get_context(log_details_context->kaa_client)->log_collector,
            log_record, NULL);

    if (error) {
            printf("Failed to add log record, error code %d\r\n");
            kaa_client_stop(log_details_context->kaa_client);
            return;
    }

    // log_record->destroy(log_record);

    kaa_client_stop(log_details_context->kaa_client);
}

int main(int argc, char **argv)
{
    double accuracy;
    kaa_string_t * uuid;
    kaa_string_t * proximity;
    int32_t major, minor, measured_power, rssi;

    if (argc != 8) {
            printf("Number of arguments are not right");
            return EXIT_FAILURE;
    }

    uuid = argv[1];
    if (sscanf (argv[2], "%i", &major) != 1) {
            printf("Major is not an integer");
            return EXIT_FAILURE;
    }

    if (sscanf (argv[3], "%i", &minor) != 1) {
            printf("Minor is not an integer");
            return EXIT_FAILURE;
    }

    if (sscanf (argv[4], "%i", &measured_power) != 1) {
            printf("Measured power is not an integer");
            return EXIT_FAILURE;
    }

    if (sscanf (argv[5], "%i", &rssi) != 1) {
            printf("RSSI is not an integer");
            return EXIT_FAILURE;
    }

    if (sscanf (argv[6], "%lf", &accuracy) != 1) {
            printf("Accuracy is not a double");
            return EXIT_FAILURE;
    }

    proximity = argv[7];
    /*
       printf("UUID = %s\n", uuid);
       printf("major = %d\n", major);
       printf("minor = %d\n", minor);
       printf("measured power = %d\n", measured_power);
       printf("RSSI = %d\n", rssi);
       printf("accuracy = %lf\n", accuracy);
       printf("proximity = %s\n", proximity);
     */


    /**
     * Initialize Kaa client.
     */
    kaa_client_t *kaa_client = NULL;
    kaa_error_t error = kaa_client_create(&kaa_client, NULL);

    if (error) {
            printf("Failed to create Kaa client\r\n", error);
            return EXIT_FAILURE;
    }

    log_context log_details_context;
    log_details_context.kaa_client = kaa_client;
    log_details_context.uuid = uuid;
    log_details_context.major = major;
    log_details_context.minor = minor;
    log_details_context.measured_power = measured_power;
    log_details_context.rssi = rssi;
    log_details_context.proximity = proximity;
    log_details_context.accuracy = accuracy;

    void *log_upload_strategy_context = NULL;
    error = ext_log_upload_strategy_create(kaa_client_get_context(kaa_client),
                                           &log_upload_strategy_context, KAA_LOG_UPLOAD_VOLUME_STRATEGY);

    if (error) {
            printf("Failed to create log upload strategy, error code %d\r\n", error);
            return EXIT_FAILURE;
    }

    error = ext_log_upload_strategy_set_threshold_count(log_upload_strategy_context,
                                                        LOG_UPLOAD_THRESHOLD);

    if (error) {
            printf("Failed to set threshold log record count, error code %d\r\n", error);
            return EXIT_FAILURE;
    }

    error = kaa_logging_set_strategy(kaa_client_get_context(kaa_client)->log_collector,
                                     log_upload_strategy_context);

    if (error) {
            printf("Failed to set log upload strategy, error code %d\r\n", error);
            return EXIT_FAILURE;
    }

    /**
     * Start Kaa client main loop.
     */
    error = kaa_client_start(kaa_client, sendLog,
                             &log_details_context, 0);

    if (error) {
            printf("Failed to start Kaa client, error code %d\r\n", error);
            return EXIT_FAILURE;
    }


    /**
     * Destroy Kaa client.
     */
    kaa_client_destroy(kaa_client);

    return EXIT_SUCCESS;
}

我在控制台上收到以下输出:

2017/02/05 0:24:47 [INFO] [kaa.c:149] (0) - Kaa SDK version 0.10.0, commit hash

2017/02/05 0:24:47 [DEBUG] [kaa_logging.c:383] (0) - Initialized log collector with log storage {0x2037c40}, log upload strategy {0x2037c78}

2017/02/05 0:24:47 [INFO] [kaa_client.c:471] (0) - Log collector init completed

2017/02/05 0:24:47 [INFO] [kaa_client.c:155] (0) - Kaa client created

2017/02/05 0:24:47 [INFO] [kaa_client.c:295] (0) - Starting Kaa client...

Segmentation fault (core dumped)

这是我从gdb

获得的调试信息
Starting program: /home/delvebyte/projects/beacon-scanner-pi/kaa-application-c/build/kaa-app UUID 0 1 -65 -72 0.873637383 near
2017/02/06 16:20:26 [INFO] [kaa.c:149] (0) - Kaa SDK version 0.10.0, commit hash 
2017/02/06 16:20:26 [DEBUG] [kaa_logging.c:383] (0) - Initialized log collector with log storage {0x5555557a14d0}, log upload strategy {0x5555557a1ab0}
2017/02/06 16:20:26 [INFO] [kaa_client.c:471] (0) - Log collector init completed
2017/02/06 16:20:26 [INFO] [kaa_client.c:155] (0) - Kaa client created
2017/02/06 16:20:26 [INFO] [kaa_client.c:295] (0) - Starting Kaa client...

Program received signal SIGSEGV, Segmentation fault.
strlen () at ../sysdeps/x86_64/strlen.S:106
106 ../sysdeps/x86_64/strlen.S: No such file or directory.
(gdb) bt
#0  strlen () at ../sysdeps/x86_64/strlen.S:106
#1  0x00005555555658c2 in kaa_string_get_size (data=0x7fffffffe256)
at /home/delvebyte/projects/beacon-scanner-pi/kaa-application-c/kaa/src/kaa/kaa_common_schema.c:117
#2  0x000055555556220d in kaa_logging_beacon_get_size (data=0x5555557a1b70)
at /home/delvebyte/projects/beacon-scanner-pi/kaa-application-c/kaa/src/kaa/gen/kaa_logging_gen.c:63
#3  0x00005555555700f4 in kaa_logging_add_record (self=0x5555557a0bc0, 
entry=0x5555557a1b70, log_info=0x0)
at /home/delvebyte/projects/beacon-scanner-pi/kaa-application-c/kaa/src/extensions/logging/kaa_logging.c:514
#4  0x0000555555556dcf in sendLog (context=0x7fffffffdd30)
at /home/delvebyte/projects/beacon-scanner-pi/kaa-application-c/src/kaa-application.c:46
#5  0x0000555555557b48 in kaa_client_start (kaa_client=0x5555557a0010, 
external_process=0x555555556cf0 <sendLog>, 
external_process_context=0x7fffffffdd30, max_delay=0)
at /home/delvebyte/projects/beacon-scanner-pi/kaa-application-c/kaa/src/kaa/platform-impl/posix/kaa_client.c:300
#6  0x0000555555557153 in main (argc=8, argv=0x7fffffffde68)
at /home/delvebyte/projects/beacon-scanner-pi/kaa-application-c/src/kaa-application.c:160
(gdb)

1 个答案:

答案 0 :(得分:0)

问题在于将kaa_string_t *视为char *,当我更好地查看kaa库时,我发现kaa_string_t是一个内部有char *的结构,所以我用这个函数来初始化变量kaa_string_move_create是吗