blob: e02b45d1038d3eb7153996f3cd654887c90aaad8 [file] [log] [blame]
Mike Lockwood94afecf2012-10-24 10:45:23 -07001/* 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
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000010uint32_t svcmgr_lookup(struct binder_state *bs, uint32_t target, const char *name)
Mike Lockwood94afecf2012-10-24 10:45:23 -070011{
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000012 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070013 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
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000024 handle = bio_get_ref(&reply);
Mike Lockwood94afecf2012-10-24 10:45:23 -070025
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000026 if (handle)
27 binder_acquire(bs, handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -070028
29 binder_done(bs, &msg, &reply);
30
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000031 return handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070032}
33
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000034int svcmgr_publish(struct binder_state *bs, uint32_t target, const char *name, void *ptr)
Mike Lockwood94afecf2012-10-24 10:45:23 -070035{
Serban Constantinescu3a345f02013-12-19 09:11:41 +000036 int status;
Mike Lockwood94afecf2012-10-24 10:45:23 -070037 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;
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000062 uint32_t svcmgr = BINDER_SERVICE_MANAGER;
63 uint32_t handle;
Mike Lockwood94afecf2012-10-24 10:45:23 -070064
65 bs = binder_open(128*1024);
Serban Constantinescua44542c2014-01-30 15:16:45 +000066 if (!bs) {
67 fprintf(stderr, "failed to open binder driver\n");
68 return -1;
69 }
Mike Lockwood94afecf2012-10-24 10:45:23 -070070
71 argc--;
72 argv++;
73 while (argc > 0) {
74 if (!strcmp(argv[0],"alt")) {
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000075 handle = svcmgr_lookup(bs, svcmgr, "alt_svc_mgr");
76 if (!handle) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070077 fprintf(stderr,"cannot find alt_svc_mgr\n");
78 return -1;
79 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000080 svcmgr = handle;
81 fprintf(stderr,"svcmgr is via %x\n", handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -070082 } else if (!strcmp(argv[0],"lookup")) {
Mike Lockwood94afecf2012-10-24 10:45:23 -070083 if (argc < 2) {
84 fprintf(stderr,"argument required\n");
85 return -1;
86 }
Serban Constantinescu5fb1b882014-01-30 14:07:34 +000087 handle = svcmgr_lookup(bs, svcmgr, argv[1]);
88 fprintf(stderr,"lookup(%s) = %x\n", argv[1], handle);
Mike Lockwood94afecf2012-10-24 10:45:23 -070089 argc--;
90 argv++;
91 } else if (!strcmp(argv[0],"publish")) {
92 if (argc < 2) {
93 fprintf(stderr,"argument required\n");
94 return -1;
95 }
96 svcmgr_publish(bs, svcmgr, argv[1], &token);
97 argc--;
98 argv++;
99 } else {
100 fprintf(stderr,"unknown command %s\n", argv[0]);
101 return -1;
102 }
103 argc--;
104 argv++;
105 }
106 return 0;
107}