modetransition.c 6.39 KB
Newer Older
1 2
/**
 * @file modetransition.c
Santtu Lakkala's avatar
Santtu Lakkala committed
3
 * This file implements the submode handling component
4 5
 * of the Mode Control Entity
 * <p>
Santtu Lakkala's avatar
Santtu Lakkala committed
6
 * Copyright © 2004-2011 Nokia Corporation and/or its subsidiary(-ies).
7
 * Copyright (C) 2013-2019 Jolla Ltd.
8 9
 * <p>
 * @author David Weinehall <david.weinehall@nokia.com>
10 11 12 13
 * @author Tapio Rantala <ext-tapio.rantala@nokia.com>
 * @author Santtu Lakkala <ext-santtu.1.lakkala@nokia.com>
 * @author Irina Bezruk <ext-irina.bezruk@nokia.com>
 * @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * mce is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * mce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with mce.  If not, see <http://www.gnu.org/licenses/>.
 */

28
#include "modetransition.h"
29

30 31 32
#include "mce.h"
#include "mce-log.h"
#include "mce-io.h"
33

34 35
#include <unistd.h>
#include <errno.h>
36

37
#include <glib/gstdio.h>
38 39 40 41 42 43 44 45 46 47 48 49 50 51

/**
 * Set the MCE submode flags
 *
 * @param submode All submodes to set OR:ed together
 * @return TRUE on success, FALSE on failure
 */
static gboolean mce_set_submode_int32(const submode_t submode)
{
	submode_t old_submode = datapipe_get_gint(submode_pipe);

	if (old_submode == submode)
		goto EXIT;

52 53
	mce_log(LL_NOTICE, "submode change: %s",
		submode_change_repr(old_submode, submode));
54

55
	datapipe_exec_full(&submode_pipe, GINT_TO_POINTER(submode));
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
EXIT:
	return TRUE;
}

/**
 * Add flags to the MCE submode
 *
 * @param submode submode(s) to add OR:ed together
 * @return TRUE on success, FALSE on failure
 */
gboolean mce_add_submode_int32(const submode_t submode)
{
	submode_t old_submode = datapipe_get_gint(submode_pipe);

	return mce_set_submode_int32(old_submode | submode);
}

/**
 * Remove flags from the MCE submode
 *
 * @param submode submode(s) to remove OR:ed together
 * @return TRUE on success, FALSE on failure
 */
gboolean mce_rem_submode_int32(const submode_t submode)
{
	submode_t old_submode = datapipe_get_gint(submode_pipe);

	return mce_set_submode_int32(old_submode & ~submode);
}

/**
 * Return all set MCE submode flags
 *
 * @return All set submode flags OR:ed together
 */
submode_t mce_get_submode_int32(void) G_GNUC_PURE;
submode_t mce_get_submode_int32(void)
{
	submode_t submode = datapipe_get_gint(submode_pipe);

	return submode;
}

/**
 * Handle system state change
 *
 * @param data The system state stored in a pointer
 */
static void system_state_trigger(gconstpointer data)
{
106
	static system_state_t old_system_state = MCE_SYSTEM_STATE_UNDEF;
107 108 109
	system_state_t system_state = GPOINTER_TO_INT(data);

	switch (system_state) {
110
	case MCE_SYSTEM_STATE_USER:
111 112
		break;

113 114
	case MCE_SYSTEM_STATE_SHUTDOWN:
	case MCE_SYSTEM_STATE_REBOOT:
115 116
		/* Actions to perform when shutting down/rebooting
		 */
117
		switch( old_system_state ) {
118 119 120 121
		case MCE_SYSTEM_STATE_USER:
		case MCE_SYSTEM_STATE_BOOT:
		case MCE_SYSTEM_STATE_UNDEF:
		case MCE_SYSTEM_STATE_ACTDEAD:
122 123
			datapipe_exec_full(&led_pattern_deactivate_pipe, MCE_LED_PATTERN_DEVICE_ON);
			datapipe_exec_full(&led_pattern_activate_pipe, MCE_LED_PATTERN_POWER_OFF);
124 125 126 127
			break;

		default:
			break;
128 129
		}

130 131 132 133
		/* If we're shutting down/rebooting from actdead or
		 * user mode, ui side will do shutdown animation.
		 * Unblank the screen to make it visible. */
		switch( old_system_state ) {
134 135
		case MCE_SYSTEM_STATE_USER:
		case MCE_SYSTEM_STATE_ACTDEAD:
136
			mce_datapipe_request_display_state(MCE_DISPLAY_ON);
137
			break;
138

139 140 141
		default:
			break;
		}
142 143
		break;

144
	case MCE_SYSTEM_STATE_ACTDEAD:
145 146
		break;

147
	case MCE_SYSTEM_STATE_UNDEF:
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		goto EXIT;

	default:
		break;
	}

	mce_log(LL_DEBUG,
		"dsmestate set to: %d (old: %d)",
		system_state, old_system_state);

	old_system_state = system_state;

EXIT:
	return;
}

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
/** Array of datapipe handlers */
static datapipe_handler_t mce_mode_datapipe_handlers[] =
{
	// output triggers
	{
		.datapipe  = &system_state_pipe,
		.output_cb = system_state_trigger,
	},
	// sentinel
	{
		.datapipe = 0,
	}
};

static datapipe_bindings_t mce_mode_datapipe_bindings =
{
	.module   = "mce_mode",
	.handlers = mce_mode_datapipe_handlers,
};

/** Append triggers/filters to datapipes
 */
static void mce_mode_datapipe_init(void)
{
	mce_datapipe_init_bindings(&mce_mode_datapipe_bindings);
}

/** Remove triggers/filters from datapipes
 */
static void mce_mode_datapipe_quit(void)
{
	mce_datapipe_quit_bindings(&mce_mode_datapipe_bindings);
}

198 199 200 201 202 203 204 205 206 207
/**
 * Init function for the modetransition component
 *
 * @return TRUE on success, FALSE on failure
 */
gboolean mce_mode_init(void)
{
	gboolean status = FALSE;

	/* Append triggers/filters to datapipes */
208
	mce_mode_datapipe_init();
209 210 211 212 213 214 215

	/* If the bootup file exists, mce has crashed / restarted;
	 * since it exists in /var/run it will be removed when we reboot.
	 *
	 * If the file doesn't exist, create it to ensure that
	 * restarting mce doesn't get mce stuck in the transition submode
	 */
Santtu Lakkala's avatar
Santtu Lakkala committed
216
	if (g_access(MCE_BOOTUP_FILENAME, F_OK) == -1) {
217 218
		if (errno == ENOENT) {
			mce_log(LL_DEBUG, "Bootup mode enabled");
219
			mce_add_submode_int32(MCE_SUBMODE_TRANSITION);
220 221
			errno = 0;

222 223
			mce_write_string_to_file(MCE_BOOTUP_FILENAME,
						 ENABLED_STRING);
224 225

			if (g_access(MALF_FILENAME, F_OK) == 0) {
226
				mce_add_submode_int32(MCE_SUBMODE_MALF);
227 228 229 230 231 232 233 234 235
				mce_log(LL_DEBUG, "Malf mode enabled");
				if (g_access(MCE_MALF_FILENAME, F_OK) == -1) {
					if (errno != ENOENT) {
						mce_log(LL_CRIT,
							"access() failed: %s. Exiting.",
							g_strerror(errno));
						goto EXIT;
					}

236 237
					mce_write_string_to_file(MCE_MALF_FILENAME,
								 ENABLED_STRING);
238 239
				}
			}
240 241 242 243 244 245
		} else {
			mce_log(LL_CRIT,
				"access() failed: %s. Exiting.",
				g_strerror(errno));
			goto EXIT;
		}
246 247 248
	} else {
		if (g_access(MALF_FILENAME, F_OK) == 0) {
			if (g_access(MCE_MALF_FILENAME, F_OK) == 0) {
249
				mce_add_submode_int32(MCE_SUBMODE_MALF);
250 251 252 253 254 255
				mce_log(LL_DEBUG, "Malf mode enabled");
			}
		} else if ((errno == ENOENT) &&
			   (g_access(MCE_MALF_FILENAME, F_OK) == 0)) {
			g_remove(MCE_MALF_FILENAME);
		}
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	}

	status = TRUE;

EXIT:
	return status;
}

/**
 * Exit function for the modetransition component
 *
 * @todo D-Bus unregistration
 */
void mce_mode_exit(void)
{
	/* Remove triggers/filters from datapipes */
272
	mce_mode_datapipe_quit();
273 274 275

	return;
}