#define DATA_WORKER_QDATA_MAX_SIZE (3840 * 2160 * 4) /* UHD BGRA8888 */
#define UNLIMITED_INSTANCE -1
+static void _ms_ipc_data_ch_init(muse_module_h m);
+static void _ms_ipc_data_ch_deinit(muse_module_h m);
static void _ms_ipc_module_data_ch_cleanup(muse_module_h m);
static void _ms_ipc_module_msg_ch_cleanup(muse_module_h m);
static void _ms_ipc_module_cleanup(muse_module_h m);
static gboolean _ms_ipc_module_instance_creation_is_allowed(int module_idx);
+static gboolean _ms_ipc_get_module_idx(muse_module_h m, void *jobj);
+static gboolean _ms_ipc_dispatch_create(muse_module_h m, void *jobj);
+static gboolean _ms_ipc_dispatch_destroy(muse_module_h m);
+static void _ms_ipc_dispatch_no_instance(muse_module_h m, void *jobj);
static gpointer _ms_ipc_dispatch_worker(gpointer data);
static gboolean _ms_ipc_data_processing(int fd, muse_recv_data_head_t *header, muse_channel_info_t *ch);
static gpointer _ms_ipc_data_worker(gpointer data);
-static void _ms_ipc_module_data_ch_cleanup(muse_module_h m)
+static void _ms_ipc_data_ch_init(muse_module_h m)
{
muse_return_if_fail(m);
- muse_return_if_fail(m->ch[MUSE_CHANNEL_DATA].thread);
- g_thread_join(m->ch[MUSE_CHANNEL_DATA].thread);
- m->ch[MUSE_CHANNEL_DATA].thread = NULL;
+ m->ch[MUSE_CHANNEL_DATA].data_queue = g_queue_new();
+ g_mutex_init(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
+ g_cond_init(&m->ch[MUSE_CHANNEL_DATA].data_cond);
+}
+
+static void _ms_ipc_data_ch_deinit(muse_module_h m)
+{
+ muse_return_if_fail(m);
g_mutex_lock(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
g_queue_free(m->ch[MUSE_CHANNEL_DATA].data_queue);
g_mutex_clear(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
g_cond_clear(&m->ch[MUSE_CHANNEL_DATA].data_cond);
+}
+
+static void _ms_ipc_module_data_ch_cleanup(muse_module_h m)
+{
+ muse_return_if_fail(m);
+ muse_return_if_fail(m->ch[MUSE_CHANNEL_DATA].thread);
+
+ g_thread_join(m->ch[MUSE_CHANNEL_DATA].thread);
+ m->ch[MUSE_CHANNEL_DATA].thread = NULL;
+
+ _ms_ipc_data_ch_deinit(m);
LOGD("[close] [%d] MUSE_CHANNEL_DATA", m->ch[MUSE_CHANNEL_DATA].sock_fd);
muse_core_connection_close(m->ch[MUSE_CHANNEL_DATA].sock_fd);
}
}
+static gboolean _ms_ipc_get_module_idx(muse_module_h m, void *jobj)
+{
+ muse_return_val_if_fail(m, FALSE);
+ muse_return_val_if_fail(jobj, FALSE);
+
+ if (!muse_core_msg_object_get_value(MSG_KEY_MODULE_INDEX, jobj, MUSE_TYPE_INT, &m->idx)) {
+ LOGE("Failed to get the value of module index");
+ ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static gboolean _ms_ipc_dispatch_create(muse_module_h m, void *jobj)
+{
+ int pid, dispatch_ret = MM_ERROR_NONE;
+
+ muse_return_val_if_fail(m, FALSE);
+ muse_return_val_if_fail(jobj, FALSE);
+
+ ms_module_dispatch_lock(m);
+
+ if (!_ms_ipc_get_module_idx(m, jobj))
+ goto out;
+
+ m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
+
+ if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
+ ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
+ goto out;
+ }
+
+ if (muse_core_msg_object_get_value(MSG_KEY_PID, jobj, MUSE_TYPE_INT, &pid) && m->pid != pid)
+ LOGW("connected pid [%d] msg [%d] is different", m->pid, pid);
+
+ ms_connection_register(m);
+
+ if (muse_server_is_ready())
+ ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_CREATE_SERVER_ACK);
+ else
+ LOGW("Do not send server acknowledgement because muse server is actually not ready");
+
+ _ms_ipc_data_ch_init(m);
+
+ LOGD("module fd: %d dll_handle: %p", m->ch[MUSE_CHANNEL_MSG].sock_fd, m->ch[MUSE_CHANNEL_MSG].dll_handle);
+ dispatch_ret = ms_module_dispatch(m);
+ ms_module_dispatch_unlock(m);
+
+ if (dispatch_ret != MM_ERROR_NONE) {
+ LOGE("create dispatch failed 0x%x, clean up module", dispatch_ret);
+ goto out;
+ }
+
+ m->is_created = TRUE;
+
+ return TRUE;
+
+out:
+
+ ms_module_dispatch_unlock(m);
+
+ return FALSE;
+}
+
+static gboolean _ms_ipc_dispatch_destroy(muse_module_h m)
+{
+ int dispatch_ret = MM_ERROR_NONE;
+
+ muse_return_val_if_fail(m, FALSE);
+
+ ms_module_dispatch_lock(m);
+ dispatch_ret = ms_module_dispatch(m);
+ if (dispatch_ret != MM_ERROR_NONE)
+ LOGE("destroy dispatch failed 0x%x", dispatch_ret);
+ ms_module_dispatch_unlock(m);
+
+ return FALSE;
+}
+
+static void _ms_ipc_dispatch_no_instance(muse_module_h m, void *jobj)
+{
+ muse_return_if_fail(m);
+ muse_return_if_fail(jobj);
+
+ ms_module_dispatch_lock(m);
+
+ if (!_ms_ipc_get_module_idx(m, jobj))
+ goto out;
+
+ m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(m->idx);
+
+ if (!_ms_ipc_module_instance_creation_is_allowed(m->idx)) {
+ ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
+ goto out;
+ }
+
+ ms_connection_register(m);
+
+ ms_module_dispatch(m);
+
+ SECURE_LOGW("_ms_ipc_module_cleanup [module %p] [loaded value %d]", m, ms_module_get_loaded_dllsym(m->idx));
+
+out:
+ ms_module_dispatch_unlock(m);
+}
+
static gpointer _ms_ipc_dispatch_worker(gpointer data)
{
- int dispatch_ret = MUSE_ERR;
- int len, api, idx, fd, i;
+ int len, api, fd, i;
int parse_len = 0;
muse_module_h m = NULL;
gboolean attempt_to_dispatch = TRUE;
- gboolean value = TRUE;
void *jobj = NULL;
char err_msg[MUSE_MSG_LEN_MAX] = {'\0',};
- int pid = MUSE_ERR;
muse_return_val_if_fail(data, NULL);
switch (api) {
case API_CREATE:
SECURE_LOGI("CREATE %p %d", m, fd);
- ms_module_dispatch_lock(m);
- if (muse_core_msg_object_get_value(MSG_KEY_MODULE_INDEX, jobj, MUSE_TYPE_INT, &idx)) {
- m->idx = idx;
- m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(idx);
- if (!_ms_ipc_module_instance_creation_is_allowed(idx)) {
- ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
- ms_module_dispatch_unlock(m);
- attempt_to_dispatch = FALSE;
- break;
- }
- if (muse_core_msg_object_get_value(MSG_KEY_PID, jobj, MUSE_TYPE_INT, &pid) && m->pid != pid)
- LOGW("connected pid [%d] msg [%d] is different", m->pid, pid);
- ms_connection_register(m);
-
- if (muse_server_is_ready())
- ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_CREATE_SERVER_ACK);
- else
- LOGW("Do not send server acknowledgement because muse server is actually not ready");
-
- m->ch[MUSE_CHANNEL_DATA].data_queue = g_queue_new();
- g_mutex_init(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
- g_cond_init(&m->ch[MUSE_CHANNEL_DATA].data_cond);
- LOGD("module fd: %d dll_handle: %p", fd, m->ch[MUSE_CHANNEL_MSG].dll_handle);
- dispatch_ret = ms_module_dispatch(m);
- }
- ms_module_dispatch_unlock(m);
-
- if (dispatch_ret != MM_ERROR_NONE) {
- LOGE("create dispatch failed 0x%x, clean up module", dispatch_ret);
- attempt_to_dispatch = FALSE;
- } else {
- m->is_created = TRUE;
- }
+ attempt_to_dispatch = _ms_ipc_dispatch_create(m, jobj);
break;
case API_DESTROY:
SECURE_LOGI("DESTROY %p %d", m, fd);
- ms_module_dispatch_lock(m);
- dispatch_ret = ms_module_dispatch(m);
- ms_module_dispatch_unlock(m);
- if (dispatch_ret == MM_ERROR_NONE)
- attempt_to_dispatch = FALSE;
- else
- LOGE("destroy dispatch failed 0x%x", dispatch_ret);
+ attempt_to_dispatch = _ms_ipc_dispatch_destroy(m);
break;
default:
- if (!m->is_created) {
- ms_module_dispatch_lock(m);
- if (muse_core_msg_object_get_value(MSG_KEY_MODULE_INDEX, jobj, MUSE_TYPE_INT, &idx)) {
- m->idx = idx;
- m->ch[MUSE_CHANNEL_MSG].dll_handle = ms_module_open(idx);
- if (!_ms_ipc_module_instance_creation_is_allowed(idx)) {
- ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_RESOURCE_NOT_AVAILABLE);
- ms_module_dispatch_unlock(m);
- attempt_to_dispatch = FALSE;
- break;
- }
- ms_connection_register(m);
-
- m->ch[MUSE_CHANNEL_DATA].data_queue = g_queue_new();
- g_mutex_init(&m->ch[MUSE_CHANNEL_DATA].data_mutex);
- g_cond_init(&m->ch[MUSE_CHANNEL_DATA].data_cond);
- value = ms_module_get_loaded_dllsym(idx);
- } else {
- LOGE("Failed to get the value of module index");
- ms_cmd_dispatch(m, MUSE_MODULE_COMMAND_DEBUG_INFO_DUMP);
- attempt_to_dispatch = FALSE;
- break;
- }
- }
-
- ms_module_dispatch(m);
-
- if (!m->is_created) {
- SECURE_LOGW("_ms_ipc_module_cleanup [module %p] [loaded value %d]", m, value);
- if (!value) {
- LOGW("release module dll handle");
- if (!ms_module_close(m))
- SECURE_LOGE("module %p close fail", m);
- }
- ms_module_dispatch_unlock(m);
- attempt_to_dispatch = FALSE;
- }
+ if (m->is_created) /* handle based */
+ ms_module_dispatch(m);
+ else
+ _ms_ipc_dispatch_no_instance(m, jobj);
break;
}
}