aboutsummaryrefslogtreecommitdiff
path: root/unix
diff options
context:
space:
mode:
authorDamien George2014-02-10 21:41:14 +0000
committerDamien George2014-02-10 21:41:14 +0000
commit8c2b333affea7445c457c5247df047947bed9b53 (patch)
treeb697957a499162ccca3219dca426b1bcd35cd0c7 /unix
parent7d0bfbedd218fadd91e2bbeb6486371ffeb7b682 (diff)
parent2e24ee8d80de20e879275c087ecc1ca9b4d27297 (diff)
Merge branch 'master' of github.com:micropython/micropython
Diffstat (limited to 'unix')
-rw-r--r--unix/ffi.c2
-rw-r--r--unix/file.c7
-rw-r--r--unix/main.c4
-rw-r--r--unix/qstrdefsport.h2
-rw-r--r--unix/socket.c32
-rw-r--r--unix/time.c18
6 files changed, 49 insertions, 16 deletions
diff --git a/unix/ffi.c b/unix/ffi.c
index b40e9a4ee..b83fc120a 100644
--- a/unix/ffi.c
+++ b/unix/ffi.c
@@ -71,7 +71,7 @@ static ffi_type *get_ffi_type(mp_obj_t o_in)
{
if (MP_OBJ_IS_STR(o_in)) {
uint len;
- const byte *s = mp_obj_str_get_data(o_in, &len);
+ const char *s = mp_obj_str_get_data(o_in, &len);
ffi_type *t = char2ffi_type(*s);
if (t != NULL) {
return t;
diff --git a/unix/file.c b/unix/file.c
index 5249b5bba..7bbe3bfd0 100644
--- a/unix/file.c
+++ b/unix/file.c
@@ -48,6 +48,12 @@ static mp_obj_t fdfile_close(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(fdfile_close_obj, fdfile_close);
+static mp_obj_t fdfile_fileno(mp_obj_t self_in) {
+ mp_obj_fdfile_t *self = self_in;
+ return MP_OBJ_NEW_SMALL_INT(self->fd);
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(fdfile_fileno_obj, fdfile_fileno);
+
static mp_obj_fdfile_t *fdfile_new(int fd) {
mp_obj_fdfile_t *o = m_new_obj(mp_obj_fdfile_t);
o->base.type = &rawfile_type;
@@ -99,6 +105,7 @@ static mp_obj_t fdfile_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
}
static const mp_method_t rawfile_type_methods[] = {
+ { "fileno", &fdfile_fileno_obj },
{ "read", &mp_stream_read_obj },
{ "readall", &mp_stream_readall_obj },
{ "readline", &mp_stream_unbuffered_readline_obj},
diff --git a/unix/main.c b/unix/main.c
index 9fb25a43e..5fdfbc36a 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -23,7 +23,7 @@
extern const mp_obj_fun_native_t mp_builtin_open_obj;
void file_init();
-void rawsocket_init();
+void microsocket_init();
void time_init();
void ffi_init();
@@ -265,7 +265,7 @@ int main(int argc, char **argv) {
rt_store_name(qstr_from_str("qstr_info"), rt_make_function_n(0, qstr_info));
file_init();
- rawsocket_init();
+ microsocket_init();
#if MICROPY_MOD_TIME
time_init();
#endif
diff --git a/unix/qstrdefsport.h b/unix/qstrdefsport.h
index 598f23bef..9f28c607e 100644
--- a/unix/qstrdefsport.h
+++ b/unix/qstrdefsport.h
@@ -12,4 +12,4 @@ Q(htons)
Q(inet_aton)
Q(gethostbyname)
Q(getaddrinfo)
-Q(rawsocket)
+Q(microsocket)
diff --git a/unix/socket.c b/unix/socket.c
index 28749f3be..eb4ea1e04 100644
--- a/unix/socket.c
+++ b/unix/socket.c
@@ -24,7 +24,7 @@ typedef struct _mp_obj_socket_t {
int fd;
} mp_obj_socket_t;
-static const mp_obj_type_t rawsocket_type;
+static const mp_obj_type_t microsocket_type;
// Helper functions
#define RAISE_ERRNO(err_flag, error_val) \
@@ -48,7 +48,7 @@ error:
static mp_obj_socket_t *socket_new(int fd) {
mp_obj_socket_t *o = m_new_obj(mp_obj_socket_t);
- o->base.type = &rawsocket_type;
+ o->base.type = &microsocket_type;
o->fd = fd;
return o;
}
@@ -84,6 +84,12 @@ static mp_obj_t socket_close(mp_obj_t self_in) {
}
static MP_DEFINE_CONST_FUN_OBJ_1(socket_close_obj, socket_close);
+static mp_obj_t socket_fileno(mp_obj_t self_in) {
+ mp_obj_socket_t *self = self_in;
+ return MP_OBJ_NEW_SMALL_INT(self->fd);
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(socket_fileno_obj, socket_fileno);
+
static mp_obj_t socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
mp_obj_socket_t *self = self_in;
buffer_info_t bufinfo;
@@ -208,7 +214,9 @@ static mp_obj_t socket_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const
return socket_new(fd);
}
-static const mp_method_t rawsocket_type_methods[] = {
+static const mp_method_t microsocket_type_methods[] = {
+ { "fileno", &socket_fileno_obj },
+ { "makefile", &mp_identity_obj },
{ "read", &mp_stream_read_obj },
{ "readall", &mp_stream_readall_obj },
{ "readline", &mp_stream_unbuffered_readline_obj},
@@ -228,7 +236,7 @@ static const mp_method_t rawsocket_type_methods[] = {
{ NULL, NULL },
};
-static const mp_obj_type_t rawsocket_type = {
+static const mp_obj_type_t microsocket_type = {
{ &mp_const_type },
"socket",
.print = socket_print,
@@ -239,7 +247,7 @@ static const mp_obj_type_t rawsocket_type = {
.read = socket_read,
.write = socket_write,
},
- .methods = rawsocket_type_methods,
+ .methods = microsocket_type_methods,
};
static mp_obj_t mod_socket_htons(mp_obj_t arg) {
@@ -283,8 +291,8 @@ static mp_obj_t mod_socket_getaddrinfo(uint n_args, const mp_obj_t *args) {
// getaddrinfo accepts port in string notation, so however
// it may seem stupid, we need to convert int to str
if (MP_OBJ_IS_SMALL_INT(args[1])) {
- int port = MP_OBJ_SMALL_INT_VALUE(args[1]);
- static char buf[20];
+ int port = (short)MP_OBJ_SMALL_INT_VALUE(args[1]);
+ char buf[6];
sprintf(buf, "%d", port);
serv = buf;
} else {
@@ -325,7 +333,7 @@ extern mp_obj_type_t sockaddr_in_type;
#define C(name) { #name, name }
-struct sym_entry {
+static const struct sym_entry {
const char *sym;
int val;
} constants[] = {
@@ -351,9 +359,9 @@ struct sym_entry {
#undef C
-void rawsocket_init() {
- mp_obj_t m = mp_obj_new_module(MP_QSTR_rawsocket);
- rt_store_attr(m, MP_QSTR_socket, (mp_obj_t)&rawsocket_type);
+void microsocket_init() {
+ mp_obj_t m = mp_obj_new_module(MP_QSTR_microsocket);
+ rt_store_attr(m, MP_QSTR_socket, (mp_obj_t)&microsocket_type);
#if MICROPY_SOCKET_EXTRA
rt_store_attr(m, MP_QSTR_sockaddr_in, (mp_obj_t)&sockaddr_in_type);
rt_store_attr(m, MP_QSTR_htons, (mp_obj_t)&mod_socket_htons_obj);
@@ -361,7 +369,7 @@ void rawsocket_init() {
rt_store_attr(m, MP_QSTR_gethostbyname, (mp_obj_t)&mod_socket_gethostbyname_obj);
#endif
rt_store_attr(m, MP_QSTR_getaddrinfo, (mp_obj_t)&mod_socket_getaddrinfo_obj);
- for (struct sym_entry *p = constants; p->sym != NULL; p++) {
+ for (const struct sym_entry *p = constants; p->sym != NULL; p++) {
rt_store_attr(m, QSTR_FROM_STR_STATIC(p->sym), MP_OBJ_NEW_SMALL_INT((machine_int_t)p->val));
}
}
diff --git a/unix/time.c b/unix/time.c
index 9d8cf497c..6dba9f0d0 100644
--- a/unix/time.c
+++ b/unix/time.c
@@ -1,5 +1,7 @@
#include <string.h>
#include <time.h>
+#include <sys/time.h>
+#include <math.h>
#include "misc.h"
#include "mpconfig.h"
@@ -23,8 +25,24 @@ static mp_obj_t mod_time_clock() {
}
static MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock);
+static mp_obj_t mod_time_sleep(mp_obj_t arg) {
+#if MICROPY_ENABLE_FLOAT
+ struct timeval tv;
+ machine_float_t val = mp_obj_get_float(arg);
+ double ipart;
+ tv.tv_usec = round(modf(val, &ipart) * 1000000);
+ tv.tv_sec = ipart;
+ select(0, NULL, NULL, NULL, &tv);
+#else
+ sleep(mp_obj_get_int(arg));
+#endif
+ return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep);
+
void time_init() {
mp_obj_t m = mp_obj_new_module(QSTR_FROM_STR_STATIC("time"));
rt_store_attr(m, QSTR_FROM_STR_STATIC("time"), (mp_obj_t)&mod_time_time_obj);
rt_store_attr(m, QSTR_FROM_STR_STATIC("clock"), (mp_obj_t)&mod_time_clock_obj);
+ rt_store_attr(m, QSTR_FROM_STR_STATIC("sleep"), (mp_obj_t)&mod_time_sleep_obj);
}