aidl_test_service.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright (C) 2015 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <map>
  17. #include <sstream>
  18. #include <string>
  19. #include <vector>
  20. #include <unistd.h>
  21. #include <android-base/unique_fd.h>
  22. #include <binder/IInterface.h>
  23. #include <binder/IPCThreadState.h>
  24. #include <binder/IServiceManager.h>
  25. #include <binder/ProcessState.h>
  26. #include <binder/Status.h>
  27. #include <binder/Value.h>
  28. #include <utils/Errors.h>
  29. #include <utils/Log.h>
  30. #include <utils/Looper.h>
  31. #include <utils/StrongPointer.h>
  32. #include "android/aidl/tests/BnTestService.h"
  33. #include "android/aidl/tests/ITestService.h"
  34. #include "android/aidl/tests/BnNamedCallback.h"
  35. #include "android/aidl/tests/INamedCallback.h"
  36. // Used implicitly.
  37. #undef LOG_TAG
  38. #define LOG_TAG "aidl_native_service"
  39. // libbase
  40. using android::base::unique_fd;
  41. // libutils:
  42. using android::Looper;
  43. using android::LooperCallback;
  44. using android::OK;
  45. using android::sp;
  46. using android::String16;
  47. // libbinder:
  48. using android::BnInterface;
  49. using android::defaultServiceManager;
  50. using android::IInterface;
  51. using android::IPCThreadState;
  52. using android::Parcel;
  53. using android::ProcessState;
  54. using android::binder::Status;
  55. // Generated code:
  56. using android::aidl::tests::BnNamedCallback;
  57. using android::aidl::tests::BnTestService;
  58. using android::aidl::tests::INamedCallback;
  59. using android::aidl::tests::SimpleParcelable;
  60. using android::binder::Map;
  61. using android::os::ParcelFileDescriptor;
  62. using android::os::PersistableBundle;
  63. // Standard library
  64. using std::map;
  65. using std::string;
  66. using std::unique_ptr;
  67. using std::vector;
  68. namespace {
  69. class BinderCallback : public LooperCallback {
  70. public:
  71. BinderCallback() {}
  72. ~BinderCallback() override {}
  73. int handleEvent(int /* fd */, int /* events */, void* /* data */) override {
  74. IPCThreadState::self()->handlePolledCommands();
  75. return 1; // Continue receiving callbacks.
  76. }
  77. };
  78. class NamedCallback : public BnNamedCallback {
  79. public:
  80. explicit NamedCallback(String16 name) : name_(name) {}
  81. Status GetName(String16* ret) {
  82. *ret = name_;
  83. return Status::ok();
  84. }
  85. private:
  86. String16 name_;
  87. };
  88. class NativeService : public BnTestService {
  89. public:
  90. NativeService() {}
  91. virtual ~NativeService() = default;
  92. void LogRepeatedStringToken(const String16& token) {
  93. ALOGI("Repeating '%s' of length=%zu", android::String8(token).string(),
  94. token.size());
  95. }
  96. template <typename T>
  97. void LogRepeatedToken(const T& token) {
  98. std::ostringstream token_str;
  99. token_str << token;
  100. ALOGI("Repeating token %s", token_str.str().c_str());
  101. }
  102. void LogRepeatedMapToken(const Map& token) {
  103. ALOGI("Repeating Map with %d elements", (int)token.size());
  104. }
  105. Status RepeatBoolean(bool token, bool* _aidl_return) override {
  106. LogRepeatedToken(token ? 1 : 0);
  107. *_aidl_return = token;
  108. return Status::ok();
  109. }
  110. Status RepeatByte(int8_t token, int8_t* _aidl_return) override {
  111. LogRepeatedToken(token);
  112. *_aidl_return = token;
  113. return Status::ok();
  114. }
  115. Status RepeatChar(char16_t token, char16_t* _aidl_return) override {
  116. LogRepeatedStringToken(String16(&token, 1));
  117. *_aidl_return = token;
  118. return Status::ok();
  119. }
  120. Status RepeatInt(int32_t token, int32_t* _aidl_return) override {
  121. LogRepeatedToken(token);
  122. *_aidl_return = token;
  123. return Status::ok();
  124. }
  125. Status RepeatLong(int64_t token, int64_t* _aidl_return) override {
  126. LogRepeatedToken(token);
  127. *_aidl_return = token;
  128. return Status::ok();
  129. }
  130. Status RepeatFloat(float token, float* _aidl_return) override {
  131. LogRepeatedToken(token);
  132. *_aidl_return = token;
  133. return Status::ok();
  134. }
  135. Status RepeatDouble(double token, double* _aidl_return) override {
  136. LogRepeatedToken(token);
  137. *_aidl_return = token;
  138. return Status::ok();
  139. }
  140. Status RepeatString(const String16& token, String16* _aidl_return) override {
  141. LogRepeatedStringToken(token);
  142. *_aidl_return = token;
  143. return Status::ok();
  144. }
  145. Status RepeatMap(const Map& token, Map* _aidl_return) override {
  146. LogRepeatedMapToken(token);
  147. *_aidl_return = token;
  148. return Status::ok();
  149. }
  150. Status RepeatSimpleParcelable(const SimpleParcelable& input,
  151. SimpleParcelable* repeat,
  152. SimpleParcelable* _aidl_return) override {
  153. ALOGI("Repeated a SimpleParcelable %s", input.toString().c_str());
  154. *repeat = input;
  155. *_aidl_return = input;
  156. return Status::ok();
  157. }
  158. Status RepeatPersistableBundle(const PersistableBundle& input,
  159. PersistableBundle* _aidl_return) override {
  160. ALOGI("Repeated a PersistableBundle");
  161. *_aidl_return = input;
  162. return Status::ok();
  163. }
  164. template <typename T>
  165. Status ReverseArray(const vector<T>& input, vector<T>* repeated,
  166. vector<T>* _aidl_return) {
  167. ALOGI("Reversing array of length %zu", input.size());
  168. *repeated = input;
  169. *_aidl_return = input;
  170. std::reverse(_aidl_return->begin(), _aidl_return->end());
  171. return Status::ok();
  172. }
  173. template<typename T>
  174. Status RepeatNullable(const unique_ptr<T>& input,
  175. unique_ptr<T>* _aidl_return) {
  176. ALOGI("Repeating nullable value");
  177. _aidl_return->reset();
  178. if (input) {
  179. _aidl_return->reset(new T(*input));
  180. }
  181. return Status::ok();
  182. }
  183. Status ReverseBoolean(const vector<bool>& input,
  184. vector<bool>* repeated,
  185. vector<bool>* _aidl_return) override {
  186. return ReverseArray(input, repeated, _aidl_return);
  187. }
  188. Status ReverseByte(const vector<uint8_t>& input,
  189. vector<uint8_t>* repeated,
  190. vector<uint8_t>* _aidl_return) override {
  191. return ReverseArray(input, repeated, _aidl_return);
  192. }
  193. Status ReverseChar(const vector<char16_t>& input,
  194. vector<char16_t>* repeated,
  195. vector<char16_t>* _aidl_return) override {
  196. return ReverseArray(input, repeated, _aidl_return);
  197. }
  198. Status ReverseInt(const vector<int32_t>& input,
  199. vector<int32_t>* repeated,
  200. vector<int32_t>* _aidl_return) override {
  201. return ReverseArray(input, repeated, _aidl_return);
  202. }
  203. Status ReverseLong(const vector<int64_t>& input,
  204. vector<int64_t>* repeated,
  205. vector<int64_t>* _aidl_return) override {
  206. return ReverseArray(input, repeated, _aidl_return);
  207. }
  208. Status ReverseFloat(const vector<float>& input,
  209. vector<float>* repeated,
  210. vector<float>* _aidl_return) override {
  211. return ReverseArray(input, repeated, _aidl_return);
  212. }
  213. Status ReverseDouble(const vector<double>& input,
  214. vector<double>* repeated,
  215. vector<double>* _aidl_return) override {
  216. return ReverseArray(input, repeated, _aidl_return);
  217. }
  218. Status ReverseString(const vector<String16>& input,
  219. vector<String16>* repeated,
  220. vector<String16>* _aidl_return) override {
  221. return ReverseArray(input, repeated, _aidl_return);
  222. }
  223. Status ReverseSimpleParcelables(
  224. const vector<SimpleParcelable>& input,
  225. vector<SimpleParcelable>* repeated,
  226. vector<SimpleParcelable>* _aidl_return) override {
  227. return ReverseArray(input, repeated, _aidl_return);
  228. }
  229. Status ReversePersistableBundles(
  230. const vector<PersistableBundle>& input,
  231. vector<PersistableBundle>* repeated,
  232. vector<PersistableBundle>* _aidl_return) override {
  233. return ReverseArray(input, repeated, _aidl_return);
  234. }
  235. Status GetOtherTestService(const String16& name,
  236. sp<INamedCallback>* returned_service) override {
  237. if (service_map_.find(name) == service_map_.end()) {
  238. sp<INamedCallback> new_item(new NamedCallback(name));
  239. service_map_[name] = new_item;
  240. }
  241. *returned_service = service_map_[name];
  242. return Status::ok();
  243. }
  244. Status VerifyName(const sp<INamedCallback>& service, const String16& name,
  245. bool* returned_value) override {
  246. String16 foundName;
  247. Status status = service->GetName(&foundName);
  248. if (status.isOk()) {
  249. *returned_value = foundName == name;
  250. }
  251. return status;
  252. }
  253. Status ReverseStringList(const vector<String16>& input,
  254. vector<String16>* repeated,
  255. vector<String16>* _aidl_return) override {
  256. return ReverseArray(input, repeated, _aidl_return);
  257. }
  258. Status ReverseNamedCallbackList(const vector<sp<IBinder>>& input,
  259. vector<sp<IBinder>>* repeated,
  260. vector<sp<IBinder>>* _aidl_return) override {
  261. return ReverseArray(input, repeated, _aidl_return);
  262. }
  263. Status RepeatFileDescriptor(const unique_fd& read,
  264. unique_fd* _aidl_return) override {
  265. ALOGE("Repeating file descriptor");
  266. *_aidl_return = unique_fd(dup(read.get()));
  267. return Status::ok();
  268. }
  269. Status ReverseFileDescriptorArray(const vector<unique_fd>& input,
  270. vector<unique_fd>* repeated,
  271. vector<unique_fd>* _aidl_return) override {
  272. ALOGI("Reversing descriptor array of length %zu", input.size());
  273. for (const auto& item : input) {
  274. repeated->push_back(unique_fd(dup(item.get())));
  275. _aidl_return->push_back(unique_fd(dup(item.get())));
  276. }
  277. std::reverse(_aidl_return->begin(), _aidl_return->end());
  278. return Status::ok();
  279. }
  280. Status RepeatParcelFileDescriptor(const ParcelFileDescriptor& read,
  281. ParcelFileDescriptor* _aidl_return) override {
  282. ALOGE("Repeating parcel file descriptor");
  283. _aidl_return->reset(unique_fd(dup(read.get())));
  284. return Status::ok();
  285. }
  286. Status ReverseParcelFileDescriptorArray(const vector<ParcelFileDescriptor>& input,
  287. vector<ParcelFileDescriptor>* repeated,
  288. vector<ParcelFileDescriptor>* _aidl_return) override {
  289. ALOGI("Reversing parcel descriptor array of length %zu", input.size());
  290. for (const auto& item : input) {
  291. repeated->push_back(ParcelFileDescriptor(unique_fd(dup(item.get()))));
  292. }
  293. for (auto i = input.rbegin(); i != input.rend(); i++) {
  294. _aidl_return->push_back(ParcelFileDescriptor(unique_fd(dup(i->get()))));
  295. }
  296. return Status::ok();
  297. }
  298. Status ThrowServiceException(int code) override {
  299. return Status::fromServiceSpecificError(code);
  300. }
  301. Status RepeatNullableIntArray(const unique_ptr<vector<int32_t>>& input,
  302. unique_ptr<vector<int32_t>>* _aidl_return) {
  303. return RepeatNullable(input, _aidl_return);
  304. }
  305. Status RepeatNullableStringList(
  306. const unique_ptr<vector<unique_ptr<String16>>>& input,
  307. unique_ptr<vector<unique_ptr<String16>>>* _aidl_return) {
  308. ALOGI("Repeating nullable string list");
  309. if (!input) {
  310. _aidl_return->reset();
  311. return Status::ok();
  312. }
  313. _aidl_return->reset(new vector<unique_ptr<String16>>);
  314. for (const auto& item : *input) {
  315. if (!item) {
  316. (*_aidl_return)->emplace_back(nullptr);
  317. } else {
  318. (*_aidl_return)->emplace_back(new String16(*item));
  319. }
  320. }
  321. return Status::ok();
  322. }
  323. Status RepeatNullableString(const unique_ptr<String16>& input,
  324. unique_ptr<String16>* _aidl_return) {
  325. return RepeatNullable(input, _aidl_return);
  326. }
  327. Status RepeatNullableParcelable(const unique_ptr<SimpleParcelable>& input,
  328. unique_ptr<SimpleParcelable>* _aidl_return) {
  329. return RepeatNullable(input, _aidl_return);
  330. }
  331. Status TakesAnIBinder(const sp<IBinder>& input) override {
  332. (void)input;
  333. return Status::ok();
  334. }
  335. Status TakesAnIBinderList(const vector<sp<IBinder>>& input) override {
  336. (void)input;
  337. return Status::ok();
  338. }
  339. Status TakesANullableIBinder(const sp<IBinder>& input) {
  340. (void)input;
  341. return Status::ok();
  342. }
  343. Status TakesANullableIBinderList(const unique_ptr<vector<sp<IBinder>>>& input) {
  344. (void)input;
  345. return Status::ok();
  346. }
  347. Status RepeatUtf8CppString(const string& token,
  348. string* _aidl_return) override {
  349. ALOGI("Repeating utf8 string '%s' of length=%zu", token.c_str(), token.size());
  350. *_aidl_return = token;
  351. return Status::ok();
  352. }
  353. Status RepeatNullableUtf8CppString(
  354. const unique_ptr<string>& token,
  355. unique_ptr<string>* _aidl_return) override {
  356. if (!token) {
  357. ALOGI("Received null @utf8InCpp string");
  358. return Status::ok();
  359. }
  360. ALOGI("Repeating utf8 string '%s' of length=%zu",
  361. token->c_str(), token->size());
  362. _aidl_return->reset(new string(*token));
  363. return Status::ok();
  364. }
  365. Status ReverseUtf8CppString(const vector<string>& input,
  366. vector<string>* repeated,
  367. vector<string>* _aidl_return) {
  368. return ReverseArray(input, repeated, _aidl_return);
  369. }
  370. Status ReverseNullableUtf8CppString(
  371. const unique_ptr<vector<unique_ptr<string>>>& input,
  372. unique_ptr<vector<unique_ptr<string>>>* repeated,
  373. unique_ptr<vector<unique_ptr<string>>>* _aidl_return) {
  374. return ReverseUtf8CppStringList(input, repeated, _aidl_return);
  375. }
  376. Status ReverseUtf8CppStringList(
  377. const unique_ptr<vector<unique_ptr<::string>>>& input,
  378. unique_ptr<vector<unique_ptr<string>>>* repeated,
  379. unique_ptr<vector<unique_ptr<string>>>* _aidl_return) {
  380. if (!input) {
  381. ALOGI("Received null list of utf8 strings");
  382. return Status::ok();
  383. }
  384. _aidl_return->reset(new vector<unique_ptr<string>>);
  385. repeated->reset(new vector<unique_ptr<string>>);
  386. for (const auto& item : *input) {
  387. (*repeated)->emplace_back(nullptr);
  388. (*_aidl_return)->emplace_back(nullptr);
  389. if (item) {
  390. (*repeated)->back().reset(new string(*item));
  391. (*_aidl_return)->back().reset(new string(*item));
  392. }
  393. }
  394. std::reverse((*_aidl_return)->begin(), (*_aidl_return)->end());
  395. return Status::ok();
  396. }
  397. Status GetCallback(bool return_null, sp<INamedCallback>* ret) {
  398. if (!return_null) {
  399. return GetOtherTestService(String16("ABT: always be testing"), ret);
  400. }
  401. return Status::ok();
  402. }
  403. virtual ::android::binder::Status FillOutStructuredParcelable(
  404. ::android::aidl::tests::StructuredParcelable* parcelable) {
  405. parcelable->shouldBeJerry = "Jerry";
  406. parcelable->shouldContainThreeFs = {parcelable->f, parcelable->f, parcelable->f};
  407. return Status::ok();
  408. }
  409. Status UnimplementedMethod(int32_t /* arg */, int32_t* /* _aidl_return */) override {
  410. LOG_ALWAYS_FATAL("UnimplementedMethod shouldn't be called");
  411. }
  412. android::status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
  413. uint32_t flags) override {
  414. if (code == ::android::IBinder::FIRST_CALL_TRANSACTION + 45 /* UnimplementedMethod */) {
  415. // pretend that UnimplementedMethod isn't implemented by this service.
  416. return android::UNKNOWN_TRANSACTION;
  417. } else {
  418. return BnTestService::onTransact(code, data, reply, flags);
  419. }
  420. }
  421. private:
  422. map<String16, sp<INamedCallback>> service_map_;
  423. };
  424. int Run() {
  425. android::sp<NativeService> service = new NativeService;
  426. sp<Looper> looper(Looper::prepare(0 /* opts */));
  427. int binder_fd = -1;
  428. ProcessState::self()->setThreadPoolMaxThreadCount(0);
  429. IPCThreadState::self()->disableBackgroundScheduling(true);
  430. IPCThreadState::self()->setupPolling(&binder_fd);
  431. ALOGI("Got binder FD %d", binder_fd);
  432. if (binder_fd < 0) return -1;
  433. sp<BinderCallback> cb(new BinderCallback);
  434. if (looper->addFd(binder_fd, Looper::POLL_CALLBACK, Looper::EVENT_INPUT, cb,
  435. nullptr) != 1) {
  436. ALOGE("Failed to add binder FD to Looper");
  437. return -1;
  438. }
  439. defaultServiceManager()->addService(service->getInterfaceDescriptor(),
  440. service);
  441. ALOGI("Entering loop");
  442. while (true) {
  443. const int result = looper->pollAll(-1 /* timeoutMillis */);
  444. ALOGI("Looper returned %d", result);
  445. }
  446. return 0;
  447. }
  448. } // namespace
  449. int main(int /* argc */, char* /* argv */ []) {
  450. return Run();
  451. }