内核模块编译

D-Bus

lazycat posted @ 2012年3月05日 14:43 in linux , 5365 阅读

D-Bus 也是一个用于进程间通信的框架。

简单介绍一下如何编写一个helloworld通信例子。

0. 下载源码

http://www.freedesktop.org/wiki/Software/dbus#Download.

1. ./configure

这时候可能会出现问题:

configure: error: Could not find expat.h, check config.log for failed attempts

可以通过

sudo apt-get install libexpat1-dev

来解决

2. make

3. make install

其中可能遇到的编译问题

1.  fatal error: dbus/dbus.h: No such file or directory

    把/usr/local/include/dbus-1.0/dbus/拷贝到/usr/include/dbus

2. fatal error: dbus/dbus-arch-deps.h: No such file or directory

  /usr/local/lib/dbus-1.0/include/dbus/dbus-arch-deps.h拷贝到/usr/include/dbus

3. 链接错误 collect2: ld returned 1 exit status

 添加链接选项 -l dbus-1

测试代码

服务端接收消息

/* server.c */

#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
    dbus_bool_t handled = false;
    char *word = NULL;
    DBusError dberr;

    if (dbus_message_is_signal(message, "client.signal.Type", "Test")) {
        dbus_error_init(&dberr);
        dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING,
            &word, DBUS_TYPE_INVALID);
        if (dbus_error_is_set(&dberr)) {
            dbus_error_free(&dberr);
        } else {
            printf("receive message %s\n", word);
            handled = true;
        }
    }
    return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}

int main(int argc, char *argv[])
{
    DBusError dberr;
    DBusConnection *dbconn;

    dbus_error_init(&dberr);

    dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
    if (dbus_error_is_set(&dberr)) {
        dbus_error_free(&dberr);
        return -1;
    }

    if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL)) {
        return -1;
    }

    dbus_bus_add_match(dbconn, "type='signal',interface='client.signal.Type'", &dberr);
    if (dbus_error_is_set(&dberr)) {
        dbus_error_free(&dberr);
        return -1;
    }

    while(dbus_connection_read_write_dispatch(dbconn, -1)) {
        /* loop */
    }
    return 0;
}
 
客户端发送消息
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int db_send(DBusConnection *dbconn)
{
    DBusMessage *dbmsg;
    char *word;
    int i;

    dbmsg = dbus_message_new_signal("/client/signal/Object", // object name of the signal
                                  "client.signal.Type",      // interface name of the signal
                                  "Test");                   // name of the signal
    if (!dbmsg) {
        return -1;
    }

    word = "hello world";
    if (!dbus_message_append_args(dbmsg, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID)) {
        return -1;
    }

    if (!dbus_connection_send(dbconn, dbmsg, NULL)) {
        return -1;
    }
    dbus_connection_flush(dbconn);
    printf("send message %s\n", word);

    dbus_message_unref(dbmsg);
    return 0;
}

int main(int argc, char *argv[])
{
    DBusError dberr;
    DBusConnection *dbconn;
    int i;

    dbus_error_init(&dberr);

    dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
    if (dbus_error_is_set(&dberr)) {
        return -1;
    }

    db_send(dbconn);

    dbus_connection_unref(dbconn);

    return 0;
}
 
测试结果如下
root@ubuntu:~/dbus# gcc -o client client.c -l dbus-1
root@ubuntu:~/dbus# gcc -o server server.c -l dbus-1
 
root@ubuntu:~/dbus# ./listen
receive message hello world
 
root@ubuntu:~/dbus# ./send
send message hello world
 
很多API和具体实现还没搞清楚,有待深入研究。
D-Bus的性能会比linux自带的send,recv高么?
Avatar_small
insolitofestival 说:
2024年11月07日 15:38

extraordinarily am searching forward to studying new articles. Maintain up the best work. <a href="https://www.insolitofestival.org/">insolitofestival</a>

Avatar_small
Find out 说:
2024年11月07日 15:40

pleasant publish.. Neat submit. There may be an problem together with your website in firefox, and you could need to check this…

Avatar_small
totodiya 说:
2024年11月07日 15:41

i simply playing each found it though, sick be checking returned soon to discover what extra posts you encompass

Avatar_small
먹튀코비드 说:
2024年11月07日 15:41

Any one can buy an expensive car with the help of extended loan terms and smaller monthly payments. But, are longer loan terms beneficial for your financial health? You have to take a look at the bigger picture and decide.

Avatar_small
check here 说:
2024年11月07日 15:43

Simply want to say your article is as astounding. The clearness in your post is simply excellent and i can assume you’re an expert on this subject.

Avatar_small
good data 说:
2024年11月07日 15:43

This is fantastic! what a blog it is! This webpage provides useful data to us, keep it up. And I would like to thanks for this information that I had been looking. Regards

Avatar_small
토토장터 说:
2024年11月07日 15:45

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.

Avatar_small
check here 说:
2024年11月07日 15:46

This is such a great resource that you are providing and you give it away for free. I love seeing blog that understand the value of providing a quality resource for free

Avatar_small
check here 说:
2024年11月07日 15:46

Useful post. This is my first time I visit here. I found so many intriguing stuff with regards to your blog particularly its conversation. Actually its incredible article. Keep it up.

Avatar_small
먹튀사이트 说:
2024年11月07日 15:47

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

Avatar_small
get more info 说:
2024年11月07日 15:48

Permainan poker online JASA, juga Virtual Account dan QRIS memudahkan anda untuk deposit dan withdraw.

Avatar_small
토토매거진 说:
2024年11月07日 19:02

Dewapoker adalah website game judi online terpercaya yang menggunakan server 24- Jam nonstop. Game judi terbaik semacam poker88 merupakan yang benar-benar dipercaya serta sangat dicari oleh Para Bettor Indonesia yang sering menghadapi kekalahan didalam bermain game. Web dewapoker merupakan agen game kartu online sangat terkenal yang berlimpah dimainkan oleh pemain Indonesia.

Avatar_small
View data 说:
2024年11月07日 19:07

What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.

Avatar_small
먹튀뱅크 说:
2024年11月07日 19:08

To meet the demand of our growing customer base, we expanded our service area to most of Los Angeles County. The main objective of the company is to provide a premium, customer oriented and high quality Plumbing Services to the people of Los Angeles.

Avatar_small
check here 说:
2024年11月07日 19:11

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information

Avatar_small
토토뱃지 说:
2024年11月07日 19:13

When I originally commented I clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I buy four emails with similar comment. Can there be any way it is possible to get rid of me from that service? Thanks

Avatar_small
information 说:
2024年11月07日 19:14

Thank you for sharing this great post, I am very surviv io impressed with your post, the information given is very meticulous and understandable. I will regularly follow your next post.

Avatar_small
토토지식백과 说:
2024年11月07日 19:16

attempting to say thank you ions, that's considering your purchasing listing with careful arranging exactness, discovering the medicinal drugs you require remember that

Avatar_small
click here 说:
2024年11月07日 19:17

When I originally commented I clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I buy four emails with similar comment. Can there be any way it is possible to get rid of me from that service? Thanks

Avatar_small
information 说:
2024年11月07日 19:19

Hei, aku punya saran game kartu online yang seru banget nih. Namanya . Game ini sangat menantang karena butuh kombinasi strategi dan keberuntungan. Ayo kita coba main bersama, dijamin bakal ketagihan!

Avatar_small
read more 说:
2024年11月07日 19:20

Thank you for sharing this great post, I am very surviv io impressed with your post, the information given is very meticulous and understandable. I will regularly follow your next post.

Avatar_small
검증데이 说:
2024年11月07日 19:22

This post is genuinely incredible. I really like this post. It is remarkable among different posts that I see read in a long time. You shake for this alright post. I genuinely welcome it!


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter