Beispiel #1
0
static int file_compile( lua_State* L )
{
  Proto* f;
  int file_fd = FILE_NOT_OPENED;
  size_t len;
  const char *fname = luaL_checklstring( L, 1, &len );
  if ( len > SPIFFS_OBJ_NAME_LEN )
    return luaL_error(L, "filename too long");

  char output[SPIFFS_OBJ_NAME_LEN];
  strcpy(output, fname);
  // check here that filename end with ".lua".
  if (len < 4 || (strcmp( output + len - 4, ".lua") != 0) )
    return luaL_error(L, "not a .lua file");

  output[strlen(output) - 2] = 'c';
  output[strlen(output) - 1] = '\0';
  
  if (luaL_loadfile(L, fname) != 0) {
    return luaL_error(L, lua_tostring(L, -1));
  }

  f = toproto(L, -1);

  int stripping = 1;      /* strip debug information? */

  file_fd = SPIFFS_open(&fs,(char*)output,mode2flag("w+"),0);
  if (file_fd < FILE_NOT_OPENED)
  {
    return luaL_error(L, "cannot open/write to file");
  }

  lua_lock(L);
  int result = luaU_dump(L, f, writer, &file_fd, stripping);
  lua_unlock(L);

  SPIFFS_fflush(&fs,file_fd);
  SPIFFS_close(&fs,file_fd);
  file_fd =FILE_NOT_OPENED;

  if (result == LUA_ERR_CC_INTOVERFLOW) {
    return luaL_error(L, "value too big or small for target integer type");
  }
  if (result == LUA_ERR_CC_NOTINTEGER) {
    return luaL_error(L, "target lua_Number is integral but fractional value found");
  }

  return 0;
}
Beispiel #2
0
// file.open(filename, mode)
static int file_open( lua_State* L )
{
  size_t len;
  const char *fname = luaL_checklstring( L, 1, &len );
  if( len > SPIFFS_OBJ_NAME_LEN )
    return luaL_error(L, "filename too long");
  
  if(FILE_NOT_OPENED!=file_fd){
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }
  const char *mode = luaL_optstring(L, 2, "r");
  file_fd = SPIFFS_open(&fs,(char*)fname,mode2flag((char*)mode),0);
  if(file_fd < FILE_NOT_OPENED){
    file_fd = FILE_NOT_OPENED;
    lua_pushnil(L);
  } else {
    lua_pushboolean(L, true);
  }
  return 1; 
}
Beispiel #3
0
//------------------------------
static int _openFile(char *mode)
{
  // open the file
  if (ftpfile == NULL) {
    ftp_log("[FTP fil] Ftpfile not assigned\r\n" );
    return -1;
  }
  ftp_log("[FTP fil] Opening local file: %s\r\n", ftpfile );
  if (FILE_NOT_OPENED != file_fd) {
    ftp_log("[FTP fil] Closing file first\r\n" );
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }
  file_fd = SPIFFS_open(&fs, (char*)ftpfile, mode2flag(mode), 0);
  if (file_fd <= FILE_NOT_OPENED) {
    ftp_log("[FTP fil] Error opening local file: %d\r\n", file_fd );
    file_fd = FILE_NOT_OPENED;
    return -2;
  }
  return 0;
}
Beispiel #4
0
FILE *fopen(const char *path, const char *mode) {
	int fd;
	FILE *file = NULL;
	int flags = 0;

	flags = mode2flag(mode);

	if ((fd = open(path, flags, DEFAULT_MODE)) < 0) {
		/* That's sad, but open sets errno, no need to alter */
		return NULL;
	}

	if (NULL == (file = stdio_file_alloc(fd))) {
		close(fd);
		SET_ERRNO(ENOMEM);
		return NULL;
	}

	file->flags = flags;

	return file;

}
Beispiel #5
0
FILE *freopen(const char *path, const char *mode, FILE *file) {
	int fd;
	int flags = 0;
	int old_fd;

	if (NULL == path || NULL == file) {
		return NULL;
	}

	flags = mode2flag(mode);

	if ((fd = open(path, flags, DEFAULT_MODE)) < 0) {
		/* That's sad, but open sets errno, no need to alter */
		return NULL;
	}
	old_fd = file->fd;

	dup2(fd, old_fd);
	file->flags = flags;

	close(fd);

	return file;
}
Beispiel #6
0
//==================================
static int file_send( lua_State* L )
{
  int8_t res = 0;
  int8_t newname = 0;
  uint8_t c;
  spiffs_stat s;
  const char *fname;
  const char *newfname;
  size_t len;
  char buff[LUAL_BUFFERSIZE];

  fname = luaL_checklstring( L, 1, &len );
  
  if( len > SPIFFS_OBJ_NAME_LEN )
    return luaL_error(L, "filename too long");
  
  if(FILE_NOT_OPENED!=file_fd){
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }
  
  if (lua_gettop(L) == 2 && lua_type( L, 2 ) == LUA_TSTRING) {
    size_t len;
    newfname = luaL_checklstring( L, 2, &len );
    newname = 1;
  }

  // Open the file
  file_fd = SPIFFS_open(&fs,(char*)fname,mode2flag("r"),0);
  if(file_fd < FILE_NOT_OPENED){
    file_fd = FILE_NOT_OPENED;
    l_message(NULL,"Error opening file.");
    return 0;
  }

  // Get file size
  SPIFFS_fstat(&fs, file_fd, &s);
  if (newname == 1) {
    sprintf(buff,"sending \"%s\" as \"%s\"\r\n", fname, newfname);
    l_message(NULL,buff);
    fname = newfname;
  }
  
  l_message(NULL,"Start Ymodem file transfer...");

  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}
  
  res = Ymodem_Transmit(fname, s.size);
  
  luaWdgReload();
  mico_thread_msleep(500);
  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}

  if(FILE_NOT_OPENED!=file_fd){
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }

  if (res == 0) {
    l_message(NULL,"\r\nFile sent successfuly.");
  }
  else if (res == 99) {
    l_message(NULL,"\r\nNo response.");
  }
  else if (res == 98) {
    l_message(NULL,"\r\nAborted.");
  }
  else {
    l_message(NULL,"\r\nError sending file.");
  }
  return 0;
}
Beispiel #7
0
//---------------------------------------------------------------------------------
static int32_t Ymodem_Receive ( char* FileName, uint32_t maxsize, uint8_t getname )
{
  uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD], file_size[FILE_SIZE_LENGTH], *file_ptr;
  int32_t i, packet_length, file_len, write_len, session_done, file_done, packets_received, errors, session_begin, size = 0;
  
  for (session_done = 0, errors = 0, session_begin = 0; ;)
  {
    for (packets_received = 0, file_done = 0; ;)
    {
      switch (Receive_Packet(packet_data, &packet_length, NAK_TIMEOUT))
      {
        case 0:
          switch (packet_length)
          {
            /* Abort by sender */
            case -1:
              Send_Byte(ACK);
              return 0;
            /* End of transmission */
            case 0:
              Send_Byte(ACK);
              file_done = 1;
              break;
            /* Normal packet */
            default:
              if ((packet_data[PACKET_SEQNO_INDEX] & 0xff) != (packets_received & 0xff))
              {
                errors ++;
                if (errors > MAX_ERRORS)
                {
                  send_CA();
                  return 0;
                }
                Send_Byte(NAK);
              }
              else
              {
                errors = 0;
                if (packets_received == 0)
                {
                  /* Filename packet */
                  if (packet_data[PACKET_HEADER] != 0)
                  {
                    // Filename packet has valid data
                    if (getname == 0) {
                      for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < SPIFFS_OBJ_NAME_LEN);)
                      {
                        FileName[i++] = *file_ptr++;
                      }
                      FileName[i++] = '\0';
                    }
                    for (i = 0, file_ptr = packet_data + PACKET_HEADER; (*file_ptr != 0) && (i < packet_length);)
                    {
                      file_ptr++;
                    }
                    for (i = 0, file_ptr ++; (*file_ptr != ' ') && (i < FILE_SIZE_LENGTH);)
                    {
                      file_size[i++] = *file_ptr++;
                    }
                    file_size[i++] = '\0';
                    Str2Int(file_size, &size);

                    // Test the size of the file
                    if (size < 1 || size > maxsize) {
                      file_fd = FILE_NOT_OPENED;
                      /* End session */
                      send_CA();
                      return -4;
                    }

                    /* *** Open the file *** */
                    if (FILE_NOT_OPENED != file_fd) {
                      SPIFFS_close(&fs,file_fd);
                      file_fd = FILE_NOT_OPENED;
                    }
                    file_fd = SPIFFS_open(&fs, (char*)FileName, mode2flag("w"), 0);
                    if (file_fd <= FILE_NOT_OPENED) {
                      file_fd = FILE_NOT_OPENED;
                      /* End session */
                      send_CA();
                      return -2;
                    }
                    file_len = 0;
                    Send_Byte(ACK);
                    Send_Byte(CRC16);
                  }
                  /* Filename packet is empty, end session */
                  else
                  {
                    Send_Byte(ACK);
                    file_done = 1;
                    session_done = 1;
                    break;
                  }
                }
                /* Data packet */
                else
                {
                  /* Write received data to file */
                  if (file_len < size) {
                    file_len = file_len + packet_length;
                    if (file_len > size) {
                      write_len = packet_length - (file_len - size);
                    }
                    else {
                      write_len = packet_length;
                    }
                    if (file_fd <= FILE_NOT_OPENED) {
                      file_fd = FILE_NOT_OPENED;
                      // File not opened, End session
                      send_CA();
                      return -2;
                    }
                    if (SPIFFS_write(&fs,file_fd, (char*)(packet_data + PACKET_HEADER), write_len) < 0)
                    { //failed
                      SPIFFS_close(&fs,file_fd);
                      file_fd = FILE_NOT_OPENED;
                      /* End session */
                      send_CA();
                      return -1;
                    }
                  }
                  //success
                  Send_Byte(ACK);
                }
                packets_received ++;
                session_begin = 1;
              }
          }
          break;
        case 1:
          send_CA();
          return -3;
        default:
          if (session_begin >= 0)
          {
            errors ++;
          }
          if (errors > MAX_ERRORS)
          {
            send_CA();
            return 0;
          }
          Send_Byte(CRC16);
          break;
      }
      if (file_done != 0)
      {
        break;
      }
    }
    if (session_done != 0)
    {
      break;
    }
  }
  return (int32_t)size;
}