Separate monitoring function plugin
[platform/core/connectivity/stc-manager.git] / src / stc-manager-plugin-pcap.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-manager-plugin-pcap.h"
21
22 static gboolean stc_plugin_enabled = FALSE;
23 static void *handle_plugin;
24 static stc_plugin_pcap_s *stc_plugin;
25
26 //LCOV_EXCL_START
27 int stc_plugin_pcap_init(void)
28 {
29         __STC_LOG_FUNC_ENTER__;
30
31         handle_plugin = dlopen(STC_PLUGIN_PCAP_FILEPATH, RTLD_NOW);
32         if (!handle_plugin) {
33                 STC_LOGE("Can't load %s: %s", STC_PLUGIN_PCAP_FILEPATH, dlerror());
34                 __STC_LOG_FUNC_EXIT__;
35                 return STC_ERROR_UNINITIALIZED;
36         }
37
38         stc_plugin = dlsym(handle_plugin, "stc_plugin_pcap");
39         if (!stc_plugin) {
40                 STC_LOGE("Can't load symbol: %s", dlerror());
41                 dlclose(handle_plugin);
42                 __STC_LOG_FUNC_EXIT__;
43                 return STC_ERROR_UNINITIALIZED;
44         }
45
46         stc_plugin->initialize_plugin();
47         stc_plugin_enabled = TRUE;
48
49         __STC_LOG_FUNC_EXIT__;
50         return STC_ERROR_NONE;
51 }
52
53 int stc_plugin_pcap_deinit(void)
54 {
55         __STC_LOG_FUNC_ENTER__;
56
57         if (!stc_plugin_enabled) {
58                 __STC_LOG_FUNC_EXIT__;
59                 return STC_ERROR_UNINITIALIZED;
60         }
61
62         stc_plugin->deinitialize_plugin();
63         stc_plugin_enabled = FALSE;
64         dlclose(handle_plugin);
65
66         __STC_LOG_FUNC_EXIT__;
67         return STC_ERROR_NONE;
68 }
69
70 int stc_plugin_pcap_lookup_dev(void)
71 {
72         if (!stc_plugin_enabled) {
73                 if (STC_DEBUG_LOG)
74                         STC_LOGE("Plugin wasn't enabled");
75                 return STC_ERROR_UNINITIALIZED;
76         }
77
78         if (!stc_plugin) {
79                 if (STC_DEBUG_LOG)
80                         STC_LOGE("Plugin wasn't loaded");
81                 return STC_ERROR_UNINITIALIZED;
82         }
83
84         return stc_plugin->lookup_dev();
85 }
86
87 int stc_plugin_pcap_lookup_net(void)
88 {
89         if (!stc_plugin_enabled) {
90                 if (STC_DEBUG_LOG)
91                         STC_LOGE("Plugin wasn't enabled");
92                 return STC_ERROR_UNINITIALIZED;
93         }
94
95         if (!stc_plugin) {
96                 if (STC_DEBUG_LOG)
97                         STC_LOGE("Plugin wasn't loaded");
98                 return STC_ERROR_UNINITIALIZED;
99         }
100
101         return stc_plugin->lookup_net();
102 }
103
104 int stc_plugin_pcap_find_alldevs(void)
105 {
106         if (!stc_plugin_enabled) {
107                 if (STC_DEBUG_LOG)
108                         STC_LOGE("Plugin wasn't enabled");
109                 return STC_ERROR_UNINITIALIZED;
110         }
111
112         if (!stc_plugin) {
113                 if (STC_DEBUG_LOG)
114                         STC_LOGE("Plugin wasn't loaded");
115                 return STC_ERROR_UNINITIALIZED;
116         }
117
118         return stc_plugin->find_alldevs();
119 }
120
121 int stc_plugin_pcap_register_loop_pcap(const char *ifname,
122                         int group)
123 {
124         if (!stc_plugin_enabled) {
125                 if (STC_DEBUG_LOG)
126                         STC_LOGE("Plugin wasn't enabled");
127                 return STC_ERROR_UNINITIALIZED;
128         }
129
130         if (!stc_plugin) {
131                 if (STC_DEBUG_LOG)
132                         STC_LOGE("Plugin wasn't loaded");
133                 return STC_ERROR_UNINITIALIZED;
134         }
135
136         return stc_plugin->register_loop_pcap(ifname, group);
137 }
138
139 int stc_plugin_pcap_unregister_loop_pcap(const char *ifname,
140                         int group)
141 {
142         if (!stc_plugin_enabled) {
143                 if (STC_DEBUG_LOG)
144                         STC_LOGE("Plugin wasn't enabled");
145                 return STC_ERROR_UNINITIALIZED;
146         }
147
148         if (!stc_plugin) {
149                 if (STC_DEBUG_LOG)
150                         STC_LOGE("Plugin wasn't loaded");
151                 return STC_ERROR_UNINITIALIZED;
152         }
153
154         return stc_plugin->unregister_loop_pcap(ifname, group);
155 }
156 //LCOV_EXCL_STOP