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