forsyth20121205-174063a80cc13 years agoHistory
2557 lines · 56.9 KB · Brainfuck
RawDownload
implement Camera; include "sys.m";	sys : Sys;include "daytime.m";	daytime: Daytime;include "styx.m";	styx: Styx;	Rmsg, Tmsg: import styx;include "styxservers.m";	styxservers: Styxservers;	Fid, Navigator, Navop: import styxservers;	Styxserver, Eexists, Eperm, Ebadfid, Enotdir, Enotfound, Ebadarg: import styxservers;	nametree: Nametree;	Tree: import nametree;include "string.m";	str : String;include "draw.m";include "arg.m"; Camera : module {	init : fn (nil : ref Draw->Context, argv : list of string);}; cdp_get_product_info: 			con 16r01;cdp_get_image_specifications:	con 16r02;cdp_get_camera_status:			con 16r03;cdp_set_product_info:			con 16r05;cdp_get_camera_capabilities:		con 16r10;cdp_get_camera_state:			con 16r11;cdp_set_camera_state:			con 16r12;cdp_get_camera_defaults:		con 16r13;cdp_set_camera_defaults:		con 16r14;cdp_restore_camera_states:		con 16r15;cdp_get_scene_analysis:			con 16r18;cdp_get_power_mode:			con 16r19;cdp_set_power_mode:			con 16r1a;cdp_get_s1_mode:				con 16r1d;cdp_set_s1_mode:				con 16r1e;cdp_start_capture:				con 16r30;cdp_get_file_list:				con 16r40;cdp_get_new_file_list:			con 16r41;cdp_get_file_data:				con 16r42;cdp_erase_file:					con 16r43;cdp_get_storage_status:			con 16r44;cdp_set_file_data:				con 16r47;cdp_get_file_tag:				con 16r48;cdp_set_user_file_tag:			con 16r49;cdp_get_clock:					con 16r70;cdp_set_clock:					con 16r71;cdp_get_error:					con 16r78;cdp_get_interface_timeout:		con 16r90;cdp_set_interface_timeout:		con 16r91; cdp_header_len:				con 12; T_DIR: con 0;T_CTL: con 1;T_ABILITIES: con 2;T_TIME: con 3;T_JPGDIR: con 4;T_JPG: con 5;T_STORAGE: con 6;T_POWER: con 7;T_THUMB: con 8;T_THUMBDIR: con 9;T_STATE: con 10;T_INTERFACE: con 11; MAXFILESIZE : con 5000000;TIMEOUT : con 4000; nextjpgqid, nexttmbqid, dirqid, Qctl, Qabl, Qstore: int;Qstate, Qtime, Qjpgdir, Qpwr, Qthumbdir, Qinterface : int; error_table := array [] of {	"No Error",	"Unimplemented",	"Unsupported Version",	"Application Timeout",	"Internal Error",	"Parameter Error",	"File System Null",	"File Not Found",	"Data Section Not Found",	"Invalid File Type",	"Unknown Drive",	"Drive Not Mounted",	"System Busy",	"Battery Low",}; bintro := array [] of {	byte 16ra5,	byte 16r5a,	byte 16r00,	byte 16rc8,	byte 16r00,	byte 16r02,	byte 16rc9,}; bak := array [] of {	byte 16r5a,	# 2 byte header	byte 16ra5,	byte 16r55,	# I/F Type	byte 16r00,	# Comm Flag	byte 16r00,	byte 16r00,	byte 16r00,	byte 16r00,	byte 16r00,	byte 16r00,	byte 16r00,	byte 16r00,	byte 16r00,}; pwl := array[] of {	byte 0,	byte 0,}; pak := array [] of {	byte 0,	byte 0,}; SERIAL, USB, IRDA: con (1<<iota);BEACON, BEACONRESULT: con (1<<iota); Camera_adt: adt {	port_type: 	int;	port_num:	int;	command:	int;	mode: 		int;	fd:			ref Sys->FD;	ctlfd:			ref Sys->FD;	cdp:			array of byte;	bufbytes:		int;	baud:		int;	dfs, hfs:		int;		# device and host frame sizes	stat:			int;	# eia status file}; statopt := array[] of {	"status",	"stat",}; DL_QUANTA: con 20000; TOUT: con -1729; Partialtag: adt {	offset, length, filesize: int;}; Cfile: adt {	driveno: int;	pathname: array of byte;	dosname: array of byte;	filelength: int;	filestatus: int;	thumblength: int;	thumbqid: int;}; Fitem : adt {	qid: Sys->Qid;	cf: Cfile;}; C: Camera_adt; filelist: array of Fitem;reslength: int;currentstate := "";wait : int;usecache := 0;connected : int;recon := 0;verbosity := 4;interfacepath := "";interfacepaths : array of string;camname := "";gpid : int; init(nil : ref Draw->Context, argv : list of string){	err: string;	sys = load Sys Sys->PATH;	gpid = sys->pctl(Sys->NEWPGRP, nil); 	str = load String String->PATH;	daytime = load Daytime Daytime->PATH; 	styx = load Styx Styx->PATH;	styx->init();	styxservers = load Styxservers Styxservers->PATH;	styxservers->init(styx);	nametree = load Nametree Nametree->PATH;	nametree->init();	arg := load Arg Arg->PATH; 	filelist = array[200] of Fitem;	C.port_num = 0;			# XXXXX from argv	C.port_type = SERIAL;		# Serial only for now	C.baud = 115200;	C.dfs = C.hfs = 1023;	C.cdp = array [DL_QUANTA] of byte;	C.mode = BEACON; 	ex.pnum = -1;	ex.offset = -1;	cachelist = nil; 	nextjpgqid = getqid(1, T_JPG);	nexttmbqid = getqid(1, T_THUMB);	dirqid = getqid(1,T_JPGDIR);	Qctl = getqid(Qroot,T_CTL);	Qabl = getqid(Qroot,T_ABILITIES);	Qstore = getqid(Qroot,T_STORAGE);	Qtime = getqid(Qroot,T_TIME);	Qstate = getqid(Qroot,T_STATE);	Qpwr = getqid(Qroot,T_POWER);		Qjpgdir = getqid(Qroot,T_JPGDIR);	Qthumbdir = getqid(Qroot,T_THUMBDIR);	Qinterface = getqid(Qroot,T_INTERFACE);		camname = "Camera";	extrafilelist: list of string = nil;	arg->init(argv);	arg->setusage("camera [-b baud] [-F framesize] [-f extrafiles] [-p port] [-n name] [-v verbosity]");	while ((opt := arg->opt()) != 0) {		case opt {		'n' =>			camname = arg->earg();		'v' =>			verbosity = int arg->earg();		'F' =>			C.dfs = C.hfs = int arg->earg();		'b' =>			C.baud = int arg->earg();		'p' =>			C.port_num = int arg->earg();		'c' =>			usecache = 1;		'f' =>			extrafilelist = arg->earg() :: extrafilelist;		* =>			arg->usage();		}	}	arg = nil;	interfacepaths = array[len extrafilelist] of string;	# sys->print("INTERFACEPATHS: %d\n", len extrafilelist);	for (i := 0; i < len interfacepaths; i++) {		interfacepaths[i] = hd extrafilelist;		# sys->print("INTERFACEPATH %d: %s\n", i, hd extrafilelist);		extrafilelist = tl extrafilelist;	}		print(sys->sprint("Trying to connect to eia%d...\n",C.port_num),2);	case C.port_type {		SERIAL =>			# open port and return fd			(C.fd, C.ctlfd, err) = serialport(C.port_num);			if (C.fd == nil) {				print("Could not open serial port\n",1);				exit;			}		USB =>			;		IRDA =>			;		* =>			;	}	if (connect() != 0) {;		print("Connection failed\n",1);		exit;	}	recon = 0;	print("Connected!\n",2);	set_interface_timeout();	set_camera_properties();	get_file_list();	connected = 1;	ignoreabls = nil;	get_camera_capabilities();	sync := chan of int;	spawn serveloop(sys->fildes(0), sync);	<-sync;} set_camera_properties(){	for (i := 0; i < len set_camera_props; i++)		set_camera_state(set_camera_props[i].t0,set_camera_props[i].t1);} set_camera_props := array[] of {	("mcap", 0),	("acpd", 65535),	("actc", 65535),	("btpd", 65535),	("bttc", 65535),	("flty", 1246774599),	("ssvl", 0),}; argval(argv: list of string, arg: string): string{	if (arg == "") return "";	if (arg[0] != '-') arg = "-" + arg;	while (argv != nil) {		if (hd argv == arg && tl argv != nil && (hd tl argv)[0] != '-')			return tonext(tl argv);		argv = tl argv;	}	return "";} tonext(los: list of string): string{	s := "";	while (los != nil) {		if ((hd los)[0] != '-') s += " " + hd los;		else break;		los = tl los;	}	if (s != "") s = s[1:];	return s;} int2hex(i:int): int{	i2 := 0;	s := string i;	for (k := 0; k < len s; k++)		i2 = (i2 * 16) + int s[k:k+1];	return i2;} connect(): int{		connected = 0;	datain := chan of array of byte;	pchan := chan of int;	tick := chan of int;	reset(C.ctlfd); 	spawn timer2(tick,TIMEOUT * 2);	tpid := <-tick; 	spawn beacon_intro(datain, pchan, C.fd);	pid := <- pchan;	# beacon phase	Beacon: for (;;) {		alt {			buf := <- datain =>				# got some data				case C.mode {					BEACON =>						if (beacon_ok(buf)) {							print("Got beacon\n",3);							beacon_ack(C);							spawn beacon_result(datain, pchan, C.fd);							pid = <-pchan;							C.mode = BEACONRESULT;							break;						}						else {							print("resetting\n",3);							reset(C.ctlfd);						}					BEACONRESULT =>						kill(tpid); 						print("Checking beacon result\n",3);						if (beacon_comp(buf, C) == 0) {							return 0;							break Beacon;						}						return -1;				}			<- tick =>				kill(pid);				return -1;		# failure		}	}} CTL, ABILITIES, DATA, JPG, PIC, TIME, CONV: con iota;NAME, FSIZE, PHOTO, THUMB: con iota; Qdir : con iota; contains(s: string, test: string): int{	num :=0;	if (len test > len s) return 0;	for (i := 0; i < (1 + (len s) - (len test)); i++) {		if (test == s[i:i+len test]) num++;	}	return num;} abilitiesfilter := array[] of {	"Time Format",	"Date Format",	"File Type",	"Video",	"Media",	"Sound",	"Volume",	"Reset Camera",	"Slide",	"Timelapse",	"Burst",	"Power",	"Sleep",}; ignoreabls : list of string; defattr : list of (string, int);defaultattr, currentattr: array of (string, int); filterabls(pname, desc: string): int{	for (i := 0; i < len abilitiesfilter; i++) {		if (contains(desc, abilitiesfilter[i])) {			ignoreabls = pname :: ignoreabls;			return 1;		}	}	return 0;} mountit(dfd, mountfd: ref sys->FD, sync: chan of int){	sys->pctl(sys->NEWNS | sys->NEWFD, 2 :: dfd.fd :: mountfd.fd :: nil);	sync <-= 1;	mountfd = sys->fildes(mountfd.fd);	dfd = sys->fildes(dfd.fd);	if (sys->mount(mountfd, nil, "/", sys->MREPL | sys->MCREATE, nil) == -1) {		sys->fprint(sys->fildes(2), "cannot mount\n");		spawn exporterror(dfd, sys->sprint("%r"));	} else {		sync = chan of int;		spawn exportpath(sync, dfd);		<-sync;	}} exporterror(dfd: ref Sys->FD, error: string){	tmsg := Tmsg.read(dfd, 0);	if (tmsg == nil) {		sys->fprint(sys->fildes(2), "exporterror() EOF\n");		exit;	}	pick t := tmsg {	Readerror =>		sys->fprint(sys->fildes(2), "exporterror() Readerror\n");	* =>		reply: ref Rmsg = ref Rmsg.Error(tmsg.tag, error);		data := reply.pack();		sys->write(dfd, data, len data);	}} exportpath(sync: chan of int, dfd: ref sys->FD){	sync <-= 1;	sys->export(dfd, "/", Sys->EXPWAIT);} Qroot : con int iota; ss : ref Styxserver;uid: string; exitfid := -1; getuid(){	buf := array [100] of byte;	fd := sys->open("/dev/user", Sys->OREAD);	uidlen := sys->read(fd, buf, len buf);	uid = string buf[0: uidlen];} dir(name: string, perm: int, length: int, qid: int): Sys->Dir{	d := sys->zerodir;	d.name = name;	d.uid = uid;

Lines 1–500 of 2557.

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