diff options
Diffstat (limited to 'firmware/target/hosted/ibasso/vold-ibasso.c')
-rw-r--r-- | firmware/target/hosted/ibasso/vold-ibasso.c | 203 |
1 files changed, 203 insertions, 0 deletions
diff --git a/firmware/target/hosted/ibasso/vold-ibasso.c b/firmware/target/hosted/ibasso/vold-ibasso.c new file mode 100644 index 0000000000..c92b86d364 --- /dev/null +++ b/firmware/target/hosted/ibasso/vold-ibasso.c | |||
@@ -0,0 +1,203 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___ | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * | ||
9 | * Copyright (C) 2014 by Ilia Sergachev: Initial Rockbox port to iBasso DX50 | ||
10 | * Copyright (C) 2014 by Mario Basister: iBasso DX90 port | ||
11 | * Copyright (C) 2014 by Simon Rothen: Initial Rockbox repository submission, additional features | ||
12 | * Copyright (C) 2014 by Udo Schläpfer: Code clean up, additional features | ||
13 | * | ||
14 | * This program is free software; you can redistribute it and/or | ||
15 | * modify it under the terms of the GNU General Public License | ||
16 | * as published by the Free Software Foundation; either version 2 | ||
17 | * of the License, or (at your option) any later version. | ||
18 | * | ||
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
20 | * KIND, either express or implied. | ||
21 | * | ||
22 | ****************************************************************************/ | ||
23 | |||
24 | |||
25 | #include <pthread.h> | ||
26 | #include <stdbool.h> | ||
27 | #include <string.h> | ||
28 | #include <unistd.h> | ||
29 | #include <sys/poll.h> | ||
30 | #include <sys/socket.h> | ||
31 | #include <sys/types.h> | ||
32 | #include <sys/un.h> | ||
33 | |||
34 | #include "config.h" | ||
35 | #include "debug.h" | ||
36 | #include "powermgmt.h" | ||
37 | |||
38 | #include "debug-ibasso.h" | ||
39 | |||
40 | |||
41 | /* | ||
42 | Without this socket iBasso Vold will not start. | ||
43 | iBasso Vold uses this to send status messages about storage devices. | ||
44 | */ | ||
45 | static const char VOLD_MONITOR_SOCKET_NAME[] = "UNIX_domain"; | ||
46 | static int _vold_monitor_socket_fd = -1; | ||
47 | |||
48 | |||
49 | static void vold_monitor_open_socket(void) | ||
50 | { | ||
51 | TRACE; | ||
52 | |||
53 | unlink(VOLD_MONITOR_SOCKET_NAME); | ||
54 | |||
55 | _vold_monitor_socket_fd = socket(AF_UNIX, SOCK_STREAM, 0); | ||
56 | |||
57 | if(_vold_monitor_socket_fd < 0) | ||
58 | { | ||
59 | _vold_monitor_socket_fd = -1; | ||
60 | return; | ||
61 | } | ||
62 | |||
63 | struct sockaddr_un addr; | ||
64 | memset(&addr, 0, sizeof(addr)); | ||
65 | addr.sun_family = AF_UNIX; | ||
66 | strncpy(addr.sun_path, VOLD_MONITOR_SOCKET_NAME, sizeof(addr.sun_path) -1); | ||
67 | |||
68 | if(bind(_vold_monitor_socket_fd, (struct sockaddr*) &addr, sizeof(addr)) < 0) | ||
69 | { | ||
70 | close(_vold_monitor_socket_fd); | ||
71 | unlink(VOLD_MONITOR_SOCKET_NAME); | ||
72 | _vold_monitor_socket_fd = -1; | ||
73 | return; | ||
74 | } | ||
75 | |||
76 | if(listen(_vold_monitor_socket_fd, 1) < 0) | ||
77 | { | ||
78 | close(_vold_monitor_socket_fd); | ||
79 | unlink(VOLD_MONITOR_SOCKET_NAME); | ||
80 | _vold_monitor_socket_fd = -1; | ||
81 | return; | ||
82 | } | ||
83 | } | ||
84 | |||
85 | |||
86 | /* | ||
87 | bionic does not have pthread_cancel. | ||
88 | 0: Vold monitor thread stopped/ending. | ||
89 | 1: Vold monitor thread started/running. | ||
90 | */ | ||
91 | static volatile sig_atomic_t _vold_monitor_active = 0; | ||
92 | |||
93 | |||
94 | /* | ||
95 | 1: /mnt/sdcard is unmounting | ||
96 | 0: else | ||
97 | */ | ||
98 | static volatile sig_atomic_t _vold_monitor_forced_close_imminent = 0; | ||
99 | |||
100 | |||
101 | static void* vold_monitor_run(void* nothing) | ||
102 | { | ||
103 | _vold_monitor_active = 1; | ||
104 | |||
105 | (void) nothing; | ||
106 | |||
107 | DEBUGF("DEBUG %s: Thread start.", __func__); | ||
108 | |||
109 | vold_monitor_open_socket(); | ||
110 | if(_vold_monitor_socket_fd < 0) | ||
111 | { | ||
112 | DEBUGF("ERROR %s: Thread end: No socket.", __func__); | ||
113 | |||
114 | _vold_monitor_active = 0; | ||
115 | return 0; | ||
116 | } | ||
117 | |||
118 | struct pollfd fds[1]; | ||
119 | fds[0].fd = _vold_monitor_socket_fd; | ||
120 | fds[0].events = POLLIN; | ||
121 | |||
122 | while(_vold_monitor_active == 1) | ||
123 | { | ||
124 | poll(fds, 1, 10); | ||
125 | if(! (fds[0].revents & POLLIN)) | ||
126 | { | ||
127 | continue; | ||
128 | } | ||
129 | |||
130 | int socket_fd = accept(_vold_monitor_socket_fd, NULL, NULL); | ||
131 | |||
132 | if(socket_fd < 0) | ||
133 | { | ||
134 | DEBUGF("ERROR %s: accept failed.", __func__); | ||
135 | |||
136 | continue; | ||
137 | } | ||
138 | |||
139 | while(true) | ||
140 | { | ||
141 | char msg[1024]; | ||
142 | memset(msg, 0, sizeof(msg)); | ||
143 | int length = read(socket_fd, msg, sizeof(msg)); | ||
144 | |||
145 | if(length <= 0) | ||
146 | { | ||
147 | close(socket_fd); | ||
148 | break; | ||
149 | } | ||
150 | |||
151 | DEBUGF("%s: msg: %s", __func__, msg); | ||
152 | |||
153 | if(strcmp(msg, "Volume flash /mnt/sdcard state changed from 4 (Mounted) to 5 (Unmounting)") == 0) | ||
154 | { | ||
155 | /* We are losing /mnt/sdcard, shutdown Rockbox before it is forced closed. */ | ||
156 | |||
157 | _vold_monitor_forced_close_imminent = 1; | ||
158 | sys_poweroff(); | ||
159 | _vold_monitor_active = 0; | ||
160 | } | ||
161 | else if(strcmp(msg, "Volume sdcard /mnt/external_sd state changed from 4 (Mounted) to 5 (Unmounting)") == 0) | ||
162 | { | ||
163 | /* We are loosing the external sdcard, inform Rockbox. */ | ||
164 | } | ||
165 | else if(strcmp(msg, "Volume sdcard /mnt/external_sd state changed from 3 (Checking) to 4 (Mounted)") == 0) | ||
166 | { | ||
167 | /* The external sdcard is back, inform Rockbox. */ | ||
168 | } | ||
169 | } | ||
170 | } | ||
171 | |||
172 | close(_vold_monitor_socket_fd); | ||
173 | unlink(VOLD_MONITOR_SOCKET_NAME); | ||
174 | _vold_monitor_socket_fd = -1; | ||
175 | |||
176 | DEBUGF("%s: Thread end.", __func__); | ||
177 | |||
178 | _vold_monitor_active = 0; | ||
179 | return 0; | ||
180 | } | ||
181 | |||
182 | |||
183 | /* Vold monitor thread. */ | ||
184 | static pthread_t _vold_monitor_thread; | ||
185 | |||
186 | |||
187 | void vold_monitor_start(void) | ||
188 | { | ||
189 | TRACE; | ||
190 | |||
191 | if(_vold_monitor_active == 0) | ||
192 | { | ||
193 | pthread_create(&_vold_monitor_thread, NULL, vold_monitor_run, NULL); | ||
194 | } | ||
195 | } | ||
196 | |||
197 | |||
198 | bool vold_monitor_forced_close_imminent(void) | ||
199 | { | ||
200 | TRACE; | ||
201 | |||
202 | return(_vold_monitor_forced_close_imminent == 1); | ||
203 | } | ||