int main (int argc, const char * argv[]){
    // handle remote addr
    if (argc > 1){
        if (sscan_bd_addr((uint8_t *)argv[1], addr)){
            serverMode = 0;
            prepare_packet();
        }
    }

	run_loop_init(RUN_LOOP_POSIX);
	int err = bt_open();
	if (err) {
		printf("Failed to open connection to BTdaemon\n");
		return err;
	}
	bt_register_packet_handler(packet_handler);
	bt_send_cmd(&l2cap_register_service, PSM_TEST, PACKET_SIZE);
	bt_send_cmd(&btstack_set_power_mode, HCI_POWER_ON );
	
	// banner
	printf("L2CAP Throughput Test (compatible with Apple's Bluetooth Explorer)\n");
	if (serverMode) {
	   printf(" * Running in Server mode. For client mode, specify remote addr 11:22:33:44:55:66\n");
    }
    printf(" * MTU: 1000 bytes\n");
	
	run_loop_execute();
	bt_close();
	return 0;
}
Beispiel #2
0
void usart_dostuff() {
  if (flag_command_ready)
    handle_command();

  if (flag_read_flash) {
    bool_t verified = false;
    while (!verified) {
      flash_read(flash_addr);
      verified = flash_verify(flash_addr);
    }

    while (!verified) {
      _delay_ms(25);
      flash_read(flash_addr);
      verified = flash_verify(flash_addr);
    }

    flash_addr++;
    flag_read_flash = false;
  }

  if (flag_xmodem_next_packet) {
    prepare_packet();
    usart_send();
  }
}
/*---------------------------------------------------------------------------*/
static int
radio_send(const void *payload, unsigned short payload_len)
{
	prepare_packet(payload, payload_len);
	transmit_packet(payload_len);

  return RADIO_TX_OK;

}
Beispiel #4
0
void ClientConnection::send_message(const char* msg, int len,
		receiver_t receiver) {
	if (_client_socket == NULL) {
		fprintf(
				stderr,
				"ClientConnection::send_message: Connection not initialized!\n");
		return;
	}

	prepare_packet(_packet_buffer, msg, len, receiver);
	send_packet(_client_socket, _packet_buffer);
}
Beispiel #5
0
static uint8_t send_packet(struct net_buf *buf,
			   mac_callback_t sent_callback, void *ptr)
{
	bool is_broadcast, ack_required;
	uint8_t attempts;
	uint8_t retries;
	int ret;

#ifdef SIMPLERDC_802154_ACK_REQ
	packetbuf_set_attr(buf, PACKETBUF_ATTR_MAC_ACK, 1);
#endif

	retries = prepare_packet(buf);
	if (!retries) {
		return MAC_TX_ERR_FATAL;
	}

	ack_required = prepare_for_ack(buf);
	is_broadcast = !!packetbuf_holds_broadcast(buf);
	attempts = 0;

	while (retries) {
		attempts++;
		retries--;

		ret = NETSTACK_RADIO.transmit(buf, packetbuf_totlen(buf));
		if (ret == RADIO_TX_COLLISION) {
			continue;
		}

		ret = wait_for_ack(is_broadcast, ack_required);
		if (ret == MAC_TX_OK) {
			break;
		}
	}

	mac_call_sent_callback(buf, sent_callback, ptr, ret, attempts);

	if (ret == MAC_TX_OK) {
		return 1;
	}

	return 0;
}
Beispiel #6
0
int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
		       struct cros_ec_command *msg)
{
	u8 *out;
	u8 csum;
	int i;

	if (ec_dev->proto_version > 2)
		return prepare_packet(ec_dev, msg);

	BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
	out = ec_dev->dout;
	out[0] = EC_CMD_VERSION0 + msg->version;
	out[1] = msg->command;
	out[2] = msg->outsize;
	csum = out[0] + out[1] + out[2];
	for (i = 0; i < msg->outsize; i++)
		csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
	out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;

	return EC_MSG_TX_PROTO_BYTES + msg->outsize;
}
Beispiel #7
0
int ProtocolParser::process_in()
{
	if (!is_complete())
	{
		return Error_packet_not_complete;
	}

	switch (get_packet_type())
	{
	case Packet_type_external_rsa_key :
		{
			c_Debug() << "process packet, type == 'Packet_type_external_rsa_key'" << "\r\n";

			if (!got_rsa_key())
			{
				// process external RSA public key
				if (parse_external_rsa_key_packet() == ProtocolParser::Error_no)
				{
					// send internal RSA public key
					std::vector<char> packet;
					int parse_result = prepare_rsa_internal_pub_key_packet(packet);
					if (parse_result == ProtocolParser::Error_no)
					{
						Net::send_data(own_node_->get_socket(), &packet[0], packet.size());
						c_Debug() << "process packet, type == 'Packet_type_external_rsa_key', OK" << "\r\n";
						return Error_no;
					}
				}
				c_Debug() << "process packet, type == 'Packet_type_external_rsa_key', Error_rsa_key_packet" << "\r\n";
				return Error_rsa_key_packet;
			}
			break;
		}
	case Packet_type_login_data :
		{
			c_Debug() << "process packet, type == 'Packet_type_login_data'" << "\r\n";

			if (!got_login_data_)
			{
				if (parse_login_packet() == ProtocolParser::Error_no)
				{
					// check login and passwd
					bool user_exist = db_.check_user_exist(login_, passwd_hash_);
					if (user_exist)
					{
						// get user ID by login
						int user_id;
						db_.get_user_id_by_login(&login_[0], &user_id);
						own_node_->set_user_id(user_id);

						// get node ID by name
						if (!node_name_.empty())
						{
							int node_id = 0;
							if (db_.get_node_id_by_name(node_name_, &node_id))
							{
								own_node_->set_node_id(node_id);
							}
						}

						// send login accept packet
						std::vector<char> accept_login_packet;
						prepare_packet(Packet_type_login_accept,
							TunnelCommon::Protocol::c_packet_login_accept,
							accept_login_packet);
						Net::send_data(own_node_->get_socket(), &accept_login_packet[0],
							accept_login_packet.size());

						c_Debug() << "process packet, send login_accept_packet\r\n";

						c_Debug() << "process packet, type == 'Packet_type_login_data', OK" << "\r\n";
						return Error_no;
					}
					c_Debug() << "process packet, type == 'Packet_type_login_data', Error_parse_login_node_not_exist" << "\r\n";
					return Error_parse_login_node_not_exist;
				}
				c_Debug() << "process packet, type == 'Packet_type_login_data', Error_parse_login_packet" << "\r\n";
				return Error_parse_login_packet;
			}
			break;
		}
	default:
		{
			c_Debug() << "process packet, Error_unknown_packet" << "\r\n";
			flush();
			return Error_unknown_packet;
		}
	}
	flush();
	return Error_no;
}
Beispiel #8
0
/*
 * This is the kthread function. Although I didn't used it you can always pass it values through the data variable.
 */
static int main_loop(void* data)
{
  sample_t *samples;
  packet_t * pkt;
  uint32_t len, i = 0;
  static uint32_t seq = 0;

  if (sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &udp_socket) < 0)
    {
      printk(KERN_EMERG "Unable to create socket.\n");
      return 0;
    }

  memset(&my_addr, 0, sizeof(my_addr));
  my_addr.sin_family = AF_INET;
  my_addr.sin_addr.s_addr = in_aton(bind_ip);
  my_addr.sin_port = htons(sport);

  if(udp_socket->ops->bind(udp_socket, (struct sockaddr*) &my_addr, sizeof(struct sockaddr)) < 0)
    {
      printk(KERN_EMERG "Unable to bind socket to %s:%d.\n", bind_ip, sport);
      return 0;
    }

  /*
   * If you need debug, just compile the code with -D__DEBUG__
   */
#ifdef __DEBUG__
  printk(KERN_INFO "Bound to %s:%u\n", bind_ip, sport);
#endif

  while (1)
    {
      if (kthread_should_stop())
        {
#ifdef __DEBUG__
          printk(KERN_INFO "Stopping sender thread...\n");
#endif
          break;
        }

      len = read_nsamples(&samples);
      if(len > 0)
        {

#ifdef __DEBUG__
          printk(KERN_INFO "Read %d samples:\n", len);
#endif

          for(i = 0; i < len; i += 1)
            {
              pkt = prepare_packet(&(samples[i]), seq++, node_id);
              if(pkt == NULL) {
                  printk(KERN_EMERG "Was not able to prepare the packet to send\n");
                  continue;
              }
              send_it(pkt);
              kfree(pkt);
            }

          kfree(samples); // Never forget samples was allocated in read_nsamples and so has to be freed
        }

      //    schedule(); //This is similar to kill a fly with a bazooka, but it works.
      msleep(SLEEP_TIME_MS);
    }
  return 0;
}
Beispiel #9
0
int main(int argc, char * argv[]) {

	if(argc < 3 || argc > 4) {
		print_usage();
	}

	int i = 0;

	if(argc == 4) {
		if(strcmp(argv[1], "-d")) {
			print_usage();
		}
		debug = 1;
		i = 1;
	}

	int portno = atoi(argv[++i]);
	set_dropper(atoi(argv[++i]));

	if((socket_descriptor = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		fprintf(stderr, "Could not create socket.\n");
		exit(2);
	}

	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_addr.s_addr = INADDR_ANY;
	server.sin_port = htons(portno);

	init();

	if(bind(socket_descriptor, (struct sockaddr *) &server, sizeof(server)) < 0) {
		fprintf(stderr, "Could not bind socket to port %d.\n", portno);
		exit(3);
	}

	if(debug)
		printf("Server started on port %d.\n", portno);

	serving = 0;

	max_in_size = 1024;
	in_buffer = (uchar *)malloc(max_in_size*sizeof(uchar));

	src = &client;

	while(1) {
		if(!serving) {
			accept_connection();
		} else if(!prepare_packet()) {
			if(write_read_data()) {
				printf("Error while receiving data. Terminating Connection");
				finished = 0;
				serving = 0;
				free(out_buffer);
				continue;
			}
			process_read();
		}
	}

	return 0;
}
Beispiel #10
0
static int vp8_handle_packet(AVFormatContext *ctx,
                             PayloadContext *vp8,
                             AVStream *st,
                             AVPacket *pkt,
                             uint32_t *timestamp,
                             const uint8_t *buf,
                             int len, int flags)
{
    int start_partition, end_packet;
    int extended_bits, non_ref, part_id;
    int pictureid_present = 0, tl0picidx_present = 0, tid_present = 0,
        keyidx_present = 0;
    int pictureid = -1, keyidx = -1;

    if (len < 1)
        return AVERROR_INVALIDDATA;

    extended_bits   = buf[0] & 0x80;
    non_ref         = buf[0] & 0x20;
    start_partition = buf[0] & 0x10;
    part_id         = buf[0] & 0x0f;
    end_packet      = flags & RTP_FLAG_MARKER;
    buf++;
    len--;
    if (extended_bits) {
        if (len < 1)
            return AVERROR_INVALIDDATA;
        pictureid_present = buf[0] & 0x80;
        tl0picidx_present = buf[0] & 0x40;
        tid_present       = buf[0] & 0x20;
        keyidx_present    = buf[0] & 0x10;
        buf++;
        len--;
    }
    if (pictureid_present) {
        if (len < 1)
            return AVERROR_INVALIDDATA;
        if (buf[0] & 0x80) {
            if (len < 2)
                return AVERROR_INVALIDDATA;
            pictureid = AV_RB16(buf) & 0x7fff;
            buf += 2;
            len -= 2;
        } else {
            pictureid = buf[0] & 0x7f;
            buf++;
            len--;
        }
    }
    if (tl0picidx_present) {
        // Ignoring temporal level zero index
        buf++;
        len--;
    }
    if (tid_present || keyidx_present) {
        // Ignoring temporal layer index and layer sync bit
        if (len < 1)
            return AVERROR_INVALIDDATA;
        if (keyidx_present)
            keyidx = buf[0] & 0x1f;
        buf++;
        len--;
    }
    if (len < 1)
        return AVERROR_INVALIDDATA;

    if (start_partition && part_id == 0) {
        int res;
        if (vp8->data) {
            uint8_t *tmp;
            avio_close_dyn_buf(vp8->data, &tmp);
            av_free(tmp);
            vp8->data = NULL;
        }
        if ((res = avio_open_dyn_buf(&vp8->data)) < 0)
            return res;
        vp8->timestamp = *timestamp;
     }

    if (!vp8->data || vp8->timestamp != *timestamp) {
        av_log(ctx, AV_LOG_WARNING,
               "Received no start marker; dropping frame\n");
        return AVERROR(EAGAIN);
    }

    avio_write(vp8->data, buf, len);

    if (end_packet) {
        prepare_packet(pkt, vp8, st->index);
        return 0;
    }

    return AVERROR(EAGAIN);
}
Beispiel #11
0
static int vp8_handle_packet(AVFormatContext *ctx,
                             PayloadContext *vp8,
                             AVStream *st,
                             AVPacket *pkt,
                             uint32_t *timestamp,
                             const uint8_t *buf,
                             int len, int flags)
{
    int start_packet, end_packet, has_au, ret = AVERROR(EAGAIN);

    if (!buf) {
        // only called when vp8_handle_packet returns 1
        if (!vp8->data) {
            av_log(ctx, AV_LOG_ERROR, "Invalid VP8 data passed\n");
            return AVERROR_INVALIDDATA;
        }
        prepare_packet(pkt, vp8, st->index);
        *timestamp = vp8->timestamp;
        return 0;
    }

    start_packet = *buf & 1;
    end_packet   = flags & RTP_FLAG_MARKER;
    has_au       = *buf & 2;
    buf++;
    len--;

    if (start_packet) {
        int res;
        uint32_t ts = *timestamp;
        if (vp8->data) {
            // missing end marker; return old frame anyway. untested
            prepare_packet(pkt, vp8, st->index);
            *timestamp = vp8->timestamp; // reset timestamp from old frame

            // if current frame fits into one rtp packet, need to hold
            // that for the next av_get_packet call
            ret = end_packet ? 1 : 0;
        }
        if ((res = url_open_dyn_buf(&vp8->data)) < 0)
            return res;
        vp8->is_keyframe = *buf & 1;
        vp8->timestamp   = ts;
     }

    if (!vp8->data || vp8->timestamp != *timestamp && ret == AVERROR(EAGAIN)) {
        av_log(ctx, AV_LOG_WARNING,
               "Received no start marker; dropping frame\n");
        return AVERROR(EAGAIN);
    }

    // cycle through VP8AU headers if needed
    // not tested with actual VP8AUs
    while (len) {
        int au_len = len;
        if (has_au && len > 2) {
            au_len = AV_RB16(buf);
            buf += 2;
            len -= 2;
            if (buf + au_len > buf + len) {
                av_log(ctx, AV_LOG_ERROR, "Invalid VP8AU length\n");
                return AVERROR_INVALIDDATA;
            }
        }

        put_buffer(vp8->data, buf, au_len);
        buf += au_len;
        len -= au_len;
    }

    if (ret != AVERROR(EAGAIN)) // did we miss a end marker?
        return ret;

    if (end_packet) {
        prepare_packet(pkt, vp8, st->index);
        return 0;
    }

    return AVERROR(EAGAIN);
}
Beispiel #12
0
int main(void)
{
    
  usi_enable();
  spiX_initslave(SPIMODE);
  sei();
  

  //Initialize slave
  slave_init();
  
  //Let settings catch up
  _delay_ms(100);
  
  //An initial packet, not sure what it's for, but the other code had it
  prepare_packet("", 0);
  spiX_put(0);
  
  unsigned char input;
  do{
    
    //Wait for SS
    while(!slave_selected());
    
    //Wait for pending transfers
    spiX_wait();
    
    //Read first character from master
    input = spiX_get();
    
    //Is the master telling us to receive?
      //If so, interpret the input and prepare a response packet
    if(input == RECEIVE_CHAR)
    {
      //Retrieve the rest of the packet from master
      receive_packet();
      
      //Check integrity
      if(!check_integrity())
      {
          prep_err();
          continue;
      }
        
      //Lowercase are read operations
      if(incoming_packet[1] > 96 && incoming_packet[1] < 127)
      {
        prep_response(incoming_packet[1]);
      }
      else
      {
        do_action(incoming_packet[1]);
      }
    }
    //Is the master telling us that it's ready to receive?
    else if(input == SEND_CHAR)
    {
      //Send the prepared packet
      send_packet();
    }
    if(waiting_measure){
       slave_run_measure();
       waiting_measure = 0;
    }
    if(waiting_write){
       slave_apply();
       waiting_write = 0;
    }
    
  } while(1);      // Loop forever...
}
Beispiel #13
0
void prep_response(char r)
{
  char arg = incoming_packet[2];
  
  switch(r)
  {
    //Prepare id to be sent
    case 'i':
      prepare_packet(from_int(slave_id), sizeof(int));
      break;
      
    //Type
    case 't':
      prepare_packet( slave_type, strlen( slave_type ) );
      break;
      
    //Name
    case 'n':
      prepare_packet(slave_name, strlen( slave_name ) );
      break;
      
    //Deployment Date
    case 'd':
      prepare_packet(slave_init_date, sizeof(slave_init_date));
      break;
      
    //Number of read channels
    case 'r':
      prepare_packet(from_int(slave_rcount),sizeof(int)); 
      break;
      
    //Number of write channels
    case 'w':
      prepare_packet(from_int(slave_wcount),sizeof(int)); 
      break;
  //Start measurment on channel
    case  'm':
    {
      //Wait for measurment
      unsigned wait = slave_measure();
      prepare_packet( from_int(wait), sizeof( unsigned ) );
      waiting_measure = 1;
    }
      break;
      
    //Channel value/output
    case  'q':
      if(arg < slave_rcount )
      {
      	char* valstr = slave_read( arg );
        prepare_packet( valstr, strlen( valstr ) );
      }
      else
        prep_err();
      break;
      
    case 'f':
      prepare_packet(slave_info, strlen( slave_info ) );
      break;
    //Echo      
     case 'e':
         prepare_packet("e", 1);
         break;
         
    //Error
    default:
      prep_err();
      break;
  }
}
Beispiel #14
0
inline void prep_ok()
{
  prepare_packet("ok", 2);
}
Beispiel #15
0
inline void prep_err()
{
  prepare_packet("err", 3);
}