void aml_append(Aml *parent_ctx, Aml *child) { GArray *buf = build_alloc_array(); build_append_array(buf, child->buf); switch (child->block_flags) { case AML_OPCODE: build_append_byte(parent_ctx->buf, child->op); break; case AML_EXT_PACKAGE: build_extop_package(buf, child->op); break; case AML_PACKAGE: build_package(buf, child->op); break; case AML_RES_TEMPLATE: build_append_byte(buf, 0x79); /* EndTag */ /* * checksum operations are treated as succeeded if checksum * field is zero. [ACPI Spec 1.0b, 6.4.2.8 End Tag] */ build_append_byte(buf, 0); /* fall through, to pack resources in buffer */ case AML_BUFFER: build_buffer(buf, child->op); break; case AML_NO_OPCODE: break; default: assert(0); break; } build_append_array(parent_ctx->buf, buf); build_free_array(buf); }
/* pack data with DefBuffer encoding */ static void build_buffer(GArray *array, uint8_t op) { GArray *data = build_alloc_array(); build_append_int(data, array->len); g_array_prepend_vals(array, data->data, data->len); build_free_array(data); build_package(array, op); }
static void build_append_and_cleanup_method(GArray *device, GArray *method) { uint8_t op = 0x14; /* MethodOp */ build_package(method, op, 0); build_append_array(device, method); build_free_array(method); }
void main() { int fifo_server,fifo_client; pthread_t thread[400]; while (1){ fifo_client=0; fifo_server=0; package packet; int id_client; char *buf; /* *********************handshake********************** */ fifo_server = open_wendy(fifo_server); printf("\n Abriendo pipe de comunicaci贸n \n"); packet = read_package(packet, fifo_server); char direccion[] = "/tmp/i_"; char st[32]; sprintf(st, "%s%s", direccion,packet.payload); fifo_client = open_input_pipe(fifo_client,st); id_client = atoi(packet.payload); packet = build_package(packet, packet.payload, "F1", "10", "Conexion establecida"); send_package(packet, fifo_client); close(fifo_server); close(fifo_client); /* *********************Fin del handshake********************** */ thdata data; data.id = id_client; data.index = indice; data.input_pipe = fifo_client; pthread_create (&thread[indice], NULL, (void *) &player, (void *) &data); indice++; } }
void test(void) { unsigned char write_position = DATA_OFFSET; while((write_position+3) < FUNK_MAX && uart_string[write_position - 2] != 0 && (write_position - 2) < UART_MAXSTRLEN) { if(uart_string[write_position - 2] >= 0x30 && uart_string[write_position - 2] < 0x40) funk_write_data[write_position] = uart_string[write_position - 2] - 0x30; else funk_write_data[write_position] = uart_string[write_position - 2]; write_position++; } struct funk_flags t; t.need_ack = 1; funk_send_data( (unsigned char *) funk_write_data, build_package(uart_string[1] - 0x30, write_position - DATA_OFFSET + 1, t)); }
static void build_append_notify_target_ifequal(GArray *method, GArray *target_name, uint32_t value, int size) { GArray *notify = build_alloc_array(); uint8_t op = 0xA0; /* IfOp */ build_append_byte(notify, 0x93); /* LEqualOp */ build_append_byte(notify, 0x68); /* Arg0Op */ build_append_value(notify, value, size); build_append_byte(notify, 0x86); /* NotifyOp */ build_append_array(notify, target_name); build_append_byte(notify, 0x69); /* Arg1Op */ /* Pack it up */ build_package(notify, op, 1); build_append_array(method, notify); build_free_array(notify); }
static void build_extop_package(GArray *package, uint8_t op) { build_package(package, op); build_prepend_byte(package, 0x5B); /* ExtOpPrefix */ }
static void build_ssdt(GArray *table_data, GArray *linker, AcpiCpuInfo *cpu, AcpiPmInfo *pm, AcpiMiscInfo *misc, PcPciInfo *pci, PcGuestInfo *guest_info) { int acpi_cpus = MIN(0xff, guest_info->apic_id_limit); int ssdt_start = table_data->len; uint8_t *ssdt_ptr; int i; /* Copy header and patch values in the S3_ / S4_ / S5_ packages */ ssdt_ptr = acpi_data_push(table_data, sizeof(ssdp_misc_aml)); memcpy(ssdt_ptr, ssdp_misc_aml, sizeof(ssdp_misc_aml)); if (pm->s3_disabled) { ssdt_ptr[acpi_s3_name[0]] = 'X'; } if (pm->s4_disabled) { ssdt_ptr[acpi_s4_name[0]] = 'X'; } else { ssdt_ptr[acpi_s4_pkg[0] + 1] = ssdt_ptr[acpi_s4_pkg[0] + 3] = pm->s4_val; } patch_pci_windows(pci, ssdt_ptr, sizeof(ssdp_misc_aml)); *(uint16_t *)(ssdt_ptr + *ssdt_isa_pest) = cpu_to_le16(misc->pvpanic_port); { GArray *sb_scope = build_alloc_array(); uint8_t op = 0x10; /* ScopeOp */ build_append_nameseg(sb_scope, "_SB_"); /* build Processor object for each processor */ for (i = 0; i < acpi_cpus; i++) { uint8_t *proc = acpi_data_push(sb_scope, ACPI_PROC_SIZEOF); memcpy(proc, ACPI_PROC_AML, ACPI_PROC_SIZEOF); proc[ACPI_PROC_OFFSET_CPUHEX] = acpi_get_hex(i >> 4); proc[ACPI_PROC_OFFSET_CPUHEX+1] = acpi_get_hex(i); proc[ACPI_PROC_OFFSET_CPUID1] = i; proc[ACPI_PROC_OFFSET_CPUID2] = i; } /* build this code: * Method(NTFY, 2) {If (LEqual(Arg0, 0x00)) {Notify(CP00, Arg1)} ...} */ /* Arg0 = Processor ID = APIC ID */ build_append_notify_method(sb_scope, "NTFY", "CP%0.02X", acpi_cpus); /* build "Name(CPON, Package() { One, One, ..., Zero, Zero, ... })" */ build_append_byte(sb_scope, 0x08); /* NameOp */ build_append_nameseg(sb_scope, "CPON"); { GArray *package = build_alloc_array(); uint8_t op = 0x12; /* PackageOp */ build_append_byte(package, acpi_cpus); /* NumElements */ for (i = 0; i < acpi_cpus; i++) { uint8_t b = test_bit(i, cpu->found_cpus) ? 0x01 : 0x00; build_append_byte(package, b); } build_package(package, op, 2); build_append_array(sb_scope, package); build_free_array(package); } { AcpiBuildPciBusHotplugState hotplug_state; Object *pci_host; PCIBus *bus = NULL; bool ambiguous; pci_host = object_resolve_path_type("", TYPE_PCI_HOST_BRIDGE, &ambiguous); if (!ambiguous && pci_host) { bus = PCI_HOST_BRIDGE(pci_host)->bus; } build_pci_bus_state_init(&hotplug_state, NULL); if (bus) { /* Scan all PCI buses. Generate tables to support hotplug. */ pci_for_each_bus_depth_first(bus, build_pci_bus_begin, build_pci_bus_end, &hotplug_state); } build_append_array(sb_scope, hotplug_state.device_table); build_pci_bus_state_cleanup(&hotplug_state); } build_package(sb_scope, op, 3); build_append_array(table_data, sb_scope); build_free_array(sb_scope); } build_header(linker, table_data, (void *)(table_data->data + ssdt_start), ACPI_SSDT_SIGNATURE, table_data->len - ssdt_start, 1); }
static void build_pci_bus_end(PCIBus *bus, void *bus_state) { AcpiBuildPciBusHotplugState *child = bus_state; AcpiBuildPciBusHotplugState *parent = child->parent; GArray *bus_table = build_alloc_array(); DECLARE_BITMAP(slot_hotplug_enable, PCI_SLOT_MAX); DECLARE_BITMAP(slot_device_present, PCI_SLOT_MAX); DECLARE_BITMAP(slot_device_system, PCI_SLOT_MAX); DECLARE_BITMAP(slot_device_vga, PCI_SLOT_MAX); DECLARE_BITMAP(slot_device_qxl, PCI_SLOT_MAX); uint8_t op; int i; QObject *bsel; GArray *method; bool bus_hotplug_support = false; if (bus->parent_dev) { op = 0x82; /* DeviceOp */ build_append_nameseg(bus_table, "S%.02X_", bus->parent_dev->devfn); build_append_byte(bus_table, 0x08); /* NameOp */ build_append_nameseg(bus_table, "_SUN"); build_append_value(bus_table, PCI_SLOT(bus->parent_dev->devfn), 1); build_append_byte(bus_table, 0x08); /* NameOp */ build_append_nameseg(bus_table, "_ADR"); build_append_value(bus_table, (PCI_SLOT(bus->parent_dev->devfn) << 16) | PCI_FUNC(bus->parent_dev->devfn), 4); } else { op = 0x10; /* ScopeOp */; build_append_nameseg(bus_table, "PCI0"); } bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL); if (bsel) { build_append_byte(bus_table, 0x08); /* NameOp */ build_append_nameseg(bus_table, "BSEL"); build_append_int(bus_table, qint_get_int(qobject_to_qint(bsel))); memset(slot_hotplug_enable, 0xff, sizeof slot_hotplug_enable); } else { /* No bsel - no slots are hot-pluggable */ memset(slot_hotplug_enable, 0x00, sizeof slot_hotplug_enable); } memset(slot_device_present, 0x00, sizeof slot_device_present); memset(slot_device_system, 0x00, sizeof slot_device_present); memset(slot_device_vga, 0x00, sizeof slot_device_vga); memset(slot_device_qxl, 0x00, sizeof slot_device_qxl); for (i = 0; i < ARRAY_SIZE(bus->devices); i += PCI_FUNC_MAX) { DeviceClass *dc; PCIDeviceClass *pc; PCIDevice *pdev = bus->devices[i]; int slot = PCI_SLOT(i); if (!pdev) { continue; } set_bit(slot, slot_device_present); pc = PCI_DEVICE_GET_CLASS(pdev); dc = DEVICE_GET_CLASS(pdev); if (pc->class_id == PCI_CLASS_BRIDGE_ISA) { set_bit(slot, slot_device_system); } if (pc->class_id == PCI_CLASS_DISPLAY_VGA) { set_bit(slot, slot_device_vga); if (object_dynamic_cast(OBJECT(pdev), "qxl-vga")) { set_bit(slot, slot_device_qxl); } } if (!dc->hotpluggable || pc->is_bridge) { clear_bit(slot, slot_hotplug_enable); } } /* Append Device object for each slot */ for (i = 0; i < PCI_SLOT_MAX; i++) { bool can_eject = test_bit(i, slot_hotplug_enable); bool present = test_bit(i, slot_device_present); bool vga = test_bit(i, slot_device_vga); bool qxl = test_bit(i, slot_device_qxl); bool system = test_bit(i, slot_device_system); if (can_eject) { void *pcihp = acpi_data_push(bus_table, ACPI_PCIHP_SIZEOF); memcpy(pcihp, ACPI_PCIHP_AML, ACPI_PCIHP_SIZEOF); patch_pcihp(i, pcihp); bus_hotplug_support = true; } else if (qxl) { void *pcihp = acpi_data_push(bus_table, ACPI_PCIQXL_SIZEOF); memcpy(pcihp, ACPI_PCIQXL_AML, ACPI_PCIQXL_SIZEOF); patch_pciqxl(i, pcihp); } else if (vga) { void *pcihp = acpi_data_push(bus_table, ACPI_PCIVGA_SIZEOF); memcpy(pcihp, ACPI_PCIVGA_AML, ACPI_PCIVGA_SIZEOF); patch_pcivga(i, pcihp); } else if (system) { /* Nothing to do: system devices are in DSDT. */ } else if (present) { void *pcihp = acpi_data_push(bus_table, ACPI_PCINOHP_SIZEOF); memcpy(pcihp, ACPI_PCINOHP_AML, ACPI_PCINOHP_SIZEOF); patch_pcinohp(i, pcihp); } } if (bsel) { method = build_alloc_method("DVNT", 2); for (i = 0; i < PCI_SLOT_MAX; i++) { GArray *notify; uint8_t op; if (!test_bit(i, slot_hotplug_enable)) { continue; } notify = build_alloc_array(); op = 0xA0; /* IfOp */ build_append_byte(notify, 0x7B); /* AndOp */ build_append_byte(notify, 0x68); /* Arg0Op */ build_append_int(notify, 0x1 << i); build_append_byte(notify, 0x00); /* NullName */ build_append_byte(notify, 0x86); /* NotifyOp */ build_append_nameseg(notify, "S%.02X_", PCI_DEVFN(i, 0)); build_append_byte(notify, 0x69); /* Arg1Op */ /* Pack it up */ build_package(notify, op, 0); build_append_array(method, notify); build_free_array(notify); } build_append_and_cleanup_method(bus_table, method); } /* Append PCNT method to notify about events on local and child buses. * Add unconditionally for root since DSDT expects it. */ if (bus_hotplug_support || child->notify_table->len || !bus->parent_dev) { method = build_alloc_method("PCNT", 0); /* If bus supports hotplug select it and notify about local events */ if (bsel) { build_append_byte(method, 0x70); /* StoreOp */ build_append_int(method, qint_get_int(qobject_to_qint(bsel))); build_append_nameseg(method, "BNUM"); build_append_nameseg(method, "DVNT"); build_append_nameseg(method, "PCIU"); build_append_int(method, 1); /* Device Check */ build_append_nameseg(method, "DVNT"); build_append_nameseg(method, "PCID"); build_append_int(method, 3); /* Eject Request */ } /* Notify about child bus events in any case */ build_append_array(method, child->notify_table); build_append_and_cleanup_method(bus_table, method); /* Append description of child buses */ build_append_array(bus_table, child->device_table); /* Pack it up */ if (bus->parent_dev) { build_extop_package(bus_table, op); } else { build_package(bus_table, op, 0); } /* Append our bus description to parent table */ build_append_array(parent->device_table, bus_table); /* Also tell parent how to notify us, invoking PCNT method. * At the moment this is not needed for root as we have a single root. */ if (bus->parent_dev) { build_append_byte(parent->notify_table, '^'); /* ParentPrefixChar */ build_append_byte(parent->notify_table, 0x2E); /* DualNamePrefix */ build_append_nameseg(parent->notify_table, "S%.02X_", bus->parent_dev->devfn); build_append_nameseg(parent->notify_table, "PCNT"); } } build_free_array(bus_table); build_pci_bus_state_cleanup(child); g_free(child); }
void player ( void *ptr ) { thdata *data; data = (thdata *) ptr; /* type cast to a pointer to thdata */ int fifo_output, fifo_input; char address[25] = "/tmp/o_"; char address_input[25] = "/tmp/i_"; char stfile[8]; char stfile_i[8]; char *b; char *respuesta; int pipe_id = data->id; int local_index = data->index; int numero_juego; char *token; bool invitado; players play; package pack; // Output pipe sprintf(stfile, "%d", pipe_id); strcat(address,stfile); // Input pipe sprintf(stfile_i, "%d", pipe_id); strcat(address_input,stfile_i); fifo_output = manage_output_pipe(address, fifo_output); fifo_input = data->input_pipe; fifo_input = open_input_pipe(fifo_input,address_input); printf("Estamos en un nuevo thread \n"); /** Comunicaci贸n con el cliente **/ int rd; int flag_out = 0; int start_game = 0; /* Opciones: FA: Crear un nuevo juego F6: Solicitar un juego F8: Unirse a un juego */ while(flag_out != 1){ pack = read_package(pack, fifo_output); fifo_input = open_input_pipe(fifo_input, address_input); if(strcmp(pack.type,"FA") == 0){ // Crear juego play = PLAYER1; // Usted es jugador 1 pthread_mutex_lock(&lock); // Proteger la variable juegos games[juegos].id_creator = pipe_id; numero_juego = juegos; games[numero_juego].state = STATE_A; juegos++; pthread_mutex_unlock(&lock); games[numero_juego].id = pipe_id; // Fin de protecci贸n variable juegos token = strtok(pack.payload , ","); int counter_tokens = 0; while (token != NULL){ counter_tokens++; if(counter_tokens == 1) games[numero_juego].dimension = atoi(token); else if(counter_tokens == 2) strcpy(games[numero_juego].name, token); else if(counter_tokens == 3) games[numero_juego].t1 = atoi(token); else if(counter_tokens == 4) games[numero_juego].t2 = atoi(token); else if(counter_tokens == 5) games[numero_juego].t3 = atoi(token); token = strtok(NULL,","); } printf ("Juego creado con los siguientes datos: \nDim: %d, Nombre: %s, T1: %d, T2: %d, T3: %d \n", games[numero_juego].dimension, games[numero_juego].name, games[numero_juego].t1, games[numero_juego].t2, games[numero_juego]. t3); flag_out = 1; start_game = 3; } else if( strcmp(pack.type,"F6") == 0){ // Solicitar juegos int index_games; char list_games[255]; char tmpID[32]; sprintf(tmpID, "%d", games[numero_juego].id ); for (index_games = 0; index_games < juegos ; index_games++){ if(games[index_games].active != 1){ char tmpPayload[255]; sprintf(tmpPayload, "%d-%s-%dx%d\n", games[index_games].id, games[index_games].name, games[index_games].dimension,games[index_games].dimension); strcat(list_games, tmpPayload); } } // Env铆a lista de juegos pack = build_package(pack, tmpID , "F5", "10", list_games); send_package(pack, fifo_input); start_game = 2; } else if( strcmp(pack.type,"F8") == 0 ){ // Unirse a un juego int i=0; for(i=0;i<indice;i++){ if((games[i].id == atoi(pack.payload)) && (flag_out != 1)){ numero_juego = i; games[i].id_guest = pipe_id; games[i].state = STATE_A; games[i].active = 1; play = PLAYER2; printf("Juego con id: %d asignado! \n", games[i].id); start_game = 1; flag_out = 1; } } } /* Opciones: 1: Confirmaci贸n de asignaci贸n de juego 0: Recepci贸n de mensaje */ if(start_game == 0){ pack = build_package(pack, "-" , "00", "10", "Mensaje recibido"); // Env铆a paquete de recepci贸n send_package(pack, fifo_input); } else if(start_game == 1){ char tmpId[32]; sprintf(tmpId, "%d", games[numero_juego].id); char info[255]; sprintf(info,"%s,%d,%d,%d,%d", games[numero_juego].name, games[numero_juego].dimension, games[numero_juego].t1, games[numero_juego].t2, games[numero_juego].t3); pack = build_package(pack, tmpId , "F9", "10", info); // Env铆a paquete de asignaci贸n send_package(pack, fifo_input); } else if(start_game == 3){ char tmpId[32]; sprintf(tmpId, "%d", games[numero_juego].id); char info[255]; sprintf(info,"Se ha creado un juego exitosamente, con ID: %d", games[numero_juego].id); pack = build_package(pack, "-" , "FB", "10", info); // Env铆a paquete de asignaci贸n send_package(pack, fifo_input); } close(fifo_input); } // Esperando a que el juego este activo para continuar while(games[numero_juego].active != 1){ // do nothing } // Env铆a mensaje de Inicio de juego fifo_input = open_input_pipe(fifo_input, address_input); pack = build_package(pack, pack.payload, "01", "10", "隆Inicia juego!"); send_package(pack, fifo_input); //Leyendo posiciones de los barcos pack = read_package(pack, fifo_output); char *token_positions; token_positions = strtok(pack.payload , ","); int ctrtokens = 0; while (token_positions != NULL){ //Posicion tempPos = get_positions(token_positions); if(play == PLAYER1){ if ((ctrtokens % 2) == 0){ games[numero_juego].posP1[ctrtokens].y = atoi(token_positions); printf("PLAYER 1 VAL: %d \n",games[numero_juego].posP1[ctrtokens].y ); } else{ games[numero_juego].posP1[ctrtokens].x = atoi(token_positions); printf("PLAYER 1 VAL: %d \n",games[numero_juego].posP1[ctrtokens].x ); } } else{ if ((ctrtokens % 2) == 0){ games[numero_juego].posP2[ctrtokens].y = atoi(token_positions); printf("PLAYER 2 VAL: %d \n",games[numero_juego].posP2[ctrtokens].y ); } else{ games[numero_juego].posP2[ctrtokens].x = atoi(token_positions); printf("PLAYER 2 VAL: %d \n",games[numero_juego].posP2[ctrtokens].x ); } } ctrtokens++; token_positions = strtok(NULL,","); } // Fin de posicionamiento de barcos int jz = 0; while(jz != 5){ if(play == PLAYER1){ /* Esperar al estado A*/ pthread_mutex_lock(&games[numero_juego].mutex); while (games[numero_juego].state != STATE_A) pthread_cond_wait(&games[numero_juego].condA, &games[numero_juego].mutex); pthread_mutex_unlock(&games[numero_juego].mutex); pack = build_package(pack, pack.payload, "03", "10", "Tu turno!"); send_package(pack, fifo_input); //rd = read (fifo_output,&jz,sizeof(int)); pack = read_package(pack, fifo_output); printf("Respuesta del cliente 1: %d del juego %s \n", jz, pack.payload); /* Liberar al estado B*/ pthread_mutex_lock(&games[numero_juego].mutex); games[numero_juego].state = STATE_B; pthread_cond_signal(&games[numero_juego].condB); pthread_mutex_unlock(&games[numero_juego].mutex); } else{ /* Esperar al estado B*/ pthread_mutex_lock(&games[numero_juego].mutex); while (games[numero_juego].state != STATE_B) pthread_cond_wait(&games[numero_juego].condB, &games[numero_juego].mutex); pthread_mutex_unlock(&games[numero_juego].mutex); pack = build_package(pack, pack.payload, "03", "10", "Tu turno!"); send_package(pack, fifo_input); //rd = read (fifo_output,&jz,sizeof(int)); pack = read_package(pack, fifo_output); printf("Respuesta del cliente 2: %d del juego %s \n", jz, pack.payload); /* Esperar al estado A*/ pthread_mutex_lock(&games[numero_juego].mutex); games[numero_juego].state = STATE_A; pthread_cond_signal(&games[numero_juego].condA); pthread_mutex_unlock(&games[numero_juego].mutex); } } close(fifo_input); close(fifo_output); printf("Fin de comunicaci贸n \n"); }
void client_mode(const char *client_port) { struct sockaddr_in dst; uint32_t build_info_len; ssize_t recv_bytes, sent_bytes; char *build_info; int fd; if (parse_sockaddr_in(client_port, &dst)) errx(1, "Could not parse addr/port"); fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) err(1, "Could not create socket"); if (connect(fd, (struct sockaddr *)&dst, sizeof(dst)) == -1) err(1, "Could not connect socket"); loop: sent_bytes = atomic_write(fd, "G", 1); if (sent_bytes == -1) err(1, "Could not write to socket"); if (sent_bytes == 0) exit(0); if (sent_bytes != 1) errx(1, "Premature end of stream while writing to socket"); recv_bytes = atomic_read(fd, &build_info_len, 4); if (recv_bytes == 0 || (recv_bytes == -1 && errno == ECONNRESET)) exit(0); if (recv_bytes == -1) err(1, "Could not read from socket"); if (recv_bytes != 4) errx(1, "Premature end while reading build info from socket"); build_info_len = ntohl(build_info_len); if (build_info_len < 10 || build_info_len > 0xffffff) errx(1, "Invalid build info length from master"); build_info = xmalloc(build_info_len + 1); build_info[build_info_len] = '\0'; recv_bytes = atomic_read(fd, build_info, build_info_len); if (recv_bytes == -1) err(1, "Could not read from socket"); if ((uint32_t)recv_bytes != build_info_len || strlen(build_info) != build_info_len) errx(1, "Premature end of stream while reading path from socket"); if (verbosity > 0) { const char *begin, *end; if (strncmp(build_info, "PKGNAME=", 8) != 0) err(1, "Inconsistent build info from server"); begin = build_info + 8; if ((end = strchr(begin, '\n')) == NULL) err(1, "Inconsistent build info from server"); printf("Building package %.*s\n", (int)(end - begin), begin); fflush(stdout); } if (build_package(build_info, build_info_len) == 0) sent_bytes = atomic_write(fd, "D", 1); else sent_bytes = atomic_write(fd, "F", 1); if (sent_bytes == -1) err(1, "Could not write to socket"); if (sent_bytes != 1) errx(1, "Premature end of stream while writing to socket"); free(build_info); goto loop; }