kaa_error_t kaa_profile_handle_server_sync(kaa_profile_manager_t *self , kaa_platform_message_reader_t *reader , uint16_t extension_options , size_t extension_length) { // Only used for logging (void)extension_options; (void)extension_length; KAA_RETURN_IF_NIL2(self, reader, KAA_ERR_BADPARAM); KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Received profile server sync: options %u, payload size %zu", extension_options, extension_length); kaa_error_t error_code = KAA_ERR_NONE; self->need_resync = false; if (extension_options & KAA_PROFILE_RESYNC_OPTION) { self->need_resync = true; KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Going to resync profile..."); kaa_transport_channel_interface_t *channel = kaa_channel_manager_get_transport_channel(self->channel_manager, profile_sync_services[0]); if (channel) channel->sync_handler(channel->context, profile_sync_services, 1); } if (!self->status->is_registered) { KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Endpoint has been registered"); self->status->is_registered = true; } return error_code; }
kaa_error_t kaa_profile_manager_update_profile(kaa_profile_manager_t *self, kaa_profile_t *profile_body) { #if KAA_PROFILE_SCHEMA_VERSION > 0 KAA_RETURN_IF_NIL2(self, profile_body, KAA_ERR_BADPARAM); size_t serialized_profile_size = profile_body->get_size(profile_body); if (!serialized_profile_size) { KAA_LOG_ERROR(self->logger, KAA_ERR_BADDATA, "Failed to update profile: serialize profile size is null. Maybe profile schema is empty"); return KAA_ERR_BADDATA; } char *serialized_profile = (char *) KAA_MALLOC(serialized_profile_size * sizeof(char)); KAA_RETURN_IF_NIL(serialized_profile, KAA_ERR_NOMEM); avro_writer_t writer = avro_writer_memory(serialized_profile, serialized_profile_size); if (!writer) { KAA_FREE(serialized_profile); return KAA_ERR_NOMEM; } profile_body->serialize(writer, profile_body); avro_writer_free(writer); kaa_digest new_hash; ext_calculate_sha_hash(serialized_profile, serialized_profile_size, new_hash); if (!memcmp(new_hash, self->status->profile_hash, SHA_1_DIGEST_LENGTH)) { self->need_resync = false; KAA_FREE(serialized_profile); return KAA_ERR_NONE; } KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Endpoint profile is updated"); if (ext_copy_sha_hash(self->status->profile_hash, new_hash)) { KAA_FREE(serialized_profile); return KAA_ERR_BAD_STATE; } if (self->profile_body.size > 0) { KAA_FREE(self->profile_body.buffer); self->profile_body.buffer = NULL; } self->profile_body.buffer = (uint8_t*)serialized_profile; self->profile_body.size = serialized_profile_size; self->need_resync = true; kaa_transport_channel_interface_t *channel = kaa_channel_manager_get_transport_channel(self->channel_manager, profile_sync_services[0]); if (channel) channel->sync_handler(channel->context, profile_sync_services, 1); #endif return KAA_ERR_NONE; }
kaa_error_t kaa_configuration_manager_handle_server_sync(kaa_configuration_manager_t *self , kaa_platform_message_reader_t *reader , uint32_t extension_options , size_t extension_length) { KAA_RETURN_IF_NIL2(self, reader, KAA_ERR_BADPARAM); KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Received configuration server sync: options %u, payload size %u", extension_options, extension_length); if (extension_length >= sizeof(uint32_t)) { self->status->config_seq_n = KAA_NTOHL(*((uint32_t *) reader->current)); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Configuration state sequence number is '%d'", self->status->config_seq_n); reader->current += sizeof(uint32_t); if (extension_options & KAA_CONFIGURATION_BODY_PRESENT) { uint32_t body_size = KAA_NTOHL(*((uint32_t *) reader->current)); reader->current += sizeof(uint32_t); KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Received configuration body, size '%u' ", body_size); const char* body = reader->current; kaa_error_t error = kaa_platform_message_skip(reader, kaa_aligned_size_get(body_size)); if (error) { KAA_LOG_ERROR(self->logger, error, "Failed to read configuration body, size %u", body_size); return error; } #if KAA_CONFIGURATION_DELTA_SUPPORT #else if (self->root_record) self->root_record->destroy(self->root_record); self->root_record = kaa_configuration_manager_deserialize(body, body_size); if (!self->root_record) { KAA_LOG_ERROR(self->logger, KAA_ERR_READ_FAILED, "Failed to deserialize configuration body, size %u", body_size); return KAA_ERR_READ_FAILED; } kaa_error_t err = ext_calculate_sha_hash(body, body_size, self->configuration_hash); if (err) { KAA_LOG_WARN(self->logger, err, "Failed to calculate configuration body hash"); return err; } ext_configuration_store(body, body_size); #endif if (self->root_receiver.on_configuration_updated) self->root_receiver.on_configuration_updated(self->root_receiver.context, self->root_record); kaa_transport_channel_interface_t *channel = kaa_channel_manager_get_transport_channel(self->channel_manager, configuration_sync_services[0]); if (channel) channel->sync_handler(channel->context, configuration_sync_services, 1); } } return KAA_ERR_NONE; }
kaa_error_t kaa_profile_force_sync(kaa_profile_manager_t *self) { if (!self) return KAA_ERR_BADPARAM; kaa_transport_channel_interface_t *channel = kaa_channel_manager_get_transport_channel( self->channel_manager, KAA_EXTENSION_PROFILE); if (!channel) { return KAA_ERR_NOT_FOUND; } channel->sync_handler(channel->context, profile_sync_services, 1); return KAA_ERR_NONE; }
kaa_error_t kaa_start(kaa_context_t *kaa_context) { KAA_RETURN_IF_NIL(kaa_context, KAA_ERR_BADPARAM); KAA_LOG_INFO(kaa_context->logger, KAA_ERR_NONE, "Going to start Kaa endpoint"); kaa_transport_channel_interface_t *bootstrap_channel = kaa_channel_manager_get_transport_channel( kaa_context->channel_manager, KAA_EXTENSION_BOOTSTRAP); if (bootstrap_channel) { const kaa_extension_id bootstrap_service[] = { KAA_EXTENSION_BOOTSTRAP }; kaa_error_t error = bootstrap_channel->sync_handler(bootstrap_channel->context, bootstrap_service, 1); if (error) { KAA_LOG_ERROR(kaa_context->logger, error, "Failed to sync Bootstrap service. Try again later"); return error; } } else { KAA_LOG_FATAL(kaa_context->logger, KAA_ERR_NOT_FOUND, "Could not find Bootstrap transport channel"); return KAA_ERR_NOT_FOUND; } return KAA_ERR_NONE; }