Example #1
0
int devlogicalnoc_xmit_sleep(uchar dst, uchar src_logical_addr, uchar* data,
        int nbytes, unsigned int sleep)
{
    int framesize;
    int i;
    uchar *tptr;
    ulong ifc_shift_offset = (0 & 0xFFF) << 4;
    ulong savedSR;

    //@@printf("in devnet_xmit, sending [%d] bytes to [%s], proto [%d], via ifc [%d]\n", nbytes, dst, proto, whichifc);

    /*  Save SR, then disable intrs:    */
    savedSR = getpsr();
    splhi();

    /*  Save the dst for future use */
    *(NIC_DST + ifc_shift_offset) = dst;

    framesize = devnet_getmaxfsz(0);

    framesize -= 1;//one information byte

    //  Write data
    tptr = data;
    while (nbytes > 0)
    {
        *(NIC_TDR + ifc_shift_offset) = src_logical_addr;//socket information flit

        for (i = 0; i < min(nbytes, framesize); i++)
        {
            *(NIC_TDR + ifc_shift_offset) = *tptr++;
        }
        if (nbytes < framesize)
            devmac_ctl(NIC_NCR_WRITE, NIC_CMD_TRANSMIT, 0);

        //failed to send, do it again
        while ((*(NIC_NSR + ifc_shift_offset)) & 0x1)
        {
            //printf("Failed to send %d \n", dst);
            devmac_ctl(NIC_NCR_WRITE, NIC_CMD_TRANSMIT, 0);
        }

        //printf("sending: n = %d, f = %d %d\n", nbytes, i, dst);
        nbytes -= i;

    }

    //send closing link message
    if (sleep > 0)
    {
        *(NIC_TDR + ifc_shift_offset) = (1 << 7);
        *(NIC_TDR + ifc_shift_offset) = (sleep) & 0xFF;
        *(NIC_TDR + ifc_shift_offset) = (sleep >> 8) & 0xFF;
        *(NIC_TDR + ifc_shift_offset) = (sleep >> 16) & 0xFF;
        *(NIC_TDR + ifc_shift_offset) = (sleep >> 24) & 0xFF;

        devmac_ctl(NIC_NCR_WRITE, NIC_CMD_TRANSMIT, 0);
        //failed to send, do it again
        while ((*(NIC_NSR + ifc_shift_offset)) & 0x1)
        {
            devmac_ctl(NIC_NCR_WRITE, NIC_CMD_TRANSMIT, 0);
        }
    }
void
startup(int argc, char *argv[])
{
	Dampkt		dam_p;
	char		tmp;
	char		*ep = &tmp;
	ulong		start = 0;
	long		sluimer;
	int		period = 0;
	int		tmpperiod, tmp_alpha, tmp_beta, tmp_ftable_alpha,
			tmp_eij, tmp_maxhops;


	strncpy(dam_myID, (char *)NIC_OUI, DAM_ID_LEN);
	hdlr_install();
	print("DAM node [%s] installed vector code.\n", dam_myID);

	devmac_ctl(NIC_NCR_WRITE, NIC_CMD_POWERUP, 0);
	print("DAM node [%s] powered up NIC.\n", dam_myID);

	dam_period = DAM_PROTOCOL_PERIOD;
	if (argc == 6)
	{
		tmpperiod = strtol(argv[0], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid DAM period supplied as argument.\n");
		}
		else
		{
			dam_period = tmpperiod;
			printf("Set dam_period to [%d] usecs\n", dam_period);
		}

		tmp_alpha = strtol(argv[1], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR alpha supplied as argument.\n");
		}
		
		tmp_beta = strtol(argv[2], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR beta supplied as argument.\n");
		}

		tmp_ftable_alpha = strtol(argv[3], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR ftable_alpha supplied as argument.\n");
		}

		tmp_eij = strtol(argv[4], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR eij supplied as argument.\n");
		}

		tmp_maxhops = strtol(argv[5], &ep, 0);
		if (*ep != '\0')
		{
			printf("Invalid EAR maxhops supplied as argument.\n");
		}

		Ear = ear_init(dam_myID, 0, tmp_alpha, tmp_beta, tmp_ftable_alpha,
				tmp_eij, tmp_maxhops,
			(uchar)devloc_getxloc(), (uchar)devloc_getyloc(), (uchar)devloc_getzloc());
		fprintf(stderr, "DAM node [%s] completed EAR init with runtime args:\n"
			"\talpha=%d\n\tbeta=%d\n\tftable_alpha=%d\n\teij=%d\n\tmaxhops=%d\n\n\n\n", 
			dam_myID, tmp_alpha, tmp_beta, tmp_ftable_alpha, tmp_eij, tmp_maxhops);

	}
	else
	{
		Ear = ear_init(dam_myID, 0, 1, 50, 1, 1 /* eij */, 8 /* maxhops */,
			(uchar)devloc_getxloc(), (uchar)devloc_getyloc(), (uchar)devloc_getzloc());
		print("DAM node [%s] completed EAR init using defaults.\n\n\n", dam_myID);
	}


	/*							*/
	/*	Implemented to mirror description in paper	*/
	/*	All variables beginning w/ dam_ correspond	*/
	/*	to variables in the paper's algorithm descr.	*/
	/*							*/
	while (1)
	{
		start = devrtc_getusecs();
		dam_p.timestamp = start;

		/*									*/
		/*	Write the log for the previous period. We want all actions to	*/
		/*	be in the timed loop, and though this log writing may seem to	*/
		/*	be not inherent to application, you can think of it as some	*/
		/*	post-peak detection actions that the algorithm must perform.	*/
		/*									*/
		if ((period > 0) && dam_participating)
		{
			if (strlen(dam_sink) > 0)
			{
				int		id;
				char		tmp;
				char		*ep = &tmp;

				id = strtol(dam_leaderID, &ep, 0);
				ear_response(Ear, dam_sink, NULL, 0, id);
			}
//print("\n\ndam_sink = [%s]\n\n\n", dam_sink);
		
			write_log(period - 1);
		}

		/*								*/
		/*	The values in this case are in Lux (see test.m)		*/
		/*	Noise floor is 0.1 Lux. DAM_THRESHOLD_ELECTION is	*/
		/*	thus set to 10 Lux.					*/
		/*								*/
		dam_myPr = devsignal_read(LIGHT_SENSOR);

		/*								*/
		/*	Algorithm description in PARC paper does not reset	*/
		/*	maxPrHeard. For each DAM period, until a packet is	*/
		/*	received, or our local reading is > threshold, the	*/
		/*	maxPrHeard should be 0.					*/
		/*								*/
		dam_maxPrHeard = 0;

		if (dam_myPr > DAM_THRESHOLD_ELECTION)
		{
LOGMARK(12);
			dam_participating = TRUE;
			dam_maxPrHeard = dam_myPr;
			strncpy(dam_leaderID, dam_myID, DAM_ID_LEN);
			dam_p.maxPr = dam_p.transPr = dam_myPr;
			strncpy((char *)dam_p.transID, dam_myID, DAM_ID_LEN);
			strncpy((char *)dam_p.maxID, dam_myID, DAM_ID_LEN);

			dam_broadcast(&dam_p);
LOGMARK(13);
		}
		else
		{
			dam_participating = FALSE;
		}

		sluimer = dam_period - (devrtc_getusecs() - start);
		sluimer = max(sluimer, 0);
LOGMARK(2);
		xusleep(sluimer);
LOGMARK(3);
		period++;
	}

	return;		
}
//BUG: we curretnly make the mac layer pass in the length of the destination address, so we can support multiple
//	mac layers with different address sizes.  Currently, the simulator maintains a 16+1 (NIC_ADDR_LEN) character
//	string format address.  When the MAC layer probes the NIC_OUI, it currently gets a string, which is
//	fine for now, but we neeed to clean this implementation up
int
devmac_xmit(uchar *dst, int dstaddrlen, uchar *header, uchar *data, int headerlen, int nbytes, int whichifc)
{
	int	max_framesize;
	int 	n = 0, i;
	uchar 	frame_cksum[2];
	ulong	ifc_shift_offset = (whichifc & 0xFFF) << 4;
	ulong	savedSR;


//print("in devmac_xmit, sending [%d] bytes to [%s], proto [%d], via ifc [%d]\n", nbytes, dst, proto, whichifc);

	/*	Save SR, then disable intrs:	*/
	savedSR = getpsr();
	splhi();


	/*					*/
	/*	The 32 bit cksum added by hw	*/
	/*	is not counted as part of fsz	*/
	/*					*/
	max_framesize = devmac_getmaxfsz(whichifc);

	/*	Save the dst for future use	*/
	for (i = 0; i < min(16, dstaddrlen); i++)
	{
		*(NIC_DST+ifc_shift_offset+i) = dst[i];
	}
	*(NIC_DST+ifc_shift_offset+i) = '\0';

	/*	Write MAC layer header	    	*/
	for (i = 0; i < headerlen && (n < max_framesize); i++)
	{
		*(NIC_TDR+ifc_shift_offset) = header[i];
		n++;
	}

	/*		Write data	    	*/
	for (i = 0; (i < nbytes) && (n < max_framesize); i++)
	{
		*(NIC_TDR+ifc_shift_offset) = data[i];
		n++;
	}

	/*		Write checksum	    	*/
	devmac_cksum(header, data, headerlen, nbytes, &frame_cksum[0]);
	for (i = 0; i < 2; i++)
	{
		*(NIC_TDR+ifc_shift_offset) = frame_cksum[i];
		n++;
	}


	/*							*/
	/*	The current frame size is calculated from 	*/
	/*	number of bytes in the TDR at this point	*/
	/*							*/
	devmac_ctl(NIC_NCR_WRITE, NIC_CMD_TRANSMIT, whichifc);


	/*	Restore SR	*/
	spldone(savedSR);


	/*					*/
	/*    If frame could'nt be sent, LSB	*/
	/*    of NIC_NSR will be 1, return -1	*/
	/*					*/
	return -((*(NIC_NSR+ifc_shift_offset)) & 0x1);
}