Add database APIs to update device service payload info
[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 /* transaction operations */
78 int __uam_db_begin_transaction(void);
79 int __uam_db_end_transaction(gboolean is_success);
80
81 /* USER QUERIES */
82
83 typedef struct db_user_info {
84         int user_id;
85         char name[UAM_USER_NAME_MAX_STRING_LEN];
86         char account[UAM_USER_ACCOUNT_MAX_STRING_LEN];
87 } db_user_info_t;
88
89 /* db init/deinit */
90 int _uam_user_db_initialize(void);
91 int _uam_user_db_deinitialize(void);
92
93 /* select operations */
94 GSList *_uam_db_get_all_users(void);
95 int _uam_db_get_user(int user_id, db_user_info_t *info);
96
97 /* delete operations */
98 int _uam_db_delete_by_user_id(int user_id);
99 int _uam_user_db_clear(void);
100
101 /* insert */
102 int _uam_db_insert_user_info(int* user_id, const char *name, const char *account);
103
104 /* update */
105 int _uam_db_update_user_info(uam_db_user_info_t *user);
106
107 /* DEVICE QUERIES */
108 typedef struct {
109         int user_id;
110         uam_device_info_s dev_info;
111         int presence_state;
112         unsigned long long last_seen;
113 } db_device_info_t;
114
115 /* db init/deinit */
116 int _uam_device_db_initialize(void);
117 int _uam_device_db_deinitialize(void);
118
119 /* select operations */
120 GSList *_uam_device_db_get_all_devices(void);
121 int _uam_device_db_get_device(
122         char *device_id, int tech_type, char *address, db_device_info_t *info);
123 int _uam_db_get_device_number(
124         const char *device_id, int tech_type, const char *address, int *device_number);
125
126 /* delete operations */
127 int _uam_device_db_clear(void);
128 int _uam_device_db_delete_device_info(
129         const char *device_id, int tech_type, const char *address);
130
131 /* insert */
132 int _uam_device_db_insert_device_info(
133         int user_id, const uam_device_info_s *dev_info, int presence_state,
134         unsigned long long last_seen);
135
136 /* update */
137 int _uam_device_db_update_device_last_seen(
138         char *device_id, int tech_type, char *address, unsigned long long last_seen);
139 int _uam_device_db_update_device_presence(
140         char *device_id, int tech_type, char *address, int presence_state);
141 int _uam_device_db_update_device_ip_address(
142         char *device_id, int tech_type, char *ip_address);
143 int _uam_device_db_update_device_mac_address(
144         char *device_id, int tech_type, char *address);
145 int _uam_device_db_update_device(char *device_id, int tech_type,
146         char *address, char *ip, char os_type, char discriminant);
147
148 /* SERVICE QUERIES */
149
150 typedef struct {
151         int service_number;
152         char service_name[UAM_SERVICE_MAX_STRING_LEN];
153         int cycle;
154         int presence_threshold;
155         int absence_threshold;
156 } db_service_info_t;
157
158 /* db init/deinit */
159 int _uam_service_db_initialize(void);
160 int _uam_service_db_deinitialize(void);
161
162 /* select operations */
163 int _uam_db_get_service_info(const char *service_name, db_service_info_t *info);
164 GSList *_uam_service_db_get_all_services(void);
165
166 /* delete operations */
167 int _uam_db_delete_service_info(const char *service_name);
168 int _uam_service_db_clear(void);
169
170 /* insert */
171 int _uam_db_insert_service_info(
172         int *service_number, uam_service_info_s *svc, int cycle);
173
174 /* update */
175 int _uam_db_update_service_cycle(const char *service_name, int cycle);
176 int _uam_db_update_service_info(uam_db_service_info_t *service);
177
178
179 /* DEVICE_SERVICE QUERIES */
180
181 typedef struct {
182         char device_id[UAM_DEVICE_ID_MAX_STRING_LEN]; /**< Device ID */
183         uam_tech_type_e type; /**< Connectivity type */
184         char svc[UAM_SERVICE_MAX_STRING_LEN]; /**< Service name */
185         int discriminant; /**< Discriminant for each service */
186         unsigned long long last_seen; /**< Last seen time for each device of each service */
187 } db_svc_dev_info_t;
188
189 /* db init/deinit */
190 int _uam_device_service_db_initialize(void);
191 int _uam_device_service_db_deinitialize(void);
192
193 /* select operations */
194 GSList *_uam_db_get_service(int device_number);
195 GSList *_uam_db_get_device_services(
196         const char *device_id, int tech_type, const char *address);
197 int _uam_db_get_device_services_count(const char *device_id, int tech_type,
198         const char *address, int *count);
199 GSList *_uam_db_get_service_devices_info();
200
201 /* delete operations */
202 int _uam_device_service_db_clear(void);
203 int _uam_db_delete_service_number(int service_number);
204 int _uam_db_delete_device_service_number(
205         int device_number, int service_number);
206 int _uam_db_delete_device_service(const char *service_name);
207 int _uam_db_delete_device_service_info(const char *device_id, int tech_type,
208                 const char *address, const char *service_name);
209 int _uam_db_delete_device_service_mapping(const char *device_id, int tech_type,
210                 const char *address);
211
212 /* insert */
213 int _uam_db_insert_service(int device_number, int service_number,
214         gboolean discriminant, unsigned long long last_seen);
215 int _uam_db_insert_device_service_info(const char *device_id, int tech_type,
216         const char *address, const char *service_name, int cycle, gboolean discriminant,
217         unsigned long long last_seen);
218
219 /* update */
220 int _uam_db_update_device_service_discriminant(const char *device_id,
221         int tech_type, const char *address, const char *service_name, gboolean discriminant);
222 int _uam_db_update_device_service_last_seen(const char *device_id,
223         int tech_type, const char *address, const char *service_name,
224         unsigned long long last_seen);
225 int _uam_db_update_device_service_payload_info(
226         uam_ble_payload_s *payload, char *service_name);
227
228 /* IBEACON_ADV QUERIES */
229
230 typedef struct {
231         int ibeacon_id;
232         int adv_len;
233         char iadv[UAM_IBEACON_ADV_MAX_LEN + 1];
234 } db_adv_info_t;
235
236 /* db init/deinit */
237 int _uam_adv_db_initialize(void);
238 int _uam_adv_db_deinitialize(void);
239
240 /* insert */
241 int _uam_db_insert_adv_info(unsigned int adv_len, const char *ibeacon_adv);
242
243 /* select operations */
244 GSList *_uam_db_get_all_advs(void);
245
246 /* delete operations */
247 int _uam_adv_db_clear(void);
248
249
250 /* PAYLOAD QUERIES */
251 typedef struct {
252         uam_ble_payload_s payload_info;
253 } db_payload_info_t;
254
255 /* db init/deinit */
256 int _uam_payload_db_initialize(void);
257 int _uam_payload_db_deinitialize(void);
258
259 /* insert */
260 int _uam_db_insert_payload_info(
261         const char *device_id, int tech_type,
262         const char *address, uam_ble_payload_s *payload);
263
264 /* delete operations */
265 int _uam_payload_db_clear(void);
266
267 /* select operations */
268 int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
269                 int *device_number);
270 int _uam_db_get_payload_number(uam_ble_payload_s *payload,
271                 int *payload_number);
272
273 #ifdef __cplusplus
274 }
275 #endif
276 #endif /* __UA_MANAGER_DATABASE_H__ */