aboutsummaryrefslogtreecommitdiffstats
path: root/tools/ipc-imei.c
blob: d3bff638f12c1ee5be9c694f04f01ac08b2cbe69 (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
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2014 Paul Kocialkowsk <contact@paulk.fr>
 * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
 *
 * libsamsung-ipc 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 2 of the License, or
 * (at your option) any later version.
 *
 * libsamsung-ipc 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 libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#include "common/modem.h"

void modem_log_handler(void *user_data, const char *msg)
{
	int i, l;
	char *message;

	message = strdup(msg);
	l = strlen(message);

	if (l > 1) {
		for (i = l ; i > 0 ; i--) {
			if (message[i] == '\n')
				message[i] = 0;
			else if (message[i] != 0)
				break;
		}
		if (user_data == NULL) {
			printf("[ ipc ] %s\n", message);
		}
		if (user_data != NULL) {
			printf("[ %s ] %s\n", user_data, message);
		}
	}

	free(message);
}

static void usage(char* progname)
{
	printf("Usage: %s\n", progname);
}

/* TODO: check authorship from git log in samsung-ipc/ipc.c */
void ipc_imei_log(struct ipc_client *client, const char *message, ...)
{
        char buffer[4096];
        va_list args;

        if (client == NULL || message == NULL)
                return;

        va_start(args, message);
        vsnprintf((char *) &buffer, sizeof(buffer), message, args);
	modem_log_handler("ipc-imei", buffer); /* No access to ipc_client) */
        va_end(args);
}

int ipc_imei_request_imei(struct ipc_client *client)
{
	struct ipc_misc_me_sn_request_data request_data;
	int rc;

	if (!client)
		return 0;

	ipc_imei_log(client, "ENTER %s\n", __func__);

	request_data.type = IPC_MISC_ME_SN_SERIAL_NUM;

	rc = ipc_client_send(client, seq_get(), IPC_MISC_ME_SN, IPC_TYPE_GET,
			     (void *) &request_data, sizeof(request_data));
	if (rc < 0)
		ipc_imei_log(client, "ipc_client_send failed with error %d\n",
			       rc);

	ipc_imei_log(client, "EXIT %s\n", __func__);
	return rc;
}

int ipc_imei_parse_imei_response(struct ipc_client *client,
				 struct ipc_message *message,
				 __attribute__((unused)) void* app_data)
{
	struct ipc_misc_me_sn_response_data *data;
	char *imei;

	if (!client)
		return 0;

	if (message == NULL) {
		ipc_imei_log(client,
			       "%s: ipc_message is null\n", __FUNCTION__);
		return -EAGAIN;
	}

	data = (struct ipc_misc_me_sn_response_data *) message->data;

	if (data->type != IPC_MISC_ME_SN_SERIAL_NUM)
		return -EAGAIN;

	imei = ipc_misc_me_sn_extract(data);

	if (strlen (imei) > 15 * sizeof(char))
		imei[15] = '\0';

	ipc_imei_log(client, "%s: found IMEI '%s'\n", __FUNCTION__, imei);

	free(imei);

	/* Exit */
	return 0;
}

static int ipc_imei_response_handle(struct ipc_client *client,
				    struct ipc_message *resp,
				    void *data)
{
	int rc;

	ipc_client_log(client, "ENTER HANDLER %s\n", __func__);

	if (!client)
		return 0;

	switch (IPC_GROUP(resp->command)) {
	case IPC_GROUP_MISC:
		if (resp->command == IPC_MISC_ME_SN) {
			rc = ipc_imei_parse_imei_response(client, resp, data);
			return rc;
		}
	default:
		ipc_imei_log(client, "Unhandled %s command",
			       ipc_group_string(IPC_GROUP(resp->command)));
		return -EAGAIN;
	}
}


int main(int argc, char** argv)
{
	struct ipc_client *client = NULL;
	struct app_modem_response_handler handler;

	int rc;

	if (argc == 0) {
		exit(EX_USAGE);
	} else if (argc != 1) {
		usage(argv[0]);
		exit(EX_USAGE);
	}

	client = ipc_client_create(IPC_CLIENT_TYPE_FMT);
	if (client == NULL) {
		printf("Creating client failed\n");
		return 1;
	}

	rc = ipc_client_log_callback_register(client, modem_log_handler, NULL);
	if (rc < 0) {
		printf("ipc_imei_log_callback_registerma failed:"
		       " error %d\n", rc);
		return 1;
	}
	ipc_imei_log(client, "ipc_client_log_callback_register done");

	/* register new callback that would take over after modem_start is
	 *  done
	 */
	rc = register_app_modem_response_handler(client, &handler,
						 ipc_imei_response_handle,
						 NULL);
	if (rc < 0) {
		ipc_imei_log(client,
			       "register_app_modem_response_handler failed: "
			       "error %d\n",
			       rc);
		return 1;
	}

	ipc_imei_log(client, "Starting modem");

	rc = modem_start(client, MODEM_STATE_LPM, &handler);
	if (rc < 0) {
		ipc_imei_log(client, "modem_start failed: error %d\n", rc);
		return 1;
	}

	ipc_imei_log(client, "modem_start done\n");

	// ipc_imei_request_imei(client);

	ipc_imei_log(client, "ipc_imei_request_imei done\n");

	/* TODO: MODEM_STATE_LPM is not needed here */
	rc = modem_read_loop(client, MODEM_STATE_LPM, &handler);
	if (rc < 0)
		ipc_imei_log(client,
			       "ipc-imei: modem_read_loop failed: error %d\n",
			       rc);

	ipc_imei_log(client, "ipc-imei: modem_read_loop done");

	rc = modem_stop(client);
	if (rc < 0) {
		ipc_imei_log(client, "modem_stop failed: error %d\n", rc);
		return 1;
	}

	return 0;
}