SNICInterface for ThingPlug Example

Dependents:   ThingPlug_WiFi_Example ThingPlug_WiFi_Example_2

Fork of SNICInterface by muRata

Committer:
lesmin
Date:
Thu Aug 13 06:44:01 2015 +0000
Revision:
54:cb4c105a2ad2
Parent:
52:29b4d0adddf3
retry codes are added by STKorea

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kishino 39:a1233ca02edf 1 /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License
kishino 41:1c1b5ad4d491 2 * muRata, SWITCH SCIENCE Wi-FI module TypeYD SNIC-UART.
kishino 20:dd736d328de6 3 *
kishino 39:a1233ca02edf 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
kishino 39:a1233ca02edf 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
kishino 39:a1233ca02edf 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
kishino 39:a1233ca02edf 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
kishino 39:a1233ca02edf 8 * furnished to do so, subject to the following conditions:
kishino 20:dd736d328de6 9 *
kishino 39:a1233ca02edf 10 * The above copyright notice and this permission notice shall be included in all copies or
kishino 39:a1233ca02edf 11 * substantial portions of the Software.
kishino 20:dd736d328de6 12 *
kishino 39:a1233ca02edf 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
kishino 39:a1233ca02edf 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
kishino 39:a1233ca02edf 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kishino 39:a1233ca02edf 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kishino 39:a1233ca02edf 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kishino 39:a1233ca02edf 18 */
kishino 20:dd736d328de6 19 #include "SNIC_UartCommandManager.h"
kishino 20:dd736d328de6 20 #include "SNIC_Core.h"
kishino 20:dd736d328de6 21
kishino 26:f2e1030964e4 22 C_SNIC_UartCommandManager::~C_SNIC_UartCommandManager()
kishino 26:f2e1030964e4 23 {
kishino 26:f2e1030964e4 24 }
kishino 26:f2e1030964e4 25
kishino 20:dd736d328de6 26 void C_SNIC_UartCommandManager::setCommandID( unsigned char cmd_id )
kishino 20:dd736d328de6 27 {
kishino 20:dd736d328de6 28 mCommandID = cmd_id;
kishino 20:dd736d328de6 29 }
kishino 20:dd736d328de6 30
kishino 20:dd736d328de6 31 unsigned char C_SNIC_UartCommandManager::getCommandID()
kishino 20:dd736d328de6 32 {
kishino 20:dd736d328de6 33 return mCommandID;
kishino 20:dd736d328de6 34 }
kishino 20:dd736d328de6 35
kishino 20:dd736d328de6 36 void C_SNIC_UartCommandManager::setCommandSID( unsigned char cmd_sid )
kishino 20:dd736d328de6 37 {
kishino 20:dd736d328de6 38 mCommandSID = cmd_sid;
kishino 20:dd736d328de6 39 }
kishino 20:dd736d328de6 40
kishino 20:dd736d328de6 41 unsigned char C_SNIC_UartCommandManager::getCommandSID()
kishino 20:dd736d328de6 42 {
kishino 20:dd736d328de6 43 return mCommandSID;
kishino 20:dd736d328de6 44 }
kishino 20:dd736d328de6 45
kishino 20:dd736d328de6 46 void C_SNIC_UartCommandManager::setCommandStatus( unsigned char status )
kishino 20:dd736d328de6 47 {
kishino 20:dd736d328de6 48 mCommandStatus = status;
kishino 20:dd736d328de6 49 }
kishino 20:dd736d328de6 50
kishino 20:dd736d328de6 51 unsigned char C_SNIC_UartCommandManager::getCommandStatus()
kishino 20:dd736d328de6 52 {
kishino 20:dd736d328de6 53 return mCommandStatus;
kishino 20:dd736d328de6 54 }
kishino 20:dd736d328de6 55
kishino 20:dd736d328de6 56 void C_SNIC_UartCommandManager::setResponseBuf( unsigned char *buf_p )
kishino 20:dd736d328de6 57 {
kishino 20:dd736d328de6 58 mResponseBuf_p = buf_p;
kishino 20:dd736d328de6 59 }
kishino 20:dd736d328de6 60
kishino 20:dd736d328de6 61 unsigned char *C_SNIC_UartCommandManager::getResponseBuf()
kishino 20:dd736d328de6 62 {
kishino 20:dd736d328de6 63 return mResponseBuf_p;
kishino 20:dd736d328de6 64 }
kishino 20:dd736d328de6 65
kishino 20:dd736d328de6 66 void C_SNIC_UartCommandManager::setScanResultHandler( void (*handler_p)(tagSCAN_RESULT_T *scan_result) )
kishino 20:dd736d328de6 67 {
kishino 20:dd736d328de6 68 mScanResultHandler_p = handler_p;
kishino 20:dd736d328de6 69 }
kishino 20:dd736d328de6 70
kishino 20:dd736d328de6 71
kishino 20:dd736d328de6 72 int C_SNIC_UartCommandManager::wait()
kishino 20:dd736d328de6 73 {
kishino 20:dd736d328de6 74 int ret = 0;
kishino 20:dd736d328de6 75
kishino 20:dd736d328de6 76 // Get thread ID
kishino 20:dd736d328de6 77 mCommandThreadID = osThreadGetId();
kishino 20:dd736d328de6 78
kishino 20:dd736d328de6 79 // Signal flags that are reported as event are automatically cleared.
kishino 20:dd736d328de6 80 osEvent event_ret = osSignalWait( UART_COMMAND_SIGNAL, UART_COMMAND_WAIT_TIMEOUT);
kishino 20:dd736d328de6 81 if( event_ret.status != osEventSignal )
kishino 20:dd736d328de6 82 {
kishino 20:dd736d328de6 83 ret = -1;
kishino 20:dd736d328de6 84 }
kishino 20:dd736d328de6 85
kishino 20:dd736d328de6 86 return ret;
kishino 20:dd736d328de6 87 }
kishino 20:dd736d328de6 88
kishino 20:dd736d328de6 89 int C_SNIC_UartCommandManager::signal()
kishino 20:dd736d328de6 90 {
kishino 20:dd736d328de6 91 // set signal
kishino 20:dd736d328de6 92 return osSignalSet(mCommandThreadID, UART_COMMAND_SIGNAL);
kishino 20:dd736d328de6 93 }
kishino 20:dd736d328de6 94
kishino 20:dd736d328de6 95 bool C_SNIC_UartCommandManager::isWaitingCommand( unsigned int command_id, unsigned char *payload_p )
kishino 20:dd736d328de6 96 {
kishino 20:dd736d328de6 97 bool ret = false;
kishino 20:dd736d328de6 98
kishino 20:dd736d328de6 99 if( (command_id == getCommandID())
kishino 20:dd736d328de6 100 && (payload_p[0] == getCommandSID()) )
kishino 20:dd736d328de6 101 {
kishino 20:dd736d328de6 102 ret = true;
kishino 20:dd736d328de6 103 }
kishino 20:dd736d328de6 104 return ret;
kishino 20:dd736d328de6 105 }
kishino 20:dd736d328de6 106
kishino 20:dd736d328de6 107 void C_SNIC_UartCommandManager::scanResultIndicate( unsigned char *payload_p, int payload_len )
kishino 20:dd736d328de6 108 {
kishino 20:dd736d328de6 109 if( (payload_p == NULL) || (mScanResultHandler_p == NULL) )
kishino 20:dd736d328de6 110 {
kishino 20:dd736d328de6 111 return;
kishino 20:dd736d328de6 112 }
kishino 20:dd736d328de6 113
kishino 20:dd736d328de6 114 tagSCAN_RESULT_T scan_result;
kishino 20:dd736d328de6 115 int ap_count = payload_p[2];
kishino 20:dd736d328de6 116
kishino 20:dd736d328de6 117 if( ap_count == 0 )
kishino 20:dd736d328de6 118 {
kishino 20:dd736d328de6 119 mScanResultHandler_p( NULL );
kishino 20:dd736d328de6 120 }
kishino 20:dd736d328de6 121
kishino 20:dd736d328de6 122 unsigned char *ap_info_p = &payload_p[3];
kishino 20:dd736d328de6 123 int ap_info_idx = 0;
kishino 20:dd736d328de6 124
kishino 20:dd736d328de6 125 for( int i = 0; i < ap_count; i++ )
kishino 20:dd736d328de6 126 {
kishino 20:dd736d328de6 127 scan_result.channel = ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 128 ap_info_idx++;
kishino 20:dd736d328de6 129 scan_result.rssi = (signed)ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 130 ap_info_idx++;
kishino 20:dd736d328de6 131 scan_result.security= ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 132 ap_info_idx++;
kishino 20:dd736d328de6 133 memcpy( scan_result.bssid, &ap_info_p[ap_info_idx], BSSID_MAC_LENTH );
kishino 20:dd736d328de6 134 ap_info_idx += BSSID_MAC_LENTH;
kishino 20:dd736d328de6 135 scan_result.network_type= ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 136 ap_info_idx++;
kishino 20:dd736d328de6 137 scan_result.max_rate= ap_info_p[ap_info_idx];
kishino 20:dd736d328de6 138 ap_info_idx++;
kishino 20:dd736d328de6 139 ap_info_idx++; // reserved
kishino 20:dd736d328de6 140 strcpy( scan_result.ssid, (char *)&ap_info_p[ap_info_idx] );
kishino 20:dd736d328de6 141 ap_info_idx += strlen( (char *)&ap_info_p[ap_info_idx] );
kishino 20:dd736d328de6 142 ap_info_idx++;
kishino 20:dd736d328de6 143
kishino 20:dd736d328de6 144 // Scanresult callback
kishino 20:dd736d328de6 145 mScanResultHandler_p( &scan_result );
kishino 20:dd736d328de6 146 }
kishino 20:dd736d328de6 147 }
kishino 20:dd736d328de6 148
kishino 20:dd736d328de6 149 void C_SNIC_UartCommandManager::bufferredPacket( unsigned char *payload_p, int payload_len )
kishino 20:dd736d328de6 150 {
kishino 20:dd736d328de6 151 if( (payload_p == NULL) || (payload_len == 0) )
kishino 20:dd736d328de6 152 {
kishino 20:dd736d328de6 153 return;
kishino 20:dd736d328de6 154 }
kishino 20:dd736d328de6 155
kishino 20:dd736d328de6 156 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 20:dd736d328de6 157
kishino 20:dd736d328de6 158 int socket_id;
kishino 20:dd736d328de6 159 unsigned short recv_len;
kishino 20:dd736d328de6 160
kishino 20:dd736d328de6 161 // Get socket id from payload
kishino 20:dd736d328de6 162 socket_id = payload_p[2];
kishino 43:d80bbb12ffe6 163
kishino 43:d80bbb12ffe6 164 // DEBUG_PRINT("bufferredPacket socket id:%d\r\n", socket_id);
kishino 20:dd736d328de6 165 // Get Connection information
kishino 22:a9ec0cad4f84 166 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id );
kishino 20:dd736d328de6 167 if( con_info_p == NULL )
kishino 20:dd736d328de6 168 {
kishino 20:dd736d328de6 169 return;
kishino 20:dd736d328de6 170 }
kishino 20:dd736d328de6 171
kishino 20:dd736d328de6 172 if( con_info_p->is_connected == false )
kishino 20:dd736d328de6 173 {
kishino 40:b6b10c22a121 174 DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", socket_id);
kishino 20:dd736d328de6 175 return;
kishino 20:dd736d328de6 176 }
kishino 20:dd736d328de6 177
kishino 20:dd736d328de6 178 // Get receive length from payload
kishino 20:dd736d328de6 179 recv_len= ((payload_p[3]<<8) & 0xFF00) | payload_p[4];
kishino 43:d80bbb12ffe6 180
kishino 43:d80bbb12ffe6 181 while( con_info_p->is_receive_complete == false )
kishino 43:d80bbb12ffe6 182 {
kishino 43:d80bbb12ffe6 183 Thread::yield();
kishino 43:d80bbb12ffe6 184 }
kishino 43:d80bbb12ffe6 185
kishino 43:d80bbb12ffe6 186 // DEBUG_PRINT("bufferredPacket recv_len:%d\r\n", recv_len);
kishino 43:d80bbb12ffe6 187 int i;
kishino 43:d80bbb12ffe6 188 for(i = 0; i < recv_len; i++ )
kishino 20:dd736d328de6 189 {
kishino 20:dd736d328de6 190 if( con_info_p->recvbuf_p->isFull() )
kishino 20:dd736d328de6 191 {
kishino 40:b6b10c22a121 192 DEBUG_PRINT("Receive buffer is full.\r\n");
kishino 20:dd736d328de6 193 break;
kishino 20:dd736d328de6 194 }
kishino 20:dd736d328de6 195 // Add to receive buffer
kishino 20:dd736d328de6 196 con_info_p->recvbuf_p->queue( payload_p[5+i] );
kishino 20:dd736d328de6 197 }
kishino 43:d80bbb12ffe6 198 //DEBUG_PRINT("###Receive queue[%d]\r\n", i);
kishino 39:a1233ca02edf 199 con_info_p->mutex.lock();
kishino 43:d80bbb12ffe6 200 con_info_p->is_receive_complete = false;
kishino 20:dd736d328de6 201 con_info_p->is_received = true;
kishino 39:a1233ca02edf 202 con_info_p->mutex.unlock();
kishino 43:d80bbb12ffe6 203 // Thread::yield();
kishino 20:dd736d328de6 204 }
kishino 32:ae95309643aa 205
kishino 32:ae95309643aa 206 void C_SNIC_UartCommandManager::connectedTCPClient( unsigned char *payload_p, int payload_len )
kishino 32:ae95309643aa 207 {
kishino 32:ae95309643aa 208 if( (payload_p == NULL) || (payload_len == 0) )
kishino 32:ae95309643aa 209 {
kishino 32:ae95309643aa 210 return;
kishino 32:ae95309643aa 211 }
kishino 32:ae95309643aa 212
kishino 32:ae95309643aa 213 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 32:ae95309643aa 214 int socket_id;
kishino 32:ae95309643aa 215
kishino 32:ae95309643aa 216 // Get socket id of client from payload
kishino 32:ae95309643aa 217 socket_id = payload_p[3];
kishino 32:ae95309643aa 218
kishino 40:b6b10c22a121 219 DEBUG_PRINT("[connectedTCPClient] socket id:%d\r\n", socket_id);
kishino 32:ae95309643aa 220
kishino 32:ae95309643aa 221 // Get Connection information
kishino 32:ae95309643aa 222 C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id );
kishino 32:ae95309643aa 223 if( con_info_p == NULL )
kishino 32:ae95309643aa 224 {
kishino 32:ae95309643aa 225 return;
kishino 32:ae95309643aa 226 }
kishino 32:ae95309643aa 227
kishino 32:ae95309643aa 228 if( con_info_p->recvbuf_p == NULL )
kishino 32:ae95309643aa 229 {
kishino 40:b6b10c22a121 230 // DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", socket_id);
kishino 32:ae95309643aa 231 con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
kishino 32:ae95309643aa 232 }
kishino 32:ae95309643aa 233 con_info_p->is_connected = true;
kishino 32:ae95309643aa 234 con_info_p->is_received = false;
kishino 32:ae95309643aa 235 con_info_p->is_accept = true;
kishino 32:ae95309643aa 236 con_info_p->parent_socket = payload_p[2];
MACRUM 52:29b4d0adddf3 237 con_info_p->from_ip = ((payload_p[4] << 24) | (payload_p[5] << 16) | (payload_p[6] << 8) | payload_p[7]);
MACRUM 52:29b4d0adddf3 238 con_info_p->from_port = ((payload_p[8] << 8) | payload_p[9]);
kishino 32:ae95309643aa 239 }
kishino 33:33f1bc919486 240
kishino 33:33f1bc919486 241 void C_SNIC_UartCommandManager::bufferredUDPPacket( unsigned char *payload_p, int payload_len )
kishino 33:33f1bc919486 242 {
kishino 33:33f1bc919486 243 if( (payload_p == NULL) || (payload_len == 0) )
kishino 33:33f1bc919486 244 {
kishino 33:33f1bc919486 245 return;
kishino 33:33f1bc919486 246 }
kishino 33:33f1bc919486 247
kishino 33:33f1bc919486 248 C_SNIC_Core *instance_p = C_SNIC_Core::getInstance();
kishino 33:33f1bc919486 249
kishino 33:33f1bc919486 250 // Get Connection information
kishino 33:33f1bc919486 251 C_SNIC_Core::tagUDP_RECVINFO_T *con_info_p = instance_p->getUdpRecvInfo( payload_p[2] );
kishino 33:33f1bc919486 252 if( con_info_p == NULL )
kishino 33:33f1bc919486 253 {
kishino 33:33f1bc919486 254 return;
kishino 33:33f1bc919486 255 }
kishino 33:33f1bc919486 256
kishino 33:33f1bc919486 257 if( con_info_p->recvbuf_p == NULL )
kishino 33:33f1bc919486 258 {
kishino 40:b6b10c22a121 259 // DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", payload_p[2]);
kishino 33:33f1bc919486 260 con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE);
kishino 33:33f1bc919486 261 }
kishino 39:a1233ca02edf 262 con_info_p->mutex.lock();
kishino 33:33f1bc919486 263 con_info_p->is_received = true;
kishino 39:a1233ca02edf 264 con_info_p->mutex.unlock();
kishino 33:33f1bc919486 265
kishino 33:33f1bc919486 266 // Set remote IP address and remote port
kishino 33:33f1bc919486 267 con_info_p->from_ip = ((payload_p[3] << 24) | (payload_p[4] << 16) | (payload_p[5] << 8) | payload_p[6]);
kishino 33:33f1bc919486 268 con_info_p->from_port = ((payload_p[7] << 8) | payload_p[8]);
kishino 33:33f1bc919486 269
kishino 33:33f1bc919486 270 unsigned short recv_len;
kishino 33:33f1bc919486 271 // Get receive length from payload
kishino 33:33f1bc919486 272 recv_len= ((payload_p[9]<<8) & 0xFF00) | payload_p[10];
kishino 33:33f1bc919486 273 for( int i = 0; i < recv_len; i++ )
kishino 33:33f1bc919486 274 {
kishino 33:33f1bc919486 275 if( con_info_p->recvbuf_p->isFull() )
kishino 33:33f1bc919486 276 {
kishino 40:b6b10c22a121 277 DEBUG_PRINT("Receive buffer is full.\r\n");
kishino 33:33f1bc919486 278 break;
kishino 33:33f1bc919486 279 }
kishino 33:33f1bc919486 280
kishino 33:33f1bc919486 281 // Add to receive buffer
kishino 33:33f1bc919486 282 con_info_p->recvbuf_p->queue( payload_p[11+i] );
kishino 33:33f1bc919486 283 }
kishino 33:33f1bc919486 284 }