/* * Initializes Kaa log collector. */ kaa_error_t kaa_log_collector_init() { kaa_error_t error_code = ext_unlimited_log_storage_create(&log_storage_context, kaa_context_->logger); if (error_code) { KAA_LOG_ERROR(kaa_context_->logger, error_code, "Failed to create log storage"); return error_code; } error_code = ext_log_upload_strategy_by_volume_create(&log_upload_strategy_context , kaa_context_->channel_manager , kaa_context_->bootstrap_manager); if (error_code) { KAA_LOG_ERROR(kaa_context_->logger, error_code, "Failed to create log upload strategy"); return error_code; } error_code = ext_log_upload_strategy_by_volume_set_threshold_count(log_upload_strategy_context , KAA_DEMO_UPLOAD_COUNT_THRESHOLD); error_code = kaa_logging_init(kaa_context_->log_collector , log_storage_context , log_upload_strategy_context); return error_code; }
kaa_error_t kaa_client_init_operations_channel(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Start operations channel initialization"); error_code = kaa_tcp_channel_create(&kaa_client->operations_channel , kaa_client->kaa_context->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Operations channel initialization failed"); return error_code; } KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Initializing Kaa SDK Operations channel added to transport channel manager"); error_code = kaa_channel_manager_add_transport_channel(kaa_client->kaa_context->channel_manager , &kaa_client->operations_channel , &kaa_client->operations_channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error during Kaa operations channel setting as transport"); return error_code; } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Operations channel initialized successfully"); print_mem_stat(kaa_client); return error_code; }
kaa_error_t kaa_configuration_manager_request_serialize(kaa_configuration_manager_t *self, kaa_platform_message_writer_t *writer) { KAA_RETURN_IF_NIL2(self, writer, KAA_ERR_BADPARAM); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Going to serialize client configuration sync"); uint32_t payload_size = sizeof(uint32_t) + SHA_1_DIGEST_LENGTH; kaa_platform_message_writer_t tmp_writer = *writer; kaa_error_t error_code = kaa_platform_message_write_extension_header(&tmp_writer, KAA_CONFIGURATION_EXTENSION_TYPE, KAA_CONFIGURATION_ALL_FLAGS, payload_size); if (error_code) { KAA_LOG_ERROR(self->logger, error_code, "Failed to write configuration extension header"); return KAA_ERR_WRITE_FAILED; } *((uint32_t *) tmp_writer.current) = KAA_HTONL(self->status->config_seq_n); tmp_writer.current += sizeof(uint32_t); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Configuration state sequence number is '%d'", self->status->config_seq_n); error_code = kaa_platform_message_write_aligned(&tmp_writer, self->configuration_hash, SHA_1_DIGEST_LENGTH); if (error_code) { KAA_LOG_ERROR(self->logger, error_code, "Failed to write configuration hash"); return KAA_ERR_WRITE_FAILED; } *writer = tmp_writer; return KAA_ERR_NONE; }
void esp8266_tcp_receive_fn(void *context, int id, const uint8 *buffer, const int receive_size) { if (!context) return; kaa_client_t *kaa_client = (kaa_client_t *)context; kaa_error_t error; if (buffer) { if (receive_size > 0) { KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa channel(0x%08X) receive %d bytes", kaa_client->channel_id, receive_size); error = kaa_tcp_channel_read_bytes(&kaa_client->channel, buffer, receive_size); if (error) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error, "Kaa channel error reading bytes"); } } } if (receive_size == -1) { KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa channel(0x%08X) connection termined by peer", kaa_client->channel_id); error = kaa_client_channel_error(kaa_client); if (error) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error, "Kaa channel error dropping connection"); } } }
void kaa_demo_add_log_record() { ++log_record_counter; printf("Going to add %zuth log record\n", log_record_counter); kaa_user_log_record_t *log_record = kaa_logging_log_data_create(); if (!log_record) { KAA_LOG_ERROR(kaa_context_->logger, KAA_ERR_NOT_INITIALIZED, "Failed to allocate log record"); return; } log_record->level = ENUM_LEVEL_INFO; log_record->tag = kaa_string_move_create(KAA_DEMO_LOG_TAG, NULL); size_t log_message_buffer_size = strlen(KAA_DEMO_LOG_MESSAGE) + sizeof(log_record_counter); char log_message_buffer[log_message_buffer_size]; snprintf(log_message_buffer, log_message_buffer_size, "%s%zu", KAA_DEMO_LOG_MESSAGE, log_record_counter); log_record->message = kaa_string_copy_create(log_message_buffer); kaa_error_t error_code = kaa_logging_add_record(kaa_context_->log_collector, log_record); if (error_code) KAA_LOG_ERROR(kaa_context_->logger, error_code, "Failed to add log record"); log_record->destroy(log_record); }
kaa_error_t kaa_client_init_channel(kaa_client_t *kaa_client, kaa_client_channel_type_t channel_type) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Initializing channel...."); switch (channel_type) { case KAA_CLIENT_CHANNEL_TYPE_BOOTSTRAP: error_code = kaa_tcp_channel_create(&kaa_client->channel, kaa_client->kaa_context->logger, BOOTSTRAP_SERVICE, BOOTSTRAP_SERVICE_COUNT); break; case KAA_CLIENT_CHANNEL_TYPE_OPERATIONS: error_code = kaa_tcp_channel_create(&kaa_client->channel , kaa_client->kaa_context->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); break; } if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error initializing channel %d", channel_type); return error_code; } error_code = kaa_tcp_channel_set_keepalive_timeout(&kaa_client->channel, 120); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error set keepalive"); } error_code = kaa_channel_manager_add_transport_channel(kaa_client->kaa_context->channel_manager, &kaa_client->channel, &kaa_client->channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error register channel %d as transport", channel_type); return error_code; } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(type=%d,id=%08X) initialized successfully" , channel_type, kaa_client->channel_id); char *hostname = NULL; size_t hostname_size = 0; uint16_t port = 0; error_code = kaa_tcp_channel_get_access_point(&kaa_client->channel, &hostname, &hostname_size, &port); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Kaa tcp channel get access point failed"); } else if (hostname_size > 0){ char *n_hostname = strndup(hostname, hostname_size); KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Channel(type=%d,id=%08X) destination %s:%d", channel_type , kaa_client->channel_id, n_hostname, port); free(n_hostname); } return error_code; }
void DefaultOperationTcpChannel::sync(TransportType type) { KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); if (isShutdown_) { KAA_LOG_WARN(boost::format("Channel [%1%] can't sync: channel is shut down") % getId()); return; } if (isPaused_) { KAA_LOG_WARN(boost::format("Channel [%1%] can't sync: channel is paused") % getId()); return; } const auto& suppportedTypes = getSupportedTransportTypes(); auto it = suppportedTypes.find(type); if (it == suppportedTypes.end() || it->second == ChannelDirection::DOWN) { KAA_LOG_ERROR(boost::format("Channel [%1%] ignore sync: unsupported transport type %2%") % getId() % LoggingUtils::toString(type)); return; } if (!currentServer_) { KAA_LOG_DEBUG(boost::format("Channel [%1%] can't sync: server is null") % getId()); return; } if (isFirstResponseReceived_) { KAA_MUTEX_UNLOCKING("channelGuard_"); KAA_UNLOCK(lock); KAA_MUTEX_UNLOCKED("channelGuard_"); std::map<TransportType, ChannelDirection> syncTypes; syncTypes.insert(std::make_pair(type, it->second)); for (const auto& typeIt : suppportedTypes) { if (typeIt.first != type) { syncTypes.insert(std::make_pair(typeIt.first, ChannelDirection::DOWN)); } } boost::system::error_code errorCode = sendKaaSync(syncTypes); if (errorCode) { KAA_LOG_ERROR(boost::format("Channel [%1%] failed to sync: %2%") % getId() % errorCode.message()); onServerFailed(); } } else { KAA_LOG_DEBUG(boost::format("Channel [%1%] can't sync: waiting for CONNACK + KAASYNC") % getId()); isPendingSyncRequest_ = true; } }
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; }
/* * Initializes Kaa log collector. */ kaa_error_t kaa_log_collector_init(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM) kaa_error_t error_code = ext_unlimited_log_storage_create(&kaa_client->log_storage_context , kaa_client->kaa_context->logger); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to create log storage"); return error_code; } error_code = ext_log_upload_strategy_create(kaa_client->kaa_context ,&kaa_client->log_upload_strategy_context , KAA_LOG_UPLOAD_VOLUME_STRATEGY); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to create log upload strategy"); return error_code; } // Due to unknown problems with networking via ESP8266, some server responses are lost. // It leads to log delivery timeouts. error_code = ext_log_upload_strategy_set_upload_timeout(kaa_client->log_upload_strategy_context , KAA_DEMO_TWO_DAYS_UPLOAD_TIMEOUT); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to create log upload strategy by volume set upload timeout to %d", KAA_DEMO_TWO_DAYS_UPLOAD_TIMEOUT); return error_code; } error_code = ext_log_upload_strategy_et_threshold_count(kaa_client->log_upload_strategy_context , KAA_DEMO_UPLOAD_COUNT_THRESHOLD); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to create log upload strategy by volume set threshold count to %d", KAA_DEMO_UPLOAD_COUNT_THRESHOLD); return error_code; } error_code = kaa_logging_init(kaa_client->kaa_context->log_collector , kaa_client->log_storage_context , kaa_client->log_upload_strategy_context); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code,"Failed to logging init"); return error_code; } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Log collector init complete"); return error_code; }
/* * Initializes Kaa log collector. */ kaa_error_t kaa_log_collector_init(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = ext_unlimited_log_storage_create( &kaa_client->log_storage_context, kaa_client->kaa_context->logger); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to create log storage"); return error_code; } error_code = ext_log_upload_strategy_create(kaa_client->kaa_context ,&kaa_client->log_upload_strategy_context , KAA_LOG_UPLOAD_VOLUME_STRATEGY); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to create log upload strategy"); return error_code; } error_code = ext_log_upload_strategy_set_threshold_count(kaa_client->log_upload_strategy_context , KAA_DEMO_UPLOAD_COUNT_THRESHOLD); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to create log upload strategy by volume set threshold count to %d", KAA_DEMO_UPLOAD_COUNT_THRESHOLD); return error_code; } error_code = kaa_logging_init(kaa_client->kaa_context->log_collector , kaa_client->log_storage_context , kaa_client->log_upload_strategy_context); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Failed to logging init"); return error_code; } KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Log collector init complete"); return error_code; }
static kaa_error_t kaa_client_sync_serialize(kaa_platform_protocol_t *self, const kaa_extension_id services[], size_t services_count, uint8_t *buffer, size_t *size) { kaa_platform_message_writer_t writer = KAA_MESSAGE_WRITER(buffer, *size); uint16_t total_services_count = services_count + 1 /* Meta extension */; kaa_error_t error_code = kaa_platform_message_header_write(&writer, KAA_PLATFORM_PROTOCOL_ID, KAA_PLATFORM_PROTOCOL_VERSION); if (error_code) { KAA_LOG_ERROR(self->logger, error_code, "Failed to write the client sync header"); goto fail; } uint16_t *extension_count_p = (uint16_t *)writer.current; writer.current += KAA_PROTOCOL_EXTENSIONS_COUNT_SIZE; // TODO: static assert KAA_PROTOCOL_EXTENSIONS_COUNT_SIZE == sizeof(uint16_t) error_code = kaa_meta_data_request_serialize(self, &writer, self->request_id); if (error_code) { goto fail; } while (!error_code && services_count--) { size_t size_required = writer.end - writer.current; bool need_resync = false; error_code = kaa_extension_request_serialize(services[services_count], self->request_id, writer.current, &size_required, &need_resync); if (error_code) { KAA_LOG_ERROR(self->logger, error_code, "Failed to serialize the '%d' extension", services[services_count]); continue; } if (!need_resync) { --total_services_count; continue; } writer.current += size_required; } *extension_count_p = KAA_HTONS(total_services_count); *size = writer.current - writer.begin; fail: return error_code; }
kaa_error_t kaa_client_init_channel(kaa_client_t *kaa_client, kaa_client_channel_type_t channel_type) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; KAA_LOG_TRACE(kaa_client->context->logger, KAA_ERR_NONE, "Initializing channel...."); switch (channel_type) { case KAA_CLIENT_CHANNEL_TYPE_BOOTSTRAP: error_code = kaa_tcp_channel_create(&kaa_client->channel , kaa_client->context->logger , BOOTSTRAP_SERVICE , BOOTSTRAP_SERVICE_COUNT); break; case KAA_CLIENT_CHANNEL_TYPE_OPERATIONS: error_code = kaa_tcp_channel_create(&kaa_client->channel , kaa_client->context->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); break; } if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to create transport channel, type %d", channel_type); return error_code; } error_code = kaa_tcp_channel_set_socket_events_callback(&kaa_client->channel, &on_kaa_tcp_channel_event, kaa_client); if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to set socket events callback, channel type %d", channel_type); return error_code; } error_code = kaa_channel_manager_add_transport_channel(kaa_client->context->channel_manager , &kaa_client->channel , &kaa_client->channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to add transport channel, type %d", channel_type); return error_code; } KAA_LOG_INFO(kaa_client->context->logger, KAA_ERR_NONE, "Channel [0x%08X] initialized successfully (type %d)" , kaa_client->channel_id, channel_type); return error_code; }
void DefaultOperationTcpChannel::startThreads() { if (!ioThreads_.empty()) { return; } KAA_LOG_TRACE(boost::format("Channel [%1%] starting %2% IO service threads...") % getId() % THREADPOOL_SIZE); ioThreads_.reserve(THREADPOOL_SIZE); for (std::size_t i = 0; i < THREADPOOL_SIZE; ++i) { ioThreads_.emplace_back( [this]() { try { KAA_LOG_TRACE(boost::format("Channel [%1%] running IO service") % getId()); // Blocking call. io_.run(); KAA_LOG_TRACE(boost::format("Channel [%1%] IO service stopped") % getId()); } catch (std::exception& e) { KAA_LOG_ERROR(boost::format("Channel [%1%] unexpected stop of IO service: %2%") % getId() % e.what()); //TODO: http://jira.kaaproject.org/browse/KAA-1321 // Reset IO service and, perhaps, notify // the channel manager about a transport failover. } }); } }
void DefaultOperationTcpChannel::onPingTimeout(const boost::system::error_code& err) { if (!err) { sendPingRequest(); } else { KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); if (err != boost::asio::error::operation_aborted && isConnected_) { KAA_MUTEX_UNLOCKING("channelGuard_"); KAA_UNLOCK(channelLock); KAA_MUTEX_UNLOCKED("channelGuard_") KAA_LOG_ERROR(boost::format("Channel [%1%] failed to process PING: %2%") % getId() % err.message()); onServerFailed(); return; } else { KAA_LOG_DEBUG(boost::format("Channel [%1%] PING timer aborted") % getId()); return; } } KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); if (isConnected_) { setPingTimer(); } }
void DefaultOperationTcpChannel::onConnack(const ConnackMessage& message) { KAA_LOG_DEBUG(boost::format("Channel [%1%] received Connack: status %2%") % getId() % message.getMessage()); switch (message.getReturnCode()) { case ConnackReturnCode::ACCEPTED: break; case ConnackReturnCode::REFUSE_VERIFICATION_FAILED: case ConnackReturnCode::REFUSE_BAD_CREDENTIALS: KAA_LOG_WARN(boost::format("Channel [%1%] failed server authentication: %2%") % getId() % ConnackMessage::returnCodeToString(message.getReturnCode())); onServerFailed(KaaFailoverReason::ENDPOINT_NOT_REGISTERED); break; default: KAA_LOG_ERROR(boost::format("Channel [%1%] failed to connect to server: %2%") % getId() % message.getMessage()); onServerFailed(KaaFailoverReason::CURRENT_OPERATIONS_SERVER_NA); break; } }
kaa_error_t on_kaa_tcp_channel_event(void *context , kaa_tcp_channel_event_t event_type , kaa_fd_t fd) { KAA_RETURN_IF_NIL(context, KAA_ERR_BADPARAM); kaa_client_t *self = (kaa_client_t *)context; switch (event_type) { case SOCKET_CONNECTED: KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Bootstrap socket(%d) Connected", fd); if (self->bootstrap_state == BOOTSRAP_UNDEF) self->bootstrap_state = BOOTSRAP_STARTED; break; case SOCKET_DISCONNECTED: KAA_LOG_INFO(self->kaa_context->logger, KAA_ERR_NONE, "Bootstrap socket Disconnected"); if (self->bootstrap_state == BOOTSRAP_STARTED) self->bootstrap_state = BOOTSRAP_FINISHED; break; default: KAA_LOG_ERROR(self->kaa_context->logger, KAA_ERR_NONE, "Bootstrap socket Error"); self->bootstrap_state = BOOTSRAP_UNDEF; break; } return KAA_ERR_NONE; }
kaa_error_t kaa_platform_protocol_alloc_serialize_client_sync(kaa_platform_protocol_t *self, const kaa_extension_id *services, size_t services_count, uint8_t **buffer, size_t *buffer_size) { if (!self || !buffer || !buffer_size) { return KAA_ERR_BADPARAM; } if (!services || services_count == 0) { return KAA_ERR_BADDATA; } *buffer_size = 0; kaa_error_t error = kaa_platform_protocol_serialize_client_sync(self, services, services_count, NULL, buffer_size); // TODO(982): assert error != KAA_ERR_NONE if (error != KAA_ERR_BUFFER_IS_NOT_ENOUGH) { return error; } *buffer = KAA_MALLOC(*buffer_size); if (!*buffer) { KAA_LOG_ERROR(self->logger, KAA_ERR_NOMEM, "No memory for buffer"); return KAA_ERR_NOMEM; } return kaa_platform_protocol_serialize_client_sync(self, services, services_count, *buffer, buffer_size); }
void AbstractHttpChannel::sync(TransportType type) { const auto& supportedTypes = getSupportedTransportTypes(); auto it = supportedTypes.find(type); if (it == supportedTypes.end() || it->second == ChannelDirection::DOWN) { KAA_LOG_ERROR(boost::format("Channel [%1%] unsupported transport type '%2%'") % getId() % LoggingUtils::toString(type)); return; } KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); if (!currentServer_) { KAA_LOG_WARN(boost::format("Channel [%1%] can't sync: server is null") % getId()); return; } processTypes(std::map<TransportType, ChannelDirection>({ { type, it->second } }) #ifdef KAA_THREADSAFE , lock #endif ); }
static kaa_error_t kaa_client_sync_get_size(kaa_platform_protocol_t *self, const kaa_extension_id *extensions, const size_t extension_count, size_t *expected_size) { // TODO(KAA-982): Use assert here if (!self || !extensions || !expected_size || extension_count == 0) { return KAA_ERR_BADPARAM; } *expected_size = KAA_PROTOCOL_MESSAGE_HEADER_SIZE + kaa_meta_data_request_size; for (size_t i = 0; i < extension_count; ++i) { size_t extension_size = 0; kaa_error_t error = get_extension_request_size(extensions[i], &extension_size); if (error && error != KAA_ERR_NOT_FOUND) { KAA_LOG_ERROR(self->logger, error, "Failed to query extension size for %u", extensions[i]); return error; } *expected_size += extension_size; } return KAA_ERR_NONE; }
kaa_error_t kaa_client_create(kaa_client_t **client, kaa_client_props_t *props) { (void)props; KAA_RETURN_IF_NIL(client, KAA_ERR_BADPARAM); kaa_client_t *self = (kaa_client_t*)KAA_CALLOC(1, sizeof(kaa_client_t)); KAA_RETURN_IF_NIL(self, KAA_ERR_NOMEM); kaa_error_t error_code = kaa_init(&self->context); if(error_code) { printf("Error initialising kaa_context\n"); kaa_client_destroy(self); return error_code; } self->operate = true; #ifndef KAA_DISABLE_FEATURE_LOGGING error_code = kaa_log_collector_init(self); if (error_code) { KAA_LOG_ERROR(self->context->logger, error_code, "Failed to init Kaa log collector, error %d", error_code); kaa_client_destroy(self); return error_code; } #endif KAA_LOG_INFO(self->context->logger,KAA_ERR_NONE, "Kaa client initiallized"); *client = self; return error_code; }
void DefaultOperationTcpChannel::onReadEvent(const boost::system::error_code& err) { if (!err) { std::ostringstream responseStream; responseStream << responseBuffer_.get(); const auto& responseStr = responseStream.str(); try { if (responseStr.empty()) { KAA_LOG_ERROR(boost::format("Channel [%1%] no data read from socket") % getId()); } else { responseProcessor.processResponseBuffer(responseStr.data(), responseStr.size()); } } catch (const TransportRedirectException& exception) { KAA_LOG_INFO(boost::format("Channel [%1%] received REDIRECT response") % getId()); return; } catch (const KaaException& exception) { KAA_LOG_ERROR(boost::format("Channel [%1%] failed to process data: %2%") % getId() % exception.what()); onServerFailed(); } } else { KAA_LOG_WARN(boost::format("Channel [%1%] socket error: %2%") % getId() % err.message()); KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); if (err != boost::asio::error::operation_aborted && isConnected_) { KAA_MUTEX_UNLOCKING("channelGuard_"); KAA_UNLOCK(channelLock); KAA_MUTEX_UNLOCKED("channelGuard_") onServerFailed(); return; } else { KAA_LOG_DEBUG(boost::format("Channel [%1%] socket operations aborted") % getId()); return; } } KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(channelLock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); if (isConnected_) { readFromSocket(); } }
kaa_error_t kaa_client_start(kaa_client_t *kaa_client , external_process_fn external_process , void *external_process_context , kaa_time_t max_delay) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa client starting ..."); print_mem_stat(kaa_client); kaa_error_t error_code = KAA_ERR_NONE; error_code = kaa_tcp_channel_create(&kaa_client->bootstrap_channel , kaa_client->kaa_context->logger , BOOTSTRAP_SERVICE , BOOTSTRAP_SERVICE_COUNT); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error during Kaa bootstrap channel creation"); return error_code; } error_code = kaa_tcp_channel_set_socket_events_callback(&kaa_client->bootstrap_channel, on_kaa_tcp_channel_event, (void*)kaa_client); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error setting callback bootstrap channel"); return error_code; } KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa client - bootstrap channel initialized"); print_mem_stat(kaa_client); error_code = kaa_channel_manager_add_transport_channel(kaa_client->kaa_context->channel_manager , &kaa_client->bootstrap_channel , &kaa_client->bootstrap_channel_id); if (error_code) { KAA_LOG_ERROR(kaa_client->kaa_context->logger, error_code, "Error setting bootstrap channel setting as transport"); return error_code; } //Push running thread sndc_sem_post(&kaa_client->start_semophore); KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Kaa client started"); return KAA_ERR_NONE; }
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_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; }
void EventManager::doSync() { if (eventTransport_) { eventTransport_->sync(); } else { KAA_LOG_ERROR("Failed to sync: event transport is not set"); throw TransportNotFoundException("Event transport not found"); } }
void KaaClient::setFailoverStrategy(IFailoverStrategyPtr strategy) { if (!strategy) { KAA_LOG_ERROR("Failed to set failover strategy: bad data"); throw KaaException("Bad failover strategy"); } KAA_LOG_INFO("New failover strategy was set"); failoverStrategy_ = strategy; channelManager_->setFailoverStrategy(failoverStrategy_); }
kaa_error_t kaa_check_readiness(kaa_context_t *kaa_context) { KAA_RETURN_IF_NIL(kaa_context, KAA_ERR_BADPARAM); if (!kaa_profile_manager_is_profile_set(kaa_context->profile_manager)) { KAA_LOG_ERROR(kaa_context->logger, KAA_ERR_PROFILE_IS_NOT_SET, "Profile isn't set"); return KAA_ERR_PROFILE_IS_NOT_SET; } return KAA_ERR_NONE; }
void LogCollector::setUploadStrategy(ILogUploadStrategyPtr strategy) { if (!strategy) { KAA_LOG_ERROR("Failed to set log upload strategy: bad data"); throw KaaException("Bad log upload strategy"); } KAA_LOG_INFO("New log upload strategy was set"); uploadStrategy_ = strategy; }
kaa_error_t kaa_deinit(kaa_context_t *kaa_context) { KAA_RETURN_IF_NIL(kaa_context, KAA_ERR_BADPARAM); kaa_logger_t *logger = kaa_context->logger; kaa_error_t error = kaa_context_destroy(kaa_context); if (error) KAA_LOG_ERROR(logger, error, "Failed to destroy Kaa context"); kaa_log_destroy(logger); return error; }
void BootstrapManager::notifyChannelManangerAboutServer(const OperationsServers& servers) { if (channelManager_ == nullptr) { KAA_LOG_ERROR("Channel manager was not specified"); return; } for (const auto& connectionInfo : servers) { channelManager_->onTransportConnectionInfoUpdated(connectionInfo); } }