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; }
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; kaa_error_t error = kaa_log_create(&logger, KAA_MAX_LOG_MESSAGE_LENGTH, KAA_MAX_LOG_LEVEL, NULL); // TODO: make log destination configurable if (error) { return error; } KAA_LOG_INFO(logger, KAA_ERR_NONE, "Kaa SDK version %s, commit hash %s", KAA_BUILD_VERSION, KAA_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_context_p = NULL; return error; } // Initialize endpoint identity uint8_t *sha1 = NULL; size_t sha1_size = 0; error = kaa_init_keys(); if (error) { KAA_LOG_ERROR(logger, error, "Failed to initialize keys"); return error; } ext_get_sha1_public(&sha1, &sha1_size); ext_copy_sha_hash((*kaa_context_p)->status->status_instance->endpoint_public_key_hash, sha1); return kaa_status_set_updated((*kaa_context_p)->status->status_instance, true); }
int kaa_demo_event_loop() { kaa_error_t error_code = kaa_start(kaa_context_); if (error_code) { KAA_LOG_FATAL(kaa_context_->logger, error_code,"Failed to start Kaa workflow"); return -1; } uint16_t select_timeout; error_code = kaa_tcp_channel_get_max_timeout(&operations_channel, &select_timeout); if (error_code) { KAA_LOG_FATAL(kaa_context_->logger, error_code,"Failed to get Operations channel keepalive timeout"); return -1; } if (select_timeout > KAA_DEMO_LOG_GENERATION_FREQUENCY) { select_timeout = KAA_DEMO_LOG_GENERATION_FREQUENCY; } fd_set read_fds, write_fds, except_fds; int ops_fd = 0, bootstrap_fd = 0; struct timeval select_tv = { 0, 0 }; int max_fd = 0; while (log_record_counter < KAA_DEMO_LOGS_TO_SEND) { FD_ZERO(&read_fds); FD_ZERO(&write_fds); FD_ZERO(&except_fds); max_fd = 0; kaa_tcp_channel_get_descriptor(&operations_channel, &ops_fd); if (max_fd < ops_fd) max_fd = ops_fd; kaa_tcp_channel_get_descriptor(&bootstrap_channel, &bootstrap_fd); if (max_fd < bootstrap_fd) max_fd = bootstrap_fd; if (kaa_tcp_channel_is_ready(&operations_channel, FD_READ)) FD_SET(ops_fd, &read_fds); if (kaa_tcp_channel_is_ready(&operations_channel, FD_WRITE)) FD_SET(ops_fd, &write_fds); if (kaa_tcp_channel_is_ready(&bootstrap_channel, FD_READ)) FD_SET(bootstrap_fd, &read_fds); if (kaa_tcp_channel_is_ready(&bootstrap_channel, FD_WRITE)) FD_SET(bootstrap_fd, &write_fds); select_tv.tv_sec = select_timeout; select_tv.tv_usec = 0; int poll_result = select(max_fd + 1, &read_fds, &write_fds, NULL, &select_tv); if (poll_result == 0) { kaa_demo_add_log_record(); kaa_tcp_channel_check_keepalive(&operations_channel); kaa_tcp_channel_check_keepalive(&bootstrap_channel); } else if (poll_result > 0) { if (bootstrap_fd >= 0) { if (FD_ISSET(bootstrap_fd, &read_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing IN event for the Bootstrap client socket %d", bootstrap_fd); error_code = kaa_tcp_channel_process_event(&bootstrap_channel, FD_READ); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, KAA_ERR_NONE,"Failed to process IN event for the Bootstrap client socket %d", bootstrap_fd); } if (FD_ISSET(bootstrap_fd, &write_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing OUT event for the Bootstrap client socket %d", bootstrap_fd); error_code = kaa_tcp_channel_process_event(&bootstrap_channel, FD_WRITE); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code,"Failed to process OUT event for the Bootstrap client socket %d", bootstrap_fd); } } if (ops_fd >= 0) { if (FD_ISSET(ops_fd, &read_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing IN event for the Operations client socket %d", ops_fd); error_code = kaa_tcp_channel_process_event(&operations_channel, FD_READ); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code,"Failed to process IN event for the Operations client socket %d", ops_fd); } if (FD_ISSET(ops_fd, &write_fds)) { KAA_LOG_DEBUG(kaa_context_->logger, KAA_ERR_NONE,"Processing OUT event for the Operations client socket %d", ops_fd); error_code = kaa_tcp_channel_process_event(&operations_channel, FD_WRITE); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code,"Failed to process OUT event for the Operations client socket %d", ops_fd); } } } else { KAA_LOG_ERROR(kaa_context_->logger, KAA_ERR_BAD_STATE,"Failed to poll descriptors: %s", strerror(errno)); return -1; } } return 0; }