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 
93  const void *data, nsapi_size_t size,
94  nsapi_msghdr_t *control, nsapi_size_t control_size) override
95  {
97  }
98 
100  void *data, nsapi_size_t size,
101  nsapi_msghdr_t *control, nsapi_size_t control_size) override
102  {
104  }
105 
106 protected:
108  public:
109  CellularSocket() :
110  id(-1),
111  connected(false),
112  proto(NSAPI_UDP),
113  remoteAddress("", 0),
114  localAddress("", 0),
115  _cb(NULL),
116  _data(NULL),
117  closed(false),
118  started(false),
119  tx_ready(false),
120  tls_socket(false),
121  pending_bytes(0),
122  txfull_event(false)
123  {
124  }
125  // Socket identifier, generally it will be the socket ID assigned by the
126  // modem. In a few special cases, modems may take that as an input argument.
127  int id;
128  // Being connected means remote ip address and port are set
129  bool connected;
130  nsapi_protocol_t proto;
131  SocketAddress remoteAddress;
132  SocketAddress localAddress;
133  void (*_cb)(void *);
134  void *_data;
135  bool closed; // socket has been closed by a peer
136  bool started; // socket has been opened on modem stack
137  bool tx_ready; // socket is ready for sending on modem stack
138  bool tls_socket; // socket uses modem's internal TLS socket functionality
139  nsapi_size_t pending_bytes; // The number of received bytes pending
140  bool txfull_event; // socket event after wouldblock
141  };
142 
143  /**
144  * Implements modem specific AT command set for socket closing
145  *
146  * @param sock_id Socket id
147  */
148  virtual nsapi_error_t socket_close_impl(int sock_id) = 0;
149 
150  /**
151  * Implements modem specific AT command set for creating socket
152  *
153  * @param socket Cellular socket handle
154  */
155  virtual nsapi_error_t create_socket_impl(CellularSocket *socket) = 0;
156 
157  /**
158  * Implements modem specific AT command set for sending data
159  *
160  * @param socket Cellular socket handle
161  * @param address The SocketAddress of the remote host
162  * @param data Buffer of data to send to the host
163  * @param size Size of the buffer in bytes
164  * @return Number of sent bytes on success, negative error
165  * code on failure
166  */
168  const void *data, nsapi_size_t size) = 0;
169 
170  /**
171  * Implements modem specific AT command set for receiving data
172  *
173  * @param socket Socket handle
174  * @param address Destination for the source address or NULL
175  * @param buffer Destination buffer for data received from the host
176  * @param size Size of the buffer in bytes
177  * @return Number of received bytes on success, negative error
178  * code on failure
179  */
181  void *buffer, nsapi_size_t size) = 0;
182 
183 protected:
184  /**
185  * Find the socket handle based on the index of the socket construct
186  * in the socket container. Please note that this index may or may not be
187  * the socket id. The actual meaning of this index depends upon the modem
188  * being used.
189  *
190  * @param index Index of the socket construct in the container
191  * @return Socket handle, NULL on error
192  */
193  CellularSocket *find_socket(int index);
194 
195  /**
196  * Find the index of the given CellularSocket handle. This index may or may
197  * not be the socket id. The actual meaning of this index depends upon the modem
198  * being used.
199  */
200  int find_socket_index(nsapi_socket_t handle);
201 
202  /**
203  * Checks if send to address is valid and if current stack type supports sending to that address type
204  */
205  bool is_addr_stack_compatible(const SocketAddress &addr);
206 
207 private:
208  int get_socket_index_by_port(uint16_t port);
209 
210 protected:
211  // socket container
212  CellularSocket **_socket;
213 
214  // IP address
215  char _ip[PDP_IPV6_SIZE];
216 
217  // PDP context id
218  int _cid;
219 
220  // stack type - initialised as PDP type and set accordingly after CGPADDR checked
221  nsapi_ip_stack_t _stack_type;
222 
223  // IP version of send to address
224  nsapi_version_t _ip_ver_sendto;
225 
226  // mutex for write/read to a _socket array, needed when multiple threads may use sockets simultaneously
227  PlatformMutex _socket_mutex;
228 
229  ATHandler &_at;
230  AT_CellularDevice &_device;
231 };
232 
233 } // namespace mbed
234 
235 #endif // AT_CELLULAR_STACK_H_
nsapi_msghdr
Definition: nsapi_types.h:414
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:254
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:142
virtual nsapi_error_t get_ip_address(SocketAddress *address)
Get the local IP address.
nsapi_size_or_error_t socket_recvfrom_control(nsapi_socket_t handle, SocketAddress *address, void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Receive a packet with ancillary data over a UDP socket.
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:153
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:146
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.
nsapi_size_or_error_t socket_sendto_control(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size) override
Send a packet with ancillary data over a UDP 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.