int ei_x_encode_binary(ei_x_buff* x, const void* p, int len) { int i = x->index; ei_encode_binary(NULL, &i, p, len); if (!x_fix_buff(x, i)) return -1; return ei_encode_binary(x->buff, &x->index, p, len); }
static void enumerate_ports() { struct serial_info *port_list = find_serialports(); int port_list_len = 0; for (struct serial_info *port = port_list; port != NULL; port = port->next) port_list_len++; debug("Found %d ports", port_list_len); char resp[4096]; int resp_index = sizeof(uint16_t); // Space for payload size resp[resp_index++] = response_id; ei_encode_version(resp, &resp_index); ei_encode_map_header(resp, &resp_index, port_list_len); for (struct serial_info *port = port_list; port != NULL; port = port->next) { ei_encode_binary(resp, &resp_index, port->name, strlen(port->name)); int info_count = (port->description ? 1 : 0) + (port->manufacturer ? 1 : 0) + (port->serial_number ? 1 : 0) + (port->vid > 0 ? 1 : 0) + (port->pid > 0 ? 1 : 0); ei_encode_map_header(resp, &resp_index, info_count); if (port->description) { ei_encode_atom(resp, &resp_index, "description"); ei_encode_binary(resp, &resp_index, port->description, strlen(port->description)); } if (port->manufacturer) { ei_encode_atom(resp, &resp_index, "manufacturer"); ei_encode_binary(resp, &resp_index, port->manufacturer, strlen(port->manufacturer)); } if (port->serial_number) { ei_encode_atom(resp, &resp_index, "serial_number"); ei_encode_binary(resp, &resp_index, port->serial_number, strlen(port->serial_number)); } if (port->vid > 0) { ei_encode_atom(resp, &resp_index, "vendor_id"); ei_encode_ulong(resp, &resp_index, port->vid); } if (port->pid > 0) { ei_encode_atom(resp, &resp_index, "product_id"); ei_encode_ulong(resp, &resp_index, port->pid); } } erlcmd_send(resp, resp_index); serial_info_free_list(port_list); }
static int mn_send_write(int fd, erlang_pid *mnesia, const char *key, ei_reg_obj *obj) { char sbuf[EISMALLBUF]; char *dbuf = NULL; char *msgbuf; int index = 0; int keylen = strlen(key) + 1; int len = 32 + keylen + obj->size; if (len > EISMALLBUF) if (!(dbuf = malloc(len))) return -1; msgbuf = (dbuf ? dbuf : sbuf); ei_encode_version(msgbuf,&index); ei_encode_tuple_header(msgbuf,&index,6); ei_encode_atom(msgbuf,&index,EI_MNESIA_WRITE); ei_encode_string(msgbuf,&index,key); ei_encode_long(msgbuf,&index,keylen); ei_encode_long(msgbuf,&index,obj->attr); ei_encode_long(msgbuf,&index,obj->size); switch (ei_reg_typeof(obj)) { case EI_INT: ei_encode_long(msgbuf,&index,obj->val.i); break; case EI_FLT: ei_encode_double(msgbuf,&index,obj->val.f); break; case EI_STR: if (obj->size > 0) ei_encode_string(msgbuf,&index,obj->val.s); else ei_encode_long(msgbuf,&index, (long)NULL); /* just the NULL pointer */ break; case EI_BIN: if (obj->size > 0) ei_encode_binary(msgbuf,&index,obj->val.p,obj->size); else ei_encode_long(msgbuf,&index,(long)(obj->val.p)); /* just the pointer */ break; default: if (dbuf) free(dbuf); return -1; } /* send it */ if (ei_send_encoded(fd,mnesia,msgbuf,index)) { if (dbuf) free(dbuf); return -1; } if (dbuf) free(dbuf); return 0; }
/* Called when uart_read completes or fails */ static void handle_read_completed(int rc, const uint8_t *data, size_t len) { if (rc >= 0) { char *resp = malloc(32 + len); int resp_index = sizeof(uint16_t); ei_encode_version(resp, &resp_index); ei_encode_tuple_header(resp, &resp_index, 2); ei_encode_atom(resp, &resp_index, "ok"); ei_encode_binary(resp, &resp_index, data, len); erlcmd_send(resp, resp_index); free(resp); } else send_error_response(uart_last_error()); }
int oe_encode_erlang_binary(CORBA_Environment *ev, erlang_binary *binary) { int size = ev->_iout; ei_encode_binary(0, &size, binary->_buffer, binary->_length); if (size >= ev->_outbufsz) { char *buf = ev->_outbuf; int bufsz = ev->_outbufsz + ev->_memchunk; while (size >= bufsz) bufsz += ev->_memchunk; if ((buf = realloc(buf, bufsz)) == NULL) { CORBA_exc_set(ev, CORBA_SYSTEM_EXCEPTION, NO_MEMORY, "End of heap memory while encoding"); return -1; /* OUT OF MEMORY */ } ev->_outbuf = buf; ev->_outbufsz = bufsz; } return ei_encode_binary(ev->_outbuf, &ev->_iout, binary->_buffer, binary->_length); }
/* Called in active mode when there's a read or an error */ static void handle_notify_read(int error_reason, const uint8_t *data, size_t len) { char *resp = malloc(64 + len); int resp_index = sizeof(uint16_t); ei_encode_version(resp, &resp_index); ei_encode_tuple_header(resp, &resp_index, 2); ei_encode_atom(resp, &resp_index, "notif"); if (error_reason == 0) { // Normal receive ei_encode_binary(resp, &resp_index, data, len); } else { // Error notification ei_encode_tuple_header(resp, &resp_index, 2); ei_encode_atom(resp, &resp_index, "error"); ei_encode_atom(resp, &resp_index, uart_last_error()); } erlcmd_send(resp, resp_index); free(resp); }
int main(void) #endif { ErlConnect conp; Erl_IpAddr thisipaddr = (Erl_IpAddr)0; FILE *fp = (FILE *)0; char* charp = "foo"; double *doublep = NULL; double doublex = 0.0; ei_cnode xec; ei_reg *ei_regp = NULL; ei_term eterm; ei_x_buff eix; erlang_big *bigp = NULL; erlang_fun efun; erlang_msg *msgp = NULL; erlang_msg emsg; erlang_pid *pidp = NULL; erlang_pid epid; erlang_port eport; erlang_ref eref; erlang_trace etrace; int *intp = NULL; int intx = 0; long *longp = NULL; long longx = 0; short creation = 0; struct ei_reg_stat *ei_reg_statp = NULL; struct ei_reg_tabstat *ei_reg_tabstatp = NULL; struct hostent *hostp = NULL; unsigned char * ucharp = (unsigned char *)"foo"; unsigned long *ulongp = NULL; unsigned long ulongx = 0; void *voidp = NULL; #ifndef VXWORKS EI_LONGLONG *longlongp = (EI_LONGLONG*)NULL; EI_LONGLONG longlongx = 0; EI_ULONGLONG *ulonglongp = (EI_ULONGLONG*)NULL; EI_ULONGLONG ulonglongx = 0; #endif enum erlang_char_encoding enc; intx = erl_errno; ei_connect_init(&xec, charp, charp, creation); ei_connect_xinit (&xec, charp, charp, charp, thisipaddr, charp, creation); ei_connect(&xec, charp); ei_xconnect (&xec, thisipaddr, charp); ei_receive(intx, ucharp, intx); ei_receive_msg(intx, &emsg, &eix); ei_xreceive_msg(intx, &emsg, &eix); ei_send(intx, &epid, charp, intx); ei_reg_send(&xec, intx, charp, charp, intx); ei_rpc(&xec, intx, charp, charp, charp, intx, &eix); ei_rpc_to(&xec, intx, charp, charp, charp, intx); ei_rpc_from(&xec, intx, intx, &emsg, &eix); ei_publish(&xec, intx); ei_accept(&xec, intx, &conp); ei_unpublish(&xec); ei_thisnodename(&xec); ei_thishostname(&xec); ei_thisalivename(&xec); ei_self(&xec); ei_gethostbyname(charp); ei_gethostbyaddr(charp, intx, intx); ei_gethostbyname_r(charp, hostp, charp, intx, intp); ei_gethostbyaddr_r(charp, intx, intx, hostp, charp, intx, intp); ei_encode_version(charp, intp); ei_x_encode_version(&eix); ei_encode_long(charp, intp, longx); ei_x_encode_long(&eix, longx); ei_encode_ulong(charp, intp, ulongx); ei_x_encode_ulong(&eix, ulongx); ei_encode_double(charp, intp, doublex); ei_x_encode_double(&eix, doublex); ei_encode_boolean(charp, intp, intx); ei_x_encode_boolean(&eix, intx); ei_encode_char(charp, intp, 'a'); ei_x_encode_char(&eix, 'a'); ei_encode_string(charp, intp, charp); ei_encode_string_len(charp, intp, charp, intx); ei_x_encode_string(&eix, charp); ei_x_encode_string_len(&eix, charp, intx); ei_encode_atom(charp, intp, charp); ei_encode_atom_as(charp, intp, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_atom_len(charp, intp, charp, intx); ei_encode_atom_len_as(charp, intp, charp, intx, ERLANG_ASCII, ERLANG_LATIN1); ei_x_encode_atom(&eix, charp); ei_x_encode_atom_as(&eix, charp, ERLANG_LATIN1, ERLANG_UTF8); ei_x_encode_atom_len(&eix, charp, intx); ei_x_encode_atom_len_as(&eix, charp, intx, ERLANG_LATIN1, ERLANG_UTF8); ei_encode_binary(charp, intp, (void *)0, longx); ei_x_encode_binary(&eix, (void*)0, intx); ei_encode_pid(charp, intp, &epid); ei_x_encode_pid(&eix, &epid); ei_encode_fun(charp, intp, &efun); ei_x_encode_fun(&eix, &efun); ei_encode_port(charp, intp, &eport); ei_x_encode_port(&eix, &eport); ei_encode_ref(charp, intp, &eref); ei_x_encode_ref(&eix, &eref); ei_encode_trace(charp, intp, &etrace); ei_x_encode_trace(&eix, &etrace); ei_encode_tuple_header(charp, intp, intx); ei_x_encode_tuple_header(&eix, longx); ei_encode_list_header(charp, intp, intx); ei_x_encode_list_header(&eix, longx); /* #define ei_encode_empty_list(buf,i) ei_encode_list_header(buf,i,0) */ ei_x_encode_empty_list(&eix); ei_get_type(charp, intp, intp, intp); ei_get_type_internal(charp, intp, intp, intp); ei_decode_version(charp, intp, intp); ei_decode_long(charp, intp, longp); ei_decode_ulong(charp, intp, ulongp); ei_decode_double(charp, intp, doublep); ei_decode_boolean(charp, intp, intp); ei_decode_char(charp, intp, charp); ei_decode_string(charp, intp, charp); ei_decode_atom(charp, intp, charp); ei_decode_atom_as(charp, intp, charp, MAXATOMLEN_UTF8, ERLANG_WHATEVER, &enc, &enc); ei_decode_binary(charp, intp, (void *)0, longp); ei_decode_fun(charp, intp, &efun); free_fun(&efun); ei_decode_pid(charp, intp, &epid); ei_decode_port(charp, intp, &eport); ei_decode_ref(charp, intp, &eref); ei_decode_trace(charp, intp, &etrace); ei_decode_tuple_header(charp, intp, intp); ei_decode_list_header(charp, intp, intp); ei_decode_ei_term(charp, intp, &eterm); ei_print_term(fp, charp, intp); ei_s_print_term(&charp, charp, intp); ei_x_format(&eix, charp); ei_x_format_wo_ver(&eix, charp); ei_x_new(&eix); ei_x_new_with_version(&eix); ei_x_free(&eix); ei_x_append(&eix, &eix); ei_x_append_buf(&eix, charp, intx); ei_skip_term(charp, intp); ei_reg_open(intx); ei_reg_resize(ei_regp, intx); ei_reg_close(ei_regp); ei_reg_setival(ei_regp, charp, longx); ei_reg_setfval(ei_regp, charp, doublex); ei_reg_setsval(ei_regp, charp, charp); ei_reg_setpval(ei_regp, charp, voidp, intx); ei_reg_setval(ei_regp, charp, intx); ei_reg_getival(ei_regp, charp); ei_reg_getfval(ei_regp, charp); ei_reg_getsval(ei_regp, charp); ei_reg_getpval(ei_regp, charp, intp); ei_reg_getval(ei_regp, charp, intx); ei_reg_markdirty(ei_regp, charp); ei_reg_delete(ei_regp, charp); ei_reg_stat(ei_regp, charp, ei_reg_statp); ei_reg_tabstat(ei_regp, ei_reg_tabstatp); ei_reg_dump(intx, ei_regp, charp, intx); ei_reg_restore(intx, ei_regp, charp); ei_reg_purge(ei_regp); #if defined(HAVE_GMP_H) && defined(HAVE_LIBGMP) { mpz_t obj; ei_decode_bignum(charp, intp, obj); ei_encode_bignum(charp, intp, obj); ei_x_encode_bignum(&eix, obj); } #endif /* HAVE_GMP_H && HAVE_LIBGMP */ #ifndef VXWORKS ei_decode_longlong(charp, intp, longlongp); ei_decode_ulonglong(charp, intp, ulonglongp); ei_encode_longlong(charp, intp, longlongx); ei_encode_ulonglong(charp, intp, ulonglongx); ei_x_encode_longlong(&eix, longlongx); ei_x_encode_ulonglong(&eix, ulonglongx); #endif #ifdef USE_EI_UNDOCUMENTED ei_decode_intlist(charp, intp, longp, intp); ei_receive_encoded(intx, &charp, intp, msgp, intp); ei_send_encoded(intx, pidp, charp, intx); ei_send_reg_encoded(intx, pidp, charp, charp, intx); ei_decode_big(charp, intp, bigp); ei_big_comp(bigp, bigp); ei_big_to_double(bigp, doublep); ei_small_to_big(intx, bigp); ei_alloc_big(intx); ei_free_big(bigp); #endif /* USE_EI_UNDOCUMENTED */ return BUFSIZ + EAGAIN + EHOSTUNREACH + EIO + EI_BIN + EI_DELET + EI_DIRTY + EI_FLT + EI_FORCE + EI_INT + EI_NOPURGE + EI_STR + EMSGSIZE + ENOMEM + ERL_ERROR + ERL_EXIT + ERL_LINK + ERL_MSG + ERL_NO_TIMEOUT + ERL_REG_SEND + ERL_SEND + ERL_TICK + ERL_TIMEOUT + ERL_UNLINK + ETIMEDOUT + MAXATOMLEN; }
static void i2c_handle_request(const char *req, void *cookie) { struct i2c_info *i2c = (struct i2c_info *) cookie; // Commands are of the form {Command, Arguments}: // { atom(), term() } int req_index = sizeof(uint16_t); if (ei_decode_version(req, &req_index, NULL) < 0) errx(EXIT_FAILURE, "Message version issue?"); int arity; if (ei_decode_tuple_header(req, &req_index, &arity) < 0 || arity != 2) errx(EXIT_FAILURE, "expecting {cmd, args} tuple"); char cmd[MAXATOMLEN]; if (ei_decode_atom(req, &req_index, cmd) < 0) errx(EXIT_FAILURE, "expecting command atom"); char resp[256]; int resp_index = sizeof(uint16_t); // Space for payload size ei_encode_version(resp, &resp_index); if (strcmp(cmd, "read") == 0) { long int len; if (ei_decode_long(req, &req_index, &len) < 0 || len < 1 || len > I2C_SMBUS_BLOCK_MAX) errx(EXIT_FAILURE, "read amount: min=1, max=%d", I2C_SMBUS_BLOCK_MAX); char data[I2C_SMBUS_BLOCK_MAX]; if (i2c_transfer(i2c, 0, 0, data, len)) ei_encode_binary(resp, &resp_index, data,len); else { ei_encode_tuple_header(resp, &resp_index, 2); ei_encode_atom(resp, &resp_index, "error"); ei_encode_atom(resp, &resp_index, "i2c_read_failed"); } } else if (strcmp(cmd, "write") == 0) { char data[I2C_SMBUS_BLOCK_MAX]; int len; int type; long llen; if (ei_get_type(req, &req_index, &type, &len) < 0 || type != ERL_BINARY_EXT || len < 1 || len > I2C_SMBUS_BLOCK_MAX || ei_decode_binary(req, &req_index, &data, &llen) < 0) errx(EXIT_FAILURE, "write: need a binary between 1 and %d bytes", I2C_SMBUS_BLOCK_MAX); if (i2c_transfer(i2c, data, len, 0, 0)) ei_encode_atom(resp, &resp_index, "ok"); else { ei_encode_tuple_header(resp, &resp_index, 2); ei_encode_atom(resp, &resp_index, "error"); ei_encode_atom(resp, &resp_index, "i2c_write_failed"); } } else if (strcmp(cmd, "wrrd") == 0) { char write_data[I2C_SMBUS_BLOCK_MAX]; char read_data[I2C_SMBUS_BLOCK_MAX]; int write_len; long int read_len; int type; long llen; if (ei_decode_tuple_header(req, &req_index, &arity) < 0 || arity != 2) errx(EXIT_FAILURE, "wrrd: expecting {write_data, read_count} tuple"); if (ei_get_type(req, &req_index, &type, &write_len) < 0 || type != ERL_BINARY_EXT || write_len < 1 || write_len > I2C_SMBUS_BLOCK_MAX || ei_decode_binary(req, &req_index, &write_data, &llen) < 0) errx(EXIT_FAILURE, "wrrd: need a binary between 1 and %d bytes", I2C_SMBUS_BLOCK_MAX); if (ei_decode_long(req, &req_index, &read_len) < 0 || read_len < 1 || read_len > I2C_SMBUS_BLOCK_MAX) errx(EXIT_FAILURE, "wrrd: read amount: min=1, max=%d", I2C_SMBUS_BLOCK_MAX); if (i2c_transfer(i2c, write_data, write_len, read_data, read_len)) ei_encode_binary(resp, &resp_index, read_data, read_len); else { ei_encode_tuple_header(resp, &resp_index, 2); ei_encode_atom(resp, &resp_index, "error"); ei_encode_atom(resp, &resp_index, "i2c_wrrd_failed"); } } else errx(EXIT_FAILURE, "unknown command: %s", cmd); debug("sending response: %d bytes", resp_index); erlcmd_send(resp, resp_index); }