2018-08-12 10:13:49 +08:00
|
|
|
#include "file_handler.h"
|
|
|
|
|
2019-11-28 04:11:40 +08:00
|
|
|
#include <assert.h>
|
2018-08-12 10:13:49 +08:00
|
|
|
#include <string.h>
|
2019-05-30 03:46:16 +08:00
|
|
|
|
2018-08-15 22:45:22 +08:00
|
|
|
#include "config.h"
|
2021-01-03 21:55:15 +08:00
|
|
|
#include "adb.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/lock.h"
|
|
|
|
#include "util/log.h"
|
2018-08-12 10:13:49 +08:00
|
|
|
|
2019-07-31 07:48:32 +08:00
|
|
|
#define DEFAULT_PUSH_TARGET "/sdcard/"
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
2019-05-30 03:46:16 +08:00
|
|
|
file_handler_request_destroy(struct file_handler_request *req) {
|
2019-05-24 23:25:31 +08:00
|
|
|
SDL_free(req->file);
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-07-31 07:48:32 +08:00
|
|
|
file_handler_init(struct file_handler *file_handler, const char *serial,
|
|
|
|
const char *push_target) {
|
2018-08-12 10:13:49 +08:00
|
|
|
|
2019-05-30 03:46:16 +08:00
|
|
|
cbuf_init(&file_handler->queue);
|
2018-08-12 10:13:49 +08:00
|
|
|
|
|
|
|
if (!(file_handler->mutex = SDL_CreateMutex())) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(file_handler->event_cond = SDL_CreateCond())) {
|
|
|
|
SDL_DestroyMutex(file_handler->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (serial) {
|
|
|
|
file_handler->serial = SDL_strdup(serial);
|
|
|
|
if (!file_handler->serial) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not strdup serial");
|
2019-05-30 03:33:34 +08:00
|
|
|
SDL_DestroyCond(file_handler->event_cond);
|
2018-08-12 10:13:49 +08:00
|
|
|
SDL_DestroyMutex(file_handler->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file_handler->serial = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lazy initialization
|
2019-03-03 06:52:22 +08:00
|
|
|
file_handler->initialized = false;
|
2018-08-12 10:13:49 +08:00
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
file_handler->stopped = false;
|
2018-08-12 10:13:49 +08:00
|
|
|
file_handler->current_process = PROCESS_NONE;
|
|
|
|
|
2019-07-31 07:48:32 +08:00
|
|
|
file_handler->push_target = push_target ? push_target : DEFAULT_PUSH_TARGET;
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
file_handler_destroy(struct file_handler *file_handler) {
|
2018-08-12 10:13:49 +08:00
|
|
|
SDL_DestroyCond(file_handler->event_cond);
|
|
|
|
SDL_DestroyMutex(file_handler->mutex);
|
2019-05-24 23:24:17 +08:00
|
|
|
SDL_free(file_handler->serial);
|
2019-05-30 03:46:16 +08:00
|
|
|
|
|
|
|
struct file_handler_request req;
|
|
|
|
while (cbuf_take(&file_handler->queue, &req)) {
|
|
|
|
file_handler_request_destroy(&req);
|
|
|
|
}
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static process_t
|
|
|
|
install_apk(const char *serial, const char *file) {
|
2018-08-12 10:40:00 +08:00
|
|
|
return adb_install(serial, file);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static process_t
|
2019-07-31 07:48:32 +08:00
|
|
|
push_file(const char *serial, const char *file, const char *push_target) {
|
|
|
|
return adb_push(serial, file, push_target);
|
2018-08-12 10:40:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
file_handler_request(struct file_handler *file_handler,
|
2019-05-30 03:46:16 +08:00
|
|
|
file_handler_action_t action, char *file) {
|
2018-08-12 10:13:49 +08:00
|
|
|
// start file_handler if it's used for the first time
|
|
|
|
if (!file_handler->initialized) {
|
|
|
|
if (!file_handler_start(file_handler)) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
2019-03-03 06:52:22 +08:00
|
|
|
file_handler->initialized = true;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
LOGI("Request to %s %s", action == ACTION_INSTALL_APK ? "install" : "push",
|
|
|
|
file);
|
2019-05-30 03:46:16 +08:00
|
|
|
struct file_handler_request req = {
|
|
|
|
.action = action,
|
|
|
|
.file = file,
|
|
|
|
};
|
2018-08-12 10:40:00 +08:00
|
|
|
|
2018-08-12 10:13:49 +08:00
|
|
|
mutex_lock(file_handler->mutex);
|
2019-05-30 03:46:16 +08:00
|
|
|
bool was_empty = cbuf_is_empty(&file_handler->queue);
|
|
|
|
bool res = cbuf_push(&file_handler->queue, req);
|
2018-08-12 10:13:49 +08:00
|
|
|
if (was_empty) {
|
|
|
|
cond_signal(file_handler->event_cond);
|
|
|
|
}
|
|
|
|
mutex_unlock(file_handler->mutex);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static int
|
|
|
|
run_file_handler(void *data) {
|
2018-08-12 10:13:49 +08:00
|
|
|
struct file_handler *file_handler = data;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
mutex_lock(file_handler->mutex);
|
|
|
|
file_handler->current_process = PROCESS_NONE;
|
2019-05-30 03:46:16 +08:00
|
|
|
while (!file_handler->stopped && cbuf_is_empty(&file_handler->queue)) {
|
2018-08-12 10:13:49 +08:00
|
|
|
cond_wait(file_handler->event_cond, file_handler->mutex);
|
|
|
|
}
|
|
|
|
if (file_handler->stopped) {
|
|
|
|
// stop immediately, do not process further events
|
|
|
|
mutex_unlock(file_handler->mutex);
|
|
|
|
break;
|
|
|
|
}
|
2019-05-30 03:46:16 +08:00
|
|
|
struct file_handler_request req;
|
|
|
|
bool non_empty = cbuf_take(&file_handler->queue, &req);
|
2019-11-28 04:11:40 +08:00
|
|
|
assert(non_empty);
|
|
|
|
(void) non_empty;
|
2018-08-15 22:46:53 +08:00
|
|
|
|
2018-08-12 10:40:00 +08:00
|
|
|
process_t process;
|
2019-05-30 03:46:16 +08:00
|
|
|
if (req.action == ACTION_INSTALL_APK) {
|
|
|
|
LOGI("Installing %s...", req.file);
|
|
|
|
process = install_apk(file_handler->serial, req.file);
|
2018-08-12 10:40:00 +08:00
|
|
|
} else {
|
2019-05-30 03:46:16 +08:00
|
|
|
LOGI("Pushing %s...", req.file);
|
2019-07-31 07:48:32 +08:00
|
|
|
process = push_file(file_handler->serial, req.file,
|
|
|
|
file_handler->push_target);
|
2018-08-12 10:40:00 +08:00
|
|
|
}
|
2018-08-12 10:13:49 +08:00
|
|
|
file_handler->current_process = process;
|
|
|
|
mutex_unlock(file_handler->mutex);
|
|
|
|
|
2019-05-30 03:46:16 +08:00
|
|
|
if (req.action == ACTION_INSTALL_APK) {
|
2018-08-12 10:40:00 +08:00
|
|
|
if (process_check_success(process, "adb install")) {
|
2019-05-30 03:46:16 +08:00
|
|
|
LOGI("%s successfully installed", req.file);
|
2018-08-12 10:40:00 +08:00
|
|
|
} else {
|
2019-05-30 03:46:16 +08:00
|
|
|
LOGE("Failed to install %s", req.file);
|
2018-08-12 10:40:00 +08:00
|
|
|
}
|
2018-08-12 10:13:49 +08:00
|
|
|
} else {
|
2018-08-12 10:40:00 +08:00
|
|
|
if (process_check_success(process, "adb push")) {
|
2019-07-31 07:48:32 +08:00
|
|
|
LOGI("%s successfully pushed to %s", req.file,
|
|
|
|
file_handler->push_target);
|
2018-08-12 10:40:00 +08:00
|
|
|
} else {
|
2019-07-31 07:48:32 +08:00
|
|
|
LOGE("Failed to push %s to %s", req.file,
|
|
|
|
file_handler->push_target);
|
2018-08-12 10:40:00 +08:00
|
|
|
}
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
2018-08-12 10:40:00 +08:00
|
|
|
|
2019-05-30 03:46:16 +08:00
|
|
|
file_handler_request_destroy(&req);
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
file_handler_start(struct file_handler *file_handler) {
|
2018-08-12 10:13:49 +08:00
|
|
|
LOGD("Starting file_handler thread");
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
file_handler->thread = SDL_CreateThread(run_file_handler, "file_handler",
|
|
|
|
file_handler);
|
2018-08-12 10:13:49 +08:00
|
|
|
if (!file_handler->thread) {
|
|
|
|
LOGC("Could not start file_handler thread");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
file_handler_stop(struct file_handler *file_handler) {
|
2018-08-12 10:13:49 +08:00
|
|
|
mutex_lock(file_handler->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
file_handler->stopped = true;
|
2018-08-12 10:13:49 +08:00
|
|
|
cond_signal(file_handler->event_cond);
|
|
|
|
if (file_handler->current_process != PROCESS_NONE) {
|
2021-01-03 21:55:15 +08:00
|
|
|
if (!process_terminate(file_handler->current_process)) {
|
2019-06-24 02:49:38 +08:00
|
|
|
LOGW("Could not terminate install process");
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
2021-01-03 21:55:15 +08:00
|
|
|
process_simple_wait(file_handler->current_process, NULL);
|
2018-08-12 10:13:49 +08:00
|
|
|
file_handler->current_process = PROCESS_NONE;
|
|
|
|
}
|
|
|
|
mutex_unlock(file_handler->mutex);
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
void
|
|
|
|
file_handler_join(struct file_handler *file_handler) {
|
2018-08-12 10:13:49 +08:00
|
|
|
SDL_WaitThread(file_handler->thread, NULL);
|
|
|
|
}
|