/* * * Copyright 2012 Samsung Electronics Co., Ltd * * Licensed under the Flora License, Version 1.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://floralicense.org/license/ * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #include #include "libalarm.h" /********************************************************************** ******************define, struct ,typedef, union, enum, global val ************************************* ***********************************************************************/ // #define COMP_FUNC(type) \ int __cmp_##type##_(type * d1, type * d2) \ { \ if (d1->ad.id == 0 && d2->ad.id == 0) return -1; \ return (d1->ad.id - d2->ad.id); \ } \ COMP_FUNC(ADList); /********************************************************************** ******************Local function declear, extern function declear************************************* ***********************************************************************/ /********************************************************************** ******************Global val , static global val************************************* ***********************************************************************/ /********************************************************************** ******************Local function ref************************************* ***********************************************************************/ static DBHandle *db = NULL; /********************************************************************** ******************Global function ref************************************* ***********************************************************************/ // A_DBAPI int alarmdb_init(const char *dbfile) { char *name = NULL; char defname[PATH_MAX] = { 0, }; if (db) { DB_INFO("Already initialized"); return 0; } if (dbfile) name = (char *)dbfile; if (name == NULL) { snprintf(defname, sizeof(defname), "%s/%s", DBROOT, DBNAME); name = defname; } DB_INFO("DB name : %s", name); db = db_init(name); retv_if(db == NULL, -1); return 0; } // A_DBAPI void alarmdb_fini(void) { if (db) { db_fini(db); db = NULL; } } // A_DBAPI struct alarm_data *alarmdb_create_data(void) { AData *ad = (struct alarm_data *)calloc(1, sizeof(struct alarm_data)); if (!ad) { return NULL; } //init memset(ad, 0, sizeof(AData)); MAGIC_VALUE_SET(ad->_magic); return ad; } // A_DBAPI struct alarm_data *alarmdb_get_data(int id) { int rc; struct alarm_data *ad = NULL; retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init"); retvm_if(id < 1, NULL, "Invalid alarm data id"); ad = alarmdb_create_data(); retv_if(ad == NULL, NULL); rc = get_data(db, id, ad); if (rc) { alarmdb_free_data(ad); return NULL; } return ad; } // A_DBAPI struct alarm_data *alarmdb_get_data_by_author(int id, char author) { int rc; struct alarm_data *ad = NULL; retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init"); retvm_if(id < 1, NULL, "Invalid alarm data id"); ad = alarmdb_create_data(); retv_if(ad == NULL, NULL); rc = get_data_by_author(db, id, ad, author); if (rc) { alarmdb_free_data(ad); return NULL; } return ad; } // A_DBAPI void alarmdb_free_data(struct alarm_data *ad) { retm_if(ad == NULL, "alarm data is NULL\n"); retm_if(!MAGIC_VALUE_CHECK(ad->_magic), "alarm data is error, ad->_magic=%d,ALARM_DB_MAGIC_VALUE=%d\n", ad->_magic, ALARM_DB_MAGIC_VALUE); if (ad) { free(ad); } } // A_DBAPI int alarmdb_add_data(struct alarm_data *ad) { retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init"); return insert_data(db, ad); } // A_DBAPI int alarmdb_mod_data(struct alarm_data *ad) { retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init"); return update_data(db, ad); } // A_DBAPI int alarmdb_del_data(int id) { retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init"); retvm_if(id < 1, -1, "Invalid alarm data id"); return remove_data(db, id); } // A_DBAPI int alarmdb_set_enable(int id, bool enable) { retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init"); return update_enable(db, id, enable); } // A_DBAPI int alarmdb_get_num_of_enable(void) { retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init"); return get_num_of_enable((sqlite3 *)db); } // A_DBAPI int alarmdb_set_snooze(int id, bool enable) { retvm_if(db == NULL, -1, "DB handler is NULL, needs alarm_init"); return update_snooze((db), id, enable); } // A_DBAPI struct alarm_data_list *alarmdb_get_data_list_all(void) { retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init"); return get_data_list_all(db); } // A_DBAPI struct alarm_data_list *alarmdb_get_data_list_by_author(char author) { retvm_if(db == NULL, NULL, "DB handler is NULL, needs alarm_init"); return get_data_list_by_author(db, author); } // A_DBAPI void alarmdb_free_data_list(struct alarm_data_list *adl) { struct alarm_data_list *d = NULL; struct alarm_data_list *t = NULL; retm_if(adl == NULL, "Invalid argument: alarm data list is NULL\n"); t = adl; while (t) { d = t; t = t->next; free(d); } } // A_DBAPI int alarmdb_get_last_id(void) { return get_last_id(db); } // A_DBAPI int alarmdb_get_last_id_by_author(char author) { return get_last_id_by_author(db, author); } // A_DBAPI int alarmdb_get_number_of_data_by_author(char author) { return get_number_of_data_by_author(db, author); } /* A_DBAPI int alarmdb_get_power_on_by_author(char author) { return get_poweron_by_author(db, author); } */ // A_DBAPI int alarmdb_get_power_onoff_by_author(char author) { return get_power_onoff_by_author(db, author); }