Exemple #1
0
/**
 * Send an edcl packet and fragment it if max_packet_size < data_size
 */
static int		send_fragmented(void)
{
	unsigned int		nb_ack_packet = 0;
	unsigned int		address_offset = 0;
	unsigned int		nb_sent_packet = 0;
	unsigned int		local_data_size = config.data_size;
	edcl_packet_t*		packet = NULL;

	/* Allocating the packet of the packet */
	packet = calloc(1, min(local_data_size, MAX_DATA_SIZE) + sizeof (edcl_header_t));
	if (packet == NULL)
		goto error_malloc;

	do {
		/* Filling up the edcl header */
		clear_header(packet->header);
		set_operation(packet->header, WRITE_OP);
		set_sequence(packet->header, config.sequence_number);
		set_address(packet->header, config.memory_address + address_offset);
		set_length(packet->header, min(local_data_size, MAX_DATA_SIZE));

		if (config.verbose)
			fprintf(stderr,
				" [%d] Trying to write at 0x%08x.",
				config.sequence_number,
				config.memory_address + address_offset);

		/* Copying the data in the packet */
		memcpy(packet->data,
		       config.data_c + address_offset,
		       min(local_data_size, MAX_DATA_SIZE));

		/* Sending the packet to the ethernet IP */
		if (sendto(config.socket,
			   packet,
			   min(local_data_size, MAX_DATA_SIZE) + sizeof (edcl_header_t),
			   0, config.serv_info->ai_addr, config.serv_info->ai_addrlen)
		    == -1)
			goto error_send;

		/* Waiting for the aknowledgment */
		if (recvfrom(config.socket,  packet,
			     sizeof (edcl_header_t) + min(local_data_size, MAX_DATA_SIZE),
			     0, NULL, 0) == -1)
			goto error_recvfrom;

		/* We've been aknowledge, keep sending */
		if (get_ack(packet->header)) {
			if (config.verbose)
				fprintf(stderr,
					"\t[OK]\n");
			address_offset += min(local_data_size, MAX_DATA_SIZE);
			local_data_size -= min(local_data_size, MAX_DATA_SIZE);
			++config.sequence_number;
			++nb_ack_packet;
		}
		/* The sequence number was wrong, fix it */
		else {
			if (config.verbose)
				fprintf(stderr,
					"\t[Failed]\n  Wrong sequence number (%d) should be: %d. Fixed.\n",
					config.sequence_number, get_sequence(packet->header));
			config.sequence_number = get_sequence(packet->header);
		}

		++nb_sent_packet;
	} while (local_data_size > 0);

	if (config.verbose) {
		fprintf(stderr,
			"The datas have been fragmented in %d packets.\n", nb_ack_packet);
		fprintf(stderr,
			"\t %d packets have been sent (%d have been lost).\n",
			nb_sent_packet,
			nb_sent_packet - nb_ack_packet);
	}

	/* Releasing ressources */
	free(packet);

	return (0);

 error_recvfrom:
 error_send:
	if (config.verbose)
		fprintf(stderr,
			"Error while sending the packet.\n");
	free(packet);
	return (errno);
 error_malloc:
	if (config.verbose)
		fprintf(stderr,
			"Unable to allocate the packet.\n");
	return (errno);
}
int main(int argc, char **argv)
{
   int operation;

   g_type_init();
   ecore_init();
   if(argc < 2) {
      fprintf(stderr, "1:set(db), 2:set(file), 3:set(memory), 4:set(gconf_d), 5:set(gconf_l) \n");
      fprintf(stderr, "6:get(db), 7:get(file), 8:get(memory), 9:get(gconf_d), 10:get(gconf_l) \n");
      fprintf(stderr, "11:unset(db), 12:unset(file), 13:unset(memory), 14:unset(gconf_d), 15:unset(gconf_l) \n");
      return -1;
   }

   operation = atoi(argv[1]);
   switch(operation)
   {
      case 1:
         set_operation(0);
         break;
      case 2:
         set_operation(1);
         break;
      case 3:
         set_operation(2);
         break;
      case 4:
         set_operation(3);
         break;
      case 5:
         set_operation(4);
         break;
      case 6:
         get_operation(0, "db/timetest");
         break;
      case 7:
         get_operation(1, "file/timetest");
         break;
      case 8:
         get_operation(2, "memory/timetest");
         break;
      case 9:
         get_operation(3, "gconf_l/timetest");
         break;
      case 10:
         get_operation(4, "gconf_d/timetest");
         break;
      case 11:
         unset_operation(0);
         break;
      case 12:
         unset_operation(1);
         break;
      case 13:
         unset_operation(2);
         break;
      case 14:
         unset_operation(3);
         break;
      case 15:
         unset_operation(4);
         break;
      case 16:
         vconf_notify_key_changed(vconfkeys[0][1], test_cb2, NULL);
         vconf_notify_key_changed(vconfkeys[1][1], test_cb2, NULL);
         vconf_notify_key_changed(vconfkeys[2][1], test_cb2, NULL);
         vconf_notify_key_changed(vconfkeys[3][1], test_cb2, NULL);
         vconf_notify_key_changed(vconfkeys[4][1], test_cb2, NULL);

         //ecore_main_loop_begin();
         GMainLoop *event_loop;
         event_loop = g_main_loop_new(NULL, FALSE);
         g_main_loop_run(event_loop);
         break;
   }

   return 0;
}