int main (int argc, char *argv[]) #endif { int ret; SDL_Init(SDL_INIT_EVERYTHING); #ifdef IN_SDL_DT WCLOCK_nxt = 20000; #else WCLOCK_nxt = CEU_WCLOCK_NONE; #endif #if defined(CEU_WCLOCKS) || defined(CEU_ASYNCS) u32 old = SDL_GetTicks(); #endif if (ceu_go_init(&ret)) goto END; #ifdef IN_START if (ceu_go_event(&ret, IN_START, NULL)) goto END; #endif SDL_Event evt; for (;;) { #ifndef SDL_SIMUL s32 tm = -1; #ifdef CEU_WCLOCKS if (WCLOCK_nxt != CEU_WCLOCK_NONE) tm = WCLOCK_nxt / 1000; #endif #ifdef CEU_ASYNCS if (ASYNC_nxt) tm = 0; #endif int has = SDL_WaitEventTimeout(&evt, tm); //printf("...\n"); #if defined(CEU_WCLOCKS) || defined(CEU_ASYNCS) u32 now = SDL_GetTicks(); #endif // OTHER EVENTS if (has) { int s = 0; //printf("EVT: %d\n", evt.type); switch (evt.type) { #ifdef IN_SDL_QUIT case SDL_QUIT: s = ceu_go_event(&ret, IN_SDL_QUIT, NULL); break; #endif #ifdef IN_SDL_WINDOWEVENT case SDL_WINDOWEVENT: s = ceu_go_event(&ret, IN_SDL_WINDOWEVENT, &evt); break; #endif #ifdef IN_SDL_KEYDOWN case SDL_KEYDOWN: s = ceu_go_event(&ret, IN_SDL_KEYDOWN, &evt); break; #endif #ifdef IN_SDL_KEYUP case SDL_KEYUP: s = ceu_go_event(&ret, IN_SDL_KEYUP, &evt); break; #endif #ifdef IN_SDL_TEXTINPUT case SDL_TEXTINPUT: s = ceu_go_event(&ret, IN_SDL_TEXTINPUT, &evt); break; #endif #ifdef IN_SDL_TEXTEDITING case SDL_TEXTEDITING: s = ceu_go_event(&ret, IN_SDL_TEXTEDITING, &evt); break; #endif #ifdef IN_SDL_MOUSEMOTION case SDL_MOUSEMOTION: s = ceu_go_event(&ret, IN_SDL_MOUSEMOTION, &evt); break; #endif #ifdef IN_SDL_MOUSEBUTTONDOWN case SDL_MOUSEBUTTONDOWN: s = ceu_go_event(&ret, IN_SDL_MOUSEBUTTONDOWN, &evt); break; #endif #ifdef IN_SDL_MOUSEBUTTONUP case SDL_MOUSEBUTTONUP: s = ceu_go_event(&ret, IN_SDL_MOUSEBUTTONUP, &evt); break; #endif } if (s) goto END; } #if defined(CEU_WCLOCKS) || defined(CEU_ASYNCS) s32 dt = now - old; old = now; #endif #ifdef CEU_WCLOCKS if (WCLOCK_nxt != CEU_WCLOCK_NONE) { s32 nxt; int s = ceu_go_wclock(&ret, 1000*dt, &nxt); if (s) goto END; while (nxt <= 0) { s = ceu_go_wclock(&ret, 0, &nxt); if (s) goto END; } } #endif #ifdef IN_SDL_DT if (ceu_go_event(&ret, IN_SDL_DT, &dt)) goto END; #endif #ifdef IN_SDL_REDRAW if (ceu_go_event(&ret, IN_SDL_REDRAW, NULL)) goto END; #endif #endif // SDL_SIMUL #ifdef CEU_ASYNCS if (ASYNC_nxt) { if (ceu_go_async(&ret, NULL)) goto END; } #endif } END: SDL_Quit(); // TODO: slow return ret; }
static int coap_receive(void) { coap_error_code = NO_ERROR; PRINTF("handle_incoming_data(): received uip_datalen=%u \n", (uint16_t)uip_datalen()); if (uip_newdata()) { PRINTF("receiving UDP datagram from: "); PRINT6ADDR(&UIP_IP_BUF->srcipaddr); PRINTF(":%u\n Length: %u\n Data: ", uip_ntohs(UIP_UDP_BUF->srcport), uip_datalen() ); PRINTBITS(uip_appdata, uip_datalen()); PRINTF("\n"); coap_error_code = coap_parse_message(message, uip_appdata, uip_datalen()); if (coap_error_code==NO_ERROR) { /*TODO duplicates suppression, if required by application */ PRINTF(" Parsed: v %u, t %u, tkl %u, c %u, mid %u\n", message->version, message->type, message->token_len, message->code, message->mid); PRINTF(" URL: %.*s\n", message->uri_path_len, message->uri_path); PRINTF(" Payload: %.*s\n", message->payload_len, message->payload); /* Handle requests. */ if (message->code >= COAP_GET && message->code <= COAP_DELETE) { #if COAP_CEU int ret; TCEU_Transaction t = { message->mid, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, request1, NULL, NULL, NULL, 0, {} }; tceu__int___void_ ps = { &ret, &t }; ceu_go_event(CEU_IN_COAP_REQUEST, &ps); if (! ret) { coap_error_code = SERVICE_UNAVAILABLE_5_03; coap_error_message = "NoFreeTraBuffer"; } #else /* Use transaction buffer for response to confirmable request. */ if ( (transaction = coap_new_transaction(message->mid, &UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport)) ) { request1(transaction,NULL); } else { coap_error_code = SERVICE_UNAVAILABLE_5_03; coap_error_message = "NoFreeTraBuffer"; } /* if (transaction buffer) */ #endif } else { /* Responses */ if (message->type==COAP_TYPE_CON && message->code==0) { PRINTF("Received Ping\n"); coap_error_code = PING_RESPONSE; } else if (message->type==COAP_TYPE_ACK) { /* Transactions are closed through lookup below */ PRINTF("Received ACK\n"); } else if (message->type==COAP_TYPE_RST) { PRINTF("Received RST\n"); /* Cancel possible subscriptions. */ #ifdef COAP_OBSERVER coap_remove_observer_by_mid(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, message->mid); #endif } #if COAP_CEU ceu_go_event(CEU_IN_COAP_RESPONSE, message); #else if ( (transaction = coap_get_transaction_by_mid(message->mid)) ) { /* Free transaction memory before callback, as it may create a new transaction. */ restful_response_handler callback = transaction->callback; void *callback_data = transaction->callback_data; coap_clear_transaction(transaction); /* Check if someone registered for the response */ if (callback) { callback(callback_data, message); } } /* if (ACKed transaction) */ transaction = NULL; #endif } /* Request or Response */ } /* if (parsed correctly) */ if (coap_error_code==NO_ERROR) { #if ! COAP_CEU if (transaction) coap_send_transaction(transaction); #endif } else if (coap_error_code==MANUAL_RESPONSE) /* TODO! */ { PRINTF("Clearing transaction for manual response"); #if ! COAP_CEU coap_clear_transaction(transaction); #endif } else { coap_message_type_t reply_type = COAP_TYPE_ACK; PRINTF("ERROR %u: %s\n", coap_error_code, coap_error_message); #if ! COAP_CEU coap_clear_transaction(transaction); #endif /* Set to sendable error code. */ if (coap_error_code >= 192) { coap_error_code = INTERNAL_SERVER_ERROR_5_00; } if (coap_error_code == PING_RESPONSE) { coap_error_code = 0; reply_type = COAP_TYPE_RST; } /* Reuse input buffer for error message. */ coap_init_message(message, reply_type, coap_error_code, message->mid); coap_set_payload(message, coap_error_message, strlen(coap_error_message)); coap_send_message(&UIP_IP_BUF->srcipaddr, UIP_UDP_BUF->srcport, uip_appdata, coap_serialize_message(message, uip_appdata)); } } /* if (new data) */ return coap_error_code; }