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/Vaike/linux/system-addons/system/soap/sensor_app.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 #include <stdio.h>
00036 #include <string.h>
00037 #include <strings.h>
00038 #include <stdlib.h>
00039 #include <stdarg.h>
00040 #include <limits.h>
00041 #include "sensor_ipc.h"
00042 #include "sensor_app.h"
00043 
00044 
00045 #define MAX_APPS  1024
00046 #define MAX_DATA  1024
00047 
00048 extern int ipc_data_port, ipc_cmd_port;
00049 extern const char *my_app;
00050 
00051 static int registered[MAX_APPS];
00052 static int count = 0;
00053 
00054 
00055 SoapDataType consume(const char *appname, unsigned char data[], size_t *bytes)
00056 {
00057         int i;
00058         SoapEvent evt;
00059         char *msg, *app = NULL, *id = NULL;
00060         char format[20] = "";
00061 
00062         if (appname == NULL || data == NULL || bytes == NULL)
00063                 return Invalid_Type;
00064 
00065         sscanf(appname, "%s : %s", app, id);
00066 
00067         if (!registered[count]) {
00068                 SoapEvent req;
00069                 sprintf(req.msg, "register %s", appname);
00070                 // FIXME: bind id to a specific neighbor address
00071                 // FIXME: fill in remainder of event
00072                 strncpy(req.from, my_ip_addr, NET_ADDR_LEN);
00073                 ipc_cmd_send(&req);
00074                 registered[count]++;  // FIXME: app_t list?
00075         }
00076         ipc_data_recv(&evt);  // FIXME accept only events matching app + id?
00077         msg = evt.msg;
00078         strcat(format, SoapDataFormat[evt.data_type]);
00079         strcat(format, ",%s");
00080 
00081         for (i = 0; i < *bytes; i += SoapDataSize[evt.data_type]) {
00082                 if (sscanf(msg, format, &data[i], msg) < 1)
00083                         break;
00084         }
00085         *bytes = i;
00086 
00087         return evt.data_type;
00088 }
00089 
00090 
00091 void produce(SoapDataType type, unsigned char data[], size_t bytes)
00092 {
00093         int i;
00094         SoapEvent evt;
00095         int msg_size = sizeof(EVENT_DATA_LEN) - 1;
00096 
00097         if (data == NULL)
00098                 return;
00099 
00100         memset(evt.msg, '\0', EVENT_DATA_LEN);
00101         memset(evt.to, '\0', NET_ADDR_LEN);
00102         memset(evt.to_app, '\0', PATH_MAX);
00103         evt.data_type = type;
00104         strncpy(evt.from_app, my_app, PATH_MAX);
00105         gettimeofday(&evt.time, NULL);
00106         evt.type = Event_Msg;
00107         strncpy(evt.from, my_ip_addr, NET_ADDR_LEN);
00108 
00109         for (i = 0; i < bytes; i += SoapDataSize[type]) {
00110                 char tmp[msg_size];
00111                 if (i)
00112                         strncat(evt.msg, ",", msg_size--);
00113                 sprintf(tmp, SoapDataFormat[type], data);
00114                 strncat(evt.msg, tmp, msg_size);
00115                 msg_size = EVENT_DATA_LEN - strlen(evt.msg) - 1;
00116         }
00117 
00118         ipc_data_send(&evt);
00119 }


© 2007, Los Alamos National Security, LLC.