static kaa_error_t test_sync_handler(void *context , const kaa_service_t services[] , size_t service_count) { KAA_RETURN_IF_NIL3(context, services, service_count, KAA_ERR_BADPARAM); return KAA_ERR_NONE; }
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; }
kaa_error_t ext_tcp_utils_open_tcp_socket(kaa_fd_t *fd , const kaa_sockaddr_t *destination , kaa_socklen_t destination_size) { KAA_RETURN_IF_NIL3(fd, destination, destination_size, KAA_ERR_BADPARAM); long lNonBlocking = 1; int status; kaa_fd_t sock = socket(destination->sa_family, SOCK_STREAM, 0); if (sock < 0) return KAA_ERR_SOCKET_ERROR; status = setsockopt(sock, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); if ( status < 0 ) { ext_tcp_utils_tcp_socket_close(sock); return KAA_ERR_SOCKET_ERROR; } int err = connect(sock, destination, destination_size); if ( (err < 0 && err != SL_EALREADY) && errno != EINPROGRESS) { ext_tcp_utils_tcp_socket_close(sock); return KAA_ERR_SOCKET_CONNECT_ERROR; } *fd = sock; return KAA_ERR_NONE; }
kaa_error_t ext_tcp_utils_open_tcp_socket(kaa_fd_t *fd, const kaa_sockaddr_t *destination, kaa_socklen_t destination_size) { KAA_RETURN_IF_NIL3(fd, destination, destination_size, KAA_ERR_BADPARAM); kaa_fd_t sock = socket(destination->sa_family, SOCK_STREAM, 0); int flags = lwip_fcntl(sock, F_GETFL, 0); if (flags < 0) { ext_tcp_utils_tcp_socket_close(sock); printf("Error getting sicket access flags\n"); return KAA_ERR_SOCKET_ERROR; } if (lwip_fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) { ext_tcp_utils_tcp_socket_close(sock); printf("Error setting socket flags. fcntl(F_GETFL): %d\n", lwip_fcntl(sock, F_GETFL, 0)); return KAA_ERR_SOCKET_ERROR; } if (connect(sock, destination, destination_size) && errno != EINPROGRESS) { ext_tcp_utils_tcp_socket_close(sock); printf("Error connecting to destination\n"); return KAA_ERR_SOCKET_CONNECT_ERROR; } *fd = sock; return KAA_ERR_NONE; }
kaa_error_t ext_tcp_utils_open_tcp_socket(kaa_fd_t *fd , const kaa_sockaddr_t *destination , kaa_socklen_t destination_size) { KAA_RETURN_IF_NIL3(fd, destination, destination_size, KAA_ERR_BADPARAM); kaa_fd_t sock = socket(destination->sa_family, SOCK_STREAM, 0); if (sock < 0) return KAA_ERR_SOCKET_ERROR; int flags = fcntl(sock, F_GETFL); if (flags < 0) { ext_tcp_utils_tcp_socket_close(sock); return KAA_ERR_SOCKET_ERROR; } if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) < 0) { ext_tcp_utils_tcp_socket_close(sock); return KAA_ERR_SOCKET_ERROR; } if (connect(sock, destination, destination_size) && errno != EINPROGRESS) { ext_tcp_utils_tcp_socket_close(sock); return KAA_ERR_SOCKET_CONNECT_ERROR; } *fd = sock; return KAA_ERR_NONE; }
kaa_error_t kaa_buffer_allocate_space(kaa_buffer_t *buffer_p, char **buffer, size_t *free_size) { KAA_RETURN_IF_NIL3(buffer_p, buffer, free_size, KAA_ERR_BADPARAM); *buffer = buffer_p->current; *free_size = buffer_p->end - buffer_p->current; return KAA_ERR_NONE; }
kaa_error_t kaa_buffer_get_unprocessed_space(kaa_buffer_t *buffer_p , char **buffer , size_t *available_size) { KAA_RETURN_IF_NIL3(buffer_p, buffer, available_size, KAA_ERR_BADPARAM); *buffer = buffer_p->begin; *available_size = buffer_p->current - buffer_p->begin; return KAA_ERR_NONE; }
static kaa_error_t test_get_supported_services(void *context , kaa_service_t **supported_services , size_t *service_count) { KAA_RETURN_IF_NIL3(context, supported_services, service_count, KAA_ERR_BADPARAM); test_channel_context_t *channel_context = (test_channel_context_t *)context; *supported_services = channel_context->services; *service_count = channel_context->services_count; return KAA_ERR_NONE; }
kaa_error_t ext_tcp_utils_open_tcp_socket(kaa_fd_t *fd, const kaa_sockaddr_t *destination, kaa_socklen_t destination_size) { KAA_RETURN_IF_NIL3(fd, destination, destination_size, KAA_ERR_BADPARAM); unsigned char *dst = (unsigned char*)destination; if(!memcmp(dst,DESTINATION_SOCKADDR,destination_size)) { access_point_test_info.new_socket_created = true; *fd = ACCESS_POINT_SOCKET_FD; return KAA_ERR_NONE; } return KAA_ERR_BADPARAM; }
kaatcp_error_t kaatcp_get_request_disconnect(const kaatcp_disconnect_t *message, char *buf, size_t *buf_size) { KAA_RETURN_IF_NIL3(message, buf, buf_size, KAATCP_ERR_BAD_PARAM); if (*buf_size < KAA_DISCONNECT_MESSAGE_SIZE) { return KAATCP_ERR_BUFFER_NOT_ENOUGH; } char *cursor = buf; create_basic_header(KAATCP_MESSAGE_DISCONNECT, 2, cursor); cursor += 2; *(cursor++) = 0; *(cursor++) = (message->reason & 0xFF); *buf_size = KAA_DISCONNECT_MESSAGE_SIZE; return KAATCP_ERR_NONE; }
kaa_error_t kaa_configuration_manager_create(kaa_configuration_manager_t **configuration_manager_p, kaa_channel_manager_t *channel_manager, kaa_status_t *status, kaa_logger_t *logger) { KAA_RETURN_IF_NIL3(configuration_manager_p, status, logger, KAA_ERR_BADPARAM); kaa_configuration_manager_t *manager = (kaa_configuration_manager_t *) KAA_MALLOC(sizeof(kaa_configuration_manager_t)); KAA_RETURN_IF_NIL(manager, KAA_ERR_NOMEM); manager->channel_manager = channel_manager; manager->status = status; manager->logger = logger; manager->root_receiver = (kaa_configuration_root_receiver_t) { NULL, NULL }; char *buffer = NULL; size_t buffer_size = 0; bool need_deallocation = false; if (status->is_updated) ext_configuration_read(&buffer, &buffer_size, &need_deallocation); else ext_configuration_delete(); if (!buffer || !buffer_size) { need_deallocation = false; #if KAA_CONFIGURATION_DATA_LENGTH > 0 buffer = (char *)KAA_CONFIGURATION_DATA; buffer_size = KAA_CONFIGURATION_DATA_LENGTH; #endif } if (buffer && buffer_size > 0) { ext_calculate_sha_hash(buffer, buffer_size, manager->configuration_hash); manager->root_record = kaa_configuration_manager_deserialize(buffer, buffer_size); if (!manager->root_record) { KAA_FREE(manager); if (need_deallocation && buffer) KAA_FREE(buffer); return KAA_ERR_NOMEM; } if (need_deallocation) KAA_FREE(buffer); } *configuration_manager_p = manager; 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_platform_protocol_process_server_sync(kaa_platform_protocol_t *self , const char *buffer , size_t buffer_size) { KAA_RETURN_IF_NIL3(self, buffer, buffer_size, KAA_ERR_BADPARAM); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Processing server sync..."); kaa_platform_message_reader_t *reader = NULL; kaa_error_t error_code = kaa_platform_message_reader_create(&reader, buffer, buffer_size); KAA_RETURN_IF_ERR(error_code); uint32_t protocol_id = 0; uint16_t protocol_version = 0; uint16_t extension_count = 0; error_code = kaa_platform_message_header_read(reader, &protocol_id, &protocol_version, &extension_count); KAA_RETURN_IF_ERR(error_code); if (protocol_id != KAA_PLATFORM_PROTOCOL_ID) { KAA_LOG_ERROR(self->logger, KAA_ERR_BAD_PROTOCOL_ID, "Unsupported protocol ID %x", protocol_id); return KAA_ERR_BAD_PROTOCOL_ID; } if (protocol_version != KAA_PLATFORM_PROTOCOL_VERSION) { KAA_LOG_ERROR(self->logger, KAA_ERR_BAD_PROTOCOL_VERSION, "Unsupported protocol version %u", protocol_version); return KAA_ERR_BAD_PROTOCOL_VERSION; } uint32_t request_id = 0; uint8_t extension_type = 0; uint32_t extension_options = 0; uint32_t extension_length = 0; while (!error_code && kaa_platform_message_is_buffer_large_enough(reader, KAA_PROTOCOL_MESSAGE_HEADER_SIZE)) { error_code = kaa_platform_message_read_extension_header(reader , &extension_type , &extension_options , &extension_length); KAA_RETURN_IF_ERR(error_code); switch (extension_type) { case KAA_BOOTSTRAP_EXTENSION_TYPE: { error_code = kaa_bootstrap_manager_handle_server_sync(self->kaa_context->bootstrap_manager , reader , extension_options , extension_length); break; } case KAA_META_DATA_EXTENSION_TYPE: { error_code = kaa_platform_message_read(reader, &request_id, sizeof(uint32_t)); request_id = KAA_NTOHL(request_id); break; } case KAA_PROFILE_EXTENSION_TYPE: { error_code = kaa_profile_handle_server_sync(self->kaa_context->profile_manager , reader , extension_options , extension_length); break; } case KAA_USER_EXTENSION_TYPE: { error_code = kaa_user_handle_server_sync(self->kaa_context->user_manager , reader , extension_options , extension_length); break; } #ifndef KAA_DISABLE_FEATURE_LOGGING case KAA_LOGGING_EXTENSION_TYPE: { error_code = kaa_logging_handle_server_sync(self->kaa_context->log_collector , reader , extension_options , extension_length); break; } #endif #ifndef KAA_DISABLE_FEATURE_EVENTS case KAA_EVENT_EXTENSION_TYPE: { error_code = kaa_event_handle_server_sync(self->kaa_context->event_manager , reader , extension_options , extension_length , request_id); break; } #endif #ifndef KAA_DISABLE_FEATURE_CONFIGURATION case KAA_CONFIGURATION_EXTENSION_TYPE: { error_code = kaa_configuration_manager_handle_server_sync(self->kaa_context->configuration_manager , reader , extension_options , extension_length); break; } #endif #ifndef KAA_DISABLE_FEATURE_NOTIFICATION case KAA_NOTIFICATION_EXTENSION_TYPE: { error_code = kaa_notification_manager_handle_server_sync(self->kaa_context->notification_manager , reader , extension_length); break; } #endif default: KAA_LOG_WARN(self->logger, KAA_ERR_UNSUPPORTED, "Unsupported extension received (type = %u)", extension_type); break; } } kaa_platform_message_reader_destroy(reader); if (!error_code) { error_code = kaa_status_save(self->status); KAA_LOG_TRACE(self->logger, KAA_ERR_NONE, "Server sync successfully processed"); } else { KAA_LOG_ERROR(self->logger, error_code, "Server sync is corrupted. Failed to read extension with type %u", extension_type); } return error_code; }
kaatcp_error_t kaatcp_get_request_connect(const kaatcp_connect_t *message , char *buf , size_t *buf_size) { KAA_RETURN_IF_NIL3(message, buf, buf_size, KAATCP_ERR_BAD_PARAM); char header[6]; size_t payload_size = message->sync_request_size + message->session_key_size + message->signature_size + KAA_CONNECT_HEADER_LENGTH; uint8_t header_size = create_basic_header(KAATCP_MESSAGE_CONNECT , payload_size , header); if ((*buf_size) < payload_size + header_size) { return KAATCP_ERR_BUFFER_NOT_ENOUGH; } char *cursor = buf; memcpy(cursor, header, header_size); cursor += header_size; uint16_t name_length = KAA_HTONS(message->protocol_name_length); memcpy(cursor, &name_length, sizeof(uint16_t)); cursor += sizeof(uint16_t); memcpy(cursor, message->protocol_name, message->protocol_name_length); cursor += message->protocol_name_length; *(cursor++) = PROTOCOL_VERSION; *(cursor++) = message->connect_flags; uint32_t next_protocol_id = KAA_HTONL(message->next_ptorocol_id); memcpy(cursor, &next_protocol_id, sizeof(uint32_t)); cursor += sizeof(uint32_t); *(cursor++) = message->session_key_flags; *(cursor++) = message->signature_flags; uint16_t keep_alive = KAA_HTONS(message->keep_alive); memcpy(cursor, &keep_alive, sizeof(uint16_t)); cursor += sizeof(uint16_t); if (message->session_key && message->session_key_flags) { memcpy(cursor, message->session_key, message->session_key_size); cursor += message->session_key_size; } if (message->signature && message->signature_flags) { memcpy(cursor, message->signature, message->signature_size); cursor += message->signature_size; } if (message->sync_request) { memcpy(cursor, message->sync_request, message->sync_request_size); cursor += message->sync_request_size; } *buf_size = cursor - buf; return KAATCP_ERR_NONE; }
static kaa_error_t add_log_record(void *storage, const char *data, size_t data_size) { KAA_RETURN_IF_NIL3(storage, data, data_size, KAA_ERR_BADPARAM); kaa_log_record_t record = { copy_data(data, data_size), data_size }; return ext_log_storage_add_log_record(storage, &record); }