int NanostackInterface::socket_connect(void *handle, const SocketAddress &addr) { // Validate parameters NanostackSocket * socket = static_cast<NanostackSocket *>(handle); if (NULL == handle) { MBED_ASSERT(false); return NSAPI_ERROR_NO_SOCKET; } nanostack_lock(); int ret; ns_address_t ns_addr; int random_port = socket->is_bound() ? 0 : 1; convert_mbed_addr_to_ns(&ns_addr, &addr); if (0 == ::socket_connect(socket->socket_id, &ns_addr, random_port)) { socket->set_connecting(&ns_addr); ret = 0; } else { ret = NSAPI_ERROR_DEVICE_ERROR; } nanostack_unlock(); tr_debug("socket_connect(socket=%p) sock_id=%d, ret=%i", socket, socket->socket_id, ret); return ret; }
int NanostackInterface::socket_sendto(void *handle, const SocketAddress &address, const void *data, unsigned int 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 (NANOSTACK_SOCKET_TCP == socket->proto) { tr_error("socket_sendto() not supported with SOCKET_STREAM!"); ret = NSAPI_ERROR_UNSUPPORTED; } else { ns_address_t ns_address; convert_mbed_addr_to_ns(&ns_address, &address); if (!socket->is_bound()) { socket->set_bound(); } int8_t send_to_status = ::socket_sendto(socket->socket_id, &ns_address, (uint8_t *)data, 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 == send_to_status) { ret = NSAPI_ERROR_WOULD_BLOCK; } else if (0 != send_to_status) { tr_error("socket_sendto: error=%d", send_to_status); ret = NSAPI_ERROR_DEVICE_ERROR; } else { ret = size; } } nanostack_unlock(); tr_debug("socket_sendto(socket=%p) sock_id=%d, ret=%i", socket, socket->socket_id, ret); return ret; }