Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CellularNonIPSocket.h Source File

CellularNonIPSocket.h

00001 /* CellularNonIPSocket
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef CELLULARNONIPSOCKET_H
00018 #define CELLULARNONIPSOCKET_H
00019 
00020 #include "netsocket/Socket.h"
00021 #include "rtos/Mutex.h"
00022 #include "rtos/EventFlags.h"
00023 #include "Callback.h"
00024 #include "mbed_toolchain.h"
00025 #include "ControlPlane_netif.h"
00026 #include "CellularContext.h"
00027 
00028 namespace mbed {
00029 
00030 /** \addtogroup netsocket */
00031 /** @{*/
00032 
00033 /** Socket implementation for cellular Non-IP data delivery(NIDD).
00034  *  Relies on Control Plane CIoT EPS optimization feature,
00035  *  implemented in ControlPlane_netif class.
00036  */
00037 class CellularNonIPSocket : public Socket {
00038 public:
00039     /** Destroy the socket.
00040     *
00041     *  @note Closes socket if it's still open.
00042     */
00043     virtual ~CellularNonIPSocket();
00044 
00045     /** Creates a socket.
00046     */
00047     CellularNonIPSocket();
00048 
00049     /** Opens a socket on the given cellular context.
00050     *
00051     *  @param cellular_context     Cellular PDP context over which this socket is sending and
00052     *                              receiving data. The context has support for providing
00053     *                              a control plane interface for data delivery.
00054     *  @return                     NSAPI_ERROR_OK on success
00055     *                              NSAPI_ERROR_PARAMETER otherwise
00056     */
00057     virtual nsapi_error_t open(mbed::CellularContext *cellular_context);
00058 
00059     /** Opens a socket that will use the given control plane interface for data delivery.
00060     *   Attaches the event as callback to the control plane interface.
00061     *
00062     *  @param cp_netif     Control plane interface for data delivery.
00063     *  @return             NSAPI_ERROR_OK on success
00064     *                      NSAPI_ERROR_PARAMETER otherwise
00065     *
00066     */
00067     virtual nsapi_error_t open(mbed::ControlPlane_netif *cp_netif);
00068 
00069     /** Closes socket
00070     *
00071     *  @return         NSAPI_ERROR_OK on success
00072     *                  NSAPI_ERROR_NO_SOCKET otherwise
00073     */
00074 
00075     virtual nsapi_error_t close();
00076 
00077     /** Send data over a control plane cellular context.
00078     *
00079     *  By default, send blocks until all data is sent. If socket is set to
00080     *  nonblocking or times out, a partial amount can be written.
00081     *  NSAPI_ERROR_WOULD_BLOCK is returned if no data was written.
00082     *
00083     *  @param data     Buffer of data to be sent.
00084     *  @param size     Size of the buffer in bytes.
00085     *  @return         Number of sent bytes on success, negative error
00086     *                  code on failure.
00087     */
00088     virtual nsapi_size_or_error_t send(const void *data, nsapi_size_t size);
00089 
00090     /** Receive data from a socket.
00091     *
00092     *  By default, recv blocks until some data is received. If socket is set to
00093     *  nonblocking or times out, NSAPI_ERROR_WOULD_BLOCK can be returned to
00094     *  indicate no data.
00095     *
00096     *  @param data     Pointer to buffer for received data.
00097     *  @param size     Size of the buffer in bytes.
00098     *  @return         Number of received bytes on success, negative error
00099     *                  code on failure.
00100     */
00101     virtual nsapi_size_or_error_t recv(void *data, nsapi_size_t size);
00102 
00103     /** @copydoc Socket::set_blocking
00104      */
00105     virtual void set_blocking (bool blocking);
00106 
00107     /** @copydoc Socket::set_timeout
00108      */
00109     virtual void set_timeout (int timeout);
00110 
00111     /** @copydoc Socket::sigio
00112      */
00113     virtual void sigio (mbed::Callback<void()> func);
00114 
00115     /// NOT APPLICABLE
00116     virtual nsapi_error_t connect(const SocketAddress &address);
00117     /// NOT APPLICABLE
00118     virtual Socket *accept(nsapi_error_t *error = NULL);
00119     /// NOT APPLICABLE
00120     virtual nsapi_error_t listen(int backlog = 1);
00121     /// NOT APPLICABLE
00122     virtual nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
00123     /// NOT APPLICABLE
00124     virtual nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
00125     /// NOT APPLICABLE
00126     virtual nsapi_error_t getpeername(SocketAddress *address);
00127     /// NOT APPLICABLE
00128     virtual nsapi_size_or_error_t sendto(const SocketAddress &address,
00129                                          const void *data, nsapi_size_t size);
00130     /// NOT APPLICABLE
00131     virtual nsapi_size_or_error_t recvfrom(SocketAddress *address,
00132                                            void *data, nsapi_size_t size);
00133     /// NOT APPLICABLE
00134     virtual nsapi_error_t bind(const SocketAddress &address);
00135 
00136 protected:
00137     virtual void event();
00138 
00139     uint32_t _timeout;
00140     mbed::Callback<void()> _event;
00141     mbed::Callback<void()> _callback;
00142     rtos::EventFlags _event_flag;
00143     rtos::Mutex _lock;
00144     uint8_t _readers;
00145     uint8_t _writers;
00146     volatile unsigned _pending;
00147 
00148     // Event flags
00149     static const int READ_FLAG     = 0x1u;
00150     static const int WRITE_FLAG    = 0x2u;
00151     static const int FINISHED_FLAG = 0x3u;
00152 
00153     ControlPlane_netif *_cp_netif;
00154     bool _opened;
00155 };
00156 
00157 /** @}*/
00158 } // namespace mbed
00159 
00160 #endif // CELLULARNONIPSOCKET_H