/*---------------------------------------------------------------------------*/
static void request_recv(struct runicast_conn *c, const rimeaddr_t *from, uint8_t seqno)
{
  const char *filename;
  uint8_t seq;
  if(packetbuf_datalen() < 2) {
    printf("download: bad filename request (null)\n");
    return;
  }
  seq = ((uint8_t *)packetbuf_dataptr())[0];
  if(seq == req_last_seq) {
    printf("download: ignoring duplicate request\n");
    return;
  }
  req_last_seq = seq;
  filename = ((char *)packetbuf_dataptr()) + 1;

  printf("file requested: '%s'\n", filename);
  /* Initiate file transfer */
  leds_on(LEDS_GREEN);
  if(fd >= 0) {
    cfs_close(fd);
  }
  fd = cfs_open(filename, CFS_READ);
  if(fd < 0) {
    printf("download: bad filename request (no read access): %s\n", filename);
  }

  rucb_close(&rucb);
  rucb_open(&rucb, RUCB_CHANNEL, &rucb_call);
  rucb_send(&rucb, from);

}
示例#2
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(shell_download_process, ev, data)
{
  const char *nextptr;
  static rimeaddr_t addr;
  int len;
  char buf[32];

  PROCESS_BEGIN();

  /* Parse node addr */
  addr.u8[0] = shell_strtolong(data, &nextptr);
  if(nextptr == data || *nextptr != '.') {
    shell_output_str(&download_command,
        "download <node addr> <filename>: need node address", "");
    PROCESS_EXIT();
  }
  ++nextptr;
  addr.u8[1] = shell_strtolong(nextptr, &nextptr);

  /* Get the length of the file, excluding a terminating NUL character. */
  while(nextptr[0] == ' ') {
    nextptr++;
  }
  len = strlen(nextptr);

  /*snprintf(buf, sizeof(buf), "%d.%d", addr.u8[0], addr.u8[1]);*/
  /*shell_output_str(&download_command, "Downloading from: ", buf);*/

  if(len > PACKETBUF_SIZE - 32) {
    snprintf(buf, sizeof(buf), "%d", len);
    shell_output_str(&download_command, "filename too large: ", buf);
    PROCESS_EXIT();
  }

  /*shell_output_str(&download_command, "Downloading file: ", nextptr);*/

  /* Send file request */
  downloading = 1;
  rucb_open(&rucb, RUCB_CHANNEL, &rucb_call);
  packetbuf_clear();
  *((uint8_t *)packetbuf_dataptr()) = ++req_seq_counter;
  memcpy(((char *)packetbuf_dataptr()) + 1, nextptr, len + 1);
  packetbuf_set_datalen(len + 2);
  PRINTF("requesting '%s'\n", nextptr);
  runicast_send(&runicast, &addr, MAX_RETRANSMISSIONS);

  /* Wait for download to finish */
  leds_on(LEDS_BLUE);
  PROCESS_WAIT_UNTIL(!runicast_is_transmitting(&runicast) && !downloading);
  leds_off(LEDS_BLUE);

  rucb_close(&rucb);
  /*shell_output_str(&download_command, "Done!", "");*/

  PROCESS_END();
}