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 
24 namespace mbed {
25 
26 // <PDP_addr_1> and <PDP_addr_2>: each is a string type that identifies the MT in the address space applicable to the PDP.
27 // The string is given as dot-separated numeric (0-255) parameter of the form:
28 // a1.a2.a3.a4 for IPv4 and
29 // a1.a2.a3.a4.a5.a6.a7.a8.a9.a10.a11.a12.a13.a14.a15.a16 for IPv6.
30 #define PDP_IPV6_SIZE 63+1
31 
32 /**
33  * Class AT_CellularStack.
34  *
35  * Implements NetworkStack and introduces interface for modem specific stack implementations.
36  */
38 
39 public:
40  AT_CellularStack(ATHandler &at, int cid, nsapi_ip_stack_t stack_type);
41  virtual ~AT_CellularStack();
42 
43 public: // NetworkStack
44 
45  virtual const char *get_ip_address();
46 protected: // NetworkStack
47 
48  /**
49  * Modem specific socket stack initialization
50  *
51  * @return 0 on success
52  */
54 
55  /**
56  * Note: Socket_open does not actually open socket on all drivers, but that's deferred until calling `sendto`.
57  * The reason is that IP stack implementations are very much modem specific and it's quite common that when a
58  * socket is created (via AT commands) it must also be given remote IP address, and that is usually known
59  * only when calling `sendto`.
60  */
61  virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
62 
64 
65  virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
66 
67  virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
68 
69  virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
70 
72  nsapi_socket_t *handle, SocketAddress *address = 0);
73 
75  const void *data, nsapi_size_t size);
76 
78  void *data, nsapi_size_t size);
79 
80  virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
81  const void *data, nsapi_size_t size);
82 
84  void *buffer, nsapi_size_t size);
85 
86  virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
87 
88 protected:
89 
91  public:
92  CellularSocket() :
93  id(-1),
94  connected(false),
95  proto(NSAPI_UDP),
96  remoteAddress("", 0),
97  localAddress("", 0),
98  _cb(NULL),
99  _data(NULL),
100  closed(false),
101  started(false),
102  tx_ready(false),
103  rx_avail(false),
104  pending_bytes(0)
105  {
106  }
107  // Socket identifier, generally it will be the socket ID assigned by the
108  // modem. In a few special cases, modems may take that as an input argument.
109  int id;
110  // Being connected means remote ip address and port are set
111  bool connected;
112  nsapi_protocol_t proto;
113  SocketAddress remoteAddress;
114  SocketAddress localAddress;
115  void (*_cb)(void *);
116  void *_data;
117  bool closed; // socket has been closed by a peer
118  bool started; // socket has been opened on modem stack
119  bool tx_ready; // socket is ready for sending on modem stack
120  bool rx_avail; // socket has data for reading on modem stack
121  nsapi_size_t pending_bytes; // The number of received bytes pending
122  };
123 
124  /**
125  * Gets maximum number of sockets modem supports
126  */
127  virtual int get_max_socket_count() = 0;
128 
129  /**
130  * Checks if modem supports the given protocol
131  *
132  * @param protocol Protocol type
133  */
134  virtual bool is_protocol_supported(nsapi_protocol_t protocol) = 0;
135 
136  /**
137  * Implements modem specific AT command set for socket closing
138  *
139  * @param sock_id Socket id
140  */
141  virtual nsapi_error_t socket_close_impl(int sock_id) = 0;
142 
143  /**
144  * Implements modem specific AT command set for creating socket
145  *
146  * @param socket Cellular socket handle
147  */
148  virtual nsapi_error_t create_socket_impl(CellularSocket *socket) = 0;
149 
150  /**
151  * Implements modem specific AT command set for sending data
152  *
153  * @param socket Cellular socket handle
154  * @param address The SocketAddress of the remote host
155  * @param data Buffer of data to send to the host
156  * @param size Size of the buffer in bytes
157  * @return Number of sent bytes on success, negative error
158  * code on failure
159  */
161  const void *data, nsapi_size_t size) = 0;
162 
163  /**
164  * Implements modem specific AT command set for receiving data
165  *
166  * @param socket Socket handle
167  * @param address Destination for the source address or NULL
168  * @param buffer Destination buffer for data received from the host
169  * @param size Size of the buffer in bytes
170  * @return Number of received bytes on success, negative error
171  * code on failure
172  */
174  void *buffer, nsapi_size_t size) = 0;
175 
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  */
185  CellularSocket *find_socket(int index);
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  */
192  int find_socket_index(nsapi_socket_t handle);
193 
194  // socket container
195  CellularSocket **_socket;
196 
197  // number of socket slots allocated in socket container
198  int _socket_count;
199 
200  // IP address
201  char _ip[PDP_IPV6_SIZE];
202 
203  // PDP context id
204  int _cid;
205 
206  // stack type from PDP context
207  nsapi_ip_stack_t _stack_type;
208 
209 private:
210 
211  int get_socket_index_by_port(uint16_t port);
212 
213  // mutex for write/read to a _socket array, needed when multiple threads may open sockets simultaneously
214  PlatformMutex _socket_mutex;
215 };
216 
217 } // namespace mbed
218 
219 #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:204
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
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
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.
SocketAddress class.
Definition: SocketAddress.h:35
virtual int get_max_socket_count()=0
Gets maximum number of sockets modem supports.
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.
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:63
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.