aboutsummaryrefslogtreecommitdiffstats
path: root/samsung-ipc/ipc.c
blob: 3054b986ba1ee19696aa42d19cef839e381678b0 (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
/**
 * 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 <stdarg.h>

#include <radio.h>

#include "ipc_private.h"

extern struct ipc_ops crespo_ipc_ops;
// extern struct ipc_ops h1_ipc_ops;


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

struct ipc_client* ipc_client_new(int client_type)
{
    struct ipc_client *client;
    struct ipc_ops *ops = NULL;

    switch (client_type)
    {
        case IPC_CLIENT_TYPE_CRESPO_FMT:
        case IPC_CLIENT_TYPE_CRESPO_RFS:
            ops = &crespo_ipc_ops;
            break;
        case IPC_CLIENT_TYPE_H1:
            // ops = &h1_ipc_ops;
            break;
        default:
            break;
    }

    client = (struct ipc_client*) malloc(sizeof(struct ipc_client));
    client->type = client_type;
    client->ops = ops;
    client->log_handler = log_handler_default;

    return client;
}

int ipc_client_free(struct ipc_client *client)
{
    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_delegates(struct ipc_client *client,
                             ipc_client_transport_cb write, void *write_data,
                             ipc_client_transport_cb read, void *read_data)
{
    if (client == NULL)
        return -1;

    client->read = read;
    client->read_data = read_data;
    client->write = write;
    client->write_data = write_data;

    return 0;
}

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)
{
    if (client == NULL ||
        client->ops == NULL ||
        client->ops->open == NULL)
        return -1;

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

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

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

int _ipc_client_send(struct ipc_client *client, struct ipc_request *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 aseq)
{
    ipc_client_send(client, command, IPC_TYPE_GET, 0, 0, aseq);
}

inline void ipc_client_send_exec(struct ipc_client *client, const unsigned short command, unsigned char aseq)
{
    ipc_client_send(client, command, IPC_TYPE_EXEC, 0, 0, aseq);
}

/* 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_request 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_response *response)
{
    if (client == NULL ||
        client->ops == NULL ||
        client->ops->recv == NULL)
        return -1;

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