Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Comments

add libcurl example #72#95

Open
KorigamiK wants to merge 2 commits intopspdev:master from
KorigamiK:net_example
Open

add libcurl example #72 #95
KorigamiK wants to merge 2 commits intopspdev:master from
KorigamiK:net_example

Conversation

@KorigamiK
Copy link
Contributor

@KorigamiK KorigamiK commented Apr 20, 2025

Addressing #72

Copy link
Member

Thanks, I really appreciate the PR. I do feel it is a little more complex than it has to be for a basic example. Would it be okay if I changed it to only require curl, use pspdebug for output and slim it down to 2 files?

Copy link
Contributor Author

Sure, would be nicer for an example

sharkwouter reacted with thumbs up emoji

Copy link
Member

Okay, I played around with this a bit now. I kind of forgot that the network dialog needs libgu to be initialized. It also turns out the pkgconfig of curl is kinda broken, since you need to include mbedtls, mbedcrypto and mbedx509 manually.

I have this now, but it freezes because it cannot show the dialog.

main.c:

#include <curl/curl.h>
#include <strings.h>
#include <pspsdk.h>
#include <pspnet_inet.h>
#include <psputility.h>
#include <pspgu.h>
#include <pspnet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <pspdisplay.h>
#include <string.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <stdlib.h>
// PSP_MODULE_INFO is required
PSP_MODULE_INFO("Curl Example", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);
int loading = 0;
int running = 1;
size_t buffer_size = 1024;
size_t writeCallback(char *contents, size_t size, size_t nmemb, void *userp) {
 size_t write_size = size * nmemb;
 if (write_size > buffer_size) {
 write_size = buffer_size;
 }
 strncpy((char *) userp, (char *)contents, write_size);
 return write_size;
}
CURLcode getRequest(const char * url, char * buffer) {
 CURL *curl = curl_easy_init();
 if (!curl) {
 return CURLE_FAILED_INIT;
 }
 curl_easy_setopt(curl, CURLOPT_URL, url);
 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
 curl_easy_setopt(curl, CURLOPT_WRITEDATA, buffer);
 curl_easy_setopt(curl, CURLOPT_BUFFERSIZE, buffer_size);
 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
 curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.64.1");
 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, curl_slist_append(NULL, "Accept: text/plain"));
 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
 curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
 curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L);
 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 20L);
 curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_TRY);
 CURLcode res = curl_easy_perform(curl);
 curl_easy_cleanup(curl);
 return res;
}
char * getNewJoke() {
 char * response = calloc(sizeof(char), buffer_size);
 if (getRequest("https://icanhazdadjoke.com/", response) != CURLE_OK) {
 strcpy(response, "Failed to get joke :(");
 }
 return response;
}
int netDialog()
{
 int done = 0;
 pspUtilityNetconfData data;
 memset(&data, 0, sizeof(data));
 data.base.size = sizeof(data);
 data.base.language = PSP_SYSTEMPARAM_LANGUAGE_ENGLISH;
 data.base.buttonSwap = PSP_UTILITY_ACCEPT_CROSS;
 data.base.graphicsThread = 17;
 data.base.accessThread = 19;
 data.base.fontThread = 18;
 data.base.soundThread = 16;
 data.action = PSP_NETCONF_ACTION_CONNECTAP;
 struct pspUtilityNetconfAdhoc adhocparam;
 memset(&adhocparam, 0, sizeof(adhocparam));
 data.adhocparam = &adhocparam;
 sceUtilityNetconfInitStart(&data);
 while (!done)
 {
 sceDisplayWaitVblankStart();
 switch (sceUtilityNetconfGetStatus())
 {
 case PSP_UTILITY_DIALOG_NONE:
 break;
 case PSP_UTILITY_DIALOG_VISIBLE:
 sceUtilityNetconfUpdate(1);
 break;
 case PSP_UTILITY_DIALOG_QUIT:
 sceUtilityNetconfShutdownStart();
 break;
 case PSP_UTILITY_DIALOG_FINISHED:
 done = 1;
 break;
 default:
 break;
 }
 sceKernelDelayThread(10 * 1000); // 10 ms
 }
 return 1;
}
void startNetworking()
{
 sceUtilityLoadNetModule(PSP_NET_MODULE_COMMON);
 sceUtilityLoadNetModule(PSP_NET_MODULE_INET);
 sceNetInit(128 * 1024, 42, 4 * 1024, 42, 4 * 1024);
 sceNetInetInit();
 sceNetApctlInit(0x8000, 48);
 netDialog();
}
void stopNetworking()
{
 sceUtilityUnloadNetModule(PSP_NET_MODULE_INET);
 sceUtilityUnloadNetModule(PSP_NET_MODULE_COMMON);
 
 sceNetApctlTerm();
 sceNetInetTerm();
 sceNetTerm();
}
int exit_callback(int arg1, int arg2, void *common) {
 stopNetworking();
 sceKernelExitGame();
 return 0;
}
int callback_thread(SceSize args, void *argp) {
 int cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
 sceKernelRegisterExitCallback(cbid);
 sceKernelSleepThreadCB();
 return 0;
}
int setup_callbacks(void) {
 int thid = sceKernelCreateThread("update_thread", callback_thread, 0x11, 0xFA0, 0, 0);
 if(thid >= 0)
 sceKernelStartThread(thid, 0, 0);
 return thid;
}
int main(void) {
 // Use above functions to make exiting possible
 setup_callbacks();
 startNetworking();
 char * joke = getNewJoke();
 
 // Print the received joke! on a debug screen on a loop
 pspDebugScreenInit();
 pspDebugScreenClear();
 while(1) {
 pspDebugScreenSetXY(0, 0);
 pspDebugScreenPrintf("%s", joke);
 sceDisplayWaitVblankStart();
 }
 return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(curl-example)
add_executable(${PROJECT_NAME} main.c)
find_package(CURL REQUIRED)
include(FindPkgConfig)
pkg_search_module(CURL REQUIRED libcurl)
target_link_libraries(${PROJECT_NAME} PRIVATE
 ${CURL_LIBRARIES}
 mbedtls
 mbedcrypto
 mbedx509
 pspdebug
 pspdisplay
 pspge
 pspnet 
 pspnet_apctl
)
# Create an EBOOT.PBP file
create_pbp_file(
 TARGET ${PROJECT_NAME}
 ICON_PATH NULL
 BACKGROUND_PATH NULL
 PREVIEW_PATH NULL
 TITLE ${PROJECT_NAME}
 VERSION 01.00
)

Copy link
Contributor Author

Ah yeah, i know this because you need to initialise graphics first to show the dialog :sad:

Copy link
Contributor Author

I may be wrong here but I do believe it's not required to have PSP_MODULE_INFO in the source files because you can configure it through cmake. I think that's better for this.

Copy link
Member

The reason you didn't need PSP_MODULE_INFO was because SDL2 makes it not needed, but I'm trying not to use SDL2. Maybe I could write the debug text to a texture.

KorigamiK reacted with thumbs up emoji

Copy link
Member

Honestly, I don't think I can simplify this much further. We should probably just add a screenshot, remove the json file and slightly simplify the CMakeLists.txt.

Copy link
Member

I will get back to this and I'm really sorry I kept this laying around for so long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /