Advertisement
xerpi

psp wifi

Feb 1st, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.00 KB | None | 0 0
  1. #include <pspkernel.h>
  2. #include <psputility.h>
  3. #include <pspdebug.h>
  4. #include <pspsdk.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <pspnet.h>
  9. #include <pspnet_inet.h>
  10. #include <pspnet_apctl.h>
  11. #include <pspnet_resolver.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14. #include <sys/select.h>
  15. #include <errno.h>
  16.  
  17. #define printf pspDebugScreenPrintf
  18.  
  19. #define MODULE_NAME "NetSample"
  20. #define HELLO_MSG   "Hello there. Type away.\r\n"
  21.  
  22. PSP_MODULE_INFO(MODULE_NAME, PSP_MODULE_USER, 1, 1);
  23. PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | THREAD_ATTR_VFPU);
  24.  
  25. int exit_callback(int arg1, int arg2, void *common);
  26. int CallbackThread(SceSize args, void *argp);
  27. int SetupCallbacks(void);
  28.  
  29. #define SERVER_PORT 4299
  30.  
  31. int make_socket(uint16_t port)
  32. {
  33.     int sock;
  34.     int ret;
  35.     struct sockaddr_in name;
  36.  
  37.     sock = socket(PF_INET, SOCK_STREAM, 0);
  38.     if(sock < 0)
  39.     {
  40.         return -1;
  41.     }
  42.  
  43.     name.sin_family = AF_INET;
  44.     name.sin_port = htons(port);
  45.     name.sin_addr.s_addr = htonl(INADDR_ANY);
  46.     ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
  47.     if(ret < 0)
  48.     {
  49.         return -1;
  50.     }
  51.  
  52.     return sock;
  53. }
  54.  
  55. /* Start a simple tcp echo server */
  56. void start_server(const char *szIpAddr)
  57. {
  58.     int ret;
  59.     int sock;
  60.     int new = -1;
  61.     struct sockaddr_in client;
  62.     size_t size;
  63.     int readbytes;
  64.     char data[1024];
  65.     fd_set set;
  66.     fd_set setsave;
  67.  
  68.     /* Create a socket for listening */
  69.     sock = make_socket(SERVER_PORT);
  70.     if(sock < 0)
  71.     {
  72.         printf("Error creating server socket\n");
  73.         return;
  74.     }
  75.  
  76.     ret = listen(sock, 1);
  77.     if(ret < 0)
  78.     {
  79.         printf("Error calling listen\n");
  80.         return;
  81.     }
  82.  
  83.     printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
  84.  
  85.     FD_ZERO(&set);
  86.     FD_SET(sock, &set);
  87.     setsave = set;
  88.  
  89.     while(1)
  90.     {
  91.         int i;
  92.         set = setsave;
  93.         if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0)
  94.         {
  95.             printf("select error\n");
  96.             return;
  97.         }
  98.  
  99.         for(i = 0; i < FD_SETSIZE; i++)
  100.         {
  101.             if(FD_ISSET(i, &set))
  102.             {
  103.                 int val = i;
  104.  
  105.                 if(val == sock)
  106.                 {
  107.                     new = accept(sock, (struct sockaddr *) &client, &size);
  108.                     if(new < 0)
  109.                     {
  110.                         printf("Error in accept %s\n", strerror(errno));
  111.                         close(sock);
  112.                         return;
  113.                     }
  114.  
  115.                     printf("New connection %d from %s:%d\n", val,
  116.                             inet_ntoa(client.sin_addr),
  117.                             ntohs(client.sin_port));
  118.  
  119.                     write(new, HELLO_MSG, strlen(HELLO_MSG));
  120.  
  121.                     FD_SET(new, &setsave);
  122.                 }
  123.                 else
  124.                 {
  125.                     readbytes = read(val, data, sizeof(data));
  126.                     if(readbytes <= 0)
  127.                     {
  128.                         printf("Socket %d closed\n", val);
  129.                         FD_CLR(val, &setsave);
  130.                         close(val);
  131.                     }
  132.                     else
  133.                     {
  134.                         write(val, data, readbytes);
  135.                         printf("%.*s", readbytes, data);
  136.                     }
  137.                 }
  138.             }
  139.         }
  140.     }
  141.  
  142.     close(sock);
  143. }
  144.  
  145. /* Connect to an access point */
  146. int connect_to_apctl(int config)
  147. {
  148.     int err;
  149.     int stateLast = -1;
  150.  
  151.     /* Connect using the first profile */
  152.     err = sceNetApctlConnect(config);
  153.     if (err != 0)
  154.     {
  155.         printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
  156.         return 0;
  157.     }
  158.  
  159.     printf(MODULE_NAME ": Connecting...\n");
  160.     while (1)
  161.     {
  162.         int state;
  163.         err = sceNetApctlGetState(&state);
  164.         if (err != 0)
  165.         {
  166.             printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
  167.             break;
  168.         }
  169.         if (state > stateLast)
  170.         {
  171.             printf("  connection state %d of 4\n", state);
  172.             stateLast = state;
  173.         }
  174.         if (state == 4)
  175.             break;  // connected with static IP
  176.  
  177.         // wait a little before polling again
  178.         sceKernelDelayThread(50*1000); // 50ms
  179.     }
  180.     printf(MODULE_NAME ": Connected!\n");
  181.  
  182.     if(err != 0)
  183.     {
  184.         return 0;
  185.     }
  186.  
  187.     return 1;
  188. }
  189.  
  190.  
  191. int main(int argc, char **argv)
  192. {
  193.     SetupCallbacks();
  194.     pspDebugScreenInit();
  195.  
  196.     if(sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON) < 0 || sceUtilityLoadNetModule(PSP_NET_MODULE_INET) < 0)
  197.     {
  198.         printf("Error, could not load inet modules\n");
  199.         sceKernelSleepThread();
  200.     }
  201.    
  202.     int err;
  203.     if((err = pspSdkInetInit()))
  204.     {
  205.         printf(MODULE_NAME ": Error, could not initialise the network %08X\n", err);
  206.         sceKernelSleepThread();
  207.     }
  208.  
  209.     if(connect_to_apctl(1))
  210.     {
  211.         // connected, get my IPADDR and run test
  212.         union SceNetApctlInfo info;
  213.        
  214.         if (sceNetApctlGetInfo(PSP_NET_APCTL_INFO_IP, &info) != 0)
  215.             strcpy(info.ip, "unknown IP");
  216.        
  217.         start_server(info.ip);
  218.     }
  219.  
  220.     sceKernelSleepThread();
  221.  
  222.     return 0;
  223. }
  224.  
  225. /* Exit callback */
  226. int exit_callback(int arg1, int arg2, void *common)
  227. {
  228.     sceKernelExitGame();
  229.     return 0;
  230. }
  231.  
  232. /* Callback thread */
  233. int CallbackThread(SceSize args, void *argp)
  234. {
  235.     int cbid;
  236.  
  237.     cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  238.     sceKernelRegisterExitCallback(cbid);
  239.     sceKernelSleepThreadCB();
  240.  
  241.     return 0;
  242. }
  243.  
  244. /* Sets up the callback thread and returns its thread id */
  245. int SetupCallbacks(void)
  246. {
  247.     int thid = 0;
  248.  
  249.     thid = sceKernelCreateThread("update_thread", CallbackThread,
  250.                      0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
  251.     if(thid >= 0)
  252.     {
  253.         sceKernelStartThread(thid, 0, 0);
  254.     }
  255.  
  256.     return thid;
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement