Example #1
0
int application_start( void )
{
  OSStatus err = kNoErr;
  
  MicoInit( );
  
   /*The notification message for the registered WiFi status change*/
  err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler );
  require_noerr( err, exit ); 
  
  err = MICOAddNotification( mico_notify_WIFI_CONNECT_FAILED, (void *)micoNotify_ConnectFailedHandler );
  require_noerr( err, exit );
  
  err = mico_rtos_init_semaphore(&tcp_sem, 1);
  require_noerr( err, exit ); 
  
  connect_ap( );
  
  mico_rtos_get_semaphore(&tcp_sem, MICO_WAIT_FOREVER);
    
  err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "TCP_client", tcp_client_thread, 0x800, NULL );
  require_noerr_action( err, exit, tcp_client_log("ERROR: Unable to start the tcp client thread.") );
  
  return err;

exit:
  tcp_client_log("ERROR, err: %d", err);
  return err;
}
Example #2
0
/**
  * @brief  通过TCP方式发送数据到TCP服务器
  * @param  buf 数据首地址
  * @param  buflen 数据长度
  * @retval 小于0表示发送失败
  */
int transport_sendPacketBuffer(unsigned char* buf, int buflen)
{
   int len;
  
    len = send( tcp_fd, buf, buflen, 0 );
    tcp_client_log("Client fd: %d, send data %d", tcp_fd, len);
  return len;
}
Example #3
0
void micoNotify_WifiStatusHandler(WiFiEvent event,  const int inContext)
{
  (void)inContext;
  switch (event) {
  case NOTIFY_STATION_UP:
    tcp_client_log("Station up");
    mico_rtos_set_semaphore(&tcp_sem);
    MicoRfLed(true);
    break;
  case NOTIFY_STATION_DOWN:
    tcp_client_log("Station down");
    MicoRfLed(false);
    break;
  default:
    break;
  }
  return;
}
Example #4
0
int transport_open()
{

  OSStatus err;
  struct sockaddr_t addr;

  
 static char tcp_remote_ip[16] = "112.74.133.7"; /*remote ip address*/
static int tcp_remote_port = 1883;               /*remote port*/
  
  
  tcp_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  
  addr.s_ip = inet_addr( tcp_remote_ip );
  addr.s_port = tcp_remote_port;
  
  tcp_client_log( "Connecting to server: ip=%s  port=%d!", tcp_remote_ip,tcp_remote_port );
  err = connect( tcp_fd, &addr, sizeof( addr ) );
  tcp_client_log( "Connect success!" );
;
  
  return 1;
}
Example #5
0
static void connect_ap( void )
{  
  memset(&wNetConfigAdv, 0x0, sizeof(network_InitTypeDef_adv_st));
  
  strcpy((char*)wNetConfigAdv.ap_info.ssid, ap_ssid);
  strcpy((char*)wNetConfigAdv.key, ap_key);
  wNetConfigAdv.key_len = strlen(ap_key);
  wNetConfigAdv.ap_info.security = SECURITY_TYPE_AUTO;
  wNetConfigAdv.ap_info.channel = 0; //Auto
  wNetConfigAdv.dhcpMode = DHCP_Client;
  wNetConfigAdv.wifi_retry_interval = 100;
  micoWlanStartAdv(&wNetConfigAdv);
  
  tcp_client_log("connect to %s...", wNetConfigAdv.ap_info.ssid);
}
Example #6
0
/**
  * @brief  阻塞方式接收TCP服务器发送的数据
  * @param  buf 数据存储首地址
  * @param  count 数据缓冲区长度
  * @retval 小于0表示接收数据失败
  */
int transport_getdata(unsigned char* buf, int count)
{
  //return recv(SOCK_TCPS,buf,count);
  int len;
  
    FD_ZERO( &readfds );
    FD_SET( tcp_fd, &readfds );
     /* recv wlan data, and send back */
    if( FD_ISSET( tcp_fd, &readfds ) )
    {
      len = recv( tcp_fd, buf, count, 0);
    
      
      if( len == 0){
        tcp_client_log( "TCP Client is disconnected, fd: %d", tcp_fd );
      }  
    }
  return len;
}
Example #7
0
void tcp_client_thread(void *inContext)
{
  OSStatus err;
  struct sockaddr_t addr;
  struct timeval_t t;
  fd_set readfds;
  int tcp_fd = -1 , len;
  char *buf;
  
  buf = (char*)malloc(BUF_LEN);
  require_action(buf, exit, err = kNoMemoryErr);
  
  while(1)
  {
    if ( tcp_fd == -1 ) 
    {
      tcp_fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
      require_action(IsValidSocket( tcp_fd ), exit, err = kNoResourcesErr );
      addr.s_ip = inet_addr(tcp_remote_ip);
      addr.s_port = tcp_remote_port;
      err = connect(tcp_fd, &addr, sizeof(addr));
      require_noerr_quiet(err, ReConnWithDelay);
      tcp_client_log("Remote server connected at port: %d, fd: %d",  addr.s_port, tcp_fd);
    }
    else
    {
      /*Check status on erery sockets */
      FD_ZERO(&readfds);
      FD_SET(tcp_fd, &readfds);
      t.tv_sec = 4;
      t.tv_usec = 0;

      select(1, &readfds, NULL, NULL, &t);
      
      /*recv wlan data using remote client fd*/
      if (FD_ISSET( tcp_fd, &readfds )) 
      {
        len = recv(tcp_fd, buf, BUF_LEN, 0);
        if( len <= 0) {
          tcp_client_log("Remote client closed, fd: %d", tcp_fd);
          goto ReConnWithDelay;
        }
        
        tcp_client_log("[tcp_rec][%d] = %.*s", len, len, buf);
        sendto(tcp_fd, buf, len, 0, &addr, sizeof(struct sockaddr_t));
      }
   
      continue;
      
    ReConnWithDelay:
        if(tcp_fd != -1){
          SocketClose(&tcp_fd);
        }
        tcp_client_log("Connect to %s failed! Reconnect in 5 sec...", tcp_remote_ip);
        sleep( 5 );
    }
  }
  
exit:
  mico_rtos_delete_thread(NULL);
}
Example #8
0
void micoNotify_ConnectFailedHandler(OSStatus err, const int inContext)
{
  (void)inContext;
  tcp_client_log("Wlan Connection Err %d", err);
}