debug version
Dependents: HTTPClient_WiFi_HelloWorld_src
Fork of SNICInterface by
SNIC/SNIC_UartMsgUtil.cpp@20:dd736d328de6, 2014-03-27 (annotated)
- Committer:
- kishino
- Date:
- Thu Mar 27 06:52:04 2014 +0000
- Revision:
- 20:dd736d328de6
- Child:
- 26:f2e1030964e4
Change to folder from library.
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 | using namespace murata_wifi; |
kishino | 20:dd736d328de6 | 17 | |
kishino | 20:dd736d328de6 | 18 | C_SNIC_UartMsgUtil::C_SNIC_UartMsgUtil() |
kishino | 20:dd736d328de6 | 19 | { |
kishino | 20:dd736d328de6 | 20 | } |
kishino | 20:dd736d328de6 | 21 | |
kishino | 20:dd736d328de6 | 22 | unsigned short C_SNIC_UartMsgUtil::makePayload( unsigned int cmd_len, unsigned char *cmd_p, unsigned char *payload_p ) |
kishino | 20:dd736d328de6 | 23 | { |
kishino | 20:dd736d328de6 | 24 | unsigned short payload_len = 0; |
kishino | 20:dd736d328de6 | 25 | int i; |
kishino | 20:dd736d328de6 | 26 | |
kishino | 20:dd736d328de6 | 27 | for( i = 0; i < cmd_len; i++, payload_p++, payload_len++ ) |
kishino | 20:dd736d328de6 | 28 | { |
kishino | 20:dd736d328de6 | 29 | /* check Escape code */ |
kishino | 20:dd736d328de6 | 30 | if( ( cmd_p[i] == UART_CMD_SOM ) || ( cmd_p[i] == UART_CMD_EOM ) || ( cmd_p[i] == UART_CMD_ESC ) ) |
kishino | 20:dd736d328de6 | 31 | { |
kishino | 20:dd736d328de6 | 32 | /* Add ESC */ |
kishino | 20:dd736d328de6 | 33 | *payload_p = UART_CMD_ESC; |
kishino | 20:dd736d328de6 | 34 | payload_len++; |
kishino | 20:dd736d328de6 | 35 | |
kishino | 20:dd736d328de6 | 36 | payload_p++; |
kishino | 20:dd736d328de6 | 37 | *payload_p = (0x80 | cmd_p[i]); |
kishino | 20:dd736d328de6 | 38 | } |
kishino | 20:dd736d328de6 | 39 | else |
kishino | 20:dd736d328de6 | 40 | { |
kishino | 20:dd736d328de6 | 41 | *payload_p = cmd_p[i]; |
kishino | 20:dd736d328de6 | 42 | } |
kishino | 20:dd736d328de6 | 43 | } |
kishino | 20:dd736d328de6 | 44 | |
kishino | 20:dd736d328de6 | 45 | return payload_len; |
kishino | 20:dd736d328de6 | 46 | } |
kishino | 20:dd736d328de6 | 47 | |
kishino | 20:dd736d328de6 | 48 | unsigned int C_SNIC_UartMsgUtil::makeRequest( unsigned char cmd_id,unsigned char *payload_p |
kishino | 20:dd736d328de6 | 49 | , unsigned short payload_len, unsigned char *uart_command_p ) |
kishino | 20:dd736d328de6 | 50 | { |
kishino | 20:dd736d328de6 | 51 | unsigned char check_sum = 0; // Check Sum |
kishino | 20:dd736d328de6 | 52 | unsigned int uart_cmd_len = 0; |
kishino | 20:dd736d328de6 | 53 | int i; |
kishino | 20:dd736d328de6 | 54 | |
kishino | 20:dd736d328de6 | 55 | // set SOM |
kishino | 20:dd736d328de6 | 56 | *uart_command_p = UART_CMD_SOM; |
kishino | 20:dd736d328de6 | 57 | uart_command_p++; |
kishino | 20:dd736d328de6 | 58 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 59 | |
kishino | 20:dd736d328de6 | 60 | // set payload length L0 |
kishino | 20:dd736d328de6 | 61 | *uart_command_p = (0x80 | (payload_len & 0x007f)); |
kishino | 20:dd736d328de6 | 62 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 63 | uart_command_p++; |
kishino | 20:dd736d328de6 | 64 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 65 | |
kishino | 20:dd736d328de6 | 66 | // set payload length L1 |
kishino | 20:dd736d328de6 | 67 | *uart_command_p = (0x80 | ( (payload_len >> 7) & 0x003f)); |
kishino | 20:dd736d328de6 | 68 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 69 | uart_command_p++; |
kishino | 20:dd736d328de6 | 70 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 71 | |
kishino | 20:dd736d328de6 | 72 | // set Command ID |
kishino | 20:dd736d328de6 | 73 | *uart_command_p = (0x80 | cmd_id); |
kishino | 20:dd736d328de6 | 74 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 75 | uart_command_p++; |
kishino | 20:dd736d328de6 | 76 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 77 | |
kishino | 20:dd736d328de6 | 78 | // set Payload |
kishino | 20:dd736d328de6 | 79 | for( i = 0; i < payload_len; i++, uart_command_p++, uart_cmd_len++ ) |
kishino | 20:dd736d328de6 | 80 | { |
kishino | 20:dd736d328de6 | 81 | *uart_command_p = payload_p[i]; |
kishino | 20:dd736d328de6 | 82 | check_sum += *uart_command_p; |
kishino | 20:dd736d328de6 | 83 | } |
kishino | 20:dd736d328de6 | 84 | |
kishino | 20:dd736d328de6 | 85 | // set Check sum |
kishino | 20:dd736d328de6 | 86 | *uart_command_p = (0x80 | check_sum); |
kishino | 20:dd736d328de6 | 87 | uart_command_p++; |
kishino | 20:dd736d328de6 | 88 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 89 | |
kishino | 20:dd736d328de6 | 90 | // set EOM |
kishino | 20:dd736d328de6 | 91 | *uart_command_p = UART_CMD_EOM; |
kishino | 20:dd736d328de6 | 92 | uart_cmd_len++; |
kishino | 20:dd736d328de6 | 93 | |
kishino | 20:dd736d328de6 | 94 | return uart_cmd_len; |
kishino | 20:dd736d328de6 | 95 | } |
kishino | 20:dd736d328de6 | 96 | |
kishino | 20:dd736d328de6 | 97 | unsigned int C_SNIC_UartMsgUtil::getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p |
kishino | 20:dd736d328de6 | 98 | , unsigned char *command_id_p, unsigned char *payload_p ) |
kishino | 20:dd736d328de6 | 99 | { |
kishino | 20:dd736d328de6 | 100 | unsigned short payload_len = 0; |
kishino | 20:dd736d328de6 | 101 | unsigned int response_len = 0; |
kishino | 20:dd736d328de6 | 102 | unsigned char *buf = NULL; |
kishino | 20:dd736d328de6 | 103 | bool isESC = false; |
kishino | 20:dd736d328de6 | 104 | int i; |
kishino | 20:dd736d328de6 | 105 | |
kishino | 20:dd736d328de6 | 106 | // get payload length |
kishino | 20:dd736d328de6 | 107 | payload_len = ( ( (recvdata_p[1] & ~0x80) & 0xff) | ( ( (recvdata_p[2] & ~0xC0) << 7) & 0xff80) ); |
kishino | 20:dd736d328de6 | 108 | |
kishino | 20:dd736d328de6 | 109 | // get Command ID |
kishino | 20:dd736d328de6 | 110 | *command_id_p = (recvdata_p[3] & ~0x80); |
kishino | 20:dd736d328de6 | 111 | |
kishino | 20:dd736d328de6 | 112 | buf = &recvdata_p[4]; |
kishino | 20:dd736d328de6 | 113 | |
kishino | 20:dd736d328de6 | 114 | // get payload data |
kishino | 20:dd736d328de6 | 115 | for( i = 0; i < payload_len; i++, buf++ ) |
kishino | 20:dd736d328de6 | 116 | { |
kishino | 20:dd736d328de6 | 117 | if( isESC ) |
kishino | 20:dd736d328de6 | 118 | { |
kishino | 20:dd736d328de6 | 119 | *payload_p = (*buf & ~0x80); |
kishino | 20:dd736d328de6 | 120 | payload_p++; |
kishino | 20:dd736d328de6 | 121 | response_len++; |
kishino | 20:dd736d328de6 | 122 | isESC = false; |
kishino | 20:dd736d328de6 | 123 | } |
kishino | 20:dd736d328de6 | 124 | else |
kishino | 20:dd736d328de6 | 125 | { |
kishino | 20:dd736d328de6 | 126 | // Check Escape code |
kishino | 20:dd736d328de6 | 127 | if( *buf == UART_CMD_ESC ) |
kishino | 20:dd736d328de6 | 128 | { |
kishino | 20:dd736d328de6 | 129 | isESC = true; |
kishino | 20:dd736d328de6 | 130 | continue; |
kishino | 20:dd736d328de6 | 131 | } |
kishino | 20:dd736d328de6 | 132 | else |
kishino | 20:dd736d328de6 | 133 | { |
kishino | 20:dd736d328de6 | 134 | *payload_p = *buf; |
kishino | 20:dd736d328de6 | 135 | payload_p++; |
kishino | 20:dd736d328de6 | 136 | response_len++; |
kishino | 20:dd736d328de6 | 137 | } |
kishino | 20:dd736d328de6 | 138 | } |
kishino | 20:dd736d328de6 | 139 | } |
kishino | 20:dd736d328de6 | 140 | |
kishino | 20:dd736d328de6 | 141 | return response_len; |
kishino | 20:dd736d328de6 | 142 | } |