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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
/**
* This file is part of libsamsung-ipc.
*
* Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
* 2011 Simon Busch <morphis@gravedo.de>
*
* 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 3 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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdbool.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <asm/types.h>
#include <radio.h>
#include "ipc_private.h"
#include "ipc_devices.h"
void log_handler_default(const char *message, void *user_data)
{
printf("%s\n", message);
}
void ipc_client_log(struct ipc_client *client, const char *message, ...)
{
assert(client->log_handler != NULL);
va_list args;
char buffer[4096];
va_start(args, message);
vsprintf(buffer, message, args);
client->log_handler(buffer, client->log_data);
va_end(args);
}
int ipc_device_detect(void)
{
int index = -1;
int i;
#ifdef IPC_DEVICE_EXPLICIT
for (i=0 ; i < ipc_devices_count ; i++)
{
if (strcmp(IPC_DEVICE_EXPLICIT, ipc_devices[i].name) == 0)
{
index = i;
break;
}
}
#else
char buf[4096];
// gather device type from /proc/cpuinfo
int fd = open("/proc/cpuinfo", O_RDONLY);
int bytesread = read(fd, buf, 4096);
close(fd);
// match hardware name with our supported devices
char *pch = strtok(buf, "\n");
while (pch != NULL)
{
int rc;
if ((rc = strncmp(pch, "Hardware", 9)) == 9)
{
char *str = (void *) (pch + 9);
int len = strlen(str);
char tmp;
for (i=0 ; i < len ; i++)
{
tmp = (char) tolower(str[i]);
str[i] = tmp;
}
for (i=0 ; i < ipc_devices_count ; i++)
{
if (strstr(pch, ipc_devices[i].board_name) != NULL)
{
index = i;
break;
}
}
}
pch = strtok(NULL, "\n");
}
#endif
return index;
}
struct ipc_client* ipc_client_new(int client_type)
{
struct ipc_client *client;
int device_index = -1;
device_index = ipc_device_detect();
if (device_index < 0 || device_index > ipc_devices_count)
return NULL;
if (client_type < 0 || client_type > IPC_CLIENT_TYPE_RFS)
return NULL;
client = (struct ipc_client*) malloc(sizeof(struct ipc_client));
client->type = client_type;
switch (client_type)
{
case IPC_CLIENT_TYPE_RFS:
client->ops = ipc_devices[device_index].rfs_ops;
break;
case IPC_CLIENT_TYPE_FMT:
client->ops = ipc_devices[device_index].fmt_ops;
break;
}
client->handlers = (struct ipc_handlers *) malloc(sizeof(struct ipc_handlers));
client->gprs_specs = ipc_devices[device_index].gprs_specs;
client->nv_data_specs = ipc_devices[device_index].nv_data_specs;
client->log_handler = log_handler_default;
if (ipc_devices[device_index].handlers != 0)
memcpy(client->handlers, ipc_devices[device_index].handlers, sizeof(struct ipc_handlers));
return client;
}
int ipc_client_free(struct ipc_client *client)
{
free(client->handlers);
free(client);
client = NULL;
return 0;
}
int ipc_client_set_log_handler(struct ipc_client *client, ipc_client_log_handler_cb log_handler_cb, void *user_data)
{
if (client == NULL)
return -1;
client->log_handler = log_handler_cb;
client->log_data = user_data;
return 0;
}
int ipc_client_set_handlers(struct ipc_client *client, struct ipc_handlers *handlers)
{
if (client == NULL ||
handlers == NULL)
return -1;
memcpy(client->handlers, handlers, sizeof(struct ipc_handlers));
return 0;
}
int ipc_client_set_io_handlers(struct ipc_client *client,
ipc_io_handler_cb read, void *read_data,
ipc_io_handler_cb write, void *write_data)
{
if (client == NULL ||
client->handlers == NULL)
return -1;
if (read != NULL)
client->handlers->read = read;
if (read_data != NULL)
client->handlers->read_data = read_data;
if (write != NULL)
client->handlers->write = write;
if (write_data != NULL)
client->handlers->write_data = write_data;
return 0;
}
int ipc_client_set_handlers_common_data(struct ipc_client *client, void *data)
{
void *common_data;
if (client == NULL ||
client->handlers == NULL ||
data == NULL)
return -1;
common_data = data;
client->handlers->common_data = common_data;
client->handlers->read_data = common_data;
client->handlers->write_data = common_data;
client->handlers->open_data = common_data;
client->handlers->close_data = common_data;
client->handlers->power_on_data = common_data;
client->handlers->power_off_data = common_data;
client->handlers->gprs_activate_data = common_data;
client->handlers->gprs_deactivate_data = common_data;
return 0;
}
void *ipc_client_get_handlers_common_data(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL)
return NULL;
return client->handlers->common_data;
}
int ipc_client_create_handlers_common_data(struct ipc_client *client)
{
void *common_data;
if (client == NULL ||
client->handlers == NULL)
return -1;
common_data = client->handlers->common_data_create();
client->handlers->common_data = common_data;
client->handlers->read_data = common_data;
client->handlers->write_data = common_data;
client->handlers->open_data = common_data;
client->handlers->close_data = common_data;
client->handlers->power_on_data = common_data;
client->handlers->power_off_data = common_data;
client->handlers->gprs_activate_data = common_data;
client->handlers->gprs_deactivate_data = common_data;
return 0;
}
int ipc_client_destroy_handlers_common_data(struct ipc_client *client)
{
void *common_data;
int rc;
if (client == NULL ||
client->handlers == NULL ||
client->handlers->common_data_destroy == NULL)
return -1;
rc = client->handlers->common_data_destroy(client->handlers->common_data);
if (rc < 0)
return -1;
common_data = NULL;
client->handlers->common_data = common_data;
client->handlers->read_data = common_data;
client->handlers->write_data = common_data;
client->handlers->open_data = common_data;
client->handlers->close_data = common_data;
client->handlers->power_on_data = common_data;
client->handlers->power_off_data = common_data;
client->handlers->gprs_activate_data = common_data;
client->handlers->gprs_deactivate_data = common_data;
return 0;
}
int ipc_client_set_handlers_common_data_fd(struct ipc_client *client, int fd)
{
if (client == NULL ||
client->handlers == NULL ||
client->handlers->common_data_set_fd == NULL)
return -1;
return client->handlers->common_data_set_fd(client->handlers->common_data, fd);
}
int ipc_client_get_handlers_common_data_fd(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL ||
client->handlers->common_data_get_fd == NULL)
return -1;
return client->handlers->common_data_get_fd(client->handlers->common_data);
}
int ipc_client_bootstrap_modem(struct ipc_client *client)
{
if (client == NULL ||
client->ops == NULL ||
client->ops->bootstrap == NULL)
return -1;
return client->ops->bootstrap(client);
}
int ipc_client_open(struct ipc_client *client)
{
int type;
int fd;
if (client == NULL ||
client->handlers == NULL ||
client->handlers->open == NULL)
return -1;
type = client->type;
return client->handlers->open(type, client->handlers->open_data);
}
int ipc_client_close(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL ||
client->handlers->close == NULL)
return -1;
return client->handlers->close(client->handlers->close_data);
}
int ipc_client_power_on(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL ||
client->handlers->power_on == NULL)
return -1;
return client->handlers->power_on(client->handlers->power_on_data);
}
int ipc_client_power_off(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL ||
client->handlers->power_off == NULL)
return -1;
return client->handlers->power_off(client->handlers->power_off_data);
}
int ipc_client_gprs_handlers_available(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL)
return -1;
if(client->handlers->gprs_activate != NULL &&
client->handlers->gprs_deactivate != NULL)
return 1;
else
return 0;
}
int ipc_client_gprs_activate(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL ||
client->handlers->gprs_activate == NULL)
return -1;
return client->handlers->gprs_activate(client->handlers->gprs_activate_data);
}
int ipc_client_gprs_deactivate(struct ipc_client *client)
{
if (client == NULL ||
client->handlers == NULL ||
client->handlers->gprs_deactivate == NULL)
return -1;
return client->handlers->gprs_deactivate(client->handlers->gprs_deactivate_data);
}
int ipc_client_gprs_get_iface(struct ipc_client *client, char **iface, int cid)
{
if (client == NULL ||
client->gprs_specs == NULL ||
client->gprs_specs->gprs_get_iface == NULL)
return -1;
return client->gprs_specs->gprs_get_iface(iface, cid);
}
int ipc_client_gprs_get_capabilities(struct ipc_client *client, struct ipc_client_gprs_capabilities *cap)
{
if (client == NULL ||
client->gprs_specs == NULL ||
client->gprs_specs->gprs_get_capabilities == NULL)
return -1;
return client->gprs_specs->gprs_get_capabilities(cap);
}
int _ipc_client_send(struct ipc_client *client, struct ipc_message_info *request)
{
if (client == NULL ||
client->ops == NULL ||
client->ops->send == NULL)
return -1;
return client->ops->send(client, request);
}
/* Convenience functions for ipc_send */
inline void ipc_client_send_get(struct ipc_client *client, const unsigned short command, unsigned char mseq)
{
ipc_client_send(client, command, IPC_TYPE_GET, 0, 0, mseq);
}
inline void ipc_client_send_exec(struct ipc_client *client, const unsigned short command, unsigned char mseq)
{
ipc_client_send(client, command, IPC_TYPE_EXEC, 0, 0, mseq);
}
/* Wrapper for ipc_send */
void ipc_client_send(struct ipc_client *client, const unsigned short command, const char type, unsigned char *data, const int length, unsigned char mseq)
{
struct ipc_message_info request;
request.mseq = mseq;
request.aseq = 0xff;
request.group = IPC_GROUP(command);
request.index = IPC_INDEX(command);
request.type = type;
request.length = length;
request.data = data;
_ipc_client_send(client, &request);
}
int ipc_client_recv(struct ipc_client *client, struct ipc_message_info *response)
{
if (client == NULL ||
client->ops == NULL ||
client->ops->recv == NULL)
return -1;
return client->ops->recv(client, response);
}
void ipc_client_response_free(struct ipc_client *client, struct ipc_message_info *response)
{
if (response->data != NULL) {
free(response->data);
response->data = NULL;
}
}
// vim:ts=4:sw=4:expandtab
|