summaryrefslogtreecommitdiffstats
path: root/firmware/target/hosted/ibasso/vold-ibasso.c
blob: f56bd683d33275ad9a5f526161fff3fdc90da734 (plain)
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
/***************************************************************************
 *             __________               __   ___
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 *
 * Copyright (C) 2014 by Ilia Sergachev: Initial Rockbox port to iBasso DX50
 * Copyright (C) 2014 by Mario Basister: iBasso DX90 port
 * Copyright (C) 2014 by Simon Rothen: Initial Rockbox repository submission, additional features
 * Copyright (C) 2014 by Udo Schläpfer: Code clean up, additional features
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/


#include <pthread.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>

#include "config.h"
#include "debug.h"
#include "powermgmt.h"

#include "debug-ibasso.h"


/*
    Without this socket iBasso Vold will not start.
    iBasso Vold uses this to send status messages about storage devices.
*/
static const char VOLD_MONITOR_SOCKET_NAME[] = "UNIX_domain";
static int _vold_monitor_socket_fd           = -1;

static void vold_monitor_open_socket(void)
{
    TRACE;

    unlink(VOLD_MONITOR_SOCKET_NAME);

    _vold_monitor_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0);

    if(_vold_monitor_socket_fd < 0)
    {
        _vold_monitor_socket_fd = -1;
        return;
    }

    struct sockaddr_un addr;
    memset(&addr, 0, sizeof(addr));
    addr.sun_family = AF_UNIX;
    strncpy(addr.sun_path, VOLD_MONITOR_SOCKET_NAME, sizeof(addr.sun_path) -1);

    if(bind(_vold_monitor_socket_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0)
    {
        close(_vold_monitor_socket_fd);
        unlink(VOLD_MONITOR_SOCKET_NAME);
        _vold_monitor_socket_fd = -1;
        return;
    }

    if(listen(_vold_monitor_socket_fd, 1) < 0)
    {
        close(_vold_monitor_socket_fd);
        unlink(VOLD_MONITOR_SOCKET_NAME);
        _vold_monitor_socket_fd = -1;
        return;
    }
}

/* Track state of external SD */
bool extsd_present = false;

/*
    bionic does not have pthread_cancel.
    0: Vold monitor thread stopped/ending.
    1: Vold monitor thread started/running.
*/
static volatile sig_atomic_t _vold_monitor_active = 0;


/*
    1: /mnt/sdcard is unmounting
    0: else
*/
static volatile sig_atomic_t _vold_monitor_forced_close_imminent = 0;


static void* vold_monitor_run(void* nothing)
{
    _vold_monitor_active = 1;

    (void) nothing;

    DEBUGF("DEBUG %s: Thread start.", __func__);

    vold_monitor_open_socket();
    if(_vold_monitor_socket_fd < 0)
    {
        DEBUGF("ERROR %s: Thread end: No socket.", __func__);

        _vold_monitor_active = 0;
        return 0;
    }

    struct pollfd fds[1];
    fds[0].fd     = _vold_monitor_socket_fd;
    fds[0].events = POLLIN;

    while(_vold_monitor_active == 1)
    {
        poll(fds, 1, 10);
        if(! (fds[0].revents & POLLIN))
        {
            continue;
        }

        int socket_fd = accept(_vold_monitor_socket_fd, NULL, NULL);

        if(socket_fd < 0)
        {
            DEBUGF("ERROR %s: accept failed.", __func__);

            continue;
        }

        while(true)
        {
            char msg[1024];
            memset(msg, 0, sizeof(msg));
            int length = read(socket_fd, msg, sizeof(msg));

            if(length <= 0)
            {
                close(socket_fd);
                break;
            }

            DEBUGF("%s: msg: %s", __func__, msg);

            if(strcmp(msg, "Volume flash /mnt/sdcard state changed from 4 (Mounted) to 5 (Unmounting)") == 0)
            {
                /* We are losing /mnt/sdcard, shutdown Rockbox before it is forced closed. */

                _vold_monitor_forced_close_imminent = 1;
                sys_poweroff();
                _vold_monitor_active = 0;
            }
            else if(strcmp(msg, "Volume sdcard /mnt/external_sd state changed from 4 (Mounted) to 5 (Unmounting)") == 0)
            {
                /* We are loosing the external sdcard, inform Rockbox. */
                extsd_present = false;
            }
            else  if(strcmp(msg, "Volume sdcard /mnt/external_sd state changed from 3 (Checking) to 4 (Mounted)") == 0)
            {
                /* The external sdcard is back, inform Rockbox. */
                extsd_present = true;
            }
        }
    }

    close(_vold_monitor_socket_fd);
    unlink(VOLD_MONITOR_SOCKET_NAME);
    _vold_monitor_socket_fd = -1;

    DEBUGF("%s: Thread end.", __func__);

    _vold_monitor_active = 0;
    return 0;
}


/* Vold monitor thread. */
static pthread_t _vold_monitor_thread;


void vold_monitor_start(void)
{
    TRACE;

    if(_vold_monitor_active == 0)
    {
        pthread_create(&_vold_monitor_thread, NULL, vold_monitor_run, NULL);
    }
}


bool vold_monitor_forced_close_imminent(void)
{
    TRACE;

    return(_vold_monitor_forced_close_imminent == 1);
}