Пример #1
0
int main(void)
{
	
	mxchipInit();
	UART_Init();
  printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
  
	readConfig(&configParas);
	
#ifdef LowPowerMode
  enable_ps_mode(SLEEP_UNIT_MS, 100, 100);
#endif	
	
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));	
	wNetConfig.wifi_mode = Soft_AP;
	strcpy(wNetConfig.wifi_ssid, configParas.wifi_ssid);
	strcpy(wNetConfig.wifi_key, configParas.wifi_key);
	sprintf(wNetConfig.local_ip_addr, "192.168.1.1");
	sprintf(wNetConfig.net_mask, "255.255.255.0");
	sprintf(wNetConfig.gateway_ip_addr, "192.168.1.1");
	sprintf(wNetConfig.dnsServer_ip_addr, "192.168.1.1");
	sprintf(wNetConfig.address_pool_start, "192.168.1.100");
	sprintf(wNetConfig.address_pool_end, "192.168.1.200");
	wNetConfig.dhcpMode = DHCP_Server;
	StartNetwork(&wNetConfig);
  printf("Establish soft AP: %s.....\r\n", wNetConfig.wifi_ssid);
	
  set_tcp_keepalive(3, 60);
	http_init();
	
  while(1) {
    mxchipTick();	
		http_tick();
	}
}
Пример #2
0
int application_start(void)
{
  mxchipInit();
  Platform_Init();
  UART_Init();
  
#ifdef RFLowPowerMode
  ps_enable();
#endif	
  
#ifdef MCULowPowerMode
  mico_mcu_powersave_config(mxEnable);
#endif	
  
  debug_out("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
  
  debug_out (menu);
  debug_out ("\nMXCHIP> ");
  
  while(1) {
      Main_Menu();
    if(configSuccess){
      wNetConfig.wifi_mode = Station;
      wNetConfig.dhcpMode = DHCP_Client;
      wNetConfig.wifi_retry_interval = 100;
      StartNetwork(&wNetConfig);
      debug_out("connect to %s.....\r\n", wNetConfig.wifi_ssid);
      configSuccess = 0;
      debug_out ("\nMXCHIP> ");
    }
  }
}
Пример #3
0
void mico_mfg_test(void)
{
    char str[64];
    char mac[6];
    char *ssid;
  
    sprintf(str, "Library Version: %s\r\n", system_lib_version());
	mf_printf(str);
    mf_printf("APP Version: ");
    memset(str, 0, sizeof(str));
    system_version(str, sizeof(str));
    mf_printf(str);
    mf_printf("\r\n");
    memset(str, 0, sizeof(str));
    wlan_driver_version(str, sizeof(str));
    mf_printf("Driver: ");
    mf_printf(str);
    mf_printf("\r\n");
	wlan_get_mac_address(mac);
	sprintf(str, "MAC: %02X-%02X-%02X-%02X-%02X-%02X\r\n",
			mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
	mf_printf(str);

    mfg_scan();

    ssid = ssid_get();
    mfg_connect(ssid);
    
    mico_thread_sleep(MICO_NEVER_TIMEOUT);
}
/* mxchip library manufacture test. */
void mxchip_mfg_test(void)
{
  char str[64];
  char mac[6];
  char *ssid;
  mico_uart_config_t uart_config;
  volatile ring_buffer_t  rx_buffer;
  volatile uint8_t *      rx_data;
  
  rx_data = malloc(50);
  require(rx_data, exit);
  
  /* Initialize UART interface */
  uart_config.baud_rate    = 115200;
  uart_config.data_width   = DATA_WIDTH_8BIT;
  uart_config.parity       = NO_PARITY;
  uart_config.stop_bits    = STOP_BITS_1;
  uart_config.flow_control = FLOW_CONTROL_DISABLED;
  uart_config.flags = UART_WAKEUP_DISABLE;
  
  ring_buffer_init  ( (ring_buffer_t *)&rx_buffer, (uint8_t *)rx_data, 50 );
  MicoUartInitialize( MFG_TEST, &uart_config, (ring_buffer_t *)&rx_buffer );  

  mf_printf("==== MXCHIP Manufacture Test ====\r\n");
  mf_printf("Bootloader Version: ");
  mf_printf(mico_get_bootloader_ver());
  mf_printf("\r\n");
  sprintf(str, "Library Version: %s\r\n", system_lib_version());
  mf_printf(str);
  mf_printf("APP Version: ");
  memset(str, 0, sizeof(str));
  system_version(str, sizeof(str));
  mf_printf(str);
  mf_printf("\r\n");
  memset(str, 0, sizeof(str));
  MicoGetRfVer(str, sizeof(str));
  mf_printf("Driver: ");
  mf_printf(str);
  mf_printf("\r\n");
  wlan_get_mac_address(mac);
  sprintf(str, "MAC: %02X-%02X-%02X-%02X-%02X-%02X\r\n",
          mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  mf_printf(str);
  
  mfg_scan();
  
  ssid = ssid_get();
  mfg_connect(ssid);
  
exit:
  mico_thread_sleep(MICO_NEVER_TIMEOUT);
}
Пример #5
0
int application_start(void)
{
  OSStatus err = kNoErr;
  net_para_st para;

  Platform_Init();
  /*Read current configurations*/
  context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) );
  require_action( context, exit, err = kNoMemoryErr );
  memset(context, 0x0, sizeof(mico_Context_t));
  mico_rtos_init_mutex(&context->flashContentInRam_mutex);
  mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); 

  MICOReadConfiguration( context );

  err = MICOInitNotificationCenter  ( context );

  err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler );
  require_noerr( err, exit );  
  
  /*wlan driver and tcpip init*/
  mxchipInit();
  getNetPara(&para, Station);
  formatMACAddr(context->micoStatus.mac, (char *)&para.mac);
  
  mico_log_trace(); 
  mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version());

  /*Start system monotor thread*/
  err = MICOStartSystemMonitor(context);
  require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") );
  
  err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000);
  require_noerr( err, exit );
  mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL);
  mico_start_timer(&_watchdog_reload_timer);
  
  if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){
    mico_log("Empty configuration. Starting configuration mode...");

#ifdef CONFIG_MODE_EASYLINK
    err = startEasyLink( context );
    require_noerr( err, exit );
#endif

#ifdef CONFIG_MODE_WAC
    err = startMfiWac( context );
    require_noerr( err, exit );
#endif
  }
  else{
    mico_log("Available configuration. Starting Wi-Fi connection...");
    
    /* Regisist notifications */
    err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler );
    require_noerr( err, exit ); 
    
    err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler );
    require_noerr( err, exit ); 

    err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler );
    require_noerr( err, exit );  
   
    if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){
      ps_enable();
    }

    if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){
      mico_mcu_powersave_config(true);
    }

    /*Bonjour service for searching*/
    if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){
      err = MICOStartBonjourService( Station, context );
      require_noerr( err, exit );
    }

    /*Local configuration server*/
    if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){
      err =  MICOStartConfigServer(context);
      require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") );
    }

    /*Start mico application*/
    err = MICOStartApplication( context );
    require_noerr( err, exit );
    
    _ConnectToAP( context );
  }


  /*System status changed*/
  while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){
    switch(context->micoStatus.sys_state){
      case eState_Normal:
        break;
      case eState_Software_Reset:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        PlatformSoftReboot();
        break;
      case eState_Wlan_Powerdown:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        wifi_power_down();
        break;
      case eState_Standby:
        mico_log("Enter standby mode");
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(100);
        wifi_power_down();
        Platform_Enter_STANDBY();
        break;
      default:
        break;
    }
  }
    
  require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) );

exit:
  mico_rtos_delete_thread(NULL);
  return kNoErr;
}
Пример #6
0
int application_start(void)
{
  OSStatus err = kNoErr;
  net_para_st para;

  Platform_Init();
  /*Read current configurations*/
  context = ( mico_Context_t *)malloc(sizeof(mico_Context_t) );
  require_action( context, exit, err = kNoMemoryErr );
  memset(context, 0x0, sizeof(mico_Context_t));
  mico_rtos_init_mutex(&context->flashContentInRam_mutex);
  mico_rtos_init_semaphore(&context->micoStatus.sys_state_change_sem, 1); 

  MICOReadConfiguration( context );

  err = MICOInitNotificationCenter  ( context );

  err = MICOAddNotification( mico_notify_READ_APP_INFO, (void *)micoNotify_ReadAppInfoHandler );
  require_noerr( err, exit );  
  
  /*wlan driver and tcpip init*/
  mxchipInit();
  getNetPara(&para, Station);
  formatMACAddr(context->micoStatus.mac, (char *)&para.mac);
  
  mico_log_trace(); 
  mico_log("%s mxchipWNet library version: %s", APP_INFO, system_lib_version());

  /*Start system monotor thread*/
  err = MICOStartSystemMonitor(context);
  require_noerr_action( err, exit, mico_log("ERROR: Unable to start the system monitor.") );
  
  err = MICORegisterSystemMonitor(&mico_monitor, APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000);
  require_noerr( err, exit );
  mico_init_timer(&_watchdog_reload_timer,APPLICATION_WATCHDOG_TIMEOUT_SECONDS*1000-100, _watchdog_reload_timer_handler, NULL);
  mico_start_timer(&_watchdog_reload_timer);
  
  if(context->flashContentInRam.micoSystemConfig.configured != allConfigured){
    mico_log("Empty configuration. Starting configuration mode...");

#ifdef CONFIG_MODE_EASYLINK
  err = startEasyLink( context );
  require_noerr( err, exit );
#endif

#ifdef CONFIG_MODE_WAC
  WACPlatformParameters_t* WAC_Params = NULL;
  WAC_Params = calloc(1, sizeof(WACPlatformParameters_t));
  require(WAC_Params, exit);

  str2hex((unsigned char *)para.mac, WAC_Params->macAddress, 6);
  WAC_Params->isUnconfigured          = 1;
  WAC_Params->supportsAirPlay         = 0;
  WAC_Params->supportsAirPrint        = 0;
  WAC_Params->supports2_4GHzWiFi      = 1;
  WAC_Params->supports5GHzWiFi        = 0;
  WAC_Params->supportsWakeOnWireless  = 0;

  WAC_Params->firmwareRevision =  FIRMWARE_REVISION;
  WAC_Params->hardwareRevision =  HARDWARE_REVISION;
  WAC_Params->serialNumber =      SERIAL_NUMBER;
  WAC_Params->name =              context->flashContentInRam.micoSystemConfig.name;
  WAC_Params->model =             MODEL;
  WAC_Params->manufacturer =      MANUFACTURER;

  WAC_Params->numEAProtocols =    1;
  WAC_Params->eaBundleSeedID =    BUNDLE_SEED_ID;
  WAC_Params->eaProtocols =       (char **)eaProtocols;;

  err = startMFiWAC( context, WAC_Params, 1200);
  free(WAC_Params);
  require_noerr( err, exit );
#endif
  }
  else{
    mico_log("Available configuration. Starting Wi-Fi connection...");
    
    /* Regisist notifications */
    err = MICOAddNotification( mico_notify_WIFI_STATUS_CHANGED, (void *)micoNotify_WifiStatusHandler );
    require_noerr( err, exit ); 
    
    err = MICOAddNotification( mico_notify_WiFI_PARA_CHANGED, (void *)micoNotify_WiFIParaChangedHandler );
    require_noerr( err, exit ); 

    err = MICOAddNotification( mico_notify_DHCP_COMPLETED, (void *)micoNotify_DHCPCompleteHandler );
    require_noerr( err, exit );  
   
    if(context->flashContentInRam.micoSystemConfig.rfPowerSaveEnable == true){
      ps_enable();
    }

    if(context->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable == true){
      mico_mcu_powersave_config(true);
    }

    /*Bonjour service for searching*/
    // if(context->flashContentInRam.micoSystemConfig.bonjourEnable == true){
    //   err = MICOStartBonjourService( Station, context );
    //   require_noerr( err, exit );
    // }

    err = mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "mDNSResponder", mDNSResponder_thread, 0x1000, (void*)context );
    require_noerr_action( err, exit, mico_log("ERROR: Unable to start mDNSResponder thread.") );

    /*Local configuration server*/
    if(context->flashContentInRam.micoSystemConfig.configServerEnable == true){
      err =  MICOStartConfigServer(context);
      require_noerr_action( err, exit, mico_log("ERROR: Unable to start the local server thread.") );
    }

    /*Start mico application*/
    err = MICOStartApplication( context );
    require_noerr( err, exit );
    
    _ConnectToAP( context );
  }


  /*System status changed*/
  while(mico_rtos_get_semaphore(&context->micoStatus.sys_state_change_sem, MICO_WAIT_FOREVER)==kNoErr){
    switch(context->micoStatus.sys_state){
      case eState_Normal:
        break;
      case eState_Software_Reset:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        PlatformSoftReboot();
        break;
      case eState_Wlan_Powerdown:
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(500);
        wifi_power_down();
        break;
      case eState_Standby:
        mico_log("Enter standby mode");
        sendNotifySYSWillPowerOff();
        mico_thread_msleep(200);
        wifi_power_down();
        Platform_Enter_STANDBY();
        break;
      default:
        break;
    }
  }
    
  require_noerr_action( err, exit, mico_log("Closing main thread with err num: %d.", err) );

exit:
  mico_rtos_delete_thread(NULL);
  return kNoErr;
}
Пример #7
0
OSStatus ConfigCreateReportJsonMessage( mico_Context_t * const inContext )
{
  OSStatus err = kNoErr;
  config_delegate_log_trace();
  char name[50], *tempString;
  OTA_Versions_t versions;
  char rfVersion[50];
  char *rfVer = NULL, *rfVerTemp = NULL;

  wlan_driver_version( rfVersion, 50 );
  rfVer = strstr(rfVersion, "version ");
  config_delegate_log("RF version=%s", rfVersion);
  if(rfVer) rfVer = rfVer + strlen("version ");
  rfVerTemp = rfVer;

  for(rfVerTemp = rfVer; *rfVerTemp != ' '; rfVerTemp++);
  *rfVerTemp = 0x0;

  if(inContext->flashContentInRam.micoSystemConfig.configured == wLanUnConfigured){
    /*You can upload a specific menu*/
  }

  mico_rtos_lock_mutex(&inContext->flashContentInRam_mutex);
  snprintf(name, 50, "%s(%c%c%c%c%c%c)",MODEL, 
                                        inContext->micoStatus.mac[9],  inContext->micoStatus.mac[10], 
                                        inContext->micoStatus.mac[12], inContext->micoStatus.mac[13],
                                        inContext->micoStatus.mac[15], inContext->micoStatus.mac[16]);

  versions.fwVersion = FIRMWARE_REVISION;
  versions.hdVersion = HARDWARE_REVISION;
  versions.protocol =  PROTOCOL;
  versions.rfVersion = NULL;

  json_object *sectors, *sector, *subMenuSectors, *subMenuSector, *mainObject;

  sectors = json_object_new_array();
  require( sectors, exit );

  err = MICOAddTopMenu(&mainObject, name, sectors, versions);
  require_noerr(err, exit);

  /*Sector 1*/
  sector = json_object_new_array();
  require( sector, exit );
  err = MICOAddSector(sectors, "MICO SYSTEM",    sector);
  require_noerr(err, exit);

    /*name cell*/
    err = MICOAddStringCellToSector(sector, "Device Name",    inContext->flashContentInRam.micoSystemConfig.name,               "RW", NULL);
    require_noerr(err, exit);

    //Bonjour switcher cell
    err = MICOAddSwitchCellToSector(sector, "Bonjour",        inContext->flashContentInRam.micoSystemConfig.bonjourEnable,      "RW");
    require_noerr(err, exit);

    //RF power save switcher cell
    err = MICOAddSwitchCellToSector(sector, "RF power save",  inContext->flashContentInRam.micoSystemConfig.rfPowerSaveEnable,  "RW");
    require_noerr(err, exit);

    //MCU power save switcher cell
    err = MICOAddSwitchCellToSector(sector, "MCU power save", inContext->flashContentInRam.micoSystemConfig.mcuPowerSaveEnable, "RW");
    require_noerr(err, exit);

    /*sub menu*/
    subMenuSectors = json_object_new_array();
    require( subMenuSectors, exit );
    err = MICOAddMenuCellToSector(sector, "Detail", subMenuSectors);
    require_noerr(err, exit);
      
      subMenuSector = json_object_new_array();
      require( subMenuSector, exit );
      err = MICOAddSector(subMenuSectors,  "",    subMenuSector);
      require_noerr(err, exit);

        err = MICOAddStringCellToSector(subMenuSector, "Firmware Rev.",  FIRMWARE_REVISION, "RO", NULL);
        require_noerr(err, exit);
        err = MICOAddStringCellToSector(subMenuSector, "Hardware Rev.",  HARDWARE_REVISION, "RO", NULL);
        require_noerr(err, exit);
        err = MICOAddStringCellToSector(subMenuSector, "MICO OS Rev.",   system_lib_version(),              "RO", NULL);
        require_noerr(err, exit);
        err = MICOAddStringCellToSector(subMenuSector, "RF Driver Rev.", rfVer,                             "RO", NULL);
        require_noerr(err, exit);
        err = MICOAddStringCellToSector(subMenuSector, "Model",          MODEL,            "RO", NULL);
        require_noerr(err, exit);
        err = MICOAddStringCellToSector(subMenuSector, "Manufacturer",   MANUFACTURER,     "RO", NULL);
        require_noerr(err, exit);
        err = MICOAddStringCellToSector(subMenuSector, "Protocol",       PROTOCOL,         "RO", NULL);
        require_noerr(err, exit);

      subMenuSector = json_object_new_array();
      err = MICOAddSector(subMenuSectors,  "WLAN",    subMenuSector);
      require_noerr(err, exit);
      
        tempString = DataToHexStringWithColons( (uint8_t *)inContext->flashContentInRam.micoSystemConfig.bssid, 6 );
        err = MICOAddStringCellToSector(subMenuSector, "BSSID",        tempString, "RO", NULL);
        require_noerr(err, exit);
        free(tempString);

        err = MICOAddNumberCellToSector(subMenuSector, "Channel",      inContext->flashContentInRam.micoSystemConfig.channel, "RO", NULL);
        require_noerr(err, exit);

        switch(inContext->flashContentInRam.micoSystemConfig.security){
          case SECURITY_TYPE_NONE:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "Open system", "RO", NULL); 
            break;
          case SECURITY_TYPE_WEP:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "WEP",         "RO", NULL); 
            break;
          case SECURITY_TYPE_WPA_TKIP:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "WPA TKIP",    "RO", NULL); 
            break;
          case SECURITY_TYPE_WPA_AES:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "WPA AES",     "RO", NULL); 
            break;
          case SECURITY_TYPE_WPA2_TKIP:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "WPA2 TKIP",   "RO", NULL); 
            break;
          case SECURITY_TYPE_WPA2_AES:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "WPA2 AES",    "RO", NULL); 
            break;
          case SECURITY_TYPE_WPA2_MIXED:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "WPA2 MIXED",  "RO", NULL); 
            break;
          default:
            err = MICOAddStringCellToSector(subMenuSector, "Security",   "Auto",      "RO", NULL); 
            break;
        }
        require_noerr(err, exit); 

        if(inContext->flashContentInRam.micoSystemConfig.keyLength == maxKeyLen){ /*This is a PMK key, generated by user key in WPA security type*/
          tempString = calloc(maxKeyLen+1, 1);
          require_action(tempString, exit, err=kNoMemoryErr);
          memcpy(tempString, inContext->flashContentInRam.micoSystemConfig.key, maxKeyLen);
          err = MICOAddStringCellToSector(subMenuSector, "PMK",          tempString, "RO", NULL);
          require_noerr(err, exit);
          free(tempString);
        }
        else{
          err = MICOAddStringCellToSector(subMenuSector, "KEY",          inContext->flashContentInRam.micoSystemConfig.user_key,  "RO", NULL);
          require_noerr(err, exit);
        }

        /*DHCP cell*/
        err = MICOAddSwitchCellToSector(subMenuSector, "DHCP",        inContext->flashContentInRam.micoSystemConfig.dhcpEnable,   "RO");
        require_noerr(err, exit);
        /*Local cell*/
        err = MICOAddStringCellToSector(subMenuSector, "IP address",  inContext->micoStatus.localIp,   "RO", NULL);
        require_noerr(err, exit);
        /*Netmask cell*/
        err = MICOAddStringCellToSector(subMenuSector, "Net Mask",    inContext->micoStatus.netMask,   "RO", NULL);
        require_noerr(err, exit);
        /*Gateway cell*/
        err = MICOAddStringCellToSector(subMenuSector, "Gateway",     inContext->micoStatus.gateWay,   "RO", NULL);
        require_noerr(err, exit);
        /*DNS server cell*/
        err = MICOAddStringCellToSector(subMenuSector, "DNS Server",  inContext->micoStatus.dnsServer, "RO", NULL);
        require_noerr(err, exit);

  /*Sector 3*/
  sector = json_object_new_array();
  require( sector, exit );
  err = MICOAddSector(sectors, "WLAN",           sector);
  require_noerr(err, exit);

    err = MICOAddStringCellToSector(sector, "Wi-Fi",        inContext->flashContentInRam.micoSystemConfig.ssid,     "RW", NULL);
    require_noerr(err, exit);

    err = MICOAddStringCellToSector(sector, "Password",     inContext->flashContentInRam.micoSystemConfig.user_key, "RW", NULL);
    require_noerr(err, exit);

  /*Sector 4*/
  sector = json_object_new_array();
  require( sector, exit );
  err = MICOAddSector(sectors, "SPP Remote Server",           sector);
  require_noerr(err, exit);


    // SPP protocol remote server connection enable
    err = MICOAddSwitchCellToSector(sector, "Connect SPP Server",   inContext->flashContentInRam.appConfig.remoteServerEnable,   "RW");
    require_noerr(err, exit);

    //Seerver address cell
    err = MICOAddStringCellToSector(sector, "SPP Server",           inContext->flashContentInRam.appConfig.remoteServerDomain,   "RW", NULL);
    require_noerr(err, exit);

    //Seerver port cell
    err = MICOAddNumberCellToSector(sector, "SPP Server Port",      inContext->flashContentInRam.appConfig.remoteServerPort,   "RW", NULL);
    require_noerr(err, exit);

  /*Sector 5*/
  sector = json_object_new_array();
  require( sector, exit );
  err = MICOAddSector(sectors, "MCU IOs",            sector);
  require_noerr(err, exit);

    /*UART Baurdrate cell*/
    json_object *selectArray;
    selectArray = json_object_new_array();
    require( selectArray, exit );
    json_object_array_add(selectArray, json_object_new_int(9600));
    json_object_array_add(selectArray, json_object_new_int(19200));
    json_object_array_add(selectArray, json_object_new_int(38400));
    json_object_array_add(selectArray, json_object_new_int(57600));
    json_object_array_add(selectArray, json_object_new_int(115200));
    err = MICOAddNumberCellToSector(sector, "Baurdrate", inContext->flashContentInRam.appConfig.USART_BaudRate, "RW", selectArray);
    require_noerr(err, exit);

  inContext->micoStatus.easylink_report = mainObject;
  mico_rtos_unlock_mutex(&inContext->flashContentInRam_mutex);
  
exit:
    return err;

}
Пример #8
0
int main(void)
{
  int i, j, fd_listen = -1, fd_udp = -1, fd_client = -1;
  char *buf, ip_address[16],ipstr[32];
  int len;
  int con = -1;
	int opt = 0;
  int clientfd[8];
  fd_set readfds, exceptfds;
  struct timeval_t t;
  struct sockaddr_t addr;
  socklen_t addrLen;
  struct timeval_t timeout;
 
	
#ifdef	DynamicMemAlloc
	int bufferSize;
  libConfig.tcp_buf_dynamic = mxEnable;
  libConfig.tcp_max_connection_num = 12;
  libConfig.tcp_rx_size = 2048;
  libConfig.tcp_tx_size = 2048;
  libConfig.hw_watchdog = 0;
  libConfig.wifi_channel = WIFI_CHANNEL_1_13;
	lib_config(&libConfig);
#endif

  for(i=0;i<8;i++) 
    clientfd[i] = -1;

  buf = (char*)malloc(3*1024);
  
	mxchipInit();
	UART_Init();
  printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
	
#ifdef LowPowerMode
  ps_enable();
#endif	
	
  memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
	wNetConfig.wifi_mode = Station;
	strcpy((char*)wNetConfig.wifi_ssid, AP_NAME);
	strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD);
	wNetConfig.dhcpMode = DHCP_Client;
	StartNetwork(&wNetConfig);
  printf("Connect to %s.....\r\n", wNetConfig.wifi_ssid);
	
  t.tv_sec = 0;
  t.tv_usec = 100;
	
  set_tcp_keepalive(3, 60);
	
  while(1) {
    mxchipTick();	
		
		/*If wifi is established, connect to www.baidu.com, and send a http request*/
#ifdef BlockMode
		if(wifi_up&&webserverTest){
			webserverTest = 0;
			if(gethostbyname(WEB_SERVER, (u8 *)ipstr, 32)!=0){
        printf("DNS test: %s failed \r\n", WEB_SERVER);	
				webserverTest = 1;
				continue;
			}
			printf("DNS test: %s address is %s \r\n", WEB_SERVER, ipstr);
			
			fd_client	= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
			addr.s_ip = inet_addr(ipstr); 
      addr.s_port = 80;
      timeout.tv_sec = 3;
      timeout.tv_usec = 0;
      setsockopt(fd_client,0,SO_CONTIMEO,&timeout,sizeof(struct timeval_t));
			if (connect(fd_client, &addr, sizeof(addr))!=0) {
        close(fd_client);
        fd_client = -1;
        printf("Connect to web server failed! \r\n");
        webserverTest = 1;
      }
			else{
				printf("Connect to web server success! Reading web pages...\r\n");
				send(fd_client, httpRequest, sizeof(httpRequest), 0);
			}
		}
#else
		if(cloud_ip_addr == -1 && dns_pending == 0 && wifi_up){  //DNS function
			cloud_ip_addr = dns_request(WEB_SERVER);
			if(cloud_ip_addr == -1)
				printf("DNS test: %s failed. \r\n", WEB_SERVER); 
			else if (cloud_ip_addr == 0) //DNS pending, waiting for callback
				dns_pending = 1;
		}

    if( fd_client == -1 && cloud_ip_addr!=-1 && !dns_pending){
			fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
			setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4);
			addr.s_ip = cloud_ip_addr; 
			addr.s_port = 80;
      printf("Connecting to %s..., at port %d\r\n", inet_ntoa(ipstr, cloud_ip_addr), addr.s_port); 
			if (connect(fd_client, &addr, sizeof(addr))!=0) {
				printf("Connect to %s failed.\r\n", WEB_SERVER); 
        printf("Free: %d, max length: %d.\r\n", total_free, max_len);
      }
		}
    if(cloud_connected == 1){
      send(fd_client, httpRequest, sizeof(httpRequest), 0);
      cloud_connected = 0;     
    }   
#endif
			
		/*Establish a TCP server that accept the tcp clients connections*/
		if (fd_listen==-1) {
      fd_listen = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
#ifdef	DynamicMemAlloc				
			  bufferSize = 5*1024;
				setsockopt(fd_listen,0,SO_RDBUFLEN,&bufferSize,4);
				bufferSize = 5*1024;
				setsockopt(fd_listen,0,SO_WRBUFLEN,&bufferSize,4);
#endif	
      addr.s_port = 8080;
      bind(fd_listen, &addr, sizeof(addr));
      listen(fd_listen, 0);
      printf("TCP server established at port: %d \r\n", addr.s_port);
    }
		
		/*Establish a UDP port to receive any data sent to this port*/
		if (fd_udp==-1) {
      fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP);
      addr.s_port = 8090;
      bind(fd_udp, &addr, sizeof(addr));
      printf("Open UDP port %d\r\n", addr.s_port);
    }
		
		/*Check status on erery sockets */
		FD_ZERO(&readfds);
		FD_SET(fd_listen, &readfds);	
		FD_SET(fd_udp, &readfds);	
		if(fd_client!=-1)
		  FD_SET(fd_client, &readfds);
		for(i=0;i<8;i++) {
			if (clientfd[i] != -1)
				FD_SET(clientfd[i], &readfds);
		}
		
		select(1, &readfds, NULL, &exceptfds, &t);
    
    /*Check tcp connection requests */
		if(FD_ISSET(fd_listen, &readfds))
		{
			j = accept(fd_listen, &addr, &len);
			if (j > 0) {
			  inet_ntoa(ip_address, addr.s_ip );
			  printf("Client %s:%d connected\r\n", ip_address, addr.s_port);
			  for(i=0;i<8;i++) {
				  if (clientfd[i] == -1) {
					  clientfd[i] = j;
					  break;
				  }
			  }
			}
		}
		
		/*Read html data from www.baidu.com */
		if(fd_client != -1){
			if(FD_ISSET(fd_client, &readfds))
			{
				con = recv(fd_client, buf, 2*1024, 0);
				if(con > 0)
					printf("Get %s data successful! data length: %d bytes\r\n", WEB_SERVER, con);
				else{
					close(fd_client);
					fd_client = -1;
          cloud_ip_addr = -1;
          webserverTest = 1;
					printf("Web connection closed.\r\n");
				}
			}
	  }
    
   /*Read data from tcp clients and send data back */ 
	 for(i=0;i<8;i++) {
      if (clientfd[i] != -1) {
        if (FD_ISSET(clientfd[i], &readfds)) {
          con = recv(clientfd[i], buf, 1*1024, 0);
          if (con > 0) 
            send(clientfd[i], buf, con, 0);
          else {
            close(clientfd[i]);
            clientfd[i] = -1;
          }
        }
        else if (FD_ISSET(clientfd[i], &exceptfds))
          clientfd[i] = -1;
      }
    }
		
		/*Read data from udp and send data back */ 
		if (FD_ISSET(fd_udp, &readfds)) {
      con = recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen);
      sendto(fd_udp, buf, con, 0, &addr, sizeof(struct sockaddr_t));
    }
	}
}
Пример #9
0
/* mxchip library manufacture test. */
void mxchip_mfg_test(void)
{
  char str[128];
  char mac[6];
  char *ssid;
  mico_uart_config_t uart_config;
  volatile ring_buffer_t  rx_buffer;
  volatile uint8_t *      rx_data;
  mico_debug_enabled = 0;
  
  rx_data = malloc(50);
  require(rx_data, exit);
  
  /* Initialize UART interface */
  uart_config.baud_rate    = 115200;
  uart_config.data_width   = DATA_WIDTH_8BIT;
  uart_config.parity       = NO_PARITY;
  uart_config.stop_bits    = STOP_BITS_1;
  uart_config.flow_control = FLOW_CONTROL_DISABLED;
  uart_config.flags = UART_WAKEUP_DISABLE;

  ring_buffer_init ((ring_buffer_t *)&rx_buffer, (uint8_t *)rx_data, 50);
  MicoUartInitialize (MFG_TEST, &uart_config, (ring_buffer_t *)&rx_buffer);

  mf_printf ("==== MXCHIP Manufacture Test ====\r\n");
  mf_printf ("Serial Number: ");
  mf_printf (SERIAL_NUMBER);
  mf_printf ("\r\n");

  mf_printf ("App CRC: ");
  memset (str, 0, sizeof (str));
  app_crc (str, sizeof (str));
  mf_printf (str);
  mf_printf ("\r\n");

  mf_printf ("Bootloader Version: ");
  mf_printf (mico_get_bootloader_ver());
  mf_printf ("\r\n");
  sprintf (str, "Library Version: %s\r\n", system_lib_version());
  mf_printf (str);
  mf_printf ("APP Version: ");
  memset (str, 0, sizeof (str));
  system_version (str, sizeof (str));
  mf_printf (str);
  mf_printf ("\r\n");
  memset (str, 0, sizeof (str));
  wlan_driver_version (str, sizeof (str));
  mf_printf ("Driver: ");
  mf_printf (str);
  mf_printf ("\r\n");

#ifdef MICO_BLUETOOTH_ENABLE
  /* Initialise MICO SmartBridge */
  mico_bt_init( MICO_BT_HCI_MODE, "SmartBridge Device", 0, 0 );  //Client + server connections
  mico_bt_smartbridge_init( 0 );
  mico_bt_dev_read_local_addr( (uint8_t *)mac );
  sprintf( str, "Local Bluetooth Address: %02X-%02X-%02X-%02X-%02X-%02X\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5] );
  mf_printf (str);
  ble_scan();
#endif

  wlan_get_mac_address (mac);
  sprintf (str, "MAC: %02X-%02X-%02X-%02X-%02X-%02X\r\n",
          mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  mf_printf(str);
  mfg_scan();

  if (test_for_app==0) {
  ssid = ssid_get();
    mfg_connect (ssid);
  }

exit:
  mico_thread_sleep(MICO_NEVER_TIMEOUT);
}
Пример #10
0
void mxchip_mfg_test(mico_Context_t *inContex)
{
  char str[80];
  char mac[6];
  char *ssid;
  UNUSED_PARAMETER(inContex);
  mico_uart_config_t uart_config;
  volatile ring_buffer_t  rx_buffer;
  volatile uint8_t *      rx_data;
  
  rx_data = malloc(100);
  require(rx_data, exit);
  
  /* Initialize UART interface */
  uart_config.baud_rate    = 115200;
  uart_config.data_width   = DATA_WIDTH_8BIT;
  uart_config.parity       = NO_PARITY;
  uart_config.stop_bits    = STOP_BITS_1;
  uart_config.flow_control = FLOW_CONTROL_DISABLED;
  uart_config.flags = UART_WAKEUP_DISABLE;
  
  ring_buffer_init  ( (ring_buffer_t *)&rx_buffer, (uint8_t *)rx_data, 100 );
  MicoUartInitialize( MFG_TEST, &uart_config, (ring_buffer_t *)&rx_buffer );  
  
  sprintf(str, "Library Version: %s\r\n", system_lib_version());
  mf_printf(str);
  mf_printf("APP Version: ");
  //memset(str, 0, sizeof(str));
  //system_version(str, sizeof(str));
  sprintf(str, "%s, build at %s %s, ", APP_INFO, __TIME__, __DATE__);
  mf_printf(str);
  sprintf(str, "%d.%d.%d",(u8)(ZC_MODULE_VERSION >> 16),(u8)(ZC_MODULE_VERSION >> 8),(u8)(ZC_MODULE_VERSION));
  mf_printf(str);
  mf_printf("\r\n");
  mf_printf("Uart Info: ");
  //memset(str, 0, sizeof(str));
  //system_version(str, sizeof(str));
  mf_printf(UART_INFO);
  mf_printf("\r\n");
  mf_printf("Server Info: ");
  //memset(str, 0, sizeof(str));
  //system_version(str, sizeof(str));
  mf_printf(ADDR_INFO);
  mf_printf("\r\n");
  memset(str, 0, sizeof(str));
  wlan_driver_version(str, sizeof(str));
  mf_printf("Driver: ");
  mf_printf(str);
  mf_printf("\r\n");
  wlan_get_mac_address(mac);
  sprintf(str, "MAC: %02X-%02X-%02X-%02X-%02X-%02X\r\n",
          mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  mf_printf(str);
  
  mfg_scan();
  
  ssid = ssid_get();
  mfg_connect(ssid);
  
exit:
  mico_thread_sleep(MICO_NEVER_TIMEOUT);
}
Пример #11
0
int main(void)
{
	int 			fd_client = -1;
	char 			*buf;
	int 			con = -1;
	int 			opt = 0;
	fd_set 			readfds, exceptfds;
	struct 			timeval_t t;
	struct 			sockaddr_t addr;
	lib_config_t 	libConfig;
	volatile int	setval;
	volatile int	readval;

	buf = (char*)malloc(3*1024);

	libConfig.tcp_buf_dynamic = mxEnable;
	libConfig.tcp_max_connection_num = 12;
	libConfig.tcp_rx_size = 2048;
	libConfig.tcp_tx_size = 2048;
	libConfig.hw_watchdog = 0;
	libConfig.wifi_channel = WIFI_CHANNEL_1_13;
	lib_config(&libConfig);
  
	mxchipInit();
	UART_Init();


	printf("\r\n%s\r\n mxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
	printf(menu);
	printf ("\nMXCHIP> ");
	
	//init http
	set_tcp_keepalive(3, 60);
	setSslMaxlen(6*1024);
	t.tv_sec = 0;
  	t.tv_usec = 100;
	
//	void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

	while(1)
	{
		mxchipTick();	
		if(menu_enable)
			Main_Menu();
		if(configSuccess)
		{
			wNetConfig.wifi_mode = Station;
			wNetConfig.dhcpMode = DHCP_Client;
			StartNetwork(&wNetConfig);
			printf("connect to %s.....\r\n", wNetConfig.wifi_ssid);
			configSuccess = 0;
			menu_enable = 1;

			sysTick_configuration();
			config_gpio_pc6();
			config_gpio_pb8();

			while(1) 
			{
				readval= GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_6);
				mxchipTick();
				//If wifi is established, connect to WEBSERVER, and send a http request
				if(https_server_addr == 0 && dns_pending == 0)
				{  
					//DNS function
					https_server_addr = dns_request(WEB_SERVER);
					if(https_server_addr == -1)
						printf("DNS test: %s failed. \r\n", WEB_SERVER); 
					else if (https_server_addr == 0) //DNS pending, waiting for callback
					{
						dns_pending = 1;
						printf("2DNS test: %s success. \r\n", WEB_SERVER); 
						printf("21http server addr=%x\n",https_server_addr);
					}
				}
				
				if( fd_client == -1 && (u32)https_server_addr>0)
				{
					fd_client = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
					setsockopt(fd_client,0,SO_BLOCKMODE,&opt,4);
					addr.s_ip = https_server_addr; 
					addr.s_port = 443;
					if (connect(fd_client, &addr, sizeof(addr))!=0) 
						printf("Connect to %s failed.\r\n", WEB_SERVER); 
					else   
						printf("Connect to %s success.\r\n", WEB_SERVER); 	
				}

				if(serverConnectted&&sslConnectted==0)
				{
					printf("Connect to web server success! Setup SSL ecryption...\r\n");
					if (setSSLmode(1, fd_client)!= MXCHIP_SUCCESS)
					{
						printf("SSL connect fail\r\n");
						close(fd_client);
						fd_client = -1;
						serverConnectted = 0;
					} 
					else 
					{
						printf("SSL connect\r\n");
						sslConnectted = 1;
					}
				}	
		
				if(readval==1&&sslConnectted)
				{
					printf("read value is 1\n");
					send(fd_client, sendhttpRequest, strlen(sendhttpRequest), 0);
				//	return 0;
				}
				if(check_responce&&sslConnectted)
				{
					printf("readval=%d\n",readval);
					check_responce=0;
					send(fd_client, responcehttpRequest, strlen(responcehttpRequest), 0);
					//Check status on erery sockets 
					FD_ZERO(&readfds);

					if(sslConnectted)
						FD_SET(fd_client, &readfds);
		
					select(1, &readfds, NULL, &exceptfds, &t);
		
					//Read html data from WEBSERVER
					if(sslConnectted)
					{
						if(FD_ISSET(fd_client, &readfds))
						{
							con = recv(fd_client, buf, 2*1024, 0);
							if(con > 0)
							{
								printf("Get %s data successful! data length: %d bytes data\r\n", WEB_SERVER, con);
								setval=r_http(buf);
								printf("read from http  is %d\n",setval);
					
								if(setval==1)
									GPIO_SetBits(GPIOB,GPIO_Pin_8);
								 else
								  	GPIO_ResetBits(GPIOB,GPIO_Pin_8);
							}
							else
							{
								close(fd_client);
								serverConnectted = 0;
								sslConnectted = 0;
								fd_client = -1;
								printf("Web connection closed.\r\n");
							}
						}
					}
				}
			}
		}
		
		if(easylink)
		{
			OpenEasylink2(60);	
			easylink = 0;
		}
	}
}