mms_ofono_manager.c 5.36 KB
Newer Older
Slava Monich's avatar
Slava Monich committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2013-2014 Jolla 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.
 *
 */

#include "mms_ofono_manager.h"
#include "mms_ofono_modem.h"
#include "mms_ofono_names.h"
#include "mms_ofono_names.h"

Slava Monich's avatar
Slava Monich committed
20 21 22
/* Generated headers */
#include "org.ofono.Manager.h"

Slava Monich's avatar
Slava Monich committed
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
/* Logging */
#define MMS_LOG_MODULE_NAME mms_ofono_manager_log
#include "mms_ofono_log.h"
MMS_LOG_MODULE_DEFINE("mms-ofono-manager");

struct mms_ofono_manager {
    GDBusConnection* bus;
    GHashTable* modems;
    OrgOfonoManager* proxy;
    gulong modem_added_signal_id;
    gulong modem_removed_signal_id;
};

static
void
mms_ofono_manager_set_modems(
    MMSOfonoManager* ofono,
    GVariant* modems)
{
    GVariantIter iter;
    GVariant* child;
    MMS_DEBUG("%u modem(s) found", (guint)g_variant_n_children(modems));
    g_hash_table_remove_all(ofono->modems);

    for (g_variant_iter_init(&iter, modems);
         (child = g_variant_iter_next_value(&iter)) != NULL;
         g_variant_unref(child)) {

        MMSOfonoModem* modem;
        const char* path = NULL;
        GVariant* properties = NULL;

        g_variant_get(child, "(&o@a{sv})", &path, &properties);
        MMS_ASSERT(path);
        MMS_ASSERT(properties);

        modem = mms_ofono_modem_new(ofono->bus, path, properties);
        if (modem) g_hash_table_replace(ofono->modems, modem->path, modem);
        g_variant_unref(properties);
    }
}

static
void
mms_ofono_manager_modem_added(
    OrgOfonoManager* proxy,
    const char* path,
    GVariant* properties,
    MMSOfonoManager* ofono)
{
    MMSOfonoModem* modem;
    MMS_VERBOSE_("%p %s", ofono, path);
    MMS_ASSERT(proxy == ofono->proxy);
    g_hash_table_remove(ofono->modems, path);
    modem = mms_ofono_modem_new(ofono->bus, path, properties);
    if (modem) g_hash_table_replace(ofono->modems, modem->path, modem);
}

static
void
mms_ofono_manager_modem_removed(
    OrgOfonoManager* proxy,
    const char* path,
    MMSOfonoManager* ofono)
{
    MMS_VERBOSE_("%p %s", ofono, path);
    MMS_ASSERT(proxy == ofono->proxy);
    g_hash_table_remove(ofono->modems, path);
}

static
void
mms_ofono_manager_hash_remove_modem(
    gpointer data)
{
    mms_ofono_modem_free(data);
}

MMSOfonoManager*
mms_ofono_manager_new(
    GDBusConnection* bus)
{
    GError* error = NULL;
    OrgOfonoManager* proxy = org_ofono_manager_proxy_new_sync(bus,
        G_DBUS_PROXY_FLAGS_NONE, OFONO_SERVICE, "/", NULL, &error);
    if (proxy) {
        GVariant* modems = NULL;
        if (org_ofono_manager_call_get_modems_sync(proxy, &modems,
            NULL, &error)) {

            MMSOfonoManager* ofono = g_new0(MMSOfonoManager, 1);
            ofono->proxy = proxy;
            g_object_ref(ofono->bus = bus);
            ofono->modems = g_hash_table_new_full(g_str_hash, g_str_equal,
                NULL, mms_ofono_manager_hash_remove_modem);

            /* Subscribe for ModemAdded/Removed notifications */
            ofono->modem_added_signal_id = g_signal_connect(
                proxy, "modem-added",
                G_CALLBACK(mms_ofono_manager_modem_added),
                ofono);
            ofono->modem_removed_signal_id = g_signal_connect(
                proxy, "modem-removed",
                G_CALLBACK(mms_ofono_manager_modem_removed),
                ofono);

            mms_ofono_manager_set_modems(ofono, modems);
            g_variant_unref(modems);
            return ofono;

        } else {
            MMS_ERR("Can't get list of modems: %s", MMS_ERRMSG(error));
            g_error_free(error);
        }
        g_object_unref(proxy);
    } else {
        MMS_ERR("%s", MMS_ERRMSG(error));
        g_error_free(error);
    }
    return NULL;
}

static
gboolean
mms_ofono_manager_modem_imsi_find_cb(
    gpointer key,
    gpointer value,
    gpointer user_data)
{
    MMSOfonoModem* modem = value;
    const char* imsi = user_data;
    MMS_ASSERT(imsi);
    return modem->imsi && !strcmp(modem->imsi, imsi);
}

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
MMSOfonoModem*
mms_ofono_manager_default_modem(
    MMSOfonoManager* ofono)
{
    if (g_hash_table_size(ofono->modems) > 0) {
        GHashTableIter iter;
        gpointer key, value = NULL;
        g_hash_table_iter_init(&iter, ofono->modems);
        if (g_hash_table_iter_next(&iter, &key, &value)) {
            return value;
        }
    }
    return NULL;
}

Slava Monich's avatar
Slava Monich committed
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
MMSOfonoModem*
mms_ofono_manager_modem_for_imsi(
    MMSOfonoManager* ofono,
    const char* imsi)
{
    return ofono ? g_hash_table_find(ofono->modems,
        mms_ofono_manager_modem_imsi_find_cb, (void*)imsi) : NULL;
}

void
mms_ofono_manager_free(
    MMSOfonoManager* ofono)
{
    if (ofono) {
        if (ofono->proxy) {
            g_signal_handler_disconnect(ofono->proxy,
                ofono->modem_added_signal_id);
            g_signal_handler_disconnect(ofono->proxy,
                ofono->modem_removed_signal_id);
            g_object_unref(ofono->proxy);
        }
        g_hash_table_destroy(ofono->modems);
        g_object_unref(ofono->bus);
        g_free(ofono);
    }
}

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