main9.pp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. (*---------------------------------------------------------------------------------
  2. $Id: template.c,v 1.4 2005/09/17 23:15:13 wntrmute Exp $
  3. Basic Hello World
  4. $Log: template.c,v $
  5. Revision 1.4 2005/09/17 23:15:13 wntrmute
  6. corrected iprintAt in templates
  7. Revision 1.3 2005/09/05 00:32:20 wntrmute
  8. removed references to IPC struct
  9. replaced with API functions
  10. Revision 1.2 2005/08/31 01:24:21 wntrmute
  11. updated for new stdio support
  12. Revision 1.1 2005/08/03 06:29:56 wntrmute
  13. added templates
  14. ---------------------------------------------------------------------------------*)
  15. program main9;
  16. {$apptype arm9}
  17. {$define ARM9}
  18. {$mode objfpc}
  19. uses
  20. ctypes, nds9, dswifi9;
  21. { $include dswifi9.inc}
  22. { $include socket.inc}
  23. { $include in.inc}
  24. { $include netdb.inc}
  25. //---------------------------------------------------------------------------------
  26. // Dswifi helper functions
  27. // wifi timer function, to update internals of sgIP
  28. procedure Timer_50ms();
  29. begin
  30. Wifi_Timer(50);
  31. end;
  32. // notification function to send fifo message to arm7
  33. procedure arm9_synctoarm7();
  34. begin
  35. // send fifo message
  36. REG_IPC_FIFO_TX^ := $87654321;
  37. end;
  38. // interrupt handler to receive fifo messages from arm7
  39. procedure arm9_fifo();
  40. var
  41. value: cuint32;
  42. begin
  43. // check incoming fifo messages
  44. value := REG_IPC_FIFO_RX^;
  45. if value = $87654321 then
  46. Wifi_Sync();
  47. end;
  48. procedure vblank_irq_handler();
  49. begin
  50. Wifi_Update();
  51. end;
  52. var
  53. Wifi_pass: cuint32;
  54. i: integer;
  55. myhost: PHostent;
  56. my_socket: cint;
  57. sain: sockaddr_in;
  58. recvd_len: cint;
  59. incoming_buffer: array [0..255] of char;
  60. const
  61. request_text =
  62. 'GET /dswifi/example1.php HTTP/1.1'#13#10 +
  63. 'Host: www.akkit.org'#13#10 +
  64. 'User-Agent: Nintendo DS'#13#10 +#13#10;
  65. begin
  66. videoSetMode(0); //not using the main screen
  67. videoSetModeSub(MODE_0_2D or DISPLAY_BG0_ACTIVE); //sub bg 0 will be used to print text
  68. vramSetBankC(VRAM_C_SUB_BG);
  69. SUB_BG0_CR^ := BG_MAP_BASE(31);
  70. BG_PALETTE_SUB[255] := RGB15(31,31,31); //by default font will be rendered with color 255
  71. //consoleInit() is a lot more flexible but this gets you up and running quick
  72. consoleInitDefault(pcuint16(SCREEN_BASE_BLOCK_SUB(31)), pcuint16(CHAR_BASE_BLOCK_SUB(0)), 16);
  73. iprintf(#10+#10+#9+'Hello World!' + #10);
  74. // send fifo message to initialize the arm7 wifi
  75. REG_IPC_FIFO_CR^ := IPC_FIFO_ENABLE or IPC_FIFO_SEND_CLEAR; // enable & clear FIFO
  76. Wifi_pass := Wifi_Init(WIFIINIT_OPTION_USELED);
  77. REG_IPC_FIFO_TX^ := $12345678;
  78. REG_IPC_FIFO_TX^ := Wifi_pass;
  79. TIMER3_CR^ := 0; // disable timer3
  80. irqInit();
  81. irqSet(IRQ_VBLANK, @vblank_irq_handler);
  82. irqSet(IRQ_TIMER3, @Timer_50ms); // setup timer IRQ
  83. irqEnable(IRQ_VBLANK or IRQ_TIMER3);
  84. irqSet(IRQ_FIFO_NOT_EMPTY, @arm9_fifo); // setup fifo IRQ
  85. irqEnable(IRQ_FIFO_NOT_EMPTY);
  86. REG_IPC_FIFO_CR^ := IPC_FIFO_ENABLE or IPC_FIFO_RECV_IRQ; // enable FIFO IRQ
  87. Wifi_SetSyncHandler(@arm9_synctoarm7); // tell wifi lib to use our handler to notify arm7
  88. // set timer3
  89. //TIMER3_DATA^ := -6553; // 6553.1 * 256 cycles = ~50ms;
  90. TIMER3_DATA^ := TIMER_FREQ_256(1000 div 50);
  91. TIMER0_CR^ := TIMER_ENABLE or TIMER_DIV_256 or TIMER_IRQ_REQ; // enable, irq, 1/256 clock
  92. while (Wifi_CheckInit() = 0) do // wait for arm7 to be initted successfully
  93. begin
  94. // wait for vblank
  95. swiWaitForVBlank();
  96. end;
  97. // wifi init complete - wifi lib can now be used!
  98. iprintf('Connecting via WFC data' + #10);
  99. // simple WFC connect:
  100. Wifi_AutoConnect(); // request connect
  101. while true do
  102. begin
  103. i := Wifi_AssocStatus(); // check status
  104. if (i = cint(ASSOCSTATUS_ASSOCIATED)) then
  105. begin
  106. iprintf('Connected successfully!' + #10);
  107. break;
  108. end;
  109. if (i = cint(ASSOCSTATUS_CANNOTCONNECT)) then
  110. begin
  111. iprintf('Could not connect!' +#10);
  112. while true do;
  113. break;
  114. end;
  115. end;
  116. // if connected, you can now use the berkley sockets interface to connect to the internet!
  117. //////////////////////////////////////////////////////////////////////////
  118. // Let's send a simple HTTP request to a server and print the results!
  119. // store the HTTP request for later
  120. // Find the IP address of the server, with gethostbyname
  121. myhost := gethostbyname('www.akkit.org');
  122. iprintf('Found IP Address!' + #10);
  123. // Create a TCP socket
  124. my_socket := socket( AF_INET, SOCK_STREAM, 0 );
  125. iprintf('Created Socket!' + #10);
  126. // Tell the socket to connect to the IP address we found, on port 80 (HTTP)
  127. sain.sin_family := AF_INET;
  128. sain.sin_port := htons(80);
  129. sain.sin_addr.s_addr := culong(Pointer(myhost^.h_addr_list^)^);
  130. iprintf(#10 + 'Accepted %d.%d.%d.%d:%d' + #10, [sain.sin_addr.s_addr and $FF,
  131. (sain.sin_addr.s_addr shr 8) and $FF,
  132. (sain.sin_addr.s_addr shr 16) and $FF,
  133. (sain.sin_addr.s_addr shr 24) and $FF,
  134. ntohs(sain.sin_port)]);
  135. connect(my_socket, psockaddr(@sain), sizeof(sain));
  136. iprintf('Connected to server!' + #10);
  137. // send our request
  138. send(my_socket, pchar(request_text), strlen(request_text), 0);
  139. iprintf('Sent our request!' + #10);
  140. // Print incoming data
  141. iprintf('Printing incoming data:' + #10);
  142. repeat
  143. recvd_len := recv( my_socket, @incoming_buffer, 255, 0 ) ;
  144. // if recv returns 0, the socket has been closed.
  145. if (recvd_len > 0) then // data was received!
  146. begin
  147. incoming_buffer[recvd_len] := #0; // null-terminate
  148. iprintf(incoming_buffer);
  149. end;
  150. until recvd_len <= 0;
  151. iprintf('Other side closed connection!' + #10);
  152. shutdown(my_socket, 0); // good practice to shutdown the socket.
  153. closesocket(my_socket); // remove the socket.
  154. while true do;
  155. end.