libsangoma  1
Macros | Functions | Variables
sample.cpp File Reference

WANPIPE(tm) API C++ Sample Code. More...

#include "sangoma_port.h"
#include "sangoma_port_configurator.h"
#include "sangoma_interface.h"
#include <conio.h>
#include <iostream>
#include <string>
Include dependency graph for sample.cpp:

Go to the source code of this file.

Macros

#define MAX_PATH   100
 
#define DBG_MAIN   if(1)printf
 
#define ERR_MAIN   printf("%s():line:%d:Error:", __FUNCTION__, __LINE__);printf
 
#define INFO_MAIN   if(1)printf
 
#define MAIN_FUNC()   if(1)printf("%s():line:%d\n", __FUNCTION__, __LINE__)
 

Functions

sangoma_interfaceinit (int wanpipe_number, int interface_number)
 Create a sangoma_interface class and setup callback functions. More...
 
void cleanup (sangoma_interface *sang_if)
 Free Sangoma Interface Object. More...
 
int start (sangoma_interface *sang_if)
 Run the main sangoma interface hanlder code. More...
 
void stop (sangoma_interface *sang_if)
 Stop the Sangoma Interface Object. More...
 
void PrintRxData (wp_api_hdr_t *hdr, void *pdata)
 Debug function used to print Rx Data. More...
 
int tx_file (sangoma_interface *sang_if)
 Transmit a file on a sangoma interface / device. More...
 
int my_getch ()
 
int __cdecl main (int argc, char *argv[])
 

Variables

wp_program_settings_t program_settings
 
callback_functions_t callback_functions
 
CRITICAL_SECTION PrintCriticalSection
 
CRITICAL_SECTION TdmEventCriticalSection
 

Detailed Description

WANPIPE(tm) API C++ Sample Code.

Author(s): David Rokhvarg Nenad Corbic

Copyright: (c) 2005-2009 Sangoma Technologies

THIS SOFTWARE IS PROVIDED BY Sangoma Technologies ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Sangoma Technologies BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT

(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

Definition in file sample.cpp.

Function Documentation

sangoma_interface * init ( int  wanpipe_number,
int  interface_number 
)

Create a sangoma_interface class and setup callback functions.

Parameters
wanpipe_numberwanpipe device number obtained from the hardware probe info, span
interface_numberwanpipe interface number, corresponds to a channel in a span
Returns
sangoma_interface object pointer - ok or NULL if error.

Definition at line 111 of file sample.cpp.

112 {
113  sangoma_interface *sang_if = NULL;
114  DBG_MAIN("%s()\n", __FUNCTION__);
115 
116  if(program_settings.use_ctrl_dev == 1){
117  sang_if = new sangoma_api_ctrl_dev();
118  }else if(program_settings.use_logger_dev == 1){
119  sang_if = new sangoma_api_logger_dev();
120  }else{
121  sang_if = new sangoma_interface(wanpipe_number, interface_number);
122  }
123 
124  if(sang_if->init(&callback_functions)){
125  delete sang_if;
126  return NULL;
127  }
128 
129  DBG_MAIN("init(): OK\n");
130  return sang_if;
131 }
Sangoma Interface Class that deals with span/chan IO (read/write/events)
void cleanup ( sangoma_interface sang_if)

Free Sangoma Interface Object.

Parameters
sang_ifSangoma interface object pointer
Returns
void

Definition at line 139 of file sample.cpp.

140 {
141  DBG_MAIN("cleanup()\n");
142  if(sang_if){
143  delete sang_if;
144  }
145 }
int start ( sangoma_interface sang_if)

Run the main sangoma interface hanlder code.

Parameters
sang_ifSangoma interface object pointer
Returns
0 - ok Non Zero - Error

Definition at line 153 of file sample.cpp.

Referenced by sangoma_hwec_audio_buffer_playout().

154 {
155  DBG_MAIN("start()\n");
156  return sang_if->run();
157 }

Here is the caller graph for this function:

void stop ( sangoma_interface sang_if)

Stop the Sangoma Interface Object.

Parameters
sang_ifSangoma interface object pointer
Returns
void

Definition at line 165 of file sample.cpp.

166 {
167  DBG_MAIN("stop()\n");
168  sang_if->stop();
169 }
void PrintRxData ( wp_api_hdr_t hdr,
void *  data 
)

Debug function used to print Rx Data.

Parameters
pRxAPI data element strcutre containt header + data
Returns
void

Definition at line 177 of file sample.cpp.

References wp_api_hdr::data_length, wp_api_hdr::time_stamp_sec, and wp_api_hdr::time_stamp_usec.

178 {
179  unsigned short datlen;
180  unsigned char * data;
181  static unsigned int rx_counter = 0;
182 
183  //NOTE: if running in BitStream mode, there will be TOO MUCH to print
184  datlen = hdr->data_length;
185  data = (unsigned char*)pdata;
186 
187  rx_counter++;
188  if(program_settings.silent){
189  if((rx_counter % 1000) == 0){
190  INFO_MAIN("Rx counter: %d, Rx datlen: %d\n", rx_counter, datlen);
191 #if 1
192  INFO_MAIN("Timestamp: Seconds: %d, Microseconds: %d\n",
193  hdr->time_stamp_sec, hdr->time_stamp_usec);
194 #endif
195  }
196  return;
197  }else{
198  INFO_MAIN("Rx counter: %d, Rx datlen: %d. Data:\n", rx_counter, datlen);
199  }
200 
201 #if 1
202  for(int ln = 0; ln < datlen; ln++){
203  if((ln % 20 == 0)){
204  if(ln){
205  INFO_MAIN("\n");
206  }
207  INFO_MAIN("%04d ", ln/20);
208  }
209  INFO_MAIN("%02X ", data[ln]);
210  }
211  INFO_MAIN("\n");
212 #endif
213 }
u_int32_t time_stamp_sec
u_int32_t time_stamp_usec
u_int16_t data_length
int tx_file ( sangoma_interface sang_if)

Transmit a file on a sangoma interface / device.

Parameters
sang_if_ptrsangoma interface pointer
Returns
0 - ok non-zero - Error

Definition at line 385 of file sample.cpp.

References wp_api_hdr::data_length, wp_api_hdr::operation_status, SANG_STATUS_SUCCESS, SANG_STATUS_TX_TIMEOUT, and sangoma_interface::transmit().

386 {
387  FILE *pFile;
388  unsigned int tx_counter=0, bytes_read_from_file, total_bytes_read_from_file=0;
389  wp_api_hdr_t hdr;
390  unsigned char local_tx_data[SAMPLE_CPP_MAX_DATA_LEN];
391 
392  pFile = fopen( program_settings.szTxFileName, "rb" );
393  if( pFile == NULL){
394  ERR_MAIN( "Can't open file: [%s]\n", program_settings.szTxFileName );
395  return 1;
396  }
397 
398  do
399  {
400  //read tx data from the file. if 'bytes_read_from_file != txlength', end of file is reached
401  bytes_read_from_file = fread( local_tx_data, 1, program_settings.txlength /* MTU size */, pFile );
402  total_bytes_read_from_file += bytes_read_from_file;
403 
404  hdr.data_length = program_settings.txlength;//ALWAYS transmit MTU size over the BitStream/Voice interface
406 
407  if(SANG_STATUS_SUCCESS != sang_if->transmit(&hdr, local_tx_data)){
408  //error
409  break;
410  }
411 
412  tx_counter++;
413 
414  //DBG_MAIN("tx_counter: %u\r",tx_counter);
415 
416  }while(bytes_read_from_file == program_settings.txlength);
417 
418  INFO_MAIN("%s: Finished transmitting file \"%s\" (tx_counter: %u, total_bytes_read_from_file: %d)\n",
419  sang_if->device_name, program_settings.szTxFileName, tx_counter, total_bytes_read_from_file);
420 
421  fclose( pFile );
422  return 0;
423 }
u_int8_t operation_status
Wanpipe API Header Structure.
int transmit(wp_api_hdr_t *hdr, void *data)
u_int16_t data_length

Here is the call graph for this function: