| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#include <pj/guid.h> |
| 21 |
#include <pj/log.h> |
| 22 |
#include <pj/os.h> |
| 23 |
#include <pj/string.h> |
| 24 |
|
| 25 |
#include <jni.h> |
| 26 |
|
| 27 |
PJ_DEF_DATA(const unsigned) PJ_GUID_STRING_LENGTH=36; |
| 28 |
|
| 29 |
PJ_DEF(unsigned) pj_GUID_STRING_LENGTH() |
| 30 |
{ |
| 31 |
return PJ_GUID_STRING_LENGTH; |
| 32 |
} |
| 33 |
|
| 34 |
PJ_DEF(pj_str_t*) pj_generate_unique_string(pj_str_t *str) |
| 35 |
{ |
| 36 |
jclass uuid_class; |
| 37 |
jmethodID get_uuid_method; |
| 38 |
jmethodID to_string_method; |
| 39 |
JNIEnv *jni_env = 0; |
| 40 |
jobject javaUuid; |
| 41 |
jstring uuid_string; |
| 42 |
const char *native_string; |
| 43 |
pj_str_t native_str; |
| 44 |
|
| 45 |
pj_bool_t attached = pj_jni_attach_jvm((void **)&jni_env); |
| 46 |
if (!jni_env) |
| 47 |
goto on_error; |
| 48 |
|
| 49 |
uuid_class = (*jni_env)->FindClass(jni_env, "java/util/UUID"); |
| 50 |
|
| 51 |
if (uuid_class == 0) |
| 52 |
goto on_error; |
| 53 |
|
| 54 |
get_uuid_method = (*jni_env)->GetStaticMethodID(jni_env, uuid_class, |
| 55 |
"randomUUID", |
| 56 |
"()Ljava/util/UUID;"); |
| 57 |
if (get_uuid_method == 0) |
| 58 |
goto on_error; |
| 59 |
|
| 60 |
javaUuid = (*jni_env)->CallStaticObjectMethod(jni_env, uuid_class, |
| 61 |
get_uuid_method); |
| 62 |
if (javaUuid == 0) |
| 63 |
goto on_error; |
| 64 |
|
| 65 |
to_string_method = (*jni_env)->GetMethodID(jni_env, uuid_class, |
| 66 |
"toString", |
| 67 |
"()Ljava/lang/String;"); |
| 68 |
if (to_string_method == 0) |
| 69 |
goto on_error; |
| 70 |
|
| 71 |
uuid_string = (*jni_env)->CallObjectMethod(jni_env, javaUuid, |
| 72 |
to_string_method); |
| 73 |
if (uuid_string == 0) |
| 74 |
goto on_error; |
| 75 |
|
| 76 |
native_string = (*jni_env)->GetStringUTFChars(jni_env, uuid_string, |
| 77 |
JNI_FALSE); |
| 78 |
if (native_string == 0) |
| 79 |
goto on_error; |
| 80 |
|
| 81 |
native_str.ptr = (char *)native_string; |
| 82 |
native_str.slen = pj_ansi_strlen(native_string); |
| 83 |
pj_strncpy(str, &native_str, PJ_GUID_STRING_LENGTH); |
| 84 |
|
| 85 |
(*jni_env)->ReleaseStringUTFChars(jni_env, uuid_string, native_string); |
| 86 |
(*jni_env)->DeleteLocalRef(jni_env, javaUuid); |
| 87 |
(*jni_env)->DeleteLocalRef(jni_env, uuid_class); |
| 88 |
(*jni_env)->DeleteLocalRef(jni_env, uuid_string); |
| 89 |
pj_jni_detach_jvm(attached); |
| 90 |
|
| 91 |
return str; |
| 92 |
|
| 93 |
on_error: |
| 94 |
PJ_LOG(2, ("guid_android.c", ("Error generating UUID"))); |
| 95 |
pj_jni_detach_jvm(attached); |
| 96 |
return NULL; |
| 97 |
} |