<commit_before>/* ****************************************************************
 *
 * Copyright 2014 Samsung Electronics All Rights Reserved.
 *
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <jni.h>

#include "caedrinterface.h"
#include "caedrutils.h"
#include "caedrserver.h"
#include "logger.h"
#include "oic_malloc.h"
#include "cathreadpool.h" /* for thread pool */
#include "camutex.h"
#include "uarraylist.h"
#include "caadapterutils.h"
#include "org_iotivity_ca_CaEdrInterface.h"
#include "oic_string.h"

//#define DEBUG_MODE
#define TAG PCF("CA_EDR_SERVER")
#define MAX_PDU_BUFFER (1024)

static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
static const char CLASSPATH_BT_UUID[] = "java/util/UUID";

static ca_thread_pool_t g_threadPoolHandle = NULL;

static JavaVM *g_jvm;

/**
 * Mutex to synchronize socket list update.
 */
static ca_mutex g_mutexSocketListManager;

/**
 * server socket instance.
 */
static jobject g_serverSocketObject = NULL;

/**
 * Mutex to synchronize unicast server.
 */
static ca_mutex g_mutexUnicastServer = NULL;

/**
 * Flag to control the Receive Unicast Data Thread.
 */
static bool g_stopUnicast = false;

/**
 * Mutex to synchronize secure multicast server.
 */
static ca_mutex g_mutexMulticastServer = NULL;

/**
 * Flag to control the Receive Multicast Data Thread.
 */
static bool g_stopMulticast = false;

/**
 * Mutex to synchronize accept server.
 */
static ca_mutex g_mutexAcceptServer = NULL;

/**
 * Flag to control the Accept Thread.
 */
static bool g_stopAccept = false;

static jobject g_inputStream = NULL;

/**
 * Mutex to synchronize server socket.
 */
static ca_mutex g_mutexServerSocket = NULL;

static jobject g_serverSocket = NULL;

/**
 * Mutex to synchronize device state list.
 */
static ca_mutex g_mutexStateList = NULL;

/**
 * Mutex to synchronize device object list.
 */
static ca_mutex g_mutexObjectList = NULL;

/**
 * Mutex to synchronize input stream.
 */
static ca_mutex g_mutexInputStream = NULL;

typedef struct send_data
{
    char* address;
    char* data;
    uint32_t id;
} data_t;

/**
 * Thread context information for unicast, multicast and secured unicast server.
 */
typedef struct
{
    bool *stopFlag;
    CAAdapterServerType_t type;
} CAAdapterReceiveThreadContext_t;

typedef struct
{
    bool *stopFlag;
} CAAdapterAcceptThreadContext_t;

/**
 * Maintains the callback to be notified when data received from remote
 * Bluetooth device.
 */
static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;

static void CAReceiveHandler(void *data)
{
    OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler..");
    // Input validation
    VERIFY_NON_NULL_VOID(data, TAG, "Invalid thread context");

    bool isAttached = false;
    JNIEnv* env;
    jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
    if (JNI_OK != res)
    {
        OIC_LOG(DEBUG, TAG, "CAReceiveHandler - Could not get JNIEnv pointer");
        res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);

        if (JNI_OK != res)
        {
            OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
            return;
        }
        isAttached = true;
    }

    CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) data;

    while (true != *(ctx->stopFlag))
    {
        // if new socket object is added in socket list after below logic is ran.
        // new socket will be started to read after next while loop
        uint32_t length = CAEDRGetSocketListLength();
        if (0 == length)
        {
            OIC_LOG(DEBUG, TAG, "socket list is empty");
            sleep(1);
        }

        uint32_t idx;
        for (idx = 0; idx < length; idx++)
        {
            OIC_LOG(DEBUG, TAG, "start CAEDRNativeReadData");
            CAEDRNativeReadData(env, idx, ctx->type);
            sleep(1);
        }
    }

    if (isAttached)
    {
        (*g_jvm)->DetachCurrentThread(g_jvm);
    }

    OICFree(ctx);

    OIC_LOG(DEBUG, TAG, "OUT - CAReceiveHandler");
}

static void CAAcceptHandler(void *data)<commit_msg>Remove sleep in Android EDR.<commit_after>/* ****************************************************************
 *
 * Copyright 2014 Samsung Electronics All Rights Reserved.
 *
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************/

#include <stdio.h>
#include <string.h>
#include <jni.h>

#include "caedrinterface.h"
#include "caedrutils.h"
#include "caedrserver.h"
#include "logger.h"
#include "oic_malloc.h"
#include "cathreadpool.h" /* for thread pool */
#include "camutex.h"
#include "uarraylist.h"
#include "caadapterutils.h"
#include "org_iotivity_ca_CaEdrInterface.h"
#include "oic_string.h"

//#define DEBUG_MODE
#define TAG PCF("CA_EDR_SERVER")
#define MAX_PDU_BUFFER (1024)

static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
static const char CLASSPATH_BT_UUID[] = "java/util/UUID";

static ca_thread_pool_t g_threadPoolHandle = NULL;

static JavaVM *g_jvm;

/**
 * Mutex to synchronize socket list update.
 */
static ca_mutex g_mutexSocketListManager;

/**
 * server socket instance.
 */
static jobject g_serverSocketObject = NULL;

/**
 * Mutex to synchronize unicast server.
 */
static ca_mutex g_mutexUnicastServer = NULL;

/**
 * Flag to control the Receive Unicast Data Thread.
 */
static bool g_stopUnicast = false;

/**
 * Mutex to synchronize secure multicast server.
 */
static ca_mutex g_mutexMulticastServer = NULL;

/**
 * Flag to control the Receive Multicast Data Thread.
 */
static bool g_stopMulticast = false;

/**
 * Mutex to synchronize accept server.
 */
static ca_mutex g_mutexAcceptServer = NULL;

/**
 * Flag to control the Accept Thread.
 */
static bool g_stopAccept = false;

static jobject g_inputStream = NULL;

/**
 * Mutex to synchronize server socket.
 */
static ca_mutex g_mutexServerSocket = NULL;

static jobject g_serverSocket = NULL;

/**
 * Mutex to synchronize device state list.
 */
static ca_mutex g_mutexStateList = NULL;

/**
 * Mutex to synchronize device object list.
 */
static ca_mutex g_mutexObjectList = NULL;

/**
 * Mutex to synchronize input stream.
 */
static ca_mutex g_mutexInputStream = NULL;

typedef struct send_data
{
    char* address;
    char* data;
    uint32_t id;
} data_t;

/**
 * Thread context information for unicast, multicast and secured unicast server.
 */
typedef struct
{
    bool *stopFlag;
    CAAdapterServerType_t type;
} CAAdapterReceiveThreadContext_t;

typedef struct
{
    bool *stopFlag;
} CAAdapterAcceptThreadContext_t;

/**
 * Maintains the callback to be notified when data received from remote
 * Bluetooth device.
 */
static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;

static void CAReceiveHandler(void *data)
{
    OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler..");
    // Input validation
    VERIFY_NON_NULL_VOID(data, TAG, "Invalid thread context");

    bool isAttached = false;
    JNIEnv* env;
    jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
    if (JNI_OK != res)
    {
        OIC_LOG(DEBUG, TAG, "CAReceiveHandler - Could not get JNIEnv pointer");
        res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);

        if (JNI_OK != res)
        {
            OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
            return;
        }
        isAttached = true;
    }

    CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) data;

    while (true != *(ctx->stopFlag))
    {
        // if new socket object is added in socket list after below logic is ran.
        // new socket will be started to read after next while loop
        uint32_t length = CAEDRGetSocketListLength();
        if (0 == length)
        {
            OIC_LOG(DEBUG, TAG, "socket list is empty");
        }
        else
        {
            uint32_t idx;
            for (idx = 0; idx < length; idx++)
            {
                OIC_LOG(DEBUG, TAG, "start CAEDRNativeReadData");
                CAEDRNativeReadData(env, idx, ctx->type);
            }
        }
    }

    if (isAttached)
    {
        (*g_jvm)->DetachCurrentThread(g_jvm);
    }

    OICFree(ctx);

    OIC_LOG(DEBUG, TAG, "OUT - CAReceiveHandler");
}

static void CAAcceptHandler(void *data)
{'fraction_non_alphanumeric': 0.1269362719617424, 'fraction_numerical': 0.004782201892088575, 'mean_word_length': 3.4516427579824156, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2561578', 'n_tokens_mistral': 3078, 'n_tokens_neox': 2989, 'n_words': 892}