void saygoodbye(void) { unsigned iodata[512/4]; struct binder_io msg, reply; /* 构造binder_io */ bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); // strict mode header /* 放入参数 */ /* 调用binder_call */ if (binder_call(g_bs, &msg, &reply, g_goodbye_handle, GOODBYE_SVR_CMD_SAYGOODBYE)) return ; /* 从reply中解析出返回值 */ binder_done(g_bs, &msg, &reply); }
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; }
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; }
static uint32_t get_handle_from_svcmgr(const uint16_t *str, binder_state *bs){ struct binder_io msg; struct binder_io reply; unsigned rdata[256]; int res; uint32_t ref; bio_init(&msg, rdata, sizeof(rdata), 0); bio_put_string16(&msg, svcmgr_id); bio_put_string16(&msg, str); res = binder_call(bs, &msg, &reply, 0, BINDER_SERVICE_MANAGER, SVC_MGR_GET_SERVICE); if (res == -1) return res; ref = bio_get_ref(&reply); if (ref == 0) return -1; return ref; }
void get_all_handles(struct binder_state *bs) { struct binder_io msg; struct binder_io reply; unsigned rdata[256]; uint16_t *str; int res; size_t sz; unsigned service_count = 0; uint32_t handle; do { bio_init(&msg, rdata, sizeof(rdata), 0); bio_put_string16(&msg, svcmgr_id); /* put the cmd */ bio_put_uint32(&msg, service_count); res = binder_call(bs, &msg, &reply, BINDER_SERVICE_MANAGER, SVC_MGR_LIST_SERVICES); if (res) { str = bio_get_string16(&reply, &sz); si = malloc((sz + 1) * sizeof(uint16_t)); if (!si) { fprintf(stderr, "malloc failed\n"); res = -1; break; } si[sz] = '/0'; services[service_count] = si; handle = get_handle_from_svcmgr(str, sz); if (handle == -1) { free(si); break; } handles[service_count] = handle; service_count++; } } while(res != -1 && service_count < MAX_SERVICES); num_handles_set = service_count; }
int sayhello_to(char * name) { unsigned iodata[512/4]; struct binder_io msg, reply; int ret; /* 构造binder_io */ bio_init(&msg, iodata, sizeof(iodata), 4); bio_put_uint32(&msg, 0); // strict mode header /* 放入参数 */ bio_put_string16_x(&msg, name); /* 调用binder_call */ if (binder_call(g_bs, &msg, &reply, g_hello_handle, HELLO_SVR_CMD_SAYHELLO_TO)) return 0; /* 从reply中解析出返回值 */ ret = bio_get_uint32(&reply); binder_done(g_bs, &msg, &reply); return ret; }
// binder_call의 래퍼를 제공합니다. 안드로이드에서는 transact 함수를 사용합니다. int transact(int handle, int code, int* msg, int* reply) { return binder_call(mProcess->get_driver(), msg, reply, handle, code); }