// читать из порта int port_read ( const askue_port_t *Port, uint8_array_t *u8a, long int timeout ) { ldiv_t Sec = ldiv ( timeout, 1000 ); struct timeval Timeout = { Sec.quot, Sec.rem * 1000 }; fd_set ReadSet; FD_ZERO ( &ReadSet ); FD_SET ( Port->RS232, &ReadSet ); int RetVal = select( Port->RS232 + 1, &ReadSet, NULL, NULL, &Timeout); if ( RetVal == 0 ) { return 0; } else if ( RetVal == -1 ) { return -1; } else { assert ( FD_ISSET ( Port->RS232, &ReadSet ) ); return __port_read ( Port, u8a ); } }
/* step */ void zmq_sender_step(ubx_block_t *b) { struct zmq_sender_info *inf = (struct zmq_sender_info*) b->private_data; // std::cout << "zmq_sender: Processing a port update" << std::endl; /* Read data from port */ ubx_port_t* port = inf->ports.zmq_out; assert(port != 0); ubx_data_t msg; checktype(port->block->ni, port->in_type, "unsigned char", port->name, 1); msg.type = port->in_type; msg.len = inf->buffer_length; msg.data = inf->buffer; // std::cout << "zmq_sender: Reading from port" << std::endl; int read_bytes = __port_read(port, &msg); if (read_bytes <= 0) { // std::cout << "zmq_sender: No data recieved from port" << std::endl; return; } std::cout << "zmq_sender: read bytes = " << read_bytes << std::endl; /* Setup ZMQ frame. At this point only single frames are sent. This can be replaced by zmsg_t messages if multi-part messages become necessary*/ zframe_t* message = zframe_new(msg.data, read_bytes); std::cout << "Created frame of length " << zframe_size(message) << std::endl; /* Send the message */ int result = zframe_send(&message, inf->publisher,0); std::cout << "send message with result " << result << std::endl; }
/* step */ void zmq_sender_step(ubx_block_t *b) { struct zmq_sender_info *inf = (struct zmq_sender_info*) b->private_data; std::cout << "zmq_sender: Processing a port update" << std::endl; /* Read data from port */ ubx_port_t* port = inf->ports.zmq_out; assert(port != 0); ubx_data_t msg; checktype(port->block->ni, port->in_type, "unsigned char", port->name, 1); msg.type = port->in_type; msg.len = inf->buffer_length; msg.data = inf->buffer; std::cout << "zmq_sender: Reading from port" << std::endl; int read_bytes = __port_read(port, &msg); if (read_bytes <= 0) { std::cout << "zmq_sender: No data recieved from port" << std::endl; return; } /* Test message */ // unsigned long length = 4; // unsigned char buffer[length]; // buffer[0] = 0xDE; //DEAFBEAF dummy message // buffer[1] = 0xAF; // buffer[2] = 0xBE; // buffer[3] = 0xAF; // msg.len = length; // msg.data = (void*)&buffer; // int read_bytes = length; std::cout << "zmq_sender: msg.len = " << data_size(&msg) << " read bytes = " << read_bytes << std::endl; /* Setup ZMQ message*/ //hexdump((unsigned char *)msg.data, read_bytes, 16); zmq::message_t message(msg.len); memcpy(message.data(), (char *)msg.data, read_bytes); /* Send the message */ inf->publisher->send(message); }
/* step */ void sherpa_msg_step(ubx_block_t *b) { struct sherpa_msg_info *inf = (struct sherpa_msg_info*) b->private_data; // std::cout << "sherpa_msg: Processing a port update" << std::endl; /* Read data from port */ ubx_port_t* port = inf->ports.msg_in; assert(port != 0); ubx_data_t msg; checktype(port->block->ni, port->in_type, "unsigned char", port->name, 1); msg.type = port->in_type; msg.len = inf->buffer_length; msg.data = inf->buffer; // std::cout << "sherpa_msg: Reading from port" << std::endl; int read_bytes = __port_read(port, &msg); if (read_bytes <= 0) { // std::cout << "sherpa_msg: No data recieved from port" << std::endl; return; } std::cout << "sherpa_msg: read bytes = " << read_bytes << std::endl; /* Either decode or encode message */ /* Write result back t0 port */ ubx_type_t* type = ubx_type_get(b->ni, "unsigned char"); ubx_data_t result_msg; // result_msg.data = (void *)zframe_data(frame); // result_msg.len = zframe_size(frame); result_msg.type = type; //hexdump((unsigned char *)result_msg.data, result_msg.len, 16); __port_write(inf->ports.msg_out, &result_msg); }
/** * luablock_step - execute lua string and call step hook * * @param b */ static void luablock_step(ubx_block_t *b) { int len=0, ret; struct luablock_info* inf = (struct luablock_info*) b->private_data; /* any lua code to execute */ ubx_port_t* p_exec_str = ubx_port_get(b, "exec_str"); if((len=__port_read(p_exec_str, inf->exec_str_buff))>0) { if ((ret=luaL_dostring(inf->L, inf->exec_str_buff->data)) != 0) { ERR("Failed to exec_str: %s", lua_tostring(inf->L, -1)); goto out; } } call_hook(b, "step", 0, 0); out: /* TODO: fix this. realloc could have changed port addr */ if(len>0) { p_exec_str = ubx_port_get(b, "exec_str"); write_int(p_exec_str, &ret); } return; }
/* step */ void zyre_bridge_step(ubx_block_t *b) { struct zyre_bridge_info *inf = (struct zyre_bridge_info*) b->private_data; /* Read data from port */ ubx_port_t* port = inf->ports.zyre_out; assert(port != 0); char * tmp_str = (char*) malloc(inf->max_msg_length*sizeof(char*)); ubx_data_t msg; checktype(port->block->ni, port->in_type, "unsigned char", port->name, 1); msg.type = port->in_type; msg.len = inf->max_msg_length; msg.data = tmp_str; //msg.data = inf->msg_buffer; int counter = 0; while (counter < inf->max_send) { int read_bytes = __port_read(port, &msg); //printf("zyrebidge: read bytes: %d\n",read_bytes); //printf("step: read strlen: %lu\n",strlen((char*) msg.data)); if (read_bytes <= 0) { //printf("zyre_bridge: No data recieved from port\n"); free(tmp_str); return; } // printf("zyrebridge: read bytes: %d\n",read_bytes); // port_read returns byte array. Need to add 0 termination manually to the string. tmp_str[read_bytes] = '\0'; // create json object and... json_t *pl; json_error_t error; pl= json_loads(tmp_str,0,&error); if(!pl) { printf("Error parsing JSON payload! line %d: %s\n", error.line, error.text); json_decref(pl); free(tmp_str); return; } // printf("[zyrebidge] retrieving msg: %s\n", json_dumps(pl, JSON_ENCODE_ANY)); // ...check for its type and embed it into msg envelope json_t *new_msg; new_msg = json_object(); json_object_set(new_msg, "payload", pl); json_object_set(new_msg, "metamodel", json_string("SHERPA")); if(json_object_get(pl, "@worldmodeltype") == 0) { printf("[zyrebidge] retrieving msg: %s\n", json_dumps(pl, JSON_ENCODE_ANY)); printf("[zyrebidge] Error parsing RSG payload! @worldmodeltype is missing.\n"); json_decref(pl); free(tmp_str); return; } std::string tmp_type = json_string_value(json_object_get(pl, "@worldmodeltype")); //can segfault char *send_msg; for (int i=0; i < inf->output_type_list.size();i++) { if (tmp_type.compare(inf->output_type_list[i])) { // need to handle exception for updates generated from RSG due to local updates if (tmp_type.compare("RSGUpdate") == 0) { json_object_set(new_msg, "model", json_string("RSGUpdate")); json_object_set(new_msg, "type", json_string("RSGUpdate_global")); // If used with mediator, add send_request envelope ubx_data_t *dmy; dmy = ubx_config_get_data(b, "mediator"); int mediator; mediator = *(int*) dmy->data; if (mediator == 1) { zuuid_t *query_uuid = zuuid_new (); assert (query_uuid); json_t *recip = json_array(); assert((recip)&&(json_array_size(recip)==0)); send_msg = send_request(zuuid_str(query_uuid),zyre_uuid(inf->node),recip,1000,"send_remote",new_msg); } else { send_msg = json_dumps(new_msg, JSON_ENCODE_ANY); } } else { json_object_set(new_msg, "model", json_string(tmp_type.c_str())); json_object_set(new_msg, "type", json_string(tmp_type.c_str())); send_msg = json_dumps(new_msg, JSON_ENCODE_ANY); } } else { printf("[zyre_bridge] Unknown output type: %s!\n",tmp_type.c_str()); } } printf("[zyrebidge] sending msg: %s\n", send_msg); zyre_shouts(inf->node, inf->group, "%s", send_msg); counter++; json_decref(pl); json_decref(new_msg); } free(tmp_str); return; }