/* * 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 "../../boards/stm32f4discovery/include/board.h" #include "../../boards/stm32f4discovery/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" #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 100 #define PWM_RES 100 // addr ipv6 link local node 1: fe80::3734:510e:3317:3402 uint8_t node1[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x37,0x34,0x51,0x0e,0x33,0x17,0x34,0x02}; // addr ipv6 link local node 2: fe80::3634:5110:3473:3762 uint8_t node2[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x34,0x51,0x10,0x34,0x73,0x37,0x62}; //addr ipv6 link local node 3: fe80::3634:5110:3471:3766 uint8_t node3[16]={0xfe,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x36,0x34,0x51,0x10,0x34,0x71,0x37,0x66}; //static char _stack_server[GNRC_PKTDUMP_STACKSIZE]; char pwm_stack[THREAD_STACKSIZE_MAIN]; char sock_server_stack[THREAD_STACKSIZE_MAIN]; char sock_client_stack[THREAD_STACKSIZE_MAIN]; // static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, // KERNEL_PID_UNDEF); // // kernel_pid_t gnrc_server_pid = KERNEL_PID_UNDEF; // int ordre = 0; uint8_t buf[128]; sock_udp_ep_t local = SOCK_IPV6_EP_ANY; sock_udp_t sock; // // static void _dump_snip(gnrc_pktsnip_t *pkt) // { // switch (pkt->type) { // case GNRC_NETTYPE_UNDEF: // printf("NETTYPE_UNDEF (%i)\n", pkt->type); // od_hex_dump(pkt->data, pkt->size, OD_WIDTH_DEFAULT); // printf("wesh magueule\n"); // /*if(strcmp((char *)pkt->data,"go")==0) // { // //pwm_set(PWM_DEV(0),1,25); // ordre = 1; // timer_set(XTIMER_DEV,0,8400); // timer_clear(TIMER_DEV(1),0); // } // else // { // //pwm_set(PWM_DEV(0),1,0); // ordre = 0; // }*/ // break; // #ifdef MODULE_GNRC_NETIF // case GNRC_NETTYPE_NETIF: // printf("NETTYPE_NETIF (%i)\n", pkt->type); // gnrc_netif_hdr_print(pkt->data); // break; // #endif // #ifdef MODULE_GNRC_SIXLOWPAN // case GNRC_NETTYPE_SIXLOWPAN: // printf("NETTYPE_SIXLOWPAN (%i)\n", pkt->type); // sixlowpan_print(pkt->data, pkt->size); // break; // #endif // #ifdef MODULE_GNRC_IPV6 // case GNRC_NETTYPE_IPV6: // printf("NETTYPE_IPV6 (%i)\n", pkt->type); // ipv6_hdr_print(pkt->data); // break; // #endif // #ifdef MODULE_GNRC_ICMPV6 // case GNRC_NETTYPE_ICMPV6: // printf("NETTYPE_ICMPV6 (%i)\n", pkt->type); // break; // #endif // #ifdef MODULE_GNRC_TCP // case GNRC_NETTYPE_TCP: // printf("NETTYPE_TCP (%i)\n", pkt->type); // break; // #endif // #ifdef MODULE_GNRC_UDP // case GNRC_NETTYPE_UDP: // printf("NETTYPE_UDP (%i)\n", pkt->type); // udp_hdr_print(pkt->data); // break; // #endif // #ifdef TEST_SUITES // case GNRC_NETTYPE_TEST: // printf("NETTYPE_TEST (%i)\n", pkt->type); // od_hex_dump(pkt->data, pkt->size, OD_WIDTH_DEFAULT); // break; // #endif // default: // printf("NETTYPE_UNKNOWN (%i)\n", pkt->type); // od_hex_dump(pkt->data, pkt->size, OD_WIDTH_DEFAULT); // break; // } // } // // static void _dump(gnrc_pktsnip_t *pkt) // { // int snips = 0; // int size = 0; // gnrc_pktsnip_t *snip = pkt; // // while (snip != NULL) { // printf("~~ SNIP %2i - size: %3u byte, type: ", snips, // (unsigned int)snip->size); // _dump_snip(snip); // ++snips; // size += snip->size; // snip = snip->next; // } // // printf("~~ PKT - %2i snips, total size: %3i byte\n", snips, size); // gnrc_pktbuf_release(pkt); // } // // // static void *_serverloop(void *arg) // { // (void)arg; // msg_t msg, reply; // msg_t msg_queue[GNRC_PKTDUMP_MSG_QUEUE_SIZE]; // // /* setup the message queue */ // msg_init_queue(msg_queue, GNRC_PKTDUMP_MSG_QUEUE_SIZE); // // reply.content.value = (uint32_t)(-ENOTSUP); // reply.type = GNRC_NETAPI_MSG_TYPE_ACK; // // while (1) { // msg_receive(&msg); // // switch (msg.type) { // case GNRC_NETAPI_MSG_TYPE_RCV: // puts("PKTDUMP: data received:"); // _dump(msg.content.ptr); // break; // case GNRC_NETAPI_MSG_TYPE_SND: // puts("PKTDUMP: data to send:"); // _dump(msg.content.ptr); // break; // case GNRC_NETAPI_MSG_TYPE_GET: // case GNRC_NETAPI_MSG_TYPE_SET: // msg_reply(&msg, &reply); // break; // default: // puts("PKTDUMP: received something unexpected"); // break; // } // } // // /* never reached */ // return NULL; // } void *pwm_thread(void *arg) { (void) arg; int tourne = 0; while(1) { if(ordre==1) { pwm_set(PWM_DEV(0),1,45); tourne = 1; } else if(ordre==0) { pwm_set(PWM_DEV(0),1,0); tourne = 0; } else if(ordre==2 && tourne == 1) pwm_set(PWM_DEV(0),1,31); } return NULL; } void *sock_server_thread(void *arg) { (void) arg; local.port = 1234; if (sock_udp_create(&sock, &local, NULL, 0) < 0) { puts("Error creating UDP sock"); return NULL; } //sntp_sync(&local,5000); //printf("temps : %i\n",(int)sntp_get_offset()); while (1) { sock_udp_ep_t remote; ssize_t res; if ((res = sock_udp_recv(&sock, buf, sizeof(buf), SOCK_NO_TIMEOUT, &remote)) >= 0) { puts("Received a message"); printf("%s\n",buf); if (sock_udp_send(&sock, buf, res, &remote) < 0) { puts("Error sending reply"); } if(strcmp((char *)buf,"go")==0) { ordre = 1; timer_set(XTIMER_DEV,0,8400); timer_clear(TIMER_DEV(1),0); } else { ordre = 0; } } } return NULL; } void *sock_client_thread(void *arg) { (void) arg; sock_udp_ep_t remote = { .family = AF_INET6 }; remote.port = 1234; remote.addr.ipv6[0] = 0xde; remote.addr.ipv6[1] = 0xad; remote.addr.ipv6[2] = 0xbe; remote.addr.ipv6[3] = 0xef; remote.addr.ipv6[14] = 0x37; remote.addr.ipv6[15] = 0x66; // memcpy(remote.addr.ipv6,addr.u8,IPV6_ADDR_BIT_LEN); while (1) { // //ipv6_addr_set_all_nodes_multicast((ipv6_addr_t *)&remote.addr.ipv6, // // IPV6_ADDR_MCAST_SCP_LINK_LOCAL); if (sock_udp_send(NULL, "go", sizeof("go"), &remote) < 0) { puts("Error sending message"); } xtimer_sleep(1); } return NULL; } static void arret_urgence(void *arg,int channel) { //pwm_set(PWM_DEV(0),1,0); ordre=0; printf("Arret d'urgence\n"); } static void degradation(void *arg,int channel) { ordre=2; //pwm_set(PWM_DEV(0),1,0); printf("Ralentissement\n"); timer_set(TIMER_DEV(1),0,25200); } static void _init_timer(void) { printf("ok timer\n"); timer_init(XTIMER_DEV, CLOCK_CORECLOCK/2 ,°radation,NULL); timer_set(XTIMER_DEV, 0, 8400); timer_irq_enable(XTIMER_DEV); 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); thread_create(pwm_stack,sizeof(pwm_stack),8,THREAD_CREATE_STACKTEST,pwm_thread,NULL,"pwm_thread"); } // static void start_server(char *port_str) // { // uint16_t port; // // /* check if server is already running */ // if (server.target.pid != KERNEL_PID_UNDEF) { // printf("Error: server already running on port %" PRIu32 "\n", // server.demux_ctx); // return; // } // /* parse port */ // port = (uint16_t)atoi(port_str); // if (port == 0) { // puts("Error: invalid port specified"); // return; // } // /* start server (which means registering pktdump for the chosen port) */ // server.target.pid = gnrc_server_pid; // server.demux_ctx = (uint32_t)port; // gnrc_netreg_register(GNRC_NETTYPE_UDP, &server); // printf("Success: started UDP server on port %" PRIu16 "\n", port); // } 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] = 0xde; addr.u8[1] = 0xad; addr.u8[2] = 0xbe; addr.u8[3] = 0xef; 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]==0x34)&&(addr.u8[15]==0x02)){ tmp_addr.u8[14] = 0x37; tmp_addr.u8[15] = 0x66; //fibroute dest: dead:beef::3766 via fe80::3634:5110:3473:3762 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); tmp_addr.u8[14] = 0x37; tmp_addr.u8[15] = 0x62; //fibroute dest: dead:beef::3762 via fe80::3634:5110:3473:3762 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); thread_create(sock_client_stack,sizeof(sock_client_stack),8,THREAD_CREATE_STACKTEST,sock_client_thread,NULL,"sock_client_thread"); }else if((addr.u8[14]==0x37)&&(addr.u8[15]==0x62)){ tmp_addr.u8[14] = 0x37; tmp_addr.u8[15] = 0x66; //fibroute dest: dead:beef::3766 via fe80::3634:5110:3471:3766 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node3, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); tmp_addr.u8[14] = 0x34; tmp_addr.u8[15] = 0x02; //fibroute dest: dead:beef::3402 via fe80::3734:510e:3317:3402 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node1, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); //sntp_sync(&local,5000); //printf("temps : %i\n",(int)sntp_get_offset()); }else if((addr.u8[14]==0x37)&&(addr.u8[15]==0x66)){ tmp_addr.u8[14] = 0x34; tmp_addr.u8[15] = 0x02; //fibroute dest: dead:beef::3402 via fe80::3634:5110:3473:3762 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); tmp_addr.u8[14] = 0x37; tmp_addr.u8[15] = 0x62; //fibroute dest: dead:beef::3762 via fe80::3634:5110:3473:3762 fib_add_entry(&gnrc_ipv6_fib_table, ifs[0],tmp_addr.u8, IN6ADDRSZ, 0,node2, IN6ADDRSZ, 0, FIB_LIFETIME_NO_EXPIRE); //start_server("1234"); thread_create(sock_server_stack,sizeof(sock_server_stack),5,THREAD_CREATE_STACKTEST,sock_server_thread,NULL,"sock_server_thread"); _init_pwm(); _init_timer(); }else{ puts("new node ?"); } } static msg_t _main_msg_queue[MAIN_QUEUE_SIZE]; extern int udp_cmd(int argc, char **argv); static const shell_command_t shell_commands[] = { { "udp", "send data over UDP and listen on UDP ports", udp_cmd }, { NULL, NULL, NULL } }; int main(void) { msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE); puts("RIOT network stack example application"); // if (gnrc_server_pid == KERNEL_PID_UNDEF) { // gnrc_server_pid = thread_create(_stack_server, sizeof(_stack_server), GNRC_PKTDUMP_PRIO, // THREAD_CREATE_STACKTEST, // _serverloop, NULL, "serverloop"); // } _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; }