/* * Copyright (C) 2015 Freie Universität Berlin * * This file is subject to the terms and conditions of the GNU Lesser * General Public License v2.1. See the file LICENSE in the top level * directory for more details. */ /** * @ingroup examples * @{ * * @file * @brief Example application for demonstrating the RIOT network stack * * @author Hauke Petersen * * @} */ #include #include #include #include #include #include #include #include "periph/gpio.h" #include "board.h" #include "periph_conf.h" #include "net/sock/udp.h" #include "net/gnrc/ipv6.h" #include "net/af.h" #include "net/sixlowpan.h" #include "net/gnrc/pktdump.h" #include "shell.h" #include "shell_commands.h" #include "msg.h" #include "thread.h" #include "sched.h" #include "thread.h" #include "kernel_types.h" #include "net/netstats.h" #include "net/ipv6/addr.h" #include "periph/timer.h" #include "net/gnrc/ipv6/netif.h" #include "net/gnrc/netif.h" #include "net/gnrc/netapi.h" #include "net/netopt.h" #include "net/gnrc/pkt.h" #include "net/gnrc/pktbuf.h" #include "net/gnrc/netif/hdr.h" #include "net/gnrc/sixlowpan/netif.h" #include "net/fib.h" #include "net/gnrc/udp.h" #include "periph/pwm.h" #include "od.h" #include "net/sntp.h" #include "net/ntp_packet.h" #include "net/gnrc/rpl.h" #include "net/gnrc/rpl/structs.h" #include "net/gnrc/rpl/dodag.h" #include "utlist.h" #include "trickle.h" #ifdef MODULE_SCHEDSTATISTICS #include "xtimer.h" #endif #ifdef MODULE_TLSF #include "tlsf.h" #endif #define MAIN_QUEUE_SIZE (8) /** * @brief The maximal expected link layer address length in byte */ #define MAX_ADDR_LEN (8U) /** * @brief The default IPv6 prefix length if not specified. */ #define SC_NETIF_IPV6_DEFAULT_PREFIX_LEN (64) #define _STACKSIZE (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF) #define MSG_TYPE_ISR (0x3456) #define PWM_FREQ 1000 #define PWM_RES 1000 #define DEADLINE 30000 #define SEC_IN_USEC (1000000) static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; char sock_server_stack[THREAD_STACKSIZE_MAIN]; char sock_client_stack[THREAD_STACKSIZE_MAIN]; char sock_time_server_stack[THREAD_STACKSIZE_MAIN]; kernel_pid_t server, client, time_server; int ordre = 0; int64_t offset = 0; int timer_run = 0; int tourne = 0; int arret = 0; sock_udp_ep_t local = SOCK_IPV6_EP_ANY; sock_udp_t sock; sock_udp_ep_t local_ntp = SOCK_IPV6_EP_ANY; sock_udp_t sock_ntp; static ntp_packet_t sntp_packet; typedef struct tableau { uint32_t heure_actuelle; char donnees[2]; }Data; /***************** RPL functions *****************/ int crea_rpl_dodag_root(uint8_t instance_id, ipv6_addr_t dodag_id) { gnrc_rpl_instance_t *inst = NULL; inst = gnrc_rpl_root_init(instance_id, &dodag_id, false, false); if (inst == NULL) { char addr_str[IPV6_ADDR_MAX_STR_LEN]; printf("error: could not add DODAG (%s) to instance (%d)\n", ipv6_addr_to_str(addr_str, &dodag_id, sizeof(addr_str)), instance_id); return 1; } printf("successfully added a new RPL DODAG\n"); return 0; } /***************** /RPL functions ****************/ void *sock_time_server_thread(void *arg) { (void) arg; local_ntp.port = NTP_PORT; if (sock_udp_create(&sock_ntp, &local_ntp, NULL, 0) < 0) { puts("Error creating UDP sock"); return NULL; } while (1) { sock_udp_ep_t remote; ssize_t res; if ((res = sock_udp_recv(&sock_ntp,&sntp_packet, sizeof(sntp_packet), SOCK_NO_TIMEOUT, &remote)) >= 0) { sntp_packet.receive.seconds=byteorder_htonl( xtimer_now_usec()); sntp_packet.origin.seconds=sntp_packet.transmit.seconds; sntp_packet.transmit.seconds=byteorder_htonl( xtimer_now_usec()); if (sock_udp_send(&sock_ntp, &sntp_packet, sizeof(sntp_packet), &remote) < 0) { puts("Error sending reply"); } } } return NULL; } void *sock_server_thread(void *arg) { (void) arg; Data buf; int compteur = 5; int deadline; local.port = 1234; int interruption_msg = 0; int sync = 0; sock_udp_ep_t server = { .port = NTP_PORT, .family = AF_INET6 }; ipv6_addr_from_str((ipv6_addr_t *)&server.addr, "baad:a555::1702"); if (sock_udp_create(&sock, &local, NULL, 0) < 0) { puts("Error creating UDP sock"); return NULL; } while (1) { sock_udp_ep_t remote; ssize_t res; if ((res = sock_udp_recv(&sock, &buf, sizeof(buf), 5 * SEC_IN_USEC ,&remote)) >= 0) { deadline = xtimer_now_usec() + offset - buf.heure_actuelle; printf("tps de transmission : %i\n",deadline); if(buf.donnees[0] == 'g' && buf.donnees[1] == 'o') { if(sync == 0) { while(sntp_sync(&server, 5000000) < 0) { puts("Erreur dans la synchronisation - Nouvelle tentative :"); } offset = sntp_get_offset(); sync = 1; printf("offset : %i\n",(int)offset); } if(interruption_msg == 1) { puts("reprise de la communication - tentative de synchronisation :"); while(sntp_sync(&server, 5000000) < 0) { puts("Erreur dans la synchronisation - Nouvelle tentative :"); } offset = sntp_get_offset(); printf("offset : %i\n",(int)offset); interruption_msg = 0; } if(deadline >= DEADLINE && compteur >=5) { compteur = 0; if(arret == 0) { if (sock_udp_send(&sock, "d", sizeof("d"), &remote) < 0) { puts("Error sending reply"); } } if(tourne == 1) { pwm_set(PWM_DEV(0),1,987); pwm_set(PWM_DEV(1),1,960); } if(timer_run == 0) { puts("Degradation"); gpio_clear(LED1_PIN); gpio_clear(LED2_PIN); gpio_set(LED0_PIN); timer_set(TIMER_DEV(1),0,25200); timer_run = 1; } } if(deadline >= DEADLINE && compteur < 5) { compteur = 0; if (sock_udp_send(&sock, "d", sizeof("d"), &remote) < 0) { puts("Error sending reply"); } } if(deadline<=DEADLINE && compteur >=5) { pwm_set(PWM_DEV(0),1,992); pwm_set(PWM_DEV(1),1,658); tourne = 1; timer_run=0; timer_clear(TIMER_DEV(1),0); if (sock_udp_send(&sock, "n", sizeof("n"), &remote) < 0) { puts("Error sending reply"); } arret = 0; gpio_clear(LED2_PIN); gpio_clear(LED0_PIN); gpio_set(LED1_PIN); } if(deadline<=DEADLINE && compteur <5) { compteur++; } printf("compteur : %d\n",compteur); memset(&buf,0,sizeof(buf)); if(arret == 1) { if (sock_udp_send(&sock, "a", sizeof("a"), &remote) < 0) { puts("Error sending reply"); } } } } else { puts("msg non recu"); if(timer_run == 0 && interruption_msg == 0) { timer_run = 1; timer_set(TIMER_DEV(1),0,25200); } interruption_msg = 1; if(tourne == 1) { pwm_set(PWM_DEV(0),1,987); pwm_set(PWM_DEV(1),1,960); gpio_clear(LED1_PIN); gpio_clear(LED2_PIN); gpio_set(LED0_PIN); } } } return NULL; } void *sock_client_thread(void *arg) { (void) arg; Data data; int vitesse=2; uint8_t buf[3]; ssize_t res; data.donnees[0] = 'g'; data.donnees[1] = 'o'; sock_udp_ep_t remote = { .family = AF_INET6 }; remote.port = 1234; remote.addr.ipv6[0] = 0xba; remote.addr.ipv6[1] = 0xad; remote.addr.ipv6[2] = 0xa5; remote.addr.ipv6[3] = 0x55; remote.addr.ipv6[14] = 0x17; remote.addr.ipv6[15] = 0x36; while (1) { data.heure_actuelle = xtimer_now_usec(); if (sock_udp_send(&sock, &data, sizeof(data), &remote) < 0) { puts("Error sending message"); } if ((res = sock_udp_recv(&sock, buf, sizeof(buf), 0.05 * SEC_IN_USEC,NULL)) < 0) { if (res == -ETIMEDOUT) { vitesse = 1; } else { puts("Error receiving message"); } } else { if(buf[0] == 'n') vitesse = 2; else if(buf[0] == 'a') { vitesse = 1; gpio_set(LED2_PIN); } else vitesse = 1; } if(vitesse==2) { gpio_clear(LED0_PIN); gpio_clear(LED2_PIN); gpio_toggle(LED1_PIN); } else { gpio_clear(LED1_PIN); gpio_toggle(LED0_PIN); } xtimer_sleep(vitesse); } return NULL; } static void arret_urgence(void *arg,int channel) { pwm_set(PWM_DEV(0),1,0); pwm_set(PWM_DEV(1),1,0); tourne = 0; timer_run = 0; arret = 1; gpio_clear(LED0_PIN); gpio_clear(LED1_PIN); gpio_set(LED2_PIN); puts("Arret d'urgence"); } static void _init_timer(void) { timer_init(TIMER_DEV(1), CLOCK_CORECLOCK/2 ,&arret_urgence,NULL); //timer_irq_enable(TIMER_DEV(1)); } static void _init_pwm(void) { pwm_init(PWM_DEV(0), PWM_LEFT, PWM_FREQ, PWM_RES); pwm_set(PWM_DEV(0),1,0); pwm_init(PWM_DEV(1), PWM_LEFT, PWM_FREQ, PWM_RES); pwm_set(PWM_DEV(1),1,0); } static void _init_interface(void) { kernel_pid_t ifs[GNRC_NETIF_NUMOF]; ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED; ipv6_addr_t tmp_addr= IPV6_ADDR_UNSPECIFIED; uint8_t hwaddr[MAX_ADDR_LEN]; int res; gnrc_netif_get(ifs); //addresses gobales addr.u8[0] = 0xba; addr.u8[1] = 0xad; addr.u8[2] = 0xa5; addr.u8[3] = 0x55; res = gnrc_netapi_get(ifs[0], NETOPT_ADDRESS, 0, hwaddr, sizeof(hwaddr)); if (res >= 0) { addr.u8[14] = *hwaddr; addr.u8[15] = *(hwaddr+1); } memcpy(tmp_addr.u8,addr.u8,IPV6_ADDR_BIT_LEN); gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST); /* model ipv6 addr: dead:beef::Hwaddr */ if((addr.u8[14]==0x17)&&(addr.u8[15]==0x02)){ crea_rpl_dodag_root(1, addr); client=thread_create(sock_client_stack,sizeof(sock_client_stack),8,THREAD_CREATE_STACKTEST | THREAD_CREATE_WOUT_YIELD,sock_client_thread,NULL,"sock_client_thread"); time_server=thread_create(sock_time_server_stack,sizeof(sock_time_server_stack),6,THREAD_CREATE_STACKTEST,sock_time_server_thread,NULL,"sock_time_server_thread"); }else if((addr.u8[14]==0x17)&&(addr.u8[15]==0x36)){ _init_timer(); _init_pwm(); xtimer_sleep(2); server=thread_create(sock_server_stack,sizeof(sock_server_stack),6,THREAD_CREATE_STACKTEST,sock_server_thread,NULL,"sock_server_thread"); }else{ puts("new node ?"); } } static const shell_command_t shell_commands[] = { { NULL, NULL, NULL } }; int main(void) { msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); puts("RIOT network stack example application"); _init_interface(); /* start shell */ puts("All up, running the shell now"); char line_buf[SHELL_DEFAULT_BUFSIZE]; shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); /* should be never reached */ return 0; }