[Title] add graceful shutdown api
authorSon Hyunjun <hj79.son@samsung.com>
Wed, 4 Apr 2012 05:12:44 +0000 (14:12 +0900)
committerSon Hyunjun <hj79.son@samsung.com>
Wed, 4 Apr 2012 05:12:44 +0000 (14:12 +0900)
[Type] Feature
[Module] Skin
[Priority] Minor
[CQ#]
[Redmine#]
[Problem]
[Cause]
[Solution]

Change-Id: Ia40885f645154ad2b5d5ca04f6c2627f27cab6b2

tizen/src/skin/maruskin_operation.c
tizen/src/skin/maruskin_operation.h
tizen/src/skin/maruskin_server.c

index 0c70b64182b6aa715fe758c1cb4fd05c76b49f11..d0bdce4a22b1257397b2291dbfd251544e3929a5 100644 (file)
@@ -30,6 +30,7 @@
 
 #include <unistd.h>
 #include <stdio.h>
+#include <pthread.h>
 #include "maruskin_operation.h"
 #include "maru_sdl.h"
 #include "debug_ch.h"
@@ -48,8 +49,11 @@ MULTI_DEBUG_CHANNEL(qemu, skin_operation);
 
 #define RESUME_KEY_SEND_INTERVAL 500 // milli-seconds
 #define CLOSE_POWER_KEY_INTERVAL 1200 // milli-seconds
+#define DATA_DELIMITER "#" // in detail info data
+#define TIMEOUT_FOR_SHUTDOWN 10 // seconds
 
-#define DATA_DELIMITER "#"
+static void* run_timed_shutdown_thread( void* args );
+static void send_to_emuld( const char* request_type, int request_size, const char* send_buf, int buf_size );
 
 void start_display( int handle_id, int lcd_size_width, int lcd_size_height, double scale_factor, short rotation_type )
 {
@@ -168,7 +172,6 @@ void do_rotation_event( int rotation_type)
 
     INFO( "do_rotation_event rotation_type:%d", rotation_type);
 
-    int buf_size = 32;
     char send_buf[32] = { 0 };
 
     switch ( rotation_type ) {
@@ -189,29 +192,9 @@ void do_rotation_event( int rotation_type)
             break;
     }
 
-    // send_to_sensor_daemon
-    int s;
+    send_to_emuld( "sensor\n\n\n\n", 10, send_buf, 32 );
 
-    s = tcp_socket_outgoing( "127.0.0.1", (uint16_t) ( tizen_base_port + SDB_TCP_EMULD_INDEX ) );
-    if ( s < 0 ) {
-        ERR( "can't create socket to talk to the sdb forwarding session \n");
-        ERR( "[127.0.0.1:%d/tcp] connect fail (%d:%s)\n" , tizen_base_port + SDB_TCP_EMULD_INDEX , errno, strerror(errno));
-        return;
-    }
-
-    socket_send( s, "sensor\n\n\n\n", 10 );
-    socket_send( s, &buf_size, 4 );
-    socket_send( s, send_buf, buf_size );
-
-    INFO( "send to sensord(size: %d) 127.0.0.1:%d/tcp \n", buf_size, tizen_base_port + SDB_TCP_EMULD_INDEX);
-
-    set_emul_rotation(rotation_type);
-
-#ifdef _WIN32
-    closesocket( s );
-#else
-    close( s );
-#endif
+    set_emul_rotation( rotation_type );
 
 }
 
@@ -348,6 +331,61 @@ void request_close( void )
 
 }
 
-void shutdown_qemu( void ) {
+void shutdown_qemu_gracefully( void ) {
+
+    pthread_t thread_id;
+    if( 0 > pthread_create( &thread_id, NULL, run_timed_shutdown_thread, NULL ) ) {
+        ERR( "!!! Fail to create run_timed_shutdown_thread. shutdown qemu right now !!!\n"  );
+        qemu_system_shutdown_request();
+    }
+
+}
+
+static void* run_timed_shutdown_thread( void* args ) {
+
+    send_to_emuld( "system\n\n\n\n", 10, "shutdown", 8 );
+
+    int sleep_interval_time = 1000; // milli-seconds
+
+    int i;
+    for ( i = 0; i < TIMEOUT_FOR_SHUTDOWN; i++ ) {
+#ifdef _WIN32
+        Sleep( sleep_time );
+#else
+        usleep( sleep_interval_time * 1000 );
+#endif
+        // do not use logger to help user see log in console
+        fprintf( stdout, "Wait for shutdown qemu...%d\n", ( i + 1 ) );
+    }
+
+    WARN( "Shutdown qemu !!!\n" );
     qemu_system_shutdown_request();
+
+    return NULL;
+
+}
+
+static void send_to_emuld( const char* request_type, int request_size, const char* send_buf, int buf_size ) {
+
+    int s = tcp_socket_outgoing( "127.0.0.1", (uint16_t) ( tizen_base_port + SDB_TCP_EMULD_INDEX ) );
+
+    if ( s < 0 ) {
+        ERR( "can't create socket to talk to the sdb forwarding session \n" );
+        ERR( "[127.0.0.1:%d/tcp] connect fail (%d:%s)\n" , tizen_base_port + SDB_TCP_EMULD_INDEX , errno, strerror(errno) );
+        return;
+    }
+
+    socket_send( s, (char*)request_type, request_size );
+    socket_send( s, &buf_size, 4 );
+    socket_send( s, (char*)send_buf, buf_size );
+
+    INFO( "send to emuld [req_type:%s, send_data:%s, send_size:%d] 127.0.0.1:%d/tcp \n",
+        request_type, send_buf, buf_size, tizen_base_port + SDB_TCP_EMULD_INDEX );
+
+#ifdef _WIN32
+    closesocket( s );
+#else
+    close( s );
+#endif
+
 }
index 92c8d5c7d4e5716cbbbfb466014ce1f02281f0a9..d1d13deee2a3a65d6cf9a2d6451e65ff4961ac65 100644 (file)
@@ -68,6 +68,6 @@ void onoff_usb_kbd( int on );
 
 void request_close( void );
 
-void shutdown_qemu( void );
+void shutdown_qemu_gracefully( void );
 
 #endif /* MARUSKIN_OPERATION_H_ */
index 218aeb5e36b19dbe45340cd8e4e57e9b40eac70f..bd1aad54e493831d8c37008d0ef04e0b3aa86870 100644 (file)
@@ -741,7 +741,7 @@ cleanup:
         ERR( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
         ERR( "!!! Fail to initialize for skin server operation. Shutdown QEMU !!!\n" );
         ERR( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
-        shutdown_qemu();
+        shutdown_qemu_gracefully();
     }
 
     return NULL;
@@ -986,7 +986,7 @@ static void* do_heart_beat( void* args ) {
         ERR( "!!! Fail to operate with heartbeat from skin client. Shutdown QEMU !!!\n" );
         ERR( "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" );
 
-        shutdown_qemu();
+        shutdown_qemu_gracefully();
 
     }