Fix error handling

Signed-off-by: Mahyar Koshkouei <deltabeard@users.noreply.github.com>
This commit is contained in:
Mahyar Koshkouei
2017-02-14 16:59:43 +00:00
parent 984192eebe
commit 711864cfa9
5 changed files with 47 additions and 50 deletions

View File

@@ -1,4 +1,5 @@
#include <errno.h>
#include <stdio.h>
/* Channel to play music on */
#define CHANNEL 0x08

View File

@@ -17,8 +17,9 @@
#include "all.h"
#include "error.h"
#include "main.h"
// TODO: Remove order requirement.
#include "playback.h"
#include "main.h"
volatile bool runThreads = true;
@@ -30,11 +31,11 @@ int main(int argc, char **argv)
int fileNum = 0;
int from = 0;
Thread watchdogThread;
Handle* playbackFailEvent = malloc(sizeof(Handle));
struct watchdogInfo* watchdogInfoIn;
struct errInfo_t* errInfo;
static volatile int error;
static volatile char* errorstr = NULL;
Handle playbackFailEvent;
struct watchdogInfo watchdogInfoIn;
struct errInfo_t errInfo;
struct playbackInfo_t playbackInfo;
int error = 0;
gfxInitDefault();
sdmcInit();
@@ -42,17 +43,18 @@ int main(int argc, char **argv)
consoleInit(GFX_BOTTOM, &bottomScreen);
consoleSelect(&bottomScreen);
svcCreateEvent(playbackFailEvent, RESET_ONESHOT);
watchdogInfoIn = calloc(1, sizeof(struct watchdogInfo));
errInfo = calloc(1, sizeof(struct errInfo_t));
errInfo->error = &error;
errInfo->failEvent = playbackFailEvent;
errInfo->errstr = errorstr;
watchdogInfoIn->screen = &topScreen;
watchdogInfoIn->errInfo = errInfo;
watchdogThread = threadCreate(playbackWatchdog, watchdogInfoIn, 4 * 1024, 0x20, -2, true);
svcCreateEvent(&playbackFailEvent, RESET_ONESHOT);
errInfo.error = &error;
errInfo.failEvent = &playbackFailEvent;
errInfo.errstr = NULL;
watchdogInfoIn.screen = &topScreen;
watchdogInfoIn.errInfo = &errInfo;
watchdogThread = threadCreate(playbackWatchdog, &watchdogInfoIn, 4 * 1024, 0x20, -2, true);
playbackInfo.file = NULL;
playbackInfo.errInfo = &errInfo;
printf("%p", playbackFailEvent);
chdir(DEFAULT_DIR);
chdir("MUSIC");
if(listDir(from, MAX_LIST, 0) < 0)
@@ -129,7 +131,7 @@ int main(int argc, char **argv)
if(kDown & KEY_B)
{
stopPlayback();
changeFile(NULL, NULL, NULL);
changeFile(NULL, NULL);
consoleSelect(&topScreen);
puts("Stopped");
continue;
@@ -219,7 +221,7 @@ int main(int argc, char **argv)
}
consoleSelect(&topScreen);
changeFile(ep->d_name, &error, playbackFailEvent);
changeFile(ep->d_name, &playbackInfo);
consoleSelect(&bottomScreen);
if(closedir(dp) != 0)
@@ -239,7 +241,7 @@ out:
puts("Exiting...");
runThreads = false;
stopPlayback();
changeFile(NULL, NULL, NULL);
changeFile(NULL, NULL);
gfxExit();
sdmcExit();
@@ -278,41 +280,35 @@ void playbackWatchdog(void* infoIn)
while(runThreads)
{
printf("%s:%d\n", __func__, __LINE__);
svcWaitSynchronization(*info->errInfo->failEvent, U64_MAX);
printf("%s:%d\n", __func__, __LINE__);
svcClearEvent(*info->errInfo->failEvent);
printf("%s:%d\n", __func__, __LINE__);
consoleSelect(info->screen);
printf("Here %s:%d err:%d\n", __func__, __LINE__, *info->errInfo->error);
#if 0
if(*info->error != 0)
if(*info->errInfo->error != 0)
{
printf("An error occurred: %s", ctrmus_strerror(*info->error));
if(info->str != NULL)
printf(" %s", info->str);
printf("An error occurred: %s", ctrmus_strerror(*info->errInfo->error));
if(info->errInfo->errstr != NULL)
{
printf(" %s", info->errInfo->errstr);
delete(info->errInfo->errstr);
}
printf("\n");
info->str = NULL;
}
#endif
}
return;
}
static int changeFile(const char* ep_file, volatile int* error, Handle* failEvent)
static int changeFile(const char* ep_file, struct playbackInfo_t* playbackInfo)
{
s32 prio;
static Thread thread = NULL;
static struct playbackInfo* info = NULL;
if(info == NULL)
info = calloc(1, sizeof(struct playbackInfo));
if(ep_file != NULL && getFileType(ep_file) < 0)
{
puts("Unsupported file.");
return -1;
}
/**
* If music is playing, stop it. Only one playback thread should be playing
* at any time.
@@ -327,19 +323,17 @@ static int changeFile(const char* ep_file, volatile int* error, Handle* failEven
thread = NULL;
/* free allocated file string */
delete(info->file);
delete(playbackInfo->file);
}
if(ep_file == NULL)
if(ep_file == NULL || playbackInfo == NULL)
return 0;
info->file = strdup(ep_file);
info->errInfo->error = error;
info->errInfo->failEvent = failEvent;
printf("Playing: %s\n", info->file);
playbackInfo->file = strdup(ep_file);
printf("Playing: %s\n", playbackInfo->file);
svcGetThreadPriority(&prio, CUR_THREAD_HANDLE);
thread = threadCreate(playFile, info, 32 * 1024, prio - 1, -2, false);
thread = threadCreate(playFile, playbackInfo, 32 * 1024, prio - 1, -2, false);
return 0;
}

View File

@@ -23,7 +23,7 @@ void playbackWatchdog(void* infoIn);
static void showControls(void);
static int changeFile(const char* ep_file, volatile int* error, Handle* failEvent);
static int changeFile(const char* ep_file, struct playbackInfo_t* playbackInfo);
/**
* Get number of files in current working folder

View File

@@ -18,7 +18,7 @@ static volatile bool stop = false;
void playFile(void* infoIn)
{
struct decoder_fn decoder;
struct playbackInfo* info = infoIn;
struct playbackInfo_t* info = infoIn;
int16_t* buffer1 = NULL;
int16_t* buffer2 = NULL;
ndspWaveBuf waveBuf[2];
@@ -26,9 +26,10 @@ void playFile(void* infoIn)
int ret = -1;
const char* file = info->file;
//info->str = strdup("Testing.");
errno = 12;
goto err;
//info->errInfo->errstr = strdup("Testing.");
//errno = 1001;
//goto err;
/* Reset previous stop command */
stop = false;
@@ -154,6 +155,7 @@ out:
err:
*info->errInfo->error = errno;
printf("%s:%d errno:%d %d\n", __func__, __LINE__, errno, *info->errInfo->error);
svcSignalEvent(*info->errInfo->failEvent);
goto out;
}

View File

@@ -8,7 +8,7 @@ enum file_types
FILE_TYPE_MP3
};
struct playbackInfo
struct playbackInfo_t
{
char* file;
struct errInfo_t* errInfo;