Loading...
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 | /*
*
* oFono - Open Source Telephony
*
* Copyright (C) 2016 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <ofono/types.h>
#include <gril.h>
#include "rilmodem-test-engine.h"
#define MAX_REQUEST_SIZE 4096
#define RIL_SERVER_SOCK_PATH "/tmp/unittestril"
static GMainLoop *mainloop;
struct engine_data {
int server_sk;
int connected_sk;
guint connection_watch;
rilmodem_test_engine_cb_t connect_func;
GIOChannel *server_io;
char *sock_name;
struct rilmodem_test_data rtd;
int step_i;
void *user_data;
};
static void send_parcel(struct engine_data *ed)
{
GIOStatus status;
gsize wbytes;
const struct rilmodem_test_step *step = &ed->rtd.steps[ed->step_i];
status = g_io_channel_write_chars(ed->server_io,
step->parcel_data,
step->parcel_size,
&wbytes, NULL);
g_assert(wbytes == step->parcel_size);
g_assert(status == G_IO_STATUS_NORMAL);
status = g_io_channel_flush(ed->server_io, NULL);
g_assert(status == G_IO_STATUS_NORMAL);
rilmodem_test_engine_next_step(ed);
}
static gboolean on_rx_data(GIOChannel *chan, GIOCondition cond, gpointer data)
{
struct engine_data *ed = data;
GIOStatus status;
gsize rbytes;
gchar *buf;
const struct rilmodem_test_step *step;
/* We have closed the socket */
if (cond == G_IO_NVAL)
return FALSE;
buf = g_malloc0(MAX_REQUEST_SIZE);
status = g_io_channel_read_chars(ed->server_io, buf, MAX_REQUEST_SIZE,
&rbytes, NULL);
g_assert(status == G_IO_STATUS_NORMAL);
/* Check this is the expected step */
step = &ed->rtd.steps[ed->step_i];
g_assert(step->type == TST_EVENT_RECEIVE);
g_assert(rbytes == step->parcel_size);
/* validate received parcel */
g_assert(!memcmp(buf, step->parcel_data, rbytes));
rilmodem_test_engine_next_step(ed);
return TRUE;
}
static gboolean on_socket_connected(GIOChannel *chan, GIOCondition cond,
gpointer data)
{
struct engine_data *ed = data;
struct sockaddr saddr;
unsigned int len = sizeof(saddr);
GIOStatus status;
g_assert(cond == G_IO_IN);
ed->connected_sk = accept(ed->server_sk, &saddr, &len);
g_assert(ed->connected_sk != -1);
ed->server_io = g_io_channel_unix_new(ed->connected_sk);
g_assert(ed->server_io != NULL);
status = g_io_channel_set_encoding(ed->server_io, NULL, NULL);
g_assert(status == G_IO_STATUS_NORMAL);
g_io_channel_set_buffered(ed->server_io, FALSE);
g_io_channel_set_close_on_unref(ed->server_io, TRUE);
if (ed->connect_func)
ed->connect_func(ed->user_data);
ed->connection_watch =
g_io_add_watch_full(ed->server_io, G_PRIORITY_DEFAULT,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
on_rx_data, ed, NULL);
g_io_channel_unref(ed->server_io);
return FALSE;
}
void rilmodem_test_engine_remove(struct engine_data *ed)
{
if (ed->connection_watch)
g_source_remove(ed->connection_watch);
g_assert(ed->server_sk);
close(ed->server_sk);
remove(ed->sock_name);
g_free(ed->sock_name);
g_free(ed);
}
struct engine_data *rilmodem_test_engine_create(
rilmodem_test_engine_cb_t connect,
const struct rilmodem_test_data *test_data,
void *data)
{
GIOChannel *io;
struct sockaddr_un addr;
int retval;
struct engine_data *ed;
ed = g_new0(struct engine_data, 1);
ed->connect_func = connect;
ed->user_data = data;
ed->rtd = *test_data;
ed->server_sk = socket(AF_UNIX, SOCK_STREAM, 0);
g_assert(ed->server_sk);
ed->sock_name =
g_strdup_printf(RIL_SERVER_SOCK_PATH"%u", (unsigned) getpid());
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
strncpy(addr.sun_path, ed->sock_name, sizeof(addr.sun_path) - 1);
/* Unlink any existing socket for this session */
unlink(addr.sun_path);
retval = bind(ed->server_sk, (struct sockaddr *) &addr, sizeof(addr));
g_assert(retval >= 0);
retval = listen(ed->server_sk, 0);
g_assert(retval >= 0);
io = g_io_channel_unix_new(ed->server_sk);
g_assert(io != NULL);
g_io_channel_set_close_on_unref(io, TRUE);
g_io_add_watch_full(io, G_PRIORITY_DEFAULT,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
on_socket_connected, ed, NULL);
g_io_channel_unref(io);
return ed;
}
void rilmodem_test_engine_write_socket(struct engine_data *ed,
const unsigned char *buf,
const size_t buf_len)
{
GIOStatus status;
gsize wbytes;
status = g_io_channel_write_chars(ed->server_io,
(const char *) buf,
buf_len,
&wbytes, NULL);
g_assert(status == G_IO_STATUS_NORMAL);
status = g_io_channel_flush(ed->server_io, NULL);
g_assert(status == G_IO_STATUS_NORMAL);
}
const char *rilmodem_test_engine_get_socket_name(struct engine_data *ed)
{
return ed->sock_name;
}
static gboolean action_call(gpointer data)
{
struct engine_data *ed = data;
const struct rilmodem_test_step *step;
step = &ed->rtd.steps[ed->step_i];
step->call_action(ed->user_data);
return FALSE;
}
void rilmodem_test_engine_next_step(struct engine_data *ed)
{
const struct rilmodem_test_step *step;
ed->step_i++;
if (ed->step_i >= ed->rtd.num_steps) {
/* Finish the test */
g_main_loop_quit(mainloop);
return;
}
step = &ed->rtd.steps[ed->step_i];
/* If next step is an action, execute it */
switch (step->type) {
case TST_ACTION_SEND:
send_parcel(ed);
break;
case TST_ACTION_CALL:
g_idle_add(action_call, ed);
break;
case TST_EVENT_RECEIVE:
case TST_EVENT_CALL:
break;
};
}
const struct rilmodem_test_step *rilmodem_test_engine_get_current_step(
struct engine_data *ed)
{
const struct rilmodem_test_step *step = &ed->rtd.steps[ed->step_i];
return step;
}
void rilmodem_test_engine_start(struct engine_data *ed)
{
mainloop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(mainloop);
g_main_loop_unref(mainloop);
}
|