コード例 #1
0
ファイル: network.c プロジェクト: Robotips/rtprog
void network_init()
{
    #ifdef USE_esp8266
        esp8266_init();
        web_server_init();
    #endif
}
コード例 #2
0
ファイル: demo.c プロジェクト: 96Levels/roadsend-php
int main() {
	int pid;
	int ret;
	struct web_server server;
	while(!web_server_init(&server,PORT,"demo.log",0)) {
		PORT++;	
	};
	printf("http://localhost:%d\n",PORT);	
	web_server_addhandler(&server,"* /",index_html,0);
	web_server_addhandler(&server,"* /log",logfile,0);
	web_server_run(&server); 
	return 0;

};
コード例 #3
0
void s2e_webserver_init(chanend c_xtcp,
                        chanend c_flash,
                        chanend c_uart_config,
                        chanend c_flash_data)
{
    web_server_init(c_xtcp, c_flash, NULL);
    // Store off these channels to be used by the functions called whilst
    // rendering web pages
    app_state.c_uart_config = c_uart_config;
    app_state.c_flash = c_flash_data;
    app_state.c_xtcp = c_xtcp;

    web_server_set_app_state((int) &app_state);
}
コード例 #4
0
ファイル: main.c プロジェクト: jks-prv/5370_proc
int main(int argc, char *argv[])
{
	int i, n;
	
	bool wasRunning = FALSE;
	bool save_cfg = FALSE;
	bool change_settings_ui(menu_e *menu, u1_t key, bool *skip_first, cfg_t *cfg);
	bool show_ip = FALSE;
	bool config_valid, config_key = FALSE;
	bool config_ip = FALSE, config_nm = FALSE, config_gw = FALSE, config_am = FALSE;
	u4_t key;
	menu_e menu;
	int addr_mode;
	int ip[4], nm[4], gw[4], bc[4];
	
	FILE *cfp, *efp;
	char *config_file = ROOT_DIR "/.5370.config";

	cfg_t *cfg = &cfg_buf;
	
	dsp_7seg_init(FALSE);	// panic routine can't use display until bus is setup

	// silently ignores unrecognized arguments
	for (i=1; i<argc; i++) {
		if (strcmp(argv[i], "-bg") == 0) background_mode = TRUE;
		if (strcmp(argv[i], "-ip") == 0) show_ip = TRUE;
		if (strcmp(argv[i], "-no") == 0) menu_action = FALSE;

		if (strcmp(argv[i], "?")==0 || strcmp(argv[i], "-?")==0 || strcmp(argv[i], "--?")==0 || strcmp(argv[i], "-h")==0 ||
			strcmp(argv[i], "h")==0 || strcmp(argv[i], "-help")==0 || strcmp(argv[i], "--h")==0 || strcmp(argv[i], "--help")==0) {
			printf( "-rcl|-recall [name]    load key settings from named profile\n"
					"-hpib-hard    use the original HPIB hardware interface, assuming installed\n"
					"-hpib-sim     simulate the HPIB interface in software (debug mode)\n"
					"-hpib-net     simulate and re-direct transfers over an Ethernet connection\n"
					"-ip           show IP address of Ethernet interface and exit\n"
			);
			xit(0);
		}
	}
	
	lprintf("HP%s v%d.%d\n", INST_STR, FIRMWARE_VER_MAJ, FIRMWARE_VER_MIN);
    lprintf("compiled: %s %s\n", __DATE__, __TIME__);

	sim_args(TRUE, argc, argv);
	hpib_args(TRUE, argc, argv);

	sim_init();
	web_server_init();
	
	if (!menu_action) printf("menu action disabled\n");

reset:
	// To support the action of the 'reset' key most code files have a reset routine that zeros static variables.
	// This is similar to the C runtime idea of zeroing the bss when a program is first run.
	
	if (wasRunning) {
		wasRunning = FALSE;
		net_reset(NET_HPIB);
		net_reset(NET_TELNET);
		web_server_stop();
		skip_first = save_cfg = config_key = config_ip = config_nm = config_gw = config_am = FALSE;
	}

	sim_reset();

	if (!(bus_read(RREG_LDACSR) & DSR_VOK)) {
		lprintf("waiting for 5370 power\n");
		usleep(1000000);
		while (!(bus_read(RREG_LDACSR) & DSR_VOK)) {
			sched_yield();
			usleep(250000);
		}
		lprintf("5370 power on\n");
		usleep(1000000);
	} else {
		lprintf("5370 is powered on\n");
	}
	
	// display firmware version
	dsp_7seg_init(TRUE);
	dsp_7seg_str(DSP_LEFT, INST_STR, DSP_CLEAR);
	dsp_7seg_chr(POS(10), 'v');
	dsp_7seg_num(POS(11), POS_IS_LSD, FIRMWARE_VER_MAJ, DEFAULT_WIDTH, SPACE_FILL);
	dsp_7seg_num(POS(12), POS_IS_MSD, FIRMWARE_VER_MIN, FIELD_WIDTH(0), ZERO_FILL);
	dsp_7seg_dp(POS(12));
	dsp_leds_clr_all();
	delay(2000);

	if ((cfp = fopen(config_file, "r")) == NULL) {
		if (errno != ENOENT) sys_panic(config_file);
		config_valid = FALSE;
	} else {
		while (fgets(lbuf, LBUF, cfp)) {
			if ((sscanf(lbuf, "key 0x%x", &key) == 1) && (key == 0xcafe5370)) config_key = TRUE; else
			if (sscanf(lbuf, "am %d", &addr_mode) == 1) config_am = TRUE; else
			if (sscanf(lbuf, "ip %d.%d.%d.%d", &ip[0], &ip[1], &ip[2], &ip[3]) == 4) config_ip = TRUE; else
			if (sscanf(lbuf, "nm %d.%d.%d.%d", &nm[0], &nm[1], &nm[2], &nm[3]) == 4) config_nm = TRUE; else
			if (sscanf(lbuf, "gw %d.%d.%d.%d", &gw[0], &gw[1], &gw[2], &gw[3]) == 4) config_gw = TRUE; else
				;
		}
		assert((addr_mode == 0) || (addr_mode == 1));
		menu = cfg->menu = (addr_mode == 0)? M_DHCP : M_IP;
		
		if (config_key && config_ip && config_nm && config_gw && config_am) {
			printf("valid config file %s\n", config_file);
			config_valid = TRUE;

			if (menu == M_IP) {
				printf("setting interface address\n");
				sprintf(lbuf, "ifconfig eth0 %d.%d.%d.%d netmask %d.%d.%d.%d",
					ip[0], ip[1], ip[2], ip[3], nm[0], nm[1], nm[2], nm[3]);
				if (menu_action) system(lbuf);
				sprintf(lbuf, "route add default %d.%d.%d.%d", gw[0], gw[1], gw[2], gw[3]);
				if (menu_action) system(lbuf);
			}
		} else {
			printf("invalid config file %s\n", config_file);
			config_valid = FALSE;
		}
		fclose(cfp);
	}

	if (!config_valid) {
		menu = cfg->menu = M_DHCP;		// try DHCP first if not valid config
	}

#define ENET_RETRY 20

	if (menu == M_DHCP) {	// see if interface configured by DHCP
		gw[3]=gw[2]=gw[1]=gw[0]=0;	// ifconfig doesn't tell us the gateway, only the broadcast which we don't care about
		
		// sometimes the link is slow to come up, so retry a few times
		for (i=0; i<ENET_RETRY; i++) {
			if ((efp = popen("ifconfig eth0", "r")) == NULL) sys_panic("ifconfig eth0");
			char *lp = lbuf;
		
			n=0;
			while (fgets(lp, LBUF, efp)) {
				if ((n = sscanf(lp, "%*[ ]inet addr:%d.%d.%d.%d Bcast:%d.%d.%d.%d Mask:%d.%d.%d.%d",
					&ip[0], &ip[1], &ip[2], &ip[3],
					&bc[0], &bc[1], &bc[2], &bc[3],
					&nm[0], &nm[1], &nm[2], &nm[3])) == 12)
					break;
			}
			pclose(efp);
			if (n == 12) break;
			delay(1000);
		}
	} else {
		i=0;	// interface configured manually above
	}

	if (i != ENET_RETRY) {
		for (i=0; i<4; i++) {
			cfg->ip[i] = ip[i]; cfg->nm[i] = nm[i]; cfg->gw[i] = gw[i];
		}

		if (menu == M_DHCP) lprintf("via DHCP ");
		lprintf("eth0: ip %d.%d.%d.%d mask %d.%d.%d.%d ",
			ip[0], ip[1], ip[2], ip[3], nm[0], nm[1], nm[2], nm[3]);
		if (menu != M_DHCP) lprintf("gw %d.%d.%d.%d", gw[0], gw[1], gw[2], gw[3]);
		lprintf("\n");
		dsp_7seg_str(DSP_LEFT, "ip", DSP_CLEAR);
		display_ipaddr(cfg->ip);
	} else {
		lprintf("eth0: not configured from DHCP?");
		dsp_7seg_str(DSP_LEFT, "no dhcp?", DSP_CLEAR);
	}
	
	if (!config_valid && (i == ENET_RETRY)) {		// configuration not valid, DHCP failed, so set some defaults
		menu = cfg->menu = M_IP;
		bcopy(default_ipinfo, cfg->if_ipinfo, sizeof(default_ipinfo));
		save_cfg = TRUE;
	}

	if (show_ip) xit(0);
	delay(2000);	// show ip on display for a moment before continuing

	net_connect(NET_HPIB, SERVER, NULL, HPIB_TCP_PORT);
	net_connect(NET_TELNET, SERVER, NULL, TELNET_TCP_PORT);
	web_server_start();

	// place a call here to setup your measurement extension code
	meas_extend_example_init();

	// reset key held down during a reboot -- drop into menu mode
	preempt_reset_key(TRUE);

	// while in the boot routine the reset key either starts the app or saves the changed config
	app_state = S_MENU;

	while (1) {
		u1_t key;

		sim_input();	// for remote debugging of menu mode
		key = handler_dev_display_read(RREG_KEY_SCAN);	// called instead of bus_read() so simulated keys will be returned

		switch(app_state) {
	
		case S_MENU:
			if (key != KEY(RESET)) {
				app_state = S_START;
				break;
			}

			dsp_7seg_str(DSP_LEFT, "ready", DSP_CLEAR);
			printf("ready\n");
			dsp_led_set(RESET);
			wait_key_release();
			dsp_led_clr(RESET);
			dsp_7seg_str(DSP_LEFT, "chg settings", DSP_CLEAR);
			printf("menu mode\n");
			skip_first = TRUE;
			menu = M_HALT;		// first menu item displayed
			
			// light up the keys valid during menu mode
			for (i=0; settings_keys[i].key; i++) {
				dsp_led_set(settings_keys[i].key);
			}

			app_state = S_MENU_POLL;
			break;

		case S_MENU_POLL:
			if (key == KEY(RESET)) {
				dsp_led_set(RESET);
				wait_key_release();
				dsp_led_clr(RESET);
				app_state = S_MENU_DONE;
				break;
			}

			if (change_settings_ui(&menu, key, &skip_first, cfg)) save_cfg = TRUE;
			break;

		case S_MENU_DONE:
			if (!skip_first && (menu == M_HALT)) {
			
					// Debian takes a while to halt, but nicely clears the GPIOs so the
					// display goes blank right when halted.
					// Angstrom with Gnome disabled halts very fast, but doesn't
					// clear the GPIOs like Debian. So we get the PRU to poll the LEDs
					// until they go off, then blank the display.
					dsp_7seg_str(DSP_LEFT, " halting...", DSP_CLEAR);
					printf("halting...\n");
					
					#ifdef DIST_DEBIAN
						if (menu_action) system("halt");
						exit(0);
					#endif

					#ifdef DIST_ANGSTROM
						dsp_7seg_chr(POS(0), ' ');		 // preload address & data
						send_pru_cmd(PRU_HALT);
						if (menu_action) system("halt");
						exit(0);
					#endif
			} else
			if (menu == M_CANCEL || (skip_first && (menu == M_HALT))) {
				app_state = S_START;
				break;
			} else {
				if (menu != M_DHCP) menu = M_IP;
				if (menu != cfg->menu) save_cfg = TRUE;
			}

			if (save_cfg) {
				dsp_7seg_str(DSP_LEFT, "config changed", DSP_CLEAR);
				delay(2000);
				cfg->menu = menu;
			
				if (menu == M_DHCP) {
					dsp_7seg_str(DSP_LEFT, "using dhcp mode", DSP_CLEAR);
				} else {
					dsp_7seg_str(DSP_LEFT, "using ip mode", DSP_CLEAR);
				}

				delay(2000);
				dsp_7seg_str(DSP_LEFT, "saving config", DSP_CLEAR);
				if ((cfp = fopen(config_file, "w")) == NULL) sys_panic(config_file);
				printf("writing config file %s\n", config_file);
				fprintf(cfp, "key 0xcafe5370\n");
				fprintf(cfp, "am %d\n", (cfg->menu == M_DHCP)? 0:1);
				fprintf(cfp, "ip %d.%d.%d.%d\n", cfg->ip[0], cfg->ip[1], cfg->ip[2], cfg->ip[3]);
				fprintf(cfp, "nm %d.%d.%d.%d\n", cfg->nm[0], cfg->nm[1], cfg->nm[2], cfg->nm[3]);
				fprintf(cfp, "gw %d.%d.%d.%d\n", cfg->gw[0], cfg->gw[1], cfg->gw[2], cfg->gw[3]);
				fclose(cfp);

				delay(2000);
			}
			
			app_state = S_START;
			break;
		
		case S_START:
			if (wasRunning) {		// if previous sim was interrupted must reset before starting new one
				goto reset;
			}
			
			preempt_reset_key(FALSE);
			sim_main();
			preempt_reset_key(TRUE);
			handler_dev_display_read(RREG_KEY_SCAN);	// flush extra sim reset key, if any
			delay(1000);
			
			// this sim was interrupted, so can't restart a new sim without doing a reset first
			wasRunning = TRUE;

			// if key still down after one second delay enter menu mode, else treat as simple reset
			if (handler_dev_display_read(RREG_KEY_SCAN) == KEY(RESET)) {
				app_state = S_MENU;
			} else {
				goto reset;
			}
			break;
		}
	}

	return 0;
}
コード例 #5
0
ファイル: main.cpp プロジェクト: fourks/Beagle_SDR_GPS
int main(int argc, char *argv[])
{
	u2_t *up;
	int i;
	char s[32];
	
	// enable generation of core file in /tmp
	scall("core_pattern", system("echo /tmp/core-%e-%s-%u-%g-%p-%t > /proc/sys/kernel/core_pattern"));
	const struct rlimit unlim = { RLIM_INFINITY, RLIM_INFINITY };
	scall("setrlimit", setrlimit(RLIMIT_CORE, &unlim));
	
	for (i=1; i<argc; ) {
		if (strcmp(argv[i], "-bg")==0) { background_mode = TRUE; bg=1; }
		if (strcmp(argv[i], "-off")==0) do_off = 1;
		if (strcmp(argv[i], "-down")==0) down = 1;
		if (strcmp(argv[i], "+gps")==0) do_gps = 1;
		if (strcmp(argv[i], "-gps")==0) do_gps = 0;
		if (strcmp(argv[i], "+sdr")==0) do_sdr = 1;
		if (strcmp(argv[i], "-sdr")==0) do_sdr = 0;
		if (strcmp(argv[i], "+fft")==0) do_fft = 1;

		if (strcmp(argv[i], "-stats")==0 || strcmp(argv[i], "+stats")==0) {
			if (i+1 < argc && isdigit(argv[i+1][0])) {
				i++; print_stats = strtol(argv[i], 0, 0);
			} else {
				print_stats = 1;
			}
		}
		
		if (strcmp(argv[i], "-eeprom")==0) create_eeprom = true;
		if (strcmp(argv[i], "-cmap")==0) color_map = 1;
		if (strcmp(argv[i], "-sim")==0) wf_sim = 1;
		if (strcmp(argv[i], "-real")==0) wf_real = 1;
		if (strcmp(argv[i], "-time")==0) wf_time = 1;
		if (strcmp(argv[i], "-port")==0) { i++; alt_port = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-p")==0) { alt_port = 8074; }
		if (strcmp(argv[i], "-dump")==0 || strcmp(argv[i], "+dump")==0) { i++; ev_dump = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-flip")==0) wf_flip = 1;
		if (strcmp(argv[i], "-start")==0) wf_start = 1;
		if (strcmp(argv[i], "-mult")==0) wf_mult = 1;
		if (strcmp(argv[i], "-multgen")==0) wf_mult_gen = 1;
		if (strcmp(argv[i], "-wmax")==0) wf_max = 1;
		if (strcmp(argv[i], "-olap")==0) wf_olap = 1;
		if (strcmp(argv[i], "-meas")==0) meas = 1;
		
		// do_fft
		if (strcmp(argv[i], "-none")==0) unwrap = 0;
		if (strcmp(argv[i], "-norm")==0) unwrap = 1;
		if (strcmp(argv[i], "-rev")==0) unwrap = 2;
		if (strcmp(argv[i], "-qi")==0) rev_iq = 1;
		if (strcmp(argv[i], "-ineg")==0) ineg = 1;
		if (strcmp(argv[i], "-qneg")==0) qneg = 1;
		if (strcmp(argv[i], "-file")==0) fft_file = 1;
		if (strcmp(argv[i], "-fftsize")==0) { i++; fftsize = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-fftuse")==0) { i++; fftuse = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-np")==0) { i++; noisePwr = strtol(argv[i], 0, 0); }

		if (strcmp(argv[i], "-rcordic")==0) rx_cordic = 1;
		if (strcmp(argv[i], "-rcic")==0) rx_cic = 1;
		if (strcmp(argv[i], "-rcic2")==0) rx_cic2 = 1;
		if (strcmp(argv[i], "-rdump")==0) rx_dump = 1;
		if (strcmp(argv[i], "-wcordic")==0) wf_cordic = 1;
		if (strcmp(argv[i], "-wcic")==0) wf_cic = 1;
		if (strcmp(argv[i], "-clkg")==0) spi_clkg = 1;
		
		if (strcmp(argv[i], "-wspr")==0) { i++; wspr = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-avg")==0) { i++; navg = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-tone")==0) { i++; tone = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-slc")==0) { i++; do_slice = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-rx")==0) { i++; rx_num = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-wf")==0) { i++; wf_num = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-spispeed")==0) { i++; spi_speed = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-spi")==0) { i++; spi_delay = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-ch")==0) { i++; gps_chans = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-y")==0) { i++; rx_yield = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-p0")==0) { i++; p0 = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-p1")==0) { i++; p1 = strtol(argv[i], 0, 0); }
		if (strcmp(argv[i], "-p2")==0) { i++; p2 = strtol(argv[i], 0, 0); }
		
		i++;
		while (i<argc && ((argv[i][0] != '+') && (argv[i][0] != '-'))) {
			i++;
		}
	}
	
	lprintf("KiwiSDR v%d.%d\n", VERSION_MAJ, VERSION_MIN);
    lprintf("compiled: %s %s\n", __DATE__, __TIME__);
    
    cfg_reload(CALLED_FROM_MAIN);
	
	if (!alt_port) {
		FILE *fp = fopen("/root/.kiwi_down", "r");
		if (fp != NULL) {
			fclose(fp);
			lprintf("down by lock file\n");
			down = 1;
		}
	}
    
	TaskInit();

	bool need_hardware = (do_gps || do_sdr) && !down;

	// called early, in case another server already running so we can detect the busy socket and bail
	web_server_init(WS_INIT_CREATE);

	if (need_hardware) {
		peri_init();
		fpga_init();
		//pru_start();
		
		u2_t ctrl = CTRL_EEPROM_WP;
		ctrl_clr_set(0xffff, ctrl);
		
		if (do_off) {
			printf("ADC_CLOCK *OFF*\n");
			xit(0);
		}

#ifdef BUILD_PROTOTYPE
		if (!do_gps)		// prevent interference to GPS by external ADC clock on prototype
#endif
		{
			ctrl |= adc_clock_enable? CTRL_OSC_EN : CTRL_ADCLK_GEN;
		}
		
		ctrl_clr_set(0, ctrl);

		if (ctrl & (CTRL_OSC_EN | CTRL_ADCLK_GEN))
			printf("ADC_CLOCK %.6f MHz, CTRL_OSC_EN=%d, CTRL_ADCLK_GEN=%d\n",
				adc_clock/MHz, (ctrl&CTRL_OSC_EN)?1:0, (ctrl&CTRL_ADCLK_GEN)?1:0);
		else
			printf("ADC_CLOCK *OFF*\n");
	}
	
	if (do_fft) {
		printf("==== IQ %s\n", rev_iq? "reverse":"normal");
		if (ineg) printf("==== I neg\n");
		if (qneg) printf("==== Q neg\n");
		printf("==== unwrap %s\n", (unwrap==0)? "none" : ((unwrap==1)? "normal":"reverse"));
	}
	
	rx_server_init();
	web_server_init(WS_INIT_START);

	if (do_gps) {
		if (!GPS_CHANS) panic("no GPS_CHANS configured");
		gps_main(argc, argv);
	}

	#if 0
	static int tty;
	if (!background_mode) {
		tty = open("/dev/tty", O_RDONLY | O_NONBLOCK);
		if (tty < 0) sys_panic("open tty");
	}
	#endif

	#if 0
	static int tty;
	if (!background_mode) {
		tty = open("/dev/tty", O_RDONLY | O_NONBLOCK);
		if (tty < 0) sys_panic("open tty");
	}
	#endif

	static u64_t stats_deadline = timer_us64() + 1000000;
	static u64_t secs;
	while (TRUE) {
	
		if (!need_hardware) {
			usleep(10000);		// pause so we don't hog the machine
			NextTask("main usleep");
			continue;
		}
	
		#if 0
		if (!background_mode && (now - last_input) >= 1000) {
			#define N_IBUF 32
			char ib[N_IBUF+1];
			int n = read(tty, ib, N_IBUF);
			printf("tty %d\n", n);
			if (n >= 1) {
				ib[n] = 0;
				webserver_collect_print_stats(1);
			}
			last_input = now;
		}
		#endif
		
		if ((secs % STATS_INTERVAL_SECS) == 0) {
			if (do_sdr) {
				webserver_collect_print_stats(!do_gps);
				if (!do_gps) nbuf_stat();
			}
			TaskCheckStacks();
		}
		NextTask("main stats");

		if (!do_gps && print_stats) {
			if (!background_mode) {
				lprintf("ECPU %4.1f%% cmds %d/%d malloc %d\n",
					ecpu_use(), ecpu_cmds, ecpu_tcmds, kiwi_malloc_stat());
				ecpu_cmds = ecpu_tcmds = 0;
				TaskDump();
				printf("\n");
			}
		}

		u64_t now_us = timer_us64();
		s64_t diff = stats_deadline - now_us;
		if (diff > 0) TaskSleep(diff);
		stats_deadline += 1000000;
		secs++;
	}
}