版权声明:本文为博主(Tower)自学笔记,欢迎转载! :-) https://blog.csdn.net/qq_33443989/article/details/77171267
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 1
4<. 删除代码
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 void
return;
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 = yes
2<. 再次修改启动文件 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_server
all : $(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;
}