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
|
/*
* Copyright (C) 2014 ~ 2018 Deepin Technology Co., Ltd.
*
* Author: jouyouyun <jouyouwen717@gmail.com>
*
* 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 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mobileprovider
import (
"encoding/xml"
"fmt"
"io/ioutil"
"strings"
"sync"
)
const mobileServiceProvidersXMLFile = "/usr/share/mobile-broadband-provider-info/serviceproviders.xml"
var (
errCountryNotFound = fmt.Errorf("country not found")
errProviderNotFound = fmt.Errorf("provider not found")
errGSMNotFound = fmt.Errorf("gsm not found")
errCDMANotFound = fmt.Errorf("cdma not found")
errAPNNotFound = fmt.Errorf("apn not found")
errPlanNotFound = fmt.Errorf("plan not found")
)
var mobileProviderDatabase *MobileServiceProviderDatabase
var mobileProviderDatabaseLock sync.Mutex
// GetMobileProviderDatabase return mobile service provider's database
// that marshaled from serviceproviders.xml.
func GetMobileProviderDatabase() (*MobileServiceProviderDatabase, error) {
mobileProviderDatabaseLock.Lock()
defer mobileProviderDatabaseLock.Unlock()
if mobileProviderDatabase != nil {
return mobileProviderDatabase, nil
}
mobileProviderDatabase = &MobileServiceProviderDatabase{}
xmlContent, err := ioutil.ReadFile(mobileServiceProvidersXMLFile)
if err != nil {
return mobileProviderDatabase, err
}
err = xml.Unmarshal(xmlContent, mobileProviderDatabase)
return mobileProviderDatabase, err
}
// GetAllCountryCode return all country code that provide mobile
// service.
func GetAllCountryCode() (codeList []string, err error) {
database, err := GetMobileProviderDatabase()
if err != nil {
return
}
for _, c := range database.Countries {
codeList = append(codeList, strings.ToUpper(c.Code))
}
return
}
// GetProviders return all providers in target country.
func GetProviders(countryCode string) (providers []*Provider, err error) {
database, err := GetMobileProviderDatabase()
if err != nil {
return
}
found := false
for _, c := range database.Countries {
if strings.EqualFold(c.Code, countryCode) {
found = true
providers = c.Providers
break
}
}
if !found {
err = errCountryNotFound
}
return
}
// GetProvider return the provider information that matched the provided country
// code and provider name.
func GetProvider(countryCode, providerName string) (provider *Provider, err error) {
providers, err := GetProviders(countryCode)
if err != nil {
return
}
found := false
for _, p := range providers {
if p.Name.Body == providerName {
found = true
provider = p
break
}
}
if !found {
err = errProviderNotFound
}
return
}
// GetGSM return the gsm information that matched the provided country
// code and provider name.
func GetGSM(countryCode, providerName string) (gsm *GSM, err error) {
provider, err := GetProvider(countryCode, providerName)
if err != nil {
return
}
gsm = provider.GSM
if gsm == nil {
err = errGSMNotFound
}
return
}
// GetCDMA return the cdma information that match the provided
// country code and provider name.
func GetCDMA(countryCode, providerName string) (cdma *CDMA, err error) {
provider, err := GetProvider(countryCode, providerName)
if err != nil {
return
}
cdma = provider.CDMA
if cdma == nil {
err = errCDMANotFound
}
return
}
// GetGSMForNetworkID return the gsm information that match the mcc
// and mnc.
func GetGSMForNetworkID(mcc, mnc string) (gsm *GSM, err error) {
database, err := GetMobileProviderDatabase()
if err != nil {
return
}
found := false
outside:
for _, c := range database.Countries {
for _, p := range c.Providers {
if p.GSM != nil {
for _, id := range p.GSM.NetworkID {
if id.MCC == mcc && id.MNC == mnc {
found = true
gsm = p.GSM
break outside
}
}
}
}
}
if !found {
err = errGSMNotFound
}
return
}
// GetAPN return the apn information that match the provided
// country code, provider name and apn value.
func GetAPN(countryCode, providerName, apnValue, apnUsageType string) (apn *APN, err error) {
gsm, err := GetGSM(countryCode, providerName)
if err != nil {
return
}
found := false
for _, a := range gsm.APN {
if a.Value == apnValue && GetAPNUsageType(a) == apnUsageType {
found = true
apn = a
}
}
if !found {
err = errAPNNotFound
}
return
}
// GetAPNName return apn's default name, if not exist, return empty
// string.
func GetAPNName(apn *APN) (name string) {
if len(apn.Name) == 0 {
return
}
name = apn.Name[0].Body
return
}
// GetAPNUsageType return apn's usage type, if not exist, return empty
// string.
func GetAPNUsageType(apn *APN) (usageType string) {
if apn.Usage == nil {
return
}
usageType = apn.Usage.Type
return
}
// GetProviderNames return all provider names in target country.
func GetProviderNames(countryCode string) (names []string, err error) {
providers, err := GetProviders(countryCode)
if err != nil {
return
}
for _, p := range providers {
names = append(names, p.Name.Body)
}
return
}
// GetDefaultProvider return the default provider in target country,
// usually is the first provider.
func GetDefaultProvider(countryCode string) (defaultProvider string, err error) {
providers, err := GetProviderNames(countryCode)
if err != nil {
return
}
if len(providers) > 0 {
defaultProvider = providers[0]
} else {
err = errProviderNotFound
}
return
}
|