Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
NanostackInterface.h
00001 /* 00002 * Copyright (c) 2016 ARM Limited. All rights reserved. 00003 */ 00004 00005 #ifndef NANOSTACK_INTERFACE_H_ 00006 #define NANOSTACK_INTERFACE_H_ 00007 00008 #include "NetworkStack.h" 00009 #include "MeshInterface.h" 00010 00011 #include "mbed-mesh-api/Mesh6LoWPAN_ND.h" 00012 #include "mbed-mesh-api/MeshThread.h" 00013 00014 class NanostackInterface : public NetworkStack { 00015 public: 00016 static NanostackInterface *get_stack(); 00017 00018 protected: 00019 00020 /** Get the local IP address 00021 * 00022 * @return Null-terminated representation of the local IP address 00023 * or null if not yet connected 00024 */ 00025 virtual const char *get_ip_address(); 00026 00027 /** Opens a socket 00028 * 00029 * Creates a network socket and stores it in the specified handle. 00030 * The handle must be passed to following calls on the socket. 00031 * 00032 * A stack may have a finite number of sockets, in this case 00033 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00034 * 00035 * @param handle Destination for the handle to a newly created socket 00036 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP 00037 * @return 0 on success, negative error code on failure 00038 */ 00039 virtual int socket_open(void **handle, nsapi_protocol_t proto); 00040 00041 /** Close the socket 00042 * 00043 * Closes any open connection and deallocates any memory associated 00044 * with the socket. 00045 * 00046 * @param handle Socket handle 00047 * @return 0 on success, negative error code on failure 00048 */ 00049 virtual int socket_close(void *handle); 00050 00051 /** Bind a specific address to a socket 00052 * 00053 * Binding a socket specifies the address and port on which to recieve 00054 * data. If the IP address is zeroed, only the port is bound. 00055 * 00056 * @param handle Socket handle 00057 * @param address Local address to bind 00058 * @return 0 on success, negative error code on failure. 00059 */ 00060 virtual int socket_bind(void *handle, const SocketAddress &address); 00061 00062 /** Listen for connections on a TCP socket 00063 * 00064 * Marks the socket as a passive socket that can be used to accept 00065 * incoming connections. 00066 * 00067 * @param handle Socket handle 00068 * @param backlog Number of pending connections that can be queued 00069 * simultaneously 00070 * @return 0 on success, negative error code on failure 00071 */ 00072 virtual int socket_listen(void *handle, int backlog); 00073 00074 /** Connects TCP socket to a remote host 00075 * 00076 * Initiates a connection to a remote server specified by the 00077 * indicated address. 00078 * 00079 * @param handle Socket handle 00080 * @param address The SocketAddress of the remote host 00081 * @return 0 on success, negative error code on failure 00082 */ 00083 virtual int socket_connect(void *handle, const SocketAddress &address); 00084 00085 /** Accepts a connection on a TCP socket 00086 * 00087 * The server socket must be bound and set to listen for connections. 00088 * On a new connection, creates a network socket and stores it in the 00089 * specified handle. The handle must be passed to following calls on 00090 * the socket. 00091 * 00092 * A stack may have a finite number of sockets, in this case 00093 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available. 00094 * 00095 * This call is non-blocking. If accept would block, 00096 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00097 * 00098 * @param server Socket handle to server to accept from 00099 * @param handle Destination for a handle to the newly created socket 00100 * @param address Destination for the remote address or NULL 00101 * @return 0 on success, negative error code on failure 00102 */ 00103 virtual int socket_accept(void *handle, void **server, SocketAddress *address); 00104 00105 /** Send data over a TCP socket 00106 * 00107 * The socket must be connected to a remote host. Returns the number of 00108 * bytes sent from the buffer. 00109 * 00110 * This call is non-blocking. If send would block, 00111 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00112 * 00113 * @param handle Socket handle 00114 * @param data Buffer of data to send to the host 00115 * @param size Size of the buffer in bytes 00116 * @return Number of sent bytes on success, negative error 00117 * code on failure 00118 */ 00119 virtual int socket_send(void *handle, const void *data, unsigned size); 00120 00121 /** Receive data over a TCP socket 00122 * 00123 * The socket must be connected to a remote host. Returns the number of 00124 * bytes received into the buffer. 00125 * 00126 * This call is non-blocking. If recv would block, 00127 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00128 * 00129 * @param handle Socket handle 00130 * @param data Destination buffer for data received from the host 00131 * @param size Size of the buffer in bytes 00132 * @return Number of received bytes on success, negative error 00133 * code on failure 00134 */ 00135 virtual int socket_recv(void *handle, void *data, unsigned size); 00136 00137 /** Send a packet over a UDP socket 00138 * 00139 * Sends data to the specified address. Returns the number of bytes 00140 * sent from the buffer. 00141 * 00142 * This call is non-blocking. If sendto would block, 00143 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00144 * 00145 * @param handle Socket handle 00146 * @param address The SocketAddress of the remote host 00147 * @param data Buffer of data to send to the host 00148 * @param size Size of the buffer in bytes 00149 * @return Number of sent bytes on success, negative error 00150 * code on failure 00151 */ 00152 virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size); 00153 00154 /** Receive a packet over a UDP socket 00155 * 00156 * Receives data and stores the source address in address if address 00157 * is not NULL. Returns the number of bytes received into the buffer. 00158 * 00159 * This call is non-blocking. If recvfrom would block, 00160 * NSAPI_ERROR_WOULD_BLOCK is returned immediately. 00161 * 00162 * @param handle Socket handle 00163 * @param address Destination for the source address or NULL 00164 * @param data Destination buffer for data received from the host 00165 * @param size Size of the buffer in bytes 00166 * @return Number of received bytes on success, negative error 00167 * code on failure 00168 */ 00169 virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size); 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 * @param handle Socket handle 00181 * @param callback Function to call on state change 00182 * @param data Argument to pass to callback 00183 */ 00184 virtual void socket_attach(void *handle, void (*callback)(void *), void *data); 00185 00186 /* Set stack-specific socket options 00187 * 00188 * The setsockopt allow an application to pass stack-specific hints 00189 * to the underlying stack. For unsupported options, 00190 * NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified. 00191 * 00192 * @param handle Socket handle 00193 * @param level Stack-specific protocol level 00194 * @param optname Stack-specific option identifier 00195 * @param optval Option value 00196 * @param optlen Length of the option value 00197 * @return 0 on success, negative error code on failure 00198 */ 00199 virtual int setsockopt(void *handle, int level, int optname, const void *optval, unsigned optlen); 00200 00201 /* Get stack-specific socket options 00202 * 00203 * The getstackopt allow an application to retrieve stack-specific hints 00204 * from the underlying stack. For unsupported options, 00205 * NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified. 00206 * 00207 * @param handle Socket handle 00208 * @param level Stack-specific protocol level 00209 * @param optname Stack-specific option identifier 00210 * @param optval Destination for option value 00211 * @param optlen Length of the option value 00212 * @return 0 on success, negative error code on failure 00213 */ 00214 virtual int getsockopt(void *handle, int level, int optname, void *optval, unsigned *optlen); 00215 00216 private: 00217 static NanostackInterface * _ns_interface; 00218 }; 00219 00220 class MeshInterfaceNanostack : public MeshInterface { 00221 public: 00222 00223 /** Start the interface 00224 * 00225 * @return 0 on success, negative on failure 00226 */ 00227 virtual int connect() = 0; 00228 00229 /** Stop the interface 00230 * 00231 * @return 0 on success, negative on failure 00232 */ 00233 virtual int disconnect(); 00234 00235 /** Get the internally stored IP address 00236 /return IP address of the interface or null if not yet connected 00237 */ 00238 virtual const char *get_ip_address(); 00239 00240 /** Get the internally stored MAC address 00241 /return MAC address of the interface 00242 */ 00243 virtual const char *get_mac_address(); 00244 00245 protected: 00246 MeshInterfaceNanostack() : connect_semaphore(0) { } 00247 int register_rf(); 00248 int actual_connect(); 00249 virtual NetworkStack * get_stack(void); 00250 00251 void mesh_network_handler(mesh_connection_status_t status); 00252 AbstractMesh *mesh_api; 00253 int8_t rf_device_id; 00254 uint8_t eui64[8]; 00255 char ip_addr_str[40]; 00256 char mac_addr_str[24]; 00257 Semaphore connect_semaphore; 00258 }; 00259 00260 class LoWPANNDInterface : public MeshInterfaceNanostack { 00261 public: 00262 int connect(); 00263 protected: 00264 Mesh6LoWPAN_ND *get_mesh_api() const { return static_cast<Mesh6LoWPAN_ND *>(mesh_api); } 00265 private: 00266 00267 }; 00268 00269 class ThreadInterface : public MeshInterfaceNanostack { 00270 public: 00271 int connect(); 00272 protected: 00273 MeshThread *get_mesh_api() const { return static_cast<MeshThread *>(mesh_api); } 00274 private: 00275 00276 }; 00277 00278 00279 #endif /* NANOSTACK_INTERFACE_H_ */
Generated on Tue Jul 12 2022 12:28:44 by
