Update version to 1.70.7
[platform/core/uifw/voice-control.git] / client / vc_data.cpp
1 /*
2 * Copyright (c) 2018 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 #include <list>
18 #include <vector>
19 #include <pthread.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <dlog.h>
23
24 #include "vc_data.h"
25
26 using namespace std;
27
28 std::list<vc_tts_data_s*> g_tts_data;
29
30 static pthread_mutex_t g_tts_data_mutex = PTHREAD_MUTEX_INITIALIZER;
31
32 int vc_data_add_tts_data(vc_tts_data_s* data)
33 {
34         if (NULL == data) {
35                 SLOG(LOG_ERROR, TAG_VCC, "[DATA ERROR] tts data is NULL");
36                 return VC_ERROR_INVALID_PARAMETER;
37         }
38
39         pthread_mutex_lock(&g_tts_data_mutex);
40
41         try {
42                 g_tts_data.push_back(data);
43         } catch (const std::bad_alloc&) {
44                 SLOG(LOG_ERROR, TAG_VCC, "[DATA ERROR] Fail to insert tts data (bad alloc)");
45                 pthread_mutex_unlock(&g_tts_data_mutex);
46
47                 return VC_ERROR_OUT_OF_MEMORY;
48         }
49
50         pthread_mutex_unlock(&g_tts_data_mutex);
51
52         return VC_ERROR_NONE;
53 }
54
55 int vc_data_get_first_tts_data(vc_tts_data_s** data)
56 {
57         if (NULL == data) {
58                 SLOG(LOG_ERROR, TAG_VCC, "[DATA ERROR] tts data is NULL");
59                 return VC_ERROR_INVALID_PARAMETER;
60         }
61
62         SLOG(LOG_DEBUG, TAG_VCC, "[DATA] tts_data : %p", *data);
63
64         pthread_mutex_lock(&g_tts_data_mutex);
65         if (g_tts_data.empty()) {
66                 SLOG(LOG_DEBUG, TAG_VCC, "[DATA] There is no tts data");
67                 *data = NULL;
68                 pthread_mutex_unlock(&g_tts_data_mutex);
69                 return VC_ERROR_OPERATION_FAILED;
70         }
71
72         *data = g_tts_data.front();
73         g_tts_data.pop_front();
74
75         pthread_mutex_unlock(&g_tts_data_mutex);
76
77         return VC_ERROR_NONE;
78 }
79
80 int vc_data_get_tts_data_size()
81 {
82         int data_size = 0;
83
84         pthread_mutex_lock(&g_tts_data_mutex);
85         data_size = g_tts_data.size();
86
87         pthread_mutex_unlock(&g_tts_data_mutex);
88
89         return data_size;
90 }
91
92 int vc_data_clear_tts_data(vc_tts_data_s** data)
93 {
94         SLOG(LOG_DEBUG, TAG_VCC, "[DATA] clear tts data");
95
96         if (NULL == *data) {
97                 SLOG(LOG_ERROR, TAG_VCC, "[DATA ERROR] tts data is NULL");
98                 return VC_ERROR_INVALID_PARAMETER;
99         }
100
101         pthread_mutex_lock(&g_tts_data_mutex);
102
103         vc_tts_data_s *temp = *data;
104         SLOG(LOG_DEBUG, TAG_VCC, "[DEBUG] event(%d) data(%p) size(%d)", temp->event, temp->data, temp->data_size);
105
106         if (temp->data)
107                 free(temp->data);
108         temp->data = NULL;
109
110         if (temp)
111                 free(temp);
112         temp = NULL;
113
114         *data = NULL;
115
116         pthread_mutex_unlock(&g_tts_data_mutex);
117
118         return VC_ERROR_NONE;
119 }
120
121 int vc_data_clear_tts_data_by_uttid(int utt_id)
122 {
123         SLOG(LOG_DEBUG, TAG_VCC, "[DATA] clear tts data by utt_id(%d)", utt_id);
124
125         pthread_mutex_lock(&g_tts_data_mutex);
126
127         if (g_tts_data.empty()) {
128                 SLOG(LOG_ERROR, TAG_VCC, "[DATA ERROR] There is no tts data");
129                 pthread_mutex_unlock(&g_tts_data_mutex);
130                 return VC_ERROR_EMPTY;
131         }
132
133         std::list<vc_tts_data_s*>::iterator iter;
134         iter = g_tts_data.begin();
135         while (iter != g_tts_data.end()) {
136                 vc_tts_data_s *temp = nullptr;
137                 temp = *iter;
138                 if (nullptr != temp && utt_id == temp->utt_id) {
139                         iter = g_tts_data.erase(iter);
140
141                         if (temp->data)
142                                 free(temp->data);
143                         temp->data = nullptr;
144                         free(temp);
145                         temp = nullptr;
146                 } else {
147                         iter++;
148                 }
149         }
150
151         pthread_mutex_unlock(&g_tts_data_mutex);
152
153         return VC_ERROR_NONE;
154 }