主要实现功能:使用Jni调用c语言,访问块设备
文件描述:
- TestJni.java 是java程序
- navite.c 是jni程序(java调用c语言)
- tty.c 驱动程序
- Makefile 通用makefile
操作步骤:
TestJni.java 和 navite.c在一个文件夹里 ,在这个目录下执行以下操作:
1. 执行javac TestJni.java 生成TestJni.class文件;
2. 执行javah TestJni 生成TestJni.h文件;
3. 执行命令,创建动态链接库:gcc -shared -fPIC navite.c -o libnavite.so -I /usr/local/jdk1.6.0_29/include/ -I /usr/local/jdk1.6.0_29/include/linux
4. 执行命令,把创建在当前文件的动态库拷贝到系统库里:sudo cp lib$a.so /usr/lib
TestJni.java
public class TestJni
{
static
{
System.loadLibrary("native");
}
public native int open(String path);
public native void close(int i);
public native String read();
public native int write();
public native long ioctl();
public static void main (String [] args)
{
/* code */
int fs;
TestJni d = new TestJni();
System.out.println (fs = d.open("/dev/tty_led"));
d.write();
System.out.println(d.read());
d.close(fs);
}
}
navite.c
#include<jni.h>
#include<errno.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdlib.h>
jint fd;
char buff[128];
jint c_open(JNIEnv *env,jobject obj,jstring prompt)
{
system("sudo chmod 777 /dev/tty_led");
const char *str;
str = (*env)->GetStringUTFChars(env, prompt, NULL);
if (str == NULL) {
return 1; /* OutOfMemoryError already thrown */
}
fd = open(str ,O_RDWR);
if(fd < 0)
{
perror("open");
return 1;
}
printf("c_open =%s \n",str);
(*env)->ReleaseStringUTFChars(env, prompt, str);
return fd;
}
void c_close(JNIEnv *env,jobject obj,jint fs)
{
close(fs);
printf("c_close \n");
return ;
}
jstring c_read(JNIEnv *env,jobject obj)
{
jint rdnm;
rdnm = read(fd,buff,sizeof(buff));
if(rdnm == -1)
{
perror("read");
}
printf("c_read = %s \n",buff);
return (*env) -> NewStringUTF(env,buff);
}
jint c_write(JNIEnv *env,jobject obj)
{
jint wdnm;
wdnm = write(fd,"hello_me",9);
if(wdnm == -1)
{
perror("write");
return 1;
}
printf("c_write \n");
return wdnm;
}
static JNINativeMethod method[] ={
"open","(Ljava/lang/String;)I",(void *)c_open,
"close","(I)V",(void *)c_close,
"read","()Ljava/lang/String;",(void *)c_read,
"write","()I",(void *)c_write,
};
JNIEXPORT JNICALL
JNI_OnLoad(JavaVM *jvm, void *reserved)
{
printf("***********************\n");
JNIEnv *env;
jclass cls;
if ((*jvm)->GetEnv(jvm, (void **)&env, JNI_VERSION_1_2)) {
return JNI_ERR; /*获得jvm环境*/
}
cls = (*env)->FindClass(env, "TestJni");/*找类*/
if (cls == NULL) {
return JNI_ERR;
}
(*env)->RegisterNatives(env, cls, method, 4);/*注册*/
return JNI_VERSION_1_2;
}
tty.c
#include<linux/module.h>
#include<linux/kernel.h>
#include<linux/fs.h>
#include<asm/io.h>
#include<asm/uaccess.h>
#include<linux/device.h>
#include<linux/cdev.h>
#include<linux/init.h>
MODULE_LICENSE("GPL");
#define TTY_MAJOR 500
#define TTY_MINOR 0
static dev_t from;
static struct cdev tty_cdev;
static struct class *mtty;
static char buff[]={"jjjkkk"};
static ssize_t tty_read (struct file *filp, char __user *user, size_t size, loff_t *loff)
{
copy_to_user(user, buff, sizeof(buff));
printk("tty_read \n");
return 0;
}
ssize_t tty_write (struct file *filp, const char __user *user, size_t size, loff_t *loff)
{
printk("tty_write \n");
return 0;
}
long tty_unlocked_ioctl (struct file *filp, unsigned int cmd, unsigned long num)
{
printk("tty_unlocked_ioctl \n");
return 0;
}
int tty_open (struct inode *inode, struct file *filp)
{
printk("tty_open \n");
return 0;
}
int tty_release (struct inode *inode, struct file *filp)
{
printk("tty_release \n");
return 0;
}
struct file_operations fopes = {
.owner = THIS_MODULE,
.read = tty_read,
.write= tty_write,
.unlocked_ioctl = tty_unlocked_ioctl,
.open= tty_open,
.release=tty_release,
};
static int tty_init(void)
{
from = MKDEV(TTY_MAJOR,TTY_MINOR);
register_chrdev_region(from,1,"tty_led");
cdev_init(&tty_cdev,&fopes);
tty_cdev.owner = THIS_MODULE;
cdev_add(&tty_cdev,from,1);
mtty = class_create(THIS_MODULE,"tty_led");
device_create(mtty,NULL,from,NULL,"tty_led");
printk("tty_init \n");
}
static void tty_exit(void)
{
device_destroy(mtty,from);
class_destroy(mtty);
cdev_del(&tty_cdev);
unregister_chrdev_region( from,1);
printk("tty_exit \n");
}
module_init(tty_init);
module_exit(tty_exit);
Makefile
$(warning KERNELRELEASE=$(KERNELRELEASE))
ifeq ($(KERNELRELEASE),)
#KERNELDIR ?= /home/lht/kernel2.6/linux-2.6.14
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions Module* modules*
.PHONY: modules modules_install clean
else
obj-m := tty.o
endif