summaryrefslogtreecommitdiffstats
path: root/power.c
blob: dde9e17b8956966ab1b8b6bb95f6d8f73d9ab716 (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
/*
 * This file is part of QMI-RIL.
 *
 * Copyright (C) 2017 Wolfgang Wiedmeyer <wolfgit@wiedmeyer.de>
 *
 * QMI-RIL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * QMI-RIL 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with QMI-RIL.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>

#define LOG_TAG "RIL"
#include <utils/Log.h>

#include <qmi-ril.h>

void dms_event_report_indication_cb(QmiClientDms *client,
				    QmiIndicationDmsEventReportOutput *output)
{
	QmiDmsOperatingMode mode;

	qmi_indication_dms_event_report_output_get_operating_mode(
		output, &mode, NULL);

	RIL_LOGD("Got operating mode indication:\n"
		 "\tMode: '%s'", qmi_dms_operating_mode_get_string(mode));

	switch (mode) {
	case QMI_DMS_OPERATING_MODE_LOW_POWER:
		RIL_LOGD("Power state is low power mode");
		ril_radio_state_update(RADIO_STATE_OFF);
		break;
	case QMI_DMS_OPERATING_MODE_SHUTTING_DOWN:
		RIL_LOGD("Power: device is in the process of shutting down");
		ril_radio_state_update(RADIO_STATE_OFF);
		break;
	case QMI_DMS_OPERATING_MODE_ONLINE:
		RIL_LOGD("Power state is normal");
		ril_radio_state_update(RADIO_STATE_SIM_NOT_READY);
		break;
	default:
		RIL_LOGE("Unknown power state");
	}
}

static void
set_operating_mode_ready(QmiClientDms *client, GAsyncResult *res,
			 RIL_Token token)
{
	QmiMessageDmsSetOperatingModeOutput *output;
	GError *error = NULL;

	output = qmi_client_dms_set_operating_mode_finish(client, res,
							  &error);
	if (!output) {
		RIL_LOGE("%s: error: operation failed: %s", __func__,
			 error->message);
		goto error;
	}

	if (!qmi_message_dms_set_operating_mode_output_get_result(
		    output, &error)) {
		RIL_LOGE("error: couldn't set operating mode: %s",
			 error->message);
		goto error;
	}

	RIL_LOGD("Operating mode  set successfully");

	ril_request_complete(token, RIL_E_SUCCESS, NULL, 0);
	goto complete;

error:
	ril_request_complete(token, RIL_E_GENERIC_FAILURE, NULL, 0);

complete:
	if (error)
		g_error_free(error);
	if (output)
		qmi_message_dms_set_operating_mode_output_unref(output);
}

int ril_request_radio_power(void *data, size_t size, RIL_Token token)
{
	struct ril_request *request;
	int power_state;
	QmiMessageDmsSetOperatingModeInput *input = NULL;
	QmiDmsOperatingMode mode;
	GError *error = NULL;
	int rc;

	if (data == NULL || size < sizeof(power_state))
		goto error;

	request = ril_request_find_request_status(RIL_REQUEST_RADIO_POWER, RIL_REQUEST_HANDLED);
	if (request != NULL)
		return RIL_REQUEST_UNHANDLED;

	power_state = *((int *)data);

	if (power_state > 0) {
		RIL_LOGD("Requesting normal power state");
		mode = QMI_DMS_OPERATING_MODE_ONLINE;
	} else {
		RIL_LOGD("Requesting low power mode power state");
		mode = QMI_DMS_OPERATING_MODE_LOW_POWER;
	}

	input = qmi_message_dms_set_operating_mode_input_new();
	if (!qmi_message_dms_set_operating_mode_input_set_mode(
		    input, mode, &error)) {
		RIL_LOGE("error: couldn't create input data bundle: '%s'",
			 error->message);
		goto error;
	}

	qmi_client_dms_set_operating_mode(ctx->DmsClient, input, 10,
					  ctx->cancellable,
					  (GAsyncReadyCallback)set_operating_mode_ready,
					  token);

	rc = RIL_REQUEST_HANDLED;
	goto complete;

error:
	ril_request_complete(token, RIL_E_GENERIC_FAILURE, NULL, 0);

	rc = RIL_REQUEST_COMPLETED;

complete:
	if (error)
		g_error_free(error);
	if (input)
		qmi_message_dms_set_operating_mode_input_unref(input);

	return rc;
}