Commit ac8ffa40 authored by Tim Kourt's avatar Tim Kourt Committed by Erich Keane

Android Base API merge to master

Introduced support for automated Jenkins build for android

Change-Id: Ide54185a0e75ee8c75b3616beb5f935810220203
Signed-off-by: default avatarErich Keane <erich.keane@intel.com>
Signed-off-by: default avatarRahul Rahul <rahul.rahul@intel.com>
Signed-off-by: default avatarTim Kourt <tim.a.kourt@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/894Tested-by: default avatarjenkins-iotivity <jenkins-iotivity@opendaylight.org>
parent 6a494128
......@@ -84,6 +84,10 @@ extlibs/master.zip
extlibs/cereal/cereal
extlibs/expat/expat*
extlibs/cereal
extlibs/android/gradle/gradle-2.2.1
extlibs/android/ndk/android-ndk-r10d
extlibs/android/sdk/android-sdk_r24.2
extlibs/boost/boost_1_58_0
*.tgz
*.zip
extlibs/arduino/arduino-1.5.8
......
......@@ -33,6 +33,10 @@ Import('env')
target_os = env.get('TARGET_OS')
if target_os == 'arduino':
SConscript('arduino.scons')
if target_os == 'android':
SConscript('android/android_api/SConscript')
# By default, src_dir is current dir, the build_dir is:
# ./out/<target_os>/<target_arch>/<release or debug>/
#
......
apply plugin: 'com.android.library'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "org.iotivity.base"
minSdkVersion 19
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
//////////////
// NDK Support
//////////////
// If using this, Android studio will fail run the following to set the environment variable for android studio:
// launchctl setenv ANDROID_NDK_HOME /Users/boos_patrick/Development/Android/android-ndk-r8e
// otherwise remove the dependsOn part and run ./gradlew buildNative from the command line
task copyNativeLibs(type: Copy, dependsOn: 'buildNative') {
dependsOn 'buildNative'
from(new File('libs')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }
clean.dependsOn 'cleanCopyNativeLibs'
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
pkgTask.jniFolders = new HashSet<File>()
pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))
}
task buildNative(type: Exec) {
if (System.env.ANDROID_NDK_HOME != null) {
def ndkBuild = new File(System.env.ANDROID_NDK_HOME, 'ndk-build')
commandLine ndkBuild
} else {
doLast {
println '##################'
println 'Skipping NDK build'
println 'Reason: ANDROID_NDK_HOME not set.'
println '##################'
}
}
}
//******************************************************************
//
// Copyright 2014 MediaTek 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 "ocstack-jni.h"
static const char* TAG = "OCHEADEROPTION-JNI";
JNIEXPORT jint JNICALL getOptionID(JNIEnv *env, jobject jobj)
{
OC::HeaderOption::OCHeaderOption *option = getHandle<OC::HeaderOption::OCHeaderOption>(env, jobj);
return option->getOptionID();
}
JNIEXPORT jstring JNICALL getOptionData(JNIEnv *env, jobject jobj)
{
OC::HeaderOption::OCHeaderOption *option = getHandle<OC::HeaderOption::OCHeaderOption>(env, jobj);
std::string data = option->getOptionData();
return env->NewStringUTF(data.c_str());
}
JNIEXPORT jlong JNICALL OCHeaderOptionConstructor(JNIEnv *env, jobject jobj, jint joption, jstring jdata)
{
OC::HeaderOption::OCHeaderOption *option = new OC::HeaderOption::OCHeaderOption(joption, env->GetStringUTFChars(jdata, 0));
jlong instptr = reinterpret_cast<jlong>(option);
return instptr;
}
//******************************************************************
//
// Copyright 2014 MediaTek 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 "ocstack-jni.h"
static const char* TAG = "OCPLATFORM-JNI";
JNIEXPORT void JNICALL configure(JNIEnv *env, jobject obj, jobject jcfg)
{
OC::PlatformConfig *cfg = getHandle<OC::PlatformConfig>(env, jcfg);
__android_log_print(ANDROID_LOG_INFO, TAG, "service : %d\n", (int) cfg->serviceType);
__android_log_print(ANDROID_LOG_INFO, TAG, "mode : %d\n", (int) cfg->mode);
__android_log_print(ANDROID_LOG_INFO, TAG, "IP : %s\n", (cfg->ipAddress).c_str());
__android_log_print(ANDROID_LOG_INFO, TAG, "port : %d\n", cfg->port);
__android_log_print(ANDROID_LOG_INFO, TAG, "QOS : %d\n", (int) cfg->QoS);
OC::OCPlatform::Configure(*cfg);
}
JNIEXPORT jint JNICALL findResource(JNIEnv* env, jobject obj, jstring jhost, jstring juri, jobject found)
{
string host = env->GetStringUTFChars(jhost,0);
string uri = env->GetStringUTFChars(juri,0);
JNICallBackContext *callbackContext;
string key = uri + "/FIND";
std:map<std::string, JNICallBackContext*>::iterator iter = gJNICallBackContextList.find(key);
if(iter == gJNICallBackContextList.end()) {
gJNICallBackContextList[key] = new JNICallBackContext(env->NewGlobalRef(found));
callbackContext = gJNICallBackContextList[key];
__android_log_print(ANDROID_LOG_INFO, TAG, "Adding %s to gJNICallBackContextList", key.c_str());
}
else
callbackContext = iter->second;
__android_log_print(ANDROID_LOG_ERROR, TAG, "Calling oic base findresource()\n");
try {
OC::OCPlatform::findResource(host, uri,
[callbackContext](std::shared_ptr<OC::OCResource> resource)
{
JNIEnv * env;
// Attach to JavaVM
// double check it's all ok
int getEnvStat = g_JavaVM->GetEnv((void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: not attached");
if (g_JavaVM->AttachCurrentThread(&env, NULL) != 0) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to attach");
}
else
__android_log_print(ANDROID_LOG_INFO, TAG, "Attached OK");
} else if (getEnvStat == JNI_OK) {
//
} else if (getEnvStat == JNI_EVERSION) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "GetEnv: version not supported");
}
// save the C++ resource ptr now
jmethodID resource_constructor = env->GetMethodID(g_ocresource_clazz, "<init>", "(J)V");
callbackContext->m_resource = resource;
jobject jresource = env->NewObject(g_ocresource_clazz, resource_constructor, \
(jlong) reinterpret_cast<jlong>(&callbackContext->m_resource));
if(jresource == NULL) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "cannot create OCResource class");
return;
}
jclass clazz = env->GetObjectClass(callbackContext->m_callBackFunction);
if (clazz == NULL) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "Failed to find class");
}
// Find Callback function
jmethodID mid = env->GetMethodID(clazz, "Callback", \
"(Lorg/iotivity/base/OCResource;)V");
if(mid == NULL) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "FoundResource.Callback() is not defined in JAVA");
return;
}
__android_log_print(ANDROID_LOG_INFO, TAG, "calling JAVA FindCallback");
try {
env->CallVoidMethod(callbackContext->m_callBackFunction, mid, jresource);
} catch(OC::OCException& e) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "callbackContext() exception : %s", e.reason(e).c_str());
}
__android_log_print(ANDROID_LOG_INFO, TAG, "fineResourceCB detach");
env->DeleteLocalRef(jresource);
g_JavaVM->DetachCurrentThread();
}
);
}
catch(OC::OCException& e) {
__android_log_print(ANDROID_LOG_ERROR, TAG, "FindCB() exception : %s", e.reason(e).c_str());
}
__android_log_print(ANDROID_LOG_ERROR, TAG, "Called oic base findresource()\n");
return 0;
}
//******************************************************************
//
// Copyright 2014 MediaTek 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 "ocstack-jni.h"
static const char* TAG = "OCREPRESENTATION-JNI";
JNIEXPORT jlong JNICALL OCRepresentationConstructor(JNIEnv *env, jobject jobj)
{
OC::OCRepresentation *rep = new OC::OCRepresentation();
jlong instptr = reinterpret_cast<jlong>(rep);
return instptr;
}
JNIEXPORT jstring JNICALL getUri(JNIEnv *env, jobject jobj)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string uri = rep->getUri();
__android_log_print(ANDROID_LOG_INFO, TAG, "getUri() URI : %s", uri.c_str());
return env->NewStringUTF(uri.c_str());
}
JNIEXPORT jint JNICALL getValueInt(JNIEnv *env, jobject jobj, jstring jstr)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string str = env->GetStringUTFChars(jstr,0);
int val;
rep->getValue(str, val);
__android_log_print(ANDROID_LOG_INFO, TAG, "getValueInt() : %d", val);
return((jint) val);
}
JNIEXPORT jboolean JNICALL getValueBool(JNIEnv *env, jobject jobj, jstring jstr)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string str = env->GetStringUTFChars(jstr,0);
bool val;
rep->getValue(str, val);
__android_log_print(ANDROID_LOG_INFO, TAG, "getValueBool() : %d", val);
return((jboolean) val);
}
JNIEXPORT jstring JNICALL getValueString(JNIEnv *env, jobject jobj, jstring jstr)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string str = env->GetStringUTFChars(jstr,0);
string get_val;
rep->getValue(str, get_val);
__android_log_print(ANDROID_LOG_INFO, TAG, "getValueString() : %s", get_val.c_str());
return env->NewStringUTF(get_val.c_str());
}
JNIEXPORT void JNICALL setValueInt(JNIEnv *env, jobject jobj, jstring jstr, jint jval)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string str = env->GetStringUTFChars(jstr,0);
rep->setValue(str, jval);
}
JNIEXPORT void JNICALL setValueBool(JNIEnv *env, jobject jobj, jstring jstr, jboolean jval)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string str = env->GetStringUTFChars(jstr,0);
rep->setValue(str, (bool) jval);
}
JNIEXPORT void JNICALL setValueString(JNIEnv *env, jobject jobj, jstring jstr, jstring jval)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string str = env->GetStringUTFChars(jstr,0);
string val = env->GetStringUTFChars(jval,0);
rep->setValue(str, val);
}
JNIEXPORT jboolean JNICALL hasAttribute(JNIEnv *env, jobject jobj, jstring jstr)
{
OC::OCRepresentation *rep = getHandle<OC::OCRepresentation>(env, jobj);
string str = env->GetStringUTFChars(jstr,0);
return rep->hasAttribute(str);
}
This diff is collapsed.
//******************************************************************
//
// Copyright 2014 MediaTek 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 "ocstack-jni.h"
// Global map to maintain callback functions and ocresource
// with key = URL + callback operation
// e.g. key = /a/light/GET
std::map<string, JNICallBackContext*> gJNICallBackContextList;
JavaVM* g_JavaVM = NULL;
jclass g_ocplatform_clazz;
jclass g_ocresource_clazz;
jclass g_ocheaderoption_clazz;
jclass g_ocrepresentation_clazz;
jclass g_platformcfg_clazz;
static const char* TAG = "OCSTACK-JNI";
static JNINativeMethod ocplatform_method_table[] = {
{ "configure", "(Lorg/iotivity/base/PlatformConfig;)V", (void *) configure},
{ "findResource", "(Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/AbstractFindCallback;)I", (void *) findResource }
};
static int ocplatform_method_table_size = sizeof(ocplatform_method_table) / sizeof(ocplatform_method_table[0]);
static JNINativeMethod platformconfig_method_table[] = {
{ "createNativeInstance", "(IILjava/lang/String;II)J", (void *) createNativeInstance },
};
static int platformconfig_method_table_size = sizeof(platformconfig_method_table) / sizeof(platformconfig_method_table[0]);
static JNINativeMethod ocresource_method_table[] = {
{ "get", "(Lorg/iotivity/base/AbstractGetCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicGet},
{ "put", "(Lorg/iotivity/base/OCRepresentation;Lorg/iotivity/base/AbstractPutCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicPut},
{ "post", "(Lorg/iotivity/base/OCRepresentation;Lorg/iotivity/base/AbstractPostCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicPost},
{ "observe", "(ILorg/iotivity/base/AbstractObserveCallback;)Lorg/iotivity/base/OCStackResult;", (void *) jniOicObserve},
{ "cancelObserve", "()Lorg/iotivity/base/OCStackResult;", (void *) jniOicCancelObserve},
{ "uri", "()Ljava/lang/String;", (void *) uri},
{ "host", "()Ljava/lang/String;", (void *) host},
{ "getResourceTypes", "()[Ljava/lang/String;", (void *) getResourceTypes},
{ "getResourceInterfaces", "()[Ljava/lang/String;", (void *) getResourceInterfaces},
};
static int ocresource_method_table_size = sizeof(ocresource_method_table) / sizeof(ocresource_method_table[0]);
static JNINativeMethod ocheaderoption_method_table[] = {
{ "getOptionID", "()I", (void *) getOptionID},
{ "getOptionData", "()Ljava/lang/String;", (void *) getOptionData},
{ "OCHeaderOptionConstructor", "(ILjava/lang/String;)J", (void *) OCHeaderOptionConstructor},
};
static int ocheaderoption_method_table_size = sizeof(ocheaderoption_method_table) / sizeof(ocheaderoption_method_table[0]);
static JNINativeMethod ocrepresentation_method_table[] = {
{ "OCRepresentationConstructor", "()J", (void *) OCRepresentationConstructor},
{ "getUri", "()Ljava/lang/String;", (void *) getUri},
{ "getValueInt", "(Ljava/lang/String;)I", (void *) getValueInt},
{ "getValueBool", "(Ljava/lang/String;)Z", (void *) getValueBool},
{ "getValueString", "(Ljava/lang/String;)Ljava/lang/String;", (void *) getValueString},
{ "setValueInt", "(Ljava/lang/String;I)V", (void *) setValueInt},
{ "setValueBool", "(Ljava/lang/String;Z)V", (void *) setValueBool},
{ "setValueString", "(Ljava/lang/String;Ljava/lang/String;)V", (void *) setValueString},
{ "hasAttribute", "(Ljava/lang/String;)Z", (void *) hasAttribute},
};
static int ocrepresentation_method_table_size = sizeof(ocrepresentation_method_table) / sizeof(ocrepresentation_method_table[0]);
// JNI OnLoad
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env;
g_JavaVM = vm;
__android_log_print(ANDROID_LOG_ERROR, TAG, "g_JavaVM = %p\n", g_JavaVM);
if (vm->GetEnv((void **) &env, JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
} else {
jint ret;
jclass clazz;
clazz = env->FindClass("org/iotivity/base/OCPlatform");
g_ocplatform_clazz = (jclass) env->NewGlobalRef(clazz);
env->DeleteLocalRef(clazz);
ret = env->RegisterNatives(g_ocplatform_clazz, ocplatform_method_table, ocplatform_method_table_size);
__android_log_print(ANDROID_LOG_ERROR, TAG, "load ocplatform = %d\n", ret);
clazz = env->FindClass("org/iotivity/base/OCResource");
g_ocresource_clazz = (jclass) env->NewGlobalRef(clazz);
env->DeleteLocalRef(clazz);
ret = env->RegisterNatives(g_ocresource_clazz, ocresource_method_table, ocresource_method_table_size);
__android_log_print(ANDROID_LOG_ERROR, TAG, "load ocresource = %d\n", ret);
clazz = env->FindClass("org/iotivity/base/PlatformConfig");
g_platformcfg_clazz = (jclass) env->NewGlobalRef(clazz);
env->DeleteLocalRef(clazz);
ret = env->RegisterNatives(g_platformcfg_clazz, platformconfig_method_table, platformconfig_method_table_size);
__android_log_print(ANDROID_LOG_ERROR, TAG, "load platformconfig = %d\n", ret);
clazz = env->FindClass("org/iotivity/base/OCHeaderOption");
g_ocheaderoption_clazz = (jclass) env->NewGlobalRef(clazz);
env->DeleteLocalRef(clazz);
ret = env->RegisterNatives(g_ocheaderoption_clazz, ocheaderoption_method_table, ocheaderoption_method_table_size);
__android_log_print(ANDROID_LOG_ERROR, TAG, "load ocheaderoption = %d\n", ret);
clazz = env->FindClass("org/iotivity/base/OCRepresentation");
g_ocrepresentation_clazz = (jclass) env->NewGlobalRef(clazz);
env->DeleteLocalRef(clazz);
ret = env->RegisterNatives(g_ocrepresentation_clazz, ocrepresentation_method_table, ocrepresentation_method_table_size);
__android_log_print(ANDROID_LOG_ERROR, TAG, "load ocrepresentation = %d\n", ret);
return JNI_VERSION_1_6;
}
}
//******************************************************************
//
// Copyright 2014 MediaTek All Rights Reserved.
//