6abdbbd57db46478e372736054b734be595b318e
[platform/core/connectivity/ua-manager.git] / ua-daemon / include / ua-manager-database.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 #ifndef __UA_MANAGER_DATABASE_H__
19 #define __UA_MANAGER_DATABASE_H__
20
21 #include <sqlite3.h>
22 #include <ua-plugin-manager.h>
23 #include <ua-api.h>
24 #include "ua-manager-core.h"
25
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29
30 #define SQLITE_BUSY_TIMEOUT 500000
31 #define SQLITE_MAX_RETRY 10
32
33 /* Helper macros */
34 #define EXEC(error_code, command, handle_error) do { \
35         if (error_code != command) { \
36                 FUNC_EXIT; \
37                 goto handle_error; \
38         } \
39 } while (0)
40
41 #define PREPARE_QUERY(rc, db, stm, query, finalize) do { \
42         rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \
43         if (rc != SQLITE_OK) { \
44                 stm = NULL; \
45                 finalize(); \
46                 UAM_ERR("Failed to prepare \"%s\" query" \
47                                 , query); \
48                 return rc; \
49         } \
50 } while (0)
51
52 #define FINALIZE(stm) do { \
53         if (stm) { \
54                 sqlite3_finalize(stm); \
55                 stm = NULL; \
56         } \
57 } while (0)
58
59 #define DB_ACTION(command, error_code, handle_error) do { \
60         if ((command) != SQLITE_OK) { \
61                 error_code = UAM_ERROR_DB_FAILED; \
62                 FUNC_EXIT; \
63                 goto handle_error; \
64         } \
65 } while (0)
66
67 /* Helper Functions */
68 int _uam_db_initialize_once(void);
69
70 /* db init/deinit */
71 int _uam_db_initialize(void);
72 int _uam_db_deinitialize(void);
73
74 /* delete operations */
75 int _uam_db_clear(void);
76
77 /* USER QUERIES */
78
79 typedef struct db_user_info {
80         int user_id;
81         char name[UAM_USER_NAME_MAX_STRING_LEN];
82         char account[UAM_USER_ACCOUNT_MAX_STRING_LEN];
83 } db_user_info_t;
84
85 /* db init/deinit */
86 int _uam_user_db_initialize(void);
87 int _uam_user_db_deinitialize(void);
88
89 /* select operations */
90 GSList *_uam_db_get_all_users(void);
91 int _uam_db_get_user(int user_id, db_user_info_t *info);
92
93 /* delete operations */
94 int _uam_db_delete_by_user_id(int user_id);
95 int _uam_user_db_clear(void);
96
97 /* insert */
98 int _uam_db_insert_user_info(int* user_id, const char *name, const char *account);
99
100 /* DEVICE QUERIES */
101 typedef struct {
102         int user_id;
103         uam_device_info_s dev_info;
104         int presence_state;
105         long timestamp;
106 } db_device_info_t;
107
108 /* db init/deinit */
109 int _uam_device_db_initialize(void);
110 int _uam_device_db_deinitialize(void);
111
112 /* select operations */
113 GSList *_uam_device_db_get_all_devices(void);
114 int _uam_device_db_get_device(
115         char *device_id, int tech_type, char *address, db_device_info_t *info);
116 int _uam_db_get_device_number(
117         const char *device_id, int tech_type, const char *address, int *device_number);
118
119 /* delete operations */
120 int _uam_device_db_clear(void);
121 int _uam_device_db_delete_device_info(
122         const char *device_id, int tech_type, const char *address);
123
124 /* insert */
125 int _uam_device_db_insert_device_info(
126         int user_id, const uam_device_info_s *dev_info, int presence_state, long timestamp);
127
128 /* update */
129 int _uam_device_db_update_device_timestamp(
130         char *device_id, int tech_type, char *address, long timestamp);
131 int _uam_device_db_update_device_presence(
132         char *device_id, int tech_type, char *address, int presence_state);
133 int _uam_device_db_update_device_ip_address(
134         char *device_id, int tech_type, char *address, char *ip_address);
135 int _uam_device_db_update_device_device(char *device_id, int tech_type,
136         char *address, char *ip, char os_type, char discriminant, uam_ble_payload_s payload);
137
138 /* SERVICE QUERIES */
139
140 typedef struct {
141         int service_number;
142         char service_name[UAM_SERVICE_MAX_STRING_LEN];
143         int cycle;
144         int presence_threshold;
145         int absence_threshold;
146 } db_service_info_t;
147
148 /* db init/deinit */
149 int _uam_service_db_initialize(void);
150 int _uam_service_db_deinitialize(void);
151
152 /* select operations */
153 int _uam_db_get_service_info(const char *service_name, db_service_info_t *info);
154 GSList *_uam_service_db_get_all_services(void);
155
156 /* delete operations */
157 int _uam_db_delete_service_info(const char *service_name);
158 int _uam_service_db_clear(void);
159
160 /* insert */
161 int _uam_db_insert_service_info(
162         int *service_number, uam_service_info_s *svc, int cycle);
163
164 /* update */
165 int _uam_db_update_service_cycle(const char *service_name, int cycle);
166 int _uam_db_update_service_info(uam_db_service_info_t *service);
167
168
169 /* DEVICE_SERVICE QUERIES */
170
171 typedef struct {
172         char device_id[UAM_DEVICE_ID_MAX_STRING_LEN];
173         uam_tech_type_e type;
174         char svc[UAM_SERVICE_MAX_STRING_LEN];
175         int discriminant;
176 } db_svc_dev_info_t;
177
178 /* db init/deinit */
179 int _uam_device_service_db_initialize(void);
180 int _uam_device_service_db_deinitialize(void);
181
182 /* select operations */
183 GSList *_uam_db_get_service(int device_number);
184 GSList *_uam_db_get_device_services(
185         const char *device_id, int tech_type, const char *address);
186 int _uam_db_get_device_services_count(const char *device_id, int tech_type,
187         const char *address, int *count);
188 GSList *_uam_db_get_service_devices_info();
189
190 /* delete operations */
191 int _uam_device_service_db_clear(void);
192 int _uam_db_delete_service_number(int service_number);
193 int _uam_db_delete_device_service_number(
194         int device_number, int service_number);
195 int _uam_db_delete_device_service(const char *service_name);
196 int _uam_db_delete_device_service_info(const char *device_id, int tech_type,
197                 const char *address, const char *service_name);
198 int _uam_db_delete_device_service_mapping(const char *device_id, int tech_type,
199                 const char *address);
200
201 /* insert */
202 int _uam_db_insert_service(int device_number, int service_number,
203         gboolean discriminant);
204 int _uam_db_insert_device_service_info(const char *device_id, int tech_type,
205         const char *address, const char *service_name, int cycle, gboolean discriminant);
206
207 /* update */
208 int _uam_db_update_device_service_info(const char *device_id, int tech_type,
209                 const char *address, const char *service_name, gboolean discriminant);
210
211 /* IBEACON_ADV QUERIES */
212
213 typedef struct {
214         int ibeacon_id;
215         int adv_len;
216         char iadv[UAM_IBEACON_ADV_MAX_LEN + 1];
217 } db_adv_info_t;
218
219 /* db init/deinit */
220 int _uam_adv_db_initialize(void);
221 int _uam_adv_db_deinitialize(void);
222
223 /* insert */
224 int _uam_db_insert_adv_info(unsigned int adv_len, const char *ibeacon_adv);
225
226 /* select operations */
227 GSList *_uam_db_get_all_advs(void);
228
229 /* delete operations */
230 int _uam_adv_db_clear(void);
231
232 #ifdef __cplusplus
233 }
234 #endif
235 #endif /* __UA_MANAGER_DATABASE_H__ */