/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; int port = 3000; /* Default to 3000 if not using service discovery. */ PROCESS_BEGIN(); PRINTF("UDP client process started\n"); #if UIP_CONF_ROUTER set_global_address(); #endif print_local_addresses(); static resolv_status_t status = RESOLV_STATUS_UNCACHED; while(status != RESOLV_STATUS_CACHED) { #if RESOLV_CONF_SUPPORTS_MDNS && RESOLV_CONF_SUPPORTS_DNS_SD status = set_connection_address(&ipaddr, &port); #else status = set_connection_address(&ipaddr, NULL); #endif if(status == RESOLV_STATUS_RESOLVING) { PROCESS_WAIT_EVENT_UNTIL(ev == resolv_event_found); } else if(status != RESOLV_STATUS_CACHED) { PRINTF("Can't get connection address.\n"); PROCESS_YIELD(); } } /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(port), NULL); udp_bind(client_conn, UIP_HTONS(port + 1)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); etimer_set(&et, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } else if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
void init_dtls(session_t *dst) { PRINTF("DTLS client started\n"); print_local_addresses(); dst->size = sizeof(dst->addr) + sizeof(dst->port); dst->port = UIP_HTONS(20220); set_connection_address(&dst->addr); client_conn = udp_new(&dst->addr, 0, NULL); udp_bind(client_conn, dst->port); PRINTF("set connection address to "); PRINT6ADDR(&dst->addr); PRINTF(":%d\n", uip_ntohs(dst->port)); set_log_level(LOG_DEBUG); dtls_context = dtls_new_context(client_conn); if (dtls_context) { dtls_set_psk(dtls_context, (unsigned char *)"secretPSK", 9, (unsigned char *)"Client_identity", 15); dtls_set_cb(dtls_context, read_from_peer, read); dtls_set_cb(dtls_context, send_to_peer, write); } }
void init_dtls(session_t *dst) { static dtls_handler_t cb = { .write = send_to_peer, .read = read_from_peer, .event = NULL, .get_key = get_key }; PRINTF("DTLS client started\n"); print_local_addresses(); dst->size = sizeof(dst->addr) + sizeof(dst->port); dst->port = UIP_HTONS(20220); set_connection_address(&dst->addr); client_conn = udp_new(&dst->addr, 0, NULL); udp_bind(client_conn, dst->port); PRINTF("set connection address to "); PRINT6ADDR(&dst->addr); PRINTF(":%d\n", uip_ntohs(dst->port)); set_log_level(LOG_DEBUG); dtls_context = dtls_new_context(client_conn); if (dtls_context) dtls_set_handler(dtls_context, &cb); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; PROCESS_BEGIN(); PRINTF("UDP client process started\n"); print_local_addresses(); set_connection_address(&ipaddr); /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); udp_bind(client_conn, UIP_HTONS(3001)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); etimer_set(&et, SEND_INTERVAL); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); } else if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et, wake_timer, periodic_timer; uip_ipaddr_t ipaddr; PROCESS_BEGIN(); PRINTF("UDP client process started\n"); #if UIP_CONF_ROUTER set_global_address(); #endif print_local_addresses(); set_connection_address(&ipaddr); /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); udp_bind(client_conn, UIP_HTONS(3001)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); etimer_set(&et, CLOCK_SECOND*10); PROCESS_WAIT_UNTIL(etimer_expired(&et)); // Wait for DAD and Router Discovery procedure to end. etimer_set(&et, SEND_INTERVAL); etimer_set(&wake_timer, AWAKE_INTERVAL); etimer_set(&periodic_timer, 1); while(1) { PROCESS_YIELD(); if(etimer_expired(&wake_timer)){ // if timer hasn't expired do not go in deep sleep, in order to receive a response. printf("Sleeping...\r\n"); sensorsPowerDown(); sleep_seconds(SLEEP_INTERVAL_SECONDS); // Put system in deep sleep mode for a while. sensorsPowerUp(); printf("Awake\r\n"); } if(etimer_expired(&et)) { timeout_handler(); etimer_restart(&et); etimer_restart(&wake_timer); } else if(ev == tcpip_event) { tcpip_handler(); } /* Make the process be called almost immediately, * so that it can force the system to go into deep sleep. */ etimer_restart(&periodic_timer); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(tcp_client_process, ev, data) { static struct etimer et; PROCESS_BEGIN(); PRINTF("TCP client process started\n"); process_start(&adjust_packet_length_process, 0); #if UIP_CONF_ROUTER set_global_address(); #endif print_local_addresses(); /* Fill buffer with test data */ for (int i = 0; i < MAX_PAYLOAD_LEN; i++) { buf[i] = i; } static resolv_status_t status = RESOLV_STATUS_UNCACHED; while(status != RESOLV_STATUS_CACHED) { status = set_connection_address(&ipaddr); if(status == RESOLV_STATUS_RESOLVING) { PROCESS_WAIT_EVENT_UNTIL(ev == resolv_event_found); } else if(status != RESOLV_STATUS_CACHED) { PRINTF("Can't get connection address.\n"); PROCESS_YIELD(); } } /* new connection with remote host */ tcp_socket_register(&socket, NULL, inputbuf, sizeof(inputbuf), outputbuf, sizeof(outputbuf), input, event); tcp_socket_connect(&socket, &ipaddr, SERVER_PORT); PRINTF("Connecting with the server..."); PRINT6ADDR(&ipaddr); PRINTF("\n"); while(1) { etimer_set(&et, send_interval); PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); } } PROCESS_END(); }
PROCESS_THREAD(dtls_client_test_process, ev, data) { PROCESS_BEGIN(); set_connection_address(&ipaddr); etimer_set(&et, CLOCK_CONF_SECOND*10); PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER); etimer_set(&et2, CLOSE_INTERVAL); DTLS_Connect(&ipaddr, 4433); while(1){ PROCESS_YIELD(); dtls_handler(ev, data); } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(ntpd_process, ev, data) { static struct etimer et; static uip_ipaddr_t ipaddr; PROCESS_BEGIN(); PRINTF("ntpd process started\n"); set_connection_address(&ipaddr); /* find the IP of router */ //etimer_set(&et, CLOCK_SECOND); //while(1){ // if(uip_ds6_defrt_choose()){ // uip_ipaddr_copy(&ipaddr, uip_ds6_defrt_choose()); // break; // } // etimer_set(&et, CLOCK_SECOND); // PROCESS_YIELD_UNTIL(etimer_expired(&et)); //} /* new connection with remote host */ ntp_conn = udp_new(&ipaddr, UIP_HTONS(NTPD_PORT), NULL); etimer_set(&et, SEND_INTERVAL * CLOCK_SECOND); while(1) { PROCESS_YIELD(); if(etimer_expired(&et)) { timeout_handler(); if((clock_seconds() > 4294967290U) || (clock_seconds() < 20)){ SEND_INTERVAL = 2 * CLOCK_SECOND; etimer_set(&et, SEND_INTERVAL); } else { if(SEND_INTERVAL <= 512 && (getCurrTime() != 0)) { SEND_INTERVAL = 2 * SEND_INTERVAL; } etimer_set(&et, SEND_INTERVAL * CLOCK_SECOND); } } else if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_client_process, ev, data) { static struct etimer et; uip_ipaddr_t ipaddr; PROCESS_BEGIN(); PRINTF("UDP client process started\n"); #if UIP_CONF_ROUTER set_global_address(); #endif print_local_addresses(); set_connection_address(&ipaddr); /* new connection with remote host */ SENSORS_ACTIVATE(button_sensor);//activate button //client_conn = udp_new(&ipaddr, UIP_HTONS(3000), NULL); simple_udp_register(&udp_connection, UDP_PORT, NULL, UDP_PORT, receiver); // udp_bind(client_conn, UIP_HTONS(3001)); //PRINTF("Created a connection with the server "); // PRINT6ADDR(&client_conn->ripaddr); // PRINTF(" local/remote port %u/%u\n", // UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); // etimer_set(&et, SEND_INTERVAL); while(1) { //PROCESS_YIELD(); PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event && data == &button_sensor); // if(etimer_expired(&et)) { timeout_handler(); // etimer_restart(&et); //} else if(ev == tcpip_event) { tcpip_handler(); } } PROCESS_END(); }
PROCESS_THREAD(default_app_process, ev, data) { uip_ipaddr_t ipaddr; PROCESS_BEGIN(); set_connection_address(&ipaddr); /* new connection with remote host */ client_conn = udp_new(&ipaddr, UIP_HTONS(3002), NULL); udp_bind(client_conn, UIP_HTONS(3003)); PRINTF("Created a connection with the server "); PRINT6ADDR(&client_conn->ripaddr); PRINTF(" local/remote port %u/%u\n", UIP_HTONS(client_conn->lport), UIP_HTONS(client_conn->rport)); SENSORS_ACTIVATE(acc_sensor); etimer_set(&timer, CLOCK_SECOND * 0.05); printf("Node configured as "); print_control(); printf("\n"); while (1) { PROCESS_YIELD(); if ( etimer_expired(&timer) ) { etimer_set(&timer, CLOCK_SECOND*0.05); x = acc_sensor.value(ACC_Y_RAW); if (x > 380) { //send udp_send_data(); etimer_set(&timer, CLOCK_SECOND*0.3); count++; } } } PROCESS_END(); }