SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
kishino
Date:
Tue Mar 25 01:42:25 2014 +0000
Revision:
12:0254eaccfda2
Child:
14:54378c96d285
Refactoring of the class name

Who changed what in which revision?

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