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/shared/configuration_xml_handling.cpp

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 <stdlib.h>
00038 #include <string.h>
00039 #include <strings.h>
00040 #include <expat.h>
00041 #include <time.h>
00042 #include "configuration.h"
00043 #include "sensor_node.h"
00044 #include "sensor_device.h"
00045 
00046 #define OQ_DEBUG  0
00047 
00048 
00049 static char *__strcatAlloc(char *pre, const char *post)
00050 {
00051         char *string;
00052         size_t len;
00053 
00054         if (pre != NULL)                
00055                 len = strlen(pre) + strlen(post) + 1;
00056         else
00057                 len = strlen(post) + 1;
00058 
00059         if ((string = (char*) malloc(len)) == NULL) {
00060                 perror("malloc");
00061                 return NULL;
00062         }
00063         memset(string, '\0', len);
00064 
00065         if (pre != NULL)
00066                 strcpy(string, pre);
00067         strcat(string, post);
00068 
00069         if (pre != NULL) {
00070                 free(pre);
00071                 pre = NULL;
00072         }
00073         return string;
00074 }
00075 
00076 
00077 static void __getMonth(int id, char *string)
00078 {
00079         switch (id) {
00080         case 1:
00081                 strcpy(string, "January");
00082                 break;
00083         case 2:
00084                 strcpy(string, "February");
00085                 break;
00086         case 3:
00087                 strcpy(string, "March");
00088                 break;
00089         case 4:
00090                 strcpy(string, "April");
00091                 break;
00092         case 5:
00093                 strcpy(string, "May");
00094                 break;
00095         case 6:
00096                 strcpy(string, "June");
00097                 break;
00098         case 7:
00099                 strcpy(string, "July");
00100                 break;
00101         case 8:
00102                 strcpy(string, "August");
00103                 break;
00104         case 9:
00105                 strcpy(string, "September");
00106                 break;
00107         case 10:
00108                 strcpy(string, "October");
00109                 break;
00110         case 11:
00111                 strcpy(string, "November");
00112                 break;
00113         case 12:
00114                 strcpy(string, "December");
00115                 break;
00116         default:
00117                 break;
00118         }
00119 }
00120 
00121 
00122 static void __getWkday(int id, char *string)
00123 {
00124         switch (id) {
00125         case 0:
00126                 strcpy(string, "Sunday");
00127                 break;
00128         case 1:
00129                 strcpy(string, "Monday");
00130                 break;
00131         case 2:
00132                 strcpy(string, "Tuesday");
00133                 break;
00134         case 3:
00135                 strcpy(string, "Wednesday");
00136                 break;
00137         case 4:
00138                 strcpy(string, "Thursday");
00139                 break;
00140         case 5:
00141                 strcpy(string, "Friday");
00142                 break;
00143         case 6:
00144                 strcpy(string, "Saturday");
00145                 break;
00146         default:
00147                 break;
00148         }
00149 }
00150 
00151 
00152 static char *__getAttrib(const char **attr_list, char *key)
00153 {
00154         int i;
00155         for (i = 0; attr_list[i] != NULL; i += 2) {
00156                 if (strcmp(attr_list[i], key) == 0) {
00157                         return (char *) attr_list[i + 1];
00158                 }
00159         }
00160         return NULL;
00161 }
00162 
00163 
00164 void Configuration::on_start_element(const char *name, const char **attr)
00165 {
00166         char tmp[strlen(_xml_path)];
00167         strcpy(tmp, _xml_path);
00168         snprintf(_xml_path, MAX_XML_PATH, "%s:%s", tmp, name);
00169 
00170 #if OQ_DEBUG
00171         fprintf(stderr, "  begin %s\n", _xml_path);
00172 #endif
00173         if (strcmp(_xml_path, ":sensor_net:topology:node") == 0) {
00174                 char *ident = __getAttrib(attr, "id");
00175                 if (ident != NULL) {
00176                         int id = (int) strtol((char *) ident, NULL, 10);
00177                         _cur_node = new Sensor_node(id);
00178                         nodes.insert_elt(_cur_node);
00179                 }
00180         } else if (strcmp(_xml_path, 
00181                           ":sensor_net:topology:node:random") == 0) {
00182                 char *size_str = __getAttrib(attr, "size");
00183                 double x, y, lat, lon;
00184 
00185                 if (size_str != NULL) {
00186                         int rand_size = 
00187                                 (int) strtol((char *) size_str, NULL, 10);
00188                         srand((unsigned int) time(NULL));
00189                         x = (double) rand_size * (rand() / (double) RAND_MAX);
00190                         y = (double) rand_size * (rand() / (double) RAND_MAX);
00191                         x = x - (rand_size / 2);
00192                         y = y - (rand_size / 2);
00193                         origin.meters2coords(x, y, &lat, &lon);
00194                         _cur_node->set_coordinates(origin, lat, lon, 0);
00195                 }
00196         } else if (strcmp(_xml_path, 
00197                           ":sensor_net:topology:node:sensor") == 0) {
00198                 char *type = __getAttrib(attr, "id");
00199                 if (type != NULL) {
00200                         AVL_tree<unsigned long> *t;
00201                         if (Source_type_index(type) < 0)
00202                                 add_Source_type(type);
00203                         _cur_sensor = 
00204                                 new Sensor_device(_num_sensors, type);
00205                         _cur_node->sensors.insert_elt(_cur_sensor);
00206                         if ((t = Source_type_tree_index(sensors, type)) != NULL)
00207                             t->insert_elt(_cur_sensor);
00208                 }
00209         }  /* ignore data display attribute */
00210         else if (strcmp(_xml_path, 
00211                           ":sensor_net:network_data:node") == 0) {
00212                 char *ident = __getAttrib(attr, "id");
00213                 if (ident != NULL) {
00214                         int id = (int) strtol((char *) ident, NULL, 10);
00215                         _cur_node = (Sensor_node *) nodes.retrieve(id);
00216                         _cur_node->modify();
00217                 }
00218         } else if (strcmp(_xml_path, 
00219                           ":sensor_net:network_data:node:location") == 0) {
00220                 _cur_node->move();
00221         } else if (strcmp(_xml_path, 
00222                           ":sensor_net:network_data:node:sensor") == 0) {
00223                 char *type = __getAttrib(attr, "id");
00224                 if (type != NULL) {
00225                         AVL_node<unsigned long> *list = NULL;
00226                         _cur_sensor = NULL;
00227 
00228                         for (list = _cur_node->sensors.find_min();
00229                              list != 0; list = list->get_next()) {
00230                                 Sensor_device *cur = 
00231                                         dynamic_cast<Sensor_device*>(list->elt());
00232                                 if (cur != NULL) {
00233                                         if (strcmp(cur->get_type(), type) == 0)
00234                                                 _cur_sensor = cur;
00235                                 }
00236                         }
00237                         if (_cur_sensor != NULL)
00238                                 _cur_sensor->modify();
00239                 }
00240         }
00241 }
00242 
00243 
00244 void Configuration::on_end_element(const char *name)
00245 {
00246 #if OQ_DEBUG
00247         fprintf(stderr, "  end %s\n", _xml_path);
00248 #endif
00249         if (strcmp(name, "node  ") == 0) {
00250                 _cur_node = NULL;
00251         } else if (strcmp(name, "sensor") == 0) {
00252                 _cur_sensor = NULL;
00253                 _num_sensors++;
00254         } else if (strcmp(name, "topology") ==0) {
00255                 _num_sensors = 0;
00256         }
00257         
00258         rindex(_xml_path, ':')[0] = '\0';
00259 }
00260 
00261 
00262 void Configuration::on_character_data(const char *content, int len)
00263 {
00264         if (strcmp(_xml_path, ":sensor_net:topology:name") == 0) {
00265                 set_name(content, len);
00266         } else if (strcmp(_xml_path, 
00267                         ":sensor_net:topology:time:hour") == 0) {
00268                 /* \todo ignore time for now */
00269         } else if (strcmp(_xml_path, 
00270                         ":sensor_net:topology:viewpoint:latitude") == 0) {
00271                 char tmp[len + 1];
00272                 memset(tmp, '\0', len + 1);
00273                 strncpy(tmp, content, len);
00274                 origin.lat = strtod(tmp, NULL);
00275         } else if (strcmp(_xml_path, 
00276                         ":sensor_net:topology:viewpoint:longitude") == 0) {
00277                 char tmp[len + 1];
00278                 memset(tmp, '\0', len + 1);
00279                 strncpy(tmp, content, len);
00280                 origin.lon = strtod(tmp, NULL);
00281         } else if (strcmp(_xml_path, 
00282                         ":sensor_net:topology:viewpoint:altitude") == 0) {
00283                 char tmp[len + 1];
00284                 memset(tmp, '\0', len + 1);
00285                 strncpy(tmp, content, len);
00286                 origin.altitude = strtod(tmp, NULL);
00287         } else if (strcmp(_xml_path, 
00288                           ":sensor_net:topology:node:latitude") == 0) {
00289                 char tmp[len + 1];
00290                 double lat, lon, alt;
00291                 memset(tmp, '\0', len + 1);
00292                 strncpy(tmp, content, len);
00293                 _cur_node->get_coordinates(&lat, &lon, &alt);
00294                 _cur_node->set_coordinates(origin, 
00295                                            strtod(tmp, NULL), lon, alt);
00296         } else if (strcmp(_xml_path, 
00297                           ":sensor_net:topology:node:longitude") == 0) {
00298                 char tmp[len + 1];
00299                 double lat, lon, alt;
00300                 memset(tmp, '\0', len + 1);
00301                 strncpy(tmp, content, len);
00302                 _cur_node->get_coordinates(&lat, &lon, &alt);
00303                 _cur_node->set_coordinates(origin,
00304                                            lat, strtod(tmp, NULL), alt);
00305         } else if (strcmp(_xml_path,
00306                         ":sensor_net:topology:node:altitude") == 0) {
00307                 char tmp[len + 1];
00308                 double lat, lon, alt;
00309                 memset(tmp, '\0', len + 1);
00310                 strncpy(tmp, content, len);
00311                 _cur_node->get_coordinates(&lat, &lon, &alt);
00312                 _cur_node->set_coordinates(origin,
00313                                            lat, lon, strtod(tmp, NULL));
00314         } else if (strcmp(_xml_path, 
00315                           ":sensor_net:topology:node:sensor:data") == 0) {
00316                 _cur_sensor->set_data(content, len);
00317         }
00318 }
00319 
00320 
00321 
00322 
00323 char *Configuration::cfg_to_xml()
00324 {
00325         AVL_node<unsigned long> *list, *sublist;
00326         char *xml = NULL;
00327         int size = 512;
00328         char tmp[size];
00329 
00330         time_t now = time(NULL);
00331         struct tm *brokendown_time = localtime(&now);  
00332         char month[10];
00333         char wkday[10];
00334 
00335         __getMonth(brokendown_time->tm_mon, month);
00336         __getWkday(brokendown_time->tm_wday, wkday);
00337         memset(tmp, '\0', size);
00338 
00339         xml = __strcatAlloc(xml, "<?xml version=\"1.0\"?>\n");
00340         xml = __strcatAlloc(xml, "<sensor_net>\n");
00341 
00342         xml = __strcatAlloc(xml, "  <topology>\n");
00343         snprintf(tmp, size, "    <name>%s</name>\n", name);
00344         xml = __strcatAlloc(xml, tmp);
00345 
00346 
00347         xml = __strcatAlloc(xml, "    <time>\n");
00348         snprintf(tmp, size, "      <weekday>%s</weekday>\n", wkday);
00349         xml = __strcatAlloc(xml, tmp);
00350         snprintf(tmp, size, "      <monthday>%d</monthday>\n", 
00351                  brokendown_time->tm_mday);
00352         xml = __strcatAlloc(xml, tmp);
00353         snprintf(tmp, size, "      <month>%s</month>\n", month);
00354         xml = __strcatAlloc(xml, tmp);
00355         snprintf(tmp, size, "      <year>%d</year>\n", 
00356                  brokendown_time->tm_year + 1900);
00357         xml = __strcatAlloc(xml, tmp);
00358         snprintf(tmp, size, "      <hour>%d</hour>\n", 
00359                  brokendown_time->tm_hour);
00360         xml = __strcatAlloc(xml, tmp);
00361         snprintf(tmp, size, "      <minute>%d</minute>\n", 
00362                  brokendown_time->tm_min);
00363         xml = __strcatAlloc(xml, tmp);
00364         snprintf(tmp, size, "      <second>%d</second>\n", 
00365                  brokendown_time->tm_sec);
00366         xml = __strcatAlloc(xml, tmp);
00367         xml = __strcatAlloc(xml, "    </time>\n");
00368 
00369 
00370         xml = __strcatAlloc(xml, "    <viewpoint>\n");
00371         snprintf(tmp, size, "      <latitude>%f</latitude>\n", origin.lat);
00372         xml = __strcatAlloc(xml, tmp);
00373         snprintf(tmp, size, "      <longitude>%f</longitude>\n", origin.lon);
00374         xml = __strcatAlloc(xml, tmp);
00375         xml = __strcatAlloc(xml, "      <range>500</range>\n");
00376         xml = __strcatAlloc(xml, "      <tilt>0</tilt>\n");
00377         xml = __strcatAlloc(xml, "      <heading>0</heading>\n");
00378         xml = __strcatAlloc(xml, "    </viewpoint>\n");
00379 
00380         for (list = nodes.find_min(); list != 0; list = list->get_next()) {
00381                 Sensor_node *node = dynamic_cast<Sensor_node*>(list->elt());
00382                 if (node == NULL)
00383                         continue;
00384 
00385                 snprintf(tmp, size, "    <node id=\"%ld\">\n", node->id());
00386                 xml = __strcatAlloc(xml, tmp);
00387                 double lat, lon, alt;
00388                 node->get_coordinates(&lat, &lon, &alt);
00389                 snprintf(tmp, size, "      <latitude>%f</latitude>\n", lat);
00390                 xml = __strcatAlloc(xml, tmp);
00391                 snprintf(tmp, size, "      <longitude>%f</longitude>\n", lon);
00392                 xml = __strcatAlloc(xml, tmp);
00393                 snprintf(tmp, size, "      <altitude>%f</altitude>\n", alt);
00394                 xml = __strcatAlloc(xml, tmp);
00395 
00396                 for (sublist = node->sensors.find_min(); sublist != 0;
00397                      sublist = sublist->get_next()) {
00398                         Sensor_device *sensor = 
00399                                 dynamic_cast<Sensor_device*>(sublist->elt());
00400                         char *display = "line";
00401                         if (sensor == NULL)
00402                                 continue;
00403 
00404                         if (strncmp(sensor->get_type(), "gps", 3) == 0)
00405                                 display = "ghostmark";
00406 
00407                         snprintf(tmp, size, "      <sensor id=\"%s\">\n", 
00408                                  sensor->get_type());
00409                         xml = __strcatAlloc(xml, tmp);
00410                         snprintf(tmp, size, 
00411                                  "        <data display=\"%s\">%s</data>\n",
00412                                  display, sensor->get_data());
00413                         xml = __strcatAlloc(xml, tmp);
00414                         xml = __strcatAlloc(xml, "      </sensor>\n");
00415                 }
00416                 xml = __strcatAlloc(xml, "    </node>\n");
00417         }
00418         xml = __strcatAlloc(xml, "  </topology>\n");
00419 
00420 
00421         xml = __strcatAlloc(xml, "  <network_data>\n");
00422         for (list = nodes.find_min(); list != 0; list = list->get_next()) {
00423                 Sensor_node *node = dynamic_cast<Sensor_node*>(list->elt());
00424                 if (node == NULL)
00425                         continue;
00426 
00427                 if (node->is_modified()) {
00428                         snprintf(tmp, size, "    <node id=\"%ld\">\n", 
00429                                  node->id());
00430                         xml = __strcatAlloc(xml, tmp);
00431 
00432                         if (node->is_moved())
00433                                 xml = __strcatAlloc(xml, 
00434                                                     "      <location/>\n");
00435 
00436                         for (sublist = node->sensors.find_min(); 
00437                              sublist != 0; sublist = sublist->get_next()) {
00438                                 Sensor_device *sensor = 
00439                                         dynamic_cast<Sensor_device*>(sublist->elt());
00440                                 if (sensor == NULL)
00441                                         continue;
00442 
00443                                 if (sensor->is_modified()) {
00444                                         snprintf(tmp, size, 
00445                                                 "      <sensor id=\"%s\"/>\n", 
00446                                                  sensor->get_type());
00447                                         xml = __strcatAlloc(xml, tmp);
00448                                 }
00449                         }
00450                         xml = __strcatAlloc(xml, "    </node>\n");
00451                 }
00452         }
00453         xml = __strcatAlloc(xml, "  </network_data>\n");
00454         xml = __strcatAlloc(xml, "</sensor_net>\n");
00455 
00456         return xml;
00457 }
00458 
00459 
00460 int Configuration::xml_to_cfg(char *string)
00461 {
00462         XML_Parser parser = XML_ParserCreate(NULL);
00463         if (parser == NULL) {
00464                 perror("XML_ParserCreate");
00465                 return -1;
00466         }
00467 
00468         _xml_path[0] = 0;
00469         _cur_node = 0;
00470         _cur_sensor = 0;
00471 
00472         XML_SetUserData(parser, this);
00473         XML_SetStartElementHandler(parser, _start_element);
00474         XML_SetEndElementHandler(parser, _end_element);
00475         XML_SetCharacterDataHandler(parser, _character_data);
00476 
00477         XML_Parse(parser, string, strlen(string), 1);
00478         XML_ParserFree(parser);
00479         
00480         modified = 1;
00481         return 0;
00482 }
00483 
00484 
00485 int Configuration::read_file(FILE *file)
00486 {
00487         int error = 0;
00488         char *xml = NULL;
00489         int len = 1024;  /* arbitrary, but moot */
00490         char buf[len];
00491 
00492         while (fgets(buf, len, file) != NULL)
00493                 xml = __strcatAlloc(xml, buf);
00494 
00495         fclose(file);
00496 
00497         error = xml_to_cfg(xml);
00498         free(xml);
00499         modified = 1;
00500         return error;
00501 }
00502 
00503 
00504 int Configuration::write_file(FILE *file)
00505 {
00506         int len = 0;
00507         char *config_string = cfg_to_xml();
00508 
00509         len = strlen(config_string);
00510         fprintf(file, "%s", config_string);
00511 
00512         free(config_string);
00513         fclose(file);
00514         return len;
00515 
00516 }


© 2007, Los Alamos National Security, LLC.