-/*
+/* -*- Mode: C; c-basic-offset: 8; indent-tabs-mode: t -*-
* emulator-daemon
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
- * Contact:
+ * Contact:
+ * SooYoung Ha <yoosah.ha@samsnung.com>
* Sungmin Ha <sungmin82.ha@samsung.com>
- * DongKyun Yun <dk77.yun@samsung.com>
* YeongKyoon Lee <yeongkyoon.lee@samsung.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
License: GNU General Public License
------------------------------------------------------------------*/
-
+#include "emuld_common.h"
#include "emuld.h"
+#include <error.h>
+
+#define MAX_CONNECT_TRY_COUNT (60 * 3)
/* global definition */
unsigned short sdbd_port = SDBD_PORT;
unsigned short vmodem_port = VMODEM_PORT;
int g_sdbd_sockfd = -1; /* sdbd fd */
static int g_vm_connect_status; /* connection status between emuld and vmodem */
int g_sdcard_sockfd = -1;
+int g_get_status_sockfd = -1; /* return status of devive to the injector */
pthread_t tid[MAX_CLIENT + 1];
int g_epoll_fd; /* epoll fd */
-struct epoll_event g_events[MAX_EVENTS];
+static pthread_mutex_t mutex_vmconnect = PTHREAD_MUTEX_INITIALIZER;
-// for vmodem packets
-typedef struct // lxt_message
-{
- unsigned short length;
- unsigned char group;
- unsigned char action;
- void *data;
-} LXT_MESSAGE;
+struct epoll_event g_events[MAX_EVENTS];
void TAPIMessageInit(LXT_MESSAGE *packet)
{
}
char SDpath[256];
-
+
+unsigned char ActionID = 0;
+char GetBuffer[256];
+void setting_device();
/*--------------------------------------------------------------*/
/* FUNCTION PART
---------------------------------------------------------------*/
}
}
+bool is_vm_connected(void)
+{
+ bool ret = false;
+
+ pthread_mutex_lock(&mutex_vmconnect);
+
+ if (g_vm_connect_status == 1)
+ {
+ ret = true;
+ }
+
+ pthread_mutex_unlock(&mutex_vmconnect);
+
+ return ret;
+}
+
+void set_vm_connect_status(const int v)
+{
+ pthread_mutex_lock(&mutex_vmconnect);
+
+ g_vm_connect_status = v;
+
+ pthread_mutex_unlock(&mutex_vmconnect);
+}
+
/*-------------------------------------------------------------
function: init_server0
io: input : integer - server port (must be positive)
{
struct sockaddr_in vm_addr;
int ret = -1;
- g_vm_connect_status = 0;
+ //int connect_try_count = 0;
+ bool is_connected = false;
- LOG("start");
+ set_vm_connect_status(0);
+
+ LOG("init_vm_connect start");
pthread_detach(pthread_self());
/* Open TCP Socket */
- if( (g_vm_sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 )
+ if( (g_vm_sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
{
fprintf(stderr, "Server Start Fails. : Can't open stream socket \n");
exit(0);
vm_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
vm_addr.sin_port = htons(vmodem_port);
- while(ret < 0)
+ while(ret < 0 /*&& (connect_try_count < MAX_CONNECT_TRY_COUNT)*/)
{
ret = connect(g_vm_sockfd, (struct sockaddr *)&vm_addr, sizeof(vm_addr));
LOG("vm_sockfd: %d, connect ret: %d", g_vm_sockfd, ret);
if(ret < 0) {
- LOG("connection failed to vmodem!");
+ LOG("connection failed to vmodem! try \n");
sleep(1);
}
+ else
+ {
+ is_connected = true;
+ }
+ //connect_try_count ++;
}
+ /*
+ if (!is_connected)
+ {
+ LOG("try to connect to vmoden was failed!");
+ exit(0);
+ }*/
userpool_add(g_vm_sockfd, vm_addr.sin_port);
/* add client sockfd at event pool */
epoll_cli_add(g_vm_sockfd);
- g_vm_connect_status = 1;
+ set_vm_connect_status(1);
+
pthread_exit((void *) 0);
}
void* mount_sdcard(void* data)
{
- int ret = -1, i = 0, vconf_value = -1;
+ int ret = -1, i = 0;
struct stat buf;
char file_name[128], command[256];
memset(file_name, '\0', sizeof(file_name));
if( i != 10 )
{
LOG( "%s is exist", file_name);
- ret = mount(file_name, "/mnt/mmc", "ext3", 0, "");
- LOG("mount ret = %d, errno = %d", ret, errno);
-
LOG("sdcard fd: %d", g_sdcard_sockfd);
if(g_sdcard_sockfd != -1)
{
- packet->length = strlen(SDpath); // length
- packet->group = 11; // sdcard
+ packet->length = strlen(SDpath); // length
+ packet->group = 11; // sdcard
if(ret == 0)
packet->action = 1; // mounted
else
send(g_sdcard_sockfd, (void*)packet, sizeof(char) * HEADER_SIZE, 0);
LOG("SDpath is %s", SDpath);
send(g_sdcard_sockfd, SDpath, packet->length, 0);
-
+
if(ret == 0)
{
- system("chmod -R 777 /opt/storage/sdcard");
- system("vconftool set -t int memory/sysman/mmc 1 -i -f");
+ system("/usr/bin/sys_event mmcblk_add");
}
}
-
break;
}
else
}
}
- pthread_exit((void *) 0);
-}
+ pthread_exit((void *) 0);
+}
int umount_sdcard(void)
{
- int ret = -1, i = 0;
+ int ret = -1, i = 0;
char file_name[128];
memset(file_name, '\0', sizeof(file_name));
LXT_MESSAGE* packet = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
+ if(packet == NULL){
+ return ret;
+ }
memset(packet, 0, sizeof(LXT_MESSAGE));
LOG("start sdcard umount");
if ( ret == 0 )
{
LOG( "%s is exist", file_name);
- ret = umount("/mnt/mmc");
- LOG("umount ret = %d, errno = %d", ret, errno);
-
LOG("sdcard fd: %d", g_sdcard_sockfd);
if(g_sdcard_sockfd != -1)
{
packet->length = strlen(SDpath); // length
packet->group = 11; // sdcard
- if(ret == 0)
- packet->action = 0; // unmounted
- else
- packet->action = 4; // failed
+ packet->action = 0; // unmounted
send(g_sdcard_sockfd, (void*)packet, sizeof(char) * HEADER_SIZE, 0);
LOG("SDpath is %s", SDpath);
send(g_sdcard_sockfd, SDpath, packet->length, 0);
- if(ret == 0)
{
memset(SDpath, '\0', sizeof(SDpath));
sprintf(SDpath, "umounted");
- system("vconftool set -t int memory/sysman/mmc 0 -i -f");
- }
+ system("/usr/bin/sys_event mmcblk_remove");
+ }
}
-
break;
}
else
}
}
+ if(packet){
+ free(packet);
+ packet = NULL;
+ }
return ret;
-}
+}
void epoll_init(void)
{
LOG("[START] epoll creation success");
/* event control set */
- events.events = EPOLLIN; // check In event
+ events.events = EPOLLIN; // check In event
events.data.fd = g_svr_sockfd;
/* server events set(read for accept) */
/* event control set for read event */
events.events = EPOLLIN;
- events.data.fd = cli_fd;
+ events.data.fd = cli_fd;
if( epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, cli_fd, &events) < 0 )
{
{
if(g_client[i].cli_sockfd == -1) break;
}
- if( i >= MAX_CLIENT ) close(cli_fd);
+ if( i >= MAX_CLIENT ){
+ close(cli_fd);
+ return;
+ }
LOG("g_client[%d].cli_port: %d", i, cli_port);
{
if(g_client[i].cli_sockfd == cli_fd)
{
- g_client[i].cli_sockfd = -1;
+ g_client[i].cli_sockfd = -1;
break;
}
}
return 0;
}
+#define STR_HELPER(x) #x
+#define STR(x) STR_HELPER(x)
+
void udp_init(void)
{
+ char emul_ip[HOST_NAME_MAX+1];
+ struct addrinfo *res;
+ struct addrinfo hints;
+ int rc;
+
LOG("start");
- char* emul_ip = getenv("HOSTNAME");
- if(emul_ip == NULL)
+
+ memset(emul_ip, 0, sizeof(emul_ip));
+ if (gethostname(emul_ip, sizeof(emul_ip)) < 0)
+ {
+ LOG("gethostname(): %s", strerror(errno));
+ assert(0);
+ }
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_family=AF_INET;
+ hints.ai_socktype=SOCK_DGRAM;
+ hints.ai_protocol=IPPROTO_UDP;
+
+ if ((rc=getaddrinfo(emul_ip, STR(SENSORD_PORT), &hints, &res)) != 0)
{
- LOG("emul_ip is null");
+ if (rc == EAI_SYSTEM)
+ LOG("getaddrinfo(sensord): %s", strerror(errno));
+ else
+ LOG("getaddrinfo(sensord): %s", gai_strerror(rc));
assert(0);
}
- if ((uSensordFd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
+ if ((uSensordFd=socket(res->ai_family, res->ai_socktype, res->ai_protocol))==-1){
fprintf(stderr, "socket error!\n");
}
+ if (res->ai_addrlen > sizeof(si_sensord_other))
+ {
+ LOG("sockaddr structure too big");
+ /* XXX: if you `return' remember to clean up */
+ assert(0);
+ }
memset((char *) &si_sensord_other, 0, sizeof(si_sensord_other));
- si_sensord_other.sin_family = AF_INET;
- si_sensord_other.sin_port = htons(sensord_port);
- if (inet_aton(emul_ip, &si_sensord_other.sin_addr)==0) {
- fprintf(stderr, "inet_aton() failed\n");
+ memcpy((char *) &si_sensord_other, res->ai_addr, res->ai_addrlen);
+ freeaddrinfo(res);
+
+ if ((rc=getaddrinfo(emul_ip, STR(GPSD_PORT), &hints, &res)) != 0)
+ {
+ if (rc == EAI_SYSTEM)
+ LOG("getaddrinfo(gpsd): %s", strerror(errno));
+ else
+ LOG("getaddrinfo(gpsd): %s", gai_strerror(rc));
+ assert(0);
}
- if ((uGpsdFd=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1){
+ if ((uGpsdFd=socket(res->ai_family, res->ai_socktype, res->ai_protocol))==-1){
fprintf(stderr, "socket error!\n");
}
- memset((char *) &si_gpsd_other, 0, sizeof(si_gpsd_other));
- si_gpsd_other.sin_family = AF_INET;
- si_gpsd_other.sin_port = htons(gpsd_port);
- if (inet_aton(emul_ip, &si_gpsd_other.sin_addr)==0) {
- fprintf(stderr, "inet_aton() failed\n");
+ if (res->ai_addrlen > sizeof(si_gpsd_other))
+ {
+ LOG("sockaddr structure too big");
+ assert(0);
}
+ memset((char *) &si_gpsd_other, 0, sizeof(si_gpsd_other));
+ memcpy((char *) &si_gpsd_other, res->ai_addr, res->ai_addrlen);
+ freeaddrinfo(res);
}
+#undef STR_HELPER
+#undef STR
+
int recv_data(int event_fd, char** r_databuf, int size)
{
int recvd_size = 0;
int len = 0;
int getcnt = 0;
char* r_tmpbuf = NULL;
+ const int alloc_size = sizeof(char) * size + 1;
+
+ r_tmpbuf = (char*)malloc(alloc_size);
+ if(r_tmpbuf == NULL)
+ {
+ return -1;
+ }
+
+ char* databuf = (char*)malloc(alloc_size);
+ if(databuf == NULL)
+ {
+ free(r_tmpbuf);
+ *r_databuf = NULL;
+ return -1;
+ }
- r_tmpbuf = (char*)malloc(sizeof(char) * size + 1);
- *r_databuf = (char*)malloc(sizeof(char) * size + 1);
- memset(*r_databuf, '\0', sizeof(char) * size + 1);
+ memset(databuf, '\0', alloc_size);
while(recvd_size < size)
{
- memset(r_tmpbuf, '\0', sizeof(char) * size);
+ memset(r_tmpbuf, '\0', alloc_size);
len = recv(event_fd, r_tmpbuf, size - recvd_size, 0);
- memcpy((*r_databuf) + recvd_size, r_tmpbuf, len);
+ if (len < 0) {
+ break;
+ }
+
+ memcpy(databuf + recvd_size, r_tmpbuf, len);
recvd_size += len;
getcnt++;
- if(getcnt > MAX_GETCNT)
+ if(getcnt > MAX_GETCNT) {
break;
+ }
}
free(r_tmpbuf);
r_tmpbuf = NULL;
+ *r_databuf = databuf;
+
return recvd_size;
}
{
struct timeval now;
int poweroff_duration = POWEROFF_DURATION;
- char *buf;
-
- /* Getting poweroff duration */
- buf = getenv("PWROFF_DUR");
- if(buf == NULL)
- {
- LOG("PWROFF_DUR is null");
- assert(0);
- }
-
- if (strlen(buf) < 1024)
- poweroff_duration = atoi(buf);
- if (poweroff_duration < 0 || poweroff_duration > 60)
- poweroff_duration = POWEROFF_DURATION;
gettimeofday(&now, NULL);
/* Waiting until power off duration and displaying animation */
LOG("wait");
usleep(100000);
gettimeofday(&now, NULL);
- }
+ }
LOG("Power off by force");
LOG("sync");
{
char* r_databuf = NULL;
char tmpbuf[48];
- int len, recvd_size, parse_len = 0;
+ int len = 0, recvd_size = 0, parse_len = 0;
+
+ if (event_fd == -1) {
+ LOG("invalid event fd");
+ return;
+ }
+
LXT_MESSAGE* packet = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
- memset(packet, 0, sizeof(LXT_MESSAGE));
+ if (!packet)
+ return;
- LOG("start");
- /* there need to be more precise code here */
+ memset(packet, 0, sizeof(*packet));
+
+ LOG("start (event fd: %d)", event_fd);
+ /* there need to be more precise code here */
/* for example , packet check(protocol needed) , real recv size check , etc. */
// vmodem to event injector
if(event_fd == g_vm_sockfd)
- {
+ {
recvd_size = recv_data(event_fd, &r_databuf, HEADER_SIZE);
-
- LOG("recv_len: %d, vmodem header recv buffer: %s", recvd_size, r_databuf);
-
+ LOG("receive size: %d", recvd_size);
if(recvd_size <= 0)
{
free(r_databuf);
packet = NULL;
userpool_delete(event_fd);
close(event_fd); /* epoll set fd also deleted automatically by this call as a spec */
+
+ set_vm_connect_status(0);
+
if(pthread_create(&tid[0], NULL, init_vm_connect, NULL) != 0)
LOG("pthread create fail!");
return;
}
- memcpy((void*)packet, (void*)r_databuf, sizeof(char) * HEADER_SIZE);
+ LOG("vmodem header recv buffer: %s", r_databuf);
+ memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
LOG("first packet of vmodem to event injector %s", r_databuf);
free(r_databuf);
r_databuf = NULL;
-
- if(g_sdbd_sockfd != -1)
- len = send(g_sdbd_sockfd, (void*)packet, sizeof(char) * HEADER_SIZE, 0);
- else
- return;
-
- LOG("send_len: %d, next packet length: %d", len, packet->length);
- if(packet->length <= 0)
- {
- free(packet);
- packet = NULL;
- return;
- }
-
if(g_sdbd_sockfd != -1)
- recvd_size = recv_data(event_fd, &r_databuf, packet->length);
- else
- return;
-
- LOG("recv_len: %d, vmodem data recv buffer: %s", recvd_size, r_databuf);
-
- if(recvd_size <= 0)
{
- free(r_databuf);
- r_databuf = NULL;
- free(packet);
- packet = NULL;
- return;
+ len = send(g_sdbd_sockfd, (void*)packet, sizeof(char) * HEADER_SIZE, 0);
+ LOG("send_len: %d, next packet length: %d", len, packet->length);
}
- len = send(g_sdbd_sockfd, r_databuf, packet->length, 0);
- LOG("send_len: %d", len);
+ if (packet->length > 0)
+ {
+ if (g_sdbd_sockfd != -1)
+ {
+ recvd_size = recv_data(event_fd, &r_databuf, packet->length);
+ LOG("recv_len: %d, vmodem data recv buffer: %s", recvd_size, r_databuf);
+ if (recvd_size > 0)
+ {
+ len = send(g_sdbd_sockfd, r_databuf, packet->length, 0);
+ LOG("send_len: %d", len);
+ }
+ }
+ else
+ {
+ // for packet clear
+ recvd_size = recv_data(event_fd, &r_databuf, packet->length);
+ }
+ }
}
else // event injector to vmodem, sensord or gpsd
{
/* read from socket */
// read identifier
recvd_size = recv_data(event_fd, &r_databuf, ID_SIZE);
-
- LOG("Something may be added in the data end, but it does not matter.");
- LOG("identifier: %s", r_databuf);
-
+ LOG("receive size: %d", recvd_size);
if( recvd_size <= 0 )
{
free(r_databuf);
r_databuf = NULL;
+ free(packet);
+ packet = NULL;
LOG("close event_fd: %d", event_fd);
userpool_delete(event_fd);
close(event_fd); /* epoll set fd also deleted automatically by this call as a spec */
return;
}
+ LOG("Something may be added in the data end, but it does not matter.");
+ LOG("identifier: %s", r_databuf);
+
memset(tmpbuf, '\0', sizeof(tmpbuf));
parse_len = parse_val(r_databuf, 0x0a, tmpbuf);
if(strncmp(tmpbuf, "telephony", 9) == 0)
{
g_sdbd_sockfd = event_fd;
- if(g_vm_connect_status != 1) // The connection is lost with vmodem
+ if(!is_vm_connected()) { // The connection is lost with vmodem
+ free(packet);
+ packet = NULL;
return;
+ }
- recvd_size = recv_data(event_fd, &r_databuf, HEADER_SIZE);
+ recvd_size = recv_data(event_fd, &r_databuf, HEADER_SIZE);
len = send(g_vm_sockfd, r_databuf, HEADER_SIZE, 0);
-
+
memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
LOG("next packet length: %d", packet->length);
r_databuf = NULL;
if(packet->length == 0)
- {
+ {
if(packet->action == 71) // that's strange packet from telephony initialize
{
packet->length = 4;
packet = NULL;
return;
}
- }
+ }
- if(g_vm_connect_status != 1) // The connection is lost with vmodem
+ if(!is_vm_connected()) // The connection is lost with vmodem
return;
recvd_size = recv_data(event_fd, &r_databuf, packet->length);
- LOG("Something may be added in the data end, but it does not matter.");
- LOG("telephony data recv buffer: %s", r_databuf);
-
if(recvd_size <= 0)
{
free(r_databuf);
- r_databuf = NULL;
+ r_databuf = NULL;
free(packet);
packet = NULL;
return;
- }
- else
+ } else {
+ LOG("Something may be added in the data end, but it does not matter.");
+ LOG("telephony data recv buffer: %s", r_databuf);
+
len = send(g_vm_sockfd, r_databuf, packet->length, 0);
+ }
}
else if(strncmp(tmpbuf, "sensor", 6) == 0)
{
recvd_size = recv_data(event_fd, &r_databuf, HEADER_SIZE);
- memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
+ memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
LOG("sensor packet_length: %d", packet->length);
LOG("Something may be added in the data end, but it does not matter.");
LOG("sensor data recv buffer: %s", r_databuf);
- if(sendto(uSensordFd, r_databuf, packet->length, 0, (struct sockaddr*)&si_sensord_other, sslen) == -1)
+ if (packet->group == STATUS) {
+ g_get_status_sockfd = event_fd;
+ ActionID = packet->action;
+ memset(GetBuffer, '\0', sizeof(GetBuffer));
+ strcpy(GetBuffer, r_databuf);
+ LOG("GetBuffer is %s", GetBuffer);
+
+ if(pthread_create(&tid[2], NULL, setting_device, NULL) != 0) {
+ LOG("pthread create fail!");
+ }
+ }
+ else if(sendto(uSensordFd, r_databuf, packet->length, 0, (struct sockaddr*)&si_sensord_other, sslen) == -1) {
LOG("sendto error!");
+ }
}
else if(strncmp(tmpbuf, "location", 8) == 0)
{
LOG("Something may be added in the data end, but it does not matter.");
LOG("location data recv buffer: %s", r_databuf);
- setting_location(r_databuf);
+ if (packet->group == STATUS) {
+ g_get_status_sockfd = event_fd;
+ ActionID = packet->action;
+ memset(GetBuffer, '\0', sizeof(GetBuffer));
+ strcpy(GetBuffer, r_databuf);
+ LOG("GetBuffer is %s", GetBuffer);
+
+ if(pthread_create(&tid[2], NULL, setting_device, NULL) != 0) {
+ LOG("pthread create fail!");
+ }
+ } else {
+ setting_location(r_databuf);
+ }
}
else if(strncmp(tmpbuf, "nfc", 3) == 0)
{
recvd_size = recv_data(event_fd, &r_databuf, HEADER_SIZE);
if( recvd_size <= 0 )
- {
- free(r_databuf);
- r_databuf = NULL;
- return;
- }
- /*
- LOG("nfc packet length r_databuf: %s", r_databuf);
-
- memset(tmpbuf, '\0', sizeof(tmpbuf));
- parse_len = parse_val(r_databuf, 0x0a, tmpbuf);
-
- int length = atoi(tmpbuf);
- */
-
- //byte to int
- int length = ((r_databuf[0] & 0xff) << 24 | (r_databuf[1] & 0xff) << 16
- | (r_databuf[2] & 0xff) << 8 | (r_databuf[3] & 0xff)) ;
- LOG("nfc packet converted length: %d", length);
- free(r_databuf);
- r_databuf = NULL;
-
- recvd_size = recv_data(event_fd, &r_databuf, length);
- /*
- char* strbuf = NULL;
- strbuf = (char*)malloc(length + 1);
- memset(strbuf, '\0', length + 1);
- memcpy(strbuf, r_databuf, length);
- */
- LOG("nfc data recv buffer: %s", r_databuf);
-
- FILE* fd;
- fd = fopen("/opt/nfc/sdkMsg", "w");
- if(!fd)
{
- LOG("nfc file open fail!");
+ free(r_databuf);
+ r_databuf = NULL;
free(packet);
packet = NULL;
return;
}
- fprintf(fd, "%s", r_databuf);
- fclose(fd);
- //free(strbuf);
- //strbuf = NULL;
+
+ memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
+ LOG("nfc packet_length: %d", packet->length);
+
+ free(r_databuf);
+ r_databuf = NULL;
+ recvd_size = recv_data(event_fd, &r_databuf, packet->length);
+ if(r_databuf == NULL){
+ free(packet);
+ packet = NULL;
+ return;
+ }
+ LOG("nfc data recv buffer: %s", r_databuf);
+
+ if (packet->group == STATUS) {
+ g_get_status_sockfd = event_fd;
+ ActionID = packet->action;
+ memset(GetBuffer, '\0', sizeof(GetBuffer));
+ strcpy(GetBuffer, r_databuf);
+ LOG("GetBuffer is %s", GetBuffer);
+
+ if(pthread_create(&tid[2], NULL, setting_device, NULL) != 0) {
+ LOG("pthread create fail!");
+ }
+ } else {
+ FILE* fd;
+ fd = fopen("/opt/nfc/sdkMsg", "w");
+ if(!fd)
+ {
+ LOG("nfc file open fail!");
+ free(r_databuf);
+ r_databuf = NULL;
+ free(packet);
+ packet = NULL;
+ return;
+ }
+ fprintf(fd, "%s", r_databuf);
+ fclose(fd);
+ //free(strbuf);
+ //strbuf = NULL;
+ }
}
else if(strncmp(tmpbuf, "system", 6) == 0)
{
-
recvd_size = recv_data(event_fd, &r_databuf, HEADER_SIZE);
memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
{
g_sdcard_sockfd = event_fd;
recvd_size = recv_data(event_fd, &r_databuf, HEADER_SIZE);
- memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
+ memcpy((void*)packet, (void*)r_databuf, HEADER_SIZE);
LOG("sdcard packet_length: %d", packet->length);
free(r_databuf);
r_databuf = NULL;
recvd_size = recv_data(event_fd, &r_databuf, packet->length);
+ if(recvd_size <= 0){
+ LOG("client_recv: recv_data err");
+ if(r_databuf) {
+ free(r_databuf);
+ r_databuf = NULL;
+ }
+ if(packet) {
+ free(packet);
+ packet = NULL;
+ }
+ LOG("close event_fd: %d", event_fd);
+ userpool_delete(event_fd);
+ close(event_fd); /* epoll set fd also deleted automatically by this call as a spec */
+ if(event_fd == g_sdbd_sockfd)
+ g_sdbd_sockfd = -1;
+ return;
+ }
LOG("Something may be added in the data end, but it does not matter.");
LOG("sdcard data recv buffer: %s", r_databuf);
-
+
char token[] = "\n";
char tmpdata[recvd_size];
memcpy(tmpdata, r_databuf, recvd_size);
{
case 0: // umount
mount_status = umount_sdcard();
- if(mount_status == 0)
+ if(mount_status == 0)
send_guest_server(r_databuf);
break;
case 1: // mount
mount_status = is_mounted();
LXT_MESSAGE* mntData = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
+ if(mntData == NULL){
+ break;
+ }
memset(mntData, 0, sizeof(LXT_MESSAGE));
mntData->length = strlen(SDpath); // length
case 0:
mntData->action = 2; // umounted status
send(g_sdcard_sockfd, (void*)mntData, sizeof(char) * HEADER_SIZE, 0);
-
+
LOG("SDpath is %s", SDpath);
send(g_sdcard_sockfd, SDpath, mntData->length, 0);
memset(SDpath, '\0', sizeof(SDpath));
default:
break;
}
+ free(mntData);
break;
default:
LOG("unknown data %s", ret);
break;
- }
- }
+ }
+ }
else
{
LOG("Unknown packet: %s", tmpbuf);
}
}
-void server_process(void)
+bool server_process(void)
{
struct sockaddr_in cli_addr;
int i,nfds;
int cli_sockfd;
int cli_len = sizeof(cli_addr);
- nfds = epoll_wait(g_epoll_fd,g_events,MAX_EVENTS,100); /* timeout 100ms */
+ nfds = epoll_wait(g_epoll_fd, g_events,MAX_EVENTS, 100); /* timeout 100ms */
if(nfds == 0){
/* no event , no work */
- return;
+ return false;
}
if(nfds < 0) {
- fprintf(stderr, "epoll wait error\n");
+ fprintf(stderr, "epoll wait %d\n", errno);
/* return but this is epoll wait error */
- return;
- }
+ if (errno == EINTR)
+ return false;
+ return true;
+ }
for( i = 0 ; i < nfds ; i++ )
{
if(g_events[i].data.fd == g_svr_sockfd)
{
cli_sockfd = accept(g_svr_sockfd, (struct sockaddr *)&cli_addr,(socklen_t *)&cli_len);
- if(cli_sockfd < 0)
+ if(cli_sockfd < 0)
{
/* accept error */
fprintf(stderr, "accept error\n");
}
/* if not server socket , this socket is for client socket, so we read it */
- client_recv(g_events[i].data.fd);
+ client_recv(g_events[i].data.fd);
} /* end of for 0-nfds */
+
+ return false;
}
/*------------------------------- end of function server_process */
exit(0);
}
+void setting_device()
+{
+ pthread_detach(pthread_self());
+
+ char* msg = 0;
+ LXT_MESSAGE* packet = (LXT_MESSAGE*)malloc(sizeof(LXT_MESSAGE));
+
+ switch(ActionID)
+ {
+ case BATTERY_LEVEL:
+ msg = get_battery_level((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting battery level");
+ }
+ break;
+ case BATTERY_CHARGER:
+ msg = get_battery_charger((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting battery charger state");
+ }
+ break;
+ case USB_STATUS:
+ msg = get_usb_status((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting usb status");
+ }
+ break;
+ case EARJACK_STATUS:
+ msg = get_earjack_status((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting earjack status");
+ }
+ break;
+ case RSSI_LEVEL:
+ msg = get_rssi_level((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting rssi level");
+ }
+ break;
+ case ACCEL_VALUE:
+ msg = get_acceleration_value((void*)packet);
+ if (msg == 0) {
+ LOG("falied getting acceleration value");
+ }
+ break;
+ case GYRO_VALUE:
+ msg = get_gyroscope_value((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting gyroscope value");
+ }
+ break;
+ case MAG_VALUE:
+ msg = get_magnetic_value((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting magnetic value");
+ }
+ break;
+ case LIGHT_VALUE:
+ msg = get_light_level((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting light level");
+ }
+ break;
+ case PROXI_VALUE:
+ msg = get_proximity_status((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting proximity status");
+ }
+ break;
+ case MOTION_VALUE:
+ LOG("not support getting motion value");
+ break;
+ case LOCATION_STATUS:
+ msg = get_location_status((void*)packet);
+ if (msg == 0) {
+ LOG("failed getting location status");
+ }
+ break;
+ case NFC_STATUS:
+ msg = get_nfc_status((void*)packet);
+ if (msg ==0) {
+ LOG("failed getting nfc status");
+ }
+ break;
+ default:
+ //TODO
+ break;
+ }
+
+ if (g_get_status_sockfd != -1) {
+ if (msg != 0) {
+ LOG("send data to injector");
+ } else {
+ LOG("send error message to injector");
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 0;
+ packet->group = STATUS;
+ packet->action = ActionID;
+ }
+ send(g_get_status_sockfd, (void*)packet, sizeof(char) * HEADER_SIZE, 0);
+ if (packet->length != 0) {
+ send(g_get_status_sockfd, msg, packet->length, 0);
+ }
+ }
+
+ if(msg != 0) {
+ free(msg);
+ }
+ free(packet);
+
+ pthread_exit((void *) 0);
+}
+
// location event
char command[512];
char latitude[128];
//sdcard event
void send_guest_server(char* databuf)
{
+ if (!databuf)
+ {
+ LOG("invalid data buf");
+ return;
+ }
+
char buf[32];
struct sockaddr_in si_other;
int s, slen=sizeof(si_other);
- FILE* fd;
+ FILE* fd;
char fbuf[16];
int port;
fd = fopen("/opt/home/sdb_port.txt", "r");
}
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1)
- LOG("socket error!");
-
+ {
+ LOG("socket error!");
+ return;
+ }
+
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(port);
}
memset(buf, '\0', sizeof(buf));
- sprintf(buf, "4\n%s", databuf);
+ snprintf(buf, sizeof(buf), "4\n%s", databuf);
+
LOG("sendGuestServer msg: %s", buf);
if(sendto(s, buf, sizeof(buf), 0, (struct sockaddr*)&si_other, slen) == -1)
- {
+ {
LOG("sendto error!");
}
-
+
close(s);
}
int main( int argc , char *argv[])
{
+ int state;
+
if(log_print == 1)
{
// for emuld log file
LOG("start");
/* entry , argument check and process */
- if(argc < 3){
+ if(argc < 3){
g_svr_port = DEFAULT_PORT;
- }else {
+ } else {
if(strcmp("-port",argv[1]) == 0 ) {
if(g_svr_port < 1024) {
fprintf(stderr, "[STOP] port number invalid : %d\n",g_svr_port);
exit(0);
- }
+ }
}
}
- init_data0();
+ init_data0();
/* init server */
init_server0(g_svr_port);
epoll_init(); /* epoll initialize */
+
+ set_vm_connect_status(0);
+
if(pthread_create(&tid[0], NULL, init_vm_connect, NULL) != 0)
LOG("pthread create fail!");
udp_init();
+ bool is_exit = false;
/* main loop */
- while(1)
+ while(!is_exit)
{
- server_process(); /* accept process. */
+ is_exit = server_process(); /* accept process. */
} /* infinite loop while end. */
+
+ state = pthread_mutex_destroy(&mutex_vmconnect);
+
+ fprintf(stderr, "emuld exit\n");
}
--- /dev/null
+/*
+ * emulator-daemon
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * SooYoung Ha <yoosah.ha@samsnung.com>
+ * Sungmin Ha <sungmin82.ha@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+//#include <vconf.h>
+#include "emuld_common.h"
+
+
+char* message;
+char* get_usb_status(void* p)
+{
+ FILE* fd = fopen("/sys/devices/platform/jack/usb_online", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ int status = 0;
+ fscanf(fd, "%d", &status);
+ fclose(fd);
+
+ // int to byte
+ message = (char*)malloc(5);
+ message[3] = (char) (status & 0xff);
+ message[2] = (char) (status >> 8 & 0xff);
+ message[1] = (char) (status >> 16 & 0xff);
+ message[0] = (char) (status >> 24 & 0xff);
+ message[4] = '\0';
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 4;
+ packet->group = STATUS;
+ packet->action = USB_STATUS;
+
+ return message;
+}
+
+char* get_earjack_status(void* p)
+{
+ FILE* fd = fopen("/sys/devices/platform/jack/earjack_online", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ int status = 0;
+ fscanf(fd, "%d", &status);
+ fclose(fd);
+
+ // int to byte
+ message = (char*)malloc(5);
+ message[3] = (char) (status & 0xff);
+ message[2] = (char) (status >> 8 & 0xff);
+ message[1] = (char) (status >> 16 & 0xff);
+ message[0] = (char) (status >> 24 & 0xff);
+ message[4] = '\0';
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 4;
+ packet->group = STATUS;
+ packet->action = EARJACK_STATUS;
+
+ return message;
+}
+
+char* get_rssi_level(void* p)
+{
+ int level;
+ int ret = vconf_get_int("memory/telephony/rssi", &level);
+ if (ret != 0) {
+ return 0;
+ }
+
+ // int to byte
+ message = (char*)malloc(5);
+ message[3] = (char) (level & 0xff);
+ message[2] = (char) (level >> 8 & 0xff);
+ message[1] = (char) (level >> 16 & 0xff);
+ message[0] = (char) (level >> 24 & 0xff);
+ message[4] = '\0';
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 4;
+ packet->group = STATUS;
+ packet->action = RSSI_LEVEL;
+
+ return message;
+}
+
+char* get_battery_level(void* p)
+{
+ FILE* fd = fopen("/sys/class/power_supply/battery/capacity", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ int level = 0;
+ fscanf(fd, "%d", &level);
+ fclose(fd);
+
+ // int to byte
+ message = (char*)malloc(5);
+ message[3] = (char) (level & 0xff);
+ message[2] = (char) (level >> 8 & 0xff);
+ message[1] = (char) (level >> 16 & 0xff);
+ message[0] = (char) (level >> 24 & 0xff);
+ message[4] = '\0';
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 4;
+ packet->group = STATUS;
+ packet->action = BATTERY_LEVEL;
+
+ return message;
+}
+
+char* get_battery_charger(void* p)
+{
+ FILE* fd = fopen("/sys/class/power_supply/battery/charge_now", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ int charge = 0;
+ fscanf(fd, "%d", &charge);
+ fclose(fd);
+ // int to byte
+ message = (char*)malloc(5);
+
+ message[3] = (char) (charge & 0xff);
+ message[2] = (char) (charge >> 8 & 0xff);
+ message[1] = (char) (charge >> 16 & 0xff);
+ message[0] = (char) (charge >> 24 & 0xff);
+ message[4] = '\0';
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 4;
+ packet->group = STATUS;
+ packet->action = BATTERY_CHARGER;
+
+ return message;
+}
+
+char* get_proximity_status(void* p)
+{
+ FILE* fd = fopen("/opt/sensor/proxi/vo", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ int status = 0;
+ fscanf(fd, "%d", &status);
+ fclose(fd);
+ // int to byte
+ message = (char*)malloc(5);
+
+ message[3] = (char) (status & 0xff);
+ message[2] = (char) (status >> 8 & 0xff);
+ message[1] = (char) (status >> 16 & 0xff);
+ message[0] = (char) (status >> 24 & 0xff);
+ message[4] = '\0';
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 4;
+ packet->group = STATUS;
+ packet->action = PROXI_VALUE;
+
+ return message;
+}
+
+char* get_light_level(void* p)
+{
+ FILE* fd = fopen("/opt/sensor/light/adc", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ int level = 0;
+ fscanf(fd, "%d", &level);
+ fclose(fd);
+
+ // int to byte
+ message = (char*)malloc(5);
+ message[3] = (char) (level & 0xff);
+ message[2] = (char) (level >> 8 & 0xff);
+ message[1] = (char) (level >> 16 & 0xff);
+ message[0] = (char) (level >> 24 & 0xff);
+ message[4] = '\0';
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = 4;
+ packet->group = STATUS;
+ packet->action = LIGHT_VALUE;
+
+ return message;
+}
+
+char* get_acceleration_value(void* p)
+{
+ FILE* fd = fopen("/opt/sensor/accel/xyz", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ message = (char*)malloc(128);
+ //fscanf(fd, "%d, %d, %d", message);
+ fgets(message, 128, fd);
+ fclose(fd);
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = strlen(message);
+ packet->group = STATUS;
+ packet->action = ACCEL_VALUE;
+
+ return message;
+}
+
+char* get_gyroscope_value(void* p)
+{
+ int x;
+ int y;
+ int z;
+ FILE* fd = fopen("/opt/sensor/gyro/gyro_x_raw", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+ fscanf(fd, "%d", &x);
+ fclose(fd);
+
+ fd = fopen("/opt/sensor/gyro/gyro_y_raw", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+ fscanf(fd, "%d", &y);
+ fclose(fd);
+
+ fd = fopen("/opt/sensor/gyro/gyro_z_raw", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+ fscanf(fd, "%d", &z);
+ fclose(fd);
+
+ message = (char*)malloc(128);
+ memset(message, 0, 128);
+ int ret = sprintf(message, "%d, %d, %d", x, y, z);
+ if (ret < 0) {
+ free(message);
+ return 0;
+ }
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = strlen(message);
+ packet->group = STATUS;
+ packet->action = GYRO_VALUE;
+
+ return message;
+}
+
+char* get_magnetic_value(void* p)
+{
+ FILE* fd = fopen("/opt/sensor/geo/tesla", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ message = (char*)malloc(128);
+ fgets(message, 128, fd);
+ fclose(fd);
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = strlen(message);
+ packet->group = STATUS;
+ packet->action = MAG_VALUE;
+ return message;
+}
+
+char* get_location_status(void* p)
+{
+ int mode;
+ int ret = vconf_get_int("db/location/replay/ReplayMode", &mode);
+ if (ret != 0) {
+ return 0;
+ }
+
+ if (mode == 0) { // STOP
+ message = (char*)malloc(5);
+ ret = sprintf(message, "%d", mode);
+ if (ret < 0) {
+ free(message);
+ return 0;
+ }
+ } else if (mode == 1) { // NMEA MODE(LOG MODE)
+ //char* temp = (char*)malloc(128);
+ char* temp = 0;
+ temp = vconf_get_str("db/location/replay/FileName");
+ if (temp == 0) {
+ //free(temp);
+ return 0;
+ }
+
+ message = (char*)malloc(256);
+ ret = sprintf(message, "%d,%s", mode, temp);
+ if (ret < 0) {
+ free(message);
+ return 0;
+ }
+ } else if (mode == 2) { // MANUAL MODE
+ double latitude;
+ double logitude;
+ ret = vconf_get_dbl("db/location/replay/ManualLatitude", &latitude);
+ if (ret != 0) {
+ return 0;
+ }
+ ret = vconf_get_dbl("db/location/replay/ManualLongitude", &logitude);
+ if (ret != 0) {
+ return 0;
+ }
+ message = (char*)malloc(128);
+ ret = sprintf(message, "%d,%f,%f", mode, latitude, logitude);
+ if (ret < 0) {
+ free(message);
+ return 0;
+ }
+ }
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = strlen(message);
+ packet->group = STATUS;
+ packet->action = LOCATION_STATUS;
+
+ return message;
+}
+
+char* get_nfc_status(void* p)
+{
+ FILE* fd = fopen("/opt/nfc/sdkMsg", "r");
+ if(!fd)
+ {
+ return 0;
+ }
+
+ message = (char*)malloc(5000);
+ fscanf(fd, "%s\n", message);
+ fclose(fd);
+
+ LXT_MESSAGE* packet = p;
+ memset(packet, 0, sizeof(LXT_MESSAGE));
+ packet->length = strlen(message);
+ packet->group = STATUS;
+ packet->action = NFC_STATUS;
+
+ return message;
+}