ll_com.c
Go to the documentation of this file.
00001 /*------------------------------------------------------------------------
00002  *---------------------           RT-WMP              --------------------
00003  *------------------------------------------------------------------------
00004  *                                                         V7.0B  11/05/10
00005  *
00006  *
00007  *  File: ./src/platforms/linux_us/hwi/sockets/ll_com.c
00008  *  Authors: Danilo Tardioli
00009  *  ----------------------------------------------------------------------
00010  *  Copyright (C) 2000-2010, Universidad de Zaragoza, SPAIN
00011  *
00012  *  Contact Addresses: Danilo Tardioli                   dantard@unizar.es
00013  *
00014  *  RT-WMP is free software; you can  redistribute it and/or  modify it
00015  *  under the terms of the GNU General Public License  as published by the
00016  *  Free Software Foundation;  either  version 2, or (at  your option) any
00017  *  later version.
00018  *
00019  *  RT-WMP  is distributed  in the  hope  that  it will be   useful, but
00020  *  WITHOUT  ANY  WARRANTY;     without  even the   implied   warranty  of
00021  *  MERCHANTABILITY  or  FITNESS FOR A  PARTICULAR PURPOSE.    See the GNU
00022  *  General Public License for more details.
00023  *
00024  *  You should have received  a  copy of  the  GNU General Public  License
00025  *  distributed with RT-WMP;  see file COPYING.   If not,  write to the
00026  *  Free Software  Foundation,  59 Temple Place  -  Suite 330,  Boston, MA
00027  *  02111-1307, USA.
00028  *
00029  *  As a  special exception, if you  link this  unit  with other  files to
00030  *  produce an   executable,   this unit  does  not  by  itself cause  the
00031  *  resulting executable to be covered by the  GNU General Public License.
00032  *  This exception does  not however invalidate  any other reasons why the
00033  *  executable file might be covered by the GNU Public License.
00034  *
00035  *----------------------------------------------------------------------*/
00036 
00037 #include <sys/types.h>
00038 #include <sys/time.h>
00039 #include <sys/socket.h>
00040 #include <arpa/inet.h>
00041 #include <unistd.h>
00042 
00043 #include <assert.h>
00044 #include <stdlib.h>
00045 #include <stdio.h>
00046 #include <string.h>
00047 #include "errno.h"
00048 #include <pthread.h>
00049 #include "core/include/definitions.h"
00050 #include "core/interface/wmp_interface.h"
00051 #include "core/include/wmp_misc.h"
00052 #include "core/include/frames.h"
00053 #include "config/compiler.h"
00054 #include "core/include/ll_com.h"
00055 
00056 static int rx, tx; /* TX and RX sockets*/
00057 static char IPBROADCAST[20];
00058 static char param[20], val[20];
00059 
00060 struct sockaddr_in rx_addr, tx_addr;
00061 
00062 int readllcfg() {
00063         char filename[256], line[256];
00064 
00065         sprintf(IPBROADCAST, "192.168.1.255");
00066 
00067         snprintf(filename, 256, "%s/.rt-wmp/rt-wmp-us-sock.ll", getenv("HOME"));
00068         FILE * f = fopen(filename, "r");
00069         if (f > 0) {
00070                 WMP_MSG(stderr,"Reading Low Level Configuration file (%s)... \n",filename);
00071                 while (fgets(line,256,f) != NULL) {
00072                         if (line[0]<65 || line[0]>90){
00073                                 continue;
00074                         }
00075                         sscanf(line,"%s %s",param,val);
00076                         int exists = 0;
00077                         if (strcmp(param, "IPBROADCAST") == 0) {
00078                                 strcpy(IPBROADCAST, val);
00079                                 exists = 1;
00080                         }
00081 
00082                         if (exists) {
00083                                 WMP_MSG(stderr, "READ OPTION: %s = %s\n", param, val);
00084                         } else {
00085                                 WMP_MSG(stderr, "*** UKNOWN OPTION %s = %s\n", param, val);
00086                         }
00087                         WMP_MSG(stderr,"Done.\n");
00088                 }
00089         } else {
00090                 WMP_MSG(stderr,"File %s not found, using default values\n",filename);
00091         }
00092         return 0;
00093 }
00094 
00095 void closeLowLevelCom() {
00096         close(rx);
00097         close(tx);
00098 }
00099 
00100 int initLowLevelCom() {
00101         int ret = 1, txport = 0x6969, rxport = 0x6969;
00102         readllcfg();
00103 
00104         WMP_MSG(stderr,"Using IP: %s\n",IPBROADCAST);
00105 
00106         memset(&rx_addr, 0, sizeof(rx_addr));
00107         rx_addr.sin_family = PF_INET;
00108         rx_addr.sin_port = htons(rxport);
00109         rx_addr.sin_addr.s_addr = inet_addr(IPBROADCAST);
00110 
00111         memset(&tx_addr, 0, sizeof(tx_addr));
00112         tx_addr.sin_family = PF_INET;
00113         tx_addr.sin_port = htons(txport);
00114         tx_addr.sin_addr.s_addr = inet_addr(IPBROADCAST);
00115 
00116         if ((rx = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
00117                 WMP_MSG(stderr,"Error creating RX socket.\n");
00118                 ret = 0;
00119         }
00120 
00121         if (bind(rx, (struct sockaddr*) &rx_addr, sizeof(struct sockaddr_in)) < 0) {
00122                 WMP_MSG(stderr,"RX Bind Error: % s.\n",inet_ntoa(rx_addr.sin_addr));
00123                 ret = 0;
00124         }
00125 
00126         if ((tx = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
00127                 WMP_MSG(stderr,"Error creating TX socket.\n");
00128                 ret = 0;
00129         }
00130 
00131         int val = 1, i;
00132         setsockopt(tx, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
00133         setsockopt(rx, SOL_SOCKET, SO_BROADCAST, &val, sizeof(val));
00134         setsockopt(rx, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
00135 
00136         if (connect(tx, (struct sockaddr*) &tx_addr, sizeof(struct sockaddr_in))
00137                         < 0) {
00138                 WMP_MSG(stderr,"Connect error: %s.\n",inet_ntoa(tx_addr.sin_addr));
00139                 ret = 0;
00140         }
00141         return ret;
00142 }
00143 
00144 int llsend(char * f, int size) {
00145         int nbytes = send(tx, f, size, 0);
00146         recvfrom(rx, 0, 0, 0, 0, 0);
00147         return nbytes;
00148 }
00149 
00150 rxInfo llreceive(char *f, int timeout) {
00151 
00152         int r = 0;
00153         struct timeval tv;
00154         rxInfo ret;
00155 
00156         ret.rate = 0;
00157         ret.has_lq = 0;
00158 
00159         if (timeout > 0) {
00160                 fd_set fd_rx;
00161                 tv.tv_sec = 0;
00162                 tv.tv_usec = 1000 * timeout;
00163 
00164                 FD_ZERO(&fd_rx);
00165                 FD_SET(rx, &fd_rx);
00166 
00167                 r = select(FD_SETSIZE, &fd_rx, NULL, NULL, &tv);
00168         } else {
00169                 r = 1;
00170         }
00171         if (r) {
00172                 /* if something is present read the frame and return the frame */
00173                 int rlen = recvfrom(rx, (char *) f, MTU, 0, 0, 0);
00174                 assert(rlen>0);
00175                 ret.size = rlen;
00176                 ret.proto = 0x6969;
00177                 ret.error = 0;
00178         } else {
00179                 ret.error = 1;
00180         }
00181         return ret;
00182 }


ros_rt_wmp
Author(s): Danilo Tardioli, dantard@unizar.es
autogenerated on Fri Jan 3 2014 12:07:55