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.
Fork of NySNICInterface by
SNICwifi/SNICwifi.cpp@10:49ffd373066b, 2014-03-18 (annotated)
- Committer:
- kishino
- Date:
- Tue Mar 18 02:57:24 2014 +0000
- Revision:
- 10:49ffd373066b
- Parent:
- 9:a98b45e766c8
[Refactoring]Created the common function for the preparation of request command of UART.
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 | 8:50d2509479cd | 19 | C_SNICwifi *C_SNICwifi::mInstance_p; |
| kishino | 0:61c402886fbb | 20 | |
| kishino | 8:50d2509479cd | 21 | C_SNICwifi::C_SNICwifi(PinName tx, PinName rx, PinName cts, PinName rts, PinName reset, PinName alarm, int baud): |
| kishino | 6:70f522934032 | 22 | mUart(tx, rx) |
| kishino | 0:61c402886fbb | 23 | { |
| kishino | 0:61c402886fbb | 24 | mUartRecvThread_p = NULL; |
| kishino | 0:61c402886fbb | 25 | mInstance_p = this; |
| kishino | 0:61c402886fbb | 26 | mUartRequestSeq = 0; |
| kishino | 0:61c402886fbb | 27 | |
| kishino | 2:0ba43344c814 | 28 | // Initialize uart |
| kishino | 2:0ba43344c814 | 29 | gUART_RCVBUF.is_receive = false; |
| kishino | 2:0ba43344c814 | 30 | gUART_RCVBUF.size = 0; |
| kishino | 2:0ba43344c814 | 31 | mUart.baud( 115200 ); |
| kishino | 2:0ba43344c814 | 32 | mUart.format(8, SerialBase::None, 1); |
| kishino | 0:61c402886fbb | 33 | } |
| kishino | 0:61c402886fbb | 34 | |
| kishino | 8:50d2509479cd | 35 | int C_SNICwifi::initUart() |
| kishino | 0:61c402886fbb | 36 | { |
| kishino | 0:61c402886fbb | 37 | // Create UART recv thread |
| kishino | 8:50d2509479cd | 38 | mUartRecvThread_p = new Thread( C_SNICwifi::uartRecvThread ); |
| kishino | 0:61c402886fbb | 39 | if( mUartRecvThread_p == NULL ) |
| kishino | 0:61c402886fbb | 40 | { |
| kishino | 8:50d2509479cd | 41 | printf("[C_SNICwifi::initUart] thread cread failed\r\n"); |
| kishino | 0:61c402886fbb | 42 | return -1; |
| kishino | 0:61c402886fbb | 43 | } |
| kishino | 2:0ba43344c814 | 44 | |
| kishino | 0:61c402886fbb | 45 | return 0; |
| kishino | 0:61c402886fbb | 46 | } |
| kishino | 0:61c402886fbb | 47 | |
| kishino | 10:49ffd373066b | 48 | unsigned int C_SNICwifi::preparationSendCommand( unsigned char cmd_id, unsigned char cmd_sid |
| kishino | 10:49ffd373066b | 49 | , unsigned char *req_buf_p, unsigned int req_buf_len |
| kishino | 10:49ffd373066b | 50 | , unsigned char *response_buf_p, unsigned char *command_p ) |
| kishino | 10:49ffd373066b | 51 | { |
| kishino | 10:49ffd373066b | 52 | unsigned char payload_array[UART_REQUEST_PAYLOAD_MAX]; |
| kishino | 10:49ffd373066b | 53 | unsigned short payload_len; |
| kishino | 10:49ffd373066b | 54 | unsigned int command_len = 0; |
| kishino | 10:49ffd373066b | 55 | |
| kishino | 10:49ffd373066b | 56 | // Make command payload |
| kishino | 10:49ffd373066b | 57 | payload_len = C_SNIC_UartMsg::makePayload( req_buf_len, req_buf_p, payload_array ); |
| kishino | 10:49ffd373066b | 58 | // Make all command request |
| kishino | 10:49ffd373066b | 59 | command_len = C_SNIC_UartMsg::makeRequest( cmd_id, payload_array, payload_len, command_p ); |
| kishino | 10:49ffd373066b | 60 | |
| kishino | 10:49ffd373066b | 61 | // Set data for response |
| kishino | 10:49ffd373066b | 62 | mUartCommand.setCommandID( cmd_id ); |
| kishino | 10:49ffd373066b | 63 | mUartCommand.setCommandSID( cmd_sid ); |
| kishino | 10:49ffd373066b | 64 | mUartCommand.setResponseBuf( response_buf_p ); |
| kishino | 10:49ffd373066b | 65 | |
| kishino | 10:49ffd373066b | 66 | return command_len; |
| kishino | 10:49ffd373066b | 67 | } |
| kishino | 10:49ffd373066b | 68 | |
| kishino | 8:50d2509479cd | 69 | int C_SNICwifi::sendUart( unsigned int len, unsigned char *data ) |
| kishino | 0:61c402886fbb | 70 | { |
| kishino | 2:0ba43344c814 | 71 | int ret = 0; |
| kishino | 2:0ba43344c814 | 72 | |
| kishino | 2:0ba43344c814 | 73 | mUartMutex.lock(); |
| kishino | 2:0ba43344c814 | 74 | for( int i = 0; i < len; i++ ) |
| kishino | 2:0ba43344c814 | 75 | { |
| kishino | 2:0ba43344c814 | 76 | // Write to UART |
| kishino | 2:0ba43344c814 | 77 | ret = mUart.putc( data[i] ); |
| kishino | 2:0ba43344c814 | 78 | if( ret == -1 ) |
| kishino | 2:0ba43344c814 | 79 | { |
| kishino | 2:0ba43344c814 | 80 | ret = -1; |
| kishino | 2:0ba43344c814 | 81 | break; |
| kishino | 2:0ba43344c814 | 82 | } |
| kishino | 2:0ba43344c814 | 83 | } |
| kishino | 2:0ba43344c814 | 84 | mUartMutex.unlock(); |
| kishino | 2:0ba43344c814 | 85 | return ret; |
| kishino | 0:61c402886fbb | 86 | } |
| kishino | 0:61c402886fbb | 87 | |
| kishino | 9:a98b45e766c8 | 88 | tagMEMPOOL_BLOCK_T *C_SNICwifi::getAlocCmdBuf() |
| kishino | 9:a98b45e766c8 | 89 | { |
| kishino | 9:a98b45e766c8 | 90 | // Get buffer from MemoryPool |
| kishino | 9:a98b45e766c8 | 91 | return mMemPoolPayload.alloc(); |
| kishino | 9:a98b45e766c8 | 92 | } |
| kishino | 9:a98b45e766c8 | 93 | |
| kishino | 9:a98b45e766c8 | 94 | void C_SNICwifi::freeCmdBuf( tagMEMPOOL_BLOCK_T *buf_p ) |
| kishino | 9:a98b45e766c8 | 95 | { |
| kishino | 9:a98b45e766c8 | 96 | mMemPoolPayload.free( buf_p ); |
| kishino | 9:a98b45e766c8 | 97 | } |
| kishino | 9:a98b45e766c8 | 98 | |
| kishino | 8:50d2509479cd | 99 | void C_SNICwifi::uartRecvThread (void const *args_p) { |
| kishino | 2:0ba43344c814 | 100 | |
| kishino | 8:50d2509479cd | 101 | C_SNICwifi *instance_p = C_SNICwifi::getInstance(); |
| kishino | 0:61c402886fbb | 102 | if ( instance_p == NULL ) |
| kishino | 0:61c402886fbb | 103 | { |
| kishino | 0:61c402886fbb | 104 | printf("Socket constructor error: no wifly instance available!\r\n"); |
| kishino | 0:61c402886fbb | 105 | } |
| kishino | 0:61c402886fbb | 106 | |
| kishino | 0:61c402886fbb | 107 | int recvdata = 0; |
| kishino | 2:0ba43344c814 | 108 | int i; |
| kishino | 2:0ba43344c814 | 109 | |
| kishino | 0:61c402886fbb | 110 | /* UART recv thread main loop */ |
| kishino | 0:61c402886fbb | 111 | for (;;) |
| kishino | 0:61c402886fbb | 112 | { |
| kishino | 2:0ba43344c814 | 113 | while( instance_p->mUart.readable() ) |
| kishino | 2:0ba43344c814 | 114 | { |
| kishino | 2:0ba43344c814 | 115 | // Receive data from UART. |
| kishino | 2:0ba43344c814 | 116 | instance_p->mUartMutex.lock(); |
| kishino | 2:0ba43344c814 | 117 | recvdata = instance_p->mUart.getc(); |
| kishino | 2:0ba43344c814 | 118 | instance_p->mUartMutex.unlock(); |
| kishino | 7:e88ccbe0225f | 119 | |
| kishino | 2:0ba43344c814 | 120 | // Check UART receiving flg |
| kishino | 2:0ba43344c814 | 121 | if( gUART_RCVBUF.is_receive ) |
| kishino | 2:0ba43344c814 | 122 | { |
| kishino | 2:0ba43344c814 | 123 | gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata; |
| kishino | 2:0ba43344c814 | 124 | gUART_RCVBUF.size++; |
| kishino | 2:0ba43344c814 | 125 | // Check received data is EOM. |
| kishino | 2:0ba43344c814 | 126 | if( recvdata == UART_CMD_EOM ) |
| kishino | 2:0ba43344c814 | 127 | { |
| kishino | 3:9f90024d7fb2 | 128 | /* |
| kishino | 2:0ba43344c814 | 129 | printf("[recv]\r\n"); |
| kishino | 2:0ba43344c814 | 130 | for( i = 0; i < gUART_RCVBUF.size; i++ ) |
| kishino | 2:0ba43344c814 | 131 | { |
| kishino | 2:0ba43344c814 | 132 | printf("%02x ", gUART_RCVBUF.buf[i]); |
| kishino | 2:0ba43344c814 | 133 | } |
| kishino | 2:0ba43344c814 | 134 | printf("\r\n"); |
| kishino | 5:ef3befe3edad | 135 | */ |
| kishino | 2:0ba43344c814 | 136 | unsigned char command_id; |
| kishino | 3:9f90024d7fb2 | 137 | // Get payload from received data from UART. |
| kishino | 8:50d2509479cd | 138 | int payload_len = C_SNIC_UartMsg::getResponsePayload( gUART_RCVBUF.size, gUART_RCVBUF.buf |
| kishino | 3:9f90024d7fb2 | 139 | , &command_id, gUART_TEMP_BUF ); |
| kishino | 3:9f90024d7fb2 | 140 | /* |
| kishino | 3:9f90024d7fb2 | 141 | printf("[payload]\r\n"); |
| kishino | 3:9f90024d7fb2 | 142 | for( i = 0; i < payload_len; i++ ) |
| kishino | 3:9f90024d7fb2 | 143 | { |
| kishino | 3:9f90024d7fb2 | 144 | printf("%02x ", gUART_TEMP_BUF[i]); |
| kishino | 3:9f90024d7fb2 | 145 | } |
| kishino | 3:9f90024d7fb2 | 146 | printf("\r\n"); |
| kishino | 5:ef3befe3edad | 147 | */ |
| kishino | 3:9f90024d7fb2 | 148 | // Check scan results indication |
| kishino | 3:9f90024d7fb2 | 149 | if( (command_id == UART_CMD_ID_WIFI) || (gUART_TEMP_BUF[0] == UART_CMD_SID_WIFI_SCAN_RESULT_IND) ) |
| kishino | 3:9f90024d7fb2 | 150 | { |
| kishino | 3:9f90024d7fb2 | 151 | // Scan result indicate |
| kishino | 3:9f90024d7fb2 | 152 | instance_p->mUartCommand.scanResultIndicate( gUART_TEMP_BUF, payload_len ); |
| kishino | 3:9f90024d7fb2 | 153 | } |
| kishino | 2:0ba43344c814 | 154 | |
| kishino | 3:9f90024d7fb2 | 155 | // Checks in the command which is waiting. |
| kishino | 3:9f90024d7fb2 | 156 | if( instance_p->mUartCommand.isWaitingCommand(command_id, gUART_TEMP_BUF) ) |
| kishino | 2:0ba43344c814 | 157 | { |
| kishino | 3:9f90024d7fb2 | 158 | // Get buffer for payload data |
| kishino | 3:9f90024d7fb2 | 159 | unsigned char *payload_buf_p = instance_p->mUartCommand.getResponseBuf(); |
| kishino | 3:9f90024d7fb2 | 160 | if( payload_buf_p != NULL ) |
| kishino | 2:0ba43344c814 | 161 | { |
| kishino | 3:9f90024d7fb2 | 162 | memcpy( payload_buf_p, gUART_TEMP_BUF, payload_len ); |
| kishino | 3:9f90024d7fb2 | 163 | instance_p->mUartCommand.setResponseBuf( NULL ); |
| kishino | 2:0ba43344c814 | 164 | } |
| kishino | 3:9f90024d7fb2 | 165 | // Set status |
| kishino | 3:9f90024d7fb2 | 166 | instance_p->mUartCommand.setCommandStatus( gUART_TEMP_BUF[2] ); |
| kishino | 3:9f90024d7fb2 | 167 | // Set signal for command response wait. |
| kishino | 3:9f90024d7fb2 | 168 | instance_p->mUartCommand.signal(); |
| kishino | 2:0ba43344c814 | 169 | } |
| kishino | 3:9f90024d7fb2 | 170 | |
| kishino | 2:0ba43344c814 | 171 | gUART_RCVBUF.size = 0; |
| kishino | 2:0ba43344c814 | 172 | gUART_RCVBUF.is_receive = false; |
| kishino | 2:0ba43344c814 | 173 | } |
| kishino | 2:0ba43344c814 | 174 | } |
| kishino | 2:0ba43344c814 | 175 | else |
| kishino | 2:0ba43344c814 | 176 | { |
| kishino | 2:0ba43344c814 | 177 | // Check received data is SOM. |
| kishino | 2:0ba43344c814 | 178 | if( recvdata == UART_CMD_SOM ) |
| kishino | 2:0ba43344c814 | 179 | { |
| kishino | 2:0ba43344c814 | 180 | gUART_RCVBUF.size = 0; |
| kishino | 2:0ba43344c814 | 181 | gUART_RCVBUF.buf[ gUART_RCVBUF.size ] = (unsigned char)recvdata; |
| kishino | 2:0ba43344c814 | 182 | gUART_RCVBUF.size++; |
| kishino | 2:0ba43344c814 | 183 | gUART_RCVBUF.is_receive = true; |
| kishino | 2:0ba43344c814 | 184 | } |
| kishino | 2:0ba43344c814 | 185 | } |
| kishino | 7:e88ccbe0225f | 186 | // Thread::yield(); |
| kishino | 2:0ba43344c814 | 187 | } |
| kishino | 2:0ba43344c814 | 188 | Thread::yield(); |
| kishino | 0:61c402886fbb | 189 | } |
| kishino | 0:61c402886fbb | 190 | } |
| kishino | 0:61c402886fbb | 191 |
