SNIC UART Interface library for Murata Type-YD module

Dependents:   WebSocketServerTest

Fork of SNICInterface_mod by Toyomasa Watarai

Committer:
kishino
Date:
Fri May 30 08:30:40 2014 +0000
Revision:
32:ae95309643aa
Parent:
29:6a0ba999597d
Child:
33:33f1bc919486
Implemented  a API of TCP server.

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_UartCommandManager.cpp
kishino 20:dd736d328de6 4 *
kishino 20:dd736d328de6 5 * Purpose: This module has implementation of function for management of
kishino 20:dd736d328de6 6 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_UartCommandManager.h"
kishino 20:dd736d328de6 15 #include "SNIC_Core.h"
kishino 20:dd736d328de6 16
kishino 26:f2e1030964e4 17 C_SNIC_UartCommandManager::~C_SNIC_UartCommandManager()
kishino 26:f2e1030964e4 18 {
kishino 26:f2e1030964e4 19 }
kishino 26:f2e1030964e4 20
kishino 20:dd736d328de6 21 void C_SNIC_UartCommandManager::setCommandID( unsigned char cmd_id )
kishino 20:dd736d328de6 22 {
kishino 20:dd736d328de6 23 mCommandID = cmd_id;
kishino 20:dd736d328de6 24 }
kishino 20:dd736d328de6 25
kishino 20:dd736d328de6 26 unsigned char C_SNIC_UartCommandManager::getCommandID()
kishino 20:dd736d328de6 27 {
kishino 20:dd736d328de6 28 return mCommandID;
kishino 20:dd736d328de6 29 }
kishino 20:dd736d328de6 30
kishino 20:dd736d328de6 31 void C_SNIC_UartCommandManager::setCommandSID( unsigned char cmd_sid )
kishino 20:dd736d328de6 32 {
kishino 20:dd736d328de6 33 mCommandSID = cmd_sid;
kishino 20:dd736d328de6 34 }
kishino 20:dd736d328de6 35
kishino 20:dd736d328de6 36 unsigned char C_SNIC_UartCommandManager::getCommandSID()
kishino 20:dd736d328de6 37 {
kishino 20:dd736d328de6 38 return mCommandSID;
kishino 20:dd736d328de6 39 }
kishino 20:dd736d328de6 40
kishino 20:dd736d328de6 41 void C_SNIC_UartCommandManager::setCommandStatus( unsigned char status )
kishino 20:dd736d328de6 42 {
kishino 20:dd736d328de6 43 mCommandStatus = status;
kishino 20:dd736d328de6 44 }
kishino 20:dd736d328de6 45
kishino 20:dd736d328de6 46 unsigned char C_SNIC_UartCommandManager::getCommandStatus()
kishino 20:dd736d328de6 47 {
kishino 20:dd736d328de6 48 return mCommandStatus;
kishino 20:dd736d328de6 49 }
kishino 20:dd736d328de6 50
kishino 20:dd736d328de6 51 void C_SNIC_UartCommandManager::setResponseBuf( unsigned char *buf_p )
kishino 20:dd736d328de6 52 {
kishino 20:dd736d328de6 53 mResponseBuf_p = buf_p;
kishino 20:dd736d328de6 54 }
kishino 20:dd736d328de6 55
kishino 20:dd736d328de6 56 unsigned char *C_SNIC_UartCommandManager::getResponseBuf()
kishino 20:dd736d328de6 57 {
kishino 20:dd736d328de6 58 return mResponseBuf_p;
kishino 20:dd736d328de6 59 }
kishino 20:dd736d328de6 60
kishino 20:dd736d328de6 61 void C_SNIC_UartCommandManager::setScanResultHandler( void (*handler_p)(tagSCAN_RESULT_T *scan_result) )
kishino 20:dd736d328de6 62 {
kishino 20:dd736d328de6 63 mScanResultHandler_p = handler_p;
kishino 20:dd736d328de6 64 }
kishino 20:dd736d328de6 65
kishino 20:dd736d328de6 66
kishino 20:dd736d328de6 67 int C_SNIC_UartCommandManager::wait()
kishino 20:dd736d328de6 68 {
kishino 20:dd736d328de6 69 int ret = 0;
kishino 20:dd736d328de6 70
kishino 20:dd736d328de6 71 // Get thread ID
kishino 20:dd736d328de6 72 mCommandThreadID = osThreadGetId();
kishino 20:dd736d328de6 73
kishino 20:dd736d328de6 74 // Signal flags that are reported as event are automatically cleared.
kishino 20:dd736d328de6 75 osEvent event_ret = osSignalWait( UART_COMMAND_SIGNAL, UART_COMMAND_WAIT_TIMEOUT);
kishino 20:dd736d328de6 76 if( event_ret.status != osEventSignal )
kishino 20:dd736d328de6 77 {
kishino 20:dd736d328de6 78 ret = -1;
kishino 20:dd736d328de6 79 }
kishino 20:dd736d328de6 80
kishino 20:dd736d328de6 81 return ret;
kishino 20:dd736d328de6 82 }
kishino 20:dd736d328de6 83
kishino 20:dd736d328de6 84 int C_SNIC_UartCommandManager::signal()
kishino 20:dd736d328de6 85 {
kishino 20:dd736d328de6 86 // set signal
kishino 20:dd736d328de6 87 return osSignalSet(mCommandThreadID, UART_COMMAND_SIGNAL);
kishino 20:dd736d328de6 88 }
kishino 20:dd736d328de6 89
kishino 20:dd736d328de6 90 bool C_SNIC_UartCommandManager::isWaitingCommand( unsigned int command_id, unsigned char *payload_p )
kishino 20:dd736d328de6 91 {
kishino 20:dd736d328de6 92 bool ret = false;
kishino 20:dd736d328de6 93
kishino 20:dd736d328de6 94 if( (command_id == getCommandID())
kishino 20:dd736d328de6 95 && (payload_p[0] == getCommandSID()) )
kishino 20:dd736d328de6 96 {
kishino 20:dd736d328de6 97 ret = true;
kishino 20:dd736d328de6 98 }
kishino 20:dd736d328de6 99 return ret;
kishino 20:dd736d328de6 100 }
kishino 20:dd736d328de6 101
kishino 20:dd736d328de6 102 void C_SNIC_UartCommandManager::scanResultIndicate( unsigned char *payload_p, int payload_len )
kishino 20:dd736d328de6 103 {
kishino 20:dd736d328de6 104 if( (payload_p == NULL) || (mScanResultHandler_p == NULL) )
kishino 20:dd736d328de6 105 {
kishino 20:dd736d328de6 106 return;
kishino 20:dd736d328de6 107 }
kishino 20:dd736d328de6 108
kishino 20:dd736d328de6 109 tagSCAN_RESULT_T scan_result;
kishino 20:dd736d328de6 110 int ap_count = payload_p[2];
kishino 20:dd736d328de6 111
kishino 20:dd736d328de6 112 if( ap_count == 0 )
kishino 20:dd736d328de6 113 {
kishino 20:dd736d328de6 114 mScanResultHandler_p( NULL );
kishino 20:dd736d328de6 115 }
kishino 20:dd736d328de6 116
kishino 20:dd736d328de6 117 unsigned char *ap_info_p = &payload_p[3];
kishino 20:dd736d328de6 118 int ap_info_idx = 0;
kishino 20:dd736d328de6 119
kishino 20:dd736d328de6 120 for( int i = 0; i < ap_count; i++ )
kishino 20:dd736d328de6 121 {
kishino 20:dd736d328de6 122 scan_result.channel = ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 123 ap_info_idx++;
kishino 20:dd736d328de6 124 scan_result.rssi = (signed)ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 125 ap_info_idx++;
kishino 20:dd736d328de6 126 scan_result.security= ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 127 ap_info_idx++;
kishino 20:dd736d328de6 128 memcpy( scan_result.bssid, &ap_info_p[ap_info_idx], BSSID_MAC_LENTH );
kishino 20:dd736d328de6 129 ap_info_idx += BSSID_MAC_LENTH;
kishino 20:dd736d328de6 130 scan_result.network_type= ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 131 ap_info_idx++;
kishino 20:dd736d328de6 132 scan_result.max_rate= ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 133 ap_info_idx++;
kishino 20:dd736d328de6 134 ap_info_idx++; // reserved
kishino 20:dd736d328de6 135 strcpy( scan_result.ssid, (char *)&ap_info_p[ap_info_idx] );
kishino 20:dd736d328de6 136 ap_info_idx += strlen( (char *)&ap_info_p[ap_info_idx] );
kishino 20:dd736d328de6 137 ap_info_idx++;
kishino 20:dd736d328de6 138
kishino 20:dd736d328de6 139 // Scanresult callback
kishino 20:dd736d328de6 140 mScanResultHandler_p( &scan_result );
kishino 20:dd736d328de6 141 }
kishino 20:dd736d328de6 142 }
kishino 20:dd736d328de6 143
kishino 20:dd736d328de6 144 void C_SNIC_UartCommandManager::bufferredPacket( unsigned char *payload_p, int payload_len )
kishino 20:dd736d328de6 145 {
kishino 20:dd736d328de6 146 if( (payload_p == NULL) || (payload_len == 0) )
kishino 20:dd736d328de6 147 {
kishino 20:dd736d328de6 148 return;
kishino 20:dd736d328de6 149 }
kishino 20:dd736d328de6 150
kishino 20:dd736d328de6 151 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 20:dd736d328de6 152
kishino 20:dd736d328de6 153 int socket_id;
kishino 20:dd736d328de6 154 unsigned short recv_len;
kishino 20:dd736d328de6 155
kishino 20:dd736d328de6 156 // Get socket id from payload
kishino 20:dd736d328de6 157 socket_id = payload_p[2];
kishino 20:dd736d328de6 158 // Get Connection information
kishino 22:a9ec0cad4f84 159 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id );
kishino 20:dd736d328de6 160 if( con_info_p == NULL )
kishino 20:dd736d328de6 161 {
kishino 20:dd736d328de6 162 return;
kishino 20:dd736d328de6 163 }
kishino 20:dd736d328de6 164
kishino 20:dd736d328de6 165 if( con_info_p->is_connected == false )
kishino 20:dd736d328de6 166 {
kishino 20:dd736d328de6 167 printf(" Socket id \"%d\" is not connected\r\n", socket_id);
kishino 20:dd736d328de6 168 return;
kishino 20:dd736d328de6 169 }
kishino 20:dd736d328de6 170
kishino 20:dd736d328de6 171 // Get receive length from payload
kishino 20:dd736d328de6 172 recv_len= ((payload_p[3]<<8) & 0xFF00) | payload_p[4];
kishino 20:dd736d328de6 173 for( int i = 0; i < recv_len; i++ )
kishino 20:dd736d328de6 174 {
kishino 20:dd736d328de6 175 if( con_info_p->recvbuf_p->isFull() )
kishino 20:dd736d328de6 176 {
kishino 20:dd736d328de6 177 printf("Receive buffer is full.\r\n");
kishino 20:dd736d328de6 178 break;
kishino 20:dd736d328de6 179 }
kishino 20:dd736d328de6 180
kishino 20:dd736d328de6 181 // Add to receive buffer
kishino 20:dd736d328de6 182 con_info_p->recvbuf_p->queue( payload_p[5+i] );
kishino 20:dd736d328de6 183 }
kishino 20:dd736d328de6 184 con_info_p->is_received = true;
kishino 20:dd736d328de6 185 }
kishino 32:ae95309643aa 186
kishino 32:ae95309643aa 187 void C_SNIC_UartCommandManager::connectedTCPClient( unsigned char *payload_p, int payload_len )
kishino 32:ae95309643aa 188 {
kishino 32:ae95309643aa 189 if( (payload_p == NULL) || (payload_len == 0) )
kishino 32:ae95309643aa 190 {
kishino 32:ae95309643aa 191 return;
kishino 32:ae95309643aa 192 }
kishino 32:ae95309643aa 193
kishino 32:ae95309643aa 194 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 32:ae95309643aa 195 int socket_id;
kishino 32:ae95309643aa 196
kishino 32:ae95309643aa 197 // Get socket id of client from payload
kishino 32:ae95309643aa 198 socket_id = payload_p[3];
kishino 32:ae95309643aa 199
kishino 32:ae95309643aa 200 printf("[connectedTCPClient] socket id:%d\r\n", socket_id);
kishino 32:ae95309643aa 201
kishino 32:ae95309643aa 202 // Get Connection information
kishino 32:ae95309643aa 203 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id );
kishino 32:ae95309643aa 204 if( con_info_p == NULL )
kishino 32:ae95309643aa 205 {
kishino 32:ae95309643aa 206 return;
kishino 32:ae95309643aa 207 }
kishino 32:ae95309643aa 208
kishino 32:ae95309643aa 209 if( con_info_p->recvbuf_p == NULL )
kishino 32:ae95309643aa 210 {
kishino 32:ae95309643aa 211 printf( "create recv buffer[socket:%d]\r\n", socket_id);
kishino 32:ae95309643aa 212 con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
kishino 32:ae95309643aa 213 }
kishino 32:ae95309643aa 214 con_info_p->is_connected = true;
kishino 32:ae95309643aa 215 con_info_p->is_received = false;
kishino 32:ae95309643aa 216 con_info_p->is_accept = true;
kishino 32:ae95309643aa 217 con_info_p->parent_socket = payload_p[2];
kishino 32:ae95309643aa 218 }