test_connman.c 4.56 KB
Newer Older
Slava Monich's avatar
Slava Monich committed
1
/*
2
 * Copyright (C) 2013-2017 Jolla Ltd.
3
 * Contact: Slava Monich <slava.monich@jolla.com>
Slava Monich's avatar
Slava Monich committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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.
 */

#include "test_connman.h"
#include "test_connection.h"

/* Logging */
19 20 21
#define GLOG_MODULE_NAME mms_connman_log
#include <gutil_log.h>
GLOG_MODULE_DEFINE("mms-connman-test");
Slava Monich's avatar
Slava Monich committed
22 23 24 25

typedef MMSConnManClass MMSConnManTestClass;
typedef struct mms_connman_test {
    MMSConnMan cm;
Slava Monich's avatar
Slava Monich committed
26
    MMSConnection* conn;
Slava Monich's avatar
Slava Monich committed
27
    unsigned short port;
28
    char* proxy;
29
    char* default_imsi;
30
    gboolean offline;
Slava Monich's avatar
Slava Monich committed
31 32
    mms_connman_test_connect_fn connect_fn;
    void* connect_param;
Slava Monich's avatar
Slava Monich committed
33 34
} MMSConnManTest;

35
G_DEFINE_TYPE(MMSConnManTest, mms_connman_test, MMS_TYPE_CONNMAN)
Slava Monich's avatar
Slava Monich committed
36 37 38 39
#define MMS_TYPE_CONNMAN_TEST (mms_connman_test_get_type())
#define MMS_CONNMAN_TEST(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
    MMS_TYPE_CONNMAN_TEST, MMSConnManTest))

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
static
gboolean
mms_connman_test_busy_cb(
    gpointer data)
{
    MMSConnManTest* test = MMS_CONNMAN_TEST(data);
    mms_connman_busy_dec(&test->cm);
    mms_connman_ref(&test->cm);
    return G_SOURCE_REMOVE;
}

static
void
mms_connman_test_make_busy(
    MMSConnManTest* test)
{
    mms_connman_ref(&test->cm);
    mms_connman_busy_inc(&test->cm);
    g_idle_add(mms_connman_test_busy_cb, test);
}

61 62 63 64 65 66 67 68 69 70 71
void
mms_connman_test_set_proxy(
    MMSConnMan* cm,
    const char* proxy,
    unsigned short port)
{
    MMSConnManTest* test = MMS_CONNMAN_TEST(cm);
    test->proxy = g_strdup(proxy);
    test->port = port;
}

Slava Monich's avatar
Slava Monich committed
72 73 74
void
mms_connman_test_set_port(
    MMSConnMan* cm,
Slava Monich's avatar
Slava Monich committed
75 76
    unsigned short port,
    gboolean proxy)
Slava Monich's avatar
Slava Monich committed
77
{
78
    return mms_connman_test_set_proxy(cm, proxy ? "127.0.0.1" : NULL, port);
Slava Monich's avatar
Slava Monich committed
79 80
}

81 82 83 84 85 86 87 88
void
mms_connman_test_set_offline(
    MMSConnMan* cm,
    gboolean offline)
{
    MMS_CONNMAN_TEST(cm)->offline = offline;
}

89 90 91 92 93 94 95 96 97 98
void
mms_connman_test_set_default_imsi(
    MMSConnMan* cm,
    const char* imsi)
{
    MMSConnManTest* test = MMS_CONNMAN_TEST(cm);
    g_free(test->default_imsi);
    test->default_imsi = g_strdup(imsi);
}

Slava Monich's avatar
Slava Monich committed
99 100 101 102 103
void
mms_connman_test_close_connection(
    MMSConnMan* cm)
{
    MMSConnManTest* test = MMS_CONNMAN_TEST(cm);
Slava Monich's avatar
Slava Monich committed
104
    if (test->conn) {
105
        GDEBUG("Closing connection...");
106
        mms_connman_test_make_busy(test);
Slava Monich's avatar
Slava Monich committed
107 108 109
        mms_connection_close(test->conn);
        mms_connection_unref(test->conn);
        test->conn = NULL;
Slava Monich's avatar
Slava Monich committed
110 111 112
    }
}

Slava Monich's avatar
Slava Monich committed
113 114 115 116 117 118 119 120 121 122 123
void
mms_connman_test_set_connect_callback(
    MMSConnMan* cm,
    mms_connman_test_connect_fn fn,
    void* param)
{
    MMSConnManTest* test = MMS_CONNMAN_TEST(cm);
    test->connect_fn = fn;
    test->connect_param = param;
}

124 125 126 127 128 129 130 131
static
char*
mms_connman_test_default_imsi(
    MMSConnMan* cm)
{
    return g_strdup(MMS_CONNMAN_TEST(cm)->default_imsi);
}

Slava Monich's avatar
Slava Monich committed
132 133 134 135 136
static
MMSConnection*
mms_connman_test_open_connection(
    MMSConnMan* cm,
    const char* imsi,
137
    MMS_CONNECTION_TYPE type)
Slava Monich's avatar
Slava Monich committed
138 139 140
{
    MMSConnManTest* test = MMS_CONNMAN_TEST(cm);
    mms_connman_test_close_connection(cm);
141 142 143
    if (test->offline) {
        return NULL;
    } else {
144
        mms_connman_test_make_busy(test);
145
        test->conn = mms_connection_test_new(imsi, test->proxy, test->port);
Slava Monich's avatar
Slava Monich committed
146
        if (test->connect_fn) test->connect_fn(test->connect_param);
Slava Monich's avatar
Slava Monich committed
147
        return mms_connection_ref(test->conn);
Slava Monich's avatar
Slava Monich committed
148 149 150 151 152 153 154 155
    }
}

static
void
mms_connman_test_dispose(
    GObject* object)
{
156 157 158 159 160 161 162 163 164
    mms_connman_test_close_connection(&MMS_CONNMAN_TEST(object)->cm);
    G_OBJECT_CLASS(mms_connman_test_parent_class)->dispose(object);
}

static
void
mms_connman_test_finalize(
    GObject* object)
{
165 166 167
    MMSConnManTest* test = MMS_CONNMAN_TEST(object);
    g_free(test->proxy);
    g_free(test->default_imsi);
168
    G_OBJECT_CLASS(mms_connman_test_parent_class)->finalize(object);
Slava Monich's avatar
Slava Monich committed
169 170 171 172 173 174 175
}

static
void
mms_connman_test_class_init(
    MMSConnManTestClass* klass)
{
176
    klass->fn_default_imsi = mms_connman_test_default_imsi;
Slava Monich's avatar
Slava Monich committed
177 178
    klass->fn_open_connection = mms_connman_test_open_connection;
    G_OBJECT_CLASS(klass)->dispose = mms_connman_test_dispose;
179
    G_OBJECT_CLASS(klass)->finalize = mms_connman_test_finalize;
Slava Monich's avatar
Slava Monich committed
180 181 182 183 184
}

static
void
mms_connman_test_init(
185
    MMSConnManTest* test)
Slava Monich's avatar
Slava Monich committed
186
{
187
    test->default_imsi = g_strdup("Default");
Slava Monich's avatar
Slava Monich committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

MMSConnMan*
mms_connman_test_new()
{
    return g_object_new(MMS_TYPE_CONNMAN_TEST,NULL);
}

/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */