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: C027_Support
Dependents: HelloC027Interface U_Blox_DeviceConnector U_Blox_DeviceConnector U-Blox_Client
Fork of LWIPInterface by
Revision 14:3d1845f5cd81, committed 2016-04-21
- Comitter:
- Christopher Haster
- Date:
- Thu Apr 21 06:53:31 2016 -0500
- Parent:
- 13:e99aa98a083f
- Child:
- 15:21d4f56eb172
- Commit message:
- Match changes to the NSAPI
Changed in this revision
| C027Interface.cpp | Show annotated file Show diff for this revision Revisions of this file |
| C027Interface.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/C027Interface.cpp Fri Apr 15 16:08:40 2016 +0000
+++ b/C027Interface.cpp Thu Apr 21 06:53:31 2016 -0500
@@ -30,7 +30,7 @@
strcpy(_pin, simpin);
}
-int32_t C027Interface::connect(const char *apn, const char *username, const char *password)
+int C027Interface::connect(const char *apn, const char *username, const char *password)
{
// create the modem
_mdm = new MDMSerial;
@@ -56,122 +56,164 @@
}
}
- if (mdmOk)
- {
+ if (mdmOk) {
// join the internet connection
MDMParser::IP ip = _mdm->join(apn, username, password);
ipToString(ip, _ip_address);
mdmOk = (ip != NOIP);
}
- return mdmOk ? 0 : NS_ERROR_DEVICE_ERROR;
+ return mdmOk ? 0 : NSAPI_ERROR_DEVICE_ERROR;
}
-int32_t C027Interface::disconnect()
+int C027Interface::disconnect()
{
if (!_mdm->disconnect()) {
- return NS_ERROR_DEVICE_ERROR;
+ return NSAPI_ERROR_DEVICE_ERROR;
}
return 0;
}
-const char *C027Interface::getIPAddress()
+const char *C027Interface::get_ip_address()
{
return _ip_address;
}
-const char *C027Interface::getMACAddress()
+const char *C027Interface::get_mac_address()
{
return 0;
}
-SocketInterface *C027Interface::createSocket(ns_protocol_t proto)
+struct c027_socket {
+ int socket;
+ MDMParser::IpProtocol proto;
+ MDMParser::IP ip;
+ int port;
+};
+
+int C027Interface::socket_open(void **handle, nsapi_protocol_t proto)
{
- MDMParser::IpProtocol mdmproto = (proto == NS_UDP) ? MDMParser::IPPROTO_UDP : MDMParser::IPPROTO_TCP;
- int socket = _mdm->socketSocket(mdmproto);
- if (socket < 0) {
- return 0;
+ MDMParser::IpProtocol mdmproto = (proto == NSAPI_UDP) ? MDMParser::IPPROTO_UDP : MDMParser::IPPROTO_TCP;
+ int fd = _mdm->socketSocket(mdmproto);
+ if (fd < 0) {
+ return NSAPI_ERROR_NO_SOCKET;
}
- _mdm->socketSetBlocking(socket, 10000);
+ _mdm->socketSetBlocking(fd, 10000);
+ struct c027_socket *socket = new struct c027_socket;
+ if (!socket) {
+ return NSAPI_ERROR_NO_SOCKET;
+ }
- return new C027Socket(_mdm, socket, mdmproto);
+ socket->socket = fd;
+ socket->proto = mdmproto;
+ *handle = socket;
+ return 0;
}
-void C027Interface::destroySocket(SocketInterface *siface)
+int C027Interface::socket_close(void *handle)
{
- C027Socket *socket = (C027Socket *)siface;
- _mdm->socketFree(socket->_socket);
+ struct c027_socket *socket = (struct c027_socket *)handle;
+ _mdm->socketFree(socket->socket);
delete socket;
+ return 0;
}
+int C027Interface::socket_bind(void *handle, const SocketAddress &address)
+{
+ return NSAPI_ERROR_UNSUPPORTED;
+}
-// TCP SocketInterface implementation
-int32_t C027Interface::C027Socket::open(const char *ip, uint16_t port)
+int C027Interface::socket_listen(void *handle, int backlog)
{
- _ip = _mdm->gethostbyname(ip);
- _port = port;
+ return NSAPI_ERROR_UNSUPPORTED;
+}
+
+int C027Interface::socket_connect(void *handle, const SocketAddress &addr)
+{
+ struct c027_socket *socket = (struct c027_socket *)handle;
- if (_proto == MDMParser::IPPROTO_TCP) {
- if (!_mdm->socketConnect(_socket, ip, port)) {
- return NS_ERROR_DEVICE_ERROR;
- }
+ if (!_mdm->socketConnect(socket->socket, addr.get_ip_address(), addr.get_port())) {
+ return NSAPI_ERROR_DEVICE_ERROR;
}
return 0;
}
-int32_t C027Interface::C027Socket::close()
+int C027Interface::socket_accept(void **handle, void *server)
{
- if (_proto == MDMParser::IPPROTO_TCP) {
- if (!_mdm->socketClose(_socket)) {
- return NS_ERROR_DEVICE_ERROR;
- }
- }
-
- return 0;
+ return NSAPI_ERROR_UNSUPPORTED;
}
-int32_t C027Interface::C027Socket::send(const void *data, uint32_t size)
+int C027Interface::socket_send(void *handle, const void *data, unsigned size)
{
- int sent;
-
- if (_proto == MDMParser::IPPROTO_TCP) {
- sent = _mdm->socketSend(_socket, (const char *)data, size);
- } else {
- sent = _mdm->socketSendTo(_socket, _ip, _port, (const char *)data, size);
- }
-
- if (sent != size) {
- return NS_ERROR_DEVICE_ERROR;
+ struct c027_socket *socket = (struct c027_socket *)handle;
+
+ int sent = _mdm->socketSend(socket->socket, (const char *)data, size);
+ if (sent == SOCKET_ERROR) {
+ return NSAPI_ERROR_DEVICE_ERROR;
}
- return 0;
+ return sent;
}
-int32_t C027Interface::C027Socket::recv(void *data, uint32_t size)
+int C027Interface::socket_recv(void *handle, void *data, unsigned size)
{
- if (!_mdm->socketReadable(_socket)) {
- return NS_ERROR_WOULD_BLOCK;
+ struct c027_socket *socket = (struct c027_socket *)handle;
+ if (!_mdm->socketReadable(socket->socket)) {
+ return NSAPI_ERROR_WOULD_BLOCK;
}
- int recv;
-
- if (_proto == MDMParser::IPPROTO_TCP) {
- recv = _mdm->socketRecv(_socket, (char *)data, size);
- } else {
- MDMParser::IP ip;
- int port;
- recv = _mdm->socketRecvFrom(_socket, &ip, &port, (char *)data, size);
- }
-
+ int recv = _mdm->socketRecv(socket->socket, (char *)data, size);
if (recv == SOCKET_ERROR) {
- return NS_ERROR_DEVICE_ERROR;
+ return NSAPI_ERROR_DEVICE_ERROR;
}
return recv;
}
+int C027Interface::socket_sendto(void *handle, const SocketAddress &addr, const void *data, unsigned size)
+{
+ struct c027_socket *socket = (struct c027_socket *)handle;
+ MDMParser::IP ip = _mdm->gethostbyname(addr.get_ip_address());
+ uint16_t port = addr.get_port();
+
+ int sent = _mdm->socketSendTo(socket->socket, ip, port, (const char *)data, size);
+ if (sent == SOCKET_ERROR) {
+ return NSAPI_ERROR_DEVICE_ERROR;
+ }
+
+ return sent;
+}
+
+int C027Interface::socket_recvfrom(void *handle, SocketAddress *addr, void *data, unsigned size)
+{
+ struct c027_socket *socket = (struct c027_socket *)handle;
+ if (!_mdm->socketReadable(socket->socket)) {
+ return NSAPI_ERROR_WOULD_BLOCK;
+ }
+
+ MDMParser::IP ip;
+ int port;
+
+ int recv = _mdm->socketRecvFrom(socket->socket, &ip, &port, (char *)data, size);
+ if (recv == SOCKET_ERROR) {
+ return NSAPI_ERROR_DEVICE_ERROR;
+ }
+
+ if (addr) {
+ char buffer[NSAPI_IP_SIZE];
+ ipToString(ip, buffer);
+ addr->set_ip_address(buffer);
+ addr->set_port(port);
+ }
+
+ return recv;
+}
+
+void C027Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
+{
+}
--- a/C027Interface.h Fri Apr 15 16:08:40 2016 +0000
+++ b/C027Interface.h Thu Apr 21 06:53:31 2016 -0500
@@ -24,52 +24,145 @@
/** C027Interface class
* Implementation of the NetworkInterface for C027
*/
-class C027Interface : public CellularInterface
+class C027Interface : public NetworkStack, public CellularInterface
{
public:
- // Lifetime of C027Interface
+ /** C027Interfacelifetime
+ * @param simpin Optional PIN for the SIM
+ * @param debug Enable debugging
+ */
C027Interface(const char *simpin=0, bool debug=false);
- // Implementation of CellularInterface
- virtual int32_t connect(const char *apn=0, const char *username=0, const char *password=0);
- virtual int32_t disconnect();
+ /** Start the interface
+ *
+ * @param apn Optional name of the network to connect to
+ * @param username Optional username for your APN
+ * @param password Optional password for your APN
+ * @return 0 on success, negative error code on failure
+ */
+ virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0);
+
+ /** Stop the interface
+ * @return 0 on success, negative on failure
+ */
+ virtual int disconnect();
+
+ /** Get the internally stored IP address
+ * @return IP address of the interface or null if not yet connected
+ */
+ virtual const char *get_ip_address();
+
+ /** Get the internally stored MAC address
+ * @return MAC address of the interface
+ */
+ virtual const char *get_mac_address();
+
+protected:
+ /** Open a socket
+ * @param handle Handle in which to store new socket
+ * @param proto Type of socket to open, NSAPI_TCP or NSAPI_UDP
+ * @return 0 on success, negative on failure
+ */
+ virtual int socket_open(void **handle, nsapi_protocol_t proto);
+
+ /** Close the socket
+ * @param handle Socket handle
+ * @return 0 on success, negative on failure
+ * @note On failure, any memory associated with the socket must still
+ * be cleaned up
+ */
+ virtual int socket_close(void *handle);
+
+ /** Bind a server socket to a specific port
+ * @param handle Socket handle
+ * @param address Local address to listen for incoming connections on
+ * @return 0 on success, negative on failure.
+ */
+ virtual int socket_bind(void *handle, const SocketAddress &address);
+
+ /** Start listening for incoming connections
+ * @param handle Socket handle
+ * @param backlog Number of pending connections that can be queued up at any
+ * one time [Default: 1]
+ * @return 0 on success, negative on failure
+ */
+ virtual int socket_listen(void *handle, int backlog);
- // Implementation of NetworkInterface
- virtual const char *getIPAddress();
- virtual const char *getMACAddress();
+ /** Connects this TCP socket to the server
+ * @param handle Socket handle
+ * @param address SocketAddress to connect to
+ * @return 0 on success, negative on failure
+ */
+ virtual int socket_connect(void *handle, const SocketAddress &address);
+
+ /** Accept a new connection.
+ * @param handle Handle in which to store new socket
+ * @param server Socket handle to server to accept from
+ * @return 0 on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_accept(void **handle, void *server);
+
+ /** Send data to the remote host
+ * @param handle Socket handle
+ * @param data The buffer to send to the host
+ * @param size The length of the buffer to send
+ * @return Number of written bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_send(void *handle, const void *data, unsigned size);
- virtual SocketInterface *createSocket(ns_protocol_t proto);
- virtual void destroySocket(SocketInterface *socket);
+ /** Receive data from the remote host
+ * @param handle Socket handle
+ * @param data The buffer in which to store the data received from the host
+ * @param size The maximum length of the buffer
+ * @return Number of received bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_recv(void *handle, void *data, unsigned size);
+
+ /** Send a packet to a remote endpoint
+ * @param handle Socket handle
+ * @param address The remote SocketAddress
+ * @param data The packet to be sent
+ * @param size The length of the packet to be sent
+ * @return the number of written bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned size);
+
+ /** Receive a packet from a remote endpoint
+ * @param handle Socket handle
+ * @param address Destination for the remote SocketAddress or null
+ * @param buffer The buffer for storing the incoming packet data
+ * If a packet is too long to fit in the supplied buffer,
+ * excess bytes are discarded
+ * @param size The length of the buffer
+ * @return the number of received bytes on success, negative on failure
+ * @note This call is not-blocking, if this call would block, must
+ * immediately return NSAPI_ERROR_WOULD_WAIT
+ */
+ virtual int socket_recvfrom(void *handle, SocketAddress *address, void *buffer, unsigned size);
+
+ /** Register a callback on state change of the socket
+ * @param handle Socket handle
+ * @param callback Function to call on state change
+ * @param data Argument to pass to callback
+ * @note Callback may be called in an interrupt context.
+ */
+ virtual void socket_attach(void *handle, void (*callback)(void *), void *data);
private:
// Modem object
bool _debug;
MDMSerial *_mdm;
- char _ip_address[NS_IP_SIZE];
- char _mac_address[NS_MAC_SIZE];
+ char _ip_address[NSAPI_IP_SIZE];
+ char _mac_address[NSAPI_MAC_SIZE];
char _pin[sizeof "1234"];
-
- // Implementation of the TCP SocketInterface for C027
- class C027Socket : public SocketInterface
- {
- public:
-
- C027Socket(MDMSerial *mdm, int socket, MDMParser::IpProtocol proto)
- : _mdm(mdm), _socket(socket), _proto(proto) {}
- MDMSerial *_mdm;
- int _socket;
- MDMParser::IpProtocol _proto;
- MDMParser::IP _ip;
- int _port;
-
- // Implementation of SocketInterface
- virtual int32_t open(const char *ip, uint16_t port);
- virtual int32_t close();
-
- virtual int32_t send(const void *data, uint32_t size);
- virtual int32_t recv(void *data, uint32_t size);
- };
};
-
#endif
