Customized SNICInterface
Fork of NySNICInterface by
SNIC/SNIC_Core.cpp@16:6100acfeb1f1, 2014-03-27 (annotated)
- Committer:
- kishino
- Date:
- Thu Mar 27 05:43:54 2014 +0000
- Revision:
- 16:6100acfeb1f1
- Parent:
- 15:5eb637414df2
The instance of C_SNIC_UartCommandManager in C_SNIC_Core class was changed from public to private.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kishino | 14:54378c96d285 | 1 | /******************* Murata Manufacturing Co.,Ltd. 2014 ***************** |
kishino | 14:54378c96d285 | 2 | * |
kishino | 14:54378c96d285 | 3 | * Filename: SNIC_Core.cpp |
kishino | 14:54378c96d285 | 4 | * |
kishino | 14:54378c96d285 | 5 | * Purpose: This module has implementation of internal common function for API. |
kishino | 14:54378c96d285 | 6 | * |
kishino | 14:54378c96d285 | 7 | * $Author: kishino $ |
kishino | 14:54378c96d285 | 8 | * |
kishino | 14:54378c96d285 | 9 | * $Date: 2014/03/26 $ |
kishino | 14:54378c96d285 | 10 | * |
kishino | 14:54378c96d285 | 11 | * $Revision: 0.0.0.1 $ |
kishino | 14:54378c96d285 | 12 | * ***********************************************************************/ |
kishino | 12:0254eaccfda2 | 13 | #include "mbed.h" |
kishino | 12:0254eaccfda2 | 14 | #include "SNIC_Core.h" |
kishino | 12:0254eaccfda2 | 15 | #include "SNIC_UartMsgUtil.h" |
kishino | 12:0254eaccfda2 | 16 | #include <string> |
kishino | 12:0254eaccfda2 | 17 | |
kishino | 12:0254eaccfda2 | 18 | using namespace murata_wifi; |
kishino | 12:0254eaccfda2 | 19 | |
kishino | 12:0254eaccfda2 | 20 | #define UART_RECVBUF_SIZE 2048 |
kishino | 12:0254eaccfda2 | 21 | typedef struct |
kishino | 12:0254eaccfda2 | 22 | { |
kishino | 12:0254eaccfda2 | 23 | unsigned char buf[UART_RECVBUF_SIZE]; |
kishino | 12:0254eaccfda2 | 24 | unsigned int size; |
kishino | 12:0254eaccfda2 | 25 | bool is_receive; |
kishino | 12:0254eaccfda2 | 26 | }tagUART_RECVBUF_T; |
kishino | 12:0254eaccfda2 | 27 | tagUART_RECVBUF_T gUART_RCVBUF; |
kishino | 12:0254eaccfda2 | 28 | unsigned char gUART_TEMP_BUF[UART_RECVBUF_SIZE]; |
kishino | 12:0254eaccfda2 | 29 | |
kishino | 12:0254eaccfda2 | 30 | C_SNIC_Core *C_SNIC_Core::mInstance_p = NULL; |
kishino | 12:0254eaccfda2 | 31 | |
kishino | 12:0254eaccfda2 | 32 | C_SNIC_Core *C_SNIC_Core::getInstance() |
kishino | 12:0254eaccfda2 | 33 | { |
kishino | 12:0254eaccfda2 | 34 | if( mInstance_p == NULL ) |
kishino | 12:0254eaccfda2 | 35 | { |
kishino | 16:6100acfeb1f1 | 36 | mInstance_p = new C_SNIC_Core(); |
kishino | 12:0254eaccfda2 | 37 | } |
kishino | 12:0254eaccfda2 | 38 | return mInstance_p; |
kishino | 12:0254eaccfda2 | 39 | } |
kishino | 12:0254eaccfda2 | 40 | |
kishino | 12:0254eaccfda2 | 41 | C_SNIC_Core::C_SNIC_Core() |
kishino | 12:0254eaccfda2 | 42 | { |
kishino | 12:0254eaccfda2 | 43 | int i; |
kishino | 12:0254eaccfda2 | 44 | |
kishino | 16:6100acfeb1f1 | 45 | mUartCommand_p = new C_SNIC_UartCommandManager(); |
kishino | 12:0254eaccfda2 | 46 | for( i = 0; i < MAX_SOCKET_ID+1; i++ ) |
kishino | 12:0254eaccfda2 | 47 | { |
kishino | 12:0254eaccfda2 | 48 | mConnectInfo[i].recvbuf_p = NULL; |
kishino | 12:0254eaccfda2 | 49 | mConnectInfo[i].is_connected = false; |
kishino | 12:0254eaccfda2 | 50 | } |
kishino | 12:0254eaccfda2 | 51 | |
kishino | 12:0254eaccfda2 | 52 | mUartRecvThread_p = NULL; |
kishino | 12:0254eaccfda2 | 53 | } |
kishino | 12:0254eaccfda2 | 54 | |
kishino | 12:0254eaccfda2 | 55 | int C_SNIC_Core::initUart(PinName tx, PinName rx, int baud) |
kishino | 12:0254eaccfda2 | 56 | { |
kishino | 12:0254eaccfda2 | 57 | // printf("[C_SNIC_Core::initUart]1\r\n"); |
kishino | 12:0254eaccfda2 | 58 | |
kishino | 12:0254eaccfda2 | 59 | mUartRequestSeq = 0; |
kishino | 12:0254eaccfda2 | 60 | |
kishino | 12:0254eaccfda2 | 61 | mUart_p = new RawSerial( tx, rx ); |
kishino | 12:0254eaccfda2 | 62 | mUart_p->baud( baud ); |
kishino | 12:0254eaccfda2 | 63 | mUart_p->format(8, SerialBase::None, 1); |
kishino | 12:0254eaccfda2 | 64 | |
kishino | 12:0254eaccfda2 | 65 | // printf("[C_SNIC_Core::initUart]2\r\n"); |
kishino | 12:0254eaccfda2 | 66 | // Initialize uart |
kishino | 12:0254eaccfda2 | 67 | gUART_RCVBUF.is_receive = false; |
kishino | 12:0254eaccfda2 | 68 | gUART_RCVBUF.size = 0; |
kishino | 12:0254eaccfda2 | 69 | |
kishino | 12:0254eaccfda2 | 70 | // Create UART recv thread |
kishino | 12:0254eaccfda2 | 71 | mUartRecvThread_p = new Thread( C_SNIC_Core::uartRecvThread ); |
kishino | 12:0254eaccfda2 | 72 | // printf("[C_SNIC_Core::initUart]3\r\n"); |
kishino | 12:0254eaccfda2 | 73 | if( mUartRecvThread_p == NULL ) |
kishino | 12:0254eaccfda2 | 74 | { |
kishino | 12:0254eaccfda2 | 75 | printf("[C_SNIC_Core::initUart] thread cread failed\r\n"); |
kishino | 12:0254eaccfda2 | 76 | return -1; |
kishino | 12:0254eaccfda2 | 77 | } |
kishino | 12:0254eaccfda2 | 78 | |
kishino | 12:0254eaccfda2 | 79 | return 0; |
kishino | 12:0254eaccfda2 | 80 | } |
kishino | 12:0254eaccfda2 | 81 | |
kishino | 12:0254eaccfda2 | 82 | unsigned int C_SNIC_Core::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid |
kishino | 12:0254eaccfda2 | 83 | , unsigned char *req_buf_p, unsigned int req_buf_len |
kishino | 12:0254eaccfda2 | 84 | , unsigned char *response_buf_p, unsigned char *command_p ) |
kishino | 12:0254eaccfda2 | 85 | { |
kishino | 12:0254eaccfda2 | 86 | unsigned char payload_array[UART_REQUEST_PAYLOAD_MAX]; |
kishino | 12:0254eaccfda2 | 87 | unsigned short payload_len; |
kishino | 12:0254eaccfda2 | 88 | unsigned int command_len = 0; |
kishino | 12:0254eaccfda2 | 89 | |
kishino | 12:0254eaccfda2 | 90 | // Make command payload |
kishino | 12:0254eaccfda2 | 91 | payload_len = C_SNIC_UartMsgUtil::makePayload( req_buf_len, req_buf_p, payload_array ); |
kishino | 12:0254eaccfda2 | 92 | // Make all command request |
kishino | 12:0254eaccfda2 | 93 | command_len = C_SNIC_UartMsgUtil::makeRequest( cmd_id, payload_array, payload_len, command_p ); |
kishino | 12:0254eaccfda2 | 94 | |
kishino | 12:0254eaccfda2 | 95 | // Set data for response |
kishino | 16:6100acfeb1f1 | 96 | mUartCommand_p->setCommandID( cmd_id ); |
kishino | 16:6100acfeb1f1 | 97 | mUartCommand_p->setCommandSID( cmd_sid | 0x80 ); |
kishino | 16:6100acfeb1f1 | 98 | mUartCommand_p->setResponseBuf( response_buf_p ); |
kishino | 12:0254eaccfda2 | 99 | |
kishino | 12:0254eaccfda2 | 100 | return command_len; |
kishino | 12:0254eaccfda2 | 101 | } |
kishino | 12:0254eaccfda2 | 102 | |
kishino | 12:0254eaccfda2 | 103 | int C_SNIC_Core::sendUart( unsigned int len, unsigned char *data ) |
kishino | 12:0254eaccfda2 | 104 | { |
kishino | 12:0254eaccfda2 | 105 | int ret = 0; |
kishino | 12:0254eaccfda2 | 106 | |
kishino | 12:0254eaccfda2 | 107 | mUartMutex.lock(); |
kishino | 12:0254eaccfda2 | 108 | for( int i = 0; i < len; i++ ) |
kishino | 12:0254eaccfda2 | 109 | { |
kishino | 12:0254eaccfda2 | 110 | // Write to UART |
kishino | 12:0254eaccfda2 | 111 | ret = mUart_p->putc( data[i] ); |
kishino | 12:0254eaccfda2 | 112 | if( ret == -1 ) |
kishino | 12:0254eaccfda2 | 113 | { |
kishino | 12:0254eaccfda2 | 114 | ret = -1; |
kishino | 12:0254eaccfda2 | 115 | break; |
kishino | 12:0254eaccfda2 | 116 | } |
kishino | 12:0254eaccfda2 | 117 | } |
kishino | 12:0254eaccfda2 | 118 | mUartMutex.unlock(); |
kishino | 12:0254eaccfda2 | 119 | return ret; |
kishino | 12:0254eaccfda2 | 120 | } |
kishino | 12:0254eaccfda2 | 121 | |
kishino | 15:5eb637414df2 | 122 | tagMEMPOOL_BLOCK_T *C_SNIC_Core::allocCmdBuf() |
kishino | 12:0254eaccfda2 | 123 | { |
kishino | 12:0254eaccfda2 | 124 | // Get buffer from MemoryPool |
kishino | 12:0254eaccfda2 | 125 | return mMemPoolPayload.alloc(); |
kishino | 12:0254eaccfda2 | 126 | } |
kishino | 12:0254eaccfda2 | 127 | |
kishino | 12:0254eaccfda2 | 128 | void C_SNIC_Core::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p ) |
kishino | 12:0254eaccfda2 | 129 | { |
kishino | 12:0254eaccfda2 | 130 | mMemPoolPayload.free( buf_p ); |
kishino | 12:0254eaccfda2 | 131 | } |
kishino | 12:0254eaccfda2 | 132 | |
kishino | 12:0254eaccfda2 | 133 | tagCONNECT_INFO_T *C_SNIC_Core::getConnectInfo( int socket_id ) |
kishino | 12:0254eaccfda2 | 134 | { |
kishino | 12:0254eaccfda2 | 135 | if( (socket_id < 0) || (socket_id > MAX_SOCKET_ID) ) |
kishino | 12:0254eaccfda2 | 136 | { |
kishino | 12:0254eaccfda2 | 137 | return NULL; |
kishino | 12:0254eaccfda2 | 138 | } |
kishino | 12:0254eaccfda2 | 139 | return &mConnectInfo[socket_id]; |
kishino | 12:0254eaccfda2 | 140 | } |
kishino | 12:0254eaccfda2 | 141 | |
kishino | 16:6100acfeb1f1 | 142 | C_SNIC_UartCommandManager *C_SNIC_Core::getUartCommand() |
kishino | 16:6100acfeb1f1 | 143 | { |
kishino | 16:6100acfeb1f1 | 144 | return mUartCommand_p; |
kishino | 16:6100acfeb1f1 | 145 | } |
kishino | 16:6100acfeb1f1 | 146 | |
kishino | 12:0254eaccfda2 | 147 | DigitalOut led1(LED1); |
kishino | 12:0254eaccfda2 | 148 | void C_SNIC_Core::uartRecvThread (void const *args_p) { |
kishino | 12:0254eaccfda2 | 149 | |
kishino | 12:0254eaccfda2 | 150 | C_SNIC_Core *instance_p = C_SNIC_Core::getInstance(); |
kishino | 12:0254eaccfda2 | 151 | if ( instance_p == NULL ) |
kishino | 12:0254eaccfda2 | 152 | { |
kishino | 12:0254eaccfda2 | 153 | printf("Socket constructor error: no wifly instance available!\r\n"); |
kishino | 12:0254eaccfda2 | 154 | } |
kishino | 16:6100acfeb1f1 | 155 | C_SNIC_UartCommandManager *uartCmdMgr_p = instance_p->getUartCommand(); |
kishino | 12:0254eaccfda2 | 156 | |
kishino | 12:0254eaccfda2 | 157 | int recvdata = 0; |
kishino | 12:0254eaccfda2 | 158 | int i; |
kishino | 12:0254eaccfda2 | 159 | |
kishino | 12:0254eaccfda2 | 160 | /* UART recv thread main loop */ |
kishino | 12:0254eaccfda2 | 161 | for (;;) |
kishino | 12:0254eaccfda2 | 162 | { |
kishino | 12:0254eaccfda2 | 163 | while( instance_p->mUart_p->readable() ) |
kishino | 12:0254eaccfda2 | 164 | { |
kishino | 12:0254eaccfda2 | 165 | // Receive data from UART. |
kishino | 12:0254eaccfda2 | 166 | instance_p->mUartMutex.lock(); |
kishino | 12:0254eaccfda2 | 167 | recvdata = instance_p->mUart_p->getc(); |
kishino | 12:0254eaccfda2 | 168 | instance_p->mUartMutex.unlock(); |
kishino | 12:0254eaccfda2 | 169 | |
kishino | 12:0254eaccfda2 | 170 | // Check UART receiving flg |
kishino | 12:0254eaccfda2 | 171 | if( gUART_RCVBUF.is_receive ) |
kishino | 12:0254eaccfda2 | 172 | { |
kishino | 12:0254eaccfda2 | 173 | gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata; |
kishino | 12:0254eaccfda2 | 174 | gUART_RCVBUF.size++; |
kishino | 12:0254eaccfda2 | 175 | // Check received data is EOM. |
kishino | 12:0254eaccfda2 | 176 | if( recvdata == UART_CMD_EOM ) |
kishino | 12:0254eaccfda2 | 177 | { |
kishino | 12:0254eaccfda2 | 178 | led1 = 0; |
kishino | 12:0254eaccfda2 | 179 | #if 0 |
kishino | 12:0254eaccfda2 | 180 | printf("[recv]\r\n"); |
kishino | 12:0254eaccfda2 | 181 | for( i = 0; i < gUART_RCVBUF.size; i++ ) |
kishino | 12:0254eaccfda2 | 182 | { |
kishino | 12:0254eaccfda2 | 183 | printf("%02x ", gUART_RCVBUF.buf[i]); |
kishino | 12:0254eaccfda2 | 184 | } |
kishino | 12:0254eaccfda2 | 185 | printf("\r\n"); |
kishino | 12:0254eaccfda2 | 186 | #endif |
kishino | 12:0254eaccfda2 | 187 | unsigned char command_id; |
kishino | 12:0254eaccfda2 | 188 | // Get payload from received data from UART. |
kishino | 12:0254eaccfda2 | 189 | int payload_len = C_SNIC_UartMsgUtil::getResponsePayload( gUART_RCVBUF.size, gUART_RCVBUF.buf |
kishino | 12:0254eaccfda2 | 190 | , &command_id, gUART_TEMP_BUF ); |
kishino | 12:0254eaccfda2 | 191 | |
kishino | 12:0254eaccfda2 | 192 | // Check receive a TCP or UDP packet |
kishino | 12:0254eaccfda2 | 193 | if( (command_id == UART_CMD_ID_SNIC) && (gUART_TEMP_BUF[0] == UART_CMD_SID_SNIC_CONNECTION_RECV_IND) ) |
kishino | 12:0254eaccfda2 | 194 | { |
kishino | 12:0254eaccfda2 | 195 | // Packet buffering |
kishino | 16:6100acfeb1f1 | 196 | uartCmdMgr_p->bufferredPacket( gUART_TEMP_BUF, payload_len ); |
kishino | 12:0254eaccfda2 | 197 | } |
kishino | 12:0254eaccfda2 | 198 | |
kishino | 12:0254eaccfda2 | 199 | // Check scan results indication |
kishino | 12:0254eaccfda2 | 200 | else if( (command_id == UART_CMD_ID_WIFI) && (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) ) |
kishino | 12:0254eaccfda2 | 201 | { |
kishino | 12:0254eaccfda2 | 202 | // Scan result indicate |
kishino | 16:6100acfeb1f1 | 203 | uartCmdMgr_p->scanResultIndicate( gUART_TEMP_BUF, payload_len ); |
kishino | 12:0254eaccfda2 | 204 | } |
kishino | 12:0254eaccfda2 | 205 | // Checks in the command which is waiting. |
kishino | 16:6100acfeb1f1 | 206 | else if( uartCmdMgr_p->isWaitingCommand(command_id, gUART_TEMP_BUF) ) |
kishino | 12:0254eaccfda2 | 207 | { |
kishino | 12:0254eaccfda2 | 208 | // Get buffer for payload data |
kishino | 16:6100acfeb1f1 | 209 | unsigned char *payload_buf_p = uartCmdMgr_p->getResponseBuf(); |
kishino | 12:0254eaccfda2 | 210 | if( payload_buf_p != NULL ) |
kishino | 12:0254eaccfda2 | 211 | { |
kishino | 12:0254eaccfda2 | 212 | memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len ); |
kishino | 16:6100acfeb1f1 | 213 | uartCmdMgr_p->setResponseBuf( NULL ); |
kishino | 12:0254eaccfda2 | 214 | } |
kishino | 12:0254eaccfda2 | 215 | // Set status |
kishino | 16:6100acfeb1f1 | 216 | uartCmdMgr_p->setCommandStatus( gUART_TEMP_BUF[2] ); |
kishino | 12:0254eaccfda2 | 217 | // Set signal for command response wait. |
kishino | 16:6100acfeb1f1 | 218 | uartCmdMgr_p->signal(); |
kishino | 12:0254eaccfda2 | 219 | } |
kishino | 12:0254eaccfda2 | 220 | |
kishino | 12:0254eaccfda2 | 221 | gUART_RCVBUF.size = 0; |
kishino | 12:0254eaccfda2 | 222 | gUART_RCVBUF.is_receive = false; |
kishino | 12:0254eaccfda2 | 223 | } |
kishino | 12:0254eaccfda2 | 224 | } |
kishino | 12:0254eaccfda2 | 225 | else |
kishino | 12:0254eaccfda2 | 226 | { |
kishino | 12:0254eaccfda2 | 227 | // Check received data is SOM. |
kishino | 12:0254eaccfda2 | 228 | if( recvdata == UART_CMD_SOM ) |
kishino | 12:0254eaccfda2 | 229 | { |
kishino | 12:0254eaccfda2 | 230 | led1 = 1; |
kishino | 12:0254eaccfda2 | 231 | gUART_RCVBUF.size = 0; |
kishino | 12:0254eaccfda2 | 232 | gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata; |
kishino | 12:0254eaccfda2 | 233 | gUART_RCVBUF.size++; |
kishino | 12:0254eaccfda2 | 234 | gUART_RCVBUF.is_receive = true; |
kishino | 12:0254eaccfda2 | 235 | } |
kishino | 12:0254eaccfda2 | 236 | } |
kishino | 12:0254eaccfda2 | 237 | // Thread::yield(); |
kishino | 12:0254eaccfda2 | 238 | } |
kishino | 12:0254eaccfda2 | 239 | Thread::yield(); |
kishino | 12:0254eaccfda2 | 240 | } |
kishino | 12:0254eaccfda2 | 241 | } |
kishino | 12:0254eaccfda2 | 242 |