void before_each(void) { persist_init(); timers_before(); }
static void ICACHE_FLASH_ATTR timer_fn(void *timer_data) { uint32_t delay = MAX_LOOP_TIMER_MS; os_timer_disarm(&timer); PRINTF("state: %d\n\r", state); switch(state) { case STATE_LOAD_PERSIST: if(persist_load()) { PRINTF("persist data sensor value: %d\r\n", persist_data_ptr()->sensor_value); PRINTF("persist data sensor poll count: %d\r\n", persist_data_ptr()->poll_counter); state = STATE_HF_READ; persist_data_ptr()->poll_counter += 1; if(persist_data_ptr()->poll_counter >= 10) { // read low-frequency sensor (temp etc) persist_data_ptr()->poll_counter = 0; } persist_dirty(); } else { PRINTF("err loading persist\r\n"); // todo: goto error state (blink led?) state = STATE_IDLE; } delay = MIN_LOOP_TIMER_MS; break; case STATE_INIT_PERSIST: persist_init(); state = STATE_HF_READ; delay = MIN_LOOP_TIMER_MS; break; case STATE_HF_READ: state = STATE_LF_READ; if(GPIO_INPUT_GET(GPIO_SWITCH)) { if(!(persist_data_ptr()->sensor_value)) { persist_data_ptr()->sensor_value = true; persist_dirty(); state = STATE_HF_WIFI_CONNECT; } } else { if(persist_data_ptr()->sensor_value) { persist_data_ptr()->sensor_value = false; persist_dirty(); state = STATE_HF_WIFI_CONNECT; } } break; case STATE_HF_WIFI_CONNECT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_HF_SEND; } else { wifi_connect(); state = STATE_HF_WIFI_WAIT; } break; case STATE_HF_WIFI_WAIT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_HF_SEND; } break; case STATE_HF_SEND: state = STATE_HF_SEND_WAIT; break; case STATE_HF_SEND_WAIT: state = STATE_LF_READ; break; case STATE_LF_READ: state = STATE_LF_WIFI_CONNECT; break; case STATE_LF_WIFI_CONNECT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_LF_SEND; } else { wifi_connect(); state = STATE_LF_WIFI_WAIT; } break; case STATE_LF_WIFI_WAIT: if(wifi_station_get_connect_status()==STATION_GOT_IP) { state = STATE_LF_SEND; } break; case STATE_LF_SEND: state = STATE_LF_SEND_WAIT; break; case STATE_LF_SEND_WAIT: state = STATE_IDLE; break; case STATE_IDLE: // go to deep sleep persist_save(); system_deep_sleep(SLEEP_TIME_US); return; break; default: // uhoh PRINTF("invalid state, reset\n\r"); state = STATE_IDLE; break; } os_timer_setfn(&timer, (os_timer_func_t *)timer_fn, NULL); os_timer_arm(&timer, delay, false); /* case STATE_IDLE: http_get("http://10.1.3.161/text", "", my_http_callback); state = STATE_HTTP_PENDING; timeout = 30; break; case STATE_HTTP_PENDING: timeout--; if(timeout==0) { PRINTF("timeout!\n\r"); state=STATE_IDLE; } else { delay_ms(1000); } break; default: PRINTF("invalid state, reset\n\r"); state=STATE_IDLE; break; } //system_os_post(user_procTaskPrio, 0, 0 ); */ }
struct ilka_region * ilka_open(const char *file, struct ilka_options *options) { journal_recover(file); struct ilka_region *r = calloc(1, sizeof(struct ilka_region)); if (!r) { ilka_fail("out-of-memory for ilka_region struct: %lu", sizeof(struct ilka_region)); return NULL; } slock_init(&r->lock); r->file = file; r->options = *options; if ((r->fd = file_open(file, &r->options)) == -1) goto fail_open; if ((r->len = file_grow(r->fd, ILKA_PAGE_SIZE)) == -1UL) goto fail_grow; if (!mmap_init(&r->mmap, r->fd, r->len, &r->options)) goto fail_mmap; if (!persist_init(&r->persist, r, r->file)) goto fail_persist; const struct meta * meta = meta_read(r); if (meta->magic != ilka_magic) { if (!r->options.create) { ilka_fail("invalid magic for file '%s'", file); goto fail_magic; } struct meta * m = meta_write(r); m->magic = ilka_magic; m->version = ilka_version; m->alloc = sizeof(struct meta); } if (meta->version != ilka_version) { ilka_fail("invalid version for file '%s': %lu != %lu", file, meta->version, ilka_version); goto fail_version; } if (!alloc_init(&r->alloc, r, &r->options, meta->alloc)) goto fail_alloc; if (!epoch_init(&r->epoch, r, &r->options)) goto fail_epoch; if (ILKA_MCHECK) mcheck_init(&r->mcheck); r->header_len = alloc_end(&r->alloc); return r; fail_epoch: fail_alloc: fail_version: fail_magic: persist_close(&r->persist); fail_persist: mmap_close(&r->mmap); fail_mmap: fail_grow: file_close(r->fd); fail_open: free(r); return NULL; }
/* Need to make this a function main called from a GO main wrapper */ int ipmi_sim_main(int argc, const char *argv[]) { sys_data_t sysinfo; misc_data_t data; int err, rv = 1; int i; poptContext poptCtx; struct timeval tv; console_info_t stdio_console; struct sigaction act; os_hnd_fd_id_t *conid; lmc_data_t *mc; int print_version = 0; poptCtx = poptGetContext(argv[0], argc, argv, poptOpts, 0); while ((i = poptGetNextOpt(poptCtx)) >= 0) { switch (i) { case 'd': debug++; break; case 'n': nostdio = 1; break; case 'v': print_version = 1; break; } } poptFreeContext(poptCtx); printf("IPMI Simulator version %s\n", PVERSION); data.os_hnd = ipmi_posix_setup_os_handler(); if (!data.os_hnd) { fprintf(stderr, "Unable to allocate OS handler\n"); exit(1); } err = os_handler_alloc_waiter_factory(data.os_hnd, 0, 0, &data.waiter_factory); if (err) { fprintf(stderr, "Unable to allocate waiter factory: 0x%x\n", err); exit(1); } err = data.os_hnd->alloc_timer(data.os_hnd, &data.timer); if (err) { fprintf(stderr, "Unable to allocate timer: 0x%x\n", err); exit(1); } sysinfo_init(&sysinfo); sysinfo.info = &data; sysinfo.alloc = balloc; sysinfo.free = bfree; sysinfo.get_monotonic_time = ipmi_get_monotonic_time; sysinfo.get_real_time = ipmi_get_real_time; sysinfo.alloc_timer = ipmi_alloc_timer; sysinfo.start_timer = ipmi_start_timer; sysinfo.stop_timer = ipmi_stop_timer; sysinfo.free_timer = ipmi_free_timer; sysinfo.add_io_hnd = ipmi_add_io_hnd; sysinfo.io_set_hnds = ipmi_io_set_hnds; sysinfo.io_set_enables = ipmi_io_set_enables; sysinfo.remove_io_hnd = ipmi_remove_io_hnd; sysinfo.gen_rand = sys_gen_rand; sysinfo.debug = debug; sysinfo.log = sim_log; sysinfo.csmi_send = smi_send; sysinfo.clog = sim_chan_log; sysinfo.calloc = ialloc; sysinfo.cfree = ifree; sysinfo.lan_channel_init = lan_channel_init; sysinfo.ser_channel_init = ser_channel_init; data.sys = &sysinfo; err = pipe(sigpipeh); if (err) { perror("Creating signal handling pipe"); exit(1); } act.sa_handler = handle_sigchld; sigemptyset(&act.sa_mask); act.sa_flags = 0; err = sigaction(SIGCHLD, &act, NULL); if (err) { perror("setting up sigchld sigaction"); exit(1); } err = data.os_hnd->add_fd_to_wait_for(data.os_hnd, sigpipeh[0], sigchld_ready, &data, NULL, &conid); if (err) { fprintf(stderr, "Unable to sigchld pipe wait: 0x%x\n", err); exit(1); } data.emu = ipmi_emu_alloc(&data, sleeper, &sysinfo); /* Set this up for console I/O, even if we don't use it. */ stdio_console.data = &data; stdio_console.outfd = 1; stdio_console.pos = 0; stdio_console.echo = 1; stdio_console.shutdown_on_close = 1; stdio_console.telnet = 0; stdio_console.tn_pos = 0; if (nostdio) { stdio_console.out.printf = dummy_printf; stdio_console.out.data = &stdio_console; } else { stdio_console.out.printf = emu_printf; stdio_console.out.data = &stdio_console; } stdio_console.next = NULL; stdio_console.prev = NULL; data.consoles = &stdio_console; err = ipmi_mc_alloc_unconfigured(&sysinfo, 0x20, &mc); if (err) { if (err == ENOMEM) fprintf(stderr, "Out of memory allocation BMC MC\n"); exit(1); } sysinfo.mc = mc; sysinfo.chan_set = ipmi_mc_get_channelset(mc); sysinfo.startcmd = ipmi_mc_get_startcmdinfo(mc); sysinfo.cpef = ipmi_mc_get_pef(mc); sysinfo.cusers = ipmi_mc_get_users(mc); sysinfo.sol = ipmi_mc_get_sol(mc); if (read_config(&sysinfo, config_file, print_version)) exit(1); if (print_version) exit(0); if (!sysinfo.name) { fprintf(stderr, "name not set in config file\n"); exit(1); } err = persist_init("ipmi_sim", sysinfo.name, statedir); if (err) { fprintf(stderr, "Unable to initialize persistence: %s\n", strerror(err)); exit(1); } read_persist_users(&sysinfo); err = sol_init(&sysinfo); if (err) { fprintf(stderr, "Unable to initialize SOL: %s\n", strerror(err)); goto out; } err = read_sol_config(&sysinfo); if (err) { fprintf(stderr, "Unable to read SOL configs: %s\n", strerror(err)); goto out; } err = load_dynamic_libs(&sysinfo, 0); if (err) goto out; if (!command_file) { FILE *tf; command_file = malloc(strlen(BASE_CONF_STR) + 6 + strlen(sysinfo.name)); if (!command_file) { fprintf(stderr, "Out of memory\n"); goto out; } strcpy(command_file, BASE_CONF_STR); strcat(command_file, "/"); strcat(command_file, sysinfo.name); strcat(command_file, ".emu"); tf = fopen(command_file, "r"); if (!tf) { free(command_file); command_file = NULL; } else { fclose(tf); } } if (command_file) read_command_file(&stdio_console.out, data.emu, command_file); if (command_string) ipmi_emu_cmd(&stdio_console.out, data.emu, command_string); if (!sysinfo.bmc_ipmb || !sysinfo.ipmb_addrs[sysinfo.bmc_ipmb]) { sysinfo.log(&sysinfo, SETUP_ERROR, NULL, "No bmc_ipmb specified or configured."); goto out; } sysinfo.console_fd = -1; if (sysinfo.console_addr_len) { int nfd; int val; nfd = socket(sysinfo.console_addr.s_ipsock.s_addr.sa_family, SOCK_STREAM, IPPROTO_TCP); if (nfd == -1) { perror("Console socket open"); goto out; } err = bind(nfd, (struct sockaddr *) &sysinfo.console_addr, sysinfo.console_addr_len); if (err) { perror("bind to console socket"); goto out; } err = listen(nfd, 1); if (err == -1) { perror("listen to console socket"); goto out; } val = 1; err = setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val)); if (err) { perror("console setsockopt reuseaddr"); goto out; } sysinfo.console_fd = nfd; err = data.os_hnd->add_fd_to_wait_for(data.os_hnd, nfd, console_bind_ready, &data, NULL, &conid); if (err) { fprintf(stderr, "Unable to add console wait: 0x%x\n", err); goto out; } } if (!nostdio) { init_term(); err = write(1, "> ", 2); err = data.os_hnd->add_fd_to_wait_for(data.os_hnd, 0, user_data_ready, &stdio_console, NULL, &stdio_console.conid); if (err) { fprintf(stderr, "Unable to add input wait: 0x%x\n", err); goto out; } } post_init_dynamic_libs(&sysinfo); act.sa_handler = shutdown_handler; act.sa_flags = SA_RESETHAND; for (i = 0; shutdown_sigs[i]; i++) { err = sigaction(shutdown_sigs[i], &act, NULL); if (err) { fprintf(stderr, "Unable to register shutdown signal %d: %s\n", shutdown_sigs[i], strerror(errno)); } } tv.tv_sec = 1; tv.tv_usec = 0; err = data.os_hnd->start_timer(data.os_hnd, data.timer, &tv, tick, &data); if (err) { fprintf(stderr, "Unable to start timer: 0x%x\n", err); goto out; } data.os_hnd->operation_loop(data.os_hnd); rv = 0; out: shutdown_handler(0); exit(rv); }