Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: SNIC-xively-jumpstart-demo SNIC-FluentLogger-example TCPEchoServer murataDemo ... more
Fork of YDwifiInterface by
SNIC/SNICwifi.cpp@11:c49007d49e52, 2014-03-19 (annotated)
- Committer:
- kishino
- Date:
- Wed Mar 19 01:48:37 2014 +0000
- Revision:
- 11:c49007d49e52
- Parent:
- SNICwifi/SNICwifi.cpp@10:49ffd373066b
C_SNICwifi was modified to singleton class.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| kishino | 0:61c402886fbb | 1 | #include "mbed.h" |
| kishino | 8:50d2509479cd | 2 | #include "SNICwifi.h" |
| kishino | 8:50d2509479cd | 3 | #include "SNICwifi_uartmsg.h" |
| kishino | 0:61c402886fbb | 4 | #include <string> |
| kishino | 0:61c402886fbb | 5 | //#include <algorithm> |
| kishino | 0:61c402886fbb | 6 | |
| kishino | 2:0ba43344c814 | 7 | using namespace murata_wifi; |
| kishino | 2:0ba43344c814 | 8 | |
| kishino | 2:0ba43344c814 | 9 | #define UART_RECVBUF_SIZE 2048 |
| kishino | 2:0ba43344c814 | 10 | typedef struct |
| kishino | 2:0ba43344c814 | 11 | { |
| kishino | 2:0ba43344c814 | 12 | unsigned char buf[UART_RECVBUF_SIZE]; |
| kishino | 2:0ba43344c814 | 13 | unsigned int size; |
| kishino | 2:0ba43344c814 | 14 | bool is_receive; |
| kishino | 2:0ba43344c814 | 15 | }tagUART_RECVBUF_T; |
| kishino | 2:0ba43344c814 | 16 | tagUART_RECVBUF_T gUART_RCVBUF; |
| kishino | 3:9f90024d7fb2 | 17 | unsigned char gUART_TEMP_BUF[UART_RECVBUF_SIZE]; |
| kishino | 2:0ba43344c814 | 18 | |
| kishino | 11:c49007d49e52 | 19 | C_SNICwifi *C_SNICwifi::mInstance_p = NULL; |
| kishino | 0:61c402886fbb | 20 | |
| kishino | 11:c49007d49e52 | 21 | C_SNICwifi *C_SNICwifi::getInstance() |
| kishino | 11:c49007d49e52 | 22 | { |
| kishino | 11:c49007d49e52 | 23 | if( mInstance_p == NULL ) |
| kishino | 11:c49007d49e52 | 24 | { |
| kishino | 11:c49007d49e52 | 25 | mInstance_p = new C_SNICwifi(); |
| kishino | 11:c49007d49e52 | 26 | } |
| kishino | 11:c49007d49e52 | 27 | return mInstance_p; |
| kishino | 11:c49007d49e52 | 28 | } |
| kishino | 11:c49007d49e52 | 29 | |
| kishino | 11:c49007d49e52 | 30 | C_SNICwifi::C_SNICwifi() |
| kishino | 0:61c402886fbb | 31 | { |
| kishino | 0:61c402886fbb | 32 | mUartRecvThread_p = NULL; |
| kishino | 11:c49007d49e52 | 33 | } |
| kishino | 11:c49007d49e52 | 34 | |
| kishino | 11:c49007d49e52 | 35 | int C_SNICwifi::initUart(PinName tx, PinName rx, int baud) |
| kishino | 11:c49007d49e52 | 36 | { |
| kishino | 11:c49007d49e52 | 37 | printf("[C_SNICwifi::initUart]1\r\n"); |
| kishino | 11:c49007d49e52 | 38 | |
| kishino | 0:61c402886fbb | 39 | mUartRequestSeq = 0; |
| kishino | 0:61c402886fbb | 40 | |
| kishino | 11:c49007d49e52 | 41 | mUart_p = new RawSerial( tx, rx ); |
| kishino | 11:c49007d49e52 | 42 | mUart_p->baud( baud ); |
| kishino | 11:c49007d49e52 | 43 | mUart_p->format(8, SerialBase::None, 1); |
| kishino | 11:c49007d49e52 | 44 | |
| kishino | 11:c49007d49e52 | 45 | printf("[C_SNICwifi::initUart]2\r\n"); |
| kishino | 2:0ba43344c814 | 46 | // Initialize uart |
| kishino | 2:0ba43344c814 | 47 | gUART_RCVBUF.is_receive = false; |
| kishino | 2:0ba43344c814 | 48 | gUART_RCVBUF.size = 0; |
| kishino | 0:61c402886fbb | 49 | |
| kishino | 0:61c402886fbb | 50 | // Create UART recv thread |
| kishino | 8:50d2509479cd | 51 | mUartRecvThread_p = new Thread( C_SNICwifi::uartRecvThread ); |
| kishino | 11:c49007d49e52 | 52 | printf("[C_SNICwifi::initUart]3\r\n"); |
| kishino | 0:61c402886fbb | 53 | if( mUartRecvThread_p == NULL ) |
| kishino | 0:61c402886fbb | 54 | { |
| kishino | 8:50d2509479cd | 55 | printf("[C_SNICwifi::initUart] thread cread failed\r\n"); |
| kishino | 0:61c402886fbb | 56 | return -1; |
| kishino | 0:61c402886fbb | 57 | } |
| kishino | 2:0ba43344c814 | 58 | |
| kishino | 0:61c402886fbb | 59 | return 0; |
| kishino | 0:61c402886fbb | 60 | } |
| kishino | 0:61c402886fbb | 61 | |
| kishino | 10:49ffd373066b | 62 | unsigned int C_SNICwifi::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid |
| kishino | 10:49ffd373066b | 63 | , unsigned char *req_buf_p, unsigned int req_buf_len |
| kishino | 10:49ffd373066b | 64 | , unsigned char *response_buf_p, unsigned char *command_p ) |
| kishino | 10:49ffd373066b | 65 | { |
| kishino | 10:49ffd373066b | 66 | unsigned char payload_array[UART_REQUEST_PAYLOAD_MAX]; |
| kishino | 10:49ffd373066b | 67 | unsigned short payload_len; |
| kishino | 10:49ffd373066b | 68 | unsigned int command_len = 0; |
| kishino | 10:49ffd373066b | 69 | |
| kishino | 10:49ffd373066b | 70 | // Make command payload |
| kishino | 10:49ffd373066b | 71 | payload_len = C_SNIC_UartMsg::makePayload( req_buf_len, req_buf_p, payload_array ); |
| kishino | 10:49ffd373066b | 72 | // Make all command request |
| kishino | 10:49ffd373066b | 73 | command_len = C_SNIC_UartMsg::makeRequest( cmd_id, payload_array, payload_len, command_p ); |
| kishino | 10:49ffd373066b | 74 | |
| kishino | 10:49ffd373066b | 75 | // Set data for response |
| kishino | 10:49ffd373066b | 76 | mUartCommand.setCommandID( cmd_id ); |
| kishino | 10:49ffd373066b | 77 | mUartCommand.setCommandSID( cmd_sid ); |
| kishino | 10:49ffd373066b | 78 | mUartCommand.setResponseBuf( response_buf_p ); |
| kishino | 10:49ffd373066b | 79 | |
| kishino | 10:49ffd373066b | 80 | return command_len; |
| kishino | 10:49ffd373066b | 81 | } |
| kishino | 10:49ffd373066b | 82 | |
| kishino | 8:50d2509479cd | 83 | int C_SNICwifi::sendUart( unsigned int len, unsigned char *data ) |
| kishino | 0:61c402886fbb | 84 | { |
| kishino | 2:0ba43344c814 | 85 | int ret = 0; |
| kishino | 2:0ba43344c814 | 86 | |
| kishino | 2:0ba43344c814 | 87 | mUartMutex.lock(); |
| kishino | 2:0ba43344c814 | 88 | for( int i = 0; i < len; i++ ) |
| kishino | 2:0ba43344c814 | 89 | { |
| kishino | 2:0ba43344c814 | 90 | // Write to UART |
| kishino | 11:c49007d49e52 | 91 | ret = mUart_p->putc( data[i] ); |
| kishino | 2:0ba43344c814 | 92 | if( ret == -1 ) |
| kishino | 2:0ba43344c814 | 93 | { |
| kishino | 2:0ba43344c814 | 94 | ret = -1; |
| kishino | 2:0ba43344c814 | 95 | break; |
| kishino | 2:0ba43344c814 | 96 | } |
| kishino | 2:0ba43344c814 | 97 | } |
| kishino | 2:0ba43344c814 | 98 | mUartMutex.unlock(); |
| kishino | 2:0ba43344c814 | 99 | return ret; |
| kishino | 0:61c402886fbb | 100 | } |
| kishino | 0:61c402886fbb | 101 | |
| kishino | 9:a98b45e766c8 | 102 | tagMEMPOOL_BLOCK_T *C_SNICwifi::getAlocCmdBuf() |
| kishino | 9:a98b45e766c8 | 103 | { |
| kishino | 9:a98b45e766c8 | 104 | // Get buffer from MemoryPool |
| kishino | 9:a98b45e766c8 | 105 | return mMemPoolPayload.alloc(); |
| kishino | 9:a98b45e766c8 | 106 | } |
| kishino | 9:a98b45e766c8 | 107 | |
| kishino | 9:a98b45e766c8 | 108 | void C_SNICwifi::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p ) |
| kishino | 9:a98b45e766c8 | 109 | { |
| kishino | 9:a98b45e766c8 | 110 | mMemPoolPayload.free( buf_p ); |
| kishino | 9:a98b45e766c8 | 111 | } |
| kishino | 9:a98b45e766c8 | 112 | |
| kishino | 8:50d2509479cd | 113 | void C_SNICwifi::uartRecvThread (void const *args_p) { |
| kishino | 2:0ba43344c814 | 114 | |
| kishino | 8:50d2509479cd | 115 | C_SNICwifi *instance_p = C_SNICwifi::getInstance(); |
| kishino | 0:61c402886fbb | 116 | if ( instance_p == NULL ) |
| kishino | 0:61c402886fbb | 117 | { |
| kishino | 0:61c402886fbb | 118 | printf("Socket constructor error: no wifly instance available!\r\n"); |
| kishino | 0:61c402886fbb | 119 | } |
| kishino | 0:61c402886fbb | 120 | |
| kishino | 0:61c402886fbb | 121 | int recvdata = 0; |
| kishino | 2:0ba43344c814 | 122 | int i; |
| kishino | 2:0ba43344c814 | 123 | |
| kishino | 0:61c402886fbb | 124 | /* UART recv thread main loop */ |
| kishino | 0:61c402886fbb | 125 | for (;;) |
| kishino | 0:61c402886fbb | 126 | { |
| kishino | 11:c49007d49e52 | 127 | while( instance_p->mUart_p->readable() ) |
| kishino | 2:0ba43344c814 | 128 | { |
| kishino | 2:0ba43344c814 | 129 | // Receive data from UART. |
| kishino | 2:0ba43344c814 | 130 | instance_p->mUartMutex.lock(); |
| kishino | 11:c49007d49e52 | 131 | recvdata = instance_p->mUart_p->getc(); |
| kishino | 2:0ba43344c814 | 132 | instance_p->mUartMutex.unlock(); |
| kishino | 7:e88ccbe0225f | 133 | |
| kishino | 2:0ba43344c814 | 134 | // Check UART receiving flg |
| kishino | 2:0ba43344c814 | 135 | if( gUART_RCVBUF.is_receive ) |
| kishino | 2:0ba43344c814 | 136 | { |
| kishino | 2:0ba43344c814 | 137 | gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata; |
| kishino | 2:0ba43344c814 | 138 | gUART_RCVBUF.size++; |
| kishino | 2:0ba43344c814 | 139 | // Check received data is EOM. |
| kishino | 2:0ba43344c814 | 140 | if( recvdata == UART_CMD_EOM ) |
| kishino | 2:0ba43344c814 | 141 | { |
| kishino | 3:9f90024d7fb2 | 142 | /* |
| kishino | 2:0ba43344c814 | 143 | printf("[recv]\r\n"); |
| kishino | 2:0ba43344c814 | 144 | for( i = 0; i < gUART_RCVBUF.size; i++ ) |
| kishino | 2:0ba43344c814 | 145 | { |
| kishino | 2:0ba43344c814 | 146 | printf("%02x ", gUART_RCVBUF.buf[i]); |
| kishino | 2:0ba43344c814 | 147 | } |
| kishino | 2:0ba43344c814 | 148 | printf("\r\n"); |
| kishino | 5:ef3befe3edad | 149 | */ |
| kishino | 2:0ba43344c814 | 150 | unsigned char command_id; |
| kishino | 3:9f90024d7fb2 | 151 | // Get payload from received data from UART. |
| kishino | 8:50d2509479cd | 152 | int payload_len = C_SNIC_UartMsg::getResponsePayload( gUART_RCVBUF.size, gUART_RCVBUF.buf |
| kishino | 3:9f90024d7fb2 | 153 | , &command_id, gUART_TEMP_BUF ); |
| kishino | 3:9f90024d7fb2 | 154 | /* |
| kishino | 3:9f90024d7fb2 | 155 | printf("[payload]\r\n"); |
| kishino | 3:9f90024d7fb2 | 156 | for( i = 0; i < payload_len; i++ ) |
| kishino | 3:9f90024d7fb2 | 157 | { |
| kishino | 3:9f90024d7fb2 | 158 | printf("%02x ", gUART_TEMP_BUF[i]); |
| kishino | 3:9f90024d7fb2 | 159 | } |
| kishino | 3:9f90024d7fb2 | 160 | printf("\r\n"); |
| kishino | 5:ef3befe3edad | 161 | */ |
| kishino | 3:9f90024d7fb2 | 162 | // Check scan results indication |
| kishino | 3:9f90024d7fb2 | 163 | if( (command_id == UART_CMD_ID_WIFI) || (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) ) |
| kishino | 3:9f90024d7fb2 | 164 | { |
| kishino | 3:9f90024d7fb2 | 165 | // Scan result indicate |
| kishino | 3:9f90024d7fb2 | 166 | instance_p->mUartCommand.scanResultIndicate( gUART_TEMP_BUF, payload_len ); |
| kishino | 3:9f90024d7fb2 | 167 | } |
| kishino | 2:0ba43344c814 | 168 | |
| kishino | 3:9f90024d7fb2 | 169 | // Checks in the command which is waiting. |
| kishino | 3:9f90024d7fb2 | 170 | if( instance_p->mUartCommand.isWaitingCommand(command_id, gUART_TEMP_BUF) ) |
| kishino | 2:0ba43344c814 | 171 | { |
| kishino | 3:9f90024d7fb2 | 172 | // Get buffer for payload data |
| kishino | 3:9f90024d7fb2 | 173 | unsigned char *payload_buf_p = instance_p->mUartCommand.getResponseBuf(); |
| kishino | 3:9f90024d7fb2 | 174 | if( payload_buf_p != NULL ) |
| kishino | 2:0ba43344c814 | 175 | { |
| kishino | 3:9f90024d7fb2 | 176 | memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len ); |
| kishino | 3:9f90024d7fb2 | 177 | instance_p->mUartCommand.setResponseBuf( NULL ); |
| kishino | 2:0ba43344c814 | 178 | } |
| kishino | 3:9f90024d7fb2 | 179 | // Set status |
| kishino | 3:9f90024d7fb2 | 180 | instance_p->mUartCommand.setCommandStatus( gUART_TEMP_BUF[2] ); |
| kishino | 3:9f90024d7fb2 | 181 | // Set signal for command response wait. |
| kishino | 3:9f90024d7fb2 | 182 | instance_p->mUartCommand.signal(); |
| kishino | 2:0ba43344c814 | 183 | } |
| kishino | 3:9f90024d7fb2 | 184 | |
| kishino | 2:0ba43344c814 | 185 | gUART_RCVBUF.size = 0; |
| kishino | 2:0ba43344c814 | 186 | gUART_RCVBUF.is_receive = false; |
| kishino | 2:0ba43344c814 | 187 | } |
| kishino | 2:0ba43344c814 | 188 | } |
| kishino | 2:0ba43344c814 | 189 | else |
| kishino | 2:0ba43344c814 | 190 | { |
| kishino | 2:0ba43344c814 | 191 | // Check received data is SOM. |
| kishino | 2:0ba43344c814 | 192 | if( recvdata == UART_CMD_SOM ) |
| kishino | 2:0ba43344c814 | 193 | { |
| kishino | 2:0ba43344c814 | 194 | gUART_RCVBUF.size = 0; |
| kishino | 2:0ba43344c814 | 195 | gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata; |
| kishino | 2:0ba43344c814 | 196 | gUART_RCVBUF.size++; |
| kishino | 2:0ba43344c814 | 197 | gUART_RCVBUF.is_receive = true; |
| kishino | 2:0ba43344c814 | 198 | } |
| kishino | 2:0ba43344c814 | 199 | } |
| kishino | 7:e88ccbe0225f | 200 | // Thread::yield(); |
| kishino | 2:0ba43344c814 | 201 | } |
| kishino | 2:0ba43344c814 | 202 | Thread::yield(); |
| kishino | 0:61c402886fbb | 203 | } |
| kishino | 0:61c402886fbb | 204 | } |
| kishino | 0:61c402886fbb | 205 |

Murata TypeYD