sflisten.c 1.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include <stdio.h>
#include <stdlib.h>

#include "serialsource.h"

static char *msgs[] = {
  "unknown_packet_type",
  "ack_timeout"	,
  "sync"	,
  "too_long"	,
  "too_short"	,
  "bad_sync"	,
  "bad_crc"	,
  "closed"	,
  "no_memory"	,
  "unix_error"
};

void stderr_msg(serial_source_msg problem)
{
  fprintf(stderr, "Note: %s\n", msgs[problem]);
}

int main(int argc, char **argv)
{
  serial_source src;

  if (argc != 3)
    {
      fprintf(stderr, "Usage: %s <device> <rate> - dump packets from a serial port\n", argv[0]);
      exit(2);
    }
  src = open_serial_source(argv[1], atoi(argv[2]), 0, stderr_msg);
  if (!src)
    {
      fprintf(stderr, "Couldn't open serial port at %s:%s\n",
	      argv[1], argv[2]);
      exit(1);
    }
  for (;;)
    {
      int len, i;
      const unsigned char *packet = read_serial_packet(src, &len);

      int rssi_val;
      int am_type;
      int orig_sender;
      float real_rssi;
      float vdc = 3.0f;


      if (!packet)
	exit(0);
      //for (i = 0; i < len; i++)
//	printf("%02x ", packet[i]);
      //printf("counter: %d RSSI: %d\n",packet[5],((packet[6] & 0xff) << 8) | (packet[7] & 0xff));

      rssi_val = ((packet[7] & 0xff) << 8) | (packet[6] & 0xff);
      am_type = packet[8];
      orig_sender = ((packet[10] & 0xff) << 8) | (packet[9] & 0xff);

      /* for mica 2, ALSO depends on the value of vdc; which for wall plugin
       * is 3 VDC; for 1.5V batts it's something else
       */ 
      real_rssi = -50.0f * (vdc * (rssi_val/1024.0f)) - 45.5f;
      
      printf("original sender: %d; AM type: %d; RSSI: %f dBm\n",
	     orig_sender,
	     am_type,
	     real_rssi);
      fflush(stdout);
      //putchar('\n');
      free((void *)packet);
    }
}