Commit 39717b67 authored by jaehong.jo's avatar jaehong.jo Committed by Erich Keane

Added Android EDR code

Android EDR functionality has been implemented.
Signed-off-by: default avatarjaehong.jo <jaehong.jo@samsung.com>
Change-Id: I036bde692098ea40e28df2b07b40e478a4eb021b
Reviewed-on: https://gerrit.iotivity.org/gerrit/757Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: default avatarErich Keane <erich.keane@intel.com>
parent b30ec677
/* ****************************************************************
*
* 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.
*
******************************************************************/
/**
* @file
*
* This file contains the APIs for BT communications.
*/
#ifndef __CA_EDRUTILES_H_
#define __CA_EDRUTILES_H_
#include "cacommon.h"
#include "cathreadpool.h"
#include "uarraylist.h"
#include "jni.h"
#ifdef __cplusplus
extern "C"
{
#endif
jstring CAEDRNativeGetAddressFromDeviceSocket(JNIEnv *env, jobject bluetoothSocketObj);
jstring CAEDRNativeGetLocalDeviceAddress(JNIEnv *env);
jobjectArray CAEDRNativeGetBondedDevices(JNIEnv *env);
jint CAEDRNativeGetBTStateOnInfo(JNIEnv *env);
jboolean CAEDRNativeIsEnableBTAdapter(JNIEnv *env);
jstring CAEDRNativeGetAddressFromBTDevice(JNIEnv *env, jobject bluetoothDevice);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
/* ****************************************************************
/******************************************************************
*
* Copyright 2014 Samsung Electronics All Rights Reserved.
*
......@@ -20,12 +20,10 @@
/**
* @file
*
* This file contains the APIs for BT EDR communications.
* @brief This file contains the APIs for BT EDR communications.
*/
#ifndef __CA_EDRCORE_H_
#define __CA_EDRCORE_H_
#ifndef CA_EDR_CLIENT_H_
#define CA_EDR_CLIENT_H_
#include <stdbool.h>
......@@ -33,25 +31,23 @@
#include "cathreadpool.h"
#include "jni.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Set context of application
* @param context [IN] context of application
* @return None
*/
void CAEDRJniSetContext(jobject context);
* @brief Initialize context of application
* @return None
*/
void CAEDRJniInitContext();
/**
* @brief Create JNI Object
* @param context [IN] context of application
* @return None
*/
void CAEDRCreateJNIInterfaceObject(jobject context);
* @brief Create JNI Object
* @param context [IN] context of application
* @return #CA_STATUS_OK or Appropriate error code
*/
CAResult_t CAEDRCreateJNIInterfaceObject(jobject context);
/**
* @brief Initialize client for EDR
......@@ -68,11 +64,9 @@ void CAEDRTerminate();
/**
* @brief Initialize JNI object
* @param env [IN] JNI interface pointer
* @param jvm [IN] java virtual machine pointer
* @return None
*/
void CAEDRCoreJniInit(JNIEnv *env, JavaVM *jvm);
void CAEDRCoreJniInit();
/**
* @brief Send data for unicast
......@@ -83,8 +77,7 @@ void CAEDRCoreJniInit(JNIEnv *env, JavaVM *jvm);
* @retval #CA_STATUS_OK Successful
* @retval #CA_STATUS_FAILED Operation failed
*/
CAResult_t CAEDRSendUnicastMessage(const char *address,
const char *data, uint32_t dataLen);
CAResult_t CAEDRSendUnicastMessage(const char *address, const char *data, uint32_t dataLen);
/**
* @brief Send data for multicast
......@@ -121,8 +114,7 @@ void CAEDRGetLocalAddress(char **address);
* @retval #CA_STATUS_OK Successful
* @retval #CA_STATUS_FAILED Operation failed
*/
CAResult_t CAEDRSendUnicastMessageImpl(const char *address,
const char *data, uint32_t dataLen);
CAResult_t CAEDRSendUnicastMessageImpl(const char *address, const char *data, uint32_t dataLen);
/**
* @brief Send data for multicast (implement)
......@@ -144,19 +136,21 @@ CAResult_t CAEDRSendMulticastMessageImpl(JNIEnv *env, const char *data, uint32_t
* @param env [IN] JNI interface pointer
* @param address [IN] Remote Address
* @param data [IN] Data to be transmitted from EDR
* @param dataLength [IN] Length of data
* @param id [IN] index of remote address
* @return None
* @return #CA_STATUS_OK or Appropriate error code
*/
void CAEDRNativeSendData(JNIEnv *env, const char *address, const char *data, uint32_t id);
CAResult_t CAEDRNativeSendData(JNIEnv *env, const char* address, const char* data,
uint32_t dataLength, uint32_t id);
/**
* @brief This function will connect to remote device.
* @param env [IN] JNI interface pointer
* @param address [IN] Remote Address
* @param id [IN] index of remote address
* @return None
* @return #CA_STATUS_OK or Appropriate error code
*/
void CAEDRNativeConnect(JNIEnv *env, const char *address, uint32_t id);
CAResult_t CAEDRNativeConnect(JNIEnv *env, const char *address, uint32_t id);
/**
* @brief This function will close socket.
......@@ -167,126 +161,6 @@ void CAEDRNativeConnect(JNIEnv *env, const char *address, uint32_t id);
*/
void CAEDRNativeSocketClose(JNIEnv *env, const char *address, uint32_t id);
/**
* BT State List
*/
/**
* @brief This function will create the device state list.
* @return None
*/
void CAEDRNativeCreateDeviceStateList();
/**
* @brief Update connection state of device
* @param state [IN] connection state
* @param address [IN] remote address
* @return None
*/
void CAEDRUpdateDeviceState(CAConnectedState_t state, const char *address);
/**
* @brief Add device object to the list
* @param state [IN] connection state object
* @return None
*/
void CAEDRNativeAddDeviceStateToList(state_t *state);
/**
* @brief Check whether the device exist in the list or not
* @param remoteAddress [IN] remote address
* @return TRUE or FALSE
*/
jboolean CAEDRNativeIsDeviceInList(const char *remoteAddress);
/**
* @brief Remove all device objects in the list
* @return None
*/
void CAEDRNativeRemoveAllDeviceState();
/**
* @brief Remove target device in the list
* @param remoteAddress [IN] remote address
* @return None
*/
void CAEDRNativeRemoveDevice(const char *remoteAddress);
/**
* @brief Get current device connection state
* @param remoteAddress [IN] remote address
* @return STATE_DISCONNECTED or STATE_CONNECTED
*/
CAConnectedState_t CAEDRIsConnectedDevice(const char *remoteAddress);
/**
* @brief Reordering for the list
* @param index [IN] index of device list that want to reordering
* @return None
*/
void CAEDRReorderingDeviceList(uint32_t index);
/**
* Bluetooth Socket Object List
*/
/**
* @brief This function will create the device socket list.
* @return None
*/
void CAEDRNativeCreateDeviceSocketList();
/**
* @brief Add device object to the list
* @param env [IN] JNI interface pointer
* @param deviceSocket [IN] device socket object
* @return None
*/
void CAEDRNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket);
/**
* @brief Add device object to the list
* @param env [IN] JNI interface pointer
* @param remoteAddress [IN] remote address
* @return TRUE or FALSE
*/
jboolean CAEDRNativeIsDeviceSocketInList(JNIEnv *env, const char *remoteAddress);
/**
* @brief Add device object to the list
* @param env [IN] JNI interface pointer
* @return None
*/
void CAEDRNativeRemoveAllDeviceSocket(JNIEnv *env);
/**
* @brief Add device object to the list
* @param env [IN] JNI interface pointer
* @param deviceSocket [IN] device socket object
* @return None
*/
void CAEDRNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket);
/**
* @brief Get device socket object from the list
* @param idx [IN] index of device list
* @return Device socket object or NULL
*/
jobject CAEDRNativeGetDeviceSocket(uint32_t idx);
/**
* @brief Get length of device socket list
* @return length of list
*/
uint32_t CAEDRGetSocketListLength();
/**
* @brief Reordering for the list
* @param index [IN] index of device list that want to reordering
* @return None
*/
void CAEDRReorderingDeviceSocketList(uint32_t index);
#ifdef __cplusplus
} /* extern "C" */
#endif
......
/******************************************************************
*
* 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 "caedrnwmonitor.h"
#include "logger.h"
#include "oic_malloc.h"
#include "cathreadpool.h" /* for thread pool */
#include "camutex.h"
#include "uarraylist.h"
#include "caadapterutils.h"
#include "caedrserver.h"
#include "caedrutils.h"
#include "org_iotivity_jar_caedrinterface.h"
//#define DEBUG_MODE
#define TAG PCF("CA_EDR_MONITOR")
// temp method
static JavaVM *g_jvm;
static jobject g_context;
static CAEDRNetworkStatusCallback g_networkChangeCb = NULL;
static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
CAResult_t CAEDRInitializeNetworkMonitor()
void CAEDRNetworkMonitorJNISetContext()
{
OIC_LOG(DEBUG, TAG, "IN");
OIC_LOG(DEBUG, TAG, "CAEDRNetworkMonitorJNISetContext");
g_context = (jobject) CANativeJNIGetContext();
}
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
//getting jvm
void CAEDRNetworkMonitorJniInit()
{
OIC_LOG(DEBUG, TAG, "CAEDRNetworkMonitorJniInit");
g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
}
void CAEDRSetNetworkChangeCallback(
CAEDRNetworkStatusCallback networkChangeCallback)
CAResult_t CAEDRInitializeNetworkMonitor(const ca_thread_pool_t threadPool)
{
OIC_LOG(DEBUG, TAG, "IN");
CAEDRNetworkMonitorJniInit();
CANativeJNIGetJavaVM();
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
void CAEDRSetNetworkChangeCallback(CAEDRNetworkStatusCallback networkChangeCallback)
{
OIC_LOG(DEBUG, TAG, "CAEDRSetNetworkChangeCallback");
g_networkChangeCb = networkChangeCallback;
}
void CAEDRTerminateNetworkMonitor(void)
......@@ -62,3 +105,66 @@ CAResult_t CAEDRClientSetCallbacks(void)
return CA_STATUS_OK;
}
JNIEXPORT void JNICALL
Java_org_iotivity_jar_caedrinterface_CAEDRStateChangedCallback(JNIEnv *env, jobject obj,
jint status)
{
// STATE_ON:12, STATE_OFF:10
OIC_LOG(DEBUG, TAG, "caedrinterface - Network State Changed");
if (NULL == g_networkChangeCb)
{
OIC_LOG_V(DEBUG, TAG, "gNetworkChangeCb is null", status);
}
jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
if (!jni_cid_BTAdapter)
{
OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTAdapter is null");
return;
}
jfieldID id_state_on = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_ON", "I");
if (!id_state_on)
{
OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_on is null");
return;
}
jfieldID id_state_off = (*env)->GetStaticFieldID(env, jni_cid_BTAdapter, "STATE_OFF", "I");
if (!id_state_off)
{
OIC_LOG(ERROR, TAG, "[EDR][Native] id_state_off is null");
return;
}
jint state_on = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_on);
jint state_off = (*env)->GetStaticIntField(env, jni_cid_BTAdapter, id_state_off);
if (state_on == status)
{
CANetworkStatus_t newStatus = CA_INTERFACE_UP;
CAEDRServerStartAcceptThread();
g_networkChangeCb(newStatus);
}
else if (state_off == status)
{
CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
CAEDRNativeRemoveAllDeviceSocket(env);
CAEDRNativeRemoveAllDeviceState(env);
g_networkChangeCb(newStatus);
}
}
JNIEXPORT void JNICALL
Java_org_iotivity_jar_caedrinterface_CAEDRBondStateChangedCallback(JNIEnv *env, jobject obj,
jstring addr)
{
OIC_LOG(DEBUG, TAG, "caedrinterface - Bond State Changed");
if (addr)
{
CAEDRNativeRemoveDeviceSocketBaseAddr(env, addr);
CAEDRNativeRemoveDevice(addr);
}
}
/******************************************************************
*
* 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.
*
******************************************************************/
/**
* @file
* @brief This file contains the APIs for BT LE communications.
*/
#ifndef CA_EDR_NWMONITOR_H_
#define CA_EDR_NWMONITOR_H_
#include "cacommon.h"
#ifdef __cplusplus
extern "C"
{
#endif
/**
* @brief Set context of application
* @return None
*/
void CAEDRNetworkMonitorJNISetContext();
/**
* @brief Initialize JNI object
* @return None
*/
void CAEDRNetworkMonitorJniInit();
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
/******************************************************************
*
* 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.
*
******************************************************************/
/**
* @file
* @brief This file contains the APIs for BT EDR communications.
*/
#ifndef CA_EDR_SERVER_H_
#define CA_EDR_SERVER_H_
#include <stdbool.h>
#include "cacommon.h"
#include "cathreadpool.h"
#include "jni.h"
#ifdef __cplusplus
extern "C"
{
#endif
typedef void (*CAPacketReceiveCallback)(const char *address, const char *data);
/**
* @brief Initialize JNI object
* @return None
*/
void CAEDRServerJniInit();
/**
* @brief Initialize server for EDR
* @param handle [IN] thread pool handle object
* @return None
*/
void CAEDRServerInitialize(ca_thread_pool_t handle);
/*
* @brief Start Accept Thread
* @return None
*/
void CAEDRServerStartAcceptThread();
/**
* @brief Start unicast server
* @param isSecured [IN] unicast server type
* @return #CA_STATUS_OK or Appropriate error code
*/
CAResult_t CAEDRStartUnicastServer(bool isSecured);
/**
* @brief Start multicast server
* @param isSecured [IN] multicst server type
* @return #CA_STATUS_OK or Appropriate error code
*/
CAResult_t CAEDRStartMulticastServer(bool isSecured);
/**
* @brief Stop unicast server
* @param serverID [IN] unicast server id
* @return #CA_STATUS_OK or Appropriate error code
*/
CAResult_t CAEDRStopUnicastServer(int32_t serverID);
/**
* @brief Stop multicast server
* @param serverID [IN] multicast server id
* @return #CA_STATUS_OK or Appropriate error code
*/
CAResult_t CAEDRStopMulticastServer(int32_t serverID);
/**
* EDR Method
*/
/**
* @brief This function will read the data from remote device.
* @param env [IN] JNI interface pointer
* @param id [IN] index of remote address
* @param type [IN] EDR server type
* @return #CA_STATUS_OK or Appropriate error code
*/
CAResult_t CAEDRNativeReadData(JNIEnv *env, uint32_t id, CAAdapterServerType_t type);
/*
* @brief Start Listen Task
* @param env [IN] JNI interface pointer
* @return None
*/
void CANativeStartListenTask(JNIEnv *env);
/**
* @brief This function will listen the connection from remote device.
* @param env [IN] JNI interface pointer
* @return server socket object or NULL
*/
jobject CAEDRNativeListen(JNIEnv *env);
/**
* @brief This function will listen the connection from remote device.
* @param env [IN] JNI interface pointer
* @param socket [IN] server socket object
* @return JNI_TRUE or JNI_FALSE
*/
jboolean CAEDRIsConnectedForSocket(JNIEnv *env, jobject socket);
/**
* @brief This function will accept the connection from remote device.
* @param env [IN] JNI interface pointer
* @param severSocketObject [IN] server socket object
* @return None