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/minisoap.h

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 <sys/time.h>
00036 #include <limits.h>
00037 #include <errno.h>
00038 
00039 #ifndef _MINISOAP_H_
00040 # define _MINISOAP_H_
00041 
00042 
00043 # define DEFAULT_MINISOAP_PORT    8000  /* versus full SOAP on port 80 */
00044 
00045 # define NET_ADDR_LEN         23
00046 # define TYPE_STR_LEN         10
00047 # define MAX_TYPE_NAME        128
00048 # define TIME_STR_LEN         30
00049 # define EVENT_DATA_LEN       8192  /* limit on event data */
00050 # define MAX_EVENT_LEN        (NET_ADDR_LEN + NET_ADDR_LEN + \
00051                               TIME_STR_LEN + PATH_MAX + EVENT_DATA_LEN)
00052 
00053 
00054 # ifndef _minisoap_impl_ 
00055 extern char my_ip_addr[NET_ADDR_LEN];
00056 extern pthread_t soap_thread;
00057 # endif
00058 
00059 typedef struct {
00060         char addr[NET_ADDR_LEN];
00061 } ip_addr_t;
00062 
00063 
00064 typedef enum { Invalid_Msg = -1, Event_Msg = 0, Command_Msg = 1 } SoapMsgType;
00065 
00066 //static const char *SoapMsgTypeStr[] = { "event", "command", NULL };
00067 
00068 
00069 # ifndef __cplusplus
00070 
00071 typedef enum { Invalid_Type = -1, Soap_Char = 0, Soap_Short, Soap_Int,
00072                Soap_Long, Soap_Long_Long, Soap_UChar, Soap_UShort,
00073                Soap_UInt, Soap_ULong, Soap_ULong_Long, Soap_Float,
00074                Soap_Double, Soap_Long_Double } SoapDataType;
00075 
00076 #  ifndef _minisoap_impl_ 
00077 #   ifndef _ipc_impl_
00078 static const char *SoapDataFormat[] = { "%hhd", "%hd", "%d",
00079                                         "%ld", "%lld",
00080                                         "%hhu", "%hu", "%u",
00081                                         "%lu", "%llu",
00082                                         "%f", "%lf", "%Lf", NULL };
00083 #   endif
00084 #  endif
00085 
00086 static const size_t SoapDataSize[] = {
00087         sizeof(char), sizeof(short), sizeof(int),
00088         sizeof(long), sizeof(long long),
00089         sizeof(unsigned char), sizeof(unsigned short), sizeof(unsigned int),
00090         sizeof(unsigned long), sizeof(unsigned long long),
00091         sizeof(float), sizeof(double), sizeof(long double), 0 };
00092 
00093 #endif  /* ifndef __cplusplus */
00094 
00095 
00096 typedef struct _soap_event{
00097         char from[NET_ADDR_LEN];
00098         char to[NET_ADDR_LEN];
00099         SoapMsgType type;
00100         char from_app[PATH_MAX];
00101         char to_app[PATH_MAX];
00102 # ifndef __cplusplus
00103         SoapDataType data_type;
00104 # else
00105         char data_type[MAX_TYPE_NAME];
00106 # endif
00107         struct timeval time;
00108         char msg[EVENT_DATA_LEN];
00109 } SoapEvent;
00110 
00111 
00112 
00113 inline static int string_SoapEvent(char *string, SoapEvent *evt)
00114 {
00115         int type_num, data_type_num, error = 0;
00116         char data_type_str[MAX_TYPE_NAME];
00117 
00118         if (string == NULL || evt == NULL)
00119                 return -EINVAL;
00120 
00121         error = sscanf(string, "<mFrom>%s</mFrom>\n<mTo>%s</mTo>\n"
00122                        "<mFromApp>%s</mFromApp>\n<mToApp>%s</mToApp>\n"
00123                        "<mType>%d</mType>\n<mTime>%ld.%ld</mTime>\n"
00124                        "<mDataType>%s</mDataType>\n<mBody>%s</mBody>\n",
00125                        evt->from, evt->to, evt->from_app, evt->to_app,
00126                        &type_num, &evt->time.tv_sec, &evt->time.tv_usec,
00127                        data_type_str, evt->msg);
00128 
00129         switch (type_num) {
00130         case Event_Msg:
00131                 evt->type = Event_Msg;
00132                 break;
00133         case Command_Msg:
00134                 evt->type = Command_Msg;
00135                 break;
00136         default:
00137                 evt->type = Invalid_Msg;
00138         }
00139 
00140 # ifndef __cplusplus
00141         data_type_num = atoi(data_type_str);
00142         switch (data_type_num) {
00143         case Soap_Char:
00144                 evt->data_type = Soap_Char;
00145                 break;
00146         case Soap_Short:
00147                 evt->data_type = Soap_Short;
00148                 break;
00149         case Soap_Int:
00150                 evt->data_type = Soap_Int;
00151                 break;
00152         case Soap_Long:
00153                 evt->data_type = Soap_Long;
00154                 break;
00155         case Soap_Long_Long:
00156                 evt->data_type = Soap_Long_Long;
00157                 break;
00158         case Soap_UChar:
00159                 evt->data_type = Soap_UChar;
00160                 break;
00161         case Soap_UShort:
00162                 evt->data_type = Soap_UShort;
00163                 break;
00164         case Soap_UInt:
00165                 evt->data_type = Soap_UInt;
00166                 break;
00167         case Soap_ULong:
00168                 evt->data_type = Soap_ULong;
00169                 break;
00170         case Soap_ULong_Long:
00171                 evt->data_type = Soap_ULong_Long;
00172                 break;
00173         case Soap_Float:
00174                 evt->data_type = Soap_Float;
00175                 break;
00176         case Soap_Double:
00177                 evt->data_type = Soap_Double;
00178                 break;
00179         case Soap_Long_Double:
00180                 evt->data_type = Soap_Long_Double;
00181                 break;
00182         default:
00183                 evt->data_type = 0;
00184         }
00185 # endif  /* ifndef __cplusplus */
00186         return error;
00187 }
00188 
00189 /* NB: in C++, using introspection, evt->data_type is loaded by: 
00190  *     T.typeid.name() */
00191 
00192 inline static int SoapEvent_string(SoapEvent *evt, char *string, int len)
00193 {
00194         if (string == NULL || evt == NULL)
00195                 return -EINVAL;
00196 
00197         return snprintf(string, len, "<mFrom>%s</mFrom>\n<mTo>%s</mTo>\n"
00198                         "<mFromApp>%s</mFromApp>\n<mToApp>%s</mToApp>\n"
00199                         "<mType>%d</mType>\n<mTime>%ld.%ld</mTime>\n"
00200 # ifndef __cplusplus
00201                         "<mDataType>%d</mDataType>\n"
00202 # else  /* __cplusplus defined */
00203                         "<mDataType>%s</mDataType>\n"
00204 # endif  /* ifndef __cplusplus */
00205                         "<mBody>%s</mBody>\n",
00206                         evt->from, evt->to, evt->from_app, evt->to_app,
00207                         evt->type, evt->time.tv_sec, evt->time.tv_usec,
00208                         evt->data_type, evt->msg);
00209 }
00210 
00211 
00212 
00213 typedef void(*cmd_callback_t)(SoapEvent*);
00214 
00215 int quitting(void);
00216 int register_soap_handler(cmd_callback_t handler);
00217 int raise_soap_event(SoapEvent *evt, char *server, int port);
00218 int raise_local_query(SoapEvent *evt, int port);
00219 int get_gateways(ip_addr_t gateways[], unsigned int size);
00220 
00221 #endif  // _MINISOAP_H_


© 2007, Los Alamos National Security, LLC.