SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
lesmin
Date:
Thu Aug 13 06:44:01 2015 +0000
Revision:
54:cb4c105a2ad2
Parent:
41:1c1b5ad4d491
retry codes are added by STKorea

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kishino 39:a1233ca02edf 1 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
kishino 41:1c1b5ad4d491 2 * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
kishino 20:dd736d328de6 3 *
kishino 39:a1233ca02edf 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kishino 39:a1233ca02edf 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kishino 39:a1233ca02edf 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kishino 39:a1233ca02edf 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kishino 39:a1233ca02edf 8 * furnished to do so, subject to the following conditions:
kishino 20:dd736d328de6 9 *
kishino 39:a1233ca02edf 10 * The above copyright notice and this permission notice shall be included in all copies or
kishino 39:a1233ca02edf 11 * substantial portions of the Software.
kishino 20:dd736d328de6 12 *
kishino 39:a1233ca02edf 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kishino 39:a1233ca02edf 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kishino 39:a1233ca02edf 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kishino 39:a1233ca02edf 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kishino 39:a1233ca02edf 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kishino 39:a1233ca02edf 18 */
kishino 20:dd736d328de6 19 #include "SNIC_UartMsgUtil.h"
kishino 20:dd736d328de6 20
kishino 20:dd736d328de6 21 C_SNIC_UartMsgUtil::C_SNIC_UartMsgUtil()
kishino 20:dd736d328de6 22 {
kishino 20:dd736d328de6 23 }
kishino 20:dd736d328de6 24
kishino 26:f2e1030964e4 25 C_SNIC_UartMsgUtil::~C_SNIC_UartMsgUtil()
kishino 26:f2e1030964e4 26 {
kishino 26:f2e1030964e4 27 }
kishino 26:f2e1030964e4 28
kishino 20:dd736d328de6 29 unsigned int C_SNIC_UartMsgUtil::makeRequest( unsigned char cmd_id,unsigned char *payload_p
kishino 20:dd736d328de6 30 , unsigned short payload_len, unsigned char *uart_command_p )
kishino 20:dd736d328de6 31 {
kishino 20:dd736d328de6 32 unsigned char check_sum = 0; // Check Sum
kishino 20:dd736d328de6 33 unsigned int uart_cmd_len = 0;
kishino 20:dd736d328de6 34 int i;
kishino 20:dd736d328de6 35
kishino 20:dd736d328de6 36 // set SOM
kishino 20:dd736d328de6 37 *uart_command_p = UART_CMD_SOM;
kishino 20:dd736d328de6 38 uart_command_p++;
kishino 20:dd736d328de6 39 uart_cmd_len++;
kishino 20:dd736d328de6 40
kishino 20:dd736d328de6 41 // set payload length L0
kishino 20:dd736d328de6 42 *uart_command_p = (0x80 | (payload_len & 0x007f));
kishino 20:dd736d328de6 43 check_sum += *uart_command_p;
kishino 20:dd736d328de6 44 uart_command_p++;
kishino 20:dd736d328de6 45 uart_cmd_len++;
kishino 20:dd736d328de6 46
kishino 20:dd736d328de6 47 // set payload length L1
kishino 20:dd736d328de6 48 *uart_command_p = (0x80 | ( (payload_len >> 7) & 0x003f));
kishino 20:dd736d328de6 49 check_sum += *uart_command_p;
kishino 20:dd736d328de6 50 uart_command_p++;
kishino 20:dd736d328de6 51 uart_cmd_len++;
kishino 20:dd736d328de6 52
kishino 20:dd736d328de6 53 // set Command ID
kishino 20:dd736d328de6 54 *uart_command_p = (0x80 | cmd_id);
kishino 20:dd736d328de6 55 check_sum += *uart_command_p;
kishino 20:dd736d328de6 56 uart_command_p++;
kishino 20:dd736d328de6 57 uart_cmd_len++;
kishino 20:dd736d328de6 58
kishino 20:dd736d328de6 59 // set Payload
kishino 20:dd736d328de6 60 for( i = 0; i < payload_len; i++, uart_command_p++, uart_cmd_len++ )
kishino 20:dd736d328de6 61 {
kishino 20:dd736d328de6 62 *uart_command_p = payload_p[i];
kishino 20:dd736d328de6 63 }
kishino 20:dd736d328de6 64
kishino 20:dd736d328de6 65 // set Check sum
kishino 20:dd736d328de6 66 *uart_command_p = (0x80 | check_sum);
kishino 20:dd736d328de6 67 uart_command_p++;
kishino 20:dd736d328de6 68 uart_cmd_len++;
kishino 20:dd736d328de6 69
kishino 20:dd736d328de6 70 // set EOM
kishino 20:dd736d328de6 71 *uart_command_p = UART_CMD_EOM;
kishino 20:dd736d328de6 72 uart_cmd_len++;
kishino 20:dd736d328de6 73
kishino 20:dd736d328de6 74 return uart_cmd_len;
kishino 20:dd736d328de6 75 }
kishino 20:dd736d328de6 76
kishino 20:dd736d328de6 77 unsigned int C_SNIC_UartMsgUtil::getResponsePayload( unsigned int recvdata_len, unsigned char *recvdata_p
kishino 20:dd736d328de6 78 , unsigned char *command_id_p, unsigned char *payload_p )
kishino 20:dd736d328de6 79 {
kishino 20:dd736d328de6 80 unsigned short payload_len = 0;
kishino 38:f13e4e563d65 81 unsigned char *buf_p = NULL;
kishino 20:dd736d328de6 82 int i;
kishino 20:dd736d328de6 83
kishino 20:dd736d328de6 84 // get payload length
kishino 20:dd736d328de6 85 payload_len = ( ( (recvdata_p[1] & ~0x80) & 0xff) | ( ( (recvdata_p[2] & ~0xC0) << 7) & 0xff80) );
kishino 20:dd736d328de6 86
kishino 20:dd736d328de6 87 // get Command ID
kishino 20:dd736d328de6 88 *command_id_p = (recvdata_p[3] & ~0x80);
kishino 20:dd736d328de6 89
kishino 38:f13e4e563d65 90 buf_p = &recvdata_p[4];
kishino 20:dd736d328de6 91
kishino 20:dd736d328de6 92 // get payload data
kishino 38:f13e4e563d65 93 for( i = 0; i < payload_len; i++, buf_p++ )
kishino 20:dd736d328de6 94 {
kishino 38:f13e4e563d65 95 *payload_p = *buf_p;
kishino 36:f33fcf5975ab 96 payload_p++;
kishino 20:dd736d328de6 97 }
kishino 20:dd736d328de6 98
kishino 36:f33fcf5975ab 99 return payload_len;
kishino 31:15c22824cc46 100 }
kishino 31:15c22824cc46 101
kishino 31:15c22824cc46 102 int C_SNIC_UartMsgUtil::addrToInteger( const char *addr_p )
kishino 31:15c22824cc46 103 {
kishino 31:15c22824cc46 104 if( addr_p == NULL )
kishino 31:15c22824cc46 105 {
kishino 40:b6b10c22a121 106 DEBUG_PRINT("addrToInteger parameter error\r\n");
kishino 31:15c22824cc46 107 return 0;
kishino 31:15c22824cc46 108 }
kishino 31:15c22824cc46 109
kishino 31:15c22824cc46 110 unsigned char ipadr[4];
kishino 31:15c22824cc46 111 unsigned char temp= 0;
kishino 31:15c22824cc46 112 int i,j,k;
kishino 31:15c22824cc46 113
kishino 31:15c22824cc46 114 /* convert to char[4] */
kishino 31:15c22824cc46 115 k=0;
kishino 31:15c22824cc46 116 for(i=0; i<4; i ++)
kishino 31:15c22824cc46 117 {
kishino 31:15c22824cc46 118 for(j=0; j<4; j ++)
kishino 31:15c22824cc46 119 {
kishino 31:15c22824cc46 120 if((addr_p[k] > 0x2F)&&(addr_p[k] < 0x3A))
kishino 31:15c22824cc46 121 {
kishino 31:15c22824cc46 122 temp = (temp * 10) + addr_p[k]-0x30;
kishino 31:15c22824cc46 123 }
kishino 31:15c22824cc46 124 else if((addr_p[k] == 0x20)&&(temp == 0))
kishino 31:15c22824cc46 125 {
kishino 31:15c22824cc46 126 }
kishino 31:15c22824cc46 127 else
kishino 31:15c22824cc46 128 {
kishino 31:15c22824cc46 129 ipadr[i]=temp;
kishino 31:15c22824cc46 130 temp = 0;
kishino 31:15c22824cc46 131 k++;
kishino 31:15c22824cc46 132 break;
kishino 31:15c22824cc46 133 }
kishino 31:15c22824cc46 134 k++;
kishino 31:15c22824cc46 135 }
kishino 31:15c22824cc46 136 }
kishino 31:15c22824cc46 137
kishino 38:f13e4e563d65 138 int addr = ( (ipadr[0]<<24) | (ipadr[1]<<16) | (ipadr[2]<<8) | ipadr[3] );
kishino 31:15c22824cc46 139
kishino 31:15c22824cc46 140 return addr;
kishino 31:15c22824cc46 141 }
kishino 31:15c22824cc46 142
kishino 31:15c22824cc46 143 void C_SNIC_UartMsgUtil::convertIntToByteAdday( int addr, char *addr_array_p )
kishino 31:15c22824cc46 144 {
kishino 31:15c22824cc46 145 addr_array_p[0] = ((addr & 0xFF000000) >> 24 );
kishino 31:15c22824cc46 146 addr_array_p[1] = ((addr & 0xFF0000) >> 16 );
kishino 31:15c22824cc46 147 addr_array_p[2] = ((addr & 0xFF00) >> 8 );
kishino 31:15c22824cc46 148 addr_array_p[3] = ( addr & 0xFF);
kishino 36:f33fcf5975ab 149 }