summaryrefslogtreecommitdiff
path: root/src/core/mp-http-mgr.c
blob: fb72f59aec98f6866575d626dfa2a92bc3e5e249 (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
/*
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
 *
 * 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.
 */


#include "mp-player-debug.h"
#include "music.h"
#include "mp-http-mgr.h"

#define USER_AGENT_KEY		VCONFKEY_ADMIN_UAGENT

static MpHttpState_t	_mp_http_mgr_get_network_status();
static void _mp_http_mgr_refresh_network_info(mp_http_mgr_t *http_mgr);
static bool _mp_http_mgr_register_vconf_change_cb(mp_http_mgr_t *http_mgr);
static void _mp_http_mgr_ignore_vconf_change_cb();
static void _mp_http_mgr_network_config_changed_cb(keynode_t *node, void *user_data);
static void _mp_http_mgr_network_disconnect_cb(mp_http_mgr_t *http_mgr);


bool mp_http_mgr_create(void *data)
{
	DEBUG_TRACE("");
	struct appdata *ad = (struct appdata *)data;

	MP_CHECK_FALSE(ad);
	MP_CHECK_FALSE((!ad->http_mgr));

	ad->http_mgr = calloc(1, sizeof(mp_http_mgr_t));
	MP_CHECK_FALSE(ad->http_mgr);
	ad->http_mgr->ad = ad;

	if (!_mp_http_mgr_register_vconf_change_cb(ad->http_mgr))
		goto mp_exception;

	_mp_http_mgr_refresh_network_info(ad->http_mgr);

	return true;

      mp_exception:
	mp_http_mgr_destory(ad);
	return false;
}

bool mp_http_mgr_destory(void *data)
{
	struct appdata *ad = (struct appdata *)data;

	MP_CHECK_FALSE(ad);
	MP_CHECK_FALSE(ad->http_mgr);

	_mp_http_mgr_ignore_vconf_change_cb();
	_mp_http_mgr_network_disconnect_cb(ad->http_mgr);

	IF_FREE(ad->http_mgr);

	return TRUE;
}

const char* mp_http_mgr_get_ip(void *data)
{
	startfunc;
	struct appdata *ad = (struct appdata *)data;
	MP_CHECK_NULL(ad);
	MP_CHECK_NULL(ad->http_mgr);

	if (strlen(ad->http_mgr->ip) > 0)
		return (const char *)ad->http_mgr->ip;
	else
		return NULL;
}

const char* mp_http_mgr_get_proxy(void *data)
{
	startfunc;
	struct appdata *ad = (struct appdata *)data;
	MP_CHECK_NULL(ad);
	MP_CHECK_NULL(ad->http_mgr);

	if (strlen(ad->http_mgr->proxy) > 0)
		return (const char *)ad->http_mgr->proxy;
	else
		return NULL;
}

bool mp_http_mgr_get_svc_state(void *data, mp_http_svc_type svc_type)
{
	struct appdata *ad = (struct appdata *)data;
	MP_CHECK_FALSE(ad);
	MP_CHECK_FALSE(ad->http_mgr);

	mp_debug("[%d]svc state is [%d]", svc_type, ad->http_mgr->svc_state[svc_type]);
	return ad->http_mgr->svc_state[svc_type];
}

bool mp_http_mgr_start_svc(void *data, mp_http_svc_type svc_type, MpHttpOpenExcuteCb open_cb, MpHttpRespExcuteCb rsp_cb, gpointer cb_data)
{
	startfunc;
	struct appdata *ad = (struct appdata *)data;
	MP_CHECK_FALSE(ad);
	MP_CHECK_FALSE(ad->http_mgr);

	if (!open_cb && !rsp_cb) {
		mp_error("no callback");
		return FALSE;
	}

	if (open_cb) {
		if (!open_cb(cb_data)) {
			mp_error("[%d] svc fail to start", svc_type);
			return FALSE;
		}
	}

	mp_debug("## [%d] svc started ##", svc_type);
	mp_http_mgr_t *http_mgr = (mp_http_mgr_t *)ad->http_mgr;

	http_mgr->cb_data[svc_type] = cb_data;
	http_mgr->http_open_cb[svc_type] = open_cb;
	http_mgr->http_resp_cb[svc_type] = rsp_cb;

	http_mgr->svc_state[svc_type] = TRUE;
	return TRUE;
}

bool mp_http_mgr_stop_svc(void *data, mp_http_svc_type svc_type, mp_http_response_type response_type)
{
	startfunc;
	struct appdata *ad = (struct appdata *)data;
	MP_CHECK_FALSE(ad);
	MP_CHECK_FALSE(ad->http_mgr);

	mp_http_mgr_t *http_mgr = (mp_http_mgr_t *)ad->http_mgr;
	if (http_mgr->svc_state[svc_type] && http_mgr->http_resp_cb[svc_type]) {
		http_mgr->http_resp_cb[svc_type](http_mgr->cb_data[svc_type], response_type);
	}

	http_mgr->cb_data[svc_type] = NULL;
	http_mgr->http_open_cb[svc_type] = NULL;
	http_mgr->http_resp_cb[svc_type] = NULL;

	http_mgr->svc_state[svc_type] = FALSE;
	mp_debug("## [%d] svc stopped ##", svc_type);

	return TRUE;
}

static MpHttpState_t _mp_http_mgr_get_network_status()
{
	MpHttpState_t state = MP_HTTP_STATE_OFF;

	int status = 0;
	if (vconf_get_int(VCONFKEY_NETWORK_STATUS, &status) == 0) {
		mp_debug("Network status = [%d]", status);
	} else {
		mp_error("vconf_get_int() fail!!");
		status = 0;
	}

	if (status == VCONFKEY_NETWORK_CELLULAR)
		state = MP_HTTP_STATE_CELLULAR;
	else if (status == VCONFKEY_NETWORK_WIFI)
		state = MP_HTTP_STATE_WIFI;
	else
		state = MP_HTTP_STATE_OFF;

	return status;
}

static bool _mp_http_mgr_register_vconf_change_cb(mp_http_mgr_t *http_mgr)
{
	startfunc;
	MP_CHECK_FALSE(http_mgr);

	if (vconf_notify_key_changed(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND,
				_mp_http_mgr_network_config_changed_cb, http_mgr) != 0) {
		mp_error("vconf_notify_key_changed() fail");
		return FALSE;
	}

	return TRUE;
}

static void _mp_http_mgr_ignore_vconf_change_cb()
{
	startfunc;
	if (vconf_ignore_key_changed(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND,
				_mp_http_mgr_network_config_changed_cb) !=0) {
		mp_error("vconf_ignore_key_changed() fail");
	}
}

static void _mp_http_mgr_refresh_network_info(mp_http_mgr_t *http_mgr)
{
	startfunc;
	MP_CHECK(http_mgr);

	memset(http_mgr->ip, 0, sizeof(http_mgr->ip));
	memset(http_mgr->proxy, 0, sizeof(http_mgr->proxy));

	http_mgr->http_state = _mp_http_mgr_get_network_status();

	if (http_mgr->http_state != MP_HTTP_STATE_OFF) {
		char *ip = vconf_get_str(VCONFKEY_NETWORK_IP);
		if (ip) {
			strncpy(http_mgr->ip, ip, sizeof(http_mgr->ip)-1);
			free(ip);
			ip = NULL;
			mp_debug("IP [%s]", http_mgr->ip);
		}

		char *proxy = vconf_get_str(VCONFKEY_NETWORK_PROXY);
		if (proxy) {
			strncpy(http_mgr->proxy, proxy, sizeof(http_mgr->proxy)-1);
			free(proxy);
			proxy = NULL;
			mp_debug("PROXY [%s]", http_mgr->proxy);
		}
	}
}

static void _mp_http_mgr_network_config_changed_cb(keynode_t *node, void *user_data)
{
	startfunc;
	mp_http_mgr_t *http_mgr = (mp_http_mgr_t *)user_data;
	MP_CHECK(http_mgr);

	if (http_mgr->http_state != MP_HTTP_STATE_OFF) {
		_mp_http_mgr_network_disconnect_cb(http_mgr);
	}

	_mp_http_mgr_refresh_network_info(http_mgr);
}

static void _mp_http_mgr_network_disconnect_cb(mp_http_mgr_t *http_mgr)
{
	MP_CHECK(http_mgr);

	mp_debug("!! disconnect old connections !!");
	int i;
	for (i = 0 ; i < MP_HTTP_SVC_MAX; i++) {
		if (http_mgr->svc_state[i])
			mp_http_mgr_stop_svc(http_mgr->ad, i , MP_HTTP_RESPONSE_DISCONNECT);
	}
}

MpHttpState_t
mp_http_mgr_get_state(void *data)
{
	struct appdata *ad = (struct appdata *)data;

	MP_CHECK_FALSE(ad);
	if (!ad->http_mgr)
	{
		return MP_HTTP_STATE_NONE;
	}

	mp_http_mgr_t *http_mgr = ad->http_mgr;
	mp_debug("http_state = [%d]", http_mgr->http_state);

	return http_mgr->http_state;
}

char* mp_http_mgr_get_user_agent()
{
	char *user_agent = vconf_get_str(USER_AGENT_KEY);
	return user_agent;
}