bctest.c 2.7 KB

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