blob: 750e87818b2083018e3d7422f80bd3678311ebd2 [file] [log] [blame]
Dmitry Shmidte4663042016-04-04 10:07:49 -07001/*
2 * binder interface for wpa_supplicant daemon
3 * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com>
5 *
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
8 */
9
10#include <binder/IPCThreadState.h>
Dmitry Shmidte4663042016-04-04 10:07:49 -070011#include <binder/IServiceManager.h>
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070012#include <binder/ProcessState.h>
Dmitry Shmidte4663042016-04-04 10:07:49 -070013
14#include "binder_manager.h"
15
16extern "C" {
Dmitry Shmidte4663042016-04-04 10:07:49 -070017#include "binder.h"
18#include "binder_i.h"
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070019#include "utils/common.h"
20#include "utils/eloop.h"
21#include "utils/includes.h"
Dmitry Shmidte4663042016-04-04 10:07:49 -070022}
23
24void wpas_binder_sock_handler(int sock, void *eloop_ctx, void *sock_ctx)
25{
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070026 struct wpa_global *global = (wpa_global *)eloop_ctx;
27 struct wpas_binder_priv *priv = (wpas_binder_priv *)sock_ctx;
Dmitry Shmidte4663042016-04-04 10:07:49 -070028
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070029 wpa_printf(
30 MSG_DEBUG, "Processing binder events on FD %d", priv->binder_fd);
Dmitry Shmidte4663042016-04-04 10:07:49 -070031 android::IPCThreadState::self()->handlePolledCommands();
32}
33
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070034struct wpas_binder_priv *wpas_binder_init(struct wpa_global *global)
Dmitry Shmidte4663042016-04-04 10:07:49 -070035{
36 struct wpas_binder_priv *priv;
37 wpa_supplicant_binder::BinderManager *binder_manager;
38
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070039 priv = (wpas_binder_priv *)os_zalloc(sizeof(*priv));
Dmitry Shmidte4663042016-04-04 10:07:49 -070040 if (!priv)
41 return NULL;
42 priv->global = global;
43
44 android::ProcessState::self()->setThreadPoolMaxThreadCount(0);
45 android::IPCThreadState::self()->disableBackgroundScheduling(true);
46 android::IPCThreadState::self()->setupPolling(&priv->binder_fd);
47 wpa_printf(MSG_INFO, "Process binder events on FD %d", priv->binder_fd);
48 if (priv->binder_fd < 0)
49 goto err;
50 /* Look for read events from the binder socket in the eloop. */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070051 if (eloop_register_read_sock(
52 priv->binder_fd, wpas_binder_sock_handler, global, priv) < 0)
Dmitry Shmidte4663042016-04-04 10:07:49 -070053 goto err;
54
55 binder_manager = wpa_supplicant_binder::BinderManager::getInstance();
56 if (!binder_manager)
57 goto err;
58 binder_manager->registerBinderService(global);
59 /* We may not need to store this binder manager reference in the
60 * global data strucure because we've made it a singleton class. */
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070061 priv->binder_manager = (void *)binder_manager;
Dmitry Shmidte4663042016-04-04 10:07:49 -070062
63 return priv;
64
65err:
66 wpas_binder_deinit(priv);
67 return NULL;
68}
69
Dmitry Shmidte4663042016-04-04 10:07:49 -070070void wpas_binder_deinit(struct wpas_binder_priv *priv)
71{
72 if (!priv)
73 return;
74
75 wpa_supplicant_binder::BinderManager::destroyInstance();
76 eloop_unregister_read_sock(priv->binder_fd);
77 android::IPCThreadState::shutdown();
78}
79
Dmitry Shmidte4663042016-04-04 10:07:49 -070080int wpas_binder_register_interface(struct wpa_supplicant *wpa_s)
81{
82 if (!wpa_s->global->binder)
83 return 1;
84
85 wpa_supplicant_binder::BinderManager *binder_manager =
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070086 wpa_supplicant_binder::BinderManager::getInstance();
Dmitry Shmidte4663042016-04-04 10:07:49 -070087 if (!binder_manager)
88 return 1;
89
90 return binder_manager->registerInterface(wpa_s);
91}
92
Dmitry Shmidte4663042016-04-04 10:07:49 -070093int wpas_binder_unregister_interface(struct wpa_supplicant *wpa_s)
94{
95 if (!wpa_s->global->binder)
96 return 1;
97
98 wpa_supplicant_binder::BinderManager *binder_manager =
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -070099 wpa_supplicant_binder::BinderManager::getInstance();
Dmitry Shmidte4663042016-04-04 10:07:49 -0700100 if (!binder_manager)
101 return 1;
102
103 return binder_manager->unregisterInterface(wpa_s);
104}