Repository navigation
Expand file tree
/
Copy pathMobileDataManager.php
More file actions
557 lines (492 loc) · 18.7 KB
/
Copy pathMobileDataManager.php
File metadata and controls
557 lines (492 loc) · 18.7 KB
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
<?php
/**
* MobileDataManager
*
* A RaspAP plugin to manage 4G/LTE QMI modems via ModemManager and NetworkManager.
* Designed for devices like the Fairphone 2 running Linux with rmtfs + ModemManager.
*
* Uses mmcli for modem status/information and nmcli for connection management.
* Connection profiles are created with: nmcli connection add type gsm ifname '*' con-name gsm apn <APN>
* Connections are activated with: nmcli connection up gsm
*
* plugins/MobileDataManager (folder)
* plugins/MobileDataManager/MobileDataManager.php (file)
* namespace RaspAP\Plugins\MobileDataManager (namespace)
* class MobileDataManager implements PluginInterface (class)
*
* @description A plugin to manage 4G/LTE modems via ModemManager/NetworkManager
* @author Marc
* @license https://github.com/raspap/raspap-webgui/blob/master/LICENSE
* @see src/RaspAP/Plugins/PluginInterface.php
* @see src/RaspAP/UI/Sidebar.php
*/
namespace RaspAP\Plugins\MobileDataManager;
use RaspAP\Plugins\PluginInterface;
use RaspAP\UI\Sidebar;
class MobileDataManager implements PluginInterface
{
private string $pluginPath;
private string $pluginName;
private string $templateMain;
// Default NM connection profile name
const NM_CON_NAME = 'gsm';
public function __construct(string $pluginPath, string $pluginName)
{
$this->pluginPath = $pluginPath;
$this->pluginName = $pluginName;
$this->templateMain = 'main';
}
/**
* Initializes the plugin and creates a sidebar item
*
* @param Sidebar $sidebar an instance of the Sidebar
*/
public function initialize(Sidebar $sidebar): void
{
$label = _('Mobile Data');
$icon = 'fas fa-signal';
$action = 'plugin__'.$this->getName();
$priority = 55;
$sidebar->addItem($label, $icon, $action, $priority);
}
/**
* Handles a page action by processing inputs and rendering the plugin template
*
* @param string $page the current page route
*/
public function handlePageAction(string $page): bool
{
if (strpos($page, "/plugin__" . $this->getName()) !== 0) {
return false;
}
$status = new \RaspAP\Messages\StatusMessage;
if (!empty($_POST)) {
$this->handlePostActions($status);
}
// Gather data for templates
$modemDetected = $this->isModemDetected();
$modemInfo = $this->getModemInfo();
$modemStatus = $this->extractModemStatus($modemInfo);
$signalInfo = $this->getSignalInfo();
$simInfo = $this->getSimInfo($modemInfo);
$connectionInfo = $this->getConnectionInfo();
$bearerInfo = $this->getBearerInfo($modemInfo);
// Determine service status for the LED indicator
$serviceStatus = 'down';
if ($modemDetected && ($modemStatus['state'] ?? '') === 'connected') {
$serviceStatus = 'up';
} elseif ($modemDetected) {
$serviceStatus = 'idle';
}
$__template_data = [
'title' => _('Mobile Data'),
'description' => _('Manage 4G/LTE modem via ModemManager and NetworkManager'),
'author' => 'Marc',
'uri' => 'https://github.com/Spin42/MobileDataManager',
'icon' => 'fas fa-signal',
'serviceStatus' => $serviceStatus,
'serviceName' => 'ModemManager.service',
'action' => 'plugin__'.$this->getName(),
'pluginName' => $this->getName(),
'modemDetected' => $modemDetected,
'modemInfo' => $modemInfo,
'modemStatus' => $modemStatus,
'signalInfo' => $signalInfo,
'simInfo' => $simInfo,
'connectionInfo' => $connectionInfo,
'bearerInfo' => $bearerInfo,
];
echo $this->renderTemplate($this->templateMain, compact(
"status",
"__template_data"
));
return true;
}
/**
* Renders a template from inside the plugin's template directory
*
* @param string $templateName
* @param array $__data
*/
public function renderTemplate(string $templateName, array $__data = []): string
{
$templateFile = "{$this->pluginPath}/{$this->getName()}/templates/{$templateName}.php";
if (!file_exists($templateFile)) {
return "Template file {$templateFile} not found.";
}
if (!empty($__data)) {
extract($__data);
}
ob_start();
include $templateFile;
return ob_get_clean();
}
/**
* Process POST form submissions
*/
private function handlePostActions(\RaspAP\Messages\StatusMessage $status): void
{
if (isset($_POST['saveConnection'])) {
$this->saveConnectionSettings($status);
} elseif (isset($_POST['connectModem'])) {
$this->connectModem($status);
} elseif (isset($_POST['disconnectModem'])) {
$this->disconnectModem($status);
} elseif (isset($_POST['unlockSim'])) {
$this->unlockSim($status);
} elseif (isset($_POST['deleteConnection'])) {
$this->deleteConnection($status);
}
}
// -----------------------------------------------------------------
// Actions: nmcli connection management
// -----------------------------------------------------------------
/**
* Create or update a GSM connection profile via nmcli.
* This mirrors: nmcli connection add type gsm ifname '*' con-name gsm apn <APN> gsm.initial-eps-bearer-apn <APN>
* The modem attaches to LTE with the initial EPS bearer APN: when it
* differs from the data APN, the network may refuse the data connection.
*/
private function saveConnectionSettings(\RaspAP\Messages\StatusMessage $status): void
{
$apn = trim($_POST['apn'] ?? '');
$conName = trim($_POST['conName'] ?? self::NM_CON_NAME);
$username = trim($_POST['nmUser'] ?? '');
$password = trim($_POST['nmPass'] ?? '');
$pinCode = trim($_POST['pinCode'] ?? '');
if (empty($apn)) {
$status->addMessage('APN is required.', 'danger');
return;
}
// Check if connection already exists
$connectionExists = $this->nmConnectionExists($conName);
if ($connectionExists) {
$cmd = sprintf(
"sudo nmcli connection modify %s gsm.apn %s gsm.initial-eps-bearer-apn %s",
escapeshellarg($conName),
escapeshellarg($apn),
escapeshellarg($apn)
);
if (!empty($username)) {
$cmd .= " gsm.username " . escapeshellarg($username);
} else {
$cmd .= " gsm.username ''";
}
if (!empty($password)) {
$cmd .= " gsm.password " . escapeshellarg($password);
} else {
$cmd .= " gsm.password ''";
}
if (!empty($pinCode)) {
$cmd .= " gsm.pin " . escapeshellarg($pinCode);
} else {
$cmd .= " gsm.pin ''";
}
} else {
$cmd = sprintf(
"sudo nmcli connection add type gsm ifname %s con-name %s apn %s gsm.initial-eps-bearer-apn %s",
escapeshellarg('*'),
escapeshellarg($conName),
escapeshellarg($apn),
escapeshellarg($apn)
);
if (!empty($username)) {
$cmd .= " gsm.username " . escapeshellarg($username);
}
if (!empty($password)) {
$cmd .= " gsm.password " . escapeshellarg($password);
}
if (!empty($pinCode)) {
$cmd .= " gsm.pin " . escapeshellarg($pinCode);
}
}
exec($cmd . " 2>&1", $output, $retval);
if ($retval === 0) {
$verb = $connectionExists ? 'updated' : 'created';
$status->addMessage("Connection profile '{$conName}' {$verb} successfully.", 'success');
} else {
$status->addMessage('Failed to save connection: ' . implode(' ', $output), 'danger');
}
}
/**
* Bring up the GSM connection: nmcli connection up <name>
*/
private function connectModem(\RaspAP\Messages\StatusMessage $status): void
{
$conName = trim($_POST['conName'] ?? self::NM_CON_NAME);
exec("sudo nmcli connection up " . escapeshellarg($conName) . " 2>&1", $output, $retval);
if ($retval === 0) {
$status->addMessage('Mobile data connection activated.', 'success');
} else {
$status->addMessage('Failed to connect: ' . implode(' ', $output), 'danger');
}
}
/**
* Bring down the GSM connection: nmcli connection down <name>
*/
private function disconnectModem(\RaspAP\Messages\StatusMessage $status): void
{
$conName = trim($_POST['conName'] ?? self::NM_CON_NAME);
exec("sudo nmcli connection down " . escapeshellarg($conName) . " 2>&1", $output, $retval);
if ($retval === 0) {
$status->addMessage('Mobile data connection deactivated.', 'success');
} else {
$status->addMessage('Failed to disconnect: ' . implode(' ', $output), 'danger');
}
}
/**
* Delete a GSM connection profile: nmcli connection delete <name>
*/
private function deleteConnection(\RaspAP\Messages\StatusMessage $status): void
{
$conName = trim($_POST['conName'] ?? self::NM_CON_NAME);
exec("sudo nmcli connection delete " . escapeshellarg($conName) . " 2>&1", $output, $retval);
if ($retval === 0) {
$status->addMessage("Connection profile '{$conName}' deleted.", 'success');
} else {
$status->addMessage('Failed to delete connection: ' . implode(' ', $output), 'danger');
}
}
/**
* Unlock SIM card with PIN via mmcli
*/
private function unlockSim(\RaspAP\Messages\StatusMessage $status): void
{
$pin = trim($_POST['simPin'] ?? '');
if (empty($pin) || !preg_match('/^\d{4,8}$/', $pin)) {
$status->addMessage('Invalid SIM PIN. Must be 4-8 digits.', 'danger');
return;
}
$modemIndex = $this->getModemIndex();
if ($modemIndex === null) {
$status->addMessage('No modem detected.', 'danger');
return;
}
exec("sudo mmcli -m {$modemIndex} --pin=" . escapeshellarg($pin) . " 2>&1", $output, $retval);
if ($retval === 0) {
$status->addMessage('SIM unlocked successfully.', 'success');
} else {
$status->addMessage('Failed to unlock SIM: ' . implode(' ', $output), 'danger');
}
}
// -----------------------------------------------------------------
// ModemManager queries (mmcli)
// -----------------------------------------------------------------
/**
* Check if ModemManager detects any modem
*/
public function isModemDetected(): bool
{
return $this->getModemIndex() !== null;
}
/**
* Get the first modem index from: mmcli -L
*/
private function getModemIndex(): ?int
{
exec("mmcli -L 2>/dev/null", $lines, $retval);
if ($retval !== 0) return null;
foreach ($lines as $line) {
if (preg_match('/\/Modem\/(\d+)/', $line, $m)) {
return (int)$m[1];
}
}
return null;
}
/**
* Get full modem info: mmcli -m <index>
* Returns parsed key-value pairs grouped by section.
*/
public function getModemInfo(): array
{
$idx = $this->getModemIndex();
if ($idx === null) return [];
exec("mmcli -m {$idx} 2>/dev/null", $lines);
return $this->parseMmcliOutput($lines);
}
/**
* Extract relevant status fields from modem info
*/
public function extractModemStatus(array $modemInfo): array
{
$fields = [
'state', 'power state', 'access tech',
'signal quality', 'failed reason',
];
$result = [];
foreach ($fields as $field) {
$result[$field] = $modemInfo['Status'][$field] ?? 'N/A';
}
return $result;
}
/**
* Get signal quality details: mmcli -m <index> --signal-get
*/
public function getSignalInfo(): array
{
$idx = $this->getModemIndex();
if ($idx === null) return [];
exec("mmcli -m {$idx} --signal-get 2>/dev/null", $lines);
return $this->parseMmcliOutput($lines);
}
/**
* Get SIM card information: mmcli -i <sim-index>
*/
public function getSimInfo(array $modemInfo): array
{
$simPath = $modemInfo['SIM']['primary sim path'] ?? '';
if (empty($simPath) || $simPath === 'none') {
return ['error' => 'No SIM card detected'];
}
if (preg_match('/\/SIM\/(\d+)/', $simPath, $m)) {
exec("mmcli -i {$m[1]} 2>/dev/null", $lines);
return $this->parseMmcliOutput($lines);
}
return [];
}
/**
* Get bearer (active data session) information: mmcli -b <bearer-index>
*/
public function getBearerInfo(array $modemInfo): array
{
// Bearer paths may be listed as space-separated or in a "paths" key
$bearerPaths = $modemInfo['Bearers']['paths'] ?? '';
if (empty($bearerPaths) || strpos($bearerPaths, 'none') !== false) {
return [];
}
if (preg_match('/\/Bearer\/(\d+)/', $bearerPaths, $m)) {
exec("mmcli -b {$m[1]} 2>/dev/null", $lines);
return $this->parseMmcliOutput($lines);
}
return [];
}
// -----------------------------------------------------------------
// NetworkManager queries (nmcli)
// -----------------------------------------------------------------
/**
* Check if a NM connection profile with the given name exists
*/
private function nmConnectionExists(string $name): bool
{
exec("nmcli -t -f NAME connection show 2>/dev/null", $lines);
return in_array($name, $lines);
}
/**
* Get current NM GSM connection profile details
*/
public function getConnectionInfo(): array
{
$result = [
'exists' => false,
'active' => false,
'name' => '',
'apn' => '',
'username' => '',
'pin' => '',
'device' => '',
'ip4' => '',
'gw4' => '',
'dns4' => '',
];
// Find the first gsm-type connection
exec("nmcli -t -f NAME,TYPE connection show 2>/dev/null", $lines);
foreach ($lines as $line) {
$parts = explode(':', $line);
if (count($parts) >= 2 && $parts[1] === 'gsm') {
$result['exists'] = true;
$result['name'] = $parts[0];
break;
}
}
if (!$result['exists']) {
return $result;
}
$conName = escapeshellarg($result['name']);
// Read APN, username, pin
exec("nmcli -t connection show {$conName} 2>/dev/null", $detailLines);
foreach ($detailLines as $line) {
if (strpos($line, 'gsm.apn:') === 0) {
$result['apn'] = substr($line, strlen('gsm.apn:'));
} elseif (strpos($line, 'gsm.username:') === 0) {
$result['username'] = substr($line, strlen('gsm.username:'));
} elseif (strpos($line, 'gsm.pin:') === 0) {
$result['pin'] = substr($line, strlen('gsm.pin:'));
}
}
// Check if active and get device
exec("nmcli -t -f NAME,DEVICE connection show --active 2>/dev/null", $activeLines);
foreach ($activeLines as $line) {
$parts = explode(':', $line);
if (count($parts) >= 2 && $parts[0] === $result['name']) {
$result['active'] = true;
$result['device'] = $parts[1];
break;
}
}
// If active, get IP details from the device
if ($result['active'] && !empty($result['device'])) {
$dev = escapeshellarg($result['device']);
exec("nmcli -t -f IP4.ADDRESS,IP4.GATEWAY,IP4.DNS device show {$dev} 2>/dev/null", $ipLines);
foreach ($ipLines as $line) {
if (strpos($line, 'IP4.ADDRESS') === 0) {
$result['ip4'] = preg_replace('/^IP4\.ADDRESS\[\d+\]:/', '', $line);
} elseif (strpos($line, 'IP4.GATEWAY') === 0) {
$result['gw4'] = preg_replace('/^IP4\.GATEWAY:/', '', $line);
} elseif (strpos($line, 'IP4.DNS') === 0) {
$dns = preg_replace('/^IP4\.DNS\[\d+\]:/', '', $line);
$result['dns4'] = empty($result['dns4']) ? $dns : $result['dns4'] . ', ' . $dns;
}
}
}
return $result;
}
// -----------------------------------------------------------------
// Parsing helpers
// -----------------------------------------------------------------
/**
* Parse mmcli tabular output into a nested associative array.
*
* mmcli outputs sections like:
* --------------------------------
* General | path: /org/freedesktop/ModemManager1/Modem/0
* | device id: abc123
* --------------------------------
* Status | state: connected
*
* @param array $lines raw output lines
* @return array nested [Section => [key => value]]
*/
private function parseMmcliOutput(array $lines): array
{
$result = [];
$currentSection = 'General';
foreach ($lines as $line) {
// mmcli colours some values (e.g. the state) with ANSI escapes
$line = preg_replace('/\x1b\[[0-9;]*[A-Za-z]/', '', $line);
if (preg_match('/^[\s\-]+$/', $line) || trim($line) === '') {
continue;
}
if (preg_match('/^\s*([^|]*?)\s*\|\s*(.+)$/', $line, $m)) {
$sectionPart = trim($m[1]);
$kvPart = trim($m[2]);
if (!empty($sectionPart)) {
$currentSection = $sectionPart;
}
// Split on first colon only
if (preg_match('/^(.+?):\s*(.*)$/', $kvPart, $kv)) {
$key = trim($kv[1]);
$value = trim($kv[2]);
$result[$currentSection][$key] = $value;
}
}
}
return $result;
}
/**
* Returns an abbreviated class name (required by PluginManager)
*/
public static function getName(): string
{
return basename(str_replace('\\', '/', static::class));
}
}