takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers AT_CellularStack.h Source File

AT_CellularStack.h

00001 /*
00002  * Copyright (c) 2017, Arm Limited and affiliates.
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef AT_CELLULAR_STACK_H_
00019 #define AT_CELLULAR_STACK_H_
00020 
00021 #include "AT_CellularBase.h"
00022 #include "NetworkStack.h"
00023 
00024 namespace mbed {
00025 
00026 // <PDP_addr_1> and <PDP_addr_2>: each is a string type that identifies the MT in the address space applicable to the PDP.
00027 // The string is given as dot-separated numeric (0-255) parameter of the form:
00028 // a1.a2.a3.a4 for IPv4 and
00029 // a1.a2.a3.a4.a5.a6.a7.a8.a9.a10.a11.a12.a13.a14.a15.a16 for IPv6.
00030 #define PDP_IPV6_SIZE 63+1
00031 
00032 /**
00033  * Class AT_CellularStack.
00034  *
00035  * Implements NetworkStack and introduces interface for modem specific stack implementations.
00036  */
00037 class AT_CellularStack : public NetworkStack, public AT_CellularBase {
00038 
00039 public:
00040     AT_CellularStack(ATHandler &at, int cid, nsapi_ip_stack_t stack_type);
00041     virtual ~AT_CellularStack();
00042 
00043 public: // NetworkStack
00044 
00045     virtual const char *get_ip_address();
00046 protected: // NetworkStack
00047 
00048     /**
00049      * Modem specific socket stack initialization
00050      *
00051      *  @return 0 on success
00052      */
00053     virtual nsapi_error_t socket_stack_init();
00054 
00055     virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
00056 
00057     virtual nsapi_error_t socket_close(nsapi_socket_t handle);
00058 
00059     virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
00060 
00061     virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
00062 
00063     virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
00064 
00065     virtual nsapi_error_t socket_accept(nsapi_socket_t server,
00066                                         nsapi_socket_t *handle, SocketAddress *address = 0);
00067 
00068     virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
00069                                               const void *data, nsapi_size_t size);
00070 
00071     virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
00072                                               void *data, nsapi_size_t size);
00073 
00074     virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
00075                                                 const void *data, nsapi_size_t size);
00076 
00077     virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
00078                                                   void *buffer, nsapi_size_t size);
00079 
00080     virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
00081 
00082 protected:
00083 
00084     class CellularSocket {
00085     public:
00086         CellularSocket() :
00087             id(-1),
00088             connected(false),
00089             proto(NSAPI_UDP ),
00090             remoteAddress("", 0),
00091             localAddress("", 0),
00092             _cb(NULL),
00093             _data(NULL),
00094             created(false),
00095             started(false),
00096             tx_ready(false),
00097             rx_avail(false),
00098             pending_bytes(0)
00099         {
00100         }
00101         // Socket id from cellular device
00102         int id;
00103         // Being connected means remote ip address and port are set
00104         bool connected;
00105         nsapi_protocol_t proto;
00106         SocketAddress remoteAddress;
00107         SocketAddress localAddress;
00108         void (*_cb)(void *);
00109         void *_data;
00110         bool created; // socket has been created on modem stack
00111         bool started; // socket has been opened on modem stack
00112         bool tx_ready; // socket is ready for sending on modem stack
00113         bool rx_avail; // socket has data for reading on modem stack
00114         nsapi_size_t pending_bytes; // The number of received bytes pending
00115     };
00116 
00117     /**
00118     * Gets maximum number of sockets modem supports
00119     */
00120     virtual int get_max_socket_count() = 0;
00121 
00122     /**
00123     * Checks if modem supports the given protocol
00124     *
00125     * @param protocol   Protocol type
00126     */
00127     virtual bool is_protocol_supported(nsapi_protocol_t protocol) = 0;
00128 
00129     /**
00130     * Implements modem specific AT command set for socket closing
00131     *
00132     * @param sock_id   Socket id
00133     */
00134     virtual nsapi_error_t socket_close_impl(int sock_id) = 0;
00135 
00136     /**
00137     * Implements modem specific AT command set for creating socket
00138     *
00139     * @param socket   Cellular socket handle
00140     */
00141     virtual nsapi_error_t create_socket_impl(CellularSocket *socket) = 0;
00142 
00143     /**
00144     * Implements modem specific AT command set for sending data
00145     *
00146     * @param socket   Cellular socket handle
00147     * @param address  The SocketAddress of the remote host
00148     * @param data     Buffer of data to send to the host
00149     * @param size     Size of the buffer in bytes
00150     * @return         Number of sent bytes on success, negative error
00151     *                 code on failure
00152     */
00153     virtual nsapi_size_or_error_t socket_sendto_impl(CellularSocket *socket, const SocketAddress &address,
00154                                                      const void *data, nsapi_size_t size) = 0;
00155 
00156     /**
00157      *  Implements modem specific AT command set for receiving data
00158      *
00159      *  @param socket   Socket handle
00160      *  @param address  Destination for the source address or NULL
00161      *  @param buffer   Destination buffer for data received from the host
00162      *  @param size     Size of the buffer in bytes
00163      *  @return         Number of received bytes on success, negative error
00164      *                  code on failure
00165      */
00166     virtual nsapi_size_or_error_t socket_recvfrom_impl(CellularSocket *socket, SocketAddress *address,
00167                                                        void *buffer, nsapi_size_t size) = 0;
00168 
00169     // socket container
00170     CellularSocket **_socket;
00171 
00172     // number of socket slots allocated in socket container
00173     int _socket_count;
00174 
00175     // IP address
00176     char _ip[PDP_IPV6_SIZE];
00177 
00178     // PDP context id
00179     int _cid;
00180 
00181     // stack type from PDP context
00182     nsapi_ip_stack_t _stack_type;
00183 
00184 private:
00185     // mutex for write/read to a _socket array, needed when multiple threads may open sockets simultaneously
00186     PlatformMutex _socket_mutex;
00187 };
00188 
00189 } // namespace mbed
00190 
00191 #endif // AT_CELLULAR_STACK_H_