/* ---- Main function ---- */ int main(void) { int temp; init_current_time(); // Intro uart0_printf("\r\n\r\nSTORM SoC Basic Configuration\r\n"); uart0_printf("Demo program + Timer\r\n\r\n"); uart1_print(); while (1) { //TIMER0_LOAD = 1562; check_timer (50) ; } }
/** * If the timer is pending, return the revents and clear the pending * status (so the timer callback won't be called). * * Usage: * revents = timer:clear_pending(loop) * * [+1, -0, e] */ static int timer_clear_pending(lua_State *L) { ev_timer* timer = check_timer(L, 1); struct ev_loop* loop = *check_loop_and_init(L, 2); int revents = ev_clear_pending(loop, timer); if ( ! timer->repeat && ( revents & EV_TIMEOUT ) ) { loop_stop_watcher(L, 2, 1); } lua_pushnumber(L, revents); return 1; }
/** * \brief Implementation of timer:get_remaining_time(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::timer_api_get_remaining_time(lua_State* l) { Timer& timer = check_timer(l, 1); LuaContext& lua_context = get_lua_context(l); const std::map<Timer*, LuaTimerData>::const_iterator it = lua_context.timers.find(&timer); if (it == lua_context.timers.end() || it->second.callback_ref == LUA_REFNIL) { // This timer is already finished or was canceled. lua_pushinteger(l, 0); } else { int remaining_time = std::max(0, int(System::now()) - int(timer.get_expiration_date())); lua_pushinteger(l, remaining_time); } return 1; }
static int run_timer(void *obj, void *arg, int flags) { struct pthread_timer *timer = obj; if (timer->state == TIMER_STATE_IDLE) { return 0; } ao2_lock(timer); if (check_timer(timer)) { write_byte(timer); } ao2_unlock(timer); return 0; }
/** * @brief Implementation of timer:set_suspended_with_map(). * @param l The Lua context that is calling this function. * @return Number of values to return to Lua. */ int LuaContext::timer_api_set_suspended_with_map(lua_State* l) { LuaContext& lua_context = get_lua_context(l); Timer& timer = check_timer(l, 1); bool suspended_with_map = true; if (lua_gettop(l) >= 2) { suspended_with_map = lua_toboolean(l, 2); } timer.set_suspended_with_map(suspended_with_map); Game* game = lua_context.get_main_loop().get_game(); timer.notify_map_suspended(game->get_current_map().is_suspended()); return 0; }
static int run_timer(void *obj, void *arg, int flags) { struct pthread_timer *timer = obj; if (timer->state == TIMER_STATE_IDLE) { return 0; } ao2_lock(timer); if (check_timer(timer)) { timer->pending_ticks++; signal_pipe(timer); } ao2_unlock(timer); return 0; }
/** * \brief Implementation of timer:set_suspended_with_map(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::timer_api_set_suspended_with_map(lua_State* l) { return LuaTools::exception_boundary_handle(l, [&] { LuaContext& lua_context = get_lua_context(l); const TimerPtr& timer = check_timer(l, 1); bool suspended_with_map = LuaTools::opt_boolean(l, 2, true); timer->set_suspended_with_map(suspended_with_map); Game* game = lua_context.get_main_loop().get_game(); if (game != nullptr && game->has_current_map()) { // If the game is running, suspend/resume the timer like the map. timer->notify_map_suspended(game->get_current_map().is_suspended()); } return 0; }); }
/** * \brief Implementation of timer:set_suspended_with_map(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::timer_api_set_suspended_with_map(lua_State* l) { LuaContext& lua_context = get_lua_context(l); Timer& timer = check_timer(l, 1); bool suspended_with_map = true; if (lua_gettop(l) >= 2) { suspended_with_map = lua_toboolean(l, 2); } timer.set_suspended_with_map(suspended_with_map); Game* game = lua_context.get_main_loop().get_game(); if (game != NULL && game->has_current_map()) { // If the game is running, suspend/unsuspend the timer like the map. timer.notify_map_suspended(game->get_current_map().is_suspended()); } return 0; }
/** * \brief Implementation of timer:set_remaining_time(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::timer_api_set_remaining_time(lua_State* l) { Timer& timer = check_timer(l, 1); uint32_t remaining_time = luaL_checkint(l, 2); LuaContext& lua_context = get_lua_context(l); const std::map<Timer*, LuaTimerData>::const_iterator it = lua_context.timers.find(&timer); if (it != lua_context.timers.end() && it->second.callback_ref != LUA_REFNIL) { // The timer is still active. const uint32_t now = System::now(); const uint32_t expiration_date = now + remaining_time; timer.set_expiration_date(expiration_date); if (now >= expiration_date) { // Execute the callback now. lua_context.do_timer_callback(timer); } } return 0; }
/** * Restart a timer with the specified repeat number of seconds. If no * repeat was specified, the timer is simply stopped. May optionally * specify a new value for repeat, otherwise uses the value set when * the timer was created. * * 1 - timer object. * 2 - loop object. * 3 - repeat (number of seconds to wait for consecutive timeouts). * * Usage: * timer:again(loop [, repeat_seconds]) * * [+0, -0, e] */ static int timer_again(lua_State *L) { ev_timer* timer = check_timer(L, 1); struct ev_loop* loop = *check_loop_and_init(L, 2); ev_tstamp repeat = luaL_optnumber(L, 3, 0); if ( repeat < 0.0 ) luaL_argerror(L, 3, "repeat must be greater than 0"); if ( repeat ) timer->repeat = repeat; if ( timer->repeat ) { ev_timer_again(loop, timer); loop_start_watcher(L, 2, 1, -1); } else { /* Just calling stop instead of again in case the symantics * change in libev */ loop_stop_watcher(L, 2, 1); ev_timer_stop(loop, timer); } return 0; }
static bool send_handler(int *err, struct sa *dst, struct mbuf *mb, void *arg) { struct tls_sock *ts = arg; struct tls_conn *tc; int r; tc = tls_udp_conn(ts, dst); if (!tc) { /* No connection found, assuming Client role */ tc = conn_alloc(ts, dst); if (!tc) { *err = ENOMEM; return true; } SSL_set_connect_state(tc->ssl); check_timer(tc); } r = SSL_write(tc->ssl, mbuf_buf(mb), (int)mbuf_get_left(mb)); if (r < 0) { switch (SSL_get_error(tc->ssl, r)) { case SSL_ERROR_WANT_READ: break; default: DEBUG_WARNING("SSL_write: %d\n", SSL_get_error(tc->ssl, r)); *err = EPROTO; return true; } } return true; }
/** * \brief Implementation of timer:get_remaining_time(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::timer_api_get_remaining_time(lua_State* l) { return LuaTools::exception_boundary_handle(l, [&] { const TimerPtr& timer = check_timer(l, 1); LuaContext& lua_context = get_lua_context(l); const auto it = lua_context.timers.find(timer); if (it == lua_context.timers.end() || it->second.callback_ref.is_empty()) { // This timer is already finished or was canceled. lua_pushinteger(l, 0); } else { int remaining_time = (int) timer->get_expiration_date() - (int) System::now(); if (remaining_time < 0) { remaining_time = 0; } lua_pushinteger(l, remaining_time); } return 1; }); }
/** * \brief Implementation of timer:set_remaining_time(). * \param l The Lua context that is calling this function. * \return Number of values to return to Lua. */ int LuaContext::timer_api_set_remaining_time(lua_State* l) { return LuaTools::exception_boundary_handle(l, [&] { const TimerPtr& timer = check_timer(l, 1); uint32_t remaining_time = LuaTools::check_int(l, 2); LuaContext& lua_context = get_lua_context(l); const auto it = lua_context.timers.find(timer); if (it != lua_context.timers.end() && !it->second.callback_ref.is_empty()) { // The timer is still active. const uint32_t now = System::now(); const uint32_t expiration_date = now + remaining_time; timer->set_expiration_date(expiration_date); if (now >= expiration_date) { // Execute the callback now. lua_context.do_timer_callback(timer); } } return 0; }); }
static int ozrtp_rtp_recvfrom(RtpTransport *t, mblk_t *m, int flags, struct sockaddr *from, socklen_t *fromlen){ int rlen; ZrtpContext *zrtpContext = (ZrtpContext*) t->data; OrtpZrtpContext *userData = (OrtpZrtpContext*) zrtpContext->userData; // Do extra stuff first check_timer(zrtpContext, userData); // Check if something to receive rlen=rtp_session_rtp_recv_abstract(t->session->rtp.socket,m,flags,from,fromlen); if (rlen<=0) { // nothing was received or error: pass the information to caller return rlen; } uint8_t* rtp = m->b_rptr; int rtpVersion = ((rtp_header_t*)rtp)->version; // If plain or secured RTP if (rtpVersion == 2) { if (userData->srtpRecv != NULL && zrtp_inState(zrtpContext, SecureState)) { // probably srtp packet, unprotect err_status_t err = srtp_unprotect(userData->srtpRecv,m->b_wptr,&rlen); if (err != err_status_ok) { ortp_warning("srtp_unprotect failed; packet may be plain RTP"); } } // in both cases (RTP plain and deciphered srtp) return rlen; } // if ZRTP packet, send to engine uint32_t *magicField=(uint32_t *)(rtp + 4); if (rlen >= ZRTP_MIN_MSG_LENGTH && rtpVersion==0 && ntohl(*magicField) == ZRTP_MAGIC) { print_zrtp_packet("received", rtp); uint8_t *ext_header = rtp+ZRTP_MESSAGE_OFFSET; uint16_t ext_length = get_zrtp_message_length(ext_header); char messageType[9]; parseZrtpMessageType(messageType, ext_header); // Check max length if (rlen < 12 + ext_length + 4) { ortp_warning("Received malformed ZRTP-like packet: size %d (expected %d)", rlen, 12 + ext_length + 4); return 0; } // Check sequence number uint16_t seq_number = get_rtp_seqnumber(rtp); if (userData->last_recv_zrtp_seq_number != 0 && seq_number <= userData->last_recv_zrtp_seq_number) { // Discard out of order ZRTP packet ortp_message("Discarding received out of order zrtp packet: %d (expected >%d)", seq_number, userData->last_recv_zrtp_seq_number); return 0; } // Check packet checksum uint32_t rcv_crc = get_zrtp_packet_crc((uint32_t*)rtp, ext_length); uint32_t zrtp_total_packet_length = ZRTP_MESSAGE_OFFSET + 4*ext_length + 4; if (!zrtp_CheckCksum(rtp, zrtp_total_packet_length-CRC_SIZE, rcv_crc)) { ortp_warning("Bad ZRTP packet checksum %u total %u", rcv_crc, zrtp_total_packet_length); return 0; } uint32_t peerssrc = ntohl(*(uint32_t*)(rtp+8)); #if HAVE_zrtpcpp_with_len zrtp_processZrtpMessage(zrtpContext, ext_header, peerssrc,rlen); #else zrtp_processZrtpMessage(zrtpContext, ext_header, peerssrc); #endif userData->last_recv_zrtp_seq_number=seq_number; return 0; } else { // Not a ZRTP packet, accept it return rlen; } }
/** * Our main entry, decode requests and monitor activity */ static void avr_evtd_main(void) { char buf[17]; char cmd; char pushed_power = 0; char pushed_reset = 0; char pressed_power_flag = 0; char pressed_reset_flag = 0; char current_status = 0; time_t idle = time(NULL); time_t power_press = idle; time_t fault_time; time_t last_shutdown_ping; fd_set serialfd_set; struct timeval timeout_poll; int fan_fault = 0; long time_diff; char extraTime = 0; char disk_full = 0; /* Update the shutdown timer */ fault_time = 0; last_shutdown_ping = time(NULL); /* Loop whilst port is valid */ while (serialfd) { timeout_poll.tv_usec = 0; int res = refresh_rate; /* After file change or startup, update the time within 20 secs as the * user may have pushed the refresh time out. */ if (check_state > 0) { res = 2; } else { /* Change our timer to check for a power/reset request need a * faster poll rate here to see the double press event * properly. */ if (pushed_power || pushed_reset || first_time_flag > 1) { timeout_poll.tv_usec = 250; res = 0; check_state = -2; /* Hold off any configuration file updates */ } } if (check_state != -2) { /* Ensure we shutdown on the nail if the timer is enabled will * be off slightly as timer reads are different */ if (timer_flag == 1) { if (shutdown_timer < res) res = shutdown_timer; } /* If we have a fan failure report, then ping frequently */ if (fan_fault > 0) res = fan_fault == 6 ? fan_fault_seize : 2; } timeout_poll.tv_sec = res; FD_ZERO(&serialfd_set); FD_SET(serialfd, &serialfd_set); /* Wait for AVR message or time-out? */ res = select(serialfd + 1, &serialfd_set, NULL, NULL, &timeout_poll); time_t time_now = time(NULL); /* catch input? */ if (res > 0) { /* Read AVR message */ res = read(serialfd, buf, 16); /* AVR command detected so force to ping only */ check_state = -2; switch (buf[0]) { /* power button release */ case 0x20: /* ' ' */ if (pressed_power_flag == 0) { cmd = POWER_RELEASE; if ((time_now - power_press) <= HOLD_TIME && first_time_flag < 2) { cmd = USER_RESET; } else if (shutdown_timer < FIVE_MINUTES || first_time_flag > 1) { if (first_time_flag == 0) first_time_flag = 10; shutdown_timer += FIVE_MINUTES; first_time_flag--; extraTime = 1; } exec_simple_cmd(cmd); power_press = time_now; } pushed_power = pressed_power_flag = 0; break; /* power button push */ case 0x21: /* '!' */ exec_simple_cmd(POWER_PRESS); pressed_power_flag = 0; pushed_power = 1; break; /* reset button release */ case 0x22: /* '"' */ if (pressed_reset_flag == 0) { cmd = RESET_RELEASE; res = 0; /* Launch our telnet daemon */ if ((time_now - power_press) <= HOLD_TIME) { cmd = SPECIAL_RESET; res = reset_presses; reset_presses++; } exec_cmd(cmd, res); power_press = time_now; } pushed_reset = pressed_reset_flag = 0; break; /* reset button push */ case 0x23: /* '#' */ exec_simple_cmd(RESET_PRESS); pressed_reset_flag = 0; pushed_reset = 1; break; /* Fan on high speed */ case 0x24: /* '$' */ fan_fault = 6; fault_time = time_now; break; /* Fan fault */ case 0x25: /* '%' */ /* Flag the EventScript */ exec_cmd(FAN_FAULT, fan_fault); if (fan_fault_seize > 0) { fan_fault = 2; fault_time = time_now; } else fan_fault = -1; break; /* Acknowledge */ case 0x30: /* '0' */ break; /* AVR halt requested */ case 0x31: /* '1' */ close_serial(); exec_simple_cmd(AVR_HALT); break; /* AVR initialization complete */ case 0x33: /* '3' */ break; default: syslog(LOG_INFO, "unknown message %X[%d]", buf[0], res); break; } /* Get time for use later */ time(&idle); } else { /* Time-out event */ /* Check if button(s) are still held after holdcyle seconds */ if ((idle + hold_cycle) < time_now) { /* Power down selected */ if (pushed_power == 1) { /* Re-validate our time wake-up; do not perform if in extra time */ if (!extraTime) set_avr_timer(1); exec_simple_cmd(USER_POWER_DOWN); pushed_power = 0; pressed_power_flag = 1; } } /* Has user held the reset button long enough to request EM-Mode? */ if ((idle + EM_MODE_TIME) < time_now) { if (pushed_reset == 1 && in_em_mode) { /* Send EM-Mode request to script. The script handles the * flash device decoding and writes the HDD no-good flag * NGNGNG into the flash status. It then flags a reboot * which causes the box to boot from ram-disk backup to * recover the HDD. */ exec_simple_cmd(EM_MODE); pushed_reset = 0; pressed_reset_flag = 1; } } /* Skip this processing during power/reset scan */ if (!pushed_reset && !pushed_power && first_time_flag < 2) { /* shutdown timer event? */ if (timer_flag == 1) { /* Decrement our powerdown timer */ if (shutdown_timer > 0) { time_diff = (time_now - last_shutdown_ping); /* If time difference is more than a minute, * force a re-calculation of shutdown time */ if (refresh_rate + 60 > labs(time_diff)) { shutdown_timer -= time_diff; /* Within five minutes of shutdown? */ if (shutdown_timer < FIVE_MINUTES) { if (first_time_flag) { first_time_flag = 0; /* Inform the EventScript */ exec_cmd(FIVE_SHUTDOWN, shutdown_timer); /* Re-validate out time wake-up; do not * perform if in extra time */ if (!extraTime) set_avr_timer(1); } } } /* Large clock drift, either user set time * or an ntp update, handle accordingly. */ else { check_timer(2); } } else { /* Prevent re-entry and execute command */ pushed_power = pressed_reset_flag = 2; exec_simple_cmd(TIMED_SHUTDOWN); } } /* Keep track of shutdown time remaining */ last_shutdown_ping = time(NULL); /* Split loading, handle disk checks * over a number of cycles, reduce CPU hog */ switch (check_state) { /* Kick state machine */ case 0: check_state = 1; break; /* Check for timer change through configuration file */ case 1: check_timer(0); check_state = 2; break; /* Check the disk and ping AVR accordingly */ case -2: /* Check the disk to see if full and output appropriate * AVR command? */ case 2: cmd = keep_alive; if ((current_status = check_disk())) { /* Execute some user code on disk full */ if (first_warning) { first_warning = pester_message; exec_cmd(DISK_FULL, pct_used); } } /* Only update DISK LED on disk full change */ if (disk_full != current_status) { /* LED status */ cmd = 0x56; /* 'V' */ if (current_status) cmd++; else { first_warning = 0; exec_cmd(DISK_FULL, 0); } disk_full = current_status; } /* Ping AVR */ write_to_uart(cmd); check_state = 3; break; /* Wait for next refresh kick */ case 3: check_state = 0; break; } } /* Try and catch spurious fan fault messages */ switch (fan_fault) { case -1: break; case 1: fan_fault = 0; break; /* Check how long we have been operating with a fan failure */ case 2: case 3: case 4: if ((fault_time + fan_fault_seize) < time_now) { /* Run some user script on no fan restart message after * FAN_FAULT_SEIZE time */ exec_cmd(FAN_FAULT, 4); fan_fault = 5; } break; /* Fan sped up message received */ case 6: /* Attempt to slow fan down again after 5 minutes */ if ((fault_time + FIVE_MINUTES) < time_now) { write_to_uart(0x5C); /* '\\' */ fan_fault = 1; } break; } /* Check that the shutdown pause function (if activated) is still * available, no then ping the delayed time */ if ((power_press + SP_MONITOR_TIME) < time_now && first_time_flag > 1) { /* Inform the EventScript */ exec_cmd(FIVE_SHUTDOWN, shutdown_timer/60); first_time_flag = 1; power_press = 0; } } } }
void DropletCustomFive::DropletMainLoop() { switch(state) { case WANDER: if(!wander_rgb) { set_rgb_led(255,0,255); wander_rgb = true; } if(!is_moving()) { move_steps((rand_byte() % 6) + 1, rand_byte() * 50); } if(!first_set) { uint8_t r, g, b; get_rgb_sensor(&r, &g, &b); if(r > 200) { first_set = true; char msg = 'R'; ir_broadcast(&msg, sizeof(msg)); cancel_move(); cancel_rotate(); guitar_id = 0; row_num = 0; num_in_row = 1; state = CALL; break; } } while(check_for_new_messages()) { char let = global_rx_buffer.buf[0]; uint8_t num; uint16_t idnum; switch(let) { case 'V': float dist, theta, phi; range_and_bearing(global_rx_buffer.sender_ID, &dist, &theta, &phi); num = global_rx_buffer.buf[1]; if(dist<20 && dist>10) { char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'C'; msg[1] = num; ir_broadcast(&msg[0], sizeof(msg)); } break; case 'S': memcpy(&idnum, &global_rx_buffer.buf[1], 2); if(idnum == get_droplet_id()) { target = global_rx_buffer.sender_ID; slot_num = (uint8_t)global_rx_buffer.buf[3]; guitar_id = (uint8_t)global_rx_buffer.buf[4]; cancel_move(); cancel_rotate(); state = DOCK; break; } break; case 'R': first_set = true; break; case 'D': range_and_bearing(global_rx_buffer.sender_ID, &dist, &theta, &phi); if(dist<30) { rotate_degrees(static_cast<int16_t>(theta+160)); move_steps(NORTH, 30); } break; default: break; } global_rx_buffer.read=true; } break; case DOCK: if(!dock_rgb) { set_rgb_led(150,150,150); dock_rgb = true; } if(!is_moving()) { float dist, theta, phi; if(!range_and_bearing(target, &dist, &theta, &phi)) { state = WANDER; set_rgb_led(0, 0, 0); move_steps((rand_byte() % 6) + 1, rand_byte()*50); break; } if(!is_rotating() && abs(theta) > 2.5f) { rotate_degrees(static_cast<int16_t>(theta)); } if(!is_rotating() && abs(theta) <= 2.5f) { move_steps(NORTH, static_cast<uint16_t>(dist)); } if(dist <= 20) { if(!clock_wise) // rotate around target counter clockwise { rotate_degrees(static_cast<int16_t>(theta-90)); move_steps(NORTH, 2); char msg = 'D'; ir_broadcast(&msg, 1); if(theta>85) { switch(slot_num) { case 0: if(phi < -75 && phi > -180) { cancel_move(); cancel_rotate(); rotate_degrees(static_cast<int16_t>(-180)); clock_wise = true; break; } if(phi<-59 && phi>-61) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 1: if(phi < -75 && phi > -180) { cancel_move(); cancel_rotate(); rotate_degrees(static_cast<int16_t>(-180)); clock_wise = true; break; } if(phi>-1 && phi<1) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 2: if(phi>59 && phi<61) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 3: if(phi<121 && phi>119) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 4: if((phi>179 && phi<181)||(phi>-181 && phi<-179)) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 5: if(phi>-121 && phi<-119) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; default: break; } } } else // rotate around target clockwise { rotate_degrees(static_cast<int16_t>(theta+90)); move_steps(NORTH, 2); char msg = 'D'; ir_broadcast(&msg, 1); if(theta<-85) { switch(slot_num) { case 0: if(phi < -30 && phi > -75) { cancel_move(); cancel_rotate(); rotate_degrees(static_cast<int16_t>(180)); clock_wise = false; break; } if(phi<121 && phi>119) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 1: if(phi < -50 && phi > -75) { cancel_move(); cancel_rotate(); rotate_degrees(static_cast<int16_t>(180)); clock_wise = false; break; } if((phi>177 && phi<181)||(phi<-177 && phi>-181)) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 2: if(phi>-121 && phi<-119) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 3: if(phi>-31 && phi<-29) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 4: if(phi>-1 && phi<1) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; case 5: if(phi>59 && phi<61) { cancel_move(); cancel_rotate(); state = ALIGN; break; } break; default: break; } } } } } if(check_for_new_messages()) { char let = (char)global_rx_buffer.buf[0]; uint8_t num = (uint8_t)global_rx_buffer.buf[1]; switch(let) { case 'A': if(num == guitar_id) { wander_rgb = false; state = WANDER; } case 'F': if(num == guitar_id) { wander_rgb = false; state = WANDER; } break; case 'T': float dist, theta, phi; range_and_bearing(global_rx_buffer.sender_ID, &dist, &theta, &phi); if(dist<5) { move_steps(SOUTH, 5); if(clock_wise) { cancel_move(); cancel_rotate(); clock_wise = false; rotate_degrees(static_cast<int16_t>(theta+165)); move_steps(NORTH, 20); break; } else { cancel_move(); cancel_rotate(); clock_wise = true; rotate_degrees(static_cast<int16_t>(theta+165)); move_steps(NORTH, 20); break; } } break; default: break; } global_rx_buffer.read = true; } break; case ALIGN: if(!align_rgb) { set_rgb_led(255,0,0); align_rgb = true; } float dist, theta, phi; range_and_bearing(target, &dist, &theta, &phi); rotate_degrees(static_cast<int16_t>(theta)); move_steps(NORTH, static_cast<uint16_t>(dist)); if(dist<5) { cancel_move(); rotate_degrees(static_cast<int16_t>(phi)); if(phi < 2 && phi >-2) { char msg[4]; msg[0] = 'F'; memcpy(&msg[1], &target, sizeof(uint16_t)); msg[3] = slot_num; ir_broadcast(&msg[0], sizeof(msg)); state = PRESET; break; } } while(check_for_new_messages()) { char let = (char)global_rx_buffer.buf[0]; uint8_t num = (uint8_t)global_rx_buffer.buf[1]; switch(let) { case 'A': if(num == guitar_id) { wander_rgb = false; state = WANDER; } case 'F': if(num == guitar_id) { wander_rgb = false; state = WANDER; } break; default: break; } global_rx_buffer.read = true; } break; case CALL: if(!call_rgb) { set_rgb_led(0,255,255); call_rgb = true; } if(!first_search) { find_empty_neighbors(guitar_id, row_num, num_in_row); first_search = true; set_timer(10000, 0); } if(check_timer(0)) { for(uint8_t i=0; i<6; i++) { if((neighbors[i]>0) && (neighbors[i]<200)) { /* this message calls needed droplets */ char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'V'; msg[1] = i; ir_broadcast(&msg[0], sizeof(msg)); } } } else { for(uint8_t i=0; i<6; i++) { if((neighbors[i]>0)) { /* timer hasn't gone off, we're list building and calling out all possible neighbors to see which ones are already there */ char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'N'; msg[1] = neighbors[i]; ir_broadcast(&msg[0], sizeof(msg)); } } } while(check_for_new_messages()) { char let = global_rx_buffer.buf[0]; uint8_t num; uint16_t send_id; switch(let) { case 'C': send_id = global_rx_buffer.sender_ID; num = (uint8_t)global_rx_buffer.buf[1]; if((neighbors[num]>0) && (neighbors[num]<200)) { char msg[5]; memset(&msg[0], 0, 3); msg[0] = 'S'; memcpy(&msg[1], &send_id, sizeof(uint16_t)); msg[3] = num; msg[4] = neighbors[num]; ir_broadcast(&msg[0], sizeof(msg)); neighbors[num] = 255; } break; case 'F': memcpy(&send_id, &global_rx_buffer.buf[1], sizeof(uint16_t)); num = (uint8_t)global_rx_buffer.buf[3]; if(send_id == get_droplet_id()) { set_rgb_led(255, 0, 0); call_rgb = false; neighbors[num] = 0; slots_set++; } break; case 'A': num = (uint8_t)global_rx_buffer.buf[1]; for(uint8_t i=0; i<6; i++) { if((neighbors[i] > 0) && (neighbors[i] == num)) { neighbors[i] = 0; slots_needed--; break; } } break; default: break; } global_rx_buffer.read = true; } if(slots_set == slots_needed) { char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'L'; msg[1] = (guitar_id + 1); ir_broadcast(&msg[0], sizeof(msg)); is_set_rgb = false; state = SET; break; } /*if((neighbors[1])==0 && (neighbors[2]==0) && (neighbors[3]==0) && (neighbors[4]==0) && (neighbors[5]==0)) { char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'L'; msg[1] = (guitar_id + 1); ir_broadcast(&msg[0], sizeof(msg)); is_set_rgb = false; state = SET; break; }*/ break; case PRESET: if(!preset_rgb) { set_rgb_led(0, 255, 0); preset_rgb = true; } set_rgb_led(0, 255, 0); while(check_for_new_messages()) { char let = (char)global_rx_buffer.buf[0]; uint8_t num = (uint8_t)global_rx_buffer.buf[1]; uint8_t num2; switch(let) { case 'S': num2 = (uint8_t)global_rx_buffer.buf[4]; if(num2 == guitar_id) { set_rgb_led(255, 0, 0); char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'A'; msg[1] = guitar_id; ir_broadcast(&msg[0], sizeof(msg)); } break; case 'L': if(num==guitar_id) { row_num = get_row_num(guitar_id); num_in_row = get_num_in_row(guitar_id); call_rgb = false; state = CALL; } break; case 'N': if(num==guitar_id) { set_rgb_led(255, 0, 0); char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'A'; msg[1] = guitar_id; ir_broadcast(&msg[0], sizeof(msg)); } break; case 'D': float dist, theta, phi; range_and_bearing(global_rx_buffer.sender_ID, &dist, &theta, &phi); if(dist<5) { char msg = 'T'; ir_broadcast(&msg, 1); } break; default: break; } global_rx_buffer.read = true; } break; case SET: if(!is_set_rgb) { set_rgb_led(0,0,255); is_set_rgb = true; } while(check_for_new_messages()) { char let = (char)global_rx_buffer.buf[0]; uint8_t num = (uint8_t)global_rx_buffer.buf[1]; switch(let) { case 'N': if(num==guitar_id) { set_rgb_led(255, 0 , 0); is_set_rgb = false; char msg[2]; memset(&msg[0], 0, 2); msg[0] = 'A'; msg[1] = guitar_id; ir_broadcast(&msg[0], sizeof(msg)); } break; case 'D': float dist, theta, phi; range_and_bearing(global_rx_buffer.sender_ID, &dist, &theta, &phi); if(dist<5) { char msg = 'T'; ir_broadcast(&msg, 1); } break; default: break; } global_rx_buffer.read = true; } break; } }
void smtpc_run (void) { /* On that function we can send data when called by main loop */ if( smtpc_init_done == 0 ) return; if( smtp_client.state < SMTP_OPEN_REQUESTED) return; /* Is there timeout of some sort? */ if(check_timer(smtp_client.tmrhandle) == 0) { /* Yep */ (void)tcp_abort(smtp_client.sochandle); smtpc_changestate(SMTP_CLOSED); /* Make user callback */ smtpc_error(); return; } if( smtp_client.state == SMTP_OPEN_REQUESTED) { /* We are on this state because user has requested connection */ /* but connection is not yet opened. */ /* Try to get TCP stack to accept our connection request */ (void)tcp_abort(smtp_client.sochandle); /* Release old connection */ if(tcp_connect(smtp_client.sochandle, smtp_client.remip, smtp_client.remport, 0) >= 0) smtpc_changestate(SMTP_CONNECTIONOPEN_SENT); return; } if( tcp_getstate(smtp_client.sochandle) != TCP_STATE_CONNECTED ) { return; } if( tcp_checksend(smtp_client.sochandle) < 0 ) return; /* It's connected and no unacked data so try to send */ if(smtp_client.state == SMTP_SERVER_READY) { /* Send HELO */ smtpc_sendhelo(); smtpc_changestate(SMTP_HELO_SENT); DEBUGOUT("SMTP HELO packet sent\r\n"); return; } if(smtp_client.state == SMTP_HELO_ACKED) { /* Send MAIL FROM */ smtpc_sendmailfrom(); smtpc_changestate(SMTP_MAILFROM_SENT); DEBUGOUT("SMTP MAIL FROM packet sent\r\n"); return; } if(smtp_client.state == SMTP_MAILFROM_ACKED) { /* Send RCPT TO */ smtpc_sendrcptto(); smtpc_changestate(SMTP_RCPTTO_SENT); DEBUGOUT("SMTP RCPT TO packet sent\r\n"); return; } if(smtp_client.state == SMTP_RCPTTO_ACKED) { /* Send DATA */ smtpc_senddatareq(); smtpc_changestate(SMTP_DATAREQ_SENT); DEBUGOUT("SMTP DATA packet sent\r\n"); return; } if(smtp_client.state == SMTP_DATAREQ_ACKED) { /* Send BODY */ smtpc_sendbody(); smtpc_changestate(SMTP_BODY_SENT); DEBUGOUT("SMTP BODY packet sent\r\n"); return; } /* Body is part of plain text so we just make internal state change */ /* when TCP has acked the body packet. This pseudo-state just helps */ /* us to regenerate the body when needed */ if(smtp_client.state == SMTP_BODY_SENT) smtpc_changestate(SMTP_SENDING_DATA); if(smtp_client.state == SMTP_SENDING_DATA) { /* Inform user app that old data is acked now */ smtpc_dataacked(); if (smtpc_senddata() < 0) { /* End of data, send CRLF.CRLF */ DEBUGOUT("SMTP End of data reached\r\n"); smtpc_senddataend(); smtpc_changestate(SMTP_DATAEND_SENT); } return; } if(smtp_client.state == SMTP_DATAEND_ACKED) { /* Send QUIT */ smtpc_sendquit(); smtpc_changestate(SMTP_QUIT_SENT); DEBUGOUT("SMTP QUIT packet sent\r\n"); return; } if(smtp_client.state == SMTP_QUIT_ACKED) { /* Inform application that data is sent OK */ smtpc_allok(); /* Try to close TCP */ if(tcp_close(smtp_client.sochandle) >= 0) { smtpc_changestate(SMTP_CLOSED); DEBUGOUT("SMTP connection closed OK\r\n"); return; } /* Close is not accepted by TCP. See if timeout */ if(check_timer(smtp_client.tmrhandle) == 0) { /* Use brute force */ (void)tcp_abort(smtp_client.sochandle); smtpc_changestate(SMTP_CLOSED); DEBUGOUT("SMTP connection closed by ABORT\r\n"); return; } /* Keep trying untill timeout */ return; } return; }
static bool recv_handler(struct sa *src, struct mbuf *mb, void *arg) { struct tls_sock *ts = arg; struct tls_conn *tc; int r; tc = tls_udp_conn(ts, src); if (!tc) { /* No connection found, assuming Server role */ tc = conn_alloc(ts, src); if (!tc) return true; SSL_set_verify(tc->ssl, 0, 0); SSL_set_accept_state(tc->ssl); } /* feed SSL data to the BIO */ r = BIO_write(tc->sbio_in, mbuf_buf(mb), (int)mbuf_get_left(mb)); if (r <= 0) return true; check_timer(tc); mbuf_set_pos(mb, 0); for (;;) { int n; if (mbuf_get_space(mb) < 4096) { if (mbuf_resize(mb, mb->size + 8192)) return true; } n = SSL_read(tc->ssl, mbuf_buf(mb), (int)mbuf_get_space(mb)); if (n < 0) { const int ssl_err = SSL_get_error(tc->ssl, n); switch (ssl_err) { case SSL_ERROR_WANT_READ: break; default: return true; } break; } else if (n == 0) break; mb->pos += n; } if (!mb->pos) return true; mbuf_set_end(mb, mb->pos); mbuf_set_pos(mb, 0); return false; }
uint32_t run_soc(soc_t* soc) { cpu_t *cpu = soc->cpu[0]; if(config.gdb_debug){ LOG(LOG_DEBUG, "last pc is %x\n", cpu->run_info.last_pc); if(cpu->run_info.halting == MAYBE){ /* The break operation code is set by debugger. So the last operation code executed should be a break trap set by the debugger. That means we should re-execute the operation code in that address. Restore last PC value to the PC can do such thing.*/ if(is_sw_breakpoint(soc->stub, cpu->run_info.last_pc)){ cpu->run_info.halting = TRUE; cpu->set_raw_pc(cpu->run_info.last_pc, cpu); } // The break operation code is directly wrote in the program else{ cpu->run_info.halting = FALSE; } } if(soc->stub->status == RSP_STEP){ cpu->run_info.halting = TRUE; } /* cpu halting for debug */ while(cpu->run_info.halting){ handle_rsp(soc->stub, cpu); } } /* store last pc */ cpu->run_info.last_pc = cpu->get_raw_pc(cpu); /* basic steps to run a single operation code */ uint32_t opcode = cpu->fetch32(cpu); ins_t ins_info = cpu->decode(cpu, &opcode); cpu->excute(cpu, ins_info); add_cycle(cpu); check_timer(cpu); /* check peripheral input every 100 */ pmp_parsed_pkt_t pmp_pkt; int result; if(config.client && reach_check_point(cpu)){ core_connect_t *peri_connect = armue_get_peri_connect(cpu); bool_t has_input = pmp_check_input(peri_connect); if(has_input){ // start input parsing loop pmp_parse_loop(peri_connect){ result = pmp_parse_input(peri_connect, &pmp_pkt); if(result >= 0){ dispatch_peri_event(&pmp_pkt); } } peri_connect->recv_buf[peri_connect->recv_len] = '\0'; LOG(LOG_INFO, "Peripheral packet type[%d] received: %s\n", pmp_pkt.pkt_kind, peri_connect->recv_buf); } updata_check_point(cpu, 10); }
/** \brief BOOTP client main loop * \author * \li Jari Lahti ([email protected]) * \date 07.10.2002 * * Main thread of the BOOTP client that should be invoked periodically. */ void bootpc_run (void) { INT16 i; UINT8 buf[4]; /* State machine */ if(bootp_app_init == 0) return; switch(bootp.state) { case BOOTPC_STATE_ENABLED: /* Check the IP address of the device and start BOOTP procedure for getting */ /* one if zero or 255.255.255.255 */ if( (localmachine.localip == 0) || (localmachine.localip == 0xFFFFFFFF) ) { /* We need to start BOOTP request procedure */ /* Firstly wait random time */ localmachine.localip = 0; localmachine.defgw = 0; localmachine.netmask = 0; init_timer(bootp.tmrhandle, ((UINT32)(localmachine.localHW[0]) << 2) + localmachine.localHW[1]); bootp.state = BOOTPC_STATE_REQUEST_NEEDED; } return; case BOOTPC_STATE_REQUEST_NEEDED: if( check_timer(bootp.tmrhandle) != 0 ) return; /* Send request */ if(localmachine.localip != 0){ bootp.state = BOOTPC_STATE_ENABLED; return; } i = 0; net_buf[UDP_APP_OFFSET + i++] = 0x01; net_buf[UDP_APP_OFFSET + i++] = 0x01; net_buf[UDP_APP_OFFSET + i++] = 0x06; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0xCA; net_buf[UDP_APP_OFFSET + i++] = 0x03; net_buf[UDP_APP_OFFSET + i++] = 0x32; net_buf[UDP_APP_OFFSET + i++] = 0xF1; net_buf[UDP_APP_OFFSET + i++] = (UINT8)(bootp.bootsecs >> 8); net_buf[UDP_APP_OFFSET + i++] = (UINT8)bootp.bootsecs; net_buf[UDP_APP_OFFSET + i++] = 0x80; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = 0x00; net_buf[UDP_APP_OFFSET + i++] = localmachine.localHW[5]; net_buf[UDP_APP_OFFSET + i++] = localmachine.localHW[4]; net_buf[UDP_APP_OFFSET + i++] = localmachine.localHW[3]; net_buf[UDP_APP_OFFSET + i++] = localmachine.localHW[2]; net_buf[UDP_APP_OFFSET + i++] = localmachine.localHW[1]; net_buf[UDP_APP_OFFSET + i++] = localmachine.localHW[0]; net_buf[UDP_APP_OFFSET + i++] = 99; net_buf[UDP_APP_OFFSET + i++] = 130; net_buf[UDP_APP_OFFSET + i++] = 83; net_buf[UDP_APP_OFFSET + i++] = 99; net_buf[UDP_APP_OFFSET + i++] = 255; for( ;i<300;i++) net_buf[UDP_APP_OFFSET + i] = 0; /* Send it */ udp_send(bootp.sochandle, IP_BROADCAST_ADDRESS, BOOTP_SERVERPORT, &net_buf[UDP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - UDP_APP_OFFSET, 300); init_timer(bootp.tmrhandle, BOOTP_RETRY_TOUT*TIMERTIC); bootp.bootsecs += BOOTP_RETRY_TOUT*TIMERTIC; bootp.state = BOOTPC_STATE_WAITING_REPLY; return; case BOOTPC_STATE_WAITING_REPLY: /* Wait untill timeout elapsed and try again */ if( check_timer(bootp.tmrhandle) != 0 ) return; bootp.state = BOOTPC_STATE_REQUEST_NEEDED; return; case BOOTPC_STATE_REPLY_GET: /* parameters configured. Inspect state of bootp.mode * and do something (if needed). Also turn BOOTP client * on/off. */ return; default: return; } }
int main(int argc, char **argv) { // These handles are only used when creating L3 and above packets. libnet_t *l; // the context libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers double cpu_time_used; reset(); if ( getopts(argc, argv) ) { (void) fprintf(stderr, " Invalid command line parameters!\n"); help(); } // Check whether hires timers are supported or not: (void) check_timer(); signal(SIGINT, signal_handler); // to close all file pointers etc upon SIGINT switch (mode) { case BYTE_STREAM: send_eth(); break; case ARP: (void) send_arp(); break; case BPDU: (void) send_bpdu(); break; case CDP: (void) send_cdp(); break; case IP: // From now on a new much more modular method is used: l = get_link_context(); t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP: tx.ip_proto = 1; l = get_link_context(); t4 = create_icmp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case ICMP6: tx.ip_proto = 58; l = get_link_context(); t4 = create_icmp6_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_ISUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case UDP: tx.ip_proto = 17; l = get_link_context(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_USUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case TCP: tx.ip_proto = 6; l = get_link_context(); t4 = create_tcp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (ipv6_mode) update_TSUM(l, t4); if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case DNS: tx.ip_proto = 17; l = get_link_context(); (void) create_dns_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RTP: tx.ip_proto = 17; l = get_link_context(); if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay); (void) create_rtp_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case RX_RTP: // Receive RTP packets rcv_rtp_init(); rcv_rtp(); break; case SYSLOG: tx.ip_proto = 17; l = get_link_context(); (void) create_syslog_packet(); t4 = create_udp_packet(l); // t4 can be used for later header changes t3 = create_ip_packet(l); // t3 can be used for later header changes if (!quiet) complexity(); if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes else send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly break; case LLDP: // start with a new concept here //l = get_link_context(); //(void) create_lldp_packet(); // // // printf("SIZE=%lu\n",sizeof(struct tx_struct)); fprintf(stderr, "LLDP is currently only supported via the interactive mode\n"); exit(1); break; default: (void) fprintf(stderr," mz/main: unknown mode! Stop.\n"); return (1); } if (!quiet) { mz_stop = clock(); cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC; if (cpu_time_used > 0) { total_d /= cpu_time_used; fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d); } else { fprintf(stderr, "\n"); } } return(0); }