summaryrefslogtreecommitdiff
path: root/src/agent/common/common_util.c
blob: 7c1bb702d166edcde759d67cb7f6eecec6c93225 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * oma-ds-agent
 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

/**
 *   @Common_Util.c
 *   @version									0.1
 *   @brief										This file is the source file of implementation of utility function
 */

#include <sync_agent.h>

#include "common/common_util.h"
#include "common/common_define_internal.h"

#ifndef OMADS_AGENT_LOG
#undef LOG_TAG
#define LOG_TAG "OMA_DS_COMMON"
#endif

#define MAX_RETRY_COUNT 10

bool create_config_str(int account_id, char *key, char *value, char *type, char *access_name, sync_agent_da_config_s ** config)
{
	_EXTERN_FUNC_ENTER;

	sync_agent_da_return_e da_err = SYNC_AGENT_DA_SUCCESS;

	da_err = sync_agent_create_config(config);
	if (da_err != SYNC_AGENT_DA_SUCCESS) {
		_DEBUG_ERROR("sync_agent_create_config is failed");
		goto error;
	}

	(*config)->config_id = account_id;
	(*config)->key = strdup(key);
	(*config)->value = value != NULL ? strcmp(value, "") == 0 ? NULL : strdup(value) : NULL;
	(*config)->type = strdup(type);
	(*config)->access_name = strdup(access_name);

 error:

	_EXTERN_FUNC_EXIT;

	if (da_err == SYNC_AGENT_DA_SUCCESS)
		return true;
	else
		return false;

}

bool set_config_str(int accountID, char *key, char *value, char *type, char *access_name)
{
	_EXTERN_FUNC_ENTER;

	sync_agent_da_return_e result;
	sync_agent_da_config_s config;

	config.config_id = accountID;
	config.key = key;
	config.value = value != NULL ? strcmp(value, "") == 0 ? NULL : value : NULL;
	config.type = type;
	config.access_name = access_name;

	result = sync_agent_update_config(&config);

	_EXTERN_FUNC_EXIT;

	if (result == SYNC_AGENT_DA_SUCCESS)
		return true;
	else
		return false;
}

bool set_config_int(int accountID, char *key, int value, char *type, char *access_name)
{
	_EXTERN_FUNC_ENTER;

	char *value_str = NULL;
	value_str = g_strdup_printf("%u", value);

	sync_agent_da_return_e result;
	sync_agent_da_config_s config;

	config.config_id = accountID;
	config.key = key;
	config.value = value_str;
	config.type = type;
	config.access_name = access_name;

	result = sync_agent_update_config(&config);

	if (value_str != NULL)
		free(value_str);

	_EXTERN_FUNC_EXIT;

	if (result == SYNC_AGENT_DA_SUCCESS)
		return true;
	else
		return false;
}

bool get_config(int account_id, char *key, char **value)
{
	_EXTERN_FUNC_ENTER;

	sync_agent_da_return_e result = SYNC_AGENT_DA_SUCCESS;
	sync_agent_da_config_s *config = NULL;

	*value = NULL;

	result = sync_agent_create_config(&config);
	if (result != SYNC_AGENT_DA_SUCCESS)
		return false;

	result = sync_agent_get_config(account_id, key, &config);
	if (result == SYNC_AGENT_DA_SUCCESS) {
		if (config != NULL) {
			if (config->value != NULL) {
				*value = strdup(config->value);
				_DEBUG_INFO("value = %s", *value);
			}
		}
	} else {
		/* FIXME  temporary solution
		 * Try MAX_RETRY_COUNT when fail to get_config
		 * */
		int i;
		bool success = false;
		for (i = 0; i < MAX_RETRY_COUNT; i++) {
			result = sync_agent_get_config(account_id, key, &config);
			if (result == SYNC_AGENT_DA_SUCCESS) {
				if (config != NULL) {
					if (config->value != NULL) {
						*value = strdup(config->value);
						_DEBUG_INFO("value = %s", *value);
						success = true;
						break;
					}
				}
			}
		}
		if (success == false) {
			sync_agent_free_config(config);
			_EXTERN_FUNC_EXIT;
			return false;
		}
	}

	sync_agent_free_config(config);
	_EXTERN_FUNC_EXIT;
	return true;
}

int get_account_id(char *profile, bool open)
{
	_EXTERN_FUNC_ENTER;

	retvm_if(profile == NULL, -1, "profile is NULL");

	int accountId = -1;
	char *profileDirName = NULL;
	bool result;

	sync_agent_acc_error_e acc_err = SYNC_AGENT_ACC_SUCCESS;
	sync_agent_fw_account_s *fw_account = NULL;
	GList *account_info_list = NULL;
	GList *iter = NULL;

	if (open == false) {
		sync_agent_da_return_e da_err = sync_agent_open_agent();
		if (da_err != SYNC_AGENT_DA_SUCCESS) {
			_DEBUG_ERROR("failed in sync_agent_open_agent");
			goto error;
		}
	}

	sync_agent_fw_account_query_s query;
	query.query = ACCOUNT_QUERY_BY_NONE;

	acc_err = sync_agent_query_fw_account(&query, &account_info_list);
	if (acc_err != SYNC_AGENT_ACC_SUCCESS) {
		_DEBUG_ERROR("sync_agent_query_fw_account is failed");
		goto error;
	}

	for (iter = account_info_list; iter != NULL; iter = g_list_next(iter)) {
		fw_account = (sync_agent_fw_account_s *) iter->data;

		if (profileDirName != NULL)
			free(profileDirName);

		result = get_config(fw_account->account_id, DEFINE_CONFIG_KEY_PROFILE_DIR_NAME, &profileDirName);
		if (result == false) {
			_DEBUG_ERROR("failed in get_Config");
			goto error;
		}

		_DEBUG_INFO("profileDirName = %s", profileDirName);
		if (profileDirName == NULL)
			continue;

		if (strcmp(profile, profileDirName) == 0) {
			_DEBUG_INFO("account_list[i] = %d", fw_account->account_id);
			accountId = fw_account->account_id;
			break;
		}
	}

	_EXTERN_FUNC_EXIT;

 error:

	sync_agent_free_fw_account_list(account_info_list);

	if (profileDirName != NULL)
		free(profileDirName);

	if (open == false)
		sync_agent_close_agent();

	return accountId;
}

/*int convert_synctype_value(char *syncType_str)
{
	int syncType_value;

	if (strcmp(syncType_str, DEFINE_ALERT_SLOW_SYNC_STR) ==0)
		syncType_value = ALERT_SLOW_SYNC ;
	else if (strcmp(syncType_str, DEFINE_ALERT_TWO_WAY_STR) ==0)
		syncType_value = ALERT_TWO_WAY ;
	else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR) ==0)
		syncType_value =  ALERT_ONE_WAY_FROM_CLIENT;
	else if (strcmp(syncType_str, DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR) ==0)
		syncType_value = ALERT_ONE_WAY_FROM_SERVER;
	else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_SERVER_STR) ==0)
		syncType_value = ALERT_REFRESH_FROM_SERVER;
	else if (strcmp(syncType_str, DEFINE_ALERT_REFRESH_FROM_CLIENT_STR) ==0)
		syncType_value = ALERT_REFRESH_FROM_CLIENT;
	else
		syncType_value = ALERT_UNKNOWN;

	return syncType_value;
}*/

/*char *convert_synctype_str(char *syncType_value)
{
	char *syncType_str = NULL;

	if (strcmp(syncType_value, DEFINE_ALERT_SLOW_SYNC_VALUE) ==0)
		syncType_str = DEFINE_ALERT_SLOW_SYNC_STR;
	else if (strcmp(syncType_value, DEFINE_ALERT_TWO_WAY_VALUE) ==0)
		syncType_str = DEFINE_ALERT_TWO_WAY_STR;
	else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0)
		syncType_str = DEFINE_ALERT_ONE_WAY_FROM_CLIENT_STR;
	else if (strcmp(syncType_value, DEFINE_ALERT_ONE_WAY_FROM_CLIENT_VALUE) ==0)
		syncType_str = DEFINE_ALERT_ONE_WAY_FROM_SERVER_STR;
	else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_SERVER_VALUE) ==0)
		syncType_str = DEFINE_ALERT_REFRESH_FROM_SERVER_STR;
	else if (strcmp(syncType_value, DEFINE_ALERT_REFRESH_FROM_CLIENT_VALUE) ==0)
		syncType_str = DEFINE_ALERT_REFRESH_FROM_CLIENT_STR;
	else
		syncType_str = DEFINE_ALERT_SLOW_SYNC_STR;

	return syncType_str;
}*/