⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shell.c

📁 NXPl788上lwip的无操作系统移植,基于Embest开发板
💻 C
📖 第 1 页 / 共 3 页
字号:
  u16_t len;
  char buf[100];
  size_t slen;

#ifdef LWIP_DEBUG
  LWIP_UNUSED_ARG(i);
  slen = strlen(elem->name);
  netconn_write(conn, elem->name, slen, NETCONN_COPY);
#else /*  LWIP_DEBUG */
  len = (u16_t)sprintf(buf, "%d", i);
  slen = strlen(buf);
  netconn_write(conn, buf, slen, NETCONN_COPY);
#endif /*  LWIP_DEBUG */
  if(slen < 10) {
    netconn_write(conn, padding_10spaces, 10-slen, NETCONN_COPY);
  }

  len = (u16_t)sprintf(buf, " * available %"MEM_SIZE_F NEWLINE, elem->avail);
  netconn_write(conn, buf, len, NETCONN_COPY);
  len = (u16_t)sprintf(buf, "           * used %"MEM_SIZE_F NEWLINE, elem->used);
  netconn_write(conn, buf, len, NETCONN_COPY);
  len = (u16_t)sprintf(buf, "           * high water mark %"MEM_SIZE_F NEWLINE, elem->max);
  netconn_write(conn, buf, len, NETCONN_COPY);
  len = (u16_t)sprintf(buf, "           * errors %"STAT_COUNTER_F NEWLINE, elem->err);
  netconn_write(conn, buf, len, NETCONN_COPY);
  len = (u16_t)sprintf(buf, "           * illegal %"STAT_COUNTER_F NEWLINE, elem->illegal);
  netconn_write(conn, buf, len, NETCONN_COPY);
}
static void
com_stat_write_sys(struct netconn *conn, struct stats_syselem *elem, const char *name)
{
  u16_t len;
  char buf[100];
  size_t slen = strlen(name);

  netconn_write(conn, name, slen, NETCONN_COPY);
  if(slen < 10) {
    netconn_write(conn, padding_10spaces, 10-slen, NETCONN_COPY);
  }

  len = (u16_t)sprintf(buf, " * used %"STAT_COUNTER_F NEWLINE, elem->used);
  netconn_write(conn, buf, len, NETCONN_COPY);
  len = (u16_t)sprintf(buf, "           * high water mark %"STAT_COUNTER_F NEWLINE, elem->max);
  netconn_write(conn, buf, len, NETCONN_COPY);
  len = (u16_t)sprintf(buf, "           * errors %"STAT_COUNTER_F NEWLINE, elem->err);
  netconn_write(conn, buf, len, NETCONN_COPY);
}
static s8_t
com_stat(struct command *com)
{
#if PROTOCOL_STATS || MEMP_STATS
  size_t i;
#endif /* PROTOCOL_STATS || MEMP_STATS */
#if PROTOCOL_STATS
  size_t k;
  char buf[100];
  u16_t len;

  /* protocol stats, @todo: add IGMP */
  for(i = 0; i < num_protostats; i++) {
    size_t s = sizeof(struct stats_proto)/sizeof(STAT_COUNTER);
    STAT_COUNTER *c = &shell_stat_proto_stats[i]->xmit;
    LWIP_ASSERT("stats not in sync", s == sizeof(stat_msgs_proto)/sizeof(char*));
    netconn_write(com->conn, shell_stat_proto_names[i], strlen(shell_stat_proto_names[i]), NETCONN_COPY);
    for(k = 0; k < s; k++) {
      len = (u16_t)sprintf(buf, "%s%"STAT_COUNTER_F NEWLINE, stat_msgs_proto[k], c[k]);
      netconn_write(com->conn, buf, len, NETCONN_COPY);
    }
  }
#endif /* PROTOCOL_STATS */
#if MEM_STATS
  com_stat_write_mem(com->conn, &lwip_stats.mem, -1);
#endif /* MEM_STATS */
#if MEMP_STATS
  for(i = 0; i < MEMP_MAX; i++) {
    com_stat_write_mem(com->conn, &lwip_stats.memp[i], -1);
  }
#endif /* MEMP_STATS */
#if SYS_STATS
  com_stat_write_sys(com->conn, &lwip_stats.sys.sem,   "SEM       ");
  com_stat_write_sys(com->conn, &lwip_stats.sys.mutex, "MUTEX     ");
  com_stat_write_sys(com->conn, &lwip_stats.sys.sem,   "MBOX      ");
#endif /* SYS_STATS */

  return ESUCCESS;
}
#endif
/*-----------------------------------------------------------------------------------*/
static s8_t
com_send(struct command *com)
{
  int i;
  err_t err;
  size_t len;
  
  i = strtol(com->args[0], NULL, 10);

  if (i > NCONNS) {
    sendstr("Connection identifier too high."NEWLINE, com->conn);
    return ESUCCESS;
  }

  if (conns[i] == NULL) {
    sendstr("Connection identifier not in use."NEWLINE, com->conn);
    return ESUCCESS;
  }

  len = strlen(com->args[1]);
  com->args[1][len] = '\r';
  com->args[1][len + 1] = '\n';
  com->args[1][len + 2] = 0;
  
  err = netconn_write(conns[i], com->args[1], len + 3, NETCONN_COPY);
  if (err != ERR_OK) {
    sendstr("Could not send data: ", com->conn);
#ifdef LWIP_DEBUG
    sendstr(lwip_strerr(err), com->conn);
#else
    sendstr("(debugging must be turned on for error message to appear)", com->conn);
#endif /* LWIP_DEBUG */
    sendstr(NEWLINE, com->conn);
    return ESUCCESS;
  }
  
  sendstr("Data enqueued for sending."NEWLINE, com->conn);
  return ESUCCESS;
}
/*-----------------------------------------------------------------------------------*/
static s8_t
com_recv(struct command *com)
{
  int i;
  err_t err;
  struct netbuf *buf;
  u16_t len;
  
  i = strtol(com->args[0], NULL, 10);

  if (i > NCONNS) {
    sendstr("Connection identifier too high."NEWLINE, com->conn);
    return ESUCCESS;
  }

  if (conns[i] == NULL) {
    sendstr("Connection identifier not in use."NEWLINE, com->conn);
    return ESUCCESS;
  }

  err = netconn_recv(conns[i], &buf);
  if (err == ERR_OK) {
      
    netbuf_copy(buf, buffer, BUFSIZE);
    len = netbuf_len(buf);
    sendstr("Reading from connection:"NEWLINE, com->conn);
    netconn_write(com->conn, buffer, len, NETCONN_COPY);
    netbuf_delete(buf);
  } else {
    sendstr("EOF."NEWLINE, com->conn); 
  }
  err = netconn_err(conns[i]);
  if (err != ERR_OK) {
    sendstr("Could not receive data: ", com->conn);
#ifdef LWIP_DEBUG
    sendstr(lwip_strerr(err), com->conn);
#else
    sendstr("(debugging must be turned on for error message to appear)", com->conn);
#endif /* LWIP_DEBUG */
    sendstr(NEWLINE, com->conn);
    return ESUCCESS;
  }
  return ESUCCESS;
}
/*-----------------------------------------------------------------------------------*/
static s8_t
com_udpc(struct command *com)
{
  ip_addr_t ipaddr;
  u16_t lport, rport;
  int i;
  err_t err;
  long tmp;

  tmp = strtol(com->args[0], NULL, 10);
  if((tmp < 0) || (tmp > 0xffff)) {
    sendstr("Invalid port number."NEWLINE, com->conn);
    return ESUCCESS;
  }
  lport = (u16_t)tmp;
  if (ipaddr_aton(com->args[1], &ipaddr) == -1) {
    sendstr(strerror(errno), com->conn);
    return ESYNTAX;
  }
  tmp = strtol(com->args[2], NULL, 10);
  if((tmp < 0) || (tmp > 0xffff)) {
    sendstr("Invalid port number."NEWLINE, com->conn);
    return ESUCCESS;
  }
  rport = (u16_t)tmp;

  /* Find the first unused connection in conns. */
  for(i = 0; i < NCONNS && conns[i] != NULL; i++);

  if (i == NCONNS) {
    sendstr("No more connections available, sorry."NEWLINE, com->conn);
    return ESUCCESS;
  }

  sendstr("Setting up UDP connection from port ", com->conn);
  netconn_write(com->conn, com->args[0], strlen(com->args[0]), NETCONN_COPY);
  sendstr(" to ", com->conn);
  netconn_write(com->conn, com->args[1], strlen(com->args[1]), NETCONN_COPY);
  sendstr(":", com->conn);
  netconn_write(com->conn, com->args[2], strlen(com->args[2]), NETCONN_COPY);
  sendstr(NEWLINE, com->conn);

  conns[i] = netconn_new(NETCONN_UDP);
  if (conns[i] == NULL) {    
    sendstr("Could not create connection identifier (out of memory)."NEWLINE, com->conn); 
    return ESUCCESS;
  }

  err = netconn_connect(conns[i], &ipaddr, rport);
  if (err != ERR_OK) {
    netconn_delete(conns[i]);
    conns[i] = NULL;
    sendstr("Could not connect to remote host: ", com->conn);
#ifdef LWIP_DEBUG
    sendstr(lwip_strerr(err), com->conn);
#else
    sendstr("(debugging must be turned on for error message to appear)", com->conn);
#endif /* LWIP_DEBUG */
    sendstr(NEWLINE, com->conn);
    return ESUCCESS;
  }

  err = netconn_bind(conns[i], IP_ADDR_ANY, lport);
  if (err != ERR_OK) {
    netconn_delete(conns[i]);
    conns[i] = NULL;
    sendstr("Could not bind: ", com->conn);
#ifdef LWIP_DEBUG
    sendstr(lwip_strerr(err), com->conn);
#else
    sendstr("(debugging must be turned on for error message to appear)", com->conn);
#endif /* LWIP_DEBUG */
    sendstr(NEWLINE, com->conn);
    return ESUCCESS;
  }

  sendstr("Connection set up, connection identifier is ", com->conn);
  snprintf((char *)buffer, sizeof(buffer), "%d"NEWLINE, i);
  netconn_write(com->conn, buffer, strlen((const char *)buffer), NETCONN_COPY);
  
  return ESUCCESS;
}
/*-----------------------------------------------------------------------------------*/
static s8_t
com_udpl(struct command *com)
{
  ip_addr_t ipaddr;
  u16_t lport, rport;
  int i;
  err_t err;
  long tmp;

  tmp = strtol(com->args[0], NULL, 10);
  if((tmp < 0) || (tmp > 0xffff)) {
    sendstr("Invalid port number."NEWLINE, com->conn);
    return ESUCCESS;
  }
  lport = (u16_t)tmp;
  if (ipaddr_aton(com->args[1], &ipaddr) == -1) {
    sendstr(strerror(errno), com->conn);
    return ESYNTAX;
  }
  tmp = strtol(com->args[2], NULL, 10);
  if((tmp < 0) || (tmp > 0xffff)) {
    sendstr("Invalid port number."NEWLINE, com->conn);
    return ESUCCESS;
  }
  rport = (u16_t)tmp;

  /* Find the first unused connection in conns. */
  for(i = 0; i < NCONNS && conns[i] != NULL; i++);

  if (i == NCONNS) {
    sendstr("No more connections available, sorry."NEWLINE, com->conn);
    return ESUCCESS;
  }

  sendstr("Setting up UDP-Lite connection from port ", com->conn);
  netconn_write(com->conn, com->args[0], strlen(com->args[0]), NETCONN_COPY);
  sendstr(" to ", com->conn);
  netconn_write(com->conn, com->args[1], strlen(com->args[1]), NETCONN_COPY);
  sendstr(":", com->conn);
  netconn_write(com->conn, com->args[2], strlen(com->args[2]), NETCONN_COPY);
  sendstr(NEWLINE, com->conn);

  conns[i] = netconn_new(NETCONN_UDPLITE);
  if (conns[i] == NULL) {    
    sendstr("Could not create connection identifier (out of memory)."NEWLINE, com->conn); 
    return ESUCCESS;
  }

  err = netconn_connect(conns[i], &ipaddr, rport);
  if (err != ERR_OK) {
    netconn_delete(conns[i]);
    conns[i] = NULL;
    sendstr("Could not connect to remote host: ", com->conn);
#ifdef LWIP_DEBUG
    sendstr(lwip_strerr(err), com->conn);
#else
    sendstr("(debugging must be turned on for error message to appear)", com->conn);
#endif /* LWIP_DEBUG */
    sendstr(NEWLINE, com->conn);
    return ESUCCESS;
  }

  err = netconn_bind(conns[i], IP_ADDR_ANY, lport);
  if (err != ERR_OK) {
    netconn_delete(conns[i]);
    conns[i] = NULL;
    sendstr("Could not bind: ", com->conn);
#ifdef LWIP_DEBUG
    sendstr(lwip_strerr(err), com->conn);
#else
    sendstr("(debugging must be turned on for error message to appear)", com->conn);
#endif /* LWIP_DEBUG */
    sendstr(NEWLINE, com->conn);
    return ESUCCESS;
  }

  sendstr("Connection set up, connection identifier is ", com->conn);
  snprintf((char *)buffer, sizeof(buffer), "%d"NEWLINE, i);
  netconn_write(com->conn, buffer, strlen((const char *)buffer), NETCONN_COPY);
  
  return ESUCCESS;
}
/*-----------------------------------------------------------------------------------*/
static s8_t
com_udpn(struct command *com)
{
  ip_addr_t ipaddr;
  u16_t lport, rport;
  int i;
  err_t err;
  long tmp;

  tmp = strtol(com->args[0], NULL, 10);
  if((tmp < 0) || (tmp > 0xffff)) {
    sendstr("Invalid port number."NEWLINE, com->conn);
    return ESUCCESS;
  }
  lport = (u16_t)tmp;
  if (ipaddr_aton(com->args[1], &ipaddr) == -1) {
    sendstr(strerror(errno), com->conn);
    return ESYNTAX;
  }
  tmp = strtol(com->args[2], NULL, 10);
  if((tmp < 0) || (tmp > 0xffff)) {
    sendstr("Invalid port number."NEWLINE, com->conn);
    return ESUCCESS;
  }
  rport = (u16_t)tmp;

  /* Find the first unused connection in conns. */
  for(i = 0; i < NCONNS && conns[i] != NULL; i++);

  if (i == NCONNS) {
    sendstr("No more connections available, sorry."NEWLINE, com->conn);
    return ESUCCESS;
  }

  sendstr("Setting up UDP connection without checksums from port ", com->conn);
  netconn_write(com->conn, com->args[0], strlen(com->args[0]), NETCONN_COPY);
  sendstr(" to ", com->conn);
  netconn_write(com->conn, com->args[1], strlen(com->args[1]), NETCONN_COPY);
  sendstr(":", com->conn);
  netconn_write(com->conn, com->args[2], strlen(com->args[2]), NETCONN_COPY);
  sendstr(NEWLINE, com->conn);

  conns[i] = netconn_new(NETCONN_UDPNOCHKSUM);
  if (conns[i] == NULL) {    
    sendstr("Could not create connection identifier (out of memory)."NEWLINE, com->conn); 
    return ESUCCESS;
  }

  err = netconn_connect(conns[i], &ipaddr, rport);
  if (err != ERR_OK) {
    netconn_delete(conns[i]);
    conns[i] = NULL;
    sendstr("Could not connect to remote host: ", com->conn);
#ifdef LWIP_DEBUG
    sendstr(lwip_strerr(err), com->conn);
#else

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -