4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
8 * Jaewon Lim <jaewon81.lim@samsung.com>
9 * Woojin Jung <woojin2.jung@samsung.com>
10 * Juyoung Kim <j0.kim@samsung.com>
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * http://www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
30 #include <stdlib.h> // for realpath
31 #include <string.h> // for strtok, strcpy, strncpy
32 #include <limits.h> // for realpath
34 #include <errno.h> // for errno
35 #include <sys/types.h> // for accept, mkdir, opendir, readdir
36 #include <sys/socket.h> // for accept
37 #include <sys/stat.h> // for mkdir
38 #include <sys/eventfd.h> // for eventfd
39 #include <sys/epoll.h> // for epoll apis
40 #include <sys/timerfd.h> // for timerfd
41 #include <unistd.h> // for access, sleep
44 #include <attr/xattr.h> // for fsetxattr
45 #include <sys/smack.h>
48 #include <linux/input.h>
54 #include "da_protocol.h"
55 #define DA_WORK_DIR "/home/developer/sdk_tools/da/"
56 #define DA_READELF_PATH "/home/developer/sdk_tools/da/readelf"
57 #define SCREENSHOT_DIR "/tmp/da"
60 #define MAX_CONNECT_SIZE 12
61 #define MAX_APP_LAUNCH_TIME 6
63 #define INPUT_ID_TOUCH 0
64 #define INPUT_ID_KEY 1
65 #define STR_TOUCH "TOUCH"
67 #define INPUT_ID_STR_KEY "ID_INPUT_KEY=1"
68 #define INPUT_ID_STR_TOUCH "ID_INPUT_TOUCHSCREEN=1"
69 #define INPUT_ID_STR_KEYBOARD "ID_INPUT_KEYBOARD=1"
70 #define INPUT_ID_STR_TABLET "ID_INPUT_TABLET=1"
73 #define MAX_FILENAME 128
75 #define ARRAY_END (-11)
77 typedef struct _input_dev
80 char fileName[MAX_FILENAME];
83 input_dev g_key_dev[MAX_DEVICE];
84 input_dev g_touch_dev[MAX_DEVICE];
86 // return bytes size of readed data
87 // return 0 if no data readed or error occurred
88 static int _file_read(FILE* fp, char *buffer, int size)
92 if(fp != NULL && size > 0)
94 ret = fread((void*)buffer, sizeof(char), size, fp);
102 ret = 0; // error case
108 // get input id of given input device
109 static int get_input_id(char* inputname)
111 static int query_cmd_type = 0; // 1 if /lib/udev/input_id, 2 if udevadm
113 char buffer[BUF_SIZE];
114 char command[MAX_FILENAME];
117 // determine input_id query command
118 if(unlikely(query_cmd_type == 0))
120 if(access("/lib/udev/input_id", F_OK) == 0) // there is /lib/udev/input_id
124 else // there is not /lib/udev/input_id
130 // make command string
131 if(query_cmd_type == 1)
133 sprintf(command, "/lib/udev/input_id /class/input/%s", inputname);
137 sprintf(command, "udevadm info --name=input/%s --query=property", inputname);
141 cmd_fp = popen(command, "r");
142 _file_read(cmd_fp, buffer, BUF_SIZE);
144 // determine input id
145 if(strstr(buffer, INPUT_ID_STR_KEY)) // key
149 else if(strstr(buffer, INPUT_ID_STR_TOUCH)) // touch
151 ret = INPUT_ID_TOUCH;
153 else if(strstr(buffer, INPUT_ID_STR_KEYBOARD)) // keyboard
157 else if(strstr(buffer, INPUT_ID_STR_TABLET)) // touch (emulator)
159 ret = INPUT_ID_TOUCH;
167 // get filename and fd of given input type devices
168 static void _get_fds(input_dev *dev, int input_id)
174 dp = opendir("/sys/class/input");
178 while((d = readdir(dp)) != NULL)
180 if(!strncmp(d->d_name, "event", 5)) // start with "event"
183 if(input_id == get_input_id(d->d_name))
185 sprintf(dev[count].fileName, "/dev/input/%s", d->d_name);
186 dev[count].fd = open(dev[count].fileName, O_RDWR | O_NONBLOCK);
194 dev[count].fd = ARRAY_END; // end of input_dev array
197 static void _device_write(input_dev *dev, struct input_event* in_ev)
200 for(i = 0; dev[i].fd != ARRAY_END; i++)
203 write(dev[i].fd, in_ev, sizeof(struct input_event));
207 long long get_total_alloc_size()
210 long long allocsize = 0;
212 for(i = 0; i < MAX_TARGET_COUNT; i++)
214 if(manager.target[i].socket != -1 && manager.target[i].allocmem > 0)
215 allocsize += manager.target[i].allocmem;
220 static int getEmptyTargetSlot()
223 for(i = 0; i < MAX_TARGET_COUNT; i++)
225 if(manager.target[i].socket == -1)
232 static void setEmptyTargetSlot(int index)
234 if(index >= 0 && index < MAX_TARGET_COUNT)
236 manager.target[index].pid = -1;
237 manager.target[index].recv_thread = -1;
238 manager.target[index].allocmem = 0;
239 manager.target[index].starttime = 0;
240 manager.target[index].initial_log = 0;
241 if(manager.target[index].event_fd != -1)
242 close(manager.target[index].event_fd);
243 manager.target[index].event_fd = -1;
244 if(manager.target[index].socket != -1)
245 close(manager.target[index].socket);
246 manager.target[index].socket = -1;
250 // ======================================================================================
251 // send functions to host
252 // ======================================================================================
254 int pseudoSendDataToHost(struct msg_data_t* log)
256 /* uint32_t total_len = MSG_DATA_HDR_LEN; */
258 /* char *buf = malloc(total_len+log->len); */
260 /* memset(p,0,total_len); */
262 /* pack_int(p,log->id); */
263 /* pack_int(p,log->seq_num); */
264 /* pack_time(p,log->time); */
265 /* pack_int(p,log->len); */
267 /* memcpy(p,log->payload,log->len); */
268 /* printBuf(buf,total_len+log->len); */
269 /* if (event_fd >0){ */
270 /* write(event_fd, buf, total_len+log->len); */
277 int sendDataToHost(msg_t* log)
279 if (manager.host.data_socket != -1)
281 char logstr[DA_MSG_MAX];
285 loglen = sprintf(logstr, "%d|%d|%s\n", log->type, log->length + 1, log->data);
287 loglen = sprintf(logstr, "%d|%d|\n", log->type, log->length + 1);
289 // loglen = sprintf(logstr, "%d|%s\n", log->type, log->data);
291 pthread_mutex_lock(&(manager.host.data_socket_mutex));
292 send(manager.host.data_socket, logstr, loglen, MSG_NOSIGNAL);
293 pthread_mutex_unlock(&(manager.host.data_socket_mutex));
300 // msgstr can be NULL
301 static int sendACKStrToHost(enum HostMessageType resp, char* msgstr)
303 if (manager.host.control_socket != -1)
305 char logstr[DA_MSG_MAX];
309 loglen = sprintf(logstr, "%d|%d|%s", (int)resp, strlen(msgstr), msgstr);
311 loglen = sprintf(logstr, "%d|0|", (int)resp);
313 send(manager.host.control_socket, logstr, loglen, MSG_NOSIGNAL);
322 static int sendACKCodeToHost(enum HostMessageType resp, int msgcode)
325 //disabled string protocol
327 if (manager.host.control_socket != -1)
330 char logstr[DA_MSG_MAX];
333 codelen = sprintf(codestr, "%d", msgcode);
334 loglen = sprintf(logstr, "%d|%d|%s", (int)resp, codelen, codestr);
336 send(manager.host.control_socket, logstr, loglen, MSG_NOSIGNAL);
343 // ========================================================================================
344 // start and terminate control functions
345 // ========================================================================================
347 static int startProfiling(long launchflag)
349 char execPath[PATH_MAX];
351 // remove previous screen capture files
352 remove_indir(SCREENSHOT_DIR);
353 mkdir(SCREENSHOT_DIR, 0777);
355 smack_lsetlabel(SCREENSHOT_DIR, "*", SMACK_LABEL_ACCESS);
357 manager.config_flag = launchflag;
359 #ifdef RUN_APP_LOADER
360 strcpy(execPath, manager.appPath);
362 get_executable(manager.appPath, execPath, PATH_MAX);
364 if(samplingStart() < 0)
367 if(exec_app(execPath, get_app_type(manager.appPath)) == 0)
373 LOGI("Timer Started\n");
378 // terminate single target
379 // just send stop message to target process
380 static void terminate_target(int index)
384 sendlog.type = MSG_STOP;
387 if(manager.target[index].socket != -1)
389 // result of sending to disconnected socket is not expected
390 sendlen = send(manager.target[index].socket, &sendlog, sizeof(sendlog.type) + sizeof(sendlog.length), MSG_NOSIGNAL);
393 LOGI("TERMINATE send exit msg (socket %d) by terminate_target()\n", manager.target[index].socket);
398 // just send stop message to all target process
399 static void terminate_all_target()
405 sendlog.type = MSG_STOP;
408 for (i = 0; i < MAX_TARGET_COUNT; i++)
410 if(manager.target[i].socket != -1)
412 sendlen = send(manager.target[i].socket, &sendlog, sizeof(sendlog.type) + sizeof(sendlog.length), MSG_NOSIGNAL);
415 LOGI("TERMINATE send exit msg (socket %d) by terminate_all_target()\n", manager.target[i].socket);
421 // terminate all target and wait for threads
422 static void terminate_all()
425 terminate_all_target();
428 // wait for all other thread exit
429 for(i = 0; i < MAX_TARGET_COUNT; i++)
431 if(manager.target[i].recv_thread != -1)
433 pthread_join(manager.target[i].recv_thread, NULL);
438 // terminate all profiling by critical error
439 static void terminate_error(char* errstr, int sendtohost)
443 LOGE("TERMINATE ERROR: %s\n", errstr);
446 log.type = MSG_ERROR;
447 log.length = sprintf(log.data, "%s", errstr);
448 sendDataToHost(&log);
454 // ===========================================================================================
455 // message parsing and handling functions
456 // ===========================================================================================
458 static int parseDeviceMessage(msg_t* log)
460 char eventType[MAX_FILENAME];
461 struct input_event in_ev;
473 for(i = 0; i < log->length; i++)
475 if(log->data[i] == '\n')
478 if(log->data[i] == '`') // meet separate
485 if(index == 0) // parse eventType
487 eventType[i] = log->data[i];
488 eventType[i+1] = '\0';
490 else if(index == 1) // parse in_ev.type
492 in_ev.type = in_ev.type * 10 + (log->data[i] - '0');
494 else if(index == 2) // parse in_ev.code
496 in_ev.code = in_ev.code * 10 + (log->data[i] - '0');
498 else if(index == 3) // parse in_ev.value
500 in_ev.value = in_ev.value * 10 + (log->data[i] - '0');
505 return -1; // parse error
507 if(0 == strncmp(eventType, STR_TOUCH, strlen(STR_TOUCH)))
509 _device_write(g_touch_dev, &in_ev);
511 else if(0 == strncmp(eventType, STR_KEY, strlen(STR_KEY)))
513 _device_write(g_key_dev, &in_ev);
519 // return 0 if normal case
520 // return plus value if non critical error occur
521 // return minus value if critical error occur
522 static int _hostMessageHandler(int efd,struct msg_t* log)
527 /* char *barloc, *tmploc; */
528 /* char execPath[PATH_MAX]; */
530 /* if (log == NULL) */
533 /* switch (log->type) */
535 /* case MSG_REPLAY: */
536 /* sendACKStrToHost(MSG_OK, NULL); */
537 /* parseDeviceMessage(log); */
539 /* case MSG_VERSION: */
540 /* if(strcmp(PROTOCOL_VERSION, log->data) != 0) */
542 /* sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_PROTOCOL_VERSION); */
546 /* sendACKStrToHost(MSG_OK, NULL); */
549 /* case MSG_START: */
550 /* LOGI("MSG_START handling : %s\n", log->data); */
551 /* if(log->length == 0) */
553 /* sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_MESSAGE_DATA); */
554 /* return -1; // wrong message format */
557 /* // parsing for host start status */
558 /* tmploc = log->data; */
559 /* barloc = strchr(tmploc, '|'); */
560 /* if(barloc == NULL) */
562 /* sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_MESSAGE_FORMAT); */
563 /* return -1; // wrong message format */
566 /* // parsing for target launch option flag */
567 /* tmploc = barloc + 1; */
568 /* barloc = strchr(tmploc, '|'); */
569 /* if(barloc != NULL) */
571 /* while(tmploc < barloc) */
573 /* flag = (flag * 10) + (*tmploc - '0'); */
579 /* sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_MESSAGE_FORMAT); */
580 /* return -1; // wrong message format */
582 /* LOGI("launch flag : %lx\n", flag); */
584 /* // parsing for application package name */
585 /* tmploc = barloc + 1; */
586 /* strcpy(manager.appPath, tmploc); */
588 /* get_executable(manager.appPath, execPath, PATH_MAX); // get exact app executable file name */
589 /* LOGI("executable app path %s\n", manager.appPath); */
591 /* #ifdef RUN_APP_LOADER */
592 /* kill_app(manager.appPath); */
594 /* kill_app(execPath); */
598 /* char command[PATH_MAX]; */
599 /* struct epoll_event ev; */
601 /* //save app install path */
602 /* mkdir(DA_WORK_DIR, 0775); */
603 /* sprintf(command, */
604 /* "%s -Wwi %s | grep DW_AT_comp_dir > %s", DA_READELF_PATH, */
605 /* execPath, DA_INSTALL_PATH); */
606 /* LOGI("appInstallCommand %s\n", command); */
607 /* system(command); */
609 /* sprintf(command, */
610 /* "%s -h %s | grep Type | cut -d\" \" -f33 > %s", DA_READELF_PATH, */
611 /* execPath, DA_BUILD_OPTION); */
612 /* LOGI("appInstallCommand %s\n", command); */
613 /* system(command); */
615 /* if(startProfiling(flag) < 0) */
617 /* sendACKCodeToHost(MSG_NOTOK, ERR_CANNOT_START_PROFILING); */
621 /* manager.app_launch_timerfd = timerfd_create(CLOCK_REALTIME, TFD_CLOEXEC); */
622 /* if(manager.app_launch_timerfd > 0) */
624 /* struct itimerspec ctime; */
625 /* ctime.it_value.tv_sec = MAX_APP_LAUNCH_TIME; */
626 /* ctime.it_value.tv_nsec = 0; */
627 /* ctime.it_interval.tv_sec = 0; */
628 /* ctime.it_interval.tv_nsec = 0; */
629 /* if(0 > timerfd_settime(manager.app_launch_timerfd, 0, &ctime, NULL)) */
631 /* LOGE("fail to set app launch timer\n"); */
632 /* close(manager.app_launch_timerfd); */
633 /* manager.app_launch_timerfd = -1; */
637 /* // add event fd to epoll list */
638 /* ev.events = EPOLLIN; */
639 /* ev.data.fd = manager.app_launch_timerfd; */
640 /* if(epoll_ctl(efd, EPOLL_CTL_ADD, manager.app_launch_timerfd, &ev) < 0) */
642 /* // fail to add event fd */
643 /* LOGE("fail to add app launch timer fd to epoll list\n"); */
644 /* close(manager.app_launch_timerfd); */
645 /* manager.app_launch_timerfd = -1; */
650 /* sendACKStrToHost(MSG_OK, NULL); */
653 /* LOGI("MSG_STOP handling\n"); */
654 /* sendACKStrToHost(MSG_OK, NULL); */
655 /* terminate_all(); */
657 /* case MSG_OPTION: */
658 /* if(log->length > 0) */
662 /* manager.config_flag = atoi(log->data); */
663 /* sendACKStrToHost(MSG_OK, NULL); */
665 /* LOGI("MSG_OPTION : str(%s), flag(%x)\n", log->data, manager.config_flag); */
667 /* sendlog.type = MSG_OPTION; */
668 /* sendlog.length = sprintf(sendlog.data, "%u", manager.config_flag); */
670 /* for(i = 0; i < MAX_TARGET_COUNT; i++) */
672 /* if(manager.target[i].socket != -1) */
674 /* send(manager.target[i].socket, &sendlog, sizeof(sendlog.type) + sizeof(sendlog.length) + sendlog.length, MSG_NOSIGNAL); */
680 /* sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_MESSAGE_DATA); */
684 /* case MSG_ISALIVE: */
685 /* sendACKStrToHost(MSG_OK, NULL); */
688 /* LOGW("Unknown msg\n"); */
689 /* sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_MESSAGE_TYPE); */
696 // ========================================================================================
697 // socket and event_fd handling functions
698 // ========================================================================================
700 // return 0 if normal case
701 // return plus value if non critical error occur
702 // return minus value if critical error occur
703 static int deviceEventHandler(input_dev* dev, int input_type)
706 struct input_event in_ev;
709 if(input_type == INPUT_ID_TOUCH)
712 read(dev->fd, &in_ev, sizeof(struct input_event));
713 log.type = MSG_RECORD;
714 log.length = sprintf(log.data, "%s`,%s`,%ld`,%ld`,%hu`,%hu`,%u",
715 STR_TOUCH, dev->fileName, in_ev.time.tv_sec,
716 in_ev.time.tv_usec, in_ev.type, in_ev.code, in_ev.value);
717 sendDataToHost(&log);
719 else if(input_type == INPUT_ID_KEY)
722 read(dev->fd, &in_ev, sizeof(struct input_event));
723 log.type = MSG_RECORD;
724 log.length = sprintf(log.data, "%s`,%s`,%ld`,%ld`,%hu`,%hu`,%u",
725 STR_KEY, dev->fileName, in_ev.time.tv_sec,
726 in_ev.time.tv_usec, in_ev.type, in_ev.code, in_ev.value);
727 sendDataToHost(&log);
731 LOGW("unknown input_type\n");
738 #define MAX_EVENTS_NUM 10
739 static int deviceEventHandlerNew(input_dev* dev, int input_type)
744 struct input_event in_ev[MAX_EVENTS_NUM];
745 struct msg_data_t log;
747 if(input_type == INPUT_ID_TOUCH || input_type == INPUT_ID_KEY)
750 // LOGI(">read %s events\n,", input_type==INPUT_ID_KEY?STR_KEY:STR_TOUCH);
751 size = read(dev->fd, &in_ev[count], sizeof(*in_ev) );
752 // LOGI("<read %s events : size = %d\n,", input_type==INPUT_ID_KEY?STR_KEY:STR_TOUCH,size);
755 } while (count < MAX_EVENTS_NUM && size > 0);
758 LOGI("readed %d %s events\n,", count, input_type==INPUT_ID_KEY?STR_KEY:STR_TOUCH);
759 /* gen_message_event(&log,&in_ev[0],count,input_type); */
760 pseudoSendDataToHost(&log);
761 reset_data_msg(&log);
766 LOGW("unknown input_type\n");
770 if(input_type == INPUT_ID_TOUCH || input_type == INPUT_ID_KEY)
773 if (size = read(dev->fd, &in_ev, sizeof(in_ev) ) !=0 )
775 LOGI("readed %d touch events\n,", 1);
776 gen_message_event(&log,&in_ev,1,input_type);
777 pseudoSendDataToHost(&log);
782 LOGW("unknown input_type\n");
789 // return 0 if normal case
790 // return plus value if non critical error occur
791 // return minus value if critical error occur
792 // return -11 if all target process closed
793 static int targetEventHandler(int epollfd, int index, uint64_t msg)
799 if(index == 0) // assume index 0 is main application process
802 char tempBuff[DA_MSG_MAX];
803 char tempBuff2[DA_MSG_MAX];
804 char tempPath[PATH_MAX];
806 get_executable(manager.appPath, tempPath, PATH_MAX);
807 if(realpath(tempPath, tempBuff) == NULL)
809 LOGW("Failed to get realpath of app\n");
810 strcpy(tempBuff, tempPath);
813 sprintf(tempPath, "/proc/%d/maps", manager.target[index].pid);
814 sprintf(tempBuff2, "cat %s | grep %s | cut -d\"-\" -f1 > %s",
815 tempPath, tempBuff, DA_BASE_ADDRESS);
816 LOGI("base address command is %s\n", tempBuff2);
819 if(access(tempPath, F_OK) != 0)
821 if(is_same_app_process(manager.appPath, manager.target[index].pid) == 0)
825 if(get_app_base_address(&base_address) == 1)
831 get_app_install_path(tempPath, PATH_MAX);
834 get_device_info(tempBuff, DA_MSG_MAX);
837 log.type = MSG_DEVICE;
838 if (strlen(tempPath) > 0)
840 get_executable(manager.appPath, tempBuff2, DA_MSG_MAX);
841 log.length = sprintf(log.data, "%s`,%d`,%Lu`,%d`,%u`,%d`,%s/%s", tempBuff,
842 manager.target[index].pid, manager.target[index].starttime,
843 is_app_built_pie(), base_address, get_app_type(manager.appPath),
844 tempPath, get_app_name(tempBuff2));
848 log.length = sprintf(log.data, "%s`,%d`,%Lu`,%d`,%u`,%d`,", tempBuff,
849 manager.target[index].pid, manager.target[index].starttime,
850 is_app_built_pie(), base_address, get_app_type(manager.appPath));
853 LOGI("%s\n", log.data);
858 log.length = sprintf(log.data, "%d`,%Lu", manager.target[index].pid, manager.target[index].starttime);
861 sendDataToHost(&log);
862 manager.target[index].initial_log = 1;
865 if(msg & EVENT_STOP || msg & EVENT_ERROR)
867 LOGI("target close, socket(%d), pid(%d) : (remaining %d target)\n",
868 manager.target[index].socket, manager.target[index].pid, manager.target_count - 1);
870 terminate_target(index);
871 epoll_ctl(epollfd, EPOLL_CTL_DEL, manager.target[index].event_fd, NULL);
872 setEmptyTargetSlot(index);
873 if (0 == __sync_sub_and_fetch(&manager.target_count, 1)) // all target client are closed
875 log.type = MSG_TERMINATE;
878 sendDataToHost(&log);
886 // return 0 if normal case
887 // return plus value if non critical error occur
888 // return minus value if critical error occur
889 static int targetServerHandler(int efd)
892 struct epoll_event ev;
894 int index = getEmptyTargetSlot();
895 if(index == MAX_TARGET_COUNT)
897 LOGW("Max target number(8) reached, no more target can connected\n");
901 manager.target[index].socket = accept(manager.target_server_socket, NULL, NULL);
903 if(manager.target[index].socket >= 0) // accept succeed
906 // set smack attribute for certification
907 fsetxattr(manager.target[index].socket, "security.SMACK64IPIN", "*", 1, 0);
908 fsetxattr(manager.target[index].socket, "security.SMACK64IPOUT", "*", 1, 0);
909 #endif /* LOCALTEST */
911 // send config message to target process
912 log.type = MSG_OPTION;
913 log.length = sprintf(log.data, "%u", manager.config_flag);
914 send(manager.target[index].socket, &log, sizeof(log.type) + sizeof(log.length) + log.length, MSG_NOSIGNAL);
917 manager.target[index].event_fd = eventfd(0, EFD_NONBLOCK);
918 if(manager.target[index].event_fd == -1)
920 // fail to make event fd
921 LOGE("fail to make event fd for socket (%d)\n", manager.target[index].socket);
922 goto TARGET_CONNECT_FAIL;
925 // add event fd to epoll list
927 ev.data.fd = manager.target[index].event_fd;
928 if(epoll_ctl(efd, EPOLL_CTL_ADD, manager.target[index].event_fd, &ev) < 0)
930 // fail to add event fd
931 LOGE("fail to add event fd to epoll list for socket (%d)\n", manager.target[index].socket);
932 goto TARGET_CONNECT_FAIL;
935 // make recv thread for target
936 if(makeRecvThread(index) != 0)
938 // fail to make recv thread
939 LOGE("fail to make recv thread for socket (%d)\n", manager.target[index].socket);
940 epoll_ctl(efd, EPOLL_CTL_DEL, manager.target[index].event_fd, NULL);
941 goto TARGET_CONNECT_FAIL;
944 if(manager.app_launch_timerfd >= 0)
946 epoll_ctl(efd, EPOLL_CTL_DEL, manager.app_launch_timerfd, NULL);
947 close(manager.app_launch_timerfd);
948 manager.app_launch_timerfd = -1;
951 LOGI("target connected = %d(running %d target)\n",
952 manager.target[index].socket, manager.target_count + 1);
954 manager.target_count++;
959 LOGE("Failed to accept at target server socket\n");
963 if(manager.target_count == 0) // if this connection is main connection
967 else // if this connection is not main connection then ignore process by error
969 setEmptyTargetSlot(index);
974 // return 0 if normal case
975 // return plus value if non critical error occur
976 // return minus value if critical error occur
977 static int hostServerHandler(int efd)
979 static int hostserverorder = 0;
981 struct epoll_event ev;
983 if(hostserverorder > 1) // control and data socket connected already
986 csocket = accept(manager.host_server_socket, NULL, NULL);
988 if(csocket >= 0) // accept succeed
991 ev.data.fd = csocket;
992 if(epoll_ctl(efd, EPOLL_CTL_ADD, csocket, &ev) < 0)
994 // consider as accept fail
995 LOGE("Failed to add socket fd to epoll list\n");
1000 if(hostserverorder == 0)
1002 manager.host.control_socket = csocket;
1004 LOGI("host control socket connected = %d\n", csocket);
1008 manager.host.data_socket = csocket;
1009 LOGI("host data socket connected = %d\n", csocket);
1015 else // accept error
1017 LOGE("Failed to accept from host server socket\n");
1023 //TODO del it or move to debug section
1024 void printBuf (char * buf, int len)
1027 char local_buf[3*16 + 2*16 + 1];
1030 for ( i = 0; i < len/16 + 1; i++)
1032 memset(local_buf, ' ', 5*16);
1034 p2 = local_buf + 3*17;
1035 for ( j = 0; j < 16; j++)
1038 sprintf(p1, "%02X ",(unsigned char) *buf);
1040 if (isprint( *buf)){
1041 sprintf(p2, "%c ",(int)*buf);
1050 LOGI("%s\n",local_buf);
1054 // return 0 if normal case
1055 // return plus value if non critical error occur
1056 // return minus value if critical error occur
1057 // return -11 if socket closed
1059 static int controlSocketHandler(int efd)
1062 char recvBuf[DA_MSG_MAX];
1065 // host log format xxx|length|str
1066 recvLen = recv(manager.host.control_socket, recvBuf, DA_MSG_MAX, 0);
1070 recvBuf[recvLen] = '\0';
1071 printBuf(recvBuf,recvLen);
1072 LOGI("host sent control msg str(%s)\n", recvBuf);
1073 if(parseHostMessage(&log, recvBuf) < 0)
1075 // error to parse host message
1076 sendACKCodeToHost(MSG_NOTOK, ERR_WRONG_MESSAGE_FORMAT);
1080 // host msg command handling
1081 return hostMessageHandle(&log);
1083 else // close request from HOST
1089 // return 0 for normal case
1092 int ret = 0; // return value
1096 struct epoll_event ev, *events;
1097 int efd; // epoll fd
1098 int numevent; // number of occured events
1100 _get_fds(g_key_dev, INPUT_ID_KEY);
1101 _get_fds(g_touch_dev, INPUT_ID_TOUCH);
1103 // initialize epoll event pool
1104 events = (struct epoll_event*) malloc(sizeof(struct epoll_event) * EPOLL_SIZE);
1107 LOGE("Out of memory when allocate epoll event pool\n");
1111 if((efd = epoll_create(MAX_CONNECT_SIZE)) < 0)
1113 LOGE("epoll creation error\n");
1118 // add server sockets to epoll event pool
1119 ev.events = EPOLLIN;
1120 ev.data.fd = manager.host_server_socket;
1121 if(epoll_ctl(efd, EPOLL_CTL_ADD, manager.host_server_socket, &ev) < 0)
1123 LOGE("Host server socket epoll_ctl error\n");
1127 ev.events = EPOLLIN;
1128 ev.data.fd = manager.target_server_socket;
1129 if(epoll_ctl(efd, EPOLL_CTL_ADD, manager.target_server_socket, &ev) < 0)
1131 LOGE("Target server socket epoll_ctl error\n");
1136 // add device fds to epoll event pool
1137 ev.events = EPOLLIN;
1138 for(i = 0; g_key_dev[i].fd != ARRAY_END; i++)
1140 if(g_key_dev[i].fd >= 0)
1142 ev.data.fd = g_key_dev[i].fd;
1143 if(epoll_ctl(efd, EPOLL_CTL_ADD, g_key_dev[i].fd, &ev) < 0)
1145 LOGE("keyboard device file epoll_ctl error\n");
1150 ev.events = EPOLLIN;
1151 for(i = 0; g_touch_dev[i].fd != ARRAY_END; i++)
1153 if(g_touch_dev[i].fd >= 0)
1155 ev.data.fd = g_touch_dev[i].fd;
1156 if(epoll_ctl(efd, EPOLL_CTL_ADD, g_touch_dev[i].fd, &ev) < 0)
1158 LOGE("touch device file epoll_ctl error\n");
1166 numevent = epoll_wait(efd, events, EPOLL_SIZE, -1);
1169 LOGE("Failed to epoll_wait : num of event(%d), errno(%d)\n", numevent, errno);
1173 for(i = 0; i < numevent; i++)
1175 // check for request from event fd
1176 for(k = 0; k < MAX_TARGET_COUNT; k++)
1178 if(manager.target[k].socket != -1 &&
1179 events[i].data.fd == manager.target[k].event_fd)
1182 recvLen = read(manager.target[k].event_fd, &u, sizeof(uint64_t));
1183 if(recvLen != sizeof(uint64_t))
1185 // maybe closed, but ignoring is more safe then removing fd from epoll list
1189 if(-11 == targetEventHandler(efd, k, u))
1191 LOGI("all target process is closed\n");
1201 if(k != MAX_TARGET_COUNT)
1204 // check for request from device fd
1205 for(k = 0; g_touch_dev[k].fd != ARRAY_END; k++)
1207 if(g_touch_dev[k].fd >= 0 &&
1208 events[i].data.fd == g_touch_dev[k].fd)
1210 if(deviceEventHandlerNew(&g_touch_dev[k], INPUT_ID_TOUCH) < 0)
1212 terminate_error("Internal DA framework error, Please re-run the profiling.", 1);
1220 if(g_touch_dev[k].fd != ARRAY_END)
1223 for(k = 0; g_key_dev[k].fd != ARRAY_END; k++)
1225 if(g_key_dev[k].fd >= 0 &&
1226 events[i].data.fd == g_key_dev[k].fd)
1228 if(deviceEventHandlerNew(&g_key_dev[k], INPUT_ID_KEY) < 0)
1230 terminate_error("Internal DA framework error, Please re-run the profiling.", 1);
1238 if(g_key_dev[k].fd != ARRAY_END)
1241 // connect request from target
1242 if(events[i].data.fd == manager.target_server_socket)
1244 if(targetServerHandler(efd) < 0) // critical error
1246 terminate_error("Internal DA framework error, Please re-run the profiling.", 1);
1251 // connect request from host
1252 else if(events[i].data.fd == manager.host_server_socket)
1254 int result = hostServerHandler(efd);
1257 terminate_error("Internal DA framework error, Please re-run the profiling.", 1);
1262 // control message from host
1263 else if(events[i].data.fd == manager.host.control_socket)
1265 int result = controlSocketHandler(efd);
1266 if(result == -11) // socket close
1268 // close target and host socket and quit
1269 LOGI("host close = %d\n", manager.host.control_socket);
1276 terminate_error("Internal DA framework error, Please re-run the profiling.", 1);
1281 else if(events[i].data.fd == manager.host.data_socket)
1284 recvLen = recv(manager.host.data_socket, recvBuf, 32, MSG_DONTWAIT);
1286 { // close data socket
1287 epoll_ctl(efd, EPOLL_CTL_DEL, manager.host.data_socket, NULL);
1288 close(manager.host.data_socket);
1289 manager.host.data_socket = -1;
1292 LOGW("host message from data socket %d\n", recvLen);
1294 // check for application launch timerfd
1295 else if(events[i].data.fd == manager.app_launch_timerfd)
1297 // send to host timeout error message for launching application
1298 terminate_error("Failed to launch application", 1);
1299 epoll_ctl(efd, EPOLL_CTL_DEL, manager.app_launch_timerfd, NULL);
1300 close(manager.app_launch_timerfd);
1301 manager.app_launch_timerfd = -1;
1309 LOGW("Unknown socket fd (%d)\n", events[i].data.fd);