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/tftp_client-l4.c

Go to the documentation of this file.
00001 
00014 /*
00015  * Copyright 2007. Los Alamos National Security, LLC. This material
00016  * was produced under U.S. Government contract DE-AC52-06NA25396 for
00017  * Los Alamos National Laboratory (LANL), which is operated by Los
00018  * Alamos National Security, LLC, for the Department of Energy. The
00019  * U.S. Government has rights to use, reproduce, and distribute this
00020  * software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY,
00021  * LLC, MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL
00022  * LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified to
00023  * produce derivative works, such modified software should be clearly
00024  * marked, so as not to confuse it with the version available from LANL.
00025  *
00026  * Additionally, this program is free software; you can redistribute
00027  * it and/or modify it under the terms of the GNU General Public
00028  * License as published by the Free Software Foundation; version 2 of
00029  * the License. Accordingly, this program is distributed in the hope
00030  * it will be useful, but WITHOUT ANY WARRANTY; without even the
00031  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00032  * PURPOSE. See the GNU General Public License for more details.
00033  */
00034 
00035 
00036 #include <stdio.h>
00037 #include <string.h>
00038 #include <stdlib.h>
00039 
00040 #include <l4/sys/types.h>
00041 #include <l4/env/errno.h>
00042 #include <l4/sys/syscalls.h>
00043 #include <l4/log/l4log.h>
00044 #include <l4/l4rm/l4rm.h>
00045 #include <l4/util/util.h>
00046 #include <l4/env/env.h>
00047 #include <l4/names/libnames.h>
00048 #include <l4/generic_fprov/generic_fprov-server.h>
00049 
00050 //#include <l4/sys/consts.h>
00051 #include <l4/dm_mem/dm_mem.h>
00052 #include <l4/thread/thread.h>
00053 //#include <l4/util/getopt.h>
00054 //#include <l4/util/macros.h>
00055 #include <l4/generic_io/libio.h>
00056 #include <l4/sys/kdebug.h>
00057 
00058 
00059 #if 0
00060 #define TIMEOUT    5
00061 int rexmtval = TIMEOUT;
00062 int maxtimeout = 5 * TIMEOUT;
00063 #endif
00064 
00065 static char tftp_stack[2*L4_PAGESIZE];
00066 static struct in_addr peeraddr;
00067 
00068 extern int disp_filesizebarrier;
00069 extern int disp_filesize;
00070 extern char tftp_orename[16];
00071 
00072 
00073 
00074 long l4fprov_file_open_component (CORBA_Object _dice_corba_obj,
00075                                   const char* fname,
00076                                   const l4_threadid_t *dm,
00077                                   unsigned long flags,
00078                                   l4dm_dataspace_t *ds,
00079                                   l4_size_t *size,
00080                                   CORBA_Server_Environment *_dice_corba_env)
00081 {
00082         int read_size;
00083         int error;
00084         char *addr;
00085         char buf[L4DM_DS_NAME_MAX_LEN];
00086         const char *ptr;
00087 
00088         if (!netboot_open((char*)fname)) {
00089                 /* file not found */
00090                 printf("File not found: \"%s\"\n", fname);
00091                 return -L4_ENOTFOUND;
00092         }
00093 
00094         /* after succesful open of file, netboot_filemax consists of
00095          * the length of the file */
00096         *size = netboot_filemax;
00097 
00098         ptr = strrchr(fname, '/');
00099         if (!ptr)
00100                 ptr=fname;
00101         else
00102                 ptr++;
00103         snprintf(buf, L4DM_DS_NAME_MAX_LEN, "tftp image: %s", ptr);
00104 
00105         if (!(addr = l4dm_mem_ds_allocate_named_dsm(*size, flags, buf, *dm,
00106                                                     (l4dm_dataspace_t *)ds))) {
00107                 printf("Allocating dataspace of size %d failed\n", *size);
00108                 netboot_close();
00109                 return -L4_ENOMEM;
00110         }
00111 
00112         printf("Loading %s [%dkB]\n", fname, (*size + 1023) / 1024);
00113 
00114         /* Reset display file position */
00115         disp_filesizebarrier = disp_filesize = 0;
00116 
00117         read_size = netboot_read((char*) addr, *size);
00118 
00119         netboot_close();
00120 
00121         /* file image is not mapped to our address space anymore */
00122         if ((error = l4rm_detach((void *)addr))) {
00123             printf("Error %d detaching dataspace\n", error);
00124             l4dm_close((l4dm_dataspace_t*)ds);
00125             return -L4_ENOMEM;
00126         }
00127 
00128         if (!read_size) {
00129                 printf("Error reading file %s\n"
00130                        "TFTP error was %d (%s)\n",
00131                        fname,
00132                        netboot_errnum, err_list[netboot_errnum]);
00133                 l4dm_close((l4dm_dataspace_t*)ds);
00134                 return -L4_EIO;
00135         }
00136 
00137         /* set dataspace owner to client */
00138         if ((error = l4dm_transfer((l4dm_dataspace_t *)ds,*_dice_corba_obj))) {
00139                 printf("Error transfering dataspace ownership: %s (%d)\n",
00140                        l4env_errstr(error), error);
00141                 l4dm_close((l4dm_dataspace_t*)ds);
00142                 return -L4_EINVAL;
00143         }
00144 
00145         return 0;
00146 }
00147 
00148 
00149 void tftp_client(void *unused)
00150 {
00151         l4_addr_t netbuf, gunzipbuf;
00152 
00153         /* get dataspace for the net buffer */
00154         netbuf = (l4_addr_t)
00155                 l4dm_mem_allocate_named(NETBUF_SIZE,0,"tftp netbuff");
00156         if (netbuf == 0)
00157         {
00158                 printf("Allocate netbuff failed\n");
00159                 exit(-1);
00160         }
00161 
00162         /* get dataspace for gunzip allocator buffer */
00163         gunzipbuf = (l4_addr_t)
00164                 l4dm_mem_allocate_named(GUNZIP_SIZE,0,"tftp gunzip");
00165         if (gunzipbuf == 0)
00166         {
00167                 printf("Allocate gunzip buffer failed\n");
00168                 exit(-1);
00169         }
00170 
00171         if (!names_register("TFTP"))
00172         {
00173                 printf("failed to register at name server\n");
00174                 exit(-1);
00175         }
00176 
00177         l4fprov_file_server_loop(NULL);
00178 }
00179 
00180 
00181 void tftp_init(char *host) {
00182         l4io_info_t *io_info_addr = (l4io_info_t*)-1;
00183 
00184         /* set TFTP server (head node) */
00185         if (!inet_aton(host, &tftp_server_addr))
00186                 printf("Invalid TFTP server address: %s!\n", host);
00187         else
00188                 printf("Using TFTP server %s (0x%08x)\n", host,
00189                        tftp_server_addr.s_addr);
00190 
00191         /* invoke L4 IO server */
00192         if (l4io_init(&io_info_addr, L4IO_DRV_INVALID))
00193                 enter_kdebug("Couldn't connect to L4 IO server!");
00194 
00195 #if 0
00196         strncpy(tftp_orename, host, sizeof(tftp_orename));
00197         tftp_orename[sizeof(tftp_orename)-1] = 0;
00198 #endif
00199 
00200         if ((ret = l4thread_create_long(L4THREAD_INVALID_ID, tftp_client,
00201                                         ".tftp_client",
00202                                         (unsigned)tftp_stack 
00203                                         + sizeof(tftp_stack),
00204                                         sizeof(tftp_stack),
00205                                         L4THREAD_DEFAULT_PRIO, 0,
00206                                         L4THREAD_CREATE_ASYNC)) < 0) {
00207                 Panic("Error %d creating tftp_client thread", ret);
00208         }
00209 }
00210 
00211 
00212 void tftp_get(char *file) {
00213         int error;
00214         l4_threadid_t tftp_id;
00215         l4_threadid_t dm_id;
00216         l4dm_dataspace_t ds;
00217         void *addr;
00218         l4_size_t size;
00219         CORBA_Environment _env = dice_default_environment;
00220   
00221         if (!names_waitfor_name("TFTP", &tftp_id, 10000)) {
00222                 printf("TFTP not found\n");
00223                 return -1;
00224         }
00225 
00226         dm_id = l4env_get_default_dsm();
00227         if (l4_is_invalid_id(dm_id)) {
00228                 printf("No dataspace manager found\n");
00229                 return -1;
00230         }
00231 
00232         if ((error = l4fprov_file_open_call(&tftp_id, file, &dm_id,
00233                                             0, &ds, &size, &_env))) {
00234                 printf("Error opening file from tftp\n");
00235                 return -1;
00236         }
00237   
00238         if ((error = l4rm_attach(&ds, size, 0, L4DM_RO | L4RM_MAP, &addr))) {
00239                 printf("Error %d attaching dataspace\n", error);
00240                 return -L4_ENOMEM;
00241         }
00242 }
00243 
00244 
00245 void tftp_put(char *file) {
00247 }
00248 
00249 
00250 void tftp_mode(enum TFTP_mode unused) {
00251         /* no mode switching in L4 */
00252 }


© 2007, Los Alamos National Security, LLC.