Doug Anson / mbedEndpointNetwork_BLE

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Revision:
11:d601b867b297
Parent:
9:bf0cf5828378
Child:
16:fb9c3f2af2df
--- a/bt_network/BleUartRPC/UartRPC.cpp	Wed Feb 18 07:11:45 2015 +0000
+++ b/bt_network/BleUartRPC/UartRPC.cpp	Wed Feb 18 19:40:24 2015 +0000
@@ -36,68 +36,99 @@
      this->m_local_dispatch_available = false;
  }
  
+ uint8_t __tmp_buffer[MAX_ARGUMENT_LENGTH+1];
+ uint8_t base64_buffer[MAX_RESULT_LENGTH+1];
+ 
  // dispatch RPC
- int UartRPC::dispatch(uint8_t fn_id,uint8_t *response,int response_length,const char *format,...)
+ int UartRPC::dispatch(uint8_t fn_id,void *cb,uint8_t *response,int response_length,const char *format,...)
  {
+     // save off the CB if it exists
+     this->m_cb = cb;
+     
      // serialize the variable arguments into a long string...
      va_list args;
-     uint8_t buffer[MAX_ARGUMENT_LENGTH+1];
-     memset(buffer,0,MAX_ARGUMENT_LENGTH+1);
+     memset(__tmp_buffer,0,MAX_ARGUMENT_LENGTH+1);
      va_start(args, format);
-     vsnprintf((char *)buffer,MAX_ARGUMENT_LENGTH,format,args);
+     vsnprintf((char *)__tmp_buffer,MAX_ARGUMENT_LENGTH,format,args);
      va_end(args);
           
      // dispatch now...
-     return this->m_dispatcher.dispatch(fn_id,buffer,strlen((char *)buffer),response,response_length);
+     return this->m_dispatcher.dispatch(fn_id,__tmp_buffer,strlen((char *)__tmp_buffer),response,response_length);
  }
  
  // dispatch locally
  void UartRPC::dispatch() {
      // assemble the argument buffer...
-     uint8_t buffer[MAX_RESULT_LENGTH+1];
-     memset(buffer,0,MAX_RESULT_LENGTH+1);
-     int length = this->m_recv_accumulator.assemble(buffer,MAX_RESULT_LENGTH);
+     memset(__tmp_buffer,0,MAX_ARGUMENT_LENGTH+1);
+     int length = this->m_recv_accumulator.assemble(__tmp_buffer,MAX_ARGUMENT_LENGTH);
      
      // DEBUG
-     DBG("UartRPC: dispatch(local): buffer [%s] length=%d\r\n",buffer,length);
+     DBG("UartRPC: dispatch(local): buffer [%s] length=%d\r\n",__tmp_buffer,length);
      
      // parse the buffer
-     uint8_t base64_buffer[MAX_RESULT_LENGTH+1];
      memset(base64_buffer,0,MAX_RESULT_LENGTH+1);
      int fn_id = 0;
      
      // strip off head,tail and separator, replace with spaces
      int final_length = 0;
      for(int i=0;i<length && final_length == 0;++i) {
-         if (buffer[i] == '[') buffer[i] = ' ';
-         if (buffer[i] == '|') buffer[i] = ' ';
-         if (buffer[i] == ']') { buffer[i] = '\0'; final_length = i; }
+         if (__tmp_buffer[i] == '[') __tmp_buffer[i] = ' ';
+         if (__tmp_buffer[i] == '|') __tmp_buffer[i] = ' ';
+         if (__tmp_buffer[i] == ']') { __tmp_buffer[i] = '\0'; final_length = i; }
      }
      
      // scan over the two values
-     sscanf((char *)buffer,"%d %s",&fn_id,base64_buffer);
+     sscanf((char *)__tmp_buffer,"%d %s",&fn_id,base64_buffer);
      int base64_buffer_length = strlen((char *)base64_buffer);
      
      // DEBUG
-     DBG("UartRPC: dispatch(local): fn_id = 0x%.2x base64_buffer = [%s] length=%d\r\n",fn_id,base64_buffer,base64_buffer_length);
-     
-     // reset our local dispatch
-     DBG("UartRPC: dispatch(local): resetting...\r\n");
-     this->resetLocalDispatch();
-     
-     // decode the arguments and save off the local dispatch...
-     DBG("UartRPC: dispatch(local): base64 decoding...\r\n");
-     Base64 b64;
-     base64_buffer_length = final_length;
-     this->m_recv_raw_data = b64.Decode((char *)base64_buffer,strlen((char *)base64_buffer),(std::size_t *)&base64_buffer_length);
-     this->m_recv_raw_data_length = base64_buffer_length; // modified by Decode()....
-     
+     //DBG("UartRPC: dispatch(local): fn_id = 0x%.2x base64_buffer = [%s] length=%d\r\n",fn_id,base64_buffer,base64_buffer_length);
+          
      // invoke the appropriate function
      DBG("UartRPC: dispatch(local): requested function ID: 0x%.2x...\r\n",fn_id);
      switch(fn_id) {
+         case SOCKET_OPEN_FN: {
+             // socket open has been attempted... call the CB if it exists and process the status
+             DBG("UartRPC: dispatch(local): SOCKET_OPEN_FN length=%d\r\n",base64_buffer_length);
+             if (this->m_cb != NULL) {
+                 // cast the callback
+                 ble_dispatch_callback_fn cb = (ble_dispatch_callback_fn)this->m_cb;
+                 
+                 // parse the packet received for the status
+                 bool open_ok = false;
+                 DBG("UartRPC: dispatch(local): SOCKET_OPEN_FN socket  open status: [%s]\r\n",base64_buffer);
+                 int int_open_ok = 0;
+                 sscanf((char *)base64_buffer,"%d",&int_open_ok);
+                 if (int_open_ok == 1) open_ok = true;
+                 
+                 // invoke the callback with the status
+                 DBG("UartRPC: dispatch(local): SOCKET_OPEN_FN: invoking socket open callback (status: %d)...\r\n",int_open_ok);
+                 cb(open_ok);
+                 DBG("UartRPC: dispatch(local): SOCKET_OPEN_FN: callback for socket open (status: %d) completed...\r\n",int_open_ok);
+             }
+             else {
+                 // no callback given... so we are stuck...
+                 DBG("UartRPC: dispatch(local): SOCKET_OPEN_FN : ERROR no connect() callback function registered!\r\n");
+             }
+             
+             // done with the callback
+             this->m_cb = NULL;
+             break;
+         }
          case RECV_DATA_FN: {
+             // reset our local dispatch
+             DBG("UartRPC: dispatch(local): resetting...\r\n");
+             this->resetLocalDispatch();
+
+             // decode the arguments and save off the local dispatch...
+             DBG("UartRPC: dispatch(local): base64 decoding...\r\n");
+             Base64 b64;
+             base64_buffer_length = final_length;
+             this->m_recv_raw_data = b64.Decode((char *)base64_buffer,strlen((char *)base64_buffer),(std::size_t *)&base64_buffer_length);
+             this->m_recv_raw_data_length = base64_buffer_length; // modified by Decode()....
+             
              // only RECV_DATA_FN is local... make it ready for retrieval and flag it as ready...
-             DBG("UartRPC: dispatch(local): RECV_DATA_FN length=%d buffer=[%s]\r\n",this->m_recv_raw_data_length,this->m_recv_raw_data);
+             DBG("UartRPC: dispatch(local): RECV_DATA_FN length=%d\r\n",this->m_recv_raw_data_length);
                      
              // local dispatch is now ready...
              this->m_local_dispatch_available = true;