3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <glib/gprintf.h>
24 #include "utility/sync_util.h"
25 #include "event/config.h"
27 #ifndef SYNC_AGENT_LOG
29 #define LOG_TAG "AF_EVENT"
32 static int current_event_count = 0;
33 static event_spec_s *event_spec_list[EVENT_MAX_EVENT_COUNT] = { 0 };
35 static int current_noti_bag_count = 0;
36 noti_spec_bag_s *noti_spec_bag_list[EVENT_MAX_NOTI_COUNT] = { 0 };
38 static char *__communication_path_event = NULL;
40 static int _find_noti_spec_bag(const char *noti_key);
42 static int _find_event_spec(int event_num);
43 static int _find_noti_spec(int noti_spec_bag_num, int noti_num);
45 const char *event_get_noti_path(const char *noti_key)
49 if (noti_key == NULL) {
50 if (noti_spec_bag_list[0] != NULL) {
51 return noti_spec_bag_list[0]->communication_path_noti;
57 int index = _find_noti_spec_bag(noti_key);
61 return noti_spec_bag_list[index]->communication_path_noti;
64 const char *event_get_event_path()
70 return __communication_path_event;
73 sync_agent_event_error_e event_set_event_spec_from_config_file(const char *event_key, const char *config_file)
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 !!");
80 FILE *fp = fopen(config_file, "r");
82 _DEBUG_ERROR("file open failed [%s]\n", config_file);
83 return SYNC_AGENT_EVENT_FAIL;
87 event_spec_s *event_spec = (event_spec_s *) malloc(sizeof(event_spec_s));
88 if (event_spec == NULL) {
89 _DEBUG_ERROR("MALLOC failed !!!");
92 return SYNC_AGENT_EVENT_FAIL;
94 memset(event_spec, 0, sizeof(event_spec_s));
97 char description[1000];
98 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));
99 if (fscanf_ret == -1) {
100 _DEBUG_ERROR("fscanf failed !!");
104 return SYNC_AGENT_EVENT_FAIL;
107 event_spec_list[current_event_count] = event_spec;
108 current_event_count++;
113 if (__communication_path_event == NULL) {
114 int comm_path_event_len = strlen(EVENT_COMMUNICATION_PATH_EVENT) + strlen(event_key) + 1;
116 __communication_path_event = (char *)calloc(comm_path_event_len, sizeof(char));
118 if (__communication_path_event != NULL) {
119 len = g_strlcat(__communication_path_event, EVENT_COMMUNICATION_PATH_EVENT, comm_path_event_len);
120 len = g_strlcat(__communication_path_event, event_key, comm_path_event_len);
122 if (len >= comm_path_event_len) {
123 _DEBUG_ERROR("__communication_path_event buffer overflow !!");
124 free(__communication_path_event);
125 return SYNC_AGENT_EVENT_FAIL;
128 _DEBUG_INFO("EVENT_COMMUNICATION_PATH_EVENT : %s", __communication_path_event);
130 _DEBUG_ERROR("__communication_path_event is NULL");
131 return SYNC_AGENT_EVENT_FAIL;
137 return SYNC_AGENT_EVENT_SUCCESS;
140 sync_agent_event_error_e event_clean_event_spec()
144 _DEBUG_INFO("Start: current_event_count = %d", current_event_count);
146 for (; i < current_event_count; i++) {
147 event_spec_s *event_spec = event_spec_list[i];
148 if (event_spec != NULL) {
152 event_spec_list[i] = 0;
155 current_event_count = 0;
156 _DEBUG_INFO("End: current_event_count = %d", current_event_count);
160 return SYNC_AGENT_EVENT_SUCCESS;
163 sync_agent_event_error_e event_set_noti_spec_from_config_file(const char *noti_key, const char *config_file)
167 if (noti_key == NULL || config_file == NULL) {
168 return SYNC_AGENT_EVENT_FAIL;
171 FILE *fp = fopen(config_file, "r");
173 _DEBUG_ERROR("file open failed [%s]\n", config_file);
174 return SYNC_AGENT_EVENT_FAIL;
178 * Create Noti Spec Bag
180 noti_spec_bag_s *noti_spec_bag = (noti_spec_bag_s *) calloc(1, sizeof(noti_spec_bag_s));
181 if (noti_spec_bag == NULL) {
182 _DEBUG_ERROR("CALLOC failed !!!");
185 return SYNC_AGENT_EVENT_FAIL;
187 noti_spec_bag->noti_count = 0;
190 * Setting noti_spec_list
193 noti_spec_s *noti_spec = (noti_spec_s *) calloc(1, sizeof(noti_spec_s));
194 if (noti_spec == NULL) {
197 _DEBUG_ERROR("CALLOC failed !!!");
199 return SYNC_AGENT_EVENT_FAIL;
201 memset(noti_spec, 0, sizeof(noti_spec_s));
204 char description[1000] = { 0, };
205 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));
206 if (fscanf_ret == -1) {
207 _DEBUG_ERROR("fscanf failed !!");
212 return SYNC_AGENT_EVENT_FAIL;
215 noti_spec_bag->noti_spec_list[noti_spec_bag->noti_count] = noti_spec;
216 noti_spec_bag->noti_count++;
223 noti_spec_bag->noti_key = strdup(noti_key);
226 * Setting communication_path_noti
228 if (noti_spec_bag->communication_path_noti == NULL) {
229 int comm_path_noti_len = strlen(EVENT_COMMUNICATION_PATH_NOTI) + strlen(noti_key) + 1;
231 noti_spec_bag->communication_path_noti = (char *)calloc(comm_path_noti_len, sizeof(char));
233 if (noti_spec_bag->communication_path_noti != NULL) {
234 len = g_strlcat(noti_spec_bag->communication_path_noti, EVENT_COMMUNICATION_PATH_NOTI, comm_path_noti_len);
235 len = g_strlcat(noti_spec_bag->communication_path_noti, noti_key, comm_path_noti_len);
237 if (len >= comm_path_noti_len) {
238 _DEBUG_ERROR("noti_spec_bag->communication_path_noti buffer overflow !!");
239 free(noti_spec_bag->communication_path_noti);
241 return SYNC_AGENT_EVENT_FAIL;
244 _DEBUG_INFO("EVENT_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_noti);
246 _DEBUG_ERROR("noti_spec_bag->communication_path_not is NULL");
248 return SYNC_AGENT_EVENT_FAIL;
253 * Setting communication_path_reply_noti
255 if (noti_spec_bag->communication_path_reply_noti == NULL) {
256 int comm_path_reply_noti_len = strlen(EVENT_REPLY_COMMUNICATION_PATH_NOTI) + strlen(noti_key) + 1;
258 noti_spec_bag->communication_path_reply_noti = (char *)calloc(comm_path_reply_noti_len, sizeof(char));
260 if (noti_spec_bag->communication_path_reply_noti != NULL) {
261 len = g_strlcat(noti_spec_bag->communication_path_reply_noti, EVENT_REPLY_COMMUNICATION_PATH_NOTI, comm_path_reply_noti_len);
262 len = g_strlcat(noti_spec_bag->communication_path_reply_noti, noti_key, comm_path_reply_noti_len);
264 if (len >= comm_path_reply_noti_len) {
265 _DEBUG_ERROR("noti_spec_bag->communication_path_reply_noti buffer overflow !!");
266 free(noti_spec_bag->communication_path_reply_noti);
268 return SYNC_AGENT_EVENT_FAIL;
271 _DEBUG_INFO("EVENT_REPLY_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_reply_noti);
273 _DEBUG_ERROR("noti_spec_bag->communication_path_reply_noti is NULL");
275 return SYNC_AGENT_EVENT_FAIL;
279 _DEBUG_INFO("before: current_noti_bag_count = %d", current_noti_bag_count);
280 noti_spec_bag_list[current_noti_bag_count] = noti_spec_bag;
281 current_noti_bag_count++;
282 _DEBUG_INFO("after: current_noti_bag_count = %d", current_noti_bag_count);
286 return SYNC_AGENT_EVENT_SUCCESS;
289 sync_agent_event_error_e event_clean_noti_spec()
293 _DEBUG_INFO("Start: current_noti_bag_count = %d", current_noti_bag_count);
295 for (; i < current_noti_bag_count; i++) {
296 noti_spec_bag_s *noti_spec_bag = noti_spec_bag_list[i];
297 if (noti_spec_bag != NULL) {
298 _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);
299 free(noti_spec_bag->noti_key);
300 free(noti_spec_bag->communication_path_noti);
301 free(noti_spec_bag->communication_path_reply_noti);
304 for (; k < noti_spec_bag->noti_count; k++) {
305 noti_spec_s *noti_spec = noti_spec_bag->noti_spec_list[k];
306 if (noti_spec != NULL) {
311 noti_spec_bag_list[i] = 0;
315 current_noti_bag_count = 0;
316 _DEBUG_INFO("End: current_noti_bag_count = %d", current_noti_bag_count);
320 return SYNC_AGENT_EVENT_SUCCESS;
323 sync_agent_event_error_e event_register_event_callback(int event_num, sync_agent_event_cb callback)
327 int index = _find_event_spec(event_num);
329 return SYNC_AGENT_EVENT_FAIL;
332 event_spec_list[index]->callback = callback;
336 return SYNC_AGENT_EVENT_SUCCESS;
339 sync_agent_event_error_e event_register_noti_callback(int noti_num, sync_agent_noti_cb callback, void *data)
343 int index = _find_noti_spec(0, noti_num);
345 return SYNC_AGENT_EVENT_FAIL;
348 noti_spec_bag_list[0]->noti_spec_list[index]->callback = callback;
349 noti_spec_bag_list[0]->noti_spec_list[index]->additional_param = data;
353 return SYNC_AGENT_EVENT_SUCCESS;
356 sync_agent_event_cb event_get_event_callback(int event_num)
360 int index = _find_event_spec(event_num);
367 return event_spec_list[index]->callback;
370 sync_agent_noti_cb event_get_noti_callback(int noti_num)
374 int index = _find_noti_spec(0, noti_num);
381 return noti_spec_bag_list[0]->noti_spec_list[index]->callback;
384 void *event_get_noti_callback_additional_param(int noti_num)
388 int index = _find_noti_spec(0, noti_num);
395 return noti_spec_bag_list[0]->noti_spec_list[index]->additional_param;
398 event_type_e event_get_event_type(int event_num)
402 int index = _find_event_spec(event_num);
409 return event_spec_list[index]->event_type;
412 noti_type_e event_get_noti_type(const char *noti_key, int noti_num)
416 retvm_if(noti_key == NULL, NOTI_TYPE_UNKNOWN, "noti key is NULL !!");
418 int noti_spec_bag_num = _find_noti_spec_bag(noti_key);
420 int noti_index = _find_noti_spec(noti_spec_bag_num, noti_num);
421 if (noti_index == -1) {
427 return noti_spec_bag_list[noti_spec_bag_num]->noti_spec_list[noti_index]->noti_type;
430 static int _find_noti_spec_bag(const char *noti_key)
434 retvm_if(noti_key == NULL, 0, "noti key is NULL !!");
437 for (; i < current_noti_bag_count; i++) {
438 noti_spec_bag_s *noti_spec_bag = noti_spec_bag_list[i];
439 if (noti_spec_bag != NULL) {
440 if (!strcmp(noti_spec_bag->noti_key, noti_key)) {
450 static int _find_event_spec(int event_num)
455 for (; i < current_event_count; i++) {
456 if (event_spec_list[i]->event_num == event_num) {
465 static int _find_noti_spec(int noti_spec_bag_num, int noti_num)
469 noti_spec_bag_s *noti_spec_bag = noti_spec_bag_list[noti_spec_bag_num];
472 for (; i < noti_spec_bag->noti_count; i++) {
473 if ((noti_spec_bag->noti_spec_list[i])->noti_num == noti_num) {