N-sim
Emulation and simulation of
Wireless Sensor Networks



   Home


   Project Page


   Download


   CVS



   Installation


   Configuration


   Plug-ins




 Hosted by
SourceForge.net Logo

/home/brennan/n-sim/OrbisQuartus/server/l4/patches/lwip-ore/oreif.c

Go to the documentation of this file.
00001 
00012 /*
00013  * Copyright 2007. Los Alamos National Security, LLC. This material
00014  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00015  * Los Alamos National Laboratory (LANL), which is operated by Los
00016  * Alamos National Security, LLC, for the Department of Energy. The
00017  * U.S. Government has rights to use, reproduce, and distribute this
00018  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00019  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00020  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00021  * produce derivative works, such modified software should be clearly
00022  * marked, so as not to confuse it with the version available from LANL.
00023  *
00024  * Additionally, this program is free software; you can redistribute
00025  * it and/or modify it under the terms of the GNU General Public
00026  * License as published by the Free Software Foundation; version 2 of
00027  * the License. Accordingly, this program is distributed in the hope
00028  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00029  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00030  * PURPOSE. See the GNU General Public License for more details.
00031  */
00032 
00033 /*
00034  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
00035  * All rights reserved. 
00036  * 
00037  * Redistribution and use in source and binary forms, with or without modification, 
00038  * are permitted provided that the following conditions are met:
00039  *
00040  * 1. Redistributions of source code must retain the above copyright notice,
00041  *    this list of conditions and the following disclaimer.
00042  * 2. Redistributions in binary form must reproduce the above copyright notice,
00043  *    this list of conditions and the following disclaimer in the documentation
00044  *    and/or other materials provided with the distribution.
00045  * 3. The name of the author may not be used to endorse or promote products
00046  *    derived from this software without specific prior written permission. 
00047  *
00048  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00049  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
00050  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
00051  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00052  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
00053  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00054  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
00055  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
00056  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
00057  * OF SUCH DAMAGE.
00058  */
00059 
00060 
00061 #include <l4/ore/ore.h>
00062 #include <l4/log/l4log.h>
00063 #include <l4/util/util.h>
00064 #include <l4/sys/ipc.h>
00065 
00066 #include <fcntl.h>
00067 #include <stdlib.h>
00068 #include <stdio.h>
00069 #include <string.h>
00070 #include <unistd.h>
00071 #include <sys/types.h>
00072 #include <sys/uio.h>
00073 #include <sys/socket.h>
00074 
00075 #include "lwip/debug.h"
00076 #include "lwip/opt.h"
00077 #include "lwip/def.h"
00078 #include "lwip/ip.h"
00079 #include "lwip/mem.h"
00080 #include "lwip/pbuf.h"
00081 #include "lwip/sys.h"
00082 #include "netif/etharp.h"
00083 #include "netif/oreif.h"
00084 
00085 
00086 #define BPS 512000
00087 #define QUEUELEN 6
00088 
00089 struct oreif {
00090   struct eth_addr *ethaddr;
00091   int fd;
00092   l4ore_config conf;
00093   l4_timeout_t timeout;
00094 };
00095 
00096 #define CONFIG_OREDEV_TIMEOUT   500000
00097 
00098 static void  oreif_input(struct netif *netif);
00099 static void oreif_thread(void *data);
00100 
00101 
00102 
00103 static void low_level_init(struct netif *netif)
00104 {
00105   struct oreif *oreif;
00106   char buf[100];
00107   unsigned char mac[6];
00108 
00109   oreif = netif->state;
00110   
00111   /* Do whatever else is needed to initialize interface. */
00112   oreif->conf = L4ORE_DEFAULT_CONFIG;
00113   oreif->conf.rw_broadcast = 1;
00114   oreif->fd = l4ore_open("eth0", mac, &oreif->conf);
00115   if (oreif->fd < 0) {
00116       LOG("Could not open connection to ORe: %d", oreif->fd);
00117       exit(1);
00118   }
00119 
00120   LOG_MAC(1, mac);
00121     
00122   /* setup rx timeout */
00123   oreif->timeout = l4_timeout(L4_IPC_TIMEOUT_NEVER,
00124                               l4util_micros2l4to(CONFIG_OREDEV_TIMEOUT));
00125   
00126   /* copy MAC */
00127   memcpy(&oreif->ethaddr->addr[0], &mac[0], 6); 
00128   
00129   LWIP_DEBUGF(OREIF_DEBUG, ("oreif_init: system(\"%s\");\n", buf));
00130   system(buf);
00131   sys_thread_new(oreif_thread, netif, DEFAULT_THREAD_PRIO);
00132 }
00133 
00134 static err_t arp_out(struct netif *netif, struct pbuf *p,
00135                      struct ip_addr *ipaddr)
00136 {
00137   return etharp_output(netif, ipaddr, p);
00138 }
00139 
00140 static err_t low_level_output(struct netif *netif, struct pbuf *p)
00141 {
00142   struct pbuf *q;
00143   char buf[1514];
00144   char *bufptr;
00145   struct oreif *oreif;
00146 
00147   oreif = netif->state;
00148 
00149   /* initiate transfer(); */
00150   bufptr = &buf[0];
00151   
00152   for(q = p; q != NULL; q = q->next) {
00153     /* Send the data from the pbuf to the interface, one pbuf at a
00154        time. The size of the data in each pbuf is kept in the ->len
00155        variable. */    
00156     /* send data from(q->payload, q->len); */
00157     memcpy(bufptr, q->payload, q->len);
00158     bufptr += q->len;
00159   }
00160 
00161   /* signal that packet should be sent(); */
00162   if(l4ore_send(oreif->fd, buf, p->tot_len) == -1) {
00163     perror("oreif: write");
00164   }
00165   return ERR_OK;
00166 }
00167 
00168 static struct pbuf *low_level_input(struct oreif *oreif)
00169 {
00170   struct pbuf *p, *q;
00171   u16_t len;
00172   char buf[1514];
00173   char *bufptr;
00174   l4_size_t buf_size = sizeof(buf);
00175 
00176   /* Obtain the size of the packet and put it into the "len"
00177      variable. */
00178   len = l4ore_recv_blocking(oreif->fd, (char**)&buf, &buf_size,
00179                             oreif->timeout);
00180   
00181   /* We allocate a pbuf chain of pbufs from the pool. */
00182   p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
00183   
00184   if(p != NULL) {
00185     /* We iterate over the pbuf chain until we have read the entire
00186        packet into the pbuf. */
00187     bufptr = &buf[0];
00188     for(q = p; q != NULL; q = q->next) {
00189       /* Read enough bytes to fill this pbuf in the chain. The
00190          available data in the pbuf is given by the q->len
00191          variable. */
00192       /* read data into(q->payload, q->len); */
00193       memcpy(q->payload, bufptr, q->len);
00194       bufptr += q->len;
00195     }
00196     /* acknowledge that packet has been read(); */
00197   } else {
00198     /* drop packet(); */
00199   }
00200 
00201   return p;  
00202 }
00203 
00204 
00205 static void oreif_thread(void *arg)
00206 {
00207   struct netif *netif;
00208   struct oreif *oreif;
00209   
00210   netif = arg;
00211   oreif = netif->state;
00212   
00213   while(1) {
00214     /* Handle incoming packet. */
00215     oreif_input(netif);
00216   }
00217 }
00218 
00219 
00220 static void oreif_input(struct netif *netif)
00221 {
00222   struct oreif *oreif;
00223   struct eth_hdr *ethhdr;
00224   struct pbuf *p;
00225 
00226 
00227   oreif = netif->state;
00228   
00229   p = low_level_input(oreif);
00230 
00231   if(p == NULL) {
00232     LWIP_DEBUGF(OREIF_DEBUG, ("oreif_input: low_level_input returned NULL\n"));
00233     return;
00234   }
00235   ethhdr = p->payload;
00236 
00237   switch(htons(ethhdr->type)) {
00238   case ETHTYPE_IP:
00239     LWIP_DEBUGF(OREIF_DEBUG, ("oreif_input: IP packet\n"));
00240 #if 0
00241 /* CSi disabled ARP table update on ingress IP packets.
00242    This seems to work but needs thorough testing. */
00243     etharp_ip_input(netif, p);
00244 #endif
00245     pbuf_header(p, -14);
00246     netif->input(p, netif);
00247     break;
00248   case ETHTYPE_ARP:
00249     LWIP_DEBUGF(OREIF_DEBUG, ("oreif_input: ARP packet\n"));
00250     etharp_arp_input(netif, oreif->ethaddr, p);
00251     break;
00252   default:
00253     pbuf_free(p);
00254     break;
00255   }
00256 }
00257 
00258 
00259 static void arp_timer(void *arg)
00260 {
00261   etharp_tmr();
00262   sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
00263 }
00264 
00265 
00266 
00267 err_t oreif_init(struct netif *netif)
00268 {
00269   struct oreif *oreif = mem_malloc(sizeof(struct oreif));
00270 
00271   if (!oreif)
00272     return ERR_MEM;
00273 
00274   netif->state = oreif;
00275   netif->name[0] = 'o';
00276   netif->name[1] = 'r';
00277   netif->output = arp_out;
00278   netif->linkoutput = low_level_output;
00279   netif->mtu = 1500;     
00280   /* hardware address length */
00281   netif->hwaddr_len = 6;
00282 
00283   oreif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
00284   low_level_init(netif);
00285   etharp_init();
00286 
00287   sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, NULL);
00288 
00289   return ERR_OK;
00290 }
00291 


© 2007, Los Alamos National Security, LLC.