2018-08-12 02:13:49 +00:00
|
|
|
#include "file_handler.h"
|
|
|
|
|
2019-11-27 20:11:40 +00:00
|
|
|
#include <assert.h>
|
2018-08-12 02:13:49 +00:00
|
|
|
#include <string.h>
|
2019-05-29 19:46:16 +00:00
|
|
|
|
2021-01-03 13:55:15 +00:00
|
|
|
#include "adb.h"
|
2019-11-24 10:53:00 +00:00
|
|
|
#include "util/log.h"
|
2021-11-18 20:33:25 +00:00
|
|
|
#include "util/process_intr.h"
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2021-06-13 20:47:16 +00:00
|
|
|
#define DEFAULT_PUSH_TARGET "/sdcard/Download/"
|
2019-07-30 23:48:32 +00:00
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
static void
|
2019-05-29 19:46:16 +00:00
|
|
|
file_handler_request_destroy(struct file_handler_request *req) {
|
2021-01-24 14:14:53 +00:00
|
|
|
free(req->file);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
bool
|
2019-07-30 23:48:32 +00:00
|
|
|
file_handler_init(struct file_handler *file_handler, const char *serial,
|
|
|
|
const char *push_target) {
|
2021-11-18 17:48:11 +00:00
|
|
|
assert(serial);
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2019-05-29 19:46:16 +00:00
|
|
|
cbuf_init(&file_handler->queue);
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
bool ok = sc_mutex_init(&file_handler->mutex);
|
|
|
|
if (!ok) {
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
ok = sc_cond_init(&file_handler->event_cond);
|
|
|
|
if (!ok) {
|
|
|
|
sc_mutex_destroy(&file_handler->mutex);
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 20:33:25 +00:00
|
|
|
ok = sc_intr_init(&file_handler->intr);
|
|
|
|
if (!ok) {
|
|
|
|
LOGE("Could not create intr");
|
|
|
|
sc_cond_destroy(&file_handler->event_cond);
|
|
|
|
sc_mutex_destroy(&file_handler->mutex);
|
2021-11-24 20:46:02 +00:00
|
|
|
return false;
|
2021-11-18 20:33:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 17:48:11 +00:00
|
|
|
file_handler->serial = strdup(serial);
|
|
|
|
if (!file_handler->serial) {
|
|
|
|
LOGE("Could not strdup serial");
|
2021-11-18 20:33:25 +00:00
|
|
|
sc_intr_destroy(&file_handler->intr);
|
2021-11-18 17:48:11 +00:00
|
|
|
sc_cond_destroy(&file_handler->event_cond);
|
|
|
|
sc_mutex_destroy(&file_handler->mutex);
|
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// lazy initialization
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->initialized = false;
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->stopped = false;
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2019-07-30 23:48:32 +00:00
|
|
|
file_handler->push_target = push_target ? push_target : DEFAULT_PUSH_TARGET;
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
return true;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
void
|
|
|
|
file_handler_destroy(struct file_handler *file_handler) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_destroy(&file_handler->event_cond);
|
|
|
|
sc_mutex_destroy(&file_handler->mutex);
|
2021-11-18 20:33:25 +00:00
|
|
|
sc_intr_destroy(&file_handler->intr);
|
2021-01-24 14:14:53 +00:00
|
|
|
free(file_handler->serial);
|
2019-05-29 19:46:16 +00:00
|
|
|
|
|
|
|
struct file_handler_request req;
|
|
|
|
while (cbuf_take(&file_handler->queue, &req)) {
|
|
|
|
file_handler_request_destroy(&req);
|
|
|
|
}
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
bool
|
2019-03-02 19:09:56 +00:00
|
|
|
file_handler_request(struct file_handler *file_handler,
|
2019-05-29 19:46:16 +00:00
|
|
|
file_handler_action_t action, char *file) {
|
2018-08-12 02:13:49 +00:00
|
|
|
// start file_handler if it's used for the first time
|
|
|
|
if (!file_handler->initialized) {
|
|
|
|
if (!file_handler_start(file_handler)) {
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->initialized = true;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
LOGI("Request to %s %s", action == ACTION_INSTALL_APK ? "install" : "push",
|
|
|
|
file);
|
2019-05-29 19:46:16 +00:00
|
|
|
struct file_handler_request req = {
|
|
|
|
.action = action,
|
|
|
|
.file = file,
|
|
|
|
};
|
2018-08-12 02:40:00 +00:00
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&file_handler->mutex);
|
2019-05-29 19:46:16 +00:00
|
|
|
bool was_empty = cbuf_is_empty(&file_handler->queue);
|
|
|
|
bool res = cbuf_push(&file_handler->queue, req);
|
2018-08-12 02:13:49 +00:00
|
|
|
if (was_empty) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_signal(&file_handler->event_cond);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
static int
|
|
|
|
run_file_handler(void *data) {
|
2018-08-12 02:13:49 +00:00
|
|
|
struct file_handler *file_handler = data;
|
2021-11-18 20:33:25 +00:00
|
|
|
struct sc_intr *intr = &file_handler->intr;
|
2018-08-12 02:13:49 +00:00
|
|
|
|
2021-11-18 18:46:40 +00:00
|
|
|
const char *serial = file_handler->serial;
|
|
|
|
assert(serial);
|
|
|
|
|
|
|
|
const char *push_target = file_handler->push_target;
|
|
|
|
assert(push_target);
|
|
|
|
|
2018-08-12 02:13:49 +00:00
|
|
|
for (;;) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&file_handler->mutex);
|
2019-05-29 19:46:16 +00:00
|
|
|
while (!file_handler->stopped && cbuf_is_empty(&file_handler->queue)) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_wait(&file_handler->event_cond, &file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
if (file_handler->stopped) {
|
|
|
|
// stop immediately, do not process further events
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-05-29 19:46:16 +00:00
|
|
|
struct file_handler_request req;
|
|
|
|
bool non_empty = cbuf_take(&file_handler->queue, &req);
|
2019-11-27 20:11:40 +00:00
|
|
|
assert(non_empty);
|
|
|
|
(void) non_empty;
|
2021-11-18 20:33:25 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-15 14:46:53 +00:00
|
|
|
|
2019-05-29 19:46:16 +00:00
|
|
|
if (req.action == ACTION_INSTALL_APK) {
|
|
|
|
LOGI("Installing %s...", req.file);
|
2021-11-18 21:11:19 +00:00
|
|
|
bool ok = adb_install(intr, serial, req.file);
|
|
|
|
if (ok) {
|
2019-05-29 19:46:16 +00:00
|
|
|
LOGI("%s successfully installed", req.file);
|
2018-08-12 02:40:00 +00:00
|
|
|
} else {
|
2019-05-29 19:46:16 +00:00
|
|
|
LOGE("Failed to install %s", req.file);
|
2018-08-12 02:40:00 +00:00
|
|
|
}
|
2018-08-12 02:13:49 +00:00
|
|
|
} else {
|
2021-11-18 21:11:19 +00:00
|
|
|
LOGI("Pushing %s...", req.file);
|
|
|
|
bool ok = adb_push(intr, serial, req.file, push_target);
|
|
|
|
if (ok) {
|
2021-11-18 18:46:40 +00:00
|
|
|
LOGI("%s successfully pushed to %s", req.file, push_target);
|
2018-08-12 02:40:00 +00:00
|
|
|
} else {
|
2021-11-18 18:46:40 +00:00
|
|
|
LOGE("Failed to push %s to %s", req.file, push_target);
|
2018-08-12 02:40:00 +00:00
|
|
|
}
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
2018-08-12 02:40:00 +00:00
|
|
|
|
2019-05-29 19:46:16 +00:00
|
|
|
file_handler_request_destroy(&req);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
bool
|
2019-03-02 19:09:56 +00:00
|
|
|
file_handler_start(struct file_handler *file_handler) {
|
2018-08-12 02:13:49 +00:00
|
|
|
LOGD("Starting file_handler thread");
|
|
|
|
|
2021-01-31 17:24:35 +00:00
|
|
|
bool ok = sc_thread_create(&file_handler->thread, run_file_handler,
|
|
|
|
"file_handler", file_handler);
|
|
|
|
if (!ok) {
|
2018-08-12 02:13:49 +00:00
|
|
|
LOGC("Could not start file_handler thread");
|
2019-03-02 22:52:22 +00:00
|
|
|
return false;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 22:52:22 +00:00
|
|
|
return true;
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
void
|
|
|
|
file_handler_stop(struct file_handler *file_handler) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_lock(&file_handler->mutex);
|
2019-03-02 22:52:22 +00:00
|
|
|
file_handler->stopped = true;
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_cond_signal(&file_handler->event_cond);
|
2021-11-18 20:33:25 +00:00
|
|
|
sc_intr_interrupt(&file_handler->intr);
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_mutex_unlock(&file_handler->mutex);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:09:56 +00:00
|
|
|
void
|
|
|
|
file_handler_join(struct file_handler *file_handler) {
|
2021-01-31 17:24:35 +00:00
|
|
|
sc_thread_join(&file_handler->thread, NULL);
|
2018-08-12 02:13:49 +00:00
|
|
|
}
|