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;
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 !!");
102 return SYNC_AGENT_EVENT_FAIL;
105 event_spec_list[current_event_count] = event_spec;
106 current_event_count++;
111 if (__communication_path_event == NULL) {
112 int comm_path_event_len = strlen(EVENT_COMMUNICATION_PATH_EVENT) + strlen(event_key) + 1;
114 __communication_path_event = (char *)calloc(comm_path_event_len, sizeof(char));
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);
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;
126 _DEBUG_INFO("EVENT_COMMUNICATION_PATH_EVENT : %s", __communication_path_event);
128 _DEBUG_ERROR("__communication_path_event is NULL");
129 return SYNC_AGENT_EVENT_FAIL;
135 return SYNC_AGENT_EVENT_SUCCESS;
138 sync_agent_event_error_e event_clean_event_spec()
142 _DEBUG_INFO("Start: current_event_count = %d", current_event_count);
144 for (; i < current_event_count; i++) {
145 event_spec_s *event_spec = event_spec_list[i];
146 if (event_spec != NULL) {
150 event_spec_list[i] = 0;
153 current_event_count = 0;
154 _DEBUG_INFO("End: current_event_count = %d", current_event_count);
158 return SYNC_AGENT_EVENT_SUCCESS;
161 sync_agent_event_error_e event_set_noti_spec_from_config_file(const char *noti_key, const char *config_file)
165 if (noti_key == NULL || config_file == NULL) {
166 return SYNC_AGENT_EVENT_FAIL;
169 FILE *fp = fopen(config_file, "r");
171 _DEBUG_ERROR("file open failed [%s]\n", config_file);
172 return SYNC_AGENT_EVENT_FAIL;
176 * Create Noti Spec Bag
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 !!!");
183 return SYNC_AGENT_EVENT_FAIL;
185 noti_spec_bag->noti_count = 0;
188 * Setting noti_spec_list
191 noti_spec_s *noti_spec = (noti_spec_s *) calloc(1, sizeof(noti_spec_s));
192 if (noti_spec == NULL) {
195 _DEBUG_ERROR("CALLOC failed !!!");
197 return SYNC_AGENT_EVENT_FAIL;
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 !!");
208 return SYNC_AGENT_EVENT_FAIL;
211 noti_spec_bag->noti_spec_list[noti_spec_bag->noti_count] = noti_spec;
212 noti_spec_bag->noti_count++;
219 noti_spec_bag->noti_key = strdup(noti_key);
222 * Setting communication_path_noti
224 if (noti_spec_bag->communication_path_noti == NULL) {
225 int comm_path_noti_len = strlen(EVENT_COMMUNICATION_PATH_NOTI) + strlen(noti_key) + 1;
227 noti_spec_bag->communication_path_noti = (char *)calloc(comm_path_noti_len, sizeof(char));
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);
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);
237 return SYNC_AGENT_EVENT_FAIL;
240 _DEBUG_INFO("EVENT_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_noti);
242 _DEBUG_ERROR("noti_spec_bag->communication_path_not is NULL");
244 return SYNC_AGENT_EVENT_FAIL;
249 * Setting communication_path_reply_noti
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;
254 noti_spec_bag->communication_path_reply_noti = (char *)calloc(comm_path_reply_noti_len, sizeof(char));
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);
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);
264 return SYNC_AGENT_EVENT_FAIL;
267 _DEBUG_INFO("EVENT_REPLY_COMMUNICATION_PATH_NOTI : %s", noti_spec_bag->communication_path_reply_noti);
269 _DEBUG_ERROR("noti_spec_bag->communication_path_reply_noti is NULL");
271 return SYNC_AGENT_EVENT_FAIL;
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);
282 return SYNC_AGENT_EVENT_SUCCESS;
285 sync_agent_event_error_e event_clean_noti_spec()
289 _DEBUG_INFO("Start: current_noti_bag_count = %d", current_noti_bag_count);
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);
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) {
307 noti_spec_bag_list[i] = 0;
311 current_noti_bag_count = 0;
312 _DEBUG_INFO("End: current_noti_bag_count = %d", current_noti_bag_count);
316 return SYNC_AGENT_EVENT_SUCCESS;
319 sync_agent_event_error_e event_register_event_callback(int event_num, sync_agent_event_cb callback)
323 int index = _find_event_spec(event_num);
325 return SYNC_AGENT_EVENT_FAIL;
328 event_spec_list[index]->callback = callback;
332 return SYNC_AGENT_EVENT_SUCCESS;
335 sync_agent_event_error_e event_register_noti_callback(int noti_num, sync_agent_noti_cb callback, void *data)
339 int index = _find_noti_spec(0, noti_num);
341 return SYNC_AGENT_EVENT_FAIL;
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;
349 return SYNC_AGENT_EVENT_SUCCESS;
352 sync_agent_event_cb event_get_event_callback(int event_num)
356 int index = _find_event_spec(event_num);
363 return event_spec_list[index]->callback;
366 sync_agent_noti_cb event_get_noti_callback(int noti_num)
370 int index = _find_noti_spec(0, noti_num);
377 return noti_spec_bag_list[0]->noti_spec_list[index]->callback;
380 void *event_get_noti_callback_additional_param(int noti_num)
384 int index = _find_noti_spec(0, noti_num);
391 return noti_spec_bag_list[0]->noti_spec_list[index]->additional_param;
394 event_type_e event_get_event_type(int event_num)
398 int index = _find_event_spec(event_num);
405 return event_spec_list[index]->event_type;
408 noti_type_e event_get_noti_type(const char *noti_key, int noti_num)
412 retvm_if(noti_key == NULL, NOTI_TYPE_UNKNOWN, "noti key is NULL !!");
414 int noti_spec_bag_num = _find_noti_spec_bag(noti_key);
416 int noti_index = _find_noti_spec(noti_spec_bag_num, noti_num);
417 if (noti_index == -1) {
423 return noti_spec_bag_list[noti_spec_bag_num]->noti_spec_list[noti_index]->noti_type;
426 static int _find_noti_spec_bag(const char *noti_key)
430 retvm_if(noti_key == NULL, 0, "noti key is NULL !!");
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)) {
446 static int _find_event_spec(int event_num)
451 for (; i < current_event_count; i++) {
452 if (event_spec_list[i]->event_num == event_num) {
461 static int _find_noti_spec(int noti_spec_bag_num, int noti_num)
465 noti_spec_bag_s *noti_spec_bag = noti_spec_bag_list[noti_spec_bag_num];
468 for (; i < noti_spec_bag->noti_count; i++) {
469 if ((noti_spec_bag->noti_spec_list[i])->noti_num == noti_num) {