Пример #1
0
int main (int argc, char *argv[])
{
  FILE *fp;
  uint8_t tx_buf[128];
  uint8_t rx_buf[128];
  TRANSDUCER_PKT_T	tran_pkt;
  TRANSDUCER_MSG_T	tran_msg;
  int32_t v,cnt,i,len,j;
  uint8_t nav_time_secs, reply_time_secs;
  int32_t tmp;
  time_t reply_timeout,nav_timeout,t;
  uint8_t cmd,error;
  char buf[1024];
  char mbuf[128];
  uint8_t num_msgs,p2p_mode,echo;
  FF_POWER_ACTUATE_PKT	ff_pwr_actuate[10];
  uint8_t ff_pwr_actuate_mac[10];
  uint8_t ff_pwr_actuate_ack[10];

debug_txt_flag=0;
p2p_mode=0;

  if (argc < 6 ) {
    printf ("Usage: server port num-msgs [mac-addr socket state] [-m or p]\n");
    printf ("  ex: %s localhost 5000 1 0x000000f0 1 on\n",argv[0]);
    printf ("  mac-addr  MAC address of node to actuate\n");
    printf ("  socket    0 or 1\n");
    printf ("  state     on or off\n");
    printf ("  -d        debug mode\n");
    printf ("  -m        multi-cast\n");
    printf ("  -p        P2P packet mode\n");
    exit (1);
  }

  for(i=0; i<argc; i++ )
	{
	// Grab dash command line options
	if(strstr(argv[i],"-d")!=NULL )
		debug_txt_flag=1;
	if(strcmp(argv[i],"-p")==0)
		p2p_mode=1;
	if(strcmp(argv[i],"-m")==0)
		p2p_mode=0;
	}

  sscanf( argv[3],"%d",&tmp);
	num_msgs=tmp;
 
  if(p2p_mode==1 && num_msgs!=1 )
	{
	printf( "Error: Can't send multi-cast p2p message\n" );
	return -1;
	} 
  if(num_msgs>9 ) { printf( "Sorry, too many messages...\n" ); return 0; }
  if(debug_txt_flag==1)
  printf( "Composing %d actuation msgs\n", num_msgs );
  for(i=0; i<num_msgs; i++ )
  {
	sscanf( argv[(i*3)+4],"%x",&tmp );
	subnet_3=(tmp&0xff000000) >> 24;
	subnet_2=(tmp&0xff0000) >> 16;
	subnet_1=(tmp&0xff00) >> 8;
	ff_pwr_actuate_mac[i]=(tmp & 0xff);	
	ff_pwr_actuate_ack[i]=0;	
  	if(debug_txt_flag==1) printf( "MAC_ADDR: 0x%x ",ff_pwr_actuate_mac[i]);
	sscanf( argv[(i*3)+5],"%d",&tmp );
  	if(debug_txt_flag==1)
		printf( " socket 0 " );
	ff_pwr_actuate[i].socket0_state=SOCKET_HOLD;
	ff_pwr_actuate[i].socket1_state=SOCKET_HOLD;
	if( strstr(argv[(i*3)+6],"on")!=NULL ) 
		{
			if(tmp==0) ff_pwr_actuate[i].socket0_state=SOCKET_ON;
			else ff_pwr_actuate[i].socket1_state=SOCKET_ON;
		}	
	else 
		{
			if(tmp==0) ff_pwr_actuate[i].socket0_state=SOCKET_OFF;
			else ff_pwr_actuate[i].socket1_state=SOCKET_OFF;
		}
  	if(debug_txt_flag==1)
		{
			printf( "Socket MAC %d\n",ff_pwr_actuate_mac[i] );
			printf( "    Socket 0:  %d\n",ff_pwr_actuate[i].socket0_state );
			printf( "    Socket 1:  %d\n",ff_pwr_actuate[i].socket1_state );
		}
  }

  v=slipstream_open(argv[1],atoi(argv[2]),NONBLOCKING);
  nav_time_secs=25; 

  cnt = 0;
  while (1) {
     error=0;
     cmd=0;


	retry:
	// Setup the packet to send out to the network

if(!p2p_mode)
{
	// These values setup the internal data structure and probably don't
	// need to be changed
	ds_pkt.payload_len=0;
	ds_pkt.buf=tx_buf;
	ds_pkt.buf_len=DS_PAYLOAD_START;
	ds_pkt.payload_start=DS_PAYLOAD_START;
	ds_pkt.payload=&(tx_buf[DS_PAYLOAD_START]);
	
	// These are parameters that can be adjusted for different packets
	//ds_pkt.pkt_type=PING_PKT; 
	ds_pkt.pkt_type=TRANSDUCER_PKT; 
	ds_pkt.ctrl_flags= DS_MASK | DEBUG_FLAG | ENCRYPT; 
	//srand(time(NULL));
	//ds_pkt.seq_num=rand()%255; // Use the gateway's spiffy auto-cnt when set to 0
	ds_pkt.seq_num=0; // Use the gateway's spiffy auto-cnt when set to 0
        if(debug_txt_flag==1)
		printf( "rand seq-num=%d\n",ds_pkt.seq_num );
	ds_pkt.priority=0;
	ds_pkt.ack_retry=10;
	ds_pkt.subnet_mac[0]=subnet_1;
	ds_pkt.subnet_mac[1]=subnet_2;
	ds_pkt.subnet_mac[2]=subnet_3;
	ds_pkt.hop_cnt=0;  // Starting depth, always keep at 0
	ds_pkt.hop_max=5;  // Max tree depth
	ds_pkt.delay_per_level=0;  // Reply delay per level in seconds
	ds_pkt.nav=0;  // Time in seconds until next message to be sent
	ds_pkt.mac_check_rate=100;  // B-mac check rate in ms
	ds_pkt.rssi_threshold=-45;  // Reply RSSI threshold
	ds_pkt.last_hop_mac=0;
	ds_pkt.mac_filter_num=0; // Increase if MAC_FILTER is active
	ds_pkt.aes_ctr[0]=0;   // Encryption AES counter
	ds_pkt.aes_ctr[1]=0;
	ds_pkt.aes_ctr[2]=0;
	ds_pkt.aes_ctr[3]=0;
} else
{
	// These values setup the internal data structure and probably don't
	// need to be changed
	p2p_pkt.payload_len=0;
	p2p_pkt.buf=tx_buf;
	p2p_pkt.buf_len=P2P_PAYLOAD_START;
	p2p_pkt.payload_start=P2P_PAYLOAD_START;
	p2p_pkt.payload=&(tx_buf[P2P_PAYLOAD_START]);
	
	// These are parameters that can be adjusted for different packets
	//p2p_pkt.pkt_type=PING_PKT; 
	p2p_pkt.pkt_type=TRANSDUCER_PKT; 
	p2p_pkt.ctrl_flags= MOBILE_MASK | LINK_ACK | DEBUG_FLAG | ENCRYPT; 
	p2p_pkt.seq_num=0; // Use the gateway's spiffy auto-cnt when set to 0
	//p2p_pkt.seq_num=rand()%255; // Use the gateway's spiffy auto-cnt when set to 0
        if(debug_txt_flag==1)
		printf( "rand seq-num=%d\n",p2p_pkt.seq_num );
	p2p_pkt.priority=0;
	p2p_pkt.hop_cnt=0;
	p2p_pkt.ttl=5;
	p2p_pkt.ack_retry=10;
	p2p_pkt.src_subnet_mac[0]=0;
	p2p_pkt.src_subnet_mac[1]=0;
	p2p_pkt.src_subnet_mac[2]=0;
	p2p_pkt.src_mac=0;
	p2p_pkt.last_hop_mac=0;
	p2p_pkt.next_hop_mac=BROADCAST;
	p2p_pkt.dst_subnet_mac[0] = subnet_1;
    	p2p_pkt.dst_subnet_mac[1] = subnet_2;
    	p2p_pkt.dst_subnet_mac[2] = subnet_3;
    	p2p_pkt.dst_mac = ff_pwr_actuate_mac[0];
	p2p_pkt.check_rate=100;  // B-mac check rate in ms


}

	// At the top level you have a SAMPL packet (ds_pkt)
	// For transducers, this holds a single transducer pkt (tran_pkt)
	// Each transducer packet contains multiple transducer messages (tran_msg)
	// Transducers typically have helper functions used to pack the transducer messages.
	
	// Setup Transducer Packet
	tran_pkt.num_msgs=0;	
	tran_pkt.checksum=0;	
	tran_pkt.msgs_payload=buf;	

for(i=0; i<num_msgs; i++ )
{
	// Setup Transducer Message
	tran_msg.mac_addr = ff_pwr_actuate_mac[i];
	tran_msg.type = TRAN_POWER_PKT;
	tran_msg.len = 0;
	tran_msg.payload = mbuf;

	// Pack application specifc message into transducer message
	tran_msg.len=ff_power_actuate_pack(mbuf, &(ff_pwr_actuate[i]));

	// Add the transducer message to the transducer packet
	len=transducer_msg_add( &tran_pkt, &tran_msg);
//	printf( "payload = " );
//	for(j=0; j<len; j++ )
//		printf( "%d ",ds_pkt.payload[j] );

//	printf( "\n" );
}
    
if(!p2p_mode)
{
	// Add the packet to the payload 
	ds_pkt.payload_len = transducer_pkt_pack(&tran_pkt, ds_pkt.payload); 

        // This takes the structure and packs it into the raw
	// array that is sent using SLIP
        pack_downstream_packet( &ds_pkt);	

     // Add MAC filter entries below
     //  downstream_packet_add_mac_filter( &ds_pkt, 0x07 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 3 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 4 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 5 ); 

    // Print your packet on the screen
    if(debug_txt_flag==1)
	print_ds_packet(&ds_pkt );
    if(error==0) 
    	v=slipstream_send(ds_pkt.buf,ds_pkt.buf_len);
    //nav_time_secs=ds_pkt.nav;
    nav_time_secs=5;
    reply_time_secs=ds_pkt.delay_per_level * ds_pkt.hop_max;
}
else {
    p2p_pkt.payload_len =
      transducer_pkt_pack(&tran_pkt,p2p_pkt.payload);
         // This takes the structure and packs it into the raw
	// array that is sent using SLIP
        pack_peer_2_peer_packet( &p2p_pkt);	

    if(error==0) 
    	v=slipstream_send(p2p_pkt.buf,p2p_pkt.buf_len);

    if(debug_txt_flag==1)
    {
    if (v == 0) printf( "Error sending\n" );
    else printf( "Sent request %d\n",p2p_pkt.seq_num);
    }
 
    nav_time_secs=10;
    reply_time_secs=10;

}



    if(debug_txt_flag==1)
    {
    if (v == 0) printf( "Error sending\n" );
    else printf( "Sent request %d\n",cnt);
    }
 

   t=time(NULL);
   reply_timeout=t+reply_time_secs+1;
   nav_timeout=t+nav_time_secs;

   echo=0;

    while (nav_timeout > time (NULL)) {
      v = slipstream_receive (rx_buf);
      if (v > 0) {
	if (check_for_ack(rx_buf,v, ff_pwr_actuate_mac[0], p2p_pkt.seq_num)==1 ) { printf( "ACK\n" ); return 1; }
      }
      usleep (1000);
    }

    cnt++;
    if(cnt>2) break;
  }
printf( "NCK\n" );
return 0;
}
Пример #2
0
int main (int argc, char *argv[])
{
  FILE *fp;
  uint8_t tx_buf[128];
  uint8_t rx_buf[128];
  TRANSDUCER_PKT_T	tran_pkt;
  TRANSDUCER_MSG_T	tran_msg;
  int32_t v,cnt,i,len,j;
  uint8_t nav_time_secs, reply_time_secs;
  int32_t tmp;
  time_t reply_timeout,nav_timeout,t;
  uint8_t cmd,error;
  char buf[1024];
  char mbuf[128];
  uint8_t num_msgs;
  FF_POWER_RQST_PKT	ff_pwr_rqst;
  uint8_t ff_pwr_mac[10];
  SAMPL_GATEWAY_PKT_T gw_pkt;

debug_txt_flag=0;

  if (argc < 4 ) {
    printf ("Usage: server port num-msgs [mac-addr] [-d]\n");
    printf ("  ex: ./jigo-ctrl localhost 5000 1 0x000000f0\n");
    printf ("  mac-addr MAC address of node to request read\n");
    printf ("  d Debug Output\n");
    exit (1);
  }

  for(i=0; i<argc; i++ )
	{
	// Grab dash command line options
	if(strstr(argv[i],"-d")!=NULL )
		{	
		debug_txt_flag=1;
		}
	}

  sscanf( argv[3],"%d", &tmp);
	num_msgs=tmp;
  if(num_msgs>9 ) { printf( "Sorry, too many messages...\n" ); return 0; }
  if(debug_txt_flag==1)
  printf( "Composing %d actuation msgs\n", num_msgs );
  for(i=0; i<num_msgs; i++ )
  {
	sscanf( argv[i+4],"%x",&tmp );
	subnet_3=(tmp&0xff000000) >> 24;
	subnet_2=(tmp&0xff0000) >> 16;
	subnet_1=(tmp&0xff00) >> 8;
	ff_pwr_mac[i]=(tmp & 0xff);	
  	if(debug_txt_flag==1) printf( "MAC_ADDR: 0x%x ",ff_pwr_mac[i]);
  }

  v=slipstream_open(argv[1],atoi(argv[2]),NONBLOCKING);
  nav_time_secs=25; 

  cnt = 0;
  while (1) {
     error=0;
     cmd=0;


	// Setup the packet to send out to the network

	// These values setup the internal data structure and probably don't
	// need to be changed
	ds_pkt.payload_len=0;
	ds_pkt.buf=tx_buf;
	ds_pkt.buf_len=DS_PAYLOAD_START;
	ds_pkt.payload_start=DS_PAYLOAD_START;
	ds_pkt.payload=&(tx_buf[DS_PAYLOAD_START]);
	
	// These are parameters that can be adjusted for different packets
	//ds_pkt.pkt_type=PING_PKT; 
	ds_pkt.pkt_type=TRANSDUCER_PKT; 
	ds_pkt.ctrl_flags= DS_MASK | DEBUG_FLAG | ENCRYPT; 
	ds_pkt.seq_num=0; // Use the gateway's spiffy auto-cnt when set to 0
	ds_pkt.priority=0;
	ds_pkt.ack_retry=10;
	ds_pkt.subnet_mac[0]=subnet_1;
	ds_pkt.subnet_mac[1]=subnet_2;
	ds_pkt.subnet_mac[2]=subnet_3;
	ds_pkt.hop_cnt=0;  // Starting depth, always keep at 0
	ds_pkt.hop_max=5;  // Max tree depth
	ds_pkt.delay_per_level=1;  // Reply delay per level in seconds
	ds_pkt.nav=0;  // Time in seconds until next message to be sent
	ds_pkt.mac_check_rate=100;  // B-mac check rate in ms
	ds_pkt.rssi_threshold=-45;  // Reply RSSI threshold
	ds_pkt.last_hop_mac=0;
	ds_pkt.mac_filter_num=0; // Increase if MAC_FILTER is active
	ds_pkt.aes_ctr[0]=0;   // Encryption AES counter
	ds_pkt.aes_ctr[1]=0;
	ds_pkt.aes_ctr[2]=0;
	ds_pkt.aes_ctr[3]=0;


	// At the top level you have a SAMPL packet (ds_pkt)
	// For transducers, this holds a single transducer pkt (tran_pkt)
	// Each transducer packet contains multiple transducer messages (tran_msg)
	// Transducers typically have helper functions used to pack the transducer messages.
	
	// Setup Transducer Packet
	tran_pkt.num_msgs=0;	
	tran_pkt.checksum=0;	
	tran_pkt.msgs_payload=buf;	

	ff_pwr_rqst.socket=0;
	ff_pwr_rqst.pkt_type=DEBUG_PKT;

for(i=0; i<num_msgs; i++ )
{
	// Setup Transducer Message
	tran_msg.mac_addr = ff_pwr_mac[i];
	tran_msg.type = TRAN_POWER_PKT;
	tran_msg.len = 0;
	tran_msg.payload = mbuf;

	printf( "calling pack on mac %d\n",ff_pwr_mac[i] );
	// Pack application specifc message into transducer message
	tran_msg.len=ff_power_rqst_pack(mbuf, &ff_pwr_rqst);

	printf( "calling add\n" );
	// Add the transducer message to the transducer packet
	len=transducer_msg_add( &tran_pkt, &tran_msg);
	printf( "done with add payload len=%d\n",len );
//	printf( "payload = " );
//	for(j=0; j<len; j++ )
//		printf( "%d ",ds_pkt.payload[j] );

//	printf( "\n" );
}
    
	// Add the packet to the payload 
	ds_pkt.payload_len = transducer_pkt_pack(&tran_pkt, ds_pkt.payload); 

        // This takes the structure and packs it into the raw
	// array that is sent using SLIP
        pack_downstream_packet( &ds_pkt);	

     // Add MAC filter entries below
     //  downstream_packet_add_mac_filter( &ds_pkt, 0x07 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 3 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 4 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 5 ); 

    // Print your packet on the screen
    if(debug_txt_flag==1)
	print_ds_packet(&ds_pkt );


    cnt++;

    for(i=0; i<ds_pkt.buf_len; i++ )
    {
	printf( "%02x",ds_pkt.buf[i] );
    }
    printf( "\n" );
    if(error==0) 
    	v=slipstream_send(ds_pkt.buf,ds_pkt.buf_len);

    if(debug_txt_flag==1)
    {
    if (v == 0) printf( "Error sending\n" );
    else printf( "Sent request %d\n",ds_pkt.seq_num);
    }
 
    //nav_time_secs=ds_pkt.nav;
    nav_time_secs=5;
    reply_time_secs=ds_pkt.delay_per_level * ds_pkt.hop_max+3;

   t=time(NULL);
   reply_timeout=t+reply_time_secs+1;
   nav_timeout=t+nav_time_secs;

   // Collect Reply packets 
   while(reply_timeout>time(NULL))
   	{
    		v=slipstream_receive( rx_buf);
    		if (v > 0) {
    			gw_pkt.buf=rx_buf;
        		  gw_pkt.buf_len=v;
        		  print_gw_packet_elements(&gw_pkt);
	
		}
    		usleep(1000);
   	}

   // What for NAV and service incoming messages 
   // This is the time window when the network is idle and can
   // be used for asynchronous communications.
   while(nav_timeout>time(NULL))
   	{
    		v=slipstream_receive( rx_buf);
    		if (v > 0) {
      			// Check for mobile/p2p packets
    			gw_pkt.buf=rx_buf;
        		  gw_pkt.buf_len=v;
        		  print_gw_packet_elements(&gw_pkt);
    			}
    		usleep(1000);
   	}
	
	// only run once
	break;
}
}
Пример #3
0
int main (int argc, char *argv[])
{
  FILE *fp;
  uint8_t tx_buf[128];
  uint8_t rx_buf[128];
  TRANSDUCER_PKT_T	tran_pkt;
  TRANSDUCER_MSG_T	tran_msg;
  int32_t v,cnt,i,len,j;
  uint8_t nav_time_secs, reply_time_secs;
  int32_t tmp;
  time_t reply_timeout,nav_timeout,t;
  uint8_t cmd,error;
  char buf[1024];
  char mbuf[128];
  uint8_t p2p_mode, retry_flag;
  uint8_t node_mac_addr;
  char* lcd_msg; // Message to send to LCD

  debug_txt_flag=0;
  p2p_mode=1;
  retry_flag=0;

  if(argc < 5 || argc > 7)
  {
    printf("Usage: %s server port mac-addr \'message\' [-p] [-d]\n", argv[0]);
    printf("Example: %s localhost 5000 0x00000001 \'Hello\\nWorld!\' -p\n", argv[0]);
    printf("  mac-addr     MAC address of node to display LCD message\n");
    printf("  -p           Use P2P packet mode\n");
    printf("  -m           Multi-cast\n");
    printf("  -d           Display debugging output\n");
    printf("  -r           Local Retry, don't use with slip mirror gateway\n");
    printf("  -l           Use Link ACK routing\n");
    exit(1);
  }

  // Grab dash command line options
  if(argc > 5)
  {
    for(i = 5; i < argc; i++)
    {
      if(strcmp(argv[i], "-p") == 0)
      {
        p2p_mode = 1;
        printf("*P2P Mode Enabled\n");
      }
      if(strcmp(argv[i], "-m") == 0)
      {
        p2p_mode = 0;
        printf("*Multi-cast Mode Enabled\n");
      }
       else if(strcmp(argv[i], "-d") == 0)
      {
        debug_txt_flag = 1;
        printf("*Debug Mode Enabled\n");
      }
        else if(strcmp(argv[i], "-r") == 0)
      {
        retry_flag= 1;
        printf("*Retry Enabled\n");
      }
}
  }

  if (strlen (argv[3]) != 8 && strlen (argv[3]) != 10) {
    printf ("Invalid MAC address\n");
    exit(1);
  }
  v = sscanf (argv[3], "%x", &tmp);
  if (v != 1) {
    printf ("Invalid MAC address\n");
    exit(1);
  }
  subnet_3=(tmp&0xff000000) >> 24;
  subnet_2=(tmp&0xff0000) >> 16;
  subnet_1=(tmp&0xff00) >> 8;
  node_mac_addr = tmp & 0xff;

  v = slipstream_open (argv[1], atoi (argv[2]), NONBLOCKING);
  
  // Set pointer to message
  lcd_msg = (char*)argv[4];

  nav_time_secs = 25;

  cnt = 0;
  while (1) {
     error=0;
     cmd=0;


	retry:
	// Setup the packet to send out to the network

if(!p2p_mode)
{
	// These values setup the internal data structure and probably don't
	// need to be changed
	ds_pkt.payload_len=0;
	ds_pkt.buf=tx_buf;
	ds_pkt.buf_len=DS_PAYLOAD_START;
	ds_pkt.payload_start=DS_PAYLOAD_START;
	ds_pkt.payload=&(tx_buf[DS_PAYLOAD_START]);
	
	// These are parameters that can be adjusted for different packets
	//ds_pkt.pkt_type=PING_PKT; 
	ds_pkt.pkt_type=TRANSDUCER_PKT; 
	ds_pkt.ctrl_flags= DS_MASK | DEBUG_FLAG | ENCRYPT; 
	//srand(time(NULL));
	//ds_pkt.seq_num=rand()%255; // Use the gateway's spiffy auto-cnt when set to 0
	ds_pkt.seq_num=0; // Use the gateway's spiffy auto-cnt when set to 0
        if(debug_txt_flag==1)
		printf( "rand seq-num=%d\n",ds_pkt.seq_num );
	ds_pkt.priority=0;
	ds_pkt.ack_retry=10;
	ds_pkt.subnet_mac[0]=subnet_1;
	ds_pkt.subnet_mac[1]=subnet_2;
	ds_pkt.subnet_mac[2]=subnet_3;
	ds_pkt.hop_cnt=0;  // Starting depth, always keep at 0
	ds_pkt.hop_max=7;  // Max tree depth
	ds_pkt.delay_per_level=0;  // Reply delay per level in seconds
	ds_pkt.nav=0;  // Time in seconds until next message to be sent
	ds_pkt.mac_check_rate=100;  // B-mac check rate in ms
	ds_pkt.rssi_threshold=-35;  // Reply RSSI threshold
	ds_pkt.last_hop_mac=0;
	ds_pkt.mac_filter_num=0; // Increase if MAC_FILTER is active
	ds_pkt.aes_ctr[0]=0;   // Encryption AES counter
	ds_pkt.aes_ctr[1]=0;
	ds_pkt.aes_ctr[2]=0;
	ds_pkt.aes_ctr[3]=0;
} else
{
	// These values setup the internal data structure and probably don't
	// need to be changed
	p2p_pkt.payload_len=0;
	p2p_pkt.buf=tx_buf;
	p2p_pkt.buf_len=P2P_PAYLOAD_START;
	p2p_pkt.payload_start=P2P_PAYLOAD_START;
	p2p_pkt.payload=&(tx_buf[P2P_PAYLOAD_START]);
	
	// These are parameters that can be adjusted for different packets
	//p2p_pkt.pkt_type=PING_PKT; 
	p2p_pkt.pkt_type=TRANSDUCER_PKT; 
//	p2p_pkt.ctrl_flags= MOBILE_MASK | /*LINK_ACK |*/ DEBUG_FLAG | ENCRYPT; 
	p2p_pkt.ctrl_flags= MOBILE_MASK | LINK_ACK | DEBUG_FLAG | ENCRYPT; 
	//p2p_pkt.ctrl_flags= MOBILE_MASK | DEBUG_FLAG | ENCRYPT; 
	p2p_pkt.seq_num=0; // Use the gateway's spiffy auto-cnt when set to 0
	//p2p_pkt.seq_num=rand()%255; // Use the gateway's spiffy auto-cnt when set to 0
        if(debug_txt_flag==1)
		printf( "rand seq-num=%d\n",p2p_pkt.seq_num );
	p2p_pkt.priority=0;
	p2p_pkt.hop_cnt=0;
	p2p_pkt.ttl=7;
	p2p_pkt.ack_retry=10;
	p2p_pkt.src_subnet_mac[0]=0;
	p2p_pkt.src_subnet_mac[1]=0;
	p2p_pkt.src_subnet_mac[2]=0;
	p2p_pkt.src_mac=0;
	p2p_pkt.last_hop_mac=0;
	p2p_pkt.next_hop_mac=BROADCAST;
	p2p_pkt.dst_subnet_mac[0] = 0;
    	p2p_pkt.dst_subnet_mac[1] = 0;
    	p2p_pkt.dst_subnet_mac[2] = 0;
    	p2p_pkt.dst_mac = node_mac_addr;
	p2p_pkt.check_rate=100;  // B-mac check rate in ms


}

	// At the top level you have a SAMPL packet (ds_pkt)
	// For transducers, this holds a single transducer pkt (tran_pkt)
	// Each transducer packet contains multiple transducer messages (tran_msg)
	// Transducers typically have helper functions used to pack the transducer messages.
	
	// Setup Transducer Packet
	tran_pkt.num_msgs=0;	
	tran_pkt.checksum=0;	
	tran_pkt.msgs_payload=buf;

        // Build message to send to LCD
        tran_msg.mac_addr = node_mac_addr;
        tran_msg.type = TRAN_LCD_MESSAGE;
        tran_msg.len = strlen(lcd_msg) + 1;
        for(i = 0; i < tran_msg.len; i++)
          mbuf[i] = (uint8_t)lcd_msg[i];
        tran_msg.payload = mbuf;

        // Add the transducer message to the transducer packet
        transducer_msg_add( &tran_pkt, &tran_msg);
   
if(!p2p_mode)
{
	// Add the packet to the payload 
	ds_pkt.payload_len = transducer_pkt_pack(&tran_pkt, ds_pkt.payload); 

        // This takes the structure and packs it into the raw
	// array that is sent using SLIP
        pack_downstream_packet( &ds_pkt);	

     // Add MAC filter entries below
     //  downstream_packet_add_mac_filter( &ds_pkt, 0x07 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 3 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 4 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 5 ); 

    // Print your packet on the screen
    if(debug_txt_flag==1)
	print_ds_packet(&ds_pkt );
    if(error==0)
    {
    	v=slipstream_send(ds_pkt.buf,ds_pkt.buf_len);
      for(i=0; i<100; i++ )
      {
        v = slipstream_receive (rx_buf);
        if(v==1 && rx_buf[0]=='A' ) { printf( "Serial Good\n" ); break;}
        if(v==1 && rx_buf[0]=='N') {
			printf( "Serial Error 1\n" );
			}
	usleep(1000);
      }
      if(i==100) printf( "Serial Error 2\n" );
    }
    //nav_time_secs=ds_pkt.nav;
    nav_time_secs=5;
    reply_time_secs=ds_pkt.delay_per_level * ds_pkt.hop_max;
}
else {
    p2p_pkt.payload_len =
      transducer_pkt_pack(&tran_pkt,p2p_pkt.payload);
         // This takes the structure and packs it into the raw
	// array that is sent using SLIP
        pack_peer_2_peer_packet( &p2p_pkt);	

    if(error==0) 
    {
	    if(retry_flag)
    	v=slipstream_acked_send(p2p_pkt.buf,p2p_pkt.buf_len,3);
    	else 
		v=slipstream_send(p2p_pkt.buf,p2p_pkt.buf_len);
    }
  


    if(debug_txt_flag==1)
    {
    if (v == 0) printf( "Serial Error\n" );
    else printf( "Sent request %d\n",p2p_pkt.seq_num);
    }
 
    nav_time_secs=5;
    reply_time_secs=5;

}

    if(debug_txt_flag==1)
    {
    if (v == 0) printf( "Error sending\n" );
    else printf( "Sent request %d\n",cnt);
    }

    //LCD message sent
    if(v > 0)
      printf("[%i] Message \'%s\' sent to node 0x%x.\n", cnt, lcd_msg, node_mac_addr);

    t=time(NULL);
    reply_timeout=t+reply_time_secs+1;
    nav_timeout=t+nav_time_secs;


    while (nav_timeout > time (NULL)) {
      v = slipstream_receive (rx_buf);
      if (v > 0) {
	if (check_for_ack(rx_buf,v, node_mac_addr, p2p_pkt.seq_num)==1 ) { printf( "ACK\n" ); return 1; }
      }
      usleep (1000);
    }

    cnt++;
    if(cnt>2) break;
  }
printf( "NCK\n" );
return 0;
}
Пример #4
0
int main (int argc, char *argv[])
{
  FILE *fp;
  uint8_t tx_buf[128];
  uint8_t rx_buf[128];
  int32_t v,cnt,i,len;
  uint8_t nav_time_secs, reply_time_secs,checksum;
  int32_t tmp;
  time_t reply_timeout,nav_timeout,t;
  uint8_t cmd,error;
  char buf[1024];
  time_t clockt;
  uint32_t epocht;
  SAMPL_GATEWAY_PKT_T gw_pkt;

debug_txt_flag=0;

  if (argc < 3 || argc > 4) {
    printf ("Usage: server port [-d]\n");
    printf ("  d Debug Output\n");
    exit (1);
  }

  if(argc==4)
	{
	// Grab dash command line options
	if(strstr(argv[3],"d")!=NULL )
		{	
		debug_txt_flag=1;
		}
	}


  v=slipstream_open(argv[1],atoi(argv[2]),NONBLOCKING);
  nav_time_secs=25; 

  cnt = 0;
  while (1) {
     error=0;
     cmd=0;


	// Setup the packet to send out to the network

	// These values setup the internal data structure and probably don't
	// need to be changed
	ds_pkt.payload_len=0;
	ds_pkt.buf=tx_buf;
	ds_pkt.buf_len=DS_PAYLOAD_START;
	ds_pkt.payload_start=DS_PAYLOAD_START;
	ds_pkt.payload=&(tx_buf[DS_PAYLOAD_START]);
	
	// These are parameters that can be adjusted for different packets
	ds_pkt.pkt_type=CONTROL_PKT; 
	ds_pkt.ctrl_flags= ENCRYPT | DS_MASK | LINK_ACK | DEBUG_FLAG; 
	ds_pkt.seq_num=0;
	ds_pkt.priority=0;
	ds_pkt.ack_retry=10;
	ds_pkt.subnet_mac[0]=0;
	ds_pkt.subnet_mac[1]=0;
	ds_pkt.subnet_mac[2]=0;
	ds_pkt.hop_cnt=0;  // Starting depth, always keep at 0
	ds_pkt.hop_max=5;  // Max tree depth
	ds_pkt.delay_per_level=1;  // Reply delay per level in seconds
	ds_pkt.nav=30;  // Time in seconds until next message to be sent
	ds_pkt.mac_check_rate=100;  // B-mac check rate in ms
	ds_pkt.rssi_threshold=-40;  // Reply RSSI threshold
	ds_pkt.last_hop_mac=0;
	ds_pkt.mac_filter_num=0; // Increase if MAC_FILTER is active
	ds_pkt.aes_ctr[0]=0;   // Encryption AES counter
	ds_pkt.aes_ctr[1]=0;
	ds_pkt.aes_ctr[2]=0;
	ds_pkt.aes_ctr[3]=0;

	clockt = time(NULL);
    	epocht = clockt;
    	ds_pkt.epoch_time[0] = epocht & 0xff;      // Epoch time 
    	ds_pkt.epoch_time[1] = (epocht>>8) & 0xff;    
    	ds_pkt.epoch_time[2] = (epocht>>16) & 0xff;    
    	ds_pkt.epoch_time[3] = (epocht>>24) & 0xff;    



        ds_pkt.payload[0]=(-40);
	ds_pkt.payload[1]=0x01;
	ds_pkt.payload[2]=0x1f;
	ds_pkt.payload[3]=0xff;
	ds_pkt.payload[4]=0x1f;
	ds_pkt.payload[5]=0xff;
	checksum=0;
	for(i=0; i<6; i++ )
		checksum+=ds_pkt.payload[i];
	ds_pkt.payload[6]=checksum;  
	ds_pkt.payload_len=7;

        // This takes the structure and packs it into the raw
	// array that is sent using SLIP
        pack_downstream_packet( &ds_pkt);	

     // Add MAC filter entries below
     //  downstream_packet_add_mac_filter( &ds_pkt, 2 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 3 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 4 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 5 ); 

    // Print your packet on the screen
    if(debug_txt_flag==1)
	print_ds_packet(&ds_pkt );


    cnt++;

    if(error==0) 
    	v=slipstream_send(ds_pkt.buf,ds_pkt.buf_len);

    if(debug_txt_flag==1)
    {
    if (v == 0) printf( "Error sending\n" );
    else printf( "Sent request %d\n",ds_pkt.seq_num);
    }
 
    nav_time_secs=ds_pkt.nav;
    reply_time_secs=ds_pkt.delay_per_level * ds_pkt.hop_max;

   t=time(NULL);
   reply_timeout=t+reply_time_secs+1;
   nav_timeout=t+nav_time_secs;

   // Collect Reply packets 
   while(reply_timeout>time(NULL))
   	{
    		v=slipstream_receive( rx_buf);
    		if (v > 0) {
          			gw_pkt.buf=rx_buf;
          			gw_pkt.buf_len=v;
          			print_gw_packet_elements(&gw_pkt);

			}
    		usleep(1000);
   	}

   // What for NAV and service incoming messages 
   // This is the time window when the network is idle and can
   // be used for asynchronous communications.
   while(nav_timeout>time(NULL))
   	{
    		v=slipstream_receive( rx_buf);
    		if (v > 0) {
      			// Check for mobile/p2p packets
          		gw_pkt.buf=rx_buf;
          		gw_pkt.buf_len=v;
          		print_gw_packet_elements(&gw_pkt);
			}
    		usleep(1000);
   	}
	


}
}
Пример #5
0
//XMLParser func called whenever an end of element is encountered
static void XMLCALL endElement (void *data, const char *element_name)
{
  int i;
  FF_POWER_RQST_PKT	ff_pwr_rqst;
  FF_POWER_ACTUATE_PKT	ff_pwr_actuate;
  //printf( "end=%s\n",element_name );
  if (strcmp (element_name, "FireFlyDSPacket") == 0) {
    pb_state = WAIT_STATE;
    if (pb_tran_pkt.num_msgs > 0) {
	// Pack tran_pkt
	pb_ds_pkt.payload_len =
		transducer_pkt_pack(&pb_tran_pkt,pb_ds_pkt.payload);
    }
    pack_downstream_packet (&pb_ds_pkt);
    pb_gw_pkt[pb_cnt].size = pb_ds_pkt.buf_len;
    for (i = 0; i < pb_ds_pkt.buf_len; i++)
      pb_gw_pkt[pb_cnt].pkt[i] = pb_ds_pkt.buf[i];
    pb_cnt++;
  }
  else if (strcmp (element_name, "Transducer") == 0)
	{
	// Add msg to tran_pkt
	switch(pb_tran_msg.type)
		{
		case TRAN_FF_BASIC_SHORT:
		    // don't need to do anything really...	
		    pb_tran_msg.len=0;	
		    //transducer_msg_add( &pb_tran_pkt, &pb_tran_msg );	
		break;
		case TRAN_LED_BLINK:
		    // don't need to do anything really...	
		    if(strstr(params,"RED")!=0) pb_tran_msg.payload[0]|=TRAN_RED_LED_MASK;	
		    if(strstr(params,"GREEN")!=0) pb_tran_msg.payload[0]|=TRAN_GREEN_LED_MASK;	
		    if(strstr(params,"BLUE")!=0) pb_tran_msg.payload[0]|=TRAN_BLUE_LED_MASK;	
		    if(strstr(params,"ORANGE")!=0) pb_tran_msg.payload[0]|=TRAN_ORANGE_LED_MASK;	
		    pb_tran_msg.len=1;	
		break;
		case TRAN_POWER_PKT:
			if(strcmp(action,"sense")==0 )
			{
				if(strstr(params,"1")!=0) ff_pwr_rqst.socket=1;
				else ff_pwr_rqst.socket=0;
        			ff_pwr_rqst.pkt_type=SENSE_PKT;
				pb_tran_msg.len=ff_power_rqst_pack(pb_tran_msg.payload, &ff_pwr_rqst);
				printf( "Transducer sense packet!\n" );
			}
			if(strcmp(action,"actuate")==0 )
			{
				ff_pwr_actuate.socket0_state=SOCKET_HOLD;
				ff_pwr_actuate.socket1_state=SOCKET_HOLD;
				if(strstr(params,"0")!=0) 
				{
					if(strstr(params,"on")!=0) ff_pwr_actuate.socket0_state=SOCKET_ON;
					if(strstr(params,"off")!=0) ff_pwr_actuate.socket0_state=SOCKET_OFF;
				}
        			else if(strstr(params,"1")!=0) 
				{
					if(strstr(params,"on")!=0) ff_pwr_actuate.socket1_state=SOCKET_ON;
					if(strstr(params,"off")!=0) ff_pwr_actuate.socket1_state=SOCKET_OFF;
				}
				ff_pwr_actuate.type=ACTUATE_PKT;
				pb_tran_msg.len=ff_power_actuate_pack(pb_tran_msg.payload, &ff_pwr_actuate);
			}

		break;
		}
	transducer_msg_add( &pb_tran_pkt, &pb_tran_msg );	
	}
  else if (strcmp (element_name, "Sleep") == 0) {
    pb_state = WAIT_STATE;
    pb_cnt++;
  }

}
Пример #6
0
int main (int argc, char *argv[])
{
  FILE *fp;
  uint8_t tx_buf[128];
  uint8_t rx_buf[128];
  TRANSDUCER_PKT_T	tran_cmd_pkt;
  TRANSDUCER_MSG_T	tran_msg_pkts[10];
  int32_t v,cnt,i,len;
  uint8_t nav_time_secs, reply_time_secs;
  int32_t tmp;
  time_t reply_timeout,nav_timeout,t;
  uint8_t cmd,error;
  char buf[1024];
  uint8_t num_msgs;
  SAMPL_GATEWAY_PKT_T gw_pkt;

debug_txt_flag=0;

  if (argc < 5 ) {
    printf ("Usage: server port num-msgs [mac-addr socket state] [-d]\n");
    printf ("  ex: ./jigo-ctrl localhost 5000 1 0x000000f0 1 on\n");
    printf ("  mac-addr MAC address of node to actuate\n");
    printf ("  socket 0 or 1\n");
    printf ("  state on or off\n");
    printf ("  d Debug Output\n");
    exit (1);
  }

  for(i=0; i<argc; i++ )
	{
	// Grab dash command line options
	if(strstr(argv[i],"-d")!=NULL )
		{	
		debug_txt_flag=1;
		}
	}

  sscanf( argv[3],"%d", &num_msgs );
  if(num_msgs>9 ) { printf( "Sorry, too many messages...\n" ); return 0; }
  if(debug_txt_flag==1)
  printf( "Composing %d actuation msgs\n", num_msgs );
  for(i=0; i<num_msgs; i++ )
  {
	sscanf( argv[(i*2)+4],"%x",&tmp );
	subnet_3=(tmp&0xff000000) >> 24;
	subnet_2=(tmp&0xff0000) >> 16;
	subnet_1=(tmp&0xff00) >> 8;
	tran_msg_pkts[i].mac_addr=(tmp & 0xff);	
  	if(debug_txt_flag==1) printf( "MAC_ADDR: 0x%x ",tran_msg_pkts[i].mac_addr );
	sscanf( argv[(i*2)+5],"%s",buf);
	tran_msg_pkts[i].key=TRAN_LED_BLINK;
	tran_msg_pkts[i].value=0;
	if(strstr(buf,"red")!=NULL) tran_msg_pkts[i].value|=TRAN_RED_LED_MASK;	
	if(strstr(buf,"green")!=NULL) tran_msg_pkts[i].value|=TRAN_GREEN_LED_MASK;	
	if(strstr(buf,"blue")!=NULL) tran_msg_pkts[i].value|=TRAN_BLUE_LED_MASK;	
	if(strstr(buf,"orange")!=NULL) tran_msg_pkts[i].value|=TRAN_ORANGE_LED_MASK;	
  }

  v=slipstream_open(argv[1],atoi(argv[2]),NONBLOCKING);
  nav_time_secs=25; 

  cnt = 0;
  while (1) {
     error=0;
     cmd=0;


	// Setup the packet to send out to the network

	// These values setup the internal data structure and probably don't
	// need to be changed
	ds_pkt.payload_len=0;
	ds_pkt.buf=tx_buf;
	ds_pkt.buf_len=DS_PAYLOAD_START;
	ds_pkt.payload_start=DS_PAYLOAD_START;
	ds_pkt.payload=&(tx_buf[DS_PAYLOAD_START]);
	
	// These are parameters that can be adjusted for different packets
	//ds_pkt.pkt_type=PING_PKT; 
	ds_pkt.pkt_type=TRANSDUCER_CMD_PKT; 
	ds_pkt.ctrl_flags= DS_MASK | ENCRYPT; 
	ds_pkt.seq_num=0; // Use the gateway's spiffy auto-cnt when set to 0
	ds_pkt.priority=0;
	ds_pkt.ack_retry=10;
	ds_pkt.subnet_mac[0]=subnet_1;
	ds_pkt.subnet_mac[1]=subnet_2;
	ds_pkt.subnet_mac[2]=subnet_3;
	ds_pkt.hop_cnt=0;  // Starting depth, always keep at 0
	ds_pkt.hop_max=5;  // Max tree depth
	ds_pkt.delay_per_level=0;  // Reply delay per level in seconds
	ds_pkt.nav=0;  // Time in seconds until next message to be sent
	ds_pkt.mac_check_rate=100;  // B-mac check rate in ms
	ds_pkt.rssi_threshold=-45;  // Reply RSSI threshold
	ds_pkt.last_hop_mac=0;
	ds_pkt.mac_filter_num=0; // Increase if MAC_FILTER is active
	ds_pkt.aes_ctr[0]=0;   // Encryption AES counter
	ds_pkt.aes_ctr[1]=0;
	ds_pkt.aes_ctr[2]=0;
	ds_pkt.aes_ctr[3]=0;

	//tran_msg_pkts[0].mac_addr=0xf0;
	//tran_msg_pkts[0].key=TRAN_POWER_CTRL_SOCK_0;
	//tran_msg_pkts[0].value=SOCKET_OFF;
	tran_cmd_pkt.num_msgs=num_msgs;	
	tran_cmd_pkt.msg=tran_msg_pkts;	
	ds_pkt.payload_len=transducer_cmd_pkt_add( &tran_cmd_pkt, ds_pkt.payload);
	transducer_cmd_pkt_checksum( &tran_cmd_pkt, ds_pkt.payload);
        // This takes the structure and packs it into the raw
	// array that is sent using SLIP
        pack_downstream_packet( &ds_pkt);	

     // Add MAC filter entries below
     //  downstream_packet_add_mac_filter( &ds_pkt, 0x07 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 3 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 4 ); 
     //  downstream_packet_add_mac_filter( &ds_pkt, 5 ); 

    // Print your packet on the screen
    if(debug_txt_flag==1)
	print_ds_packet(&ds_pkt );


    cnt++;

    for(i=0; i<ds_pkt.buf_len; i++ )
    {
	printf( "%02x",ds_pkt.buf[i] );
    }
    printf( "\n" );
    if(error==0) 
    	v=slipstream_send(ds_pkt.buf,ds_pkt.buf_len);

    if(debug_txt_flag==1)
    {
    if (v == 0) printf( "Error sending\n" );
    else printf( "Sent request %d\n",ds_pkt.seq_num);
    }
 
    //nav_time_secs=ds_pkt.nav;
    nav_time_secs=5;
    reply_time_secs=ds_pkt.delay_per_level * ds_pkt.hop_max;

   t=time(NULL);
   reply_timeout=t+reply_time_secs+1;
   nav_timeout=t+nav_time_secs;

   // Collect Reply packets 
   while(reply_timeout>time(NULL))
   	{
    		v=slipstream_receive( rx_buf);
    		if (v > 0) {
    			gw_pkt.buf=rx_buf;
		          gw_pkt.buf_len=v;
        		  print_gw_packet_elements(&gw_pkt);

			}
    		usleep(1000);
   	}

   // What for NAV and service incoming messages 
   // This is the time window when the network is idle and can
   // be used for asynchronous communications.
   while(nav_timeout>time(NULL))
   	{
    		v=slipstream_receive( rx_buf);
    		if (v > 0) {
      			// Check for mobile/p2p packets
    			gw_pkt.buf=rx_buf;
		          gw_pkt.buf_len=v;
        		  print_gw_packet_elements(&gw_pkt);
    			}
    		usleep(1000);
   	}
	
	// only run once
	break;
}
}