Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pspkernel.h>
- #include <psputility.h>
- #include <pspdebug.h>
- #include <pspsdk.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <pspnet.h>
- #include <pspnet_inet.h>
- #include <pspnet_apctl.h>
- #include <pspnet_resolver.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <sys/select.h>
- #include <errno.h>
- #define printf pspDebugScreenPrintf
- #define MODULE_NAME "NetSample"
- #define HELLO_MSG "Hello there. Type away.\r\n"
- PSP_MODULE_INFO(MODULE_NAME, PSP_MODULE_USER, 1, 1);
- PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER | THREAD_ATTR_VFPU);
- int exit_callback(int arg1, int arg2, void *common);
- int CallbackThread(SceSize args, void *argp);
- int SetupCallbacks(void);
- #define SERVER_PORT 4299
- int make_socket(uint16_t port);
- void start_server(const char *szIpAddr);
- int connect_to_apctl(int config);
- void list_netconfigs();
- void Handler(
- int prev_state,
- int new_state,
- int event,
- int error_code,
- void *arg
- )
- {
- printf("Handler:\n\tprev_state: %i\n\tnew_state: %i\n\tevent: %i\n\terror_code: %i\n", prev_state, new_state, event, error_code);
- }
- int main(int argc, char **argv)
- {
- SetupCallbacks();
- pspDebugScreenInit();
- if(sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON) < 0 ||
- sceUtilityLoadNetModule(PSP_NET_MODULE_INET) < 0) {
- printf("Error, could not load inet modules\n");
- sceKernelSleepThread();
- }
- int err;
- err = sceNetInit(128*1024, 42, 4*1024, 42, 4*1024);
- printf("sceNetInit returns: %i\n", err);
- err = sceNetInetInit();
- printf("sceNetInetInit returns: %i\n", err);
- err = sceNetApctlInit(0x8000, 48);
- printf("sceNetApctlInit returns: %i\n", err);
- sceNetApctlAddHandler(Handler, NULL);
- list_netconfigs();
- while (connect_to_apctl(1) == 0) sceKernelDelayThread(1000*1000);
- union SceNetApctlInfo info_ip;
- if (sceNetApctlGetInfo(PSP_NET_APCTL_INFO_IP, &info_ip) != 0)
- strcpy(info_ip.ip, "unknown IP");
- union SceNetApctlInfo info_ssid;
- if (sceNetApctlGetInfo(PSP_NET_APCTL_INFO_SSID, &info_ssid) != 0)
- strcpy(info_ssid.ip, "unknown SSID");
- printf("SSID: %s\n", info_ssid.ssid);
- start_server(info_ip.ip);
- sceKernelSleepThread();
- return 0;
- }
- int make_socket(uint16_t port)
- {
- int sock;
- int ret;
- struct sockaddr_in name;
- sock = socket(PF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- return -1;
- }
- name.sin_family = AF_INET;
- name.sin_port = htons(port);
- name.sin_addr.s_addr = htonl(INADDR_ANY);
- ret = bind(sock, (struct sockaddr *) &name, sizeof(name));
- if (ret < 0) {
- return -1;
- }
- return sock;
- }
- /* Start a simple tcp echo server */
- void start_server(const char *szIpAddr)
- {
- int ret;
- int sock;
- int new = -1;
- struct sockaddr_in client;
- size_t size;
- int readbytes;
- char data[1024];
- fd_set set;
- fd_set setsave;
- /* Create a socket for listening */
- sock = make_socket(SERVER_PORT);
- if (sock < 0) {
- printf("Error creating server socket\n");
- return;
- }
- ret = listen(sock, 1);
- if (ret < 0) {
- printf("Error calling listen\n");
- return;
- }
- printf("Listening for connections ip %s port %d\n", szIpAddr, SERVER_PORT);
- FD_ZERO(&set);
- FD_SET(sock, &set);
- setsave = set;
- while (1) {
- int i;
- set = setsave;
- if(select(FD_SETSIZE, &set, NULL, NULL, NULL) < 0) {
- printf("select error\n");
- return;
- }
- for (i = 0; i < FD_SETSIZE; i++) {
- if (FD_ISSET(i, &set)) {
- int val = i;
- if (val == sock) {
- new = accept(sock, (struct sockaddr *) &client, &size);
- if(new < 0) {
- printf("Error in accept %s\n", strerror(errno));
- close(sock);
- return;
- }
- printf("New connection %d from %s:%d\n", val,
- inet_ntoa(client.sin_addr),
- ntohs(client.sin_port));
- write(new, HELLO_MSG, strlen(HELLO_MSG));
- FD_SET(new, &setsave);
- } else {
- readbytes = read(val, data, sizeof(data));
- if(readbytes <= 0) {
- printf("Socket %d closed\n", val);
- FD_CLR(val, &setsave);
- close(val);
- } else {
- write(val, data, readbytes);
- printf("%.*s", readbytes, data);
- }
- }
- }
- }
- }
- close(sock);
- }
- /* Connect to an access point */
- int connect_to_apctl(int config)
- {
- int err;
- int stateLast = -1;
- printf("Before connect\n");
- /* Connect using the first profile */
- err = sceNetApctlConnect(config);
- if (err != 0) {
- printf(MODULE_NAME ": sceNetApctlConnect returns %08X\n", err);
- return 0;
- }
- printf("After connect\n");
- printf(MODULE_NAME ": Connecting...\n");
- while (1) {
- int state;
- err = sceNetApctlGetState(&state);
- if (err != 0) {
- printf(MODULE_NAME ": sceNetApctlGetState returns $%x\n", err);
- break;
- }
- if (state > stateLast) {
- printf(" connection state %d of 4\n", state);
- stateLast = state;
- }
- if (state == 4)
- break; // connected with static IP
- // wait a little before polling again
- sceKernelDelayThread(50*1000); // 50ms
- }
- printf(MODULE_NAME ": Connected!\n");
- if(err != 0) {
- return 0;
- }
- return 1;
- }
- void list_netconfigs()
- {
- #define MAX_CONFIG 10
- int i;
- for (i = 1; i <= MAX_CONFIG; ++i) {
- if (sceUtilityCheckNetParam(i) == 0) {
- printf("->Configuration %i\n", i);
- netData data;
- sceUtilityGetNetParam(i, PSP_NETPARAM_NAME, &data);
- printf("\tNAME: %s\n", data.asString);
- sceUtilityGetNetParam(i, PSP_NETPARAM_SSID, &data);
- printf("\tSSID: %s\n", data.asString);
- sceUtilityGetNetParam(i, PSP_NETPARAM_SECURE, &data);
- printf("\tSECURE: %s\n", data.asUint==2?"WEP":(data.asUint==3?"WPA":"NONE"));
- }
- }
- }
- /* Exit callback */
- int exit_callback(int arg1, int arg2, void *common)
- {
- sceKernelExitGame();
- return 0;
- }
- /* Callback thread */
- int CallbackThread(SceSize args, void *argp)
- {
- int cbid;
- cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
- sceKernelRegisterExitCallback(cbid);
- sceKernelSleepThreadCB();
- return 0;
- }
- /* Sets up the callback thread and returns its thread id */
- int SetupCallbacks(void)
- {
- int thid = 0;
- thid = sceKernelCreateThread("update_thread", CallbackThread,
- 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0);
- if (thid >= 0) {
- sceKernelStartThread(thid, 0, 0);
- }
- return thid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement