draganrakita

wrapper.c

May 30th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.31 KB | None | 0 0
  1. #include <string.h>
  2. #include <jni.h>
  3. #include <android/log.h>
  4. #include <pthread.h>
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14.  
  15. #include "com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication.h"
  16. #include "libcpe_api.h"
  17. #include "libcpe.h"
  18.  
  19. #ifndef _Included_com_pnrs_RemoteFileManagerCPE_jni_main
  20. #define _Included_com_pnrs_RemoteFileManagerCPE_jni_main
  21.  
  22. // Header for class com_rtrk_myjni_MyJNIActivity
  23.  
  24.  
  25. #define DEBUG_TAG "### NDK_ServerCommunication"
  26.  
  27.  
  28. pthread_mutex_t mutexlock;
  29. //typedef void* cpe_ctx_t;
  30. static cpe_ctx_t cpe;
  31. //static void* cpe;
  32.  
  33. static JavaVM *jVM = NULL; // Java VM
  34. static jobject mObject; // Global reference to object of TestJNIActivity class
  35. static jclass mClass; // Global reference to TestJNIActivity class
  36.  
  37. jmethodID setClientParameter; // callback to Android (Method ID of a toast() method in TestJNI Activity class)
  38.  
  39. char* msgBuffer;
  40.  
  41. /**
  42.  * Slanje poruke u javu.
  43.  *<br>
  44.  * Sending message (*param) to java.
  45.  *@param char *param C string;
  46.  *@return returning error code if error invoked
  47.  */
  48. unsigned int setParameter(char *param) {
  49.     // Get param to JNI string and pass it to Java
  50.     JNIEnv *env;
  51.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  52.             "SET CLIENT PARAMETER CALLING TO JAVA METHOD");
  53.  
  54.     int attached = -1;
  55.     // get the ENV for this thread
  56.     jint res = jVM->GetEnv((void **) &env, JNI_VERSION_1_4);
  57.     if (res == JNI_EDETACHED) {
  58.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  59.                 "Attaching current thread");
  60.         attached = jVM->AttachCurrentThread(&env, NULL);
  61.         if (attached != 0) {
  62.             __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  63.                     "Failed to attach current thread");
  64.             return 1;
  65.         }
  66.     } else if (res == JNI_EVERSION) {
  67.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  68.                 "The specified interface is not supported");
  69.         return 1;
  70.     }
  71.  
  72.     if (env == NULL) {
  73.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Cannot get ENV");
  74.         return 1;
  75.     }
  76.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "INVOKING SET METHOD");
  77.  
  78.     //************************************************************
  79.     jstring jStr = env->NewStringUTF(param);
  80.     env->CallVoidMethod(mObject, setClientParameter, jStr);
  81.  
  82.     //************************************************************
  83.     if (attached == 0) {
  84.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  85.                 "Detaching current thread %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Call from NDK-compiled native code. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
  86.         jVM->DetachCurrentThread();
  87.     }
  88.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "SET");
  89.  
  90.     return 0;
  91.  
  92. }
  93. /**
  94.  * Dobijanje poruke iz jave.
  95.  *<br>
  96.  * Getting message from java.
  97.  *@param char **param Pointer to C string (array of chars);
  98.  *@return returning error code if error invoked
  99.  */
  100. unsigned int getParameter(char **param) {
  101.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "setMsg() %s",msgBuffer);+
  102.  
  103.     pthread_mutex_lock (&mutexlock);
  104.     strncpy((*param), msgBuffer, 10000);
  105.     pthread_mutex_unlock (&mutexlock);
  106.  
  107.     return 0;
  108. }
  109.  
  110. JNIEXPORT
  111. jint JNICALL
  112. Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeInitNative(
  113.         JNIEnv *env, jobject thiz) {
  114.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeInitNative()");
  115.  
  116.     msgBuffer = new char[10001];
  117.     jint temp = cpe_init("tr135", NULL, &cpe);
  118.  
  119.     register_set_custom_param(setParameter);
  120.     register_get_custom_param(getParameter);
  121.  
  122.     if(pthread_mutex_init (&mutexlock , 0 ) != 0){
  123.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "Mutex se nije inicijalizovao");
  124.         return (jint)4;
  125.     }
  126.  
  127.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeInitNative() - finished");
  128.     return temp;
  129. }
  130.  
  131. JNIEXPORT
  132. jint JNICALL
  133. Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeStartNative(
  134.         JNIEnv *env, jobject thiz) {
  135.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeStartNative() - finished");
  136.  
  137.     return cpe_start(cpe);
  138.  
  139. }
  140.  
  141. JNIEXPORT
  142. jint JNICALL
  143. Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeStopNative(
  144.         JNIEnv *env, jobject thiz) {
  145.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeStopNative()");
  146.     return cpe_stop(cpe);
  147. }
  148.  
  149. JNIEXPORT
  150. jint JNICALL
  151. Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeDestroyNative(
  152.         JNIEnv *env, jobject thiz) {
  153.     delete[] msgBuffer;
  154.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeDestroyNative()");
  155.     jint temp = cpe_destroy(&cpe);
  156.     pthread_mutex_destroy(&mutexlock);
  157.     return temp;
  158. }
  159.  
  160. JNIEXPORT jstring JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_cpeGetVersionInfoNative(
  161.         JNIEnv *env, jobject thiz) {
  162.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  163.             "cpeGetVersionInfoNative() - finished");
  164.  
  165.     return env->NewStringUTF(cpe_get_version_info());
  166.  
  167. }
  168.  
  169. JNIEXPORT jint
  170. JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setRefreshRateNative(
  171.         JNIEnv *env, jobject thiz, jint seconds) {
  172.  
  173.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  174.             "cpeSetRefreshRateNative() - finished");
  175.     return set_refresh_rate(cpe,seconds);
  176. }
  177.  
  178. JNIEXPORT jint
  179. JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setServerAdressNative(
  180.         JNIEnv *env, jobject thiz, jstring url) {
  181.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  182.             "cpeSetServerAdressNative() - finished");
  183.     //const char *nativeString = env->GetStringUTFChars(url, 0);
  184.  
  185.     //jint ret =  set_server_adress(cpe,nativeString);
  186.     //env->ReleaseStringUTFChars(url, nativeString);
  187.     return 1;
  188. }
  189.  
  190. JNIEXPORT void
  191. JNICALL Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setMsg(
  192.         JNIEnv *env, jobject thiz, jstring msg) {
  193.  
  194.     const char *str = env->GetStringUTFChars(msg, 0);
  195.     pthread_mutex_lock (&mutexlock);
  196.     strncpy(msgBuffer,str,10000);
  197.     pthread_mutex_unlock (&mutexlock);
  198.     env->ReleaseStringUTFChars(msg, str);
  199.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "setMsg()");
  200. }
  201. /**
  202.  *
  203.  *
  204.  * Setting mObject, for using method of specific Instance of class ServerCommunication.
  205.  */
  206. JNIEXPORT
  207. void JNICALL
  208. Java_com_rtrk_pnrs_remotefilemanager_cpe_service_ServerCommunication_setMObjectNative(JNIEnv *jEnv, jobject jThis)
  209. {
  210.     mObject = jEnv->NewGlobalRef(jThis);
  211.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG, "cpeSetMObjectNative() - finished");
  212.  
  213. }
  214.  
  215.  
  216.  
  217. /**
  218.  * Inicialization method. Here we are setting our callback methods setClientParameter
  219.  *
  220.  */
  221. jint
  222. JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
  223.     JNIEnv * env;
  224.     jVM = vm;
  225.  
  226.     if (vm->GetEnv((void **) &env, JNI_VERSION_1_4) != JNI_OK) {
  227.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  228.                 "Failed to get the environment using GetEnv()");
  229.         return -1;
  230.     }
  231.  
  232.     mClass = env->FindClass(
  233.             "com/rtrk/pnrs/remotefilemanager/cpe/service/ServerCommunication");
  234.  
  235. //    setClientParameter
  236.     if (mClass == NULL) {
  237.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  238.                 "Failed to find class ServerCommunication");
  239.         return -1;
  240.     }
  241.  
  242.     mClass = (jclass) env->NewGlobalRef(mClass);
  243.     //mObject = NULL;
  244.     if (mClass == NULL) {
  245.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  246.                         "Failed to find class in native");
  247.         return -1;
  248.     }
  249.  
  250.  
  251.     setClientParameter = env->GetMethodID(mClass, "setClientParameter",
  252.             "(Ljava/lang/String;)V");
  253.  
  254.     if (setClientParameter == NULL) {
  255.         __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  256.                 "Failed to find method setClientParameter() in class ServerCommunication");
  257.         return -1;
  258.     }
  259.  
  260.  
  261.     __android_log_print(ANDROID_LOG_DEBUG, DEBUG_TAG,
  262.             "ON LOAD OK. Call from NDK-compiled native code.");
  263.     return JNI_VERSION_1_4;
  264. }
  265.  
  266.  
  267.  
  268. #endif
  269. #ifdef __cplusplus
  270. }
  271. #endif
Advertisement
Add Comment
Please, Sign In to add comment