Okundu Omeni
/
wifi-https-ble-sm-uart-atcmd-5-13-1
this is using the mbed os version 5-13-1
Diff: source/ATCmdManager.cpp
- Revision:
- 83:9c271a50a70b
- Parent:
- 82:10072c1794d3
- Child:
- 84:7c7add00f4bf
--- a/source/ATCmdManager.cpp Sun Mar 24 17:32:06 2019 +0000 +++ b/source/ATCmdManager.cpp Tue Mar 26 23:33:51 2019 +0000 @@ -58,6 +58,7 @@ _parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID)); _parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD)); _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity)); + _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage)); //AT+UWSC=0,2,<SSID> //AT+UWSC=0,8,<PWD> @@ -146,14 +147,126 @@ } - +bool ATCmdManager::validate(edm_header_t edm_header) +{ + if(edm_header.startByte != EDM_START_BYTE)return false; // unexpected start byte found! + if(edm_header.payloadID != CONNECT_EVENT && + edm_header.payloadID != DISCONNECT_EVENT && + edm_header.payloadID != DATA_EVENT && + edm_header.payloadID != DATA_COMMAND && + edm_header.payloadID != AT_REQUEST && + edm_header.payloadID != AT_CONFIRMATION && + edm_header.payloadID != AT_EVENT + )return false; // unexpected payload ID found! + if(edm_header.payloadLen > MAX_EDM_PAYLOAD_LEN || + edm_header.payloadLen < MIN_EDM_PAYLOAD_LEN + ) return false; // unexpected length received! + return true; +} // OOB processing void ATCmdManager::_process_oob(uint32_t timeout, bool all){ set_timeout(timeout); - // Poll for inbound packets - while (_parser.process_oob() && all) { + static int cnt = 0; + if(dataMode == AT_EXT_DATA_MODE) + { + int n; + if(cnt++ % 10 == 0)printf("In EDM mode\n"); + // Poll for edm packets + do{ + n = _parser.read((char *)&edm_hdr, EDM_HDR_LEN); + if(n == -1) break; // break if it times out + printf("%d bytes read!\n", n); + if(n==5) + printf("Start = %d, payloadID = %d len = %d\n", edm_hdr.startByte, + edm_hdr.payloadID, + edm_hdr.payloadLen); + if(n == EDM_HDR_LEN && validate(edm_hdr)) // if AT command use process oob to decode + { + if(edm_hdr.payloadID == AT_REQUEST) + { + _parser.process_oob(); + break; + } + else + { + int pLen = edm_hdr.payloadLen-1; + rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte + rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected + n = _parser.read((char *)rx_buf_ptr, pLen); + if(n == -1) break; // timeout! + printf("%d bytes read - expected %d!\n", n, pLen); + for(int i =0;i<pLen;i++){ + if(i%8==0) printf("\n[%3d]",i/8); + printf("%2x ", rx_buf_ptr[i]); + } + printf("\n"); + printf("rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]); + if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) { + break; // exit if stop byte now found - possible data corruption! + } + switch(edm_hdr.payloadID) + { + case CONNECT_EVENT: + printf("Connection Event received!\n"); + break; + case DISCONNECT_EVENT: + printf("DISCONNECT_EVENT received!\n"); + break; + case DATA_EVENT: + printf("DATA_EVENT received!\n"); + break; + case DATA_COMMAND: + char s1[100]; + printf("DATA_COMMAND received!\n"); + int n = 0; + int cnt = 0; + int len = 0; + char * strPtr = (char *)&rx_buf_ptr[1]; + char * p2 = strstr(strPtr, "\r\n\r\n"); + char * nxtPtr = strPtr; + printf("\nstrPtr address= %x",strPtr); + printf("\np2 address= %x", p2); + while(1){ + n = sscanf(nxtPtr,"%s[^:]", s1); + len = strlen(s1); + if(n<=0) break; + printf("\ns[%d] = %s len = %d, n = %d",cnt++,s1, len, n ); + if(cnt==20) break; + nxtPtr = strstr(nxtPtr, s1)+len; + printf("\nxtPtr address= %x p2 addr = %x",nxtPtr, p2); + if(nxtPtr > p2) break; + } + + break; + case AT_REQUEST: + printf("AT_REQUEST received!\n"); + break; + case AT_CONFIRMATION: + printf("AT_CONFIRMATION received!\n"); + break; + case AT_EVENT: + printf("AT_EVENT received!\n"); + break; + default: + printf("UNKNOWN MESSAGE received!\n"); + break; + } + } + //_parser.process_oob(); + } + else // incorrect # of bytes received abort!! + { + break; + } + }while (all); // continue to process until timeout } + else + { + // Poll for inbound packets + while (_parser.process_oob() && all) { + } + } set_timeout(); } @@ -234,7 +347,6 @@ void ATCmdManager::_oob_data_mode(){ - int dataMode; _smutex.lock(); printf("\n Received EDM mode command!!\n"); if(_parser.scanf("%d", &dataMode) >0) { @@ -242,15 +354,15 @@ switch(dataMode) { case 0: - printf("\nATCmdParser: Command Mode request received"); + printf("\nATCmdParser: Command Mode request received\n"); dataMode = AT_CMD_DATA_MODE; break; case 1: - printf("\nATCmdParser: Data Mode request received"); + printf("\nATCmdParser: Data Mode request received\n"); dataMode = AT_STD_DATA_MODE; break; case 2: - printf("\nATCmdParser: Extended data Mode request received"); + printf("\nATCmdParser: Extended data Mode request received\n"); dataMode = AT_EXT_DATA_MODE; break; default: @@ -258,7 +370,7 @@ break; } } else { - printf("\nATCmdParser: Retrieving Uart Options failed"); + printf("\nATCmdParser: Retrieving Uart Options failed\n"); } _parser.send("OK\n"); _smutex.unlock(); @@ -405,21 +517,36 @@ wifi_cfg.security = NSAPI_SECURITY_UNKNOWN; return wifi_cfg; } - -int ATCmdManager::readBytes(uint8_t *buf, int maxBytes) +/* read ASCII characters into buffer and null terminate */ +int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes) { int c; int sptr = 0; for(int i=0;i<maxBytes;i++){ c = _parser.getc(); if(c==-1){ - buf[sptr] = '\0'; // null terminate if string + buf[sptr] = NULL; // null terminate if string return i; } if(c != ',' && c!= '"'){ buf[sptr++] = (uint8_t) c; } } + return maxBytes; +} + +int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes) +{ + int c; + int sptr = 0; + for(int i=0;i<maxBytes;i++){ + c = _parser.getc(); + if(c==-1){ + return i; + } + buf[sptr++] = (uint8_t) c; + } + return maxBytes; } void ATCmdManager::_oob_setWiFiSSID() @@ -427,7 +554,7 @@ int n; wifi_config_t wifi_cfg = init_wifi_config(); _smutex.lock(); - n = readBytes((uint8_t *)wifi_cfg.ssid, 32); + n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32); printf("[ATCMD MAN]: number of bytes read = %d\n", n); if(n>0) { @@ -452,7 +579,7 @@ wifi_config_t wifi_cfg = init_wifi_config(); _smutex.lock(); //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass); - n = readBytes((uint8_t *)wifi_cfg.pass, 32); + n = readStringBytes((uint8_t *)wifi_cfg.pass, 32); if(n>0) { printf("ATCMD MAN]: wifi_cfg.pass = %s\n", wifi_cfg.pass); @@ -493,6 +620,33 @@ _smutex.unlock(); } + +void ATCmdManager::_oob_sendHttpMessage() +{ + int n; + http_post_request_t http_post_request; + _smutex.lock(); + /* + n = _parser.scanf(",%d", &wifi_cfg.security); + if(n>0) + { + printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security)); + // package and send on wifi data queue + wifi_data_msg_t data_msg; + data_msg.wifi_cmd = WIFI_CMD_CONFIG; + data_msg.dataLen = sizeof(wifi_config_t); + memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen); + queueWiFiDataRequest(data_msg); + _parser.send("OK\n"); + } else { + printf("\n[ATCMD MAN]: wifi configuration failed \n"); + _parser.send("NAK\n"); + } + */ + _smutex.unlock(); + +} + bool ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){ wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc(); wifiCmd->wifi_cmd = cmd;