/*******************************************************************************
	scan.c	3/15/04
   - This sample program is for use with the CompactFlash WiFi Application Kit.
     The program will initialize the WiFi card and scan the network for other
     WiFi devices that are in Ad-Hoc mode and/or access points.

     NOTE: 	No network parameter setting are need since the Compactflash card does
            not actually join an 802.11b network.

*******************************************************************************/
//#define  CF_VERBOSE
#define  CF_DEBUG

#define 	WIFI_SCANRATE	5

#define	WIFI_INIT	\
  			pd_ioctl(0,WIFI_MODE,"BSS",0);	\
   		pd_ioctl(0,WIFI_SSID,"",0);		\
			pd_ioctl(0,WIFI_ROAM,"manual",0);

#memmap	xmem

#define TCPCONFIG 1
#define PKTDRV	"cfprism.lib"		// bring in the cfprism.lib
#use dcrtcp.lib                  // bring in tcp functions

void prism_scan();
void prism_scanresult();
void display_scanresult();
void gotoxy (int x, int y);
void cls ();

unsigned scanbuf[1000];

void main()
{
   brdInit();		// Initialize I/O
   sock_init();

	while(1)
   {
   	costate
      {
      	waitfor(DelayMs(10));
         tcp_tick(NULL);
      }
		costate
      {
 			prism_scanresult();
			display_scanresult();
			waitfor(DelayMs(10));
		}
		costate
      {
      	prism_scan();
			waitfor(DelayMs(WIFI_SCANRATE*1000));
		}
 	}
}


void prism_scan()
{
	pd_ioctl(0,WIFI_SCANREQ,(char *)0,0);
}

void prism_scanresult()
{
	pd_ioctl(0,WIFI_SCANRES,(char *)scanbuf,sizeof(scanbuf));
}

void display_scanresult()
{
	int len,i,j,offset;
	unsigned char c;
	int nstations;
   float temp[2];
	static int display;

	len = scanbuf[0];
	offset = 4;
	nstations = 0;

	if(scanbuf[1] == 0xFD88 && (scanbuf[3] == 0 || len == 3))
   {
		gotoxy(0,0);
      printf("Scanning...%d %x\n",scanbuf[0],scanbuf[3]);
		display ++;
		return;
	}
	if(!display)
   	return;

	cls();
	display = 0;
	gotoxy(0,0);
   printf("Done...\n");
	gotoxy(0,1);

	printf("WiFi Scan Results %d Entries\n",(len-3)/31);
   printf("Channel   Signal   Noise   MAC	  		 AccessPoint SSID\n");
   printf("-------------------------------------------------------------------\n");

	while(offset < len)
   {
		i = 0;

		// 	 Channel	 Signal   Noise
      printf("%3d        %3d     %3d     ",
		scanbuf[offset+0],
		scanbuf[offset+1],
		scanbuf[offset+2]);
		// BSSID
		printf("%02x:%02x:%02x:%02x:%02x:%02x      ",
		scanbuf[offset+3] & 0xff,scanbuf[offset+3] >> 8,
		scanbuf[offset+4] & 0xff,scanbuf[offset+4] >> 8,
		scanbuf[offset+5] & 0xff,scanbuf[offset+5] >> 8);
		// SSID
		for(j=0;j<scanbuf[offset+8];j++)
      {
			if(j % 2)
				c = scanbuf[offset+9+(j/2)] >> 8;
			else
				c = scanbuf[offset+9+(j/2)] & 0xff;
			if(c > 32 && c < 127)
				putchar(c);
			else
				printf("(%2x)",c);
		}
		printf("\n");
		offset += 31;
		nstations ++;
	}

   // turn on the leds to indicate the number of stations found (max is binary 3)
   switch(nstations)
   {
		case 0:
			BitWrPortI(PGDR,&PGDRShadow,1,7);
			BitWrPortI(PGDR,&PGDRShadow,1,6);
			break;
		case 1:
			BitWrPortI(PGDR,&PGDRShadow,0,7);
			BitWrPortI(PGDR,&PGDRShadow,1,6);
			break;
		case 2:
			BitWrPortI(PGDR,&PGDRShadow,1,7);
			BitWrPortI(PGDR,&PGDRShadow,0,6);
			break;
		default:
			BitWrPortI(PGDR,&PGDRShadow,0,7);
			BitWrPortI(PGDR,&PGDRShadow,0,6);
			break;
	}

}

void cls ()
{
   printf ( "\x1Bt" );            	// Space Opens Window
}

void gotoxy (int x, int y)
{
	char xl,xh,yl,yh;
   x += 0x20;
   y += 0x20;
   printf ("\x1B=%c%c",x,y );
}

