run dos2unix to get rid of ^M
[platform/core/multimedia/libmm-wfd.git] / server / miracast_server.c
1 /*
2  * libmm-wfd
3  *
4  * Copyright (c) 2011 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: JongHyuk Choi <jhchoi.choi@samsung.com>, ByungWook Jang <bw.jang@samsung.com>,
7  * Manoj Kumar K <manojkumar.k@samsung.com>, Hyunil Park <hyunil46.park@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23  /*===========================================================================================
24 |                                             |
25 |  INCLUDE FILES                                      |
26 |                                               |
27 ========================================================================================== */
28 //#define MTRACE;
29 #include <glib.h>
30 #include <mm_types.h>
31 #include <mm_error.h>
32 #include <mm_message.h>
33 #include <mm_wfd.h>
34 #include <mm_debug.h>
35 #include <mm_message.h>
36 #include <mm_error.h>
37 #include <mm_types.h>
38
39 #include <iniparser.h>
40 #include <mm_ta.h>
41 #include <dlfcn.h>
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <fcntl.h>
46 #include <signal.h>
47
48 #include <netdb.h>
49 #include <dbus/dbus.h>
50 #include <dbus/dbus-glib-lowlevel.h>
51
52 #include <vconf.h>
53
54 #define PACKAGE "mm_wfd_testsuite"
55
56 /*===========================================================================================
57 |                                             |
58 |  LOCAL DEFINITIONS AND DECLARATIONS FOR MODULE                      |
59 |                                               |
60 ========================================================================================== */
61
62 #if defined(_USE_GMAINLOOP)
63 GMainLoop *g_loop;
64 #endif
65
66 gboolean wfd_server_test_method(void *pObject, int pid, char *test_name,
67          int *return_code)
68 {
69   debug_log("Received test %s\n", test_name);
70   return TRUE;
71 }
72
73 #include "wfd-structure.h"
74
75 #define WFD_PROXY_COMM_PORT 8888
76
77 typedef struct WfdServerObject WfdServerObject;
78 typedef struct WfdServerObjectClass WfdServerObjectClass;
79
80 GType Daemon_Object_get_type(void);
81 struct WfdServerObject {
82   GObject parent;
83 };
84
85 struct WfdServerObjectClass {
86   GObjectClass parent;
87 };
88
89 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
90
91 #define WFD_SERVER_TYPE_OBJECT (Daemon_Object_get_type())
92
93 #define WFD_SERVER_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST \
94   ((object), WFD_SERVER_TYPE_OBJECT, WfdServerObject))
95
96 G_DEFINE_TYPE(WfdServerObject, Daemon_Object, G_TYPE_OBJECT)
97 static void Daemon_Object_init(WfdServerObject * obj)
98 {
99   debug_log("Daemon_Object_init\n");
100 }
101
102 static void Daemon_Object_class_init(WfdServerObjectClass * klass)
103 {
104   debug_log("Daemon_Object_class_init\n");
105 }
106
107 /*---------------------------------------------------------------------------
108 |    LOCAL #defines:                            |
109 ---------------------------------------------------------------------------*/
110 #define MAX_STRING_LEN    2048
111
112 /*---------------------------------------------------------------------------
113 |    LOCAL CONSTANT DEFINITIONS:                      |
114 ---------------------------------------------------------------------------*/
115 enum
116 {
117   CURRENT_STATUS_MAINMENU,
118   CURRENT_STATUS_SERVER_IP,
119   CURRENT_STATUS_SERVER_PORT,
120   CURRENT_STATUS_CONNECT,
121 };
122
123 typedef struct {
124   int connfd;
125   socklen_t clilen;
126   struct sockaddr_in cli_addr;
127   GThread *thread;
128   char inbuff[512];
129   gboolean inactive;
130   GMutex *lock;
131   GIOChannel *channel;
132   void *parent;
133 } WFDClient;
134
135 typedef struct {
136   int sockfd;
137   int portno;
138   struct sockaddr_in serv_addr;
139   GThread *thread;
140   GList *clients;
141 } WFDServer;
142
143 /*---------------------------------------------------------------------------
144 |    LOCAL VARIABLE DEFINITIONS:                      |
145 ---------------------------------------------------------------------------*/
146
147 int  g_current_state;
148 static MMHandleType g_wfd = 0;
149 char *g_err_name = NULL;
150 gboolean quit_pushing;
151 int socket_id = 0;
152 /*---------------------------------------------------------------------------
153 |    LOCAL FUNCTION PROTOTYPES:                       |
154 ---------------------------------------------------------------------------*/
155 static void wfd_connect();
156 static void wfd_start();
157 static void wfd_stop();
158
159 void quit_program();
160
161 static void wfd_set_signal();
162
163 /*---------------------------------------------------------------------------
164   |    LOCAL FUNCTION DEFINITIONS:                      |
165   ---------------------------------------------------------------------------*/
166 static bool msg_callback(int message, MMMessageParamType *param, void *user_param)
167 {
168   /* TODO any error notification or state change should be forwarded to the active list of proxy apps */
169   switch (message) {
170     case MM_MESSAGE_ERROR:
171       {
172         WFDClient *client = (WFDClient *)user_param;
173         if(!client) return FALSE;
174         WFDServer *lserver = (WFDServer*) client->parent;
175         if(!lserver) return FALSE;
176         debug_log("msg_callback error : code = %x\n", param->code);
177         debug_log ("DESTROY..\n\n");
178         quit_pushing = TRUE;
179         shutdown (lserver->sockfd, SHUT_RDWR);
180             debug_log("msg_callback error call quit_program()");
181         quit_program();
182       }
183       break;
184     case MM_MESSAGE_WARNING:
185       // debug_log("warning : code = %d\n", param->code);
186       break;
187     case MM_MESSAGE_END_OF_STREAM:
188       debug_log("msg_callback end of stream\n");
189       mm_wfd_stop(g_wfd);
190       break;
191     case MM_MESSAGE_STATE_CHANGED:
192       g_current_state = param->state.current;
193       switch(g_current_state)
194       {
195         case MM_WFD_STATE_NULL:
196           debug_log("\n                                                            ==> [WFDApp] Player is [NULL]\n");
197           break;
198         case MM_WFD_STATE_READY:
199           debug_log("\n                                                            ==> [WFDApp] Player is [READY]\n");
200           break;
201         case MM_WFD_STATE_PLAYING:
202           debug_log("\n                                                            ==> [WFDApp] Player is [PLAYING]\n");
203           break;
204         case MM_WFD_STATE_PAUSED:
205           debug_log("\n                                                            ==> [WFDApp] Player is [PAUSED]\n");
206           break;
207       }
208       break;
209     default:
210       return FALSE;
211   }
212   return TRUE;
213 }
214
215 static gboolean proxy_write (WFDClient *client, char *wbuf)
216 {
217   write(client->connfd, wbuf, strlen(wbuf));
218   return TRUE;
219 }
220
221 static void input_server_ip_and_port(char *server_ip, char* port, WFDClient *client)
222 {
223   int len = strlen(server_ip);
224   int ret = MM_ERROR_NONE;
225   if ( len < 0 || len > MAX_STRING_LEN )
226     return;
227
228   if (!g_wfd)
229   {
230     if ( mm_wfd_create(&g_wfd) != MM_ERROR_NONE )
231     {
232       debug_log("input_server_ip wfd create is failed\n");
233       return;
234     }
235     ret = mm_wfd_set_message_callback(g_wfd, msg_callback, (void*)client);
236     if (ret != MM_ERROR_NONE)
237     {
238       debug_log ("input_server_ip Error in setting server_port...\n");
239       return;
240     }
241   }
242   ret = mm_wfd_set_attribute(g_wfd,
243         &g_err_name,
244         "server_ip", server_ip, strlen(server_ip),
245         NULL);
246   if (ret != MM_ERROR_NONE)
247   {
248     debug_log ("input_server_ip Error in setting server_port...\n");
249     return;
250   }
251   ret = mm_wfd_set_attribute(g_wfd,
252           &g_err_name,
253           "server_port", port, strlen(port),
254           NULL);
255   if (ret != MM_ERROR_NONE)
256   {
257     debug_log ("input_server_port Error in setting server_port...\n");
258     return;
259   }
260   /*set wfd source status*/
261   gboolean status = VCONFKEY_MIRACAST_WFD_SOURCE_ON;
262   if (!vconf_set_bool(VCONFKEY_MIRACAST_WFD_SOURCE_STATUS, status)) {
263     debug_log ("set VCONFKEY_MIRACAST_WFD_SOURCE_ON");
264   }
265 }
266 static void wfd_connect()
267 {
268   int ret = MM_ERROR_NONE;
269
270   if (!g_wfd)
271   {
272     debug_log ("wfd_start Creating server with default values : ");
273     if ( mm_wfd_create(&g_wfd) != MM_ERROR_NONE )
274     {
275       debug_log("wfd_start wfd create is failed\n");
276       return;
277     }
278
279     ret = mm_wfd_set_message_callback(g_wfd, msg_callback, (void*)g_wfd);
280     if (ret != MM_ERROR_NONE)
281     {
282       debug_log ("wfd_start Error in setting server_port...\n");
283       return;
284     }
285   }
286   if ( mm_wfd_realize(g_wfd) != MM_ERROR_NONE )
287   {
288     debug_log("wfd_start wfd realize is failed\n");
289     return;
290   }
291
292   if ( mm_wfd_connect(g_wfd) != MM_ERROR_NONE )
293   {
294     debug_log("wfd_start wfd connect is failed\n");
295     return;
296   }
297
298 }
299
300 static void wfd_start()
301 {
302   int ret = MM_ERROR_NONE;
303
304   if (!g_wfd)
305   {
306     debug_log ("wfd_start Creating server with default values : ");
307     if ( mm_wfd_create(&g_wfd) != MM_ERROR_NONE )
308     {
309       debug_log("wfd_start wfd create is failed\n");
310       return;
311     }
312
313     ret = mm_wfd_set_message_callback(g_wfd, msg_callback, (void*)g_wfd);
314     if (ret != MM_ERROR_NONE)
315     {
316       debug_log ("wfd_start Error in setting server_port...\n");
317       return;
318     }
319
320    if ( mm_wfd_realize(g_wfd) != MM_ERROR_NONE )
321    {
322      debug_log("wfd_start wfd realize is failed\n");
323      return;
324    }
325
326    if ( mm_wfd_connect(g_wfd) != MM_ERROR_NONE )
327    {
328      debug_log("wfd_start wfd connect is failed\n");
329      return;
330     }
331   }
332
333   if (mm_wfd_start(g_wfd) != MM_ERROR_NONE)
334   {
335     debug_log ("wfd_start Failed to start WFD");
336     return;
337   }
338 }
339
340 static void wfd_stop()
341 {
342   int ret = MM_ERROR_NONE;
343
344   ret = mm_wfd_stop(g_wfd);
345   if (ret != MM_ERROR_NONE)
346   {
347     debug_log ("wfd_stop Error to do stop...\n");
348     return;
349   }
350 }
351
352 static void wfd_pause()
353 {
354   int ret = MM_ERROR_NONE;
355
356   ret = mm_wfd_pause(g_wfd);
357   if (ret != MM_ERROR_NONE)
358   {
359     debug_log ("wfd_pause Error to do pausep...\n");
360     return;
361   }
362 }
363
364 static void wfd_resume()
365 {
366   int ret = MM_ERROR_NONE;
367
368   ret = mm_wfd_resume(g_wfd);
369   if (ret != MM_ERROR_NONE)
370   {
371     debug_log ("wfd_resume Error to do resume...\n");
372     return;
373   }
374 }
375
376 static void wfd_standby()
377 {
378   int ret = MM_ERROR_NONE;
379   ret = mm_wfd_standby(g_wfd);
380   if (ret != MM_ERROR_NONE)
381   {
382     debug_log ("wfd_standby Error to do standby...\n");
383     return;
384   }
385 }
386
387 void quit_program()
388 {
389   MMTA_ACUM_ITEM_SHOW_RESULT_TO(MMTA_SHOW_STDOUT);
390   MMTA_RELEASE();
391   debug_log ("quit_program...\n");
392
393   /*set wfd source status*/
394   gboolean status = VCONFKEY_MIRACAST_WFD_SOURCE_OFF;
395   if (!vconf_set_bool(VCONFKEY_MIRACAST_WFD_SOURCE_STATUS, status)) {
396     debug_log ("set VCONFKEY_MIRACAST_WFD_SOURCE_OFF");
397   }
398
399   if (g_wfd) {
400         mm_wfd_unrealize(g_wfd);
401         mm_wfd_destroy(g_wfd);
402         g_wfd = 0;
403   }
404   g_main_loop_quit(g_loop);
405 }
406
407 gchar * convert_state_to_string(MMWfdStateType aState)
408 {
409   GString *cmd_replay;
410   cmd_replay = g_string_new ("");
411   switch(aState)
412   {
413     case MM_WFD_STATE_NULL:
414       g_string_append_printf (cmd_replay, "MM_WFD_STATE_NULL");
415       break;
416     case MM_WFD_STATE_READY:
417       g_string_append_printf (cmd_replay, "MM_WFD_STATE_READY");
418       break;
419     case MM_WFD_STATE_CONNECTION_WAIT:
420       g_string_append_printf (cmd_replay, "MM_WFD_STATE_CONNECTION_WAIT");
421       break;
422     case MM_WFD_STATE_CONNECTED:
423       g_string_append_printf (cmd_replay, "MM_WFD_STATE_CONNECTED");
424       break;
425     case MM_WFD_STATE_PLAYING:
426       g_string_append_printf (cmd_replay, "MM_WFD_STATE_PLAYING");
427       break;
428     case MM_WFD_STATE_PAUSED:
429       g_string_append_printf (cmd_replay, "MM_WFD_STATE_PAUSED");
430       break;
431     case MM_WFD_STATE_NONE:
432       g_string_append_printf (cmd_replay, "MM_WFD_STATE_NONE");
433       break;
434     case MM_WFD_STATE_NUM:
435       g_string_append_printf (cmd_replay, "MM_WFD_STATE_NUM");
436       break;
437   }
438   return g_string_free (cmd_replay, FALSE);
439 }
440
441 static void interpret (WFDClient *client, char *cmd)
442 {
443   GString *cmd_replay;
444   if (strstr(cmd, "WFD_PROXY_SET_IP_PORT"))
445   {
446     gchar **IP_port;
447     gchar **IP;
448     gchar **port;
449     debug_log ("setting attributes... WFD..\n\n");
450     IP_port = g_strsplit(cmd,"\r\n",0);
451     IP = g_strsplit(IP_port[1]," ",0);
452     port = g_strsplit(IP_port[2]," ",0);
453     debug_log ("received IP %s port %s\n", IP[1], port[1]);
454     input_server_ip_and_port(IP[1], port[1], client);
455     cmd_replay = g_string_new ("");
456     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_SET_IP_PORT");
457     g_string_append_printf (cmd_replay, "\r\n");
458     g_string_append_printf (cmd_replay, "MM_ERROR_NONE");
459     proxy_write(client, g_string_free (cmd_replay, FALSE));
460     debug_log ("STATE QUERY..demon return\n\n");
461   }
462   else if (strstr(cmd, "WFD_PROXY_CONNECT"))
463   {
464     debug_log ("Starting... WFD..\n\n");
465     cmd_replay = g_string_new ("");
466     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_CONNECT");
467     g_string_append_printf (cmd_replay, "\r\n");
468     g_string_append_printf (cmd_replay, "MM_ERROR_NONE");
469     proxy_write(client, g_string_free (cmd_replay, FALSE));
470     debug_log ("STATE QUERY..demon return\n\n");
471     wfd_connect();
472   }
473   else if (strstr(cmd, "WFD_PROXY_START"))
474   {
475     debug_log ("Starting... WFD..\n\n");
476     cmd_replay = g_string_new ("");
477     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_START");
478     g_string_append_printf (cmd_replay, "\r\n");
479     g_string_append_printf (cmd_replay, "MM_ERROR_NONE");
480     proxy_write(client, g_string_free (cmd_replay, FALSE));
481     debug_log ("STATE QUERY..demon return\n\n");
482     wfd_start();
483   }
484   else if (strstr(cmd, "WFD_PROXY_PAUSE"))
485   {
486     debug_log ("PAUSING..\n\n");
487     wfd_pause();
488     cmd_replay = g_string_new ("");
489     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_PAUSE");
490     g_string_append_printf (cmd_replay, "\r\n");
491     g_string_append_printf (cmd_replay, "MM_ERROR_NONE");
492     proxy_write(client, g_string_free (cmd_replay, FALSE));
493     debug_log ("STATE QUERY..demon return\n\n");
494   }
495   else if (strstr(cmd, "WFD_PROXY_RESUME"))
496   {
497     debug_log ("RESUMING..\n\n");
498     wfd_resume();
499     cmd_replay = g_string_new ("");
500     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_RESUME");
501     g_string_append_printf (cmd_replay, "\r\n");
502     g_string_append_printf (cmd_replay, "MM_ERROR_NONE");
503     proxy_write(client, g_string_free (cmd_replay, FALSE));
504     debug_log ("STATE QUERY..demon return\n\n");
505   }
506   else if (strstr(cmd, "WFD_PROXY_STOP"))
507   {
508     debug_log ("STOPPING..\n\n");
509     wfd_stop();
510     cmd_replay = g_string_new ("");
511     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_STOP");
512     g_string_append_printf (cmd_replay, "\r\n");
513     g_string_append_printf (cmd_replay, "MM_ERROR_NONE");
514     proxy_write(client, g_string_free (cmd_replay, FALSE));
515     debug_log ("STATE QUERY..demon return\n\n");
516   }
517   else if (strstr(cmd, "WFD_PROXY_DESTROY"))
518   {
519     WFDServer *lserver = (WFDServer*) client->parent;
520     debug_log ("DESTROY..\n\n");
521     cmd_replay = g_string_new ("");
522     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_DESTROY");
523     g_string_append_printf (cmd_replay, "\r\n");
524     g_string_append_printf (cmd_replay, "MM_ERROR_NONE");
525     proxy_write(client, g_string_free (cmd_replay, FALSE));
526     debug_log ("STATE QUERY..demon return\n\n");
527     quit_pushing = TRUE;
528     shutdown (lserver->sockfd, SHUT_RDWR);
529     debug_log("interpret calll quit_program()");
530     quit_program();
531   }
532   else if (strstr(cmd, "WFD_PROXY_STATE_QUERY"))
533   {
534     debug_log ("STATE QUERY..\n\n");
535     cmd_replay = g_string_new ("");
536     g_string_append_printf (cmd_replay, "REPLAY WFD_PROXY_STATE_QUERY");
537     g_string_append_printf (cmd_replay, "\r\n");
538     g_string_append_printf (cmd_replay, convert_state_to_string(g_current_state));
539     proxy_write(client, g_string_free (cmd_replay, FALSE));
540     debug_log ("STATE QUERY..demon return\n\n");
541   }
542   else
543   {
544     debug_log("unknown menu \n");
545   }
546 }
547
548 gboolean input (GIOChannel *channel, GIOCondition condition, gpointer data)
549 {
550   WFDClient *client = (WFDClient*)data;
551   gsize read;
552   GError *error = NULL;
553   if(condition & G_IO_IN) {
554     memset(&(client->inbuff),0,sizeof(client->inbuff));
555     g_io_channel_read(client->channel, client->inbuff, sizeof(client->inbuff), &read);
556     if(read)
557     {
558       client->inbuff[read] = '\0';
559       g_strstrip(client->inbuff);
560       debug_log("got command %s\n", client->inbuff);
561       if(!g_str_has_prefix(client->inbuff,"WFD")) goto client_cleanup;
562       interpret (client, client->inbuff);
563       debug_log("command return \n");
564     }
565     else goto client_cleanup;
566   }
567   if (condition & G_IO_HUP) goto client_cleanup;
568   return TRUE;
569 client_cleanup:
570   {
571     WFDServer *lserver = (WFDServer *)client->parent;
572     client->inactive = TRUE;
573     debug_log("client connection closed \n");
574     shutdown (client->connfd, SHUT_RDWR);
575     g_io_channel_shutdown(client->channel, TRUE, &error);
576     debug_log("IO channel closed \n");
577     if(g_list_length(lserver->clients))
578     {
579       lserver->clients = g_list_remove(lserver->clients, client);
580     }
581     free(client);
582     debug_log("client removed from list \n");
583   }
584   return TRUE;
585 }
586
587 static void* wfd_server_thread_function(void * asrc)
588 {
589   int  newsockfd;
590   int i=0;
591   int nSockOpt = 1;
592   WFDServer *pserver = (WFDServer *)asrc;
593   pserver->sockfd = socket(AF_INET, SOCK_STREAM, 0);
594   debug_log("wfd_proxy_initialize socke is %d", pserver->sockfd);
595   if (pserver->sockfd < 0)
596   {
597     debug_log("ERROR opening socket");
598     goto cleanup;
599   }
600   debug_log("wfd_proxy_initialize get socket created\n");
601   bzero((char *) &pserver->serv_addr, sizeof(pserver->serv_addr));
602   pserver->portno = WFD_PROXY_COMM_PORT;
603   pserver->serv_addr.sin_family = AF_INET;
604   pserver->serv_addr.sin_addr.s_addr = INADDR_ANY;
605   pserver->serv_addr.sin_port = htons(pserver->portno);
606   debug_log("serv_addr = %p sizeof %d ", (struct sockaddr *) &pserver->serv_addr, sizeof(pserver->serv_addr));
607   setsockopt(pserver->sockfd, SOL_SOCKET, SO_REUSEADDR, &nSockOpt, sizeof(nSockOpt));
608   if (bind(pserver->sockfd, (struct sockaddr *) &pserver->serv_addr, sizeof(pserver->serv_addr)) < 0)
609   {
610     debug_log("ERROR on binding");
611     goto cleanup;
612   }
613   debug_log("wfd_proxy_initialize get socket bind\n");
614   if (listen(pserver->sockfd,5) <0)
615   {
616     debug_log("ERROR on socket listen");
617     goto cleanup;
618   }
619   wfd_set_signal();
620   MMTA_INIT();
621
622   while(!quit_pushing)
623   {
624     socklen_t clilen = {0};
625     struct sockaddr_in cli_addr = {0};
626     WFDClient *client = NULL;
627     clilen = sizeof(cli_addr);
628     debug_log("wfd_proxy_initialize waiting for accept \n");
629     newsockfd = accept(pserver->sockfd, (struct sockaddr *) &cli_addr, &clilen);
630     if (newsockfd < 0) {
631       debug_log("ERROR on accept");
632       continue;
633     }
634     debug_log("wfd_proxy_initialize get socket accept \n");
635     client = g_malloc(sizeof(WFDClient));
636     if(!client)
637     {
638       debug_log("client malloc failed. out of memory");
639       continue;
640     }
641     client->connfd = newsockfd;
642     socket_id = newsockfd;
643     client->lock = g_mutex_new ();
644     client->inactive = 0;
645     client->parent = pserver;
646     client->channel = g_io_channel_unix_new(client->connfd);
647     g_io_add_watch(client->channel, G_IO_IN|G_IO_HUP|G_IO_ERR, (GIOFunc)input, client);
648     pserver->clients = g_list_prepend(pserver->clients, client);
649   }
650 cleanup:
651   debug_log("wfd_server_thread_function cleanup \n");
652   for(i=0;i<g_list_length(pserver->clients); i++)
653   {
654     GError *error = NULL;
655     WFDClient *tempclient = (WFDClient *)g_list_nth_data(pserver->clients,i);
656     if(tempclient) {
657       tempclient->inactive = TRUE;
658       shutdown (tempclient->connfd, SHUT_RDWR);
659       g_io_channel_shutdown(tempclient->channel, TRUE, &error);
660       pserver->clients = g_list_remove(pserver->clients, tempclient);
661       free(tempclient);
662     }
663   }
664   g_list_free(pserver->clients);
665   shutdown (pserver->sockfd, SHUT_RDWR);
666   debug_log("wfd_server_thread_function calll quit_program()");
667   quit_program();
668
669   debug_log("wfd_server_thread_function THREAD EXIT \n");
670
671   return NULL;
672 }
673
674 static gboolean __func1(void *data)
675 {
676   GError *error = NULL;
677   WFDServer *server = (WFDServer *)data;
678   debug_log("__func1 enter \n");
679   server->thread = g_thread_create ((GThreadFunc) wfd_server_thread_function, server, TRUE, &error);
680   server->clients = g_list_alloc ();
681   debug_log("__func1 exit \n");
682   return FALSE;
683 }
684
685 static bool __wfd_server_setup(void* pserver)
686 {
687   WFDServer *server = (WFDServer *)pserver;
688   DBusGConnection *conn = NULL;
689   GObject *object = NULL;
690   DBusGObjectInfo dbus_glib_wfd_server_object_info;
691   GError *err = NULL;
692   DBusError derr;
693   int ret = 0;
694   debug_log("__wfd_server_setup start\n");
695   dbus_g_object_type_install_info(WFD_SERVER_TYPE_OBJECT,
696     &dbus_glib_wfd_server_object_info);
697
698   conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, &err);
699   if (NULL == conn) {
700     debug_log("Unable to get dbus!");
701     return false;
702   }
703   dbus_error_init(&derr);
704
705   ret = dbus_bus_request_name(dbus_g_connection_get_connection(conn),
706     "com.samsung.wfd.server",
707     DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &derr);
708
709   if (dbus_error_is_set(&derr)) {
710     debug_log("dbus_bus_request_name failed with error:%s", derr.message);
711     dbus_error_free(&derr);
712     return false;
713   }
714   object = g_object_new(WFD_SERVER_TYPE_OBJECT, NULL);
715   if (NULL == object) {
716     debug_log("Unable to create new object");
717     return false;
718   }
719   dbus_g_connection_register_g_object(conn, "/com/samsung/wfd/server", G_OBJECT(object));
720   g_idle_add(__func1, server);
721   debug_log("__wfd_server_setup end\n");
722   return true;
723 }
724 void wfd_signal_handler(int signo) {
725   quit_pushing = TRUE;
726   debug_log("wfd_signal_handler calll quit_program()");
727   shutdown (socket_id, SHUT_RDWR);
728   quit_program();
729   exit(1);
730 }
731
732 static void wfd_set_signal()
733 {
734
735   /*refer to signal.h*/
736   /*SIGABRT  A  Process abort signal.
737      SIGALRM   T  Alarm clock.
738      SIGBUS    A  Access to an undefined portion of a memory object.
739      SIGCHLD  I  Child process terminated, stopped,
740      SIGCONT  C  Continue executing, if stopped.
741      SIGFPE  A  Erroneous arithmetic operation.
742      SIGHUP  T  Hangup.
743      SIGILL  A  Illegal instruction.
744      SIGINT  T  Terminal interrupt signal.
745      SIGKILL  T  Kill (cannot be caught or ignored).
746      SIGPIPE  T  Write on a pipe with no one to read it.
747      SIGQUIT  A  Terminal quit signal.
748      SIGSEGV  A  Invalid memory reference.
749      SIGSTOP  S  Stop executing (cannot be caught or ignored).
750      SIGTERM  T  Termination signal.
751      SIGTSTP  S  Terminal stop signal.
752      SIGTTIN  S  Background process attempting read.
753      SIGTTOU  S  Background process attempting write.
754      SIGUSR1  T  User-defined signal 1.
755      SIGUSR2  T  User-defined signal 2.
756      SIGPOLL  T  Pollable event.
757      SIGPROF  T  Profiling timer expired.
758      SIGSYS  A  Bad system call.
759      SIGTRAP  A  Trace/breakpoint trap.
760      SIGURG  I  High bandwidth data is available at a socket.
761      SIGVTALRM  T  Virtual timer expired.
762      SIGXCPU  A  CPU time limit exceeded.
763      SIGXFSZ  A  File size limit exceeded.
764
765 The default actions are as follows:
766 T : Abnormal termination of the process. The process is terminated with all the consequences of _exit()
767 A : Abnormal termination of the process.
768 I  : Ignore the signal.
769 S : Stop the process
770 */
771   struct sigaction act_new;
772   memset (&act_new, 0, sizeof (struct sigaction));
773
774   act_new.sa_handler = wfd_signal_handler;
775
776   sigaction(SIGABRT, &act_new, NULL);
777   sigaction(SIGBUS, &act_new, NULL);
778   sigaction(SIGINT, &act_new, NULL); //
779   sigaction(SIGKILL, &act_new, NULL);
780   sigaction(SIGPIPE, &act_new, NULL);
781   sigaction(SIGQUIT, &act_new, NULL); //
782   sigaction(SIGSEGV, &act_new, NULL);
783   sigaction(SIGTERM, &act_new, NULL); //
784   sigaction(SIGSYS, &act_new, NULL);
785
786 }
787
788
789
790 static void wifi_direct_state_change_cb(keynode_t *key, void *data)
791 {
792   WFDServer *server = (WFDServer *)data;
793   if(!server) return;
794   debug_log("wifi direct state changed");
795   int state = -1;
796   state = vconf_keynode_get_int(key);
797   if (state < VCONFKEY_WIFI_DIRECT_GROUP_OWNER) {
798     debug_log("wifi disconnected");
799     quit_pushing = TRUE;
800     shutdown (server->sockfd, SHUT_RDWR);
801     debug_log("wifi_direct_state_change_cb calll quit_program()");
802     quit_program();
803   }
804 }
805
806 static void lcd_state_change_cb(keynode_t *key, void *data)
807 {
808   WFDServer *server = (WFDServer *)data;
809   if(!server)
810     return;
811   int state = -1;
812   state = vconf_keynode_get_int(key);
813   if (state == VCONFKEY_PM_STATE_NORMAL) {
814     debug_log("source has woke up time to wake-up-sink");
815     wfd_resume();
816   }
817   else if(state == VCONFKEY_PM_STATE_LCDOFF || VCONFKEY_PM_STATE_SLEEP) {
818     debug_log("source is sleeping time to go to sleep");
819     wfd_standby();
820   }
821 }
822
823 int main(int argc, char *argv[])
824 {
825   WFDServer server;
826   server.thread = NULL;
827   server.clients = NULL;
828
829   g_loop = g_main_loop_new(NULL, FALSE);
830   if(NULL == g_loop) {
831     debug_log("Unable to create gmain loop! Aborting wfd server\n");
832     exit(-1);
833   }
834   g_type_init();
835   debug_log("wfd_proxy_initialize\n");
836   if (!__wfd_server_setup(&server)) {
837     debug_log("Unable to initialize test server\n");
838     exit(-1);
839   }
840   debug_log("set vconf_notify_key_changed about wifi direct");
841   if (0 != vconf_notify_key_changed(VCONFKEY_WIFI_DIRECT_STATE, wifi_direct_state_change_cb, &server)) {
842     debug_log("vconf_notify_key_changed() failed");
843   }
844   debug_log("set vconf_notify_key_changed about LCD state");
845   if (0 != vconf_notify_key_changed(VCONFKEY_PM_STATE, lcd_state_change_cb, &server)) {
846     debug_log("vconf_notify_key_changed() failed");
847   }
848   debug_log("wfd_proxy_initialize run loop \n");
849   g_main_loop_run(g_loop);
850   g_thread_join(server.thread);
851   shutdown (server.sockfd, SHUT_RDWR);
852   debug_log("WFD SERVER EXIT \n");
853   exit(0);
854 }