Add unit tests for VC manager APIs
[platform/core/uifw/voice-control.git] / server / vcd_server_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 "vcd_main.h"
25 #include "vcd_server_data.h"
26
27 #define DATA_DEBUG
28
29 using namespace std;
30
31 std::list<vc_tts_text_data_s*> g_tts_text_data;
32
33 static pthread_mutex_t g_tts_text_data_mutex = PTHREAD_MUTEX_INITIALIZER;
34
35
36 static int __data_show_text_list(void)
37 {
38         SLOG(LOG_DEBUG, TAG_VCD, "----- Text list -----");
39
40         if (!g_tts_text_data.empty()) {
41                 std::list<vc_tts_text_data_s*>::iterator iter;
42                 for (iter = g_tts_text_data.begin(); (NULL != *iter && iter != g_tts_text_data.end()); ++iter) {
43                         SLOG(LOG_DEBUG, TAG_VCD, "[%p] uid(%u), pid(%d), utt_id(%d), text(%s), language(%s)",
44                                 *iter, (*iter)->uid, (*iter)->pid, (*iter)->utt_id, (*iter)->text, (*iter)->language);
45                 }
46         } else {
47                 SLOG(LOG_DEBUG, TAG_VCD, "No Text Data");
48         }
49
50         SLOG(LOG_DEBUG, TAG_VCD, "---------------------");
51         return VCD_ERROR_NONE;
52 }
53
54 int vcd_data_add_tts_text_data(vc_tts_text_data_s* data)
55 {
56         if (NULL == data) {
57                 SLOG(LOG_ERROR, TAG_VCD, "[DATA ERROR] feedback data is NULL");
58                 return VCD_ERROR_INVALID_PARAMETER;
59         }
60
61         /* mutex is locked */
62         pthread_mutex_lock(&g_tts_text_data_mutex);
63
64         std::list<vc_tts_text_data_s*>::iterator iter;
65
66         try {
67                 iter = g_tts_text_data.insert(g_tts_text_data.end(), data);
68         } catch (const std::bad_alloc&) {
69                 SLOG(LOG_ERROR, TAG_VCD, "[DATA][ERROR] Fail to insert tts_text_data (bad_alloc)");
70                 pthread_mutex_unlock(&g_tts_text_data_mutex);
71
72                 return VCD_ERROR_OUT_OF_MEMORY;
73         }
74         SLOG(LOG_ERROR, TAG_VCD, "[DATA][%p] uid(%u), pid(%d), utt_id(%d), text(%s), language(%s)",
75                 *iter, (*iter)->uid, (*iter)->pid, (*iter)->utt_id, (*iter)->text, (*iter)->language);
76
77 #ifdef DATA_DEBUG
78         __data_show_text_list();
79 #endif
80         pthread_mutex_unlock(&g_tts_text_data_mutex);
81
82         return VCD_ERROR_NONE;
83 }
84
85 int vcd_data_clear_tts_text_data(vc_tts_text_data_s** tts_text_data)
86 {
87         pthread_mutex_lock(&g_tts_text_data_mutex);
88
89         if (!g_tts_text_data.empty()) {
90                 if (NULL != *tts_text_data) {
91                         SLOG(LOG_DEBUG, TAG_VCD, "[DEBUG] uid(%u), pid(%d), utt_id(%d), text(%s), language(%s)",
92                                 (*tts_text_data)->uid, (*tts_text_data)->pid, (*tts_text_data)->utt_id, (*tts_text_data)->text, (*tts_text_data)->language);
93
94                         if (NULL != (*tts_text_data)->text) {
95                                 free((*tts_text_data)->text);
96                                 (*tts_text_data)->text = NULL;
97                         }
98                         if (NULL != (*tts_text_data)->language) {
99                                 free((*tts_text_data)->language);
100                                 (*tts_text_data)->language = NULL;
101                         }
102
103                         free(*tts_text_data);
104                         *tts_text_data = NULL;
105                 }
106         }
107
108         pthread_mutex_unlock(&g_tts_text_data_mutex);
109
110         return VCD_ERROR_NONE;
111 }
112
113 int vcd_data_get_tts_text_data(unsigned int tts_uid, vc_tts_text_data_s** data)
114 {
115         SLOG(LOG_INFO, TAG_VCD, "[DATA] Get tts text data : uid(%u)", tts_uid);
116
117         /* mutex is locked */
118         pthread_mutex_lock(&g_tts_text_data_mutex);
119
120         if (0 == g_tts_text_data.size()) {
121                 SLOG(LOG_ERROR, TAG_VCD, "[DATA ERROR] There is no tts_text data");
122                 *data = NULL;
123                 pthread_mutex_unlock(&g_tts_text_data_mutex);
124                 return VCD_ERROR_INVALID_PARAMETER;
125         }
126
127         std::list<vc_tts_text_data_s *>::iterator iter;
128         iter = g_tts_text_data.begin();
129         while (iter != g_tts_text_data.end()) {
130                 if (tts_uid == (*iter)->uid) {
131                         *data = *iter;
132                         iter = g_tts_text_data.erase(iter);
133                 } else {
134                         iter++;
135                 }
136         }
137
138 #ifdef DATA_DEBUG
139         __data_show_text_list();
140 #endif
141
142         pthread_mutex_unlock(&g_tts_text_data_mutex);
143
144         return VCD_ERROR_NONE;
145 }
146
147 int vcd_data_get_first_tts_text_data(vc_tts_text_data_s** data)
148 {
149         SLOG(LOG_INFO, TAG_VCD, "[DATA] Get first tts text data");
150
151 #ifdef DATA_DEBUG
152         __data_show_text_list();
153 #endif
154
155         /* mutex is locked */
156         pthread_mutex_lock(&g_tts_text_data_mutex);
157
158         if (0 == g_tts_text_data.size()) {
159                 SLOG(LOG_ERROR, TAG_VCD, "[DATA ERROR] There is no tts_text data");
160                 *data = NULL;
161                 pthread_mutex_unlock(&g_tts_text_data_mutex);
162                 return VCD_ERROR_INVALID_PARAMETER;
163         }
164
165         std::list<vc_tts_text_data_s*>::iterator iter;
166
167         if (!g_tts_text_data.empty()) {
168                 iter = g_tts_text_data.begin();
169                 *data = *iter;
170                 g_tts_text_data.pop_front();
171         }
172
173         pthread_mutex_unlock(&g_tts_text_data_mutex);
174
175         return VCD_ERROR_NONE;
176 }
177
178 int vcd_data_get_tts_text_data_size(void)
179 {
180         /* mutex is locked */
181         pthread_mutex_lock(&g_tts_text_data_mutex);
182         int size = g_tts_text_data.size();
183
184         SLOG(LOG_INFO, TAG_VCD, "[DATA] get feedback data size(%d)", size);
185
186         /* mutex is unlocked */
187         pthread_mutex_unlock(&g_tts_text_data_mutex);
188
189         return size;
190 }