Esempio n. 1
0
int8_t transducer_aggregate (SAMPL_UPSTREAM_PKT_T * in,
                             SAMPL_UPSTREAM_PKT_T * out)
{
  uint8_t len, i, k,l, dup;

  if (in->num_msgs > MAX_PKT_PAYLOAD / 4)
    return NRK_ERROR;

  //printf( "%d a[%d %d]\n",my_mac, in->num_msgs, out->num_msgs );

  k = transducer_pkt_unpack(&p1, in->payload );
  k = transducer_pkt_unpack(&p2, out->payload );
  //p1.msgs_payload=in->payload;
  //p2.msgs_payload=out->payload;
  //p2.num_msgs=out->num_msgs;

  for (i = 0; i < p1.num_msgs; i++) {
    dup = 0;
    // get next ping packet to compare against current outgoing list
    transducer_msg_get (&p1,&m1, i);
    for (k = 0; ((k < p2.num_msgs) && (dup==0)); k++) {
      // get packet from outgoing list and compare against incomming packet
      transducer_msg_get (&p2,&m2, k);
      if (m1.mac_addr == m2.mac_addr && m1.type == m2.type && m1.len == m2.len)
        dup = 1;
	// Go through each byte in the pkt
	for(l=0; l<m1.len; l++ ) if(m1.payload[l]!=m2.payload[l]) dup=0;	
    }
    if (dup == 0) {
      // if packet is unique, add to outgoing packet
      //out->payload_len=transducer_reply_pkt_add( &p1, out->payload, out->num_msgs );
      len = transducer_msg_add (&p2, &m1);
     if(len>0) {
        out->payload_len = transducer_pkt_pack(&p2, out->payload );
	//printf( "[%d]: %d %d %d\r\n",my_mac, len,out->payload_len, p2.num_msgs );
	}
      else out->error_code |= OVERFLOW_ERROR_MASK;
    }
  }

  return NRK_OK;
}
Esempio n. 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,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;
}
Esempio n. 3
0
void event_detector_task()
{
uint8_t len,val,delta,delta2;

while (!sampl_started())
    nrk_wait_until_next_period ();

  tran_pkt.msgs_payload=async_buf;
  tran_pkt.num_msgs=0;

   current_last=rms_current;
   current_last2=rms_current2;

while(1)
{
if(rms_current>current_last) delta=rms_current-current_last;
else delta=current_last-rms_current;

if(rms_current2>current_last2) delta2=rms_current2-current_last2;
else delta2=current_last2-rms_current2;

if(delta>2 || delta2>2)
{
	pd_pkt.type=DEBUG_PKT;
        pd_pkt.rms_voltage=rms_voltage;
        pd_pkt.rms_current=rms_current;
        pd_pkt.rms_current2=rms_current2;
        pd_pkt.true_power=true_power;
        pd_pkt.true_power2=true_power2;
        pd_pkt.freq=freq;
        pd_pkt.energy=tmp_energy;
        pd_pkt.energy2=tmp_energy2;
        pd_pkt.current_p2p_high=l_c_p2p_high;
        pd_pkt.current_p2p_low=l_c_p2p_low;
        pd_pkt.current_p2p_high2=l_c_p2p_high2;
        pd_pkt.current_p2p_low2=l_c_p2p_low2;
        pd_pkt.voltage_p2p_high=l_v_p2p_high;
        pd_pkt.voltage_p2p_low=l_v_p2p_low;
        pd_pkt.total_secs=total_secs;


tran_pkt.num_msgs=0;
tran_pkt.checksum=0;
tran_pkt.msgs_payload=&(async_buf[TRANSDUCER_PKT_HEADER_SIZE]);

tran_msg.payload=&(async_buf[TRANSDUCER_PKT_HEADER_SIZE+TRANSDUCER_MSG_HEADER_SIZE]);
tran_msg.type=TRAN_POWER_PKT;
tran_msg.len=ff_power_debug_pack(tran_msg.payload, &pd_pkt);
tran_msg.mac_addr=my_mac;
//printf( "sending: %d\r\n",my_mac);
len=transducer_msg_add( &tran_pkt, &tran_msg);

len = transducer_pkt_pack(&tran_pkt, async_buf);
val=push_p2p_pkt( async_buf,len,TRANSDUCER_PKT, 0x0 );
}

current_last=rms_current;
current_last2=rms_current2;
nrk_wait_until_next_period();
}

}
Esempio n. 4
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;
}
}
Esempio n. 5
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;
}
Esempio n. 6
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++;
  }

}
Esempio n. 7
0
int8_t transducer_generate (SAMPL_UPSTREAM_PKT_T * pkt,
                            SAMPL_DOWNSTREAM_PKT_T * ds_pkt)
{
  ACK_PKT_T a;
  uint8_t i,total_len;
  uint8_t status;
  uint8_t pkt_generated,t,v;


  if(ds_pkt->seq_num==tran_last_seq_num) return NRK_ERROR;
  tran_last_seq_num=ds_pkt->seq_num;


  pkt_generated = 0;
  pkt->payload_len = 0;
  pkt->num_msgs = 0;
  status = 0;
  t = transducer_pkt_unpack (&p1, ds_pkt->payload);
/*
  for(i=0;i <ds_pkt->payload_len; i++ )
  	printf( "%d ",ds_pkt->payload[i] );

  nrk_kprintf( PSTR(")\n" ));
*/
  total_len=0;

  p2.num_msgs=0;
  p2.checksum=0;
  p2.msgs_payload=&(pkt->payload[TRANSDUCER_PKT_HEADER_SIZE]);

  if (t) {
    for (i = 0; i < p1.num_msgs; i++) {
      v=transducer_msg_get(&p1, &m1, i);
      if (v==1 && (m1.mac_addr == my_mac || m1.mac_addr == 255)) {
        pkt_generated = 1;

	m2.len=0;
	m2.mac_addr = my_mac;
	m2.type=0;
	m2.payload =  &(pkt->payload[total_len+TRANSDUCER_MSG_HEADER_SIZE+TRANSDUCER_PKT_HEADER_SIZE]);

        // Call application transducer handler
        status = transducer_handler (&m1, &m2);

	if(status!=0)
	{
		total_len+=(m2.len+TRANSDUCER_MSG_HEADER_SIZE+TRANSDUCER_PKT_HEADER_SIZE);
		if(total_len>=(MAX_PKT_PAYLOAD-US_PAYLOAD_START-TRANSDUCER_PKT_HEADER_SIZE))
		{
          		pkt->error_code |= OVERFLOW_ERROR_MASK;
	  		total_len=MAX_PKT_PAYLOAD-US_PAYLOAD_START-TRANSDUCER_PKT_HEADER_SIZE;	
		}
		if (status == 0)
          		pkt->error_code |= INVALID_DATA_ERROR_MASK;
	
	
        	// Copy header elements into packet
		transducer_msg_add(&p2, &m2);

		t=transducer_pkt_pack( &p2, pkt->payload );

        	// Update new length of packet
        	pkt->payload_len =  t;
		// Multiple msg can be in the packet, but there is only 1 packet
        	pkt->num_msgs=1;  
        	pkt->pkt_type = TRANSDUCER_PKT;
	}
      }
    }
  }
  else {
    pkt_generated = 1;
    // build NCK reply packet
    a.mac_addr = my_mac;
    pkt->payload_len = ack_pkt_add (&a, pkt->payload, 0);
    pkt->num_msgs = 1;
    pkt->pkt_type = ACK_PKT;
    pkt->error_code |= NCK_ERROR_MASK;        // set error type for NCK
  }

  if (pkt_generated == 0) {
    pkt->pkt_type = EMPTY_PKT;
    pkt->num_msgs = 0;
    pkt->payload_len = 0;
  }


  return NRK_OK;
}
Esempio n. 8
0
int8_t transducer_p2p_generate (SAMPL_PEER_2_PEER_PKT_T * p2p_pkt_in,
                            SAMPL_PEER_2_PEER_PKT_T* p2p_pkt_out)
{
  ACK_PKT_T a;
  uint8_t i,total_len;
  uint8_t status,reply;
  uint8_t pkt_generated,t,v;

  if(p2p_pkt_in->seq_num==tran_last_seq_num) return NRK_ERROR;
  tran_last_seq_num=p2p_pkt_in->seq_num;
  pkt_generated = 0;
  //p2p_pkt_out->payload_len = 0;
  status = 0;
  t = transducer_pkt_unpack (&p2p_tran_pkt_in, p2p_pkt_in->payload);

  if(t==0) return NRK_ERROR;

  total_len=0;
  reply=0;

  p2p_tran_pkt_out.num_msgs=0;
  p2p_tran_pkt_out.checksum=0;
  p2p_tran_pkt_out.msgs_payload=&(p2p_pkt_out->payload[TRANSDUCER_PKT_HEADER_SIZE]);

  
    for (i = 0; i < p2p_tran_pkt_in.num_msgs; i++) {
      v=transducer_msg_get(&p2p_tran_pkt_in, &p2p_tran_msg_in, i);
      if (v==1 && (p2p_tran_msg_in.mac_addr == my_mac || p2p_tran_msg_in.mac_addr == 255)) {
        pkt_generated = 1;

	p2p_tran_msg_out.len=0;
	p2p_tran_msg_out.mac_addr = my_mac;
	p2p_tran_msg_out.type=0;
	p2p_tran_msg_out.payload =  &(p2p_pkt_out->payload[total_len+TRANSDUCER_MSG_HEADER_SIZE+TRANSDUCER_PKT_HEADER_SIZE]);

        // Call application transducer handler
        status = transducer_handler (&p2p_tran_msg_in, &p2p_tran_msg_out);

	if(status!=0)
	{
		total_len+=(p2p_tran_msg_out.len+TRANSDUCER_MSG_HEADER_SIZE+TRANSDUCER_PKT_HEADER_SIZE);
		if(total_len>=(MAX_PKT_PAYLOAD-US_PAYLOAD_START-TRANSDUCER_PKT_HEADER_SIZE))
		{
          		//p2p_pkt_out->error_code |= OVERFLOW_ERROR_MASK;
	  		total_len=MAX_PKT_PAYLOAD-US_PAYLOAD_START-TRANSDUCER_PKT_HEADER_SIZE;	
		}
		//if (status == 0)
          	//	p2p_pkt_out->error_code |= INVALID_DATA_ERROR_MASK;
	
	
        	// Copy header elements into packet
		transducer_msg_add(&p2p_tran_pkt_out, &p2p_tran_msg_out);

		t=transducer_pkt_pack( &p2p_tran_pkt_out, p2p_pkt_out->payload );

        	// Update new length of packet
        	p2p_pkt_out->payload_len =  t;
		// Multiple msg can be in the packet, but there is only 1 packet
        	p2p_pkt_out->pkt_type = TRANSDUCER_PKT;
		reply=1;
	}
      }
    }
/*  else {
    pkt_generated = 1;
    // build NCK reply packet
    a.mac_addr = my_mac;
    p2p_pkt_out->payload_len = ack_pkt_add (&a, pkt->payload, 0);
    p2p_pkt_out->num_msgs = 1;
    p2p_pkt_out->pkt_type = ACK_PKT;
    p2p_pkt_out->error_code |= NCK_ERROR_MASK;        // set error type for NCK
  }


  if (pkt_generated == 0) {
    pkt->pkt_type = EMPTY_PKT;
    pkt->num_msgs = 0;
    pkt->payload_len = 0;
  }
*/

if(reply)  return NRK_OK;
else return NRK_ERROR;

}