*/
#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;
}
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 0;
}
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);
+
return 0;
}
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);
}
// utility functions
static int guest_connection = 0;
-static pthread_mutex_t mutex_guest = PTHREAD_MUTEX_INITIALIZER;
+extern QemuMutex mutex_guest_connection;
/*static function define*/
static void handle_sdcard(char* dataBuf, size_t dataLen);
}
}
} else if (!strncmp(cmd, MSG_TYPE_GUEST, 5)) {
- pthread_mutex_lock(&mutex_guest);
+ qemu_mutex_lock(&mutex_guest_connection);
value = guest_connection;
- pthread_mutex_unlock(&mutex_guest);
+ qemu_mutex_unlock(&mutex_guest_connection);
send_status_injector_ntf(MSG_TYPE_GUEST, 5, value, NULL);
return true;
}
return true;
} else if (!strncmp(cat, MSG_TYPE_GUEST, 5)) {
INFO("emuld connection is %d\n", action);
- pthread_mutex_lock(&mutex_guest);
+ qemu_mutex_lock(&mutex_guest_connection);
guest_connection = action;
- pthread_mutex_unlock(&mutex_guest);
+ qemu_mutex_unlock(&mutex_guest_connection);
return false;
}
static QTAILQ_HEAD(GS_ClientHead, GS_Client)
clients = QTAILQ_HEAD_INITIALIZER(clients);
-static pthread_mutex_t mutex_clilist = PTHREAD_MUTEX_INITIALIZER;
+static QemuThread guest_thread_id;
+static QemuMutex mutex_clients;
+static int running = 0;
static void remove_sdb_client(GS_Client* client)
{
return;
}
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clients);
QTAILQ_REMOVE(&clients, client, next);
- g_free(client);
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clients);
+
+ g_free(client);
}
static void send_to_sdb_client(GS_Client* client, int state)
void notify_all_sdb_clients(int state)
{
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clients);
GS_Client *client, *next;
QTAILQ_FOREACH_SAFE(client, &clients, next, next)
{
send_to_sdb_client(client, state);
}
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clients);
}
return;
}
+ qemu_mutex_lock(&mutex_clients);
QTAILQ_FOREACH_SAFE(cli, &clients, next, next)
{
if (!strcmp(serial, cli->serial) && !strcmp(inet_ntoa(addr->sin_addr), inet_ntoa((cli->addr).sin_addr))) {
return;
}
}
+ qemu_mutex_unlock(&mutex_clients);
client = g_malloc0(sizeof(GS_Client));
if (NULL == client) {
client->port = port;
strcpy(client->serial, serial);
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clients);
QTAILQ_INSERT_TAIL(&clients, client, next);
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clients);
INFO("Added new sdb client. ip: %s, port: %d, serial: %s\n", inet_ntoa((client->addr).sin_addr), client->port, client->serial);
client_len = sizeof(client_addr);
- while (1) {
+ running = 1;
+
+ while (running) {
memset(&readbuf, 0, RECV_BUF_SIZE);
if (server_sock == 0) {
INFO("server_sock is closed\n");
return;
}
- read_cnt = recvfrom(server_sock, readbuf, RECV_BUF_SIZE, 0,
+ read_cnt = recvfrom(server_sock, readbuf, RECV_BUF_SIZE, MSG_DONTWAIT,
(struct sockaddr*) &client_addr, &client_len);
if (read_cnt < 0) {
static void close_clients(void)
{
- pthread_mutex_lock(&mutex_clilist);
+ qemu_mutex_lock(&mutex_clients);
GS_Client * client, *next;
QTAILQ_FOREACH_SAFE(client, &clients, next, next)
}
}
- pthread_mutex_unlock(&mutex_clilist);
+ qemu_mutex_unlock(&mutex_clients);
}
static void close_server(void)
}
#endif
server_sock = 0;
+
+ qemu_mutex_destroy(&mutex_clients);
}
static void* run_guest_server(void* args)
INFO("guest server start...port:%d\n", port);
+ qemu_mutex_init(&mutex_clients);
+
server_process();
close_server();
void start_guest_server(int server_port)
{
- QemuThread thread_id;
svr_port = server_port;
- qemu_thread_create(&thread_id, "guest_server", run_guest_server, NULL, QEMU_THREAD_DETACHED);
+ qemu_thread_create(&guest_thread_id, "guest_server", run_guest_server, NULL, QEMU_THREAD_JOINABLE);
INFO("created guest server thread\n");
}
void shutdown_guest_server(void)
{
+ void *ret = NULL;
INFO("shutdown_guest_server.\n");
- close_server();
+ running = 0;
+
+ ret = qemu_thread_join(&guest_thread_id);
+ if (ret)
+ ERR("guest_thread_id join failed.\n");
}