2.Binder系统_C程序示例_编写程序 & 平台测试
来源:互联网 发布:linux signal函数 编辑:程序博客网 时间:2024/05/18 18:00
1>. Binder系统 之 C语言 代码编写
- 按照上一章的分析:参考btest.c,编写test_client.c / test_server.c
2>. 编译代码
1<. 包含所有的头文件
cp /work/android-5.0.2/system/core/include/private/android_filesystem_config.h ./include/private/cp /work/android-5.0.2/system/core/include/private/android_filesystem_capability.h ./include/private/cp /work/android-5.0.2/bionic/libc/kernel/uapi/linux/binder.h ./include/linux/
2<. 修改 service_manager.c :
1<. 减少头文件 fatal error: selinux/android.h: No such file or directory -- #include <selinux/avc.h>-- #include <selinux/android.h>2<. 添加头文件 error: expected '=', ',', ';', 'asm' or '__attribute__' before 'binder_size_t' ++ #include <liunx/types.h> error: expected '=', ',', ';', 'asm' or '__attribute__' before 'check_mac_perms' ++ #include <stdbool.h>3<. 修改打印语句宏 #if 14<. 删除代码 1<. union selinux_callback cb; cb.func_audit = audit_callback; selinux_set_callback(SELINUX_CB_AUDIT, cb); cb.func_log = selinux_log_callback; selinux_set_callback(SELINUX_CB_LOG, cb); 2<. static int audit_callback(void *data, security_class_t cls, char *buf, size_t len) { snprintf(buf, len, "service=%s", !data ? "NULL" : (char *)data); return 0; } 3<. undefined reference to `selinux_status_updated' if (sehandle && selinux_status_updated() > 0) { struct selabel_handle *tmp_sehandle = selinux_android_service_context_handle(); if (tmp_sehandle) { selabel_close(sehandle); sehandle = tmp_sehandle; } } 4<. undefined reference to `selinux_android_service_context_handle' undefined reference to `selabel_close' undefined reference to `is_selinux_enabled' undefined reference to `getcon' selinux_enabled = is_selinux_enabled(); sehandle = selinux_android_service_context_handle(); if (selinux_enabled > 0) { if (sehandle == NULL) { ALOGE("SELinux: Failed to acquire sehandle. Aborting.\n"); abort(); } if (getcon(&service_manager_context) != 0) { ALOGE("SELinux: Failed to acquire service_manager context. Aborting.\n"); abort(); } }3<. 修改Makefile %.o : %.c arm-linux-gcc -c -I include -DBINDER_IPC_32BIT=1 -o $@ $<
3<. 修改 binder.c
2<. 添加头文件 error: incompatible implicit declaration of built-in function 'memset' ++ #include <string.h> error: expected '=', ',', ';', 'asm' or '__attribute__' before 'binder_size_t' ++ #include <liunx/types.h> error: expected '=', ',', ';', 'asm' or '__attribute__' before 'check_mac_perms' ++ #include <stdbool.h> 4<. 删除代码 1<. fatal error: cutils/log.h: No such file or directory #define LOG_TAG "Binder" #include <cutils/log.h> 5<. 添加代码 undefined reference to `ALOGE' #if TRACE 1 #define ALOGI(x...) fprintf(stderr, "binder: " x) #define ALOGE(x...) fprintf(stderr, "binder: " x)
4<. 修改 test_client.c
2<. 添加头文件 error: expected '=', ',', ';', 'asm' or '__attribute__' before 'binder_size_t' ++ #include <liunx/types.h> error: expected '=', ',', ';', 'asm' or '__attribute__' before 'check_mac_perms' ++ #include <stdbool.h> expected specifier-qualifier-list before '__u32'6<. 修改代码warning: 'return' with a value, in function returning voidreturn;
5<. 修改 test_server.c
2<. 添加头文件 error: expected '=', ',', ';', 'asm' or '__attribute__' before 'binder_size_t' ++ #include <liunx/types.h> error: expected '=', ',', ';', 'asm' or '__attribute__' before 'check_mac_perms' ++ #include <stdbool.h>
3<. 测试代码
1<. 运行环境: 非 Android 系统, 例如 QT
2<. 上机测试:
软件层:1<. 重新编写内核让它支持NFS, 更新板上内核 make menuconfig <*> NFS client support | | [*] NFS client support for NFS version 3 | | [*] NFS client support for the NFSv3 ACL protocol extension | | [*] NFS client support for NFS version 4 | | [*] NFS client support for NFSv4.1 (EXPERIMENTAL) make zImage, 并使用新的zImage启动单板2<. mount nfs , 运行 server_manager, test_client, test_server pint <server:ip> mount -t nfs -o nolock <server:ip>:/<share-dir> /mnt eg : mount -t nfs -o nolock 192.168.1.111:/home/book/student /mnt硬件层: 1<. 修改启动文件 FriendlyARM.ini OS = Linux #OS = Android #USB-Mode = yes2<. 再次修改启动文件 FriendlyARM.ini OS = Linux #OS = Android USB-Mode = yes
3<. 测试程序:
./server_manager &./test_server &./test_client hello or ./test_client hello luo[杀死程序]:killall <pid_name>
示例代码:
1<. Makefile
APPS = service_manager test_client test_serverall : $(APPS)service_manager : service_manager.o binder.o arm-linux-gcc -o $@ $^test_client : test_client.o binder.o arm-linux-gcc -o $@ $^test_server : test_server.o binder.o arm-linux-gcc -o $@ $^ %.o : %.c arm-linux-gcc -c -I include -DBINDER_IPC_32BIT=1 -o $@ $<clean: rm $(APPS) -f; rm -f *.o
2<. test_client.c
/* Copyright 2008 The Android Open Source Project */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <linux/types.h>#include <stdbool.h>#include <private/android_filesystem_config.h>#include "binder.h"#include "test_server.h"uint32_t svcmgr_lookup(struct binder_state *bs, uint32_t target, const char *name){ uint32_t handle; unsigned iodata[512/4]; struct binder_io msg, reply; bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); // strict mode header bio_put_string16_x(&msg, SVC_MGR_NAME); bio_put_string16_x(&msg, name); if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE)) return 0; handle = bio_get_ref(&reply); if (handle) binder_acquire(bs, handle); binder_done(bs, &msg, &reply); return handle;}int svcmgr_publish(struct binder_state *bs, uint32_t target, const char *name, void *ptr){ int status; unsigned iodata[512/4]; struct binder_io msg, reply; bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); // strict mode header bio_put_string16_x(&msg, SVC_MGR_NAME); bio_put_string16_x(&msg, name); bio_put_obj(&msg, ptr); if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE)) return -1; status = bio_get_uint32(&reply); binder_done(bs, &msg, &reply); return status;}struct binder_state *g_tBs;uint32_t g_uiHandle;void sayhello(void){ /* 构造一个 binder_io */ unsigned iodata[512 / 4]; struct binder_io msg, reply; bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); /* strict mode header */ /* 调用binder_call */ if (binder_call(g_tBs, &msg, &reply, g_uiHandle, HELLO_SVR_CMD_SAYHELLO)) { return ; } /* 从reply取出返回值 */ /* 无:因为被调用的sayhello没有返回值 */ /* 调用binder_done表示完成 */ binder_done(g_tBs, &msg, &reply);}int sayhello_to(char *name){ int iRet = 0; /* 构造一个 binder_io */ unsigned iodata[512 / 4]; struct binder_io msg, reply; bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); /* strict mode header */ /* 放入参数 */ bio_put_string16(&msg, (uint16_t *)name); /* 调用binder_call */ if (binder_call(g_tBs, &msg, &reply, g_uiHandle, HELLO_SVR_CMD_SAYHELLO_TO)) { return 0; } /* 从reply取出返回值 */ iRet = bio_get_uint32(&reply); /* 调用binder_done表示完成 */ binder_done(g_tBs, &msg, &reply); return iRet;}/* test_client: * ./test_client helloc * ./test_client helloc <xxx>; */int main(int argc, char **argv){ int fd; int iRet; struct binder_state *bs; uint32_t svcmgr = BINDER_SERVICE_MANAGER; uint32_t handle; bs = binder_open(128*1024); if (!bs) { fprintf(stderr, "failed to open binder driver\n"); return -1; } /* 获得服务 */ handle = svcmgr_lookup(bs, svcmgr, "hello"); if (handle) { fprintf(stderr, "svcmgr_lookup get hello server error! \n"); return -1; } g_tBs = bs; g_uiHandle = handle; /* 发送数据给服务器 */ if (argc == 2) { sayhello(); } else if (argc == 3) { iRet = sayhello_to(argv[2]); fprintf(stderr, "get ret of sayhelloc_to = %d \n", iRet); } binder_release(bs, handle); return 0;}
3<. test_server.c
/* Copyright 2008 The Android Open Source Project */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <linux/types.h>#include <stdbool.h>#include <private/android_filesystem_config.h>#include "binder.h"#include "test_server.h"uint32_t svcmgr_lookup(struct binder_state *bs, uint32_t target, const char *name){ uint32_t handle; unsigned iodata[512/4]; struct binder_io msg, reply; bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); // strict mode header bio_put_string16_x(&msg, SVC_MGR_NAME); bio_put_string16_x(&msg, name); if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE)) return 0; handle = bio_get_ref(&reply); if (handle) binder_acquire(bs, handle); binder_done(bs, &msg, &reply); return handle;}int svcmgr_publish(struct binder_state *bs, uint32_t target, const char *name, void *ptr){ int status; unsigned iodata[512/4]; struct binder_io msg, reply; bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); // strict mode header bio_put_string16_x(&msg, SVC_MGR_NAME); bio_put_string16_x(&msg, name); bio_put_obj(&msg, ptr); if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE)) return -1; status = bio_get_uint32(&reply); binder_done(bs, &msg, &reply); return status;}void say_hello(void) { static int cnt = 0; fprintf(stderr, "say hello to %s : %d\n", cnt++);}int say_hello_to(char *name){ static int cnt = 0; fprintf(stderr, "say hello to %s : %d\n", name, cnt); return cnt;}int hello_service_handler(struct binder_state *bs, struct binder_transaction_data *txn, struct binder_io *msg, struct binder_io *reply){ /* 根据txn->code知道要调用那个函数 * 如果需要参数,可以从msg取出 * 如果要返回结果,可以把结果放入reply */ /* sayhello * sayhello_to */ uint16_t *s; size_t len; char name[512]; int i; uint32_t strict_policy; strict_policy = bio_get_uint32(msg); switch (txn->code) { case HELLO_SVR_CMD_SAYHELLO: { say_hello(); }break; case HELLO_SVR_CMD_SAYHELLO_TO: { /* 从 msg 取出字符串 */ s = bio_get_string16(msg, &len); if (s == NULL) { return -1; } for (i = 0; i < len; ++i) { name[i] = s[i]; } name[i] = '\0'; /* 处理 */ i = say_hello_to(name); /* 把结果放入 reply */ bio_put_uint32(reply, i); }break; defalut: { ALOGE("unknown code %d \n", txn->code); }break; }}int main(int argc, char **argv){ int iFd; int iRet; struct binder_state *bs; uint32_t svcmgr = BINDER_SERVICE_MANAGER; uint32_t handle; bs = binder_open(128*1024); if (!bs) { fprintf(stderr, "failed to open binder driver\n"); return -1; } /* 添加服务 */ iRet = svcmgr_publish(bs, svcmgr, "hello", (void *)123); if (!iRet) { fprintf(stderr, "svcmgr_publish : hello service error! \n"); return -1; } iRet = svcmgr_publish(bs, svcmgr, "goodbye", (void *)123); if (!iRet) { fprintf(stderr, "svcmgr_publish : goodbye service error! \n"); }#if 0 while (1) { /* 读数据 */ /* 解析数据,并且处理 */ /* 回复 */ }#endif /* 可以替换上面的 while (1)循环 */ binder_loop(bs, hello_service_handler); return 0;}
阅读全文
0 0
- 2.Binder系统_C程序示例_编写程序 & 平台测试
- 1.Binder系统_C程序示例_框架分析
- Android Binder系统完全分析系列文章之一:Binder系统的C程序使用示例
- 购物系统程序练习_C版
- colspan测试示例程序
- 数字签名示例程序 Java编写
- 数字签名示例程序 Java编写
- VS2008编写C++示例程序
- 热点_C#实现蜘蛛_爬虫程序的多线程控制
- 利用联合体union编写测试系统大小端程序
- 编写跨平台的程序
- Ex20b示例程序:DLL测试客户程序
- 使用CppUnit编写测试程序
- 编写可测试的程序
- 编写可测试的程序
- android 编写命令行测试程序
- 驱动测试程序的编写
- netlink测试程序的编写
- #1326 : 有序01字符串
- ZigBee CC2530 Z-Stack 17 网络参数设置及自组网
- 关于多线程的sleep和wait
- ccf认证字符串匹配
- React Native在Genymotion上运行遇到的问题
- 2.Binder系统_C程序示例_编写程序 & 平台测试
- html中插入map
- JAVA单例模式
- ES内存那点事
- 一:数据结构
- http get post 慢速攻击
- 带你了解----->BFC
- Hdu 5401 Persistent Link/cut Tree 树上分治/记忆化搜索
- RESTful