/ src / examples / groupreadresponse.c
groupreadresponse.c
  1  /*
  2      EIB Demo program - reads group telegrams
  3      Copyright (C) 2005-2011 Martin Koegler <mkoegler@auto.tuwien.ac.at>
  4  
  5      This program is free software; you can redistribute it and/or modify
  6      it under the terms of the GNU General Public License as published by
  7      the Free Software Foundation; either version 2 of the License, or
  8      (at your option) any later version.
  9  
 10      This program is distributed in the hope that it will be useful,
 11      but WITHOUT ANY WARRANTY; without even the implied warranty of
 12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13      GNU General Public License for more details.
 14  
 15      You should have received a copy of the GNU General Public License
 16      along with this program; if not, write to the Free Software
 17      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 18  */
 19  #include "common.h"
 20  #include <sys/types.h>
 21  #ifdef HAVE_SYS_TIME_H
 22  #include <sys/time.h>
 23  #endif
 24  
 25  int
 26  main (int ac, char *ag[])
 27  {
 28    int len;
 29    EIBConnection *con;
 30    fd_set read;
 31    eibaddr_t dest;
 32    eibaddr_t src;
 33    uint8_t req_buf[2] = { 0, 0 };
 34    uint8_t buf[200];
 35    struct timeval tv;
 36  
 37    if (ac != 3)
 38      die ("usage: %s url eibaddr", ag[0]);
 39    con = EIBSocketURL (ag[1]);
 40    if (!con)
 41      die ("Open failed");
 42    dest = readgaddr (ag[2]);
 43  
 44    if (EIBOpenT_Group (con, dest, 0) == -1)
 45      die ("Connect failed");
 46  
 47    len = EIBSendAPDU (con, 2, req_buf);
 48    if (len == -1)
 49      die ("Request failed");
 50    printf ("Send request\n");
 51  
 52    while (1)
 53      {
 54        tv.tv_usec=0;
 55        tv.tv_sec=10;
 56  lp:
 57        FD_ZERO (&read);
 58        FD_SET (EIB_Poll_FD (con), &read);
 59  
 60        if (select (EIB_Poll_FD (con) + 1, &read, 0, 0, &tv) == -1)
 61          die ("select failed");
 62  
 63        len = EIB_Poll_Complete (con);
 64        if (len == -1)
 65          die ("Read failed");
 66        if (len == 0)
 67          {
 68            if(tv.tv_sec == 0 && tv.tv_usec==0)
 69              goto end;
 70            goto lp;
 71          }
 72  
 73        len = EIBGetAPDU_Src (con, sizeof (buf), buf, &src);
 74        if (len == -1)
 75          die ("Read failed");
 76        if (len < 2)
 77          die ("Invalid Packet");
 78        if (buf[0] & 0x3 || (buf[1] & 0xC0) == 0xC0)
 79          {
 80            printf ("Unknown APDU from ");
 81            printIndividual (src);
 82            printf (": ");
 83            printHex (len, buf);
 84            printf ("\n");
 85          }
 86        else
 87          {
 88            switch (buf[1] & 0xC0)
 89              {
 90              case 0x00:
 91                printf ("Read");
 92                break;
 93              case 0x40:
 94                printf ("Response");
 95                break;
 96              case 0x80:
 97                printf ("Write");
 98                break;
 99              }
100            printf (" from ");
101            printIndividual (src);
102            if (buf[1] & 0xC0)
103              {
104                printf (": ");
105                if (len == 2)
106                  printf ("%02X", buf[1] & 0x3F);
107                else
108                  printHex (len - 2, buf + 2);
109              }
110            printf ("\n");
111            switch (buf[1] & 0xC0)
112              {
113              case 0x80:
114              case 0x40:
115                goto end;
116              }
117          }
118      }
119  end:
120    EIBClose (con);
121    return 0;
122  }