kaa_error_t kaa_init(kaa_context_t **kaa_context_p) { KAA_RETURN_IF_NIL(kaa_context_p, KAA_ERR_BADPARAM); // Initialize logger kaa_logger_t *logger = NULL; FILE *logfile = fopen("run.log", "w"); kaa_error_t error = kaa_log_create(&logger, KAA_MAX_LOG_MESSAGE_LENGTH, KAA_MAX_LOG_LEVEL, logfile); // TODO: make log destination configurable if (error) return error; KAA_LOG_INFO(logger, KAA_ERR_NONE, "Kaa SDK version %s, commit hash %s", BUILD_VERSION, BUILD_COMMIT_HASH); // Initialize general Kaa context error = kaa_context_create(kaa_context_p, logger); if (error) { KAA_LOG_FATAL(logger, error, "Failed to create Kaa context"); kaa_log_destroy(logger); *kaa_context_p = NULL; return error; } // Initialize endpoint identity char *pub_key_buffer = NULL; size_t pub_key_buffer_size = 0; bool need_deallocation = false; ext_get_endpoint_public_key(&pub_key_buffer, &pub_key_buffer_size, &need_deallocation); kaa_digest pub_key_hash; error = ext_calculate_sha_hash(pub_key_buffer, pub_key_buffer_size, pub_key_hash); if (need_deallocation && pub_key_buffer_size > 0) { KAA_FREE(pub_key_buffer); } if (error) { KAA_LOG_FATAL(logger, error, "Failed to calculate EP ID"); kaa_context_destroy(*kaa_context_p); *kaa_context_p = NULL; kaa_log_destroy(logger); return error; } error = ext_copy_sha_hash((*kaa_context_p)->status->status_instance->endpoint_public_key_hash, pub_key_hash); if (error) { KAA_LOG_FATAL(logger, error, "Failed to set Endpoint public key"); kaa_context_destroy(*kaa_context_p); *kaa_context_p = NULL; kaa_log_destroy(logger); return error; } return KAA_ERR_NONE; }
kaa_error_t kaa_profile_request_get_size(kaa_profile_manager_t *self, size_t *expected_size) { // TODO(KAA-982): Use asserts if (!self || !expected_size) { return KAA_ERR_BADPARAM; } if (!resync_is_required(self)) { *expected_size = 0; return KAA_ERR_NONE; } *expected_size = KAA_EXTENSION_HEADER_SIZE; *expected_size += sizeof(uint32_t); // profile body size #if KAA_PROFILE_SCHEMA_VERSION > 0 if (resync_is_required(self)) *expected_size += kaa_aligned_size_get(self->profile_body.size); // profile data #endif if (!self->status->is_registered) { if (!self->extension_data->public_key.buffer) { ext_get_endpoint_public_key((const uint8_t **)&self->extension_data->public_key.buffer, (size_t *)&self->extension_data->public_key.size); } if (self->extension_data->public_key.buffer && self->extension_data->public_key.size > 0) { *expected_size += sizeof(uint32_t); // public key size *expected_size += kaa_aligned_size_get(self->extension_data->public_key.size); // public key } else { return KAA_ERR_BADDATA; } } self->extension_data->access_token.buffer = (uint8_t *) self->status->endpoint_access_token; if (self->extension_data->access_token.buffer) { self->extension_data->access_token.size = strlen((const char*)self->extension_data->access_token.buffer); *expected_size += sizeof(uint32_t); // access token length *expected_size += kaa_aligned_size_get(self->extension_data->access_token.size); // access token } self->extension_data->payload_size = *expected_size - KAA_EXTENSION_HEADER_SIZE; return KAA_ERR_NONE; }
void setup() { pinMode(CH_PD, OUTPUT); pinMode(ESP8266_RST, OUTPUT); pinMode(BOARD_BUTTON_PIN, INPUT); pinMode(BOARD_LED_PIN, OUTPUT); pinMode(LEFT_LIGHT, OUTPUT); pinMode(RIGHT_LIGHT, OUTPUT); light_blink_counter = -1; systick_enable(); esp8266_serial_init(&esp8266_serial, ESP_SERIAL, ESP_SERIAL_BAUD); if (!esp8266_serial) { debug("Serial Initialization failed, no memory\r\n"); return; } RFID_READER.begin(RFID_BAUD_RATE); kaa_client_props_t props; props.serial = esp8266_serial; props.wifi_ssid = SSID; props.wifi_pswd = PWD; bool need_deallocation; ext_get_endpoint_public_key(&props.kaa_public_key, &props.kaa_public_key_length, &need_deallocation); kaa_error_t error = kaa_client_create(&kaa_client, &props); if (error) { debug("Failed to init Kaa client, error code %d\r\n", error); return; } error = kaa_user_manager_default_attach_to_user(kaa_client_get_context(kaa_client)->user_manager , KAA_USER_ID , KAA_USER_ACCESS_TOKEN); if (error) { debug("Failed to attach to user '%s', error code %d\r\n", KAA_USER_ID, error); return; } }