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