summaryrefslogtreecommitdiffstats
path: root/tools/srs-test.c
blob: ac91f1a3314d79eb1748b5d6d0c991bb1d1afd10 (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
/*
 * This file is part of Samsung-RIL.
 *
 * Copyright (C) 2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * Samsung-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.
 *
 * Samsung-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 Samsung-RIL.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <telephony/ril.h>

#include <samsung-ril-socket.h>
#include <srs-client.h>

struct radio_state_string {
	char *string;
	RIL_RadioState state;
};

void display_help(void)
{
	printf("Usage: srs-test [COMMAND]\n");
	printf("\n");
	printf("Commands:\n");
	printf("  radio-state [STATE] - set radio state\n");
	printf("\n");
	printf("States: off, unavailable, sim-not-ready, sim-locked-absent, sim-ready, on\n");
}

int radio_state(struct srs_client *client, char *string)
{
	struct radio_state_string radio_state_strings[] = {
		{ "off",		RADIO_STATE_OFF },
		{ "unavailable",	RADIO_STATE_UNAVAILABLE },
		{ "sim-not-ready",	RADIO_STATE_SIM_NOT_READY },
		{ "sim-locked-absent",	RADIO_STATE_SIM_LOCKED_OR_ABSENT },
		{ "sim-ready",		RADIO_STATE_SIM_READY },
		{ "on",			RADIO_STATE_ON },
	};
	struct srs_test_set_radio_state_data data;
	RIL_RadioState state = 0;
	unsigned int count;
	unsigned int i;
	int rc;

	if (client == NULL || string == NULL)
		return -1;

	count = sizeof(radio_state_strings) / sizeof(struct radio_state_string);

	for (i = 0; i < count; i++) {
		if (radio_state_strings[i].string == NULL)
			break;

		if (strcmp(radio_state_strings[i].string, string) == 0) {
			state = radio_state_strings[i].state;
			break;
		}
	}

	if (i >= count)
		return -1;

	printf("%s: Setting radio state to %s\n", __func__, string);

	memset(&data, 0, sizeof(data));
	data.state = (int) state;

	rc = srs_client_send(client, SRS_TEST_SET_RADIO_STATE, &data, sizeof(data));
	if (rc < 0) {
		printf("%s: Settings radio state failed\n", __func__);
		return -1;
	}

	return 0;
}

int main(int argc, char *argv[])
{
	int (*callback)(struct srs_client *client, char *string) = NULL;
	struct srs_client *client = NULL;
	char *string = NULL;
	int rc;

	if (argc < 2) {
		display_help();
		goto error;
	}

	if (strcmp(argv[1], "radio-state") == 0) {
		if (argc < 3) {
			display_help();
			goto error;
		}

		callback = radio_state;
		string = argv[2];
	} else {
		display_help();
		goto error;
	}

	if (callback == NULL)
		goto error;

	client = srs_client_create();
	if (client == NULL) {
		printf("Creating SRS client failed\n");
		goto error;
	}

	rc = srs_client_open(client);
	if (rc < 0) {
		printf("Opening SRS client failed\n");
		goto error;
	}

	rc = callback(client, string);
	if (rc < 0)
		goto error;

	rc = 0;
	goto complete;

error:
	rc = 1;

complete:
	if (client != NULL) {
		srs_client_close(client);
		srs_client_destroy(client);
	}

	return rc;
}