From c93bf034698d017a05a894c9f354af076d6bb4c6 Mon Sep 17 00:00:00 2001 From: Liong Sauw Ming Date: Thu, 17 Mar 2016 02:56:27 +0000 Subject: Fixed #1909: GUID implementation for Android. Thanks to Johan Lantz for the contribution. git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@5264 74dad513-b988-da41-8d7b-12977e46ad98 --- aconfigure | 17 +++++-- aconfigure.ac | 17 +++++-- pjlib/src/pj/guid_android.c | 117 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+), 10 deletions(-) create mode 100644 pjlib/src/pj/guid_android.c diff --git a/aconfigure b/aconfigure index 33a08f51..82592e76 100755 --- a/aconfigure +++ b/aconfigure @@ -5733,11 +5733,18 @@ case $target in ;; esac # UUID - if test "$ac_has_uuid_lib" = "1" -a "$ac_has_uuid_h" = "1"; then - ac_os_objs="$ac_os_objs guid_uuid.o" - else - ac_os_objs="$ac_os_objs guid_simple.o" - fi + case $target in + *android*) + ac_os_objs="$ac_os_objs guid_android.o" + ;; + *) + if test "$ac_has_uuid_lib" = "1" -a "$ac_has_uuid_h" = "1"; then + ac_os_objs="$ac_os_objs guid_uuid.o" + else + ac_os_objs="$ac_os_objs guid_simple.o" + fi + ;; + esac ;; esac diff --git a/aconfigure.ac b/aconfigure.ac index 3e88124b..27855ff1 100644 --- a/aconfigure.ac +++ b/aconfigure.ac @@ -455,11 +455,18 @@ case $target in ;; esac # UUID - if test "$ac_has_uuid_lib" = "1" -a "$ac_has_uuid_h" = "1"; then - ac_os_objs="$ac_os_objs guid_uuid.o" - else - ac_os_objs="$ac_os_objs guid_simple.o" - fi + case $target in + *android*) + ac_os_objs="$ac_os_objs guid_android.o" + ;; + *) + if test "$ac_has_uuid_lib" = "1" -a "$ac_has_uuid_h" = "1"; then + ac_os_objs="$ac_os_objs guid_uuid.o" + else + ac_os_objs="$ac_os_objs guid_simple.o" + fi + ;; + esac ;; esac diff --git a/pjlib/src/pj/guid_android.c b/pjlib/src/pj/guid_android.c new file mode 100644 index 00000000..02d473c9 --- /dev/null +++ b/pjlib/src/pj/guid_android.c @@ -0,0 +1,117 @@ +/* $Id$ */ +/* + * Copyright (C) 2015-2016 Teluu Inc. (http://www.teluu.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* This original code was kindly contributed by Johan Lantz. + */ +#include +#include +#include + +#include + +extern JavaVM *pj_jni_jvm; + +static pj_bool_t attach_jvm(JNIEnv **jni_env) +{ + if ((*pj_jni_jvm)->GetEnv(pj_jni_jvm, (void **)jni_env, + JNI_VERSION_1_4) < 0) + { + if ((*pj_jni_jvm)->AttachCurrentThread(pj_jni_jvm, jni_env, NULL) < 0) + { + jni_env = NULL; + return PJ_FALSE; + } + return PJ_TRUE; + } + + return PJ_FALSE; +} + +#define detach_jvm(attached) \ + if (attached) \ + (*pj_jni_jvm)->DetachCurrentThread(pj_jni_jvm); + + +PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=37; + +PJ_DEF(unsigned) pj_GUID_STRING_LENGTH() +{ + return PJ_GUID_STRING_LENGTH; +} + +PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) +{ + jclass uuid_class; + jmethodID get_uuid_method; + jmethodID to_string_method; + JNIEnv *jni_env = 0; + jobject javaUuid; + jstring uuid_string; + const char *native_string; + pj_str_t native_str; + + pj_bool_t attached = attach_jvm(&jni_env); + if (!jni_env) + goto on_error; + + uuid_class = (jclass)(*jni_env)->NewGlobalRef(jni_env, + (*jni_env)->FindClass(jni_env, "java/util/UUID")); + if (uuid_class == 0) + goto on_error; + + get_uuid_method = (*jni_env)->GetStaticMethodID(jni_env, uuid_class, + "randomUUID", + "()Ljava/util/UUID;"); + if (get_uuid_method == 0) + goto on_error; + + javaUuid = (*jni_env)->CallStaticObjectMethod(jni_env, uuid_class, + get_uuid_method); + if (javaUuid == 0) + goto on_error; + + to_string_method = (*jni_env)->GetMethodID(jni_env, uuid_class, + "toString", + "()Ljava/lang/String;"); + if (to_string_method == 0) + goto on_error; + + uuid_string = (*jni_env)->CallObjectMethod(jni_env, javaUuid, + to_string_method); + if (uuid_string == 0) + goto on_error; + + native_string = (*jni_env)->GetStringUTFChars(jni_env, uuid_string, + JNI_FALSE); + if (native_string == 0) + goto on_error; + + native_str.ptr = (char *)native_string; + native_str.slen = pj_ansi_strlen(native_string); + pj_strncpy(str, &native_str, str->slen); + + (*jni_env)->ReleaseStringUTFChars(jni_env, uuid_string, native_string); + detach_jvm(attached); + + return str; + +on_error: + PJ_LOG(2, ("guid_android.c", ("Error generating UUID"))); + detach_jvm(attached); + return NULL; +} -- cgit v1.2.3