mms_connection_nemo.c 18.4 KB
Newer Older
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 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
/*
 * Copyright (C) 2016 Jolla Ltd.
 * Contact: Slava Monich <slava.monich@jolla.com>
 *
 * 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_connection_nemo.h"
#include "mms_connman.h"

#include <gofono_connmgr.h>
#include <gofono_connctx.h>

/* Logging */
#define MMS_LOG_MODULE_NAME mms_connection_log
#include "mms_connman_nemo_log.h"
MMS_LOG_MODULE_DEFINE("mms-connection-nemo");

enum mm_handler_id {
    MM_HANDLER_VALID,
    MM_HANDLER_MMS_IMSI,
    MM_HANDLER_COUNT
};

enum connmgr_handler_id {
    CONNMGR_HANDLER_VALID,
    CONNMGR_HANDLER_ATTACHED,
    CONNMGR_HANDLER_COUNT
};

enum context_handler_id {
    CONTEXT_HANDLER_VALID,
    CONTEXT_HANDLER_ACTIVE,
    CONTEXT_HANDLER_INTERFACE,
    CONTEXT_HANDLER_MMS_PROXY,
    CONTEXT_HANDLER_MMS_CENTER,
    CONTEXT_HANDLER_ACTIVATE_FAILED,
    CONTEXT_HANDLER_COUNT
};

typedef struct mms_connection_nemo {
    MMSConnection connection;
    MMSConnMan* cm;
    OfonoExtModemManager* mm;
    OfonoConnCtx* context;
    OfonoConnMgr* connmgr;
    gulong mm_handler_id[MM_HANDLER_COUNT];
    gulong context_handler_id[CONTEXT_HANDLER_COUNT];
    gulong connmgr_handler_id[CONNMGR_HANDLER_COUNT];
    char* imsi;
    char* path;
} MMSConnectionNemo;

typedef MMSConnectionClass MMSConnectionNemoClass;
G_DEFINE_TYPE(MMSConnectionNemo, mms_connection_nemo, MMS_TYPE_CONNECTION)
#define MMS_TYPE_CONNECTION_NEMO (mms_connection_nemo_get_type())
#define MMS_CONNECTION_NEMO(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj),\
   MMS_TYPE_CONNECTION_NEMO, MMSConnectionNemo))

static
void
mms_connection_nemo_disconnect(
    MMSConnectionNemo* self)
{
    ofonoext_mm_remove_handlers(self->mm, self->mm_handler_id,
        G_N_ELEMENTS(self->mm_handler_id));
    ofono_connmgr_remove_handlers(self->connmgr, self->connmgr_handler_id,
        G_N_ELEMENTS(self->connmgr_handler_id));
    ofono_connctx_remove_handlers(self->context, self->context_handler_id,
        G_N_ELEMENTS(self->context_handler_id));
}

static
void
mms_connection_nemo_disconnect_connmgr(
    MMSConnectionNemo* self,
    enum connmgr_handler_id id)
{
    if (self->connmgr_handler_id[id]) {
        ofono_connmgr_remove_handler(self->connmgr,
            self->connmgr_handler_id[id]);
        self->connmgr_handler_id[id] = 0;
    }
}

static
void
mms_connection_nemo_disconnect_context(
    MMSConnectionNemo* self,
    enum context_handler_id id)
{
    if (self->context_handler_id[id]) {
        ofono_connctx_remove_handler(self->context,
            self->context_handler_id[id]);
        self->context_handler_id[id] = 0;
    }
}

static
void
mms_connection_nemo_reset_mms_imsi_done(
    OfonoExtModemManager* mm,
    const char* path,
    const GError* error,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_DEBUG("Released %s", self->path);
    mms_connman_busy_dec(self->cm);
    mms_connection_unref(&self->connection);
}

static
gboolean
mms_connection_nemo_set_state(
    MMSConnectionNemo* self,
    MMS_CONNECTION_STATE state)
{
    if (self->connection.state != state) {
        if (self->connection.state == MMS_CONNECTION_STATE_FAILED ||
            self->connection.state == MMS_CONNECTION_STATE_CLOSED) {
            /* These are terminal states, can't change those */
            return FALSE;
        } else if (self->connection.state > state) {
            /* Can't move back to a previous state */
            return FALSE;
        }
        if (state == MMS_CONNECTION_STATE_FAILED ||
            state == MMS_CONNECTION_STATE_CLOSED) {

            if (self->path) {
                /* Tell ofono that this SIM is no longer reserved for MMS.
                 * Bump the reference while this D-Bus call is pending */
                mms_connection_ref(&self->connection);
                mms_connman_busy_inc(self->cm);
                ofonoext_mm_set_mms_imsi_full(self->mm, "",
                    mms_connection_nemo_reset_mms_imsi_done, self);
            }

            /* Stop listening for property changes */
            mms_connection_nemo_disconnect(self);
        }
        self->connection.state = state;
        mms_connection_signal_state_change(&self->connection);
    }
    return TRUE;
}

static
void
mms_connection_nemo_cancel(
    MMSConnectionNemo* self)
{
    if ((self->connection.state <= MMS_CONNECTION_STATE_OPENING &&
        mms_connection_nemo_set_state(self, MMS_CONNECTION_STATE_FAILED)) ||
        mms_connection_nemo_set_state(self, MMS_CONNECTION_STATE_CLOSED)) {
        MMS_DEBUG("Cancelled %s", self->connection.imsi);
    }
}

static
void
mms_connection_nemo_cancel_if_invalid(
    OfonoObject* obj,
    void* arg)
{
    if (!obj->valid) {
        MMS_VERBOSE_("oops!");
        mms_connection_nemo_cancel(MMS_CONNECTION_NEMO(arg));
    }
}

static
void
mms_connection_nemo_active_changed(
    OfonoConnCtx* context,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_ASSERT(self->context == context);
    if (context->active) {
        MMS_DEBUG("Connection %s opened", self->connection.imsi);
        mms_connection_nemo_set_state(self, MMS_CONNECTION_STATE_OPEN);
    } else {
        mms_connection_nemo_disconnect_context(self, CONTEXT_HANDLER_ACTIVE);
        mms_connection_nemo_set_state(self, MMS_CONNECTION_STATE_CLOSED);
    }
}

static
void
mms_connection_nemo_interface_changed(
    OfonoConnCtx* context,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_ASSERT(self->context == context);
    if (context->ifname) {
        self->connection.netif = context->ifname;
        MMS_DEBUG("Interface: %s", self->connection.netif);
    } else {
        self->connection.netif = NULL;
    }
}

static
void
mms_connection_nemo_mms_proxy_changed(
    OfonoConnCtx* context,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_ASSERT(self->context == context);
    if (context->mms_proxy) {
        self->connection.mmsproxy = context->mms_proxy;
        MMS_DEBUG("MessageProxy: %s", self->connection.mmsproxy);
    } else {
        self->connection.mmsproxy = NULL;
    }
}

static
void
mms_connection_nemo_mms_center_changed(
    OfonoConnCtx* context,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_ASSERT(self->context == context);
    if (context->mms_center) {
        self->connection.mmsc = context->mms_center;
        MMS_DEBUG("MessageCenter: %s", self->connection.mmsc);
    } else {
        self->connection.mmsc = NULL;
    }
}

static
void
mms_connection_nemo_activate_failed(
    OfonoConnCtx* context,
    const GError* error,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_ASSERT(self->context == context);
    mms_connection_nemo_cancel(self);
}

static
void
mms_connection_nemo_check_context(
    MMSConnectionNemo* self)
{
    if (self->connection.state == MMS_CONNECTION_STATE_OPENING) {
        OfonoConnCtx* context = self->context;
        if (ofono_connctx_valid(context) && context->active) {
            /* Already connected */
            mms_connection_nemo_set_state(self, MMS_CONNECTION_STATE_OPEN);
        } else {
            OfonoConnMgr* connmgr = self->connmgr;
            if (ofono_connmgr_valid(connmgr) && connmgr->attached) {
                self->context_handler_id[CONTEXT_HANDLER_ACTIVATE_FAILED] =
                    ofono_connctx_add_activate_failed_handler(context,
                    mms_connection_nemo_activate_failed, self);
                MMS_DEBUG("Activate %s", ofono_connctx_path(context));
                ofono_connctx_activate(context);
            }
        }
    }
}

static
void
mms_connection_nemo_connmgr_attached_changed(
    OfonoConnMgr* connmgr,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_DEBUG("Data registration %s for %s", connmgr->attached ? "on" : "off",
        ofono_connmgr_path(connmgr));
    mms_connection_nemo_check_context(self);
}

static
void
mms_connection_nemo_setup_context(
    MMSConnectionNemo* self)
{
    MMSConnection* conn = &self->connection;
    OfonoConnCtx* context = self->context;

    /* From this point on, cancel the connection if OfonoConnMgr becomes
     * invalid (which would probably mean SIM removal or ofono crash) */
    mms_connection_nemo_disconnect_context(self, CONTEXT_HANDLER_VALID);
    self->context_handler_id[CONTEXT_HANDLER_VALID] =
        ofono_object_add_valid_changed_handler(
            ofono_connctx_object(self->context),
            mms_connection_nemo_cancel_if_invalid, self);

    /* Capture the current context state */
    if (context->mms_proxy && context->mms_proxy[0]) {
        conn->mmsproxy = context->mms_proxy;
        MMS_DEBUG("MessageProxy: %s", conn->mmsproxy);
    }
    if (context->mms_center && context->mms_center[0]) {
        conn->mmsc = context->mms_center;
        MMS_DEBUG("MessageCenter: %s", conn->mmsc);
    }
    if (context->ifname && context->ifname[0]) {
        conn->netif = context->ifname;
        MMS_DEBUG("Interface: %s", conn->netif);
    }

    /* Track context property changes */
    self->context_handler_id[CONTEXT_HANDLER_ACTIVE] =
        ofono_connctx_add_active_changed_handler(context,
        mms_connection_nemo_active_changed, self);
    self->context_handler_id[CONTEXT_HANDLER_INTERFACE] =
        ofono_connctx_add_interface_changed_handler(context,
        mms_connection_nemo_interface_changed, self);
    self->context_handler_id[CONTEXT_HANDLER_MMS_PROXY] =
        ofono_connctx_add_mms_proxy_changed_handler(context,
        mms_connection_nemo_mms_proxy_changed, self);
    self->context_handler_id[CONTEXT_HANDLER_MMS_CENTER] =
        ofono_connctx_add_mms_center_changed_handler(context,
        mms_connection_nemo_mms_center_changed, self);

    /* And start tracking the data registration state */
    self->connmgr_handler_id[CONNMGR_HANDLER_ATTACHED] =
        ofono_connmgr_add_attached_changed_handler(self->connmgr,
        mms_connection_nemo_connmgr_attached_changed, self);

    /* Check if we can actually connect */
    mms_connection_nemo_check_context(self);
}

static
void
mms_connection_nemo_context_valid_changed(
    OfonoConnCtx* context,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    GASSERT(self->connection.state == MMS_CONNECTION_STATE_OPENING);
    if (ofono_connctx_valid(context)) {
        mms_connection_nemo_setup_context(self);
    }
}

static
void
mms_connection_nemo_init_context(
    MMSConnectionNemo* self)
{
    MMS_ASSERT(ofono_connmgr_valid(self->connmgr));
    MMS_ASSERT(!self->context);

    /* From this point on, cancel the connection if OfonoConnMgr becomes
     * invalid (which would probably mean SIM removal or ofono crash) */
    mms_connection_nemo_disconnect_connmgr(self, CONNMGR_HANDLER_VALID);
    self->connmgr_handler_id[CONNMGR_HANDLER_VALID] =
        ofono_object_add_valid_changed_handler(
            ofono_connmgr_object(self->connmgr),
            mms_connection_nemo_cancel_if_invalid, self);

    /* ofono_connctx_ref has no problem with a NULL pointer */
    self->context = ofono_connctx_ref(ofono_connmgr_get_context_for_type(
        self->connmgr, OFONO_CONNCTX_TYPE_MMS));
    if (self->context) {
        MMS_DEBUG("MMS context %s", ofono_connctx_path(self->context));
        if (ofono_connctx_valid(self->context)) {
            mms_connection_nemo_setup_context(self);
        } else {
            self->context_handler_id[CONTEXT_HANDLER_VALID] =
                ofono_connctx_add_valid_changed_handler(self->context,
                mms_connection_nemo_context_valid_changed, self);
        }
    } else {
        MMS_WARN("No MMS context");
        mms_connection_nemo_cancel(self);
    }
}

static
void
mms_connection_nemo_connmgr_valid_changed(
    OfonoConnMgr* connmgr,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    GASSERT(self->connection.state == MMS_CONNECTION_STATE_OPENING);
    if (ofono_connmgr_valid(connmgr)) {
        mms_connection_nemo_init_context(self);
    }
}

static
void
mms_connection_nemo_mms_imsi_changed(
    OfonoExtModemManager* mm,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    if (mm->valid && g_strcmp0(mm->mms_imsi, self->connection.imsi)) {
        MMS_VERBOSE_("%s", mm->mms_imsi);
        mms_connection_nemo_cancel(self);
    }
}

static
void
mms_connection_nemo_set_mms_imsi_done(
    OfonoExtModemManager* mm,
    const char* path,
    const GError* error,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    if (error) {
        mms_connection_nemo_cancel(self);
    } else {
        MMS_DEBUG("MMS modem path %s", path);
        MMS_ASSERT(!self->path);
        MMS_ASSERT(!self->connmgr);
        self->path = g_strdup(path);

        if (self->connection.state == MMS_CONNECTION_STATE_OPENING) {
            self->connmgr = ofono_connmgr_new(path);

            /* Cancel connection if MMS SIM changes */
            self->mm_handler_id[MM_HANDLER_MMS_IMSI] =
                ofonoext_mm_add_mms_imsi_changed_handler(mm,
                mms_connection_nemo_mms_imsi_changed, self);

            if (ofono_connmgr_valid(self->connmgr)) {
                mms_connection_nemo_init_context(self);
            } else {
                /* Wait for OfonoConnMgr to become valid */
                self->connmgr_handler_id[CONNMGR_HANDLER_VALID] =
                    ofono_connmgr_add_valid_changed_handler(self->connmgr,
                    mms_connection_nemo_connmgr_valid_changed, self);
            }
        } else {
            /* Connection has been cancelled, release the slot */
            MMS_DEBUG("Canceled, releasing %s", path);
            mms_connection_ref(&self->connection);
            mms_connman_busy_inc(self->cm);
            ofonoext_mm_set_mms_imsi_full(self->mm, "",
                mms_connection_nemo_reset_mms_imsi_done, self);
        }
    }
    /* Release the reference */
    mms_connman_busy_dec(self->cm);
    mms_connection_unref(&self->connection);
}

static
void
mms_connection_nemo_cancel_if_mm_invalid(
    OfonoExtModemManager* mm,
    void* arg)
{
    if (!mm->valid) {
        MMS_VERBOSE_("oops!");
        mms_connection_nemo_cancel(MMS_CONNECTION_NEMO(arg));
    }
}

static
void
mms_connection_nemo_request_sim(
    MMSConnectionNemo* self)
{
    /* Cancel the connection if OfonoExtModemManager becomes invalid */
    GASSERT(self->mm->valid);
    ofonoext_mm_remove_handler(self->mm,
        self->mm_handler_id[MM_HANDLER_VALID]);
    self->mm_handler_id[MM_HANDLER_VALID] =
        ofonoext_mm_add_valid_changed_handler(self->mm,
        mms_connection_nemo_cancel_if_mm_invalid, self);

    /* Bump the reference while this D-Bus call is pending */
    mms_connection_ref(&self->connection);
    mms_connman_busy_inc(self->cm);
    ofonoext_mm_set_mms_imsi_full(self->mm, self->imsi,
        mms_connection_nemo_set_mms_imsi_done, self);
}

static
void
mms_connection_nemo_mm_valid_changed(
    OfonoExtModemManager* mm,
    void* arg)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(arg);
    MMS_VERBOSE_("%p %d", self, mm->valid);
    if (mm->valid) {
        mms_connection_nemo_request_sim(arg);
    }
}

MMSConnection*
mms_connection_nemo_new(
    MMSConnMan* cm,
    const char* imsi,
    gboolean user_request)
{
    MMSConnectionNemo* self = g_object_new(MMS_TYPE_CONNECTION_NEMO, NULL);
    MMSConnection* conn = &self->connection;

    MMS_VERBOSE_("%p %s", self, imsi);
    conn->user_connection = user_request;
    conn->imsi = self->imsi = g_strdup(imsi);
    conn->state = MMS_CONNECTION_STATE_OPENING;
    self->mm = ofonoext_mm_new();
    self->cm = mms_connman_ref(cm);

    if (self->mm->valid) {
        mms_connection_nemo_request_sim(self);
    } else {
        self->mm_handler_id[MM_HANDLER_VALID] =
            ofonoext_mm_add_valid_changed_handler(self->mm,
            mms_connection_nemo_mm_valid_changed, self);
    }

    return &self->connection;
}

static
void
mms_connection_nemo_close(
    MMSConnection* connection)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(connection);
    OfonoConnCtx* context = self->context;
    if (ofono_connctx_valid(context) && context->active) {
        MMS_DEBUG("Deactivate %s", ofono_connctx_path(context));
        ofono_connctx_deactivate(context);
    } else {
        if (self->connection.state != MMS_CONNECTION_STATE_FAILED &&
            self->connection.state != MMS_CONNECTION_STATE_CLOSED) {
            MMS_DEBUG("Cancelling %s", self->connection.imsi);
            mms_connection_nemo_cancel(self);
        }
    }
}

/**
 * First stage of deinitialization (release all references).
 * May be called more than once in the lifetime of the object.
 */
static
void
mms_connection_nemo_dispose(
    GObject* object)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(object);
    MMS_VERBOSE_("%p %s", self, self->imsi);
    MMS_ASSERT(!mms_connection_is_active(&self->connection));
    mms_connection_nemo_disconnect(self);
    if (self->context) {
        if (mms_connection_is_active(&self->connection) &&
            self->context->active) {
            ofono_connctx_deactivate(self->context);
        }
        ofono_connctx_unref(self->context);
        self->context = NULL;
    }
    self->connection.netif = NULL;
    G_OBJECT_CLASS(mms_connection_nemo_parent_class)->dispose(object);
}

/**
 * Final stage of deinitialization
 */
static
void
mms_connection_nemo_finalize(
    GObject* object)
{
    MMSConnectionNemo* self = MMS_CONNECTION_NEMO(object);
    MMS_VERBOSE_("%p %s", self, self->imsi);
    ofono_connmgr_unref(self->connmgr);
    ofono_connctx_unref(self->context);
    ofonoext_mm_unref(self->mm);
    mms_connman_unref(self->cm);
    g_free(self->imsi);
    g_free(self->path);
    G_OBJECT_CLASS(mms_connection_nemo_parent_class)->finalize(object);
}

/**
 * Per class initializer
 */
static
void
mms_connection_nemo_class_init(
    MMSConnectionNemoClass* klass)
{
    GObjectClass* object_class = G_OBJECT_CLASS(klass);
    klass->fn_close = mms_connection_nemo_close;
    object_class->dispose = mms_connection_nemo_dispose;
    object_class->finalize = mms_connection_nemo_finalize;
}

/**
 * Per instance initializer
 */
static
void
mms_connection_nemo_init(
    MMSConnectionNemo* self)
{
    MMS_VERBOSE_("%p", self);
}

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