Home | History | Annotate | Download | only in protocol
      1 /* Copyright (C) 2010 The Android Open Source Project
      2 **
      3 ** This software is licensed under the terms of the GNU General Public
      4 ** License version 2, as published by the Free Software Foundation, and
      5 ** may be copied, distributed, and modified under those terms.
      6 **
      7 ** This program is distributed in the hope that it will be useful,
      8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
      9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     10 ** GNU General Public License for more details.
     11 */
     12 
     13 /*
     14  * Contains the UI-side implementation of the "core-ui-control" service that is
     15  * part of the UI control protocol. Here we handle UI control commands received
     16  * from the Core.
     17  */
     18 
     19 #include "android/utils/debug.h"
     20 #include "android/utils/panic.h"
     21 #include "android/protocol/core-connection.h"
     22 #include "android/protocol/attach-ui-impl.h"
     23 
     24 /* Descriptor for the UI-side of the "attach-ui" service. */
     25 typedef struct AttachUIImpl {
     26     /* Address of the Core's console socket. */
     27     SockAddress     console_socket;
     28 
     29     /* Core connection established for this service. */
     30     CoreConnection* core_connection;
     31 
     32     /* Socket descriptor for the UI service. */
     33     int             sock;
     34 } AttachUIImpl;
     35 
     36 /* One and only one AttachUIImpl instance. */
     37 static AttachUIImpl  _attachUiImpl;
     38 
     39 SockAddress*
     40 attachUiImpl_get_console_socket(void)
     41 {
     42     return &_attachUiImpl.console_socket;
     43 }
     44 
     45 int
     46 attachUiImpl_create(SockAddress* console_socket)
     47 {
     48     char* handshake = NULL;
     49 
     50     _attachUiImpl.console_socket = *console_socket;
     51 
     52     // Connect to the core-ui-control service.
     53     _attachUiImpl.core_connection =
     54         core_connection_create_and_switch(console_socket, "attach-UI",
     55                                           &handshake);
     56     if (_attachUiImpl.core_connection == NULL) {
     57         derror("Unable to connect to the attach-UI service: %s\n",
     58                errno_str);
     59         return -1;
     60     }
     61 
     62     fprintf(stdout, "UI is now attached to the core at %s.",
     63             sock_address_to_string(console_socket));
     64     if (handshake != NULL) {
     65         if (handshake[0] != '\0') {
     66             fprintf(stdout, " Handshake: %s", handshake);
     67         }
     68         free(handshake);
     69     }
     70     fprintf(stdout, "\n");
     71 
     72     return 0;
     73 }
     74 
     75 void
     76 attachUiImpl_destroy(void)
     77 {
     78     if (_attachUiImpl.core_connection != NULL) {
     79         core_connection_close(_attachUiImpl.core_connection);
     80         core_connection_free(_attachUiImpl.core_connection);
     81         _attachUiImpl.core_connection = NULL;
     82     }
     83 }
     84