Release Vine 1.0.0
[platform/core/api/vine.git] / src / vine-private.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
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
18 #include <stdbool.h>
19
20 #include "vine.h"
21 #include "vine-data-path.h"
22 #include "vine-disc.h"
23 #include "vine-dp.h"
24 #include "vine-event-loop.h"
25 #include "vine-log.h"
26 #include "vine-private.h"
27 #include "vine-utils.h"
28
29 static int __vine_ref_count = 0;
30 static pthread_mutex_t __vine_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 static bool __vine_is_initialized()
33 {
34         return __vine_ref_count > 0;
35 }
36
37 static int __vine_ref()
38 {
39         __vine_ref_count++;
40         VINE_LOGD("__vine_ref_count: %d", __vine_ref_count);
41         return __vine_ref_count;
42 }
43
44 static int __vine_unref()
45 {
46         __vine_ref_count--;
47         VINE_LOGD("__vine_ref_count: %d", __vine_ref_count);
48         return __vine_ref_count;
49 }
50
51 int _vine_init()
52 {
53         VINE_LOGD("_vine_init");
54         VINE_LOCK(&__vine_mutex);
55         if (!__vine_is_initialized()) {
56                 if (vine_disc_init()) {
57                         VINE_LOGE("Fail to init vine discovery");
58                         VINE_UNLOCK(&__vine_mutex);
59                         return VINE_ERROR_OPERATION_FAILED;
60                 }
61
62                 if (vine_data_path_init()) {
63                         VINE_LOGE("Fail to init vine data path");
64                         vine_disc_deinit();
65                         VINE_UNLOCK(&__vine_mutex);
66                         return VINE_ERROR_OPERATION_FAILED;
67                 }
68
69                 if (vine_event_loop_init()) {
70                         VINE_LOGE("Fail to init vine event loop");
71                         vine_data_path_deinit();
72                         vine_disc_deinit();
73                         VINE_UNLOCK(&__vine_mutex);
74                         return VINE_ERROR_OPERATION_FAILED;
75                 }
76
77                 if (vine_event_loop_start()) {
78                         VINE_LOGE("Fail to start vine event loop");
79                         vine_event_loop_deinit();
80                         vine_data_path_deinit();
81                         vine_disc_deinit();
82                         VINE_UNLOCK(&__vine_mutex);
83                         return VINE_ERROR_OPERATION_FAILED;
84                 }
85         }
86
87         __vine_ref();
88         VINE_UNLOCK(&__vine_mutex);
89         return VINE_ERROR_NONE;
90 }
91
92 int _vine_deinit()
93 {
94         VINE_LOGD("_vine_deinit");
95         VINE_LOCK(&__vine_mutex);
96         if (!__vine_is_initialized()) {
97                 VINE_LOGE("Not initialized");
98                 VINE_UNLOCK(&__vine_mutex);
99                 return VINE_ERROR_NOT_INITIALIZED;
100         }
101
102         if (__vine_unref() == 0) {
103                 vine_event_loop_stop();
104                 vine_event_loop_deinit();
105                 vine_data_path_deinit();
106                 vine_disc_deinit();
107         }
108         VINE_UNLOCK(&__vine_mutex);
109         return VINE_ERROR_NONE;
110 }
111
112 int _vine_get_capabilities(int type, int *capabilities)
113 {
114         RET_VAL_IF(type < VINE_CAPA_DISCOVERY_METHODS || type > VINE_CAPA_MAX_CONNECTIONS,
115                         VINE_ERROR_INVALID_PARAMETER, "Invalid type.");
116         RET_VAL_IF(capabilities == NULL, VINE_ERROR_INVALID_PARAMETER, "capabilities is NULL");
117
118         *capabilities = 0;
119         if (type == VINE_CAPA_DISCOVERY_METHODS) {
120                 if (vine_disc_is_plugin_loaded(VINE_DISCOVERY_METHOD_DNS_SD))
121                         *capabilities |= VINE_DISCOVERY_METHOD_DNS_SD;
122         } else if (type == VINE_CAPA_MAX_CONNECTIONS) {
123                 *capabilities = VINE_DP_MAX_CONNECTIONS_NUM;
124         }
125
126         return VINE_ERROR_NONE;
127 }