Blame view

RIOT/examples/network_app_sntp/udp.c 5.04 KB
f8d83614   elopes   new app sntp serv...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  /*
   * 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       Demonstrating the sending and receiving of UDP data
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  #include <inttypes.h>
  
  #include "net/gnrc.h"
  #include "net/gnrc/ipv6.h"
  #include "net/gnrc/udp.h"
  #include "net/gnrc/pktdump.h"
  #include "timex.h"
  #include "xtimer.h"
  
  //static gnrc_netreg_entry_t server = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
    //                                                             KERNEL_PID_UNDEF);
  
  
  static void send(char *addr_str, char *port_str, char *data, unsigned int num,
                   unsigned int delay)
  {
      uint16_t port;
      ipv6_addr_t addr;
  
      /* parse destination address */
      if (ipv6_addr_from_str(&addr, addr_str) == NULL) {
          puts("Error: unable to parse destination address");
          return;
      }
      /* parse port */
      port = (uint16_t)atoi(port_str);
      if (port == 0) {
          puts("Error: unable to parse destination port");
          return;
      }
  
      for (unsigned int i = 0; i < num; i++) {
          gnrc_pktsnip_t *payload, *udp, *ip;
          unsigned payload_size;
          /* allocate payload */
          payload = gnrc_pktbuf_add(NULL, data, strlen(data), GNRC_NETTYPE_UNDEF);
          if (payload == NULL) {
              puts("Error: unable to copy data to packet buffer");
              return;
          }
          /* store size for output */
          payload_size = (unsigned)payload->size;
          /* allocate UDP header, set source port := destination port */
          udp = gnrc_udp_hdr_build(payload, port, port);
          if (udp == NULL) {
              puts("Error: unable to allocate UDP header");
              gnrc_pktbuf_release(payload);
              return;
          }
          /* allocate IPv6 header */
          ip = gnrc_ipv6_hdr_build(udp, NULL, &addr);
          if (ip == NULL) {
              puts("Error: unable to allocate IPv6 header");
              gnrc_pktbuf_release(udp);
              return;
          }
          /* send packet */
          if (!gnrc_netapi_dispatch_send(GNRC_NETTYPE_UDP, GNRC_NETREG_DEMUX_CTX_ALL, ip)) {
              puts("Error: unable to locate UDP thread");
              gnrc_pktbuf_release(ip);
              return;
          }
          /* access to `payload` was implicitly given up with the send operation above
           * => use temporary variable for output */
          printf("Success: send %u byte to [%s]:%u\n", payload_size, addr_str,
                 port);
          xtimer_usleep(delay);
      }
  }
  
  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_pktdump_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 stop_server(void)
  {
  //     /* check if server is running at all */
  //     if (server.target.pid == KERNEL_PID_UNDEF) {
  //         printf("Error: server was not running\n");
  //         return;
  //     }
  //     /* stop server */
  //     gnrc_netreg_unregister(GNRC_NETTYPE_UDP, &server);
  //     server.target.pid = KERNEL_PID_UNDEF;
  //     puts("Success: stopped UDP server");
  }
  
  int udp_cmd(int argc, char **argv)
  {
      if (argc < 2) {
          printf("usage: %s [send|server]\n", argv[0]);
          return 1;
      }
  
      if (strcmp(argv[1], "send") == 0) {
          uint32_t num = 1;
          uint32_t delay = 1000000;
          if (argc < 5) {
              printf("usage: %s send <addr> <port> <data> [<num> [<delay in us>]]\n",
                     argv[0]);
              return 1;
          }
          if (argc > 5) {
              num = (uint32_t)atoi(argv[5]);
          }
          if (argc > 6) {
              delay = (uint32_t)atoi(argv[6]);
          }
          send(argv[2], argv[3], argv[4], num, delay);
      }
      else if (strcmp(argv[1], "server") == 0) {
          if (argc < 3) {
              printf("usage: %s server [start|stop]\n", argv[0]);
              return 1;
          }
          if (strcmp(argv[2], "start") == 0) {
              if (argc < 4) {
                  printf("usage %s server start <port>\n", argv[0]);
                  return 1;
              }
              start_server(argv[3]);
          }
          else if (strcmp(argv[2], "stop") == 0) {
              stop_server();
          }
          else {
              puts("error: invalid command");
          }
      }
      else {
          puts("error: invalid command");
      }
      return 0;
  }