dev_ethernet.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include <stdio.h>
  2. #include "minilisp.h"
  3. #include "alloc.h"
  4. #include "stream.h"
  5. #include "compiler_new.h"
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. static uint8_t* eth_rx_buffer;
  9. int ethernet_rx(uint8_t* packet) {
  10. int frame_len = 0;
  11. USPiReceiveFrame(packet, &frame_len);
  12. if (frame_len) {
  13. printf("[ethfs] frame received! len: %d\r\n",frame_len);
  14. memdump(packet,frame_len,0);
  15. }
  16. return frame_len;
  17. }
  18. void ethernet_tx(uint8_t* packet, int len) {
  19. USPiSendFrame(packet, len);
  20. printf("[ethfs] frame sent (%d)\r\n",len);
  21. }
  22. #include "devices/mini-ip.c"
  23. Cell* ethfs_open(Cell* path_cell) {
  24. // TODO typecheck
  25. char* path = path_cell->addr;
  26. if (!strncmp((char*)(path+4),"/tcp/",5)) {
  27. printf("[tcp] %s",path+4);
  28. char ip[4] = {0,0,0,0};
  29. int len = 0;
  30. int j = 0;
  31. int dgt = 0;
  32. int i = 0;
  33. for (i=9; i<strlen(path) && j<4; i++) {
  34. if (path[i]=='/') break;
  35. if (path[i]=='.') {
  36. j++;
  37. } else {
  38. ip[j]=ip[j]*10+(path[i]-'0');
  39. }
  40. len++;
  41. }
  42. printf("[tcp] ip from path: %d.%d.%d.%d\r\n",ip[0],ip[1],ip[2],ip[3]);
  43. int port = 0;
  44. i++;
  45. for (; i<strlen(path); i++) {
  46. if (path[i]=='/') break;
  47. port=port*10+(path[i]-'0');
  48. }
  49. printf("[tcp] port from path: %d\r\n",port);
  50. connect_tcp(ip,port);
  51. }
  52. return alloc_int(1);
  53. }
  54. Cell* ethfs_read() {
  55. /*int len = ethernet_rx(eth_rx_buffer);
  56. Cell* packet = alloc_num_bytes(len);
  57. memcpy(packet->addr, eth_rx_buffer, len);*/
  58. return eth_task();
  59. //return packet;
  60. }
  61. // FIXME differntiate connections, protocols
  62. Cell* ethfs_write(Cell* stream, Cell* packet) {
  63. //ethernet_tx(packet->addr,packet->size);
  64. return send_tcp(packet);
  65. }
  66. void mount_ethernet() {
  67. eth_rx_buffer=malloc(64*1024);
  68. init_mini_ip();
  69. fs_mount_builtin("/net", ethfs_open, ethfs_read, ethfs_write, 0, 0);
  70. printf("[ethfs] mounted\r\n");
  71. }