int init_platform() { #if __MICROBLAZE__ || __PPC__ enable_caches(); #ifdef PLATFORM_STDOUT_IS_16550 /* if we have a uart 16550, then that needs to be initialized */ XUartNs550_SetBaud(PLATFORM_STDOUT_BASEADDR, XPAR_XUARTNS550_CLOCK_HZ, PLATFORM_BAUDRATE); XUartNs550_mSetLineControlReg(PLATFORM_STDOUT_BASEADDR, XUN_LCR_8_DATA_BITS); #endif platform_setup_interrupts(); /* initialize file system layer */ if (platform_init_fs() < 0) return -1; #endif #ifdef __arm__ if (Init_ScuTimer() != XST_SUCCESS) while(1); SetupIntrSystem(&TimerInstance, TIMER_IRPT_INTR); /* initialize file system layer */ if (platform_init_fs() < 0) return -1; #endif return 0; }
int init_platform() { enable_caches(); #ifdef PLATFORM_STDOUT_IS_16550 /* if we have a uart 16550, then that needs to be initialized */ XUartNs550_SetBaud(PLATFORM_STDOUT_BASEADDR, XPAR_XUARTNS550_CLOCK_HZ, PLATFORM_BAUDRATE); XUartNs550_mSetLineControlReg(PLATFORM_STDOUT_BASEADDR, XUN_LCR_8_DATA_BITS); #endif platform_setup_interrupts(); /* initialize file system layer */ if (platform_init_fs() < 0) return -1; return 0; }
int start_web_application() { struct tcp_pcb *pcb; err_t err; /* initialize file system layer */ platform_init_fs(); /* initialize devices */ platform_init_gpios(); /* create new TCP PCB structure */ pcb = tcp_new(); if (!pcb) { xil_printf("Error creating PCB. Out of Memory\r\n"); return -1; } /* bind to http port 80 */ err = tcp_bind(pcb, IP_ADDR_ANY, http_port); if (err != ERR_OK) { xil_printf("Unable to bind to port 80: err = %d\r\n", err); return -2; } /* we do not need any arguments to the first callback */ tcp_arg(pcb, NULL); /* listen for connections */ pcb = tcp_listen(pcb); if (!pcb) { xil_printf("Out of memory while tcp_listen\r\n"); return -3; } /* specify callback to use for incoming connections */ tcp_accept(pcb, http_accept_callback); http_server_running = 1; return 0; }
// respond for a file GET request int do_http_get(struct tcp_pcb* pcb, char* req, int rlen) { int BUFSIZE = 1400; char filename[MAX_FILENAME]; unsigned char buf[BUFSIZE]; signed int fsize, hlen, n; int fd; char* fext; err_t err; // determine file name extract_file_name(filename, req, rlen, MAX_FILENAME); // respond with 404 if not present if (mfs_exists_file(filename) != 1) { xil_printf("requested file %s not found, returning 404\r\n", filename); do_404(pcb, req, rlen); return -1; } // respond with correct file // debug statement on UART xil_printf("http GET: %s\r\n", filename); // get a pointer to file extension fext = get_file_extension(filename); fd = mfs_file_open(filename, MFS_MODE_READ); if (fd == -1) { platform_init_fs(); extract_file_name(filename, req, rlen, MAX_FILENAME); if (mfs_exists_file(filename) != 1) { xil_printf("requested file %s not found, returning 404\r\n", filename); do_404(pcb, req, rlen); return -1; } fext = get_file_extension(filename); fd = mfs_file_open(filename, MFS_MODE_READ); return -1; } // obtain file size, // note that lseek with offset 0, MFS_SEEK_END does not move file pointer fsize = mfs_file_lseek(fd, 0, MFS_SEEK_END); if (fsize == -1) { xil_printf("\r\nFile Read Error\r\n"); return -1; } // write the http headers hlen = generate_http_header((char*)buf, fext, fsize); if ((err = tcp_write(pcb, buf, hlen, 3)) != ERR_OK) { xil_printf("error (%d) writing http header to socket\r\n", err); xil_printf("attempted to write #bytes = %d, tcp_sndbuf = %d\r\n", hlen, tcp_sndbuf(pcb)); xil_printf("http header = %s\r\n", buf); return -1; } // now write the file while (fsize > 0) { int sndbuf; sndbuf = tcp_sndbuf(pcb); if (sndbuf < BUFSIZE) { // not enough space in sndbuf, so send remaining bytes when there is space // this is done by storing the fd in as part of the tcp_arg, // so that the sent callback handler knows to send data http_arg* a = (http_arg*)pcb->callback_arg; a->fd = fd; a->fsize = fsize; return -1; } n = mfs_file_read(fd, (char*)buf, BUFSIZE); if ((err = tcp_write(pcb, buf, n, 3)) != ERR_OK) { xil_printf("error writing file (%s) to socket, remaining unwritten bytes = %d\r\n", filename, fsize - n); xil_printf("attempted to lwip_write %d bytes, tcp write error = %d\r\n", n, err); break; } if (fsize >= n) fsize -= n; else fsize = 0; } mfs_file_close(fd); return 0; }