| | @@ -0,0 +1,95 @@ |
| 1 | +/*
|
| 2 | + * Copyright (c) 2006, 2007
|
| 3 | + * Nintendo Co., Ltd.
|
| 4 | + *
|
| 5 | + * Permission to use, copy, modify, distribute and sell this software
|
| 6 | + * and its documentation for any purpose is hereby granted without fee,
|
| 7 | + * provided that the above copyright notice appear in all copies and
|
| 8 | + * that both that copyright notice and this permission notice appear
|
| 9 | + * in supporting documentation. Nintendo makes no
|
| 10 | + * representations about the suitability of this software for any
|
| 11 | + * purpose. It is provided "as is" without express or implied warranty.
|
| 12 | + */
|
| 13 | +
|
| 14 | +#include <new>
|
| 15 | +#include <stdlib.h>
|
| 16 | +#include <es.h>
|
| 17 | +#include <es/clsid.h>
|
| 18 | +#include <es/handle.h>
|
| 19 | +#include <es/base/IProcess.h>
|
| 20 | +#include <es/device/IFileSystem.h>
|
| 21 | +
|
| 22 | +using namespace es;
|
| 23 | +
|
| 24 | +int esInit(IInterface** nameSpace);
|
| 25 | +IStream* esReportStream();
|
| 26 | +
|
| 27 | +#define TEST(exp) \
|
| 28 | + (void) ((exp) || \
|
| 29 | + (esPanic(__FILE__, __LINE__, "\nFailed test " #exp), 0))
|
| 30 | +
|
| 31 | +void test(Handle<IContext> nameSpace)
|
| 32 | +{
|
| 33 | + Handle<IIterator> iter;
|
| 34 | + Handle<IFile> file;
|
| 35 | + Handle<IStream> stream;
|
| 36 | + long long size = 0;
|
| 37 | +
|
| 38 | + file = nameSpace->lookup("file/newlib.elf");
|
| 39 | + size = file->getSize();
|
| 40 | + esReport("server size: %lld\n", size);
|
| 41 | +
|
| 42 | + Handle<IProcess> process;
|
| 43 | + process = reinterpret_cast<IProcess*>(
|
| 44 | + esCreateInstance(CLSID_Process, IProcess::iid()));
|
| 45 | + TEST(process);
|
| 46 | + process->setRoot(nameSpace);
|
| 47 | + process->setInput(esReportStream());
|
| 48 | + process->setOutput(esReportStream());
|
| 49 | + process->setError(esReportStream());
|
| 50 | + process->start(file, "");
|
| 51 | + process->wait();
|
| 52 | + esReport("server process exited.\n");
|
| 53 | +}
|
| 54 | +
|
| 55 | +int main(int argc, char* argv[])
|
| 56 | +{
|
| 57 | + IInterface* ns = 0;
|
| 58 | + esInit(&ns);
|
| 59 | +
|
| 60 | + Handle<IContext> nameSpace(ns);
|
| 61 | +
|
| 62 | + Handle<IClassStore> classStore(nameSpace->lookup("class"));
|
| 63 | + esRegisterFatFileSystemClass(classStore);
|
| 64 | +
|
| 65 | + Handle<IStream> disk = nameSpace->lookup("device/ata/channel0/device0");
|
| 66 | + long long diskSize;
|
| 67 | + diskSize = disk->getSize();
|
| 68 | + esReport("diskSize: %lld\n", diskSize);
|
| 69 | +
|
| 70 | + Handle<IFileSystem> fatFileSystem;
|
| 71 | + long long freeSpace;
|
| 72 | + long long totalSpace;
|
| 73 | +
|
| 74 | + fatFileSystem = reinterpret_cast<IFileSystem*>(
|
| 75 | + esCreateInstance(CLSID_FatFileSystem, IFileSystem::iid()));
|
| 76 | + fatFileSystem->mount(disk);
|
| 77 | + {
|
| 78 | + Handle<IContext> root;
|
| 79 | +
|
| 80 | + root = fatFileSystem->getRoot();
|
| 81 | + nameSpace->bind("file", root);
|
| 82 | +
|
| 83 | + test(nameSpace);
|
| 84 | +
|
| 85 | + freeSpace = fatFileSystem->getFreeSpace();
|
| 86 | + totalSpace = fatFileSystem->getTotalSpace();
|
| 87 | + esReport("Free space %lld, Total space %lld\n", freeSpace, totalSpace);
|
| 88 | + nameSpace->unbind("file");
|
| 89 | + }
|
| 90 | + fatFileSystem->dismount();
|
| 91 | + fatFileSystem = 0;
|
| 92 | +
|
| 93 | + esSleep(10000000);
|
| 94 | + esReport("done.\n");
|
| 95 | +}
|
| | @@ -0,0 +1,126 @@ |
| 1 | +/*
|
| 2 | + * Copyright (c) 2007
|
| 3 | + * Nintendo Co., Ltd.
|
| 4 | + *
|
| 5 | + * Permission to use, copy, modify, distribute and sell this software
|
| 6 | + * and its documentation for any purpose is hereby granted without fee,
|
| 7 | + * provided that the above copyright notice appear in all copies and
|
| 8 | + * that both that copyright notice and this permission notice appear
|
| 9 | + * in supporting documentation. Nintendo makes no
|
| 10 | + * representations about the suitability of this software for any
|
| 11 | + * purpose. It is provided "as is" without express or implied warranty.
|
| 12 | + */
|
| 13 | +
|
| 14 | +#include <dirent.h>
|
| 15 | +#include <fcntl.h>
|
| 16 | +#include <stdio.h>
|
| 17 | +#include <time.h>
|
| 18 | +#include <pthread.h>
|
| 19 | +#include <unistd.h>
|
| 20 | +#include <sys/stat.h>
|
| 21 | +#include <sys/types.h>
|
| 22 | +
|
| 23 | +#include <es/base/IProcess.h>
|
| 24 | +#include <es/base/IThread.h>
|
| 25 | +
|
| 26 | +using namespace es;
|
| 27 | +
|
| 28 | +extern ICurrentProcess* System();
|
| 29 | +
|
| 30 | +char buf[4096];
|
| 31 | +
|
| 32 | +pthread_mutex_t mutex;
|
| 33 | +
|
| 34 | +void* start(void* param)
|
| 35 | +{
|
| 36 | + printf("start: %p\n", pthread_self());
|
| 37 | +
|
| 38 | + pthread_mutex_lock(&mutex);
|
| 39 | +
|
| 40 | + printf("start: sleep for three seconds.\n");
|
| 41 | + sleep(3);
|
| 42 | +
|
| 43 | + pthread_mutex_unlock(&mutex);
|
| 44 | + printf("start: done.\n");
|
| 45 | +
|
| 46 | + return 0;
|
| 47 | +}
|
| 48 | +
|
| 49 | +int main(int argc, char* argv[])
|
| 50 | +{
|
| 51 | + int fd;
|
| 52 | + struct stat statbuf;
|
| 53 | +
|
| 54 | + printf("hello, world\n");
|
| 55 | +
|
| 56 | + fd = open("/file/cat.js", O_RDWR);
|
| 57 | + printf("fd: %d\n", fd);
|
| 58 | + if (fd == -1)
|
| 59 | + {
|
| 60 | + return 1;
|
| 61 | + }
|
| 62 | + int len = read(fd, buf, sizeof buf);
|
| 63 | + if (len <= 0)
|
| 64 | + {
|
| 65 | + return 1;
|
| 66 | + }
|
| 67 | +
|
| 68 | + write(1, buf, len);
|
| 69 | +
|
| 70 | + if (fstat(fd, &statbuf) == 0)
|
| 71 | + {
|
| 72 | + printf("%d's mode: %o\n", fd, statbuf.st_mode);
|
| 73 | + printf("%d is a tty: %d\n", fd, isatty(fd));
|
| 74 | + }
|
| 75 | +
|
| 76 | + close(fd);
|
| 77 | +
|
| 78 | + fd = 1;
|
| 79 | + if (fstat(fd, &statbuf) == 0)
|
| 80 | + {
|
| 81 | + printf("%d's mode: %o\n", fd, statbuf.st_mode);
|
| 82 | + printf("%d is a tty: %d\n", fd, isatty(fd));
|
| 83 | + }
|
| 84 | +
|
| 85 | + DIR* dir = opendir("/file");
|
| 86 | + if (dir)
|
| 87 | + {
|
| 88 | + struct dirent* ent;
|
| 89 | + for (int i = 0; ent = readdir(dir); ++i) // for getdents()
|
| 90 | + {
|
| 91 | + printf("%d: %s\n", i, ent->d_name);
|
| 92 | + }
|
| 93 | + closedir(dir);
|
| 94 | + }
|
| 95 | +
|
| 96 | + for (int i = 0; i < 3; ++i)
|
| 97 | + {
|
| 98 | + sleep(1); // for nanosleep()
|
| 99 | + time_t t = time(0);
|
| 100 | + printf("%s", ctime(&t));
|
| 101 | + }
|
| 102 | +
|
| 103 | + // pthread test
|
| 104 | +
|
| 105 | + pthread_mutex_init(&mutex, 0);
|
| 106 | +
|
| 107 | + IThread* thread = System()->createThread((void*) start, 0);
|
| 108 | + thread->start();
|
| 109 | +
|
| 110 | + sleep(1);
|
| 111 | + printf("main: try to get lock.\n");
|
| 112 | + pthread_mutex_lock(&mutex);
|
| 113 | + printf("main: got lock.\n");
|
| 114 | + pthread_mutex_unlock(&mutex);
|
| 115 | +
|
| 116 | + void* rval;
|
| 117 | + thread->join(&rval);
|
| 118 | + thread->release();
|
| 119 | + printf("rval : %p : %p\n", pthread_self(), rval);
|
| 120 | +
|
| 121 | + pthread_mutex_destroy(&mutex);
|
| 122 | +
|
| 123 | + printf("done.\n");
|
| 124 | +
|
| 125 | + return 0;
|
| 126 | +}
|