SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Fri Mar 28 03:03:40 2014 +0000
Revision:
26:f2e1030964e4
Parent:
20:dd736d328de6
Child:
29:6a0ba999597d
Created a virtual destructor

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