*/
#include <stdbool.h>
-#include <pthread.h>
#include <stdlib.h>
#include "hw/qdev.h"
static int g_client_id = 1;
-static pthread_mutex_t mutex_clilist = PTHREAD_MUTEX_INITIALIZER;
+static QemuMutex mutex_clilist;
+QemuMutex mutex_guest_connection;
+
+static QemuThread ecs_thread_id;
static int suspend_state = 1;
if (clii == NULL)
return;
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clilist);
if (clii->client_fd > 0) {
INFO("ecs client closed with fd: %d\n", clii->client_fd);
g_free(clii);
clii = NULL;
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clilist);
}
bool send_to_all_client(const char* data, const int len) {
TRACE("data len: %d, data: %s\n", len, data);
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clilist);
ECS_Client *clii,*next;
{
send_to_client(clii->client_fd, data, len);
}
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clilist);
return true;
}
void send_to_single_client(ECS_Client *clii, const char* data, const int len)
{
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clilist);
send_to_client(clii->client_fd, data, len);
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clilist);
}
void send_to_client(int fd, const char* data, const int len)
g_free(cs);
cs = NULL;
current_ecs = NULL;
+
+ qemu_mutex_destroy(&mutex_clilist);
+ qemu_mutex_destroy(&mutex_guest_connection);
}
#ifndef _WIN32
FD_SET(fd, &cs->reads);
#endif
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clilist);
QTAILQ_INSERT_TAIL(&clients, clii, next);
TRACE("Add an ecs client. fd: %d\n", fd);
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clilist);
// send_ecs_version_check(clii);
current_ecs = cs;
cs->ecs_running = 1;
+ qemu_mutex_init(&mutex_clilist);
+ qemu_mutex_init(&mutex_guest_connection);
+
TRACE("ecs_loop entered.\n");
while (cs->ecs_running) {
ret = ecs_loop(cs);
if (0 > ret) {
- ecs_close(cs);
break;
}
}
TRACE("ecs_loop exited.\n");
+ ecs_close(cs);
+
return NULL;
}
static int stop_ecs(void) {
+ void *ret = NULL;
+
INFO("ecs is closing.\n");
if (NULL != current_ecs) {
current_ecs->ecs_running = 0;
- // ecs_close(current_ecs);
}
- pthread_mutex_destroy(&mutex_clilist);
+ ret = qemu_thread_join(&ecs_thread_id);
+ if (ret) {
+ ERR("ecs is failed to join thread.\n");
+ return -1;
+ }
return 0;
}
static Notifier ecs_exit = { .notify = ecs_notify_exit };
int start_ecs(void) {
- pthread_t thread_id;
-
- if (0 != pthread_create(&thread_id, NULL, ecs_initialize, NULL)) {
- ERR("pthread creation failed.\n");
- return -1;
- }
+ qemu_thread_create(&ecs_thread_id, "ecs", ecs_initialize, NULL, QEMU_THREAD_JOINABLE);
emulator_add_exit_notifier(&ecs_exit);
if (!msg)
goto fail;
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clilist);
if(cli->client_type == TYPE_NONE) {
if (!strncmp(msg->category, MSG_TYPE_NFC, 3)) {
QTAILQ_REMOVE(&clients, cli, next);
}
else {
ERR("unsupported category is found: %s\n", msg->category);
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clilist);
goto fail;
}
}
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clilist);
msgproc_nfc_req(cli, msg);
}
#define MSG_TYPE_NFC "nfc"
#define MSG_TYPE_SIMUL_NFC "simul_nfc"
#define MSG_TYPE_SDCARD "sdcard"
+#define MSG_TYPE_GUEST "guest"
#define MSG_GROUP_STATUS 15
*/
#include <stdbool.h>
-#include <pthread.h>
#include <glib.h>
#include "hw/qdev.h"
#include "emul_state.h"
#include "debug_ch.h"
-MULTI_DEBUG_CHANNEL(qemu, ecs);
+MULTI_DEBUG_CHANNEL(qemu, ecs-msg);
// utility functions
+static int guest_connection = 0;
+extern QemuMutex mutex_guest_connection;
/*static function define*/
static void handle_sdcard(char* dataBuf, size_t dataLen);
type_group group = GROUP_STATUS;
type_action action = act;
- if (cmd == NULL || on == NULL || cmdlen > 10)
+ if (cmd == NULL || cmdlen > 10)
return;
- datalen = strlen(on);
- length = (unsigned short)datalen;
+ if (on == NULL) {
+ msglen = 14;
+ } else {
+ datalen = strlen(on);
+ length = (unsigned short)datalen;
+
+ msglen = datalen + 15;
+ }
- msglen = datalen + 15;
char* status_msg = (char*) malloc(msglen);
if(!status_msg)
return;
memcpy(status_msg + 10, &length, sizeof(unsigned short));
memcpy(status_msg + 12, &group, sizeof(unsigned char));
memcpy(status_msg + 13, &action, sizeof(unsigned char));
- memcpy(status_msg + 14, on, datalen);
+
+ if (on != NULL) {
+ memcpy(status_msg + 14, on, datalen);
+ }
send_injector_ntf(status_msg, msglen);
char data[10];
bool ret = false;
int sndlen = 0;
+ int value = 0;
char* sndbuf;
memset(cmd, 0, 10);
strncpy(cmd, msg->category, sizeof(cmd) -1);
}
TRACE("status : %s", data);
send_status_injector_ntf(MSG_TYPE_SENSOR, 6, action, data);
- ret = true;
- goto injector_req_success;
+ msgproc_injector_ans(ccli, cmd, true);
+ return true;
} else {
if (msg->data.data && datalen > 0) {
set_injector_data((char*) msg->data.data);
}
}
+ } else if (!strncmp(cmd, MSG_TYPE_GUEST, 5)) {
+ qemu_mutex_lock(&mutex_guest_connection);
+ value = guest_connection;
+ qemu_mutex_unlock(&mutex_guest_connection);
+ send_status_injector_ntf(MSG_TYPE_GUEST, 5, value, NULL);
+ return true;
}
injector_send:
sndlen = datalen + 14;
sndbuf = (char*) g_malloc(sndlen + 1);
if (!sndbuf) {
- goto injector_req_fail;
+ msgproc_injector_ans(ccli, cmd, ret);
+ return false;
}
memset(sndbuf, 0, sndlen + 1);
g_free(sndbuf);
- if (!ret)
- goto injector_req_fail;
-
-injector_req_success:
msgproc_injector_ans(ccli, cmd, ret);
- return true;
-injector_req_fail:
- msgproc_injector_ans(ccli, cmd, ret);
- return false;
+ if (!ret) {
+ return false;
+ }
+
+ return true;
}
void ecs_suspend_lock_state(int state)
return true;
}
-static bool injector_req_handle(const char* cat)
+static bool injector_req_handle(const char* cat, type_action action)
{
/*SD CARD msg process*/
if (!strncmp(cat, MSG_TYPE_SDCARD, strlen(MSG_TYPE_SDCARD))) {
return false;
- }else
- if (!strncmp(cat, "suspend", 7)) {
+ } else if (!strncmp(cat, "suspend", 7)) {
ecs_suspend_lock_state(ecs_get_suspend_state());
return true;
+ } else if (!strncmp(cat, MSG_TYPE_GUEST, 5)) {
+ INFO("emuld connection is %d\n", action);
+ qemu_mutex_lock(&mutex_guest_connection);
+ guest_connection = action;
+ qemu_mutex_unlock(&mutex_guest_connection);
+ return false;
}
return false;
read_val_char(data + catsize + 2, &group);
read_val_char(data + catsize + 2 + 1, &action);
- if (injector_req_handle(cat)) {
+ if (injector_req_handle(cat, action)) {
return true;
}