432
技術社區[雲棲]
Linux驅動程序:統計單詞個數
本例為Android升讀探索(卷1):HAL與驅動開發 一書中附帶的示例程序。現粘貼出來,以便查閱。
終端操作,可能用到的命令:
insmond word_count.ko lsmod | grep word_count 查看驅動是否安裝成功 rmmod word_count dmesg | grep word_cout | tail -n 2 查看有linux驅動輸出的日誌信息 cat /var/log/syslong | grep word_count | tail -n 2 modinfo word_count.ko 查看驅動的信息
驅動源代碼
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <asm/uaccess.h>
// 定義設備文件名
#define DEVICE_NAME "wordcount"
static unsigned char mem[10000]; // 保存向設備文件寫入的數據
static int word_count = 0;
#define TRUE 255
#define FALSE 0
// 判斷指定字符是否為空格(包括空格符、製表符、回車符和換行符)
static unsigned char is_spacewhite(char c)
{
if (c == 32 || c == 9 || c == 13 || c == 10)
return TRUE;
else
return FALSE;
}
static int get_word_count(const char *buf)
{
int n = 1;
int i = 0;
char c = ' ';
char flag = 0; // 處理多個空格分隔的情況,0:正常情況,1:已遇到一個空格
if (*buf == '\0')
return 0;
// 第1個字符是空格,從0開始計數
if (is_spacewhite(*buf) == TRUE)
n--;
// 掃描字符串中的每一個字符
for (; (c = *(buf + i)) != '\0'; i++)
{
// 隻由一個空格分隔單詞的情況
if (flag == 1 && is_spacewhite(c) == FALSE)
{
flag = 0;
}
// 由多個空格分隔單詞的情況,忽略多餘的空格
else if (flag == 1 && is_spacewhite(c) == TRUE)
{
continue;
}
// 當前字符為空格是單詞數加1
if (is_spacewhite(c) == TRUE)
{
n++;
flag = 1;
}
}
// 如果字符串以一個或多個空格結尾,不計數(單詞數減1)
if (is_spacewhite(*(buf + i - 1)) == TRUE)
n--;
return n;
}
static ssize_t word_count_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
unsigned char temp[4];
temp[0] = word_count >> 24;
temp[1] = word_count >> 16;
temp[2] = word_count >> 8;
temp[3] = word_count;
if (copy_to_user(buf, (void*) temp, 4))
{
return -EINVAL;
}
printk("read:word count:%d", (int) count);
return count;
}
static ssize_t word_count_write(struct file *file, const char __user *buf,
size_t count, loff_t *ppos)
{
ssize_t written = count;
if (copy_from_user(mem, buf, count))
{
return -EINVAL;
}
mem[count] = '\0';
word_count = get_word_count(mem);
printk("write:word count:%d\n", (int) word_count);
return written;
}
// 描述與設備文件觸發的事件對應的回調函數指針
static struct file_operations dev_fops =
{ .owner = THIS_MODULE, .read = word_count_read, .write = word_count_write };
// 描述設備文件的信息
static struct miscdevice misc =
{ .minor = MISC_DYNAMIC_MINOR, .name = DEVICE_NAME, .fops = &dev_fops };
// 初始化Linux驅動
static int __init word_count_init(void)
{
int ret;
// 建立設備文件
ret = misc_register(&misc);
// 輸出日誌信息
printk("word_count_init_success\n");
return ret;
}
// 卸載Linux驅動
static void __exit word_count_exit(void)
{
// 刪除設備文件
misc_deregister(&misc);
// 輸出日誌信息
printk("word_count_init_exit_success\n");
}
// 注冊初始化Linux驅動的函數
module_init( word_count_init);
// 注冊卸載Linux驅動的函數
module_exit( word_count_exit);
MODULE_AUTHOR("lining");
MODULE_DESCRIPTION("statistics of word count.");
MODULE_ALIAS("word count module.");
MODULE_LICENSE("GPL");
測試代碼
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int testdev;
unsigned char buf[4];
testdev = open("/dev/wordcount", O_RDWR);
if (testdev == -1)
{
printf("Cann't open file \n");
return 0;
}
if (argc > 1)
{
write(testdev, argv[1], strlen(argv[1]));
printf("string:%s\n", argv[1]);
}
read(testdev, buf, 4);
int n = 0;
// 將4個字節還原成int類型的值
n = ((int) buf[0]) << 24 | ((int) buf[1]) << 16 | ((int) buf[2]) << 8
| ((int) buf[3]);
printf("word byte display:%d,%d,%d,%d\n", buf[0], buf[1], buf[2], buf[3]);
printf("word count:%d\n", n);
close(testdev);
return 0;
}
最後更新:2017-04-03 07:56:55