nsapi_error_t NanostackInterface::socket_connect(void *handle, const SocketAddress &addr) { // Validate parameters NanostackSocket *socket = static_cast<NanostackSocket *>(handle); nsapi_error_t ret; if (handle == NULL) { MBED_ASSERT(false); return NSAPI_ERROR_NO_SOCKET; } NanostackLockGuard lock; if (addr.get_ip_version() != NSAPI_IPv6) { ret = NSAPI_ERROR_UNSUPPORTED; goto out; } if (socket->closed()) { ret = NSAPI_ERROR_NO_CONNECTION; goto out; } if (socket->is_connecting()) { ret = NSAPI_ERROR_ALREADY; goto out; } if (socket->is_connected()) { ret = NSAPI_ERROR_IS_CONNECTED; goto out; } ns_address_t ns_addr; convert_mbed_addr_to_ns(&ns_addr, &addr); if (::socket_connect(socket->socket_id, &ns_addr, 0) == 0) { if (socket->proto == SOCKET_TCP) { socket->set_connecting(&ns_addr); ret = NSAPI_ERROR_IN_PROGRESS; } else { ret = NSAPI_ERROR_OK; } } else { ret = NSAPI_ERROR_DEVICE_ERROR; } out: tr_debug("socket_connect(socket=%p) sock_id=%d, ret=%i", socket, socket->socket_id, ret); return ret; }
int NanostackInterface::socket_send(void *handle, const void *p, unsigned size) { // Validate parameters NanostackSocket * socket = static_cast<NanostackSocket *>(handle); if (NULL == handle) { MBED_ASSERT(false); return NSAPI_ERROR_NO_SOCKET; } nanostack_lock(); int ret; if (socket->closed()) { ret = NSAPI_ERROR_NO_CONNECTION; } else if (socket->is_connecting()) { ret = NSAPI_ERROR_WOULD_BLOCK; } else { ret = ::socket_sendto(socket->socket_id, &socket->ns_address, (uint8_t*)p, size); /* * \return 0 on success. * \return -1 invalid socket id. * \return -2 Socket memory allocation fail. * \return -3 TCP state not established. * \return -4 Socket tx process busy. * \return -5 TLS authentication not ready. * \return -6 Packet too short. * */ if (-4 == ret) { ret = NSAPI_ERROR_WOULD_BLOCK; } else if (ret != 0) { tr_warning("socket_sendto ret %i, socket_id %i", ret, socket->socket_id); ret = NSAPI_ERROR_DEVICE_ERROR; } else { ret = size; } } nanostack_unlock(); tr_debug("socket_send(socket=%p) sock_id=%d, ret=%i", socket, socket->socket_id, ret); return ret; }