sympathy_battery.c from EmStar at Krugle
Show sympathy_battery.c syntax highlighted
/* ex: set tabstop=2 expandtab shiftwidth=2 softtabstop=2: */
/*
*
* Copyright (c) 2003 The Regents of the University of California. All
* rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
/*
*
* Author: Nithya Ramanthan
*
*/
/*
* simple example of using a status device
*/
#include <sympathy.h>
int sbattery_write(status_context_t *info, char* command, size_t buf_size)
{
parser_state_t *ps = misc_parse_init(command, MISC_PARSE_COLON_SCHEME);
int node_id, sctr = -1;
int voltage = -1;
int retval = STATUS_WRITE_DONE;
while (misc_parse_next_kvp(ps) >= 0)
{
if (!ps->value) {
elog(LOG_ERR, "ERROR: No node-id returned with key: %s!\n",
ps->key);
retval = EVENT_ERROR(EINVAL);
goto done;
}
if (strcmp(ps->key, "node") == 0) {
node_id = atoi(ps->value);
sctr = find_status(node_id);
}
else if (strcmp(ps->key, "voltage") == 0) {
voltage = atoi(ps->value);
}
else {
elog(LOG_ERR, "Unrecognized key: %s\n", ps->key);
}
}
if ((sctr > -1) && (voltage >-1)) {
/* fixme For now make it a stupid check! */
if (voltage > sink.status_srcs[sctr].battery_mv) {
gettimeofday(&sink.status_srcs[sctr].time_battery_changed, NULL);
}
sink.status_srcs[sctr].battery_mv = voltage;
}
else {
elog(LOG_ERR, "ERROR: Either node-id or voltage was not provided (%s)!\n",
command);
retval = EVENT_ERROR(EINVAL);
goto done;
}
done:
misc_parse_cleanup(ps);
return retval;
}
int sbattery_print(status_context_t *info, buf_t *buf)
{
int i;
bufprintf(buf, "Node-Id\t Battery(mV)\t Date-last-changed\n");
bufprintf(buf, "-------\t -----------\t -----------------\n");
for (i=0; i < sink.num_srcs; i++) {
if (sink.status_srcs[i].addr != my_node_id) {
bufprintf(buf, "%d\t %d\t\t %s\n", sink.status_srcs[i].addr,
sink.status_srcs[i].battery_mv,
misc_print_date(&sink.status_srcs[i].time_battery_changed));
}
}
bufprintf(buf, "\nTo update a node's battery, write:\n");
bufprintf(buf, "\tnode=<node-id>:voltage=<int[milli-volts]>\n");
return STATUS_MSG_COMPLETE;
}
int sbattery_html(status_context_t *info, buf_t *buf)
{
int i;
sympathy_node_info_t* status_srcs_cpy[SMAX_SRCS];
order_by_id(status_srcs_cpy);
bufprintf(buf, "<br><table frame=void cellspacing=5 cellpadding=6>\n");
bufprintf(buf, "<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n",
"Node ID", "Battery(mV)", "Date-last-changed");
bufprintf(buf, "<tr><td colspan=3>------------------------------------------------------</td></tr>\n");
for (i=0; i < sink.num_srcs; i++) {
if (status_srcs_cpy[i]->addr != my_node_id) {
bufprintf(buf, "<tr><td>%d</td><td>%d</td><td>%s</td></tr>\n",
status_srcs_cpy[i]->addr,
status_srcs_cpy[i]->battery_mv,
misc_print_date(&status_srcs_cpy[i]->time_battery_changed));
}
}
bufprintf(buf, "</table><br>\n");
return STATUS_MSG_COMPLETE;
}
See more files for this project here