this is using the mbed os version 5-13-1

Dependencies:   mbed-http

Files at this revision

API Documentation at this revision

Comitter:
ocomeni
Date:
Mon May 27 17:00:43 2019 +0000
Branch:
PassingRegression
Parent:
119:8d939a902333
Child:
121:ac4f59839e4f
Commit message:
- progressed implementation of AT/BLE commands; - started basic testing; - sending short strings from BLE works ok

Changed in this revision

source/ATCmdManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/ATCmdManager.h Show annotated file Show diff for this revision Revisions of this file
source/BleManager.cpp Show annotated file Show diff for this revision Revisions of this file
source/BleManager.h Show annotated file Show diff for this revision Revisions of this file
source/common_types.h Show annotated file Show diff for this revision Revisions of this file
source/main-https.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/source/ATCmdManager.cpp	Mon May 27 12:34:58 2019 +0000
+++ b/source/ATCmdManager.cpp	Mon May 27 17:00:43 2019 +0000
@@ -119,6 +119,7 @@
 void ATCmdManager::processResponses(){
         dequeueATresponse();
         dequeueWiFidataResponse();
+        dequeueBleDataResponse();
         updateWiFiMgrStatus();
         char * respStr;
         switch(at_resp){
@@ -289,6 +290,14 @@
                 at_resp = AT_RESP_NONE;
                 break;
             }
+            case BLE_DISCONNECT_EVENT:  
+            {
+                // AT Event state 
+                dbg_printf(LOG, "\n [ATCMD MAN] BLE_DISCONNECT_EVENT RECEIVED!!\r\n");
+                sendBleDisconnectEvent();
+                at_resp = AT_RESP_NONE;
+                break;
+            }
             default:
                  //UNKNOWN response state
                 dbg_printf(LOG, "\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\r\n");
@@ -298,7 +307,7 @@
         }
 }
 
-
+//
 void ATCmdManager::updateWiFiMgrStatus()
 {
     static int wifiBusyMonitor = 0;
@@ -1226,6 +1235,34 @@
     return true;
 }
 
+bool  ATCmdManager::queueBleDataRequest(at_ble_msg_t data_req){
+    at_ble_msg_t *bleData = _aT2BleDatamPool->alloc();
+    if(bleData == NULL)
+    {
+#ifdef SEND_DEBUG_MESSAGES
+        sendAtConfirmation("\r\nBLE QUEUE MEMORY FULL\r\n");
+#endif
+        return false;
+    }
+    bleData->ble_cmd        = data_req.ble_cmd;
+    bleData->dataLen        = data_req.dataLen;
+    memcpy(bleData->buffer, data_req.buffer, data_req.dataLen);
+    _aT2BleDataQueue->put(bleData);
+    dbg_printf(LOG, "[ATCMD MAN] queued BLE data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.ble_cmd);
+    return true;
+}
+
+bool  ATCmdManager::dequeueBleDataResponse(){
+    if(at_resp != AT_RESP_NONE) return false; // busy
+    osEvent evt = _ble2ATDataQueue->get(0);
+    if(evt.status == osEventMessage){
+        ble_resp_data = (ble_at_msg_t*)evt.value.p;
+        setNextResponse(ble_resp_data->at_resp);
+        dbg_printf(LOG, "[ATCMD MAN] dequeued data size = %d : at_resp = %d\n", resp_data->dataLen, resp_data->at_resp);
+    }
+    return true;
+}
+
 
 void ATCmdManager::sendAtConfirmation(const char *buf)
 {
@@ -1349,6 +1386,15 @@
     ble_resp_data = NULL;
 }
 
+void ATCmdManager::sendBleDisconnectEvent()
+{
+    _smutex.lock();
+    outputEDMdata(NULL, 0, DISCONNECT_MSG_ID, EVENT_MSG_TYPE, BLE_CHANNEL);
+    _smutex.unlock();
+    _ble2ATDatamPool->free(ble_resp_data);
+    ble_resp_data = NULL;
+}
+
 
 void ATCmdManager::sendConnectEvent(const uint8_t *buf, int len)
 {
@@ -1389,7 +1435,10 @@
     if(channel_id != NO_CHANNEL)
         _parser.putc(channel_id);
     // send the data
-    _parser.write((const char *)buf, pLen);
+    if(pLen > 0)
+    {
+        _parser.write((const char *)buf, pLen);
+    }
     // send EDM Message stop byte
     _parser.putc(EDM_STOP_BYTE);
     _smutex.unlock();
--- a/source/ATCmdManager.h	Mon May 27 12:34:58 2019 +0000
+++ b/source/ATCmdManager.h	Mon May 27 17:00:43 2019 +0000
@@ -157,6 +157,9 @@
     void        sendBleDataEvent(const char *buf, int len);
     void        sendBleAtEvent(const char *buf, int len);
     void        sendBleConnectEvent(const char *buf, int len);
+    void        sendBleDisconnectEvent();
+    bool        dequeueBleDataResponse();
+    bool        queueBleDataRequest(at_ble_msg_t data_req);
     
     /**
     * Allows timeout to be changed between commands
--- a/source/BleManager.cpp	Mon May 27 12:34:58 2019 +0000
+++ b/source/BleManager.cpp	Mon May 27 17:00:43 2019 +0000
@@ -317,14 +317,12 @@
 
 
 /** Send data aynchronously using BLE                                       */
-void SMDevice::sendBLEUartData(char * str)
+void SMDevice::sendBLEUartData(const uint8_t * buf, int len)
 {
-    //Gap::GapState_t gapState = _ble.gap().getState();
-    //Gap::GapState_t gapState = _ble.getGapState();
     //gapState.connected
     if(isConnected){    
-        uart->writeString(str);
-        uart->writeString("\n");    //flushes uart output buffer and sends data
+        uart->write(buf, len);
+        //uart->writeString("\n");    //flushes uart output buffer and sends data
     }
     else
     {
@@ -356,10 +354,13 @@
         }
 
         buffer[index++] = 0;
-
+        at_data_resp = new ble_at_msg_t;
+        at_data_resp->dataLen = params->len;
+        memcpy(at_data_resp->buffer, params->data, params->len);
         dbg_printf(LOG, "Data : %s ",buffer);
         dbg_printf(LOG, "\r\n");
         /* start echo in 50 ms */
+        _event_queue.call(this, &SMDevice::setNextCommand, BLE_CMD_SEND_RX_DATA_2AT);
         _event_queue.call_in(50, this, &SMDevice::EchoBleUartReceived);
         //_event_queue.call(EchoBleUartReceived);
 
@@ -507,6 +508,7 @@
     }
     dbg_printf(LOG, "SM::setLinkSecurity setup\r\n");
     isConnected = true;
+    _event_queue.call(this, &SMDevicePeripheral::setNextCommand, BLE_CMD_CONNECT);
 }
 
 void SMDevicePeripheral::stopAdvertising()
@@ -549,18 +551,32 @@
         bleCmd = BLE_CMD_NONE;
         break;
     case BLE_CMD_CONNECT:
-        _aT2BleDatamPool->free(data_msg);
+        at_data_resp = new ble_at_msg_t;
+        at_data_resp->dataLen = 10; // 10 bytes total
+        int idx = 0;
+        // connect type BLE = 0x01
+        at_data_resp->buffer[idx++] = 0x01;
+        // Serial Port Service BLE profile = 0x0E (14)
+        at_data_resp->buffer[idx++] = 0x0E;
+        // copy peer device address
+        memcpy(&at_data_resp->buffer[idx], _peer_address, sizeof(_peer_address));
+        idx+=sizeof(_peer_address);
+        // frame size 0x0166
+        at_data_resp->buffer[idx++] = 0x01;
+        at_data_resp->buffer[idx++] = 0x66;
         bleCmd = BLE_CMD_NONE;
+        sendATresponseBytes(BLE_CONNECT_EVENT);
         break;
     case BLE_CMD_DISCONNECT:
-        _aT2BleDatamPool->free(data_msg);
+        sendATresponseBytes(BLE_CONNECT_EVENT);
         bleCmd = BLE_CMD_NONE;
         break;
     case BLE_CMD_SEND_RX_DATA_2AT:
-        _aT2BleDatamPool->free(data_msg);
+        sendATresponseBytes(AT_BLE_RESPONSE);
         bleCmd = BLE_CMD_NONE;
         break;
     case BLE_CMD_SEND_AT_DATA_2BLE:
+        sendBLEUartData(data_msg->buffer, data_msg->dataLen);
         _aT2BleDatamPool->free(data_msg);
         bleCmd = BLE_CMD_NONE;
         break;
@@ -597,14 +613,14 @@
     return true;
 }
 
-void  SMDevicePeripheral::sendATresponseBytes(at_cmd_resp_t at_cmd, const uint8_t * buf, int len)
+void  SMDevicePeripheral::sendATresponseBytes(at_cmd_resp_t at_cmd)
 {
-    at_data_resp = new ble_at_msg_t;
+    //at_data_resp = new ble_at_msg_t;
     // package and send on BLE data queue    
     // set string length 
-    at_data_resp->dataLen = len;
+    //at_data_resp->dataLen = len;
     // copy data
-    memcpy(at_data_resp->buffer, buf, len);
+    //memcpy(at_data_resp->buffer, buf, len);
     // copy response type
     at_data_resp->at_resp = at_cmd;
     bool queueResult = true;
--- a/source/BleManager.h	Mon May 27 12:34:58 2019 +0000
+++ b/source/BleManager.h	Mon May 27 17:00:43 2019 +0000
@@ -90,7 +90,7 @@
     );
     
     void shutDown();
-    void sendBLEUartData(char * str);
+    void sendBLEUartData(const uint8_t * buf, int len);
 
 protected:
     // connection status
@@ -128,7 +128,7 @@
     // application virtual methods called by peripheral
     virtual bool queueBleDataResponse(ble_at_msg_t at_resp) = 0;
     virtual bool dequeueATdataResponse() = 0;
-    virtual void sendATresponseBytes(at_cmd_resp_t at_cmd, const uint8_t * buf, int len) = 0;
+    virtual void sendATresponseBytes(at_cmd_resp_t at_cmd) = 0;
     virtual bool setNextCommand(ble_cmd_t cmd) = 0;
     virtual void processQueues() = 0;
     
@@ -186,7 +186,7 @@
     void startAdvertising();
     virtual bool queueBleDataResponse(ble_at_msg_t at_resp);
     virtual bool dequeueATdataResponse();
-    virtual void sendATresponseBytes(at_cmd_resp_t at_cmd, const uint8_t * buf, int len);
+    virtual void sendATresponseBytes(at_cmd_resp_t at_cmd);
     virtual bool setNextCommand(ble_cmd_t cmd);
     virtual void processQueues();
     
--- a/source/common_types.h	Mon May 27 12:34:58 2019 +0000
+++ b/source/common_types.h	Mon May 27 17:00:43 2019 +0000
@@ -116,7 +116,8 @@
   AT_BLE_EVENT = 21,
   AT_BLE_RESPONSE = 22,
   AT_SOCKET_KEEP_ALIVE_OK = 23,
-  AT_SOCKET_KEEP_ALIVE_FAILED = 24
+  AT_SOCKET_KEEP_ALIVE_FAILED = 24,
+  BLE_DISCONNECT_EVENT = 25
 }at_cmd_resp_t;
 
 typedef enum edm_msg_id
--- a/source/main-https.cpp	Mon May 27 12:34:58 2019 +0000
+++ b/source/main-https.cpp	Mon May 27 17:00:43 2019 +0000
@@ -168,11 +168,13 @@
     dbg_printf(LOG, "%s", printStr);
     dbg_printf(LOG, "Waiting for %d seconds... [press key to abort]\n", numSecs);
     char fmtstr[20];
+    int len;
     for(int i=0;i<numSecs;i++){
         dbg_printf(LOG, "%d", i);
         dbg_printf(LOG, "\n");
         sprintf(fmtstr, "BLE: loop # %d\n", i);
-        peripheral->sendBLEUartData(fmtstr);
+        len = strlen(fmtstr)+1;
+        peripheral->sendBLEUartData((const uint8_t *)fmtstr, len);
         wait(0.5);
         //eventQueue_atcmd.dispatch(500);        // Dispatch time - 500msec
         if(device->readable()){