SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Wed Mar 26 04:38:12 2014 +0000
Revision:
14:54378c96d285
Parent:
12:0254eaccfda2
Added a comment of copyright.

Who changed what in which revision?

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