Fix the problem that the Sensor Frameowk and UAF use different data
[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         unsigned long 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,
127         unsigned long long timestamp);
128
129 /* update */
130 int _uam_device_db_update_device_timestamp(
131         char *device_id, int tech_type, char *address, unsigned long long timestamp);
132 int _uam_device_db_update_device_presence(
133         char *device_id, int tech_type, char *address, int presence_state);
134 int _uam_device_db_update_device_ip_address(
135         char *device_id, int tech_type, char *address, char *ip_address);
136 int _uam_device_db_update_device_device(char *device_id, int tech_type,
137         char *address, char *ip, char os_type, char discriminant, uam_ble_payload_s payload);
138
139 /* SERVICE QUERIES */
140
141 typedef struct {
142         int service_number;
143         char service_name[UAM_SERVICE_MAX_STRING_LEN];
144         int cycle;
145         int presence_threshold;
146         int absence_threshold;
147 } db_service_info_t;
148
149 /* db init/deinit */
150 int _uam_service_db_initialize(void);
151 int _uam_service_db_deinitialize(void);
152
153 /* select operations */
154 int _uam_db_get_service_info(const char *service_name, db_service_info_t *info);
155 GSList *_uam_service_db_get_all_services(void);
156
157 /* delete operations */
158 int _uam_db_delete_service_info(const char *service_name);
159 int _uam_service_db_clear(void);
160
161 /* insert */
162 int _uam_db_insert_service_info(
163         int *service_number, uam_service_info_s *svc, int cycle);
164
165 /* update */
166 int _uam_db_update_service_cycle(const char *service_name, int cycle);
167 int _uam_db_update_service_info(uam_db_service_info_t *service);
168
169
170 /* DEVICE_SERVICE QUERIES */
171
172 typedef struct {
173         char device_id[UAM_DEVICE_ID_MAX_STRING_LEN];
174         uam_tech_type_e type;
175         char svc[UAM_SERVICE_MAX_STRING_LEN];
176         int discriminant;
177 } db_svc_dev_info_t;
178
179 /* db init/deinit */
180 int _uam_device_service_db_initialize(void);
181 int _uam_device_service_db_deinitialize(void);
182
183 /* select operations */
184 GSList *_uam_db_get_service(int device_number);
185 GSList *_uam_db_get_device_services(
186         const char *device_id, int tech_type, const char *address);
187 int _uam_db_get_device_services_count(const char *device_id, int tech_type,
188         const char *address, int *count);
189 GSList *_uam_db_get_service_devices_info();
190
191 /* delete operations */
192 int _uam_device_service_db_clear(void);
193 int _uam_db_delete_service_number(int service_number);
194 int _uam_db_delete_device_service_number(
195         int device_number, int service_number);
196 int _uam_db_delete_device_service(const char *service_name);
197 int _uam_db_delete_device_service_info(const char *device_id, int tech_type,
198                 const char *address, const char *service_name);
199 int _uam_db_delete_device_service_mapping(const char *device_id, int tech_type,
200                 const char *address);
201
202 /* insert */
203 int _uam_db_insert_service(int device_number, int service_number,
204         gboolean discriminant);
205 int _uam_db_insert_device_service_info(const char *device_id, int tech_type,
206         const char *address, const char *service_name, int cycle, gboolean discriminant);
207
208 /* update */
209 int _uam_db_update_device_service_info(const char *device_id, int tech_type,
210                 const char *address, const char *service_name, gboolean discriminant);
211
212 /* IBEACON_ADV QUERIES */
213
214 typedef struct {
215         int ibeacon_id;
216         int adv_len;
217         char iadv[UAM_IBEACON_ADV_MAX_LEN + 1];
218 } db_adv_info_t;
219
220 /* db init/deinit */
221 int _uam_adv_db_initialize(void);
222 int _uam_adv_db_deinitialize(void);
223
224 /* insert */
225 int _uam_db_insert_adv_info(unsigned int adv_len, const char *ibeacon_adv);
226
227 /* select operations */
228 GSList *_uam_db_get_all_advs(void);
229
230 /* delete operations */
231 int _uam_adv_db_clear(void);
232
233 #ifdef __cplusplus
234 }
235 #endif
236 #endif /* __UA_MANAGER_DATABASE_H__ */