2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __UA_MANAGER_DATABASE_H__
19 #define __UA_MANAGER_DATABASE_H__
22 #include <ua-plugin-manager.h>
24 #include "ua-manager-core.h"
30 #define SQLITE_BUSY_TIMEOUT 500000
31 #define SQLITE_MAX_RETRY 10
34 #define EXEC(error_code, command, handle_error) do { \
35 if (error_code != command) { \
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) { \
46 UAM_ERR("Failed to prepare \"%s\" query" \
52 #define FINALIZE(stm) do { \
54 sqlite3_finalize(stm); \
59 #define DB_ACTION(command, error_code, handle_error) do { \
60 if ((command) != SQLITE_OK) { \
61 error_code = UAM_ERROR_DB_FAILED; \
67 /* Helper Functions */
68 int _uam_db_initialize_once(void);
71 int _uam_db_initialize(void);
72 int _uam_db_deinitialize(void);
74 /* delete operations */
75 int _uam_db_clear(void);
77 /* transaction operations */
78 int __uam_db_begin_transaction(void);
79 int __uam_db_end_transaction(gboolean is_success);
83 typedef struct db_user_info {
85 char name[UAM_USER_NAME_MAX_STRING_LEN];
86 char account[UAM_USER_ACCOUNT_MAX_STRING_LEN];
90 int _uam_user_db_initialize(void);
91 int _uam_user_db_deinitialize(void);
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);
97 /* delete operations */
98 int _uam_db_delete_by_user_id(int user_id);
99 int _uam_user_db_clear(void);
102 int _uam_db_insert_user_info(int* user_id, const char *name, const char *account);
105 int _uam_db_update_user_info(uam_db_user_info_t *user);
110 uam_device_info_s dev_info;
112 unsigned long long last_seen;
116 int _uam_device_db_initialize(void);
117 int _uam_device_db_deinitialize(void);
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);
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);
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);
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);
148 /* SERVICE QUERIES */
152 char service_name[UAM_SERVICE_MAX_STRING_LEN];
154 int presence_threshold;
155 int absence_threshold;
159 int _uam_service_db_initialize(void);
160 int _uam_service_db_deinitialize(void);
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);
166 /* delete operations */
167 int _uam_db_delete_service_info(const char *service_name);
168 int _uam_service_db_clear(void);
171 int _uam_db_insert_service_info(
172 int *service_number, uam_service_info_s *svc, int cycle);
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);
179 /* DEVICE_SERVICE QUERIES */
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 */
190 int _uam_device_service_db_initialize(void);
191 int _uam_device_service_db_deinitialize(void);
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();
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);
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);
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);
228 /* IBEACON_ADV QUERIES */
233 char iadv[UAM_IBEACON_ADV_MAX_LEN + 1];
237 int _uam_adv_db_initialize(void);
238 int _uam_adv_db_deinitialize(void);
241 int _uam_db_insert_adv_info(unsigned int adv_len, const char *ibeacon_adv);
243 /* select operations */
244 GSList *_uam_db_get_all_advs(void);
246 /* delete operations */
247 int _uam_adv_db_clear(void);
250 /* PAYLOAD QUERIES */
252 uam_ble_payload_s payload_info;
256 int _uam_payload_db_initialize(void);
257 int _uam_payload_db_deinitialize(void);
260 int _uam_db_insert_payload_info(
261 const char *device_id, int tech_type,
262 const char *address, uam_ble_payload_s *payload);
264 /* delete operations */
265 int _uam_payload_db_clear(void);
267 /* select operations */
268 int _uam_db_get_payload_device_number(uam_ble_payload_s *payload,
270 int _uam_db_get_payload_number(uam_ble_payload_s *payload,
271 int *payload_number);
276 #endif /* __UA_MANAGER_DATABASE_H__ */