1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
/*
* Copyright (c) 2021 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* \file interfaces/android/android.c
* \brief Netlink interface
*/
#include <assert.h>
#include <pthread.h>
#include <sys/eventfd.h>
#include <unistd.h> // close
#include <hicn/facemgr.h>
#include <hicn/util/ip_address.h>
#include <hicn/util/log.h>
#include "../../common.h"
#include "../../interface.h"
#include "../../facelet_array.h"
#include "android.h"
/*
* aar_modules/FaceMgrLibrary/facemgrLibrary/src/main/java/com/cisco/hicn/facemgrlibrary/supportlibrary/FacemgrUtility.java
*/
#define FACEMGR_ANDROID_CLASS \
"com/cisco/hicn/facemgrlibrary/supportlibrary/FacemgrUtility"
/* Internal data storage */
typedef struct {
int fd;
android_cfg_t cfg;
JNIEnv *env;
jclass cls;
bool attached_to_vm;
facelet_array_t *facelets;
pthread_mutex_t mutex;
} android_data_t;
// might replace android utility
jclass find_class_global(JNIEnv *env, const char *name) {
jclass c = (*env)->FindClass(env, name);
jclass c_global = 0;
if (c) {
c_global = (jclass)(*env)->NewGlobalRef(env, c);
(*env)->DeleteLocalRef(env, c);
}
return c_global;
}
int android_on_network_event(interface_t *interface, const char *interface_name,
netdevice_type_t netdevice_type, bool up,
int family, const char *ip_address) {
android_data_t *data = (android_data_t *)interface->data;
netdevice_t *netdevice = netdevice_create_from_name(interface_name);
if (!netdevice) {
ERROR("[android_on_network_event] error creating netdevice '%s'",
interface_name);
goto ERR_ND;
}
ip_address_t local_addr = IP_ADDRESS_EMPTY;
if (ip_address) {
if (ip_address_pton(ip_address, &local_addr) < 0) {
ERROR("[android_on_network_event] error processing IP address");
goto ERR_IP_ADDRESS;
}
}
facelet_t *facelet = facelet_create();
if (!facelet) {
ERROR("[android_on_network_event] error creating facelet");
goto ERR_FACELET;
}
if (facelet_set_netdevice(facelet, *netdevice) < 0) {
ERROR("[android_on_network_event] error setting netdevice");
goto ERR;
}
if (netdevice_type != NETDEVICE_TYPE_UNDEFINED) {
if (facelet_set_netdevice_type(facelet, netdevice_type) < 0) {
ERROR("[android_on_network_event] error setting netdevice type");
goto ERR;
}
}
if (facelet_set_family(facelet, family) < 0) {
ERROR("[android_on_network_event] error setting family");
goto ERR;
}
if (ip_address) {
if (facelet_set_local_addr(facelet, local_addr) < 0) {
ERROR("[android_on_network_event] error setting local address");
goto ERR;
}
}
netdevice_free(netdevice);
facelet_set_event(facelet, up ? FACELET_EVENT_CREATE : FACELET_EVENT_DELETE);
// FACELET_EVENT_UPDATE, FACELET_EVENT_SET_DOWN
facelet_set_attr_clean(facelet);
pthread_mutex_lock(&data->mutex);
if (facelet_array_add(data->facelets, facelet)) {
ERROR("[android_on_network_event] Could not add facelet to buffer");
goto ERR_ADD;
}
pthread_mutex_unlock(&data->mutex);
eventfd_write(data->fd, 1);
return 0;
ERR_ADD:
pthread_mutex_unlock(&data->mutex);
ERR:
facelet_free(facelet);
ERR_FACELET:
ERR_IP_ADDRESS:
netdevice_free(netdevice);
ERR_ND:
return -1;
}
bool get_jni_env(JavaVM *jvm, JNIEnv **env) {
bool did_attach_thread = false;
INFO("initialize: get_jni_env");
*env = NULL;
// Check if the current thread is attached to the VM
int get_env_result = (*jvm)->GetEnv(jvm, (void **)env, JNI_VERSION_1_6);
if (get_env_result == JNI_EDETACHED) {
INFO("initialize: detached!");
if ((*jvm)->AttachCurrentThread(jvm, env, NULL) == JNI_OK) {
INFO("initialize: attached...");
did_attach_thread = true;
} else {
INFO("initialize: failed to attach");
// Failed to attach thread. Throw an exception if you want to.
}
} else if (get_env_result == JNI_EVERSION) {
// Unsupported JNI version. Throw an exception if you want to.
INFO("initialize: unsupported");
}
return did_attach_thread;
}
int android_initialize(interface_t *interface, void *cfg) {
android_data_t *data = malloc(sizeof(android_data_t));
if (!data) goto ERR_MALLOC;
interface->data = data;
if (!cfg) goto ERR_CFG;
data->cfg = *(android_cfg_t *)cfg;
JavaVM *jvm = data->cfg.jvm;
if (!jvm) goto ERR_JVM;
data->facelets = facelet_array_create();
if (!data->facelets) goto ERR_FACELETS;
if ((data->fd = eventfd(0, EFD_SEMAPHORE)) == -1) goto ERR_EVENTFD;
if (interface_register_fd(interface, data->fd, NULL) < 0) {
ERROR("[android_initialize] Error registering fd");
goto ERR_REGISTER_FD;
}
pthread_mutex_init(&data->mutex, NULL);
data->attached_to_vm = get_jni_env(jvm, &data->env);
if (!data->env) goto ERR_ENV;
data->cls = find_class_global(data->env, FACEMGR_ANDROID_CLASS);
if (data->cls == 0) goto ERR_CLS;
jmethodID mid_initialize =
(*data->env)
->GetStaticMethodID(data->env, data->cls, "initialize", "()I");
if (!mid_initialize) goto ERR_MID;
(*data->env)
->CallStaticIntMethod(data->env, data->cls, mid_initialize,
&android_on_network_event, interface);
return 0;
ERR_MID:
(*data->env)->DeleteGlobalRef(data->env, data->cls);
ERR_CLS:
if (data->attached_to_vm) {
(*jvm)->DetachCurrentThread(jvm);
data->attached_to_vm = false;
}
data->env = NULL;
ERR_ENV:
interface_unregister_fd(interface, data->fd);
ERR_REGISTER_FD:
close(data->fd);
ERR_EVENTFD:
facelet_array_free(data->facelets);
ERR_FACELETS:
ERR_JVM:
ERR_CFG:
free(data);
ERR_MALLOC:
return -1;
}
int android_finalize(interface_t *interface) {
android_data_t *data = (android_data_t *)interface->data;
jmethodID mid_terminate =
(*data->env)->GetStaticMethodID(data->env, data->cls, "terminate", "()I");
if (mid_terminate) {
(*data->env)
->CallStaticIntMethod(data->env, data->cls, mid_terminate,
&android_on_network_event, interface);
}
(*data->env)->DeleteGlobalRef(data->env, data->cls);
JavaVM *jvm = data->cfg.jvm;
if (data->attached_to_vm) {
(*jvm)->DetachCurrentThread(jvm);
data->attached_to_vm = false;
}
data->env = NULL;
pthread_mutex_destroy(&data->mutex);
// interface_unregister_fd(interface, data->fd); // XXX done in
// facemgr_delete_interface...
close(data->fd);
facelet_array_free(data->facelets);
free(data);
return 0;
}
int android_callback(interface_t *interface, int fd, void *unused) {
android_data_t *data = (android_data_t *)interface->data;
uint64_t ret;
if (read(data->fd, &ret, sizeof(ret)) < 0) return -1;
if (ret == 0) // EOF
return 0;
pthread_mutex_lock(&data->mutex);
for (unsigned i = 0; i < facelet_array_len(data->facelets); i++) {
facelet_t *facelet;
if (facelet_array_get_index(data->facelets, i, &facelet) < 0) {
ERROR("[android_callback] Error getting facelet in array");
continue;
}
interface_raise_event(interface, facelet);
}
for (unsigned i = 0; i < facelet_array_len(data->facelets); i++) {
if (facelet_array_remove_index(data->facelets, i, NULL) < 0) {
ERROR("[android_callback] Could not purge facelet from array");
}
}
pthread_mutex_unlock(&data->mutex);
return 0;
}
const interface_ops_t android_ops = {
.type = "android",
.initialize = android_initialize,
.callback = android_callback,
.finalize = android_finalize,
.on_event = NULL,
};
|