private void receive(SerialDataReceivedEventArgs e) { log(string.Format("Received: {0}", m_serialPort.BytesToRead)); int availableBufferSpace = m_rx.Length - m_rxOffset; int bytesToRead = Math.Min(availableBufferSpace, m_serialPort.BytesToRead); int read = m_stream.Read(m_rx, m_rxOffset, bytesToRead); m_rxOffset += read; while (true) { log(string.Format("m_rxOffset: {0}", m_rxOffset)); if (m_rxOffset < SIZE_HEADER) { // wait for more data log(string.Format("Waiting for header: {0}", m_rxOffset)); return; } // read payload int length = ((m_rx[0] & 0x7F) << 8) | m_rx[1]; log(string.Format("length: {0}", length)); if (m_rxOffset < SIZE_HEADER + length) { // wait for more data log(string.Format("Waiting for more data, expected {1}, got {0}", SIZE_HEADER + length, m_rxOffset)); return; } // full msg in m_rx, evt or rsp ? byte[] evtRspBuffer = new byte[SIZE_HEADER + length]; Array.Copy(m_rx, evtRspBuffer, evtRspBuffer.Length); // remove first event int excessBytes = m_rxOffset - evtRspBuffer.Length; for (int i = 0; i < excessBytes; i++) { m_rx[i] = m_rx[evtRspBuffer.Length + i]; } m_rxOffset -= evtRspBuffer.Length; log(string.Format("m_rxOffset to {0}", m_rxOffset)); BgApiEventResponse evtRsp = parseEventResponse(new BgApiEventResponse() { Data = evtRspBuffer }); if (evtRsp.IsEvent) { HandleEvent((BgApiEvent)evtRsp); } else { m_response = (BgApiResponse)evtRsp; m_waitHandleResponse.Set(); } } }
/// <summary> /// Sends a command and receives a response from a ble device. /// </summary> /// <param name="command">The BgApiCommand to send.</param> /// <param name="no_return">True, iff the command has no response (ex. the reset command does not return a response).</param> /// <returns>The response from the bgapi device.</returns> private BgApiResponse Send(BgApiCommand command, bool no_return) { try { // ensure an open connection before attempting to send Open(); // wait for response m_waitHandleResponse.Reset(); m_response = null; #if NET35 log("--> " + string.Join(" ", command.Data.Select(x => x.ToString("X2")).ToArray())); #else log("--> " + string.Join(" ", command.Data.Select(x => x.ToString("X2")))); #endif // write command m_stream.Write(command.Data, 0, command.Data.Length); m_stream.Flush(); if (no_return) { // do not expect a response for this command return(null); } // what is the maximum wait time for a response if (!m_waitHandleResponse.WaitOne(5000)) { throw new BgApiException("Response timeout"); } #if NET35 log("<-- " + string.Join(" ", m_response.Data.Select(x => x.ToString("X2")).ToArray())); #else log("<-- " + string.Join(" ", m_response.Data.Select(x => x.ToString("X2")))); #endif return(m_response); } catch (Exception e) { log(e.Message); // do not assume anything about the state of the ble device // after an exception Close(); throw; } }
public bool HangupAllCalls() { bool ret = false; BgApiResponse bg_api_response = (BgApiResponse)BgAPICommand("hupall NORMAL_CLEARING"); string job_uuid = bg_api_response.GetJobUUID(); if (string.IsNullOrEmpty(job_uuid)) { //log.error("Hangup All Calls Failed -- JobUUID not received") ret = false; } else { ret = true; } //log.info("Executed Hangup for all calls") return(ret); }
/// <summary> /// Sends a command and receives a response from a ble device. /// </summary> /// <param name="command">The BgApiCommand to send.</param> /// <param name="no_return">True, iff the command has no response (ex. the reset command does not return a response).</param> /// <returns>The response from the bgapi device.</returns> private BgApiResponse Send(BgApiCommand command, bool no_return) { try { // ensure an open connection before attempting to send Open(); // wait for response m_waitHandleResponse.Reset(); m_response = null; log("--> " + string.Join(" ", command.Data.Select(x => x.ToString("X2")))); // write command m_stream.Write(command.Data, 0, command.Data.Length); m_stream.Flush(); if (no_return) { // do not expect a response for this command return null; } // what is the maximum wait time for a response if (!m_waitHandleResponse.WaitOne(5000)) { throw new BgApiException("Response timeout"); } log("<-- " + string.Join(" ", m_response.Data.Select(x => x.ToString("X2")))); return m_response; } catch (Exception e) { log(e.Message); // do not assume anything about the state of the ble device // after an exception Close(); throw e; } }
private void receive(SerialDataReceivedEventArgs e) { log(string.Format("Received: {0}", m_serialPort.BytesToRead)); int read = m_stream.Read(m_rx, m_rxOffset, m_serialPort.BytesToRead); m_rxOffset += read; while (true) { log(string.Format("m_rxOffset: {0}", m_rxOffset)); if (m_rxOffset < SIZE_HEADER) { // wait for more data log(string.Format("Waiting for header: {0}", m_rxOffset)); return; } // read payload int length = ((m_rx[0] & 0x7F) << 8) | m_rx[1]; log(string.Format("length: {0}", length)); if (m_rxOffset < SIZE_HEADER + length) { // wait for more data log(string.Format("Waiting for more data, expected {1}, got {0}", SIZE_HEADER + length, m_rxOffset)); return; } // full msg in m_rx, evt or rsp ? byte[] evtRspBuffer = new byte[SIZE_HEADER + length]; Array.Copy(m_rx, evtRspBuffer, evtRspBuffer.Length); // remove first event int excessBytes = m_rxOffset - evtRspBuffer.Length; for (int i = 0; i < excessBytes; i++) { m_rx[i] = m_rx[evtRspBuffer.Length + i]; } m_rxOffset -= evtRspBuffer.Length; log(string.Format("m_rxOffset to {0}", m_rxOffset)); BgApiEventResponse evtRsp = parseEventResponse(new BgApiEventResponse() { Data = evtRspBuffer }); if(evtRsp.IsEvent) { HandleEvent((BgApiEvent)evtRsp); } else { m_response = (BgApiResponse)evtRsp; m_waitHandleResponse.Set(); } } }
public bool Originate(string CallSid) { Request CallReq; bool ret = false; string outbound_str = string.Empty; DialString gw = new DialString(null, null, null, null, null, null); try { CallReq = CallRequest[CallSid]; } catch (Exception ex) { return(false); } try { gw = CallReq.gateways.Dequeue(); } catch (Exception ex) { CallRequest.Remove(CallSid); return(false); } StringBuilder _options = new StringBuilder(); //Set agbara app flag _options.Append("agbara_app=true,"); if (!string.IsNullOrEmpty(gw.timeout)) { _options.Append(string.Format("originate_timeout={0},", gw.timeout)); } _options.Append("ignore_early_media=true,"); if (!string.IsNullOrEmpty(gw.codecs)) { _options.Append(string.Format("absolute_codec_string={0}", gw.codecs)); } if (!string.IsNullOrEmpty(FSOutboundAddress)) { outbound_str = string.Format("'socket:{0} async full' inline", FSOutboundAddress); } else { outbound_str = "&park()"; } string dial_str = string.Format("originate {0}{1},{2}{3}{4}/{5} {6}", "{", gw.extra_dial_string, _options, "}", gw.gw, gw.to, outbound_str); BgApiResponse ApiResponse = (BgApiResponse)Task <Event> .Factory.StartNew(() => BgAPICommand(dial_str)).Result; if (ApiResponse.IsSuccess()) { string job_uuid = ApiResponse.GetJobUUID(); if (!string.IsNullOrEmpty(job_uuid)) { BackgroundJobs[job_uuid] = CallSid; ret = true; } else { ret = false; } } return(ret); }