summaryrefslogtreecommitdiffstats
path: root/samsung-ril-client/samsung-ril-client.c
blob: 102af875bd1a212a344bc10503c487e3352b993a (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
356
357
358
359
360
/**
 * Samsung RIL Client (Samsung RIL Socket Client-side implementation)
 *
 * Copyright (C) 2011 Paul Kocialkowski <contact@paulk.fr>
 *
 * samsung-ril-client 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-client 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-client.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/select.h>

#include <signal.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <cutils/sockets.h>

#define LOG_TAG "SRS-Client"
#include <cutils/log.h>

#include <ril_interface.h>
#include <samsung-ril-socket.h>

/*
 * Structures
 */

struct srs_client {
	int fd;
};

/*
 * SRS transport
 */

int srs_send_message(struct srs_client *client, struct srs_message *message)
{
	struct srs_header header;
	void *data;
	int client_fd = client->fd;
	fd_set fds;
	int rc;

	if(client_fd < 0)
		return -1;

	header.length = message->data_len + sizeof(header);
	header.group = SRS_GROUP(message->command);
	header.index = SRS_INDEX(message->command);

	data = malloc(header.length);
	memset(data, 0, header.length);

	memcpy(data, &header, sizeof(header));
	memcpy((void *) (data + sizeof(header)), message->data, message->data_len);

	FD_ZERO(&fds);
	FD_SET(client_fd, &fds);

	// We can't rely on select RC
	select(client_fd + 1, NULL, &fds, NULL, NULL);

	rc = write(client_fd, data, header.length);

	free(data);

	return rc;
}

int srs_send(struct srs_client *client, unsigned short command, void *data, int data_len)
{
	struct srs_message message;
	int rc;

	LOGE("%s", __func__);

	message.command = command;
	message.data = data;
	message.data_len = data_len;

	rc = srs_send_message(client, &message);

	return rc;
}

int srs_recv_timed(struct srs_client *client, struct srs_message *message, long sec, long usec)
{
	void *raw_data = malloc(SRS_DATA_MAX_SIZE);
	struct srs_header *header;
	int rc;

	int client_fd = client->fd;

	if(client_fd < 0)
		return -1;

	struct timeval timeout;
	fd_set fds;

	FD_ZERO(&fds);
	FD_SET(client_fd, &fds);

	timeout.tv_sec = sec;
	timeout.tv_usec = usec;

	select(client_fd + 1, &fds, NULL, NULL, &timeout);

	rc = read(client_fd, raw_data, SRS_DATA_MAX_SIZE);
	if(rc < sizeof(struct srs_header)) {
		return -1;
	}

	header = raw_data;

	message->command = SRS_COMMAND(header);
	message->data_len = header->length - sizeof(struct srs_header);
	message->data = malloc(message->data_len);

	memcpy(message->data, raw_data + sizeof(struct srs_header), message->data_len);

	free(raw_data);

	return 0;
}

int srs_recv(struct srs_client *client, struct srs_message *message)
{
	return srs_recv_timed(client, message, 0, 0);
}

int srs_ping(struct srs_client *client)
{
	int caffe_w = SRS_CONTROL_CAFFE;
	int caffe_r = 0;	
	int rc = 0;

	struct srs_message message;

	rc = srs_send(client, SRS_CONTROL_PING, &caffe_w, sizeof(caffe_w));

	if(rc < 0) {
		return -1;
	}

	rc = srs_recv_timed(client, &message, 0, 300);

	if(rc < 0) {
		return -1;
	}

	if(message.data == NULL) 
		return -1;

	caffe_r = *((int *) message.data);

	if(caffe_r == SRS_CONTROL_CAFFE) {
		LOGD("Caffe is ready!");
		rc = 0;
	} else {
		LOGE("Caffe went wrong!");
		rc = -1;
	}

	free(message.data);
	return rc;
}

/*
 * Interface
 */

void *OpenClient_RILD(void)
{
	struct srs_client *client = NULL;

	LOGE("%s", __func__);

	signal(SIGPIPE, SIG_IGN);

	client = calloc(1, sizeof(struct srs_client));
	client->fd = -1;

	return (void *) client;
}

int Connect_RILD(void *data)
{
	struct srs_client *client = (struct srs_client *) data;
	int t = 0;
	int fd = -1;
	int rc;

	LOGE("%s", __func__);

	if(client == NULL)
		return RIL_CLIENT_ERR_INVAL;

socket_connect:
	while(t < 5) {
		fd = socket_local_client(SRS_SOCKET_NAME, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM);

		if(fd > 0)
			break;

		LOGE("Socket creation to RIL failed: trying another time");

		t++;
		usleep(300);
	}

	if(fd < 0) {
		LOGE("Socket creation to RIL failed too many times");
		return RIL_CLIENT_ERR_CONNECT;
	}

	client->fd = fd;

	LOGE("Socket creation done, sending ping");

	rc = srs_ping(client);

	if(rc < 0) {
		LOGE("Ping failed!");
		goto socket_connect;
	} else {
		LOGD("Ping went alright");
	}

	return RIL_CLIENT_ERR_SUCCESS;
}

int Disconnect_RILD(void *data)
{
	struct srs_client *client = (struct srs_client *) data;

	LOGE("%s", __func__);

	if(client == NULL)
		return RIL_CLIENT_ERR_INVAL;

	if(client->fd > 0)
		close(client->fd);

	return RIL_CLIENT_ERR_SUCCESS;
}

int CloseClient_RILD(void *data)
{
	struct srs_client *client = (struct srs_client *) data;

	LOGE("%s", __func__);

	if(client == NULL)
		return RIL_CLIENT_ERR_INVAL;

	free(client);

	return RIL_CLIENT_ERR_SUCCESS;
}

int isConnected_RILD(void *data)
{
	struct srs_client *client = (struct srs_client *) data;
	int rc;

	LOGE("%s", __func__);

	if(client == NULL)
		return RIL_CLIENT_ERR_INVAL;

	if(client->fd < 0)
		return 0;

	rc = srs_ping(client);

	if(rc < 0) {
		LOGE("Ping failed!");
		close(client->fd);

		return 0;
	} else {
		LOGD("Ping went alright");
	}

	return 1;
}

int SetCallVolume(void *data, enum ril_sound_type type, int vol_level)
{
	struct srs_client *client = (struct srs_client *) data;
	struct srs_snd_call_volume call_volume;

	LOGD("Asking call volume");

	if(client == NULL)
		return RIL_CLIENT_ERR_INVAL;

	call_volume.type = (enum srs_snd_type) type;
	call_volume.volume = vol_level;	

	srs_send(client, SRS_SND_SET_CALL_VOLUME, (void *) &call_volume, sizeof(call_volume));

	return RIL_CLIENT_ERR_SUCCESS;
}


int SetCallAudioPath(void *data, enum ril_audio_path path)
{
	struct srs_client *client = (struct srs_client *) data;

	LOGD("Asking audio path");

	if(client == NULL)
		return RIL_CLIENT_ERR_INVAL;

	srs_send(client, SRS_SND_SET_CALL_AUDIO_PATH, (void *) &path, sizeof(enum srs_snd_path));

	return RIL_CLIENT_ERR_SUCCESS;
}

int SetCallClockSync(void *data, enum ril_clock_state condition)
{
	struct srs_client *client = (struct srs_client *) data;
	unsigned char clock_data = condition;

	LOGD("Asking clock sync");

	if(client == NULL)
		return RIL_CLIENT_ERR_INVAL;

	srs_send(client, SRS_SND_SET_CALL_CLOCK_SYNC, &clock_data, sizeof(data));

	return RIL_CLIENT_ERR_SUCCESS;
}

int RegisterUnsolicitedHandler(void *data, int command, void *callback)
{
	LOGD("Unsolicited handler isn't implemented yet!");

	return RIL_CLIENT_ERR_SUCCESS;
}