Gleb Klochkov / Mbed OS Climatcontroll_Main

Dependencies:   esp8266-driver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Socket.h Source File

Socket.h

00001 
00002 /** \addtogroup netsocket */
00003 /** @{*/
00004 /* Socket
00005  * Copyright (c) 2015 ARM Limited
00006  *
00007  * Licensed under the Apache License, Version 2.0 (the "License");
00008  * you may not use this file except in compliance with the License.
00009  * You may obtain a copy of the License at
00010  *
00011  *     http://www.apache.org/licenses/LICENSE-2.0
00012  *
00013  * Unless required by applicable law or agreed to in writing, software
00014  * distributed under the License is distributed on an "AS IS" BASIS,
00015  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016  * See the License for the specific language governing permissions and
00017  * limitations under the License.
00018  */
00019 
00020 #ifndef SOCKET_H
00021 #define SOCKET_H
00022 
00023 #include "netsocket/SocketAddress.h"
00024 #include "netsocket/NetworkStack.h"
00025 #include "rtos/Mutex.h"
00026 #include "Callback.h"
00027 #include "mbed_toolchain.h"
00028 
00029 
00030 /** Abstract socket class
00031  */
00032 class Socket {
00033 public:
00034     /** Destroy a socket
00035      *
00036      *  Closes socket if the socket is still open
00037      */
00038     virtual ~Socket() {}
00039 
00040     /** Opens a socket
00041      *
00042      *  Creates a network socket on the network stack of the given
00043      *  network interface. Not needed if stack is passed to the
00044      *  socket's constructor.
00045      *
00046      *  @param stack    Network stack as target for socket
00047      *  @return         0 on success, negative error code on failure
00048      */
00049     nsapi_error_t open(NetworkStack *stack);
00050 
00051     template <typename S>
00052     nsapi_error_t open(S *stack) {
00053         return open(nsapi_create_stack(stack));
00054     }
00055     
00056     /** Close the socket
00057      *
00058      *  Closes any open connection and deallocates any memory associated
00059      *  with the socket. Called from destructor if socket is not closed.
00060      *
00061      *  @return         0 on success, negative error code on failure
00062      */
00063     nsapi_error_t close();
00064     
00065     /** Subscribes to an IP multicast group
00066      *
00067      * @param address   Multicast group IP address
00068      * @return          Negative error code on failure
00069      */
00070     int join_multicast_group(const SocketAddress &address);
00071 
00072     /** Leave an IP multicast group
00073      *
00074      * @param address   Multicast group IP address
00075      * @return          Negative error code on failure
00076      */
00077     int leave_multicast_group(const SocketAddress &address);
00078 
00079     /** Bind a specific address to a socket
00080      *
00081      *  Binding a socket specifies the address and port on which to receive
00082      *  data.
00083      *
00084      *  @param port     Local port to bind
00085      *  @return         0 on success, negative error code on failure.
00086      */
00087     nsapi_error_t bind(uint16_t port);
00088 
00089     /** Bind a specific address to a socket
00090      *
00091      *  Binding a socket specifies the address and port on which to receive
00092      *  data. If the IP address is zeroed, only the port is bound.
00093      *
00094      *  @param address  Null-terminated local address to bind
00095      *  @param port     Local port to bind
00096      *  @return         0 on success, negative error code on failure.
00097      */
00098     nsapi_error_t bind(const char *address, uint16_t port);
00099 
00100     /** Bind a specific address to a socket
00101      *
00102      *  Binding a socket specifies the address and port on which to receive
00103      *  data. If the IP address is zeroed, only the port is bound.
00104      *
00105      *  @param address  Local address to bind
00106      *  @return         0 on success, negative error code on failure.
00107      */
00108     nsapi_error_t bind(const SocketAddress &address);
00109     
00110     /** Set blocking or non-blocking mode of the socket
00111      *
00112      *  Initially all sockets are in blocking mode. In non-blocking mode
00113      *  blocking operations such as send/recv/accept return
00114      *  NSAPI_ERROR_WOULD_BLOCK if they can not continue.
00115      *
00116      *  set_blocking(false) is equivalent to set_timeout(-1)
00117      *  set_blocking(true) is equivalent to set_timeout(0)
00118      *
00119      *  @param blocking true for blocking mode, false for non-blocking mode.
00120      */
00121     void set_blocking(bool blocking);
00122     
00123     /** Set timeout on blocking socket operations
00124      *
00125      *  Initially all sockets have unbounded timeouts. NSAPI_ERROR_WOULD_BLOCK
00126      *  is returned if a blocking operation takes longer than the specified
00127      *  timeout. A timeout of 0 removes the timeout from the socket. A negative
00128      *  value give the socket an unbounded timeout.
00129      *
00130      *  set_timeout(0) is equivalent to set_blocking(false)
00131      *  set_timeout(-1) is equivalent to set_blocking(true)
00132      *
00133      *  @param timeout  Timeout in milliseconds
00134      */
00135     void set_timeout(int timeout);
00136 
00137     /*  Set socket options
00138      *
00139      *  setsockopt allows an application to pass stack-specific options
00140      *  to the underlying stack using stack-specific level and option names,
00141      *  or to request generic options using levels from nsapi_socket_level_t.
00142      *
00143      *  For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
00144      *  and the socket is unmodified.
00145      *
00146      *  @param level    Stack-specific protocol level or nsapi_socket_level_t
00147      *  @param optname  Level-specific option name
00148      *  @param optval   Option value
00149      *  @param optlen   Length of the option value
00150      *  @return         0 on success, negative error code on failure
00151      */    
00152     nsapi_error_t setsockopt(int level, int optname, const void *optval, unsigned optlen);
00153 
00154     /*  Get socket options
00155      *
00156      *  getsockopt allows an application to retrieve stack-specific options
00157      *  from the underlying stack using stack-specific level and option names,
00158      *  or to request generic options using levels from nsapi_socket_level_t.
00159      *
00160      *  For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned
00161      *  and the socket is unmodified.
00162      *
00163      *  @param level    Stack-specific protocol level or nsapi_socket_level_t
00164      *  @param optname  Level-specific option name
00165      *  @param optval   Destination for option value
00166      *  @param optlen   Length of the option value
00167      *  @return         0 on success, negative error code on failure
00168      */    
00169     nsapi_error_t getsockopt(int level, int optname, void *optval, unsigned *optlen);
00170 
00171     /** Register a callback on state change of the socket
00172      *
00173      *  The specified callback will be called on state changes such as when
00174      *  the socket can recv/send/accept successfully and on when an error
00175      *  occurs. The callback may also be called spuriously without reason.
00176      *
00177      *  The callback may be called in an interrupt context and should not
00178      *  perform expensive operations such as recv/send calls.
00179      *
00180      *  Note! This is not intended as a replacement for a poll or attach-like
00181      *  asynchronous api, but rather as a building block for constructing
00182      *  such functionality. The exact timing of when the registered function
00183      *  is called is not guaranteed and susceptible to change.
00184      *
00185      *  @param func     Function to call on state change
00186      */
00187     void sigio(mbed::Callback<void()> func);
00188 
00189     /** Register a callback on state change of the socket
00190      *
00191      *  @see Socket::sigio
00192      *  @deprecated
00193      *      The behaviour of Socket::attach differs from other attach functions in
00194      *      mbed OS and has been known to cause confusion. Replaced by Socket::sigio.
00195      */
00196     MBED_DEPRECATED_SINCE("mbed-os-5.4",
00197         "The behaviour of Socket::attach differs from other attach functions in "
00198         "mbed OS and has been known to cause confusion. Replaced by Socket::sigio.")
00199     void attach(mbed::Callback<void()> func);
00200 
00201     /** Register a callback on state change of the socket
00202      *
00203      *  @see Socket::sigio
00204      *  @deprecated
00205      *      The attach function does not support cv-qualifiers. Replaced by
00206      *      attach(callback(obj, method)).
00207      */
00208     template <typename T, typename M>
00209     MBED_DEPRECATED_SINCE("mbed-os-5.1",
00210         "The attach function does not support cv-qualifiers. Replaced by "
00211         "attach(callback(obj, method)).")
00212     void attach(T *obj, M method) {
00213         attach(mbed::callback(obj, method));
00214     }
00215 
00216 protected:
00217     Socket();
00218     virtual nsapi_protocol_t get_proto() = 0;
00219     virtual void event() = 0;
00220     int modify_multicast_group(const SocketAddress &address, nsapi_socket_option_t socketopt);
00221 
00222     NetworkStack *_stack;
00223     nsapi_socket_t _socket;
00224     uint32_t _timeout;
00225     mbed::Callback<void()> _event;
00226     mbed::Callback<void()> _callback;
00227     rtos::Mutex _lock;
00228 };
00229 
00230 
00231 #endif
00232 
00233 /** @}*/