Updated httpc code and added an example.

This commit is contained in:
yellows8 2014-11-22 00:43:39 -05:00
parent 70e1bb7486
commit 240ab44f9c
5 changed files with 506 additions and 2 deletions

170
examples/http/Makefile Normal file
View File

@ -0,0 +1,170 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
TOPDIR ?= $(CURDIR)
include $(DEVKITARM)/3ds_rules
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# DATA is a list of directories containing data files
# INCLUDES is a list of directories containing header files
#
# NO_SMDH: if set to anything, no SMDH file is generated.
# APP_TITLE is the name of the app stored in the SMDH file (Optional)
# APP_DESCRIPTION is the description of the app stored in the SMDH file (Optional)
# APP_AUTHOR is the author of the app stored in the SMDH file (Optional)
# ICON is the filename of the icon (.png), relative to the project folder.
# If not set, it attempts to use one of the following (in this order):
# - <Project name>.png
# - icon.png
# - <libctru folder>/default_icon.png
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
DATA := data
INCLUDES := include
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=softfp
CFLAGS := -g -Wall -O2 -mword-relocations \
-fomit-frame-pointer -ffast-math \
$(ARCH)
CFLAGS += $(INCLUDE) -DARM11 -D_3DS
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
ASFLAGS := -g $(ARCH)
LDFLAGS = -specs=3dsx.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
LIBS := -lctru -lm
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib
#---------------------------------------------------------------------------------
LIBDIRS := $(CTRULIB)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(TARGET)
export TOPDIR := $(CURDIR)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES := $(addsuffix .o,$(BINFILES)) \
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
ifeq ($(strip $(ICON)),)
icons := $(wildcard *.png)
ifneq (,$(findstring $(TARGET).png,$(icons)))
export APP_ICON := $(TOPDIR)/$(TARGET).png
else
ifneq (,$(findstring icon.png,$(icons)))
export APP_ICON := $(TOPDIR)/icon.png
endif
endif
else
export APP_ICON := $(TOPDIR)/$(ICON)
endif
.PHONY: $(BUILD) clean all
#---------------------------------------------------------------------------------
all: $(BUILD)
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).3dsx $(OUTPUT).smdh $(TARGET).elf
#---------------------------------------------------------------------------------
else
DEPENDS := $(OFILES:.o=.d)
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
ifeq ($(strip $(NO_SMDH)),)
.PHONY: all
all : $(OUTPUT).3dsx $(OUTPUT).smdh
endif
$(OUTPUT).3dsx : $(OUTPUT).elf
$(OUTPUT).elf : $(OFILES)
#---------------------------------------------------------------------------------
# you need a rule like this for each extension you use as binary data
#---------------------------------------------------------------------------------
%.bin.o : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
# WARNING: This is not the right way to do this! TODO: Do it right!
#---------------------------------------------------------------------------------
%.vsh.o : %.vsh
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@python $(AEMSTRO)/aemstro_as.py $< ../$(notdir $<).shbin
@bin2s ../$(notdir $<).shbin | $(PREFIX)as -o $@
@echo "extern const u8" `(echo $(notdir $<).shbin | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"_end[];" > `(echo $(notdir $<).shbin | tr . _)`.h
@echo "extern const u8" `(echo $(notdir $<).shbin | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`"[];" >> `(echo $(notdir $<).shbin | tr . _)`.h
@echo "extern const u32" `(echo $(notdir $<).shbin | sed -e 's/^\([0-9]\)/_\1/' | tr . _)`_size";" >> `(echo $(notdir $<).shbin | tr . _)`.h
@rm ../$(notdir $<).shbin
-include $(DEPENDS)
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------

4
examples/http/README.md Normal file
View File

@ -0,0 +1,4 @@
# http
This is an example for using HTTPC. This downloads a raw image for displaying on the top-screen. The URL used here is a LAN-only one, hence this URL must be changed before building+running this example.

128
examples/http/source/main.c Normal file
View File

@ -0,0 +1,128 @@
#include <3ds.h>
Result http_download(httpcContext *context)//This error handling needs updated with proper text printing once ctrulib itself supports that.
{
Result ret=0;
u8* framebuf_top, *framebuf_bottom;
u32 statuscode=0;
u32 size=0, contentsize=0;
u8 *buf;
framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
memset(framebuf_bottom, 0x40, 240*320*3);
gfxFlushBuffers();
gfxSwapBuffers();
framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
memset(framebuf_bottom, 0x40, 240*320*3);
gfxFlushBuffers();
gfxSwapBuffers();
gspWaitForVBlank();
ret = httpcBeginRequest(context);
if(ret!=0)return ret;
ret = httpcGetResponseStatusCode(context, &statuscode, 0);
if(ret!=0)return ret;
if(statuscode!=200)return -2;
ret=httpcGetDownloadSizeState(context, NULL, &contentsize);
if(ret!=0)return ret;
buf = (u8*)malloc(contentsize);
if(buf==NULL)return -1;
memset(buf, 0, contentsize);
framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
memset(framebuf_bottom, 0xc0, 240*320*3);
gfxFlushBuffers();
gfxSwapBuffers();
framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
memset(framebuf_bottom, 0xc0, 240*320*3);
gfxFlushBuffers();
gfxSwapBuffers();
gspWaitForVBlank();
framebuf_top = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
ret = httpcDownloadData(context, buf, contentsize, NULL);
if(ret!=0)
{
free(buf);
return ret;
}
size = contentsize;
if(size>(240*400*3))size = 240*400*3;
memset(framebuf_bottom, 0xff, 240*320*3);
memcpy(framebuf_top, buf, size);
gfxFlushBuffers();
gfxSwapBuffers();
framebuf_top = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
memset(framebuf_bottom, 0xff, 240*320*3);
memcpy(framebuf_top, buf, size);
gfxFlushBuffers();
gfxSwapBuffers();
gspWaitForVBlank();
free(buf);
return 0;
}
int main()
{
Result ret=0;
httpcContext context;
// Initialize services
srvInit();
aptInit();
hidInit(NULL);
gfxInit();
//gfxSet3D(true); // uncomment if using stereoscopic 3D
httpcInit();
ret = httpcOpenContext(&context, "http://10.0.0.3/httpexample_rawimg.bin", 0);//Change this to your own URL.
if(ret==0)
{
ret=http_download(&context);
httpcCloseContext(&context);
}
// Main loop
while (aptMainLoop())
{
gspWaitForVBlank();
hidScanInput();
// Your code goes here
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break; // break in order to return to hbmenu
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
}
// Exit services
httpcExit();
gfxExit();
hidExit();
aptExit();
srvExit();
return 0;
}

View File

@ -1,5 +1,32 @@
#pragma once
typedef struct {
Handle servhandle;
u32 httphandle;
} httpcContext;
typedef enum{
HTTPCREQSTAT_INPROGRESS_REQSENT = 0x5,
HTTPCREQSTAT_DLREADY = 0x7
} httpcReqStatus;
#define HTTPC_RESULTCODE_DOWNLOADPENDING 0xd840a02b
Result httpcInit();
void httpcExit();
Result httpcOpenContext(httpcContext *context, char* url, u32 use_defaultproxy);//use_defaultproxy should be zero normally, unless you don't want HTTPC_SetProxyDefault() to be used automatically.
Result httpcCloseContext(httpcContext *context);
Result httpcBeginRequest(httpcContext *context);
Result httpcReceiveData(httpcContext *context, u8* buffer, u32 size);
Result httpcGetRequestState(httpcContext *context, httpcReqStatus* out);
Result httpcGetDownloadSizeState(httpcContext *context, u32* downloadedsize, u32* contentsize);
Result httpcGetResponseStatusCode(httpcContext *context, u32* out, u64 delay);//delay isn't used yet. This writes the HTTP status code from the server to out.
Result httpcDownloadData(httpcContext *context, u8* buffer, u32 size, u32 *downloadedsize);//The *entire* content must be downloaded before using httpcCloseContext(), otherwise httpcCloseContext() will hang.
//Using the below functions directly is not recommended, use the above functions. See also the http example.
Result HTTPC_Initialize(Handle handle);
Result HTTPC_InitializeConnectionSession(Handle handle, Handle contextHandle);
Result HTTPC_CreateContext(Handle handle, char* url, Handle* contextHandle);
@ -8,3 +35,7 @@ Result HTTPC_SetProxyDefault(Handle handle, Handle contextHandle);
Result HTTPC_AddRequestHeaderField(Handle handle, Handle contextHandle, char* name, char* value);
Result HTTPC_BeginRequest(Handle handle, Handle contextHandle);
Result HTTPC_ReceiveData(Handle handle, Handle contextHandle, u8* buffer, u32 size);
Result HTTPC_GetRequestState(Handle handle, Handle contextHandle, httpcReqStatus* out);
Result HTTPC_GetDownloadSizeState(Handle handle, Handle contextHandle, u32* downloadedsize, u32* contentsize);
Result HTTPC_GetResponseStatusCode(Handle handle, Handle contextHandle, u32* out);

View File

@ -1,13 +1,137 @@
#include <string.h>
#include <3ds.h>
Handle __httpc_servhandle = 0;
Result httpcInit()
{
Result ret=0;
if(__httpc_servhandle)return 0;
if((ret=srvGetServiceHandle(&__httpc_servhandle, "http:C")))*((u32*)0x500) = ret;//return ret;
//*((u32*)0x600) = __httpc_servhandle;
ret = HTTPC_Initialize(__httpc_servhandle);
if(ret!=0)*((u32*)0x400) = ret;//return ret;
return 0;
}
void httpcExit()
{
if(__httpc_servhandle==0)return;
svcCloseHandle(__httpc_servhandle);
}
Result httpcOpenContext(httpcContext *context, char* url, u32 use_defaultproxy)
{
Result ret=0;
ret = HTTPC_CreateContext(__httpc_servhandle, url, &context->httphandle);
if(ret!=0)*((u32*)0x100) = ret;//return ret;
ret = srvGetServiceHandle(&context->servhandle, "http:C");
if(ret!=0)*((u32*)0x104) = ret;//return ret;
ret = HTTPC_InitializeConnectionSession(context->servhandle, context->httphandle);
if(ret!=0)*((u32*)0x108) = ret;//return ret;
if(use_defaultproxy==0)return 0;
ret = HTTPC_SetProxyDefault(context->servhandle, context->httphandle);
if(ret!=0)*((u32*)0x10c) = ret;//return ret;
return 0;
}
Result httpcCloseContext(httpcContext *context)
{
Result ret=0;
ret = HTTPC_CloseContext(context->servhandle, context->httphandle);
svcCloseHandle(context->servhandle);
return ret;
}
Result httpcAddRequestHeaderField(httpcContext *context, char* name, char* value)
{
return HTTPC_AddRequestHeaderField(context->servhandle, context->httphandle, name, value);
}
Result httpcBeginRequest(httpcContext *context)
{
return HTTPC_BeginRequest(context->servhandle, context->httphandle);
}
Result httpcReceiveData(httpcContext *context, u8* buffer, u32 size)
{
return HTTPC_ReceiveData(context->servhandle, context->httphandle, buffer, size);
}
Result httpcGetRequestState(httpcContext *context, httpcReqStatus* out)
{
return HTTPC_GetRequestState(context->servhandle, context->httphandle, out);
}
Result httpcGetDownloadSizeState(httpcContext *context, u32* downloadedsize, u32* contentsize)
{
return HTTPC_GetDownloadSizeState(context->servhandle, context->httphandle, downloadedsize, contentsize);
}
Result httpcGetResponseStatusCode(httpcContext *context, u32* out, u64 delay)
{
return HTTPC_GetResponseStatusCode(context->servhandle, context->httphandle, out);
}
Result httpcDownloadData(httpcContext *context, u8* buffer, u32 size, u32 *downloadedsize)
{
Result ret=0;
u32 contentsize=0;
u32 pos=0, sz=0;
if(downloadedsize)*downloadedsize = 0;
ret=httpcGetDownloadSizeState(context, NULL, &contentsize);
if(ret!=0)return ret;
while(pos < size)
{
sz = size - pos;
ret=httpcReceiveData(context, &buffer[pos], sz);
if(ret==HTTPC_RESULTCODE_DOWNLOADPENDING)
{
ret=httpcGetDownloadSizeState(context, &pos, NULL);
if(ret!=0)return ret;
}
else if(ret!=0)
{
return ret;
}
else
{
pos+= sz;
}
if(downloadedsize)*downloadedsize = pos;
}
return 0;
}
Result HTTPC_Initialize(Handle handle)
{
u32* cmdbuf=getThreadCommandBuffer();
cmdbuf[0]=0x10044; //request header code
cmdbuf[1]=0x1000; //unk
cmdbuf[2]=0x20; //unk
cmdbuf[2]=0x20;//processID header, following word is set to processID by the arm11kernel.
cmdbuf[4]=0;
cmdbuf[5]=0;//Some sort of handle.
Result ret=0;
if((ret=svcSendSyncRequest(handle)))return ret;
@ -124,3 +248,50 @@ Result HTTPC_ReceiveData(Handle handle, Handle contextHandle, u8* buffer, u32 si
return cmdbuf[1];
}
Result HTTPC_GetRequestState(Handle handle, Handle contextHandle, httpcReqStatus* out)
{
u32* cmdbuf=getThreadCommandBuffer();
cmdbuf[0]=0x50040; //request header code
cmdbuf[1]=contextHandle;
Result ret=0;
if((ret=svcSendSyncRequest(handle)))return ret;
*out = cmdbuf[2];
return cmdbuf[1];
}
Result HTTPC_GetDownloadSizeState(Handle handle, Handle contextHandle, u32* downloadedsize, u32* contentsize)
{
u32* cmdbuf=getThreadCommandBuffer();
cmdbuf[0]=0x60040; //request header code
cmdbuf[1]=contextHandle;
Result ret=0;
if((ret=svcSendSyncRequest(handle)))return ret;
if(downloadedsize)*downloadedsize = cmdbuf[2];
if(contentsize)*contentsize = cmdbuf[3];
return cmdbuf[1];
}
Result HTTPC_GetResponseStatusCode(Handle handle, Handle contextHandle, u32* out)
{
u32* cmdbuf=getThreadCommandBuffer();
cmdbuf[0]=0x220040; //request header code
cmdbuf[1]=contextHandle;
Result ret=0;
if((ret=svcSendSyncRequest(handle)))return ret;
*out = cmdbuf[2];
return cmdbuf[1];
}