java实现看门狗_Android看门狗的实现 | 学步园

1、jni实现。

2、需要Root。

3、设备的/dev/watchdog文件存在。

jni文件的.C文件

#include

//#include

#include

#include

#include

#include

//struct watchdog_info{

// unsigned int options; //options the card/driver supprots 19

// unsigned int firmware_version; //firmcard version of the card

// unsigned char identity[32]; //identity of the board 21

// };

#define WATCHDOG_IOCTL_BASE 'W'

//#define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)

#define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)

#define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int)

#define WDIOS_DISABLECARD 0x0001 /* Turn off the watchdog timer */

#define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */

#define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)

#define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)

#include "android/log.h"

static const char *TAG="serial_port";

#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)

#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)

#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)

//int main()

JNIEXPORT jint JNICALL Java_com_aaa_bbb_cccTestWatchdog_main

(JNIEnv *env, jobject thiz, jint timeout)

{

int fd = -1;

fd = open("/dev/watchdog", O_RDWR); //打开设备

int timesout = timeout; //定时时间为10秒

if (fd < 0) {

printf("Open File /dev/watchdog Error!\n");

LOGD("open() watchdog fd = %d", fd);

return -1;

}

LOGD("open()watchdog fd =ok2 %d", timesout);

ioctl(fd, WDIOC_SETOPTIONS, WDIOS_ENABLECARD);//使能看门狗

ioctl(fd, WDIOC_SETTIMEOUT, ×out);//设置超时时间

/*

在循环10次后系统复位

如果在循环中加入喂狗指令 ioctl(fd, WDIOC_KEEPALIVE, 0),那么程序继续运行,系统不会复位

*/

//while (1) {

//sleep(1);

//}

close(fd);

return 0;

}

Android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

TARGET_PLATFORM := android-3

LOCAL_MODULE := WatchDog

LOCAL_SRC_FILES := WatchDog.c

LOCAL_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

.java文件的调用

/**

* 设置watchdog为可读写

*/

private boolean chmodExec() {

Process process = null;

DataOutputStream os = null;

if (!isRun) {

return false;

}

try {

String cmd = "busybox chmod 666 /dev/watchdog";

System.out.println("_____watchdog "+cmd);

process = Runtime.getRuntime().exec("su"); //切换到root帐号

os = new DataOutputStream(process.getOutputStream());

os.writeBytes(cmd + "\n");

os.writeBytes("exit\n");

os.flush();

process.waitFor();

} catch (Exception e) {

return false;

} finally {

try {

if(os !=null){

os.close();

}

process.destroy();

} catch (Exception e) {

}

}

return true;

}

// JNI

public native int main(int timeout);

static {

System.loadLibrary("WatchDog");

}

//先调用chmodExec();再

//int i = main(10); //看门狗超时时间为10秒

//if(i<0){

//;//设置成功

//}

OK,这样就可以了。Good Luck.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值