working example
This commit is contained in:
commit
bdedf8d814
7 changed files with 300 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
build
|
||||
lib
|
134
Makefile
Normal file
134
Makefile
Normal file
|
@ -0,0 +1,134 @@
|
|||
#---------------------------------------------------------------------------------
|
||||
.SUFFIXES:
|
||||
#---------------------------------------------------------------------------------
|
||||
|
||||
ifeq ($(strip $(DEVKITARM)),)
|
||||
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
|
||||
endif
|
||||
|
||||
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
|
||||
#---------------------------------------------------------------------------------
|
||||
TARGET := $(shell basename $(CURDIR))
|
||||
BUILD := build
|
||||
SOURCES := source
|
||||
DATA := data
|
||||
INCLUDES := include
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# options for code generation
|
||||
#---------------------------------------------------------------------------------
|
||||
ARCH := -march=armv6k -mtune=mpcore -mfloat-abi=hard -mtp=soft
|
||||
|
||||
CFLAGS := -g -Wall -O2 -mword-relocations \
|
||||
-fomit-frame-pointer -ffunction-sections \
|
||||
$(ARCH)
|
||||
|
||||
CFLAGS += $(INCLUDE) -DARM11 -D_3DS
|
||||
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
|
||||
|
||||
ASFLAGS := -g $(ARCH)
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# list of directories containing libraries, this must be the top level containing
|
||||
# include and lib
|
||||
#---------------------------------------------------------------------------------
|
||||
LIBDIRS := $(CTRULIB) $(DEVKITPRO)/portlibs/3ds
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# 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)/lib/lib$(TARGET).a
|
||||
|
||||
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)
|
||||
|
||||
.PHONY: $(BUILD) clean all
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
|
||||
|
||||
all: $(BUILD)
|
||||
|
||||
ifeq ($(OS),Windows_NT)
|
||||
install:
|
||||
@cp -rf lib/* $(DEVKITPRO)/portlibs/3ds/lib/
|
||||
@cp -rf include/* $(DEVKITPRO)/portlibs/3ds/include/
|
||||
else
|
||||
install:
|
||||
@sudo cp -rf lib/* $(DEVKITPRO)/portlibs/3ds/lib/
|
||||
@sudo cp -rf include/* $(DEVKITPRO)/portlibs/3ds/include/
|
||||
endif
|
||||
|
||||
lib:
|
||||
@[ -d $@ ] || mkdir -p $@
|
||||
|
||||
$(BUILD): lib
|
||||
@[ -d $@ ] || mkdir -p $@
|
||||
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
clean:
|
||||
@echo clean ...
|
||||
@rm -fr $(BUILD) lib
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
else
|
||||
|
||||
DEPENDS := $(OFILES:.o=.d)
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
# main targets
|
||||
#---------------------------------------------------------------------------------
|
||||
$(OUTPUT) : $(OFILES)
|
||||
|
||||
#---------------------------------------------------------------------------------
|
||||
%.bin.o : %.bin
|
||||
#---------------------------------------------------------------------------------
|
||||
@echo $(notdir $<)
|
||||
@$(bin2o)
|
||||
|
||||
|
||||
-include $(DEPENDS)
|
||||
|
||||
#---------------------------------------------------------------------------------------
|
||||
endif
|
||||
#---------------------------------------------------------------------------------------
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
# template
|
||||
|
||||
This is a template for starting new 3DS library projects.
|
17
include/matrixclient.h
Normal file
17
include/matrixclient.h
Normal file
|
@ -0,0 +1,17 @@
|
|||
#ifndef _matrixclient_h_
|
||||
#define _matrixclient_h_
|
||||
|
||||
#include <string>
|
||||
#include <3ds.h>
|
||||
#include <jansson.h>
|
||||
|
||||
class MatrixClient {
|
||||
private:
|
||||
std::string hsUrl;
|
||||
std::string token;
|
||||
public:
|
||||
MatrixClient(std::string homeserverUrl, std::string matrixToken);
|
||||
Result doRequest(json_t* content, HTTPC_RequestMethod method, std::string path, json_t* body = NULL);
|
||||
};
|
||||
|
||||
#endif // _matrixclient_h_
|
6
include/templatelib.h
Normal file
6
include/templatelib.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef _templatelib_h_
|
||||
#define _templatelib_h_
|
||||
|
||||
int myLibFunction();
|
||||
|
||||
#endif // _templatelib_h_
|
132
source/matrixclient.cpp
Normal file
132
source/matrixclient.cpp
Normal file
|
@ -0,0 +1,132 @@
|
|||
#include "../include/matrixclient.h"
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <3ds.h>
|
||||
#include <jansson.h>
|
||||
|
||||
MatrixClient::MatrixClient(std::string homeserverUrl, std::string matrixToken) {
|
||||
hsUrl = homeserverUrl;
|
||||
token = matrixToken;
|
||||
}
|
||||
|
||||
Result MatrixClient::doRequest(json_t* content, HTTPC_RequestMethod method, std::string path, json_t* body) {
|
||||
std::string url = hsUrl + path;
|
||||
Result ret = 0;
|
||||
httpcContext context;
|
||||
std::string newUrl = "";
|
||||
u32 statusCode = 0;
|
||||
u32 contentSize = 0, readsize = 0, size = 0;
|
||||
u8* buf, *lastbuf;
|
||||
do {
|
||||
printf("Opening Request\n");
|
||||
printf(url.c_str());
|
||||
printf("\n");
|
||||
ret = httpcOpenContext(&context, method, url.c_str(), 1);
|
||||
printf("return from httpcOpenContext: %" PRId32 "\n",ret);
|
||||
|
||||
ret = httpcSetSSLOpt(&context, SSLCOPT_DisableVerify);
|
||||
printf("return from httpcSetSSLOpt: %" PRId32 "\n",ret);
|
||||
|
||||
ret = httpcSetKeepAlive(&context, HTTPC_KEEPALIVE_ENABLED);
|
||||
printf("return from httpcSetKeepAlive: %" PRId32 "\n",ret);
|
||||
|
||||
// ret = httpcAddRequestHeaderField(&context, "User-Agent", "httpc-example/1.0.0");
|
||||
// printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
|
||||
ret = httpcAddRequestHeaderField(&context, "Authorization", ("Bearer " + token).c_str());
|
||||
printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
|
||||
ret = httpcAddRequestHeaderField(&context, "Connection", "Keep-Alive");
|
||||
printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
|
||||
if (body) {
|
||||
// we have a body to send!
|
||||
ret = httpcAddRequestHeaderField(&context, "Content-Type", "application/json");
|
||||
printf("return from httpcAddRequestHeaderField: %" PRId32 "\n",ret);
|
||||
}
|
||||
|
||||
printf("Do Request\n");
|
||||
ret = httpcBeginRequest(&context);
|
||||
printf("return from httpcBeginRequest: %" PRId32 "\n",ret);
|
||||
if (ret != 0) {
|
||||
printf("PANIC!!!\n");
|
||||
httpcCloseContext(&context);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = httpcGetResponseStatusCode(&context, &statusCode);
|
||||
printf("status code: %" PRId32 "\n", statusCode);
|
||||
if(ret!=0){
|
||||
httpcCloseContext(&context);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((statusCode >= 301 && statusCode <= 303) || (statusCode >= 307 && statusCode <= 308)) {
|
||||
char newUrl[0x100];
|
||||
ret = httpcGetResponseHeader(&context, "Location", newUrl, 0x100);
|
||||
url = std::string(newUrl);
|
||||
}
|
||||
} while ((statusCode >= 301 && statusCode <= 303) || (statusCode >= 307 && statusCode <= 308));
|
||||
|
||||
if (statusCode < 200 || statusCode > 299) {
|
||||
printf("Non-200 status code: %" PRId32 "\n", statusCode);
|
||||
httpcCloseContext(&context);
|
||||
return -2;
|
||||
}
|
||||
|
||||
ret = httpcGetDownloadSizeState(&context, NULL, &contentSize);
|
||||
if (ret != 0) {
|
||||
httpcCloseContext(&context);
|
||||
return ret;
|
||||
}
|
||||
|
||||
printf("reported size: %" PRId32 "\n", contentSize);
|
||||
// Start with a single page buffer
|
||||
buf = (u8*)malloc(0x1000);
|
||||
if (buf == NULL) {
|
||||
httpcCloseContext(&context);
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
// This download loop resizes the buffer as data is read.
|
||||
ret = httpcDownloadData(&context, buf+size, 0x1000, &readsize);
|
||||
size += readsize;
|
||||
if (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING) {
|
||||
lastbuf = buf; // Save the old pointer, in case realloc() fails.
|
||||
buf = (u8*)realloc(buf, size + 0x1000);
|
||||
if (buf == NULL) {
|
||||
httpcCloseContext(&context);
|
||||
free(lastbuf);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
} while (ret == (s32)HTTPC_RESULTCODE_DOWNLOADPENDING);
|
||||
printf("return from httpcDownloadData: %" PRId32 "\n",ret);
|
||||
|
||||
if (ret != 0) {
|
||||
httpcCloseContext(&context);
|
||||
free(buf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Resize the buffer back down to our actual final size
|
||||
lastbuf = buf;
|
||||
buf = (u8*)realloc(buf, size);
|
||||
if (buf == NULL) { // realloc() failed.
|
||||
printf("WAT?!\n");
|
||||
httpcCloseContext(&context);
|
||||
free(lastbuf);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("downloaded size: %" PRId32 "\n",size);
|
||||
printf((const char*)buf);
|
||||
httpcCloseContext(&context);
|
||||
|
||||
json_error_t error;
|
||||
content = json_loads((const char*)buf, 0, &error);
|
||||
if (!content) {
|
||||
free(buf);
|
||||
return -3;
|
||||
}
|
||||
free(buf);
|
||||
return 0;
|
||||
}
|
6
source/templatelib.c
Normal file
6
source/templatelib.c
Normal file
|
@ -0,0 +1,6 @@
|
|||
int myLibFunction() {
|
||||
|
||||
|
||||
return 42;
|
||||
|
||||
}
|
Loading…
Reference in a new issue