Exemple #1
0
//ftp.stop()
//===================================
static int lftp_stop( lua_State* L )
{
  if ( (ftp_thread_is_started) && (ftpCmdSocket != NULL) ) {
    ftpCmdSocket->clientFlag = REQ_ACTION_QUIT;

    if (ftpCmdSocket->disconnect_cb != LUA_NOREF) {
      lua_pushinteger(L, 0);
      return 1;
    }
    // wait max 10 sec for disconnect
    uint32_t tmo = mico_get_time();
    while (ftp_thread_is_started) {
      if ((mico_get_time() - tmo) > 10000) break;
      mico_thread_msleep(100);
      luaWdgReload();
    }
    if (!ftp_thread_is_started) {
      _ftp_deinit(0);    
      lua_pushinteger(L, 0);
    }
    else lua_pushinteger(L, -1);
  }
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #2
0
//ftp.chdir([dir])
//===================================
static int lftp_chdir( lua_State* L )
{
  if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -1);
    return 1;
  }
  _getFName(L, 1);

  free(ftpresponse);
  ftpresponse = NULL;
  cmd_done = 0;
  uint32_t tmo = mico_get_time();
  ftpCmdSocket->clientFlag = REQ_ACTION_CHDIR;

  while (cmd_done == 0) {
    if ((mico_get_time() - tmo) > 4000) break;
    mico_thread_msleep(60);
    luaWdgReload();
  }
  if (cmd_done == 0) {
    ftp_log("[FTP usr] Timeout\r\n" );
    lua_pushinteger(L, -2);
    return 1;
  }

  lua_pushinteger(L, 0);
  if (ftpresponse != NULL) {
    lua_pushstring(L, ftpresponse);
    free(ftpresponse);
    ftpresponse = NULL;
  }
  else lua_pushstring(L, "?");
  return 2;
}
Exemple #3
0
//--------------------------------------------------------
static uint8_t Ymodem_WaitACK(uint8_t ackchr, uint8_t tmo)
{
  uint8_t receivedC[2];
  uint32_t errors = 0;

  do {
    if (Receive_Byte(&receivedC[0], NAK_TIMEOUT) == 0) {
      if (receivedC[0] == ackchr) {
        return 1;
      }
      else if (receivedC[0] == CA) {
        send_CA();
        return 2; // CA received, Sender abort
      }
      else if (receivedC[0] == NAK) {
        return 3;
      }
      else {
        return 4;
      }
    }
    else {
      errors++;
    }
    luaWdgReload();
  }while (errors < tmo);
  return 0;
}
Exemple #4
0
//ftp.sendstring(file, str [,append])
//========================================
static int lftp_sendstring( lua_State* L )
{
  if ((gL != NULL) && (ftpCmdSocket != NULL)) {
    if ((status & FTP_LOGGED)) {
      if (lua_gettop(L) >= 3) {
        send_type = (uint8_t)luaL_checkinteger(L, 3);
        if (send_type != SEND_APPEND) send_type = SEND_OVERWRITTE;
      }
      else send_type = SEND_OVERWRITTE;
      send_type |= SEND_STRING;

      if (_getFName(L, 1) < 0) {
        ftp_log("[FTP fil] File name missing\r\n" );
        lua_pushinteger(L, -13);
        return 1;
      }
      
      size_t len;
      sendDataBuf = (char*)luaL_checklstring( L, 2, &len );
      if (sendDataBuf == NULL) {
        ftp_log("[FTP fil] Bad string\r\n");
        lua_pushinteger(L, -14);
      }
      else {
        file_size = len;
        data_done = 0;
        ftpCmdSocket->clientFlag = REQ_ACTION_SEND;
      
        uint32_t tmo = mico_get_time();
        while (!data_done) {
          if ((mico_get_time() - tmo) > 10000) break;
          mico_thread_msleep(60);
          luaWdgReload();
        }
        if (!data_done) {
          ftp_log("[FTP usr] Timeout: string not sent\r\n" );
          lua_pushinteger(L, -15);
        }
        else {
          ftp_log("[FTP usr] String sent\r\n" );
          lua_pushinteger(L, file_status);
        }
        sendDataBuf = NULL;
      }
    }
    else {
      ftp_log("[FTP usr] Not logged\r\n" );
      lua_pushinteger(L, -12);
    }
  }
  else {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -11);
  }
  return 1;
}
Exemple #5
0
//-----------------------------------------------------------
static void Ymodem_SendPacket(uint8_t *data, uint16_t length)
{
  uint16_t i;

  luaWdgReload();
  i = 0;
  while (i < length)
  {
    Send_Byte(data[i]);
    i++;
  }
}
Exemple #6
0
//ftp.send(file [,append])
//==================================
static int lftp_send( lua_State* L )
{
  if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -11);
    return 1;
  }
  if (lua_gettop(L) >= 2) {
    send_type = (uint8_t)luaL_checkinteger(L, 2);
    if (send_type != SEND_APPEND) send_type = SEND_OVERWRITTE;
  }
  else send_type = SEND_OVERWRITTE;
  
  if (_getFName(L, 1) < 0) {
    ftp_log("[FTP fil] File name missing\r\n" );
    lua_pushinteger(L, -12);
    return 1;
  }
  if (_openFile("r") < 0) {
    lua_pushinteger(L, -13);
    return 1;
  }

  spiffs_stat s;
  // Get file size
  SPIFFS_fstat(&fs, file_fd, &s);
  file_size = s.size;
  
  data_done = 0;
  ftpCmdSocket->clientFlag = REQ_ACTION_SEND;
  
  if (ftpCmdSocket->sent_cb == LUA_NOREF) {
    // no cb function, wait until file received (max 10 sec)
    uint32_t tmo = mico_get_time();
    while (!data_done) {
      if ((mico_get_time() - tmo) > 10000) break;
      mico_thread_msleep(60);
      luaWdgReload();
    }
    if (!data_done) {
      ftp_log("[FTP usr] Timeout: file not sent\r\n" );
      lua_pushinteger(L, -14);
    }
    else {
      ftp_log("[FTP usr] File sent\r\n" );
      lua_pushinteger(L, file_status);
    }
  }
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #7
0
//stat = ftp.start()
//===================================
static int lftp_start( lua_State* L )
{
  LinkStatusTypeDef wifi_link;
  int err = micoWlanGetLinkStatus( &wifi_link );

  if ( wifi_link.is_connected == false ) {
    ftp_log("[FTP usr] WiFi NOT CONNECTED!\r\n" );
    lua_pushinteger(L, -1);
    return 1;
  }
  
  if ( (gL == NULL) || (ftpCmdSocket == NULL) ) {
    ftp_log("[FTP usr] Execute ftp.new first!\r\n" );
    lua_pushinteger(L, -2);
    return 1;
  }
  if (ftp_thread_is_started) {
    ftp_log("[FTP usr] Already started!\r\n" );
    lua_pushinteger(L, -3);
    return 1;
  }
  
  mico_system_notify_register( mico_notify_TCP_CLIENT_CONNECTED, (void *)_micoNotify_FTPClientConnectedHandler, NULL );

  // all setup, start the ftp thread
  if (!ftp_thread_is_started) {
    if (mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY-1, "Ftp_Thread", _thread_ftp, 1024, NULL) != kNoErr) {
      _ftp_deinit(0);    
      ftp_log("[FTP usr] Create thread failed\r\n" );
      lua_pushinteger(L, -4);
      return 1;
    }
    else ftp_thread_is_started = true;
  } 

  if (ftpCmdSocket->logon_cb != LUA_NOREF) {
    lua_pushinteger(L, 0);
    return 1;
  }
  
  // wait max 10 sec for login
  uint32_t tmo = mico_get_time();
  while ( (ftp_thread_is_started) && !(status & FTP_LOGGED) ) {
    if ((mico_get_time() - tmo) > 10000) break;
    mico_thread_msleep(100);
    luaWdgReload();
  }
  if (!(status & FTP_LOGGED)) lua_pushinteger(L, -4);
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #8
0
//ftp.recv(file [,tostr])
//===================================
static int lftp_recv( lua_State* L )
{
  if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -11);
    return 1;
  }

  if (_getFName(L, 1) < 0) {
    ftp_log("[FTP fil] File name missing\r\n" );
    lua_pushinteger(L, -12);
    return 1;
  }
  if (_openFile("w") < 0) {
    lua_pushinteger(L, -13);
    return 1;
  }
  
  recv_type = RECV_TOFILE;
  if (lua_gettop(L) >= 2) {
    int tos = luaL_checkinteger(L, 2);
    if (tos == 1) recv_type = RECV_TOSTRING;
  }
  data_done = 0;
  ftpCmdSocket->clientFlag = REQ_ACTION_RECV;
  
  if (ftpCmdSocket->received_cb == LUA_NOREF) {
    // no cb function, wait until file received (max 10 sec)
    uint32_t tmo = mico_get_time();
    while (!data_done) {
      if ((mico_get_time() - tmo) > 10000) break;
      mico_thread_msleep(60);
      luaWdgReload();
    }
    if (!data_done) {
      ftp_log("[FTP usr] Timeout: file not received\r\n" );
      lua_pushinteger(L, -14);
    }
    else {
      ftp_log("[FTP usr] File received\r\n" );
      lua_pushinteger(L, file_status);
      if (recv_type == RECV_TOSTRING) {
        lua_pushstring(L, recvDataBuf);
        return 2;
      }
    }
  }
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #9
0
//===================================
static int lwifi_scan( lua_State* L )
{
  OSStatus err = 0;
  int tmo = mico_get_time();
  
  wifi_scanned_print = 0;
  
  if (lua_type(L, 1) == LUA_TFUNCTION || lua_type(L, 1) == LUA_TLIGHTFUNCTION) {
    lua_pushvalue(L, 1);  // copy argument (func) to the top of stack
    if (wifi_scan_succeed != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
    
    wifi_scan_succeed = luaL_ref(L, LUA_REGISTRYINDEX);
  } 
  else {
    if (wifi_scan_succeed != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, wifi_scan_succeed);
    wifi_scan_succeed = LUA_NOREF;
    if (lua_type(L, 1) == LUA_TNUMBER) {
      int prn = luaL_checkinteger( L, 1 );
      if (prn == 1) wifi_scanned_print = 1;
    }
  }
  err = mico_system_notify_register( mico_notify_WIFI_SCAN_ADV_COMPLETED, (void *)_micoNotify_WiFi_Scan_OK, NULL );
  require_noerr( err, exit );
  gL = L;
  wifi_scanned = 0;
  micoWlanStartScanAdv();

  tmo = mico_get_time();
  if (wifi_scan_succeed == LUA_NOREF) {
    while (wifi_scanned == 0) {
      if ((mico_get_time() - tmo) > 8000) break;
      mico_thread_msleep(100);
      luaWdgReload();
    }
    if ((wifi_scanned == 1) && (wifi_scanned_print == 0)) {
      return 2;
    }
  }
exit:
  return 0;
}
Exemple #10
0
//====================================
static int rtc_standby( lua_State* L )
{
  char buff[60];

  uint8_t mode = luaL_checkinteger( L, 1 );
  if (mode > 1) {
    l_message( NULL, "mode has to be 0 or 1" );
    return 0;
  }
  if (mode==1 && use_wwdg == 0) {
    l_message(NULL,"IWDG active, cannot enter STOP mode."); 
    return 0;
  }
  int nsec = luaL_checkinteger( L, 2 );
  if ((nsec < 1) || (nsec > 84559)) {
    l_message(NULL,"wrong interval (1~84599)"); 
    return 0;
  }

  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);

  platform_rtc_time_t time;
  uint32_t currentSecond;
  RTC_AlarmTypeDef  RTC_AlarmStructure;

  platform_rtc_get_time(&time);
  currentSecond = time.hr*3600 + time.min*60 + time.sec;
  currentSecond += nsec;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_HourFormat_24;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = currentSecond/3600%24;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = currentSecond/60%60;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = currentSecond%60;
  RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
  RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay ;

  RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);

  // Enable RTC Alarm A Interrupt
  RTC_ITConfig(RTC_IT_ALRA, ENABLE);
  // Enable the Alarm A
  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);
  /* Clear Alarm A pending bit */
  /* Clear RTC Alarm Flag */ 
  RTC_ClearFlag(RTC_FLAG_ALRAF);
  RTC_ClearFlag(RTC_IT_ALRA);

  if (mode == 0) sprintf(buff,"Going to STANDBY MODE...\r\n");
  else if (mode == 1) sprintf(buff,"Going to STOP MODE...\r\n");
  l_message(NULL,buff);
  sprintf(buff,"Wake up in %d second(s)\r\n", nsec);
  l_message(NULL,buff);

  //mico_rtos_suspend_all_thread();
  if (mode == 0) {
    PWR_EnterSTANDBYMode();
    // RESET
  }
  else if (mode == 1) {
    PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
    // restore clocks
    init_clocks();
  }
  //mico_rtos_resume_all_thread();

  // *** Back from stop ***
  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);
  luaWdgReload();

  l_message(NULL,"Back from power save mode.");
  return 0;
}
Exemple #11
0
//ftp.list(ltype, otype [,dir])
//===================================
static int lftp_list( lua_State* L )
{
  int err = 0;
  uint16_t dptr = 0;
  uint8_t n = 0;
  uint8_t i = 0;
  int nlin = 0;
  char buf[255] = {0};
  uint32_t tmo;

  uint8_t ltype = luaL_checkinteger(L, 1);
  uint8_t otype = luaL_checkinteger(L, 2);
  if (otype == 1) ltype = 1;
  err = _getFName(L, 3);
  err = 0;
  
  if ((gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED))) {
    ftp_log("[FTP usr] Login first\r\n" );
    err = -1;
    goto exit;
  }

  list_type = ltype;
  data_done = 0;
  ftpCmdSocket->clientFlag = REQ_ACTION_LIST;
  
  if (ftpCmdSocket->list_cb != LUA_NOREF) {
    goto exit;
  }
  
  // no cb function, wait until List received (max 10 sec)
  tmo = mico_get_time();
  while (data_done == 0) {
    if ((mico_get_time() - tmo) > 10000) break;
    mico_thread_msleep(60);
    luaWdgReload();
  }
  if (data_done == 0) {
    ftp_log("[FTP usr] Timeout: list not received\r\n" );
    err = -3;
    goto exit;
  }

  if ((recvDataBuf == NULL) || (recvDataLen == 0)) {
    ftp_log("[FTP usr] List not received\r\n" );
    err = -4;
    goto exit;
  }
  
  if (otype != 1) {
    printf("===================\r\n");
    printf("FTP directory list:");
    if (recvDataLen == (max_recv_datalen-16)) {
      printf(" (buffer full)");
    }
    printf("\r\n");
  }
  else {
    lua_newtable( L );
  }

  while (dptr < recvDataLen) {
    if (*(recvDataBuf+dptr) == '\0') break;
    if ((*(recvDataBuf+dptr) == '\n') || (*(recvDataBuf+dptr) == '\r') || (n >= 254)) {
      // EOL, print line
      if (n > 0) {
        nlin++;
        if (otype != 1) printf("%s\r\n", &buf[0]);
        else {
          lua_pushstring( L, &buf[0] );
          lua_rawseti(L,-2,i++);
        }
        n = 0;
      }
    }
    if (*(recvDataBuf+dptr) >= ' ') buf[n++] = *(recvDataBuf+dptr);
    buf[n] = '\0';
    dptr++;
  }
  if (n > 0) { // last line
    nlin++;
    if (otype != 1) printf("%s\r\n", &buf[0]);
    else {
      lua_pushstring( L, &buf[0] );
      lua_rawseti(L,-2,i++);
    }
  }
  if (otype != 1) {
    printf("===================\r\n");
    lua_pushinteger(L, nlin);
    return 1;
  }
  else {
    ftp_log("[FTP usr] List received to table\r\n" );
    lua_pushinteger(L, nlin);
    return 2;
  }
  
exit:
  if (otype == 1) {
    lua_newtable( L );
    lua_pushinteger(L, err);
    return 2;
  }
  lua_pushinteger(L, err);
  return 1;
}
Exemple #12
0
//------------------------------------------------------------------------------
static int32_t Receive_Packet (uint8_t *data, int32_t *length, uint32_t timeout)
{
  uint16_t i, packet_size;
  uint8_t c;
  //uint16_t tempCRC;
  *length = 0;
  
  luaWdgReload();
  if (Receive_Byte(&c, timeout) != 0)
  {
    luaWdgReload();
    return -1;
  }
  luaWdgReload();
  switch (c)
  {
    case SOH:
      packet_size = PACKET_SIZE;
      break;
    case STX:
      packet_size = PACKET_1K_SIZE;
      break;
    case EOT:
      return 0;
    case CA:
      if ((Receive_Byte(&c, timeout) == 0) && (c == CA))
      {
        *length = -1;
        luaWdgReload();
        return 0;
      }
      else
      {
        luaWdgReload();
        return -1;
      }
    case ABORT1:
    case ABORT2:
      luaWdgReload();
      return 1;
    default:
      luaWdgReload();
      return -1;
  }
  *data = c;
  luaWdgReload();
  for (i = 1; i < (packet_size + PACKET_OVERHEAD); i ++)
  {
    if (Receive_Byte(data + i, 10) != 0)
    {
      luaWdgReload();
      return -1;
    }
  }
  luaWdgReload();
  if (data[PACKET_SEQNO_INDEX] != ((data[PACKET_SEQNO_COMP_INDEX] ^ 0xff) & 0xff))
  {
    return -1;
  }
  if (crc16(&data[PACKET_HEADER], packet_size + PACKET_TRAILER) != 0) {
    return -1;
  }
  *length = packet_size;
  return 0;
}
Exemple #13
0
//----------------------------
static void send_CA ( void ) {
  Send_Byte(CA);
  Send_Byte(CA);
  mico_thread_msleep(500);
  luaWdgReload();
}
Exemple #14
0
//=======================================
static int lwifi_startsta( lua_State* L )
{
  LinkStatusTypeDef link;
  network_InitTypeDef_st wNetConfig;
  size_t len=0;
  lua_system_param_t lua_system_param;
  uint8_t has_default = 0;
  signed retry_interval = 0;
  int con_wait = 0;
  int tmo = mico_get_time();
  
  // check if wifi is already connected  
  memset(&link, 0x00, sizeof(link));
  micoWlanGetLinkStatus(&link);

  if (!lua_istable(L, 1)) {
    // ==== Call without parameters, return connection status ===
    if (link.is_connected != 0) lua_pushboolean(L, true);
    else lua_pushboolean(L, false);
    return 1;
  }

  // ==== parameters exists, configure and start ====
  if (wifi_sta_started == 1) _stopWifiSta();
  
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
  // check if default params exists
  if (getLua_systemParams(&lua_system_param) == 1) {
    if ((strlen(lua_system_param.wifi_ssid) > 0) && (strlen(lua_system_param.wifi_key) > 0)) {
      has_default = 1;
    }
  }
  
  //wait for connection
  lua_getfield(L, 1, "wait");
  if (!lua_isnil(L, -1)) {
      int wfc = luaL_checkinteger( L, -1 );
      if ((wfc > 0) && (wfc < 16)) con_wait = wfc * 1000;
      else return luaL_error( L, "wait must be 1 ~ 15");
  }

  //ssid  
  lua_getfield(L, 1, "ssid");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *ssid = luaL_checklstring( L, -1, &len );
      if (len >= 32) return luaL_error( L, "ssid: <32" );
      strncpy(wNetConfig.wifi_ssid,ssid,len);
    } 
    else return luaL_error( L, "wrong arg type:ssid" );
  }
  else if (has_default == 1) {
    strcpy(wNetConfig.wifi_ssid, lua_system_param.wifi_ssid);
  }
  else return luaL_error( L, "arg: ssid needed" );
  
  //pwd  
  lua_getfield(L, 1, "pwd");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *pwd = luaL_checklstring( L, -1, &len );
      if (len >= 64) return luaL_error( L, "pwd: <64" );
      if (len > 0) strncpy(wNetConfig.wifi_key,pwd,len);
      else strcpy(wNetConfig.wifi_key,"");  
    } 
    else return luaL_error( L, "wrong arg type: pwd" );
  }
  else if (has_default == 1) {
    strcpy(wNetConfig.wifi_key, lua_system_param.wifi_key);
  }
  else return luaL_error( L, "arg: pwd needed" );

  //dhcp
  wNetConfig.dhcpMode = DHCP_Client;
  lua_getfield(L, 1, "dhcp");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *pwd = luaL_checklstring( L, -1, &len );
      if (strcmp(pwd, "disable") == 0) wNetConfig.dhcpMode = DHCP_Disable;
    }   
    else return luaL_error( L, "wrong arg type: dhcp" );
  }

  //ip
  lua_getfield(L, 1, "ip");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *ip = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "ip: <16" );
      if (is_valid_ip(ip) == false) return luaL_error( L, "ip invalid" );
      strncpy(wNetConfig.local_ip_addr,ip,len);
    } 
    else return luaL_error( L, "wrong arg type:ip" );
  }
  else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: ip needed" );

  //netmask  
  lua_getfield(L, 1, "netmask");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *netmask = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "netmask: <16" );
      if (is_valid_ip(netmask) == false) return luaL_error( L, "netmask invalid" );
      strncpy(wNetConfig.net_mask,netmask,len);
    } 
    else return luaL_error( L, "wrong arg type: netmask" );
  }
  else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: netmask needed" );
  
  //gateway 
  lua_getfield(L, 1, "gateway");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *gateway = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "gateway: <16" );
      if (is_valid_ip(gateway) == false) return luaL_error( L, "gateway invalid" );
      strncpy(wNetConfig.gateway_ip_addr,gateway,len);
    } 
    else return luaL_error( L, "wrong arg type: gateway" );
  }
  else if(wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: gateway needed" );
  
  //dnsSrv
  lua_getfield(L, 1, "dnsSrv");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *dnsSrv = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "dnsSrv: <16" );
      if (is_valid_ip(dnsSrv) == false) return luaL_error( L, "dnsSrv invalid" );
      strncpy(wNetConfig.dnsServer_ip_addr,dnsSrv,len);
    } 
    else return luaL_error( L, "wrong arg type: dnsSrv" );
  }
  else if (wNetConfig.dhcpMode == DHCP_Disable) return luaL_error( L, "arg: dnsSrv needed" );
  
  //retry_interval
  lua_getfield(L, 1, "retry_interval");
  if (!lua_isnil(L, -1)) {
      retry_interval = (signed)luaL_checknumber( L, -1 );
      if (retry_interval < 0) return luaL_error( L, "retry_interval: >=0ms" );
      if (retry_interval == 0) retry_interval = 0x7FFFFFFF;
  }
  else retry_interval = 1000; 
  wNetConfig.wifi_retry_interval = retry_interval;
  
  gL = L;
  //notify, set CB function for wifi state change
  if (wifi_status_changed_STA != LUA_NOREF)
    luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_STA);
  wifi_status_changed_STA = LUA_NOREF;

  if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) {
    lua_pushvalue(L, 2);  // copy argument (func) to the top of stack
      
    wifi_status_changed_STA = luaL_ref(L, LUA_REGISTRYINDEX);
  } 
  mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler, NULL );
  
  //start  
  wNetConfig.wifi_mode = Station;
  micoWlanStart(&wNetConfig);
  wifi_sta_started = 1;

  if (con_wait == 0) {
    lua_pushboolean(L, false);
    return 1;
  }

  tmo = mico_get_time();
  micoWlanGetLinkStatus(&link);
  while (link.is_connected == 0) {
    if ((mico_get_time() - tmo) > con_wait) break;
    mico_thread_msleep(50);
    luaWdgReload();
    micoWlanGetLinkStatus(&link);
  }

  if (link.is_connected == 0) lua_pushboolean(L, false);
  else lua_pushboolean(L, true);

  return 1;
}
Exemple #15
0
//--------------------------------------------------
static s32_t lspiffs_erase(u32_t addr, u32_t size) {
    MicoFlashErase(MICO_PARTITION_LUA,addr,addr+size-1);
    luaWdgReload(); //in case wathdog
    return SPIFFS_OK;
  } 
Exemple #16
0
//--------------------------------------------------------------------------
static uint8_t Ymodem_Transmit (const char* sendFileName, uint32_t sizeFile)
{
  uint8_t packet_data[PACKET_1K_SIZE + PACKET_OVERHEAD];
  uint8_t filename[SPIFFS_OBJ_NAME_LEN];
  uint16_t blkNumber;
  uint8_t receivedC[1], i, err;
  uint32_t size = 0;

  for (i = 0; i < (SPIFFS_OBJ_NAME_LEN - 1); i++)
  {
    filename[i] = sendFileName[i];
  }
    
  while (MicoUartRecv( MICO_UART_1, &receivedC[0], 1, 10 ) == kNoErr) {};

  // Wait for response from receiver
  err = 0;
  do {
    luaWdgReload();
    Send_Byte(CRC16);
  } while (Receive_Byte(&receivedC[0], NAK_TIMEOUT) < 0 && err++ < 45);
  if (err >= 45 || receivedC[0] != CRC16) {
    send_CA();
    return 99;
  }
  
  // === Prepare first block and send it =======================================
  /* When the receiving program receives this block and successfully
   * opened the output file, it shall acknowledge this block with an ACK
   * character and then proceed with a normal YMODEM file transfer
   * beginning with a "C" or NAK tranmsitted by the receiver.
   */
  Ymodem_PrepareIntialPacket(&packet_data[0], filename, &sizeFile);
  do 
  {
    // Send Packet
    Ymodem_SendPacket(packet_data, PACKET_SIZE + PACKET_OVERHEAD);
    // Wait for Ack
    err = Ymodem_WaitACK(ACK, 10);
    if (err == 0 || err == 4) {
      send_CA();
      return 90;                  // timeout or wrong response
    }
    else if (err == 2) return 98; // abort
  }while (err != 1);

  // After initial block the receiver sends 'C' after ACK
  if (Ymodem_WaitACK(CRC16, 10) != 1) {
    send_CA();
    return 90;
  }
  
  // === Send file blocks ======================================================
  size = sizeFile;
  blkNumber = 0x01;
  
  // Resend packet if NAK  for a count of 10 else end of communication
  while (size)
  {
    // Prepare and send next packet
    Ymodem_PreparePacket(&packet_data[0], blkNumber, size);
    do
    {
      Ymodem_SendPacket(packet_data, PACKET_1K_SIZE + PACKET_OVERHEAD);
      // Wait for Ack
      err = Ymodem_WaitACK(ACK, 10);
      if (err == 1) {
        blkNumber++;
        if (size > PACKET_1K_SIZE) size -= PACKET_1K_SIZE; // Next packet
        else size = 0; // Last packet sent
      }
      else if (err == 0 || err == 4) {
        send_CA();
        return 90;                  // timeout or wrong response
      }
      else if (err == 2) return 98; // abort
    }while(err != 1);
  }
  
  // === Send EOT ==============================================================
  Send_Byte(EOT); // Send (EOT)
  // Wait for Ack
  do 
  {
    // Wait for Ack
    err = Ymodem_WaitACK(ACK, 10);
    if (err == 3) {   // NAK
      Send_Byte(EOT); // Send (EOT)
    }
    else if (err == 0 || err == 4) {
      send_CA();
      return 90;                  // timeout or wrong response
    }
    else if (err == 2) return 98; // abort
  }while (err != 1);
  
  // === Receiver requests next file, prepare and send last packet =============
  if (Ymodem_WaitACK(CRC16, 10) != 1) {
    send_CA();
    return 90;
  }

  Ymodem_PrepareLastPacket(&packet_data[0]);
  do 
  {
    Ymodem_SendPacket(packet_data, PACKET_SIZE + PACKET_OVERHEAD); // Send Packet
    // Wait for Ack
    err = Ymodem_WaitACK(ACK, 10);
    if (err == 0 || err == 4) {
      send_CA();
      return 90;                  // timeout or wrong response
    }
    else if (err == 2) return 98; // abort
  }while (err != 1);
  
  return 0; // file transmitted successfully
}
Exemple #17
0
//tmr.wdclr()
static int ltmr_wdclr( lua_State* L )
{
  luaWdgReload();
  return 0;
}
Exemple #18
0
//==================================
static int file_recv( lua_State* L )
{
  int32_t fsize = 0;
  uint8_t c, gnm;
  char fnm[SPIFFS_OBJ_NAME_LEN];
  char buff[LUAL_BUFFERSIZE];
  spiffs_DIR d;
  struct spiffs_dirent e;
  struct spiffs_dirent *pe = &e;
  uint32_t total, used;

  SPIFFS_info(&fs, &total, &used);
  if(total>2000000 || used>2000000 || used > total)
  {
    return luaL_error(L, "file system error");;
  }

  gnm = 0;
  if (lua_gettop(L) == 1 && lua_type( L, 1 ) == LUA_TSTRING) {
    size_t len;
    const char *fname = luaL_checklstring( L, 1, &len );
    if (len > 0 && len < SPIFFS_OBJ_NAME_LEN) {
      // use given file name
      for (c=0; c<len; c++) {
        fnm[c] = fname[c];
      }
      fnm[len] = '\0';
      gnm = 1;
    }
  }

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

  l_message(NULL,"Start Ymodem file transfer...");

  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}
  
  fsize = Ymodem_Receive(fnm, total-used-10000, gnm);
  
  luaWdgReload();
  mico_thread_msleep(500);
  while (MicoUartRecv( MICO_UART_1, &c, 1, 10 ) == kNoErr) {}

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

  mico_thread_msleep(500);
  if (fsize > 0)
  {
    sprintf(buff,"\r\nReceived successfully, %d\r\n",fsize);
    l_message(NULL,buff);
  }
  else if (fsize == -1)
  {
    l_message(NULL,"\r\nFile write error!\r\n");
  }
  else if (fsize == -2)
  {
    l_message(NULL,"\r\nFile open error!\r\n");
  }
  else if (fsize == -3)
  {
    l_message(NULL,"\r\nAborted.\r\n");
  }
  else if (fsize == -4)
  {
    l_message(NULL,"\r\nFile size too big, aborted.\r\n");
  }
  else
  {
    l_message(NULL,"\r\nReceive failed!");
  }
  
  if (fsize > 0) {
    SPIFFS_opendir(&fs, "/", &d);
    while ((pe = SPIFFS_readdir(&d, pe))) {
      sprintf(buff," %-32s size: %i", pe->name, pe->size);
      l_message(NULL,buff);
    }
    SPIFFS_closedir(&d);
  }

  return 0;
}
Exemple #19
0
//=========================================
static int rtc_standbyUntil( lua_State* L )
{
  uint8_t h,m,s;
  RTC_AlarmTypeDef  RTC_AlarmStructure;
  char buff[64];
  
  uint8_t mode = luaL_checkinteger( L, 1 );
  if (mode > 1) {
    l_message( NULL, "mode has to be 0 or 1" );
    return 0;
  }
  if (mode==1 && use_wwdg == 0) {
    l_message(NULL,"IWDG active, cannot enter STOP mode."); 
    return 0;
  }
  
  if (!lua_istable(L, 2)) {
    l_message( NULL, "table arg needed" );
    return 0;
  }
  if (lua_objlen( L, 2 ) != 3) {
    l_message( NULL, "hour,minute,second expected" );
    return 0;
  }
  lua_rawgeti( L, 2, 1 );
  h = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 2, 2 );
  m = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );
  lua_rawgeti( L, 2, 3 );
  s = ( int )luaL_checkinteger( L, -1 );
  lua_pop( L, 1 );

  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);

  RTC_AlarmStructure.RTC_AlarmTime.RTC_H12     = RTC_HourFormat_24;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours   = h;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes = m;
  RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds = s;
  RTC_AlarmStructure.RTC_AlarmDateWeekDay = 0x31;
  RTC_AlarmStructure.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay ;

  RTC_AlarmCmd(RTC_Alarm_A, DISABLE);
  /* Disable the Alarm A */
  RTC_ITConfig(RTC_IT_ALRA, DISABLE);
  /* Clear RTC Alarm Flag */ 
  RTC_ClearFlag(RTC_FLAG_ALRAF);
  RTC_ClearFlag(RTC_IT_ALRA);

  RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &RTC_AlarmStructure);

  /* Enable RTC Alarm A Interrupt: this Interrupt will wake-up the system from
     STANDBY mode (RTC Alarm IT not enabled in NVIC) */
  RTC_ITConfig(RTC_IT_ALRA, ENABLE);

  /* Enable the Alarm A */
  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);

  if (mode == 0) sprintf(buff,"Going to STANDBY MODE...\r\n");
  else if (mode == 1) sprintf(buff,"Going to STOP MODE...\r\n");
  l_message(NULL,buff);
  sprintf(buff,"Wake up at %02d:%02d:%02d\r\n", RTC_AlarmStructure.RTC_AlarmTime.RTC_Hours, RTC_AlarmStructure.RTC_AlarmTime.RTC_Minutes, RTC_AlarmStructure.RTC_AlarmTime.RTC_Seconds);
  l_message(NULL,buff);

  //mico_rtos_suspend_all_thread();
  if (mode == 0) {
    PWR_EnterSTANDBYMode();
  }
  else if (mode == 1) {
    PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI);
    init_clocks();
  }
  //mico_rtos_resume_all_thread();

  // *** Back from stop ***
  TM_RTC_DisableAlarm(TM_RTC_Alarm_A);
  TM_RTC_DisableAlarm(TM_RTC_Alarm_B);
  luaWdgReload();

  l_message(NULL,"Back from power save mode.");
  return 0;
}
Exemple #20
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;
}
Exemple #21
0
//======================================
static int lwifi_startap( lua_State* L )
{
  //4 stations Max
  network_InitTypeDef_st wNetConfig;
  size_t len=0;
  
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
  
  if (!lua_istable(L, 1)) {
    // ==== Call without parameters, return status ===
    if (wifi_ap_started == 1) lua_pushboolean(L, true);
    else lua_pushboolean(L, false);
    return 1;
  }
  
  //ssid  
  lua_getfield(L, 1, "ssid");
  if (!lua_isnil(L, -1)) {
    if( lua_isstring(L, -1) ) {
      const char *ssid = luaL_checklstring( L, -1, &len );
      if(len >= 32) return luaL_error( L, "ssid: <32" );
      strncpy(wNetConfig.wifi_ssid,ssid,len);
    } 
    else return luaL_error( L, "wrong arg type: ssid" );
  }
  else return luaL_error( L, "arg: ssid needed" );

  //pwd  
  lua_getfield(L, 1, "pwd");
  if (!lua_isnil(L, -1)) {
    if( lua_isstring(L, -1) ) {
      const char *pwd = luaL_checklstring( L, -1, &len );
      if (len >= 64) return luaL_error( L, "pwd: <64" );
      if (len > 0) strncpy(wNetConfig.wifi_key, pwd, len);
      else strcpy(wNetConfig.wifi_key, "");  
    } 
    else return luaL_error( L, "wrong arg type: pwd" );
  }
  else return luaL_error( L, "arg: pwd needed" );
  
  //ip  
  lua_getfield(L, 1, "ip");
  if (!lua_isnil(L, -1)) {
    if( lua_isstring(L, -1) ) {
      const char *ip = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "ip: <16" );
      if (is_valid_ip(ip) == false) return luaL_error( L, "ip invalid" );
      strncpy(wNetConfig.local_ip_addr, ip, len);
    } 
    else return luaL_error( L, "wrong arg type: ip" );
  }
  else {
    strcpy(wNetConfig.local_ip_addr, "11.11.11.1");
  }

  //netmask  
  lua_getfield(L, 1, "netmask");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *netmask = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "netmask: <16" );
      if (is_valid_ip(netmask) == false) return luaL_error( L, "netmask invalid" );
      strncpy(wNetConfig.net_mask,netmask,len);
    }
    else return luaL_error( L, "wrong arg type: netmask" );
  }
  else {
    strcpy(wNetConfig.net_mask, "255.255.255.0");
  }

  //gateway  
  lua_getfield(L, 1, "gateway");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *gateway = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "gateway: <16" );
      if (is_valid_ip(gateway) == false) return luaL_error( L, "gateway invalid" );
      strncpy(wNetConfig.gateway_ip_addr,gateway,len);
    }
    else return luaL_error( L, "wrong arg type: gateway" );
  }
  else {
    strcpy(wNetConfig.gateway_ip_addr,"11.11.11.1");
  }

  //dnsSrv  
  lua_getfield(L, 1, "dnsSrv");
  if (!lua_isnil(L, -1)) {
    if ( lua_isstring(L, -1) ) {
      const char *dnsSrv = luaL_checklstring( L, -1, &len );
      if (len >= 16) return luaL_error( L, "dnsSrv: <16" );
      if (is_valid_ip(dnsSrv) == false) return luaL_error( L, "dnsSrv invalid" );
      strncpy(wNetConfig.dnsServer_ip_addr,dnsSrv,len);
    }
    else return luaL_error( L, "wrong arg type: dnsSrv" );
  }
  else {
    strcpy(wNetConfig.dnsServer_ip_addr, "11.11.11.1");
  }

  //retry_interval
  signed retry_interval = 0;
  lua_getfield(L, 1, "retry_interval");
  if (!lua_isnil(L, -1)) {
      retry_interval = (signed)luaL_checknumber( L, -1 );
      if (retry_interval < 0) return luaL_error( L, "retry_interval: >=0ms" );
      if (retry_interval == 0) retry_interval = 0x7FFFFFFF;
  }
  else retry_interval = 1000; 
  wNetConfig.wifi_retry_interval = retry_interval;
  
  //notify
  gL = L;

  if (wifi_status_changed_AP != LUA_NOREF)
    luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP);
  wifi_status_changed_AP = LUA_NOREF;

  if (lua_type(L, 2) == LUA_TFUNCTION || lua_type(L, 2) == LUA_TLIGHTFUNCTION) {
    lua_pushvalue(L, 2);  // copy argument (func) to the top of stack
    if (wifi_status_changed_AP != LUA_NOREF)
      luaL_unref(L, LUA_REGISTRYINDEX, wifi_status_changed_AP);    
      
    wifi_status_changed_AP = luaL_ref(L, LUA_REGISTRYINDEX);
  } 
  mico_system_notify_register( mico_notify_WIFI_STATUS_CHANGED, (void *)_micoNotify_WifiStatusHandler, NULL );

  //start
  wifi_ap_started = 0;
  wNetConfig.dhcpMode = DHCP_Server;  
  wNetConfig.wifi_mode = Soft_AP;
  micoWlanStart(&wNetConfig);  

  int tmo = mico_get_time();
  while (wifi_ap_started == 0) {
    if ((mico_get_time() - tmo) > 1000) break;
    mico_thread_msleep(10);
    luaWdgReload();
  }
  if (wifi_ap_started == 1) lua_pushboolean(L, true);
  else lua_pushboolean(L, false);
  return 1;
}