henesyinit from lsubb0c8bdb6 years agoHistory
748 lines · 11.8 KB · C
RawDownload
#include <u.h>#include <libc.h>#include <b.h>#include <thread.h>#include <fcall.h>#include <auth.h>#include <9p.h>#include <bio.h> typedef struct Pos Pos;typedef struct Loc Loc;typedef struct Badge Badge;typedef struct Name Name; enum {	Qdir,	Qbadge, 	Ntoks	= 32,	Interval= 2,	// poll interval, in seconds.	Nlocs	= 3,	// # of locations kept per badge	Nbadges	= 16,	// Max # of badges	Nnames	= 128,	// Max # of names (badges+locations)	Troam	= 5,	// time (secs) to forget old location	Told	= 120,	// time (secs) to forget last location	Tdial	= 15,	// time (secs) to retry a dial for srv.	Stack	= 16 * 1024, 	Esc	= 27, }; struct Pos {	int	badge;	int	loc;}; struct Loc {	int	loc;	int	time;}; struct Badge {	int	id;	Loc	locs[Nlocs];}; struct Name {	int	id;	char*	name;}; #define dprint	if(debug)fprintstatic int	debug;static char*	ctladdr;static Channel*	posc;static Channel*	fsc;static Badge	badges[Nbadges];static Name*	bnames[Nnames];static int	nbnames;static Name*	mnames[Nnames];static int	nmnames;static File*	slash;static int	iofd = -1;static char*	addr;static char*	vname; int mainstacksize = 32*1024; intfileclass(File* fp){	return ((fp->qid.path&0xff000000)>>24);} /* A badge may be seen by several receivers, and * may be at several locations. It may be unnoticed * for a while, so it takes a while to assume that * its last location is no longer valid. * We keep the last Nlocs locations for each badge. * We forget those older than Told secs. * * Badges and locations are added into the data * structure and never removed. Files for badges keep * a pointer into the corresponding badge. Their content * appears to be the set of active locations (most recent first).. */ static voidaddname(int id, char* s){	Name*	n; 	n = emalloc9p(sizeof(Name));	n->id = id;	n->name= s; 	dprint(2, "addname: %d %s\n", id, s);	if (id < 1000){		if (nbnames >= nelem(bnames))			sysfatal("too many names");		bnames[nbnames++] = n;	} else {		if (nmnames >= nelem(mnames))			sysfatal("too many names");		mnames[nmnames++] = n;	}}  static char*id2name(int id){	int	i;	Name**	n;	int	nn; 	if (id < 1000){		n = bnames;		nn= nbnames;	} else {		n = mnames;		nn= nmnames;	}	for (i = 0; i < nn; i++)		if (n[i]->id == id)			return n[i]->name;	return nil;} static voiddumpnames(void){	int	i; 	fprint(2, "badges:\n");	for (i = 0; i < nbnames; i++)		fprint(2, "0x%x -> %s\n", bnames[i]->id, bnames[i]->name);	fprint(2, "monitors:\n");	for (i = 0; i < nmnames; i++)		fprint(2, "0x%x -> %s\n", mnames[i]->id, mnames[i]->name);} static voiddumpbadges(void){	int	i, j;	char*	n; 	fprint(2, "locations:\n");	for (i = 0; i < nelem(badges); i++)		if (badges[i].id){			n = id2name(badges[i].id);			if (n == nil)				continue;			fprint(2,"\t %s:\t", n);			for (j = 0; j < Nlocs; j++){				if (badges[i].locs[j].time){					n = id2name(badges[i].locs[j].loc);					if (n)						fprint(2, " %s", n);					else						fprint(2, " unknown");				}			}			fprint(2,"\n");		}	fprint(2,"\n");} static voidfsread(Req* r){	File* file;	int	id;	Badge*	b;	char	buf[80];	char*	s;	int	i;	char*	n; 	if (r->fid->qid.type&QTAUTH){		authread(r);		return;	}	file = r->fid->file;	id = fileclass(file);	switch(id){	case Qbadge:		rlock(file);		b = file->aux;		assert(b);		s = buf;		for (i = 0; i < Nlocs; i++){			if (b->locs[i].loc){				n = id2name(b->locs[i].loc);				if (n)					s = seprint(s, buf+80, "%s\n", n);				else					s = seprint(s, buf+80, "unknown\n");			}		}		if (s == buf)			strcpy(buf, "none\n");		runlock(file);		readstr(r, buf);		respond(r, nil);		break;	default:		respond(r, "bug: bad id in fspread");	}} static voidfsattach(Req* r){	if (r->srv->auth != nil && authattach(r) < 0)		return;	respond(r, nil);} static voidfsopen(Req* r){	int	mode; 	if (r->fid->qid.type&QTAUTH)		sysfatal("fsopen for an AUTH file. fixme: add respond/return\n");	mode = (r->ifcall.mode&3);	if (mode != OREAD)		respond(r, "permission denied");	else		respond(r, nil);} static voidfswrite(Req* r){	if (r->fid->qid.type&QTAUTH)		authwrite(r);	else		respond(r, "permission denied");} static voidfsclean(Srv* s){	Channel* rc; 	rc = s->aux;	if (rc)		chanfree(rc);}	static voidfssend(Req* r){	Channel* rc; 		if (r->srv->aux == nil)		r->srv->aux = chancreate(sizeof(ulong), 0);	rc = r->srv->aux;	sendp(fsc, r);	recvul(rc);} static voidfsclunk(Fid* fid){ 	if (fid->qid.type&QTAUTH)		authdestroy(fid);} static Srv sfs={	.auth	=auth9p,	.attach	=fsattach,	.open	=fsopen,	.write = fswrite,	.read  = fssend,	.end	= fsclean,	.destroyfid = 	fsclunk,}; static intloccmp(void* l1, void* l2){	Loc* l1p = l1;	Loc* l2p = l2; 	return l2p->time - l1p->time;} static voidsortlocs(Badge* b){	qsort(b->locs, Nlocs, sizeof(b->locs[0]), loccmp);} static voidcollectold(void){	int	i, j;	long	now;	long	dt; 	now = time(nil);	for (i = 0; i < nelem(badges); i++)		if (badges[i].id){			for (j = 0; j < Nlocs; j++){				if (badges[i].locs[j].time){					dt = now - badges[i].locs[j].time;					if (j > 0 && dt > Troam){						badges[i].locs[j].time = 0;						badges[i].locs[j].loc = 0;					}					if (j == 0 && dt > Told){						badges[i].locs[j].time = 0;						badges[i].locs[j].loc = 0;					}				}			}			sortlocs(badges+i);		}} static voidupdatebadge(Badge* b, int loc){	int	old;	int	i;	long	now; 	now = time(nil);	for (old = i = 0; i < Nlocs; i++){		if (b->locs[i].loc == loc){			old = i;			break;		}		if (b->locs[old].time > b->locs[i].time)			old = i;	}	b->locs[old].time = now;	b->locs[old].loc = loc;	sortlocs(b);} static voidaddfile(Badge* b){	File*	f;	char*	n; 	n = id2name(b->id);	if (n){		f = createfile(slash, n, "sys", 0444, b);		f->qid.path |= ((Qbadge&0xff)<<24);		closefile(f);	}} static voidupdatepos(Pos* p){	int	i; 	for (i = 0; i < nelem(badges); i++){		if (badges[i].id == p->badge){			updatebadge(badges+i, p->loc);			return;		}	}	for (i = 0; i < nelem(badges); i++){		if (!badges[i].id){			badges[i].id = p->badge;			updatebadge(badges+i, p->loc);			addfile(badges+i);			return;		}	}	fprint(2, "%s: too many badges; ignoring %x\n", argv0, p->badge);} static voidparsepos(char* s){	char	mname[] = "0xXXXX";	char	bname[] = "0xXXXX";	char*	p;	Pos	pos; 	p = strchr(s, ' ');	if (p == nil || p - s != 4){		dprint(2, "bad monitor name: %s\n", s);		return;	}	*p++ = 0;	strcpy(mname + 2, s);	pos.loc = strtol(mname, nil, 0);	while(p && *p){		s = p;		p = strchr(s, ' ');		if (p == nil)			break;		*p = 0;		if (p - s < 6){			dprint(2, "bad badge name: %s\n", s);			break;		}		p++;		strncpy(bname + 2, s, 4);		pos.badge = strtol(bname, nil, 0);		dprint(2, "badge %d at %d\n", pos.badge, pos.loc);		send(posc, &pos);	}} static inthxcmd(int fd, char cmd, char ans){	char	buf[1];	int	i, n; 	i = 0;	do {		if (write(fd, &cmd, 1) != 1)			return 0;		if (ans == 0)			break;		n = read(fd, buf, sizeof(buf));		if (n <= 0)			return 0;		if (buf[0] != ans){			if(0)dprint(2, "%s: cmd %c: bad answer '%c' 0x%x\n",				argv0, cmd, buf[0], buf[0]);			continue;		}	} while (buf[0] != ans && i++ < 100);	if (i == 100){		werrstr("HX5C not responding");		return 0;	}	return 1;} static voidhxinit(int fd){	hxcmd(fd, Esc, 0);} static uchar*PUTS(uchar* p, int s, int* sum){	p[0] = ((s >> 8) & 0xff);	*sum = ((*sum + p[0]) & 0xff);	p[1] = (s & 0xff);	*sum = ((*sum + p[1]) & 0xff);	return p + 2;} static uchar*PUTB(uchar* p, int s, int* sum){	p[0] = (s & 0xff);	*sum = ((*sum + p[0]) % 256);	return p + 1;} static voiddumpbuf(uchar* buf, int n){	int	i; 	for (i = 0; i < n; i++)		print("%c ", buf[i]);	print("\n");} static inthxconfig(int fd){	uchar	buf[4096];	uchar*	p;	int	sum;	int	i;	char	cmd; 	cmd = '$';	hxcmd(fd, cmd, '!');	sum = 0;	p = buf;	p = PUTS(p, 2*nmnames + 1, &sum);	// size	p = PUTB(p, Interval, &sum);	for (i = 0; i < nmnames; i++)		p = PUTS(p, mnames[i]->id, &sum);	p = PUTB(p, sum, &sum); 

Lines 1–500 of 748.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t