bctest.c revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/* Copyright 2008 The Android Open Source Project
2 */
3
4#include <stdio.h>
5#include <stdlib.h>
6#include <errno.h>
7
8#include "binder.h"
9
10void *svcmgr_lookup(struct binder_state *bs, void *target, const char *name)
11{
12    void *ptr;
13    unsigned iodata[512/4];
14    struct binder_io msg, reply;
15
16    bio_init(&msg, iodata, sizeof(iodata), 4);
17    bio_put_string16_x(&msg, SVC_MGR_NAME);
18    bio_put_string16_x(&msg, name);
19
20    if (binder_call(bs, &msg, &reply, target, SVC_MGR_CHECK_SERVICE))
21        return 0;
22
23    ptr = bio_get_ref(&reply);
24
25    if (ptr)
26        binder_acquire(bs, ptr);
27
28    binder_done(bs, &msg, &reply);
29
30    return ptr;
31}
32
33int svcmgr_publish(struct binder_state *bs, void *target, const char *name, void *ptr)
34{
35    unsigned status;
36    unsigned iodata[512/4];
37    struct binder_io msg, reply;
38
39    bio_init(&msg, iodata, sizeof(iodata), 4);
40
41    bio_put_string16_x(&msg, SVC_MGR_NAME);
42    bio_put_string16_x(&msg, name);
43    bio_put_obj(&msg, ptr);
44
45    if (binder_call(bs, &msg, &reply, target, SVC_MGR_ADD_SERVICE))
46        return -1;
47
48    status = bio_get_uint32(&reply);
49
50    binder_done(bs, &msg, &reply);
51
52    return status;
53}
54
55unsigned token;
56
57int main(int argc, char **argv)
58{
59    int fd;
60    struct binder_state *bs;
61    void *svcmgr = BINDER_SERVICE_MANAGER;
62
63    bs = binder_open(128*1024);
64
65    argc--;
66    argv++;
67    while (argc > 0) {
68        if (!strcmp(argv[0],"alt")) {
69            void *ptr = svcmgr_lookup(bs, svcmgr, "alt_svc_mgr");
70            if (!ptr) {
71                fprintf(stderr,"cannot find alt_svc_mgr\n");
72                return -1;
73            }
74            svcmgr = ptr;
75            fprintf(stderr,"svcmgr is via %p\n", ptr);
76        } else if (!strcmp(argv[0],"lookup")) {
77            void *ptr;
78            if (argc < 2) {
79                fprintf(stderr,"argument required\n");
80                return -1;
81            }
82            ptr = svcmgr_lookup(bs, svcmgr, argv[1]);
83            fprintf(stderr,"lookup(%s) = %p\n", argv[1], ptr);
84            argc--;
85            argv++;
86        } else if (!strcmp(argv[0],"publish")) {
87            if (argc < 2) {
88                fprintf(stderr,"argument required\n");
89                return -1;
90            }
91            svcmgr_publish(bs, svcmgr, argv[1], &token);
92            argc--;
93            argv++;
94        } else {
95            fprintf(stderr,"unknown command %s\n", argv[0]);
96            return -1;
97        }
98        argc--;
99        argv++;
100    }
101    return 0;
102}
103