Example #1
0
void zg_drv_process()
{
//	taskENTER_CRITICAL();

	// TX frame
	if (tx_ready && !cnf_pending) {
		zg_send(zg_buf, zg_buf_len);
		tx_ready = 0;
		cnf_pending = 1;
	}

	// process interrupt
	if (intr_occured)
	{
		zg_process_isr();
	}

	if (intr_valid) {
		switch (zg_buf[1]) {
		case ZG_MAC_TYPE_TXDATA_CONFIRM:
			cnf_pending = 0;
			break;
		case ZG_MAC_TYPE_MGMT_CONFIRM:
			if (zg_buf[3] == ZG_RESULT_SUCCESS) {
				switch (zg_buf[2]) {
                case ZG_MAC_SUBTYPE_MGMT_SCAN:
                    scan_cnt = 1;
                    break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_GET_PARAM:
					mac[0] = zg_buf[7];
					mac[1] = zg_buf[8];
					mac[2] = zg_buf[9];
					mac[3] = zg_buf[10];
					mac[4] = zg_buf[11];
					mac[5] = zg_buf[12];
					zg_drv_state = DRV_STATE_SETUP_SECURITY;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_WEP_KEY:
					zg_drv_state = DRV_STATE_ENABLE_CONN_MANAGE;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_CALC_PSK:
					memcpy(wpa_psk_key, ((zg_psk_calc_cnf_t*)&zg_buf[3])->psk, 32);
					zg_drv_state = DRV_STATE_INSTALL_PSK;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_PMK_KEY:
					zg_drv_state = DRV_STATE_ENABLE_CONN_MANAGE;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT_MANAGE:
					zg_drv_state = DRV_STATE_START_CONN;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT:
					LEDConn_on();
					zg_conn_status = 1;	// connected
					break;
				default:
					break;
				}
			}
			break;
		case ZG_MAC_TYPE_RXDATA_INDICATE:
			zg_drv_state = DRV_STATE_PROCESS_RX;
			break;
		case ZG_MAC_TYPE_MGMT_INDICATE:
			switch (zg_buf[2]) {
			case ZG_MAC_SUBTYPE_MGMT_IND_DISASSOC:
			case ZG_MAC_SUBTYPE_MGMT_IND_DEAUTH:
				LEDConn_off();
				zg_conn_status = 0;	// lost connection

				//try to reconnect
				zg_drv_state = DRV_STATE_START_CONN;
				break;
			case ZG_MAC_SUBTYPE_MGMT_IND_CONN_STATUS:
				{
					uint16_t status = (((uint16_t)(zg_buf[3]))<<8)|zg_buf[4];

					if (status == 1 || status == 5) {
						LEDConn_off();
						zg_conn_status = 0;	// not connected
					}
					else if (status == 2 || status == 6) {
						LEDConn_on();
						zg_conn_status = 1;	// connected
					}
				}
				break;
			}
			break;
		}

		intr_valid = 0;
	}

	switch (zg_drv_state) {
	case DRV_STATE_INIT:
		zg_drv_state = DRV_STATE_GET_MAC;
		break;
	case DRV_STATE_GET_MAC:
		// get MAC address
		zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
		zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
		zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_GET_PARAM;
		zg_buf[3] = 0;
		zg_buf[4] = ZG_PARAM_MAC_ADDRESS;
		spi_transfer(zg_buf, 5, 1);

		zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
		spi_transfer(zg_buf, 1, 1);

		zg_drv_state = DRV_STATE_IDLE;
		break;
	case DRV_STATE_SETUP_SECURITY:
		switch (security_type) {
		case ZG_SECURITY_TYPE_NONE:
			zg_drv_state = DRV_STATE_ENABLE_CONN_MANAGE;
			break;
		case ZG_SECURITY_TYPE_WEP:
			// Install all four WEP keys on G2100
			zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
			zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
			zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_WEP_KEY;
			zg_write_wep_key(&zg_buf[3]);
			spi_transfer(zg_buf, ZG_WEP_KEY_REQ_SIZE+3, 1);

			zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
			spi_transfer(zg_buf, 1, 1);

			zg_drv_state = DRV_STATE_IDLE;
			break;
		case ZG_SECURITY_TYPE_WPA:
		case ZG_SECURITY_TYPE_WPA2:
			// Initiate PSK calculation on G2100
			zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
			zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
			zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_CALC_PSK;
			zg_calc_psk_key(&zg_buf[3]);
			spi_transfer(zg_buf, ZG_PSK_CALC_REQ_SIZE+3, 1);

			zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
			spi_transfer(zg_buf, 1, 1);

			zg_drv_state = DRV_STATE_IDLE;
			break;
		default:
			break;
		}
		break;
	case DRV_STATE_INSTALL_PSK:
		// Install the PSK key on G2100
		zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
		zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
		zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_PMK_KEY;
		zg_write_psk_key(&zg_buf[3]);
		spi_transfer(zg_buf, ZG_PMK_KEY_REQ_SIZE+3, 1);

		zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
		spi_transfer(zg_buf, 1, 1);

		zg_drv_state = DRV_STATE_IDLE;
		break;
	case DRV_STATE_ENABLE_CONN_MANAGE:
		// enable connection manager
		zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
		zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
		zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT_MANAGE;
		zg_buf[3] = 0x01;	// 0x01 - enable; 0x00 - disable
		zg_buf[4] = 10;		// num retries to reconnect
		zg_buf[5] = 0x10 | 0x02 | 0x01;	// 0x10 -	enable start and stop indication messages
										// 		 	from G2100 during reconnection
										// 0x02 -	start reconnection on receiving a deauthentication
										// 			message from the AP
										// 0x01 -	start reconnection when the missed beacon count
										// 			exceeds the threshold. uses default value of
										//			100 missed beacons if not set during initialization
		zg_buf[6] = 0;
		spi_transfer(zg_buf, 7, 1);

		zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
		spi_transfer(zg_buf, 1, 1);

		zg_drv_state = DRV_STATE_IDLE;
		break;
	case DRV_STATE_START_CONN:
	{
		zg_connect_req_t* cmd = (zg_connect_req_t*)&zg_buf[3];

		// start connection to AP
		zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
		zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
		zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT;

		cmd->secType = security_type;

		cmd->ssidLen = ssid_len;
		memset(cmd->ssid, 0, 32);
		memcpy(cmd->ssid, ssid, ssid_len);

		// units of 100 milliseconds
		cmd->sleepDuration = 0;

		if (wireless_mode == WIRELESS_MODE_INFRA)
			cmd->modeBss = 1;
		else if (wireless_mode == WIRELESS_MODE_ADHOC)
			cmd->modeBss = 2;

		spi_transfer(zg_buf, ZG_CONNECT_REQ_SIZE+3, 1);

		zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
		spi_transfer(zg_buf, 1, 1);

		zg_drv_state = DRV_STATE_IDLE;
		break;
	}
	case DRV_STATE_PROCESS_RX:
		zg_recv(zg_buf, &zg_buf_len);
		rx_ready = 1;

		zg_drv_state = DRV_STATE_IDLE;
		break;
	case DRV_STATE_IDLE:
		break;
	}
//	taskEXIT_CRITICAL();
}
Example #2
0
void zg_drv_process()
{
   // TX frame
   if (tx_ready && !cnf_pending) {
      zg_send(zg_buf, zg_buf_len);
      tx_ready = 0;
      cnf_pending = 1;
   }

//   xprintf("zg_drv_process() after // TX frame");FFL_();

   // process interrupt
   if (intr_occured) {
      zg_process_isr();
   }
//   xprintf("zg_drv_process() after // process interrupt");FFL_();

//   uart_writestr("\n");
//   uart_send_32_Hex(intr_valid);
//   uart_writestr("\n");
//   uart_send_32_Hex(zg_buf[1]);
//   uart_writestr("\n");
//   uart_send_32_Hex(zg_buf[2]);
//   uart_writestr("\n");

	if (intr_valid) {
		switch (zg_buf[1]) {
		case ZG_MAC_TYPE_TXDATA_CONFIRM:
			cnf_pending = 0;
			break;
		case ZG_MAC_TYPE_MGMT_CONFIRM:
			if (zg_buf[3] == ZG_RESULT_SUCCESS) {
				switch (zg_buf[2]) {
                case ZG_MAC_SUBTYPE_MGMT_SCAN:
                    scan_cnt = 1;
                    break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_GET_PARAM:
//          	   	uart_writestr("\ncase ZG_MAC_SUBTYPE_MGMT_REQ_GET_PARAM");
					mac[0] = zg_buf[7];
					mac[1] = zg_buf[8];
					mac[2] = zg_buf[9];
					mac[3] = zg_buf[10];
					mac[4] = zg_buf[11];
					mac[5] = zg_buf[12];
					zg_drv_state = DRV_STATE_SETUP_SECURITY;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_WEP_KEY:
//         	   		uart_writestr("\ncase ZG_MAC_SUBTYPE_MGMT_REQ_WEP_KEY");
					zg_drv_state = DRV_STATE_ENABLE_CONN_MANAGE;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_CALC_PSK:
					memcpy(wpa_psk_key, ((zg_psk_calc_cnf_t*)&zg_buf[3])->psk, ZG_MAX_PMK_LEN);
					zg_drv_state = DRV_STATE_INSTALL_PSK;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_PMK_KEY:
					zg_drv_state = DRV_STATE_ENABLE_CONN_MANAGE;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT_MANAGE:
					zg_drv_state = DRV_STATE_START_CONN;
					break;
				case ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT:
//					LEDConn_on();
					zg_conn_status = 1;      // connected
					break;
				default:
					break;
				}
			}
			break;
		case ZG_MAC_TYPE_RXDATA_INDICATE:
			zg_drv_state = DRV_STATE_PROCESS_RX;
			break;
		case ZG_MAC_TYPE_MGMT_INDICATE:
			switch (zg_buf[2]) {
			case ZG_MAC_SUBTYPE_MGMT_IND_DISASSOC:
			case ZG_MAC_SUBTYPE_MGMT_IND_DEAUTH:
 //           	LEDConn_off();
				zg_conn_status = 0; // lost connection

				//try to reconnect
				zg_drv_state = DRV_STATE_START_CONN;
				break;
			case ZG_MAC_SUBTYPE_MGMT_IND_CONN_STATUS:
				{
					U16 status = (((U16)(zg_buf[3]))<<8)|zg_buf[4];

					if (status == 1 || status == 5) {
//						LEDConn_off();
						zg_conn_status = 0;	// not connected
					}
					else if (status == 2 || status == 6) {
//						LEDConn_on();
						zg_conn_status = 1;   // connected
					}
				}
				break;
			}
			break;
		}

      intr_valid = 0;
   }

   switch (zg_drv_state) {
   case DRV_STATE_INIT:
      zg_drv_state = DRV_STATE_GET_MAC;
      break;
   case DRV_STATE_GET_MAC:
      // get MAC address
		xprintf(INFO "// get MAC address");FFL_();
		xprintf("[INFO]-if this is last printed wifi info, then the g2100 probably cannot be seen");FFL_();
//	   	uart_writestr("\n// get MAC address");
		zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
		zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
		zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_GET_PARAM;
		zg_buf[3] = 0;
		zg_buf[4] = ZG_PARAM_MAC_ADDRESS;
		spi_transfer(zg_buf, 5, 1);
		zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
      spi_transfer(zg_buf, 1, 1);

      zg_drv_state = DRV_STATE_IDLE;
      break;
	case DRV_STATE_SETUP_SECURITY:
		switch (security_type) {
		case ZG_SECURITY_TYPE_NONE:
			zg_drv_state = DRV_STATE_ENABLE_CONN_MANAGE;
			break;
		case ZG_SECURITY_TYPE_WEP:
			// Install all four WEP keys on G2100
			xprintf(INFO "// Install all four WEP keys on G2100");FFL_();
//   	   	uart_writestr("\n// Install all four WEP keys on G2100");
			zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
			zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
			zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_WEP_KEY;
			zg_write_wep_key(&zg_buf[3]);
			spi_transfer(zg_buf, ZG_WEP_KEY_REQ_SIZE+3, 1);

         zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
         spi_transfer(zg_buf, 1, 1);

         zg_drv_state = DRV_STATE_IDLE;
         break;
		case ZG_SECURITY_TYPE_WPA:
		case ZG_SECURITY_TYPE_WPA2:
			// Initiate PSK calculation on G2100
//    	  	RTC_print_time();
			xprintf(INFO "// Initiate PSK calculation on G2100");FFL_();
//      	uart_writestr("\n// Initiate PSK calculation on G2100");
			zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
			zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
			zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_CALC_PSK;
			zg_calc_psk_key(&zg_buf[3]);
			spi_transfer(zg_buf, ZG_PSK_CALC_REQ_SIZE+3, 1);

			zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
			spi_transfer(zg_buf, 1, 1);

			zg_drv_state = DRV_STATE_IDLE;
			break;
		case ZG_SECURITY_TYPE_WPA_PRECALC:
		case ZG_SECURITY_TYPE_WPA2_PRECALC:
//			memcpy_P(wpa_psk_key, security_data, ZG_MAX_PMK_LEN);
			memcpy(wpa_psk_key, security_data, ZG_MAX_PMK_LEN);
			zg_drv_state = DRV_STATE_INSTALL_PSK;
		default:
			break;
		}
		break;
	case DRV_STATE_INSTALL_PSK:
		// Install the PSK key on G2100
		xprintf(INFO "// Install the PSK key on G2100");FFL_();
		zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
		zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
		zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_PMK_KEY;
		zg_write_psk_key(&zg_buf[3]);
		spi_transfer(zg_buf, ZG_PMK_KEY_REQ_SIZE+3, 1);

		zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
		spi_transfer(zg_buf, 1, 1);

		zg_drv_state = DRV_STATE_IDLE;
		break;
	case DRV_STATE_ENABLE_CONN_MANAGE:
		// enable connection manager
		xprintf(INFO "// enable connection manager");FFL_();
		zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
		zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
		zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT_MANAGE;
		zg_buf[3] = 0x01; // 0x01 - enable; 0x00 - disable
		zg_buf[4] = 10;           // num retries to reconnect
		zg_buf[5] = 0x10 | 0x02 | 0x01;   // 0x10 -       enable start and stop indication messages
                              //                        from G2100 during reconnection
                              // 0x02 - start reconnection on receiving a deauthentication
                              //                        message from the AP
                              // 0x01 - start reconnection when the missed beacon count
                              //                        exceeds the threshold. uses default value of
                              //                        100 missed beacons if not set during initialization
      zg_buf[6] = 0;
      spi_transfer(zg_buf, 7, 1);

      zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
      spi_transfer(zg_buf, 1, 1);

      zg_drv_state = DRV_STATE_IDLE;
      break;
   case DRV_STATE_START_CONN:
   {
      zg_connect_req_t* cmd = (zg_connect_req_t*)&zg_buf[3];

      // start connection to AP
	  xprintf(INFO "// start connection to AP");FFL_();
      zg_buf[0] = ZG_CMD_WT_FIFO_MGMT;
      zg_buf[1] = ZG_MAC_TYPE_MGMT_REQ;
      zg_buf[2] = ZG_MAC_SUBTYPE_MGMT_REQ_CONNECT;

		// adjust security_type to mask the PRECALC types
		if (security_type < ZG_SECURITY_TYPE_WPA_PRECALC)
			cmd->secType = security_type;
		else
			cmd->secType = security_type - 2;

		cmd->ssidLen = ssid_len;
		strncpy(cmd->ssid, ssid, ZG_MAX_SSID_LENGTH);
      // units of 100 milliseconds
      cmd->sleepDuration = 0;

      if (wireless_mode == WIRELESS_MODE_INFRA)
         cmd->modeBss = 1;
      else if (wireless_mode == WIRELESS_MODE_ADHOC)
         cmd->modeBss = 2;

      spi_transfer(zg_buf, ZG_CONNECT_REQ_SIZE+3, 1);

      zg_buf[0] = ZG_CMD_WT_FIFO_DONE;
      spi_transfer(zg_buf, 1, 1);

      zg_drv_state = DRV_STATE_IDLE;
      break;
   }
	case DRV_STATE_PROCESS_RX:
		zg_recv(zg_buf, &zg_buf_len);
		rx_ready = 1;

		zg_drv_state = DRV_STATE_IDLE;
		break;
	case DRV_STATE_IDLE:
		break;
	}
//   xprintf("[END]-zg_drv_process()");FFL_();
}