当前位置: 首页 > article >正文

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());

			}
	}


http://www.kler.cn/a/282630.html

相关文章:

  • 字节青训-小M的多任务下载器挑战、版本号比较
  • 机器学习 ---线性回归
  • flink实战 -- flink SQL 实现列转行
  • 新手小白学习docker第八弹------实现MySQL主从复制搭建
  • Android音视频直播低延迟探究之:WLAN低延迟模式
  • 用 Python 从零开始创建神经网络(三):添加层级(Adding Layers)
  • 深入理解微服务中的负载均衡算法与配置策略
  • Python实现Http Server及Https Server
  • Kafka的Offset(偏移量)详解
  • 爆改YOLOv8 | 利用CPA-Enhancer提高低照度物体检测(适用于雨,雪,雾天)
  • hadoop的sbin
  • Redis 实现哨兵模式
  • 买入股票的思维法
  • [米联客-XILINX-H3_CZ08_7100] FPGA程序设计基础实验连载-18 SPI接口ADC采集驱动设计
  • 操作系统信号量
  • 【数据结构-二维前缀和】力扣1314. 矩阵区域和
  • Linux学习(15)-网络编程:滑动窗口、拥塞控制、udp
  • HTML 总结
  • 数据挖掘之分类算法
  • Java框架Spring(一)
  • 向量数据库Faiss的搭建与使用|Faiss|向量数据库|高效检索|机器学习|大规模数据
  • 大模型Prompt提示设计简介(2):有效的建议
  • 在C语言中使用POSIX线程库(pthread)实现多线程编程
  • Redis多线程特性
  • CSS中禁用DOM事件
  • OpenCV绘图函数(12)绘制直线函数 line()的使用