SNIC UART Interface library for Murata Type-YD module
Dependents: WebSocketServerTest
Fork of SNICInterface_mod by
SNIC/SNIC_UartMsgUtil.cpp@29:6a0ba999597d, 2014-05-26 (annotated)
- Committer:
- kishino
- Date:
- Mon May 26 05:17:28 2014 +0000
- Revision:
- 29:6a0ba999597d
- Parent:
- 26:f2e1030964e4
- Child:
- 31:15c22824cc46
Changed the receive logic of UART.; Changed the use area of the global buffer.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 20:dd736d328de6 | 1 | /******************* Murata Manufacturing Co.,Ltd. 2014 ***************** |
kishino | 20:dd736d328de6 | 2 | * |
kishino | 20:dd736d328de6 | 3 | * Filename: SNIC_UartMsgUtil.cpp |
kishino | 20:dd736d328de6 | 4 | * |
kishino | 20:dd736d328de6 | 5 | * Purpose: This module has implementation of function for utilities |
kishino | 20:dd736d328de6 | 6 | * of SNIC UART Command. |
kishino | 20:dd736d328de6 | 7 | * |
kishino | 20:dd736d328de6 | 8 | * $Author: kishino $ |
kishino | 20:dd736d328de6 | 9 | * |
kishino | 20:dd736d328de6 | 10 | * $Date: 2014/03/26 $ |
kishino | 20:dd736d328de6 | 11 | * |
kishino | 20:dd736d328de6 | 12 | * $Revision: 0.0.0.1 $ |
kishino | 20:dd736d328de6 | 13 | * ***********************************************************************/ |
kishino | 20:dd736d328de6 | 14 | #include "SNIC_UartMsgUtil.h" |
kishino | 20:dd736d328de6 | 15 | |
kishino | 20:dd736d328de6 | 16 | C_SNIC_UartMsgUtil::C_SNIC_UartMsgUtil() |
kishino | 20:dd736d328de6 | 17 | { |
kishino | 20:dd736d328de6 | 18 | } |
kishino | 20:dd736d328de6 | 19 | |
kishino | 26:f2e1030964e4 | 20 | C_SNIC_UartMsgUtil::~C_SNIC_UartMsgUtil() |
kishino | 26:f2e1030964e4 | 21 | { |
kishino | 26:f2e1030964e4 | 22 | } |
kishino | 26:f2e1030964e4 | 23 | |
kishino | 20:dd736d328de6 | 24 | unsigned short C_SNIC_UartMsgUtil::makePayload( unsigned int cmd_len, unsigned char *cmd_p, unsigned char *payload_p ) |
kishino | 20:dd736d328de6 | 25 | { |
kishino | 20:dd736d328de6 | 26 | unsigned short payload_len = 0; |
kishino | 20:dd736d328de6 | 27 | int i; |
kishino | 20:dd736d328de6 | 28 | |
kishino | 20:dd736d328de6 | 29 | for( i = 0; i < cmd_len; i++, payload_p++, payload_len++ ) |
kishino | 20:dd736d328de6 | 30 | { |
kishino | 20:dd736d328de6 | 31 | /* check Escape code */ |
kishino | 20:dd736d328de6 | 32 | if( ( cmd_p[i] == UART_CMD_SOM ) || ( cmd_p[i] == UART_CMD_EOM ) || ( cmd_p[i] == UART_CMD_ESC ) ) |
kishino | 20:dd736d328de6 | 33 | { |
kishino | 20:dd736d328de6 | 34 | /* Add ESC */ |
kishino | 20:dd736d328de6 | 35 | *payload_p = UART_CMD_ESC; |
kishino | 20:dd736d328de6 | 36 | payload_len++; |
kishino | 20:dd736d328de6 | 37 | |
kishino | 20:dd736d328de6 | 38 | payload_p++; |
kishino | 20:dd736d328de6 | 39 | *payload_p = (0x80 | cmd_p[i]); |
kishino | 20:dd736d328de6 | 40 | } |
kishino | 20:dd736d328de6 | 41 | else |
kishino | 20:dd736d328de6 | 42 | { |
kishino | 20:dd736d328de6 | 43 | *payload_p = cmd_p[i]; |
kishino | 20:dd736d328de6 | 44 | } |
kishino | 20:dd736d328de6 | 45 | } |
kishino | 20:dd736d328de6 | 46 | |
kishino | 20:dd736d328de6 | 47 | return payload_len; |
kishino | 20:dd736d328de6 | 48 | } |
kishino | 20:dd736d328de6 | 49 | |
kishino | 20:dd736d328de6 | 50 | unsigned int C_SNIC_UartMsgUtil::makeRequest( unsigned char cmd_id,unsigned char *payload_p |
kishino | 20:dd736d328de6 | 51 | , unsigned short payload_len, unsigned char *uart_command_p ) |
kishino | 20:dd736d328de6 | 52 | { |
kishino | 20:dd736d328de6 | 53 | unsigned char check_sum = 0; // Check Sum |
kishino | 20:dd736d328de6 | 54 | unsigned int uart_cmd_len = 0; |
kishino | 20:dd736d328de6 | 55 | int i; |
kishino | 20:dd736d328de6 | 56 | |
kishino | 20:dd736d328de6 | 57 | // set SOM |
kishino | 20:dd736d328de6 | 58 | *uart_command_p = UART_CMD_SOM; |
kishino | 20:dd736d328de6 | 59 | uart_command_p++; |
kishino | 20:dd736d328de6 | 60 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 61 | |
kishino | 20:dd736d328de6 | 62 | // set payload length L0 |
kishino | 20:dd736d328de6 | 63 | *uart_command_p = (0x80 | (payload_len & 0x007f)); |
kishino | 20:dd736d328de6 | 64 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 65 | uart_command_p++; |
kishino | 20:dd736d328de6 | 66 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 67 | |
kishino | 20:dd736d328de6 | 68 | // set payload length L1 |
kishino | 20:dd736d328de6 | 69 | *uart_command_p = (0x80 | ( (payload_len >> 7) & 0x003f)); |
kishino | 20:dd736d328de6 | 70 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 71 | uart_command_p++; |
kishino | 20:dd736d328de6 | 72 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 73 | |
kishino | 20:dd736d328de6 | 74 | // set Command ID |
kishino | 20:dd736d328de6 | 75 | *uart_command_p = (0x80 | cmd_id); |
kishino | 20:dd736d328de6 | 76 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 77 | uart_command_p++; |
kishino | 20:dd736d328de6 | 78 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 79 | |
kishino | 20:dd736d328de6 | 80 | // set Payload |
kishino | 20:dd736d328de6 | 81 | for( i = 0; i < payload_len; i++, uart_command_p++, uart_cmd_len++ ) |
kishino | 20:dd736d328de6 | 82 | { |
kishino | 20:dd736d328de6 | 83 | *uart_command_p = payload_p[i]; |
kishino | 20:dd736d328de6 | 84 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 85 | } |
kishino | 20:dd736d328de6 | 86 | |
kishino | 20:dd736d328de6 | 87 | // set Check sum |
kishino | 20:dd736d328de6 | 88 | *uart_command_p = (0x80 | check_sum); |
kishino | 20:dd736d328de6 | 89 | uart_command_p++; |
kishino | 20:dd736d328de6 | 90 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 91 | |
kishino | 20:dd736d328de6 | 92 | // set EOM |
kishino | 20:dd736d328de6 | 93 | *uart_command_p = UART_CMD_EOM; |
kishino | 20:dd736d328de6 | 94 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 95 | |
kishino | 20:dd736d328de6 | 96 | return uart_cmd_len; |
kishino | 20:dd736d328de6 | 97 | } |
kishino | 20:dd736d328de6 | 98 | |
kishino | 20:dd736d328de6 | 99 | unsigned int C_SNIC_UartMsgUtil::getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p |
kishino | 20:dd736d328de6 | 100 | , unsigned char *command_id_p, unsigned char *payload_p ) |
kishino | 20:dd736d328de6 | 101 | { |
kishino | 20:dd736d328de6 | 102 | unsigned short payload_len = 0; |
kishino | 20:dd736d328de6 | 103 | unsigned int response_len = 0; |
kishino | 20:dd736d328de6 | 104 | unsigned char *buf = NULL; |
kishino | 20:dd736d328de6 | 105 | bool isESC = false; |
kishino | 20:dd736d328de6 | 106 | int i; |
kishino | 20:dd736d328de6 | 107 | |
kishino | 20:dd736d328de6 | 108 | // get payload length |
kishino | 20:dd736d328de6 | 109 | payload_len = ( ( (recvdata_p[1] & ~0x80) & 0xff) | ( ( (recvdata_p[2] & ~0xC0) << 7) & 0xff80) ); |
kishino | 20:dd736d328de6 | 110 | |
kishino | 20:dd736d328de6 | 111 | // get Command ID |
kishino | 20:dd736d328de6 | 112 | *command_id_p = (recvdata_p[3] & ~0x80); |
kishino | 20:dd736d328de6 | 113 | |
kishino | 20:dd736d328de6 | 114 | buf = &recvdata_p[4]; |
kishino | 20:dd736d328de6 | 115 | |
kishino | 20:dd736d328de6 | 116 | // get payload data |
kishino | 20:dd736d328de6 | 117 | for( i = 0; i < payload_len; i++, buf++ ) |
kishino | 20:dd736d328de6 | 118 | { |
kishino | 20:dd736d328de6 | 119 | if( isESC ) |
kishino | 20:dd736d328de6 | 120 | { |
kishino | 20:dd736d328de6 | 121 | *payload_p = (*buf & ~0x80); |
kishino | 20:dd736d328de6 | 122 | payload_p++; |
kishino | 20:dd736d328de6 | 123 | response_len++; |
kishino | 20:dd736d328de6 | 124 | isESC = false; |
kishino | 20:dd736d328de6 | 125 | } |
kishino | 20:dd736d328de6 | 126 | else |
kishino | 20:dd736d328de6 | 127 | { |
kishino | 20:dd736d328de6 | 128 | // Check Escape code |
kishino | 20:dd736d328de6 | 129 | if( *buf == UART_CMD_ESC ) |
kishino | 20:dd736d328de6 | 130 | { |
kishino | 20:dd736d328de6 | 131 | isESC = true; |
kishino | 20:dd736d328de6 | 132 | continue; |
kishino | 20:dd736d328de6 | 133 | } |
kishino | 20:dd736d328de6 | 134 | else |
kishino | 20:dd736d328de6 | 135 | { |
kishino | 20:dd736d328de6 | 136 | *payload_p = *buf; |
kishino | 20:dd736d328de6 | 137 | payload_p++; |
kishino | 20:dd736d328de6 | 138 | response_len++; |
kishino | 20:dd736d328de6 | 139 | } |
kishino | 20:dd736d328de6 | 140 | } |
kishino | 20:dd736d328de6 | 141 | } |
kishino | 20:dd736d328de6 | 142 | |
kishino | 20:dd736d328de6 | 143 | return response_len; |
kishino | 20:dd736d328de6 | 144 | } |