uint64_t rough_local_tick_count() { static atomic<uint32_t> h_time(0); static atomic<uint32_t> l_time(time_get_time()); uint32_t now =time_get_time(); uint32_t ltime=l_time; uint32_t htime=h_time; uint64_t lastTime64= ((((uint64_t)htime)<<32)|ltime); uint32_t diff=now-ltime; lastTime64+=diff; if (ltime>now||diff>0x7fffffffU) { h_time=(lastTime64>>32); l_time=now; ltime=l_time; htime=h_time; }
return ((uint64_t(ts.tv_sec) * 1000ULL + uint64_t(ts.tv_nsec)/1000000ULL))TIME_INACCURACY_TEST; } # endif #endif #if (defined(_WIN32) || defined(WIN32)) # include <MMSystem.h> # pragma comment(lib, "winmm.lib") inline uint32_t time_get_time() { return timeGetTime()TIME_INACCURACY_TEST; } static uint64_t s_diff_precise__=precise_local_tick_count()-time_get_time(); uint64_t rough_local_tick_count() { static atomic<uint32_t> h_time(0); static atomic<uint32_t> l_time(time_get_time()); uint32_t now =time_get_time(); uint32_t ltime=l_time; uint32_t htime=h_time; uint64_t lastTime64= ((((uint64_t)htime)<<32)|ltime); uint32_t diff=now-ltime; lastTime64+=diff; if (ltime>now||diff>0x7fffffffU) { h_time=(lastTime64>>32); l_time=now;
int main( void ){ DDRC = 0xDB; //PORTC = 0x7E;; PORTC = 0xE7; uint8_t error; Packet packet; Queue* packets; packets = Packets_getQueue(); // initialize globals globals_init(); // initialize Time time_init(); TimeResult tr; uint32_t previous = 0; uint32_t watchdog = 0; // initialize usart usart_init(); // initialize motors motors_init(); motors_set(MOTORS_LEFT, 0); motors_set(MOTORS_RIGHT, 0); // initialize analog input analoginput_init(); // enable interrupts sei(); // loop forever while(1){ error = Packets_getError(); if (error){ Packets_sendError(ERROR_PACKETS, error); continue; } if (packets->count > 0){ error = 0x00; packet = Packets_getNext(); error = Queue_getError(packets); if (error){ Packets_sendError(ERROR_QUEUE, error); continue; } handle_packet(packet); watchdog = time_get_time(); } tr = time_get_time_delta(previous); // every 20ms, calculate rpm if (get_time_in_ms(tr.delta) > 20){ previous = tr.previous; motors_tick(); send_status(); } tr = time_get_time_delta(watchdog); if (get_time_in_ms(tr.delta) > 500){ motors_set(MOTORS_LEFT, 0); motors_set(MOTORS_RIGHT, 0); } } }
/* hopefully obvious */ gchar *time_field_to_string (Track *track, T_item t_item) { return (time_time_to_string (time_get_time (track, t_item))); }
int init_socket_tcp(int port){ //IpV4 - TCP Socket int server_socket = socket(AF_INET, SOCK_STREAM, 0); int server_init_state = -1; if (server_socket == (-1)){ //Error while creating tcp socket perror("init_socket_tcp"); //Cancelling next operations return server_socket; } //Binding socket if ((server_init_state = bind_socket_tcp(server_socket, port)) == -1) //Cancelling next operations return server_init_state; //Listening if ((server_init_state = listen_socket_tcp(server_socket)) == -1) //Cancelling next operations return server_init_state; //LOG struct tm * timeinfo; timeinfo = time_get_time(); printf("[%d:%d:%d] Server started on port %d\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, port); //Accepting connections (Server loop process) if (!fork()){ //Init signals init_signals(); //Clients accept() loop while(1){ //Client information struct sockaddr_in client_addr; //Accepting client int client_socket = accept_socket_tcp(server_socket, &client_addr); if (client_socket != (-1)){ //LOG _client_idata client_information; get_client_information(&client_addr, &client_information); printf("Request from [%s:%d] !\n", client_information.client_ip, client_information.client_port); //New instance for client if(!fork()){ //Client pipe FILE * clnt_sd = fdopen(client_socket, "w+"); //Receive buffer char rcv_buff[SERVER_RECEIVE_BUFFER_SIZE]; //HTTP header http_request http_req; //Header check vars int header_valid = 0, url_valid = 0; //Reading client request fgets_or_exit(rcv_buff, sizeof(rcv_buff), clnt_sd); //Parsing http header_valid = parse_http_request(rcv_buff, &http_req); //Checking header header_valid = (((http_req.method == HTTP_GET) && (http_req.major_version == 1) && ((http_req.minor_version == 1) || (http_req.minor_version = 0))) ? 1 : 0); //Valid URL url_valid = ((strcmp(http_req.url,"/") == 0) ? 1 : 0); //Skipping headers skip_headers(clnt_sd); //Header validation if (http_req.method == HTTP_UNSUPPORTED) //Method not supported send_response (clnt_sd, 405, "Method Not Allowed", "Method Not Allowed\r\n"); else if ((header_valid == 1) && (url_valid == 1)) //Welcome message send_response(clnt_sd, 200 , "OK" , SERVER_WELCOME_MSG ); else if ((header_valid == 1) && (url_valid == 0)) //404 send_response(clnt_sd, 404,"Not Found", "Not Found\r\n"); else //Bad request send_response(clnt_sd, 400, "Bad Request" , "Bad request\r\n"); //Disconnected fclose(clnt_sd); //Client child-process ending exit(0); } //Closing client socket close(client_socket); } } } //Success return server_socket; }
void log_event(struct error * e, ...) { PGM_P txt_sev; uint16_t flags; va_list ap; time_h tv; if( e->severity > log_level) return; // save flags flags = stdout->flags; va_start(ap, e); tv = time_get_time(); // print timestamp printf_P(PSTR("%ld.%3.3ld | "), tv.s, (tv.us/1000UL)); // print severity switch(e->severity) { case ERROR_SEVERITY_EMERG : txt_sev = PSTR(ERROR_SEVTEXT_EMERG); break; case ERROR_SEVERITY_ERROR : txt_sev = PSTR(ERROR_SEVTEXT_ERROR); break; case ERROR_SEVERITY_WARNING : txt_sev = PSTR(ERROR_SEVTEXT_WARNING); break; case ERROR_SEVERITY_NOTICE : txt_sev = PSTR(ERROR_SEVTEXT_NOTICE); break; case ERROR_SEVERITY_DEBUG : txt_sev = PSTR(ERROR_SEVTEXT_DEBUG); break; default : txt_sev = PSTR("XXX"); break; } printf_P(txt_sev); printf_P(PSTR(": ")); // print message vfprintf_P(stdout,e->text,ap); printf_P(PSTR("\n")); va_end(ap); // restore flags stdout->flags = flags; // dead end if( (e->severity == ERROR_SEVERITY_ERROR) ||(e->severity == ERROR_SEVERITY_EMERG) ) { printf_P(PSTR("\nprogram stopped, strike a key other than 'x' to reset\n")); #if 0 //TODO:temp //XXX Add shutdown procedures here XXX // breaking motors motor_cs_break(1); // killing cs & position tasks scheduler_del_event(event_cs); // wait for key uint8_t key; while(1) { key = cli_getkey(); if( (key == -1)||(key == 'x')) continue; break; } #endif uart_recv(1); // reset MCU reset(); } }