app-comm-svc: add licenses for BSD-2-Clause/ISC/MIT
[platform/core/convergence/app-comm-svc.git] / main.cpp
1 /*
2  *
3  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 /*Product: Samsung NService Framework (remote-server)
20  * Version: 1.0
21  *
22  * Copyright 2013-2014 SWC, Samsung Electronics, Inc.
23  * All rights reserved.
24  */
25
26 /**
27  * @file        main.cpp
28  * @brief       This file is resposible to run the convergence manager as deamon.
29                 and initialize the RCR and convergence manager
30  * @created on: 2013.10.30
31  * @author: Shivaram Yadav (shivaram.y@samsung.com)
32  * @reviewer: Ji-Hoon Kim(jihoon95.kim@samsung.com), Han Kang (han264,kang@samsung.com)
33  */
34
35
36 #include <CCDebugRemote.h>
37
38 #include "IPCServer.h"
39 #include "MSFServer.h"
40 #include <stdio.h>
41 #include <signal.h>
42 #include <errno.h>
43 #include <glib.h>
44 #include <systemd/sd-daemon.h>
45 #include <sys/stat.h>
46 #include <wait.h>
47 #include <vconf.h>
48 #include "net_connection.h"
49
50 extern "C" {
51     #include <argos.h>
52 }
53
54 #define config_path_nservice "/opt/usr/apps/remote-server"
55 #define uri_nservice         "nservice/"
56
57 #define NOTIFY_TIME 15000       //watchdog notify time
58
59 #undef LOG_TAG
60 #define LOG_TAG "Remote_Daemon"
61
62
63 static GMainLoop*    mainloop = NULL;
64 IPCServer* ipc_server = NULL;
65 MSFServer* msf_server = NULL;
66 const char *service_name = "remote-server";
67
68 gboolean watchdog_notify_callback(gpointer data)
69 {
70         int ret = 0;
71         ret = aw_notify();
72
73         if(ret < 0)
74         {
75                 REMOTE_PRINT_WARN("smart_deadlock_watchdog_notify failed return = %d", ret);
76         }
77
78
79         REMOTE_PRINT_WARN("[sd_notify debug] called sd_notify() : ret = %d : Proc : %s(%d)", ret, service_name, (unsigned long)getpid());
80
81         return TRUE;
82 }
83
84 static void CheckWifiSecurityEncryption(connection_h connection_info)
85 {
86         //Check_Result (Wifi Check for Omission OK : 0, Not OK : 1)
87         //int Check_Result = 1;
88
89         REMOTE_PRINT_FATAL("CheckWifiSecurityEncryption START");
90
91         connection_h connection = connection_info;
92         connection_type_e connection_type;
93         connection_profile_h connection_profile;
94         connection_wifi_security_type_e connection_wifi_security_type;
95         connection_wifi_encryption_type_e connection_wifi_encryption_type;
96
97         if(connection == NULL)
98         {
99                 REMOTE_PRINT_FATAL("Failed to get Connection Profile");
100                 //pCMInstance->SetACLOmissionResult(Check_Result);
101                 return;
102         }
103
104         // Check WIFI & WPA2 & AES (If all those are applied to TV, Allow Omission Access Popup)
105         if(connection_get_type(connection, &connection_type) == CONNECTION_ERROR_NONE)
106         {
107                 if(connection_type == CONNECTION_TYPE_WIFI)
108                 {
109                         if(connection_get_current_profile(connection, &connection_profile) != CONNECTION_ERROR_NONE)
110                         {
111                                 REMOTE_PRINT_FATAL("Failed to get Connection Profile");
112                                 //pCMInstance->SetACLOmissionResult(Check_Result);
113                                 return;
114                         }
115
116                         if(connection_profile_get_wifi_security_type(connection_profile, &connection_wifi_security_type) != CONNECTION_ERROR_NONE)
117                         {
118                                 REMOTE_PRINT_DEBUG("Failed to get Wifi Security Type");
119                         }
120                         else if (connection_profile_get_wifi_encryption_type(connection_profile, &connection_wifi_encryption_type) != CONNECTION_ERROR_NONE)
121                         {
122                                 REMOTE_PRINT_DEBUG("Failed to get Wifi Encryption Type");
123                         }
124                         else if ( (connection_wifi_security_type == CONNECTION_WIFI_SECURITY_TYPE_WPA_PSK) &
125                                 ( (connection_wifi_encryption_type == CONNECTION_WIFI_ENCRYPTION_TYPE_AES) || (connection_wifi_encryption_type == CONNECTION_WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED)))
126                         {
127                                 //Check_Result = 0;
128                                 REMOTE_PRINT_DEBUG("Wifi Check for Omission is OK");
129                                 //pCMInstance->SetACLOmissionResult(Check_Result);
130                                 return;
131                         }
132                         else
133                         {
134                                 REMOTE_PRINT_DEBUG("Not Allow Ommission Of Access Popup : Because of Security type & Encryption type");
135                         }
136                 }
137         }
138         else
139         {
140                 REMOTE_PRINT_DEBUG("Failed to get Connection Type");
141         }
142         //pCMInstance->SetACLOmissionResult(Check_Result);
143         REMOTE_PRINT_FATAL("Wifi Check for Omission is Not OK");
144 }
145
146 void InitUpnpDevices()
147 {
148         //Init IPC server
149         ipc_server = new IPCServer();
150         ipc_server->Init();
151
152         msf_server = new MSFServer();
153         msf_server->Init();
154 }
155
156 static void Setup_CMDaemon()
157 {
158         InitUpnpDevices();
159
160         mainloop = g_main_loop_new(NULL, FALSE);
161 }
162
163 void getTvName_cb(keynode_t *key, void* data)
164 {
165         ipc_server->UpdateMITNode(); // update Node Server When TV Name is changed.
166 }
167
168 void NetworkIPChanged_cb(const char* ipv4_address, const char* ipv6_address, void* user_data)
169 {
170         if(ipv4_address != 0 && (strlen(ipv4_address) > 7))
171         {
172                 REMOTE_PRINT_DEBUG("UpdateMITNode request, IP_address : [%s]", ipv4_address);
173                 ipc_server->UpdateMITNode(); //update Node Server when IP is changed.
174
175                 CheckWifiSecurityEncryption(user_data);
176         }
177         else
178         {
179                 REMOTE_PRINT_DEBUG("There is no IP address");
180                 //pCMInstance->SetACLOmissionResult(1); //Check WIFI for omission is 'Not OK'(1)
181         }
182 }
183
184 int main(void)
185 {
186         REMOTE_PRINT_MAJOR("remote server main");
187         try
188         {
189                 Setup_CMDaemon();
190
191                 vconf_notify_key_changed("db/menu/network/devicename/tv_name", getTvName_cb, NULL);
192
193                 if(mainloop != NULL)
194                 {
195                         connection_h connection = NULL;
196                         int ret = -1;
197                         ret = connection_create(&connection);
198                         if(ret == CONNECTION_ERROR_NONE)
199                         {
200 //                              connection_set_type_changed_cb(connection, NetworkTypeChanged_cb, NULL);
201                                 CheckWifiSecurityEncryption(connection);
202                                 connection_set_ip_address_changed_cb(connection, NetworkIPChanged_cb, connection);
203                         }
204                         else
205                         {
206                                 REMOTE_PRINT_DEBUG("Fail to create connection_set_type_changed_cb\n");
207                         }
208                         //REMOTE_PRINT_DEBUG("Start Convergence Manager!");
209                         ret = aw_register(60);
210
211                         if (ret < 0) {
212                                 REMOTE_PRINT_DEBUG("Fail to register argos watchdog. ret is (%d)\n", ret);
213                         } else {
214                                 REMOTE_PRINT_DEBUG("Success to register argos watchdog. ret is (%d)\n", ret);
215
216                                 g_timeout_add(NOTIFY_TIME, watchdog_notify_callback, mainloop);
217                         }
218
219                         // Run GMainLoop
220                         REMOTE_PRINT_DEBUG("remote-server mainloop Start");
221                         g_main_loop_run(mainloop);
222                         g_main_loop_unref(mainloop);
223                         connection_destroy(connection);
224                 }
225                 else
226                 {
227                         REMOTE_PRINT_WARN("Fail to start Convergence Manager!");
228                 }
229
230                 vconf_ignore_key_changed("db/menu/network/devicename/tv_name", getTvName_cb);
231         }
232         catch(...)
233         {
234         }
235         return 0;
236 }