首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C:从kernel-module写文件正确吗?

C:从kernel-module写文件正确吗?
EN

Stack Overflow用户
提问于 2012-06-13 22:22:34
回答 1查看 1.3K关注 0票数 0

我尝试将pcm-bytes (从ALSA-buffer)从内核空间复制/写入到内核模块(LKM)内的一个文件中。

得到的文件是写入的,大小对我来说看起来还可以,但因为它是PCM数据,所以我看不出它是否正确(原始音频数据,不可读)。

我的音频播放器(MPlayer="Invalid seek to negative position

当我通过SCITE打开文件时,我看到许多"NUL“和其他加密的东西(bytes ;)。

也许你们中的一个发现了窃听器?

我有这个脚本:

代码语言:javascript
复制
unsigned char *dma_area; // this is the source, an mmapped-area
int pcm_offset_bytes; // the actual offset in the dma_area
int size_bytes; // the amount of bytes to copy
struct file *target_file; // the target-file


int ret; // used in write-process below
int wrote = 0; // used in write-process below
mm_segment_t fs;
void *data; // dma_area + pcm_offset_bytes

// (..) calculate offset and size

// open the target file
target_file = filp_open("/dev/snd/pcmC0D0p_output", (O_CREAT | O_TRUNC | O_WRONLY), (S_IRWXU | S_IRWXG | S_IRWXO));

data = dma_area + pcm_offset_bytes


fs = get_fs();
set_fs (get_ds ());

if (!target_file || !target_file->f_op || !target_file->f_op->write) {
    LOGI("something's missing\n");
    return -EIO;
}

// loop until every byte is written
while (size_bytes > 0) {

    ret = target_file->f_op->write(target_file, (char *)data, size_bytes, &target_file->f_pos);

    LOGI ("wrote %d bytes to target_file@%p, return %d\n", size_bytes, target_file, ret);
    if (ret <= 0)
        goto done;

    size_bytes -= ret;
    data += ret;
    wrote += ret;
}

ret = wrote;

done:
    set_fs(fs);
    LOGI("result %d\n", ret);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-13 22:32:43

首先,您“不应该”写入内核中的文件。

对于如何播放无头原始pcm这一更恰当的问题,您可以使用audacity的导入函数。

或使用aplay

代码语言:javascript
复制
aplay some_file -t raw -f S16_LE -c 2 -r 44100

使用任何合适的参数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11016927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档