Exemplo n.º 1
0
void comms_sock_free(comms_socket_t *m_sock)
{
    log_enter();
    if (!m_sock) {
        log_leave("No socket");
        return;
    }

    comms_sock_close(m_sock);
    netbuf_free(m_sock->in_buffer);
    netbuf_free(m_sock->out_buffer);
    BLAST_SAFE_FREE(m_sock->host);
    BLAST_SAFE_FREE(m_sock);
    log_leave("Freed");
}
Exemplo n.º 2
0
/**
 * Allocates space for a new socket structure
 * @return Pointer to the allocated structure or NULL on failure
 */
comms_socket_t *comms_sock_new(void)
{
    comms_socket_t *sock;

    sock = (comms_socket_t *) calloc(1, sizeof(struct comms_socket));
    if (!sock) return NULL;

    log_enter();

    sock->can_read = false;
    sock->can_write = false;
    sock->have_exception = false;
    sock->poll_handle = NULL;
    sock->state = NETSOCK_STATE_NONE;
    sock->fd = INVALID_SOCK;
    sock->last_errno = -1;
    sock->is_socket = true;
    sock->in_buffer = netbuf_new(1, 4 * 1024 * 1024);
    if (!sock->in_buffer) {
        BLAST_SAFE_FREE(sock);
        blast_err("Could not allocate in_buffer");
        return NULL;
    }
    sock->out_buffer = netbuf_new(1, 1024 * 1024);
    if (!sock->out_buffer) {
        netbuf_free(sock->in_buffer);
        BLAST_SAFE_FREE(sock);
        log_leave("Could not allocate out_buffer");
        return NULL;
    }

    log_leave();
    return sock;
}
Exemplo n.º 3
0
nsapi_size_or_error_t LWIP::socket_sendto(nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size)
{
    struct mbed_lwip_socket *s = (struct mbed_lwip_socket *)handle;
    ip_addr_t ip_addr;

    nsapi_addr_t addr = address.get_addr();
    if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) {
        return NSAPI_ERROR_PARAMETER;
    }

    struct netbuf *buf = netbuf_new();

    err_t err = netbuf_ref(buf, data, (u16_t)size);
    if (err != ERR_OK) {
        netbuf_free(buf);
        return err_remap(err);
    }

    err = netconn_sendto(s->conn, buf, &ip_addr, address.get_port());
    netbuf_delete(buf);
    if (err != ERR_OK) {
        return err_remap(err);
    }

    return size;
}
Exemplo n.º 4
0
void ICACHE_FLASH_ATTR
_setBulbState(int idx, uint16_t state) {
  struct netconn * sendconn;
  struct netbuf * sendbuf;
  char * data;
  err_t ret;

  if (!network_ready) return;

  sendconn = netconn_new( NETCONN_UDP );
  if (sendconn == NULL) {
    os_printf("[setbulb] Couldn't get netconn\r\n");
    return;
  }
  sendbuf = netbuf_new();
  if (sendbuf == NULL) {
    os_printf("[setbulb] couldn't get sendbuff\r\n");
    return;
  }
  data = netbuf_alloc(sendbuf, 38);
  if (data == NULL) {
    os_printf("[setbulb] couldn't alloc data\r\n");
    return;
  }

  getPktSetBulbPower(data, 38, idx, state);

  ret = netconn_bind(sendconn, &myaddr, 34435);
  if (ret != ERR_OK) {
    os_printf("[setbulb] connect error: %d\r\n", ret);
    return;
  }

  netconn_set_recvtimeout( sendconn, 1000);
  ret = netconn_connect(sendconn, getBulbAddr(idx), 56700);
  if (ret != ERR_OK) {
    os_printf("[setbulb] connect error: %d\r\n", ret);
    return;
  }
  ret = netconn_send(sendconn, sendbuf);
  if (ret != ERR_OK) {
    os_printf("[setbulb] send error: %d\r\n", ret);
  } else {
  }

  netbuf_free(sendbuf);
  netbuf_delete(sendbuf);
  netconn_disconnect(sendconn);
  netconn_delete(sendconn);
}
Exemplo n.º 5
0
static int lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, unsigned size)
{
    struct lwip_socket *s = (struct lwip_socket *)handle;
    if (addr.version != NSAPI_IPv4) {
        return NSAPI_ERROR_PARAMETER;
    }

    struct netbuf *buf = netbuf_new();
    err_t err = netbuf_ref(buf, data, (u16_t)size);
    if (err != ERR_OK) {
        netbuf_free(buf);
        return lwip_err_remap(err);;
    }

    err = netconn_sendto(s->conn, buf, (ip_addr_t *)addr.bytes, port);
    netbuf_delete(buf);
    if (err != ERR_OK) {
        return lwip_err_remap(err);
    }

    return size;
}
Exemplo n.º 6
0
static nsapi_size_or_error_t mbed_lwip_socket_sendto(nsapi_stack_t *stack, nsapi_socket_t handle, nsapi_addr_t addr, uint16_t port, const void *data, nsapi_size_t size)
{
    struct lwip_socket *s = (struct lwip_socket *)handle;
    ip_addr_t ip_addr;

    if (!convert_mbed_addr_to_lwip(&ip_addr, &addr)) {
        return NSAPI_ERROR_PARAMETER;
    }

    struct netbuf *buf = netbuf_new();
    err_t err = netbuf_ref(buf, data, (u16_t)size);
    if (err != ERR_OK) {
        netbuf_free(buf);
        return mbed_lwip_err_remap(err);
    }

    err = netconn_sendto(s->conn, buf, &ip_addr, port);
    netbuf_delete(buf);
    if (err != ERR_OK) {
        return mbed_lwip_err_remap(err);
    }

    return size;
}
Exemplo n.º 7
0
int 
main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("usage: %s ip:port\n", argv[0]);
        return -1;
    }

    char ip_port[24] = {0};
    strncpy(ip_port, argv[1], sizeof(ip_port));

    uint32_t addr = INADDR_ANY;
    uint16_t port = 0;

    char* tmp = strchr(ip_port, ':');
    if (tmp == NULL) {
        port = strtol(ip_port, NULL, 10);
    } else {
        port = strtol(tmp+1, NULL, 10);
        *tmp = '\0';
        addr = inet_addr(ip_port);
    }

    int max = 10;
    if (argc > 2)
        max = strtol(argv[2], NULL, 10);

    int buf_size = 64;
    if (argc > 3)
        buf_size = strtol(argv[3], NULL, 10);

    if (argc > 4)
        package_size = strtol(argv[4], NULL, 10);

    struct netev* ne = netev_create(max, 64*1024); 
    struct netbuf* wbuf = netbuf_create(max, buf_size*1024);

    s = malloc(sizeof(struct server));
    s->ne = ne;
    s->wbuf = wbuf; 
    s->clients = _alloc_clients(max);
    s->free_client = &s->clients[0];
    s->max = max;
    s->nconnected = 0;
    s->nconnectfail = 0;
    s->nclosedwrite = 0;
    s->nclosedread = 0;
    s->rstat = 0;
    s->wstat = 0;
    printf("connect to %s\n", argv[1]);
    if (_start_connect(addr, port, max) != 0) {
        printf("connect failed\n");
        return -1;
    }

    signal(SIGINT, _sigint_handler);
    for (;;) {
        int nfd = netev_poll(s->ne, 1);
        usleep(100000);
        printf("nfd %d, max %d, connect %d, fail %d, wclose %d, rclose %d,  wstat %d, rstat %d\n", 
                nfd, s->max, s->nconnected, s->nconnectfail, s->nclosedwrite, s->nclosedread, s->wstat, s->rstat);
        _start_write(s);
    }
    netev_free(s->ne);
    netbuf_free(s->wbuf);
    free(s->clients);
    free(s);
    return 0;
}