81154b4bddd8b30199b494097537889a824709c5
[platform/core/system/sync-agent.git] / src / framework / event / config.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the License);
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <glib/gprintf.h>
23
24 #include "utility/sync_util.h"
25 #include "event/config.h"
26
27 #ifndef SYNC_AGENT_LOG
28 #undef LOG_TAG
29 #define LOG_TAG "AF_EVENT"
30 #endif
31
32 static int current_event_count = 0;
33 static event_spec_s *event_spec_list[EVENT_MAX_EVENT_COUNT] = { 0 };
34
35 static int current_noti_bag_count = 0;
36 noti_spec_bag_s *noti_spec_bag_list[EVENT_MAX_NOTI_COUNT] = { 0 };
37
38 static char *__communication_path_event = NULL;
39
40 static int _find_noti_spec_bag(const char *noti_key);
41
42 static int _find_event_spec(int event_num);
43 static int _find_noti_spec(int noti_spec_bag_num, int noti_num);
44
45 const char *event_get_noti_path(const char *noti_key)
46 {
47         _EXTERN_FUNC_ENTER;
48
49         if (noti_key == NULL) {
50                 if (noti_spec_bag_list[0] != NULL) {
51                         return noti_spec_bag_list[0]->communication_path_noti;
52                 } else {
53                         return NULL;
54                 }
55         }
56
57         int index = _find_noti_spec_bag(noti_key);
58
59         _EXTERN_FUNC_EXIT;
60
61         return noti_spec_bag_list[index]->communication_path_noti;
62 }
63
64 const char *event_get_event_path()
65 {
66         _EXTERN_FUNC_ENTER;
67
68         _EXTERN_FUNC_EXIT;
69
70         return __communication_path_event;
71 }
72
73 sync_agent_event_error_e event_set_event_spec_from_config_file(const char *event_key, const char *config_file)
74 {
75         _EXTERN_FUNC_ENTER;
76
77         retvm_if(event_key == NULL, SYNC_AGENT_EVENT_FAIL, "event key is NULL !!");
78         retvm_if(config_file == NULL, SYNC_AGENT_EVENT_FAIL, "config file is NULL !!");
79
80         FILE *fp = fopen(config_file, "r");
81         if (fp == NULL) {
82                 _DEBUG_ERROR("file open failed [%s]\n", config_file);
83                 return SYNC_AGENT_EVENT_FAIL;
84         }
85
86         while (!feof(fp)) {
87                 event_spec_s *event_spec = (event_spec_s *) malloc(sizeof(event_spec_s));
88                 if (event_spec == NULL) {
89                         _DEBUG_ERROR("MALLOC failed !!!");
90                         if (fp != NULL)
91                                 fclose(fp);
92                         return SYNC_AGENT_EVENT_FAIL;
93                 }
94
95                 char description[1000];
96                 int fscanf_ret = fscanf(fp, "%d, %d, %d, %d, %999s\n", &(event_spec->event_num), (int *)(&(event_spec->event_type)), &(event_spec->relational_noti_num), &(event_spec->pendingtime), (char *)(&description));
97                 if (fscanf_ret == -1) {
98                         _DEBUG_ERROR("fscanf failed !!");
99                         if (fp != NULL)
100                                 fclose(fp);
101                         free(event_spec);
102                         return SYNC_AGENT_EVENT_FAIL;
103                 }
104
105                 event_spec_list[current_event_count] = event_spec;
106                 current_event_count++;
107         }
108
109         fclose(fp);
110
111         if (__communication_path_event == NULL) {
112                 int comm_path_event_len = strlen(EVENT_COMMUNICATION_PATH_EVENT) + strlen(event_key) + 1;
113                 int len = 0;
114                 __communication_path_event = (char *)calloc(comm_path_event_len, sizeof(char));
115
116                 if (__communication_path_event != NULL) {
117                         len = g_strlcat(__communication_path_event, EVENT_COMMUNICATION_PATH_EVENT, comm_path_event_len);
118                         len = g_strlcat(__communication_path_event, event_key, comm_path_event_len);
119
120                         if (len >= comm_path_event_len) {
121                                 _DEBUG_ERROR("__communication_path_event buffer overflow !!");
122                                 free(__communication_path_event);
123                                 return SYNC_AGENT_EVENT_FAIL;
124                         }
125
126                         _DEBUG_INFO("EVENT_COMMUNICATION_PATH_EVENT : %s", __communication_path_event);
127                 } else {
128                         _DEBUG_ERROR("__communication_path_event is NULL");
129                         return SYNC_AGENT_EVENT_FAIL;
130                 }
131         }
132
133         _EXTERN_FUNC_EXIT;
134
135         return SYNC_AGENT_EVENT_SUCCESS;
136 }
137
138 sync_agent_event_error_e event_clean_event_spec()
139 {
140         _EXTERN_FUNC_ENTER;
141
142         _DEBUG_INFO("Start: current_event_count = %d", current_event_count);
143         int i = 0;
144         for (; i < current_event_count; i++) {
145                 event_spec_s *event_spec = event_spec_list[i];
146                 if (event_spec != NULL) {
147                         free(event_spec);
148                 }
149
150                 event_spec_list[i] = 0;
151         }
152
153         current_event_count = 0;
154         _DEBUG_INFO("End: current_event_count = %d", current_event_count);
155
156         _EXTERN_FUNC_EXIT;
157
158         return SYNC_AGENT_EVENT_SUCCESS;
159 }
160
161 sync_agent_event_error_e event_set_noti_spec_from_config_file(const char *noti_key, const char *config_file)
162 {
163         _EXTERN_FUNC_ENTER;
164
165         if (noti_key == NULL || config_file == NULL) {
166                 return SYNC_AGENT_EVENT_FAIL;
167         }
168
169         FILE *fp = fopen(config_file, "r");
170         if (fp == NULL) {
171                 _DEBUG_ERROR("file open failed [%s]\n", config_file);
172                 return SYNC_AGENT_EVENT_FAIL;
173         }
174
175         /*
176          * Create Noti Spec Bag
177          */
178         noti_spec_bag_s *noti_spec_bag = (noti_spec_bag_s *) calloc(1, sizeof(noti_spec_bag_s));
179         if (noti_spec_bag == NULL) {
180                 _DEBUG_ERROR("CALLOC failed !!!");
181                 if (fp != NULL)
182                         fclose(fp);
183                 return SYNC_AGENT_EVENT_FAIL;
184         }
185         noti_spec_bag->noti_count = 0;
186
187         /*
188          * Setting noti_spec_list
189          */
190         while (!feof(fp)) {
191                 noti_spec_s *noti_spec = (noti_spec_s *) calloc(1, sizeof(noti_spec_s));
192                 if (noti_spec == NULL) {
193                         if (fp != NULL)
194                                 fclose(fp);
195                         _DEBUG_ERROR("CALLOC failed !!!");
196                         free(noti_spec_bag);
197                         return SYNC_AGENT_EVENT_FAIL;
198                 }
199
200                 char description[1000] = { 0, };
201                 int fscanf_ret = fscanf(fp, "%d, %d, %d, %d, %999s\n", &(noti_spec->noti_num), (int *)(&(noti_spec->noti_type)), &(noti_spec->relational_event_num), &(noti_spec->pendingtime), (char *)(&description));
202                 if (fscanf_ret == -1) {
203                         _DEBUG_ERROR("fscanf failed !!");
204                         if (fp != NULL)
205                                 fclose(fp);
206                         free(noti_spec_bag);
207                         free(noti_spec);
208                         return SYNC_AGENT_EVENT_FAIL;
209                 }
210
211                 noti_spec_bag->noti_spec_list[noti_spec_bag->noti_count] = noti_spec;
212                 noti_spec_bag->noti_count++;
213         }
214         fclose(fp);
215
216         /*
217          * Setting noti_key
218          */
219         noti_spec_bag->noti_key = strdup(noti_key);
220
221         /*
222          * Setting communication_path_noti
223          */
224         if (noti_spec_bag->communication_path_noti == NULL) {
225                 int comm_path_noti_len = strlen(EVENT_COMMUNICATION_PATH_NOTI) + strlen(noti_key) + 1;
226                 int len = 0;
227                 noti_spec_bag->communication_path_noti = (char *)calloc(comm_path_noti_len, sizeof(char));
228
229                 if (noti_spec_bag->communication_path_noti != NULL) {
230                         len = g_strlcat(noti_spec_bag->communication_path_noti, EVENT_COMMUNICATION_PATH_NOTI, comm_path_noti_len);
231                         len = g_strlcat(noti_spec_bag->communication_path_noti, noti_key, comm_path_noti_len);
232
233                         if (len >= comm_path_noti_len) {
234                                 _DEBUG_ERROR("noti_spec_bag->communication_path_noti buffer overflow !!");
235                                 free(noti_spec_bag->communication_path_noti);
236                                 free(noti_spec_bag);
237                                 return SYNC_AGENT_EVENT_FAIL;
238                         }
239
240                         _DEBUG_INFO("EVENT_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_noti);
241                 } else {
242                         _DEBUG_ERROR("noti_spec_bag->communication_path_not is NULL");
243                         free(noti_spec_bag);
244                         return SYNC_AGENT_EVENT_FAIL;
245                 }
246         }
247
248         /*
249          * Setting communication_path_reply_noti
250          */
251         if (noti_spec_bag->communication_path_reply_noti == NULL) {
252                 int comm_path_reply_noti_len = strlen(EVENT_REPLY_COMMUNICATION_PATH_NOTI) + strlen(noti_key) + 1;
253                 int len = 0;
254                 noti_spec_bag->communication_path_reply_noti = (char *)calloc(comm_path_reply_noti_len, sizeof(char));
255
256                 if (noti_spec_bag->communication_path_reply_noti != NULL) {
257                         len = g_strlcat(noti_spec_bag->communication_path_reply_noti, EVENT_REPLY_COMMUNICATION_PATH_NOTI, comm_path_reply_noti_len);
258                         len = g_strlcat(noti_spec_bag->communication_path_reply_noti, noti_key, comm_path_reply_noti_len);
259
260                         if (len >= comm_path_reply_noti_len) {
261                                 _DEBUG_ERROR("noti_spec_bag->communication_path_reply_noti buffer overflow !!");
262                                 free(noti_spec_bag->communication_path_reply_noti);
263                                 free(noti_spec_bag);
264                                 return SYNC_AGENT_EVENT_FAIL;
265                         }
266
267                         _DEBUG_INFO("EVENT_REPLY_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_reply_noti);
268                 } else {
269                         _DEBUG_ERROR("noti_spec_bag->communication_path_reply_noti is NULL");
270                         free(noti_spec_bag);
271                         return SYNC_AGENT_EVENT_FAIL;
272                 }
273         }
274
275         _DEBUG_INFO("before: current_noti_bag_count = %d", current_noti_bag_count);
276         noti_spec_bag_list[current_noti_bag_count] = noti_spec_bag;
277         current_noti_bag_count++;
278         _DEBUG_INFO("after: current_noti_bag_count = %d", current_noti_bag_count);
279
280         _EXTERN_FUNC_EXIT;
281
282         return SYNC_AGENT_EVENT_SUCCESS;
283 }
284
285 sync_agent_event_error_e event_clean_noti_spec()
286 {
287         _EXTERN_FUNC_ENTER;
288
289         _DEBUG_INFO("Start: current_noti_bag_count = %d", current_noti_bag_count);
290         int i = 0;
291         for (; i < current_noti_bag_count; i++) {
292                 noti_spec_bag_s *noti_spec_bag = noti_spec_bag_list[i];
293                 if (noti_spec_bag != NULL) {
294                         _DEBUG_INFO("noti_key = %s, communication_path_noti = %s, communication_path_reply_noti = %s", noti_spec_bag->noti_key, noti_spec_bag->communication_path_noti, noti_spec_bag->communication_path_reply_noti);
295                         free(noti_spec_bag->noti_key);
296                         free(noti_spec_bag->communication_path_noti);
297                         free(noti_spec_bag->communication_path_reply_noti);
298
299                         int k = 0;
300                         for (; k < noti_spec_bag->noti_count; k++) {
301                                 noti_spec_s *noti_spec = noti_spec_bag->noti_spec_list[k];
302                                 if (noti_spec != NULL) {
303                                         free(noti_spec);
304                                 }
305                         }
306
307                         noti_spec_bag_list[i] = 0;
308                 }
309         }
310
311         current_noti_bag_count = 0;
312         _DEBUG_INFO("End: current_noti_bag_count = %d", current_noti_bag_count);
313
314         _EXTERN_FUNC_EXIT;
315
316         return SYNC_AGENT_EVENT_SUCCESS;
317 }
318
319 sync_agent_event_error_e event_register_event_callback(int event_num, sync_agent_event_cb callback)
320 {
321         _EXTERN_FUNC_ENTER;
322
323         int index = _find_event_spec(event_num);
324         if (index == -1) {
325                 return SYNC_AGENT_EVENT_FAIL;
326         }
327
328         event_spec_list[index]->callback = callback;
329
330         _EXTERN_FUNC_EXIT;
331
332         return SYNC_AGENT_EVENT_SUCCESS;
333 }
334
335 sync_agent_event_error_e event_register_noti_callback(int noti_num, sync_agent_noti_cb callback, void *data)
336 {
337         _EXTERN_FUNC_ENTER;
338
339         int index = _find_noti_spec(0, noti_num);
340         if (index == -1) {
341                 return SYNC_AGENT_EVENT_FAIL;
342         }
343
344         noti_spec_bag_list[0]->noti_spec_list[index]->callback = callback;
345         noti_spec_bag_list[0]->noti_spec_list[index]->additional_param = data;
346
347         _EXTERN_FUNC_EXIT;
348
349         return SYNC_AGENT_EVENT_SUCCESS;
350 }
351
352 sync_agent_event_cb event_get_event_callback(int event_num)
353 {
354         _EXTERN_FUNC_ENTER;
355
356         int index = _find_event_spec(event_num);
357         if (index == -1) {
358                 return NULL;
359         }
360
361         _EXTERN_FUNC_EXIT;
362
363         return event_spec_list[index]->callback;
364 }
365
366 sync_agent_noti_cb event_get_noti_callback(int noti_num)
367 {
368         _EXTERN_FUNC_ENTER;
369
370         int index = _find_noti_spec(0, noti_num);
371         if (index == -1) {
372                 return NULL;
373         }
374
375         _EXTERN_FUNC_EXIT;
376
377         return noti_spec_bag_list[0]->noti_spec_list[index]->callback;
378 }
379
380 void *event_get_noti_callback_additional_param(int noti_num)
381 {
382         _EXTERN_FUNC_ENTER;
383
384         int index = _find_noti_spec(0, noti_num);
385         if (index == -1) {
386                 return NULL;
387         }
388
389         _EXTERN_FUNC_EXIT;
390
391         return noti_spec_bag_list[0]->noti_spec_list[index]->additional_param;
392 }
393
394 event_type_e event_get_event_type(int event_num)
395 {
396         _EXTERN_FUNC_ENTER;
397
398         int index = _find_event_spec(event_num);
399         if (index == -1) {
400                 return 0;
401         }
402
403         _EXTERN_FUNC_EXIT;
404
405         return event_spec_list[index]->event_type;
406 }
407
408 noti_type_e event_get_noti_type(const char *noti_key, int noti_num)
409 {
410         _EXTERN_FUNC_ENTER;
411
412         retvm_if(noti_key == NULL, NOTI_TYPE_UNKNOWN, "noti key is NULL !!");
413
414         int noti_spec_bag_num = _find_noti_spec_bag(noti_key);
415
416         int noti_index = _find_noti_spec(noti_spec_bag_num, noti_num);
417         if (noti_index == -1) {
418                 return 0;
419         }
420
421         _EXTERN_FUNC_EXIT;
422
423         return noti_spec_bag_list[noti_spec_bag_num]->noti_spec_list[noti_index]->noti_type;
424 }
425
426 static int _find_noti_spec_bag(const char *noti_key)
427 {
428         _INNER_FUNC_ENTER;
429
430         retvm_if(noti_key == NULL, 0, "noti key is NULL !!");
431
432         int i = 0;
433         for (; i < current_noti_bag_count; i++) {
434                 noti_spec_bag_s *noti_spec_bag = noti_spec_bag_list[i];
435                 if (noti_spec_bag != NULL) {
436                         if (!strcmp(noti_spec_bag->noti_key, noti_key)) {
437                                 _INNER_FUNC_EXIT;
438                                 return i;
439                         }
440                 }
441         }
442
443         return 0;
444 }
445
446 static int _find_event_spec(int event_num)
447 {
448         _INNER_FUNC_ENTER;
449
450         int i = 0;
451         for (; i < current_event_count; i++) {
452                 if (event_spec_list[i]->event_num == event_num) {
453                         _INNER_FUNC_EXIT;
454                         return i;
455                 }
456         }
457
458         return -1;
459 }
460
461 static int _find_noti_spec(int noti_spec_bag_num, int noti_num)
462 {
463         _INNER_FUNC_ENTER;
464
465         noti_spec_bag_s *noti_spec_bag = noti_spec_bag_list[noti_spec_bag_num];
466
467         int i = 0;
468         for (; i < noti_spec_bag->noti_count; i++) {
469                 if ((noti_spec_bag->noti_spec_list[i])->noti_num == noti_num) {
470                         _INNER_FUNC_EXIT;
471                         return i;
472                 }
473         }
474
475         return -1;
476 }