// convert a native value to a MicroPython object based on type mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) { DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type); switch (type & 0xf) { case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val; case MP_NATIVE_TYPE_BOOL: return mp_obj_new_bool(val); case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val); case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val); default: // a pointer // we return just the value of the pointer as an integer return mp_obj_new_int_from_uint(val); } }
STATIC mp_obj_t lwip_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) { lwip_socket_obj_t *socket = self_in; int _errno; lwip_socket_check_connected(socket); mp_buffer_info_t bufinfo; mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); uint8_t ip[NETUTILS_IPV4ADDR_BUFSIZE]; mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG); mp_uint_t ret = 0; switch (socket->type) { case MOD_NETWORK_SOCK_STREAM: { ret = lwip_tcp_send(socket, bufinfo.buf, bufinfo.len, &_errno); break; } case MOD_NETWORK_SOCK_DGRAM: { ret = lwip_udp_send(socket, bufinfo.buf, bufinfo.len, ip, port, &_errno); break; } } if (ret == -1) { mp_raise_OSError(_errno); } return mp_obj_new_int_from_uint(ret); }
STATIC mp_obj_t lwip_socket_send(mp_obj_t self_in, mp_obj_t buf_in) { lwip_socket_obj_t *socket = self_in; int _errno; if (socket->pcb == NULL) { // not connected _errno = error_lookup_table[-(socket->connected)]; socket->connected = -16; nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); } mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); mp_uint_t ret = 0; switch (socket->type) { case MOD_NETWORK_SOCK_STREAM: { ret = lwip_tcp_send(socket, bufinfo.buf, bufinfo.len, &_errno); break; } case MOD_NETWORK_SOCK_DGRAM: { ret = lwip_udp_send(socket, bufinfo.buf, bufinfo.len, NULL, 0, &_errno); break; } } if (ret == -1) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); } return mp_obj_new_int_from_uint(ret); }
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) { byte *p = *ptr; mp_uint_t align; int size = mp_binary_get_size(struct_type, val_type, &align); if (struct_type == '@') { // Make pointer aligned p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1)); #if MP_ENDIANNESS_LITTLE struct_type = '<'; #else struct_type = '>'; #endif } *ptr = p + size; mp_int_t val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p); if (val_type == 'O') { return (mp_obj_t)val; } else if (val_type == 'S') { return mp_obj_new_str((char*)val, strlen((char*)val), false); } else if (is_signed(val_type)) { return mp_obj_new_int(val); } else { return mp_obj_new_int_from_uint(val); } }
STATIC mp_obj_t lwip_socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) { lwip_socket_obj_t *socket = self_in; int _errno; if (socket->pcb == NULL) { // not connected _errno = error_lookup_table[-(socket->connected)]; socket->connected = -16; nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); } mp_buffer_info_t bufinfo; mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); uint8_t ip[NETUTILS_IPV4ADDR_BUFSIZE]; mp_uint_t port = netutils_parse_inet_addr(addr_in, ip, NETUTILS_BIG); mp_uint_t ret = 0; switch (socket->type) { case MOD_NETWORK_SOCK_STREAM: { ret = lwip_tcp_send(socket, bufinfo.buf, bufinfo.len, &_errno); break; } case MOD_NETWORK_SOCK_DGRAM: { ret = lwip_udp_send(socket, bufinfo.buf, bufinfo.len, ip, port, &_errno); break; } } if (ret == -1) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); } return mp_obj_new_int_from_uint(ret); }
STATIC mp_obj_t socket_sendto(mp_obj_t self_in, mp_obj_t data_in, mp_obj_t addr_in) { socket_obj_t *self = MP_OBJ_TO_PTR(self_in); // get the buffer to send mp_buffer_info_t bufinfo; mp_get_buffer_raise(data_in, &bufinfo, MP_BUFFER_READ); // create the destination address struct sockaddr_in to; to.sin_len = sizeof(to); to.sin_family = AF_INET; to.sin_port = lwip_htons(netutils_parse_inet_addr(addr_in, (uint8_t*)&to.sin_addr, NETUTILS_BIG)); // send the data for (int i=0; i<=self->retries; i++) { MP_THREAD_GIL_EXIT(); int ret = lwip_sendto_r(self->fd, bufinfo.buf, bufinfo.len, 0, (struct sockaddr*)&to, sizeof(to)); MP_THREAD_GIL_ENTER(); if (ret > 0) return mp_obj_new_int_from_uint(ret); if (ret == -1 && errno != EWOULDBLOCK) { exception_from_errno(errno); } check_for_exceptions(); } mp_raise_OSError(MP_ETIMEDOUT); }
STATIC mp_obj_t lwip_socket_send(mp_obj_t self_in, mp_obj_t buf_in) { lwip_socket_obj_t *socket = self_in; int _errno; lwip_socket_check_connected(socket); mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); mp_uint_t ret = 0; switch (socket->type) { case MOD_NETWORK_SOCK_STREAM: { ret = lwip_tcp_send(socket, bufinfo.buf, bufinfo.len, &_errno); break; } case MOD_NETWORK_SOCK_DGRAM: { ret = lwip_udp_send(socket, bufinfo.buf, bufinfo.len, NULL, 0, &_errno); break; } } if (ret == -1) { mp_raise_OSError(_errno); } return mp_obj_new_int_from_uint(ret); }
mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) { mp_int_t val = 0; switch (typecode) { case 'b': val = ((signed char*)p)[index]; break; case BYTEARRAY_TYPECODE: case 'B': val = ((unsigned char*)p)[index]; break; case 'h': val = ((short*)p)[index]; break; case 'H': val = ((unsigned short*)p)[index]; break; case 'i': return mp_obj_new_int(((int*)p)[index]); case 'I': return mp_obj_new_int_from_uint(((unsigned int*)p)[index]); case 'l': return mp_obj_new_int(((long*)p)[index]); case 'L': return mp_obj_new_int_from_uint(((unsigned long*)p)[index]); #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE case 'q': return mp_obj_new_int_from_ll(((long long*)p)[index]); case 'Q': return mp_obj_new_int_from_ull(((unsigned long long*)p)[index]); #endif #if MICROPY_PY_BUILTINS_FLOAT case 'f': return mp_obj_new_float(((float*)p)[index]); case 'd': return mp_obj_new_float(((double*)p)[index]); #endif // Extension to CPython: array of objects case 'O': return ((mp_obj_t*)p)[index]; // Extension to CPython: array of pointers case 'P': return mp_obj_new_int((mp_int_t)((void**)p)[index]); } return MP_OBJ_NEW_SMALL_INT(val); }
STATIC mp_obj_t mod_thread_stack_size(size_t n_args, const mp_obj_t *args) { mp_obj_t ret = mp_obj_new_int_from_uint(thread_stack_size); if (n_args == 0) { thread_stack_size = 0; } else { thread_stack_size = mp_obj_get_int(args[0]); } return ret; }
// convert a native value to a Micro Python object based on type mp_obj_t mp_convert_native_to_obj(mp_uint_t val, mp_uint_t type) { DEBUG_printf("mp_convert_native_to_obj(" UINT_FMT ", " UINT_FMT ")\n", val, type); switch (type & 3) { case MP_NATIVE_TYPE_OBJ: return (mp_obj_t)val; case MP_NATIVE_TYPE_BOOL: return MP_BOOL(val); case MP_NATIVE_TYPE_INT: return mp_obj_new_int(val); case MP_NATIVE_TYPE_UINT: return mp_obj_new_int_from_uint(val); default: assert(0); return mp_const_none; } }
STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); int err = 0; mp_uint_t len = sock_write(self_in, bufinfo.buf, bufinfo.len, &err); if (len == MP_STREAM_ERROR) { mp_raise_OSError(err); } return mp_obj_new_int_from_uint(len); }
// method socket.send(bytes) STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { mod_network_socket_obj_t *self = self_in; mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); int _errno; mp_int_t ret = wlan_socket_send(self, bufinfo.buf, bufinfo.len, &_errno); if (ret < 0) { mp_raise_OSError(-_errno); } return mp_obj_new_int_from_uint(ret); }
// method socket.send(bytes) STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { mod_network_socket_obj_t *self = self_in; mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); int _errno; mp_uint_t ret = wlan_socket_send(self, bufinfo.buf, bufinfo.len, &_errno); if (ret == -1) { nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(_errno))); } return mp_obj_new_int_from_uint(ret); }
mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) { mp_int_t val = 0; switch (typecode) { case 'b': val = ((signed char*)p)[index]; break; case BYTEARRAY_TYPECODE: case 'B': val = ((unsigned char*)p)[index]; break; case 'h': val = ((short*)p)[index]; break; case 'H': val = ((unsigned short*)p)[index]; break; case 'i': return mp_obj_new_int(((int*)p)[index]); case 'I': return mp_obj_new_int_from_uint(((unsigned int*)p)[index]); case 'l': return mp_obj_new_int(((long*)p)[index]); case 'L': return mp_obj_new_int_from_uint(((unsigned long*)p)[index]); #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE case 'q': case 'Q': // TODO: Explode API more to cover signedness return mp_obj_new_int_from_ll(((long long*)p)[index]); #endif #if MICROPY_PY_BUILTINS_FLOAT case 'f': return mp_obj_new_float(((float*)p)[index]); case 'd': return mp_obj_new_float(((double*)p)[index]); #endif } return MP_OBJ_NEW_SMALL_INT(val); }
/// \function mktime() /// This is inverse function of localtime. It's argument is a full 8-tuple /// which expresses a time as per localtime. It returns an integer which is /// the number of seconds since Jan 1, 2000. STATIC mp_obj_t time_mktime(mp_obj_t tuple) { size_t len; mp_obj_t *elem; mp_obj_get_array(tuple, &len, &elem); // localtime generates a tuple of len 8. CPython uses 9, so we accept both. if (len < 8 || len > 9) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len)); } return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); }
STATIC mp_obj_t sd_info(mp_obj_t self) { if (sd_handle.Instance == NULL) { return mp_const_none; } HAL_SD_CardInfoTypedef cardinfo; HAL_SD_Get_CardInfo(&sd_handle, &cardinfo); // cardinfo.SD_csd and cardinfo.SD_cid have lots of info but we don't use them mp_obj_t tuple[3] = { mp_obj_new_int_from_ull(cardinfo.CardCapacity), mp_obj_new_int_from_uint(cardinfo.CardBlockSize), mp_obj_new_int(cardinfo.CardType), }; return mp_obj_new_tuple(3, tuple); }
STATIC mp_obj_t time_mktime(mp_obj_t tuple) { mp_uint_t len; mp_obj_t *elem; mp_obj_get_array(tuple, &len, &elem); // localtime generates a tuple of len 8. CPython uses 9, so we accept both. if (len < 8 || len > 9) { nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments)); } return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); }
STATIC mp_obj_t esp_flash_size(void) { extern char flashchip; // For SDK 1.5.2, either address has shifted and not mirrored in // eagle.rom.addr.v6.ld, or extra initial member was added. SpiFlashChip *flash = (SpiFlashChip*)(&flashchip + 4); #if 0 printf("deviceId: %x\n", flash->deviceId); printf("chip_size: %u\n", flash->chip_size); printf("block_size: %u\n", flash->block_size); printf("sector_size: %u\n", flash->sector_size); printf("page_size: %u\n", flash->page_size); printf("status_mask: %u\n", flash->status_mask); #endif return mp_obj_new_int_from_uint(flash->chip_size); }
// method socket.send(bytes) STATIC mp_obj_t socket_send(mp_obj_t self_in, mp_obj_t buf_in) { mod_network_socket_obj_t *self = self_in; if (self->nic == MP_OBJ_NULL) { // not connected mp_raise_OSError(MP_EPIPE); } mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_READ); int _errno; mp_uint_t ret = self->nic_type->send(self, bufinfo.buf, bufinfo.len, &_errno); if (ret == -1) { mp_raise_OSError(_errno); } return mp_obj_new_int_from_uint(ret); }
mp_obj_t mp_obj_id(mp_obj_t o_in) { mp_int_t id = (mp_int_t)o_in; if (!MP_OBJ_IS_OBJ(o_in)) { return mp_obj_new_int(id); } else if (id >= 0) { // Many OSes and CPUs have affinity for putting "user" memories // into low half of address space, and "system" into upper half. // We're going to take advantage of that and return small int // (signed) for such "user" addresses. return MP_OBJ_NEW_SMALL_INT(id); } else { // If that didn't work, well, let's return long int, just as // a (big) positve value, so it will never clash with the range // of small int returned in previous case. return mp_obj_new_int_from_uint((mp_uint_t)id); } }
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) { byte *p = *ptr; mp_uint_t align; size_t size = mp_binary_get_size(struct_type, val_type, &align); if (struct_type == '@') { // Make pointer aligned p = (byte*)(((mp_uint_t)p + align - 1) & ~((mp_uint_t)align - 1)); #if MP_ENDIANNESS_LITTLE struct_type = '<'; #else struct_type = '>'; #endif } *ptr = p + size; long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p); if (val_type == 'O') { return (mp_obj_t)(mp_uint_t)val; } else if (val_type == 'S') { const char *s_val = (const char*)(mp_uint_t)val; return mp_obj_new_str(s_val, strlen(s_val), false); #if MICROPY_PY_BUILTINS_FLOAT } else if (val_type == 'f') { union { uint32_t i; float f; } fpu = {val}; return mp_obj_new_float(fpu.f); } else if (val_type == 'd') { union { uint64_t i; double f; } fpu = {val}; return mp_obj_new_float(fpu.f); #endif } else if (is_signed(val_type)) { if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) { return mp_obj_new_int((mp_int_t)val); } else { return mp_obj_new_int_from_ll(val); } } else { if ((unsigned long long)val <= (unsigned long long)MP_SMALL_INT_MAX) { return mp_obj_new_int_from_uint((mp_uint_t)val); } else { return mp_obj_new_int_from_ull(val); } } }
STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) { const mp_stream_p_t *stream_p = mp_get_stream_raise(args[0], MP_STREAM_OP_IOCTL); struct mp_stream_seek_t seek_s; // TODO: Could be uint64 seek_s.offset = mp_obj_get_int(args[1]); seek_s.whence = 0; if (n_args == 3) { seek_s.whence = mp_obj_get_int(args[2]); } int error; mp_uint_t res = stream_p->ioctl(args[0], MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error); if (res == MP_STREAM_ERROR) { mp_raise_OSError(error); } // TODO: Could be uint64 return mp_obj_new_int_from_uint(seek_s.offset); }
mp_obj_t file_obj_seek(mp_uint_t n_args, const mp_obj_t *args) { pyb_file_obj_t *self = args[0]; mp_int_t offset = mp_obj_get_int(args[1]); mp_int_t whence = 0; if (n_args == 3) { whence = mp_obj_get_int(args[2]); } switch (whence) { case 0: // SEEK_SET f_lseek(&self->fp, offset); break; case 1: // SEEK_CUR if (offset != 0) { goto error; } // no-operation break; case 2: // SEEK_END if (offset != 0) { goto error; } f_lseek(&self->fp, f_size(&self->fp)); break; default: goto error; } return mp_obj_new_int_from_uint(f_tell(&self->fp)); error: // A bad whence is a ValueError, while offset!=0 is an io.UnsupportedOperation. // But the latter inherits ValueError (as well as IOError), so we just raise ValueError. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "invalid whence and/or offset")); }
mp_obj_t mp_binary_get_val(char typecode, void *p, int index) { machine_int_t val = 0; switch (typecode) { case 'b': val = ((int8_t*)p)[index]; break; case BYTEARRAY_TYPECODE: case 'B': val = ((uint8_t*)p)[index]; break; case 'h': val = ((int16_t*)p)[index]; break; case 'H': val = ((uint16_t*)p)[index]; break; case 'i': case 'l': return mp_obj_new_int(((int32_t*)p)[index]); case 'I': case 'L': return mp_obj_new_int_from_uint(((uint32_t*)p)[index]); #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE case 'q': case 'Q': // TODO: Explode API more to cover signedness return mp_obj_new_int_from_ll(((long long*)p)[index]); #endif #if MICROPY_ENABLE_FLOAT case 'f': return mp_obj_new_float(((float*)p)[index]); case 'd': return mp_obj_new_float(((double*)p)[index]); #endif } return MP_OBJ_NEW_SMALL_INT(val); }
STATIC mp_obj_t stream_seek(size_t n_args, const mp_obj_t *args) { struct mp_stream_seek_t seek_s; // TODO: Could be uint64 seek_s.offset = mp_obj_get_int(args[1]); seek_s.whence = SEEK_SET; if (n_args == 3) { seek_s.whence = mp_obj_get_int(args[2]); } // In POSIX, it's error to seek before end of stream, we enforce it here. if (seek_s.whence == SEEK_SET && seek_s.offset < 0) { mp_raise_OSError(MP_EINVAL); } const mp_stream_p_t *stream_p = mp_get_stream(args[0]); int error; mp_uint_t res = stream_p->ioctl(args[0], MP_STREAM_SEEK, (mp_uint_t)(uintptr_t)&seek_s, &error); if (res == MP_STREAM_ERROR) { mp_raise_OSError(error); } // TODO: Could be uint64 return mp_obj_new_int_from_uint(seek_s.offset); }
/// \function mktime() /// This is inverse function of localtime. It's argument is a full 8-tuple /// which expresses a time as per localtime. It returns an integer which is /// the number of seconds since Jan 1, 2000. STATIC mp_obj_t time_mktime(mp_obj_t tuple) { mp_uint_t len; mp_obj_t *elem; mp_obj_get_array(tuple, &len, &elem); // localtime generates a tuple of len 8. CPython uses 9, so we accept both. if (len < 8 || len > 9) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len)); } mp_int_t year = mp_obj_get_int(elem[0]); mp_int_t month = mp_obj_get_int(elem[1]); mp_int_t mday = mp_obj_get_int(elem[2]); mp_int_t hours = mp_obj_get_int(elem[3]); mp_int_t minutes = mp_obj_get_int(elem[4]); mp_int_t seconds = mp_obj_get_int(elem[5]); // Normalise the tuple. This allows things like: // // tm_tomorrow = list(time.localtime()) // tm_tomorrow[2] += 1 # Adds 1 to mday // tomorrow = time.mktime(tm_tommorrow) // // And not have to worry about all the weird overflows. // // You can subtract dates/times this way as well. minutes += seconds / 60; if ((seconds = seconds % 60) < 0) { seconds += 60; minutes--; } hours += minutes / 60; if ((minutes = minutes % 60) < 0) { minutes += 60; hours--; } mday += hours / 24; if ((hours = hours % 24) < 0) { hours += 24; mday--; } month--; // make month zero based year += month / 12; if ((month = month % 12) < 0) { month += 12; year--; } month++; // back to one based while (mday < 1) { if (--month == 0) { month = 12; year--; } mday += mod_time_days_in_month(year, month); } while (mday > mod_time_days_in_month(year, month)) { mday -= mod_time_days_in_month(year, month); if (++month == 13) { month = 1; year++; } } return mp_obj_new_int_from_uint(mod_time_seconds_since_2000(year, month, mday, hours, minutes, seconds)); }
STATIC mp_obj_t mod_thread_get_ident(void) { return mp_obj_new_int_from_uint((uintptr_t)mp_thread_get_state()); }
mp_obj_t file_obj_tell(mp_obj_t self_in) { pyb_file_obj_t *self = self_in; return mp_obj_new_int_from_uint(f_tell(&self->fp)); }