void tcp_task (void *data) { tcp_socket_t *lsock; unsigned short serv_port = 2222; unsigned char ch; int n; lsock = tcp_listen (&ip, 0, serv_port); if (! lsock) { printf (&debug, "Error on listen, aborted\n"); uos_halt (0); } /* Добавляем заголовок - длину пакета (LSB). */ memset (buf, 0xff, sizeof (buf)); buf[0] = (char) sizeof (buf); buf[1] = sizeof (buf) >> 8; for (;;) { printf (&debug, "Server waiting on port %d...\n", serv_port); printf (&debug, "Free memory: %d bytes\n", mem_available (&pool)); user_socket = tcp_accept (lsock); if (! user_socket) { printf (&debug, "Error on accept\n"); uos_halt (0); } /* Пересылаем данные, пока юзер не отключится. */ for (;;) { /* Десять пакетов в секуду. */ /* mutex_wait (&timer.decisec);*/ for (n=0; n<2; ++n) { if (tcp_write (user_socket, buf, sizeof (buf)) < 0) { /*printf (&debug, "tcp_write failed\n");*/ goto closed; } } /* Обрабатываем команды от пользователя. */ for (;;) { n = tcp_read_poll (user_socket, &ch, 1, 1); if (n < 0) goto closed; if (n == 0) break; /*process_command (ch);*/ } } closed: tcp_close (user_socket); mem_free (user_socket); user_socket = 0; } }
void mem_validate_hole(mem_pool_t *m, mheader_t *h){ assert( (size_t)h >= (size_t)m->store ); assert( ((size_t)h + h->size) <= (((size_t)m->store)+m->size) ); assert((size_t)h == MEM_ALIGN(h)); if (h->magic != MEMORY_HOLE_MAGIC) { debug_printf ("mem: bad hole magic at 0x%x\n", h); debug_printf (" size=%d, pool=%p\n", h->size, h->pool); uos_halt(1); } if (h->pool != m) { debug_printf ("mem: incorect pool pointer=%p, must be %p\n", h->pool, m); uos_halt(1); } }
void __assert_fail_ndebug () { debug_printf ("\nAssertion failed at address %p\n\n", __builtin_return_address (0)); uos_halt (1); }
//__NORETURN void __assert_fail (const char *cond, const char *file, unsigned int line, const char *func) { arch_intr_off (); #ifdef UOS_EXCEPTION_STACK //* if have another stack, and current stack is broken or small, // just go to exception as inevitable UOS_STRICT(STACK,) if(!(task_stack_enough( VPRINTF_FRAME )) ) { debug_puts("\nAssertion failed in function `"); debug_puts(func); debug_puts("':"); debug_puts(cond); debug_puts("\n"); debug_puts(file); ARCH_BREAK(); } #endif//UOS_EXCEPTION_STACK if (0 == debug_printf ("\nAssertion failed in function `%S':\n%S, %u: %S\n\n", func, file, line, cond) ) //если неудалась печать, обрушусь в стек исключений ARCH_BREAK(); uos_halt (1); }
void console_task(void *data) { printf(&debug, "\nTesting Ethernet\n\n"); eth_set_promisc(ð, 1, 1); data_pattern = mem_alloc(&pool, packet_size); if (!data_pattern) { printf(&debug, "No memory for data_pattern\n"); uos_halt(1); } memset(data_pattern, 0xFF, packet_size); if (packet_size >= 12) memcpy(data_pattern + 6, eth.netif.ethaddr, 6); for (;;) menu(); }
/** * Return the pool pointer of the given block. */ mem_pool_t *mem_pool (void *block) { mheader_t *h; if (! block) return 0; /* Make the header pointer. */ h = H_OF( block ); #if MEM_DEBUG if (h->magic != MEMORY_BLOCK_MAGIC) { debug_printf ("pool: bad block magic\n"); uos_halt(1); } #endif return h->pool; }
/* * Return the size of the given block. */ size_t mem_size (void *block) { mheader_t *h; if (! block) return 0; /* Make the header pointer. */ h = H_OF(block); #if MEM_DEBUG if (h->magic != MEMORY_BLOCK_MAGIC) { debug_printf ("size: bad block magic\n"); uos_halt(1); } #endif return h->size - MEM_HSIZE; }
void main_telnet (void *data) { tcp_socket_t *lsock, *sock; unsigned short serv_port = 23; lsock = tcp_listen (ip, 0, serv_port); if (! lsock) { debug_printf ("Error on listen, aborted\n"); uos_halt (1); } for (;;) { sock = tcp_accept (lsock); if (! sock) { debug_printf ("Error on accept\n"); continue; } start_session (sock); } }
void *mem_realloc (void *old_block, size_t bytes) { mheader_t *h; size_t old_size; void *block; if (! old_block) return 0; /* Make the header pointer. */ h = H_OF(old_block); #ifdef ARM_CACHED /* Clear the non-cached bit. */ h = (mheader_t*) ARM_CACHED (h); #endif #if MEM_DEBUG if (h->magic != MEMORY_BLOCK_MAGIC) { debug_printf ("realloc: bad block magic\n"); uos_halt(1); } #endif old_size = h->size - MEM_HSIZE; if (old_size >= bytes) return old_block; block = mem_alloc (h->pool, bytes); if (! block) { mem_make_hole (h); return 0; } #ifdef ARM_CACHED if (ARM_ISNONCACHED (old_block)) { /* Set the non-cached bit. */ block = (void*) ARM_NONCACHED (block); } #endif memcpy (block, old_block, old_size); mem_make_hole (h); return block; }
void mem_truncate (void *block, size_t required) { mheader_t *h, *newh; if (! block) return; #ifdef ARM_CACHED /* Clear the non-cached bit. */ block = (void*) ARM_CACHED (block); #endif /* Add the size of header. */ if (required < SIZEOF_POINTER) required = SIZEOF_POINTER; required = MEM_ALIGN (required + MEM_HSIZE); /* Make the header pointer. */ h = H_OF( block ); #if MEM_DEBUG if (h->magic != MEMORY_BLOCK_MAGIC) { debug_printf ("truncate: bad block magic\n"); uos_halt(1); } #endif /* Is there enough space to split? */ if (h->size >= required + MEM_HSIZE + 2*SIZEOF_POINTER) { /* Split into two blocks. */ newh = (mheader_t*) ((size_t)h + required); newh->pool = h->pool; newh->size = h->size - required; #if MEM_DEBUG newh->magic = MEMORY_HOLE_MAGIC; #endif mutex_lock (&newh->pool->lock); h->size = required; make_hole_locked (newh); mutex_unlock (&newh->pool->lock); } }
/** * Release a block of memory. */ void mem_free (void *block) { mheader_t *h; if (! block) return; #ifdef ARM_CACHED /* Clear the non-cached bit. */ block = (void*) ARM_CACHED (block); #endif /* Make the header pointer. */ h = H_OF(block); #if MEM_DEBUG if (h->magic != MEMORY_BLOCK_MAGIC) { debug_printf ("free[$%x]: bad block[$%x*$%x pool $%x] magic $%2s=$%hx \n" , block, h, h->size, h->pool , &h->magic, h->magic); uos_halt(1); } #endif /* Convert our block into a hole. */ mem_make_hole (h); }
void menu() { small_uint_t cmd; int full_duplex; printf(&debug, "Free memory: %d bytes\n", mem_available(&pool)); printf(&debug, "Ethernet: %s", eth_get_carrier(ð) ? "Cable OK" : "No cable"); if (eth_get_speed(ð, &full_duplex)) { printf(&debug, ", %s", full_duplex ? "Full Duplex" : "Half Duplex"); } printf(&debug, ", %u interrupts\n", eth.intr); printf(&debug, "Transmit: %ld packets, %ld collisions, %ld errors\n", eth.netif.out_packets, eth.netif.out_collisions, eth.netif.out_errors); printf(&debug, "Receive: %ld packets, %ld errors, %ld lost\n", eth.netif.in_packets, eth.netif.in_errors, eth.netif.in_discards); printf(&debug, "\n 1. Transmit 1 packet"); printf(&debug, "\n 2. Transmit 2 packets"); printf(&debug, "\n 3. Transmit 8 packets"); printf(&debug, "\n 4. Run send/receive test"); printf(&debug, "\n 5. Packet size: %d bytes", packet_size); printf(&debug, "\n 6. Local loopback: %s", local_loop ? "Enabled" : "Disabled"); puts(&debug, "\n\n"); for (;;) { /* Ввод команды. */ puts(&debug, "Command: "); while (peekchar (&debug) < 0) timer_delay(&timer, 50); cmd = getchar(&debug); putchar(&debug, '\n'); if (cmd == '\n' || cmd == '\r') break; if (cmd == '1') { send_packets(1); break; } if (cmd == '2') { send_packets(2); break; } if (cmd == '3') { send_packets(8); break; } if (cmd == '4') { run_test(); break; } if (cmd == '5') { try_again: printf(&debug, "Enter packet size (1-1518): "); packet_size = get_short(packet_size); if (packet_size <= 0 || packet_size > 1518) { printf(&debug, "Invalid value, try again."); goto try_again; } putchar(&debug, '\n'); data_pattern = mem_realloc(data_pattern, packet_size); if (!data_pattern) { printf(&debug, "No memory for data_pattern\n"); uos_halt(1); } int i; for (i = 0; i < packet_size; i++) data_pattern[i] = i; if (packet_size >= 6) memset(data_pattern, 0xFF, 6); if (packet_size >= 12) memcpy(data_pattern + 6, eth.netif.ethaddr, 6); break; } if (cmd == '6') { local_loop = !local_loop; eth_set_loop(ð, local_loop); break; } if (cmd == CTL('E')) { /* Регистры Ethernet. */ putchar(&debug, '\n'); eth_debug(ð, &debug); putchar(&debug, '\n'); continue; } if (cmd == CTL('T')) { /* Список задач uOS. */ printf(&debug, "\nFree memory: %u bytes\n\n", mem_available(&pool)); task_print(&debug, 0); task_print(&debug, (task_t*) stack_console); task_print(&debug, (task_t*) stack_test); task_print(&debug, (task_t*) eth.stack); putchar(&debug, '\n'); continue; } } }
/* Create a new sprite from the specified image file. Returns the address of * the new sprite on success or 0 on failure. If width is -1, the real * dimensions of the image file will be used, otherwise the image will be * scaled up or down to the specified dimensions. */ sprite *load_sprite(nbstate *state, unsigned char *fname, int width, int height) { sprite *s; GR_DRAW_ID a; GR_DRAW_ID p; GR_IMAGE_ID img; GR_IMAGE_INFO ii; /* Make sure the file name has been specified: */ if (! fname) return 0; /* Try to find a sprite in the list with the specified filename and * dimensions (any dimensions are OK if width is -1). If one is found, * increment its usage count and return its address. */ for(s = state->spritelist; s; s = s->next) { if ((width == -1 || (width == s->w && height == s->h)) && s->fname && !strcmp(fname, s->fname)) { s->usage++; return s; } } #ifdef HAVE_FILEIO { /* Make the full path to the filename because the Nano-X server * probably isn't running from the game data directory: */ char buf[256]; if (snprintf(buf, 256, "%s/%s", state->gamedir, fname) >= 256){ debug_printf ("Warning: image path \"%s/%s\" is too long\n", state->gamedir, fname); return 0; } /* Try to load the image file, and return 0 on failure: */ img = GrLoadImageFromFile (buf, 0); } #else if (strcmp (fname, (unsigned char*) "nbb1.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb1_data, sizeof( nbb1_data), 0); else if (strcmp (fname, (unsigned char*) "nbb2.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb2_data, sizeof( nbb2_data), 0); else if (strcmp (fname, (unsigned char*) "nbb3.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb3_data, sizeof( nbb3_data), 0); else if (strcmp (fname, (unsigned char*) "nbb4.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb4_data, sizeof( nbb4_data), 0); else if (strcmp (fname, (unsigned char*) "nbb5.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb5_data, sizeof( nbb5_data), 0); else if (strcmp (fname, (unsigned char*) "nbb6.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb6_data, sizeof( nbb6_data), 0); else if (strcmp (fname, (unsigned char*) "nbb7.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb7_data, sizeof( nbb7_data), 0); else if (strcmp (fname, (unsigned char*) "nbb8.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb8_data, sizeof( nbb8_data), 0); else if (strcmp (fname, (unsigned char*) "nbb9.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbb9_data, sizeof( nbb9_data), 0); else if (strcmp (fname, (unsigned char*)"nbball1.gif") == 0) img = GrLoadImageFromBuffer ((char*)nbball1_data, sizeof(nbball1_data), 0); else if (strcmp (fname, (unsigned char*) "nbbat1.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbat1_data, sizeof( nbbat1_data), 0); else if (strcmp (fname, (unsigned char*) "nbbat2.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbat2_data, sizeof( nbbat2_data), 0); else if (strcmp (fname, (unsigned char*) "nbbat3.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbat3_data, sizeof( nbbat3_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg10.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg10_data, sizeof( nbbg10_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg1.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg1_data, sizeof( nbbg1_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg2.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg2_data, sizeof( nbbg2_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg3.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg3_data, sizeof( nbbg3_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg4.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg4_data, sizeof( nbbg4_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg5.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg5_data, sizeof( nbbg5_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg6.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg6_data, sizeof( nbbg6_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg7.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg7_data, sizeof( nbbg7_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg8.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg8_data, sizeof( nbbg8_data), 0); else if (strcmp (fname, (unsigned char*) "nbbg9.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbbg9_data, sizeof( nbbg9_data), 0); else if (strcmp (fname, (unsigned char*) "nbpf.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbpf_data, sizeof( nbpf_data), 0); else if (strcmp (fname, (unsigned char*) "nbpn.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbpn_data, sizeof( nbpn_data), 0); else if (strcmp (fname, (unsigned char*) "nbpp.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbpp_data, sizeof( nbpp_data), 0); else if (strcmp (fname, (unsigned char*) "nbps.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbps_data, sizeof( nbps_data), 0); else if (strcmp (fname, (unsigned char*) "nbpt.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbpt_data, sizeof( nbpt_data), 0); else if (strcmp (fname, (unsigned char*) "nbpw.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbpw_data, sizeof( nbpw_data), 0); else if (strcmp (fname, (unsigned char*) "nbsp1.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbsp1_data, sizeof( nbsp1_data), 0); else if (strcmp (fname, (unsigned char*) "nbsp2.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbsp2_data, sizeof( nbsp2_data), 0); else if (strcmp (fname, (unsigned char*) "nbsp3.gif") == 0) img = GrLoadImageFromBuffer ((char*) nbsp3_data, sizeof( nbsp3_data), 0); else { debug_printf ("No such image: \"%s\"\n", fname); uos_halt (0); return 0; } #endif if (! img) { debug_printf ("Warning: failed to load image \"%s\"- make " "sure it is where the server can find it and " "that support for loading the relevant image " "type has been built into the server\n", fname); return 0; } /* If a size wasn't specified, get the real image size from the server * instead: */ if(width == -1 || height == -1) { GrGetImageInfo(img, &ii); width = ii.width; height = ii.height; } /* Make the alpha channel and pixmap to store the image in: */ if(!(a = GrNewPixmap(width, height, 0))) { GrFreeImage(img); return 0; } if(!(p = GrNewPixmap(width, height, 0))) { GrFreeImage(img); GrDestroyWindow(a); return 0; } /* Draw the image into the specified pixmap and alpha channel, scaling * it up or down if necessary: */ GrDrawImageToFit(p, state->gc, 0, 0, width, height, img); GrFreeImage(img); /* Destroy the server image object. */ /* Make a new sprite and link it into the list, then return its * address to the caller: */ s = make_sprite(state, fname, width, height, p, a); if (! s) { GrDestroyWindow(a); GrDestroyWindow(p); return 0; } return s; }
void uos_init (void) { mutex_group_t *g; /* Baud 9600. */ uart_init (&uart, 0, PRIO_UART, KHZ, 9600); timer_init (&timer, KHZ, 50); watchdog_alive (); /* Configure control pins. */ control_link_led (0); gpio_config (PIN_DCD, 1); /* P0 - output */ gpio_config (PIN_CTS, 1); /* P4 - output */ gpio_config (PIN_LINK, 1); /* P16 - output */ gpio_config (PIN_DSR, 1); /* P21 - output */ configure_ram (RAM_START, RAM_START + RAM_SIZE, REFRESH_USEC, IO_START); mem_init (&pool, RAM_START, RAM_START + RAM_SIZE); watchdog_alive (); /* Configure Ethernet. */ eth = mem_alloc (&pool, sizeof (eth_t)); if (! eth) { debug_printf ("No memory for eth_t\n"); uos_halt (1); } /* * Create a group of two locks: timer and eth. */ g = mutex_group_init (group, sizeof(group)); mutex_group_add (g, ð->netif.lock); mutex_group_add (g, &timer.decisec); ip = mem_alloc (&pool, sizeof (*ip)); if (! ip) { debug_printf ("No memory for ip_t\n"); uos_halt (1); } arp = arp_init (arp_data, sizeof(arp_data), ip); ip_init (ip, &pool, PRIO_IP, &timer, arp, g); /* * Create interface eth0 144.206.181.188 / 255.255.255.0 */ eth_init (eth, "eth0", PRIO_ETH_RX, PRIO_ETH_TX, &pool, arp); route_add_netif (ip, &route, (unsigned char*) "\220\316\265\274", 24, ð->netif); /* Configure HDLC. */ hdlc = mem_alloc (&pool, sizeof (hdlc_t)); if (! hdlc) { debug_printf ("No memory for hdlc_t\n"); uos_halt (1); } hdlc_init (hdlc, 0, "hdlc0", PRIO_HDLC_RX, PRIO_HDLC_TX, &pool, KHZ * 1000); hdlc->callback_receive = receive_hdlc; stack_console = mem_alloc (&pool, CONSOLE_STACKSZ); assert (stack_console != 0); task_create (main_console, 0, "console", PRIO_CONSOLE, stack_console, CONSOLE_STACKSZ); stack_poll = mem_alloc (&pool, POLL_STACKSZ); assert (stack_poll != 0); task_create (main_poll, 0, "poll", PRIO_POLL, stack_poll, POLL_STACKSZ); stack_telnet = mem_alloc (&pool, TELNET_STACKSZ); assert (stack_telnet != 0); task_create (main_telnet, 0, "telnet", PRIO_TELNET, stack_telnet, TELNET_STACKSZ); }