add guest_server for sensor daemon
authorSon Hyunjun <hj79.son@samsung.com>
Tue, 13 Mar 2012 01:23:32 +0000 (10:23 +0900)
committerSon Hyunjun <hj79.son@samsung.com>
Tue, 13 Mar 2012 01:23:32 +0000 (10:23 +0900)
tizen/src/Makefile.tizen
tizen/src/emulator.c
tizen/src/guest_server.c [new file with mode: 0644]
tizen/src/guest_server.h [new file with mode: 0644]
tizen/src/skin/maruskin_server.c
tizen/src/skin/maruskin_server.h

index d5b2de8..aca8322 100644 (file)
@@ -39,6 +39,8 @@ endif
 # maru skin
 obj-i386-y += maruskin_client.o maruskin_server.o maruskin_operation.o maruskin_keymap.o
 
+# guest server
+obj-i386-y += guest_server.o 
 
 ##########################################################
 # opengl library for i386
index 61f04ca..98db5b0 100644 (file)
@@ -36,6 +36,7 @@
 #include "string.h"
 #include "skin/maruskin_server.h"
 #include "skin/maruskin_client.h"
+#include "guest_server.h"
 #include "debug_ch.h"
 
 MULTI_DEBUG_CHANNEL(qemu, main);
@@ -69,6 +70,9 @@ static void construct_main_window(int skin_argc, char* skin_argv[])
         //TODO:
     }
 #endif
+
+//    start_guest_server();
+
 }
 
 static void parse_options(int argc, char* argv[], int* skin_argc, char*** skin_argv, int* qemu_argc, char*** qemu_argv)
@@ -127,13 +131,13 @@ int main(int argc, char* argv[])
 */
 
 //     printf("%d\n", qemu_argc);
-       printf("Start emulator : =====================================\n");
+       INFO("Start emulator : =====================================\n");
        for(i = 0; i < qemu_argc; ++i)
        {
-               printf("%s ", qemu_argv[i]);
+           INFO("%s ", qemu_argv[i]);
        }
-       printf("\n");
-       printf("======================================================\n");
+       INFO("\n");
+       INFO("======================================================\n");
 
        construct_main_window(skin_argc, skin_argv);
 
@@ -141,6 +145,8 @@ int main(int argc, char* argv[])
 
        qemu_main(qemu_argc, qemu_argv, NULL);
 
+//     shutdown_guest_server();
+
        return 0;
 }
 
diff --git a/tizen/src/guest_server.c b/tizen/src/guest_server.c
new file mode 100644 (file)
index 0000000..6fd7801
--- /dev/null
@@ -0,0 +1,191 @@
+/*
+ * 
+ *
+ * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * JiHye Kim <jihye1128.kim@samsung.com>
+ * Hyunjun Son <hj79.son@samsung.com>
+ * GiWoong Kim <giwoong.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
+ *
+ * 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 <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <arpa/inet.h>
+#include "guest_server.h"
+#include "sdb.h"
+#include "skin/maruskin_server.h"
+#include "debug_ch.h"
+
+MULTI_DEBUG_CHANNEL(qemu, guest_server);
+
+//TODO change size
+#define RECV_BUF_SIZE 4
+
+static void* run_guest_server( void* args );
+
+static int stop_svr = 0;
+static int svr_port = 0;
+static int server_sock = 0;
+static int client_sock = 0;
+
+enum {
+    SENSOR_DAEMON_START = 3,
+};
+
+pthread_t start_guest_server( void ) {
+
+    svr_port = get_sdb_base_port() + SDB_UDP_SENSOR_INDEX;
+
+    pthread_t thread_id = -1;
+
+    if ( 0 != pthread_create( &thread_id, NULL, run_guest_server, NULL ) ) {
+        ERR( "fail to create guest_server pthread.\n" );
+    }
+
+    return thread_id;
+
+}
+
+static void* run_guest_server( void* args ) {
+
+    uint16_t port;
+    struct sockaddr_in server_addr, client_addr;
+    socklen_t client_len;
+    port = svr_port;
+
+    if ( ( server_sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 ) {
+        INFO( "create listen socket error: " );
+        perror( "socket" );
+        goto cleanup;
+    }
+    memset( &server_addr, '\0', sizeof( server_addr ) );
+    server_addr.sin_family = PF_INET;
+    server_addr.sin_addr.s_addr = inet_addr( "127.0.0.1" );
+    server_addr.sin_port = htons( port );
+
+    int opt = 1;
+    setsockopt( server_sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof( opt ) );
+
+    if ( 0 > bind( server_sock, (struct sockaddr*) &server_addr, sizeof( server_addr ) ) ) {
+        ERR( "guest server bind error: " );
+        perror( "bind" );
+        goto cleanup;
+    } else {
+        INFO( "success to bind port[127.0.0.1:%d/tcp] for guest_server in host \n", port );
+    }
+
+    if ( listen( server_sock, 1 ) < 0 ) {
+        INFO( "guest_server listen error: " );
+        perror( "listen" );
+        goto cleanup;
+    }
+
+    char readbuf[RECV_BUF_SIZE];
+
+    INFO( "guest server start...port:%d\n", port );
+
+    while ( 1 ) {
+        if ( stop_svr ) {
+            break;
+        }
+
+        INFO( "start accepting socket...\n" );
+
+        client_len = sizeof( client_addr );
+        if ( 0 > ( client_sock = accept( server_sock, (struct sockaddr*) &client_addr, &client_len ) ) ) {
+            ERR( "guest_servier accept error: " );
+            perror( "accept" );
+            continue;
+        }
+
+        while ( 1 ) {
+
+            if ( stop_svr ) {
+                INFO( "stop reading this socket.\n" );
+                break;
+            }
+
+            memset( &readbuf, 0, RECV_BUF_SIZE );
+
+            int read_cnt = read( client_sock, readbuf, RECV_BUF_SIZE );
+
+            if ( 0 > read_cnt ) {
+
+                perror( "error : guest_server read" );
+                break;
+
+            } else {
+
+                if ( 0 == read_cnt ) {
+                    INFO( "read_cnt is 0.\n" );
+                    break;
+                }
+
+                INFO( "================= recv =================\n" );
+                INFO( "read_cnt:%d\n", read_cnt );
+
+                //TODO parse data
+                char cmd = 0;
+                memcpy( &cmd, readbuf, sizeof( cmd ) );
+                INFO( "cmd:%s\n", cmd );
+
+                INFO( "----------------------------------------\n" );
+                switch ( cmd ) {
+                case SENSOR_DAEMON_START: {
+                    notify_sensor_daemon_start();
+                    break;
+                }
+                default: {
+                    ERR( "!!! unknown command : %d\n", cmd );
+                    break;
+                }
+                }
+
+                INFO( "========================================\n" );
+
+            }
+
+        }
+
+        if ( client_sock ) {
+            close( client_sock );
+        }
+    }
+
+    cleanup: if ( server_sock ) {
+        close( server_sock );
+    }
+
+    return NULL;
+}
+
+void shutdown_guest_server( void ) {
+    stop_svr = 1;
+    if ( client_sock ) {
+        close( client_sock );
+    }
+    if ( server_sock ) {
+        close( server_sock );
+    }
+}
diff --git a/tizen/src/guest_server.h b/tizen/src/guest_server.h
new file mode 100644 (file)
index 0000000..376bb5c
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 
+ *
+ * Copyright (C) 2011 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * JiHye Kim <jihye1128.kim@samsung.com>
+ * Hyunjun Son <hj79.son@samsung.com>
+ * GiWoong Kim <giwoong.kim@samsung.com>
+ * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
+ *
+ * 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 GUEST_SERVER_H_
+#define GUEST_SERVER_H_
+
+#include <pthread.h>
+
+pthread_t start_guest_server( void );
+void shutdown_guest_server( void );
+
+#endif /* GUEST_SERVER_H_ */
index 3d05709..f5e3606 100644 (file)
@@ -69,6 +69,7 @@ enum {
 enum {
     SEND_HEART_BEAT = 1,
     SEND_HEART_BEAT_RESPONSE = 2,
+    SEND_SENSOR_DAEMON_START = 800,
     SEND_SHUTDOWN = 999,
 };
 
@@ -97,7 +98,7 @@ pthread_t start_skin_server( uint16_t default_svr_port, int argc, char** argv )
     pthread_t thread_id = -1;
 
     if ( 0 != pthread_create( &thread_id, NULL, run_skin_server, NULL ) ) {
-        INFO( "fail to create skin_server pthread.\n" );
+        ERR( "fail to create skin_server pthread.\n" );
     }
 
     return thread_id;
@@ -108,7 +109,7 @@ void shutdown_skin_server(void) {
     if ( client_sock ) {
         INFO( "Send shutdown to skin.\n" );
         if ( 0 > send_skin( client_sock, SEND_SHUTDOWN ) ) {
-            INFO( "fail to send shutdown to skin.\n" );
+            ERR( "fail to send SEND_SHUTDOWN to skin.\n" );
             stop = 1;
             // force close
             close( client_sock );
@@ -121,6 +122,12 @@ void shutdown_skin_server(void) {
     }
 }
 
+void notify_sensor_daemon_start(void) {
+    if ( 0 > send_skin( client_sock, SEND_SENSOR_DAEMON_START ) ) {
+        ERR( "fail to send SEND_SENSOR_DAEMON_START to skin.\n" );
+    }
+}
+
 static void* run_skin_server( void* args ) {
 
     uint16_t port;
@@ -138,7 +145,7 @@ static void* run_skin_server( void* args ) {
     port = svr_port;
 
     if ( ( server_sock = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP ) ) < 0 ) {
-        INFO( "create listen socket error: " );
+        ERR( "create listen socket error: " );
         perror( "socket" );
         goto cleanup;
     }
@@ -154,7 +161,7 @@ static void* run_skin_server( void* args ) {
 
     if ( 0 > bind( server_sock, (struct sockaddr *) &server_addr, sizeof( server_addr ) ) ) {
         //TODO rebind in case of port
-        INFO( "skin server bind error: " );
+        ERR( "skin server bind error: " );
         perror( "bind" );
         goto cleanup;
     } else {
@@ -162,7 +169,7 @@ static void* run_skin_server( void* args ) {
     }
 
     if ( listen( server_sock, 4 ) < 0 ) {
-        INFO( "skin_server listen error: " );
+        ERR( "skin_server listen error: " );
         perror( "listen" );
         goto cleanup;
     }
@@ -183,7 +190,7 @@ static void* run_skin_server( void* args ) {
 
         client_len = sizeof(client_addr);
         if ( 0 > ( client_sock = accept( server_sock, (struct sockaddr *) &client_addr, &client_len ) ) ) {
-            INFO( "skin_servier accept error: " );
+            ERR( "skin_servier accept error: " );
             perror( "accept" );
             continue;
         }
@@ -254,10 +261,10 @@ static void* run_skin_server( void* args ) {
                         perror( "error : skin_server read data" );
                         break;
                     } else if ( 0 == read_cnt ) {
-                        INFO( "data read_cnt is 0.\n" );
+                        ERR( "data read_cnt is 0.\n" );
                         break;
                     } else if ( read_cnt != length ) {
-                        INFO( "read_cnt is not equal to length.\n" );
+                        ERR( "read_cnt is not equal to length.\n" );
                         break;
                     }
 
@@ -574,23 +581,3 @@ static void stop_heart_beat(void) {
     pthread_cond_signal( &cond_heartbeat );
     pthread_mutex_unlock( &mutex_heartbeat );
 }
-
-#if 0 //XXX for test
-int main( int argc, char** argv ) {
-
-    int thread_return;
-
-    pthread_t thread_id = start_skin_server( 11111, argc, argv );
-
-    if ( -1 == thread_id ) {
-        return -1;
-    }
-
-    pthread_join( thread_id, (void**) &thread_return );
-
-    INFO( "exit program...thread_return:%d\n", thread_return );
-
-    return 0;
-
-}
-#endif
index 73a846e..aa75ac9 100644 (file)
@@ -35,5 +35,6 @@
 
 pthread_t start_skin_server( uint16_t default_svr_port, int argc, char** argv );
 void shutdown_skin_server(void);
+void notify_sensor_daemon_start(void);
 
 #endif /* MARUSKIN_SERVER_H_ */