Skip to content

Commit 841173e

Browse files
author
Kimmo Vaisanen
committed
Cellular: Refactor APN db implementation to reduce memory usage
Reduce memory usage by moving actual APN database into single object file instead of having it defined in header as a static. Also implementation is now only enabled if either cellular.use-apn-lookup or ppp-cell-iface.apn-lookup is enabled.
1 parent 3e6f5eb commit 841173e

File tree

3 files changed

+204
-150
lines changed

3 files changed

+204
-150
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ublox, ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/* ----------------------------------------------------------------
18+
APN stands for Access Point Name, a setting on your modem or phone
19+
that identifies an external network your phone can access for data
20+
(e.g. 3G or 4G Internet service on your phone).
21+
22+
The APN settings can be forced when calling the join function.
23+
Below is a list of known APNs that us used if no apn config
24+
is forced. This list could be extended by other settings.
25+
26+
For further reading:
27+
wiki apn: http://en.wikipedia.org/wiki/Access_Point_Name
28+
wiki mcc/mnc: http://en.wikipedia.org/wiki/Mobile_country_code
29+
google: https://www.google.de/search?q=APN+list
30+
---------------------------------------------------------------- */
31+
32+
#include "APN_db.h"
33+
34+
#if (MBED_CONF_CELLULAR_USE_APN_LOOKUP || (NSAPI_PPP_AVAILABLE && MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP))
35+
36+
/**
37+
* Helper to generate the APN string
38+
*/
39+
#define _APN(apn,username,password) apn "\0" username "\0" password "\0"
40+
41+
/**
42+
* APN lookup struct
43+
*/
44+
typedef struct {
45+
const char *mccmnc; /**< mobile country code (MCC) and mobile network code MNC */
46+
const char *cfg; /**< APN configuartion string, use _APN macro to generate */
47+
} APN_t;
48+
49+
/**
50+
* Default APN settings used by many networks
51+
*/
52+
static const char *apndef = _APN("internet",,);
53+
54+
/**
55+
* List of special APNs for different network operators.
56+
*
57+
* No need to add default, "internet" will be used as a default if no entry matches.
58+
* The APN without username/password have to be listed first.
59+
*/
60+
61+
static const APN_t apnlut[] = {
62+
// MCC Country
63+
// { /* Operator */ "MCC-MNC[,MNC]" _APN(APN,USERNAME,PASSWORD) },
64+
// MCC must be 3 digits
65+
// MNC must be either 2 or 3 digits
66+
// MCC must be separated by '-' from MNC, multiple MNC can be separated by ','
67+
68+
// 232 Austria - AUT
69+
{ /* T-Mobile */ "232-03", _APN("m2m.business",,) },
70+
71+
// 460 China - CN
72+
{ /* CN Mobile */"460-00", _APN("cmnet",,)
73+
_APN("cmwap",,)
74+
},
75+
{ /* Unicom */ "460-01", _APN("3gnet",,)
76+
_APN("uninet", "uninet", "uninet")
77+
},
78+
79+
// 262 Germany - DE
80+
{ /* T-Mobile */ "262-01", _APN("internet.t-mobile", "t-mobile", "tm") },
81+
{ /* T-Mobile */ "262-02,06",
82+
_APN("m2m.business",,)
83+
},
84+
85+
// 222 Italy - IT
86+
{ /* TIM */ "222-01", _APN("ibox.tim.it",,) },
87+
{ /* Vodafone */ "222-10", _APN("web.omnitel.it",,) },
88+
{ /* Wind */ "222-88", _APN("internet.wind.biz",,) },
89+
90+
// 440 Japan - JP
91+
{ /* Softbank */ "440-04,06,20,40,41,42,43,44,45,46,47,48,90,91,92,93,94,95"
92+
",96,97,98"
93+
_APN("open.softbank.ne.jp", "opensoftbank", "ebMNuX1FIHg9d3DA")
94+
_APN("smile.world", "dna1trop", "so2t3k3m2a")
95+
},
96+
{ /* NTTDoCoMo */"440-09,10,11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,"
97+
"28,29,30,31,32,33,34,35,36,37,38,39,58,59,60,61,62,63,"
98+
"64,65,66,67,68,69,87,99",
99+
_APN("bmobilewap",,) /*BMobile*/
100+
_APN("mpr2.bizho.net", "Mopera U",) /* DoCoMo */
101+
_APN("bmobile.ne.jp", "bmobile@wifi2", "bmobile") /*BMobile*/
102+
},
103+
104+
// 204 Netherlands - NL
105+
{ /* Vodafone */ "204-04", _APN("public4.m2minternet.com",,) },
106+
107+
// 293 Slovenia - SI
108+
{ /* Si.mobil */ "293-40", _APN("internet.simobil.si",,) },
109+
{ /* Tusmobil */ "293-70", _APN("internet.tusmobil.si",,) },
110+
111+
// 240 Sweden SE
112+
{ /* Telia */ "240-01", _APN("online.telia.se",,) },
113+
{ /* Telenor */ "240-06,08",
114+
_APN("services.telenor.se",,)
115+
},
116+
{ /* Tele2 */ "240-07", _APN("mobileinternet.tele2.se",,) },
117+
118+
// 228 Switzerland - CH
119+
{ /* Swisscom */ "228-01", _APN("gprs.swisscom.ch",,) },
120+
{ /* Orange */ "228-03", _APN("internet",,) /* contract */
121+
_APN("click",,) /* pre-pay */
122+
},
123+
124+
// 234 United Kingdom - GB
125+
{ /* O2 */ "234-02,10,11",
126+
_APN("mobile.o2.co.uk", "faster", "web") /* contract */
127+
_APN("mobile.o2.co.uk", "bypass", "web") /* pre-pay */
128+
_APN("payandgo.o2.co.uk", "payandgo", "payandgo")
129+
},
130+
{ /* Vodafone */ "234-15", _APN("internet", "web", "web") /* contract */
131+
_APN("pp.vodafone.co.uk", "wap", "wap") /* pre-pay */
132+
},
133+
{ /* Three */ "234-20", _APN("three.co.uk",,) },
134+
{ /* Jersey */ "234-50", _APN("jtm2m",,) /* as used on u-blox C030 U201 boards */ },
135+
136+
// 310 United States of America - US
137+
{ /* T-Mobile */ "310-026,260,490",
138+
_APN("epc.tmobile.com",,)
139+
_APN("fast.tmobile.com",,) /* LTE */
140+
},
141+
{ /* AT&T */ "310-030,150,170,260,410,560,680",
142+
_APN("phone",,)
143+
_APN("wap.cingular", "[email protected]", "CINGULAR1")
144+
_APN("isp.cingular", "[email protected]", "CINGULAR1")
145+
},
146+
147+
// 901 International - INT
148+
{ /* Transatel */ "901-37", _APN("netgprs.com", "tsl", "tsl") },
149+
};
150+
151+
const char *apnconfig(const char *imsi)
152+
{
153+
const char *config = NULL;
154+
if (imsi && *imsi) {
155+
// many carriers use internet without username and password, os use this as default
156+
// now try to lookup the setting for our table
157+
for (size_t i = 0; i < sizeof(apnlut) / sizeof(*apnlut) && !config; i ++) {
158+
const char *p = apnlut[i].mccmnc;
159+
// check the MCC
160+
if ((0 == memcmp(imsi, p, 3))) {
161+
p += 3;
162+
// check all the MNC, MNC length can be 2 or 3 digits
163+
while (((p[0] == '-') || (p[0] == ',')) &&
164+
(p[1] >= '0') && (p[1] <= '9') &&
165+
(p[2] >= '0') && (p[2] <= '9') && !config) {
166+
int l = ((p[3] >= '0') && (p[3] <= '9')) ? 3 : 2;
167+
if (0 == memcmp(imsi + 3, p + 1, l)) {
168+
config = apnlut[i].cfg;
169+
}
170+
p += 1 + l;
171+
}
172+
}
173+
}
174+
}
175+
// use default if not found
176+
if (!config) {
177+
config = apndef;
178+
}
179+
return config;
180+
}
181+
182+
#endif

features/cellular/framework/common/APN_db.h

Lines changed: 10 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
wiki mcc/mnc: http://en.wikipedia.org/wiki/Mobile_country_code
2929
google: https://www.google.de/search?q=APN+list
3030
---------------------------------------------------------------- */
31+
#ifndef APN_DB_H_
32+
#define APN_DB_H_
3133

32-
/**
33-
* Helper to generate the APN string
34-
*/
35-
#define _APN(apn,username,password) apn "\0" username "\0" password "\0"
34+
#if (MBED_CONF_CELLULAR_USE_APN_LOOKUP || (NSAPI_PPP_AVAILABLE && MBED_CONF_PPP_CELL_IFACE_APN_LOOKUP))
35+
36+
#include <string.h>
3637

3738
/**
3839
* Helper to extract a field from the cfg string
@@ -42,147 +43,12 @@
4243
cfg += strlen(cfg) + 1
4344

4445
/**
45-
* APN lookup struct
46-
*/
47-
typedef struct {
48-
const char *mccmnc; /**< mobile country code (MCC) and mobile network code MNC */
49-
const char *cfg; /**< APN configuartion string, use _APN macro to generate */
50-
} APN_t;
51-
52-
/**
53-
* Default APN settings used by many networks
54-
*/
55-
static const char *apndef = _APN("internet",,);
56-
57-
/**
58-
* List of special APNs for different network operators.
46+
* Configuring APN by extraction from IMSI and matching the table.
5947
*
60-
* No need to add default, "internet" will be used as a default if no entry matches.
61-
* The APN without username/password have to be listed first.
48+
* @param imsi string containing IMSI
6249
*/
50+
const char *apnconfig(const char *imsi);
6351

64-
static const APN_t apnlut[] = {
65-
// MCC Country
66-
// { /* Operator */ "MCC-MNC[,MNC]" _APN(APN,USERNAME,PASSWORD) },
67-
// MCC must be 3 digits
68-
// MNC must be either 2 or 3 digits
69-
// MCC must be separated by '-' from MNC, multiple MNC can be separated by ','
70-
71-
// 232 Austria - AUT
72-
{ /* T-Mobile */ "232-03", _APN("m2m.business",,) },
73-
74-
// 460 China - CN
75-
{ /* CN Mobile */"460-00", _APN("cmnet",,)
76-
_APN("cmwap",,)
77-
},
78-
{ /* Unicom */ "460-01", _APN("3gnet",,)
79-
_APN("uninet", "uninet", "uninet")
80-
},
81-
82-
// 262 Germany - DE
83-
{ /* T-Mobile */ "262-01", _APN("internet.t-mobile", "t-mobile", "tm") },
84-
{ /* T-Mobile */ "262-02,06",
85-
_APN("m2m.business",,)
86-
},
52+
#endif
8753

88-
// 222 Italy - IT
89-
{ /* TIM */ "222-01", _APN("ibox.tim.it",,) },
90-
{ /* Vodafone */ "222-10", _APN("web.omnitel.it",,) },
91-
{ /* Wind */ "222-88", _APN("internet.wind.biz",,) },
92-
93-
// 440 Japan - JP
94-
{ /* Softbank */ "440-04,06,20,40,41,42,43,44,45,46,47,48,90,91,92,93,94,95"
95-
",96,97,98"
96-
_APN("open.softbank.ne.jp", "opensoftbank", "ebMNuX1FIHg9d3DA")
97-
_APN("smile.world", "dna1trop", "so2t3k3m2a")
98-
},
99-
{ /* NTTDoCoMo */"440-09,10,11,12,13,14,15,16,17,18,19,21,22,23,24,25,26,27,"
100-
"28,29,30,31,32,33,34,35,36,37,38,39,58,59,60,61,62,63,"
101-
"64,65,66,67,68,69,87,99",
102-
_APN("bmobilewap",,) /*BMobile*/
103-
_APN("mpr2.bizho.net", "Mopera U",) /* DoCoMo */
104-
_APN("bmobile.ne.jp", "bmobile@wifi2", "bmobile") /*BMobile*/
105-
},
106-
107-
// 204 Netherlands - NL
108-
{ /* Vodafone */ "204-04", _APN("public4.m2minternet.com",,) },
109-
110-
// 293 Slovenia - SI
111-
{ /* Si.mobil */ "293-40", _APN("internet.simobil.si",,) },
112-
{ /* Tusmobil */ "293-70", _APN("internet.tusmobil.si",,) },
113-
114-
// 240 Sweden SE
115-
{ /* Telia */ "240-01", _APN("online.telia.se",,) },
116-
{ /* Telenor */ "240-06,08",
117-
_APN("services.telenor.se",,)
118-
},
119-
{ /* Tele2 */ "240-07", _APN("mobileinternet.tele2.se",,) },
120-
121-
// 228 Switzerland - CH
122-
{ /* Swisscom */ "228-01", _APN("gprs.swisscom.ch",,) },
123-
{ /* Orange */ "228-03", _APN("internet",,) /* contract */
124-
_APN("click",,) /* pre-pay */
125-
},
126-
127-
// 234 United Kingdom - GB
128-
{ /* O2 */ "234-02,10,11",
129-
_APN("mobile.o2.co.uk", "faster", "web") /* contract */
130-
_APN("mobile.o2.co.uk", "bypass", "web") /* pre-pay */
131-
_APN("payandgo.o2.co.uk", "payandgo", "payandgo")
132-
},
133-
{ /* Vodafone */ "234-15", _APN("internet", "web", "web") /* contract */
134-
_APN("pp.vodafone.co.uk", "wap", "wap") /* pre-pay */
135-
},
136-
{ /* Three */ "234-20", _APN("three.co.uk",,) },
137-
{ /* Jersey */ "234-50", _APN("jtm2m",,) /* as used on u-blox C030 U201 boards */ },
138-
139-
// 310 United States of America - US
140-
{ /* T-Mobile */ "310-026,260,490",
141-
_APN("epc.tmobile.com",,)
142-
_APN("fast.tmobile.com",,) /* LTE */
143-
},
144-
{ /* AT&T */ "310-030,150,170,260,410,560,680",
145-
_APN("phone",,)
146-
_APN("wap.cingular", "[email protected]", "CINGULAR1")
147-
_APN("isp.cingular", "[email protected]", "CINGULAR1")
148-
},
149-
150-
// 901 International - INT
151-
{ /* Transatel */ "901-37", _APN("netgprs.com", "tsl", "tsl") },
152-
};
153-
154-
/**
155-
* Configuring APN by extraction from IMSI and matching the table.
156-
*
157-
* @param imsi strinf containing IMSI
158-
*/
159-
inline const char *apnconfig(const char *imsi)
160-
{
161-
const char *config = NULL;
162-
if (imsi && *imsi) {
163-
// many carriers use internet without username and password, os use this as default
164-
// now try to lookup the setting for our table
165-
for (size_t i = 0; i < sizeof(apnlut) / sizeof(*apnlut) && !config; i ++) {
166-
const char *p = apnlut[i].mccmnc;
167-
// check the MCC
168-
if ((0 == memcmp(imsi, p, 3))) {
169-
p += 3;
170-
// check all the MNC, MNC length can be 2 or 3 digits
171-
while (((p[0] == '-') || (p[0] == ',')) &&
172-
(p[1] >= '0') && (p[1] <= '9') &&
173-
(p[2] >= '0') && (p[2] <= '9') && !config) {
174-
int l = ((p[3] >= '0') && (p[3] <= '9')) ? 3 : 2;
175-
if (0 == memcmp(imsi + 3, p + 1, l)) {
176-
config = apnlut[i].cfg;
177-
}
178-
p += 1 + l;
179-
}
180-
}
181-
}
182-
}
183-
// use default if not found
184-
if (!config) {
185-
config = apndef;
186-
}
187-
return config;
188-
}
54+
#endif // #define APN_DB_H_

0 commit comments

Comments
 (0)