2 * Copyright (c) 2013 - 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <bundle_internal.h>
25 #include "data_control_provider_internal.h"
26 #include "data_control_types.h"
27 #include "data_control_provider.h"
28 #include "data_control_sql.h"
29 #include "data_control_log.h"
30 #include "data_control_internal.h"
32 #define INSERT_STMT_CONST_LEN 25
33 #define DELETE_STMT_CONST_LEN 12
34 #define UPDATE_STMT_CONST_LEN 15
35 #define SELECT_STMT_CONST_LEN 13
36 #define WHERE_COND_CONST_LEN 7
37 #define ORDER_CLS_CONST_LEN 10
39 #define QUERY_MAXLEN 4096
40 #define ROW_ID_SIZE 32
41 #define RESULT_PATH_MAX 512
42 #define MAX_COLUMN_COUNT 32767 /* Base on sqlite3 maximum column count */
44 #define RESULT_PAGE_NUMBER "RESULT_PAGE_NUMBER"
45 #define MAX_COUNT_PER_PAGE "MAX_COUNT_PER_PAGE"
46 #define RESULT_VALUE_COUNT "RESULT_VALUE_COUNT"
48 #define PACKET_INDEX_REQUEST_RESULT 0
49 #define PACKET_INDEX_ERROR_MSG 1
50 #define PACKET_INDEX_SELECT_RESULT_FILE 2
51 #define PACKET_INDEX_ROW_ID 2
52 #define PACKET_INDEX_VALUE_COUNT 2
53 #define PACKET_INDEX_GET_RESULT_FILE 3
55 #define PACKET_INDEX_DATAID 0
56 #define PACKET_INDEX_MAP 2
58 #define PACKET_INDEX_UPDATEWHERE 1
59 #define PACKET_INDEX_DELETEWHERE 1
61 #define PACKET_INDEX_MAP_KEY 1
62 #define PACKET_INDEX_MAP_VALUE_1ST 2
63 #define PACKET_INDEX_MAP_VALUE_2ND 3
64 #define PACKET_INDEX_MAP_PAGE_NO 2
65 #define PACKET_INDEX_MAP_COUNT_PER_PAGE 3
67 #define DATA_CONTROL_BUS_NAME "org.tizen.data_control_service"
68 #define DATA_CONTROL_OBJECT_PATH "/org/tizen/data_control_service"
69 #define DATA_CONTROL_INTERFACE_NAME "org.tizen.data_control_service"
70 #define DATA_CONTROL_DATA_CHANGE_DATA_CHANGED "noti_data_changed"
71 #define DATA_CONTROL_DATA_CHANGE_ADD_REMOVE_RESULT "noti_add_remove_result"
80 typedef struct datacontrol_consumer {
85 } datacontrol_consumer_info;
90 data_control_provider_data_change_consumer_filter_cb callback;
91 } changed_noti_consumer_filter_info_s;
93 static GHashTable *__request_table = NULL;
94 static GHashTable *__socket_pair_hash = NULL;
95 static sqlite3 *__provider_db = NULL;
97 static data_control_provider_sql_cb sql_callback;
98 static data_control_provider_map_cb map_callback;
99 static data_control_provider_bulk_cb sql_bulk_callback;
100 static data_control_provider_bulk_cb map_bulk_callback;
102 static void *sql_user_data;
103 static void *map_user_data;
104 static void *sql_bulk_callback_user_data = NULL;
105 static void *map_bulk_callback_user_data = NULL;
107 static GList *__noti_consumer_app_list = NULL;
108 static GList *__noti_consumer_filter_info_list = NULL;
110 static int __create_consumer_list_db();
111 static int __delete_consumer_list_db_info(char *unique_id);
113 static int __is_null_sql_callback(data_control_provider_sql_cb callback)
115 if (callback.select_cb == NULL &&
116 callback.insert_cb == NULL &&
117 callback.delete_cb == NULL &&
118 callback.update_cb == NULL)
124 static int __is_null_map_callback(data_control_provider_map_cb callback)
126 if (callback.get_cb == NULL &&
127 callback.set_cb == NULL &&
128 callback.add_cb == NULL &&
129 callback.remove_cb == NULL)
135 static void __free_bundle_data(gpointer data)
143 static void __free_data(gpointer data)
151 static void __initialize_provider(void)
154 __request_table = g_hash_table_new_full(g_int_hash, g_int_equal,
155 __free_data, __free_bundle_data);
156 __socket_pair_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
157 free, _socket_info_free);
158 result = __create_consumer_list_db();
159 if (result != DATA_CONTROL_ERROR_NONE)
160 _LOGE("fail to create consumer list db");
163 static int __provider_new_request_id(void)
166 g_atomic_int_inc(&id);
170 static int __data_changed_filter_cb_info_compare_cb(gconstpointer a, gconstpointer b)
172 changed_noti_consumer_filter_info_s *key1 =
173 (changed_noti_consumer_filter_info_s *)a;
174 changed_noti_consumer_filter_info_s *key2 =
175 (changed_noti_consumer_filter_info_s *)b;
177 return !(key1->callback_id == key2->callback_id);
180 static int __noti_consumer_app_list_compare_cb(gconstpointer a, gconstpointer b)
182 datacontrol_consumer_info *info_a = (datacontrol_consumer_info *)a;
183 datacontrol_consumer_info *info_b = (datacontrol_consumer_info *)b;
185 return strcmp(info_a->unique_id, info_b->unique_id);
188 static void __free_consumer_info(const gchar *name)
190 datacontrol_consumer_info find_key;
191 datacontrol_consumer_info *info;
192 GList *find_list = NULL;
195 find_key.unique_id = (char *)name;
196 find_list = g_list_find_custom(__noti_consumer_app_list, &find_key,
197 (GCompareFunc)__noti_consumer_app_list_compare_cb);
198 if (find_list == NULL) {
199 _LOGI("__free_consumer_info %s not exist", name);
203 info = (datacontrol_consumer_info *)find_list->data;
204 result = __delete_consumer_list_db_info(info->unique_id);
205 if (result != DATA_CONTROL_ERROR_NONE)
206 _LOGE("__delete_consumer_list_db_info fail %d", result);
210 if (info->object_path)
211 free(info->object_path);
213 free(info->unique_id);
214 g_bus_unwatch_name(info->monitor_id);
216 __noti_consumer_app_list =
217 g_list_remove(__noti_consumer_app_list, find_list->data);
220 _LOGI("__free_consumer_info done");
222 static int __check_consumer_cert(const char *provider_id,
223 const char *consumer_appid,
224 data_control_data_change_type_e type)
228 if (consumer_appid == NULL)
229 return DATA_CONTROL_ERROR_IO_ERROR;
231 if (type >= DATA_CONTROL_DATA_CHANGE_SQL_UPDATE &&
232 type <= DATA_CONTROL_DATA_CHANGE_SQL_DELETE)
237 return _check_cert(provider_id, is_map, consumer_appid);
240 int __datacontrol_get_data_changed_filter_callback_id(void)
243 g_atomic_int_inc(&id);
248 static int __get_int_from_str(const char *str)
253 result = strtol(str, &pend, 10);
254 if ((result == LONG_MIN || result == LONG_MAX)
265 static bundle *__get_bundle_data_from_fd(int fd)
272 ret = read(fd, &len, sizeof(int));
273 if (ret < sizeof(int)) {
274 _LOGE("read error :%d", ret);
277 LOGD("read len : %d", len);
279 if (len > 0 && len < MAX_REQUEST_ARGUMENT_SIZE) {
280 buf = (char *)calloc(len, sizeof(char));
282 _LOGE("calloc fail");
285 ret = read(fd, buf, len);
287 _LOGE("read error :%d", ret);
292 b = bundle_decode_raw((bundle_raw *)buf, len);
297 _LOGE("__get_bundle_data_from_fd read count : %d", len);
303 static data_control_bulk_data_h __get_bulk_data_from_fd(int fd)
305 data_control_bulk_data_h ret_bulk_data_h = NULL;
311 data_control_bulk_data_create(&ret_bulk_data_h);
312 ret = read(fd, &size, sizeof(int));
313 if (ret < sizeof(int)) {
314 _LOGE("read error :%d", ret);
315 data_control_bulk_data_destroy(ret_bulk_data_h);
319 _LOGI("bulk data size : %d", size);
320 if (size < 0 || size >= MAX_REQUEST_ARGUMENT_SIZE) {
321 _LOGE("Invalid data size");
322 data_control_bulk_data_destroy(ret_bulk_data_h);
326 for (i = 0; i < size; i++) {
327 _LOGI("bulk data : %d", i);
328 data = __get_bundle_data_from_fd(fd);
330 _LOGE("get bundle data from fd fail");
331 data_control_bulk_data_destroy(ret_bulk_data_h);
334 data_control_bulk_data_add(ret_bulk_data_h, data);
337 return ret_bulk_data_h;
340 static void __on_name_appeared(GDBusConnection *connection,
342 const gchar *name_owner,
345 _LOGI("name appeared : %s", name);
348 static void __on_name_vanished(GDBusConnection *connection,
352 _LOGI("name vanished : %s", name);
353 __free_consumer_info(name);
356 static int __init_changed_noti_consumer_list()
359 char *unique_id = NULL;
360 char *object_path = NULL;
361 int ret = DATA_CONTROL_ERROR_NONE;
362 sqlite3_stmt *stmt = NULL;
363 char query[QUERY_MAXLEN];
364 datacontrol_consumer_info *consumer_info = NULL;
366 sqlite3_snprintf(QUERY_MAXLEN, query,
367 "SELECT app_id, object_path, unique_id " \
368 "FROM data_control_consumer_path_list");
370 _LOGI("__init_changed_noti_consumer_list query : %s", query);
371 ret = sqlite3_prepare_v2(__provider_db, query, -1, &stmt, NULL);
372 if (ret != SQLITE_OK) {
373 _LOGE("prepare stmt fail");
374 return DATA_CONTROL_ERROR_IO_ERROR;
377 while (SQLITE_ROW == sqlite3_step(stmt)) {
378 app_id = (char *)sqlite3_column_text(stmt, 0);
380 _LOGE("Failed to get package name\n");
384 object_path = (char *)sqlite3_column_text(stmt, 1);
386 _LOGE("Failed to get object_path\n");
390 unique_id = (char *)sqlite3_column_text(stmt, 2);
392 _LOGE("Failed to get unique_id\n");
395 _LOGI("sql : app_id : %s, object_path : %s, unique_id : %s",
396 app_id, object_path, unique_id);
398 consumer_info = (datacontrol_consumer_info *)
399 calloc(1, sizeof(datacontrol_consumer_info));
400 if (consumer_info == NULL) {
401 _LOGE("out of memory.");
402 ret = DATA_CONTROL_ERROR_OUT_OF_MEMORY;
406 consumer_info->appid = strdup(app_id);
407 consumer_info->object_path = strdup(object_path);
408 consumer_info->unique_id = strdup(unique_id);
410 consumer_info->monitor_id = g_bus_watch_name_on_connection(
411 _get_dbus_connection(),
412 consumer_info->unique_id,
413 G_BUS_NAME_WATCHER_FLAGS_NONE,
419 if (consumer_info->monitor_id == 0) {
420 _LOGE("g_bus_watch_name_on_connection fail");
422 free(consumer_info->appid);
423 free(consumer_info->object_path);
424 free(consumer_info->unique_id);
429 _LOGI("noti consumer_app_list append %s", consumer_info->object_path);
430 __noti_consumer_app_list =
431 g_list_append(__noti_consumer_app_list, consumer_info);
434 sqlite3_finalize(stmt);
439 static int __delete_consumer_list_db_info(char *unique_id)
443 char query[QUERY_MAXLEN];
444 sqlite3_snprintf(QUERY_MAXLEN, query,
445 "DELETE FROM data_control_consumer_path_list WHERE unique_id = %Q",
447 _LOGI("consumer list db DELETE : %s, unique_id : %s", query, unique_id);
448 r = sqlite3_exec(__provider_db, query, NULL, NULL, &error);
449 if (r != SQLITE_OK) {
450 _LOGE("sqlite3_exec error(query = %s, error = %s)", query, error);
452 return DATA_CONTROL_ERROR_IO_ERROR;
455 _LOGI("__delete_consumer_list_db_info done");
456 return DATA_CONTROL_ERROR_NONE;
460 static int __create_consumer_list_db()
462 char *db_path = NULL;
464 int open_flags = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
465 char *table_command =
466 "CREATE TABLE IF NOT EXISTS data_control_consumer_path_list" \
467 "(app_id TEXT NOT NULL, provider_id TEXT NOT NULL, "\
468 "data_id TEXT NOT NULL, " \
469 "unique_id TEXT NOT NULL, object_path TEXT NOT NULL, " \
470 "PRIMARY KEY(object_path))";
472 if (__provider_db == NULL) {
473 db_path = _get_encoded_db_path();
475 return DATA_CONTROL_ERROR_IO_ERROR;
476 _LOGI("data-control provider db path : %s", db_path);
478 ret = sqlite3_open_v2(db_path, &__provider_db, open_flags, NULL);
480 if (ret != SQLITE_OK) {
481 _LOGE("database creation failed with error: %d", ret);
482 return DATA_CONTROL_ERROR_IO_ERROR;
484 ret = sqlite3_exec(__provider_db, table_command, NULL, NULL, NULL);
485 if (ret != SQLITE_OK) {
486 _LOGE("database table creation failed with error: %d", ret);
487 return DATA_CONTROL_ERROR_IO_ERROR;
489 ret = __init_changed_noti_consumer_list();
490 if (ret != DATA_CONTROL_ERROR_NONE) {
491 _LOGE("__init_changed_noti_consumer_list fail %d", ret);
495 return DATA_CONTROL_ERROR_NONE;
498 static int __get_sender_pid(const char *sender_name)
500 GDBusMessage *msg = NULL;
501 GDBusMessage *reply = NULL;
506 msg = g_dbus_message_new_method_call("org.freedesktop.DBus",
507 "/org/freedesktop/DBus",
508 "org.freedesktop.DBus",
509 "GetConnectionUnixProcessID");
511 _LOGE("Can't allocate new method call");
515 g_dbus_message_set_body(msg, g_variant_new("(s)", sender_name));
517 g_dbus_connection_send_message_with_reply_sync(_get_dbus_connection(),
519 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
527 _LOGE("Failed to get pid [%s]", err->message);
533 body = g_dbus_message_get_body(reply);
534 g_variant_get(body, "(u)", &pid);
540 g_object_unref(reply);
545 static int __insert_consumer_list_db_info(char *app_id, char *provider_id,
546 char *data_id, char *unique_id, char *object_path)
550 char query[QUERY_MAXLEN];
551 sqlite3_snprintf(QUERY_MAXLEN, query,
552 "INSERT OR REPLACE INTO data_control_consumer_path_list(app_id, " \
553 "provider_id, data_id, unique_id, object_path)" \
554 "VALUES (%Q, %Q, %Q, %Q, %Q)",
555 app_id, provider_id, data_id, unique_id, object_path);
556 _LOGI("consumer list db insert sql : %s", query);
558 r = sqlite3_exec(__provider_db, query, NULL, NULL, &error);
559 if (r != SQLITE_OK) {
560 _LOGE("sqlite3_exec error(query = %s, error = %s)", query, error);
562 return DATA_CONTROL_ERROR_IO_ERROR;
565 return DATA_CONTROL_ERROR_NONE;
568 static int __set_consumer_app_list(
573 datacontrol_consumer_info find_key;
574 datacontrol_consumer_info *consumer_info;
575 GList *find_list = NULL;
576 int ret = DATA_CONTROL_ERROR_NONE;
577 _LOGI("set consumer_app_list caller : %s, path : %s, unique_id : %s",
578 caller, object_path, unique_id);
580 find_key.unique_id = unique_id;
581 find_list = g_list_find_custom(__noti_consumer_app_list,
583 (GCompareFunc)__noti_consumer_app_list_compare_cb);
586 consumer_info = (datacontrol_consumer_info *)
587 calloc(1, sizeof(datacontrol_consumer_info));
588 if (consumer_info == NULL) {
589 _LOGE("out of memory.");
590 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
593 consumer_info->appid = strdup(caller);
594 consumer_info->object_path = strdup(object_path);
595 consumer_info->unique_id = strdup(unique_id);
597 consumer_info->monitor_id = g_bus_watch_name_on_connection(
598 _get_dbus_connection(),
599 consumer_info->unique_id,
600 G_BUS_NAME_WATCHER_FLAGS_NONE,
605 if (consumer_info->monitor_id == 0) {
606 _LOGE("g_bus_watch_name_on_connection fail");
608 free(consumer_info->appid);
609 free(consumer_info->object_path);
610 free(consumer_info->unique_id);
613 return DATA_CONTROL_ERROR_IO_ERROR;
615 _LOGI("new noti consumer_app_list append %s",
616 consumer_info->object_path);
617 __noti_consumer_app_list =
618 g_list_append(__noti_consumer_app_list, consumer_info);
623 static bundle *__set_result(bundle *b, datacontrol_request_type type, void *data)
625 bundle *res = bundle_create();
628 char type_str[MAX_LEN_DATACONTROL_REQ_TYPE] = {0,};
629 const char *request_type;
630 const char *provider_id;
632 const char *request_id;
633 const char *caller_appid;
635 const char *page_num = bundle_get_val(b, RESULT_PAGE_NUMBER);
636 const char *count_per_page = bundle_get_val(b, MAX_COUNT_PER_PAGE);
638 if (type == DATACONTROL_TYPE_UNDEFINED || type == DATACONTROL_TYPE_ERROR) {
639 request_type = bundle_get_val(b, OSP_K_DATACONTROL_REQUEST_TYPE);
641 strncpy(type_str, request_type, MAX_LEN_DATACONTROL_REQ_TYPE);
642 _LOGI("type is %s", type_str);
645 snprintf(type_str, MAX_LEN_DATACONTROL_REQ_TYPE, "%d", (int)type);
648 bundle_add_str(res, OSP_K_DATACONTROL_REQUEST_TYPE, type_str);
650 /* Set the provider id */
651 provider_id = bundle_get_val(b, OSP_K_DATACONTROL_PROVIDER);
652 bundle_add_str(res, OSP_K_DATACONTROL_PROVIDER, provider_id);
654 /* Set the data id */
655 data_id = bundle_get_val(b, OSP_K_DATACONTROL_DATA);
656 bundle_add_str(res, OSP_K_DATACONTROL_DATA, data_id);
658 /* Set the caller request id */
659 request_id = bundle_get_val(b, OSP_K_REQUEST_ID);
660 bundle_add_str(res, OSP_K_REQUEST_ID, request_id);
662 caller_appid = bundle_get_val(b, AUL_K_CALLER_APPID);
663 bundle_add_str(res, AUL_K_CALLER_APPID, caller_appid);
666 case DATACONTROL_TYPE_SQL_SELECT:
668 list[PACKET_INDEX_REQUEST_RESULT] = "1"; /* request result */
669 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
670 list[PACKET_INDEX_SELECT_RESULT_FILE] = DATACONTROL_EMPTY;
672 page_num = bundle_get_val(b, RESULT_PAGE_NUMBER);
673 count_per_page = bundle_get_val(b, MAX_COUNT_PER_PAGE);
675 _LOGI("page num: %s, count_per_page: %s", page_num, count_per_page);
677 bundle_add_str(res, RESULT_PAGE_NUMBER, page_num);
678 bundle_add_str(res, MAX_COUNT_PER_PAGE, count_per_page);
680 bundle_add_str_array(res, OSP_K_ARG, list, 3);
684 case DATACONTROL_TYPE_SQL_BULK_INSERT:
686 list[PACKET_INDEX_REQUEST_RESULT] = "1"; /* request result */
687 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
688 bundle_add_str_array(res, OSP_K_ARG, list, 2);
691 case DATACONTROL_TYPE_SQL_INSERT:
693 long long row_id = *(long long *)data;
696 list[PACKET_INDEX_REQUEST_RESULT] = "1"; /* request result */
697 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
699 /* Set the row value */
700 char row_str[ROW_ID_SIZE] = {0,};
701 snprintf(row_str, ROW_ID_SIZE, "%lld", row_id);
703 list[PACKET_INDEX_ROW_ID] = row_str;
704 bundle_add_str_array(res, OSP_K_ARG, list, 3);
707 case DATACONTROL_TYPE_SQL_UPDATE:
708 case DATACONTROL_TYPE_SQL_DELETE:
711 list[PACKET_INDEX_REQUEST_RESULT] = "1"; /* request result */
712 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
714 bundle_add_str_array(res, OSP_K_ARG, list, 2);
717 case DATACONTROL_TYPE_MAP_BULK_ADD:
719 list[PACKET_INDEX_REQUEST_RESULT] = "1"; /* request result */
720 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
721 bundle_add_str_array(res, OSP_K_ARG, list, 2);
724 case DATACONTROL_TYPE_MAP_GET:
727 list[PACKET_INDEX_REQUEST_RESULT] = "1"; /* request result */
728 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
730 bundle_add_str_array(res, OSP_K_ARG, list, 2);
731 const char *page_num = bundle_get_val(b, RESULT_PAGE_NUMBER);
732 const char *count_per_page = bundle_get_val(b, MAX_COUNT_PER_PAGE);
733 const char *value_count = bundle_get_val(b, RESULT_VALUE_COUNT);
735 bundle_add_str(res, RESULT_PAGE_NUMBER, page_num);
736 bundle_add_str(res, MAX_COUNT_PER_PAGE, count_per_page);
737 bundle_add_str(res, RESULT_VALUE_COUNT, value_count);
741 case DATACONTROL_TYPE_ADD_DATA_CHANGED_CB:
744 char result_str[2] = {0,};
745 bool result = *(bool *)data;
746 snprintf(result_str, 2, "%d", result);
748 list[PACKET_INDEX_REQUEST_RESULT] = result_str; /* request result */
749 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
751 bundle_add_str_array(res, OSP_K_ARG, list, 2);
754 case DATACONTROL_TYPE_UNDEFINED: /* DATACONTROL_TYPE_MAP_SET || ADD || REMOVE */
757 list[PACKET_INDEX_REQUEST_RESULT] = "1"; /* request result */
758 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
760 bundle_add_str_array(res, OSP_K_ARG, list, 2);
766 list[PACKET_INDEX_REQUEST_RESULT] = "0"; /* request result */
767 list[PACKET_INDEX_ERROR_MSG] = (char *)data; /* error string */
769 bundle_add_str_array(res, OSP_K_ARG, list, 2);
777 static int __send_select_result(int fd, bundle *b, void *data)
780 In this function, the result set is written in socket as specific form.
781 [sizeof(int)] column count
782 [sizeof(int)] column type x N
783 [ variant ] (column name leng, column name) x N
784 [sieeof(int)] total size of column names
785 [sizeof(int)] row count
786 [ variant ] (type, size, content) x N
789 sqlite3_stmt *state = (sqlite3_stmt *)data;
790 int column_count = DATACONTROL_RESULT_NO_DATA;
792 char *column_name = NULL;
793 int total_len_of_column_names = 0;
794 int count_per_page = 0;
797 sqlite3_int64 offset_idx = 0;
798 sqlite3_int64 row_count = 0;
802 const char *page_number_str;
806 long long tmp_long = 0;
807 double tmp_double = 0.0;
810 const char *count_per_page_str;
812 _LOGI("__send_select_result");
813 if (b == NULL || data == NULL) {
814 _LOGE("The input param is invalid.");
815 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
818 if (sqlite3_reset(state) != SQLITE_OK) {
819 _LOGE("sqlite3_reset() is failed.");
820 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
823 if (sqlite3_step(state) != SQLITE_ROW) {
824 _LOGE("The DB does not have another row.");
825 if (_write_socket(fd, &column_count, sizeof(int), &nb) !=
826 DATA_CONTROL_ERROR_NONE) {
827 _LOGE("Writing a column_count to a file descriptor is failed.");
828 return DATA_CONTROL_ERROR_IO_ERROR;
830 return DATA_CONTROL_ERROR_NONE;
833 /* 1. column count */
834 column_count = sqlite3_column_count(state);
835 if (_write_socket(fd, &column_count, sizeof(int), &nb) !=
836 DATA_CONTROL_ERROR_NONE) {
837 _LOGE("Writing a column_count to a file descriptor is failed. errno = %d", errno);
838 return DATA_CONTROL_ERROR_IO_ERROR;
841 _LOGI("Writing a column_count %d", column_count);
844 2. column type x column_count
845 #define SQLITE_INTEGER 1
846 #define SQLITE_FLOAT 2
847 #define SQLITE_TEXT 3
848 #define SQLITE_BLOB 4
849 #define SQLITE_NULL 5
851 for (i = 0; i < column_count; i++) {
852 type = sqlite3_column_type(state, i);
853 if (_write_socket(fd, &type, sizeof(int), &nb) !=
854 DATA_CONTROL_ERROR_NONE) {
855 _LOGI("Writing a type to a file descriptor is failed.");
856 return DATA_CONTROL_ERROR_IO_ERROR;
858 _LOGI("Writing a column_type %d", type);
861 /* 3. column name x column_count */
862 for (i = 0; i < column_count; i++) {
863 column_name = (char *)sqlite3_column_name(state, i);
864 if (column_name == NULL) {
865 _LOGI("sqlite3_column_name is failed. errno = %d", errno);
866 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
868 column_name_len = strlen(column_name) + 1;
869 if (_write_socket(fd, &column_name_len, sizeof(int), &nb) !=
870 DATA_CONTROL_ERROR_NONE) {
871 _LOGI("Writing a column_name_len to a file descriptor is failed. errno = %d", errno);
872 return DATA_CONTROL_ERROR_IO_ERROR;
875 _LOGI("Writing a column_name_len %d", column_name_len);
877 if (_write_socket(fd, column_name, column_name_len, &nb) !=
878 DATA_CONTROL_ERROR_NONE) {
879 _LOGI("Writing a column_name to a file descriptor is failed. errno = %d", errno);
880 return DATA_CONTROL_ERROR_IO_ERROR;
882 total_len_of_column_names += strlen(column_name);
883 _LOGI("Writing a column_name %s", column_name);
887 /* 4. total length of column names */
888 if (_write_socket(fd, &total_len_of_column_names, sizeof(int), &nb) !=
889 DATA_CONTROL_ERROR_NONE) {
890 _LOGI("Writing a total_len_of_column_names to a file descriptor is failed. errno = %d", errno);
891 return DATA_CONTROL_ERROR_IO_ERROR;
894 _LOGI("Writing a total_len_of_column_names %d", total_len_of_column_names);
896 page_number_str = bundle_get_val(b, RESULT_PAGE_NUMBER);
897 count_per_page_str = bundle_get_val(b, MAX_COUNT_PER_PAGE);
899 _LOGI("page_number: %s, per_page: %s", page_number_str, count_per_page_str);
901 /* 5. type, size and value of each element */
902 if (page_number_str != NULL)
903 page_number = atoi(page_number_str);
907 if (count_per_page_str != NULL)
908 count_per_page = atoi(count_per_page_str);
912 offset = (page_number - 1) * count_per_page;
914 _LOGI("page_number: %d, count_per_page: %d, offset: %d",
915 page_number, count_per_page, offset);
917 if (sqlite3_reset(state) != SQLITE_OK) {
918 _LOGE("sqlite3_reset() is failed.");
919 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
922 if (sqlite3_step(state) != SQLITE_ROW) {
923 _LOGE("The DB does not have another row.");
924 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
928 if (offset_idx > offset)
930 } while (sqlite3_step(state) == SQLITE_ROW && row_count < count_per_page);
933 if (_write_socket(fd, &row_count, sizeof(row_count), &nb) !=
934 DATA_CONTROL_ERROR_NONE) {
935 _LOGI("Writing a row_count to a file descriptor is failed. errno = %d", errno);
936 return DATA_CONTROL_ERROR_IO_ERROR;
938 _LOGI("Writing a row_count %lld", row_count);
942 if (sqlite3_reset(state) != SQLITE_OK) {
943 _LOGI("sqlite3_reset() is failed.");
944 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
947 if (sqlite3_step(state) != SQLITE_ROW) {
948 _LOGE("The DB does not have another row.");
949 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
953 if (offset_idx > offset) {
955 for (i = 0; i < column_count; ++i) {
956 column_type = sqlite3_column_type(state, i);
958 switch (column_type) {
961 size = sizeof(long long);
962 tmp_long = sqlite3_column_int64(state, i);
967 size = sizeof(double);
968 tmp_double = sqlite3_column_double(state, i);
973 value = (char *)sqlite3_column_text(state, i);
975 size = strlen(value) + 1;
981 size = sqlite3_column_bytes(state, i);
982 value = (char *)sqlite3_column_blob(state, i);
989 _LOGI("The column type is invalid.");
993 if (value == NULL && type != 5)
994 return DATA_CONTROL_ERROR_IO_ERROR;
996 buf_len = sizeof(int) * 2 + size;
997 buf = calloc(buf_len, sizeof(void));
999 _LOGE("calloc failed");
1000 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
1002 memcpy(buf, &type, sizeof(int));
1003 memcpy(buf + sizeof(int), &size, sizeof(int));
1005 memcpy(buf + sizeof(int) + sizeof(int), value, size);
1007 if (_write_socket(fd, buf, buf_len, &nb) !=
1008 DATA_CONTROL_ERROR_NONE) {
1009 _LOGE("Writing a size to a file descriptor is failed. errno = %d", errno);
1011 return DATA_CONTROL_ERROR_IO_ERROR;
1017 _LOGI("row_count ~~~~ %lld", row_count);
1021 } while (sqlite3_step(state) == SQLITE_ROW && row_count < count_per_page);
1023 return DATA_CONTROL_ERROR_NONE;
1026 static int __send_get_value_result(int fd, bundle *b, void *data)
1029 char **value_list = (char **)data;
1030 const char *page_num_str = bundle_get_val(b, RESULT_PAGE_NUMBER);
1031 const char *count_per_page_str = bundle_get_val(b, MAX_COUNT_PER_PAGE);
1032 const char *value_count_str = bundle_get_val(b, RESULT_VALUE_COUNT);
1033 int page_number = 0;
1034 int count_per_page = 0;
1035 int value_count = 0;
1036 int current_offset = 0;
1037 int remain_count = 0;
1039 int add_value_count;
1042 _LOGI("page num: %s, count_per_page: %s, value_count %s",
1043 page_num_str, count_per_page_str, value_count_str);
1046 page_number = __get_int_from_str(page_num_str);
1047 if (count_per_page_str)
1048 count_per_page = __get_int_from_str(count_per_page_str);
1049 if (value_count_str)
1050 value_count = __get_int_from_str(value_count_str);
1052 current_offset = (page_number - 1) * count_per_page;
1053 remain_count = value_count - current_offset;
1054 remain_count = (remain_count > 0) ? remain_count : 0; /* round off to zero if the negative num is found */
1057 (count_per_page > remain_count) ? remain_count : count_per_page;
1059 _LOGI("add_value_count: %d, current_offset: %d, remain_count %d",
1060 add_value_count, current_offset, remain_count);
1062 if (_write_socket(fd, &add_value_count, sizeof(int), &nb) !=
1063 DATA_CONTROL_ERROR_NONE) {
1064 _LOGE("Writing a length to a file descriptor is failed. errno = %d", errno);
1065 return DATA_CONTROL_ERROR_IO_ERROR;
1068 add_value_count += current_offset;
1070 for (i = current_offset; i < add_value_count; i++) {
1071 length = strlen(value_list[i]);
1072 _LOGI("length = %d", length);
1073 if (_write_socket(fd, &length, sizeof(int), &nb) !=
1074 DATA_CONTROL_ERROR_NONE) {
1075 _LOGE("Writing a length to a file descriptor is failed. errno = %d", errno);
1076 return DATA_CONTROL_ERROR_IO_ERROR;
1079 _LOGI("value_list = %s", value_list[i]);
1080 if (_write_socket(fd, value_list[i], length, &nb) !=
1081 DATA_CONTROL_ERROR_NONE) {
1082 _LOGE("Writing a value_list to a file descriptor is failed. errno = %d", errno);
1083 return DATA_CONTROL_ERROR_IO_ERROR;
1086 return DATA_CONTROL_ERROR_NONE;
1089 static int __send_bulk_result(int fd, void *data)
1091 data_control_bulk_result_data_h bulk_results =
1092 (data_control_bulk_result_data_h)data;
1096 bundle_raw *encode_data = NULL;
1097 int encode_datalen = 0;
1098 bundle *result_data;
1099 unsigned int nb = 0;
1101 data_control_bulk_result_data_get_count(bulk_results, &count);
1102 if (_write_socket(fd, &count, sizeof(int), &nb) !=
1103 DATA_CONTROL_ERROR_NONE) {
1104 _LOGE("Writing a result count to a file descriptor is failed. errno = %d", errno);
1105 return DATA_CONTROL_ERROR_IO_ERROR;
1107 LOGD("__send_bulk_result %d", count);
1109 for (i = 0; i < count; i++) {
1110 data_control_bulk_result_data_get_result_data(bulk_results, i,
1111 &result_data, &result);
1112 if (_write_socket(fd, &result, sizeof(int), &nb) !=
1113 DATA_CONTROL_ERROR_NONE) {
1114 _LOGE("Writing a result to a file descriptor is failed. errno = %d", errno);
1115 return DATA_CONTROL_ERROR_IO_ERROR;
1118 if (result_data != NULL) {
1119 bundle_encode_raw(result_data, &encode_data, &encode_datalen);
1120 if (encode_data == NULL) {
1121 _LOGE("bundle encode error");
1122 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
1126 if (_write_socket(fd, &encode_datalen, sizeof(int), &nb) !=
1127 DATA_CONTROL_ERROR_NONE) {
1128 _LOGE("Writing a encode_datalen to a file descriptor is failed. errno = %d", errno);
1130 return DATA_CONTROL_ERROR_IO_ERROR;
1133 if (_write_socket(fd, encode_data, encode_datalen, &nb) !=
1134 DATA_CONTROL_ERROR_NONE) {
1135 _LOGE("Writing a encode_data to a file descriptor is failed. errno = %d", errno);
1137 return DATA_CONTROL_ERROR_IO_ERROR;
1140 _LOGE("result %d, encode_datalen %d", result, encode_datalen);
1145 return DATA_CONTROL_ERROR_NONE;
1148 int __datacontrol_send_async(int sockfd, bundle *kb,
1149 datacontrol_request_type type, void *data)
1151 bundle_raw *kb_data = NULL;
1152 int ret = DATA_CONTROL_ERROR_NONE;
1156 unsigned int nb = 0;
1158 _LOGI("send async ~~~");
1159 bundle_encode_raw(kb, &kb_data, &datalen);
1160 if (kb_data == NULL) {
1161 _LOGE("bundle encode error");
1162 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
1165 /* encoded bundle + encoded bundle len */
1166 buf = (char *)calloc(datalen + 4, sizeof(char));
1168 _LOGE("buf calloc error");
1169 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
1171 memcpy(buf, &datalen, sizeof(datalen));
1172 memcpy(buf + sizeof(datalen), kb_data, datalen);
1174 total_len = sizeof(datalen) + datalen;
1176 _LOGI("write : %d", datalen);
1177 if (_write_socket(sockfd, buf, total_len, &nb) != DATA_CONTROL_ERROR_NONE) {
1178 _LOGI("write data fail ");
1179 ret = DATA_CONTROL_ERROR_IO_ERROR;
1183 if (DATACONTROL_TYPE_SQL_SELECT == type)
1184 ret = __send_select_result(sockfd, kb, data);
1185 else if (DATACONTROL_TYPE_MAP_GET == type)
1186 ret = __send_get_value_result(sockfd, kb, data);
1187 else if (DATACONTROL_TYPE_SQL_BULK_INSERT == type ||
1188 DATACONTROL_TYPE_MAP_BULK_ADD == type)
1189 ret = __send_bulk_result(sockfd, data);
1193 bundle_free_encoded_rawdata(&kb_data);
1198 static int __send_result(bundle *b, datacontrol_request_type type, void *data)
1200 datacontrol_socket_info *socket_info;
1201 char *caller_app_id = (char *)bundle_get_val(b, AUL_K_CALLER_APPID);
1202 int ret = DATA_CONTROL_ERROR_NONE;
1204 _LOGI("__datacontrol_send_async caller_app_id : %s ", caller_app_id);
1206 socket_info = g_hash_table_lookup(__socket_pair_hash, caller_app_id);
1207 if (socket_info == NULL) {
1208 _LOGE("__socket_pair_hash lookup fail");
1209 return DATA_CONTROL_ERROR_IO_ERROR;
1211 ret = __datacontrol_send_async(socket_info->socket_fd, b, type, data);
1212 _LOGI("__datacontrol_send_async result : %d ", ret);
1213 if (ret != DATA_CONTROL_ERROR_NONE)
1214 g_hash_table_remove(__socket_pair_hash, caller_app_id);
1219 static int __send_add_callback_result(
1220 data_control_data_change_type_e result_type,
1224 int callback_result)
1227 int result = DATA_CONTROL_ERROR_NONE;
1228 gboolean signal_result = TRUE;
1229 _LOGI("add callback_result type : %d, callback_id : %d, result : %d",
1230 result_type, callback_id, callback_result);
1232 signal_result = g_dbus_connection_emit_signal(
1233 _get_dbus_connection(),
1236 DATA_CONTROL_INTERFACE_NAME,
1237 DATA_CONTROL_DATA_CHANGE_ADD_REMOVE_RESULT,
1238 g_variant_new("(iii)",
1241 callback_result), &err);
1242 if (signal_result == FALSE) {
1243 _LOGE("g_dbus_connection_emit_signal() is failed");
1245 _LOGE("g_dbus_connection_emit_signal() err : %s",
1249 return DATA_CONTROL_ERROR_IO_ERROR;
1252 _LOGI("Send __send_add_callback_result done %d", result);
1256 static int __provider_noti_process(bundle *b, datacontrol_request_type type)
1258 data_control_h provider = NULL;
1259 bool noti_allow = true;
1261 int result = DATA_CONTROL_ERROR_NONE;
1262 char *unique_id = NULL;
1263 datacontrol_data_change_type_e result_type =
1264 DATACONTROL_DATA_CHANGE_CALLBACK_ADD_RESULT;
1265 char *callback_id_str = NULL;
1266 int callback_id = -1;
1268 changed_noti_consumer_filter_info_s *filter_info;
1269 char caller_app_id[255];
1270 const char *pid_str;
1274 pid_str = bundle_get_val(b, AUL_K_CALLER_PID);
1275 if (pid_str == NULL) {
1276 _LOGE("fail to get caller pid");
1277 result = DATA_CONTROL_ERROR_IO_ERROR;
1280 pid = atoi(pid_str);
1282 _LOGE("invalid caller pid %s", pid_str);
1283 result = DATA_CONTROL_ERROR_IO_ERROR;
1286 if (aul_app_get_appid_bypid(pid, caller_app_id, sizeof(caller_app_id)) != 0) {
1287 _LOGE("Failed to get appid by pid");
1288 return DATA_CONTROL_ERROR_IO_ERROR;
1291 unique_id = (char *)bundle_get_val(b, OSP_K_DATACONTROL_UNIQUE_NAME);
1292 _LOGI("unique_id : %s", unique_id);
1293 sender_pid = __get_sender_pid(unique_id);
1294 if (sender_pid != pid) {
1295 _LOGE("invalid unique id. sender does not have unique_id %s", unique_id);
1296 return DATA_CONTROL_ERROR_PERMISSION_DENIED;
1299 result = __create_consumer_list_db();
1300 if (result != DATA_CONTROL_ERROR_NONE) {
1301 _LOGE("fail to create consumer list db");
1305 provider = malloc(sizeof(struct data_control_s));
1306 if (provider == NULL) {
1307 _LOGE("Out of memory. fail to alloc provider.");
1308 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
1310 provider->provider_id =
1311 (char *)bundle_get_val(b, OSP_K_DATACONTROL_PROVIDER);
1312 provider->data_id = (char *)bundle_get_val(b, OSP_K_DATACONTROL_DATA);
1313 if (provider->provider_id == NULL || provider->data_id == NULL) {
1314 _LOGE("invalid provider value %s, %s",
1315 provider->provider_id, provider->data_id);
1317 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1320 _LOGI("Noti Provider ID: %s, data ID: %s, request type: %d",
1321 provider->provider_id, provider->data_id, type);
1322 path = _get_encoded_path(provider, caller_app_id);
1324 _LOGE("can not get encoded path out of memory");
1326 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
1329 callback_id_str = (char *)bundle_get_val(b, OSP_K_DATA_CHANGED_CALLBACK_ID);
1330 if (callback_id_str == NULL) {
1331 _LOGE("callback_id is NULL");
1332 result = DATA_CONTROL_ERROR_IO_ERROR;
1335 callback_id = atoi(callback_id_str);
1338 case DATACONTROL_TYPE_ADD_DATA_CHANGED_CB:
1340 _LOGI("DATACONTROL_TYPE_ADD_DATA_CHANGED_CB called");
1341 result_type = DATACONTROL_DATA_CHANGE_CALLBACK_ADD_RESULT;
1342 filter_iter = g_list_first(__noti_consumer_filter_info_list);
1343 for (; filter_iter != NULL; filter_iter = filter_iter->next) {
1345 (changed_noti_consumer_filter_info_s *)filter_iter->data;
1346 noti_allow = filter_info->callback(
1347 (data_control_h)provider,
1349 filter_info->user_data);
1353 _LOGI("provider_sql_consumer_filter_cb result %d", noti_allow);
1356 result = __insert_consumer_list_db_info(
1358 provider->provider_id,
1362 if (result != DATA_CONTROL_ERROR_NONE) {
1363 _LOGE("fail to set consumer list db info %d", result);
1364 result = DATA_CONTROL_ERROR_PERMISSION_DENIED;
1368 result = __set_consumer_app_list(
1372 if (result != DATA_CONTROL_ERROR_NONE)
1373 _LOGE("fail to __set_consumer_app_list");
1376 result = DATA_CONTROL_ERROR_PERMISSION_DENIED;
1381 case DATACONTROL_TYPE_REMOVE_DATA_CHANGED_CB:
1383 _LOGI("DATACONTROL_NOTI_REMOVE_DATA_CHANGED_CB called");
1384 result_type = DATACONTROL_DATA_CHANGE_CALLBACK_REMOVE_RESULT;
1385 if (__noti_consumer_app_list) {
1386 __free_consumer_info(unique_id);
1387 _LOGI("unregister %s from __noti_consumer_app_list", unique_id);
1389 _LOGI("empty __consumer_app_list");
1391 result = __delete_consumer_list_db_info(unique_id);
1392 if (result != DATA_CONTROL_ERROR_NONE) {
1393 _LOGE("__delete_consumer_list_db_info fail %d", result);
1394 result = DATA_CONTROL_ERROR_IO_ERROR;
1404 __send_add_callback_result(
1405 result_type, unique_id, path, callback_id, result);
1415 int __provider_process(bundle *b, int fd, const char *consumer_appid)
1419 const char **arg_list = NULL;
1420 const char **column_list = NULL;
1421 data_control_h provider = NULL;
1422 int provider_req_id = 0;
1427 bundle *value = NULL;
1429 const char *request_type =
1430 bundle_get_val(b, OSP_K_DATACONTROL_REQUEST_TYPE);
1431 if (request_type == NULL) {
1432 _LOGE("Invalid data control request");
1433 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1436 /* Get the request type */
1437 datacontrol_request_type type = atoi(request_type);
1438 if (type >= DATACONTROL_TYPE_SQL_SELECT &&
1439 type <= DATACONTROL_TYPE_SQL_BULK_INSERT) {
1441 } else if (type >= DATACONTROL_TYPE_MAP_GET &&
1442 type <= DATACONTROL_TYPE_MAP_BULK_ADD) {
1445 _LOGE("Invalid request type");
1446 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1449 caller_appid = (char *)bundle_get_val(b, AUL_K_CALLER_APPID);
1450 if (caller_appid && strncmp(caller_appid,
1451 consumer_appid, strlen(consumer_appid)) != 0) {
1452 _LOGE("The passed appid(%s) is different from the registered appid(%s).",
1453 caller_appid, consumer_appid);
1454 return DATA_CONTROL_ERROR_NONE;
1457 provider_id = (char *)bundle_get_val(b, OSP_K_DATACONTROL_PROVIDER);
1458 ret = _check_cert(provider_id, is_map, consumer_appid);
1459 if (ret != DATA_CONTROL_ERROR_NONE) {
1460 if (ret == DATA_CONTROL_ERROR_PERMISSION_DENIED) {
1461 _LOGE("The consumer (%s) is not signed with the same certificate",
1463 return DATA_CONTROL_ERROR_NONE;
1465 return DATA_CONTROL_ERROR_IO_ERROR;
1469 arg_list = bundle_get_str_array(b, OSP_K_ARG, &len);
1471 provider = malloc(sizeof(struct data_control_s));
1472 if (provider == NULL) {
1473 _LOGE("Out of memory. fail to alloc provider.");
1474 return DATA_CONTROL_ERROR_IO_ERROR;
1477 /* Set the provider ID */
1478 provider->provider_id = provider_id;
1480 /* Set the data ID */
1481 provider->data_id = (char *)arg_list[PACKET_INDEX_DATAID];
1483 /* Set the request ID */
1484 provider_req_id = __provider_new_request_id();
1486 _LOGI("Provider ID: %s, data ID: %s, request type: %s",
1487 provider->provider_id, provider->data_id, request_type);
1489 /* Add the data to the table */
1490 key = malloc(sizeof(int));
1492 _LOGE("Out of memory. fail to malloc key");
1495 *key = provider_req_id;
1497 value = bundle_dup(b);
1498 if (value == NULL) {
1499 _LOGE("Fail to dup value");
1502 g_hash_table_insert(__request_table, key, value);
1505 case DATACONTROL_TYPE_SQL_SELECT:
1509 int column_count = __get_int_from_str(arg_list[i++]); /* Column count */
1511 if (column_count <= 0 || column_count > MAX_COLUMN_COUNT) {
1512 _LOGE("Invalid column count %d", column_count);
1516 _LOGI("SELECT column count: %d", column_count);
1517 column_list = (const char **)malloc(column_count * (sizeof(char *)));
1518 if (column_list == NULL) {
1519 _LOGE("Out of memory. Fail to malloc column_list.");
1523 while (current < column_count) {
1524 column_list[current++] = arg_list[i++]; /* Column data */
1525 _LOGI("Column %d: %s", current, column_list[current-1]);
1528 const char *where = arg_list[i++]; /* where */
1529 const char *order = arg_list[i++]; /* order */
1530 _LOGI("where: %s, order: %s", where, order);
1532 if (strncmp(where, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
1535 if (strncmp(order, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
1538 const char *page_number = arg_list[i++];
1539 const char *per_page = arg_list[i];
1541 _LOGI("page_number: %s, per_page: %s", page_number, per_page);
1542 bundle_add_str(value, RESULT_PAGE_NUMBER, page_number);
1543 bundle_add_str(value, MAX_COUNT_PER_PAGE, per_page);
1544 if (sql_callback.select_cb) {
1545 sql_callback.select_cb(
1554 _LOGE("sql_callback.select_cb is NULL");
1559 case DATACONTROL_TYPE_SQL_INSERT:
1560 case DATACONTROL_TYPE_SQL_UPDATE:
1562 _LOGI("INSERT / UPDATE handler");
1563 bundle *sql = __get_bundle_data_from_fd(fd);
1565 _LOGE("__get_bundle_data_from_fd fail");
1568 if (type == DATACONTROL_TYPE_SQL_INSERT) {
1569 if (sql_callback.insert_cb) {
1570 sql_callback.insert_cb(
1576 _LOGE("sql_callback.insert_cb is NULL");
1579 const char *where = arg_list[PACKET_INDEX_UPDATEWHERE];
1580 _LOGI("UPDATE from where: %s", where);
1581 if (strncmp(where, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
1583 if (sql_callback.update_cb) {
1584 sql_callback.update_cb(
1591 _LOGE("sql_callback.update_cb is NULL");
1597 case DATACONTROL_TYPE_SQL_BULK_INSERT:
1599 _LOGI("BULK INSERT handler");
1600 data_control_bulk_data_h data = __get_bulk_data_from_fd(fd);
1602 _LOGE("__get_bulk_data_from_fd fail");
1606 if (sql_bulk_callback) {
1611 sql_bulk_callback_user_data);
1613 _LOGE("sql_bulk_callback is NULL");
1616 data_control_bulk_data_destroy(data);
1619 case DATACONTROL_TYPE_SQL_DELETE:
1621 const char *where = arg_list[PACKET_INDEX_DELETEWHERE];
1622 _LOGI("DELETE from where: %s", where);
1623 if (strncmp(where, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
1625 if (sql_callback.delete_cb) {
1626 sql_callback.delete_cb(
1632 _LOGE("sql_callback.delete_cb is NULL");
1636 case DATACONTROL_TYPE_MAP_GET:
1638 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
1639 const char *page_number = arg_list[PACKET_INDEX_MAP_PAGE_NO];
1640 const char *count_per_page = arg_list[PACKET_INDEX_MAP_COUNT_PER_PAGE];
1641 bundle_add_str(value, RESULT_PAGE_NUMBER, page_number);
1642 bundle_add_str(value, MAX_COUNT_PER_PAGE, count_per_page);
1643 _LOGI("Gets the value list related with the key(%s) from Map datacontrol. ", map_key);
1644 if (map_callback.get_cb) {
1645 map_callback.get_cb(
1651 _LOGE("map_callback.get_cb is NULL");
1655 case DATACONTROL_TYPE_MAP_SET:
1657 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
1658 const char *old_value = arg_list[PACKET_INDEX_MAP_VALUE_1ST];
1659 const char *new_value = arg_list[PACKET_INDEX_MAP_VALUE_2ND];
1660 _LOGI("Sets the old value(%s) of the key(%s) to the new value(%s) in Map datacontrol.", old_value, map_key, new_value);
1661 if (map_callback.set_cb) {
1662 map_callback.set_cb(
1670 _LOGE("map_callback.set_cb is NULL");
1674 case DATACONTROL_TYPE_MAP_ADD:
1676 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
1677 const char *map_value = arg_list[PACKET_INDEX_MAP_VALUE_1ST];
1678 _LOGI("Adds the %s-%s in Map datacontrol.", map_key, map_value);
1679 if (map_callback.add_cb) {
1680 map_callback.add_cb(
1687 _LOGE("map_callback.add_cb is NULL");
1691 case DATACONTROL_TYPE_MAP_BULK_ADD:
1693 _LOGI("BULK ADD handler");
1694 data_control_bulk_data_h data = __get_bulk_data_from_fd(fd);
1696 _LOGE("__get_bulk_data_from_fd fail");
1700 if (map_bulk_callback) {
1701 map_bulk_callback(provider_req_id,
1704 map_bulk_callback_user_data);
1705 _LOGI("bulk_add callback call done");
1707 _LOGE("map_bulk_callback is NULL");
1710 data_control_bulk_data_destroy(data);
1713 case DATACONTROL_TYPE_MAP_REMOVE:
1715 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
1716 const char *map_value = arg_list[PACKET_INDEX_MAP_VALUE_1ST];
1717 _LOGI("Removes the %s-%s in Map datacontrol.", map_key, map_value);
1718 if (map_callback.remove_cb) {
1719 map_callback.remove_cb(
1726 _LOGE("map_callback.remove_cb is NULL");
1736 return DATA_CONTROL_ERROR_NONE;
1746 return DATA_CONTROL_ERROR_IO_ERROR;
1749 gboolean __provider_recv_message(GIOChannel *channel,
1755 char *consumer_appid = (char *)data;
1758 gint fd = g_io_channel_unix_get_fd(channel);
1759 gboolean retval = TRUE;
1761 _LOGI("__provider_recv_message : ...from %d:%s%s%s%s\n", fd,
1762 (cond & G_IO_ERR) ? " ERR" : "",
1763 (cond & G_IO_HUP) ? " HUP" : "",
1764 (cond & G_IO_IN) ? " IN" : "",
1765 (cond & G_IO_PRI) ? " PRI" : "");
1767 if (cond & (G_IO_ERR | G_IO_HUP))
1770 if (cond & G_IO_IN) {
1771 if (_read_socket(fd, (char *)&data_len, sizeof(data_len), &nb) !=
1772 DATA_CONTROL_ERROR_NONE) {
1773 _LOGE("read socket fail : data_len");
1777 _LOGI("data_len : %d", data_len);
1780 _LOGI("__provider_recv_message : ...from %d: EOF\n", fd);
1784 _LOGI("__provider_recv_message: ...from %d: %d bytes\n", fd, data_len);
1785 if (data_len > 0 && data_len < MAX_REQUEST_ARGUMENT_SIZE) {
1788 buf = (char *)calloc(data_len + 1, sizeof(char));
1790 _LOGE("calloc failed");
1794 if (_read_socket(fd, buf, data_len, &nb) !=
1795 DATA_CONTROL_ERROR_NONE) {
1796 _LOGI("read socket fail : data_len\n");
1801 _LOGI("__provider_recv_message: nb 0 : EOF\n");
1805 kb = bundle_decode_raw((bundle_raw *)buf, data_len);
1806 if (__provider_process(kb, fd, consumer_appid) !=
1807 DATA_CONTROL_ERROR_NONE) {
1819 if (consumer_appid != NULL)
1820 g_hash_table_remove(__socket_pair_hash, consumer_appid);
1827 int __datacontrol_handler_cb(bundle *b, int request_id, void *data)
1829 datacontrol_socket_info *socket_info;
1830 int ret = DATA_CONTROL_ERROR_NONE;
1835 const char *request_type =
1836 bundle_get_val(b, OSP_K_DATACONTROL_REQUEST_TYPE);
1837 if (request_type == NULL) {
1838 caller = bundle_get_val(b, AUL_K_CALLER_APPID);
1839 callee = bundle_get_val(b, AUL_K_CALLEE_APPID);
1841 if (caller == NULL) {
1842 _LOGE("Invalid data control request caller is NULL");
1843 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1846 dup_caller = strdup(caller);
1847 socket_info = g_hash_table_lookup(__socket_pair_hash, dup_caller);
1849 if (socket_info != NULL)
1850 g_hash_table_remove(__socket_pair_hash, dup_caller);
1852 ret = _add_watch_on_socket_info(dup_caller, callee, "provider",
1853 NULL, NULL, __provider_recv_message,
1854 dup_caller, &socket_info);
1855 if (ret != DATA_CONTROL_ERROR_NONE)
1858 g_hash_table_insert(__socket_pair_hash, dup_caller, socket_info);
1860 /* Get the request type */
1861 datacontrol_request_type type = atoi(request_type);
1862 if (type == DATACONTROL_TYPE_ADD_DATA_CHANGED_CB ||
1863 type == DATACONTROL_TYPE_REMOVE_DATA_CHANGED_CB) {
1864 __provider_noti_process(b, type);
1866 _LOGE("Invalid data control request");
1867 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1874 EXPORT_API int data_control_provider_sql_register_cb(
1875 data_control_provider_sql_cb *callback, void *user_data)
1877 int retval = _check_privilege(PRIVILEGE_PROVIDER);
1878 if (retval != DATA_CONTROL_ERROR_NONE)
1882 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1884 if (__request_table == NULL)
1885 __initialize_provider();
1887 memcpy(&sql_callback, callback, sizeof(data_control_provider_sql_cb));
1888 sql_user_data = user_data;
1890 /* If the provider_map_cb was registered(not NULL),
1891 * __datacontrol_handler_cb is set already. */
1892 if (__is_null_map_callback(map_callback))
1893 retval = aul_set_data_control_provider_cb(__datacontrol_handler_cb);
1898 EXPORT_API int data_control_provider_sql_unregister_cb(void)
1900 /* When both SQL_cb and Map_cb are unregisted,
1901 * unsetting the provider cb is possible. */
1902 if (__is_null_map_callback(map_callback))
1903 aul_unset_data_control_provider_cb();
1905 memset(&sql_callback, 0, sizeof(data_control_provider_sql_cb));
1906 sql_user_data = NULL;
1908 return DATA_CONTROL_ERROR_NONE;
1911 EXPORT_API int data_control_provider_sql_register_insert_bulk_data_request_cb(
1912 data_control_provider_bulk_cb callback, void *user_data)
1914 int retval = _check_privilege(PRIVILEGE_PROVIDER);
1915 if (retval != DATA_CONTROL_ERROR_NONE)
1918 if (callback == NULL)
1919 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1921 sql_bulk_callback = callback;
1922 sql_bulk_callback_user_data = user_data;
1927 EXPORT_API int data_control_provider_sql_unregister_insert_bulk_data_request_cb(void)
1929 sql_bulk_callback = NULL;
1930 sql_bulk_callback_user_data = NULL;
1932 return DATA_CONTROL_ERROR_NONE;
1935 EXPORT_API int data_control_provider_map_register_cb(
1936 data_control_provider_map_cb *callback, void *user_data)
1938 int retval = _check_privilege(PRIVILEGE_PROVIDER);
1939 if (retval != DATA_CONTROL_ERROR_NONE)
1943 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1945 if (__request_table == NULL)
1946 __initialize_provider();
1948 memcpy(&map_callback, callback, sizeof(data_control_provider_map_cb));
1949 map_user_data = user_data;
1951 /* If the provider_sql_cb was registered(not NULL),
1952 * __datacontrol_handler_cb is set already. */
1953 if (__is_null_sql_callback(sql_callback))
1954 retval = aul_set_data_control_provider_cb(__datacontrol_handler_cb);
1959 EXPORT_API int data_control_provider_map_unregister_cb(void)
1961 /* When both SQL_cb and Map_cb are unregisted,
1962 * unsetting the provider cb is possible. */
1963 if (__is_null_sql_callback(sql_callback))
1964 aul_unset_data_control_provider_cb();
1966 memset(&map_callback, 0, sizeof(data_control_provider_map_cb));
1967 map_user_data = NULL;
1969 return DATA_CONTROL_ERROR_NONE;
1972 EXPORT_API int data_control_provider_map_register_add_bulk_data_request_cb(
1973 data_control_provider_bulk_cb callback, void *user_data)
1975 int retval = _check_privilege(PRIVILEGE_PROVIDER);
1976 if (retval != DATA_CONTROL_ERROR_NONE)
1979 if (callback == NULL)
1980 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
1982 map_bulk_callback = callback;
1983 map_bulk_callback_user_data = user_data;
1988 EXPORT_API int data_control_provider_map_unregister_add_bulk_data_request_cb(void)
1990 map_bulk_callback = NULL;
1991 map_bulk_callback_user_data = NULL;
1993 return DATA_CONTROL_ERROR_NONE;
1996 EXPORT_API int data_control_provider_get_client_appid(
1997 int request_id, char **appid)
2002 if (__request_table == NULL)
2003 __initialize_provider();
2005 b = g_hash_table_lookup(__request_table, &request_id);
2007 _LOGE("No data for the request id: %d", request_id);
2008 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2011 caller = bundle_get_val(b, AUL_K_CALLER_APPID);
2013 _LOGE("No appid for the request id: %d", request_id);
2014 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2017 _LOGI("Request ID: %d, caller appid: %s", request_id, caller);
2019 *appid = strdup(caller);
2021 return DATA_CONTROL_ERROR_NONE;
2024 EXPORT_API int data_control_provider_send_bulk_insert_result(
2025 int request_id, data_control_bulk_result_data_h bulk_results)
2028 int ret = DATA_CONTROL_ERROR_NONE;
2032 if (bulk_results == NULL) {
2033 _LOGE("Null bulk result data");
2034 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2037 ret = data_control_bulk_result_data_get_count(bulk_results, &count);
2038 if (ret != DATA_CONTROL_ERROR_NONE) {
2039 _LOGE("Fail to get count from bulk result data");
2040 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2044 _LOGE("Bulk result data is empty");
2045 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2048 _LOGI("Send an insert result for request id: %d", request_id);
2050 if (__request_table == NULL)
2051 __initialize_provider();
2053 b = g_hash_table_lookup(__request_table, &request_id);
2055 _LOGE("No data for the request id: %d", request_id);
2056 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2058 res = __set_result(b, DATACONTROL_TYPE_SQL_BULK_INSERT, NULL);
2059 ret = __send_result(
2060 res, DATACONTROL_TYPE_SQL_BULK_INSERT, (void *)bulk_results);
2062 g_hash_table_remove(__request_table, &request_id);
2067 EXPORT_API int data_control_provider_send_select_result(
2068 int request_id, void *db_handle)
2074 _LOGI("Send a select result for request id: %d", request_id);
2076 if (__request_table == NULL)
2077 __initialize_provider();
2079 b = g_hash_table_lookup(__request_table, &request_id);
2081 _LOGE("No data for the request id: %d", request_id);
2082 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2085 res = __set_result(b, DATACONTROL_TYPE_SQL_SELECT, db_handle);
2086 ret = __send_result(res, DATACONTROL_TYPE_SQL_SELECT, db_handle);
2088 g_hash_table_remove(__request_table, &request_id);
2093 EXPORT_API int data_control_provider_send_insert_result(
2094 int request_id, long long row_id)
2100 _LOGI("Send an insert result for request id: %d", request_id);
2102 if (__request_table == NULL)
2103 __initialize_provider();
2105 b = g_hash_table_lookup(__request_table, &request_id);
2107 _LOGE("No data for the request id: %d", request_id);
2108 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2111 res = __set_result(b, DATACONTROL_TYPE_SQL_INSERT, (void *)&row_id);
2113 ret = __send_result(res, DATACONTROL_TYPE_SQL_INSERT, NULL);
2115 g_hash_table_remove(__request_table, &request_id);
2120 EXPORT_API int data_control_provider_send_update_result(int request_id)
2126 _LOGI("Send an update result for request id: %d", request_id);
2128 if (__request_table == NULL)
2129 __initialize_provider();
2131 b = g_hash_table_lookup(__request_table, &request_id);
2133 _LOGE("No data for the request id: %d", request_id);
2134 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2137 res = __set_result(b, DATACONTROL_TYPE_SQL_UPDATE, NULL);
2139 ret = __send_result(res, DATACONTROL_TYPE_SQL_UPDATE, NULL);
2141 g_hash_table_remove(__request_table, &request_id);
2146 EXPORT_API int data_control_provider_send_delete_result(int request_id)
2152 _LOGI("Send a delete result for request id: %d", request_id);
2154 if (__request_table == NULL)
2155 __initialize_provider();
2157 b = g_hash_table_lookup(__request_table, &request_id);
2159 _LOGE("No data for the request id: %d", request_id);
2160 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2163 res = __set_result(b, DATACONTROL_TYPE_SQL_DELETE, NULL);
2165 ret = __send_result(res, DATACONTROL_TYPE_SQL_DELETE, NULL);
2167 g_hash_table_remove(__request_table, &request_id);
2172 EXPORT_API int data_control_provider_send_error(
2173 int request_id, const char *error)
2179 _LOGI("Send an error for request id: %d", request_id);
2181 if (__request_table == NULL)
2182 __initialize_provider();
2184 b = g_hash_table_lookup(__request_table, &request_id);
2186 _LOGE("No data for the request id: %d", request_id);
2187 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2190 res = __set_result(b, DATACONTROL_TYPE_ERROR, (void *)error);
2191 ret = __send_result(res, DATACONTROL_TYPE_ERROR, NULL);
2197 static void bundle_foreach_cb(const char *key, const int type,
2198 const bundle_keyval_t *kv, void *user_data)
2202 size_t value_len = 0;
2204 if (!key || !kv || !user_data)
2207 key_val_pair *pair = (key_val_pair *)user_data;
2208 index = pair->no_of_elements;
2210 pair->keys[index] = strdup(key);
2212 bundle_keyval_get_basic_val((bundle_keyval_t *)kv, (void **)&value, &value_len);
2213 pair->vals[index] = strdup(value);
2214 pair->length += strlen(key) + value_len;
2216 ++(pair->no_of_elements);
2221 EXPORT_API char *data_control_provider_create_insert_statement(
2222 data_control_h provider, bundle *insert_map)
2225 char *data_id = NULL;
2228 int row_count = bundle_get_count(insert_map);
2230 if (provider == NULL || row_count == 0) {
2231 set_last_result(DATA_CONTROL_ERROR_INVALID_PARAMETER);
2235 key_val_pair *cols = (key_val_pair *) calloc(1, sizeof(key_val_pair));
2237 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2241 cols->no_of_elements = 0;
2243 cols->keys = (char **) calloc(row_count, sizeof(char *));
2244 if (cols->keys == NULL) {
2246 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2250 cols->vals = (char **) calloc(row_count, sizeof(char *));
2251 if (cols->vals == NULL) {
2254 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2259 bundle_foreach(insert_map, bundle_foreach_cb, (void *)(cols));
2261 _get_data_id(provider, &data_id);
2263 sql_len = INSERT_STMT_CONST_LEN + strlen(data_id) +
2264 (row_count - 1) * 4 + (cols->length) + 1;
2266 _LOGI("SQL statement length: %d", sql_len);
2268 sql = (char *)calloc(sql_len, sizeof(char));
2274 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2277 memset(sql, 0, sql_len);
2279 snprintf(sql, sql_len, "INSERT INTO %s (", data_id);
2282 for (index = 0; index < row_count - 1; index++) {
2283 strncat(sql, cols->keys[index], sql_len - (strlen(sql) + 1));
2284 strncat(sql, ", ", sql_len - (strlen(sql) + 1));
2287 strncat(sql, cols->keys[index], sql_len - (strlen(sql) + 1));
2288 strncat(sql, ") VALUES (", sql_len - (strlen(sql) + 1));
2290 for (index = 0; index < row_count - 1; index++) {
2291 strncat(sql, cols->vals[index], sql_len - (strlen(sql) + 1));
2292 strncat(sql, ", ", sql_len - (strlen(sql) + 1));
2295 strncat(sql, cols->vals[index], sql_len - (strlen(sql) + 1));
2296 strncat(sql, ")", sql_len - (strlen(sql) + 1));
2298 _LOGI("SQL statement is: %s", sql);
2300 for (index = 0; index < row_count; index++) {
2301 free(cols->keys[index]);
2302 free(cols->vals[index]);
2311 EXPORT_API char *data_control_provider_create_delete_statement(
2312 data_control_h provider, const char *where)
2314 char *data_id = NULL;
2319 if (provider == NULL) {
2320 set_last_result(DATA_CONTROL_ERROR_INVALID_PARAMETER);
2324 _get_data_id(provider, &data_id);
2326 cond_len = (where != NULL) ? (WHERE_COND_CONST_LEN + strlen(where)) : 0;
2327 sql_len = DELETE_STMT_CONST_LEN + strlen(data_id) + cond_len + 1;
2329 _LOGI("SQL statement length: %d", sql_len);
2331 sql = (char *)calloc(sql_len, sizeof(char));
2334 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2337 memset(sql, 0, sql_len);
2339 snprintf(sql, sql_len, "DELETE FROM %s", data_id);
2341 strncat(sql, " WHERE ", sql_len - (strlen(sql) + 1));
2342 strncat(sql, where, sql_len - (strlen(sql) + 1));
2345 _LOGI("SQL statement is: %s", sql);
2351 EXPORT_API char *data_control_provider_create_update_statement(
2352 data_control_h provider, bundle *update_map, const char *where)
2355 char *data_id = NULL;
2359 int row_count = bundle_get_count(update_map);
2361 if (provider == NULL || row_count == 0) {
2362 set_last_result(DATA_CONTROL_ERROR_INVALID_PARAMETER);
2366 key_val_pair *cols = (key_val_pair *) calloc(1, sizeof(key_val_pair));
2368 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2372 cols->no_of_elements = 0;
2374 cols->keys = (char **)calloc(row_count, sizeof(char *));
2375 if (cols->keys == NULL) {
2377 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2380 cols->vals = (char **)calloc(row_count, sizeof(char *));
2381 if (cols->vals == NULL) {
2384 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2389 bundle_foreach(update_map, bundle_foreach_cb, (void *)(cols));
2391 _get_data_id(provider, &data_id);
2393 cond_len = (where != NULL) ? (WHERE_COND_CONST_LEN + strlen(where)) : 0;
2394 sql_len = UPDATE_STMT_CONST_LEN + strlen(data_id) + (cols->length) +
2395 (row_count - 1) * 5 + cond_len + 1;
2397 _LOGI("SQL statement length: %d", sql_len);
2399 sql = (char *) calloc(sql_len, sizeof(char));
2405 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2408 memset(sql, 0, sql_len);
2410 snprintf(sql, sql_len, "UPDATE %s SET ", data_id);
2413 for (index = 0; index < row_count - 1; index++) {
2414 strncat(sql, cols->keys[index], sql_len - (strlen(sql) + 1));
2415 strncat(sql, " = ", sql_len - (strlen(sql) + 1));
2416 strncat(sql, cols->vals[index], sql_len - (strlen(sql) + 1));
2417 strncat(sql, ", ", sql_len - (strlen(sql) + 1));
2420 strncat(sql, cols->keys[index], sql_len - (strlen(sql) + 1));
2421 strncat(sql, " = ", sql_len - (strlen(sql) + 1));
2422 strncat(sql, cols->vals[index], sql_len - (strlen(sql) + 1));
2425 strncat(sql, " WHERE ", sql_len - (strlen(sql) + 1));
2426 strncat(sql, where, sql_len - (strlen(sql) + 1));
2429 _LOGI("SQL statement is: %s", sql);
2431 for (index = 0; index < row_count; index++) {
2432 free(cols->keys[index]);
2433 free(cols->vals[index]);
2442 EXPORT_API char *data_control_provider_create_select_statement(
2443 data_control_h provider, const char **column_list,
2444 int column_count, const char *where, const char *order)
2447 int col_name_length = 0;
2448 char *data_id = NULL;
2454 if (provider == NULL) {
2455 set_last_result(DATA_CONTROL_ERROR_INVALID_PARAMETER);
2460 for (index = 0; index < column_count; index++)
2461 col_name_length += strlen(column_list[index]);
2463 if (column_count > 0)
2464 col_name_length += (column_count - 1) * 2;
2466 col_name_length = 1;
2469 _get_data_id(provider, &data_id);
2471 cond_len = (where != NULL) ? (WHERE_COND_CONST_LEN + strlen(where)) : 0;
2472 order_len = (order != NULL) ? (ORDER_CLS_CONST_LEN + strlen(order)) : 0;
2473 sql_len = SELECT_STMT_CONST_LEN + col_name_length + strlen(data_id) +
2474 cond_len + order_len + 1;
2476 _LOGI("SQL statement length: %d", sql_len);
2478 sql = (char *) calloc(sql_len, sizeof(char));
2481 set_last_result(DATA_CONTROL_ERROR_OUT_OF_MEMORY);
2484 memset(sql, 0, sql_len);
2486 strncpy(sql, "SELECT ", sql_len);
2488 strncat(sql, "*", sql_len - (strlen(sql) + 1));
2490 for (index = 0; index < column_count - 1; index++) {
2491 strncat(sql, column_list[index], sql_len - (strlen(sql) + 1));
2492 strncat(sql, ", ", sql_len - (strlen(sql) + 1));
2494 strncat(sql, column_list[index], sql_len - (strlen(sql) + 1));
2497 strncat(sql, " FROM ", sql_len - (strlen(sql) + 1));
2498 strncat(sql, data_id, sql_len - (strlen(sql) + 1));
2501 strncat(sql, " WHERE ", sql_len - (strlen(sql) + 1));
2502 strncat(sql, where, sql_len - (strlen(sql) + 1));
2505 strncat(sql, " ORDER BY ", sql_len - (strlen(sql) + 1));
2506 strncat(sql, order, sql_len - (strlen(sql) + 1));
2509 _LOGI("SQL statement is: %s", sql);
2515 EXPORT_API bool data_control_provider_match_provider_id(
2516 data_control_h provider, const char *provider_id)
2518 int ret = DATA_CONTROL_ERROR_NONE;
2519 char *prov_id = NULL;
2521 if (provider == NULL || provider_id == NULL) {
2522 set_last_result(DATA_CONTROL_ERROR_INVALID_PARAMETER);
2526 ret = _get_provider_id(provider, &prov_id);
2527 set_last_result(ret);
2528 if (ret != DATA_CONTROL_ERROR_NONE)
2531 if (strcmp(prov_id, provider_id) == 0) {
2540 EXPORT_API bool data_control_provider_match_data_id(
2541 data_control_h provider, const char *data_id)
2543 int ret = DATA_CONTROL_ERROR_NONE;
2546 if (provider == NULL || data_id == NULL) {
2547 set_last_result(DATA_CONTROL_ERROR_INVALID_PARAMETER);
2551 ret = _get_data_id(provider, &data);
2552 set_last_result(ret);
2553 if (ret != DATA_CONTROL_ERROR_NONE)
2556 if (strcmp(data, data_id) == 0) {
2565 EXPORT_API int data_control_provider_send_map_bulk_add_result(
2566 int request_id, data_control_bulk_result_data_h bulk_results)
2569 int ret = DATA_CONTROL_ERROR_NONE;
2573 if (bulk_results == NULL) {
2574 _LOGE("Null bulk result data");
2575 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2578 ret = data_control_bulk_result_data_get_count(bulk_results, &count);
2579 if (ret != DATA_CONTROL_ERROR_NONE) {
2580 _LOGE("Fail to get count from bulk result data");
2581 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2585 _LOGE("Bulk result data is empty");
2586 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2589 _LOGI("Send bulk add result for request id: %d", request_id);
2591 if (__request_table == NULL)
2592 __initialize_provider();
2594 b = g_hash_table_lookup(__request_table, &request_id);
2596 _LOGE("No data for the request id: %d", request_id);
2597 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2599 res = __set_result(b, DATACONTROL_TYPE_MAP_BULK_ADD, NULL);
2600 ret = __send_result(res, DATACONTROL_TYPE_MAP_BULK_ADD,
2601 (void *)bulk_results);
2603 g_hash_table_remove(__request_table, &request_id);
2608 EXPORT_API int data_control_provider_send_map_result(int request_id)
2614 _LOGI("Send a set/add/remove result for request id: %d", request_id);
2616 if (__request_table == NULL)
2617 __initialize_provider();
2619 b = g_hash_table_lookup(__request_table, &request_id);
2621 _LOGE("No data for the request id: %d", request_id);
2622 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2625 res = __set_result(b, DATACONTROL_TYPE_UNDEFINED, NULL);
2627 ret = __send_result(res, DATACONTROL_TYPE_UNDEFINED, NULL);
2629 g_hash_table_remove(__request_table, &request_id);
2634 EXPORT_API int data_control_provider_send_map_get_value_result(
2635 int request_id, char **value_list, int value_count)
2638 char value_count_str[32];
2642 _LOGI("Send a get result for request id: %d", request_id);
2644 if (__request_table == NULL)
2645 __initialize_provider();
2647 b = g_hash_table_lookup(__request_table, &request_id);
2649 _LOGE("No data for the request id: %d", request_id);
2650 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2653 snprintf(value_count_str, 32, "%d", value_count);
2654 bundle_add_str(b, RESULT_VALUE_COUNT, value_count_str);
2656 res = __set_result(b, DATACONTROL_TYPE_MAP_GET, value_list);
2658 ret = __send_result(res, DATACONTROL_TYPE_MAP_GET, value_list);
2660 g_hash_table_remove(__request_table, &request_id);
2665 static int __send_signal_to_consumer(data_control_h provider,
2668 data_control_data_change_type_e type,
2671 int result = DATA_CONTROL_ERROR_NONE;
2673 bundle_raw *raw = NULL;
2675 gboolean signal_result = TRUE;
2678 if (bundle_encode(data, &raw, &len) != BUNDLE_ERROR_NONE) {
2679 _LOGE("bundle_encode fail");
2680 result = DATA_CONTROL_ERROR_IO_ERROR;
2685 _LOGI("emit signal to object path %s", path);
2686 signal_result = g_dbus_connection_emit_signal(
2687 _get_dbus_connection(),
2690 DATA_CONTROL_INTERFACE_NAME,
2691 DATA_CONTROL_DATA_CHANGE_DATA_CHANGED,
2692 g_variant_new("(isssi)",
2694 provider->provider_id,
2696 ((raw) ? (char *)raw : ""),
2699 if (signal_result == FALSE) {
2700 _LOGE("g_dbus_connection_emit_signal() is failed");
2702 _LOGE("g_dbus_connection_emit_signal() err : %s",
2706 result = DATA_CONTROL_ERROR_IO_ERROR;
2717 EXPORT_API int data_control_provider_add_data_change_consumer_filter_cb(
2718 data_control_provider_data_change_consumer_filter_cb callback,
2723 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2724 if (callback_id == NULL)
2725 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2727 changed_noti_consumer_filter_info_s *filter_info =
2728 (changed_noti_consumer_filter_info_s *)calloc(1, sizeof(changed_noti_consumer_filter_info_s));
2729 if (filter_info == NULL) {
2730 _LOGE("out of memory.");
2731 return DATA_CONTROL_ERROR_OUT_OF_MEMORY;
2734 *callback_id = __datacontrol_get_data_changed_filter_callback_id();
2736 filter_info->callback_id = *callback_id;
2737 filter_info->callback = callback;
2738 filter_info->user_data = user_data;
2739 __noti_consumer_filter_info_list =
2740 g_list_append(__noti_consumer_filter_info_list, filter_info);
2742 return DATA_CONTROL_ERROR_NONE;
2745 EXPORT_API int data_control_provider_remove_data_change_consumer_filter_cb(int callback_id)
2748 changed_noti_consumer_filter_info_s filter_info;
2749 changed_noti_consumer_filter_info_s *temp;
2751 if (callback_id < 1)
2752 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2754 filter_info.callback_id = callback_id;
2755 find_list = g_list_find_custom(__noti_consumer_filter_info_list, &filter_info,
2756 (GCompareFunc)__data_changed_filter_cb_info_compare_cb);
2757 if (find_list != NULL) {
2758 temp = (changed_noti_consumer_filter_info_s *)find_list->data;
2759 __noti_consumer_filter_info_list =
2760 g_list_remove(__noti_consumer_filter_info_list, find_list->data);
2763 _LOGE("invalid callback_id : %d", callback_id);
2764 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2767 return DATA_CONTROL_ERROR_NONE;
2770 EXPORT_API int data_control_provider_send_data_change_noti(
2771 data_control_h provider,
2772 data_control_data_change_type_e type,
2775 int retval = _check_privilege(PRIVILEGE_PROVIDER);
2776 GList *consumer_iter = NULL;
2777 datacontrol_consumer_info *consumer_info = NULL;
2779 if (retval != DATA_CONTROL_ERROR_NONE)
2782 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2783 if (provider->provider_id == NULL || provider->data_id == NULL)
2784 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2786 if (type < DATA_CONTROL_DATA_CHANGE_SQL_UPDATE ||
2787 type > DATA_CONTROL_DATA_CHANGE_MAP_REMOVE)
2788 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2790 _LOGE("datacontrol_provider_send_data_change_noti %d, %d",
2791 g_list_length(__noti_consumer_app_list), type);
2792 consumer_iter = g_list_first(__noti_consumer_app_list);
2793 for (; consumer_iter != NULL; consumer_iter = consumer_iter->next) {
2794 consumer_info = (datacontrol_consumer_info *)consumer_iter->data;
2795 if (__check_consumer_cert(provider->provider_id,
2796 consumer_info->appid,
2797 type) != DATA_CONTROL_ERROR_NONE)
2799 retval = __send_signal_to_consumer(
2801 consumer_info->unique_id,
2802 consumer_info->object_path,
2805 if (retval != DATA_CONTROL_ERROR_NONE) {
2806 _LOGE("__send_signal_to_consumer fail : %d", retval);
2813 EXPORT_API int data_control_provider_foreach_data_change_consumer(
2814 data_control_h provider,
2815 data_control_provider_data_change_consumer_cb list_cb,
2818 char *app_id = NULL;
2819 char *unique_id = NULL;
2820 int ret = DATA_CONTROL_ERROR_NONE;
2821 sqlite3_stmt *stmt = NULL;
2822 char query[QUERY_MAXLEN];
2823 bool callback_result;
2824 data_control_provider_data_change_consumer_cb consumer_list_cb;
2825 consumer_list_cb = list_cb;
2827 if (!provider || !list_cb)
2828 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2829 if (provider->provider_id == NULL || provider->data_id == NULL)
2830 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2832 sqlite3_snprintf(QUERY_MAXLEN, query,
2833 "SELECT app_id, unique_id " \
2834 "FROM data_control_consumer_path_list " \
2835 "WHERE provider_id = ? AND data_id = ?");
2836 _LOGI("get_changed_noti_consumer_list query : %s", query);
2838 ret = sqlite3_prepare_v2(__provider_db, query, -1, &stmt, NULL);
2839 if (ret != SQLITE_OK) {
2840 _LOGE("prepare stmt fail");
2841 ret = DATA_CONTROL_ERROR_IO_ERROR;
2845 ret = sqlite3_bind_text(stmt, 1, provider->provider_id, -1, SQLITE_TRANSIENT);
2846 if (ret != SQLITE_OK) {
2847 _LOGE("bind provider id fail: %s", sqlite3_errmsg(__provider_db));
2848 ret = DATA_CONTROL_ERROR_IO_ERROR;
2852 ret = sqlite3_bind_text(stmt, 2, provider->data_id, -1, SQLITE_TRANSIENT);
2853 if (ret != SQLITE_OK) {
2854 _LOGE("bind data id fail: %s", sqlite3_errmsg(__provider_db));
2855 ret = DATA_CONTROL_ERROR_IO_ERROR;
2859 while (SQLITE_ROW == sqlite3_step(stmt)) {
2861 app_id = (char *)sqlite3_column_text(stmt, 0);
2863 _LOGE("Failed to get package name\n");
2866 unique_id = (char *)sqlite3_column_text(stmt, 1);
2868 consumer_list_cb((data_control_h)provider, app_id, user_data);
2869 _LOGI("app_id : %s, unique_id : %s, result : %d ",
2870 app_id, unique_id, callback_result);
2871 if (!callback_result)
2876 sqlite3_reset(stmt);
2877 sqlite3_clear_bindings(stmt);
2878 sqlite3_finalize(stmt);
2884 int datacontrol_provider_write_socket(
2885 int fd, void *buffer, unsigned int nbytes,
2886 unsigned int *bytes_write)
2888 int ret = _write_socket(fd, buffer, nbytes, bytes_write);
2892 int datacontrol_provider_send_select_result_without_data(
2893 int request_id, int *fd)
2898 datacontrol_socket_info *socket_info;
2899 char *caller_app_id;
2901 _LOGI("Send select result by byte array for request id: %d", request_id);
2903 if (__request_table == NULL)
2904 __initialize_provider();
2906 b = g_hash_table_lookup(__request_table, &request_id);
2908 _LOGE("No data for the request id: %d", request_id);
2909 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2911 res = __set_result(b, DATACONTROL_TYPE_SQL_SELECT, NULL);
2912 ret = __send_result(res, DATACONTROL_TYPE_UNDEFINED, NULL);
2915 caller_app_id = (char *)bundle_get_val(b, AUL_K_CALLER_APPID);
2916 socket_info = g_hash_table_lookup(__socket_pair_hash, caller_app_id);
2917 if (socket_info == NULL) {
2918 _LOGE("__socket_pair_hash lookup fail");
2919 return DATA_CONTROL_ERROR_IO_ERROR;
2921 *fd = socket_info->socket_fd;
2922 g_hash_table_remove(__request_table, &request_id);
2927 int datacontrol_provider_get_select_page_info(
2928 int request_id, int *page_num, int *count_per_page)
2931 const char *page_num_str;
2932 const char *count_per_page_str;
2934 if (__request_table == NULL) {
2935 _LOGE("__request_table is NULL");
2936 return DATA_CONTROL_ERROR_IO_ERROR;
2939 b = g_hash_table_lookup(__request_table, &request_id);
2941 _LOGE("No data for the request id: %d", request_id);
2942 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2945 page_num_str = bundle_get_val(b, RESULT_PAGE_NUMBER);
2946 count_per_page_str = bundle_get_val(b, MAX_COUNT_PER_PAGE);
2947 if (page_num_str == NULL || count_per_page_str == NULL) {
2948 _LOGE("No page data for the request id: %d, ", request_id);
2949 return DATA_CONTROL_ERROR_INVALID_PARAMETER;
2951 *page_num = __get_int_from_str(page_num_str);
2952 *count_per_page = __get_int_from_str(count_per_page_str);
2954 return DATA_CONTROL_ERROR_NONE;