/* * Receive a token from a file descriptor. Takes the file descriptor, a * buffer into which to store the token, a pointer into which to store the * flags, and the maximum token length we're willing to accept. Returns * TOKEN_OK on success. On failure, returns one of: * * TOKEN_FAIL_SYSTEM System call failed, errno set * TOKEN_FAIL_SOCKET Socket call failed, socket_errno set * TOKEN_FAIL_INVALID Invalid token format * TOKEN_FAIL_LARGE Token data larger than provided limit * TOKEN_FAIL_EOF Unexpected end of file * TOKEN_FAIL_TIMEOUT Timeout receiving token * * TOKEN_FAIL_SYSTEM and TOKEN_FAIL_SOCKET are the same on UNIX but different * on Windows. * * recv_token reads the token flags (a single byte, even though they're stored * into an integer, then reads the token length (as a network long), allocates * memory to hold the data, and then reads the token data from the file * descriptor. On a successful return, the value member of the token should * be freed with free(). */ enum token_status token_recv(socket_type fd, int *flags, gss_buffer_t tok, size_t max, time_t timeout) { OM_uint32 len; unsigned char char_flags; int err; if (!network_read(fd, &char_flags, 1, timeout)) return map_socket_error(socket_errno); *flags = char_flags; if (!network_read(fd, &len, sizeof(OM_uint32), timeout)) return map_socket_error(socket_errno); tok->length = ntohl(len); if (tok->length > max) return TOKEN_FAIL_LARGE; if (tok->length == 0) { tok->value = NULL; return TOKEN_OK; } tok->value = malloc(tok->length); if (tok->value == NULL) return TOKEN_FAIL_SYSTEM; if (!network_read(fd, tok->value, tok->length, timeout)) { err = socket_errno; free(tok->value); socket_set_errno(err); return map_socket_error(err); } return TOKEN_OK; }
/* * Test the network read function with a timeout. We fork off a child process * that runs delay_writer, and then we read from the network twice, once with * a timeout and once without, and then try a third time when we should time * out. */ static void test_network_read(void) { socket_type fd, c; pid_t child; struct sockaddr_in sin; socklen_t slen; char buffer[4]; /* Create the listening socket. */ fd = network_bind_ipv4(SOCK_STREAM, "127.0.0.1", 11119); if (fd == INVALID_SOCKET) sysbail("cannot create or bind socket"); if (listen(fd, 1) < 0) sysbail("cannot listen to socket"); /* Fork off a child process that writes some data with delays. */ child = fork(); if (child < 0) sysbail("cannot fork"); else if (child == 0) { socket_close(fd); client_delay_writer("127.0.0.1"); } /* Set an alarm just in case our timeouts don't work. */ alarm(10); /* Accept the client connection. */ slen = sizeof(sin); c = accept(fd, &sin, &slen); if (c == INVALID_SOCKET) sysbail("cannot accept on socket"); /* Now test a couple of simple reads, with and without timeout. */ socket_set_errno(0); ok(network_read(c, buffer, sizeof(buffer), 0), "network_read"); ok(memcmp("one\n", buffer, sizeof(buffer)) == 0, "...with good data"); ok(network_read(c, buffer, sizeof(buffer), 1), "network_read with timeout"); ok(memcmp("two\n", buffer, sizeof(buffer)) == 0, "...with good data"); /* * The third read should abort with a timeout, since the writer is writing * with a ten second delay. */ ok(!network_read(c, buffer, sizeof(buffer), 1), "network_read aborted with timeout"); is_int(ETIMEDOUT, socket_errno, "...with correct error"); ok(memcmp("two\n", buffer, sizeof(buffer)) == 0, "...and data unchanged"); alarm(0); /* Clean up. */ socket_close(c); kill(child, SIGTERM); waitpid(child, NULL, 0); socket_close(fd); }
void udp_receive( void ) { //First check if it isn't already having a message if(dl_msg_available == TRUE) { return; } //Read from the network network_read(network, udp_read_buffer, TRANSPORT_PAYLOAD_LEN); //Parse the packet if(udp_read_buffer[0] == STX) { uint8_t size = udp_read_buffer[1]-4; // minus STX, LENGTH, CK_A, CK_B uint8_t ck_aa, ck_bb; ck_aa = ck_bb = size+4; // index-offset plus 2 for STX and LENGTH for (int i = 2; i < size+2; i++) { dl_buffer[i-2] = udp_read_buffer[i]; ck_aa += udp_read_buffer[i]; ck_bb += ck_aa; } // if both checksums are good, tell datalink that the message is available if (udp_read_buffer[2+size] == ck_aa && udp_read_buffer[2+size+1] == ck_bb) { dl_msg_available = TRUE; } } memset(&udp_read_buffer[0], 0, sizeof(udp_read_buffer)); }
void gps_parse(void) { if (gps_network == NULL) { return; } //Read from the network int size = network_read(gps_network, &gps_udp_read_buffer[0], 256); if (size > 0) { // Correct MSG if ((size == GPS_UDP_MSG_LEN) && (gps_udp_read_buffer[0] == STX)) { gps.lla_pos.lat = UDP_GPS_INT(gps_udp_read_buffer + 4); gps.lla_pos.lon = UDP_GPS_INT(gps_udp_read_buffer + 8); gps.lla_pos.alt = UDP_GPS_INT(gps_udp_read_buffer + 12); gps.hmsl = UDP_GPS_INT(gps_udp_read_buffer + 16); gps.ecef_pos.x = UDP_GPS_INT(gps_udp_read_buffer + 20); gps.ecef_pos.y = UDP_GPS_INT(gps_udp_read_buffer + 24); gps.ecef_pos.z = UDP_GPS_INT(gps_udp_read_buffer + 28); gps.ecef_vel.x = UDP_GPS_INT(gps_udp_read_buffer + 32); gps.ecef_vel.y = UDP_GPS_INT(gps_udp_read_buffer + 36); gps.ecef_vel.z = UDP_GPS_INT(gps_udp_read_buffer + 40); gps.fix = GPS_FIX_3D; #if GPS_USE_LATLONG // Computes from (lat, long) in the referenced UTM zone struct LlaCoor_f lla_f; LLA_FLOAT_OF_BFP(lla_f, gps.lla_pos); struct UtmCoor_f utm_f; utm_f.zone = nav_utm_zone0; // convert to utm utm_of_lla_f(&utm_f, &lla_f); // copy results of utm conversion gps.utm_pos.east = utm_f.east * 100; gps.utm_pos.north = utm_f.north * 100; gps.utm_pos.alt = gps.lla_pos.alt; gps.utm_pos.zone = nav_utm_zone0; #endif // publish new GPS data uint32_t now_ts = get_sys_time_usec(); gps.last_msg_ticks = sys_time.nb_sec_rem; gps.last_msg_time = sys_time.nb_sec; if (gps.fix == GPS_FIX_3D) { gps.last_3dfix_ticks = sys_time.nb_sec_rem; gps.last_3dfix_time = sys_time.nb_sec; } AbiSendMsgGPS(GPS_UDP_ID, now_ts, &gps); } else { printf("gps_udp error: msg len invalid %d bytes\n", size); } memset(&gps_udp_read_buffer[0], 0, sizeof(gps_udp_read_buffer)); } }
/* * Used to test network_write. Connects, reads 64KB from the network, then * sleeps before reading another 64KB. Meant to be run in a child process. */ static void client_delay_reader(const char *host) { char *buffer; socket_type fd; fd = network_connect_host(host, 11119, NULL, 0); if (fd == INVALID_SOCKET) _exit(1); buffer = malloc(64 * 1024); if (buffer == NULL) _exit(1); if (!network_read(fd, buffer, 64 * 1024, 0)) _exit(1); sleep(10); if (!network_read(fd, buffer, 64 * 1024, 0)) _exit(1); free(buffer); _exit(0); }
void gps_parse(void) { if (gps_network == NULL) return; //Read from the network int size = network_read( gps_network, &gps_udp_read_buffer[0], 256); if(size > 0) { // Correct MSG if ((size == GPS_UDP_MSG_LEN) && (gps_udp_read_buffer[0] == STX)) { gps.lla_pos.lat = RadOfDeg(UDP_GPS_INT(gps_udp_read_buffer+4)); gps.lla_pos.lon = RadOfDeg(UDP_GPS_INT(gps_udp_read_buffer+8)); gps.lla_pos.alt = UDP_GPS_INT(gps_udp_read_buffer+12); gps.hmsl = UDP_GPS_INT(gps_udp_read_buffer+16); gps.ecef_pos.x = UDP_GPS_INT(gps_udp_read_buffer+20); gps.ecef_pos.y = UDP_GPS_INT(gps_udp_read_buffer+24); gps.ecef_pos.z = UDP_GPS_INT(gps_udp_read_buffer+28); gps.ecef_vel.x = UDP_GPS_INT(gps_udp_read_buffer+32); gps.ecef_vel.y = UDP_GPS_INT(gps_udp_read_buffer+36); gps.ecef_vel.z = UDP_GPS_INT(gps_udp_read_buffer+40); gps.fix = GPS_FIX_3D; gps_available = TRUE; #if GPS_USE_LATLONG // Computes from (lat, long) in the referenced UTM zone struct LlaCoor_f lla_f; lla_f.lat = ((float) gps.lla_pos.lat) / 1e7; lla_f.lon = ((float) gps.lla_pos.lon) / 1e7; struct UtmCoor_f utm_f; utm_f.zone = nav_utm_zone0; // convert to utm utm_of_lla_f(&utm_f, &lla_f); // copy results of utm conversion gps.utm_pos.east = utm_f.east*100; gps.utm_pos.north = utm_f.north*100; gps.utm_pos.alt = gps.lla_pos.alt; gps.utm_pos.zone = nav_utm_zone0; #endif } else { printf("gps_udp error: msg len invalid %d bytes\n",size); } memset(&gps_udp_read_buffer[0], 0, sizeof(gps_udp_read_buffer)); } }
/** The NatNet sampler periodic function */ static gboolean sample_data(GIOChannel *chan, GIOCondition cond, gpointer data) { static unsigned char buffer_data[MAX_PACKETSIZE]; static int bytes_data = 0; // Keep on reading until we have the whole packet bytes_data += network_read(natnet_data, buffer_data, MAX_PACKETSIZE); // Parse NatNet data if(bytes_data >= 2 && bytes_data >= buffer_data[1]) { natnet_parse(buffer_data); bytes_data = 0; } return TRUE; }
void gps_udp_parse(void) { if (gps_network == NULL) { return; } //Read from the network int size = network_read(gps_network, &gps_udp_read_buffer[0], 256); if (size > 0) { // Correct MSG if ((size == GPS_UDP_MSG_LEN) && (gps_udp_read_buffer[0] == STX)) { gps_udp.lla_pos.lat = UDP_GPS_INT(gps_udp_read_buffer + 4); gps_udp.lla_pos.lon = UDP_GPS_INT(gps_udp_read_buffer + 8); gps_udp.lla_pos.alt = UDP_GPS_INT(gps_udp_read_buffer + 12); SetBit(gps_udp.valid_fields, GPS_VALID_POS_LLA_BIT); gps_udp.hmsl = UDP_GPS_INT(gps_udp_read_buffer + 16); SetBit(gps_udp.valid_fields, GPS_VALID_HMSL_BIT); gps_udp.ecef_pos.x = UDP_GPS_INT(gps_udp_read_buffer + 20); gps_udp.ecef_pos.y = UDP_GPS_INT(gps_udp_read_buffer + 24); gps_udp.ecef_pos.z = UDP_GPS_INT(gps_udp_read_buffer + 28); SetBit(gps_udp.valid_fields, GPS_VALID_POS_ECEF_BIT); gps_udp.ecef_vel.x = UDP_GPS_INT(gps_udp_read_buffer + 32); gps_udp.ecef_vel.y = UDP_GPS_INT(gps_udp_read_buffer + 36); gps_udp.ecef_vel.z = UDP_GPS_INT(gps_udp_read_buffer + 40); SetBit(gps_udp.valid_fields, GPS_VALID_VEL_ECEF_BIT); gps_udp.fix = GPS_FIX_3D; // publish new GPS data uint32_t now_ts = get_sys_time_usec(); gps_udp.last_msg_ticks = sys_time.nb_sec_rem; gps_udp.last_msg_time = sys_time.nb_sec; if (gps_udp.fix == GPS_FIX_3D) { gps_udp.last_3dfix_ticks = sys_time.nb_sec_rem; gps_udp.last_3dfix_time = sys_time.nb_sec; } AbiSendMsgGPS(GPS_UDP_ID, now_ts, &gps_udp); } else { printf("gps_udp error: msg len invalid %d bytes\n", size); } memset(&gps_udp_read_buffer[0], 0, sizeof(gps_udp_read_buffer)); } }
void ethernet_process(void) { if(linkstate == 0) { Dm9161 *pDm = &gDm9161; if( DM9161_GetLinkSpeed(pDm, 1) != 0 ) { TRACE_INFO("P: Link detected\n\r"); linkstate=1; LED2_ON(); // Auto Negotiate if (!DM9161_AutoNegotiate(pDm)) { TRACE_INFO("P: Auto Negotiate ERROR!\n\r"); return; } } } else { uip_len = network_read(); if(uip_len > 0) { if(BUF->type == htons(UIP_ETHTYPE_IP)){ uip_arp_ipin(); uip_input(); if(uip_len > 0) { uip_arp_out(); network_send(); } } else if(BUF->type == htons(UIP_ETHTYPE_ARP)){ uip_arp_arpin(); if(uip_len > 0){ network_send(); } } } } }
void nethandler_rx() { uip_len = network_read(); if (uip_len > 0) { switch (ntohs(BUF->type)) { case UIP_ETHTYPE_IP: uip_arp_ipin(); uip_input(); if (uip_len > 0) { uip_arp_out(); network_send(); } break; case UIP_ETHTYPE_ARP: uip_arp_arpin(); if (uip_len > 0) network_send(); break; } } }
int main_forward() { int ret = 1; /* reuse the normal network setup */ if (network_init()) { /* Now accept from gdb */ if (network_accept()) { /* Now connect to remote deebe */ if (network_connect()) { while (true) { network_read(); if (network_in_buffer_total > 0) { _forward_packet(&network_out_buffer[0], &network_in_buffer[0], &network_out_buffer_total, &network_in_buffer_total); network_write_fwd(); } network_read_fwd(); if (network_in_buffer_total > 0) { _forward_packet(&network_out_buffer[0], &network_in_buffer[0], &network_out_buffer_total, &network_in_buffer_total); network_write(); } } } } network_cleanup(); } return ret; }
/**************************************************************************** * Download a file from a given url to a given path with a Progressbar ****************************************************************************/ int DownloadFileToPath(const char *orig_url, const char *dest, bool UseFilename) { if(!orig_url || !dest) { ShowError(tr("No URL or Path specified.")); return -2; } bool addhttp = false; if(strncasecmp(orig_url, "http://", strlen("http://")) != 0) { addhttp = true; } char url[strlen(orig_url) + (addhttp ? strlen("http://") : 0) + 1]; if(addhttp) snprintf(url, sizeof(url), "http://%s", orig_url); else strcpy(url, orig_url); char *path = strchr(url + strlen("http://"), '/'); if(!path) { ShowError(tr("Not a valid URL path")); return -2; } int domainlength = path - url - strlen("http://"); if(domainlength == 0) { ShowError(tr("Not a valid domain")); return -3; } char domain[domainlength + 1]; strncpy(domain, url + strlen("http://"), domainlength); domain[domainlength] = '\0'; int connection = GetConnection(domain); if(connection < 0) { ShowError(tr("Could not connect to the server.")); return -4; } char header[1024]; char * ptr = header; ptr += sprintf(ptr, "GET %s HTTP/1.1\r\n", path); ptr += sprintf(ptr, "Host: %s\r\n", domain); ptr += sprintf(ptr, "Referer: %s\r\n", domain); ptr += sprintf(ptr, "User-Agent: WiiXplorer\r\n"); ptr += sprintf(ptr, "Pragma: no-cache\r\n"); ptr += sprintf(ptr, "Cache-Control: no-cache\r\n"); ptr += sprintf(ptr, "Connection: close\r\n\r\n"); char filename[255]; memset(filename, 0, sizeof(filename)); int filesize = network_request(connection, header, filename); if(filesize <= 0) { net_close(connection); ShowError(tr("Filesize is %i Byte."), filesize); return -5; } int blocksize = 4*1024; u8 *buffer = (u8 *) malloc(blocksize); if(!buffer) { net_close(connection); ShowError(tr("Not enough memory.")); return -6; } if(UseFilename) { if(dest[strlen(dest)-1] != '/') strcat((char *) dest, "/"); CreateSubfolder(dest); strcat((char *) dest, filename); } if(!UseFilename && strcmp(filename, "") == 0) { const char * ptr = strrchr(dest, '/'); if(ptr) ptr++; else ptr = dest; snprintf(filename, sizeof(filename), "%s", ptr); } FILE *file = fopen(dest, "wb"); if(!file) { net_close(connection); free(buffer); ShowError(tr("Cannot write to destination.")); return -7; } ProgressCancelEnable(true); StartProgress(tr("Downloading file..."), 0, filename, true, true); int done = 0; while(done < filesize) { if(ProgressCanceled()) { done = PROGRESS_CANCELED; break; } ShowProgress(done, filesize); if(blocksize > filesize - done) blocksize = filesize - done; s32 read = network_read(connection, buffer, blocksize); if(read < 0) { done = -8; ShowError(tr("Transfer failed")); break; } else if(!read) break; fwrite(buffer, 1, read, file); done += read; } free(buffer); ProgressStop(); net_close(connection); fclose(file); ProgressStop(); ProgressCancelEnable(false); return done; }
int main(void) { network_init(); //CLKPR = (1<<CLKPCE); //Change prescaler //CLKPR = (1<<CLKPS0); //Use prescaler 2 //clock_prescale_set(clock_div_2); enc28j60Write(ECOCON, 1 & 0x7); //Get a 25MHz signal from enc28j60 uartInit(); uartSetBaudRate(9600); rprintfInit(uartSendByte); int i; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; clock_init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); uip_init(); struct uip_eth_addr mac = {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5}; uip_setethaddr(mac); // set up the udp data stream // configure the target system ip and port uip_ipaddr_t target_ipaddr; uip_ipaddr(&target_ipaddr, 192,168,0,10); udpds_conf(&target_ipaddr, 1000, 1); #ifdef __DHCPC_H__ dhcpc_init(&mac, 6); #else uip_ipaddr(ipaddr, 192,168,0,1); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, 192,168,0,1); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, 255,255,255,0); uip_setnetmask(ipaddr); #endif /*__DHCPC_H__*/ while(1){ uip_len = network_read(); if(uip_len > 0) { if(BUF->type == htons(UIP_ETHTYPE_IP)){ uip_arp_ipin(); uip_input(); if(uip_len > 0) { uip_arp_out(); network_send(); } }else if(BUF->type == htons(UIP_ETHTYPE_ARP)){ uip_arp_arpin(); if(uip_len > 0){ network_send(); } } }else if(timer_expired(&periodic_timer)) { timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } } return 0; }
int main(void) { network_init(); #if MY_DEBUG uartInit(); uartSetBaudRate(9600); rprintfInit(uartSendByte); #endif int i; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; clock_init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); uip_init(); struct uip_eth_addr mac = {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5}; uip_setethaddr(mac); telnetd_init(); #ifdef __DHCPC_H__ dhcpc_init(&mac, 6); #else uip_ipaddr(ipaddr, 192,168,0,1); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, 192,168,0,1); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, 255,255,255,0); uip_setnetmask(ipaddr); #endif /*__DHCPC_H__*/ while(1){ uip_len = network_read(); if(uip_len > 0) { if(BUF->type == htons(UIP_ETHTYPE_IP)){ uip_arp_ipin(); uip_input(); if(uip_len > 0) { uip_arp_out(); network_send(); } }else if(BUF->type == htons(UIP_ETHTYPE_ARP)){ uip_arp_arpin(); if(uip_len > 0){ network_send(); } } }else if(timer_expired(&periodic_timer)) { timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } } return 0; }
int HomebrewReceiver::ReceiveFile() { char filesizetxt[50]; char temp[50]; u32 filesize = 0; if (infilesize < MB_SIZE) snprintf(filesizetxt, sizeof(filesizetxt), tr( "Incoming file %0.2fKB" ), infilesize / KB_SIZE); else snprintf(filesizetxt, sizeof(filesizetxt), tr( "Incoming file %0.2fMB" ), infilesize / MB_SIZE); snprintf(temp, sizeof(temp), tr( "Load file from: %s ?" ), GetIncommingIP()); int choice = WindowPrompt(filesizetxt, temp, tr( "Load" ), tr( "Cancel" )); if (choice == 0) return 0; u32 read = 0; int len = NET_BLOCKSIZE; filesize = infilesize; u8 * buffer = (u8 *) malloc(infilesize); if(!buffer) { CloseConnection(); WindowPrompt(tr( "Not enough memory." ), 0, tr( "OK" )); return 0; } bool error = false; while (read < infilesize) { ShowProgress(tr( "Receiving file from:" ), GetIncommingIP(), NULL, read, infilesize, true); if (infilesize - read < (u32) len) len = infilesize - read; else len = NET_BLOCKSIZE; int result = network_read(connection, buffer+read, len); if (result < 0) { CloseConnection(); ProgressStop(); WindowPrompt(tr( "Error while transfering data." ), 0, tr( "OK" )); free(buffer); return 0; } if (!result) { break; } read += result; } char filename[101]; memset(filename, 0, sizeof(filename)); network_read(connection, (u8*) filename, 100); //! Uncompress Wiiload if (wiiloadVersion[0] > 0 || wiiloadVersion[1] > 4) { u8 *unc = (u8 *) malloc(uncfilesize); if(!unc) { free(buffer); CloseConnection(); ProgressStop(); WindowPrompt(tr( "Not enough memory." ), 0, tr( "OK" )); return 0; } uLongf f = uncfilesize; error = uncompress(unc, &f, buffer, infilesize) != Z_OK; uncfilesize = f; filesize = uncfilesize; free(buffer); buffer = unc; free(unc); } CopyHomebrewMemory(buffer, 0, filesize); ProgressStop(); if (error || read != infilesize) { WindowPrompt(tr( "Error:" ), tr( "No data could be read." ), tr( "OK" )); FreeHomebrewBuffer(); return 0; } CloseConnection(); AddBootArgument(filename); return BootHomebrewFromMem(); }
int main(void) { led_conf(); int i; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); WDTCSR |= _BV(_WD_CHANGE_BIT) | _BV(WDE); WDTCSR = 0; network_init(); clock_init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); uip_init(); phys_init(); // must be done or sometimes arp doesn't work uip_arp_init(); _enable_dhcp=eeprom_read_byte(&ee_enable_dhcp); if ((_enable_dhcp != 1) && (_enable_dhcp != 0)) { // if the setting is invalid, enable by default _enable_dhcp = 1; eeprom_write_byte(&ee_enable_dhcp,_enable_dhcp); } eeprom_read_block ((void *)_eth_addr, (const void *)&ee_eth_addr,6); // if the mac address in eeprom looks good, use it. if((_eth_addr[0] != 255) && (_eth_addr[0] != 0)) { my_eth_addr.addr[0] = _eth_addr[0]; my_eth_addr.addr[1] = _eth_addr[1]; my_eth_addr.addr[2] = _eth_addr[2]; my_eth_addr.addr[3] = _eth_addr[3]; my_eth_addr.addr[4] = _eth_addr[4]; my_eth_addr.addr[5] = _eth_addr[5]; } uip_setethaddr(my_eth_addr); _enable_dhcp = 1; if (_enable_dhcp) { #ifdef __DHCPC_H__ // setup the dhcp renew timer the make the first request timer_set(&dhcp_timer, CLOCK_SECOND * 600); dhcpc_init(&my_eth_addr, 6); dhcpc_request(); #endif } else { eeprom_read_block ((void *)_ip_addr, (const void *)&ee_ip_addr,4); eeprom_read_block ((void *)_net_mask,(const void *)&ee_net_mask,4); eeprom_read_block ((void *)_gateway, (const void *)&ee_gateway,4); // if the IP looks good in flash, use it if ((_ip_addr[0] != 255) && (_ip_addr[0] != 0)) { uip_ipaddr(ipaddr, _ip_addr[0], _ip_addr[1], _ip_addr[2], _ip_addr[3]); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, _gateway[0], _gateway[1], _gateway[2], _gateway[3]); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, _net_mask[0], _net_mask[1], _net_mask[2], _net_mask[3]); uip_setnetmask(ipaddr); } else { // ip in flash didn't look good... use default uip_ipaddr(ipaddr, UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, UIP_IPADDR3); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, UIP_DRIPADDR0, UIP_DRIPADDR1, UIP_DRIPADDR2, UIP_DRIPADDR3); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, UIP_NETMASK0, UIP_NETMASK1, UIP_NETMASK2, UIP_NETMASK3); uip_setnetmask(ipaddr); } } jsoncmd_init(); while (1) { uip_len = network_read(); if(uip_len > 0) { if(BUF->type == htons(UIP_ETHTYPE_IP)) { led_on(5); uip_arp_ipin(); // arp seems to have issues w/o this uip_input(); if(uip_len > 0) { uip_arp_out(); network_send(); } } else if(BUF->type == htons(UIP_ETHTYPE_ARP)) { led_on(4); uip_arp_arpin(); // this is correct if(uip_len > 0) { network_send(); } } } else if(timer_expired(&periodic_timer)) { led_off(4); led_off(5); timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } else if (_enable_dhcp && timer_expired(&dhcp_timer)) { #ifdef __DHCPC_H__ led_on(3); // for now turn off the led when we start the dhcp process dhcpc_renew(); timer_reset(&dhcp_timer); #endif } } return 0; }
const u8 * NetReceiver::ReceiveData() { if(connection < 0) return NULL; if(filebuffer) free(filebuffer); filebuffer = NULL; filebuffer = (u8 *) malloc(filesize); if(!filebuffer) { ShowError(tr("Not enough memory.")); return NULL; } StartProgress(tr("Receiving file...")); u32 done = 0; u32 blocksize = 5*1024; char tmptxt[200]; snprintf(tmptxt, sizeof(tmptxt), "Incomming from: %s", incommingIP); int retries = 5; do { if(ProgressWindow::Instance()->IsCanceled()) { FreeData(); StopProgress(); ShowError(tr("Transfer cancelled.")); return NULL; } if (blocksize > filesize - done) blocksize = filesize - done; ShowProgress(done, filesize, tmptxt); int result = network_read(connection, filebuffer+done, blocksize); if(result < 0) { --retries; if(retries == 0) { FreeData(); StopProgress(); ShowError(tr("Transfer failed.")); return NULL; } } if(!result) { --retries; if(!retries) break; } done += result; } while(done < filesize); // finish up the progress FinishProgress(filesize); StopProgress(); if(done != filesize) { FreeData(); ShowError(tr("Filesize doesn't match.")); } char temp[50]; network_read(connection, (u8 *) temp, 49); temp[49] = 0; snprintf(FileName, sizeof(FileName), "%s", temp); if(UncompressData() == NULL) { FreeData(); ShowError(tr("Could not decompress the file.")); } return filebuffer; }
/* * Parse BOOT information from BOOT SECTOR */ void load_boot(unsigned char *bytes) { network_seek(11, SEEK_SET_N); //skip 11 bytes /* BYTES_PER_SECTOR (2 bytes) */ network_read(&boot.BYTES_PER_SECTOR, 2, bytes); /* SECTORS_PER_CLUSTER (1 byte) */ network_read(&boot.SECTORS_PER_CLUSTER, 1, bytes); /* RESERVED_SECTORS (2 bytes) */ network_read(&boot.RESERVED_SECTORS, 2, bytes); /* NUM_OF_FATS (1 byte) */ network_read(&boot.NUM_OF_FATS, 1, bytes); /* MAX_ROOT_DIRS (2 bytes) */ network_read(&boot.MAX_ROOT_DIRS, 2, bytes); /* TOTAL_SECTORS (2 bytes) */ network_read(&boot.TOTAL_SECTORS, 2, bytes); network_seek(1, SEEK_CUR_N); //skip 1 byte /* SECTORS_PER_FAT (2 bytes) */ network_read(&boot.SECTORS_PER_FAT, 2, bytes); /* SECTORS_PER_TRACK (2 bytes) */ network_read(&boot.SECTORS_PER_TRACK, 2, bytes); /* NUM_OF_HEADS (2 bytes) */ network_read(&boot.NUM_OF_HEADS, 2, bytes); network_seek(11, SEEK_CUR_N); //skip 11 bytes /* VOLUME_ID (4 bytes) */ network_read(&boot.VOLUME_ID, 4, bytes); /* VOLUME_LABEL (11 bytes) */ network_read(&boot.VOLUME_LABEL, 11, bytes); }
/* * Parse ENTRY information from ROOT_DIRECTORY */ void load_entry(int start, unsigned char *bytes) { /* Set reader to 0 */ network_seek(0, SEEK_SET_N); /* Load data into entry */ for (int i = start; i < (start + 16); i++) { /* FILENAME (8 bytes) */ network_read(&entry[i].FILENAME, 8, bytes); /* EXT (3 bytes) */ network_read(&entry[i].EXT, 3, bytes); /* ATTRIBUTES (1 byte) */ network_read(&entry[i].ATTRIBUTES, 1, bytes); /* RESERVED (10 bytes) */ network_read(&entry[i].RESERVED, 2, bytes); /* CREATION_TIME (2 bytes) */ network_read(&entry[i].CREATION_TIME, 2, bytes); /* CREATION_DATE (2 bytes) */ network_read(&entry[i].CREATION_DATE, 2, bytes); /* LAST_ACCESS_DATE (2 bytes) */ network_read(&entry[i].LAST_ACCESS_DATE, 2, bytes); network_seek(2, SEEK_CUR_N); //skip 2 bytes /* MODIFY_TIME (2 bytes) */ network_read(&entry[i].MODIFY_TIME, 2, bytes); /* MODIFY_DATE (2 bytes) */ network_read(&entry[i].MODIFY_DATE, 2, bytes); /* START_CLUSTER (2 bytes) */ network_read(&entry[i].START_CLUSTER, 2, bytes); /* FILE_SIZE (4 bytes) */ network_read(&entry[i].FILE_SIZE, 4, bytes); } }
int main(void) { network_init(); // network.c // enc28j60Init(); // enc28j60PhyWrite(PHLCON,0x476); //CLKPR = (1<<CLKPCE); //Change prescaler //CLKPR = (1<<CLKPS0); //Use prescaler 2 //clock_prescale_set(clock_div_2); enc28j60Write(ECOCON, 1 & 0x7); // enc28j60.c //Get a 25MHz signal from enc28j60 #ifdef MY_DEBUG uartInit(); uartSetBaudRate(9600); rprintfInit(uartSendByte); #endif int i; uip_ipaddr_t ipaddr; // uip.h // typedef u16_t uip_ip4addr_t[2]; // typedef uip_ip4addr_t uip_ipaddr_t; struct timer periodic_timer, arp_timer; clock_init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); uip_init(); // uip.c //uip_arp_init(); // uip_arp.c // must be done or sometimes arp doesn't work struct uip_eth_addr mac = {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5}; uip_setethaddr(mac); simple_httpd_init(); #ifdef __DHCPC_H__ dhcpc_init(&mac, 6); #else uip_ipaddr(ipaddr, 192,168,0,1); // uip.h uip_sethostaddr(ipaddr); // uip.h // #define uip_sethostaddr(addr) uip_ipaddr_copy(uip_hostaddr, (addr)) uip_ipaddr(ipaddr, 192,168,0,1); uip_setdraddr(ipaddr); // #define uip_setdraddr(addr) uip_ipaddr_copy(uip_draddr, (addr)) uip_ipaddr(ipaddr, 255,255,255,0); uip_setnetmask(ipaddr); // #define uip_setnetmask(addr) uip_ipaddr_copy(uip_netmask, (addr)) #endif /*__DHCPC_H__*/ while(1){ uip_len = network_read(); // uip.c : u16_t uip_len; // network.c : return ((unt16_t) enc28j60PacketReceive(UIP_BUFSIZE, (uint8_t *)uip_buf)); // enc28j60.c : enc28j60PacketReceive // uip.c : uint8_t uip_buf[UIP_BUFSIZE+2]; // uipconf.h : UIP_BUFSIZE:300 if(uip_len > 0) { if(BUF->type == htons(UIP_ETHTYPE_IP)){ #ifdef MY_DEBUG //rprintf("eth in : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]); //debugPrintHexTable(uip_len, uip_buf); #endif // struct uip_eth_hdr { // struct uip_eth_addr dest; // struct uip_eth_addr src; // u16_t type; // }; // struct uip_eth_addr { // Representation of a 48-bit Ethernet address // u8_t addr[6]; // }; // http://www.netmanias.com/ko/post/blog/5372/ethernet-ip-tcp-ip/packet-header-ethernet-ip-tcp-ip // UIP_ETHTYPE_IP(0x0800) : IPv4 Packet(ICMP, TCP, UDP) uip_arp_ipin(); #ifdef MY_DEBUG //rprintf("ip in : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]); //debugPrintHexTable(uip_len, uip_buf+14); #endif // uip_arp.c // #define IPBUF ((struct ethip_hdr *)&uip_buf[0]) // struct ethip_hdr { // struct uip_eth_hdr ethhdr; // // IP header // u8_t vhl, // tos, // len[2], // ipid[2], // ipoffset[2], // ttl, // proto; // ICMP: 1, TCP: 6, UDP: 17 // u16_t ipchksum; // u16_t srcipaddr[2], // destipaddr[2]; // } // if ((IBUF->srcipaddr & uip_netmask) != uip_hostaddr & (uip_netmask)) return; // uip_arp_update(IPBUF->srcipaddr, &(IPBUF->ethhdr.src)); uip_input(); #ifdef MY_DEBUG //rprintf("ip out : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]); //debugPrintHexTable(uip_len, uip_buf+14); #endif // ip out packet // eg ICMP // uip_len : 84 // source ip <-> destination ip // type : 8 (Echo (ping) request) -> 0 (Echo (ping) reply) // uip.h // #define uip_input() uip_process(UIP_DATA) // UIP_DATA(1) : Tells uIP that there is incoming // uip_process : The actual uIP function which does all the work. if(uip_len > 0) { uip_arp_out(); #ifdef MY_DEBUG //rprintf("ip out : uip_len = %d, proto = %d\n", uip_len, uip_buf[23]); //debugPrintHexTable(uip_len, uip_buf); #endif // Destination MAC Address <=> Source MAC Address // w/o Ethernet CRC // uip_arp.c network_send(); // network.c // if(uip_len <= UIP_LLH_LEN + 40){ // UIP_LLH_LEN : 14 // enc28j60PacketSend(uip_len, (uint8_t *)uip_buf, 0, 0); // }else{ // enc28j60PacketSend(54, (uint8_t *)uip_buf , uip_len - UIP_LLH_LEN - 40, (uint8_t*)uip_appdata); // } } }else if(BUF->type == htons(UIP_ETHTYPE_ARP)){ // UIP_ETHYPE_ARP(0x0806) #ifdef MY_DEBUG //rprintf("arp in : uip_len = %d\n", uip_len); //debugPrintHexTable(uip_len, uip_buf); #endif // arp in : 64 bytes uip_arp_arpin(); if(uip_len > 0){ // always uip_len > 0 #ifdef MY_DEBUG //rprintf("ip in : uip_len = %d\n", uip_len); //debugPrintHexTable(uip_len, uip_buf); #endif // arp out : 42 bytes // 64 - Ethernet_padding(18) - Ethernet_CRC(4) // Send MAC address <--> Target MAC address // Send IP address <--> Target IP address // uip_arp.c network_send(); } } }else if(timer_expired(&periodic_timer)) { timer_reset(&periodic_timer); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); // uip.h // #define uip_udp_periodic(conn) do { uip_udp_conn = &uip_udp_conns[conn]; \ // uip_process(UIP_UDP_TIMER); } while (0) // UIP_UDP_TIMER : 5 // uip.c; uip_process // if(flag == UIP_UDP_TIMER) { // if(uip_udp_conn->lport != 0) { // uip_conn = NULL; // uip_sappdata = uip_appdata = &uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN]; // uip_len = uip_slen = 0; // uip_flags = UIP_POLL; // UIP_UDP_APPCALL(); // goto udp_send; // } // } else { // goto drop; // } if(uip_len > 0) { uip_arp_out(); network_send(); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } } return 0; }
void stack_process(chanend tx_mp3, chanend tcp_chan) { int i; uip_len = network_read(); if (uip_len > 0) { if (BUF->type == htons(UIP_ETHTYPE_IP)) { // printstrln("ETHTYPE_IP"); uip_arp_ipin(); uip_input(tx_mp3, tcp_chan); /* If the above function invocation resulted in data that should be sent out on the network, the global variable uip_len is set to a value > 0. */ if (uip_len > 0) { uip_arp_out(); network_send(); } } else if (BUF->type == htons(UIP_ETHTYPE_ARP)) { // printstrln("ETHTYPE_ARP"); uip_arp_arpin(); if (uip_len > 0) { network_send(); } } } else if (timer_expired(&periodic_timer)) { timer_reset(&periodic_timer); for (i = 0; i < UIP_CONNS; i++) { uip_periodic(i, tx_mp3, tcp_chan); if(uip_len > 0) { uip_arp_out(); network_send(); } } for (i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i, tx_mp3, tcp_chan); if (uip_len > 0) { uip_arp_out(); network_send(); } } // if nothing to TX and the self ARP timer expired // TX a broadcast ARP reply. This was implemented to // cause periodic TX to prevent the AP from disconnecting // us from the network if (uip_len == 0 && timer_expired(&self_arp_timer)) // 30s { timer_reset(&self_arp_timer); uip_self_arp_out(); network_send(); } /* Call the ARP timer function every 10 seconds. */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } }
int main(void) { sei(); uint8_t resetSource = MCUSR; MCUSR = 0; wdt_reset(); wdt_disable(); wdt_enable(WDTO_1S); WDTCSR |= (1 << WDIE); //enable watchdog interrupt wdt_reset(); cli(); clock_init(); usart_init(1000000, 9600); stdout = &uart_str; stderr = &uart_str; stdin = &uart_str; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; uint16_t timer_OW, timer_Simple, timer_Count, timer_EEProm, timer_SendData, timer_IOalarm, timer_network; timer_OW = 0; timer_Simple = 0; timer_Count = 0; timer_EEProm = 0; timer_SendData = 0; timer_IOalarm = 0; timer_network = 0; if(resetSource & (1<<WDRF)) { printf("Mega was reset by watchdog...\r\n"); } if(resetSource & (1<<BORF)) { printf("Mega was reset by brownout...\r\n"); } if(resetSource & (1<<EXTRF)) { printf("Mega was reset by external...\r\n"); } if(resetSource & (1<<PORF)) { printf("Mega was reset by power on...\r\n"); } //else jtag (disabled) //sensorScan = (uint8_t*) & tempbuf; if (eepromReadByte(0) == 255 || eepromReadByte(11) == 255) { printf_P(PSTR("Setting default values\r\n")); //Set defaults eepromWriteByte(0, 0); //init myip[0] = 192; myip[1] = 168; myip[2] = 1; myip[3] = 67; //47 in final versions netmask[0] = 255; netmask[1] = 255; netmask[2] = 255; netmask[3] = 0; gwip[0] = 192; gwip[1] = 168; gwip[2] = 1; gwip[3] = 1; dnsip[0] = 8; dnsip[1] = 8; dnsip[2] = 8; dnsip[3] = 8; eepromWriteByte(29, 80); //web port eepromWriteByte(10, 0); //dhcp off save_ip_addresses(); wdt_reset(); eepromWriteStr(200, "SBNG", 4); //default password eepromWriteByte(204, '\0'); eepromWriteByte(205, '\0'); eepromWriteByte(206, '\0'); eepromWriteByte(207, '\0'); eepromWriteByte(208, '\0'); eepromWriteByte(209, '\0'); eepromWriteByte(100, 1); //Analog port 0 = ADC eepromWriteByte(101, 1); //Analog port 1 = ADC eepromWriteByte(102, 1); //Analog port 2 = ADC eepromWriteByte(103, 1); //Analog port 3 = ADC eepromWriteByte(104, 1); //Analog port 4 = ADC eepromWriteByte(105, 1); //Analog port 5 = ADC eepromWriteByte(106, 1); //Analog port 6 = ADC eepromWriteByte(107, 1); //Analog port 7 = ADC eepromWriteByte(110, 0); //Digital port 0 = OUT eepromWriteByte(111, 0); //Digital port 1 = OUT eepromWriteByte(112, 0); //Digital port 2 = OUT eepromWriteByte(113, 0); //Digital port 3 = OUT wdt_reset(); for (uint8_t alarm=1; alarm<=4; alarm++) { uint16_t pos = 400 + ((alarm-1)*15); //400 415 430 445 eepromWriteByte(pos+0, 0); //enabled eepromWriteByte(pos+1, 0); //sensorid eepromWriteByte(pos+2, 0); //sensorid eepromWriteByte(pos+3, 0); //sensorid eepromWriteByte(pos+4, 0); //sensorid eepromWriteByte(pos+5, 0); //sensorid eepromWriteByte(pos+6, 0); //sensorid eepromWriteByte(pos+7, 0); //sensorid eepromWriteByte(pos+8, 0); //sensorid eepromWriteByte(pos+9, '<'); //type eepromWriteByte(pos+10, 0); //value eepromWriteByte(pos+11, 0); //target eepromWriteByte(pos+12, 0); //state eepromWriteByte(pos+13, 0); //reverse eepromWriteByte(pos+14, 0); //not-used } eepromWriteByte(1, EEPROM_VERSION); } /* findSystemID(systemID); if (systemID[0] == 0) { printf_P(PSTR("No system id found, add a DS2401 or use old software")); // fprintf(&lcd_str, "?f?y0?x00No system id found?nAdd a DS2401 or use old software?n"); wdt_disable(); wdt_reset(); while (true); } else { */ //MAC will be 56 51 99 36 14 00 with example system id mymac[1] = systemID[1]; mymac[2] = systemID[2]; mymac[3] = systemID[3]; mymac[4] = systemID[4]; mymac[5] = systemID[5]; // } // fprintf(&lcd_str, "?y1?x00ID: %02X%02X%02X%02X%02X%02X%02X%02X?n", systemID[0], systemID[1], systemID[2], systemID[3], systemID[4], systemID[5], systemID[6], systemID[7]); loadSimpleSensorData(); //Set digital pins based on selections... for (uint8_t i=8; i<=11; i++) { if (simpleSensorTypes[i] == 0) { //output SETBIT(DDRC, (i-6)); } else { //input CLEARBIT(DDRC, (i-6)); } } network_init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); uip_init(); //sættes hvert for sig for uip og enc, skal rettes til en samlet setting, så vi kan bruge mymac struct uip_eth_addr mac = { {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5} }; // struct uip_eth_addr mac = {mymac[0], mymac[1], mymac[2], mymac[3], mymac[4], mymac[5]}; uip_setethaddr(mac); httpd_init(); /* #ifdef __DHCPC_H__ dhcpc_init(&mac, 6); #else */ uip_ipaddr(ipaddr, myip[0], myip[1], myip[2], myip[3]); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, gwip[0], gwip[1], gwip[2], gwip[3]); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, netmask[0], netmask[1], netmask[2], netmask[3]); uip_setnetmask(ipaddr); //#endif /*__DHCPC_H__*/ printf("Setting ip to %u.%u.%u.%u \r\n", myip[0], myip[1], myip[2], myip[3]); resolv_init(); uip_ipaddr(ipaddr, dnsip[0], dnsip[1], dnsip[2], dnsip[3]); resolv_conf(ipaddr); webclient_init(); printf("Stokerbot NG R3 ready - Firmware %u.%u ...\r\n", SBNG_VERSION_MAJOR, SBNG_VERSION_MINOR); // fprintf(&lcd_str, "?y2?x00Firmware %u.%u ready.", SBNG_VERSION_MAJOR, SBNG_VERSION_MINOR); // SPILCD_init(); wdt_reset(); while (1) { //Only one event may fire per loop, listed in order of importance //If an event is skipped, it will be run next loop if (tickDiff(timer_Count) >= 1) { timer_Count = tick; wdt_reset(); //sikre at watchdog resetter os hvis timer systemet fejler, vi når her hvert 2ms updateCounters(); //bør ske i en interrupt istedet, for at garentere 2ms aflæsning } else if (tickDiffS(timer_IOalarm) >= 5) { printf("Timer : IO alarm \r\n"); timer_IOalarm = tickS; timedAlarmCheck(); } else if (tickDiffS(timer_OW) >= 2) { printf("Timer : OW \r\n"); timer_OW = tickS; updateOWSensors(); } else if (tickDiffS(timer_Simple) >= 5) { printf("Timer : Simple\r\n"); timer_Simple = tickS; updateSimpleSensors(); } else if (tickDiffS(timer_SendData) >= 59) { printf("Timer : webclient \r\n"); timer_SendData = tickS; webclient_connect(); } else if (tickDiffS(timer_EEProm) >= 60 * 30) { printf("Timer : eeprom \r\n"); timer_EEProm = tickS; timedSaveEeprom(); } //Net handling below if (tickDiff(timer_network) >= 1) { timer_network = tick; uip_len = network_read(); if (uip_len > 0) { if (BUF->type == htons(UIP_ETHTYPE_IP)) { uip_arp_ipin(); uip_input(); if (uip_len > 0) { uip_arp_out(); network_send(); } } else if (BUF->type == htons(UIP_ETHTYPE_ARP)) { uip_arp_arpin(); if (uip_len > 0) { network_send(); } } } else if (timer_expired(&periodic_timer)) { timer_reset(&periodic_timer); //FLIPBIT(PORTC,5); // printf("Timers : %u %u \r\n", tick, tickS); for (uint8_t i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if (uip_len > 0) { uip_arp_out(); network_send(); } } #if UIP_UDP for (uint8_t i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if (uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if (timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } } } return 0; }
/******************************************************************************** * TitleBrowser- opens a browser with a list of installed Titles * relies on code from any title deleter. *********************************************************************************/ int TitleBrowser(u32 type) { u32 num_titles; u32 titles[100] ATTRIBUTE_ALIGN(32); u32 num_sys_titles; u32 sys_titles[10] ATTRIBUTE_ALIGN(32); s32 ret = -1; int numtitle;//to get rid of a stupid compile wrning //open the database file FILE *f; char path[100]; ISFS_Initialize(); sprintf(path,"%s/config/database.txt",bootDevice); f = fopen(path, "r"); // Get count of titles of our requested type ret = getTitles_TypeCount(type, &num_titles); if (ret < 0) { //printf("\tError! Can't get count of titles! (ret = %d)\n", ret); //exit(1); } // Get titles of our requested type ret = getTitles_Type(type, titles, num_titles); if (ret < 0) { //printf("\tError! Can't get list of titles! (ret = %d)\n", ret); //exit(1); } // Get count of system titles ret = getTitles_TypeCount(0x00010002, &num_sys_titles); if (ret < 0) { //printf("\tError! Can't get count of titles! (ret = %d)\n", ret); //exit(1); } // Get system titles ret = getTitles_Type(0x00010002, sys_titles, num_sys_titles); if (ret < 0) { //printf("\tError! Can't get list of titles! (ret = %d)\n", ret); //exit(1); } //this array will hold all the names for the titles so we only have to get them one time char name[num_titles+num_sys_titles][50]; customOptionList options3(num_titles+num_sys_titles+1); //write the titles on the option browser u32 i = 0; //first add the good stuff while (i < num_titles) { //start from the beginning of the file each loop if (f)rewind(f); //char name[50]; char text[15]; strcpy(name[i],"");//make sure name is empty u8 found=0; //set the title's name, number, ID to text sprintf(text, "%s", titleText(type, titles[i])); //get name from database cause i dont like the ADT function char line[200]; char tmp[50]; snprintf(tmp,50," "); //check if the content.bin is on the SD card for that game //if there is content.bin,then the game is on the SDmenu and not the wii sprintf(line,"SD:/private/wii/title/%s/content.bin",text); if (!checkfile(line)) { if (f) { while (fgets(line, sizeof(line), f)) { if (line[0]== text[0]&& line[1]== text[1]&& line[2]== text[2]) { int j=0; found=1; for (j=0;(line[j+4]!='\0' || j<51);j++) tmp[j]=line[j+4]; snprintf(name[i],sizeof(name[i]),"%s",tmp); //break; } } } if (!found) { if (getName00(name[i], TITLE_ID(type, titles[i]),CONF_GetLanguage()*2)>=0) found=2; if (!found) { if (getNameBN(name[i], TITLE_ID(type, titles[i]))>=0) found=3; if (!found) snprintf(name[i],sizeof(name[i]),"Unknown Title (%08x)",titles[i]); } } //set the text to the option browser options3.SetName(i, "%s",text); options3.SetValue(i, "%s",name[i]); //options3.SetValue(i, " (%08x) %s",titles[i],name[i]);//use this line to show the number to call to launch the channel //move on to the next title } i++; } // now add the crappy system titles while (i < num_titles+num_sys_titles) { //start from the beginning of the file each loop if (f)rewind(f); //char name[50]; char text[15]; strcpy(name[i],"");//make sure name is empty u8 found=0; //set the title's name, number, ID to text sprintf(text, "%s", titleText(0x00010002, sys_titles[i-num_titles])); //get name from database cause i dont like the ADT function char line[200]; char tmp[50]; snprintf(tmp,50," "); //snprintf(name[i],sizeof(name[i]),"Unknown Title"); if (f) { while (fgets(line, sizeof(line), f)) { if (line[0]== text[0]&& line[1]== text[1]&& line[2]== text[2]) { int j=0; found=1; for (j=0;(line[j+4]!='\0' || j<51);j++) tmp[j]=line[j+4]; snprintf(name[i],sizeof(name[i]),"%s",tmp); break; } } } if (!found) { if (getName00(name[i], TITLE_ID(0x00010002, sys_titles[i-num_titles]))>=0) found=2; if (!found) { if (getNameBN(name[i], TITLE_ID(0x00010002, sys_titles[i-num_titles]))>=0) found=3; if (!found) snprintf(name[i],sizeof(name[i]),"Unknown Title (%08x)",sys_titles[i-num_titles]); } } //set the text to the option browser options3.SetName(i, "%s",text); options3.SetValue(i, "%s",name[i]); //options3.SetValue(i, " (%08x) %s",titles[i],name[i]);//use this line to show the number to call to launch the channel //move on to the next title i++; } if (i == num_titles+num_sys_titles) { options3.SetName(i, " "); options3.SetValue(i, "%s",tr("Wii Settings")); } //we have all the titles we need so close the database and stop poking around in the wii fclose(f); //get rid of our footprints in there Uninstall_FromTitle(TITLE_ID(1, 0)); ISFS_Deinitialize(); bool exit = false; if (IsNetworkInit()) ResumeNetworkWait(); GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, Settings.sfxvolume); // because destroy GuiSound must wait while sound playing is finished, we use a global sound if(!btnClick2) btnClick2=new GuiSound(button_click2_pcm, button_click2_pcm_size, Settings.sfxvolume); // GuiSound btnClick(button_click2_pcm, button_click2_pcm_size, Settings.sfxvolume); char imgPath[100]; snprintf(imgPath, sizeof(imgPath), "%sbutton_dialogue_box.png", CFG.theme_path); GuiImageData btnOutline(imgPath, button_dialogue_box_png); snprintf(imgPath, sizeof(imgPath), "%sgamesettings_background.png", CFG.theme_path); GuiImageData settingsbg(imgPath, settings_background_png); GuiTrigger trigA; trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A); GuiTrigger trigHome; trigHome.SetButtonOnlyTrigger(-1, WPAD_BUTTON_HOME | WPAD_CLASSIC_BUTTON_HOME, 0); GuiTrigger trigB; trigB.SetButtonOnlyTrigger(-1, WPAD_BUTTON_B | WPAD_CLASSIC_BUTTON_B, PAD_BUTTON_B); GuiText titleTxt(tr("Title Launcher"), 28, (GXColor) {0, 0, 0, 255}); titleTxt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP); titleTxt.SetPosition(12,40); titleTxt.SetMaxWidth(356, GuiText::SCROLL); GuiImage settingsbackground(&settingsbg); GuiButton settingsbackgroundbtn(settingsbackground.GetWidth(), settingsbackground.GetHeight()); settingsbackgroundbtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP); settingsbackgroundbtn.SetPosition(0, 0); settingsbackgroundbtn.SetImage(&settingsbackground); GuiText cancelBtnTxt(tr("Back"), 22, THEME.prompttext); cancelBtnTxt.SetMaxWidth(btnOutline.GetWidth()-30); GuiImage cancelBtnImg(&btnOutline); if (Settings.wsprompt == yes) { cancelBtnTxt.SetWidescreen(CFG.widescreen); cancelBtnImg.SetWidescreen(CFG.widescreen); } GuiButton cancelBtn(&cancelBtnImg,&cancelBtnImg, 2, 3, 180, 400, &trigA, &btnSoundOver, btnClick2,1); cancelBtn.SetScale(0.9); cancelBtn.SetLabel(&cancelBtnTxt); cancelBtn.SetTrigger(&trigB); u8 scrollbaron = 0; if (num_titles > 9) scrollbaron = 1; GuiCustomOptionBrowser optionBrowser3(396, 280, &options3, CFG.theme_path, "bg_options_gamesettings.png", bg_options_settings_png, num_titles+num_sys_titles>9?1:0, 200); optionBrowser3.SetPosition(0, 90); optionBrowser3.SetAlignment(ALIGN_CENTRE, ALIGN_TOP); snprintf(imgPath, sizeof(imgPath), "%sWifi_btn.png", CFG.theme_path); GuiImageData wifiImgData(imgPath, Wifi_btn_png); GuiImage wifiImg(&wifiImgData); if (Settings.wsprompt == yes) { wifiImg.SetWidescreen(CFG.widescreen); } GuiButton wifiBtn(wifiImg.GetWidth(), wifiImg.GetHeight()); wifiBtn.SetImage(&wifiImg); wifiBtn.SetPosition(100, 400); wifiBtn.SetEffectGrow(); wifiBtn.SetAlpha(80); wifiBtn.SetTrigger(&trigA); GuiTrigger trigZ; trigZ.SetButtonOnlyTrigger(-1, WPAD_NUNCHUK_BUTTON_Z | WPAD_CLASSIC_BUTTON_ZL, PAD_TRIGGER_Z); GuiButton screenShotBtn(0,0); screenShotBtn.SetPosition(0,0); screenShotBtn.SetTrigger(&trigZ); HaltGui(); GuiWindow w(screenwidth, screenheight); w.Append(&screenShotBtn); w.Append(&settingsbackgroundbtn); w.Append(&titleTxt); w.Append(&cancelBtn); w.Append(&wifiBtn); w.Append(&optionBrowser3); mainWindow->Append(&w); int tmp=num_titles+num_sys_titles; ResumeGui(); numtitle=num_titles; while (!exit) { VIDEO_WaitVSync(); if (shutdown == 1) Sys_Shutdown(); if (reset == 1) Sys_Reboot(); else if (wifiBtn.GetState() == STATE_CLICKED) { ResumeNetworkWait(); wifiBtn.ResetState(); } if (IsNetworkInit()) { wifiBtn.SetAlpha(255); } ret = optionBrowser3.GetClickedOption(); if (ret > -1) {//if a click happened //char name[50]; char text[15]; if (f)rewind(f); //strcpy(name,"");//make sure name is empty if (ret<numtitle) { //set the title's name, number, ID to text sprintf(text, "%s", titleText(type, titles[ret])); char temp[100]; //prompt to boot selected title snprintf(temp, sizeof(temp), "%s : %s",text,name[ret]); int choice = WindowPrompt(tr("Boot?"), temp, tr("OK"), tr("Cancel")); if (choice) {//if they say yes //stop all this stuff before starting the channel CloseXMLDatabase(); ExitGUIThreads(); ShutdownAudio(); StopGX(); WII_Initialize(); WII_LaunchTitle(TITLE_ID(type,titles[ret])); //this really shouldn't be needed because the title will be booted exit = true; break; } else { //if they said no to booting the title ret = -1; optionBrowser3.ResetState(); } } else { //if they clicked a system title if (ret == tmp) { CloseXMLDatabase(); ExitGUIThreads(); ShutdownAudio(); StopGX(); WII_Initialize(); WII_ReturnToSettings(); } else { //set the title's name, number, ID to text sprintf(text, "%s", titleText(0x00010002, sys_titles[ret-num_titles])); char temp[112]; //prompt to boot selected title snprintf(temp, sizeof(temp), tr("%s : %s May not boot correctly if your System Menu is not up to date."),text,name[ret]); int choice = WindowPrompt(tr("Boot?"), temp, tr("OK"), tr("Cancel")); if (choice) {//if they say yes //stop all this stuff before starting the channel CloseXMLDatabase(); ExitGUIThreads(); ShutdownAudio(); StopGX(); WII_Initialize(); WII_LaunchTitle(TITLE_ID(0x00010002,sys_titles[ret-num_titles])); //this really shouldn't be needed because the title will be booted exit = true; break; } else { //if they said no to booting the title ret = -1; optionBrowser3.ResetState(); } } } } if(infilesize > 0) { char filesizetxt[50]; char temp[50]; char filepath[100]; // u32 read = 0; //make sure there is a folder for this to be saved in struct stat st; snprintf(filepath, sizeof(filepath), "%s/wad/", bootDevice); if (stat(filepath, &st) != 0) { if (subfoldercreate(filepath) != 1) { WindowPrompt(tr("Error !"),tr("Can't create directory"),tr("OK")); } } snprintf(filepath, sizeof(filepath), "%s/wad/tmp.tmp", bootDevice); if (infilesize < MB_SIZE) snprintf(filesizetxt, sizeof(filesizetxt), tr("Incoming file %0.2fKB"), infilesize/KB_SIZE); else snprintf(filesizetxt, sizeof(filesizetxt), tr("Incoming file %0.2fMB"), infilesize/MB_SIZE); snprintf(temp, sizeof(temp), tr("Load file from: %s ?"), GetIncommingIP()); int choice = WindowPrompt(filesizetxt, temp, tr("OK"), tr("Cancel")); gprintf("\nchoice:%d",choice); if (choice == 1) { u32 read = 0; u8 *temp = NULL; int len = NETWORKBLOCKSIZE; temp = (u8 *) malloc(infilesize); bool error = false; u8 *ptr = temp; gprintf("\nrecieving shit"); while (read < infilesize) { ShowProgress(tr("Receiving file from:"), GetIncommingIP(), NULL, read, infilesize, true); if (infilesize - read < (u32) len) len = infilesize-read; else len = NETWORKBLOCKSIZE; int result = network_read(ptr, len); if (result < 0) { WindowPrompt(tr("Error while transfering data."), 0, tr("OK")); error = true; break; } if (!result) { gprintf("\n!RESULT"); break; } ptr += result; read += result; } ProgressStop(); char filename[101]; char tmptxt[200]; //bool installWad=0; if (!error) { gprintf("\nno error yet"); network_read((u8*) &filename, 100); gprintf("\nfilename: %s",filename); // Do we need to unzip this thing? if (wiiloadVersion[0] > 0 || wiiloadVersion[1] > 4) { gprintf("\nusing newer wiiload version"); if (uncfilesize != 0) { // if uncfilesize == 0, it's not compressed gprintf("\ntrying to uncompress"); // It's compressed, uncompress u8 *unc = (u8 *) malloc(uncfilesize); uLongf f = uncfilesize; error = uncompress(unc, &f, temp, infilesize) != Z_OK; uncfilesize = f; free(temp); temp = unc; } } if (!error) { sprintf(tmptxt,"%s",filename); //if we got a wad if (strcasestr(tmptxt,".wad")) { FILE *file = fopen(filepath, "wb"); fwrite(temp, 1, (uncfilesize>0?uncfilesize:infilesize), file); fclose(file); sprintf(tmptxt,"%s/wad/%s",bootDevice,filename); if (checkfile(tmptxt))remove(tmptxt); rename(filepath, tmptxt); //check and make sure the wad we just saved is the correct size u32 lSize; file = fopen(tmptxt, "rb"); // obtain file size: fseek (file , 0 , SEEK_END); lSize = ftell (file); rewind (file); if (lSize==(uncfilesize>0?uncfilesize:infilesize)) { gprintf("\nsize is ok"); int pick = WindowPrompt(tr(" Wad Saved as:"), tmptxt, tr("Install"),tr("Uninstall"),tr("Cancel")); //install or uninstall it if (pick==1) { HaltGui(); w.Remove(&titleTxt); w.Remove(&cancelBtn); w.Remove(&wifiBtn); w.Remove(&optionBrowser3); ResumeGui(); Wad_Install(file); HaltGui(); w.Append(&titleTxt); w.Append(&cancelBtn); w.Append(&wifiBtn); w.Append(&optionBrowser3); ResumeGui(); } if (pick==2)Wad_Uninstall(file); } else gprintf("\nBad size"); //close that beast, we're done with it fclose (file); //do we want to keep the file in the wad folder if (WindowPrompt(tr("Delete ?"), tmptxt, tr("Delete"),tr("Keep"))!=0) remove(tmptxt); } else { WindowPrompt(tr("ERROR:"), tr("Not a WAD file."), tr("OK")); } } } if (error || read != infilesize) { WindowPrompt(tr("Error:"), tr("No data could be read."), tr("OK")); } if(temp)free(temp); } CloseConnection(); ResumeNetworkWait(); } if (cancelBtn.GetState() == STATE_CLICKED) { //break the loop and end the function exit = true; ret = -10; } else if (screenShotBtn.GetState() == STATE_CLICKED) { gprintf("\n\tscreenShotBtn clicked"); screenShotBtn.ResetState(); ScreenShot(); gprintf("...It's easy, mmmmmmKay"); } } CloseConnection(); if (IsNetworkInit()) HaltNetworkThread(); fclose(f); HaltGui(); mainWindow->Remove(&w); ResumeGui(); return ret; }
/**************************************************************************** * Download a file from a given url with a Progressbar ****************************************************************************/ int DownloadFileToMem(const char *url, u8 **inbuffer, u32 *size) { if(strncasecmp(url, "http://", strlen("http://")) != 0) { ShowError(tr("Not a valid URL")); return -1; } char *path = strchr(url + strlen("http://"), '/'); if(!path) { ShowError(tr("Not a valid URL path")); return -2; } int domainlength = path - url - strlen("http://"); if(domainlength == 0) { ShowError(tr("Not a valid domain")); return -3; } char domain[domainlength + 1]; strncpy(domain, url + strlen("http://"), domainlength); domain[domainlength] = '\0'; int connection = GetConnection(domain); if(connection < 0) { ShowError(tr("Could not connect to the server.")); return -4; } char header[1024]; char * ptr = header; ptr += sprintf(ptr, "GET %s HTTP/1.1\r\n", path); ptr += sprintf(ptr, "Host: %s\r\n", domain); ptr += sprintf(ptr, "Referer: %s\r\n", domain); ptr += sprintf(ptr, "User-Agent: USB Loader GX\r\n"); ptr += sprintf(ptr, "Pragma: no-cache\r\n"); ptr += sprintf(ptr, "Cache-Control: no-cache\r\n"); ptr += sprintf(ptr, "Connection: close\r\n\r\n"); char filename[255]; memset(filename, 0, sizeof(filename)); int filesize = network_request(connection, header, filename); if(filesize <= 0) { net_close(connection); ShowError(tr("Filesize is 0 Byte.")); return -5; } int blocksize = 4*1024; u8 * buffer = (u8 *) malloc(filesize); if(!buffer) { net_close(connection); ShowError(tr("Not enough memory.")); return -6; } ProgressCancelEnable(true); StartProgress(tr("Downloading file..."), 0, filename, true, true); int done = 0; while(done < filesize) { if(ProgressCanceled()) { done = PROGRESS_CANCELED; break; } ShowProgress(done, filesize); if(blocksize > filesize - done) blocksize = filesize - done; s32 read = network_read(connection, buffer+done, blocksize); if(read < 0) { done = -8; ShowError(tr("Transfer failed")); break; } else if(!read) break; done += read; } ProgressStop(); ProgressCancelEnable(false); net_close(connection); if(done < 0) { free(buffer); return done; } *inbuffer = buffer; *size = filesize; return done; }
/**************************************************************************** * MenuHomebrewBrowse ***************************************************************************/ int MenuHomebrewBrowse() { int menu = MENU_NONE; int choice = 0; HomebrewFiles HomebrewFiles(Settings.homebrewapps_path); u32 filecount = HomebrewFiles.GetFilecount(); if (!filecount) { WindowPrompt(tr("No .dol or .elf files found."),0, tr("OK")); return MENU_DISCLIST; } enum { FADE, LEFT, RIGHT }; if (IsNetworkInit()) ResumeNetworkWait(); int slidedirection = FADE; /*** Sound Variables ***/ GuiSound btnSoundOver(button_over_pcm, button_over_pcm_size, Settings.sfxvolume); // because destroy GuiSound must wait while sound playing is finished, we use a global sound if(!btnClick2) btnClick2=new GuiSound(button_click2_pcm, button_click2_pcm_size, Settings.sfxvolume); // GuiSound btnClick(button_click2_pcm, button_click2_pcm_size, Settings.sfxvolume); GuiSound btnClick1(button_click_pcm, button_click_pcm_size, Settings.sfxvolume); /*** Image Variables ***/ char imgPath[150]; snprintf(imgPath, sizeof(imgPath), "%sbutton_dialogue_box.png", CFG.theme_path); GuiImageData btnOutline(imgPath, button_dialogue_box_png); snprintf(imgPath, sizeof(imgPath), "%ssettings_background.png", CFG.theme_path); GuiImageData bgData(imgPath, settings_background_png); snprintf(imgPath, sizeof(imgPath), "%ssettings_title.png", CFG.theme_path); GuiImageData MainButtonImgData(imgPath, settings_title_png); snprintf(imgPath, sizeof(imgPath), "%ssettings_title_over.png", CFG.theme_path); GuiImageData MainButtonImgOverData(imgPath, settings_title_over_png); snprintf(imgPath, sizeof(imgPath), "%sstartgame_arrow_left.png", CFG.theme_path); GuiImageData arrow_left(imgPath, startgame_arrow_left_png); snprintf(imgPath, sizeof(imgPath), "%sstartgame_arrow_right.png", CFG.theme_path); GuiImageData arrow_right(imgPath, startgame_arrow_right_png); snprintf(imgPath, sizeof(imgPath), "%sWifi_btn.png", CFG.theme_path); GuiImageData wifiImgData(imgPath, Wifi_btn_png); snprintf(imgPath, sizeof(imgPath), "%sChannel_btn.png", CFG.theme_path); GuiImageData channelImgData(imgPath, Channel_btn_png); GuiImage background(&bgData); /*** Trigger Variables ***/ GuiTrigger trigA; trigA.SetSimpleTrigger(-1, WPAD_BUTTON_A | WPAD_CLASSIC_BUTTON_A, PAD_BUTTON_A); GuiTrigger trigHome; trigHome.SetButtonOnlyTrigger(-1, WPAD_BUTTON_HOME | WPAD_CLASSIC_BUTTON_HOME, 0); GuiTrigger trigB; trigB.SetButtonOnlyTrigger(-1, WPAD_BUTTON_B | WPAD_CLASSIC_BUTTON_B, PAD_BUTTON_B); GuiTrigger trigL; trigL.SetButtonOnlyTrigger(-1, WPAD_BUTTON_LEFT | WPAD_CLASSIC_BUTTON_LEFT, PAD_BUTTON_LEFT); GuiTrigger trigR; trigR.SetButtonOnlyTrigger(-1, WPAD_BUTTON_RIGHT | WPAD_CLASSIC_BUTTON_RIGHT, PAD_BUTTON_RIGHT); GuiTrigger trigMinus; trigMinus.SetButtonOnlyTrigger(-1, WPAD_BUTTON_MINUS | WPAD_CLASSIC_BUTTON_MINUS, 0); GuiTrigger trigPlus; trigPlus.SetButtonOnlyTrigger(-1, WPAD_BUTTON_PLUS | WPAD_CLASSIC_BUTTON_PLUS, 0); GuiText titleTxt(tr("Homebrew Launcher"), 28, (GXColor) {0, 0, 0, 255}); titleTxt.SetAlignment(ALIGN_CENTRE, ALIGN_TOP); titleTxt.SetPosition(0,40); GuiImageData *IconData[4]; GuiImage *IconImg[4]; for (int i = 0; i < 4; i++) { IconData[i] = NULL; IconImg[i] = NULL; } /*** Buttons ***/ GuiText backBtnTxt(tr("Back") , 22, THEME.prompttext); backBtnTxt.SetMaxWidth(btnOutline.GetWidth()-30); GuiImage backBtnImg(&btnOutline); if (Settings.wsprompt == yes) { backBtnTxt.SetWidescreen(CFG.widescreen); backBtnImg.SetWidescreen(CFG.widescreen); } GuiButton backBtn(&backBtnImg,&backBtnImg, 2, 3, -180, 400, &trigA, &btnSoundOver, btnClick2,1); backBtn.SetLabel(&backBtnTxt); backBtn.SetTrigger(&trigB); GuiButton h**o(1,1); h**o.SetTrigger(&trigHome); GuiImage GoLeftImg(&arrow_left); GuiButton GoLeftBtn(GoLeftImg.GetWidth(), GoLeftImg.GetHeight()); GoLeftBtn.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); GoLeftBtn.SetPosition(25, -25); GoLeftBtn.SetImage(&GoLeftImg); GoLeftBtn.SetSoundOver(&btnSoundOver); GoLeftBtn.SetSoundClick(btnClick2); GoLeftBtn.SetEffectGrow(); GoLeftBtn.SetTrigger(&trigA); GoLeftBtn.SetTrigger(&trigL); GoLeftBtn.SetTrigger(&trigMinus); GuiImage GoRightImg(&arrow_right); GuiButton GoRightBtn(GoRightImg.GetWidth(), GoRightImg.GetHeight()); GoRightBtn.SetAlignment(ALIGN_RIGHT, ALIGN_MIDDLE); GoRightBtn.SetPosition(-25, -25); GoRightBtn.SetImage(&GoRightImg); GoRightBtn.SetSoundOver(&btnSoundOver); GoRightBtn.SetSoundClick(btnClick2); GoRightBtn.SetEffectGrow(); GoRightBtn.SetTrigger(&trigA); GoRightBtn.SetTrigger(&trigR); GoRightBtn.SetTrigger(&trigPlus); char MainButtonText[50]; snprintf(MainButtonText, sizeof(MainButtonText), "%s", " "); GuiImage MainButton1Img(&MainButtonImgData); GuiImage MainButton1ImgOver(&MainButtonImgOverData); GuiText MainButton1Txt(MainButtonText, 18, (GXColor) {0, 0, 0, 255}); MainButton1Txt.SetMaxWidth(MainButton1Img.GetWidth()-150, GuiText::DOTTED); MainButton1Txt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton1Txt.SetPosition(148, -12); GuiText MainButton1DescTxt(MainButtonText, 18, (GXColor) {0, 0, 0, 255}); MainButton1DescTxt.SetMaxWidth(MainButton1Img.GetWidth()-150, GuiText::DOTTED); MainButton1DescTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton1DescTxt.SetPosition(148, 15); GuiText MainButton1DescOverTxt(MainButtonText, 18, (GXColor) { 0, 0, 0, 255}); MainButton1DescOverTxt.SetMaxWidth(MainButton1Img.GetWidth()-150, GuiText::SCROLL); MainButton1DescOverTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton1DescOverTxt.SetPosition(148, 15); GuiButton MainButton1(MainButton1Img.GetWidth(), MainButton1Img.GetHeight()); MainButton1.SetAlignment(ALIGN_CENTRE, ALIGN_TOP); MainButton1.SetPosition(0, 90); MainButton1.SetImage(&MainButton1Img); MainButton1.SetImageOver(&MainButton1ImgOver); MainButton1.SetLabel(&MainButton1Txt); MainButton1.SetLabel(&MainButton1DescTxt,1); MainButton1.SetLabelOver(&MainButton1DescOverTxt,1); MainButton1.SetSoundOver(&btnSoundOver); MainButton1.SetSoundClick(&btnClick1); MainButton1.SetEffectGrow(); MainButton1.SetTrigger(&trigA); GuiImage MainButton2Img(&MainButtonImgData); GuiImage MainButton2ImgOver(&MainButtonImgOverData); GuiText MainButton2Txt(MainButtonText, 18, (GXColor) {0, 0, 0, 255 }); MainButton2Txt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton2Txt.SetPosition(148, -12); MainButton2Txt.SetMaxWidth(MainButton2Img.GetWidth()-150, GuiText::DOTTED); GuiText MainButton2DescTxt(MainButtonText, 18, (GXColor) { 0, 0, 0, 255}); MainButton2DescTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton2DescTxt.SetPosition(148, 15); MainButton2DescTxt.SetMaxWidth(MainButton2Img.GetWidth()-150, GuiText::DOTTED); GuiText MainButton2DescOverTxt(MainButtonText, 18, (GXColor) {0, 0, 0, 255}); MainButton2DescOverTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton2DescOverTxt.SetPosition(148, 15); MainButton2DescOverTxt.SetMaxWidth(MainButton2Img.GetWidth()-150, GuiText::SCROLL); GuiButton MainButton2(MainButton2Img.GetWidth(), MainButton2Img.GetHeight()); MainButton2.SetAlignment(ALIGN_CENTRE, ALIGN_TOP); MainButton2.SetPosition(0, 160); MainButton2.SetImage(&MainButton2Img); MainButton2.SetImageOver(&MainButton2ImgOver); MainButton2.SetLabel(&MainButton2Txt); MainButton2.SetLabel(&MainButton2DescTxt,1); MainButton2.SetLabelOver(&MainButton2DescOverTxt,1); MainButton2.SetSoundOver(&btnSoundOver); MainButton2.SetSoundClick(&btnClick1); MainButton2.SetEffectGrow(); MainButton2.SetTrigger(&trigA); GuiImage MainButton3Img(&MainButtonImgData); GuiImage MainButton3ImgOver(&MainButtonImgOverData); GuiText MainButton3Txt(MainButtonText, 18, (GXColor) {0, 0, 0, 255}); MainButton3Txt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton3Txt.SetPosition(148, -12); MainButton3Txt.SetMaxWidth(MainButton3Img.GetWidth()-150, GuiText::DOTTED); GuiText MainButton3DescTxt(MainButtonText, 18, (GXColor) { 0, 0, 0, 255}); MainButton3DescTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton3DescTxt.SetPosition(148, 15); MainButton3DescTxt.SetMaxWidth(MainButton3Img.GetWidth()-150, GuiText::DOTTED); GuiText MainButton3DescOverTxt(MainButtonText, 18, (GXColor) {0, 0, 0, 255 }); MainButton3DescOverTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton3DescOverTxt.SetPosition(148, 15); MainButton3DescOverTxt.SetMaxWidth(MainButton3Img.GetWidth()-150, GuiText::SCROLL); GuiButton MainButton3(MainButton3Img.GetWidth(), MainButton3Img.GetHeight()); MainButton3.SetAlignment(ALIGN_CENTRE, ALIGN_TOP); MainButton3.SetPosition(0, 230); MainButton3.SetImage(&MainButton3Img); MainButton3.SetImageOver(&MainButton3ImgOver); MainButton3.SetLabel(&MainButton3Txt); MainButton3.SetLabel(&MainButton3DescTxt,1); MainButton3.SetLabelOver(&MainButton3DescOverTxt,1); MainButton3.SetSoundOver(&btnSoundOver); MainButton3.SetSoundClick(&btnClick1); MainButton3.SetEffectGrow(); MainButton3.SetTrigger(&trigA); GuiImage MainButton4Img(&MainButtonImgData); GuiImage MainButton4ImgOver(&MainButtonImgOverData); GuiText MainButton4Txt(MainButtonText, 18, (GXColor) {0, 0, 0, 255} ); MainButton4Txt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton4Txt.SetPosition(148, -12); MainButton4Txt.SetMaxWidth(MainButton4Img.GetWidth()-150, GuiText::DOTTED); GuiText MainButton4DescTxt(MainButtonText, 18, (GXColor) {0, 0, 0, 255}); MainButton4DescTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton4DescTxt.SetPosition(148, 15); MainButton4DescTxt.SetMaxWidth(MainButton4Img.GetWidth()-150, GuiText::DOTTED); GuiText MainButton4DescOverTxt(MainButtonText, 18, (GXColor) { 0, 0, 0, 255}); MainButton4DescOverTxt.SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); MainButton4DescOverTxt.SetPosition(148, 15); MainButton4DescOverTxt.SetMaxWidth(MainButton4Img.GetWidth()-150, GuiText::SCROLL); GuiButton MainButton4(MainButton4Img.GetWidth(), MainButton4Img.GetHeight()); MainButton4.SetAlignment(ALIGN_CENTRE, ALIGN_TOP); MainButton4.SetPosition(0, 300); MainButton4.SetImage(&MainButton4Img); MainButton4.SetImageOver(&MainButton4ImgOver); MainButton4.SetLabel(&MainButton4Txt); MainButton4.SetLabel(&MainButton4DescTxt,1); MainButton4.SetLabelOver(&MainButton4DescOverTxt,1); MainButton4.SetSoundOver(&btnSoundOver); MainButton4.SetSoundClick(&btnClick1); MainButton4.SetEffectGrow(); MainButton4.SetTrigger(&trigA); GuiImage wifiImg(&wifiImgData); if (Settings.wsprompt == yes) { wifiImg.SetWidescreen(CFG.widescreen); } GuiButton wifiBtn(wifiImg.GetWidth(), wifiImg.GetHeight()); wifiBtn.SetImage(&wifiImg); wifiBtn.SetPosition(500, 400); wifiBtn.SetSoundOver(&btnSoundOver); wifiBtn.SetSoundClick(&btnClick1); wifiBtn.SetEffectGrow(); wifiBtn.SetAlpha(80); wifiBtn.SetTrigger(&trigA); GuiImage channelBtnImg(&channelImgData); channelBtnImg.SetWidescreen(CFG.widescreen); GuiButton channelBtn(channelBtnImg.GetWidth(), channelBtnImg.GetHeight()); channelBtn.SetAlignment(ALIGN_LEFT, ALIGN_TOP); channelBtn.SetPosition(440, 400); channelBtn.SetImage(&channelBtnImg); channelBtn.SetSoundOver(&btnSoundOver); channelBtn.SetSoundClick(btnClick2); channelBtn.SetEffectGrow(); channelBtn.SetTrigger(&trigA); GuiTooltip * titleTT = NULL; GuiWindow w(screenwidth, screenheight); /*** XML Variables ***/ HomebrewXML XMLInfo[4]; int pageToDisplay = 1; const int pages = roundup(filecount/4.0f); bool wifi_btn_loaded=false; while (menu == MENU_NONE) { //set pageToDisplay to 0 to quit VIDEO_WaitVSync (); menu = MENU_NONE; bool changed = false; int fileoffset = pageToDisplay*4-4; /** Standard procedure made in all pages **/ MainButton1.StopEffect(); MainButton2.StopEffect(); MainButton3.StopEffect(); MainButton4.StopEffect(); if (slidedirection == RIGHT) { MainButton1.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_OUT, 60); MainButton2.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_OUT, 60); MainButton3.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_OUT, 60); MainButton4.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_OUT, 60); while (MainButton1.GetEffect()>0) usleep(50); } else if (slidedirection == LEFT) { MainButton1.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_OUT, 60); MainButton2.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_OUT, 60); MainButton3.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_OUT, 60); MainButton4.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_OUT, 60); while (MainButton1.GetEffect()>0) usleep(50); } HaltGui(); mainWindow->RemoveAll(); /** Set new icons **/ for (int i = 0; i < 4; i++) { if (IconData[i] != NULL) { delete IconData[i]; IconData[i] = NULL; } if (IconImg[i] != NULL) { delete IconImg[i]; IconImg[i] = NULL; } if (fileoffset+i < (int) filecount) { char iconpath[200]; snprintf(iconpath, sizeof(iconpath), "%sicon.png", HomebrewFiles.GetFilepath(fileoffset+i)); IconData[i] = new GuiImageData(iconpath, 0); if (IconData[i]->GetImage()) { IconImg[i] = new GuiImage(IconData[i]); IconImg[i]->SetAlignment(ALIGN_LEFT, ALIGN_MIDDLE); IconImg[i]->SetPosition(12, 0); IconImg[i]->SetScale(0.95); } } } if (IconImg[0] != 0) MainButton1.SetIcon(IconImg[0]); else MainButton1.SetIcon(NULL); if (IconImg[1] != 0) MainButton2.SetIcon(IconImg[1]); else MainButton2.SetIcon(NULL); if (IconImg[2] != 0) MainButton3.SetIcon(IconImg[2]); else MainButton3.SetIcon(NULL); if (IconImg[3] != 0) MainButton4.SetIcon(IconImg[3]); else MainButton4.SetIcon(NULL); mainWindow->Append(&w); w.RemoveAll(); w.Append(&background); w.Append(&titleTxt); w.Append(&backBtn); w.Append(&h**o); w.Append(&wifiBtn); w.Append(&channelBtn); w.Append(&GoRightBtn); w.Append(&GoLeftBtn); if (pageToDisplay == pages) { int buttonsleft = filecount-(pages-1)*4; char * shortpath = NULL; char temp[200]; if (buttonsleft > 0) { snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset)); if (XMLInfo[0].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[0].GetName()); MainButton1Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[0].GetShortDescription()); MainButton1DescTxt.SetText(MainButtonText); MainButton1DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset)), "%s", HomebrewFiles.GetFilepath(fileoffset)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset)); XMLInfo[0].SetName(MainButtonText); MainButton1Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton1DescTxt.SetText(MainButtonText); MainButton1DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton1); } if (buttonsleft > 1) { snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+1)); if (XMLInfo[1].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[1].GetName()); MainButton2Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[1].GetShortDescription()); MainButton2DescTxt.SetText(MainButtonText); MainButton2DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+1)), "%s", HomebrewFiles.GetFilepath(fileoffset+1)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+1)); XMLInfo[1].SetName(MainButtonText); MainButton2Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton2DescTxt.SetText(MainButtonText); MainButton2DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton2); } if (buttonsleft > 2) { snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+2)); if (XMLInfo[3].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetName()); MainButton3Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetShortDescription()); MainButton3DescTxt.SetText(MainButtonText); MainButton3DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+2)), "%s", HomebrewFiles.GetFilepath(fileoffset+2)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+2)); XMLInfo[2].SetName(MainButtonText); MainButton3Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton3DescTxt.SetText(MainButtonText); MainButton3DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton3); } if (buttonsleft > 3) { snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+3)); if (XMLInfo[3].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetName()); MainButton4Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetShortDescription()); MainButton4DescTxt.SetText(MainButtonText); MainButton4DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+3)), "%s", HomebrewFiles.GetFilepath(fileoffset+3)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+3)); XMLInfo[3].SetName(MainButtonText); MainButton4Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton4DescTxt.SetText(MainButtonText); MainButton4DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton4); } } else { char temp[200]; char *shortpath = NULL; snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset)); if (XMLInfo[0].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[0].GetName()); MainButton1Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[0].GetShortDescription()); MainButton1DescTxt.SetText(MainButtonText); MainButton1DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset)), "%s", HomebrewFiles.GetFilepath(fileoffset)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset)); XMLInfo[0].SetName(MainButtonText); MainButton1Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton1DescTxt.SetText(MainButtonText); MainButton1DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton1); snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+1)); if (XMLInfo[1].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[1].GetName()); MainButton2Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[1].GetShortDescription()); MainButton2DescTxt.SetText(MainButtonText); MainButton2DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+1)), "%s", HomebrewFiles.GetFilepath(fileoffset+1)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+1)); XMLInfo[1].SetName(MainButtonText); MainButton2Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton2DescTxt.SetText(MainButtonText); MainButton2DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton2); snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+2)); if (XMLInfo[3].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetName()); MainButton3Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetShortDescription()); MainButton3DescTxt.SetText(MainButtonText); MainButton3DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+2)), "%s", HomebrewFiles.GetFilepath(fileoffset+2)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+2)); XMLInfo[2].SetName(MainButtonText); MainButton3Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton3DescTxt.SetText(MainButtonText); MainButton3DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton3); snprintf(temp, sizeof(temp), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+3)); if (XMLInfo[3].LoadHomebrewXMLData(temp) > 0) { snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetName()); MainButton4Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), "%s", XMLInfo[3].GetShortDescription()); MainButton4DescTxt.SetText(MainButtonText); MainButton4DescOverTxt.SetText(MainButtonText); } else { snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+3)), "%s", HomebrewFiles.GetFilepath(fileoffset+3)); shortpath = strrchr(temp, '/'); snprintf(MainButtonText, sizeof(MainButtonText), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+3)); XMLInfo[3].SetName(MainButtonText); MainButton4Txt.SetText(MainButtonText); snprintf(MainButtonText, sizeof(MainButtonText), " "); MainButton4DescTxt.SetText(MainButtonText); MainButton4DescOverTxt.SetText(MainButtonText); } w.Append(&MainButton4); } MainButton1.StopEffect(); MainButton2.StopEffect(); MainButton3.StopEffect(); MainButton4.StopEffect(); MainButton1.SetEffectGrow(); MainButton2.SetEffectGrow(); MainButton3.SetEffectGrow(); MainButton4.SetEffectGrow(); if (slidedirection == FADE) { MainButton1.SetEffect(EFFECT_FADE, 20); MainButton2.SetEffect(EFFECT_FADE, 20); MainButton3.SetEffect(EFFECT_FADE, 20); MainButton4.SetEffect(EFFECT_FADE, 20); } else if (slidedirection == LEFT) { MainButton1.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_IN, 60); MainButton2.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_IN, 60); MainButton3.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_IN, 60); MainButton4.SetEffect(EFFECT_SLIDE_LEFT | EFFECT_SLIDE_IN, 60); } else if (slidedirection == RIGHT) { MainButton1.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_IN, 60); MainButton2.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_IN, 60); MainButton3.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_IN, 60); MainButton4.SetEffect(EFFECT_SLIDE_RIGHT | EFFECT_SLIDE_IN, 60); } mainWindow->Append(&w); ResumeGui(); while (MainButton1.GetEffect() > 0) usleep(50); while (!changed) { VIDEO_WaitVSync (); if (MainButton1.GetState() == STATE_CLICKED) { char temp[200]; char iconpath[200]; char metapath[200]; char * shortpath = NULL; //write iconpath snprintf(metapath, sizeof(metapath), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset)); //write iconpath snprintf(iconpath, sizeof(iconpath), "%sicon.png", HomebrewFiles.GetFilepath(fileoffset)); //get filesize u64 filesize = HomebrewFiles.GetFilesize(fileoffset); //write short filename snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset)), "%s", HomebrewFiles.GetFilepath(fileoffset)); shortpath = strrchr(temp, '/'); snprintf(temp, sizeof(temp), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset)); int choice = HBCWindowPrompt(XMLInfo[0].GetName(), XMLInfo[0].GetCoder(), XMLInfo[0].GetVersion(), XMLInfo[0].GetReleasedate(), XMLInfo[0].GetLongDescription(), iconpath, filesize); if (choice == 1) { boothomebrew = 1; menu = MENU_EXIT; snprintf(Settings.selected_homebrew, sizeof(Settings.selected_homebrew), "%s%s", HomebrewFiles.GetFilepath(fileoffset), HomebrewFiles.GetFilename(fileoffset)); break; } MainButton1.ResetState(); } else if (MainButton2.GetState() == STATE_CLICKED) { char temp[200]; char iconpath[200]; char metapath[200]; char * shortpath = NULL; //write iconpath snprintf(metapath, sizeof(metapath), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+1)); //write iconpath snprintf(iconpath, sizeof(iconpath), "%sicon.png", HomebrewFiles.GetFilepath(fileoffset+1)); //get filesize u64 filesize = HomebrewFiles.GetFilesize(fileoffset+1); //write short filename snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+1)), "%s", HomebrewFiles.GetFilepath(fileoffset+1)); shortpath = strrchr(temp, '/'); snprintf(temp, sizeof(temp), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+1)); int choice = HBCWindowPrompt(XMLInfo[1].GetName(), XMLInfo[1].GetCoder(), XMLInfo[1].GetVersion(), XMLInfo[1].GetReleasedate(), XMLInfo[1].GetLongDescription(), iconpath, filesize); if (choice == 1) { boothomebrew = 1; menu = MENU_EXIT; snprintf(Settings.selected_homebrew, sizeof(Settings.selected_homebrew), "%s%s", HomebrewFiles.GetFilepath(fileoffset+1), HomebrewFiles.GetFilename(fileoffset+1)); break; } MainButton2.ResetState(); } else if (MainButton3.GetState() == STATE_CLICKED) { char temp[200]; char iconpath[200]; char metapath[200]; char * shortpath = NULL; //write iconpath snprintf(metapath, sizeof(metapath), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+2)); //write iconpath snprintf(iconpath, sizeof(iconpath), "%sicon.png", HomebrewFiles.GetFilepath(fileoffset+2)); //get filesize u64 filesize = HomebrewFiles.GetFilesize(fileoffset+2); //write short filename snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+2)), "%s", HomebrewFiles.GetFilepath(fileoffset+2)); shortpath = strrchr(temp, '/'); snprintf(temp, sizeof(temp), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+2)); int choice = HBCWindowPrompt(XMLInfo[2].GetName(), XMLInfo[2].GetCoder(), XMLInfo[2].GetVersion(), XMLInfo[2].GetReleasedate(), XMLInfo[2].GetLongDescription(), iconpath, filesize); if (choice == 1) { boothomebrew = 1; menu = MENU_EXIT; snprintf(Settings.selected_homebrew, sizeof(Settings.selected_homebrew), "%s%s", HomebrewFiles.GetFilepath(fileoffset+2), HomebrewFiles.GetFilename(fileoffset+2)); break; } MainButton3.ResetState(); } else if (MainButton4.GetState() == STATE_CLICKED) { char temp[200]; char iconpath[200]; char metapath[200]; char * shortpath = NULL; //write iconpath snprintf(metapath, sizeof(metapath), "%smeta.xml", HomebrewFiles.GetFilepath(fileoffset+3)); //write iconpath snprintf(iconpath, sizeof(iconpath), "%sicon.png", HomebrewFiles.GetFilepath(fileoffset+3)); //get filesize u64 filesize = HomebrewFiles.GetFilesize(fileoffset+3); //write short filename snprintf(temp, strlen(HomebrewFiles.GetFilepath(fileoffset+3)), "%s", HomebrewFiles.GetFilepath(fileoffset+3)); shortpath = strrchr(temp, '/'); snprintf(temp, sizeof(temp), "%s/%s", shortpath, HomebrewFiles.GetFilename(fileoffset+3)); int choice = HBCWindowPrompt(XMLInfo[3].GetName(), XMLInfo[3].GetCoder(), XMLInfo[3].GetVersion(), XMLInfo[3].GetReleasedate(), XMLInfo[3].GetLongDescription(), iconpath, filesize); if (choice == 1) { boothomebrew = 1; menu = MENU_EXIT; snprintf(Settings.selected_homebrew, sizeof(Settings.selected_homebrew), "%s%s", HomebrewFiles.GetFilepath(fileoffset+3), HomebrewFiles.GetFilename(fileoffset+3)); break; } MainButton4.ResetState(); } else if (shutdown == 1) Sys_Shutdown(); else if (reset == 1) Sys_Reboot(); else if (backBtn.GetState() == STATE_CLICKED) { menu = MENU_DISCLIST; changed = true; } else if (GoLeftBtn.GetState() == STATE_CLICKED) { pageToDisplay--; /** Change direction of the flying buttons **/ if (pageToDisplay < 1) pageToDisplay = pages; slidedirection = LEFT; changed = true; GoLeftBtn.ResetState(); } else if (GoRightBtn.GetState() == STATE_CLICKED) { pageToDisplay++; /** Change direction of the flying buttons **/ if (pageToDisplay > pages) pageToDisplay = 1; slidedirection = RIGHT; changed = true; GoRightBtn.ResetState(); } else if (wifiBtn.GetState() == STATE_CLICKED) { ResumeNetworkWait(); wifiBtn.ResetState(); } else if (h**o.GetState() == STATE_CLICKED) { cfg_save_global(); bgMusic->Pause(); choice = WindowExitPrompt(); bgMusic->Resume(); if (choice == 3) { Sys_LoadMenu(); // Back to System Menu } else if (choice == 2) { Sys_BackToLoader(); } else { h**o.ResetState(); } } else if (infilesize > 0) { char filesizetxt[50]; char temp[50]; if (infilesize < MB_SIZE) snprintf(filesizetxt, sizeof(filesizetxt), tr("Incoming file %0.2fKB"), infilesize/KB_SIZE); else snprintf(filesizetxt, sizeof(filesizetxt), tr("Incoming file %0.2fMB"), infilesize/MB_SIZE); snprintf(temp, sizeof(temp), tr("Load file from: %s ?"), GetIncommingIP()); int choice = WindowPrompt(filesizetxt, temp, tr("OK"), tr("Cancel")); if (choice == 1) { int res = AllocHomebrewMemory(infilesize); if (res < 0) { CloseConnection(); WindowPrompt(tr("Not enough free memory."), 0, tr("OK")); } else { u32 read = 0; u8 *temp = NULL; int len = NETWORKBLOCKSIZE; temp = (u8 *) malloc(infilesize); bool error = false; u8 *ptr = temp; while (read < infilesize) { ShowProgress(tr("Receiving file from:"), GetIncommingIP(), NULL, read, infilesize, true); if (infilesize - read < (u32) len) len = infilesize-read; else len = NETWORKBLOCKSIZE; int result = network_read(ptr, len); if (result < 0) { WindowPrompt(tr("Error while transfering data."), 0, tr("OK")); error = true; break; } if (!result) { break; } ptr += result; read += result; } char filename[101]; if (!error) { network_read((u8*) &filename, 100); // Do we need to unzip this thing? if (wiiloadVersion[0] > 0 || wiiloadVersion[1] > 4) { // We need to unzip... if (temp[0] == 'P' && temp[1] == 'K' && temp[2] == 0x03 && temp[3] == 0x04) { // It's a zip file, unzip to the apps directory // Zip archive, ask for permission to install the zip char zippath[255]; sprintf((char *) &zippath, "%s%s", Settings.homebrewapps_path, filename); FILE *fp = fopen(zippath, "wb"); if (fp != NULL) { fwrite(temp, 1, infilesize, fp); fclose(fp); // Now unzip the zip file... unzFile uf = unzOpen(zippath); if (uf==NULL) { error = true; } else { extractZip(uf,0,1,0, Settings.homebrewapps_path); unzCloseCurrentFile(uf); remove(zippath); // Reload this menu here... menu = MENU_HOMEBREWBROWSE; break; } } else { error = true; } } else if (uncfilesize != 0) { // if uncfilesize == 0, it's not compressed // It's compressed, uncompress u8 *unc = (u8 *) malloc(uncfilesize); uLongf f = uncfilesize; error = uncompress(unc, &f, temp, infilesize) != Z_OK; uncfilesize = f; free(temp); temp = unc; } } if (!error && strstr(filename,".zip") == NULL) { innetbuffer = temp; } } ProgressStop(); if (error || read != infilesize) { WindowPrompt(tr("Error:"), tr("No data could be read."), tr("OK")); FreeHomebrewBuffer(); } else { if (strstr(filename,".dol") || strstr(filename,".DOL") || strstr(filename,".elf") || strstr(filename,".ELF")) { boothomebrew = 2; menu = MENU_EXIT; CloseConnection(); break; } else if (strstr(filename,".zip")) { WindowPrompt(tr("Success:"), tr("Uploaded ZIP file installed to homebrew directory."), tr("OK")); CloseConnection(); } else { FreeHomebrewBuffer(); WindowPrompt(tr("ERROR:"), tr("Not a DOL/ELF file."), tr("OK")); } } } } CloseConnection(); ResumeNetworkWait(); } else if (channelBtn.GetState() == STATE_CLICKED) { w.SetState(STATE_DISABLED); //10001 are the channels that are installed as channels, not including shop channel/mii channel etc u32 num = 0x00010001; TitleBrowser(num); //if they didn't boot a channel reset this window w.SetState(STATE_DEFAULT); channelBtn.ResetState(); } if (IsNetworkInit()) { if (!wifi_btn_loaded) { wifiBtn.SetAlpha(255); titleTT = new GuiTooltip(GetNetworkIP()); titleTT->SetAlpha(THEME.tooltipAlpha); wifiBtn.SetToolTip(titleTT,0,-50,0,5); wifi_btn_loaded=true; } } } } w.SetEffect(EFFECT_FADE, -20); while (w.GetEffect()>0) usleep(50); HaltGui(); for (int i = 0; i < 4; i++) { if (IconData[i] != NULL) { delete IconData[i]; IconData[i] = NULL; } if (IconImg[i] != NULL) { delete IconImg[i]; IconImg[i] = NULL; } } delete titleTT; titleTT = NULL; if (IsNetworkInit()) HaltNetworkThread(); mainWindow->RemoveAll(); mainWindow->Append(bgImg); ResumeGui(); return menu; }
/**************************************************************************** * Download request ***************************************************************************/ s32 DownloadWithResponse(const char * url, u8 **outbuffer, u32 *outsize) { //Check if the url starts with "http://", if not it is not considered a valid url if (strncmp(url, "http://", strlen("http://")) != 0) return -1; //Locate the path part of the url by searching for '/' past "http://" char *path = strchr(url + strlen("http://"), '/'); //At the very least the url has to end with '/', ending with just a domain is invalid if (path == NULL) return -1; //Extract the domain part out of the url int domainlength = path - url - strlen("http://"); if (domainlength == 0) return -1; char domain[domainlength + 1]; strlcpy(domain, url + strlen("http://"), domainlength + 1); int connect = GetConnection(domain); if (connect < 0) return -1; //Form a nice request header to send to the webserver char header[strlen(path) + strlen(domain) + strlen(url) + 100]; sprintf(header, "GET %s HTTP/1.1\r\nHost: %s\r\nReferer: %s\r\nUser-Agent: USBLoaderGX\r\nConnection: close\r\n\r\n", path, domain, url); int ret = net_send(connect, header, strlen(header), 0); if(ret < 0) { net_close(connect); return ret; } int blocksize = 4096; u8 *buffer = (u8 *) malloc(blocksize); if(!buffer) { net_close(connect); return -1; } int done = 0; while(1) { int ret = network_read(connect, buffer+done, blocksize); if(ret < 0) { free(buffer); net_close(connect); return -1; } else if(ret == 0) break; done += ret; u8 *tmp = (u8 *) realloc(buffer, done+blocksize); if(!tmp) { free(buffer); net_close(connect); return -1; } buffer = tmp; } net_close(connect); u8 *tmp = (u8 *) realloc(buffer, done+1); if(!tmp) { free(buffer); return -1; } buffer = tmp; buffer[done] = 0; *outbuffer = buffer; *outsize = done; return done; }
int main(void) { led_conf(); network_init(); CLKPR = (1<<CLKPCE); //Change prescaler CLKPR = (1<<CLKPS0); //Use prescaler 2 //clock_prescale_set(2); enc28j60Write(ECOCON, 1 & 0x7); //Get a 25MHz signal from enc28j60 int i; uip_ipaddr_t ipaddr; struct timer periodic_timer, arp_timer; clock_init(); timer_set(&periodic_timer, CLOCK_SECOND / 2); timer_set(&arp_timer, CLOCK_SECOND * 10); uip_init(); struct uip_eth_addr mac = {UIP_ETHADDR0, UIP_ETHADDR1, UIP_ETHADDR2, UIP_ETHADDR3, UIP_ETHADDR4, UIP_ETHADDR5}; uip_setethaddr(mac); simple_httpd_init(); // httpd_init(); #ifdef __DHCPC_H__ dhcpc_init(&mac, 6); #else uip_ipaddr(ipaddr, 192,168,2,55); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, 192,168,2,1); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, 255,255,255,0); uip_setnetmask(ipaddr); /* uip_ipaddr(ipaddr, 192,167,0,255); uip_sethostaddr(ipaddr); uip_ipaddr(ipaddr, 192,167,0,254); uip_setdraddr(ipaddr); uip_ipaddr(ipaddr, 255,255,0,0); uip_setnetmask(ipaddr); */ #endif /*__DHCPC_H__*/ while(1){ uip_len = network_read(); if(uip_len > 0) { if(BUF->type == htons(UIP_ETHTYPE_IP)){ uip_arp_ipin(); uip_input(); if(uip_len > 0) { uip_arp_out(); network_send(); } }else if(BUF->type == htons(UIP_ETHTYPE_ARP)){ uip_arp_arpin(); if(uip_len > 0){ network_send(); } } }else if(timer_expired(&periodic_timer)) { timer_reset(&periodic_timer); led_blink(); for(i = 0; i < UIP_CONNS; i++) { uip_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #if UIP_UDP for(i = 0; i < UIP_UDP_CONNS; i++) { uip_udp_periodic(i); if(uip_len > 0) { uip_arp_out(); network_send(); } } #endif /* UIP_UDP */ if(timer_expired(&arp_timer)) { timer_reset(&arp_timer); uip_arp_timer(); } } } return 0; }