~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
cvs/emstar/devel/sympathy_devel/sympathy_battery.c


  1 /* ex: set tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
  2 /*
  3  *
  4  * Copyright (c) 2003 The Regents of the University of California.  All 
  5  * rights reserved.
  6  *
  7  * Redistribution and use in source and binary forms, with or without
  8  * modification, are permitted provided that the following conditions
  9  * are met:
 10  *
 11  * - Redistributions of source code must retain the above copyright
 12  *   notice, this list of conditions and the following disclaimer.
 13  *
 14  * - Neither the name of the University nor the names of its
 15  *   contributors may be used to endorse or promote products derived
 16  *   from this software without specific prior written permission.
 17  *
 18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
 19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 21  * PARTICULAR  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
 22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 25  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
 26  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 29  *
 30  */
 31 
 32  /*
 33   *
 34   * Author: Nithya Ramanthan
 35   *
 36   */
 37 
 38 
 39 /*
 40  *  simple example of using a status device
 41  */
 42 
 43 #include <sympathy.h>
 44 
 45 int sbattery_write(status_context_t *info, char* command, size_t buf_size)
 46 {
 47   parser_state_t *ps = misc_parse_init(command, MISC_PARSE_COLON_SCHEME);
 48   int node_id, sctr = -1;
 49   int voltage = -1;
 50   int retval = STATUS_WRITE_DONE;
 51 
 52   while (misc_parse_next_kvp(ps) >= 0)
 53   {
 54     if (!ps->value) {
 55       elog(LOG_ERR, "ERROR: No node-id returned with key: %s!\n", 
 56           ps->key);
 57       retval = EVENT_ERROR(EINVAL);
 58       goto done;
 59     }
 60 
 61     if (strcmp(ps->key, "node") == 0) {
 62       node_id = atoi(ps->value);
 63       sctr = find_status(node_id);
 64     }
 65     else if (strcmp(ps->key, "voltage") == 0) {
 66       voltage = atoi(ps->value);
 67     }
 68     else {
 69       elog(LOG_ERR, "Unrecognized key: %s\n", ps->key);
 70     }
 71   }
 72 
 73   if ((sctr > -1) && (voltage >-1)) {
 74     /* fixme For now make it a stupid check! */
 75     if (voltage > sink.status_srcs[sctr].battery_mv) {
 76       gettimeofday(&sink.status_srcs[sctr].time_battery_changed, NULL);
 77     }
 78     sink.status_srcs[sctr].battery_mv = voltage;
 79   }
 80   else {
 81     elog(LOG_ERR, "ERROR: Either node-id or voltage was not provided (%s)!\n",
 82         command);
 83     retval = EVENT_ERROR(EINVAL);
 84     goto done;
 85   }
 86 done:
 87   misc_parse_cleanup(ps);
 88   return retval;
 89 }
 90 
 91 int sbattery_print(status_context_t *info, buf_t *buf)
 92 {
 93   int i;
 94   
 95   bufprintf(buf, "Node-Id\t Battery(mV)\t Date-last-changed\n");
 96   bufprintf(buf, "-------\t -----------\t -----------------\n");
 97 
 98   for (i=0; i < sink.num_srcs; i++) {
 99     if (sink.status_srcs[i].addr != my_node_id) {
100       bufprintf(buf, "%d\t %d\t\t %s\n", sink.status_srcs[i].addr, 
101         sink.status_srcs[i].battery_mv, 
102         misc_print_date(&sink.status_srcs[i].time_battery_changed));
103     }
104   }
105   bufprintf(buf, "\nTo update a node's battery, write:\n");
106   bufprintf(buf, "\tnode=<node-id>:voltage=<int[milli-volts]>\n");
107   return STATUS_MSG_COMPLETE;
108 }
109 
110 int sbattery_html(status_context_t *info, buf_t *buf)
111 {
112   int i;
113   sympathy_node_info_t* status_srcs_cpy[SMAX_SRCS];
114   
115   order_by_id(status_srcs_cpy);
116   
117   bufprintf(buf, "<br><table frame=void cellspacing=5 cellpadding=6>\n");
118   bufprintf(buf, "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
119             "Node ID", "Battery(mV)", "Date-last-changed");
120   bufprintf(buf, "<tr><td colspan=3>------------------------------------------------------</td></tr>\n");
121 
122   for (i=0; i < sink.num_srcs; i++) {
123     if (status_srcs_cpy[i]->addr != my_node_id) {
124       bufprintf(buf, "<tr><td>%d</td><td>%d</td><td>%s</td></tr>\n",
125                 status_srcs_cpy[i]->addr, 
126                 status_srcs_cpy[i]->battery_mv, 
127                 misc_print_date(&status_srcs_cpy[i]->time_battery_changed));
128     }
129   }
130 
131   bufprintf(buf, "</table><br>\n");
132   
133   return STATUS_MSG_COMPLETE;
134 }
135 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.