int main() { uint8_t status = 0; struct Protocol* obj = (struct Protocol*)malloc(sizeof(struct Protocol*)); uint8_t* buf = (uint8_t*)malloc(100); uint8_t* response = (uint8_t*)malloc(100); packetHdr hdr; uint8_t packetId; uint8_t type; uint8_t typeAck; uint8_t rta; memset(&hdr, 0, sizeof(packetHdr)); SET_HEADER_TYPE_ACK(hdr.type, TEST_TYPE_ACK); SET_HEADER_TYPE(hdr.type, TEST_HEADER_TYPE); SET_TXINFO_PACKET_ID(hdr.txInfo, TEST_PACKET_ID); SET_TXINFO_RTA(hdr.txInfo, TEST_RTA); GET_HEADER_TYPE_ACK(hdr.type, typeAck); GET_HEADER_TYPE(hdr.type, type); GET_TXINFO_PACKET_ID(hdr.txInfo, packetId); GET_TXINFO_RTA(hdr.txInfo, rta); assert(typeAck == TEST_TYPE_ACK); assert(type == TEST_HEADER_TYPE); assert(packetId == TEST_PACKET_ID); assert(rta == TEST_RTA); Protocol_init(obj); /*buf[0] = CMD_READ_REG; //CMD*/ /*buf[1] = 0x2; //size*/ /*buf[2] = 0x40; //payload*/ /*buf[3] = 0x40; //payload*/ /*buf[4] = 0 - (buf[0] + buf[1] + buf[2] + buf[3]);*/ /*Protocol_parse_packet(obj, buf, response);*/ return status; }
void init_protocol(SimStruct *S) { /* Open file descriptors. * * NOTE: This must be done /after/ the fork, or there'll be issues with * the forked process. Likely, these things are inherited. */ hs_input_fd = open(hs_input_fifo, O_WRONLY); hs_output_fd = open(hs_output_fifo, O_RDONLY); if (hs_input_fd < 0 || hs_output_fd < 0) { ssSetErrorStatus(S, "Error opening file descriptor."); return; } //-- Init protocol struct and call handshake procedure ----------------// protocol = Protocol_init(hs_input_fd, hs_output_fd); if (protocol == NULL) { ssPrintf("Protocol_init returned NULL pointer.\n"); ssSetErrorStatus (S, "Protocol_init: PROTOCOL_MEM_ERROR"); return; } switch(Protocol_handshake(protocol)) { case PROTOCOL_MEM_ERROR: free(protocol); ssSetErrorStatus(S, "Protocol_handshake: PROTOCOL_MEM_ERROR"); return; case MEM_ERROR: free(protocol); ssSetErrorStatus(S, "Protocol_handshake: MEM_ERROR"); return; case PROTOCOL_ERROR: free(protocol); ssSetErrorStatus(S, "Protocol_handshake: PROTOCOL_ERROR"); return; case PROTOCOL_SUCCESS: default: ; } /* Set mask port labels. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * * XXX NOTE: * Decided on a fixed command size of 256 until I figure this out. If * a 256-byte string is used the null-termination imposed by calloc * will fail and we'll get a segfault. * XXX */ const char *block_label = ssGetPath(S); #ifdef MATLAB_MEX_FILE /* This code adds labels to the model and prevents * generated C code from compiling */ for (uint8_t i = 0; i < protocol->p_input_labels; i++) { char *cmd = calloc(256, sizeof(char)); sprintf(cmd, "setMaskLabel('%s', '%s', %u, '%s', %u);", block_label, protocol->p_input_labels_str[i], i + 1, "input", i == 0); mexEvalString(cmd); free(cmd); } for (uint8_t i = 0; i < protocol->p_output_labels; i++) { char *cmd = calloc(256, sizeof(char)); sprintf(cmd, "setMaskLabel('%s', '%s', %u, '%s', %u);", block_label, protocol->p_output_labels_str[i], i + 1, "output", 0); mexEvalString(cmd); free(cmd); } #endif /* - Set up some intermediate storage (MATLAB crux). * - Run the simulator one step (need some outputs to be ready since * Simulink updates the model in a backwards order). * * TODO: Error handling for calloc */ intermediate = calloc(protocol->p_input_width, sizeof(double)); Protocol_send_data(protocol, intermediate); sim_running = true; }