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 const char *get_ip_address();
47 
48  /**
49  * Set PDP context ID for this stack
50  *
51  * @param cid value from AT+CGDCONT, where -1 is undefined
52  */
53  void set_cid(int cid);
54 
55 protected: // NetworkStack
56 
57  /**
58  * Modem specific socket stack initialization
59  *
60  * @return 0 on success
61  */
63 
64  /**
65  * Note: Socket_open does not actually open socket on all drivers, but that's deferred until calling `sendto`.
66  * The reason is that IP stack implementations are very much modem specific and it's quite common that when a
67  * socket is created (via AT commands) it must also be given remote IP address, and that is usually known
68  * only when calling `sendto`.
69  */
70  virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
71 
73 
74  virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
75 
76  virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
77 
78  virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
79 
81  nsapi_socket_t *handle, SocketAddress *address = 0);
82 
84  const void *data, nsapi_size_t size);
85 
87  void *data, nsapi_size_t size);
88 
89  virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
90  const void *data, nsapi_size_t size);
91 
93  void *buffer, nsapi_size_t size);
94 
95  virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
96 
97 protected:
98 
100  public:
101  CellularSocket() :
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  rx_avail(false),
113  tls_socket(false),
114  pending_bytes(0)
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 rx_avail; // socket has data for reading on modem stack
131  bool tls_socket; // socket uses modem's internal TLS socket functionality
132  nsapi_size_t pending_bytes; // The number of received bytes pending
133  };
134 
135  /**
136  * Gets maximum number of sockets modem supports
137  */
138  virtual int get_max_socket_count() = 0;
139 
140  /**
141  * Checks if modem supports the given protocol
142  *
143  * @param protocol Protocol type
144  */
145  virtual bool is_protocol_supported(nsapi_protocol_t protocol) = 0;
146 
147  /**
148  * Implements modem specific AT command set for socket closing
149  *
150  * @param sock_id Socket id
151  */
152  virtual nsapi_error_t socket_close_impl(int sock_id) = 0;
153 
154  /**
155  * Implements modem specific AT command set for creating socket
156  *
157  * @param socket Cellular socket handle
158  */
159  virtual nsapi_error_t create_socket_impl(CellularSocket *socket) = 0;
160 
161  /**
162  * Implements modem specific AT command set for sending data
163  *
164  * @param socket Cellular socket handle
165  * @param address The SocketAddress of the remote host
166  * @param data Buffer of data to send to the host
167  * @param size Size of the buffer in bytes
168  * @return Number of sent bytes on success, negative error
169  * code on failure
170  */
172  const void *data, nsapi_size_t size) = 0;
173 
174  /**
175  * Implements modem specific AT command set for receiving data
176  *
177  * @param socket Socket handle
178  * @param address Destination for the source address or NULL
179  * @param buffer Destination buffer for data received from the host
180  * @param size Size of the buffer in bytes
181  * @return Number of received bytes on success, negative error
182  * code on failure
183  */
185  void *buffer, nsapi_size_t size) = 0;
186 
187  /**
188  * Find the socket handle based on the index of the socket construct
189  * in the socket container. Please note that this index may or may not be
190  * the socket id. The actual meaning of this index depends upon the modem
191  * being used.
192  *
193  * @param index Index of the socket construct in the container
194  * @return Socket handle, NULL on error
195  */
196  CellularSocket *find_socket(int index);
197 
198  /**
199  * Find the index of the given CellularSocket handle. This index may or may
200  * not be the socket id. The actual meaning of this index depends upon the modem
201  * being used.
202  */
203  int find_socket_index(nsapi_socket_t handle);
204 
205  /**
206  * Checks if send to address is valid and if current stack type supports sending to that address type
207  */
208  bool is_addr_stack_compatible(const SocketAddress &addr);
209 
210  // socket container
211  CellularSocket **_socket;
212 
213  // number of socket slots allocated in socket container
214  int _socket_count;
215 
216  // IP address
217  char _ip[PDP_IPV6_SIZE];
218 
219  // PDP context id
220  int _cid;
221 
222  // stack type - initialised as PDP type and set accordingly after CGPADDR checked
223  nsapi_ip_stack_t _stack_type;
224 
225  // IP version of send to address
226  nsapi_version_t _ip_ver_sendto;
227 
228 private:
229 
230  int get_socket_index_by_port(uint16_t port);
231 
232  // mutex for write/read to a _socket array, needed when multiple threads may open sockets simultaneously
233  PlatformMutex _socket_mutex;
234 };
235 
236 } // namespace mbed
237 
238 #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.
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.
virtual const char * get_ip_address()
Get the local IP address.
Class for sending AT commands and parsing AT responses.
Definition: ATHandler.h:66
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.