summaryrefslogtreecommitdiff
path: root/src/libalarm.c
blob: 1149255f2ca6437302e20097e03641376df5a589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
*
* Copyright 2012  Samsung Electronics Co., Ltd
*
* Licensed under the Flora License, Version 1.1 (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 <limits.h>
#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);
}