customized SNICInterface

Dependents:   MurataTypeYD_Ping

Fork of NySNICInterface by Ryo Iizuka

Committer:
kishino
Date:
Thu Jun 19 10:15:47 2014 +0000
Revision:
36:f33fcf5975ab
Parent:
31:15c22824cc46
Child:
38:f13e4e563d65
Supported a new command format of SNIC UART.

Who changed what in which revision?

UserRevisionLine numberNew 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 int C_SNIC_UartMsgUtil::makeRequest( unsigned char cmd_id,unsigned char *payload_p
kishino 20:dd736d328de6 25 , unsigned short payload_len, unsigned char *uart_command_p )
kishino 20:dd736d328de6 26 {
kishino 20:dd736d328de6 27 unsigned char check_sum = 0; // Check Sum
kishino 20:dd736d328de6 28 unsigned int uart_cmd_len = 0;
kishino 20:dd736d328de6 29 int i;
kishino 20:dd736d328de6 30
kishino 20:dd736d328de6 31 // set SOM
kishino 20:dd736d328de6 32 *uart_command_p = UART_CMD_SOM;
kishino 20:dd736d328de6 33 uart_command_p++;
kishino 20:dd736d328de6 34 uart_cmd_len++;
kishino 20:dd736d328de6 35
kishino 20:dd736d328de6 36 // set payload length L0
kishino 20:dd736d328de6 37 *uart_command_p = (0x80 | (payload_len & 0x007f));
kishino 20:dd736d328de6 38 check_sum += *uart_command_p;
kishino 20:dd736d328de6 39 uart_command_p++;
kishino 20:dd736d328de6 40 uart_cmd_len++;
kishino 20:dd736d328de6 41
kishino 20:dd736d328de6 42 // set payload length L1
kishino 20:dd736d328de6 43 *uart_command_p = (0x80 | ( (payload_len >> 7) & 0x003f));
kishino 20:dd736d328de6 44 check_sum += *uart_command_p;
kishino 20:dd736d328de6 45 uart_command_p++;
kishino 20:dd736d328de6 46 uart_cmd_len++;
kishino 20:dd736d328de6 47
kishino 20:dd736d328de6 48 // set Command ID
kishino 20:dd736d328de6 49 *uart_command_p = (0x80 | cmd_id);
kishino 20:dd736d328de6 50 check_sum += *uart_command_p;
kishino 20:dd736d328de6 51 uart_command_p++;
kishino 20:dd736d328de6 52 uart_cmd_len++;
kishino 20:dd736d328de6 53
kishino 20:dd736d328de6 54 // set Payload
kishino 20:dd736d328de6 55 for( i = 0; i < payload_len; i++, uart_command_p++, uart_cmd_len++ )
kishino 20:dd736d328de6 56 {
kishino 20:dd736d328de6 57 *uart_command_p = payload_p[i];
kishino 20:dd736d328de6 58 }
kishino 20:dd736d328de6 59
kishino 20:dd736d328de6 60 // set Check sum
kishino 20:dd736d328de6 61 *uart_command_p = (0x80 | check_sum);
kishino 20:dd736d328de6 62 uart_command_p++;
kishino 20:dd736d328de6 63 uart_cmd_len++;
kishino 20:dd736d328de6 64
kishino 20:dd736d328de6 65 // set EOM
kishino 20:dd736d328de6 66 *uart_command_p = UART_CMD_EOM;
kishino 20:dd736d328de6 67 uart_cmd_len++;
kishino 20:dd736d328de6 68
kishino 20:dd736d328de6 69 return uart_cmd_len;
kishino 20:dd736d328de6 70 }
kishino 20:dd736d328de6 71
kishino 20:dd736d328de6 72 unsigned int C_SNIC_UartMsgUtil::getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p
kishino 20:dd736d328de6 73 , unsigned char *command_id_p, unsigned char *payload_p )
kishino 20:dd736d328de6 74 {
kishino 20:dd736d328de6 75 unsigned short payload_len = 0;
kishino 20:dd736d328de6 76 unsigned char *buf = NULL;
kishino 20:dd736d328de6 77 bool isESC = false;
kishino 20:dd736d328de6 78 int i;
kishino 20:dd736d328de6 79
kishino 20:dd736d328de6 80 // get payload length
kishino 20:dd736d328de6 81 payload_len = ( ( (recvdata_p[1] & ~0x80) & 0xff) | ( ( (recvdata_p[2] & ~0xC0) << 7) & 0xff80) );
kishino 20:dd736d328de6 82
kishino 20:dd736d328de6 83 // get Command ID
kishino 20:dd736d328de6 84 *command_id_p = (recvdata_p[3] & ~0x80);
kishino 20:dd736d328de6 85
kishino 20:dd736d328de6 86 buf = &recvdata_p[4];
kishino 20:dd736d328de6 87
kishino 20:dd736d328de6 88 // get payload data
kishino 20:dd736d328de6 89 for( i = 0; i < payload_len; i++, buf++ )
kishino 20:dd736d328de6 90 {
kishino 36:f33fcf5975ab 91 *payload_p = *buf;
kishino 36:f33fcf5975ab 92 payload_p++;
kishino 20:dd736d328de6 93 }
kishino 20:dd736d328de6 94
kishino 36:f33fcf5975ab 95 return payload_len;
kishino 31:15c22824cc46 96 }
kishino 31:15c22824cc46 97
kishino 31:15c22824cc46 98 int C_SNIC_UartMsgUtil::addrToInteger( const char *addr_p )
kishino 31:15c22824cc46 99 {
kishino 31:15c22824cc46 100 if( addr_p == NULL )
kishino 31:15c22824cc46 101 {
kishino 31:15c22824cc46 102 printf("addrToInteger parameter error\r\n");
kishino 31:15c22824cc46 103 return 0;
kishino 31:15c22824cc46 104 }
kishino 31:15c22824cc46 105
kishino 31:15c22824cc46 106 unsigned char ipadr[4];
kishino 31:15c22824cc46 107 unsigned char temp= 0;
kishino 31:15c22824cc46 108 int i,j,k;
kishino 31:15c22824cc46 109
kishino 31:15c22824cc46 110 /* convert to char[4] */
kishino 31:15c22824cc46 111 k=0;
kishino 31:15c22824cc46 112 for(i=0; i<4; i ++)
kishino 31:15c22824cc46 113 {
kishino 31:15c22824cc46 114 for(j=0; j<4; j ++)
kishino 31:15c22824cc46 115 {
kishino 31:15c22824cc46 116 if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A))
kishino 31:15c22824cc46 117 {
kishino 31:15c22824cc46 118 temp = (temp * 10) + addr_p[k]-0x30;
kishino 31:15c22824cc46 119 }
kishino 31:15c22824cc46 120 else if((addr_p[k] == 0x20)&&(temp == 0))
kishino 31:15c22824cc46 121 {
kishino 31:15c22824cc46 122 }
kishino 31:15c22824cc46 123 else
kishino 31:15c22824cc46 124 {
kishino 31:15c22824cc46 125 ipadr[i]=temp;
kishino 31:15c22824cc46 126 temp = 0;
kishino 31:15c22824cc46 127 k++;
kishino 31:15c22824cc46 128 break;
kishino 31:15c22824cc46 129 }
kishino 31:15c22824cc46 130 k++;
kishino 31:15c22824cc46 131 }
kishino 31:15c22824cc46 132 }
kishino 31:15c22824cc46 133
kishino 31:15c22824cc46 134 int addr = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] );
kishino 31:15c22824cc46 135
kishino 31:15c22824cc46 136 return addr;
kishino 31:15c22824cc46 137 }
kishino 31:15c22824cc46 138
kishino 31:15c22824cc46 139 void C_SNIC_UartMsgUtil::convertIntToByteAdday( int addr, char *addr_array_p )
kishino 31:15c22824cc46 140 {
kishino 31:15c22824cc46 141 addr_array_p[0] = ((addr & 0xFF000000) >> 24 );
kishino 31:15c22824cc46 142 addr_array_p[1] = ((addr & 0xFF0000) >> 16 );
kishino 31:15c22824cc46 143 addr_array_p[2] = ((addr & 0xFF00) >> 8 );
kishino 31:15c22824cc46 144 addr_array_p[3] = ( addr & 0xFF);
kishino 36:f33fcf5975ab 145 }