Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Created March 15, 2010 17:27
Show Gist options
  • Save Mon-Ouie/333077 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/333077 to your computer and use it in GitHub Desktop.
/*
Inits the wlan connection. You have to call this before calling the socket
function.
*/
VALUE Wlan_init(VALUE self) {
int err;
err = sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
if (err < 0)
rb_raise(rb_eRuntimeError, "Failled to load net module");
err = sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
if (err < 0)
rb_raise(rb_eRuntimeError, "Failled to load inet module");
err = sceNetInit(0x20000, 0x20, 0x1000, 0x20, 0x1000);
if (err < 0)
rb_raise(rb_eRuntimeError, "Failled to init net module");
err = sceNetInetInit();
if (err < 0)
rb_raise(rb_eRuntimeError, "Failled to init inet module");
err = sceNetResolverInit();
if (err < 0)
rb_raise(rb_eRuntimeError, "Failled to init resolver");
err = sceNetApctlInit(0x1400, 0x42);
if (err < 0)
rb_raise(rb_eRuntimeError, "Failled to init apctl");
return Qnil;
}
/*
call-seq: connect(access_point, timeout)
Connects to a given acces point. You can specify a timeout in seconds.
If the connection isn't established after the specified timeout,
a TimeoutError is raised.
*/
VALUE Wlan_connect(VALUE self, VALUE config, VALUE timeout) {
if (sceNetApctlConnect(FIX2INT(config)))
rb_raise(rb_eRuntimeError, "Failled to connect to acces point %d",
FIX2INT(config));
time_t startTime, currTime;
time(&startTime);
int last_state = -1;
while (true) {
time(&currTime);
if (currTime - startTime >= FIX2INT(timeout)) {
sceNetApctlDisconnect();
rb_raise(rb_eTimeoutError, "Connection timeouted after %d seconds",
FIX2INT(timeout));
break;
}
int state;
if (sceNetApctlGetState(&state)) {
rb_raise(rb_eRuntimeError,
"Error occured while getting connection state");
break;
}
if (state > last_state) {
last_state = state;
}
if (state == PSP_NET_APCTL_STATE_GOT_IP)
break;
sceKernelDelayThread(50 * 1000);
}
return Qnil;
}
/*
Ends a Wlan connection.
*/
VALUE Wlan_disconnect(VALUE self) {
sceNetApctlDisconnect();
return Qnil;
}
/*
Unlods net modules.
*/
VALUE Wlan_stop(VALUE self) {
sceNetApctlTerm();
sceUtilityUnloadNetModule(PSP_NET_MODULE_INET);
sceUtilityUnloadNetModule(PSP_NET_MODULE_COMMON);
return Qnil;
}
VALUE Wlan_configs(VALUE self) {
VALUE ret = rb_ary_new();
for (int i = 1; i < 20; ++i) {
if (sceUtilityCheckNetParam(i))
break;
VALUE entry = rb_ary_new();
netData data;
sceUtilityGetNetParam(i, PSP_NETPARAM_IP, &data);
rb_ary_push(entry, rb_str_new2(data.asString));
sceUtilityGetNetParam(i, PSP_NETPARAM_NAME, &data);
rb_ary_push(entry, rb_str_new2(data.asString));
rb_ary_push(ret, entry);
}
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment