aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/ipc_utils.c
blob: 3518475d146a8dd85bb42b108fc50cff6834ac5f (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
 * Copyright (C) 2013-2014 Paul Kocialkowski <contact@paulk.fr>
 *
 * 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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <asm/types.h>

#include <samsung-ipc.h>

#include "ipc.h"

int ipc_seq_valid(unsigned char seq)
{
	if (seq == 0x00 || seq == 0xff)
		return 0;

	return 1;
}

int ipc_data_dump(struct ipc_client *client, const void *data, size_t size)
{
	unsigned int cols = 8;
	unsigned int cols_count = 2;
	int spacer;
	char string[81] = { 0 };
	char final[161] = { 0 };
	size_t length;
	char *print;
	unsigned char *p;
	unsigned int offset;
	unsigned int rollback;
	unsigned int i, j, k;
	int rc;

	if (data == NULL || size == 0)
		return -1;

	/*
	 * spacer = string length - offset print length - data print length -
	 *	    ascii print length
	 */
	spacer = (sizeof(string) - 1) - 6 -
		(3 * cols * cols_count - 1 + (cols_count - 1)) -
		(cols * cols_count + cols_count - 1);

	/* Need 3 spacers */
	spacer /= 3;

	if (spacer <= 0)
		return -1;

	p = (unsigned char *) data;
	offset = 0;

	while (offset < size) {
		rollback = 0;

		print = (char *) &string;
		length = sizeof(string);

		/* Offset print */

		rc = snprintf(print, length, "[%04x]", offset);
		print += rc;
		length -= rc;

		/* Spacer print */

		for (i = 0; i < (unsigned int) spacer; i++) {
			*print++ = ' ';
			length--;
		}

		/* Data print */

		for (i = 0; i < cols_count; i++) {
			for (j = 0; j < cols; j++) {
				if (offset < size) {
					rc = snprintf(print, length, "%02X",
						      *p);
					print += rc;
					length -= rc;

					p++;
					offset++;
					rollback++;
				} else {
					for (k = 0; k < 2; k++) {
						*print++ = ' ';
						length--;
					}
				}

				if (j != (cols - 1)) {
					*print++ = ' ';
					length--;
				}
			}

			if (i != (cols_count - 1)) {
				for (k = 0; k < 2; k++) {
					*print++ = ' ';
					length--;
				}
			}
		}

		/* Spacer print */
		for (i = 0; i < (unsigned int) spacer; i++) {
			*print++ = ' ';
			length--;
		}

		/* ASCII print */
		p -= rollback;
		offset -= rollback;

		for (i = 0; i < cols_count; i++) {
			for (j = 0; j < cols; j++) {
				if (offset < size) {
					if (isascii(*p) && isprint(*p))
						*print = *p;
					else
						*print = '.';

					print++;
					length--;

					p++;
					offset++;
					rollback++;
				}
			}

			if (i != (cols_count - 1) && offset < size) {
				*print++ = ' ';
				length--;
			}
		}

		*print = '\0';

		/* Escape string */
		j = 0;
		for (i = 0; i < sizeof(string); i++) {
			if (string[i] == '%')
				final[j++] = string[i];

			final[j++] = string[i];
		}

		ipc_client_log(client, final);
	}

	return 0;
}

void ipc_client_log_send(struct ipc_client *client, struct ipc_message *message,
			 const char *prefix)
{
	if (client == NULL || message == NULL || prefix == NULL)
		return;

	switch (client->type) {
	case IPC_CLIENT_TYPE_FMT:
		ipc_client_log(client, "\n");
		ipc_client_log(client, "%s: Sent FMT message", prefix);
		ipc_client_log(
			client,
			"%s: Message: mseq=0x%02x, command=%s, type=%s, size=%d",
			prefix, message->mseq,
			ipc_command_string(message->command),
			ipc_request_type_string(message->type),
			message->size);
#ifdef DEBUG
		if (message->size > 0) {
			ipc_client_log(
				client,
				"================================= IPC FMT data =================================");
			ipc_data_dump(client, (void *) message->data,
				      message->size > 0x100 ?
				      0x100 : message->size);
			ipc_client_log(
				client,
				"================================================================================");
		}
#endif
		break;
	case IPC_CLIENT_TYPE_RFS:
		ipc_client_log(client, "\n");
		ipc_client_log(client, "%s: Sent RFS message", prefix);
		ipc_client_log(client,
			       "%s: Message: mseq=0x%02x, command=%s, size=%d",
			       prefix, message->mseq,
			       ipc_command_string(message->command),
			       message->size);
#ifdef DEBUG
		if (message->size > 0) {
			ipc_client_log(
				client,
				"================================= IPC RFS data =================================");
			ipc_data_dump(client, (void *) message->data,
				      message->size > 0x100 ?
				      0x100 : message->size);
			ipc_client_log(
				client,
				"================================================================================");
		}
#endif
		break;
	}
}

void ipc_client_log_recv(struct ipc_client *client, struct ipc_message *message,
			 const char *prefix)
{
	if (client == NULL || message == NULL || prefix == NULL)
		return;

	switch (client->type) {
	case IPC_CLIENT_TYPE_FMT:
		ipc_client_log(client, "\n");
		ipc_client_log(client, "%s: Received FMT message", prefix);
		ipc_client_log(
			client,
			"%s: Message: aseq=0x%02x, command=%s, type=%s, size=%d",
			prefix, message->aseq,
			ipc_command_string(message->command),
			ipc_response_type_string(message->type),
			message->size);
#ifdef DEBUG
		if (message->size > 0) {
			ipc_client_log(
				client,
				"================================= IPC FMT data =================================");
			ipc_data_dump(client, (void *) message->data,
				      message->size > 0x100 ?
				      0x100 : message->size);
			ipc_client_log(
				client,
				"================================================================================");
		}
#endif
		break;
	case IPC_CLIENT_TYPE_RFS:
		ipc_client_log(client, "\n");
		ipc_client_log(client, "%s: Received RFS message", prefix);
		ipc_client_log(client,
			       "%s: Message: aseq=0x%02x, command=%s, size=%d",
			       prefix, message->aseq,
			       ipc_command_string(message->command),
			       message->size);
#ifdef DEBUG
		if (message->size > 0) {
			ipc_client_log(
				client,
				"================================= IPC RFS data =================================");
			ipc_data_dump(client, (void *) message->data,
				      message->size > 0x100 ?
				      0x100 : message->size);
			ipc_client_log(
				client,
				"================================================================================");
		}
#endif
		break;
	}
}

int ipc_fmt_header_setup(struct ipc_fmt_header *header,
			 const struct ipc_message *message)
{
	if (header == NULL || message == NULL)
		return -1;

	memset(header, 0, sizeof(struct ipc_fmt_header));
	header->length = message->size + sizeof(struct ipc_fmt_header);
	header->mseq = message->mseq;
	header->aseq = message->aseq;
	header->group = IPC_GROUP(message->command);
	header->index = IPC_INDEX(message->command);
	header->type = message->type;

	return 0;
}

int ipc_fmt_message_setup(const struct ipc_fmt_header *header,
			  struct ipc_message *message)
{
	if (header == NULL || message == NULL)
		return -1;

	memset(message, 0, sizeof(struct ipc_message));
	message->mseq = header->mseq;
	message->aseq = header->aseq;
	message->command = IPC_COMMAND(header->group, header->index);
	message->type = header->type;
	message->data = NULL;
	message->size = 0;

	return 0;
}

int ipc_rfs_header_setup(struct ipc_rfs_header *header,
			 const struct ipc_message *message)
{
	if (header == NULL || message == NULL)
		return -1;

	memset(header, 0, sizeof(struct ipc_rfs_header));
	header->length = message->size + sizeof(struct ipc_rfs_header);
	header->id = message->mseq;
	header->index = IPC_INDEX(message->command);

	return 0;
}

int ipc_rfs_message_setup(const struct ipc_rfs_header *header,
			  struct ipc_message *message)
{
	if (header == NULL || message == NULL)
		return -1;

	memset(message, 0, sizeof(struct ipc_message));
	message->aseq = header->id;
	message->command = IPC_COMMAND(IPC_GROUP_RFS, header->index);
	message->data = NULL;
	message->size = 0;

	return 0;
}