Mbed OS Reference
Loading...
Searching...
No Matches
CellularContext.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, Arm Limited and affiliates.
3 * SPDX-License-Identifier: Apache-2.0
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17#ifndef _CELLULARCONTEXT_H_
18#define _CELLULARCONTEXT_H_
19
21#include "netsocket/CellularInterface.h"
22#include "CellularDevice.h"
23#include "CellularUtil.h"
25#include "PinNames.h"
26
27/** @file CellularContext.h
28 * @brief Cellular PDP context class
29 *
30 */
31
32namespace mbed {
33
34/**
35 * @defgroup connectivity-public-api Connectivity
36 * @ingroup mbed-os-public
37 * @{
38
39 * @defgroup Cellular Cellular
40 * @ingroup connectivity-public-api
41 * @{
42 */
43
44/// CellularContext is CellularInterface/NetworkInterface with extensions for cellular connectivity
46
47public:
48
49 // max simultaneous PDP contexts active
50 static const int PDP_CONTEXT_COUNT = 4;
51
52 /* authentication type when activating or modifying the pdp context */
53 enum AuthenticationType {
54 NOAUTH = 0,
55 PAP,
56 CHAP,
57 AUTOMATIC
58 };
59
60 /* whether the additional exception reports are allowed to be sent when the maximum uplink rate is reached */
61 enum RateControlExceptionReports {
62 NotAllowedToBeSent = 0,
63 AllowedToBeSent
64 };
65
66 /* specifies the time unit to be used for the maximum uplink rate */
67 enum RateControlUplinkTimeUnit {
68 Unrestricted = 0,
69 Minute,
70 Hour,
71 Day,
72 Week
73 };
74
75 /// PDP Context information
77 char apn[MAX_ACCESSPOINT_NAME_LENGTH + 1];
78 char local_addr[MAX_IPV6_ADDR_IN_IPV4LIKE_DOTTED_FORMAT + 1];
79 char local_subnet_mask[MAX_IPV6_ADDR_IN_IPV4LIKE_DOTTED_FORMAT + 1];
80 char gateway_addr[MAX_IPV6_ADDR_IN_IPV4LIKE_DOTTED_FORMAT + 1];
81 char dns_primary_addr[MAX_IPV6_ADDR_IN_IPV4LIKE_DOTTED_FORMAT + 1];
82 char dns_secondary_addr[MAX_IPV6_ADDR_IN_IPV4LIKE_DOTTED_FORMAT + 1];
83 char p_cscf_prim_addr[MAX_IPV6_ADDR_IN_IPV4LIKE_DOTTED_FORMAT + 1];
84 char p_cscf_sec_addr[MAX_IPV6_ADDR_IN_IPV4LIKE_DOTTED_FORMAT + 1];
85 int cid;
86 int bearer_id;
87 int im_signalling_flag;
88 int lipa_indication;
89 int ipv4_mtu;
90 int wlan_offload;
91 int local_addr_ind;
92 int non_ip_mtu;
93 int serving_plmn_rate_control_value;
95
97 {
98 apn[0] = '\0';
99 local_addr[0] = '\0';
100 local_subnet_mask[0] = '\0';
101 gateway_addr[0] = '\0';
102 dns_primary_addr[0] = '\0';
103 dns_secondary_addr[0] = '\0';
104 p_cscf_prim_addr[0] = '\0';
105 p_cscf_sec_addr[0] = '\0';
106 cid = -1;
107 bearer_id = -1;
108 im_signalling_flag = -1;
109 lipa_indication = -1;
110 ipv4_mtu = -1;
111 wlan_offload = -1;
112 local_addr_ind = -1;
113 non_ip_mtu = -1;
114 serving_plmn_rate_control_value = -1;
115 next = NULL;
116 }
117 };
119
120 // pointer for next item when used as a linked list
121 CellularContext *_next;
122protected:
123 // friend of CellularDevice, so it's the only way to close or delete this class.
124 friend class CellularDevice;
126 virtual ~CellularContext()
127 {
128#if !NSAPI_PPP_AVAILABLE
129 if (_stack) {
130 delete _stack;
131 }
132#endif
133 }
134
135public: // from NetworkInterface
136 virtual nsapi_error_t set_blocking(bool blocking) = 0;
137 virtual NetworkStack *get_stack() = 0;
139
140 /** Register callback for status reporting.
141 *
142 * The specified status callback function is called on the network, and the cellular device status changes.
143 * The parameters on the callback are the event type and event type dependent reason parameter.
144 *
145 * @remark deleting CellularDevice/CellularContext in callback is not allowed.
146 * @remark Allocating/adding lots of traces not recommended as callback is called mostly from State machines thread which
147 * is now 2048. You can change to main thread for example via EventQueue.
148 *
149 * @param status_cb The callback for status changes.
150 */
151 virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) = 0;
152 virtual nsapi_error_t connect() = 0;
154
155 // from CellularInterface
156 virtual void set_plmn(const char *plmn) = 0;
157 virtual void set_sim_pin(const char *sim_pin) = 0;
158 virtual nsapi_error_t connect(const char *sim_pin, const char *apn = 0, const char *uname = 0,
159 const char *pwd = 0) = 0;
160 virtual void set_credentials(const char *apn, const char *uname = 0, const char *pwd = 0) = 0;
161 virtual bool is_connected() = 0;
162
163 /** Same as NetworkInterface::get_default_instance()
164 *
165 * @note not to be used if get_default_nonip_instance() was already used
166 *
167 */
169
170 /** Instantiates a default Non-IP cellular interface
171 *
172 * This function creates a new Non-IP PDP context.
173 *
174 * @note not to be used if get_default_instance() was already used
175 *
176 * @return A Non-IP cellular PDP context
177 *
178 */
180
181 /** Get pointer to CellularDevice instance. May be null if not AT-layer.
182 *
183 * @return pointer to CellularDevice instance
184 */
186
187// Operations, can be sync/async. Also Connect() is this kind of operation, inherited from NetworkInterface above.
188
189 /** Start the interface
190 *
191 * Initializes the modem for communication.
192 * By default, this API is synchronous. API can be set to asynchronous with method set_blocking(...).
193 * In synchronous and asynchronous mode application can get result in from callback which is set with
194 * attach(...)
195 *
196 * @return NSAPI_ERROR_OK on success
197 * NSAPI_ERROR_NO_MEMORY on case of memory failure
198 */
200
201 /** Start the interface
202 *
203 * Attempts to open the SIM.
204 * By default, this API is synchronous. API can be set to asynchronous with method set_blocking(...).
205 * In synchronous and asynchronous mode, the application can get result in from callback, which is set with
206 * attach(...)
207 *
208 * @return NSAPI_ERROR_OK on success
209 * NSAPI_ERROR_NO_MEMORY on case of memory failure
210 */
212
213 /** Start the interface
214 *
215 * Attempts to register the device to cellular network.
216 * By default, this API is synchronous. API can be set to asynchronous with method set_blocking(...).
217 * In synchronous and asynchronous mode, the application can get result in from callback, which is set with
218 * attach(...)
219 *
220 * @return NSAPI_ERROR_OK on success
221 * NSAPI_ERROR_NO_MEMORY on case of memory failure
222 */
224
225 /** Start the interface
226 *
227 * Attempts to attach the device to cellular network.
228 * By default, this API is synchronous. API can be set to asynchronous with method set_blocking(...).
229 * In synchronous and asynchronous mode, the application can get result in from callback, which is set with
230 * attach(...)
231 *
232 * @return NSAPI_ERROR_OK on success
233 * NSAPI_ERROR_NO_MEMORY on case of memory failure
234 */
236
237// PDP Context specific functions
238
239 /** Get APN rate control.
240 *
241 * @remark optional params are not updated if not received from network, so use good defaults
242 * @param reports Additional exception reports at maximum rate reached are allowed to be sent [optional]
243 * @param time_unit Uplink time unit with values 0=unrestricted, 1=minute, 2=hour, 3=day, 4=week [optional]
244 * @param uplink_rate Maximum number of messages per timeUnit [optional]
245 * @return NSAPI_ERROR_OK on success
246 * NSAPI_ERROR_DEVICE_ERROR on case of failure
247 */
248 virtual nsapi_error_t get_rate_control(CellularContext::RateControlExceptionReports &reports,
249 CellularContext::RateControlUplinkTimeUnit &time_unit, int &uplink_rate) = 0;
250
251 /** Get the relevant information for an active nonsecondary PDP context.
252 *
253 * @remark optional params are not updated if not received from network.
254 * @param params_list reference to linked list, which is filled on successful call
255 * @return NSAPI_ERROR_OK on success
256 * NSAPI_ERROR_NO_MEMORY on memory failure
257 * NSAPI_ERROR_DEVICE_ERROR on other failures
258 */
260
261 /** Get backoff timer value
262 *
263 * @param backoff_timer Backoff timer value associated with PDP APN in seconds
264 * @return NSAPI_ERROR_OK on success
265 * NSAPI_ERROR_PARAMETER if no access point is set or found when activating context
266 * NSAPI_ERROR_DEVICE_ERROR on failure
267 */
268 virtual nsapi_error_t get_apn_backoff_timer(int &backoff_timer) = 0;
269
270#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY)
271
272 /** Enable or disable hang-up detection.
273 *
274 * This method will use data carrier detect to be able to detect disconnection much faster in PPP mode.
275 *
276 * When in PPP data pump mode, it is helpful if the FileHandle will signal hang-up via
277 * POLLHUP, e.g., if the DCD line is deasserted on a UART. During command mode, this
278 * signaling is not desired.
279 *
280 * @param dcd_pin Pin used to set data carrier detect on/off for the given UART. NC if feature is disabled.
281 * @param active_high a boolean set to true if DCD polarity is active low
282 *
283 * @return NSAPI_ERROR_OK if success,
284 * NSAPI_ERROR_UNSUPPORTED if modem does not support this feature
285 */
286 virtual nsapi_error_t configure_hup(PinName dcd_pin = NC, bool active_high = false) = 0;
287#endif // #if DEVICE_SERIAL
288
289 /** Returns the control plane AT command interface
290 */
292
293 /** Get the pdp context id associated with this context.
294 *
295 * @return cid
296 */
297 int get_cid() const;
298
299 /** Set the authentication type to be used in user authentication if user name and password are defined
300 *
301 * @param type enum AuthenticationType
302 */
303 void set_authentication_type(AuthenticationType type);
304
305protected: // Device specific implementations might need these so protected
306 enum ContextOperation {
307 OP_INVALID = -1,
308 OP_DEVICE_READY = 0,
309 OP_SIM_READY = 1,
310 OP_REGISTER = 2,
311 OP_ATTACH = 3,
312 OP_CONNECT = 4,
313 OP_MAX = 5
314 };
315
316 enum pdp_type_t {
317 DEFAULT_PDP_TYPE = DEFAULT_STACK,
318 IPV4_PDP_TYPE = IPV4_STACK,
319 IPV6_PDP_TYPE = IPV6_STACK,
320 IPV4V6_PDP_TYPE = IPV4V6_STACK,
321 NON_IP_PDP_TYPE
322 };
323
324 /** The CellularDevice calls the status callback function on status changes on the network or CellularDevice.
325 *
326 * @param ev event type
327 * @param ptr event-type dependent reason parameter
328 */
329 virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr) = 0;
330
331 /** Return PDP type string for Non-IP if modem uses other than standard "Non-IP"
332 *
333 * Some modems uses a non-standard PDP type string for non-ip (e.g. "NONIP").
334 * In those cases modem driver must implement this method to return the PDP type string
335 * used by the modem.
336 *
337 * @return PDP type string used by the modem or NULL if standard ("Non-IP")
338 */
339 virtual const char *get_nonip_context_type_str() = 0;
340
341 /** Triggers control plane's operations needed when control plane data is received,
342 * like socket event, for example.
343 */
345
346 /** Retry logic after device attached to network. Retry to find and activate pdp context or in case
347 * of PPP find correct pdp context and open data channel. Retry logic is the same which is used in
348 * CellularStateMachine.
349 */
350 virtual void do_connect_with_retry();
351
352 /** Helper method to call callback function if it is provided
353 *
354 * @param status connection status which is parameter in callback function
355 */
356 void call_network_cb(nsapi_connection_status_t status);
357
358 /** Find and activate pdp context or in case of PPP find correct pdp context and open data channel.
359 */
360 virtual void do_connect();
361
362 /** After we have connected successfully we must check that we have a valid IP address.
363 * Some modems/networks don't give IP address right after connect so we must poll it for a while.
364 */
366
367 /** Converts the given pdp type in char format to enum pdp_type_t
368 *
369 * @param pdp_type pdp type in string format
370 * @return converted pdp_type_t enum
371 */
372 CellularContext::pdp_type_t string_to_pdp_type(const char *pdp_type);
373
374protected:
375 // member variables needed in target override methods
376 NetworkStack *_stack; // must be pointer because of PPP
377 pdp_type_t _pdp_type;
378 CellularContext::AuthenticationType _authentication_type;
379 nsapi_connection_status_t _connect_status;
380 cell_callback_data_t _cb_data;
381 Callback<void(nsapi_event_t, intptr_t)> _status_cb;
382 int _cid;
383 bool _new_context_set;
384 bool _is_context_active;
385 bool _is_context_activated; // did we activate the context
386 const char *_apn;
387 const char *_uname;
388 const char *_pwd;
389
390 ControlPlane_netif *_cp_netif;
391 uint16_t _retry_timeout_array[CELLULAR_RETRY_ARRAY_SIZE];
392 int _retry_array_length;
393 int _retry_count;
394 CellularDevice *_device;
395 CellularNetwork *_nw;
396 bool _is_blocking;
397 // flag indicating if Non-IP context was requested to be setup
398 bool _nonip_req;
399 // tells if CCIOTOPTI received green from network for CP optimization use
400 bool _cp_in_use;
401};
402
403/**
404 * @}
405 * @}
406 */
407
408} // namespace mbed
409
410
411#endif /* _CELLULARCONTEXT_H_ */
Class CellularDevice.
Implements support for data transfer using Control Plane CIoT EPS optimization.
NetworkStack class.
Common interface that is shared between cellular interfaces.
NetworkStack class.
SocketAddress class.
Callback class based on template specialization.
Definition Callback.h:53
CellularContext is CellularInterface/NetworkInterface with extensions for cellular connectivity.
virtual void set_plmn(const char *plmn)=0
Set the plmn.
virtual void set_sim_pin(const char *sim_pin)=0
Set the PIN code for SIM card.
void validate_ip_address()
After we have connected successfully we must check that we have a valid IP address.
virtual nsapi_error_t get_ip_address(SocketAddress *address)=0
Get the local IP address.
virtual nsapi_error_t configure_hup(PinName dcd_pin=NC, bool active_high=false)=0
Enable or disable hang-up detection.
virtual bool is_connected()=0
Check if the connection is currently established.
CellularDevice * get_device() const
Get pointer to CellularDevice instance.
virtual ControlPlane_netif * get_cp_netif()=0
Returns the control plane AT command interface.
virtual void do_connect_with_retry()
Retry logic after device attached to network.
CellularContext::pdp_type_t string_to_pdp_type(const char *pdp_type)
Converts the given pdp type in char format to enum pdp_type_t.
virtual nsapi_error_t get_apn_backoff_timer(int &backoff_timer)=0
Get backoff timer value.
static CellularContext * get_default_instance()
Same as NetworkInterface::get_default_instance()
void set_authentication_type(AuthenticationType type)
Set the authentication type to be used in user authentication if user name and password are defined.
virtual const char * get_nonip_context_type_str()=0
Return PDP type string for Non-IP if modem uses other than standard "Non-IP".
virtual nsapi_error_t get_rate_control(CellularContext::RateControlExceptionReports &reports, CellularContext::RateControlUplinkTimeUnit &time_unit, int &uplink_rate)=0
Get APN rate control.
virtual void cellular_callback(nsapi_event_t ev, intptr_t ptr)=0
The CellularDevice calls the status callback function on status changes on the network or CellularDev...
virtual void set_credentials(const char *apn, const char *uname=0, const char *pwd=0)=0
Set the cellular network credentials.
virtual nsapi_error_t connect()=0
Attempt to connect to a cellular network.
virtual nsapi_error_t register_to_network()=0
Start the interface.
virtual nsapi_error_t set_sim_ready()=0
Start the interface.
virtual nsapi_error_t connect(const char *sim_pin, const char *apn=0, const char *uname=0, const char *pwd=0)=0
Attempt to connect to a cellular network with a PIN and credentials.
virtual void attach(mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)=0
Register callback for status reporting.
virtual nsapi_error_t set_device_ready()=0
Start the interface.
virtual nsapi_error_t attach_to_network()=0
Start the interface.
void cp_data_received()
Triggers control plane's operations needed when control plane data is received, like socket event,...
void call_network_cb(nsapi_connection_status_t status)
Helper method to call callback function if it is provided.
static CellularContext * get_default_nonip_instance()
Instantiates a default Non-IP cellular interface.
virtual nsapi_error_t set_blocking(bool blocking)=0
Set asynchronous operation of connect() and disconnect() calls.
virtual nsapi_error_t get_pdpcontext_params(pdpContextList_t &params_list)=0
Get the relevant information for an active nonsecondary PDP context.
virtual void do_connect()
Find and activate pdp context or in case of PPP find correct pdp context and open data channel.
virtual nsapi_error_t disconnect()=0
Stop the interface.
int get_cid() const
Get the pdp context id associated with this context.
Class CellularDevice.
Class CellularList.
An abstract interface for connecting to a network and getting information from it.
Implements support for data transfer using Control Plane CIoT EPS optimization specified in 3GPP 23....
signed int nsapi_error_t
Type used to represent error codes.