示例#1
0
static xjse_result_t xpnet_pollwait_apcon(XPNETCTX* pxpnctx)
{
#define	XPNET_APCTLCONN_TIMEDOUT	(12000)
#define	XPNET_APCTLCONN_POLLDURA	(300)
#ifdef	XTPF_Linux
	// simulate apcon wait
	xjse_time_t	tnow;
	while(1) {
		xjse_time_now(&tnow);
		if(tnow > pxpnctx->tlastcomm + 3 * 1000) {
			XJSE_TRACE("(X) exiting hoax apcon wait.(%lld, %lld)",
				tnow, pxpnctx->tlastcomm);
			break;
		}
		xjse_sleep(XPNET_APCTLCONN_POLLDURA);
	}
	pxpnctx->napconstatus = XPNETAPCTLSTAT_CONNED;
#endif	//XTPF_Linux
#ifdef	XTPF_PSP
	xjse_int_t	ncnt = 0, nsceerr = 0, nstatelast = -1;
	while(1) {
		nsceerr = sceNetApctlGetState(&(pxpnctx->napconstatus));
		if(nsceerr != 0) {
			XJSE_TRACE("(E) sceNetApctlGetState() failed!");
			goto	failed;
		}
		if(pxpnctx->napconstatus > nstatelast) {
			XJSE_TRACE("(X) connecting... (%d -> %d)",
				nstatelast, pxpnctx->napconstatus);
			nstatelast = pxpnctx->napconstatus;
		}
		if(pxpnctx->napconstatus == XPNETAPCTLSTAT_CONNED)
			break;	// connected
		sceKernelDelayThread(XPNET_APCTLCONN_POLLDURA * 1000);

		if(ncnt++ > (XPNET_APCTLCONN_TIMEDOUT / XPNET_APCTLCONN_POLLDURA)) {
			XJSE_TRACE("(E) connection timed out!");
			goto	failed;
		}
	}

#if	1
	char	szipaddr[32];
	if(sceNetApctlGetInfo(8, szipaddr) != 0) {
		XJSE_TRACE("(E) no ip address!");
	} else {
		XJSE_TRACE("(X) ip address retrieved: [%s]", szipaddr);
	}
#endif
#endif	//XTPF_PSP

	return	XJSE_SUCCESS;

#ifdef	XTPF_PSP
failed:
	pxpnctx->napconstatus = XPNETAPCTLSTAT_NOTCONN;

	return	XJSE_E_UNKNOWN;
#endif	//XTPF_PSP
}
示例#2
0
int connectApctl( int config )
{
	int stat_last = -1;
	int ret = sceNetApctlConnect( config );
	while ( ret >= 0 )
	{
		int stat;
		ret = sceNetApctlGetState( &stat );
		if ( ret < 0 )
			break;
		if ( stat > stat_last )
		{
			log( "connection state %d\n", stat );
			stat_last = stat;
		}
		if ( stat < stat_last )
		{
			ret = -1;
			break;
		}
		if ( stat == 4 )
			break;
		sceKernelDelayThread( 50000 );
	}
	if ( ret < 0 )
	{
		log( "Error connecting to entry %d (0x%08x)\n", config, ret );
		return ret;
	}
	return 0;
}
示例#3
0
/* Connect to an access point */
int JNetwork::connect_to_apctl(int config)
{
#ifdef NETWORK_SUPPORT
    int err;
    int stateLast = -1;
    char buffer[4096];

    /* Connect using the first profile */
    err = sceNetApctlConnect(config);
    if (err != 0)
    {
        sprintf(buffer, "JGE: sceNetApctlConnect returns %08X", err);
        printf(buffer);
        printf("\n");
        error = buffer;
        return 0;
    }

    sprintf(buffer,"JGE: Connecting...");
    printf(buffer);
    printf("\n");
    error = buffer;
    while (1)
    {
        int state;
        err = sceNetApctlGetState(&state);
        if (err != 0)
        {
            sprintf(buffer,"JGE: sceNetApctlGetState returns $%x", err);
            printf(buffer);
            printf("\n");
            error = buffer;
            break;
        }
        if (state > stateLast)
        {
            sprintf(buffer, "  connection state %d of 4", state);
            printf(buffer);
            printf("\n");
            error = buffer;
            stateLast = state;
        }
        if (state == 4) {
            connected_to_ap = 1;
            break;  // connected with static IP
        }
        // wait a little before polling again
        sceKernelDelayThread(50*1000); // 50ms
    }
    printf("JGE: Connected!\n");

    if(err != 0)
    {
        return 0;
    }
#endif
    return 1;
}
示例#4
0
int disconnectApctl()
{
	int stat = 1;
	int ret = sceNetApctlDisconnect();
	while( stat && ret >= 0 )
	{
		ret = sceNetApctlGetState( &stat );
		sceKernelDelayThread( 50000 );
	}
	return 0;
}
示例#5
0
文件: netcomm.c 项目: raphot/openbor
/* Select && Connect to an Access Point */
int selectAccessPoint(int selected)
{
	int err;
	int stateLast = -1;
	int state;

	/* Connect using the first profile */
	err = sceNetApctlConnect(selected);
	if (err != 0)
	{
		writeToLogFile("Error, sceNetApctlConnect returns %08X\n", err);
		wifiError = 1;
		return -1;
	}

	//printf("Connecting...\n");
	while(1)
	{
		err = sceNetApctlGetState(&state);
		if(err != 0)
		{
			writeToLogFile("sceNetApctlGetState returns $%x\n", err);
			wifiError = 1;
			break;
		}
		if(state > stateLast)
		{
			//printf("Connection state %d of 4\n", state);
			stateLast = state;
		}
		/* Connected with IP Address*/
		if(state == 4) break;
		/* Wait 50 ms before polling again */
		sceKernelDelayThread(50*1000);
	}
	/* Now obtain IP Address */
	while(1)
	{
		if(sceNetApctlGetInfo(8, AccessPoints[selected-1].pInfo) == 0)
		{
			AccessPoints[selected-1].color = DARK_GREEN;
			addPSP("SamuraiX", "11:11:11:11", "22:22:22:22", AccessPoints[selected-1].pInfo->ip, 5060, "88:88:88:88", "99:99:99:99", AccessPoints[selected-1].pInfo->ip, 5060, 0);
			break;
		}
		sceKernelDelayThread(1000 * 1000);
	}
	if(err != 0)
	{
		wifiError = 1;
		return -1;
	}
	return 1;
}
示例#6
0
int CSocket::socketSystemInit()
{
//	errorOut("debuglog.txt", ":: Initializing socket system...");
#if defined(_WIN32) || defined(_WIN64)
	WORD wVersionRequested;
	WSADATA wsaData;
	int err;

	wVersionRequested = MAKEWORD(2, 2);

	err = WSAStartup(wVersionRequested, &wsaData);
	if (err != 0)
	{
		SLOG("Failed to initialize winsocks!\n");
		return 1;
	}

	if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
	{
		SLOG("Failed to initialize winsocks!  Wasn't version 2.2!\n");
		WSACleanup();
		return 1;
	}
#elif defined(PSPSDK)
	if (sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON) < 0)
		return -1;
	if (sceUtilityLoadNetModule(PSP_NET_MODULE_INET) < 0)
		return -1;
	if (pspSdkInetInit() != 0)
		return -2; // false
	if (sceNetApctlConnect(1) != 0)
		return -3;

	while (true)
	{
		int state = 0;
		if (sceNetApctlGetState(&state) != 0)
			return -3;
		if (state == 4)
			break;

		sceKernelDelayThread(1000 * 50); // 50ms
	}
#endif

	CSocket::was_initiated = 1;
	return 0;
}
示例#7
0
			/* Connect to an access point */
			int connect_to_apctl(int config)
			{
				int err;
				int stateLast = -1;
				int timeout = 0;

				/* Connect using the first profile */
				err = sceNetApctlConnect(config);
				if (err != 0)
				{
					return 0;
				}

				while (1)
				{
					int state;
					err = sceNetApctlGetState(&state);
					if (err != 0)
					{
						break;
					}
					if (state > stateLast)
					{
						stateLast = state;
						timeout = 0;
					}
					if (state == 4)
						break;  // connected with static IP

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

					timeout++;
					if(timeout > 200)
					{
						Con_Printf("Timeout connecting to access point. State=%d\n", state);
						return 0;
					}
				}

				if(err != 0)
				{
					return 0;
				}

				return 1;
			}
示例#8
0
static PyObject* get_apctl_state(PyObject *self, PyObject *args)
{
    int state, ret;

    if (!PyArg_ParseTuple(args, ""))
       return NULL;

    Py_BEGIN_ALLOW_THREADS
       ret = sceNetApctlGetState(&state);
    Py_END_ALLOW_THREADS

    if (ret)
    {
       PyErr_Format(net_error, "sceNetApctlGetState returned 0x%08x", ret);
       return NULL;
    }

    return Py_BuildValue("i", state);
}
示例#9
0
文件: init.c 项目: BackupGGCode/aemu
/**
 * Initialize Networking Components for Adhocctl Emulator
 * @param adhoc_id Game Product Code
 * @param server_ip Server IP
 * @return 0 on success or... -1
 */
int _initNetwork(const SceNetAdhocctlAdhocId * adhoc_id, const char * server_ip)
{
	// WLAN Switch Check
	if(sceWlanGetSwitchState() == 1)
	{
		// Initialize Access Point Control
		if(sceNetApctlInit(0x1800, 0x30) == 0)
		{
			// Attempt Counter
			int attemptmax = 10;
			
			// Attempt Number
			int attempt = 0;
			
			// Attempt Connection Setup
			for(; attempt < attemptmax; attempt++)
			{
				// Start Connection
				if(sceNetApctlConnect(_hotspot) == 0)
				{
					// Wait for Connection
					int statebefore = 0;
					int state = 0; while(state != 4)
					{
						// Query State
						int getstate = sceNetApctlGetState(&state);
						
						// Log State Change
						if(statebefore != state) printk("New Connection State: %d\n", state);					
						
						// Query Success
						if(getstate == 0 && state != 4)
						{
							// Wait for Retry
							sceKernelDelayThread(1000000);
						}
						
						// Query Error
						else break;
						
						// Save Before State
						statebefore = state;
					}
					
					// Connected
					if(state == 4)
					{
						// Create Friend Finder Socket
						int socket = sceNetInetSocket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
						
						// Created Socket
						if(socket > 0)
						{
							// Enable Port Re-use
							sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEADDR, &_one, sizeof(_one));
							sceNetInetSetsockopt(socket, SOL_SOCKET, SO_REUSEPORT, &_one, sizeof(_one));
							
							// Apply Receive Timeout Settings to Socket
							// uint32_t timeout = ADHOCCTL_RECV_TIMEOUT;
							// sceNetInetSetsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
							
							// Server IP
							uint32_t ip = 0;
							
							// Initialize DNS Resolver
							if(sceNetResolverInit() == 0)
							{
								// Create DNS Resolver
								unsigned char rbuf[512]; int rid = 0;
								if(sceNetResolverCreate(&rid, rbuf, sizeof(rbuf)) == 0)
								{
									// Resolve Domain
									if(sceNetResolverStartNtoA(rid, server_ip, &ip, 500000, 2) != 0)
									{
										// Attempt IP Conversion
										sceNetInetInetAton(server_ip, &ip);
									}
									
									// Delete DNS Resolver
									sceNetResolverDelete(rid);
								}
								
								// Shutdown DNS Resolver
								sceNetResolverTerm();
							}
							
							// Prepare Server Address
							SceNetInetSockaddrIn addr;
							addr.sin_len = sizeof(addr);
							addr.sin_family = AF_INET;
							addr.sin_addr = ip;
							addr.sin_port = sceNetHtons(ADHOCCTL_METAPORT);
							
							// Connect to Server
							if(sceNetInetConnect(socket, (SceNetInetSockaddr *)&addr, sizeof(addr)) == 0)
							{
								// Save Meta Socket
								_metasocket = socket;
								
								// Save Product Code
								_product_code = *adhoc_id;
								
								// Clear Event Handler
								memset(_event_handler, 0, sizeof(_event_handler[0]) * ADHOCCTL_MAX_HANDLER);
								memset(_event_args, 0, sizeof(_event_args[0]) * ADHOCCTL_MAX_HANDLER);
								
								// Clear Internal Control Status
								memset(&_parameter, 0, sizeof(_parameter));
								
								// Read PSP Player Name
								sceUtilityGetSystemParamString(PSP_SYSTEMPARAM_ID_STRING_NICKNAME, (char *)_parameter.nickname.data, ADHOCCTL_NICKNAME_LEN);
								
								// Read Adhoc Channel
								sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_ADHOC_CHANNEL, &_parameter.channel);
								
								// Fake Channel Number 1 on Automatic Channel
								if(_parameter.channel == 0) _parameter.channel = 1;
								
								// Read PSP MAC Address
								sceWlanGetEtherAddr((void *)&_parameter.bssid.mac_addr.data);
								
								// Prepare Login Packet
								SceNetAdhocctlLoginPacketC2S packet;
								
								// Set Packet Opcode
								packet.base.opcode = OPCODE_LOGIN;
								
								// Set MAC Address
								packet.mac = _parameter.bssid.mac_addr;
								
								// Set Nickname
								packet.name = _parameter.nickname;
								
								// Set Game Product ID
								memcpy(packet.game.data, adhoc_id->data, ADHOCCTL_ADHOCID_LEN);
								
								// Acquire Network Layer Lock
								_acquireNetworkLock();
								
								// Send Login Packet
								sceNetInetSend(_metasocket, &packet, sizeof(packet), INET_MSG_DONTWAIT);
								
								// Free Network Layer Lock
								_freeNetworkLock();
								
								// Load UPNP Library
								_upnp_uid = sceKernelLoadModule("ms0:/kd/pspnet_miniupnc.prx", 0, NULL);
								
								// Start UPNP Library
								int status = 0; sceKernelStartModule(_upnp_uid, 0, NULL, &status, NULL);
								
								// Return Success
								return 0;
							}
							
							// Delete Socket
							sceNetInetClose(socket);
						}
						
						// Close Hotspot Connection
						sceNetApctlDisconnect();
					}
				}
			}
			
			// Terminate Access Point Control
			sceNetApctlTerm();	
		}
	}
	
	// Generic Error
	return -1;
}
示例#10
0
static PyObject* connect_to_apctl(PyObject *self, PyObject *args, PyObject *kwargs)
{
    int err, config = 1;
    int stateLast = -1;
    int timeout = -1;
    PyObject *callback = NULL, *ret;

    time_t started;

    static char* kwids[] = { "config", "callback", "timeout", NULL };

    if (PyErr_CheckSignals())
       return NULL;

    time(&started);

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iOi", kwids,
                                     &config, &callback, &timeout))
       return NULL;

    if (callback)
    {
       if (!PyCallable_Check(callback))
       {
          PyErr_SetString(PyExc_TypeError, "callback must be callable");
          return NULL;
       }
    }

    Py_BEGIN_ALLOW_THREADS
       err = sceNetApctlConnect(config);
    Py_END_ALLOW_THREADS

    if (err != 0)
    {
       PyErr_Format(net_error, "sceNetApctlConnect returns %08x\n", err);
       return NULL;
    }

    while (1)
    {
       int state;

       if (PyErr_CheckSignals())
          return NULL;

       Py_BEGIN_ALLOW_THREADS
          err = sceNetApctlGetState(&state);
       Py_END_ALLOW_THREADS

       if (err != 0)
       {
          PyErr_Format(net_error, "sceNetApctlGetState returns %08x\n", err);
          return NULL;
       }

       if (state > stateLast)
       {
          if (callback)
          {
             ret = PyObject_CallFunction(callback, "i", state);
             if (!ret)
                return NULL;
             Py_XDECREF(ret);
          }

          stateLast = state;
       }

       if (state == 4)
          break;  // connected with static IP

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

       if (timeout > 0)
       {
          time_t now;

          time(&now);
          if ((int)(now - started) >= timeout)
          {
             PyErr_SetString(net_error, "Timeout while trying to connect");
             return NULL;
          }
       }
    }

    if (callback)
    {
       ret = PyObject_CallFunction(callback, "i", -1);
       if (!ret)
          return NULL;
       Py_XDECREF(ret);
    }

    Py_INCREF(Py_None);
    return Py_None;
}
示例#11
0
void WiFiManager::Connect()
{
	if (IsConnected())
		return;
	if (!sceWlanGetSwitchState())
	{
		LCD::PrintMessage("WiFi not enabled");
		return;
	}
	
	Console::Print("Initializing WiFi...");
	int err = WiFiManager::Init();
	if (err != 0)
 	{
 		Console::Print("Error, bailing out...");
 		Terminate();
 		return;
 	}
 	
 	#define MAX_PICK 10
    struct
    {
        int index;
        char name[64];
    } picks[MAX_PICK];
    int pick_count = 0;

    int iNetIndex;
    for (iNetIndex = 1; iNetIndex < 100; iNetIndex++) // skip the 0th connection
    {
        if (sceUtilityCheckNetParam(iNetIndex) != 0)
            break;  // no more
        sceUtilityGetNetParam(iNetIndex, 0, picks[pick_count].name);
        picks[pick_count].index = iNetIndex;
        pick_count++;
        if (pick_count >= MAX_PICK)
            break;  // no more room
    }
    if (pick_count == 0)
    {
        Console::Print("No connections");
        LCD::PrintMessage("WIFI: No connections");
        Terminate();
        return;
    }
    Console::Print("Found connections");
    
    int connectionConfig = picks[0].index;


    // Connect
    err = sceNetApctlConnect(connectionConfig);
    if (err != 0)
    {
       Console::Print("sceNetApctlConnect returns error");
       Terminate();
       return;
    }

    // Report status while waiting for connection to access point
    int stateLast = -1;
    Console::Print("Connecting...");
    while (1)
    {
        int state;
        err = sceNetApctlGetState(&state);
        if (err != 0)
        {
            Console::Print("sceNetApctlGetState returns error");
				return;
        }
        if (state > stateLast)
        {
            Console::Print("Next state...");
            stateLast = state;
        }
        if (state == 4)
            break;  // connected with static IP
        sceKernelDelayThread(50*1000); // 50ms
    }
    Console::Print("Connected!");
    LCD::PrintMessage("WIFI Connected");
    _connected = true;
}