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