aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/device/h1/h1_ipc.c
blob: 8363449bca6cee2952526fa063d57f18740c1121 (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
/**
 * This file is part of libsamsung-ipc.
 *
 * Copyright (C) 2010-2011 Joerie de Gram <j.de.gram@gmail.com>
 *
 * 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 <termios.h>
#include <unistd.h>
#include <fcntl.h>

#include "ipc_private.h"
#include "h1_ipc.h"

int h1_ipc_open(void *io_data)
{
    struct termios termios;

    int fd = -1;

    if(io_data == NULL)
        return -1;

    fd = *((int *) io_data);

    fd = open(DPRAM_TTY, O_RDWR);

    if(fd < 0) {
        return 1;
    }

    tcgetattr(fd, &termios);
    cfmakeraw(&termios);
    tcsetattr(fd, TCSANOW, &termios);

    return 0;
}

int h1_ipc_close(void *io_data)
{
    int fd = -1;

    if(io_data == NULL)
        return -1;

    fd = *((int *) io_data);

    if(fd) {
        return close(fd);
    }

    return 0;
}

int h1_ipc_power_on(void *io_data)
{
    int fd = -1;

    if(io_data == NULL)
        return -1;

    fd = *((int *) io_data);

    ioctl(fd, IOCTL_PHONE_ON);

    return 0;
}

int h1_ipc_power_off(void *io_data)
{
    int fd = -1;

    if(io_data == NULL)
        return -1;

    fd = *((int *) io_data);

    ioctl(fd, IOCTL_PHONE_OFF);

    return 0;
}

int h1_ipc_send(struct ipc_client *client, struct ipc_message_info *request)
{
    struct hdlc_header *hdlc;
    unsigned char *frame;
    unsigned char *payload;
    int frame_length;

    /* Frame length: HDLC/IPC header + payload length + HDLC flags (2) */
    frame_length = (sizeof(*hdlc) + request->length + 2);

    frame = (unsigned char*)malloc(frame_length);
    frame[0] = FRAME_START;
    frame[frame_length-1] = FRAME_END;

    /* Setup HDLC header */
    hdlc = (struct hdlc_header*)(frame + 1);

    hdlc->length = (sizeof(*hdlc) + request->length);
    hdlc->unknown = 0;

    /* IPC header */
    hdlc->ipc.length = (sizeof(hdlc->ipc) + request->length);
    hdlc->ipc.mseq = request->mseq;
    hdlc->ipc.aseq = request->aseq;
    hdlc->ipc.group = request->group;
    hdlc->ipc.index = request->index;
    hdlc->ipc.type = request->type;

    /* IPC payload */
    payload = (frame + 1 + sizeof(*hdlc));
    memcpy(payload, request->data, request->length);

    ipc_client_log(client, "sending %s %s\n",
            ipc_command_type_to_str(IPC_COMMAND(request)),
            ipc_response_type_to_str(request->type));

    hex_dump(frame, frame_length);

    client->handlers->write(frame, frame_length, client->handlers->io_data);

    free(frame);

    return 0;
}

int h1_ipc_recv(struct ipc_client *client, struct ipc_message_info *response)
{
    unsigned char buf[4];
    unsigned char *data;
    unsigned short *frame_length;
    struct ipc_header *ipc;
    int num_read;
    int left;

    num_read = client->handlers->read((void*)buf, sizeof(buf), client->handlers->io_data);

    if(num_read == sizeof(buf) && *buf == FRAME_START) {
        frame_length = (unsigned short*)&buf[1];
        left = (*frame_length - 3 + 1);

        data = (unsigned char*)malloc(left);
        num_read = client->handlers->read((void*)data, left, client->handlers->io_data);

        if(num_read == left && data[left-1] == FRAME_END) {
            ipc = (struct ipc_header*)data;
            response->mseq = ipc->mseq;
            response->aseq = ipc->aseq;
            response->group = ipc->group;
            response->index = ipc->index;
            response->type = ipc->type;
            response->length = (ipc->length - sizeof(*ipc));

            response->data = (unsigned char*)malloc(response->length);
            memcpy(response->data, (data + sizeof(*ipc)), response->length);

            ipc_client_log(client, "received %s %s\n",
                    ipc_command_type_to_str(IPC_COMMAND(response)),
                    ipc_response_type_to_str(response->type));

            hex_dump(data, num_read-1);

            return 0;
        }
    }

    return 0;
}

int h1_ipc_read(void *data, unsigned int size, void *io_data)
{
    int fd = -1;

    if(io_data == NULL)
        return -1;

    fd = *((int *) io_data);

    if(fd < 0)
        return -1;

    return read(fd, data, size);
}

int h1_ipc_write(void *data, unsigned int size, void *io_data)
{
    int fd = -1;

    if(io_data == NULL)
        return -1;

    fd = *((int *) io_data);

    if(fd < 0)
        return -1;

    return write(fd, data, size);
}

void *h1_ipc_common_data_create(void)
{
    void *io_data;
    int io_data_len;

    io_data_len = sizeof(int);
    io_data = malloc(io_data_len);

    if(io_data == NULL)
        return NULL;

    memset(io_data, 0, io_data_len);

    return io_data;
}

int h1_ipc_common_data_destroy(void *io_data)
{
    // This was already done, not an error but we need to return
    if(io_data == NULL)
        return 0;

    free(io_data);

    return 0;
}

int h1_ipc_common_data_set_fd(void *io_data, int fd)
{
    int *common_data;

    if(io_data == NULL)
        return -1;

    common_data = (int *) io_data;
    common_data = &fd;

    return 0;
}

int h1_ipc_common_data_get_fd(void *io_data)
{
    int *common_data;

    if(io_data == NULL)
        return -1;

    common_data = (int *) io_data;

    return (int) *(common_data);
}

struct ipc_handlers ipc_default_handlers = {
    .open = h1_ipc_open,
    .close = h1_ipc_close,
    .power_on = h1_ipc_power_on,
    .power_off = h1_ipc_power_off,
    .read = h1_ipc_read,
    .write = h1_ipc_write,
    .common_data = NULL,
    .common_data_create = h1_ipc_common_data_create,
    .common_data_destroy = h1_ipc_common_data_destroy,
    .common_data_set_fd = h1_ipc_common_data_set_fd,
    .common_data_get_fd = h1_ipc_common_data_get_fd,
};

struct ipc_ops ipc_fmt_ops = {
    .send = h1_ipc_send,
    .recv = h1_ipc_recv,
    .bootstrap = NULL,
};

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