usb_moded-control.c 9.09 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
/**
 * @file usb_moded-control.c
 *
 * Copyright (C) 2013-2018 Jolla. All rights reserved.
 *
 * @author: Philippe De Swert <philippe.deswert@jollamobile.com>
 * @author: Simo Piiroinen <simo.piiroinen@jollamobile.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Lesser 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 Lesser 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
 */

#include "usb_moded-control.h"

#include "usb_moded-config-private.h"
#include "usb_moded-dbus-private.h"
28
#include "usb_moded-dyn-config.h"
29 30 31
#include "usb_moded-log.h"
#include "usb_moded-modes.h"
#include "usb_moded-worker.h"
32 33 34

#include <string.h>
#include <stdlib.h>
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

/* ========================================================================= *
 * Prototypes
 * ========================================================================= */

/* -- usbmoded -- */

void           control_rethink_usb_charging_fallback(void);
const char    *control_get_external_mode            (void);
static void    control_set_external_mode            (const char *mode);
void           control_clear_external_mode          (void);
static void    control_update_external_mode         (void);
const char    *control_get_usb_mode                 (void);
void           control_clear_internal_mode          (void);
void           control_set_usb_mode                 (const char *mode);
void           control_mode_switched                (const char *override);
void           control_select_usb_mode              (void);
void           control_set_cable_state              (cable_state_t cable_state);
cable_state_t  control_get_cable_state              (void);
void           control_clear_cable_state            (void);
bool           control_get_connection_state         (void);

/* ========================================================================= *
 * Data
 * ========================================================================= */

/* The external mode;
 *
 * What was the last mode signaled over D-Bus.
 */
char *control_external_mode = NULL;

/** The logical mode name
 *
 * Full set of valid modes can occur here
 */
char *control_internal_mode = NULL;

/** Connection status
 *
 * Access only via:
 * - control_set_cable_state()
 * - control_get_connection_state()
 */
cable_state_t control_cable_state = CABLE_STATE_UNKNOWN;

/* ========================================================================= *
 * Functions
 * ========================================================================= */

/** Check if we can/should leave charging fallback mode
 *
 * Called when device lock status, or device status (dsme)
 * changes.
 */
void
control_rethink_usb_charging_fallback(void)
{
    /* Cable must be connected to a pc */
    if( control_get_cable_state() != CABLE_STATE_PC_CONNECTED )
        goto EXIT;

    /* Switching can happen only from MODE_UNDEFINED
     * or MODE_CHARGING_FALLBACK */
    const char *usb_mode = control_get_usb_mode();

    if( strcmp(usb_mode, MODE_UNDEFINED) &&
        strcmp(usb_mode, MODE_CHARGING_FALLBACK) )
        goto EXIT;

    if( !usbmoded_can_export() ) {
        log_notice("exporting data not allowed; stay in %s", usb_mode);
        goto EXIT;
    }

    log_debug("attempt to leave %s", usb_mode);
    control_select_usb_mode();

EXIT:
    return;
}

const char *control_get_external_mode(void)
{
    return control_external_mode ?: MODE_UNDEFINED;
}

static void control_set_external_mode(const char *mode)
{
    gchar *previous = control_external_mode;
    if( !g_strcmp0(previous, mode) )
        goto EXIT;

    log_debug("external_mode: %s -> %s",
              previous, mode);

    control_external_mode = g_strdup(mode);
    g_free(previous);

    // DO THE DBUS BROADCAST

    if( !strcmp(control_external_mode, MODE_ASK) ) {
        /* send signal, mode will be set when the dialog service calls
         * the set_mode method call. */
        umdbus_send_state_signal(USB_CONNECTED_DIALOG_SHOW);
    }

    umdbus_send_state_signal(control_external_mode);

EXIT:
    return;
}

void control_clear_external_mode(void)
{
    g_free(control_external_mode),
        control_external_mode = 0;
}

static void control_update_external_mode(void)
{
    const char *internal_mode = control_get_usb_mode();
    const char *external_mode = common_map_mode_to_external(internal_mode);

    control_set_external_mode(external_mode);
}

/** get the usb mode
 *
 * @return the currently set mode
 *
 */
const char * control_get_usb_mode(void)
{
    return control_internal_mode;
}

void control_clear_internal_mode(void)
{
    g_free(control_internal_mode),
        control_internal_mode = 0;
}

/** set the usb mode
 *
 * @param mode The requested USB mode
 */
void control_set_usb_mode(const char *mode)
{
    gchar *previous = control_internal_mode;
    if( !g_strcmp0(previous, mode) )
        goto EXIT;

    log_debug("internal_mode: %s -> %s",
              previous, mode);

    control_internal_mode = g_strdup(mode);
    g_free(previous);

    /* Invalidate current mode for the duration of mode transition */
    control_set_external_mode(MODE_BUSY);

    /* Propagate down to gadget config */
    worker_request_hardware_mode(control_internal_mode);

EXIT:
    return;
}

/* Worker thread has finished mode switch
 *
 * @param mode The activated USB mode
 */
void control_mode_switched(const char *mode)
{
    /* Update state data - without retriggering the worker thread
     */
    if( g_strcmp0(control_internal_mode, mode) ) {
        log_debug("internal_mode: %s -> %s",
                  control_internal_mode, mode);
        g_free(control_internal_mode),
            control_internal_mode = g_strdup(mode);
    }

    /* Propagate up to D-Bus */
    control_update_external_mode();

    return;
}

/** set the chosen usb state
 *
 * gauge what mode to enter and then call control_set_usb_mode()
 *
 */
void control_select_usb_mode(void)
{
    char *mode_to_set = 0;

    if( usbmoded_rescue_mode ) {
        log_debug("Entering rescue mode!\n");
        control_set_usb_mode(MODE_DEVELOPER);
        goto EXIT;
    }

    if( usbmoded_diag_mode ) {
        log_debug("Entering diagnostic mode!\n");
        if( usbmoded_modelist ) {
            /* XXX 1st entry is just assumed to be diag mode??? */
            GList *iter = usbmoded_modelist;
            struct mode_list_elem *data = iter->data;
            control_set_usb_mode(data->mode_name);
        }
        goto EXIT;
    }

    mode_to_set = config_get_mode_setting();

    /* If there is only one allowed mode, use it without
     * going through ask-mode */
    if( !strcmp(MODE_ASK, mode_to_set) ) {
        // FIXME free() vs g_free() conflict
        gchar *available = common_get_mode_list(AVAILABLE_MODES_LIST);
        if( *available && !strchr(available, ',') ) {
            free(mode_to_set), mode_to_set = available, available = 0;
        }
        g_free(available);
    }

    if( mode_to_set && usbmoded_can_export() ) {
        control_set_usb_mode(mode_to_set);
    }
    else {
        /* config is corrupted or we do not have a mode configured, fallback to charging
         * We also fall back here in case the device is locked and we do not
         * export the system contents. Or if we are in acting dead mode.
         */
        control_set_usb_mode(MODE_CHARGING_FALLBACK);
    }
EXIT:
    free(mode_to_set);
}

/** set the usb connection status
 *
 * @param cable_state CABLE_STATE_DISCONNECTED, ...
 */
void control_set_cable_state(cable_state_t cable_state)
{
    cable_state_t prev = control_cable_state;
    control_cable_state = cable_state;

    if( control_cable_state == prev )
        goto EXIT;

    log_debug("control_cable_state: %s -> %s",
              cable_state_repr(prev),
              cable_state_repr(control_cable_state));

    switch( control_cable_state ) {
    default:
    case CABLE_STATE_DISCONNECTED:
        control_set_usb_mode(MODE_UNDEFINED);
        break;
    case CABLE_STATE_CHARGER_CONNECTED:
        control_set_usb_mode(MODE_CHARGER);
        break;
    case CABLE_STATE_PC_CONNECTED:
        control_select_usb_mode();
        break;
    }

EXIT:
    return;
}

/** get the usb connection status
 *
 * @return CABLE_STATE_DISCONNECTED, ...
 */
cable_state_t control_get_cable_state(void)
{
    return control_cable_state;
}

void control_clear_cable_state(void)
{
    control_cable_state = CABLE_STATE_UNKNOWN;
}

/** Get if the cable (pc or charger) is connected or not
 *
 * @ return true if connected, false if disconnected
 */
bool control_get_connection_state(void)
{
    bool connected = false;
    switch( control_get_cable_state() ) {
    case CABLE_STATE_CHARGER_CONNECTED:
    case CABLE_STATE_PC_CONNECTED:
        connected = true;
        break;
    default:
        break;
    }
    return connected;
}