13 #include <appsvc/appsvc.h>
16 #include <pkgmgr-info.h>
17 #include <security-server.h>
19 #include "data-control-sql.h"
20 #include "data-control-provider.h"
21 #include "data-control-internal.h"
23 #define ROW_ID_SIZE 32
24 #define RESULT_PATH_MAX 512
27 #define RESULT_PAGE_NUMBER "RESULT_PAGE_NUMBER"
28 #define MAX_COUNT_PER_PAGE "MAX_COUNT_PER_PAGE"
29 #define RESULT_VALUE_COUNT "RESULT_VALUE_COUNT"
31 #define PACKET_INDEX_REQUEST_RESULT 0
32 #define PACKET_INDEX_ERROR_MSG 1
33 #define PACKET_INDEX_SELECT_RESULT_FILE 2
34 #define PACKET_INDEX_ROW_ID 2
35 #define PACKET_INDEX_VALUE_COUNT 2
36 #define PACKET_INDEX_GET_RESULT_FILE 3
38 #define PACKET_INDEX_DATAID 0
39 #define PACKET_INDEX_COLUMNCOUNT 1
40 #define PACKET_INDEX_MAP 2
42 #define PACKET_INDEX_UPDATEWHERE 3
43 #define PACKET_INDEX_DELETEWHERE 1
45 #define PACKET_INDEX_MAP_KEY 1
46 #define PACKET_INDEX_MAP_VALUE_1ST 2
47 #define PACKET_INDEX_MAP_VALUE_2ND 3
48 #define PACKET_INDEX_MAP_PAGE_NO 2
49 #define PACKET_INDEX_MAP_COUNT_PER_PAGE 3
51 static const int MAX_ARGUMENT_SIZE = 16384; // 16KB
52 static GHashTable *request_table = NULL;
54 //static pthread_mutex_t provider_lock = PTHREAD_MUTEX_INITIALIZER;
56 struct datacontrol_s {
62 typedef int (*provider_handler_cb) (bundle * b, int request_id, void *data);
64 static datacontrol_provider_sql_cb* provider_sql_cb = NULL;
65 static datacontrol_provider_map_cb* provider_map_cb = NULL;
66 static void* provider_map_user_data = NULL;
67 static void* provider_sql_user_data = NULL;
70 __free_data(gpointer data)
80 __initialize_provider(void)
82 request_table = g_hash_table_new_full(g_int_hash, g_int_equal, __free_data, __free_data);
86 __provider_new_request_id(void)
90 g_atomic_int_inc(&id);
96 __get_client_pkgid(bundle *b)
98 const char *caller_appid = NULL;
99 char *caller_pkgid = NULL;
100 pkgmgrinfo_appinfo_h app_info_handle = NULL;
102 caller_appid = bundle_get_val(b, AUL_K_CALLER_APPID);
103 pkgmgrinfo_appinfo_get_appinfo(caller_appid, &app_info_handle);
104 pkgmgrinfo_appinfo_get_pkgname(app_info_handle, &caller_pkgid);
105 SECURE_LOGI("client pkg id : %s", caller_pkgid);
107 return caller_pkgid ? strdup(caller_pkgid) : NULL;
111 __get_data_map(const char *path, int column_count)
113 bundle* b = bundle_create();
123 SECURE_LOGI("The request file of INSERT/UPDATE: %s", path);
124 ret = security_server_shared_file_reopen(path, &fd);
125 if (ret != SECURITY_SERVER_API_SUCCESS)
127 SECURE_LOGE("Opening the file(%s) is failed : %d", path, ret);
131 for (i = 0; i < column_count; i++)
133 size = read(fd, &len, sizeof(int));
134 if ((unsigned int)size < sizeof(int) || len < 0 || len > MAX_ARGUMENT_SIZE)
136 SECURE_LOGE("key length:%d, read():%s, returned:%d", len, strerror(errno), size);
140 key = calloc(len + 1, sizeof(char));
142 SECURE_LOGE("out of memory");
146 size = read(fd, key, len); // key
149 SECURE_LOGE("key length:%d, read():%s, returned:%d", len, strerror(errno), size);
154 size = read(fd, &len, sizeof(int));
155 if ((unsigned int)size < sizeof(int) || len < 0 || len > MAX_ARGUMENT_SIZE)
157 SECURE_LOGE("value length:%d, read():%s, returned:%d", len, strerror(errno), size);
162 value = calloc(len + 1, sizeof(char));
164 SECURE_LOGE("out of memory");
169 size = read(fd, value, len); // value
172 SECURE_LOGE("value length:%d, read():%s, returned:%d", len, strerror(errno), size);
178 LOGI("key: %s, value: %s", key, value);
180 bundle_add_str(b, key, value);
188 ret = security_server_shared_file_delete(path);
189 if (ret != SECURITY_SERVER_API_SUCCESS)
191 SECURE_LOGE("unable to remove the request file of INSERT/UPDATE(%s): %d", path, ret);
198 __set_select_result(bundle* b, const char* path, void* data)
200 LOGI("__set_select_result");
201 // The provider application should call the sqlite3_open().
202 // and it also should call the sqlite3_prepare_v2() in datacontrol_provider_sql_select_request_cb().
203 // and then, it should call the datacontrol_provider_send_select_result() with a pointer to sqlite3_stmt.
204 // The 3rd param 'data' is the pointer to sqlite3_stmt.
206 // In this function, the result set is written in specified file path as specific form.
207 // [sizeof(int)] row count
208 // [sizeof(int)] column count
209 // [sieeof(int)] total size of column names
210 // [sizeof(int)] column type x N
211 // [ variant ] column name x N
212 // [sizeof(int)] type
213 // [sizeof(int)] size
214 // [ varient ] content
216 int column_count = 0;
218 char *column_name = NULL;
219 int total_len_of_column_names = 0;
220 int count_per_page = 0;
225 sqlite3_stmt *state = (sqlite3_stmt *)data;
228 char *client_pkgid = NULL;
230 if (b ==NULL || path == NULL || data == NULL)
232 LOGE("The input param is invalid.");
233 return DATACONTROL_ERROR_INVALID_PARAMETER;
236 if (sqlite3_reset(state) != SQLITE_OK)
238 LOGE("sqlite3_reset() is failed.");
239 return DATACONTROL_ERROR_INVALID_PARAMETER;
242 if (sqlite3_step(state) != SQLITE_ROW)
244 LOGE("The DB does not have another row.");
245 return DATACONTROL_ERROR_INVALID_PARAMETER;
248 client_pkgid = __get_client_pkgid(b);
249 ret = security_server_shared_file_open(path, client_pkgid, &fd);
250 if (ret == SECURITY_SERVER_API_ERROR_FILE_EXIST) {
251 SECURE_LOGE("The file(%s) already exist, delete and retry to open", path);
252 int ret_temp = security_server_shared_file_delete(path);
253 if (ret_temp != SECURITY_SERVER_API_SUCCESS) {
254 SECURE_LOGE("Delete the file(%s) is failed : %d", path, ret_temp);
256 ret = security_server_shared_file_open(path, client_pkgid, &fd);
260 if (ret != SECURITY_SERVER_API_SUCCESS)
262 SECURE_LOGE("Opening the file(%s) is failed : %d", path, ret);
264 return DATACONTROL_ERROR_IO_ERROR;
269 column_count = sqlite3_column_count(state);
270 if (lseek(fd, sizeof(int), SEEK_SET) == -1)
272 LOGE("lseek is failed. errno = %d", errno);
274 if (write(fd, &column_count, sizeof(int)) == -1)
276 LOGE("Writing a column_count to a file descriptor is failed. errno = %d", errno);
279 // 2. column type x column_count
280 // #define SQLITE_INTEGER 1
281 // #define SQLITE_FLOAT 2
282 // #define SQLITE_TEXT 3
283 // #define SQLITE_BLOB 4
284 // #define SQLITE_NULL 5
285 if (lseek(fd, sizeof(int), SEEK_CUR) == -1)
287 LOGE("lseek is failed. errno = %d", errno);
290 for (i = 0; i < column_count; ++i)
292 int type = sqlite3_column_type(state, i);
293 if (write(fd, &type, sizeof(int)) == -1)
295 LOGE("Writing a type to a file descriptor is failed. errno = %d", errno);
299 // 3. column name x column_count
300 for (i = 0; i < column_count; i++)
302 column_name = (char *)sqlite3_column_name(state, i);
303 if (column_name == NULL) {
304 LOGE("sqlite3_column_name is failed. errno = %d", errno);
306 column_name = strcat(column_name, "\n");
307 if (write(fd, column_name, strlen(column_name)) == -1)
309 LOGE("Writing a column_name to a file descriptor is failed. errno = %d", errno);
311 total_len_of_column_names += strlen(column_name);
315 // 4. total length of column names
316 if (lseek(fd, sizeof(int) * 2, SEEK_SET) == -1)
318 LOGE("lseek is failed. errno = %d", errno);
320 if (write(fd, &total_len_of_column_names, sizeof(int)) == -1)
322 LOGE("Writing a total_len_of_column_names to a file descriptor is failed. errno = %d", errno);
324 // 5. type, size and value of each element
325 if (lseek(fd, (sizeof(int) * column_count) + total_len_of_column_names, SEEK_CUR) == -1)
327 LOGE("lseek is failed. errno = %d", errno);
330 page_number = atoi(bundle_get_val(b, RESULT_PAGE_NUMBER));
331 count_per_page = atoi(bundle_get_val(b, MAX_COUNT_PER_PAGE));
332 offset = (page_number - 1) * count_per_page;
334 LOGI("page_number: %d, count_per_page: %d, offset: %d", page_number, count_per_page, offset);
339 if (offset_idx > offset) {
341 for (i = 0; i < column_count; ++i)
346 bool is_null_type = false;
347 int column_type = sqlite3_column_type(state, i);
348 long long tmp_long = 0;
349 double tmp_double = 0.0;
355 size = sizeof(long long);
356 tmp_long = sqlite3_column_int64(state, i);
363 size = sizeof(double);
364 tmp_double = sqlite3_column_double(state, i);
371 value = (char *)sqlite3_column_text(state, i);
372 size = strlen(value);
378 size = sqlite3_column_bytes(state, i);
379 value = (char *)sqlite3_column_blob(state, i);
391 LOGE("The column type is invalid.");
396 if (write(fd, &type, sizeof(int)) == -1)
398 LOGE("Writing a type to a file descriptor is failed. errno = %d", errno);
400 if (write(fd, &size, sizeof(int)) == -1)
402 LOGE("Writing a size to a file descriptor is failed. errno = %d", errno);
404 if (size > 0 && size < INT_MAX && !is_null_type)
406 if (write(fd, value, size) == -1)
408 LOGE("Writing a value to a file descriptor is failed. errno = %d", errno);
415 } while(sqlite3_step(state) == SQLITE_ROW && row_count < count_per_page);
418 if (lseek(fd, 0, SEEK_SET) == -1)
420 LOGE("lseek is failed. errno = %d", errno);
422 if (write(fd, &row_count, sizeof(int)) == -1)
424 LOGE("Writing a row_count to a file descriptor is failed. errno = %d", errno);
429 return DATACONTROL_ERROR_NONE;
433 __set_get_value_result(bundle *b, const char* path, char **value_list)
438 char *client_pkgid = NULL;
440 if (b == NULL || path == NULL || value_list == NULL)
442 LOGE("The input param is invalid.");
443 return DATACONTROL_ERROR_INVALID_PARAMETER;
446 int page_number = atoi(bundle_get_val(b, RESULT_PAGE_NUMBER));
447 int count_per_page = atoi(bundle_get_val(b, MAX_COUNT_PER_PAGE));
448 int value_count = atoi(bundle_get_val(b, RESULT_VALUE_COUNT));
449 int current_offset = (page_number - 1) * count_per_page;
450 int remain_count = value_count - current_offset;
451 remain_count = (remain_count > 0) ? remain_count : 0; // round off to zero if the negative num is found
452 int add_value_count = (count_per_page > remain_count) ? remain_count : count_per_page;
454 if (add_value_count < value_count)
456 bundle_del(b, RESULT_VALUE_COUNT);
457 char value_count_str[32] = {0,};
458 snprintf(value_count_str, 32, "%d", add_value_count);
459 bundle_add_str(b, RESULT_VALUE_COUNT, value_count_str);
462 if (add_value_count <= 0)
464 LOGI("There is no value list.");
465 return DATACONTROL_ERROR_NONE;
468 client_pkgid = __get_client_pkgid(b);
469 ret = security_server_shared_file_open(path, client_pkgid, &fd);
470 if (ret == SECURITY_SERVER_API_ERROR_FILE_EXIST) {
471 SECURE_LOGE("The file(%s) already exist, delete and retry to open", path);
472 int ret_temp = security_server_shared_file_delete(path);
473 if (ret_temp != SECURITY_SERVER_API_SUCCESS) {
474 SECURE_LOGE("Delete the file(%s) is failed : %d", path, ret_temp);
476 ret = security_server_shared_file_open(path, client_pkgid, &fd);
480 if (ret != SECURITY_SERVER_API_SUCCESS)
482 SECURE_LOGE("Opening the file(%s) is failed : %d", path, ret);
484 return DATACONTROL_ERROR_IO_ERROR;
488 for (i = 0; i < add_value_count; ++i)
490 int length = strlen(value_list[current_offset + i]);
491 if (write(fd, &length, sizeof(int)) == -1)
493 LOGE("Writing a length to a file descriptor is failed. errno = %d", errno);
495 if (write(fd, value_list[current_offset + i], length) == -1)
497 LOGE("Writing a value_list to a file descriptor is failed. errno = %d", errno);
503 return DATACONTROL_ERROR_NONE;
507 __get_result_file_path(bundle *b)
509 LOGI("__get_result_file_path");
510 const char *caller = bundle_get_val(b, AUL_K_CALLER_APPID);
513 LOGE("caller appid is NULL.");
517 const char *caller_req_id = bundle_get_val(b, OSP_K_REQUEST_ID);
519 char *result_path = calloc(RESULT_PATH_MAX, sizeof(char));
523 LOGE("result path is NULL.");
526 snprintf(result_path, RESULT_PATH_MAX, "%s%s%s", DATACONTROL_RESULT_FILE_PREFIX, caller, caller_req_id);
528 SECURE_LOGI("result file path: %s", result_path);
534 __set_result(bundle* b, datacontrol_request_type type, void* data)
537 aul_create_result_bundle(b, &res);
540 char type_str[MAX_LEN_DATACONTROL_REQ_TYPE + 1] = {0,};
541 if (type == DATACONTROL_TYPE_UNDEFINED || type == DATACONTROL_TYPE_ERROR)
543 char *request_type = (char*)bundle_get_val(b, OSP_K_DATACONTROL_REQUEST_TYPE);
544 strncpy(type_str, request_type, MAX_LEN_DATACONTROL_REQ_TYPE);
545 LOGI("type is %s", type_str);
549 snprintf(type_str, MAX_LEN_DATACONTROL_REQ_TYPE, "%d", (int)type);
551 bundle_add_str(res, OSP_K_DATACONTROL_REQUEST_TYPE, type_str);
553 // Set the provider id
554 char *provider_id = (char*)bundle_get_val(b, OSP_K_DATACONTROL_PROVIDER);
555 bundle_add_str(res, OSP_K_DATACONTROL_PROVIDER, provider_id);
558 char *data_id = (char*)bundle_get_val(b, OSP_K_DATACONTROL_DATA);
559 bundle_add_str(res, OSP_K_DATACONTROL_DATA, data_id);
561 // Set the caller request id
562 char *request_id = (char*)bundle_get_val(b, OSP_K_REQUEST_ID);
563 bundle_add_str(res, OSP_K_REQUEST_ID, request_id);
567 case DATACONTROL_TYPE_SQL_SELECT:
571 list[PACKET_INDEX_REQUEST_RESULT] = "1"; // request result
572 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
574 char *path = __get_result_file_path(b);
577 int ret = __set_select_result(b, path, data);
580 memset(path, 0, RESULT_PATH_MAX);
581 strcpy(path, "NoResultSet");
582 LOGI("Empty ResultSet");
584 list[PACKET_INDEX_SELECT_RESULT_FILE] = path;
588 list[PACKET_INDEX_SELECT_RESULT_FILE] = DATACONTROL_EMPTY;
591 bundle_add_str_array(res, OSP_K_ARG, list, 3);
601 case DATACONTROL_TYPE_SQL_INSERT:
603 long long row_id = *(long long*)data;
606 list[PACKET_INDEX_REQUEST_RESULT] = "1"; // request result
607 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
610 char row_str[ROW_ID_SIZE] = {0,};
611 snprintf(row_str, ROW_ID_SIZE, "%lld", row_id);
613 list[PACKET_INDEX_ROW_ID] = row_str;
615 bundle_add_str_array(res, OSP_K_ARG, list, 3);
618 case DATACONTROL_TYPE_SQL_UPDATE:
619 case DATACONTROL_TYPE_SQL_DELETE:
622 list[PACKET_INDEX_REQUEST_RESULT] = "1"; // request result
623 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
625 bundle_add_str_array(res, OSP_K_ARG, list, 2);
628 case DATACONTROL_TYPE_MAP_GET:
632 list[PACKET_INDEX_REQUEST_RESULT] = "1"; // request result
633 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
635 char *path = __get_result_file_path(b);
638 char **value_list = (char **)data;
639 __set_get_value_result(b, path, value_list);
640 list[PACKET_INDEX_VALUE_COUNT] = bundle_get_val(b, RESULT_VALUE_COUNT); // value count
641 list[PACKET_INDEX_GET_RESULT_FILE] = path;
645 list[PACKET_INDEX_VALUE_COUNT] = 0; // value count
646 list[PACKET_INDEX_GET_RESULT_FILE] = DATACONTROL_EMPTY;
649 bundle_add_str_array(res, OSP_K_ARG, list, 4);
658 case DATACONTROL_TYPE_UNDEFINED: // DATACONTROL_TYPE_MAP_SET || ADD || REMOVE
661 list[PACKET_INDEX_REQUEST_RESULT] = "1"; // request result
662 list[PACKET_INDEX_ERROR_MSG] = DATACONTROL_EMPTY;
664 bundle_add_str_array(res, OSP_K_ARG, list, 2);
670 list[PACKET_INDEX_REQUEST_RESULT] = "0"; // request result
671 list[PACKET_INDEX_ERROR_MSG] = (char*)data; // error string
673 bundle_add_str_array(res, OSP_K_ARG, list, 2);
682 __send_result(bundle* b, datacontrol_request_type type)
684 int ret = aul_send_service_result(b);
688 LOGE("Fail to send a result to caller");
694 case DATACONTROL_TYPE_SQL_SELECT:
696 index = PACKET_INDEX_SELECT_RESULT_FILE;
699 case DATACONTROL_TYPE_MAP_GET:
701 index = PACKET_INDEX_GET_RESULT_FILE;
707 return DATACONTROL_ERROR_IO_ERROR;
712 const char **str_arr = bundle_get_str_array(b, OSP_K_ARG, &len);
713 SECURE_LOGI("result file: %s (%d)", str_arr[index], index);
714 ret = security_server_shared_file_delete(str_arr[index]);
715 if (ret != SECURITY_SERVER_API_SUCCESS)
717 SECURE_LOGE("unable to remove the result file: %d", ret);
721 return DATACONTROL_ERROR_IO_ERROR;
725 return DATACONTROL_ERROR_NONE;
729 __datacontrol_handler_cb(bundle *b, int request_id, void* data)
731 LOGI("datacontrol_handler_cb");
733 const char *request_type = bundle_get_val(b, OSP_K_DATACONTROL_REQUEST_TYPE);
734 if (request_type == NULL)
736 LOGE("Invalid data control request");
737 return DATACONTROL_ERROR_INVALID_PARAMETER;
740 // Get the request type
741 datacontrol_request_type type = atoi(request_type);
742 if (type >= DATACONTROL_TYPE_SQL_SELECT && type <= DATACONTROL_TYPE_SQL_DELETE)
744 if (provider_sql_cb == NULL)
746 LOGE("SQL callback is not registered.");
747 return DATACONTROL_ERROR_INVALID_PARAMETER;
750 else if (type >= DATACONTROL_TYPE_MAP_GET && type <= DATACONTROL_TYPE_MAP_REMOVE)
752 if (provider_map_cb == NULL)
754 LOGE("Map callback is not registered.");
755 return DATACONTROL_ERROR_INVALID_PARAMETER;
760 LOGE("Invalid requeste type");
761 return DATACONTROL_ERROR_INVALID_PARAMETER;
765 const char **arg_list = bundle_get_str_array(b, OSP_K_ARG, &len);
767 datacontrol_h provider = malloc(sizeof(struct datacontrol_s));
768 if (provider == NULL) {
769 LOGE("Fail to alloc provider");
770 return DATACONTROL_ERROR_OUT_OF_MEMORY;
773 // Set the provider ID
774 provider->provider_id = (char*)bundle_get_val(b, OSP_K_DATACONTROL_PROVIDER);
777 provider->data_id = (char*)arg_list[PACKET_INDEX_DATAID];
779 // Set the request ID
780 int provider_req_id = __provider_new_request_id();
782 SECURE_LOGI("Provider ID: %s, data ID: %s, request type: %s", provider->provider_id, provider->data_id, request_type);
784 // Add the data to the table
785 int *key = malloc(sizeof(int));
788 LOGE("Fail to alloc key");
789 return DATACONTROL_ERROR_OUT_OF_MEMORY;
791 *key = provider_req_id;
793 bundle *value = bundle_dup(b);
794 g_hash_table_insert(request_table, key, value);
798 case DATACONTROL_TYPE_SQL_SELECT:
802 int column_count = atoi(arg_list[i++]); // Column count
804 LOGI("SELECT column count: %d", column_count);
806 const char** column_list = (const char**)malloc(column_count * (sizeof(char *)));
807 if (column_list == NULL) {
809 LOGE("Fail to alloc column_list");
810 return DATACONTROL_ERROR_OUT_OF_MEMORY;
813 while (current < column_count)
815 column_list[current++] = arg_list[i++]; // Column data
817 LOGI("Column %d: %s", current, column_list[current-1]);
820 const char *where = arg_list[i++]; // where
821 const char *order = arg_list[i++]; // order
823 LOGI("where: %s, order: %s", where, order);
825 if (strncmp(where, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
830 if (strncmp(order, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
835 const char *page_number = arg_list[i++];
836 const char *per_page = arg_list[i];
838 bundle_add_str(value, RESULT_PAGE_NUMBER, page_number);
839 bundle_add_str(value, MAX_COUNT_PER_PAGE, per_page);
841 provider_sql_cb->select(provider_req_id, provider, column_list, column_count, where, order, provider_sql_user_data);
847 case DATACONTROL_TYPE_SQL_INSERT:
848 case DATACONTROL_TYPE_SQL_UPDATE:
850 int column_count = atoi(arg_list[PACKET_INDEX_COLUMNCOUNT]);
851 const char *map_path = arg_list[PACKET_INDEX_MAP];
853 LOGI("INSERT / UPDATE handler");
854 SECURE_LOGI("Data path: %s, Column count: %d", map_path, column_count);
856 bundle* map = __get_data_map(map_path, column_count);
858 if (type == DATACONTROL_TYPE_SQL_INSERT)
860 SECURE_LOGI("INSERT column count: %d, map_path: %s", column_count, map_path);
861 provider_sql_cb->insert(provider_req_id, provider, map, provider_sql_user_data);
865 const char *where = arg_list[PACKET_INDEX_UPDATEWHERE];
866 LOGI("UPDATE from where: %s", where);
868 if (strncmp(where, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
872 provider_sql_cb->update(provider_req_id, provider, map, where, provider_sql_user_data);
878 case DATACONTROL_TYPE_SQL_DELETE:
880 const char *where = arg_list[PACKET_INDEX_DELETEWHERE];
882 LOGI("DELETE from where: %s", where);
883 if (strncmp(where, DATACONTROL_EMPTY, strlen(DATACONTROL_EMPTY)) == 0)
887 provider_sql_cb->delete(provider_req_id, provider, where, provider_sql_user_data);
890 case DATACONTROL_TYPE_MAP_GET:
892 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
893 const char *page_number= arg_list[PACKET_INDEX_MAP_PAGE_NO];
894 const char *count_per_page = arg_list[PACKET_INDEX_MAP_COUNT_PER_PAGE];
895 bundle_add_str(value, RESULT_PAGE_NUMBER, page_number);
896 bundle_add_str(value, MAX_COUNT_PER_PAGE, count_per_page);
898 LOGI("Gets the value list related with the key(%s) from Map datacontrol. ", map_key);
900 provider_map_cb->get(provider_req_id, provider, map_key, provider_map_user_data);
903 case DATACONTROL_TYPE_MAP_SET:
905 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
906 const char *old_value = arg_list[PACKET_INDEX_MAP_VALUE_1ST];
907 const char *new_value = arg_list[PACKET_INDEX_MAP_VALUE_2ND];
909 LOGI("Sets the old value(%s) of the key(%s) to the new value(%s) in Map datacontrol.", old_value, map_key, new_value);
911 provider_map_cb->set(provider_req_id, provider, map_key, old_value, new_value, provider_map_user_data);
914 case DATACONTROL_TYPE_MAP_ADD:
916 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
917 const char *map_value = arg_list[PACKET_INDEX_MAP_VALUE_1ST];
919 LOGI("Adds the %s-%s in Map datacontrol.", map_key, map_value);
921 provider_map_cb->add(provider_req_id, provider, map_key, map_value, provider_map_user_data);
924 case DATACONTROL_TYPE_MAP_REMOVE:
926 const char *map_key = arg_list[PACKET_INDEX_MAP_KEY];
927 const char *map_value = arg_list[PACKET_INDEX_MAP_VALUE_1ST];
929 LOGI("Removes the %s-%s in Map datacontrol.", map_key, map_value);
931 provider_map_cb->remove(provider_req_id, provider, map_key, map_value, provider_map_user_data);
940 return DATACONTROL_ERROR_NONE;
944 datacontrol_provider_sql_register_cb(datacontrol_provider_sql_cb *callback, void *user_data)
946 int ret = DATACONTROL_ERROR_NONE;
948 if (callback == NULL)
950 return DATACONTROL_ERROR_INVALID_PARAMETER;
953 if (request_table == NULL)
955 __initialize_provider();
958 LOGI("datacontrol_provider_sql_register_cb");
960 provider_sql_cb = callback;
961 provider_sql_user_data = user_data;
963 if (provider_map_cb == NULL) // If the provider_map_cb was registered(not NULL), __datacontrol_handler_cb is set already.
965 ret = aul_set_data_control_provider_cb(__datacontrol_handler_cb);
972 datacontrol_provider_sql_unregister_cb(void)
974 if (provider_map_cb == NULL) // When both SQL_cb and Map_cb are unregisted, unsetting the provider cb is possible.
976 aul_unset_data_control_provider_cb();
978 provider_sql_cb = NULL;
979 provider_sql_user_data = NULL;
981 return DATACONTROL_ERROR_NONE;
984 int datacontrol_provider_map_register_cb(datacontrol_provider_map_cb *callback, void *user_data)
986 int ret = DATACONTROL_ERROR_NONE;
988 if (callback == NULL)
990 return DATACONTROL_ERROR_INVALID_PARAMETER;
993 if (request_table == NULL)
995 __initialize_provider();
998 LOGI("datacontrol_provider_map_register_cb");
1000 provider_map_cb = callback;
1001 provider_map_user_data = user_data;
1003 if (provider_sql_cb == NULL) // If the provider_sql_cb was registered(not NULL), __datacontrol_handler_cb is set already.
1005 ret = aul_set_data_control_provider_cb(__datacontrol_handler_cb);
1011 int datacontrol_provider_map_unregister_cb(void)
1013 if (provider_sql_cb == NULL) // When both SQL_cb and Map_cb are unregisted, unsetting the provider cb is possible.
1015 aul_unset_data_control_provider_cb();
1017 provider_map_cb = NULL;
1018 provider_map_user_data = NULL;
1020 return DATACONTROL_ERROR_NONE;
1024 datacontrol_provider_get_client_appid(int request_id, char **appid)
1026 if (request_table == NULL)
1028 __initialize_provider();
1031 bundle* b = g_hash_table_lookup(request_table, &request_id);
1034 SECURE_LOGE("No data for the request id: %d", request_id);
1035 return DATACONTROL_ERROR_INVALID_PARAMETER;
1038 const char *caller = bundle_get_val(b, AUL_K_CALLER_APPID);
1041 SECURE_LOGE("No appid for the request id: %d", request_id);
1042 return DATACONTROL_ERROR_INVALID_PARAMETER;
1045 SECURE_LOGI("Request ID: %d, caller appid: %s", request_id, caller);
1047 *appid = strdup(caller);
1049 return DATACONTROL_ERROR_NONE;
1053 datacontrol_provider_send_select_result(int request_id, void *db_handle)
1055 SECURE_LOGI("Send a select result for request id: %d", request_id);
1057 if (request_table == NULL)
1059 __initialize_provider();
1062 bundle* b = g_hash_table_lookup(request_table, &request_id);
1065 SECURE_LOGE("No data for the request id: %d", request_id);
1066 return DATACONTROL_ERROR_INVALID_PARAMETER;
1069 bundle* res = __set_result(b, DATACONTROL_TYPE_SQL_SELECT, db_handle);
1071 return __send_result(res, DATACONTROL_TYPE_SQL_SELECT);
1075 datacontrol_provider_send_insert_result(int request_id, long long row_id)
1077 SECURE_LOGI("Send an insert result for request id: %d", request_id);
1079 if (request_table == NULL)
1081 __initialize_provider();
1084 bundle* b = g_hash_table_lookup(request_table, &request_id);
1087 SECURE_LOGE("No data for the request id: %d", request_id);
1088 return DATACONTROL_ERROR_INVALID_PARAMETER;
1091 bundle* res = __set_result(b, DATACONTROL_TYPE_SQL_INSERT, (void*)&row_id);
1093 return __send_result(res, DATACONTROL_TYPE_SQL_INSERT);
1097 datacontrol_provider_send_update_result(int request_id)
1099 SECURE_LOGI("Send an update result for request id: %d", request_id);
1101 if (request_table == NULL)
1103 __initialize_provider();
1106 bundle* b = g_hash_table_lookup(request_table, &request_id);
1109 SECURE_LOGE("No data for the request id: %d", request_id);
1110 return DATACONTROL_ERROR_INVALID_PARAMETER;
1113 bundle* res = __set_result(b, DATACONTROL_TYPE_SQL_UPDATE, NULL);
1115 return __send_result(res, DATACONTROL_TYPE_SQL_UPDATE);
1119 datacontrol_provider_send_delete_result(int request_id)
1121 SECURE_LOGI("Send a delete result for request id: %d", request_id);
1123 if (request_table == NULL)
1125 __initialize_provider();
1128 bundle* b = g_hash_table_lookup(request_table, &request_id);
1131 SECURE_LOGE("No data for the request id: %d", request_id);
1132 return DATACONTROL_ERROR_INVALID_PARAMETER;
1135 bundle* res = __set_result(b, DATACONTROL_TYPE_SQL_DELETE, NULL);
1137 return __send_result(res, DATACONTROL_TYPE_SQL_DELETE);
1141 datacontrol_provider_send_error(int request_id, const char *error)
1143 SECURE_LOGI("Send an error for request id: %d", request_id);
1145 if (request_table == NULL)
1147 __initialize_provider();
1150 bundle* b = g_hash_table_lookup(request_table, &request_id);
1153 SECURE_LOGE("No data for the request id: %d", request_id);
1154 return DATACONTROL_ERROR_INVALID_PARAMETER;
1157 bundle* res = __set_result(b, DATACONTROL_TYPE_ERROR, (void*)error);
1159 return __send_result(res, DATACONTROL_TYPE_ERROR);
1163 datacontrol_provider_send_map_result(int request_id)
1165 SECURE_LOGI("Send a set/add/remove result for request id: %d", request_id);
1167 if (request_table == NULL)
1169 __initialize_provider();
1172 bundle* b = g_hash_table_lookup(request_table, &request_id);
1175 SECURE_LOGE("No data for the request id: %d", request_id);
1176 return DATACONTROL_ERROR_INVALID_PARAMETER;
1179 bundle* res = __set_result(b, DATACONTROL_TYPE_UNDEFINED, NULL);
1181 return __send_result(res, DATACONTROL_TYPE_UNDEFINED);
1185 datacontrol_provider_send_map_get_value_result(int request_id, char **value_list, int value_count)
1187 SECURE_LOGI("Send a get result for request id: %d", request_id);
1189 if (request_table == NULL)
1191 __initialize_provider();
1194 bundle* b = g_hash_table_lookup(request_table, &request_id);
1197 SECURE_LOGE("No data for the request id: %d", request_id);
1198 return DATACONTROL_ERROR_INVALID_PARAMETER;
1201 char value_count_str[32] = {0,};
1202 snprintf(value_count_str, 32, "%d", value_count);
1203 bundle_add_str(b, RESULT_VALUE_COUNT, value_count_str);
1205 bundle* res = __set_result(b, DATACONTROL_TYPE_MAP_GET, value_list);
1207 return __send_result(res, DATACONTROL_TYPE_MAP_GET);