How to call a Java function via JNI from a signal handler function in Android -


my objective send signal kernel android service running in userspace.on receiving signal, service should make ioctl call kernel. after gets data kernel through ioctl call,it has display user. this, call native method java service, registers sigaction structure, includes handler function signal. handler function make ioctl call , call java function pass string java service.

here signal.java class

public class signal { static{     system.loadlibrary("signal"); } public native string hello();  public string messageme(string s) {     if(null != mainactivity.muihandler)     {         message msgtoactivity = new message();         msgtoactivity.what = 0;              msgtoactivity.obj  = s; // can put message here          mainactivity.muihandler.sendmessage(msgtoactivity);     }      system.out.println(s);         return s; } 

}

i need call "messageme" function signal handler. here native hello() function registers sigaction structure.

jniexport jstring jnicall java_com_example_service_signal_hello (jnienv * env, jobject obj) {   int configfd; char buf[10]; /* setup signal handler sig_test  * sa_siginfo -> want signal handler function 3 arguments  */ struct sigaction sig; sig.sa_sigaction = receivedata; sig.sa_flags = sa_siginfo; sigaction(sig_test, &sig,null); } 

the receivedata handler function. here goes.

void receivedata(int n, siginfo_t *info,void* unused) {     char buf[200];     char *msg = "hello world";     int fd = -1;     if ((fd = open("/dev/my_device", o_rdwr)) < 0) {             perror("on");             return;     }     if(ioctl(fd, read_ioctl, buf) < 0)             perror("second ioctl");       jstring jstr = (*env)->newstringutf(env, buf);      jclass *clazz = (*env)->getobjectclass(env, obj);      jmethodid messageme = (*env)->getmethodid(env,clazz, "messageme", "(ljava/lang/string;)ljava/lang/string;");      jobject result = (*env)->callobjectmethod(env,obj, messageme, jstr);      printf("message: %s\n", buf); 

} handler function should call java function "messageme" , pass string read kernel argument. not have environment variables jnienv * , jobject obj. when maintained global pointers these variables in hello() function, , used them here, giving noclassdeffounderror. how send string signal handler java function?

i have following global variables. cached these objects future use.

javavm* vm; static jclass cl; static jobject ob; 

in hello function, made global references above variables.

jniexport jstring jnicall java_com_example_service_signal_hello (jnienv * env, jobject obj) { (*env)->getjavavm(env,&vm); ob = (*env)->newglobalref(env,obj); jclass clazz = (*env)->findclass(env,"com/example/service/signal"); cl = (*env)->newglobalref(env,clazz); } 

now used these in handler function

void receivedata(int n, siginfo_t *info,void* unused) { //other code jnienv* env = null;     if(vm==null)         return;     (*vm)->attachcurrentthread(vm,&env,0);  jmethodid messageme = (*env)->getstaticmethodid(env,cl, "messageme", "(ljava/lang/string;)ljava/lang/string;"); jobject result = (*env)->callstaticobjectmethod(env,cl, messageme, jstr); } 

one change made use of static method instead of normal one. didn't have use specific instance of class. if method not static, global variable "ob" can used in callobjectmethod.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -