SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Thu Mar 27 07:37:55 2014 +0000
Revision:
22:a9ec0cad4f84
Parent:
20:dd736d328de6
Child:
26:f2e1030964e4
Refactoring

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