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