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.