/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * For a copy of the GNU Library General Public License * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. or go to http://www.gnu.org * * $Id: netmeter.c,v 1.2 2005/04/15 16:25:25 mefemal Exp $ */ /* * Read the readings from the meter server for client print out. * These routines perform the appropriate actions to the mlogger meter * server and return the data. Most of these routines are stubs to * other lower-level implementation routines. For specific instructions * on semantic usage, reference the netmeter.h header file. */ #include #include #include "connectsock.h" #include "meter.h" extern int errno; static int sock; int netmeter_connect(char *hostname, unsigned short port) { sock = tcp_connect_port(hostname, port); return sock; } int netmeter_close() { int r; netmeter_req_t msg = { NETMETER_CMD_QUIT, NULL, 0, 0 }; r = write(sock, &msg, sizeof(netmeter_req_t)); return close(sock); } int netmeter_request(netmeter_req_t *msg) { int r; r = write(sock, msg, sizeof(netmeter_req_t)); if (r != sizeof(netmeter_req_t)) return -1; if (msg->cmd == NETMETER_CMD_RESET) /* no reply */ return r; r = read(sock, msg, sizeof(netmeter_req_t)); if (r != sizeof(netmeter_req_t)) return -1; return r; } int netmeter_convert(netmeter_req_t *msg, mm_t *mm) { if (msg == NULL || mm == NULL) { errno = EINVAL; return -1; } mm->units = msg->units; if (msg->cmd == NETMETER_CMD_READ) mm->value = (float)(((unsigned int)msg->value) / 100000.0); else mm->value = (float)(unsigned int)msg->value; return 0; }