1 #include <stdio.h> 2 #include <string.h> 3 4 #include "py/mperrno.h" 5 #include "py/obj.h" 6 #include "py/runtime.h" 7 #include "py/builtin.h" 8 9 #if 0 10 11 extern const mp_obj_type_t pyb_ipc_type; 12 // this is the actual C-structure for our new object 13 typedef struct _pyb_ipc_obj_t 14 { 15 // base represents some basic information, like type 16 mp_obj_base_t base; 17 // a member created by us 18 kbuf_queue_t* queue; 19 } pyb_ipc_obj_t; 20 21 22 /* 23 STATIC const pyb_led_obj_t pyb_led_obj[] = { 24 {{&pyb_led_type}, PYB_LED_TOP_LEFT_1}, 25 {{&pyb_led_type}, PYB_LED_TOP_RIGHT_1}, 26 {{&pyb_led_type}, PYB_LED_TOP_RIGHT_2}, 27 {{&pyb_led_type}, PYB_LED_TOP_LEFT_2}, 28 }; 29 */ 30 31 void ipc_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) 32 { 33 pyb_ipc_obj_t *self = MP_OBJ_TO_PTR(self_in); 34 mp_printf(print, "queue(%u)", self->queue); 35 } 36 37 STATIC mp_obj_t ipc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) 38 { 39 kbuf_queue_t *queue; 40 // check arguments 41 mp_arg_check_num(n_args, n_kw, 3, 3, false); 42 43 44 char* pname = (char*)mp_obj_str_get_str(args[0]); 45 //printf("name:%s\r\n",pname); 46 47 48 mp_uint_t size = mp_obj_get_int(args[1]); 49 mp_uint_t max_msg = mp_obj_get_int(args[2]); 50 51 52 pyb_ipc_obj_t* ipc_obj = m_new_obj(pyb_ipc_obj_t); 53 if (!ipc_obj) { 54 mp_raise_OSError(ENOMEM); 55 } 56 57 mp_int_t ret = krhino_buf_queue_dyn_create(&queue, pname, size, max_msg); 58 if(ret){ 59 mp_raise_OSError(EFAULT); 60 } 61 62 ipc_obj->base.type = &pyb_ipc_type; 63 ipc_obj->queue = queue; 64 65 return MP_OBJ_FROM_PTR(ipc_obj); 66 } 67 68 69 static mp_obj_t ipc_obj_send(mp_obj_t self_in, mp_obj_t msg_in, mp_obj_t size_in) 70 { 71 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(self_in); 72 pyb_ipc_obj_t* ipc_obj = (pyb_ipc_obj_t *)self; 73 74 void *msg = (void *)mp_obj_get_int(msg_in); 75 mp_uint_t size = mp_obj_get_int(size_in); 76 77 mp_int_t ret = krhino_buf_queue_send(ipc_obj->queue, msg, size); 78 if(ret){ 79 printf("err:%d\r\n",ret); 80 mp_raise_OSError(EFAULT); 81 } 82 83 return mp_const_none; 84 } 85 STATIC MP_DEFINE_CONST_FUN_OBJ_3(ipc_obj_send_obj, ipc_obj_send); 86 87 88 static mp_obj_t ipc_obj_recv(size_t n_args, const mp_obj_t *args) 89 { 90 mp_obj_base_t *self = (mp_obj_base_t*)MP_OBJ_TO_PTR(args[0]); 91 pyb_ipc_obj_t* ipc_obj = (pyb_ipc_obj_t *)self; 92 93 mp_uint_t tick = mp_obj_get_int(args[1]); 94 void *msg = (void *)mp_obj_get_int(args[2]); 95 void *psize = mp_obj_get_int(args[3]); 96 97 mp_int_t ret = krhino_buf_queue_recv(ipc_obj->queue, tick , msg, psize); 98 if(ret && ((ret != RHINO_NO_PEND_WAIT) && (ret != RHINO_BLK_TIMEOUT))){ 99 printf("ipc_obj_recv ret:%d\r\n",ret); 100 mp_raise_OSError(EFAULT); 101 } 102 103 return mp_const_none; 104 } 105 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(ipc_obj_recv_obj, 4, ipc_obj_recv); 106 107 108 109 STATIC const mp_rom_map_elem_t ipc_locals_dict_table[] = { 110 {MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ipc)}, 111 {MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&ipc_obj_send_obj)}, 112 {MP_ROM_QSTR(MP_QSTR_recv), MP_ROM_PTR(&ipc_obj_recv_obj)}, 113 }; 114 115 STATIC MP_DEFINE_CONST_DICT(ipc_locals_dict, ipc_locals_dict_table); 116 117 const mp_obj_type_t pyb_ipc_type = { 118 .base = {&mp_type_type}, 119 .name = MP_QSTR_ipc, 120 .print = ipc_obj_print, 121 .make_new = ipc_obj_make_new, 122 .locals_dict = (mp_obj_dict_t *)&ipc_locals_dict, 123 }; 124 125 #endif 126