Add initial source codes for gtest
[platform/core/connectivity/stc-manager.git] / src / stc-manager-plugin.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dlfcn.h>
18
19 #include "stc-manager.h"
20 #include "stc-plugin.h"
21 #include "stc-manager-plugin.h"
22
23 static gboolean stc_plugin_enabled = FALSE;
24 static void *handle_plugin;
25 static stc_plugin_s *stc_plugin;
26
27 //LCOV_EXCL_START
28 void stc_manager_plugin_init(void)
29 {
30         __STC_LOG_FUNC_ENTER__;
31
32         handle_plugin = dlopen(STC_PLUGIN_FILEPATH, RTLD_NOW);
33         if (!handle_plugin) {
34                 STC_LOGE("Can't load %s: %s", STC_PLUGIN_FILEPATH, dlerror());
35                 __STC_LOG_FUNC_EXIT__;
36                 return;
37         }
38
39         stc_plugin = dlsym(handle_plugin, "stc_plugin");
40         if (!stc_plugin) {
41                 STC_LOGE("Can't load symbol: %s", dlerror());
42                 dlclose(handle_plugin);
43                 __STC_LOG_FUNC_EXIT__;
44                 return;
45         }
46
47         stc_plugin_enabled = TRUE;
48         __STC_LOG_FUNC_EXIT__;
49 }
50
51 void stc_manager_plugin_deinit(void)
52 {
53         __STC_LOG_FUNC_ENTER__;
54
55         if (!stc_plugin_enabled) {
56                 __STC_LOG_FUNC_EXIT__;
57                 return;
58         }
59
60         stc_plugin_enabled = FALSE;
61         dlclose(handle_plugin);
62         __STC_LOG_FUNC_EXIT__;
63 }
64
65 int stc_send_warn_message_to_net_popup(const char *content, const char *type,
66                                        const char *app_id, const char *iftype,
67                                        const char *warn)
68 {
69         __STC_LOG_FUNC_ENTER__;
70
71         if (!stc_plugin_enabled) {
72                 STC_LOGE("Plugin wasn't enabled");
73                 __STC_LOG_FUNC_EXIT__;
74                 return 0;
75         }
76
77         if (!stc_plugin) {
78                 STC_LOGE("Plugin wasn't loaded");
79                 __STC_LOG_FUNC_EXIT__;
80                 return 0;
81         }
82
83         __STC_LOG_FUNC_EXIT__;
84         return stc_plugin->send_warn_message_to_net_popup(content, type, app_id,
85                                                           iftype, warn);
86 }
87
88 int stc_send_restriction_message_to_net_popup(const char *content,
89                                               const char *type,
90                                               const char *app_id,
91                                               const char *iftype,
92                                               const char *limit)
93 {
94         __STC_LOG_FUNC_ENTER__;
95
96         if (!stc_plugin_enabled) {
97                 STC_LOGE("Plugin wasn't enabled");
98                 __STC_LOG_FUNC_EXIT__;
99                 return 0;
100         }
101
102         if (!stc_plugin) {
103                 STC_LOGE("Plugin wasn't loaded");
104                 __STC_LOG_FUNC_EXIT__;
105                 return 0;
106         }
107
108         __STC_LOG_FUNC_EXIT__;
109         return stc_plugin->send_restriction_message_to_net_popup(content, type,
110                                                                  app_id, iftype,
111                                                                  limit);
112 }
113
114 int stc_register_state_changed_cb(stc_s *stc,
115                                   stc_plugin_app_state_changed_cb cb,
116                                   void *data)
117 {
118         __STC_LOG_FUNC_ENTER__;
119
120         if (!stc_plugin_enabled) {
121                 STC_LOGE("Plugin wasn't enabled");
122                 __STC_LOG_FUNC_EXIT__;
123                 return 0;
124         }
125
126         if (!stc_plugin) {
127                 STC_LOGE("Plugin wasn't loaded");
128                 __STC_LOG_FUNC_EXIT__;
129                 return 0;
130         }
131
132         __STC_LOG_FUNC_EXIT__;
133         return stc_plugin->register_state_changed_cb(stc, cb, data);
134 }
135 int stc_deregister_state_changed_cb(stc_s *stc)
136 {
137         __STC_LOG_FUNC_ENTER__;
138
139         if (!stc_plugin_enabled) {
140                 STC_LOGE("Plugin wasn't enabled");
141                 __STC_LOG_FUNC_EXIT__;
142                 return 0;
143         }
144
145         if (!stc_plugin) {
146                 STC_LOGE("Plugin wasn't loaded");
147                 __STC_LOG_FUNC_EXIT__;
148                 return 0;
149         }
150
151         __STC_LOG_FUNC_EXIT__;
152         return stc_plugin->deregister_state_changed_cb(stc);
153 }
154 //LCOV_EXCL_STOP