Contiki 2.5
gateway.c
1 #include <stdio.h>
2 #include <sys/etimer.h>
3 #include <sys/process.h>
4 #include <sys/autostart.h>
5 #include <usb/usb-api.h>
6 #include <usb/cdc-acm.h>
7 #include <dev/leds.h>
8 #include <debug-uart.h>
9 
10 #include <net/uip-fw-drv.h>
11 #include <net/uip-over-mesh.h>
12 #include <dev/slip.h>
13 
14 #include "contiki-main.h"
15 
16 /* SLIP interface */
17 
18 extern struct uip_fw_netif cc2420if;
19 
20 rimeaddr_t node_addr = {{0,129}};
21 
22 static struct uip_fw_netif slipif =
23 {UIP_FW_NETIF(0,0,0,0, 255,255,255,255, slip_send)};
24 
25 /* USB buffers */
26 static unsigned char input_buffer[128];
27 static unsigned char output_buffer[128];
28 static unsigned char interrupt_buffer[16];
29 
30 #define DEV_TO_HOST 0x81
31 #define HOST_TO_DEV 0x02
32 
33 #define GATEWAY_TRICKLE_CHANNEL 8
34 void
35 slip_arch_init(unsigned long ubr)
36 {
37 }
38 
39 void
40 slip_arch_writeb(unsigned char c)
41 {
42  while(usb_send_data(DEV_TO_HOST, &c, 1) != 1);
43 }
44 
45 #if WITH_UIP
46 
47 static void
48 set_gateway(void)
49 {
50  struct gateway_msg msg;
51  /* Make this node the gateway node, unless it already is the
52  gateway. */
53  if(!is_gateway) {
54  leds_on(LEDS_RED);
55  printf("%d.%d: making myself the gateway\n",
57  uip_over_mesh_set_gateway(&rimeaddr_node_addr);
58  rimeaddr_copy(&(msg.gateway), &rimeaddr_node_addr);
59  rimebuf_copyfrom(&msg, sizeof(struct gateway_msg));
60  trickle_send(&gateway_trickle);
61  is_gateway = 1;
62  }
63 }
64 #endif /* WITH_UIP */
65 
66 PROCESS(gateway_process, "Gateway process");
67 
68 
69 PROCESS_THREAD(gateway_process, ev , data)
70 {
71  static struct etimer timer;
72  PROCESS_BEGIN();
73  usb_set_user_process(process_current);
74  usb_setup();
75  usb_cdc_acm_setup();
76 
77  uip_fw_default(&slipif);
78  uip_over_mesh_set_gateway_netif(&slipif);
79 
80  process_start(&slip_process, NULL);
81 
82  set_gateway();
83 
84  while(ev != PROCESS_EVENT_EXIT) {
86  if (ev == PROCESS_EVENT_TIMER) {
87  leds_toggle(LEDS_YELLOW);
88  /* printf("FIFOP: %d\n", FIFOP_IS_1); */
90  } else if (ev == PROCESS_EVENT_MSG) {
91  const struct usb_user_msg * const msg = data;
92  switch(msg->type) {
93  case USB_USER_MSG_TYPE_CONFIG:
94  printf("User config\n");
95  if (msg->data.config != 0) {
96  usb_setup_bulk_endpoint(DEV_TO_HOST,
97  input_buffer, sizeof(input_buffer));
98  usb_setup_bulk_endpoint(HOST_TO_DEV,
99  output_buffer, sizeof(output_buffer));
100  usb_setup_interrupt_endpoint(0x83,interrupt_buffer,
101  sizeof(interrupt_buffer));
103  } else {
104  etimer_stop(&timer);
105  usb_disable_endpoint(DEV_TO_HOST);
106  usb_disable_endpoint(HOST_TO_DEV);
107  usb_disable_endpoint(0x83);
108 
109  }
110  break;
111  case USB_USER_MSG_TYPE_EP_OUT(2):
112  {
113  /*unsigned int len = msg->data.length;
114  printf("Received %d:\n", len); */
115  {
116  unsigned char ch;
117  unsigned int xfer;
118  while((xfer = usb_recv_data(HOST_TO_DEV, &ch, 1)) > 0) {
119  /* printf(" %02x",ch); */
120  if (slip_input_byte(ch)) break;
121  }
122  /* printf("\n"); */
123  }
124  }
125  break;
126  }
127 
128  }
129  }
130  printf("USB test process exited\n");
131  PROCESS_END();
132 }
133 
134 AUTOSTART_PROCESSES(&gateway_process);