2017-07-07 19:26:49 +02:00
|
|
|
#include "pxt.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2017-07-08 12:16:12 +02:00
|
|
|
#include <sys/ioctl.h>
|
2017-07-07 19:26:49 +02:00
|
|
|
|
2018-01-12 05:05:45 +01:00
|
|
|
/**
|
|
|
|
* Mode for lseek()
|
|
|
|
*/
|
|
|
|
enum class SeekWhence {
|
|
|
|
Set = 0,
|
|
|
|
Current = 1,
|
|
|
|
End = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-07-07 19:26:49 +02:00
|
|
|
namespace pxt {
|
|
|
|
PXT_VTABLE_CTOR(MMap) {
|
|
|
|
length = 0;
|
|
|
|
fd = -1;
|
|
|
|
data = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MMap::print() {
|
2019-10-09 06:57:55 +02:00
|
|
|
DMESG("MMap %p len=%d fd=%d data=%p", this, length, fd, data);
|
2017-07-07 19:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MMap::destroy() {
|
|
|
|
munmap(data, length);
|
|
|
|
close(fd);
|
|
|
|
}
|
2019-10-09 06:57:55 +02:00
|
|
|
|
|
|
|
void MMap::scan(MMap *) {}
|
|
|
|
|
|
|
|
unsigned MMap::gcsize(MMap *) {
|
|
|
|
return TOWORDS(sizeof(MMap));
|
|
|
|
}
|
|
|
|
|
2017-07-07 19:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace control {
|
|
|
|
|
|
|
|
/** Create new file mapping in memory */
|
|
|
|
//%
|
|
|
|
MMap *mmap(String filename, int size, int offset) {
|
2019-10-09 06:57:55 +02:00
|
|
|
DMESG("mmap %s len=%d off=%d", filename->getUTF8Data(), size, offset);
|
|
|
|
int fd = open(filename->getUTF8Data(), O_RDWR, 0);
|
2017-07-07 19:26:49 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return 0;
|
2017-07-09 18:51:16 +02:00
|
|
|
|
|
|
|
void *data = NULL;
|
|
|
|
if (size > 0) {
|
|
|
|
data = ::mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
|
|
|
|
if (data == MAP_FAILED) {
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-07-07 19:26:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto r = new MMap();
|
|
|
|
r->fd = fd;
|
|
|
|
r->length = size;
|
2017-07-08 12:16:12 +02:00
|
|
|
r->data = (uint8_t *)data;
|
2017-07-07 19:26:49 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace MMapMethods {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Write a number in specified format in the buffer.
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
void setNumber(MMap *buf, NumberFormat format, int offset, TNumber value) {
|
|
|
|
if (offset < 0)
|
|
|
|
return;
|
|
|
|
setNumberCore(buf->data + offset, buf->length - offset, format, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read a number in specified format from the buffer.
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
TNumber getNumber(MMap *buf, NumberFormat format, int offset) {
|
|
|
|
if (offset < 0)
|
|
|
|
return fromInt(0);
|
|
|
|
return getNumberCore(buf->data + offset, buf->length - offset, format);
|
|
|
|
}
|
|
|
|
|
2017-07-09 18:51:16 +02:00
|
|
|
/**
|
|
|
|
* Read a range of bytes into a buffer.
|
|
|
|
*/
|
|
|
|
//%
|
|
|
|
Buffer slice(MMap *buf, int offset = 0, int length = -1) {
|
|
|
|
offset = min((int)buf->length, offset);
|
|
|
|
if (length < 0)
|
|
|
|
length = buf->length;
|
|
|
|
length = min(length, buf->length - offset);
|
|
|
|
return mkBuffer(buf->data + offset, length);
|
|
|
|
}
|
|
|
|
|
2017-07-07 19:26:49 +02:00
|
|
|
/** Returns the length of a Buffer object. */
|
|
|
|
//% property
|
|
|
|
int length(MMap *s) {
|
|
|
|
return s->length;
|
|
|
|
}
|
2017-07-08 12:16:12 +02:00
|
|
|
|
|
|
|
/** Perform ioctl(2) on the underlaying file */
|
|
|
|
//%
|
2017-07-10 15:16:31 +02:00
|
|
|
int ioctl(MMap *mmap, uint32_t id, Buffer data) {
|
|
|
|
return ::ioctl(mmap->fd, id, data->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Perform write(2) on the underlaying file */
|
|
|
|
//%
|
|
|
|
int write(MMap *mmap, Buffer data) {
|
|
|
|
return ::write(mmap->fd, data->data, data->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Perform read(2) on the underlaying file */
|
|
|
|
//%
|
|
|
|
int read(MMap *mmap, Buffer data) {
|
|
|
|
return ::read(mmap->fd, data->data, data->length);
|
2017-07-08 12:16:12 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 05:05:45 +01:00
|
|
|
/** Set pointer on the underlaying file. */
|
|
|
|
//%
|
|
|
|
int lseek(MMap *mmap, int offset, SeekWhence whence) {
|
|
|
|
return ::lseek(mmap->fd, offset, (int)whence);
|
|
|
|
}
|
|
|
|
|
2017-07-07 19:26:49 +02:00
|
|
|
}
|