gnokii-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Help understanding gnokii / Nokia FBUS please


From: root
Subject: Help understanding gnokii / Nokia FBUS please
Date: Wed, 30 Oct 2002 01:21:24 +0000
User-agent: KMail/1.4.1

I had a burrow through the xgnokii code to try and follow what was going on 
and how the communications take place with my nokia 3310. 

I tried to write a simple program to ping the phone (by cuting bits from 
xgnokii) but i dont seem to get any kind of responce from the phone (select 
never returns anything > 0).

I am new to linux programming so please forgive any obviouse blunder's but I 
would be most greatfull if someone could give me a clue as to what I am doing 
wrong.

Regard's

Rob Beattie


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/wait.h>


int main(int argc, char *argv[])
{
  int fd;
  int rt;
  termios tp;

  // O_NONBLOCK MUST be used here as the CLOCAL may be currently off
  // and if DCD is down the "open" syscall would be stuck wating for DCD.
  fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
  if (fd == -1)
  {
    perror("nping : ");
    return -1;
  }

  rt = tcgetattr(fd, &tp);
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  rt = tcflush(fd, TCIFLUSH);
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  tp.c_cflag = B0 | CS8 | CLOCAL | CREAD | HUPCL;
  tp.c_iflag = IGNPAR;
  tp.c_cflag &= ~CRTSCTS;
  tp.c_oflag = 0;
  tp.c_lflag = 0;
  tp.c_cc[VMIN] = 1;
  tp.c_cc[VTIME] = 0;


  // port attributes
  rt = tcsetattr(fd, TCSANOW, &tp);
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  rt = cfsetspeed(&tp, 9600);
  //rt = cfsetspeed(&tp, 19200);
  //rt = cfsetspeed(&tp, 115200);
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  // set the speed
  rt = tcsetattr(fd, TCSANOW, &tp);
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  // comment out ? we need to turn off O_NONBLOCK now (we have CLOCAL set so 
it is safe).
  // when we run some device script it really doesn't expect NONBLOCK!
  rt = fcntl(fd, F_SETFL, 0);
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  // allow process/thread to receive SIGIO
  rt = fcntl(fd, F_SETOWN, getpid());
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  // we need to supply FNONBLOCK (or O_NONBLOCK) again as it would get reset
  // by F_SETFL as a side-effect!
  rt = fcntl(fd, F_SETFL, FASYNC | FNONBLOCK);
  if (rt == -1)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  // comment out ? set DTR / RCS.
  unsigned int flags = TIOCM_DTR;
  ioctl(fd, TIOCMBIS, &flags);

  flags = TIOCM_RTS;
  ioctl(fd, TIOCMBIC, &flags);

  // init  the link
  unsigned char initc = 0x55;
  for (int c = 0; c < 255; c++)
  {
    usleep(100);
    rt = write(fd, &initc, 1);
    if (rt != 1)
    {
      perror("nping : ");
      close(fd);
      return -1;
    }
  }

  usleep(5000);

  // message to retrieve phone details
  unsigned char txm[255] = 
{0x1e,0x00,0x0c,0xd1,0x00,0x07,0x00,0x01,0x00,0x03,0x00,0x01,0x40,0x00,0x52,0xd5};
  rt = write(fd, txm, 16);
  if (rt != 16)
  {
    perror("nping : ");
    close(fd);
    return -1;
  }

  // wait for a responce
  unsigned char rxm[255] = {0};
  struct timeval to;
  fd_set fs;
  for (int j = 0; j < 50; j++)
  {

    FD_ZERO(&fs);
    FD_SET(fd, &fs);
    to.tv_sec = 0;
    to.tv_usec = 100000;
    rt = select(fd + 1, &fs, NULL, NULL, &to);
    if (rt > 0)
    {
      // would like to end up here !
      rt = read(fd, rxm, 255);
      close(fd);
      return 0;
    }
  }


  // allways end up here !
  close(fd);
  return -1;
}
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]