mms_connman.c 3.52 KB
Newer Older
Slava Monich's avatar
Slava Monich committed
1
/*
2
 * Copyright (C) 2013-2016 Jolla Ltd.
Slava Monich's avatar
Slava Monich committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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 "mms_connman.h"

17 18 19 20
/* Logging */
#define MMS_LOG_MODULE_NAME mms_connman_log
#include "mms_lib_log.h"

Slava Monich's avatar
Slava Monich committed
21 22 23 24 25 26
G_DEFINE_TYPE(MMSConnMan, mms_connman, G_TYPE_OBJECT);
#define MMS_CONNMAN(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
    MMS_TYPE_CONNMAN, MMSConnMan))
#define MMS_CONNMAN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), \
    MMS_TYPE_CONNMAN, MMSConnManClass))

27 28 29 30 31 32 33 34 35
enum {
    MMS_CONNMAN_SIGNAL_DONE,
    MMS_CONNMAN_SIGNAL_COUNT
};

#define MMS_CONNMAN_SIGNAL_DONE_NAME "connman-done"

static guint mms_connman_sig[MMS_CONNMAN_SIGNAL_COUNT] = { 0 };

Slava Monich's avatar
Slava Monich committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
MMSConnMan*
mms_connman_ref(
    MMSConnMan* cm)
{
    if (cm) g_object_ref(MMS_CONNMAN(cm));
    return cm;
}

void
mms_connman_unref(
    MMSConnMan* cm)
{
    if (cm) g_object_unref(MMS_CONNMAN(cm));
}

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
gulong
mms_connman_add_done_callback(
    MMSConnMan* cm,
    mms_connman_event_fn fn,
    void* param)
{
    if (cm && fn) {
        return g_signal_connect_data(cm, MMS_CONNMAN_SIGNAL_DONE_NAME,
            G_CALLBACK(fn), param, NULL, 0);
    }
    return 0;
}

void
mms_connman_remove_callback(
    MMSConnMan* cm,
    gulong handler_id)
{
    if (cm && handler_id) g_signal_handler_disconnect(cm, handler_id);
}

static
void
mms_connman_finalize(
    GObject* object)
{
    MMS_ASSERT(!MMS_CONNMAN(object)->busy);
    G_OBJECT_CLASS(mms_connman_parent_class)->finalize(object);
}

Slava Monich's avatar
Slava Monich committed
81 82 83 84 85 86 87 88
/**
 * Per class initializer
 */
static
void
mms_connman_class_init(
    MMSConnManClass* klass)
{
89 90 91 92 93
    G_OBJECT_CLASS(klass)->finalize = mms_connman_finalize;
    mms_connman_sig[MMS_CONNMAN_SIGNAL_DONE] =
        g_signal_new(MMS_CONNMAN_SIGNAL_DONE_NAME,
            G_OBJECT_CLASS_TYPE(klass), G_SIGNAL_RUN_FIRST,
            0, NULL, NULL, NULL, G_TYPE_NONE, 0);
Slava Monich's avatar
Slava Monich committed
94 95 96 97 98 99 100 101 102 103 104 105
}

/**
 * Per instance initializer
 */
static
void
mms_connman_init(
    MMSConnMan* cm)
{
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
/**
 * Returns default (first available) IMSI or NULL if SIM is not present
 * or not configured. Caller must g_free() the returned string.
 */
char*
mms_connman_default_imsi(
    MMSConnMan* cm)
{
    if (cm) {
        MMSConnManClass* klass = MMS_CONNMAN_GET_CLASS(cm);
        if (klass->fn_default_imsi) {
            return klass->fn_default_imsi(cm);
        }
    }
    return NULL;
}

Slava Monich's avatar
Slava Monich committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/**
 * Creates a new connection or returns the reference to an aready active one.
 * The caller must release the reference.
 */
MMSConnection*
mms_connman_open_connection(
    MMSConnMan* cm,
    const char* imsi,
    gboolean user_request)
{
    if (cm) {
        MMSConnManClass* klass = MMS_CONNMAN_GET_CLASS(cm);
        if (klass->fn_open_connection) {
            return klass->fn_open_connection(cm, imsi, user_request);
        }
    }
    return NULL;
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
void
mms_connman_busy_update(
    MMSConnMan* cm,
    int change)
{
    MMS_ASSERT(change);
    if (cm && change) {
        cm->busy += change;
        MMS_VERBOSE("busy count %d", cm->busy);
        MMS_ASSERT(cm->busy >= 0);
        if (cm->busy < 1) {
            g_signal_emit(cm, mms_connman_sig[MMS_CONNMAN_SIGNAL_DONE], 0);
        }
    }
}

Slava Monich's avatar
Slava Monich committed
158 159 160 161 162 163 164
/*
 * Local Variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */