gateway.c

00001 #include <stdio.h>
00002 #include <sys/etimer.h>
00003 #include <sys/process.h>
00004 #include <sys/autostart.h>
00005 #include <usb/usb-api.h>
00006 #include <usb/cdc-acm.h>
00007 #include <dev/leds.h>
00008 #include <debug-uart.h>
00009 
00010 #include <net/uip-fw-drv.h>
00011 #include <net/uip-over-mesh.h>
00012 #include <dev/slip.h>
00013 
00014 #include "contiki-main.h"
00015 
00016 /* SLIP interface */
00017 
00018 extern struct uip_fw_netif cc2420if;
00019 
00020 rimeaddr_t node_addr = {{0,129}};
00021 
00022 static struct uip_fw_netif slipif =
00023 {UIP_FW_NETIF(0,0,0,0, 255,255,255,255, slip_send)};
00024 
00025 /* USB buffers */
00026 static unsigned char input_buffer[128];
00027 static unsigned char output_buffer[128];
00028 static unsigned char interrupt_buffer[16];
00029 
00030 #define DEV_TO_HOST 0x81
00031 #define HOST_TO_DEV 0x02
00032 
00033 #define GATEWAY_TRICKLE_CHANNEL 8
00034 void
00035 slip_arch_init(unsigned long ubr)
00036 {
00037 }
00038 
00039 void
00040 slip_arch_writeb(unsigned char c)
00041 {
00042   while(usb_send_data(DEV_TO_HOST, &c, 1) != 1);
00043 }
00044 
00045 #if WITH_UIP
00046 
00047 static void
00048 set_gateway(void)
00049 {
00050   struct gateway_msg msg;
00051   /* Make this node the gateway node, unless it already is the
00052      gateway. */
00053   if(!is_gateway) {
00054     leds_on(LEDS_RED);
00055     printf("%d.%d: making myself the gateway\n",
00056            rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1]);
00057     uip_over_mesh_set_gateway(&rimeaddr_node_addr);
00058     rimeaddr_copy(&(msg.gateway), &rimeaddr_node_addr);
00059     rimebuf_copyfrom(&msg, sizeof(struct gateway_msg));
00060     trickle_send(&gateway_trickle);
00061     is_gateway = 1;
00062   }
00063 }
00064 #endif /* WITH_UIP */
00065 
00066 PROCESS(gateway_process, "Gateway process");
00067 
00068 
00069 PROCESS_THREAD(gateway_process, ev , data)
00070 {
00071   static struct etimer timer;
00072   PROCESS_BEGIN();
00073   usb_set_user_process(process_current);
00074   usb_setup();
00075   usb_cdc_acm_setup();
00076 
00077   uip_fw_default(&slipif);
00078   uip_over_mesh_set_gateway_netif(&slipif);
00079 
00080   process_start(&slip_process, NULL);
00081 
00082   set_gateway();
00083   
00084   while(ev != PROCESS_EVENT_EXIT) {
00085     PROCESS_WAIT_EVENT();
00086     if (ev == PROCESS_EVENT_TIMER) {
00087       leds_toggle(LEDS_YELLOW);
00088       /* printf("FIFOP: %d\n", FIFOP_IS_1); */
00089       etimer_restart(&timer);
00090     } else if (ev == PROCESS_EVENT_MSG) {
00091       const struct usb_user_msg * const msg = data;
00092       switch(msg->type) {
00093       case USB_USER_MSG_TYPE_CONFIG:
00094         printf("User config\n");
00095         if (msg->data.config != 0) {
00096           usb_setup_bulk_endpoint(DEV_TO_HOST,
00097                                   input_buffer, sizeof(input_buffer));
00098           usb_setup_bulk_endpoint(HOST_TO_DEV,
00099                                   output_buffer, sizeof(output_buffer));
00100           usb_setup_interrupt_endpoint(0x83,interrupt_buffer,
00101                                        sizeof(interrupt_buffer));
00102           etimer_set(&timer, CLOCK_SECOND);
00103         } else {
00104           etimer_stop(&timer);
00105           usb_disable_endpoint(DEV_TO_HOST);
00106           usb_disable_endpoint(HOST_TO_DEV);
00107           usb_disable_endpoint(0x83);
00108 
00109         }
00110         break;
00111       case USB_USER_MSG_TYPE_EP_OUT(2):
00112         {
00113           /*unsigned int len = msg->data.length; 
00114             printf("Received %d:\n", len);  */
00115           {
00116             unsigned char ch;
00117             unsigned int xfer;
00118             while((xfer = usb_recv_data(HOST_TO_DEV, &ch, 1)) > 0) {
00119               /* printf(" %02x",ch); */
00120               if (slip_input_byte(ch)) break;
00121             }
00122             /* printf("\n"); */
00123           }
00124         }
00125         break;
00126       }
00127       
00128     }
00129   }
00130   printf("USB test process exited\n");
00131   PROCESS_END();
00132 }
00133 
00134 AUTOSTART_PROCESSES(&gateway_process);

Generated on Mon Apr 11 14:23:43 2011 for Contiki 2.5 by  doxygen 1.6.1