Mbed OS Reference
Loading...
Searching...
No Matches
AT_CellularStack.h
1/*
2 * Copyright (c) 2017, 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
18#ifndef AT_CELLULAR_STACK_H_
19#define AT_CELLULAR_STACK_H_
20
21#include "ATHandler.h"
23#include "rtos/Mutex.h"
24#include "AT_CellularDevice.h"
25
26
27namespace mbed {
28
29/**
30 * @addtogroup at-hayes AT/Hayes Command Set
31 * @ingroup Cellular
32 * @{
33 */
34
35// <PDP_addr_1> and <PDP_addr_2>: each is a string type that identifies the MT in the address space applicable to the PDP.
36// The string is given as dot-separated numeric (0-255) parameter of the form:
37// a1.a2.a3.a4 for IPv4 and
38// a1.a2.a3.a4.a5.a6.a7.a8.a9.a10.a11.a12.a13.a14.a15.a16 for IPv6.
39#define PDP_IPV6_SIZE 63+1
40
41/**
42 * Class AT_CellularStack.
43 *
44 * Implements NetworkStack and introduces interface for modem specific stack implementations.
45 */
47
48public:
49 AT_CellularStack(ATHandler &at, int cid, nsapi_ip_stack_t stack_type, AT_CellularDevice &device);
50 virtual ~AT_CellularStack();
51
52public: // NetworkStack
53
55
56 /**
57 * Set PDP context ID for this stack
58 *
59 * @param cid value from AT+CGDCONT, where -1 is undefined
60 */
61 void set_cid(int cid);
62
63protected: // NetworkStack
64
65 /**
66 * Note: Socket_open does not actually open socket on all drivers, but that's deferred until calling `sendto`.
67 * The reason is that IP stack implementations are very much modem specific and it's quite common that when a
68 * socket is created (via AT commands) it must also be given remote IP address, and that is usually known
69 * only when calling `sendto`.
70 */
71 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
72
74
75 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
76
77 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
78
80
82 nsapi_socket_t *handle, SocketAddress *address = 0);
83
85 const void *data, nsapi_size_t size);
86
88 void *data, nsapi_size_t size);
89
91 const void *data, nsapi_size_t size);
92
94 void *buffer, nsapi_size_t size);
95
96 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
97
98protected:
100 public:
102 id(-1),
103 connected(false),
104 proto(NSAPI_UDP),
105 remoteAddress("", 0),
106 localAddress("", 0),
107 _cb(NULL),
108 _data(NULL),
109 closed(false),
110 started(false),
111 tx_ready(false),
112 tls_socket(false),
113 pending_bytes(0),
114 txfull_event(false)
115 {
116 }
117 // Socket identifier, generally it will be the socket ID assigned by the
118 // modem. In a few special cases, modems may take that as an input argument.
119 int id;
120 // Being connected means remote ip address and port are set
121 bool connected;
122 nsapi_protocol_t proto;
123 SocketAddress remoteAddress;
124 SocketAddress localAddress;
125 void (*_cb)(void *);
126 void *_data;
127 bool closed; // socket has been closed by a peer
128 bool started; // socket has been opened on modem stack
129 bool tx_ready; // socket is ready for sending on modem stack
130 bool tls_socket; // socket uses modem's internal TLS socket functionality
131 nsapi_size_t pending_bytes; // The number of received bytes pending
132 bool txfull_event; // socket event after wouldblock
133 };
134
135 /**
136 * Implements modem specific AT command set for socket closing
137 *
138 * @param sock_id Socket id
139 */
140 virtual nsapi_error_t socket_close_impl(int sock_id) = 0;
141
142 /**
143 * Implements modem specific AT command set for creating socket
144 *
145 * @param socket Cellular socket handle
146 */
148
149 /**
150 * Implements modem specific AT command set for sending data
151 *
152 * @param socket Cellular socket handle
153 * @param address The SocketAddress of the remote host
154 * @param data Buffer of data to send to the host
155 * @param size Size of the buffer in bytes
156 * @return Number of sent bytes on success, negative error
157 * code on failure
158 */
160 const void *data, nsapi_size_t size) = 0;
161
162 /**
163 * Implements modem specific AT command set for receiving data
164 *
165 * @param socket Socket handle
166 * @param address Destination for the source address or NULL
167 * @param buffer Destination buffer for data received from the host
168 * @param size Size of the buffer in bytes
169 * @return Number of received bytes on success, negative error
170 * code on failure
171 */
173 void *buffer, nsapi_size_t size) = 0;
174
175protected:
176 /**
177 * Find the socket handle based on the index of the socket construct
178 * in the socket container. Please note that this index may or may not be
179 * the socket id. The actual meaning of this index depends upon the modem
180 * being used.
181 *
182 * @param index Index of the socket construct in the container
183 * @return Socket handle, NULL on error
184 */
186
187 /**
188 * Find the index of the given CellularSocket handle. This index may or may
189 * not be the socket id. The actual meaning of this index depends upon the modem
190 * being used.
191 */
193
194 /**
195 * Checks if send to address is valid and if current stack type supports sending to that address type
196 */
198
199private:
200 int get_socket_index_by_port(uint16_t port);
201
202protected:
203 // socket container
204 CellularSocket **_socket;
205
206 // IP address
207 char _ip[PDP_IPV6_SIZE];
208
209 // PDP context id
210 int _cid;
211
212 // stack type - initialised as PDP type and set accordingly after CGPADDR checked
213 nsapi_ip_stack_t _stack_type;
214
215 // IP version of send to address
216 nsapi_version_t _ip_ver_sendto;
217
218 // mutex for write/read to a _socket array, needed when multiple threads may use sockets simultaneously
219 rtos::Mutex _socket_mutex;
220
221 ATHandler &_at;
222 AT_CellularDevice &_device;
223};
224
225/**
226 * @}
227 */
228
229} // namespace mbed
230
231#endif // AT_CELLULAR_STACK_H_
NetworkStack class.
NetworkStack class.
Definition: NetworkStack.h:42
SocketAddress class.
Definition: SocketAddress.h:37
Class AT_CellularDevice.
Class AT_CellularStack.
virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address)
Bind a specific address to a socket.
bool is_addr_stack_compatible(const SocketAddress &addr)
Checks if send to address is valid and if current stack type supports sending to that address type.
virtual nsapi_error_t create_socket_impl(CellularSocket *socket)=0
Implements modem specific AT command set for creating socket.
virtual nsapi_error_t get_ip_address(SocketAddress *address)
Get the local IP address.
virtual nsapi_error_t socket_close_impl(int sock_id)=0
Implements modem specific AT command set for socket closing.
virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address)
Connects TCP socket to a remote host.
int find_socket_index(nsapi_socket_t handle)
Find the index of the given CellularSocket handle.
virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size)
Send a packet over a UDP socket.
virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog)
Listen for connections on a TCP socket.
virtual void socket_attach(nsapi_socket_t handle, void(*callback)(void *), void *data)
Register a callback on state change of the socket.
virtual nsapi_error_t socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address=0)
Accepts a connection on a TCP socket.
void set_cid(int cid)
Set PDP context ID for this stack.
virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address, void *buffer, nsapi_size_t size)
Receive a packet over a UDP socket.
virtual nsapi_size_or_error_t socket_sendto_impl(CellularSocket *socket, const SocketAddress &address, const void *data, nsapi_size_t size)=0
Implements modem specific AT command set for sending data.
CellularSocket * find_socket(int index)
Find the socket handle based on the index of the socket construct in the socket container.
virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto)
Note: Socket_open does not actually open socket on all drivers, but that's deferred until calling sen...
virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle, void *data, nsapi_size_t size)
Receive data over a TCP socket.
virtual nsapi_size_or_error_t socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address, void *buffer, nsapi_size_t size)=0
Implements modem specific AT command set for receiving data.
virtual nsapi_error_t socket_close(nsapi_socket_t handle)
Close the socket.
virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle, const void *data, nsapi_size_t size)
Send data over a TCP socket.
Class for sending AT commands and parsing AT responses.
Definition: ATHandler.h:68
The Mutex class is used to synchronize the execution of threads.
Definition: Mutex.h:70
signed int nsapi_size_or_error_t
Type used to represent either a size or error passed through sockets.
Definition: nsapi_types.h:153
signed int nsapi_error_t
Type used to represent error codes.
Definition: nsapi_types.h:142
void * nsapi_socket_t
Opaque handle for network sockets.
Definition: nsapi_types.h:254
unsigned int nsapi_size_t
Type used to represent the size of data passed through sockets.
Definition: nsapi_types.h:146
@ NSAPI_UDP
Definition: nsapi_types.h:266
Callback< R(ArgTs...)> callback(R(*func)(ArgTs...)=nullptr) noexcept
Create a callback class with type inferred from the arguments.
Definition: Callback.h:678