Customized SNICInterface
Fork of NySNICInterface by
SNIC/SNIC_UartMsgUtil.cpp@31:15c22824cc46, 2014-05-29 (annotated)
- Committer:
- kishino
- Date:
- Thu May 29 03:23:21 2014 +0000
- Revision:
- 31:15c22824cc46
- Parent:
- 29:6a0ba999597d
- Child:
- 36:f33fcf5975ab
Created a command of IP address settings.
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 | 31:15c22824cc46 | 144 | } |
kishino | 31:15c22824cc46 | 145 | |
kishino | 31:15c22824cc46 | 146 | int C_SNIC_UartMsgUtil::addrToInteger( const char *addr_p ) |
kishino | 31:15c22824cc46 | 147 | { |
kishino | 31:15c22824cc46 | 148 | if( addr_p == NULL ) |
kishino | 31:15c22824cc46 | 149 | { |
kishino | 31:15c22824cc46 | 150 | printf("addrToInteger parameter error\r\n"); |
kishino | 31:15c22824cc46 | 151 | return 0; |
kishino | 31:15c22824cc46 | 152 | } |
kishino | 31:15c22824cc46 | 153 | |
kishino | 31:15c22824cc46 | 154 | unsigned char ipadr[4]; |
kishino | 31:15c22824cc46 | 155 | unsigned char temp= 0; |
kishino | 31:15c22824cc46 | 156 | int i,j,k; |
kishino | 31:15c22824cc46 | 157 | |
kishino | 31:15c22824cc46 | 158 | /* convert to char[4] */ |
kishino | 31:15c22824cc46 | 159 | k=0; |
kishino | 31:15c22824cc46 | 160 | for(i=0; i<4; i ++) |
kishino | 31:15c22824cc46 | 161 | { |
kishino | 31:15c22824cc46 | 162 | for(j=0; j<4; j ++) |
kishino | 31:15c22824cc46 | 163 | { |
kishino | 31:15c22824cc46 | 164 | if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A)) |
kishino | 31:15c22824cc46 | 165 | { |
kishino | 31:15c22824cc46 | 166 | temp = (temp * 10) + addr_p[k]-0x30; |
kishino | 31:15c22824cc46 | 167 | } |
kishino | 31:15c22824cc46 | 168 | else if((addr_p[k] == 0x20)&&(temp == 0)) |
kishino | 31:15c22824cc46 | 169 | { |
kishino | 31:15c22824cc46 | 170 | } |
kishino | 31:15c22824cc46 | 171 | else |
kishino | 31:15c22824cc46 | 172 | { |
kishino | 31:15c22824cc46 | 173 | ipadr[i]=temp; |
kishino | 31:15c22824cc46 | 174 | temp = 0; |
kishino | 31:15c22824cc46 | 175 | k++; |
kishino | 31:15c22824cc46 | 176 | break; |
kishino | 31:15c22824cc46 | 177 | } |
kishino | 31:15c22824cc46 | 178 | k++; |
kishino | 31:15c22824cc46 | 179 | } |
kishino | 31:15c22824cc46 | 180 | } |
kishino | 31:15c22824cc46 | 181 | |
kishino | 31:15c22824cc46 | 182 | int addr = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] ); |
kishino | 31:15c22824cc46 | 183 | |
kishino | 31:15c22824cc46 | 184 | return addr; |
kishino | 31:15c22824cc46 | 185 | } |
kishino | 31:15c22824cc46 | 186 | |
kishino | 31:15c22824cc46 | 187 | void C_SNIC_UartMsgUtil::convertIntToByteAdday( int addr, char *addr_array_p ) |
kishino | 31:15c22824cc46 | 188 | { |
kishino | 31:15c22824cc46 | 189 | addr_array_p[0] = ((addr & 0xFF000000) >> 24 ); |
kishino | 31:15c22824cc46 | 190 | addr_array_p[1] = ((addr & 0xFF0000) >> 16 ); |
kishino | 31:15c22824cc46 | 191 | addr_array_p[2] = ((addr & 0xFF00) >> 8 ); |
kishino | 31:15c22824cc46 | 192 | addr_array_p[3] = ( addr & 0xFF); |
kishino | 20:dd736d328de6 | 193 | } |