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

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.