kaa_error_t kaa_client_deinit_bootstrap_channel(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); KAA_LOG_TRACE(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Bootstrap channel deinitialization starting ...."); print_mem_stat(kaa_client); kaa_error_t error_code = KAA_ERR_NONE; error_code = kaa_channel_manager_remove_transport_channel( kaa_client->kaa_context->channel_manager,kaa_client->bootstrap_channel_id); if (error_code) { KAA_LOG_TRACE(kaa_client->kaa_context->logger, error_code, "Bootstrap channel error removing from channel manager"); return error_code; } kaa_client->bootstrap_channel.context = NULL; kaa_client->bootstrap_channel.destroy = NULL; kaa_client->bootstrap_channel.get_protocol_id = NULL; kaa_client->bootstrap_channel.get_supported_services = NULL; kaa_client->bootstrap_channel.init = NULL; kaa_client->bootstrap_channel.set_access_point = NULL; kaa_client->bootstrap_channel.sync_handler = NULL; kaa_client->bootstrap_state = BOOTSRAP_UNDEF; KAA_LOG_INFO(kaa_client->kaa_context->logger, KAA_ERR_NONE, "Bootstrap channel deinitialized"); return error_code; }
RsaEncoderDecoder::RsaEncoderDecoder( const Botan::MemoryVector<boost::uint8_t>& pubKey, const std::string& privKey, const Botan::MemoryVector<boost::uint8_t>& remoteKey) : pubKey_(NULL), privKey_(NULL), remoteKey_(NULL), sessionKey_(KeyUtils().generateSessionKey(16)) { KAA_LOG_TRACE("Creating MessageEncoderDecoder with following parameters: "); if (!pubKey.empty()) { Botan::DataSource_Memory pubMem(pubKey); pubKey_.reset(Botan::X509::load_key(pubMem)); } KAA_LOG_TRACE(boost::format("PublicKey: %1%") % ( pubKey_ ? LoggingUtils::ByteArrayToString( pubKey_->x509_subject_public_key().begin(), pubKey_->x509_subject_public_key().size()) : "empty")); if (!privKey.empty()) { Botan::DataSource_Memory privMem(privKey); privKey_.reset(Botan::PKCS8::load_key(privMem, rng_)); } if (!remoteKey.empty()) { Botan::DataSource_Memory remoteMem(remoteKey); remoteKey_.reset(Botan::X509::load_key(remoteMem)); } KAA_LOG_TRACE(boost::format("RemotePublicKey: %1%") % ( remoteKey_ ? LoggingUtils::ByteArrayToString( remoteKey_->x509_subject_public_key().begin(), remoteKey_->x509_subject_public_key().size()) : "empty")); }
kaa_error_t kaa_platform_protocol_serialize_client_sync(kaa_platform_protocol_t *self , const kaa_serialize_info_t *info , char **buffer , size_t *buffer_size) { KAA_RETURN_IF_NIL4(self, info, buffer, buffer_size, KAA_ERR_BADPARAM); KAA_RETURN_IF_NIL3(info->allocator, info->services, info->services_count, KAA_ERR_BADDATA); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Serializing client sync..."); *buffer_size = 0; kaa_error_t error = kaa_client_sync_get_size(self, info->services, info->services_count, buffer_size); KAA_RETURN_IF_ERR(error) KAA_LOG_DEBUG(self->logger, KAA_ERR_NONE, "Going to request sync buffer (size %zu)", *buffer_size); *buffer = info->allocator(info->allocator_context, *buffer_size); if (*buffer) { self->request_id++; error = kaa_client_sync_serialize(self, info->services, info->services_count, *buffer, buffer_size); } else { error = KAA_ERR_WRITE_FAILED; } if (error) { self->request_id--; } else { KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Client sync successfully serialized"); } return error; }
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 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. } }); } }
kaa_error_t kaa_client_deinit_channel(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = KAA_ERR_NONE; if (kaa_client->channel_id == 0) return KAA_ERR_NONE; KAA_LOG_TRACE(kaa_client->context->logger, KAA_ERR_NONE, "Deinitializing channel...."); error_code = kaa_channel_manager_remove_transport_channel(kaa_client->context->channel_manager,kaa_client->channel_id); if (error_code) { KAA_LOG_TRACE(kaa_client->context->logger, error_code, "Bootstrap channel error removing from channel manager"); return error_code; } kaa_client->channel_id = 0; kaa_client->channel_socket_closed = false; kaa_client->channel.context = NULL; kaa_client->channel.destroy = NULL; kaa_client->channel.get_protocol_id = NULL; kaa_client->channel.get_supported_services = NULL; kaa_client->channel.init = NULL; kaa_client->channel.set_access_point = NULL; kaa_client->channel.sync_handler = NULL; KAA_LOG_TRACE(kaa_client->context->logger, KAA_ERR_NONE, "Channel deinitialized successfully"); 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; }
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_meta_data_request_serialize(kaa_platform_protocol_t *self, kaa_platform_message_writer_t *writer, uint32_t request_id) { // TODO(KAA-982): Use assert here if (!self || !self->status || !writer) { return KAA_ERR_BADPARAM; } KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Going to serialize client meta sync"); uint16_t options = TIMEOUT_VALUE | PUBLIC_KEY_HASH_VALUE | PROFILE_HASH_VALUE | APP_TOKEN_VALUE; const size_t payload_length = kaa_meta_data_request_size - KAA_EXTENSION_HEADER_SIZE; kaa_error_t err_code = kaa_platform_message_write_extension_header(writer, KAA_EXTENSION_META_DATA, options, payload_length); if (err_code) { return err_code; } uint32_t request_id_network = KAA_HTONL(request_id); err_code = kaa_platform_message_write(writer, &request_id_network, sizeof(uint32_t)); if (err_code) { return err_code; } uint32_t timeout = KAA_HTONL(KAA_SYNC_TIMEOUT); err_code = kaa_platform_message_write(writer, &timeout, sizeof(timeout)); if (err_code) { return err_code; } err_code = kaa_platform_message_write_aligned(writer, self->status->endpoint_public_key_hash, SHA_1_DIGEST_LENGTH); if (err_code) { return err_code; } err_code = kaa_platform_message_write_aligned(writer, self->status->profile_hash, SHA_1_DIGEST_LENGTH); if (err_code) { return err_code; } err_code = kaa_platform_message_write_aligned(writer, KAA_SDK_TOKEN, KAA_SDK_TOKEN_LENGTH); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Meta sync: payload length '%u', request id '%u'", payload_length, request_id); return err_code; }
std::int32_t EventManager::findEventListeners(const std::list<std::string>& eventFQNs, IFetchEventListenersPtr listener) { if (eventFQNs.empty() || !listener) { KAA_LOG_WARN("Failed to add event listeners request: bad input data"); throw KaaException("Bad event listeners data"); } std::int32_t requestId = UuidGenerator::generateRandomInt(); std::shared_ptr<EventListenersInfo> info(new EventListenersInfo); info->eventFQNs_ = eventFQNs; info->listener_ = listener; { KAA_MUTEX_LOCKING("eventListenersGuard_"); KAA_MUTEX_UNIQUE_DECLARE(eventListenersLock, eventListenersGuard_); KAA_MUTEX_LOCKED("eventListenersGuard_"); eventListenersRequests_.insert(std::make_pair(requestId, info)); KAA_MUTEX_UNLOCKED("eventListenersGuard_"); } KAA_LOG_TRACE("Added event listeners resolving request"); doSync(); return requestId; }
void DefaultOperationTcpChannel::doShutdown() { KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); KAA_LOG_DEBUG(boost::format("Channel [%1%] is shutting down: isShutdown '%2%'") % getId() % boost::io::group(std::boolalpha, isShutdown_)); if (!isShutdown_) { isShutdown_ = true; KAA_MUTEX_UNLOCKING("channelGuard_"); KAA_UNLOCK(lock); KAA_MUTEX_UNLOCKED("channelGuard_"); closeConnection(); KAA_LOG_TRACE(boost::format("Channel [%1%] stopping IO service: isStopped '%2%'") % getId() % boost::io::group(std::boolalpha, io_.stopped())); if (!io_.stopped()) { io_.stop(); } stopThreads(); } }
void DefaultOperationTcpChannel::closeConnection() { KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); bool wasConnected = isConnected_; KAA_LOG_TRACE(boost::format("Channel [%1%] closing connection: isConnected '%2%'") % getId() % boost::io::group(std::boolalpha, wasConnected)); isFirstResponseReceived_ = false; isConnected_ = false; isPendingSyncRequest_ = false; KAA_MUTEX_UNLOCKING("channelGuard_"); KAA_UNLOCK(lock); KAA_MUTEX_UNLOCKED("channelGuard_"); if (wasConnected) { pingTimer_.cancel(); connAckTimer_.cancel(); sendDisconnect(); boost::system::error_code errorCode; sock_->shutdown(boost::asio::ip::tcp::socket::shutdown_both, errorCode); sock_->close(errorCode); responseProcessor.flush(); } }
void DefaultOperationTcpChannel::onServerFailed(KaaFailoverReason failoverReason) { if (isFailoverInProgress_) { KAA_LOG_TRACE(boost::format("Channel [%1%] failover processing already in progress. " "Ignore '%2%' failover") % getId() % LoggingUtils::toString(failoverReason)); return; } isFailoverInProgress_ = true; closeConnection(); KaaFailoverReason finalFailoverReason = failoverReason; if (failoverReason == KaaFailoverReason::CURRENT_OPERATIONS_SERVER_NA) { if (connectivityChecker_ && !connectivityChecker_->checkConnectivity()) { KAA_LOG_INFO(boost::format("Channel [%1%] detected loss of connectivity") % getId()); finalFailoverReason = KaaFailoverReason::NO_CONNECTIVITY; } } auto server = std::dynamic_pointer_cast<ITransportConnectionInfo, IPTransportInfo>(currentServer_); KAA_LOG_WARN(boost::format("Channel [%1%] detected '%2%' failover for %3%") % getId() % LoggingUtils::toString(finalFailoverReason) % LoggingUtils::toString(*server)); channelManager_.onServerFailed(server, finalFailoverReason); }
kaa_error_t kaa_demo_configuration_receiver(void *context, const kaa_root_configuration_t *configuration) { (void) context; KAA_LOG_TRACE(kaa_client_get_context(kaa_client)->logger, KAA_ERR_NONE, "Received configuration data"); kaa_demo_print_configuration_message(configuration); kaa_client_stop(kaa_client); return KAA_ERR_NONE; }
boost::system::error_code DefaultOperationTcpChannel::sendData(const IKaaTcpRequest& request) { boost::system::error_code errorCode; const auto& data = request.getRawMessage(); KAA_LOG_TRACE(boost::format("Channel [%1%] sending data: size %2%") % getId() % data.size()); boost::asio::write(*sock_, boost::asio::buffer(reinterpret_cast<const char *>(data.data()), data.size()), errorCode); return errorCode; }
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; }
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; }
void DefaultOperationTcpChannel::setServer(ITransportConnectionInfoPtr server) { KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); if (isShutdown_) { KAA_LOG_WARN(boost::format("Channel [%1%] ignore new server: channel is shut down") % getId()); return; } if (server->getTransportId() != getTransportProtocolId()) { KAA_LOG_WARN(boost::format("Channel [%1%] ignore new server: unsupported transport %2%") % getId() % LoggingUtils::toString(server->getTransportId())); return; } KAA_LOG_TRACE(boost::format("Channel [%1%] preparing to use new server %2%") % getId() % LoggingUtils::toString(*server)); currentServer_ = std::make_shared<IPTransportInfo>(server); encDec_ = std::make_shared<RsaEncoderDecoder>(clientKeys_.getPublicKey(), clientKeys_.getPrivateKey(), currentServer_->getPublicKey(), context_); isFailoverInProgress_ = false; if (!isPaused_) { KAA_MUTEX_UNLOCKING("channelGuard_"); KAA_UNLOCK(lock); KAA_MUTEX_UNLOCKED("channelGuard_"); closeConnection(); KAA_LOG_TRACE(boost::format("Channel [%1%] scheduling open connection") % getId()); io_.post(std::bind(&DefaultOperationTcpChannel::openConnection, this)); } }
void ConfigurationManager::updateConfiguration(const std::uint8_t* data, const std::uint32_t dataSize) { static AvroByteArrayConverter<KaaRootConfiguration> converter; converter.fromByteArray(data, dataSize, configuration_); configurationHash_ = EndpointObjectHash(data, dataSize); KAA_LOG_TRACE(boost::format("Calculated configuration hash: %1%") % LoggingUtils::ByteArrayToString(configurationHash_.getHashDigest())); }
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 SDK. */ kaa_error_t kaa_sdk_init() { kaa_error_t error_code = kaa_init(&kaa_context_); if (error_code) { printf("Error during kaa context creation %d", error_code); return error_code; } error_code = kaa_log_collector_init(); if (error_code) { KAA_LOG_ERROR(kaa_context_->logger, error_code, "Failed to init Kaa log collector %d", error_code); return error_code; } KAA_LOG_TRACE(kaa_context_->logger, KAA_ERR_NONE, "Adding transport channels"); error_code = kaa_tcp_channel_create(&operations_channel , kaa_context_->logger , OPERATIONS_SERVICES , OPERATIONS_SERVICES_COUNT); KAA_RETURN_IF_ERR(error_code); error_code = kaa_tcp_channel_create(&bootstrap_channel , kaa_context_->logger , BOOTSTRAP_SERVICE , BOOTSTRAP_SERVICE_COUNT); KAA_RETURN_IF_ERR(error_code); error_code = kaa_channel_manager_add_transport_channel(kaa_context_->channel_manager , &bootstrap_channel , NULL); KAA_RETURN_IF_ERR(error_code); error_code = kaa_channel_manager_add_transport_channel(kaa_context_->channel_manager , &operations_channel , NULL); KAA_RETURN_IF_ERR(error_code); KAA_LOG_TRACE(kaa_context_->logger, KAA_ERR_NONE, "Kaa SDK started"); return KAA_ERR_NONE; }
kaa_error_t kaa_meta_data_request_serialize(kaa_platform_protocol_t *self, kaa_platform_message_writer_t* writer, uint32_t request_id) { KAA_RETURN_IF_NIL3(self, self->status, writer, KAA_ERR_BADPARAM); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Going to serialize client meta sync"); uint16_t options = TIMEOUT_VALUE | PUBLIC_KEY_HASH_VALUE | PROFILE_HASH_VALUE | APP_TOKEN_VALUE; size_t payload_length = 0; kaa_error_t err_code = kaa_meta_data_request_get_size(&payload_length); KAA_RETURN_IF_ERR(err_code); payload_length -= KAA_EXTENSION_HEADER_SIZE; err_code = kaa_platform_message_write_extension_header(writer , KAA_META_DATA_EXTENSION_TYPE , options , payload_length); KAA_RETURN_IF_ERR(err_code); uint32_t request_id_network = KAA_HTONL(request_id); err_code = kaa_platform_message_write(writer, &request_id_network, sizeof(uint32_t)); KAA_RETURN_IF_ERR(err_code); uint32_t timeout = KAA_HTONL(KAA_SYNC_TIMEOUT); err_code = kaa_platform_message_write(writer, &timeout, sizeof(timeout)); KAA_RETURN_IF_ERR(err_code); err_code = kaa_platform_message_write_aligned(writer, self->status->endpoint_public_key_hash, SHA_1_DIGEST_LENGTH); KAA_RETURN_IF_ERR(err_code); err_code = kaa_platform_message_write_aligned(writer, self->status->profile_hash, SHA_1_DIGEST_LENGTH); KAA_RETURN_IF_ERR(err_code); err_code = kaa_platform_message_write_aligned(writer, KAA_SDK_TOKEN, KAA_SDK_TOKEN_LENGTH); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Meta sync: payload length '%u', request id '%u'", payload_length, request_id); return err_code; }
kaa_error_t kaa_client_process_channel_disconnected(kaa_client_t *kaa_client) { KAA_RETURN_IF_NIL(kaa_client, KAA_ERR_BADPARAM); kaa_error_t error_code = kaa_tcp_channel_check_keepalive(&kaa_client->channel); if (error_code) { KAA_LOG_ERROR(kaa_client->context->logger, error_code, "Failed to connect channel [0x%08X]", kaa_client->channel_id); } else { KAA_LOG_TRACE(kaa_client->context->logger, error_code, "Channel [0x%08X] successfully connected", kaa_client->channel_id); kaa_client->channel_state = KAA_CLIENT_CHANNEL_STATE_CONNECTED; } return error_code; }
void DefaultOperationTcpChannel::stopThreads() { for (std::size_t i = 0; i < THREADPOOL_SIZE; ++i) { if (ioThreads_[i].joinable()) { ioThreads_[i].join(); } } ioThreads_.clear(); KAA_LOG_TRACE(boost::format("Channel [%1%] %2% IO service threads stopped") % getId() % THREADPOOL_SIZE); }
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 LogCollector::processLogUploadDecision(LogUploadStrategyDecision decision) { switch (decision) { case LogUploadStrategyDecision::UPLOAD: { KAA_LOG_DEBUG("Going to upload logs"); doSync(); break; } case LogUploadStrategyDecision::NOOP: KAA_LOG_TRACE("Nothing to do now"); if (storage_->getStatus().getRecordsCount() > 0) { startLogUploadCheckTimer(); } break; default: KAA_LOG_WARN("Unknown log upload decision"); break; } }
kaa_error_t kaa_platform_protocol_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_size) { return KAA_ERR_BADPARAM; } if (!services || services_count == 0) { return KAA_ERR_BADDATA; } size_t required_buffer_size = 0; kaa_error_t error = kaa_client_sync_get_size(self, services, services_count, &required_buffer_size); if (error) { KAA_LOG_ERROR(self->logger, error, "Failed to get required buffer size"); return error; } if (*buffer_size < required_buffer_size || !buffer) { *buffer_size = required_buffer_size; return KAA_ERR_BUFFER_IS_NOT_ENOUGH; } *buffer_size = required_buffer_size; KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Serializing client sync..."); self->request_id++; error = kaa_client_sync_serialize(self, services, services_count, buffer, buffer_size); if (error) { self->request_id--; return error; } KAA_LOG_INFO(self->logger, KAA_ERR_NONE, "Client sync serialized: request id '%u', payload size '%zu'", self->request_id, *buffer_size); return KAA_ERR_NONE; }
IPTransportInfo::IPTransportInfo(ITransportConnectionInfoPtr connectionInfo) : GenericTransportInfo(connectionInfo->getServerType(), ProtocolMetaData()) { if (!connectionInfo || connectionInfo->getConnectionInfo().empty()) { KAA_LOG_ERROR("Failed to create IP transport data: bad input data"); throw KaaException("Bad connection data"); } serverType_ = connectionInfo->getServerType(); connectionData_ = connectionInfo->getConnectionInfo(); accessPointId_ = connectionInfo->getAccessPointId(); protocolId_ = connectionInfo->getTransportId(); std::uint8_t *data = connectionData_.data(); std::int32_t publicKeyLength = boost::asio::detail::socket_ops::network_to_host_long(*((int32_t *)data)); data += sizeof(std::int32_t); publicKey_ = PublicKey(data, publicKeyLength); data += publicKeyLength; std::int32_t hostLength = boost::asio::detail::socket_ops::network_to_host_long(*((int32_t *)data)); data += sizeof(std::int32_t); host_.assign(data, data + hostLength); data += hostLength; port_ = boost::asio::detail::socket_ops::network_to_host_long(*((int32_t *)data)); std::ostringstream ss; ss << "http://" << host_ << ":" << port_; url_.assign(ss.str()); if ((3 * sizeof(std::int32_t) + publicKeyLength + hostLength) > connectionData_.size()) { KAA_LOG_ERROR("Failed to create IP transport data: less size of input data than needed"); throw KaaException("Bad connection data"); } KAA_LOG_TRACE(boost::format("Create IP transport data: host=%1%, port=%2%, publicKeyLength=%3%") % host_% port_ % publicKey_.size()); }
void EventManager::produceEvent(const std::string& fqn, const std::vector<std::uint8_t>& data, const std::string& target, TransactionIdPtr trxId) { if (fqn.empty()) { KAA_LOG_WARN("Failed to process outgoing event: bad input data"); return; } KAA_LOG_DEBUG(boost::format("Going to produce Event [FQN: %1%, target: %2%, data_size = %3%]") % fqn % (target.empty() ? "broadcast" : target) % data.size()); Event event; event.eventClassFQN = fqn; event.eventData.assign(data.begin(), data.end()); if (target.empty()) { event.target.set_null(); } else { event.target.set_string(target); } if (trxId) { getContainerByTrxId(trxId, context_).push_back(event); return; } KAA_LOG_TRACE(boost::format("New event %1% is produced for %2%") % fqn % target); { KAA_MUTEX_LOCKING("pendingEventsGuard_"); KAA_MUTEX_UNIQUE_DECLARE(eventsLock, pendingEventsGuard_); KAA_MUTEX_LOCKED("pendingEventsGuard_"); pendingEvents_.insert(std::make_pair(currentEventIndex_++, event)); KAA_MUTEX_UNLOCKED("pendingEventsGuard_"); } doSync(); }
void AbstractHttpChannel::setServer(ITransportConnectionInfoPtr server) { if (server->getTransportId() != getTransportProtocolId()) { KAA_LOG_ERROR(boost::format("Channel [%1%] ignored invalid server info") % getId()); return; } KAA_MUTEX_LOCKING("channelGuard_"); KAA_MUTEX_UNIQUE_DECLARE(lock, channelGuard_); KAA_MUTEX_LOCKED("channelGuard_"); KAA_LOG_TRACE(boost::format("Channel [%1%] preparing to use new server %2%") % getId() % LoggingUtils::toString(*server)); currentServer_ = std::make_shared<IPTransportInfo>(server); httpDataProcessor_.setEncoderDecoder( std::make_shared<RsaEncoderDecoder>(clientKeys_.getPublicKey(), clientKeys_.getPrivateKey(), currentServer_->getPublicKey(), context_)); }