All pastes #430169 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-04-08 20:00 UTC
rendered paste body
#include <string.h>
#include <errno.h>
#include <malloc.h>
#include <stdlib.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <psputility.h>
#include <pspnet_apctl.h>
#include <pspnet_inet.h>
#include <pspwlan.h>
#include <pspctrl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <pspnet_resolver.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <pspnet.h>
#include <pspdebug.h>
#include <psptypes.h>
#include "engine.c"
#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/types.h>

PSP_MODULE_INFO("NEBAS", 0x1000, 1, 1);
PSP_MAIN_THREAD_ATTR(0);
PSP_MAIN_THREAD_STACK_SIZE_KB(32); 

int exit_callback(int arg1, int arg2, void *common) {
          sceNetApctlDisconnect();
          pspSdkInetTerm();
          sceKernelExitGame();
          return 0;
}

int CallbackThread(SceSize args, void *argp) {
          int cbid;

          cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
          sceKernelRegisterExitCallback(cbid);

          sceKernelSleepThreadCB();

          return 0;
}

int SetupCallbacks(void) {
          int thid = 0;

          thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
          if(thid >= 0) {
                    sceKernelStartThread(thid, 0, 0);
          }

          return thid;
}

int NetConnect(int config)
{
   pspDebugScreenInit();
   SceCtrlData pad;

   	while (sceWlanGetSwitchState() != 1)
	{
		pspDebugScreenPrintf("Turn the wifi switch on!\n");
		sceKernelDelayThread(1000*1000);
	}

   // init wlan
   int err = pspSdkInetInit();
   if (err != 0) {
      pspDebugScreenPrintf("pspSdkInetInit failed: %i", err);
      return 1;
   }

connectWifi:
	err = sceNetApctlConnect(config);
	if (err != 0)
	{
		pspDebugScreenPrintf("sceNetApctlConnect returns $%x\n", err);
		sceKernelDelayThread(4*1000000); // 4sec to read before exit
		return 1;
	}

	// Report status while waiting for connection to access point
	int stateLast = -1;
	pspDebugScreenPrintf("Connecting To Wifi...\n");
	while (1)
	{
		sceCtrlPeekBufferPositive(&pad, 1); //READ CONTROLS
		
		if (pad.Buttons & PSP_CTRL_CIRCLE)
                     return 1;

                int state;
		err = sceNetApctlGetState(&state);
		if (err != 0)
		{
			pspDebugScreenPrintf("sceNetApctlGetState returns $%x\n", err);
			sceKernelDelayThread(10*1000000); // 10sec to read before exit
			return 1;
		}
		if (state != stateLast)
		{
			if (stateLast == 2 && state == 0)
			{
				pspDebugScreenPrintf("  Connecting to wifi Failed, Retrying...\n");
				sceKernelDelayThread(500*1000); // 500ms
				goto connectWifi;
			}
			pspDebugScreenPrintf("  connection state %d of 4\n", state);
			stateLast = state;
		}
		if (state == 4)
			break;  // connected

		// wait a little before polling again
		sceKernelDelayThread(50*1000); // 50ms
	}



	sceKernelDelayThread(2*1000000); // 2sec to read any final status

	return 0;
}

int user_main(SceSize argc, void* argv)
{
	char buffer;
          SetupCallbacks();
		   pspDebugScreenInit();
          pspDebugScreenPrintf("Starting WiFi initialization procedures.\n");
		  NetConnect(1);
		  
		  cINIT();
		  cPROCESS("GET", "http://www.stapol.biaklan.com/txtnews.txt", "http://www.google.ca", 0);
		  buffer = cPASSDATA();
		  if(strstr(buffer, "STAPOL")) printf("Successfully parsed required code\n");
		  else printf("The required code to parse was not present\n");
		  sceKernelDelayThread(3 * 1000 * 1000);
   return 0;
}

int main(int argc, char *argv[])
{

   pspDebugScreenInit();
   int err = pspSdkLoadInetModules();

   if (err != 0) {
      pspDebugScreenPrintf("pspSdkLoadInetModules failed with %x\n", err);
      return 1;
   }

  	SceUID thid = sceKernelCreateThread("User Mode Thread", user_main, 8, 256 * 1024, PSP_THREAD_ATTR_USER, NULL);

	sceKernelStartThread(thid, 0, NULL);
	sceKernelWaitThreadEnd(thid, NULL);

	sceKernelExitGame();
	return 0;
}