void client_thread() { transport_t trans; trans.init("localhost", 999); do { if (!trans.is_connecting()) { trans.try_connect(); } else { std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } while (!trans.is_connected()); { spmsg_t msg = trans.alloc_msg(3); fill_msg(msg, 3); trans.send(msg); } { spmsg_t msg = trans.alloc_msg(rand() % 100); fill_msg(msg, msg->size() - 4); trans.send(msg); } while (trans.is_connected()) { spmsg_t msg = trans.recv(); if (msg) { trans.send(msg); } std::this_thread::sleep_for(std::chrono::milliseconds(100)); } trans.destroy(); }
// Receives a message. If message is not immediately available, blocks // until one arrives. Use function returned in 'ffn' to deallocate the // received data chunk. bool receive (void **data, size_t *size, free_fn **ffn) { // If the other party have closed the socket, we cannot read // messages. if (peer_dead) return false; // The message to read msg_t msg = {NULL, 0, 0, NULL}; // Get and parse the message header unsigned char header_buf [9]; msg_t hdr1 = {header_buf, 1, 0, NULL}; if (!fill_msg (hdr1)) return false; if (header_buf [0] != 0xff) { msg.size = (size_t) hdr1.data [0]; } else { msg_t hdr2 = {header_buf + 1, 8, 0, NULL}; if (!fill_msg (hdr2)) assert (0); msg.size = get_size_64 (hdr2.data); } // Allocate the memory to store message body. msg.data = (unsigned char*) malloc (msg.size); assert (msg.data); if (!fill_msg (msg)) { free (msg.data); return false; } *data = msg.data; *size = msg.size; *ffn = msg.ffn; return true; }
/** * Run signal test */ int msg_testRun(void) { MsgPort test_portMain; TestMsg msg0; TestMsg msg1; TestMsg msg2; TestMsg msg3; TestMsg msg4; TestMsg msg5; TestMsg *reply; // Allocate and start the test process struct Process *recv0 = RECV_INIT_PROC(0); struct Process *recv1 = RECV_INIT_PROC(1); struct Process *recv2 = RECV_INIT_PROC(2); struct Process *recv3 = RECV_INIT_PROC(3); struct Process *recv4 = RECV_INIT_PROC(4); struct Process *recv5 = RECV_INIT_PROC(5); kprintf("Run Message test..\n"); // Init port and message RECV_INIT_MSG(Main, proc_current(), SIG_SINGLE); RECV_INIT_MSG(0, recv0, SIG_USER0); RECV_INIT_MSG(1, recv1, SIG_USER1); RECV_INIT_MSG(2, recv2, SIG_USER2); RECV_INIT_MSG(3, recv3, SIG_USER3); RECV_INIT_MSG(4, recv4, SIG_SYSTEM5); RECV_INIT_MSG(5, recv5, SIG_SYSTEM6); // Fill-in first message and send it out. fill_msg(&msg0, INC_PROC_T0, DELAY_PROC_T0, 0); fill_msg(&msg1, INC_PROC_T1, DELAY_PROC_T1, 0); fill_msg(&msg2, INC_PROC_T2, DELAY_PROC_T2, 0); fill_msg(&msg3, INC_PROC_T3, DELAY_PROC_T3, 0); fill_msg(&msg4, INC_PROC_T4, DELAY_PROC_T4, 0); fill_msg(&msg5, INC_PROC_T5, DELAY_PROC_T5, 0); // Send and wait the message for (int i = 0; i < 23; ++i) { process_num = 0; SEND_MSG(0); SEND_MSG(1); SEND_MSG(2); SEND_MSG(3); SEND_MSG(4); SEND_MSG(5); while(1) { sigmask_t sigs = sig_waitTimeout(SIG_SINGLE, ms_to_ticks(TEST_TIME_OUT_MS)); if (sigs & SIG_SINGLE) { // Wait for a reply... while ((reply = (TestMsg *)msg_get(&test_portMain))) { count += reply->result; kprintf("Main recv[%d] count[%d]\n", reply->result, count); } } if (process_num == 6) break; if (sigs & SIG_TIMEOUT) { kputs("Main: sig timeout\n"); goto error; } } } if(count == MAX_GLOBAL_COUNT) { kprintf("Message test finished..ok!\n"); return 0; } error: kprintf("Message test finished..fail!\n"); return -1; }
int main(int argc, char *argv[]) { uint32_t us_delay; if(argc > 1) { us_delay = atoi(argv[1]); } else { us_delay = 1953; } printf("Delay: %dus\n", us_delay); if (spi_link_init()) { TRACE(TRACE_ERROR, "%s", "failed to open SPI link \n"); return -1; } uint8_t skip_buf_check = 0; uint8_t skip_crc_check = 0; uint32_t buf_check_errors = 0; while (1) { struct AutopilotMessageCRCFrame crc_msg_out; struct AutopilotMessageCRCFrame msg_out_prev; struct AutopilotMessageCRCFrame crc_msg_in; uint8_t crc_valid; /* backup message for later comparison */ memcpy(&msg_out_prev, &crc_msg_out, sizeof(struct AutopilotMessageCRCFrame)); /* fill message with data */ fill_msg(&crc_msg_out); /* send it over spi */ spi_link_send(&crc_msg_out, sizeof(struct AutopilotMessageCRCFrame), &crc_msg_in, &crc_valid); /* check that received message is identical to the one previously sent */ if (!skip_buf_check && spi_link.msg_cnt > 1) { if (memcmp(&crc_msg_in.payload, &msg_out_prev.payload, sizeof(struct OVERO_LINK_MSG_DOWN))) { printf("Compare failed: (received != expected): \n"); print_up_msg(&crc_msg_in); print_down_msg(&msg_out_prev); buf_check_errors++; } } /* report crc error */ if (!skip_crc_check & !crc_valid) { printf("CRC checksum failed: received %04X != computed %04X\n", crc_msg_in.crc, crc_calc_block_crc8((uint8_t*)&crc_msg_in.payload, sizeof(struct OVERO_LINK_MSG_DOWN))); } /* report message count */ if (!(spi_link.msg_cnt % 1000)) printf("msg %d, buf err %d, CRC errors: %d\n", spi_link.msg_cnt, buf_check_errors, spi_link.crc_err_cnt); /* give it some rest */ if(us_delay > 0) usleep(us_delay); } return 0; }