aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/ipc.c
blob: 68e99e01ec3a16132fd6f91e07e2095d852d395a (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
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
476
477
478
479
480
/*
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
 * Copyright (C) 2011 Simon Busch <morphis@gravedo.de>
 * Copyright (C) 2013-2014 Paul Kocialkowsk <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 <stdlib.h>
#include <stdio.h>
#include <string.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 <sys/utsname.h>

#include <samsung-ipc.h>

#include <ipc.h>
#include <ipc_devices.h>

int ipc_device_detect(void)
{
    char buffer[4096] = { 0 };
    struct utsname utsname;
    char *board_name = NULL;
    char *kernel_version = NULL;
    char *name = NULL;
    char *line, *p, *c;
    int index = -1;
    int fd = -1;
    size_t length;
    int rc;
    int i;

#ifdef IPC_DEVICE_NAME
    name = strdup(IPC_DEVICE_NAME);
#endif

#ifdef IPC_DEVICE_BOARD_NAME
    board_name = strdup(IPC_DEVICE_BOARD_NAME);
#else
    // Read board name from cpuinfo

    fd = open("/proc/cpuinfo", O_RDONLY);
    if (fd < 0)
        goto error;

    length = read(fd, &buffer, sizeof(buffer));

    close(fd);
    fd = -1;

    line = strtok(buffer, "\n");
    while (line != NULL) {
        if (strncmp(line, "Hardware", 9) == 9) {
            p = line + 11;
            c = p;

            while (*c != '\n' && *c != '\0') {
                *c = tolower(*c);
                c++;
            }

            *c = '\0';

            board_name = strdup(p);
            break;
        }

        line = strtok(NULL, "\n");
    }
#endif

#ifdef IPC_DEVICE_KERNEL_VERSION
    kernel_version = strdup(IPC_DEVICE_KERNEL_VERSION);
#else
    memset(&utsname, 0, sizeof(utsname));

    uname(&utsname);

    kernel_version = strdup(utsname.release);
#endif

    for (i = 0; i < (int) ipc_devices_count; i++) {
        // Eliminate index if the name doesn't match
        if (name != NULL && ipc_devices[i].name != NULL && strcmp(name, ipc_devices[i].name) != 0)
            continue;

        // Eliminate index if the board name doesn't match
        if (board_name != NULL && ipc_devices[i].board_name != NULL && strcmp(board_name, ipc_devices[i].board_name) != 0)
            continue;

        // Keep index but don't break yet since we may have a better match with kernel version
        index = i;

        if (kernel_version == NULL || ipc_devices[i].kernel_version == NULL)
            continue;

        if (kernel_version != NULL && ipc_devices[i].kernel_version != NULL && strcmp(kernel_version, ipc_devices[i].kernel_version) != 0)
            continue;

        // Everything matches this particular index
        break;
    }

    goto complete;

error:
    index = -1;

complete:
    if (board_name != NULL)
        free(board_name);

    if (kernel_version != NULL)
        free(kernel_version);

    if (fd >= 0)
        close(fd);

    return index;
}

struct ipc_client *ipc_client_create(int type)
{
    struct ipc_client *client = NULL;
    unsigned int device_index;
    int rc;

    if (type < 0 || type > IPC_CLIENT_TYPE_RFS)
        return NULL;

    rc = ipc_device_detect();
    if (rc < 0)
        goto error;

    device_index = (unsigned int) rc;
    if (device_index > ipc_devices_count)
        goto error;

    client = (struct ipc_client *) calloc(1, sizeof(struct ipc_client));
    client->type = type;

    switch (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;
        default:
            goto error;
    }

    client->gprs_specs = ipc_devices[device_index].gprs_specs;
    client->nv_data_specs = ipc_devices[device_index].nv_data_specs;

    // Handlers can be modified
    client->handlers = (struct ipc_client_handlers *) calloc(1, sizeof(struct ipc_client_handlers));

    if (ipc_devices[device_index].handlers != NULL)
        memcpy(client->handlers, ipc_devices[device_index].handlers, sizeof(struct ipc_client_handlers));

    goto complete;

error:
    if (client != NULL) {
        free(client);
        client = NULL;
    }

complete:
    return client;
}

int ipc_client_destroy(struct ipc_client *client)
{
    if (client == NULL)
        return -1;

    if (client->handlers != NULL)
        free(client->handlers);

    memset(client, 0, sizeof(struct ipc_client));
    free(client);

    return 0;
}

int ipc_client_transport_handlers_register(struct ipc_client *client,
    int (*open)(void *transport_data, int type),
    int (*close)(void *transport_data),
    int (*read)(void *transport_data, void *data, size_t size),
    int (*write)(void *transport_data, const void *data, size_t size),
    int (*poll)(void *transport_data, struct timeval *timeout),
    void *transport_data)
{
    if (client == NULL || client->handlers == NULL)
        return -1;

    if (read != NULL)
        client->handlers->read = read;
    if (write != NULL)
        client->handlers->write = write;
    if (poll != NULL)
        client->handlers->poll = poll;
    if (open != NULL)
        client->handlers->open = open;
    if (close != NULL)
        client->handlers->close = close;
    if (transport_data != NULL)
        client->handlers->transport_data = transport_data;

    return 0;
}

int ipc_client_power_handlers_register(struct ipc_client *client,
    int (*power_on)(void *power_data), int (*power_off)(void *power_data),
    void *power_data)
{
    if (client == NULL || client->handlers == NULL)
        return -1;

    if (power_on != NULL)
        client->handlers->power_on = power_on;
    if (power_off != NULL)
        client->handlers->power_off = power_off;
    if (power_data != NULL)
        client->handlers->power_data = power_data;

    return 0;
}

int ipc_client_gprs_handlers_register(struct ipc_client *client,
    int (*gprs_activate)(void *gprs_data, int cid),
    int (*gprs_deactivate)(void *gprs_data, int cid), void *gprs_data)
{
    if (client == NULL || client->handlers == NULL)
        return -1;

    if (gprs_activate != NULL)
        client->handlers->gprs_activate = gprs_activate;
    if (gprs_deactivate != NULL)
        client->handlers->gprs_deactivate = gprs_deactivate;
    if (gprs_data != NULL)
        client->handlers->gprs_data = gprs_data;

    return 0;
}

void ipc_client_log(struct ipc_client *client, const char *message, ...)
{
    char buffer[4096];
    va_list args;

    if (client == NULL || client->log_callback == NULL || message == NULL)
        return;

    va_start(args, message);
    vsnprintf((char *) &buffer, sizeof(buffer), message, args);
    client->log_callback(client->log_data, buffer);
    va_end(args);
}

int ipc_client_log_callback_register(struct ipc_client *client,
    void (*log_callback)(void *log_data, const char *message), void *log_data)
{
    if (client == NULL)
        return -1;

    client->log_callback = log_callback;
    client->log_data = log_data;

    return 0;
}

int ipc_client_boot(struct ipc_client *client)
{
    if (client == NULL || client->ops == NULL || client->ops->boot == NULL)
        return -1;

    return client->ops->boot(client);
}

int ipc_client_send(struct ipc_client *client, unsigned char mseq,
    unsigned short command, unsigned char type, const void *data, size_t size)
{
    struct ipc_message message;

    if (client == NULL || client->ops == NULL || client->ops->send == NULL)
        return -1;

    memset(&message, 0, sizeof(message));
    message.mseq = mseq;
    message.aseq = 0xff;
    message.command = command;
    message.type = type;
    message.data = (void *) data;
    message.size = size;

    return client->ops->send(client, &message);
}

int ipc_client_recv(struct ipc_client *client, struct ipc_message *message)
{
    if (client == NULL || client->ops == NULL || client->ops->recv == NULL || message == NULL)
        return -1;

    return client->ops->recv(client, message);
}

int ipc_client_open(struct ipc_client *client)
{
    if (client == NULL || client->handlers == NULL || client->handlers->open == NULL)
        return -1;

    return client->handlers->open(client->handlers->transport_data, client->type);
}

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->transport_data);
}

int ipc_client_poll(struct ipc_client *client, struct timeval *timeout)
{
    if (client == NULL || client->handlers == NULL || client->handlers->poll == NULL)
        return -1;

    return client->handlers->poll(client->handlers->transport_data, timeout);
}

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_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_data);
}

int ipc_client_gprs_activate(struct ipc_client *client, int cid)
{
    if (client == NULL || client->handlers == NULL || client->handlers->gprs_activate == NULL)
        return -1;

    return client->handlers->gprs_activate(client->handlers->gprs_data, cid);
}

int ipc_client_gprs_deactivate(struct ipc_client *client, int cid)
{
    if (client == NULL || client->handlers == NULL || client->handlers->gprs_deactivate == NULL)
        return -1;

    return client->handlers->gprs_deactivate(client->handlers->gprs_data, cid);
}

int ipc_client_data_create(struct ipc_client *client)
{
    if (client == NULL || client->handlers == NULL || client->handlers->data_create == NULL)
        return -1;

    return client->handlers->data_create(&client->handlers->transport_data, &client->handlers->power_data, &client->handlers->power_data);
}

int ipc_client_data_destroy(struct ipc_client *client)
{
    if (client == NULL || client->handlers == NULL || client->handlers->data_destroy == NULL)
        return -1;

    return client->handlers->data_destroy(client->handlers->transport_data, client->handlers->power_data, client->handlers->power_data);
}

char *ipc_client_gprs_get_iface(struct ipc_client *client, int cid)
{
    if (client == NULL || client->gprs_specs == NULL || client->gprs_specs->gprs_get_iface == NULL)
        return NULL;

    return client->gprs_specs->gprs_get_iface(cid);
}

int ipc_client_gprs_get_capabilities(struct ipc_client *client,
    struct ipc_client_gprs_capabilities *capabilities)
{
    if (client == NULL || client->gprs_specs == NULL || client->gprs_specs->gprs_get_capabilities == NULL)
        return -1;

    return client->gprs_specs->gprs_get_capabilities(capabilities);
}

char *ipc_client_nv_data_path(struct ipc_client *client)
{
    if (client == NULL || client->nv_data_specs == NULL || client->nv_data_specs->nv_data_path == NULL)
        return NULL;

    return client->nv_data_specs->nv_data_path;
}

char *ipc_client_nv_data_md5_path(struct ipc_client *client)
{
    if (client == NULL || client->nv_data_specs == NULL || client->nv_data_specs->nv_data_md5_path == NULL)
        return NULL;

    return client->nv_data_specs->nv_data_md5_path;
}

char *ipc_client_nv_data_backup_path(struct ipc_client *client)
{
    if (client == NULL || client->nv_data_specs == NULL || client->nv_data_specs->nv_data_backup_path == NULL)
        return NULL;

    return client->nv_data_specs->nv_data_backup_path;
}

char *ipc_client_nv_data_backup_md5_path(struct ipc_client *client)
{
    if (client == NULL || client->nv_data_specs == NULL || client->nv_data_specs->nv_data_backup_md5_path == NULL)
        return NULL;

    return client->nv_data_specs->nv_data_backup_md5_path;
}

char *ipc_client_nv_data_secret(struct ipc_client *client)
{
    if (client == NULL || client->nv_data_specs == NULL || client->nv_data_specs->nv_data_secret == NULL)
        return NULL;

    return client->nv_data_specs->nv_data_secret;
}

size_t ipc_client_nv_data_size(struct ipc_client *client)
{
    if (client == NULL || client->nv_data_specs == NULL || client->nv_data_specs->nv_data_size == 0)
        return 0;

    return client->nv_data_specs->nv_data_size;
}

size_t ipc_client_nv_data_chunk_size(struct ipc_client *client)
{
    if (client == NULL || client->nv_data_specs == NULL || client->nv_data_specs->nv_data_chunk_size == 0)
        return 0;

    return client->nv_data_specs->nv_data_chunk_size;
}

// vim:ts=4:sw=4:expandtab