SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Wed Jun 25 00:04:11 2014 +0000
Revision:
38:f13e4e563d65
Parent:
36:f33fcf5975ab
Child:
39:a1233ca02edf
Fixed a variable name of pointer type.

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 38:f13e4e563d65 76 unsigned char *buf_p = NULL;
kishino 20:dd736d328de6 77 int i;
kishino 20:dd736d328de6 78
kishino 20:dd736d328de6 79 // get payload length
kishino 20:dd736d328de6 80 payload_len = ( ( (recvdata_p[1] & ~0x80) & 0xff) | ( ( (recvdata_p[2] & ~0xC0) << 7) & 0xff80) );
kishino 20:dd736d328de6 81
kishino 20:dd736d328de6 82 // get Command ID
kishino 20:dd736d328de6 83 *command_id_p = (recvdata_p[3] & ~0x80);
kishino 20:dd736d328de6 84
kishino 38:f13e4e563d65 85 buf_p = &recvdata_p[4];
kishino 20:dd736d328de6 86
kishino 20:dd736d328de6 87 // get payload data
kishino 38:f13e4e563d65 88 for( i = 0; i < payload_len; i++, buf_p++ )
kishino 20:dd736d328de6 89 {
kishino 38:f13e4e563d65 90 *payload_p = *buf_p;
kishino 36:f33fcf5975ab 91 payload_p++;
kishino 20:dd736d328de6 92 }
kishino 20:dd736d328de6 93
kishino 36:f33fcf5975ab 94 return payload_len;
kishino 31:15c22824cc46 95 }
kishino 31:15c22824cc46 96
kishino 31:15c22824cc46 97 int C_SNIC_UartMsgUtil::addrToInteger( const char *addr_p )
kishino 31:15c22824cc46 98 {
kishino 31:15c22824cc46 99 if( addr_p == NULL )
kishino 31:15c22824cc46 100 {
kishino 31:15c22824cc46 101 printf("addrToInteger parameter error\r\n");
kishino 31:15c22824cc46 102 return 0;
kishino 31:15c22824cc46 103 }
kishino 31:15c22824cc46 104
kishino 31:15c22824cc46 105 unsigned char ipadr[4];
kishino 31:15c22824cc46 106 unsigned char temp= 0;
kishino 31:15c22824cc46 107 int i,j,k;
kishino 31:15c22824cc46 108
kishino 31:15c22824cc46 109 /* convert to char[4] */
kishino 31:15c22824cc46 110 k=0;
kishino 31:15c22824cc46 111 for(i=0; i<4; i ++)
kishino 31:15c22824cc46 112 {
kishino 31:15c22824cc46 113 for(j=0; j<4; j ++)
kishino 31:15c22824cc46 114 {
kishino 31:15c22824cc46 115 if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A))
kishino 31:15c22824cc46 116 {
kishino 31:15c22824cc46 117 temp = (temp * 10) + addr_p[k]-0x30;
kishino 31:15c22824cc46 118 }
kishino 31:15c22824cc46 119 else if((addr_p[k] == 0x20)&&(temp == 0))
kishino 31:15c22824cc46 120 {
kishino 31:15c22824cc46 121 }
kishino 31:15c22824cc46 122 else
kishino 31:15c22824cc46 123 {
kishino 31:15c22824cc46 124 ipadr[i]=temp;
kishino 31:15c22824cc46 125 temp = 0;
kishino 31:15c22824cc46 126 k++;
kishino 31:15c22824cc46 127 break;
kishino 31:15c22824cc46 128 }
kishino 31:15c22824cc46 129 k++;
kishino 31:15c22824cc46 130 }
kishino 31:15c22824cc46 131 }
kishino 31:15c22824cc46 132
kishino 38:f13e4e563d65 133 int addr = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] );
kishino 31:15c22824cc46 134
kishino 31:15c22824cc46 135 return addr;
kishino 31:15c22824cc46 136 }
kishino 31:15c22824cc46 137
kishino 31:15c22824cc46 138 void C_SNIC_UartMsgUtil::convertIntToByteAdday( int addr, char *addr_array_p )
kishino 31:15c22824cc46 139 {
kishino 31:15c22824cc46 140 addr_array_p[0] = ((addr & 0xFF000000) >> 24 );
kishino 31:15c22824cc46 141 addr_array_p[1] = ((addr & 0xFF0000) >> 16 );
kishino 31:15c22824cc46 142 addr_array_p[2] = ((addr & 0xFF00) >> 8 );
kishino 31:15c22824cc46 143 addr_array_p[3] = ( addr & 0xFF);
kishino 36:f33fcf5975ab 144 }