SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Wed Mar 26 04:38:12 2014 +0000
Revision:
14:54378c96d285
Parent:
12:0254eaccfda2
Added a comment of copyright.

Who changed what in which revision?

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