Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <jni.h>
- #include <android/log.h>
- #include <pthread.h>
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include "com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication.h"
- #include "libcpe_api.h"
- #include "libcpe.h"
- #ifndef _Included_com_pnrs_RemoteFileManagerCPE_jni_main
- #define _Included_com_pnrs_RemoteFileManagerCPE_jni_main
- // Header for class com_rtrk_myjni_MyJNIActivity
- #define DEBUG_TAG "### NDK_ServerCommunication"
- pthread_mutex_t mutexlock;
- //typedef void* cpe_ctx_t;
- static cpe_ctx_t cpe;
- //static void* cpe;
- static JavaVM *jVM = NULL; // Java VM
- static jobject mObject; // Global reference to object of TestJNIActivity class
- static jclass mClass; // Global reference to TestJNIActivity class
- jmethodID setClientParameter; // callback to Android (Method ID of a toast() method in TestJNI Activity class)
- char* msgBuffer;
- /**
- * Slanje poruke u javu.
- *<br>
- * Sending message (*param) to java.
- *@param char *param C string;
- *@return returning error code if error invoked
- */
- unsigned int setParameter(char *param) {
- // Get param to JNI string and pass it to Java
- JNIEnv *env;
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "SET CLIENT PARAMETER CALLING TO JAVA METHOD");
- int attached = -1;
- // get the ENV for this thread
- jint res = jVM->GetEnv((void **) &env, JNI_VERSION_1_4);
- if (res == JNI_EDETACHED) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "Attaching current thread");
- attached = jVM->AttachCurrentThread(&env, NULL);
- if (attached != 0) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "Failed to attach current thread");
- return 1;
- }
- } else if (res == JNI_EVERSION) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "The specified interface is not supported");
- return 1;
- }
- if (env == NULL) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Cannot get ENV");
- return 1;
- }
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "INVOKING SET METHOD");
- //************************************************************
- jstring jStr = env->NewStringUTF(param);
- env->CallVoidMethod(mObject, setClientParameter, jStr);
- //************************************************************
- if (attached == 0) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "Detaching current thread %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Call from NDK-compiled native code. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
- jVM->DetachCurrentThread();
- }
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "SET");
- return 0;
- }
- /**
- * Dobijanje poruke iz jave.
- *<br>
- * Getting message from java.
- *@param char **param Pointer to C string (array of chars);
- *@return returning error code if error invoked
- */
- unsigned int getParameter(char **param) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "setMsg() %s",msgBuffer);+
- pthread_mutex_lock (&mutexlock);
- strncpy((*param), msgBuffer, 10000);
- pthread_mutex_unlock (&mutexlock);
- return 0;
- }
- JNIEXPORT
- jint JNICALL
- Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeInitNative(
- JNIEnv *env, jobject thiz) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeInitNative()");
- msgBuffer = new char[10001];
- jint temp = cpe_init("tr135", NULL, &cpe);
- register_set_custom_param(setParameter);
- register_get_custom_param(getParameter);
- if(pthread_mutex_init (&mutexlock , 0 ) != 0){
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Mutex se nije inicijalizovao");
- return (jint)4;
- }
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeInitNative() - finished");
- return temp;
- }
- JNIEXPORT
- jint JNICALL
- Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeStartNative(
- JNIEnv *env, jobject thiz) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeStartNative() - finished");
- return cpe_start(cpe);
- }
- JNIEXPORT
- jint JNICALL
- Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeStopNative(
- JNIEnv *env, jobject thiz) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeStopNative()");
- return cpe_stop(cpe);
- }
- JNIEXPORT
- jint JNICALL
- Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeDestroyNative(
- JNIEnv *env, jobject thiz) {
- delete[] msgBuffer;
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeDestroyNative()");
- jint temp = cpe_destroy(&cpe);
- pthread_mutex_destroy(&mutexlock);
- return temp;
- }
- JNIEXPORT jstring JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeGetVersionInfoNative(
- JNIEnv *env, jobject thiz) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "cpeGetVersionInfoNative() - finished");
- return env->NewStringUTF(cpe_get_version_info());
- }
- JNIEXPORT jint
- JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setRefreshRateNative(
- JNIEnv *env, jobject thiz, jint seconds) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "cpeSetRefreshRateNative() - finished");
- return set_refresh_rate(cpe,seconds);
- }
- JNIEXPORT jint
- JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setServerAdressNative(
- JNIEnv *env, jobject thiz, jstring url) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "cpeSetServerAdressNative() - finished");
- //const char *nativeString = env->GetStringUTFChars(url, 0);
- //jint ret = set_server_adress(cpe,nativeString);
- //env->ReleaseStringUTFChars(url, nativeString);
- return 1;
- }
- JNIEXPORT void
- JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setMsg(
- JNIEnv *env, jobject thiz, jstring msg) {
- const char *str = env->GetStringUTFChars(msg, 0);
- pthread_mutex_lock (&mutexlock);
- strncpy(msgBuffer,str,10000);
- pthread_mutex_unlock (&mutexlock);
- env->ReleaseStringUTFChars(msg, str);
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "setMsg()");
- }
- /**
- *
- *
- * Setting mObject, for using method of specific Instance of class ServerCommunication.
- */
- JNIEXPORT
- void JNICALL
- Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setMObjectNative(JNIEnv *jEnv, jobject jThis)
- {
- mObject = jEnv->NewGlobalRef(jThis);
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeSetMObjectNative() - finished");
- }
- /**
- * Inicialization method. Here we are setting our callback methods setClientParameter
- *
- */
- jint
- JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
- JNIEnv * env;
- jVM = vm;
- if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "Failed to get the environment using GetEnv()");
- return -1;
- }
- mClass = env->FindClass(
- "com/rtrk/pnrs/remotefilemanager/cpe/service/ServerCommunication");
- // setClientParameter
- if (mClass == NULL) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "Failed to find class ServerCommunication");
- return -1;
- }
- mClass = (jclass) env->NewGlobalRef(mClass);
- //mObject = NULL;
- if (mClass == NULL) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "Failed to find class in native");
- return -1;
- }
- setClientParameter = env->GetMethodID(mClass, "setClientParameter",
- "(Ljava/lang/String;)V");
- if (setClientParameter == NULL) {
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "Failed to find method setClientParameter() in class ServerCommunication");
- return -1;
- }
- __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
- "ON LOAD OK. Call from NDK-compiled native code.");
- return JNI_VERSION_1_4;
- }
- #endif
- #ifdef __cplusplus
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment