linux 创建文件节点
如何在linux 系统目录里面创建设备节点给上层调用。如添加/sys/touchscreen/glove 节点,如何JAVA层调用
echo 1 > /sys/touchscreen/glove
(1)驱动内容:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kobject.h>
#include <linux/device.h>
#include "focaltech_core.h"
enum _ex_mode {
MODE_GLOVE = 0,
MODE_COVER,
MODE_CHARGER,
};
static struct kobject *k_obj = NULL;
//static char glove_buf[20] = {0};
static int fts_ex_mode_switch(enum _ex_mode mode, u8 value)
{
int ret = 0;
u8 m_val = 0;
if (value)
m_val = 0x01;
else
m_val = 0x00;
switch (mode) {
case MODE_GLOVE:
ret = fts_write_reg(FTS_REG_GLOVE_MODE_EN, m_val);
if (ret < 0) {
FTS_ERROR("MODE_GLOVE switch to %d fail", m_val);
}
break;
case MODE_COVER:
ret = fts_write_reg(FTS_REG_COVER_MODE_EN, m_val);
if (ret < 0) {
FTS_ERROR("MODE_COVER switch to %d fail", m_val);
}
break;
case MODE_CHARGER:
ret = fts_write_reg(FTS_REG_CHARGER_MODE_EN, m_val);
if (ret < 0) {
FTS_ERROR("MODE_CHARGER switch to %d fail", m_val);
}
break;
default:
FTS_ERROR("mode(%d) unsupport", mode);
ret = -EINVAL;
break;
}
return ret;
}
static ssize_t sysfs_demo_show_glove(struct kobject *kobj, struct kobj_attribute *attr, char * buf)
{
u8 val = 0;
struct fts_ts_data *ts_data = fts_data;
struct input_dev *input_dev = ts_data->input_dev;
pr_info("enter, node: %s\n", attr->attr.name);
mutex_lock(&input_dev->mutex);
fts_read_reg(FTS_REG_GLOVE_MODE_EN, &val);
mutex_unlock(&input_dev->mutex);
return sprintf(buf, "%s: %d\n", "Charger Reg(0x8B)", val);
}
static ssize_t sysfs_demo_store_glove(struct kobject *kobj, struct kobj_attribute *attr, const char * buf, size_t n)
{
//pr_info("enter, node: %s\n", attr->attr.name);
//sprintf(glove_buf, "%s", buf);
//fts_ex_mode_switch(MODE_GLOVE,1);
//return n;
int ret = 0;
struct fts_ts_data *ts_data = fts_data;
if (FTS_SYSFS_ECHO_ON(buf)) {
if (!ts_data->glove_mode) {
FTS_DEBUG("enter glove mode");
ret = fts_ex_mode_switch(MODE_GLOVE, ENABLE);
if (ret >= 0) {
ts_data->glove_mode = ENABLE;
}
}
} else if (FTS_SYSFS_ECHO_OFF(buf)) {
if (ts_data->glove_mode) {
FTS_DEBUG("exit glove mode");
ret = fts_ex_mode_switch(MODE_GLOVE, DISABLE);
if (ret >= 0) {
ts_data->glove_mode = DISABLE;
}
}
}
FTS_DEBUG("glove mode:%d", ts_data->glove_mode);
return n;
}
static struct kobj_attribute glove_attribute =
__ATTR(glove, S_IWUSR|S_IRUGO, sysfs_demo_show_glove, sysfs_demo_store_glove);
static struct attribute *sysfs_demo_attributes[] = {
&glove_attribute.attr,
NULL
};
static const struct attribute_group sysfs_demo_attr_group = {
.attrs = sysfs_demo_attributes,
};
static int __init sysfs_demo_init(void)
{
if ((k_obj = kobject_create_and_add("touchscreen", NULL)) == NULL ) {
pr_err("sysfs_demo sys node create error \n");
goto out;
}
if(sysfs_create_group(k_obj, &sysfs_demo_attr_group) ) {
pr_err("sysfs_create_group failed\n");
goto out2;
}
pr_info("enter.\n");
return 0;
out2:
kobject_put(k_obj);
out:
return -1;
}
module_init(sysfs_demo_init);
static void __exit sysfs_demo_exit(void)
{
pr_info("enter.\n");
if (k_obj) {
sysfs_remove_group(k_obj, &sysfs_demo_attr_group);
kobject_put(k_obj);
}
}
module_exit(sysfs_demo_exit);
MODULE_LICENSE("GPL");
int sysfs_demo_init(void)
{
if ((k_obj = kobject_create_and_add("touchscreen", NULL)) == NULL ) {
pr_err("sysfs_demo sys node create error \n");
goto out;
}
if(sysfs_create_group(k_obj, &sysfs_demo_attr_group) ) {
pr_err("sysfs_create_group failed\n");
goto out2;
}
pr_info("enter.\n");
return 0;
out2:
kobject_put(k_obj);
out:
return -1;
}
void sysfs_demo_exit(void)
{
pr_info("enter.\n");
if (k_obj) {
sysfs_remove_group(k_obj, &sysfs_demo_attr_group);
kobject_put(k_obj);
}
}
/*
int fts_ex_mode_init_glove(struct fts_ts_data *ts_data)
{
//sysfs_demo_init();
return sysfs_demo_init();
}
int fts_ex_mode_exit_glove(struct fts_ts_data *ts_data)
{
sysfs_demo_exit();
return 0;
}
*/
烧录进系统,ls sys 能看到add 近来的touchscreen文件夹,并且近来的glove文件。
(2)JAVA层应用:
package com.demo.myapplication;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//import com.rockchip.newton.UserModeManager;
import android.content.pm.PackageManager;
import android.graphics.Picture;
import android.util.Log;
public class SystemUtil {
private static final String TAG = "SystemUtil";
public static int execShellCmdForStatue(String command) {
int status = -1;
try {
Process p = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = "";
while((s = bufferedReader.readLine()) != null){
Log.d(TAG, " >>>> " + s);
}
status = p.waitFor();
Log.d(TAG, " ________________----------- command: " + command + " status = " + status);
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
public static String execShellCmd(String command) {
String result = "";
Log.i("execShellCmd", command);
try {
//Runtime.getRuntime().exec("sh");
Process process = Runtime.getRuntime().exec(command + "\n");
DataOutputStream stdin = new DataOutputStream(
process.getOutputStream());
DataInputStream stdout = new DataInputStream(
process.getInputStream());
DataInputStream stderr = new DataInputStream(
process.getErrorStream());
String line;
stdin.writeBytes(command);
// 需要stdin.writeBytes才能 echo 1 > /sys/touchscreen/glove
while ((line = stdout.readLine()) != null) {
result += line + "\n";
}
if (result.length() > 0) {
result = result.substring(0, result.length() - 1);
}
while ((line = stderr.readLine()) != null) {
Log.e("EXEC", line);
}
process.waitFor();
} catch (Exception e) {
e.getMessage();
}
return result;
}
public static void echo(String[] command) {
Log.d(TAG, "------echo -------: ");
try {
Process process = Runtime.getRuntime().exec("sh");
DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());
for (String tmpCmd : command) {
Log.d(TAG,"tmpCmd:"+tmpCmd);
outputStream.writeBytes(tmpCmd + "\n");
// 需要stdin.writeBytes才能 echo 1 > /sys/touchscreen/glove
}
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "ex" + e.toString());
}
}