# IoT.js: Platform for Internet of Things with JavaScript
+[](https://gitter.im/Samsung/iotjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[](LICENSE)
[](https://travis-ci.org/Samsung/iotjs)
[](https://scan.coverity.com/projects/samsung-iotjs)
[](https://app.fossa.io/projects/git%2Bhttps%3A%2F%2Fgithub.com%2FSamsung%2Fiotjs?ref=badge_shield)
-[](https://kiwiirc.com/client/irc.freenode.net/#iotjs)
You can find project details on our [project page](http://samsung.github.io/iotjs/) and [wiki](https://github.com/Samsung/iotjs/wiki).
}
-bool iotjs_jval_set_prototype(const iotjs_jval_t* jobj, iotjs_jval_t* jproto) {
- jerry_value_t ret =
- jerry_set_prototype(iotjs_jval_as_raw(jobj), iotjs_jval_as_raw(jproto));
- bool error_found = jerry_value_has_error_flag(ret);
- jerry_release_value(ret);
-
- return !error_found;
-}
-
-
void iotjs_jval_set_method(const iotjs_jval_t* jobj, const char* name,
iotjs_native_handler_t handler) {
IOTJS_VALIDATABLE_STRUCT_METHOD_VALIDATE(iotjs_jval_t, jobj);
const iotjs_jval_t* iotjs_jval_as_function(THIS_JVAL);
/* Methods for General JavaScript Object */
-bool iotjs_jval_set_prototype(const iotjs_jval_t* jobj, iotjs_jval_t* jproto);
void iotjs_jval_set_method(THIS_JVAL, const char* name,
iotjs_native_handler_t handler);
void iotjs_jval_set_property_jval(THIS_JVAL, const char* name,
#define IOTJS_MAGIC_STRING_IPV6 "IPv6"
#define IOTJS_MAGIC_STRING_ISALIVEEXCEPTFOR "isAliveExceptFor"
#define IOTJS_MAGIC_STRING_ISDEVUP "isDevUp"
-#define IOTJS_MAGIC_STRING_ISDIRECTORY "isDirectory"
-#define IOTJS_MAGIC_STRING_ISFILE "isFile"
#define IOTJS_MAGIC_STRING_KEY "key"
#define IOTJS_MAGIC_STRING_LENGTH "length"
#define IOTJS_MAGIC_STRING_LISTEN "listen"
#define IOTJS_MAGIC_STRING_SPI "Spi"
#define IOTJS_MAGIC_STRING_START "start"
#define IOTJS_MAGIC_STRING_STAT "stat"
-#define IOTJS_MAGIC_STRING_STATS "stats"
#define IOTJS_MAGIC_STRING_STATUS_MSG "status_msg"
#define IOTJS_MAGIC_STRING_STATUS "status"
#define IOTJS_MAGIC_STRING_STDERR "stderr"
var util = require('util');
var fsBuiltin = process.binding(process.binding.fs);
+fs.Stats = function(stat) {
+ this.dev = stat.dev;
+ this.mode = stat.mode;
+ this.nlink = stat.nlink;
+ this.uid = stat.uid;
+ this.gid = stat.gid;
+ this.rdev = stat.rdev;
+ this.blksize = stat.blksize;
+ this.ino = stat.ino;
+ this.size = stat.size;
+ this.blocks = stat.blocks;
+};
+
+
+fs.Stats.prototype.isDirectory = function() {
+ return ((this.mode & constants.S_IFMT) === constants.S_IFDIR);
+};
+
+
+fs.Stats.prototype.isFile = function() {
+ return ((this.mode & constants.S_IFMT) === constants.S_IFREG);
+};
+
+
+fsBuiltin._createStat = function(stat) {
+ return new fs.Stats(stat);
+};
+
+
fs.exists = function(path, callback) {
if (!path || !path.length) {
process.nextTick(function () {
iotjs_jval_t MakeStatObject(uv_stat_t* statbuf) {
const iotjs_jval_t* fs = iotjs_module_get(MODULE_FS);
- iotjs_jval_t stat_prototype =
- iotjs_jval_get_property(fs, IOTJS_MAGIC_STRING_STATS);
- IOTJS_ASSERT(iotjs_jval_is_object(&stat_prototype));
+ iotjs_jval_t create_stat =
+ iotjs_jval_get_property(fs, IOTJS_MAGIC_STRING__CREATESTAT);
+ IOTJS_ASSERT(iotjs_jval_is_function(&create_stat));
iotjs_jval_t jstat = iotjs_jval_create_object();
- iotjs_jval_set_prototype(&jstat, &stat_prototype);
-
- iotjs_jval_destroy(&stat_prototype);
-
#define X(statobj, name) \
iotjs_jval_set_property_number(statobj, #name, statbuf->st_##name);
#undef X
- return jstat;
+ iotjs_jargs_t jargs = iotjs_jargs_create(1);
+ iotjs_jargs_append_jval(&jargs, &jstat);
+ iotjs_jval_destroy(&jstat);
+
+ iotjs_jval_t res =
+ iotjs_jhelper_call_ok(&create_stat, iotjs_jval_get_undefined(), &jargs);
+
+ iotjs_jargs_destroy(&jargs);
+ iotjs_jval_destroy(&create_stat);
+
+ return res;
}
iotjs_string_destroy(&path);
}
-static void StatsIsTypeOf(iotjs_jhandler_t* jhandler, int type) {
- DJHANDLER_CHECK_THIS(object);
- const iotjs_jval_t* stats = JHANDLER_GET_THIS(object);
- iotjs_jval_t mode = iotjs_jval_get_property(stats, IOTJS_MAGIC_STRING_MODE);
-
- int mode_number = (int)iotjs_jval_as_number(&mode);
-
- iotjs_jval_destroy(&mode);
-
- iotjs_jhandler_return_boolean(jhandler, (mode_number & S_IFMT) == type);
-}
-
-JHANDLER_FUNCTION(StatsIsDirectory) {
- StatsIsTypeOf(jhandler, S_IFDIR);
-}
-
-JHANDLER_FUNCTION(StatsIsFile) {
- StatsIsTypeOf(jhandler, S_IFREG);
-}
iotjs_jval_t InitFs() {
iotjs_jval_t fs = iotjs_jval_create_object();
iotjs_jval_set_method(&fs, IOTJS_MAGIC_STRING_RENAME, Rename);
iotjs_jval_set_method(&fs, IOTJS_MAGIC_STRING_READDIR, ReadDir);
- iotjs_jval_t stats_prototype = iotjs_jval_create_object();
-
- iotjs_jval_set_method(&stats_prototype, IOTJS_MAGIC_STRING_ISDIRECTORY,
- StatsIsDirectory);
- iotjs_jval_set_method(&stats_prototype, IOTJS_MAGIC_STRING_ISFILE,
- StatsIsFile);
-
- iotjs_jval_set_property_jval(&fs, IOTJS_MAGIC_STRING_STATS, &stats_prototype);
- iotjs_jval_destroy(&stats_prototype);
-
return fs;
}
if (devicePath) {
// Linux API uses nanoseconds, thus 1E9
unsigned int value = (unsigned)(adjust_period(_this->period) * 1.E9);
- DDDLOG("%s - path: %s, value: %fs", __func__, devicePath, 1.E-9 * value);
+ DDLOG("%s - path: %s, value: %fs", __func__, devicePath, 1.E-9 * value);
char buf[PWM_VALUE_BUFFER_SIZE];
if (snprintf(buf, sizeof(buf), "%d", value) > 0) {
result = iotjs_systemio_open_write_close(devicePath, buf);
// Linux API uses nanoseconds, thus 1E9
unsigned dutyCycleValue = (unsigned)(period * _this->duty_cycle * 1E9);
- DDDLOG("%s - path: %s, value: %d\n", __func__, devicePath,
- dutyCycleValue);
+ DDLOG("%s - path: %s, value: %d\n", __func__, devicePath, dutyCycleValue);
char buf[PWM_VALUE_BUFFER_SIZE];
if (snprintf(buf, sizeof(buf), "%d", dutyCycleValue) < 0) {
return false;
return false;
}
- DDDLOG("%s - path: %s, set: %d\n", __func__, devicePath, _this->enable);
+ DDLOG("%s - path: %s, set: %d\n", __func__, devicePath, _this->enable);
char buf[PWM_VALUE_BUFFER_SIZE];
if (snprintf(buf, sizeof(buf), "%d", _this->enable) < 0) {
return false;
return false;
}
- DDDLOG(
+ DDLOG(
"SPI Options \n mode: %d\n chipSelect: %d\n bitOrder: %d\n "
"maxSpeed: %d\n bitPerWord: %d\n loopback: %d",
_this->mode, _this->chip_select, _this->bit_order, _this->max_speed,
// Transfer data
int err = ioctl(_this->device_fd, SPI_IOC_MESSAGE(1), &data);
if (err < 1) {
- DLOG("%s - transfer failed: %d", __func__, err);
+ DDLOG("%s - transfer failed: %d", __func__, err);
return false;
}
int err = uv_fs_close(loop, &fs_close_req, _this->device_fd, NULL);
uv_fs_req_cleanup(&fs_close_req);
if (err < 0) {
- DLOG("%s - close failed: %d", __func__, err);
+ DDLOG("%s - close failed: %d", __func__, err);
return false;
}
_this->device_fd = -1;
int fd = uv_fs_open(loop, &fs_req, path, O_WRONLY, 0666, NULL);
uv_fs_req_cleanup(&fs_req);
if (fd < 0) {
- DLOG("%s - open %s failed: %d", __func__, path, fd);
+ DDLOG("%s - open %s failed: %d", __func__, path, fd);
return false;
}
uv_fs_req_cleanup(&fs_req);
if (write_err < 0) {
- DLOG("%s - write %s %s failed: %d", __func__, path, value, write_err);
+ DDLOG("%s - write %s %s failed: %d", __func__, path, value, write_err);
return false;
}
if (close_err < 0) {
- DLOG("%s - close failed: %d", __func__, close_err);
+ DDLOG("%s - close failed: %d", __func__, close_err);
return false;
}
int fd = uv_fs_open(loop, &fs_open_req, path, O_RDONLY, 0666, NULL);
uv_fs_req_cleanup(&fs_open_req);
if (fd < 0) {
- DLOG("%s - open %s failed: %d", __func__, path, fd);
+ DDLOG("%s - open %s failed: %d", __func__, path, fd);
return false;
}
int err = uv_fs_read(loop, &fs_write_req, fd, &uvbuf, 1, 0, NULL);
uv_fs_req_cleanup(&fs_write_req);
if (err < 0) {
- DLOG("%s - read failed: %d", __func__, err);
+ DDLOG("%s - read failed: %d", __func__, err);
return false;
}
err = uv_fs_close(loop, &fs_close_req, fd, NULL);
uv_fs_req_cleanup(&fs_close_req);
if (err < 0) {
- DLOG("%s - close failed: %d", __func__, err);
+ DDLOG("%s - close failed: %d", __func__, err);
return false;
}
return true;
}
- DDDLOG("%s - path: %s", __func__, export_path);
+ DDLOG("%s - path: %s", __func__, export_path);
// Write export pin.
char buff[DEVICE_IO_PIN_BUFFER_SIZE] = { 0 };
snprintf(path, DEVICE_IO_PATH_BUFFER_SIZE - 1, check_format,
created_files[i]);
- DDDLOG("%s - created file: %s", __func__, path);
+ DDLOG("%s - created file: %s", __func__, path);
while (!iotjs_systemio_open_read_close(path, buffer,
DEVICE_IO_PIN_BUFFER_SIZE) &&
return false;
}
- DDDLOG("ADC Read - path: %s, value: %d", path, msg->am_data);
+ DDLOG("ADC Read - path: %s, value: %d", path, msg->am_data);
return true;
}
return;
}
- DDDLOG("%s - path: %s, number: %d, timer: %d", __func__, path, adc_number,
- timer);
+ DDLOG("%s - path: %s, number: %d, timer: %d", __func__, path, adc_number,
+ timer);
req_data->result = true;
}
IOTJS_VALIDATED_STRUCT_METHOD(iotjs_i2c_t, i2c);
_this->i2c_master = iotjs_i2c_config_nuttx(req_data->device);
if (!_this->i2c_master) {
- DLOG("I2C OpenWorker : cannot open");
+ DDLOG("I2C OpenWorker : cannot open");
req_data->error = kI2cErrOpen;
return;
}
int ret = i2c_write(_this->i2c_master, &_this->config, data, len);
if (ret < 0) {
- DLOG("I2C WriteWorker : cannot write - %d", ret);
+ DDLOG("I2C WriteWorker : cannot write - %d", ret);
req_data->error = kI2cErrWrite;
} else {
req_data->error = kI2cErrOk;
int ret = i2c_read(_this->i2c_master, &_this->config,
(uint8_t*)req_data->buf_data, len);
if (ret != 0) {
- DLOG("I2C ReadWorker : cannot read - %d", ret);
+ DDLOG("I2C ReadWorker : cannot read - %d", ret);
req_data->error = kI2cErrRead;
return;
}
int fd = _this->device_fd;
if (fd < 0) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
return false;
}
// File open
_this->device_fd = open(path, O_RDONLY);
if (_this->device_fd < 0) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
req_data->result = false;
return;
}
int fd = _this->device_fd;
if (fd < 0) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
return false;
}
}
if (ret < 0) {
- DLOG("%s - setEnable failed", __func__);
+ DDLOG("%s - setEnable failed", __func__);
return false;
}
int fd = _this->device_fd;
if (fd < 0) {
- DLOG("%s - file not opened", __func__);
+ DDLOG("%s - file not opened", __func__);
return false;
}
iotbus_pwm_context_h ctx = _this->ctx;
if (ctx == NULL) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
return false;
}
_this->ctx = iotbus_pwm_open(0, (int)_this->pin);
if (_this->ctx == NULL) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
req_data->result = false;
return;
}
iotbus_pwm_context_h ctx = _this->ctx;
if (ctx == NULL) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
return false;
}
iotbus_pwm_context_h ctx = _this->ctx;
if (ctx == NULL) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
return false;
}
iotbus_pwm_context_h ctx = _this->ctx;
if (ctx == NULL) {
- DLOG("%s - file open failed", __func__);
+ DDLOG("%s - file open failed", __func__);
return false;
}
}
if (ret < 0) {
- DLOG("%s - setEnable failed", __func__);
+ DDLOG("%s - setEnable failed", __func__);
return false;
}
iotbus_pwm_context_h ctx = _this->ctx;
if (ctx == NULL) {
- DLOG("%s - file not opened", __func__);
+ DDLOG("%s - file not opened", __func__);
return false;
}