Customized SNICInterface
Fork of NySNICInterface by
SNIC/SNIC_UartCommandManager.cpp@40:b6b10c22a121, 2014-07-15 (annotated)
- Committer:
- kishino
- Date:
- Tue Jul 15 09:56:37 2014 +0000
- Revision:
- 40:b6b10c22a121
- Parent:
- 39:a1233ca02edf
- Child:
- 41:1c1b5ad4d491
The platform-dependent code was modified to implemented in ifdef.; The process of debug log output was changed to macro.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 39:a1233ca02edf | 1 | /* Copyright (C) 2014 Murata Manufacturing Co.,Ltd., MIT License |
kishino | 39:a1233ca02edf | 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 | 20:dd736d328de6 | 163 | // Get Connection information |
kishino | 22:a9ec0cad4f84 | 164 | C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id ); |
kishino | 20:dd736d328de6 | 165 | if( con_info_p == NULL ) |
kishino | 20:dd736d328de6 | 166 | { |
kishino | 20:dd736d328de6 | 167 | return; |
kishino | 20:dd736d328de6 | 168 | } |
kishino | 20:dd736d328de6 | 169 | |
kishino | 20:dd736d328de6 | 170 | if( con_info_p->is_connected == false ) |
kishino | 20:dd736d328de6 | 171 | { |
kishino | 40:b6b10c22a121 | 172 | DEBUG_PRINT(" Socket id \"%d\" is not connected\r\n", socket_id); |
kishino | 20:dd736d328de6 | 173 | return; |
kishino | 20:dd736d328de6 | 174 | } |
kishino | 20:dd736d328de6 | 175 | |
kishino | 20:dd736d328de6 | 176 | // Get receive length from payload |
kishino | 20:dd736d328de6 | 177 | recv_len= ((payload_p[3]<<8) & 0xFF00) | payload_p[4]; |
kishino | 20:dd736d328de6 | 178 | for( int i = 0; i < recv_len; i++ ) |
kishino | 20:dd736d328de6 | 179 | { |
kishino | 20:dd736d328de6 | 180 | if( con_info_p->recvbuf_p->isFull() ) |
kishino | 20:dd736d328de6 | 181 | { |
kishino | 40:b6b10c22a121 | 182 | DEBUG_PRINT("Receive buffer is full.\r\n"); |
kishino | 20:dd736d328de6 | 183 | break; |
kishino | 20:dd736d328de6 | 184 | } |
kishino | 20:dd736d328de6 | 185 | |
kishino | 20:dd736d328de6 | 186 | // Add to receive buffer |
kishino | 20:dd736d328de6 | 187 | con_info_p->recvbuf_p->queue( payload_p[5+i] ); |
kishino | 20:dd736d328de6 | 188 | } |
kishino | 39:a1233ca02edf | 189 | con_info_p->mutex.lock(); |
kishino | 20:dd736d328de6 | 190 | con_info_p->is_received = true; |
kishino | 39:a1233ca02edf | 191 | con_info_p->mutex.unlock(); |
kishino | 20:dd736d328de6 | 192 | } |
kishino | 32:ae95309643aa | 193 | |
kishino | 32:ae95309643aa | 194 | void C_SNIC_UartCommandManager::connectedTCPClient( unsigned char *payload_p, int payload_len ) |
kishino | 32:ae95309643aa | 195 | { |
kishino | 32:ae95309643aa | 196 | if( (payload_p == NULL) || (payload_len == 0) ) |
kishino | 32:ae95309643aa | 197 | { |
kishino | 32:ae95309643aa | 198 | return; |
kishino | 32:ae95309643aa | 199 | } |
kishino | 32:ae95309643aa | 200 | |
kishino | 32:ae95309643aa | 201 | C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); |
kishino | 32:ae95309643aa | 202 | int socket_id; |
kishino | 32:ae95309643aa | 203 | |
kishino | 32:ae95309643aa | 204 | // Get socket id of client from payload |
kishino | 32:ae95309643aa | 205 | socket_id = payload_p[3]; |
kishino | 32:ae95309643aa | 206 | |
kishino | 40:b6b10c22a121 | 207 | DEBUG_PRINT("[connectedTCPClient] socket id:%d\r\n", socket_id); |
kishino | 32:ae95309643aa | 208 | |
kishino | 32:ae95309643aa | 209 | // Get Connection information |
kishino | 32:ae95309643aa | 210 | C_SNIC_Core::tagCONNECT_INFO_T *con_info_p = instance_p->getConnectInfo( socket_id ); |
kishino | 32:ae95309643aa | 211 | if( con_info_p == NULL ) |
kishino | 32:ae95309643aa | 212 | { |
kishino | 32:ae95309643aa | 213 | return; |
kishino | 32:ae95309643aa | 214 | } |
kishino | 32:ae95309643aa | 215 | |
kishino | 32:ae95309643aa | 216 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 32:ae95309643aa | 217 | { |
kishino | 40:b6b10c22a121 | 218 | // DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", socket_id); |
kishino | 32:ae95309643aa | 219 | con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); |
kishino | 32:ae95309643aa | 220 | } |
kishino | 32:ae95309643aa | 221 | con_info_p->is_connected = true; |
kishino | 32:ae95309643aa | 222 | con_info_p->is_received = false; |
kishino | 32:ae95309643aa | 223 | con_info_p->is_accept = true; |
kishino | 32:ae95309643aa | 224 | con_info_p->parent_socket = payload_p[2]; |
kishino | 32:ae95309643aa | 225 | } |
kishino | 33:33f1bc919486 | 226 | |
kishino | 33:33f1bc919486 | 227 | void C_SNIC_UartCommandManager::bufferredUDPPacket( unsigned char *payload_p, int payload_len ) |
kishino | 33:33f1bc919486 | 228 | { |
kishino | 33:33f1bc919486 | 229 | if( (payload_p == NULL) || (payload_len == 0) ) |
kishino | 33:33f1bc919486 | 230 | { |
kishino | 33:33f1bc919486 | 231 | return; |
kishino | 33:33f1bc919486 | 232 | } |
kishino | 33:33f1bc919486 | 233 | |
kishino | 33:33f1bc919486 | 234 | C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); |
kishino | 33:33f1bc919486 | 235 | |
kishino | 33:33f1bc919486 | 236 | // Get Connection information |
kishino | 33:33f1bc919486 | 237 | C_SNIC_Core::tagUDP_RECVINFO_T *con_info_p = instance_p->getUdpRecvInfo( payload_p[2] ); |
kishino | 33:33f1bc919486 | 238 | if( con_info_p == NULL ) |
kishino | 33:33f1bc919486 | 239 | { |
kishino | 33:33f1bc919486 | 240 | return; |
kishino | 33:33f1bc919486 | 241 | } |
kishino | 33:33f1bc919486 | 242 | |
kishino | 33:33f1bc919486 | 243 | if( con_info_p->recvbuf_p == NULL ) |
kishino | 33:33f1bc919486 | 244 | { |
kishino | 40:b6b10c22a121 | 245 | // DEBUG_PRINT( "create recv buffer[socket:%d]\r\n", payload_p[2]); |
kishino | 33:33f1bc919486 | 246 | con_info_p->recvbuf_p = new CircBuffer<char>(SNIC_UART_RECVBUF_SIZE); |
kishino | 33:33f1bc919486 | 247 | } |
kishino | 39:a1233ca02edf | 248 | con_info_p->mutex.lock(); |
kishino | 33:33f1bc919486 | 249 | con_info_p->is_received = true; |
kishino | 39:a1233ca02edf | 250 | con_info_p->mutex.unlock(); |
kishino | 33:33f1bc919486 | 251 | |
kishino | 33:33f1bc919486 | 252 | // Set remote IP address and remote port |
kishino | 33:33f1bc919486 | 253 | con_info_p->from_ip = ((payload_p[3] << 24) | (payload_p[4] << 16) | (payload_p[5] << 8) | payload_p[6]); |
kishino | 33:33f1bc919486 | 254 | con_info_p->from_port = ((payload_p[7] << 8) | payload_p[8]); |
kishino | 33:33f1bc919486 | 255 | |
kishino | 33:33f1bc919486 | 256 | unsigned short recv_len; |
kishino | 33:33f1bc919486 | 257 | // Get receive length from payload |
kishino | 33:33f1bc919486 | 258 | recv_len= ((payload_p[9]<<8) & 0xFF00) | payload_p[10]; |
kishino | 33:33f1bc919486 | 259 | for( int i = 0; i < recv_len; i++ ) |
kishino | 33:33f1bc919486 | 260 | { |
kishino | 33:33f1bc919486 | 261 | if( con_info_p->recvbuf_p->isFull() ) |
kishino | 33:33f1bc919486 | 262 | { |
kishino | 40:b6b10c22a121 | 263 | DEBUG_PRINT("Receive buffer is full.\r\n"); |
kishino | 33:33f1bc919486 | 264 | break; |
kishino | 33:33f1bc919486 | 265 | } |
kishino | 33:33f1bc919486 | 266 | |
kishino | 33:33f1bc919486 | 267 | // Add to receive buffer |
kishino | 33:33f1bc919486 | 268 | con_info_p->recvbuf_p->queue( payload_p[11+i] ); |
kishino | 33:33f1bc919486 | 269 | } |
kishino | 33:33f1bc919486 | 270 | } |