summaryrefslogtreecommitdiff
path: root/pm_lsensor.c
blob: 0f1cf521fcdc0c0aba3697761559bf6dd35ecd13 (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
/*
 * 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 <stdbool.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <glib.h>
#include <vconf.h>
#include <sensor.h>

#include "pm_core.h"
#include "pm_device_plugin.h"

#define SAMPLING_INTERVAL	1	/* 1 sec */
#define MAX_FAULT			5

static int (*prev_init_extention) (void *data);
static int (*_default_action) (int);
static int alc_timeout_id = 0;
static int sf_handle = -1;
static int max_brightness = 10;
static int fault_count = 0;

static gboolean alc_handler(gpointer data)
{
	int value = 0;
	static int cur_index = 1;
	static int old_index = 1;

	sensor_data_t light_data;
	if (cur_state != S_NORMAL){
		if (alc_timeout_id != 0)
			g_source_remove(alc_timeout_id);
		alc_timeout_id = 0;
	} else {
		if (sf_get_data(sf_handle, LIGHT_BASE_DATA_SET, &light_data) < 0) {
			fault_count++;
		} else {
			if (light_data.values[0] < 0.0 || light_data.values[0] > 10.0) {
				LOGINFO("fail to load light data : %d",	(int)light_data.values[0]);
				fault_count++;
			} else {
				int tmp_value;
				value = max_brightness * (int)light_data.values[0] / 10;
				plugin_intf->OEM_sys_get_backlight_brightness(DEFAULT_DISPLAY, &tmp_value);
				if (tmp_value != value) {
					set_default_brt(value);
					backlight_restore();
				}
				LOGINFO("load light data : %d, brightness : %d", (int)light_data.values[0], value);
			}
		}
	}

	if (fault_count > MAX_FAULT) {
		if (alc_timeout_id != 0)
			g_source_remove(alc_timeout_id);
		alc_timeout_id = 0;
		vconf_set_bool(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_BOOL, 0);
		LOGERR("Fault counts is over 5, disable automatic brightness");
		return FALSE;
	}

	if (alc_timeout_id != 0)
		return TRUE;

	return FALSE;
}

static int alc_action(int timeout)
{
	LOGDBG("alc action");
	/* sampling timer add */
	if (alc_timeout_id == 0 && !(status_flag & PWRSV_FLAG))
		alc_timeout_id =
		    g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
					       SAMPLING_INTERVAL,
					       (GSourceFunc) alc_handler, NULL,
					       NULL);

	if (_default_action != NULL)
		return _default_action(timeout);

	/* unreachable code */
	return -1;
}

static int connect_sfsvc()
{
	int sf_state = -1;
	/* connect with sensor fw */
	LOGDBG("connect with sensor fw");
	sf_handle = sf_connect(LIGHT_SENSOR);
	if (sf_handle < 0) {
		LOGERR("sensor attach fail");
		return -1;
	}
	sf_state = sf_start(sf_handle, 0);
	if (sf_state < 0) {
		LOGERR("sensor attach fail");
		sf_disconnect(sf_handle);
		sf_handle = -1;
		return -2;
	}
	fault_count = 0;
	return 0;
}

static int disconnect_sfsvc()
{
	LOGDBG("disconnect with sensor fw");
	if(sf_handle >= 0)
	{
		sf_stop(sf_handle);
		sf_disconnect(sf_handle);
		sf_handle = -1;
	}

	if (_default_action != NULL) {
		states[S_NORMAL].action = _default_action;
		_default_action = NULL;
	}
	if (alc_timeout_id != 0) {
		g_source_remove(alc_timeout_id);
		alc_timeout_id = 0;
	}

	return 0;
}

static int set_alc_function(keynode_t *key_nodes, void *data)
{
	int onoff = 0;

	if (key_nodes == NULL) {
		LOGERR("wrong parameter, key_nodes is null");
		return -1;
	}

	onoff = vconf_keynode_get_bool(key_nodes);

	if (onoff == true) {
		if(connect_sfsvc() < 0)
			return -1;

		/* change alc action func */
		if (_default_action == NULL)
			_default_action = states[S_NORMAL].action;
		states[S_NORMAL].action = alc_action;
		alc_timeout_id =
		    g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
					       SAMPLING_INTERVAL,
					       (GSourceFunc) alc_handler, NULL,
					       NULL);
	} else {
		disconnect_sfsvc();
	}

	return 0;
}

static gboolean check_sfsvc(gpointer data)
{
	/* this function will return opposite value for re-callback in fail */
	int vconf_auto;
	int sf_state = 0;

	LOGINFO("register sfsvc");

	vconf_get_bool(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_BOOL, &vconf_auto);
	if (vconf_auto == true) {
		if(connect_sfsvc() < 0)
			return TRUE;

		/* change alc action func */
		if (_default_action == NULL)
			_default_action = states[S_NORMAL].action;
		states[S_NORMAL].action = alc_action;
		alc_timeout_id =
			g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
					SAMPLING_INTERVAL,
					(GSourceFunc) alc_handler, NULL,
					NULL);
		if (alc_timeout_id != 0)
			        return FALSE;
		disconnect_sfsvc();
		return TRUE;
	}
	LOGINFO("change vconf value before registering sfsvc");
	return FALSE;
}

static int prepare_lsensor(void *data)
{
	int alc_conf;
	int sf_state = 0;

	plugin_intf->OEM_sys_get_backlight_max_brightness(DEFAULT_DISPLAY, &max_brightness);
	vconf_get_bool(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_BOOL, &alc_conf);

	if (alc_conf == true) {
		g_timeout_add_seconds_full(G_PRIORITY_DEFAULT,
				SAMPLING_INTERVAL,
				(GSourceFunc) check_sfsvc, NULL,
				NULL);
	}

	/* add auto_brt_setting change handler */
	vconf_notify_key_changed(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_BOOL,
				 (void *)set_alc_function, NULL);

	if (prev_init_extention != NULL)
		return prev_init_extention(data);

	return 0;
}

static void __attribute__ ((constructor)) pm_lsensor_init()
{
	_default_action = NULL;
	if (pm_init_extention != NULL)
		prev_init_extention = pm_init_extention;
	pm_init_extention = prepare_lsensor;
}

static void __attribute__ ((destructor)) pm_lsensor_fini()
{

}