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
#[macro_use]
extern crate serde_derive;

use std::collections::BTreeMap;

pub type Countries = BTreeMap<MCC, Country>;
pub type EARFCN = u16;
#[allow(non_camel_case_types)]
pub type eNBId = u32;
pub type LocationId = u16;
pub type Locations = BTreeMap<LocationId, Location>;
pub type LTECellGlobalId = u64;
pub type LTECellId = u32;
pub type LTECells = BTreeMap<LTECellGlobalId, LTECell>;
pub type LTECellShortId = u8;
pub type MCC = u16;
pub type MNC = u16;
pub type NetworkId = u32;
pub type Networks = BTreeMap<NetworkId, Network>;
pub type PCI = u16;

/// Represents a country.
#[derive(Serialize, Deserialize)]
pub struct Country {
    /// The two-letter country code representing the country. E.g. `NZ` for New Zealand. See the
    /// Wikipedia article about [ISO 3166-1 alpha-2][1] country codes for more information and a
    /// list of valid codes.
    /// [1]: https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
    pub code: String,
    /// The three-digit Mobile Country Code (MCC) representing the country. E.g. `530` for New
    /// Zealand. Note that it is possible for a country to have more than one MCC representing it.
    /// See the Wikipedia article about [Mobile Country Codes][1] for more information and a list of
    /// valid codes.
    /// [1]: https://en.wikipedia.org/wiki/Mobile_country_code
    pub mcc: MCC,
    /// The name of the country. E.g. `New Zealand`.
    pub name: String,
}

/// Represents a location.
#[derive(Serialize, Deserialize)]
pub struct Location {
    /// An identifier for this location.
    pub id: LocationId,
    /// A name for this location.
    pub name: String,
}

/// Represents a LTE cell.
#[derive(Serialize, Deserialize)]
pub struct LTECell {
    pub earfcn: Option<EARFCN>,
    pub id: LTECellId,
    pub location_id: Option<LocationId>,
    pub mcc: MCC,
    pub mnc: MNC,
    pub pci: Option<PCI>,
}

/// Represents a network.
#[derive(Serialize, Deserialize)]
pub struct Network {
    /// The three-digit Mobile Country Code (MCC) representing the country this network is located
    /// in. See [wireless::Country.mcc](struct.Country.html#structfield.mcc) for more information.
    pub mcc: MCC,
    /// The three-digit Mobile Network Code (MNC) representing this network within its parent Mobile
    /// Country Code (MCC). E.g. `240` for 2degrees within the MCC `530` for New Zealand (usually
    /// formatted as `530-24` with a two-digit MNC omitting the third digit). See the Wikipedia
    /// article about [Mobile Country Codes][1] for more information and a list of valid codes.
    /// [1]: https://en.wikipedia.org/wiki/Mobile_country_code
    pub mnc: MNC,
    /// The name of the network. E.g. `2degrees`.
    pub name: String,
}

impl LTECell {
    pub fn short_cid(&self) -> LTECellShortId {
        (self.id & (2 ^ 8 - 1)) as LTECellShortId
    }
    pub fn global_id(&self) -> LTECellGlobalId {
        Network::generate_id(self.mcc, self.mnc) as LTECellGlobalId + self.id as LTECellGlobalId
    }
    pub fn enb_id(&self) -> eNBId {
        self.id >> 8
    }
}

impl Network {
    /// Derive a 24-bit global identifier from the given Mobile Country Code (MCC) and Mobile
    /// Network Code (MNC). The MCC makes up the first 12 bits and the MNC makes up the last 12
    /// bits. The identifier is useful as a key value in a key-value collection.
    pub fn generate_id(mcc: MCC, mnc: MNC) -> NetworkId {
        ((mcc as NetworkId) << 12) + (mnc as NetworkId)
    }
    /// Derive a global identifier for this network. See [generate_id()](#method.generate_id) for
    /// more information.
    pub fn id(&self) -> NetworkId {
        Network::generate_id(self.mcc, self.mnc)
    }
}