<commit_before>/*
 * Copyright 2019 Fitbit, Inc. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.fitbit.bluetooth.fbgatt.tx;

import com.fitbit.bluetooth.fbgatt.FitbitGatt;
import com.fitbit.bluetooth.fbgatt.GattServerConnection;
import com.fitbit.bluetooth.fbgatt.GattServerTransaction;
import com.fitbit.bluetooth.fbgatt.GattState;
import com.fitbit.bluetooth.fbgatt.GattTransactionCallback;
import com.fitbit.bluetooth.fbgatt.TransactionResult;
import com.fitbit.bluetooth.fbgatt.util.GattDisconnectReason;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattService;

import timber.log.Timber;

/**
 * Will perform a transaction adding a service to the gatt server.  The caller should check first
 * as to whether the service exists before adding this service or the result will be a failure.  Keep
 * in mind that another process or application can manipulate the services and characteristics on
 * the phone in different ways, so it is always prudent to check and ensure that the state of the
 * characteristics, descriptors, and services are to your liking before proceeding.
 *
 * Created by iowens on 11/15/17.
 */

public class AddGattServerServiceTransaction extends GattServerTransaction {

    private static final String NAME = "AddGattServerServiceTransaction";

    private BluetoothGattService service;

    public AddGattServerServiceTransaction(GattServerConnection server, GattState successEndState, BluetoothGattService service) {
        super(server, successEndState);
        this.service = service;
    }

    public AddGattServerServiceTransaction(GattServerConnection server, GattState successEndState, BluetoothGattService service, long timeoutMillis) {
        super(server, successEndState, timeoutMillis);
        this.service = service;
    }

    @Override
    protected void transaction(GattTransactionCallback callback) {
        super.transaction(callback);
        getGattServer().setState(GattState.ADDING_SERVICE);
        if(getGattServer().getServer() == null) {
            mainThreadHandler.post(() -> {
                TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
                Timber.w("The GATT Server was not started yet, did you start the gatt?");
                builder.responseStatus(GattDisconnectReason.getReasonForCode(GattDisconnectReason.GATT_CONN_NO_RESOURCES.getCode()).ordinal());
                getGattServer().setState(GattState.ADD_SERVICE_FAILURE);
                Timber.e("The gatt service could not be added: %s", service.getUuid());
                builder.gattState(getGattServer().getGattState())
                        .serviceUuid(service.getUuid())
                        .resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
                callCallbackWithTransactionResultAndRelease(callback, builder.build());
            });
        } else {
            if(doesGattServerServiceAlreadyExist(service)) {
                TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
                builder.responseStatus(GattDisconnectReason.getReasonForCode(GattDisconnectReason.GATT_CONN_NO_RESOURCES.getCode()).ordinal());
                getGattServer().setState(GattState.ADD_SERVICE_FAILURE);
                Timber.w("The gatt service %s, is a duplicate, and could not be added to server", service.getUuid());
                builder.gattState(getGattServer().getGattState())
                        .serviceUuid(service.getUuid())
                        .resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
                callCallbackWithTransactionResultAndRelease(callback, builder.build());
                getGattServer().setState(GattState.IDLE);
                return;
            }
            boolean success = getGattServer().getServer().addService(service);
            if(!success) {
                TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
                builder.responseStatus(GattDisconnectReason.getReasonForCode(GattDisconnectReason.GATT_CONN_NO_RESOURCES.getCode()).ordinal());
                getGattServer().setState(GattState.ADD_SERVICE_FAILURE);
                Timber.w("The gatt service failed to be added at the gatt level, try again later.", service.getUuid());
                builder.gattState(getGattServer().getGattState())
                    .serviceUuid(service.getUuid())
                    .resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
                callCallbackWithTransactionResultAndRelease(callback, builder.build());
                getGattServer().setState(GattState.IDLE);
            }
        }
    }

    @Override
    public void onServerServiceAdded(int status, BluetoothGattService service) {
        TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
        builder.responseStatus(GattDisconnectReason.getReasonForCode(status).ordinal());<commit_msg>Fixing an issue with add server service<commit_after>/*
 * Copyright 2019 Fitbit, Inc. All rights reserved.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.fitbit.bluetooth.fbgatt.tx;

import com.fitbit.bluetooth.fbgatt.FitbitGatt;
import com.fitbit.bluetooth.fbgatt.GattServerConnection;
import com.fitbit.bluetooth.fbgatt.GattServerTransaction;
import com.fitbit.bluetooth.fbgatt.GattState;
import com.fitbit.bluetooth.fbgatt.GattTransactionCallback;
import com.fitbit.bluetooth.fbgatt.TransactionResult;
import com.fitbit.bluetooth.fbgatt.util.GattDisconnectReason;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattService;

import timber.log.Timber;

/**
 * Will perform a transaction adding a service to the gatt server.  The caller should check first
 * as to whether the service exists before adding this service or the result will be a failure.  Keep
 * in mind that another process or application can manipulate the services and characteristics on
 * the phone in different ways, so it is always prudent to check and ensure that the state of the
 * characteristics, descriptors, and services are to your liking before proceeding.
 *
 * Created by iowens on 11/15/17.
 */

public class AddGattServerServiceTransaction extends GattServerTransaction {

    private static final String NAME = "AddGattServerServiceTransaction";

    private BluetoothGattService service;

    public AddGattServerServiceTransaction(GattServerConnection server, GattState successEndState, BluetoothGattService service) {
        super(server, successEndState);
        this.service = service;
    }

    public AddGattServerServiceTransaction(GattServerConnection server, GattState successEndState, BluetoothGattService service, long timeoutMillis) {
        super(server, successEndState, timeoutMillis);
        this.service = service;
    }

    @Override
    protected void transaction(GattTransactionCallback callback) {
        super.transaction(callback);
        getGattServer().setState(GattState.ADDING_SERVICE);
        if(getGattServer().getServer() == null) {
            mainThreadHandler.post(() -> {
                TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
                Timber.w("The GATT Server was not started yet, did you start the gatt?");
                builder.responseStatus(GattDisconnectReason.getReasonForCode(GattDisconnectReason.GATT_CONN_NO_RESOURCES.getCode()).ordinal());
                getGattServer().setState(GattState.ADD_SERVICE_FAILURE);
                Timber.e("The gatt service could not be added: %s", service.getUuid());
                builder.gattState(getGattServer().getGattState())
                        .serviceUuid(service.getUuid())
                        .resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
                callCallbackWithTransactionResultAndRelease(callback, builder.build());
            });
        } else {
            if(doesGattServerServiceAlreadyExist(service)) {
                TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
                builder.responseStatus(GattDisconnectReason.getReasonForCode(GattDisconnectReason.GATT_CONN_NO_RESOURCES.getCode()).ordinal());
                getGattServer().setState(GattState.ADD_SERVICE_FAILURE);
                Timber.w("The gatt service %s, is a duplicate, and could not be added to server", service.getUuid());
                builder.gattState(getGattServer().getGattState())
                        .serviceUuid(service.getUuid())
                        .resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
                callCallbackWithTransactionResultAndRelease(callback, builder.build());
                getGattServer().setState(GattState.IDLE);
                return;
            }
            boolean success = getGattServer().getServer().addService(service);
            if(!success) {
                TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
                builder.responseStatus(GattDisconnectReason.getReasonForCode(GattDisconnectReason.GATT_CONN_NO_RESOURCES.getCode()).ordinal());
                getGattServer().setState(GattState.ADD_SERVICE_FAILURE);
                Timber.w("The gatt service %s failed to be added at the gatt level, try again later.", service.getUuid());
                builder.gattState(getGattServer().getGattState())
                    .serviceUuid(service.getUuid())
                    .resultStatus(TransactionResult.TransactionResultStatus.FAILURE);
                callCallbackWithTransactionResultAndRelease(callback, builder.build());
                getGattServer().setState(GattState.IDLE);
            }
        }
    }

    @Override
    public void onServerServiceAdded(int status, BluetoothGattService service) {
        TransactionResult.Builder builder = new TransactionResult.Builder().transactionName(getName());
        builder.responseStatus(GattDisconnectReason.getReasonForCode(status).ordinal());
{'fraction_non_alphanumeric': 0.08691935024223425, 'fraction_numerical': 0.0026598271112377695, 'mean_word_length': 3.3076923076923075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3406510', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2653, 'n_words': 606}