SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Tue Mar 25 01:42:25 2014 +0000
Revision:
12:0254eaccfda2
Child:
14:54378c96d285
Refactoring of the class name

Who changed what in which revision?

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