Example #1
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;

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

    esp8266_error_t esp8266_error = esp8266_create(&self->controler, props->serial, DEFAULT_ESP8266_CONTROLER_BUFFER_SIZE);
    if (esp8266_error) {
        debug("Error during esp8266 creation %d\n", esp8266_error);
        kaa_client_destroy(self);
        return KAA_ERR_BADDATA;
    }

    esp8266_error = esp8266_tcp_register_receive_callback(self->controler, esp8266_tcp_receive_fn, (void *)self);
    if (esp8266_error) {
        debug("Error during esp8266 registering receive callback %d\n", esp8266_error);
        kaa_client_destroy(self);
        return KAA_ERR_BADDATA;
    }

    error_code = kaa_init_security_stuff(props->kaa_public_key, props->kaa_public_key_length);
    if (error_code) {
        debug("Error generate SHA1 diges form Public Key, error %d", error_code);
        kaa_client_destroy(self);
        return error_code;
    }

    error_code = kaa_init(&self->kaa_context);
    if (error_code) {
        debug("Error during Kaa context creation %d\n", error_code);
        kaa_client_destroy(self);
        return error_code;
    }

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

    self->wifi_ssid = props->wifi_ssid;
    self->wifi_pswd = props->wifi_pswd;
    self->operate = true;
    self->blink_timeout = 500;

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

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

    *kaa_client = self;

    return error_code;
}
Example #2
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;
}