2 * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "vine-data-path.h"
22 #include "vine-disc.h"
24 #include "vine-event-loop.h"
26 #include "vine-private.h"
27 #include "vine-utils.h"
29 static int __vine_ref_count = 0;
30 static pthread_mutex_t __vine_mutex = PTHREAD_MUTEX_INITIALIZER;
32 static bool __vine_is_initialized()
34 return __vine_ref_count > 0;
37 static int __vine_ref()
40 VINE_LOGD("__vine_ref_count: %d", __vine_ref_count);
41 return __vine_ref_count;
44 static int __vine_unref()
47 VINE_LOGD("__vine_ref_count: %d", __vine_ref_count);
48 return __vine_ref_count;
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;
62 if (vine_data_path_init()) {
63 VINE_LOGE("Fail to init vine data path");
65 VINE_UNLOCK(&__vine_mutex);
66 return VINE_ERROR_OPERATION_FAILED;
69 if (vine_event_loop_init()) {
70 VINE_LOGE("Fail to init vine event loop");
71 vine_data_path_deinit();
73 VINE_UNLOCK(&__vine_mutex);
74 return VINE_ERROR_OPERATION_FAILED;
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();
82 VINE_UNLOCK(&__vine_mutex);
83 return VINE_ERROR_OPERATION_FAILED;
88 VINE_UNLOCK(&__vine_mutex);
89 return VINE_ERROR_NONE;
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;
102 if (__vine_unref() == 0) {
103 vine_event_loop_stop();
104 vine_event_loop_deinit();
105 vine_data_path_deinit();
108 VINE_UNLOCK(&__vine_mutex);
109 return VINE_ERROR_NONE;
112 int _vine_get_capabilities(int type, int *capabilities)
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");
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;
126 return VINE_ERROR_NONE;