Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

packetread.h

Go to the documentation of this file.
00001 /*
00002 Opie-Remote.  emulates remote controlls on an iPaq (and maybe a Zaurus) in Opie.
00003 Copyright (C) 2002 Thomas Stephens
00004 
00005 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
00006 License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
00007 version.
00008 
00009 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
00010 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
00011 Public License for more details.
00012 
00013 You should have received a copy of the GNU General Public License along with this program; if not, write to the Free
00014 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00015 
00016 file not used
00017 */
00018 
00019 #include <stdio.h>
00020 #include <stdlib.h>
00021 #include <string.h>
00022 #include <sys/socket.h>
00023 #include <sys/types.h>
00024 #include <sys/un.h>
00025 #include <unistd.h>
00026 #include <errno.h>
00027 
00028 #define PACKET_SIZE 256
00029 #define TIMEOUT 3
00030 
00031 //this function was ripped for rc.c in xrc, it is available here: http://www.lirc.org/software.html
00032 const char *readPacket(int fd)
00033 {
00034         static char buffer[PACKET_SIZE+1]="";
00035         char *end;
00036         static int ptr=0,end_len=0;
00037         ssize_t ret;
00038 
00039         if(ptr>0)
00040         {
00041                 memmove(buffer,buffer+ptr,strlen(buffer+ptr)+1);
00042                 ptr=strlen(buffer);
00043                 end=strchr(buffer,'\n');
00044         }
00045         else
00046         {
00047                 end=NULL;
00048         }
00049         alarm(TIMEOUT);
00050         while(end==NULL)
00051         {
00052                 if(PACKET_SIZE<=ptr)
00053                 {
00054 //                      fprintf(stderr,"%s: bad packet\n",progname);
00055                         ptr=0;
00056                         return(NULL);
00057                 }
00058                 ret=read(fd,buffer+ptr,PACKET_SIZE-ptr);
00059 
00060                 if(ret<=0 || timeout)
00061                 {
00062                         if(timeout)
00063                         {
00064 //                              fprintf(stderr,"%s: timeout\n",progname);
00065                         }
00066                         else
00067                         {
00068                                 alarm(0);
00069                         }
00070                         ptr=0;
00071                         return(NULL);
00072                 }
00073                 buffer[ptr+ret]=0;
00074                 ptr=strlen(buffer);
00075                 end=strchr(buffer,'\n');
00076         }
00077         alarm(0);timeout=0;
00078 
00079         end[0]=0;
00080         ptr=strlen(buffer)+1;
00081 //#       ifdef DEBUG
00082 //      printf("buffer: -%s-\n",buffer);
00083 //#       endif
00084         return(buffer);
00085 }
00086 QStringList getRemotes(int fd)
00087 {
00088         const char write_buffer[] = "LIST\n";
00089         const char *readbuffer;
00090         int i, numlines;
00091         QStringList list;
00092 
00093         fd = socket(AF_UNIX, SOCK_STREAM, 0);
00094 
00095         if(std::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1)
00096         {
00097                 QMessageBox *mb = new QMessageBox("Error!",
00098                                                                                         "couldnt connect to socket",
00099                                                                                         QMessageBox::NoIcon,
00100                                                                                         QMessageBox::Ok,
00101                                                                                         QMessageBox::NoButton,
00102                                                                                         QMessageBox::NoButton);
00103                 mb->exec();
00104                 perror("ConfigTab::GetRemotes");
00105                 return NULL;
00106         }
00107 
00108         write(fd, write_buffer, strlen(write_buffer) );
00109 
00110         for(i=0; i<5; i++)
00111         {
00112                 readbuffer = readPacket(int fd);
00113         }
00114 
00115         numlines = atoi(readbuffer);
00116 
00117         for(i=0; i<numlines; i++)
00118         {
00119                 list+=readPacket();
00120         }
00121 
00122         if(strcasecmp(readPacket(), "END") != 0)
00123         {
00124                 QMessageBox *mb = new QMessageBox("Error!",
00125                                                                                         "bad packet",
00126                                                                                         QMessageBox::NoIcon,
00127                                                                                         QMessageBox::Ok,
00128                                                                                         QMessageBox::NoButton,
00129                                                                                         QMessageBox::NoButton);
00130                 mb->exec();
00131                 perror("ConfigTab::GetRemotes");
00132                 return NULL;
00133         }
00134 
00135         std::close(fd);
00136         return list;
00137 }
00138 
00139 QStringList getButtons(int fd, const char *remoteName)
00140 {
00141         QString write_buffer = "LIST ";
00142         const char *readbuffer;
00143         int i, j, numlines;
00144         QStringList list;
00145         QString string;
00146 
00147         write_buffer += remoteName;
00148         write_buffer += '\n';
00149 
00150         fd = socket(AF_UNIX, SOCK_STREAM, 0);
00151 
00152         if(std::connect(fd,(struct sockaddr *) &addr, sizeof(addr) ) == -1)
00153         {
00154                 QMessageBox *mb = new QMessageBox("Error!",
00155                                                                                         "couldnt connect to socket",
00156                                                                                         QMessageBox::NoIcon,
00157                                                                                         QMessageBox::Ok,
00158                                                                                         QMessageBox::NoButton,
00159                                                                                         QMessageBox::NoButton);
00160                 mb->exec();
00161                 perror("ConfigTab::GetRemotes");
00162                 return NULL;
00163         }
00164 
00165         write(fd, write_buffer.latin1(), strlen(write_buffer) );
00166 
00167         for(i=0; i<5; i++)
00168         {
00169                 readbuffer = readPacket();
00170         }
00171 
00172         numlines = atoi(readbuffer);
00173 
00174         for(i=0; i<numlines; i++)
00175         {
00176                 list+=readPacket();
00177                 for(j=0; j<list[i].length(); j++)
00178                 {
00179                         if(list[i][j] == ' ')
00180                                 break;
00181                 }
00182                 list[i].remove(0, j+1);
00183         }
00184 
00185         if(strcasecmp(readPacket(), "END") != 0)
00186         {
00187                 QMessageBox *mb = new QMessageBox("Error!",
00188                                                                                         "bad packet",
00189                                                                                         QMessageBox::NoIcon,
00190                                                                                         QMessageBox::Ok,
00191                                                                                         QMessageBox::NoButton,
00192                                                                                         QMessageBox::NoButton);
00193                 mb->exec();
00194                 perror("ConfigTab::GetRemotes");
00195                 return NULL;
00196         }
00197 
00198         std::close(fd);
00199         return list;
00200 }

Generated on Sat Nov 5 16:18:03 2005 for OPIE by  doxygen 1.4.2