2018-08-12 10:13:49 +08:00
|
|
|
#include "file_handler.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
2018-08-15 22:45:22 +08:00
|
|
|
#include <SDL2/SDL_assert.h>
|
|
|
|
#include "config.h"
|
2018-08-12 10:13:49 +08:00
|
|
|
#include "command.h"
|
2018-08-12 10:40:00 +08:00
|
|
|
#include "device.h"
|
2018-08-15 23:01:54 +08:00
|
|
|
#include "lock_util.h"
|
2018-08-12 10:13:49 +08:00
|
|
|
#include "log.h"
|
|
|
|
|
2018-08-12 10:40:00 +08:00
|
|
|
struct request {
|
|
|
|
file_handler_action_t action;
|
2019-05-24 23:25:31 +08:00
|
|
|
char *file;
|
2018-08-12 10:40:00 +08:00
|
|
|
};
|
2018-08-12 10:13:49 +08:00
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static struct request *
|
2019-05-24 23:25:31 +08:00
|
|
|
request_new(file_handler_action_t action, char *file) {
|
2018-08-12 10:40:00 +08:00
|
|
|
struct request *req = SDL_malloc(sizeof(*req));
|
|
|
|
if (!req) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
req->action = action;
|
|
|
|
req->file = file;
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
|
|
|
request_free(struct request *req) {
|
2018-08-12 10:40:00 +08:00
|
|
|
if (!req) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-24 23:25:31 +08:00
|
|
|
SDL_free(req->file);
|
2019-05-24 23:15:32 +08:00
|
|
|
SDL_free(req);
|
2018-08-12 10:40:00 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
request_queue_is_empty(const struct request_queue *queue) {
|
2018-08-12 10:13:49 +08:00
|
|
|
return queue->head == queue->tail;
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
request_queue_is_full(const struct request_queue *queue) {
|
2018-08-12 10:40:00 +08:00
|
|
|
return (queue->head + 1) % REQUEST_QUEUE_SIZE == queue->tail;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
request_queue_init(struct request_queue *queue) {
|
2018-08-12 10:13:49 +08:00
|
|
|
queue->head = 0;
|
|
|
|
queue->tail = 0;
|
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
|
|
|
static void
|
|
|
|
request_queue_destroy(struct request_queue *queue) {
|
2018-08-12 10:13:49 +08:00
|
|
|
int i = queue->tail;
|
|
|
|
while (i != queue->head) {
|
2018-08-12 10:40:00 +08:00
|
|
|
request_free(queue->reqs[i]);
|
|
|
|
i = (i + 1) % REQUEST_QUEUE_SIZE;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
request_queue_push(struct request_queue *queue, struct request *req) {
|
2018-08-12 10:40:00 +08:00
|
|
|
if (request_queue_is_full(queue)) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
2018-08-12 10:40:00 +08:00
|
|
|
queue->reqs[queue->head] = req;
|
|
|
|
queue->head = (queue->head + 1) % REQUEST_QUEUE_SIZE;
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
static bool
|
2019-03-03 03:09:56 +08:00
|
|
|
request_queue_take(struct request_queue *queue, struct request **req) {
|
2018-08-12 10:40:00 +08:00
|
|
|
if (request_queue_is_empty(queue)) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
// transfer ownership
|
2018-08-12 10:40:00 +08:00
|
|
|
*req = queue->reqs[queue->tail];
|
|
|
|
queue->tail = (queue->tail + 1) % REQUEST_QUEUE_SIZE;
|
2019-03-03 06:52:22 +08:00
|
|
|
return true;
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
|
|
|
|
2019-03-03 06:52:22 +08:00
|
|
|
bool
|
2019-03-03 03:09:56 +08:00
|
|
|
file_handler_init(struct file_handler *file_handler, const char *serial) {
|
2018-08-12 10:13:49 +08:00
|
|
|
|
2018-08-12 10:40:00 +08:00
|
|
|
if (!request_queue_init(&file_handler->queue)) {
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
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) {
|
|
|
|
LOGW("Cannot strdup serial");
|
|
|
|
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-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);
|
2018-08-12 10:40:00 +08:00
|
|
|
request_queue_destroy(&file_handler->queue);
|
2019-05-24 23:24:17 +08:00
|
|
|
SDL_free(file_handler->serial);
|
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
|
|
|
|
push_file(const char *serial, const char *file) {
|
2018-08-12 10:40:00 +08:00
|
|
|
return adb_push(serial, file, DEVICE_SDCARD_PATH);
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
file_handler_action_t action,
|
2019-05-24 23:25:31 +08:00
|
|
|
char *file) {
|
2019-03-03 06:52:22 +08:00
|
|
|
bool res;
|
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);
|
2018-08-12 10:40:00 +08:00
|
|
|
struct request *req = request_new(action, file);
|
|
|
|
if (!req) {
|
|
|
|
LOGE("Could not create request");
|
2019-03-03 06:52:22 +08:00
|
|
|
return false;
|
2018-08-12 10:40:00 +08:00
|
|
|
}
|
|
|
|
|
2018-08-12 10:13:49 +08:00
|
|
|
mutex_lock(file_handler->mutex);
|
2019-03-03 06:52:22 +08:00
|
|
|
bool was_empty = request_queue_is_empty(&file_handler->queue);
|
2018-08-12 10:40:00 +08:00
|
|
|
res = request_queue_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-03-03 03:09:56 +08:00
|
|
|
while (!file_handler->stopped
|
|
|
|
&& request_queue_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;
|
|
|
|
}
|
2018-08-12 10:40:00 +08:00
|
|
|
struct request *req;
|
2019-03-03 06:52:22 +08:00
|
|
|
bool non_empty = request_queue_take(&file_handler->queue, &req);
|
2018-08-12 10:13:49 +08:00
|
|
|
SDL_assert(non_empty);
|
2018-08-15 22:46:53 +08:00
|
|
|
|
2018-08-12 10:40:00 +08:00
|
|
|
process_t process;
|
|
|
|
if (req->action == ACTION_INSTALL_APK) {
|
|
|
|
LOGI("Installing %s...", req->file);
|
|
|
|
process = install_apk(file_handler->serial, req->file);
|
|
|
|
} else {
|
|
|
|
LOGI("Pushing %s...", req->file);
|
|
|
|
process = push_file(file_handler->serial, req->file);
|
|
|
|
}
|
2018-08-12 10:13:49 +08:00
|
|
|
file_handler->current_process = process;
|
|
|
|
mutex_unlock(file_handler->mutex);
|
|
|
|
|
2018-08-12 10:40:00 +08:00
|
|
|
if (req->action == ACTION_INSTALL_APK) {
|
|
|
|
if (process_check_success(process, "adb install")) {
|
|
|
|
LOGI("%s successfully installed", req->file);
|
|
|
|
} else {
|
|
|
|
LOGE("Failed to install %s", req->file);
|
|
|
|
}
|
2018-08-12 10:13:49 +08:00
|
|
|
} else {
|
2018-08-12 10:40:00 +08:00
|
|
|
if (process_check_success(process, "adb push")) {
|
|
|
|
LOGI("%s successfully pushed to /sdcard/", req->file);
|
|
|
|
} else {
|
|
|
|
LOGE("Failed to push %s to /sdcard/", req->file);
|
|
|
|
}
|
2018-08-12 10:13:49 +08:00
|
|
|
}
|
2018-08-12 10:40:00 +08:00
|
|
|
|
|
|
|
request_free(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) {
|
|
|
|
if (!cmd_terminate(file_handler->current_process)) {
|
|
|
|
LOGW("Cannot terminate install process");
|
|
|
|
}
|
|
|
|
cmd_simple_wait(file_handler->current_process, NULL);
|
|
|
|
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);
|
|
|
|
}
|