From bb4b15f33c710176dfd266991e156fe597d13e5d Mon Sep 17 00:00:00 2001 From: Jinhyung Choi Date: Wed, 25 Sep 2013 20:45:25 +0900 Subject: [PATCH] qemu: fixed bug(message format) and added boilerplate Change-Id: I018b69a1f647fbcab03410beb0abd21b4b7419e5 Signed-off-by: Jinhyung Choi --- tizen/src/ecs/ecs.c | 31 +- tizen/src/ecs/ecs.h | 30 ++ tizen/src/ecs/ecs_msg.c | 31 ++ tizen/src/ecs/ecs_sensor.c | 3 +- tizen/src/hw/maru_virtio_nfc.c | 662 +++++++++++++++++++------------------- tizen/src/hw/maru_virtio_sensor.c | 255 +++++++-------- 6 files changed, 546 insertions(+), 466 deletions(-) diff --git a/tizen/src/ecs/ecs.c b/tizen/src/ecs/ecs.c index 1e17d91..b5c2c8f 100644 --- a/tizen/src/ecs/ecs.c +++ b/tizen/src/ecs/ecs.c @@ -1,3 +1,33 @@ +/* + * Emulator Control Server + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: + * Jinhyung choi + * MunKyu Im + * Daiyoung Kim + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + #include #include #include @@ -881,7 +911,6 @@ static Monitor *monitor_create(void) { LOG("monitor allocation failed."); return NULL; } - memset(mon, 0, sizeof(*mon)); return mon; } diff --git a/tizen/src/ecs/ecs.h b/tizen/src/ecs/ecs.h index 9283747..efab4db 100644 --- a/tizen/src/ecs/ecs.h +++ b/tizen/src/ecs/ecs.h @@ -1,3 +1,33 @@ +/* + * Emulator Control Server + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: + * Jinhyung choi + * MunKyu Im + * Daiyoung Kim + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + #ifndef __ECS_H__ #define __ECS_H__ diff --git a/tizen/src/ecs/ecs_msg.c b/tizen/src/ecs/ecs_msg.c index 864c1a6..68b2e46 100644 --- a/tizen/src/ecs/ecs_msg.c +++ b/tizen/src/ecs/ecs_msg.c @@ -1,3 +1,32 @@ +/* Emulator Control Server + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: + * Jinhyung choi + * MunKyu Im + * Daiyoung Kim + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + #include #include @@ -426,6 +455,8 @@ bool send_device_ntf(const char* data, const int len) ntf.data.data = g_malloc(length); ntf.data.len = length; memcpy(ntf.data.data, ijdata, length); + + LOG("data = %s, length = %hu", ijdata, length); } master.type = ECS__MASTER__TYPE__DEVICE_NTF; diff --git a/tizen/src/ecs/ecs_sensor.c b/tizen/src/ecs/ecs_sensor.c index 666eff7..ccb04c3 100644 --- a/tizen/src/ecs/ecs_sensor.c +++ b/tizen/src/ecs/ecs_sensor.c @@ -1,10 +1,11 @@ /* * Emulator Control Server - Sensor Device Handler * - * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved * * Contact: * Jinhyung choi + * MunKyu Im * Daiyoung Kim * YeongKyoon Lee * diff --git a/tizen/src/hw/maru_virtio_nfc.c b/tizen/src/hw/maru_virtio_nfc.c index 7a38d8c..d40ac50 100755 --- a/tizen/src/hw/maru_virtio_nfc.c +++ b/tizen/src/hw/maru_virtio_nfc.c @@ -1,334 +1,328 @@ -/* - * Virtio NFC Device - * - * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved - * - * Contact: - * Munkyu Im - * YeongKyoon Lee - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU General Public License - * as published by the Free Software Foundation; either version 2 - * of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * Contributors: - * - S-Core Co., Ltd - * - */ - -#include - -#include "maru_device_ids.h" -#include "maru_virtio_nfc.h" -#include "debug_ch.h" -#include "../ecs/ecs.h" - -MULTI_DEBUG_CHANNEL(qemu, virtio-nfc); - -#define NFC_DEVICE_NAME "virtio-nfc" - - -enum { - IOTYPE_INPUT = 0, - IOTYPE_OUTPUT = 1 -}; - - -#ifndef min -#define min(a,b) ((a)<(b)?(a):(b)) -#endif - -#define MAX_BUF_SIZE 4096 - - -VirtIONFC* vio_nfc; - - -typedef unsigned int CSCliSN; - -typedef struct msg_info { - char buf[MAX_BUF_SIZE]; - - uint32_t route; - uint32_t use; - uint16_t count; - uint16_t index; - - CSCliSN cclisn; -}msg_info; - -// - -typedef struct MsgInfo -{ - msg_info info; - QTAILQ_ENTRY(MsgInfo) next; -}MsgInfo; - -static QTAILQ_HEAD(MsgInfoRecvHead , MsgInfo) nfc_recv_msg_queue = - QTAILQ_HEAD_INITIALIZER(nfc_recv_msg_queue); - - -static QTAILQ_HEAD(MsgInfoSendHead , MsgInfo) nfc_send_msg_queue = - QTAILQ_HEAD_INITIALIZER(nfc_send_msg_queue); - - -// - -typedef struct NFCBuf { - VirtQueueElement elem; - - QTAILQ_ENTRY(NFCBuf) next; -} NFCBuf; - -static QTAILQ_HEAD(NFCMsgHead , NFCBuf) nfc_in_queue = - QTAILQ_HEAD_INITIALIZER(nfc_in_queue); - -static int count = 0; - -static pthread_mutex_t recv_buf_mutex = PTHREAD_MUTEX_INITIALIZER; - -bool send_to_nfc(enum request_cmd_nfc req, const char* data, const uint32_t len) -{ - MsgInfo* _msg = (MsgInfo*) malloc(sizeof(MsgInfo)); - if (!_msg) - return false; - - memset(&_msg->info, 0, sizeof(msg_info)); - - memcpy(_msg->info.buf, data, len); - _msg->info.use = len; - _msg->info.index = count++; - _msg->info.route = req; - pthread_mutex_lock(&recv_buf_mutex); - - QTAILQ_INSERT_TAIL(&nfc_recv_msg_queue, _msg, next); - - pthread_mutex_unlock(&recv_buf_mutex); - - qemu_bh_schedule(vio_nfc->bh); - - return true; -} - - -static int g_cnt = 0; - -static void flush_nfc_recv_queue(void) -{ - int index; - - if (unlikely(!virtio_queue_ready(vio_nfc->rvq))) { - INFO("virtio queue is not ready\n"); - return; - } - - if (unlikely(virtio_queue_empty(vio_nfc->rvq))) { - TRACE("virtqueue is empty\n"); - return; - } - - - pthread_mutex_lock(&recv_buf_mutex); - - while (!QTAILQ_EMPTY(&nfc_recv_msg_queue)) - { - MsgInfo* msginfo = QTAILQ_FIRST(&nfc_recv_msg_queue); - if (!msginfo) - break; - - VirtQueueElement elem; - index = virtqueue_pop(vio_nfc->rvq, &elem); - if (index == 0) - { - //ERR("unexpected empty queue"); - break; - } - - INFO(">> virtqueue_pop. index: %d, out_num : %d, in_num : %d\n", index, elem.out_num, elem.in_num); - - memcpy(elem.in_sg[0].iov_base, &msginfo->info, sizeof(struct msg_info)); - - INFO(">> send to guest count = %d, use = %d, msg = %s, iov_len = %d \n", - ++g_cnt, msginfo->info.use, msginfo->info.buf, elem.in_sg[0].iov_len); - - virtqueue_push(vio_nfc->rvq, &elem, sizeof(msg_info)); - virtio_notify(&vio_nfc->vdev, vio_nfc->rvq); - - QTAILQ_REMOVE(&nfc_recv_msg_queue, msginfo, next); - if (msginfo) - free(msginfo); - } - - pthread_mutex_unlock(&recv_buf_mutex); - -} - - -static void virtio_nfc_recv(VirtIODevice *vdev, VirtQueue *vq) -{ - flush_nfc_recv_queue(); -} - -static void send_to_ecs(struct msg_info* msg) -{ - int buf_len; - char data_len [2]; - char group [1] = { 15 }; - char action [1]; - int message_len = 0; - - char* ecs_message = NULL; - - buf_len = strlen(msg->buf); - message_len = buf_len + 14; - - ecs_message = (char*) malloc(message_len + 1); - if (!ecs_message) - return; - - memset(ecs_message, 0, message_len + 1); - - data_len[0] = buf_len; - action[0] = 0; - - memcpy(ecs_message, "nfc", 10); - memcpy(ecs_message + 10, &data_len, 2); - memcpy(ecs_message + 12, &group, 1); - memcpy(ecs_message + 13, &action, 1); - memcpy(ecs_message + 14, msg->buf, buf_len); - - INFO("ntf_to_injector- bufnum: %s, group: %s, action: %s, data: %s\n", data_len, group, action, msg->buf); - - //ntf_to_injector(ecs_message, message_len); - send_device_ntf(ecs_message, message_len); - - if (ecs_message) - free(ecs_message); -} - -static void virtio_nfc_send(VirtIODevice *vdev, VirtQueue *vq) -{ - VirtIONFC *vnfc = (VirtIONFC *)vdev; - int index = 0; - struct msg_info _msg; - - if (virtio_queue_empty(vnfc->svq)) { - INFO("<< virtqueue is empty.\n"); - return; - } - - VirtQueueElement elem; - - while ((index = virtqueue_pop(vq, &elem))) { - - INFO("<< virtqueue pop. index: %d, out_num : %d, in_num : %d\n", index, elem.out_num, elem.in_num); - - if (index == 0) { - INFO("<< virtqueue break\n"); - break; - } - - INFO("<< use=%d, iov_len = %d\n", _msg.use, elem.out_sg[0].iov_len); - - memset(&_msg, 0x00, sizeof(_msg)); - memcpy(&_msg, elem.out_sg[0].iov_base, elem.out_sg[0].iov_len); - - INFO("<< recv from guest len = %d, msg = %s \n", _msg.use, _msg.buf); - - send_to_ecs(&_msg); - - } - - virtqueue_push(vq, &elem, sizeof(VirtIONFC)); - virtio_notify(&vio_nfc->vdev, vq); -} - -static uint32_t virtio_nfc_get_features(VirtIODevice *vdev, - uint32_t request_feature) -{ - TRACE("virtio_nfc_get_features.\n"); - return 0; -} - -static void maru_nfc_bh(void *opaque) -{ - flush_nfc_recv_queue(); -} - -static int virtio_nfc_init(VirtIODevice* vdev) -{ - INFO("initialize nfc device\n"); - vio_nfc = VIRTIO_NFC(vdev); - - virtio_init(vdev, NFC_DEVICE_NAME, VIRTIO_ID_NFC, 0); - - if (vio_nfc == NULL) { - ERR("failed to initialize nfc device\n"); - return -1; - } - - vio_nfc->rvq = virtio_add_queue(&vio_nfc->vdev, 256, virtio_nfc_recv); - vio_nfc->svq = virtio_add_queue(&vio_nfc->vdev, 256, virtio_nfc_send); - - vio_nfc->bh = qemu_bh_new(maru_nfc_bh, vio_nfc); - - return 0; -} - -static int virtio_nfc_exit(DeviceState* dev) -{ - INFO("destroy nfc device\n"); - VirtIODevice *vdev = VIRTIO_DEVICE(dev); - - if (vio_nfc->bh) { - qemu_bh_delete(vio_nfc->bh); - } - - virtio_cleanup(vdev); - - return 0; -} - -static void virtio_nfc_reset(VirtIODevice *vdev) -{ - TRACE("virtio_sensor_reset.\n"); -} - - -static void virtio_nfc_class_init(ObjectClass *klass, void *data) -{ - DeviceClass *dc = DEVICE_CLASS(klass); - VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); - dc->exit = virtio_nfc_exit; - vdc->init = virtio_nfc_init; - vdc->get_features = virtio_nfc_get_features; - vdc->reset = virtio_nfc_reset; -} - - - -static const TypeInfo virtio_device_info = { - .name = TYPE_VIRTIO_NFC, - .parent = TYPE_VIRTIO_DEVICE, - .instance_size = sizeof(VirtIONFC), - .class_init = virtio_nfc_class_init, -}; - -static void virtio_register_types(void) -{ - type_register_static(&virtio_device_info); -} - -type_init(virtio_register_types) - +/* + * Virtio NFC Device + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: + * Munkyu Im + * YeongKyoon Lee + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributors: + * - S-Core Co., Ltd + * + */ + +#include + +#include "maru_device_ids.h" +#include "maru_virtio_nfc.h" +#include "debug_ch.h" +#include "../ecs/ecs.h" + +MULTI_DEBUG_CHANNEL(qemu, virtio-nfc); + +#define NFC_DEVICE_NAME "virtio-nfc" + + +enum { + IOTYPE_INPUT = 0, + IOTYPE_OUTPUT = 1 +}; + + +#ifndef min +#define min(a,b) ((a)<(b)?(a):(b)) +#endif + +#define MAX_BUF_SIZE 4096 + + +VirtIONFC* vio_nfc; + + +typedef unsigned int CSCliSN; + +typedef struct msg_info { + char buf[MAX_BUF_SIZE]; + + uint32_t route; + uint32_t use; + uint16_t count; + uint16_t index; + + CSCliSN cclisn; +}msg_info; + +// + +typedef struct MsgInfo +{ + msg_info info; + QTAILQ_ENTRY(MsgInfo) next; +}MsgInfo; + +static QTAILQ_HEAD(MsgInfoRecvHead , MsgInfo) nfc_recv_msg_queue = + QTAILQ_HEAD_INITIALIZER(nfc_recv_msg_queue); + + +static QTAILQ_HEAD(MsgInfoSendHead , MsgInfo) nfc_send_msg_queue = + QTAILQ_HEAD_INITIALIZER(nfc_send_msg_queue); + + +// + +typedef struct NFCBuf { + VirtQueueElement elem; + + QTAILQ_ENTRY(NFCBuf) next; +} NFCBuf; + +static QTAILQ_HEAD(NFCMsgHead , NFCBuf) nfc_in_queue = + QTAILQ_HEAD_INITIALIZER(nfc_in_queue); + +static int count = 0; + +static pthread_mutex_t recv_buf_mutex = PTHREAD_MUTEX_INITIALIZER; + +bool send_to_nfc(enum request_cmd_nfc req, const char* data, const uint32_t len) +{ + MsgInfo* _msg = (MsgInfo*) malloc(sizeof(MsgInfo)); + if (!_msg) + return false; + + memset(&_msg->info, 0, sizeof(msg_info)); + + memcpy(_msg->info.buf, data, len); + _msg->info.use = len; + _msg->info.index = count++; + _msg->info.route = req; + pthread_mutex_lock(&recv_buf_mutex); + + QTAILQ_INSERT_TAIL(&nfc_recv_msg_queue, _msg, next); + + pthread_mutex_unlock(&recv_buf_mutex); + + qemu_bh_schedule(vio_nfc->bh); + + return true; +} + + +static int g_cnt = 0; + +static void flush_nfc_recv_queue(void) +{ + int index; + + if (unlikely(!virtio_queue_ready(vio_nfc->rvq))) { + INFO("virtio queue is not ready\n"); + return; + } + + if (unlikely(virtio_queue_empty(vio_nfc->rvq))) { + TRACE("virtqueue is empty\n"); + return; + } + + + pthread_mutex_lock(&recv_buf_mutex); + + while (!QTAILQ_EMPTY(&nfc_recv_msg_queue)) + { + MsgInfo* msginfo = QTAILQ_FIRST(&nfc_recv_msg_queue); + if (!msginfo) + break; + + VirtQueueElement elem; + index = virtqueue_pop(vio_nfc->rvq, &elem); + if (index == 0) + { + //ERR("unexpected empty queue"); + break; + } + + INFO(">> virtqueue_pop. index: %d, out_num : %d, in_num : %d\n", index, elem.out_num, elem.in_num); + + memcpy(elem.in_sg[0].iov_base, &msginfo->info, sizeof(struct msg_info)); + + INFO(">> send to guest count = %d, use = %d, msg = %s, iov_len = %d \n", + ++g_cnt, msginfo->info.use, msginfo->info.buf, elem.in_sg[0].iov_len); + + virtqueue_push(vio_nfc->rvq, &elem, sizeof(msg_info)); + virtio_notify(&vio_nfc->vdev, vio_nfc->rvq); + + QTAILQ_REMOVE(&nfc_recv_msg_queue, msginfo, next); + if (msginfo) + free(msginfo); + } + + pthread_mutex_unlock(&recv_buf_mutex); + +} + + +static void virtio_nfc_recv(VirtIODevice *vdev, VirtQueue *vq) +{ + flush_nfc_recv_queue(); +} + +static void send_to_ecs(struct msg_info* msg) +{ + type_length length = 0; + type_group group = 15; + type_action action = 0; + + int buf_len = strlen(msg->buf); + int message_len = buf_len + 14; + + char* ecs_message = (char*) malloc(message_len + 1); + if (!ecs_message) + return; + + memset(ecs_message, 0, message_len + 1); + + length = (unsigned short) buf_len; + + memcpy(ecs_message, "nfc", 10); + memcpy(ecs_message + 10, &length, sizeof(unsigned short)); + memcpy(ecs_message + 12, &group, sizeof(unsigned char)); + memcpy(ecs_message + 13, &action, sizeof(unsigned char)); + memcpy(ecs_message + 14, msg->buf, buf_len); + + INFO("ntf_to_injector- len: %d, group: %d, action: %d, data: %s\n", length, group, action, msg->buf); + + send_device_ntf(ecs_message, message_len); + + if (ecs_message) + free(ecs_message); +} + +static void virtio_nfc_send(VirtIODevice *vdev, VirtQueue *vq) +{ + VirtIONFC *vnfc = (VirtIONFC *)vdev; + int index = 0; + struct msg_info _msg; + + if (virtio_queue_empty(vnfc->svq)) { + INFO("<< virtqueue is empty.\n"); + return; + } + + VirtQueueElement elem; + + while ((index = virtqueue_pop(vq, &elem))) { + + INFO("<< virtqueue pop. index: %d, out_num : %d, in_num : %d\n", index, elem.out_num, elem.in_num); + + if (index == 0) { + INFO("<< virtqueue break\n"); + break; + } + + INFO("<< use=%d, iov_len = %d\n", _msg.use, elem.out_sg[0].iov_len); + + memset(&_msg, 0x00, sizeof(_msg)); + memcpy(&_msg, elem.out_sg[0].iov_base, elem.out_sg[0].iov_len); + + INFO("<< recv from guest len = %d, msg = %s \n", _msg.use, _msg.buf); + + send_to_ecs(&_msg); + + } + + virtqueue_push(vq, &elem, sizeof(VirtIONFC)); + virtio_notify(&vio_nfc->vdev, vq); +} + +static uint32_t virtio_nfc_get_features(VirtIODevice *vdev, + uint32_t request_feature) +{ + TRACE("virtio_nfc_get_features.\n"); + return 0; +} + +static void maru_nfc_bh(void *opaque) +{ + flush_nfc_recv_queue(); +} + +static int virtio_nfc_init(VirtIODevice* vdev) +{ + INFO("initialize nfc device\n"); + vio_nfc = VIRTIO_NFC(vdev); + + virtio_init(vdev, NFC_DEVICE_NAME, VIRTIO_ID_NFC, 0); + + if (vio_nfc == NULL) { + ERR("failed to initialize nfc device\n"); + return -1; + } + + vio_nfc->rvq = virtio_add_queue(&vio_nfc->vdev, 256, virtio_nfc_recv); + vio_nfc->svq = virtio_add_queue(&vio_nfc->vdev, 256, virtio_nfc_send); + + vio_nfc->bh = qemu_bh_new(maru_nfc_bh, vio_nfc); + + return 0; +} + +static int virtio_nfc_exit(DeviceState* dev) +{ + INFO("destroy nfc device\n"); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); + + if (vio_nfc->bh) { + qemu_bh_delete(vio_nfc->bh); + } + + virtio_cleanup(vdev); + + return 0; +} + +static void virtio_nfc_reset(VirtIODevice *vdev) +{ + TRACE("virtio_sensor_reset.\n"); +} + + +static void virtio_nfc_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); + dc->exit = virtio_nfc_exit; + vdc->init = virtio_nfc_init; + vdc->get_features = virtio_nfc_get_features; + vdc->reset = virtio_nfc_reset; +} + + + +static const TypeInfo virtio_device_info = { + .name = TYPE_VIRTIO_NFC, + .parent = TYPE_VIRTIO_DEVICE, + .instance_size = sizeof(VirtIONFC), + .class_init = virtio_nfc_class_init, +}; + +static void virtio_register_types(void) +{ + type_register_static(&virtio_device_info); +} + +type_init(virtio_register_types) + diff --git a/tizen/src/hw/maru_virtio_sensor.c b/tizen/src/hw/maru_virtio_sensor.c index a651a63..a89272d 100644 --- a/tizen/src/hw/maru_virtio_sensor.c +++ b/tizen/src/hw/maru_virtio_sensor.c @@ -1,7 +1,7 @@ /* * Virtio Sensor Device * - * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved + * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved * * Contact: * Jinhyung Choi @@ -38,19 +38,19 @@ MULTI_DEBUG_CHANNEL(qemu, virtio-sensor); -#define SENSOR_DEVICE_NAME "sensor" -#define _MAX_BUF 1024 +#define SENSOR_DEVICE_NAME "sensor" +#define _MAX_BUF 1024 VirtIOSENSOR* vsensor; typedef struct msg_info { - char buf[_MAX_BUF]; + char buf[_MAX_BUF]; - uint16_t type; - uint16_t req; + uint16_t type; + uint16_t req; - QTAILQ_ENTRY(msg_info) next; + QTAILQ_ENTRY(msg_info) next; } msg_info; @@ -61,163 +61,158 @@ static pthread_mutex_t buf_mutex = PTHREAD_MUTEX_INITIALIZER; static void add_msg_queue(msg_info* msg) { - pthread_mutex_lock(&buf_mutex); - QTAILQ_INSERT_TAIL(&sensor_msg_queue, msg, next); - pthread_mutex_unlock(&buf_mutex); + pthread_mutex_lock(&buf_mutex); + QTAILQ_INSERT_TAIL(&sensor_msg_queue, msg, next); + pthread_mutex_unlock(&buf_mutex); - qemu_bh_schedule(vsensor->bh); + qemu_bh_schedule(vsensor->bh); } void req_sensor_data (enum sensor_types type, enum request_cmd req, char* data, int len) { - if (type >= sensor_type_max || (req != request_get && req != request_set)) { - ERR("unavailable sensor type request.\n"); - } - - msg_info* msg = (msg_info*) malloc(sizeof(msg_info)); - if (!msg) { - ERR("The allocation of msg_info is failed.\n"); - return; - } - - memset(msg, 0, sizeof(msg_info)); - - if (req == request_set) { - if (len > _MAX_BUF) { - ERR("The data is too big to send.\n"); - return; - } - memcpy(msg->buf, data, len); - } - - msg->type = type; - msg->req = req; - - add_msg_queue(msg); + if (type >= sensor_type_max || (req != request_get && req != request_set)) { + ERR("unavailable sensor type request.\n"); + } + + msg_info* msg = (msg_info*) malloc(sizeof(msg_info)); + if (!msg) { + ERR("The allocation of msg_info is failed.\n"); + return; + } + + memset(msg, 0, sizeof(msg_info)); + + if (req == request_set) { + if (len > _MAX_BUF) { + ERR("The data is too big to send.\n"); + return; + } + memcpy(msg->buf, data, len); + } + + msg->type = type; + msg->req = req; + + add_msg_queue(msg); } static void flush_sensor_recv_queue(void) { - int index; + int index; if (unlikely(!virtio_queue_ready(vsensor->rvq))) { INFO("virtio queue is not ready\n"); return; } - if (unlikely(virtio_queue_empty(vsensor->rvq))) { - INFO("virtqueue is empty\n"); - return; - } - - pthread_mutex_lock(&buf_mutex); - while (!QTAILQ_EMPTY(&sensor_msg_queue)) - { - msg_info* msginfo = QTAILQ_FIRST(&sensor_msg_queue); - if (!msginfo) { - ERR("msginfo is NULL!\n"); - break; - } - - INFO("sending message: %s, type: %d, req: %d\n", msginfo->buf, msginfo->type, msginfo->req); - - VirtQueueElement elem; - index = virtqueue_pop(vsensor->rvq, &elem); - if (index == 0) - break; - - memcpy(elem.in_sg[0].iov_base, msginfo, sizeof(struct msg_info)); - - virtqueue_push(vsensor->rvq, &elem, sizeof(msg_info)); - virtio_notify(&vsensor->vdev, vsensor->rvq); - - QTAILQ_REMOVE(&sensor_msg_queue, msginfo, next); - if (msginfo) - free(msginfo); - } - pthread_mutex_unlock(&buf_mutex); + if (unlikely(virtio_queue_empty(vsensor->rvq))) { + INFO("virtqueue is empty\n"); + return; + } + + pthread_mutex_lock(&buf_mutex); + while (!QTAILQ_EMPTY(&sensor_msg_queue)) + { + msg_info* msginfo = QTAILQ_FIRST(&sensor_msg_queue); + if (!msginfo) { + ERR("msginfo is NULL!\n"); + break; + } + + INFO("sending message: %s, type: %d, req: %d\n", msginfo->buf, msginfo->type, msginfo->req); + + VirtQueueElement elem; + index = virtqueue_pop(vsensor->rvq, &elem); + if (index == 0) + break; + + memcpy(elem.in_sg[0].iov_base, msginfo, sizeof(struct msg_info)); + + virtqueue_push(vsensor->rvq, &elem, sizeof(msg_info)); + virtio_notify(&vsensor->vdev, vsensor->rvq); + + QTAILQ_REMOVE(&sensor_msg_queue, msginfo, next); + if (msginfo) + free(msginfo); + } + pthread_mutex_unlock(&buf_mutex); } static void virtio_sensor_recv(VirtIODevice *vdev, VirtQueue *vq) { - flush_sensor_recv_queue(); + flush_sensor_recv_queue(); } static void maru_sensor_bh(void *opaque) { - flush_sensor_recv_queue(); + flush_sensor_recv_queue(); } -static int get_action(enum sensor_types type) +static type_action get_action(enum sensor_types type) { - int action = 0; - - switch (type) { - case sensor_type_accel: - action = ACTION_ACCEL; - break; - case sensor_type_gyro: - action = ACTION_GYRO; - break; - case sensor_type_mag: - action = ACTION_MAG; - break; - case sensor_type_light: - action = ACTION_LIGHT; - break; - case sensor_type_proxi: - action = ACTION_PROXI; - break; - default: - break; - } - - return action; + type_action action = 0; + + switch (type) { + case sensor_type_accel: + action = ACTION_ACCEL; + break; + case sensor_type_gyro: + action = ACTION_GYRO; + break; + case sensor_type_mag: + action = ACTION_MAG; + break; + case sensor_type_light: + action = ACTION_LIGHT; + break; + case sensor_type_proxi: + action = ACTION_PROXI; + break; + default: + break; + } + + return action; } static void send_to_ecs(struct msg_info* msg) { - int buf_len; - char data_len [2]; - char group [1] = { GROUP_STATUS }; - char action [1]; - int message_len = 0; + type_length length = 0; + type_group group = GROUP_STATUS; + type_action action = 0; - char* ecs_message = NULL; + int buf_len = strlen(msg->buf); + int message_len = buf_len + 14; - buf_len = strlen(msg->buf); - message_len = buf_len + 14; - - ecs_message = (char*) malloc(message_len + 1); - if (!ecs_message) - return; + char* ecs_message = (char*) malloc(message_len + 1); + if (!ecs_message) + return; - memset(ecs_message, 0, message_len + 1); + memset(ecs_message, 0, message_len + 1); - data_len[0] = buf_len; - action[0] = get_action(msg->type); + length = (unsigned short) buf_len; + action = get_action(msg->type); - memcpy(ecs_message, MESSAGE_TYPE_SENSOR, 10); - memcpy(ecs_message + 10, &data_len, 2); - memcpy(ecs_message + 12, &group, 1); - memcpy(ecs_message + 13, &action, 1); - memcpy(ecs_message + 14, msg->buf, buf_len); + memcpy(ecs_message, MESSAGE_TYPE_SENSOR, 10); + memcpy(ecs_message + 10, &length, sizeof(unsigned short)); + memcpy(ecs_message + 12, &group, sizeof(unsigned char)); + memcpy(ecs_message + 13, &action, sizeof(unsigned char)); + memcpy(ecs_message + 14, msg->buf, buf_len); - INFO("ntf_to_injector- bufnum: %s, group: %s, action: %s, data: %s\n", data_len, group, action, msg->buf); + INFO("ntf_to_injector- len: %d, group: %d, action: %d, data: %s\n", length, group, action, msg->buf); - //ntf_to_injector(ecs_message, message_len); - send_device_ntf(ecs_message, message_len); + send_device_ntf(ecs_message, message_len); - if (ecs_message) - free(ecs_message); + if (ecs_message) + free(ecs_message); } static void virtio_sensor_send(VirtIODevice *vdev, VirtQueue *vq) { - VirtIOSENSOR *vsensor = (VirtIOSENSOR*)vdev; - struct msg_info msg; + VirtIOSENSOR *vsensor = (VirtIOSENSOR*)vdev; + struct msg_info msg; VirtQueueElement elem; - int index = 0; + int index = 0; if (virtio_queue_empty(vsensor->svq)) { INFO("<< virtqueue is empty.\n"); @@ -226,15 +221,15 @@ static void virtio_sensor_send(VirtIODevice *vdev, VirtQueue *vq) while ((index = virtqueue_pop(vq, &elem))) { - memset(&msg, 0x00, sizeof(msg)); - memcpy(&msg, elem.out_sg[0].iov_base, elem.out_sg[0].iov_len); + memset(&msg, 0x00, sizeof(msg)); + memcpy(&msg, elem.out_sg[0].iov_base, elem.out_sg[0].iov_len); - INFO("send to ecs: %s, len: %d, type: %d, req: %d\n", msg.buf, strlen(msg.buf), msg.type, msg.req); - send_to_ecs(&msg); + INFO("send to ecs: %s, len: %d, type: %d, req: %d\n", msg.buf, strlen(msg.buf), msg.type, msg.req); + send_to_ecs(&msg); } - virtqueue_push(vq, &elem, sizeof(VirtIOSENSOR)); - virtio_notify(&vsensor->vdev, vq); + virtqueue_push(vq, &elem, sizeof(VirtIOSENSOR)); + virtio_notify(&vsensor->vdev, vq); } @@ -261,11 +256,11 @@ static int virtio_sensor_init(VirtIODevice *vdev) static int virtio_sensor_exit(DeviceState *dev) { - VirtIODevice *vdev = VIRTIO_DEVICE(dev); + VirtIODevice *vdev = VIRTIO_DEVICE(dev); INFO("destroy sensor device\n"); - if (vsensor->bh) - qemu_bh_delete(vsensor->bh); + if (vsensor->bh) + qemu_bh_delete(vsensor->bh); virtio_cleanup(vdev); -- 2.7.4