Exemplo n.º 1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(shell_append_process, ev, data)
{
  static int fd = 0;
  struct shell_input *input;

  PROCESS_EXITHANDLER(cfs_close(fd));
  
  PROCESS_BEGIN();

  fd = cfs_open(data, CFS_WRITE | CFS_APPEND);

  if(fd < 0) {
    shell_output_str(&append_command,
		     "append: could not open file for writing: ", data);
  } else {
    while(1) {
      PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);
      input = data;
      /*    printf("cat input %d %d\n", input->len1, input->len2);*/
      if(input->len1 + input->len2 == 0) {
	cfs_close(fd);
	PROCESS_EXIT();
      }
      
      cfs_write(fd, input->data1, input->len1);
      cfs_write(fd, input->data2, input->len2);
      
      shell_output(&append_command,
		   input->data1, input->len1,
		   input->data2, input->len2);
    }
  }
  
  PROCESS_END();
}
PROCESS_THREAD(cfs_write_test, ev, data) {
    PROCESS_BEGIN();

    char str[20], str2[20];
    strcpy(str, "Vot bl'a nahuj!");

    int fd = cfs_open("a", CFS_WRITE);
    if (fd == -1) {
        cfs_close(fd);
        fd = cfs_open("a", CFS_WRITE);
        if (fd == -1) {
            alarma(5);
        }
    }
    cfs_write(fd, (void *)str, strlen(str));
    cfs_close(fd);

    fd = cfs_open("a", CFS_READ);
    cfs_read(fd, (void *)str2, strlen(str));
    cfs_close(fd);

    strcat(str2, " Da yopt!");

    fd = cfs_open("a", CFS_WRITE);
    cfs_write(fd, (void *)str2, strlen(str2));
    cfs_close(fd);

    ledd_on(PB5);
    PROCESS_END();
}
Exemplo n.º 3
0
int
logging_log(enum loggingtypes type, void *log, uint8_t len)
{
  if(log_state == LOGGING_OK_TO_LOG){
    /* create a log header, with time stamp etc */
    struct logheader e;
    log_state = LOGGING_BUSY;
    logheader.divider = LOGGING_LOGDIVIDER;
    logheader.time = (uint32_t) clock_time());
    logheader.type = type;
    logheader.len = len;

    if(cfs_write(fd, &e, LOG_HDR_LEN) == -1){
      /* error when storing the log header */
      printf("Log: ** FATAL ** could not write log hdr.\n");
      log_state = LOGGING_FATAL_ERROR;
      return 1;
    }

    if(cfs_write(fd, log, len) == -1){
      /* error when storing the log */
      printf("Log: ** FATAL ** could not write log.\n");
      log_state = LOGGING_FATAL_ERROR;
      return 2;
    }

    /* storing the log was successful */
    log_state = LOGGING_OK_TO_LOG;
    return 0;
  }

  /* logging was not ready to store the log, dropping */
  return 3;
}
Exemplo n.º 4
0
/*-----------------------------------------------------------------------------------*/
static void
savescript(void)
{
  char line[40];
  /*  struct c64_fs_file f;*/
  int f;
  
  f = cfs_open("@:config.cfg", CFS_WRITE);
  if(f == -1) {
    log_message("Could not open config.cfg", "");
    return;
  }
  if(cfs[0] != 0) {
	int len = makeline(line, 'c',cfs);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 'c', cfs));
  }
  if(theme[0] != 0) {
	int len = makeline(line, 't',theme);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 't', theme));
  }
  if(driver[0] != 0) {
	int len = makeline(line, 'n',driver);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 'n', driver));
  }
  if(ipaddr[0] != 0) {
	int len = makeline(line, 'i',ipaddr);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 'i', ipaddr));
  }
  if(netmask[0] != 0) {
	int len = makeline(line, 'm',netmask);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 'm', netmask));
  }
  if(gateway[0] != 0) {
	int len = makeline(line, 'r',gateway);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 'r', gateway));
  }
  if(dnsserver[0] != 0) {
	int len = makeline(line, 'd',dnsserver);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 'd', dnsserver));
  }
  
  if(screensaver[0] != 0) {
	int len = makeline(line, 's',screensaver);
	cfs_write(f, line, len);
//    cfs_write(f, line, makeline(line, 's', screensaver));
  }
  
  strcpy(line, ".\n\0\n\n\n");
  cfs_write(f, line, strlen(line));
  
  cfs_close(f);
  
}
Exemplo n.º 5
0
db_result_t
storage_put_row(relation_t *rel, storage_row_t row)
{
  cfs_offset_t end;
  unsigned remaining;
  int r;
  unsigned char *last_byte;
#if DB_FEATURE_INTEGRITY
  int missing_bytes;
  char buf[rel->row_length];
#endif

  end = cfs_seek(rel->tuple_storage, 0, CFS_SEEK_END);
  if(end == (cfs_offset_t)-1) {
    return DB_STORAGE_ERROR;
  }

#if DB_FEATURE_INTEGRITY
  missing_bytes = end % rel->row_length;
  if(missing_bytes > 0) {
    memset(buf, 0xff, sizeof(buf));
    r = cfs_write(rel->tuple_storage, buf, sizeof(buf));
    if(r != missing_bytes) {
      return DB_STORAGE_ERROR;
    }
  }
#endif

  /* Ensure that last written byte is separated from 0, to make file
     lengths correct in Coffee. */
  last_byte = row + rel->row_length - 1;
  *last_byte ^= ROW_XOR;

  remaining = rel->row_length;
  do {
    r = cfs_write(rel->tuple_storage, row, remaining);
    if(r < 0) {
      PRINTF("DB: Failed to store %u bytes\n", remaining);
      *last_byte ^= ROW_XOR;
      return DB_STORAGE_ERROR;
    }
    row += r;
    remaining -= r;
  } while(remaining > 0);

  PRINTF("DB: Stored a of %d bytes\n", rel->row_length);

  *last_byte ^= ROW_XOR;

  return DB_OK;
}
Exemplo n.º 6
0
/*---------------------------------------------------------------------------*/
static void
write_byte(int fd, uint8_t c)
{
#if DATA_AS_HEX
  uint8_t hex[2];
  sprintf(hex, "%02x", c);
  if(cfs_write(fd, hex, 2) != 2) {
    printf("err #1\n");
  }
#else /* DATA_AS_HEX */
  if(cfs_write(fd, &c, 1) != 1) {
    printf("err #2\n");
  }
#endif /* DATA_AS_HEX */
}/*---------------------------------------------------------------------------*/
Exemplo n.º 7
0
void TestScriptEngine(CuTest *tc)
{
    // COPY A SCRIPT FILE FOR SCRIPT TESTING
  FILE* fp = fopen("script1.amx", "rb");
  int fd = cfs_open("/script1.amx", CFS_WRITE);

  if (fp != NULL && fd != -1)
  {
    // copy the file
    char buf[1024];
    int length;

    length = fread(buf, 1, 1024, fp);
    while(length > 0)
    {
      cfs_write(fd, buf, length);
      length = fread(buf, 1, 1024, fp);
    }
    fclose(fp);
    cfs_close(fd);
  }

  test_window(&script_process, "/script1.amx");
  test_window(&script_process, "/notexist.amx");
}
Exemplo n.º 8
0
/*-----------------------------------------------------------------------------------*/
static void
apply_tcpipconfig(void)
{
  int file = cfs_open("contiki.cfg", CFS_READ);
  int size = cfs_read(file, uip_buf, 100);
  cfs_close(file);

  nullterminate(ipaddr);
  uiplib_ipaddrconv(ipaddr, (uip_ipaddr_t *)&uip_buf[0]);

  nullterminate(netmask);
  uiplib_ipaddrconv(netmask, (uip_ipaddr_t *)&uip_buf[4]);

  nullterminate(gateway);
  uiplib_ipaddrconv(gateway, (uip_ipaddr_t *)&uip_buf[8]);
  
#if WITH_DNS
  nullterminate(dnsserver);
  uiplib_ipaddrconv(dnsserver, (uip_ipaddr_t *)&uip_buf[12]);
#endif /* WITH_DNS */

  file = cfs_open("contiki.cfg", CFS_WRITE);
  cfs_write(file, uip_buf, size);
  cfs_close(file);
}
Exemplo n.º 9
0
ssize_t
_write_r(struct _reent *r, int fd, const void *ptr, size_t len) {

  if (fd < 0) {
    /* invalid file descriptor */
    r->_errno = EBADF;
    return -1;
  }

  if (fd >= MAX_OPEN_DEVICES) {
    int ret;
    /* CFS file */
    fd -= MAX_OPEN_DEVICES; /* Remap to CFS FD number */
    /* this is not really reentrant */
    ret = cfs_write(fd, (const char *)ptr, len);
    if (ret < 0) {
      r->_errno = EBADF;
    }
    return ret;
  }
  if (devoptab_list[fd] == NULL) {
    /* nothing mapped on that FD */
    r->_errno = EBADF;
    return -1;
  }
  if (devoptab_list[fd]->write_r == NULL) {
    /* Function not implemented */
    r->_errno = ENOSYS;
    return -1;
  }
  /* Call method from device operations table */
  return devoptab_list[fd]->write_r(r, fd, ptr, len);
}
Exemplo n.º 10
0
db_result_t
storage_put_attribute(relation_t *rel, attribute_t *attr)
{
  int fd;
  struct attribute_record record;
  int r;

  PRINTF("DB: put_attribute(%s, %s)\n", rel->name, attr->name);

  fd = cfs_open(rel->name, CFS_WRITE | CFS_APPEND);
  if(fd < 0) {
    return DB_STORAGE_ERROR;
  }

  memset(&record.name, 0, sizeof(record.name));
  memcpy(record.name, attr->name, sizeof(record.name));
  record.domain = attr->domain;
  record.element_size = attr->element_size;
  r = cfs_write(fd, &record, sizeof(record));
  if(r != sizeof(record)) {
    cfs_close(fd);
    cfs_remove(rel->name);
    return DB_STORAGE_ERROR;
  }

  cfs_close(fd);
  return DB_OK;
}
Exemplo n.º 11
0
db_result_t
storage_put_index(index_t *index)
{
  char filename[INDEX_NAME_LENGTH];
  int fd;
  int r;
  struct index_record record;
  db_result_t result;

  merge_strings(filename, index->rel->name, INDEX_NAME_SUFFIX);

  fd = cfs_open(filename, CFS_WRITE | CFS_APPEND);
  if(fd < 0) {
    return DB_STORAGE_ERROR;
  }

  strcpy(record.attribute_name, index->attr->name);
  memcpy(record.file_name, index->descriptor_file, sizeof(record.file_name));
  record.type = index->type;

  result = DB_OK;
  r = cfs_write(fd, &record, sizeof(record));
  if(r < sizeof(record)) {
    result = DB_STORAGE_ERROR;
  } else {
    PRINTF("DB: Wrote an index record for %s.%s, type %d\n",
      index->rel->name, index->attr->name, record.type);
  }

  cfs_close(fd);

  return result;
}
Exemplo n.º 12
0
/*---------------------------------------------------------------------------*/
static void
write_chunk(struct rudolph2_conn *c, int offset, int flag,
	    uint8_t *data, int datalen)
{
  int fd;
#if CONTIKI_TARGET_NETSIM
  {
    char buf[100];
    sprintf(buf, "%d%%", (100 * (offset + datalen)) / FILESIZE);
    ether_set_text(buf);
  }
#endif /* CONTIKI_TARGET_NETSIM */

  if(flag == RUDOLPH2_FLAG_NEWFILE) {
    printf("+++ rudolph2 new file incoming at %lu\n", clock_time());
    leds_on(LEDS_RED);
    fd = cfs_open("codeprop.out", CFS_WRITE);
  } else {
    fd = cfs_open("codeprop.out", CFS_WRITE + CFS_APPEND);
  }
  
  if(datalen > 0) {
    int ret;
    cfs_seek(fd, offset, CFS_SEEK_SET);
    ret = cfs_write(fd, data, datalen);
    printf("+++ rudolph2 offset %d, length %d\n", offset, datalen);
  }

  cfs_close(fd);

  if(flag == RUDOLPH2_FLAG_LASTCHUNK) {
    int i;
    printf("+++ rudolph2 entire file received at %d, %d\n",
	   rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
    leds_off(LEDS_RED);
    leds_on(LEDS_YELLOW);

    fd = cfs_open("hej", CFS_READ);
    for(i = 0; i < FILESIZE; ++i) {
      unsigned char buf;
      int r = cfs_read(fd, &buf, 1);
      if (r != 1) {
	printf("%d.%d: error: read failed at %d\n",
	       rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	       i);
	break;
      }       
      else if(buf != (unsigned char)i) {
	printf("%d.%d: error: diff at %d, %d != %d\n",
	       rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1],
	       i, (unsigned char)i, buf);
	break;
      }
    }
#if CONTIKI_TARGET_NETSIM
    ether_send_done();
#endif
    cfs_close(fd);
  }
}
Exemplo n.º 13
0
/* Flush tmpdata to CFS */
static int
queuebuf_flush_tmpdata(void)
{
  int fileid, fd, ret;
  cfs_offset_t offset;
  if(tmpdata_qbuf) {
    queuebuf_remove_from_file(tmpdata_qbuf->swap_id);
    tmpdata_qbuf->swap_id = get_new_swap_id();
    if(tmpdata_qbuf->swap_id == -1) {
      return -1;
    }
    fileid = tmpdata_qbuf->swap_id / NQBUF_PER_FILE;
    offset = (tmpdata_qbuf->swap_id % NQBUF_PER_FILE) * sizeof(struct queuebuf_data);
    fd = qbuf_files[fileid].fd;
    ret = cfs_seek(fd, offset, CFS_SEEK_SET);
    if(ret == -1) {
      PRINTF("queuebuf_flush_tmpdata: cfs seek error\n");
      return -1;
    }
    ret = cfs_write(fd, &tmpdata, sizeof(struct queuebuf_data));
    if(ret == -1) {
      PRINTF("queuebuf_flush_tmpdata: cfs write error\n");
      return -1;
    }
  }
  return 0;
}
Exemplo n.º 14
0
static int recv_data(int fd, int block) {
    int len;
    tftp_header *h = (tftp_header *)uip_appdata;
    len = cfs_write(fd, h->data, uip_datalen()-4);
    PRINTF("< block %d (len %d)\n", block, len);
    return len;
}
Exemplo n.º 15
0
/*---------------------------------------------------------------------------*/
static void
copy_file_from_romfs_to_cfs(const char * from, const char * to)
{
  static char buf[128];
  cfs_offset_t filesize, read, pos;

  /* Format CFS */
  cfs_coffee_format();

  /* Open file for writing in CFS */
  int cfs_fd = cfs_open(to, CFS_WRITE);

  /* Open file for reading in ROMFS */
  int rom_fd = romfs_open(from, CFS_READ);

  /* Determine file size */
  filesize = romfs_seek(rom_fd, 0, CFS_SEEK_END) - 1;

  /* Restore offset to start of file */
  romfs_seek(rom_fd, 0, CFS_SEEK_SET);

  /* Copy file data from romfs to cfs in chunks of 128 bytes */
  for (pos = 0; pos < filesize; pos += read) {
    read = romfs_read(rom_fd, buf, sizeof(buf));
    cfs_write(cfs_fd, buf, read);
  }

  /* Close both files */
  cfs_close(cfs_fd);
  romfs_close(rom_fd);
}
Exemplo n.º 16
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(shell_write_process, ev, data)
{
  static int fd = 0;
  struct shell_input *input;
  int r;

  PROCESS_EXITHANDLER(cfs_close(fd));
  
  PROCESS_BEGIN();

  fd = cfs_open(data, CFS_WRITE);

  if(fd < 0) {
    shell_output_str(&write_command,
		     "write: could not open file for writing: ", data);
  } else {
    while(1) {
      PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);
      input = data;
      /*    printf("cat input %d %d\n", input->len1, input->len2);*/
      if(input->len1 + input->len2 == 0) {
	cfs_close(fd);
	PROCESS_EXIT();
      }

      r = 0;      
      if(input->len1 > 0) {
	r = cfs_write(fd, input->data1, input->len1);
      }

      if(r >= 0 && input->len2 > 0) {
	r = cfs_write(fd, input->data2, input->len2);
      }

      if(r < 0) {
	shell_output_str(&write_command, "write: could not write to the file",
			 NULL);
      } else {
	shell_output(&write_command,
		     input->data1, input->len1,
		     input->data2, input->len2);
      }
    }
  }
  
  PROCESS_END();
}
Exemplo n.º 17
0
static int
sendDelugeGroup(void* inst, ContainerRoot* model)
{
	// so, we receive a new model to distribute
	DelugeGroup* instance = (DelugeGroup*) inst;

	PRINTF("Sending with DELUGE %p %p\n", instance->lastReceivedModel, model);

	/* we don't want to resend the model as if we were the creators if we just received */
	if (instance->lastReceivedModel == model){
		return 0;
	}

	PRINTF("Sending the model through deluge\n");
	
	// TODO serialize model to a file

	// TODO calculate number of pages in the file
	
	int fd = cfs_open(instance->fileNameWithModel, CFS_READ);
	cfs_offset_t offset = cfs_seek(fd, 0, CFS_SEEK_END);
	uint16_t nPages = offset / S_PAGE;
	cfs_close(fd);
	if (offset % S_PAGE != 0) {
		
		uint16_t mod = 	offset % S_PAGE;
		PRINTF("INFO: +++++++++++++++++++++++++ %d\n", mod);
		mod = S_PAGE - mod;
		char* buf = (char*)malloc(mod);
		memset(buf, ' ', mod);
		fd = cfs_open(instance->fileNameWithModel, CFS_WRITE | CFS_APPEND);
		cfs_seek(fd, 0, CFS_SEEK_END);
		cfs_write(fd, buf, mod);
		cfs_close(fd);
		free(buf);
		nPages++;
	}

	// set my local announcement to the new version
	instance->info.version = instance->info.version + 1;
	instance->info.nr_pages = nPages;
	
	// FIXME distribute announcement to other motes
#if 0
	announcement_bump(&instance->a);
#endif

	// activate deluge
	if (deluge_disseminate(instance->fileNameWithModel, 1, NULL)) {
		PRINTF("ERROR: some problem dissemineting\n");
	}
	else {
		PRINTF("INFO: dissemineting new version of the file with version %d\n", newVersion);
	}
	
	return 0;
}
Exemplo n.º 18
0
/*---------------------------------------------------------------------------*/
static int
coffee_test_append(void)
{
  int error;
  int afd;
  unsigned char buf[256], buf2[11];
  int r, i, j, total_read;
#define APPEND_BYTES 1000
#define BULK_SIZE 10

  cfs_remove("T2");

  /* Test 1 and 2: Append data to the same file many times. */
  for(i = 0; i < APPEND_BYTES; i += BULK_SIZE) {
    afd = cfs_open("T3", CFS_WRITE | CFS_APPEND);
    if(afd < 0) {
      FAIL(1);
    }
    for(j = 0; j < BULK_SIZE; j++) {
      buf[j] = 1 + ((i + j) & 0x7f);
    }
    if((r = cfs_write(afd, buf, BULK_SIZE)) != BULK_SIZE) {
      printf("r=%d\n", r);
      FAIL(2);
    }
    cfs_close(afd);
  }

  /* Test 3-6: Read back the data written previously and verify that it 
     is correct. */
  afd = cfs_open("T3", CFS_READ);
  if(afd < 0) {
    FAIL(3);
  }
  total_read = 0;
  while((r = cfs_read(afd, buf2, sizeof(buf2))) > 0) {
    for(j = 0; j < r; j++) {
      if(buf2[j] != 1 + ((total_read + j) & 0x7f)) {
	FAIL(4);
      }
    }
    total_read += r;
  }
  if(r < 0) {
    FAIL(5);
  }
  if(total_read != APPEND_BYTES) {
    FAIL(6);
  }
  cfs_close(afd);

  error = 0;
end:
  cfs_close(afd);
  return error;
}
Exemplo n.º 19
0
int main()
{
  init();

  printf("\n\nFirmware info - git: " GIT_VERSION ", built: " __DATE__ " " __TIME__ "\n");
  printf("--- COFFEE TEST ---\n");

  char buf[80] = "Hello, world\n";

  int ret = cfs_coffee_format();

  if (ret < 0)
    while(1);

  int fd = cfs_open("test", CFS_READ | CFS_WRITE);

  if(fd >= 0) {
    ret = cfs_write(fd, buf, strlen(buf));
    if (ret < 0) while(1);
    ret = cfs_write(fd, "JELLO\n", 7);
    if (ret < 0) while(1);
    ret = cfs_seek(fd, 0, CFS_SEEK_SET);
    if (ret < 0) while(1);
    ret = cfs_read(fd, buf, sizeof(buf));
    if (ret < 0) while(1);
    printf("Read message: %s\n", buf);
    ret = cfs_seek(fd, 0, CFS_SEEK_SET);
    if (ret < 0) while(1);
    ret = cfs_write(fd, "JELLO", 5);
    if (ret < 0) while(1);
    ret = cfs_seek(fd, 0, CFS_SEEK_SET);
    if (ret < 0) while(1);
    ret = cfs_read(fd, buf, sizeof(buf));
    if (ret < 0) while(1);
    printf("Read message: %s\n", buf);
    cfs_close(fd);
    if (ret < 0) while(1);
  }

  while (1);

	return 0;
}
Exemplo n.º 20
0
Arquivo: ftp.c Projeto: 1uk3/contiki
/*---------------------------------------------------------------------------*/
void
ftpc_data(uint8_t *data, uint16_t len)
{
  if(data == NULL) {
    show_statustext("Download complete", "");
    close_file();
    start_loaddir();
  } else {
    cfs_write(fd, data, len);
  }
}
Exemplo n.º 21
0
static int
write_page(struct deluge_object *obj, unsigned pagenum, unsigned char *data)
{
  cfs_offset_t offset;

  offset = pagenum * S_PAGE;

  if(cfs_seek(obj->cfs_fd, offset, CFS_SEEK_SET) != offset) {
    return -1;
  }
  return cfs_write(obj->cfs_fd, (char *)data, S_PAGE);
}
Exemplo n.º 22
0
static void
write_array(int fd, unsigned char *mem, uint16_t len)
{
#if DATA_AS_HEX
  int i;
  for(i = 0; i < len; i++) {
    write_byte(fd, mem[i]);
  }
#else /* DATA_AS_HEX */
  cfs_write(fd, mem, len);
#endif /* DATA_AS_HEX */
}
Exemplo n.º 23
0
static void settings_save_callback(u16 sender_id, u8 len, u8 msg[], void* context)
{
  int f = cfs_open(SETTINGS_FILE, CFS_WRITE);
  const char *sec = NULL;
  char buf[128];
  int i;

  (void)sender_id; (void) context; (void)len; (void)msg;

  if (f == -1) {
    log_error("Error opening config file!\n");
    return;
  }

  for (struct setting *s = settings_head; s; s = s->next) {
    /* Skip unchanged parameters */
    if (!s->dirty)
      continue;

    if ((sec == NULL) || (strcmp(s->section, sec) != 0)) {
      /* New section, write section header */
      sec = s->section;
      i = snprintf(buf, sizeof(buf), "[%s]\n", sec);
      if (cfs_write(f, buf, i) != i) {
        log_error("Error writing to config file!\n");
      }
    }

    /* Write setting */
    i = snprintf(buf, sizeof(buf), "%s=", s->name);
    i += s->type->to_string(s->type->priv, &buf[i], sizeof(buf) - i - 1, s->addr, s->len);
    buf[i++] = '\n';
    if (cfs_write(f, buf, i) != i) {
      log_error("Error writing to config file!\n");
    }
  }

  cfs_close(f);
  log_info("Wrote settings to config file.\n");
}
Exemplo n.º 24
0
static void write_chunk(struct rucb_conn *c, int offset, int flag, char *data, int datalen)
{
  //printf("write_chunk offset=%d, length=%d, filename=%s\n", offset, datalen, filename_download);
  if(datalen > 0) {
    cfs_write(fd, data, datalen);
  }
  if(flag == RUCB_FLAG_LASTCHUNK) {
	//printf("last chunk!\n");
    downloading = 0;
    cfs_close(fd);
    process_poll(&download_and_execute_process);
  }
}
Exemplo n.º 25
0
int handle_file_data(int fd, uint8_t* data, uint8_t size)
{
    log_info("handle_file_data(%x, %d)\n", fd, size);
    if (fd == FIRMWARE_HD)
    {
        WriteFirmware(data, offset, size);
        offset += size;
        process_post(ui_process, EVENT_FIRMWARE_UPGRADE, (void*)offset);
    }
    else if (fd != -1)
    {
        return cfs_write(fd, data, size);
    }
    
    return 0;
}
Exemplo n.º 26
0
db_result_t
storage_rename_relation(char *old_name, char *new_name)
{
  db_result_t result;
  int old_fd;
  int new_fd;
  int r;
  char buf[64];

  result = DB_STORAGE_ERROR;
  old_fd = new_fd = -1;

  old_fd = cfs_open(old_name, CFS_READ);
  new_fd = cfs_open(new_name, CFS_WRITE);
  if(old_fd < 0 || new_fd < 0) {
    goto error;
  }

  for(;;) {
    r = cfs_read(old_fd, buf, sizeof(buf));
    if(r < 0) {
      goto error;
    } else if(r == 0) {
      break;
    }
    if(cfs_write(new_fd, buf, r) != r) {
      goto error;
    }
  };

  cfs_remove(old_name);
  result = DB_OK;

error:
  cfs_close(old_fd);
  cfs_close(new_fd);

  if(result != DB_OK) {
    cfs_remove(new_name);
  }
  return result;
}
Exemplo n.º 27
0
PROCESS_THREAD(test_cfs_process, ev, data)
{
  static struct etimer et;
  static int fd;
  static uint16_t counter;
  static char buf[30];

  PROCESS_BEGIN();

  printf("Starting CFS test process\n");

  while(1) {
    etimer_set(&et, CLOCK_SECOND);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

    /* Write to filesystem */
    sprintf(buf, "filedata%04ifiledata%04i", counter, counter);
    fd = cfs_open("filename", CFS_READ | CFS_WRITE);
    cfs_seek(fd, 0, CFS_SEEK_SET);
    cfs_write(fd, buf, 24);
    cfs_close(fd);
    printf("Wrote to filesystem: '%s'\n", buf);
    counter++;

    etimer_set(&et, CLOCK_SECOND);
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

    /* Read from filesystem */
    fd = cfs_open("file1", CFS_READ | CFS_WRITE);
    cfs_seek(fd, 4, CFS_SEEK_SET);
    cfs_read(fd, buf, 12);
    cfs_close(fd);
    buf[12] = '\0';
    printf("Read from filesystem: '%s'\n", buf);

  }

  PROCESS_END();
}
Exemplo n.º 28
0
static void almanac_callback(u16 sender_id, u8 len, u8 msg[], void* context)
{
  (void)sender_id; (void)len; (void) context;

  almanac_t *new_almanac = (almanac_t*)msg;

  log_info("Received alamanc for PRN %02d\n", new_almanac->prn);
  memcpy(&almanac[new_almanac->prn-1], new_almanac, sizeof(almanac_t));

  int fd = cfs_open("almanac", CFS_WRITE);
  if (fd != -1) {
    cfs_seek(fd, (new_almanac->prn-1)*sizeof(almanac_t), CFS_SEEK_SET);
    if (cfs_write(fd, new_almanac, sizeof(almanac_t)) != sizeof(almanac_t)) {
      log_error("Error writing to almanac file\n");
    } else {
      log_info("Saved almanac to flash\n");
    }
    cfs_close(fd);
  } else {
    log_error("Error opening almanac file\n");
  }
}
Exemplo n.º 29
0
/* Callback function. Called from the webclient module when HTTP data
 * has arrived.
 */
void
webclient_datahandler(char *data, uint16_t len)
{
  static unsigned long dload_bytes;
  int ret;

  if(len > 0) {
    dload_bytes += len;
    printf("Downloading (%lu bytes)\n", dload_bytes);
    if(file != -1) {
      ret = cfs_write(file, data, len);
      if(ret != len) {
        printf("Wrote only %d bytes\n", ret);
      }
    }
  }

  if(data == NULL) {
    printf("Finished downloading %lu bytes.\n", dload_bytes);
    app_quit();
  }
}
Exemplo n.º 30
0
db_result_t
storage_write(db_storage_id_t fd,
	      void *buffer, unsigned long offset, unsigned length)
{
  char *ptr;
  int r;

  if(cfs_seek(fd, offset, CFS_SEEK_SET) == (cfs_offset_t)-1) {
    return DB_STORAGE_ERROR;
  }

  ptr = buffer;
  while(length > 0) {
    r = cfs_write(fd, ptr, length);
    if(r <= 0) {
      return DB_STORAGE_ERROR;
    }
    ptr += r;
    length -= r;
  }

  return DB_OK;
}