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.
21 #include <glib-object.h>
22 #include <glib/gprintf.h>
24 #include <vconf-keys.h>
28 #include <msg_storage.h>
29 #include <msg_transport.h>
31 #include "plugin_slp_sysnoti_wap_push.h"
32 #include "plugin/platform_monitor_interface.h"
34 /* for data logging */
35 #include "fsapi/operation.h"
38 #include "utility/sync_util.h"
40 #include <tzplatform_config.h>
43 #define EXPORT_API __attribute__ ((visibility("default")))
46 #ifndef SYNC_AGENT_LOG
48 #define LOG_TAG "PLUGIN_PM_SYSNOTI_WAP_PUSH"
51 #define CP_HEADER_DATA_PATH tzplatform_mkpath(TZ_SYS_DATA,"header_log.wbxml")
52 #define CP_BODY_DATA_PATH tzplatform_mkpath(TZ_SYS_DATA,"body_log.wbxml")
54 static msg_handle_t msg_handle = NULL;
56 static int (*registered_wap_push_incoming_cb) (int data_id, void *user_data);
57 static int (*registered_wap_push_operation_cb) (int data_id, void *user_data);
59 static int _start_wap_push();
60 static int _end_wap_push();
61 static int _add_message(void *add_data, int *added_data_id);
62 static int _remove_message(int remove_data_id);
64 static int __register_msg_callback();
65 static void __check_msg_server_cb(keynode_t * node, void *data);
66 static void ___incomming_syncml_msg_cb(msg_handle_t msg_handle, msg_syncml_message_type_t msg_type, const char *push_body, int push_body_len, const char *wsp_header, int wsp_header_len, void *user_param);
67 void ___operation_syncml_msg_cb(msg_handle_t handle, int msg_id, int ext_id, void *user_param);
69 EXPORT_API void sync_agent_plugin_init_service_noti(void *data)
75 res = _start_wap_push();
78 _DEBUG_ERROR("_start_wap_push() failed !!");
80 _DEBUG_INFO("_start_wap_push() success !!");
84 if (((sync_agent_pm_register_data_s *) data)->pkg_name != NULL)
85 free(((sync_agent_pm_register_data_s *) data)->pkg_name);
87 if (((sync_agent_pm_register_data_s *) data)->additional_data != NULL)
88 free(((sync_agent_pm_register_data_s *) data)->additional_data);
92 _DEBUG_INFO("free sync_agent_pm_register_data_s !!");
98 EXPORT_API sync_agent_pm_return_e sync_agent_plugin_unregister_service_noti(void)
102 sync_agent_pm_return_e ret = SYNC_AGENT_PM_SUCCESS;
106 res = _end_wap_push();
109 _DEBUG_ERROR("_end_wap_push() failed !!");
112 _DEBUG_INFO("_end_wap_push() success !!");
120 EXPORT_API sync_agent_pm_return_e sync_agent_plugin_add_service_data(void *add_data, int *added_data_id)
124 retvm_if(add_data == NULL, SYNC_AGENT_PM_FAIL, "add_data is NULL. FAIL !!!");
126 sync_agent_pm_return_e ret = SYNC_AGENT_PM_SUCCESS;
130 res = _add_message(add_data, added_data_id);
133 _DEBUG_ERROR("_add_message() failed !!");
134 return SYNC_AGENT_PM_FAIL;
136 _DEBUG_INFO("_add_message() success !!");
144 EXPORT_API sync_agent_pm_return_e sync_agent_plugin_remove_service_data(int remove_data_id)
148 sync_agent_pm_return_e ret = SYNC_AGENT_PM_SUCCESS;
152 res = _remove_message(remove_data_id);
155 _DEBUG_ERROR("_remove_message() failed !!");
158 _DEBUG_INFO("_remove_message() success !!");
166 EXPORT_API sync_agent_pm_return_e sync_agent_plugin_get_service_data(int get_data_id, void **service_data)
170 sync_agent_pm_return_e ret = SYNC_AGENT_PM_SUCCESS;
172 _DEBUG_INFO("not support this feature !!");
179 EXPORT_API void sync_agent_plugin_set_user_callback(int callback_counts, va_list list)
185 _DEBUG_INFO("callback_counts : %d", callback_counts);
186 for (; i < callback_counts; i++) {
189 registered_wap_push_incoming_cb = va_arg(list, sync_agent_user_callback_cb_plugin);
190 _DEBUG_INFO("set user callback ( for WAP Push Incoming ) !!");
193 registered_wap_push_operation_cb = va_arg(list, sync_agent_user_callback_cb_plugin);
194 _DEBUG_INFO("set user callback ( for WAP Push Operation End ) !!");
204 static int _start_wap_push()
211 msg_error_t err = MSG_SUCCESS;
213 if (vconf_get_bool(VCONFKEY_MSG_SERVER_READY, &msg_key) == 0) {
215 _DEBUG_TRACE("msg server is ready !!");
217 err = msg_open_msg_handle(&msg_handle);
218 if (err != MSG_SUCCESS) {
219 _DEBUG_ERROR("msg_open_msg_handle() failed ( err_code : %d ) !!", err);
223 _DEBUG_TRACE("msg_open_msg_handle() success !!");
225 res = __register_msg_callback();
227 _DEBUG_ERROR("register_syncml_msg_callback() failed ( err_code : %d ) !!", res);
229 err = msg_close_msg_handle(&msg_handle);
230 if (err != MSG_SUCCESS) {
231 _DEBUG_ERROR("msg_close_msg_handle() failed ( err_code : %d ) !!", err);
235 _DEBUG_TRACE("msg_close_msg_handle() success !!");
241 _DEBUG_TRACE("register_syncml_msg_callback() success !!");
245 _DEBUG_ERROR("msg server is not ready !!");
247 if (vconf_notify_key_changed((char *)VCONFKEY_MSG_SERVER_READY, __check_msg_server_cb, NULL) != 0) {
248 _DEBUG_ERROR("vconf_notify_key_changed( VCONFKEY_MSG_SERVER_READY ) failed !!");
252 _DEBUG_TRACE("vconf_notify_key_changed() success !!");
256 _DEBUG_ERROR("vconf_get_bool ( VCONFKEY_MSG_SERVER_READY ) failed !!");
268 static int _end_wap_push()
273 msg_error_t err = MSG_SUCCESS;
275 err = msg_close_msg_handle(&msg_handle);
276 if (err != MSG_SUCCESS) {
277 _DEBUG_ERROR("msg_close_msg_handle() failed ( err_code : %d ) !!", err);
281 _DEBUG_TRACE("msg_close_msg_handle() success !!");
292 static int _add_message(void *add_data, int *added_data_id)
296 retvm_if(add_data == NULL, 0, "add_data is NULL. FAIL !!!");
299 msg_error_t err = MSG_SUCCESS;
301 msg_struct_t syncml_msg = msg_create_struct(MSG_STRUCT_SYNCML_INFO);
302 pmci_san_message_s *add_msg = (pmci_san_message_s *) add_data;
304 *added_data_id = add_msg->ext_id;
305 msg_set_int_value(syncml_msg, MSG_SYNCML_INFO_EXTID_INT, add_msg->ext_id);
306 msg_set_int_value(syncml_msg, MSG_SYNCML_INFO_PINCODE_INT, add_msg->pin_code);
308 msg_struct_t msg = NULL;
309 msg_get_struct_handle(syncml_msg, MSG_SYNCML_INFO_MESSAGE_HND, &msg);
310 msg_set_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, MSG_INBOX_ID);
311 msg_set_int_value(msg, MSG_MESSAGE_TYPE_INT, MSG_TYPE_SMS_SYNCML);
312 msg_set_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, MSG_NETWORK_RECEIVED);
313 msg_set_int_value(msg, MSG_MESSAGE_DIRECTION_INT, MSG_DIRECTION_TYPE_MT);
315 msg_struct_list_s *addr_list = NULL;
316 msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
317 addr_list->nCount = 1;
319 msg_struct_t addr_info = NULL;
320 addr_info = addr_list->msg_struct_info[0];
321 msg_set_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_TO);
322 char addr_value[MAX_ADDRESS_VAL_LEN] = "+1004";
323 msg_set_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr_value, MAX_ADDRESS_VAL_LEN);
325 time_t t = time(NULL);
326 time_t utfTime = time(&t);
327 msg_set_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, utfTime);
329 msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, add_msg->msg_data, strlen(add_msg->msg_data));
331 err = msg_add_syncml_message(msg_handle, syncml_msg);
332 if (err != MSG_SUCCESS) {
333 _DEBUG_ERROR("msg_add_syncml_message() failed ( err_code : %d ) !!", err);
337 _DEBUG_TRACE("msg_add_syncml_message() success !!");
341 msg_release_struct(&syncml_msg);
348 static int _remove_message(int remove_data_id)
353 msg_error_t err = MSG_SUCCESS;
355 if (msg_handle == NULL) {
356 _DEBUG_ERROR("msg_handle is NULL !!");
361 err = msg_delete_message(msg_handle, remove_data_id);
362 if (err != MSG_SUCCESS) {
363 _DEBUG_ERROR("msg_delete_message() fail !!");
366 _DEBUG_INFO("msg_delete_message() success !!");
376 static int __register_msg_callback()
381 msg_error_t err = MSG_SUCCESS;
383 if (msg_handle == NULL) {
384 _DEBUG_ERROR("msg_handle is NULL !!");
388 // err = msg_reg_syncml_message_callback(msg_handle, &___incomming_syncml_msg_cb, NULL);
389 err = msg_reg_syncml_message_callback(msg_handle, ___incomming_syncml_msg_cb, NULL);
390 if (err != MSG_SUCCESS) {
391 _DEBUG_ERROR("Register SyncML Incoming Callback fail [%d] !!", err);
395 _DEBUG_VERBOSE("Register SyncML Incoming Callback success !!");
398 // err = msg_reg_syncml_message_operation_callback(msg_handle, &___operation_syncml_msg_cb, NULL);
399 err = msg_reg_syncml_message_operation_callback(msg_handle, ___operation_syncml_msg_cb, NULL);
400 if (err != MSG_SUCCESS) {
401 _DEBUG_ERROR("Register SyncML Operation Callback fail [%d] !!", err);
405 _DEBUG_VERBOSE("Register SyncML Operation Callback success !!");
415 static void __check_msg_server_cb(keynode_t * node, void *data)
421 msg_error_t err = MSG_SUCCESS;
423 if (vconf_get_bool(VCONFKEY_MSG_SERVER_READY, &msg_key) == 0) {
425 _DEBUG_VERBOSE("msg server is ready !!");
427 if (msg_handle == NULL) {
428 err = msg_open_msg_handle(&msg_handle);
429 if (err != MSG_SUCCESS) {
430 _DEBUG_ERROR("msg_open_msg_handle() fail [%d] !!", err);
433 _DEBUG_VERBOSE("msg_open_msg_handle() success !!");
435 res = __register_msg_callback();
437 _DEBUG_ERROR("register_syncml_msg_callback() fail !!");
440 _DEBUG_VERBOSE("register_syncml_msg_callback() success !!");
444 _DEBUG_VERBOSE("callback is already registered !!");
447 _DEBUG_VERBOSE("msg server is not ready !!");
450 _DEBUG_ERROR("vconf_get_bool ( VCONFKEY_MSG_SERVER_READY ) fail !!");
461 static void ___incomming_syncml_msg_cb(msg_handle_t msg_handle, msg_syncml_message_type_t msg_type, const char *push_body, int push_body_len, const char *wsp_header, int wsp_header_len, void *user_param)
465 pmci_san_incoming_s *san_incoming = (pmci_san_incoming_s *) calloc(1, sizeof(pmci_san_incoming_s));
466 if (san_incoming == NULL) {
467 _DEBUG_ERROR("pmci_san_incomming_t calloc error !!");
468 /* todo : exception handling */
471 _DEBUG_VERBOSE("push body : %s, push body len : %d", push_body, push_body_len);
472 _DEBUG_VERBOSE("wsp header : %s, wsp header len : %d", wsp_header, wsp_header_len);
474 /* FIXME : for recoding log data - Start */
475 _DEBUG_VERBOSE("logging wsp_header >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
477 int file_ret = sync_agent_write_whole_file(CP_HEADER_DATA_PATH, wsp_header, wsp_header_len, 1);
479 _DEBUG_ERROR("sync_agent_write_whole_file() - CP header data failed !!");
481 _DEBUG_VERBOSE("sync_agent_write_whole_file() - CP header data success !!");
484 _DEBUG_VERBOSE("logging PushBody >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
485 file_ret = sync_agent_write_whole_file(CP_BODY_DATA_PATH, push_body, push_body_len, 1);
488 _DEBUG_ERROR("sync_agent_write_whole_file() - CP body data failed !!");
490 _DEBUG_VERBOSE("sync_agent_write_whole_file() - CP body data success !!");
493 /* FIXME : for recoding log data - End */
495 if ((msg_type == CP_WBXML) || (msg_type == CP_XML)) { /* CP case : merging header & body messages */
496 if ((push_body_len > 0) && (wsp_header_len > 0)) {
497 int msg_len = push_body_len + wsp_header_len + 1;
499 san_incoming->msg_body = (char *)calloc(msg_len, sizeof(char));
500 if (san_incoming->msg_body == NULL) {
501 _DEBUG_ERROR("san_incoming->msg_body calloc error !!");
504 if (wsp_header != NULL) {
505 _DEBUG_VERBOSE("CP : add wsp_header data !!");
506 memcpy((void *)(san_incoming->msg_body), wsp_header, wsp_header_len + 1);
508 _DEBUG_ERROR("wrong message - wapHeader is NULL !!");
512 if (push_body != NULL) {
513 _DEBUG_VERBOSE("CP : add push_body data !!");
514 memcpy((void *)((san_incoming->msg_body) + wsp_header_len), push_body, push_body_len + 1);
516 _DEBUG_ERROR("wrong message - push_body is NULL !!");
520 san_incoming->msg_size = msg_len - 1;
523 _DEBUG_ERROR("wrong message format !!");
527 if ((push_body_len > 0) && (push_body != NULL)) {
528 san_incoming->msg_body = (char *)calloc(push_body_len + 1, sizeof(char));
529 if (san_incoming->msg_body == NULL) {
530 _DEBUG_ERROR("san_incoming->msg_body calloc error !!");
533 _DEBUG_VERBOSE("OTHERS : set push_body data !!");
534 memcpy((void *)(san_incoming->msg_body), push_body, push_body_len + 1);
535 san_incoming->msg_size = push_body_len;
538 _DEBUG_VERBOSE("PushBody message is NULL !!");
539 san_incoming->msg_body = NULL;
540 san_incoming->msg_size = 0;
547 _DEBUG_VERBOSE("MsgNoti ( msg_type = 0, DM_WBXML ) detected.\n");
548 san_incoming->version = SAN_DM_WBXML;
553 _DEBUG_VERBOSE("MsgNoti ( msg_type = 1, DM_XML ) detected.\n");
554 san_incoming->version = SAN_DM_XML;
557 case DM_NOTIFICATION:
559 _DEBUG_VERBOSE("MsgNoti ( msg_type = 2, DM_NOTIFICATION ) detected.\n");
560 san_incoming->version = SAN_DM_NOTI;
563 case DS_NOTIFICATION:
565 _DEBUG_VERBOSE("MsgNoti ( msg_type = 3, DS_NOTIFICATION ) detected.\n");
566 san_incoming->version = SAN_VERSION_12;
571 _DEBUG_VERBOSE("MsgNoti ( msg_type = 4, DS_WBXML ) detected.\n");
572 san_incoming->version = SAN_VERSION_11;
577 _DEBUG_VERBOSE("MsgNoti ( msg_type = 5, CP_XML ) detected.\n");
578 san_incoming->version = SAN_CP_XML;
583 _DEBUG_VERBOSE("MsgNoti ( msg_type = 6, CP_WBXML ) detected.\n");
584 san_incoming->version = SAN_CP_WBXML;
589 _DEBUG_VERBOSE("Unknown MsgNoti ( %d ) detected.\n", msg_type);
593 if (registered_wap_push_incoming_cb != NULL) {
594 registered_wap_push_incoming_cb(0, (void *)san_incoming);
596 _DEBUG_ERROR("not registered registered_wap_push_incoming_cb !!");
600 if (san_incoming != NULL) {
601 if (san_incoming->msg_body != NULL)
602 free((void *)(san_incoming->msg_body));
610 void ___operation_syncml_msg_cb(msg_handle_t handle, int msg_id, int ext_id, void *user_param)
614 pmci_san_operation_s *san_operation = (pmci_san_operation_s *) calloc(1, sizeof(pmci_san_operation_s));
615 if (san_operation == NULL) {
616 _DEBUG_ERROR("pmci_san_operation_s calloc error !!");
617 /* todo : exception handling */
621 san_operation->msg_id = msg_id;
622 _DEBUG_VERBOSE("msg_id : %d", msg_id);
624 san_operation->ext_id = ext_id;
625 _DEBUG_VERBOSE("ext_id : %d", ext_id);
627 san_operation->user_param = user_param;
628 /*memcpy(san_operation->user_param, user_param, sizeof(user_param)); */
630 if (registered_wap_push_operation_cb != NULL) {
631 registered_wap_push_operation_cb(0, (void *)san_operation);
633 _DEBUG_ERROR("not registered registered_wap_push_operation_cb !!");
634 if(san_operation != NULL) {
636 san_operation = NULL;