summaryrefslogtreecommitdiffstats
path: root/lib/network/connection/cellular.pm
blob: 308448088808a39aa42322374fe49fdf59230dfa (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
package network::connection::cellular;

use base qw(network::connection::ppp);

use strict;
use common;

my $cellular_d = "/etc/sysconfig/network-scripts/cellular.d";

sub get_providers {
    # manually-defined providers
    require network::connection::providers::cellular;
    # providers imported from mobile-broadband-provider-info
    require network::connection::providers::cellular_extra;
    # combine custom providers with m.b.p.i. imported ones,
    # filtering out CDMA-only providers which we do not support for now
    my %providers = (
        %network::connection::providers::cellular::data,
        grep_each { !$::b->{cdma} } %network::connection::providers::cellular_extra::data
    );
    (\%providers, '|');
}

sub get_cellular_settings_file {
    my ($self) = @_;
    my $network = $self->get_selected_network or return;
    $::prefix . $cellular_d . '/' . $network->{name};
}

sub load_cellular_settings {
    my ($self) = @_;
    my $file = $self->get_cellular_settings_file or return;
    -f $file && { getVarsFromSh($file) };
}

sub network_is_configured {
    my ($self, $_network) = @_;
    #- only one network is supported, assume it is configured if settings are available
    defined($self->load_cellular_settings);
}

sub write_cellular_settings {
    my ($self) = @_;
    my $file = $self->get_cellular_settings_file or return;
    setVarsInShMode($file, 0600, { map { (uc($_) => $self->{access}{$_}) } qw(login password apn) });
}

sub guess_apn_from_chat {
    my ($self) = @_;
    my $chat = cat_($::prefix . $self->get_chat_file);
    $chat =~ /\bAT\+CGDCONT=\d+,"IP","([^"]+)"/ && $1;
}

sub guess_provider_settings {
    my ($self) = @_;
    my $settings = $self->load_cellular_settings;
    my $apn = $settings && $settings->{APN} || $self->guess_apn_from_chat;
    if ($apn) {
        my @providers_data = $self->get_providers;
        $self->{provider_name} ||= find { $providers_data[0]{$_}{apn} eq $apn } keys %{$providers_data[0]};
        return;
    }
    $self->SUPER::guess_provider_settings;
}

sub guess_access_settings {
    my ($self, $o_provider_only) = @_;
    my $settings = !$o_provider_only && $self->load_cellular_settings || {};
    $self->{access}{$_} = $settings->{uc($_)} || $self->{provider}{$_} foreach qw(login password apn);
}

sub get_access_settings {
   my ($self) = @_;
   [
       { label => N("Access Point Name"), val => \$self->{access}{apn} },
       @{$self->SUPER::get_access_settings},
   ];
}

sub set_ppp_settings {
    my ($self) = @_;
    $self->{access}{use_chat} = 1;
    $self->{access}{dial_number} = !$self->{access}{no_dial} && "*99***$self->{access}{cid}#";
}

sub write_settings {
    my ($self) = @_;
    $self->write_cellular_settings;
    $self->set_ppp_settings;
    $self->SUPER::write_settings;
}

sub apply_network_selection {
    my ($self) = @_;
    $self->set_ppp_settings;
    $self->write_ppp_settings;
}

1;