libsangoma  1
Classes | Macros | Typedefs | Functions | Variables
sample.c File Reference

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

#include "libsangoma.h"
#include "lib_api.h"
Include dependency graph for sample.c:

Go to the source code of this file.

Classes

struct  sangoma_chan
 

Macros

#define TEST_NUMBER_OF_OBJECTS   1
 Number of wait objects to define in object array. More...
 

Typedefs

typedef struct sangoma_chan sangoma_chan_t
 

Functions

int __cdecl main (int argc, char *argv[])
 Main function that starts the sample code. More...
 
int open_sangoma_device (void)
 Open a single span chan device. More...
 
void handle_span_chan (int open_device_counter)
 Write data buffer into a file. More...
 
int handle_tdm_event (uint32_t dev_index)
 Read Event buffer from the device. More...
 
int handle_data (uint32_t dev_index, int flags_out)
 Read data buffer from the device and transmit it back down. More...
 
int read_data (uint32_t dev_index, wp_api_hdr_t *rx_hdr, void *rx_buffer, int rx_buffer_length)
 
int write_data (uint32_t dev_index, wp_api_hdr_t *tx_hdr, void *tx_buffer, int tx_len)
 Transmit a data buffer to a device. More...
 
int dtmf_event (sng_fd_t fd, unsigned char digit, unsigned char type, unsigned char port)
 
int rbs_event (sng_fd_t fd, unsigned char rbs_bits)
 
int rxhook_event (sng_fd_t fd, unsigned char hook_state)
 
int rxring_event (sng_fd_t fd, unsigned char ring_state)
 
int ringtrip_event (sng_fd_t fd, unsigned char ring_state)
 
int write_data_to_file (unsigned char *data, unsigned int data_length)
 Write data buffer into a file. More...
 
int sangoma_print_stats (sng_fd_t sangoma_dev)
 
void cleanup (void)
 Protperly shutdown single device. More...
 
int handle_fe_rw (void)
 
void TerminateHandler (int)
 Signal handler for graceful shutdown. More...
 
void print_rxdata (unsigned char *data, int datalen, wp_api_hdr_t *hdr)
 

Variables

sangoma_chan_t sangoma_channels [TEST_NUMBER_OF_OBJECTS]
 
unsigned char rx_rbs_bits = WAN_RBS_SIG_A
 
FILE * pRxFile
 
int application_termination_flag = 0
 

Detailed Description

WANPIPE(tm) API C Sample Code.

Authors: David Rokhvarg david.nosp@m.r@sa.nosp@m.ngoma.nosp@m..com Nenad Corbic ncorb.nosp@m.ic@s.nosp@m.angom.nosp@m.a.co.nosp@m.m

Copyright (c) 2007 - 08, Sangoma Technologies All rights reserved.

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.c.

Macro Definition Documentation

#define TEST_NUMBER_OF_OBJECTS   1

Number of wait objects to define in object array.

Objects are used to wait on file descripotrs. Usually there is one wait object per file descriptor.

In this example there is a single file descriptor and a single wait object.

Definition at line 50 of file sample.c.

Referenced by cleanup(), handle_span_chan(), and main().

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Main function that starts the sample code.

Main function start of the applicatoin.

Parameters
argcnumber of arguments
argvargument list
argcnumber of arguments
argvargument list
Returns
0 - ok non-zero - Error

Get user Input Set program settings based on user input Create SangomaInterface Class based on user span/chan input. Bind callback functions read/event to SangomaInteface class. Execute the SangomaInterface handling function -> start() The start function will read/write/event data. In Main thread prompt the user for commands.

Definition at line 1018 of file sample.c.

References _getch, cleanup(), handle_span_chan(), open_sangoma_device(), TerminateHandler(), TEST_NUMBER_OF_OBJECTS, and TRUE.

1019 {
1020  int proceed, i;
1021 
1022 
1023  proceed=init_args(argc,argv);
1024  if (proceed != WAN_TRUE){
1025  usage(argv[0]);
1026  return -1;
1027  }
1028 
1029  if (hw_pci_rescan) {
1030  /* Function used to rescan pci/usb bus.
1031  added here as use example. Uncomment to use.
1032  This function should only be used when all ports
1033  are down. */
1034  return sangoma_hardware_rescan();
1035  }
1036 
1037  /* register Ctrl+C handler - we want a clean termination */
1038 #if defined(__WINDOWS__)
1039  if (!SetConsoleCtrlHandler(TerminateHandler, TRUE)) {
1040  printf("ERROR : Unable to register terminate handler ( %d ).\nProcess terminated.\n",
1041  GetLastError());
1042  return -1;
1043  }
1044 #else
1045  signal(SIGHUP,TerminateHandler);
1046  signal(SIGTERM,TerminateHandler);
1047 #endif
1048 
1049  for(i = 0; i < TEST_NUMBER_OF_OBJECTS; i++){
1050  sangoma_wait_objects[i] = NULL;
1051  }
1052 
1053  poll_events_bitmap = 0;
1054  if(read_enable == 1){
1055  poll_events_bitmap |= POLLIN;
1056  }
1057 
1058  if(write_enable == 1 && rx2tx == 1){
1059  /* These two options are mutually exclusive because 'rx2tx' option
1060  * indicates "use Reciever as the timing source for Transmitter". */
1061  write_enable = 0;
1062  }
1063 
1064  if(write_enable == 1){
1065  poll_events_bitmap |= POLLOUT;
1066  }
1067 
1068  /* Front End connect/disconnect, and other events, such as DTMF... */
1069  poll_events_bitmap |= (POLLHUP | POLLPRI);
1070 #if defined(__WINDOWS__)
1071  printf("Enabling Poll Events:\n");
1072  print_poll_event_bitmap(poll_events_bitmap);
1073 #endif
1074  printf("Connecting to Port/Span: %d, Interface/Chan: %d\n",
1075  wanpipe_port_no, wanpipe_if_no);
1076 
1077 
1078  if(open_sangoma_device()){
1079  return -1;
1080  }
1081 
1082  printf("********************************\n");
1083  printf("files_used: 0x%x\n", files_used);
1084  printf("********************************\n");
1085  if(files_used & RX_FILE_USED){
1086  pRxFile = fopen( (const char*)&rx_file[0], "wb" );
1087  if(pRxFile == NULL){
1088  printf("Can't open Rx file: [%s]!!\n", rx_file);
1089  }else{
1090  printf("Open Rx file: %s. OK.\n", rx_file);
1091  }
1092  }
1093 
1094  if (fe_read_cmd || fe_write_cmd) {
1095  handle_fe_rw();
1096  goto done;
1097  }
1098 
1099  handle_span_chan(1 /* handle a single device */);
1100 
1101 done:
1102  /* returned from main loop, do the cleanup before exiting: */
1103  cleanup();
1104 
1105  printf("\nSample application exiting.(press any key)\n");
1106  _getch();
1107  return 0;
1108 }
void cleanup(void)
Protperly shutdown single device.
Definition: sample.c:697
#define _getch
get character, Ported from Windows
Definition: libsangoma.h:252
#define TEST_NUMBER_OF_OBJECTS
Number of wait objects to define in object array.
Definition: sample.c:50
int open_sangoma_device(void)
Open a single span chan device.
Definition: sample.c:760
void handle_span_chan(int open_device_counter)
Write data buffer into a file.
Definition: sample.c:576
void TerminateHandler(int)
Signal handler for graceful shutdown.
Definition: sample.c:679
#define TRUE
TRUE value is 1, Ported from Windows.
Definition: libsangoma.h:248

Here is the call graph for this function:

int open_sangoma_device ( void  )

Open a single span chan device.

Returns
0 ok otherise error.

This function will open a single span chan.

However it can be rewritten to iterate for all spans and chans and try to open all existing wanpipe devices.

For each opened device, a wait object will be initialized. For each device, configure the chunk size for tx/rx enable events such as DTMF/RBS ...etc

Definition at line 760 of file sample.c.

References __sangoma_open_api_span_chan(), INVALID_HANDLE_VALUE, SANG_STATUS_SUCCESS, SANGOMA_DEVICE_WAIT_OBJ, sangoma_fe_reg_read(), sangoma_flush_bufs(), sangoma_flush_stats(), sangoma_get_full_cfg(), sangoma_get_rx_queue_sz(), sangoma_get_tx_queue_sz(), sangoma_open_api_span_chan(), sangoma_set_rx_queue_sz(), sangoma_set_tx_queue_sz(), sangoma_ss7_get_cfg_status(), sangoma_tdm_enable_dtmf_events(), sangoma_tdm_enable_rbs_events(), sangoma_tdm_enable_rm_dtmf_events(), sangoma_tdm_enable_rxhook_events(), sangoma_tdm_set_buffer_multiplier(), sangoma_tdm_set_codec(), sangoma_tdm_set_usr_period(), and sangoma_wait_obj_create().

Referenced by main().

761 {
762  int span, chan, err = 0, open_dev_cnt = 0;
763  sangoma_status_t status;
765  wanpipe_api_t tdm_api;
766 
767  span = wanpipe_port_no;
768  chan = wanpipe_if_no;
769 
770  /* span and chan are 1-based */
771  if (force_open) {
772  dev_fd = __sangoma_open_api_span_chan(span, chan);
773  } else {
774  dev_fd = sangoma_open_api_span_chan(span, chan);
775  }
776  if( dev_fd == INVALID_HANDLE_VALUE){
777  printf("Warning: Failed to open span %d, chan %d\n", span , chan);
778  return 1;
779  }else{
780  printf("Successfuly opened span %d, chan %d\n", span , chan);
781  }
782 
783  memset(&tdm_api, 0x00, sizeof(tdm_api));
784 
785  status = sangoma_wait_obj_create(&sangoma_wait_objects[open_dev_cnt], dev_fd, SANGOMA_DEVICE_WAIT_OBJ);
786  if(status != SANG_STATUS_SUCCESS){
787  printf("Error: Failed to create 'sangoma_wait_object'!\n");
788  return 1;
789  }
790  sangoma_channels[open_dev_cnt].channo = chan;
791  sangoma_channels[open_dev_cnt].spanno = span;
792  sangoma_wait_obj_set_context(sangoma_wait_objects[open_dev_cnt], &sangoma_channels[open_dev_cnt]);
793  /* open_dev_cnt++; */
794 
795  if((err = sangoma_get_full_cfg(dev_fd, &tdm_api))){
796  return 1;
797  }
798 
799  if(set_codec_slinear){
800  printf("Setting SLINEAR codec\n");
801  if((err=sangoma_tdm_set_codec(dev_fd, &tdm_api, WP_SLINEAR))){
802  return 1;
803  }
804  }
805 
806  if(set_codec_none){
807  printf("Disabling codec\n");
808  if((err=sangoma_tdm_set_codec(dev_fd, &tdm_api, WP_NONE))){
809  return 1;
810  }
811  }
812 
813  if(usr_period){
814  printf("Setting user period: %d\n", usr_period);
815  if((err=sangoma_tdm_set_usr_period(dev_fd, &tdm_api, usr_period))){
816  return 1;
817  }
818  }
819 
820  if (rx_gain_cmd) {
821  printf("Setting rx gain : %f\n", rx_gain);
822  if (sangoma_set_rx_gain(dev_fd,&tdm_api,rx_gain)) {
823  return 1;
824  }
825  }
826 
827  if (tx_gain_cmd) {
828  printf("Setting tx gain : %f\n", rx_gain);
829  if (sangoma_set_tx_gain(dev_fd,&tdm_api,tx_gain)) {
830  return 1;
831  }
832  }
833 
834  if(set_codec_slinear || usr_period || set_codec_none){
835  /* display new configuration AFTER it was changed */
836  if((err=sangoma_get_full_cfg(dev_fd, &tdm_api))){
837  return 1;
838  }
839  }
840 
841  if(dtmf_enable_octasic == 1){
842  poll_events_bitmap |= POLLPRI;
843  /* enable dtmf detection on Octasic chip */
844  if((err=sangoma_tdm_enable_dtmf_events(dev_fd, &tdm_api))){
845  return 1;
846  }
847  }
848 
849  if(dtmf_enable_remora == 1){
850  poll_events_bitmap |= POLLPRI;
851  /* enable dtmf detection on Sangoma's Remora SLIC chip (A200 ONLY) */
852  if((err=sangoma_tdm_enable_rm_dtmf_events(dev_fd, &tdm_api))){
853  return 1;
854  }
855  }
856 
857  if(remora_hook == 1){
858  poll_events_bitmap |= POLLPRI;
859  if((err=sangoma_tdm_enable_rxhook_events(dev_fd, &tdm_api))){
860  return 1;
861  }
862  }
863 
864  if(rbs_events == 1){
865  poll_events_bitmap |= POLLPRI;
866  if((err=sangoma_tdm_enable_rbs_events(dev_fd, &tdm_api, 20))){
867  return 1;
868  }
869  }
870  if (buffer_multiplier) {
871  printf("Setting buffer multiplier to %i\n",buffer_multiplier);
872  err=sangoma_tdm_set_buffer_multiplier(dev_fd,&tdm_api,buffer_multiplier);
873  if (err) {
874  return 1;
875  }
876  }
877  if (ss7_cfg_status) {
878  wan_api_ss7_cfg_status_t ss7_hw_status;
879  printf("Getting ss7 cfg status\n");
880  err=sangoma_ss7_get_cfg_status(dev_fd,&tdm_api,&ss7_hw_status);
881  if (err) {
882  return 1;
883  }
884  printf("SS7 HW Configuratoin\n");
885  printf("SS7 hw support = %s\n",ss7_hw_status.ss7_hw_enable?"Enabled":"Disabled");
886  printf("SS7 hw mode = %s\n",ss7_hw_status.ss7_hw_mode?"4096":"128");
887  printf("SS7 hw lssu size = %d\n",ss7_hw_status.ss7_hw_lssu_size);
888  printf("SS7 driver repeat= %s\n",ss7_hw_status.ss7_driver_repeat?"Enabled":"Disabled");
889  }
890 
891  if (stats_test) {
892  sangoma_print_stats(dev_fd);
893  exit(0);
894  }
895  if (flush_stats_test) {
896  sangoma_flush_stats(dev_fd, &tdm_api);
897  sangoma_print_stats(dev_fd);
898  exit(0);
899  }
900 
901  if (fe_read_test) {
902  uint8_t data=0;
903  uint8_t prev_data=0;
904  int i;
905  int elapsed=0;
906  int elapsed_cnt=0;
907 
908 
909 #if defined(__WINDOWS__)
910  long started, ended;
911  started = GetTickCount();
912 #else
913  struct timeval started, ended;
914 
915  gettimeofday(&started, NULL);
916 #endif
917 
918  for (i=0;;i++) {
919  elapsed_cnt++;
920  sangoma_fe_reg_read(dev_fd, 0xF8, &data);
921  prev_data=data;
922  if (data==0) {
923  printf("Error: Bad FE Read 0x00 cnt=%i !!!!!!!!!!! \n",i);
924  exit(1);
925  } else {
926  data=(data>>3)&0x1F;
927  if (data != 0x0C) {
928  printf("Error: Bad FE Read Prev Data = 0x%X cnt=%i !!!!!!!!!!!!\n",prev_data,i);
929  exit(1);
930  }
931  }
932 
933 #if defined(__WINDOWS__)
934  ended = GetTickCount();
935 #else
936  gettimeofday(&ended, NULL);
937 #endif
938 
939 #if defined(__WINDOWS__)
940  elapsed = ended - started;
941 #else
942  elapsed = (((ended.tv_sec * 1000) + ended.tv_usec / 1000) - ((started.tv_sec * 1000) + started.tv_usec / 1000));
943 #endif
944 
945  if (elapsed > 5000) {
946  printf("FE Reg 0xF8 = Prev Data = 0x%X int_cnt=%08i tot_cnt %08i\n",prev_data,elapsed_cnt,i);
947 #if defined(__WINDOWS__)
948  started = GetTickCount();
949 #else
950  gettimeofday(&started, NULL);
951 #endif
952 
953  elapsed_cnt=0;
954  }
955  }
956  exit(0);
957  }
958 
959  printf("Device Config RxQ=%i TxQ=%i \n",
960  sangoma_get_rx_queue_sz(dev_fd,&tdm_api),
961  sangoma_get_rx_queue_sz(dev_fd,&tdm_api));
962 
963 #if 0
964  sangoma_set_rx_queue_sz(dev_fd,&tdm_api,20);
965  sangoma_set_tx_queue_sz(dev_fd,&tdm_api,30);
966 #endif
967 
968  printf("Device Config RxQ=%i TxQ=%i \n",
969  sangoma_get_rx_queue_sz(dev_fd,&tdm_api),
970  sangoma_get_tx_queue_sz(dev_fd,&tdm_api));
971 
972  sangoma_flush_bufs(dev_fd,&tdm_api);
973 
974  sangoma_print_stats(dev_fd);
975 
976 
977  return err;
978 }
sng_fd_t _LIBSNG_CALL __sangoma_open_api_span_chan(int span, int chan)
Open a Device based on Span/Chan values.
Definition: libsangoma.c:1740
#define INVALID_HANDLE_VALUE
Invalid file handle value -1, Ported from Windows.
Definition: libsangoma.h:191
int _LIBSNG_CALL sangoma_flush_stats(sng_fd_t fd, wanpipe_api_t *tdm_api)
Flush/Reset device statistics.
Definition: libsangoma.c:3299
int _LIBSNG_CALL sangoma_get_full_cfg(sng_fd_t fd, wanpipe_api_t *tdm_api)
Read tdm api device configuration.
Definition: libsangoma.c:2026
int _LIBSNG_CALL sangoma_tdm_enable_rxhook_events(sng_fd_t fd, wanpipe_api_t *tdm_api)
Enable RX HOOK Events (Analog Only)
Definition: libsangoma.c:2676
int _LIBSNG_CALL sangoma_set_rx_queue_sz(sng_fd_t fd, wanpipe_api_t *tdm_api, int size)
Get Tx Queue Size for this channel.
Definition: libsangoma.c:2969
int _LIBSNG_CALL sangoma_set_tx_queue_sz(sng_fd_t fd, wanpipe_api_t *tdm_api, int size)
Get Tx Queue Size for this channel.
Definition: libsangoma.c:2939
int _LIBSNG_CALL sangoma_tdm_enable_dtmf_events(sng_fd_t fd, wanpipe_api_t *tdm_api)
Enable DTMF Detection on Octasic chip (if hw supports it)
Definition: libsangoma.c:2636
int sng_fd_t
Windows/Unix file handle abstraction.
int _LIBSNG_CALL sangoma_tdm_set_usr_period(sng_fd_t fd, wanpipe_api_t *tdm_api, int period)
Set Tx/Rx Period in Milliseconds.
Definition: libsangoma.c:2126
Sangoma object associated to some device which cannot be signaled (cannot call sangoma_wait_obj_signa...
Definition: libsangoma.h:371
sng_fd_t _LIBSNG_CALL sangoma_open_api_span_chan(int span, int chan)
Open a Device based on Span/Chan values.
Definition: libsangoma.c:1682
int _LIBSNG_CALL sangoma_get_rx_queue_sz(sng_fd_t fd, wanpipe_api_t *tdm_api)
Get Rx Queue Size for this channel.
Definition: libsangoma.c:2951
int32_t sangoma_status_t
return status from sangoma APIs
Definition: libsangoma.h:338
int _LIBSNG_CALL sangoma_tdm_enable_rm_dtmf_events(sng_fd_t fd, wanpipe_api_t *tdm_api)
Enable DTMF Detection on Analog/Remora SLIC Chip.
Definition: libsangoma.c:2656
sangoma_status_t _LIBSNG_CALL sangoma_wait_obj_create(sangoma_wait_obj_t **sangoma_wait_object, sng_fd_t fd, sangoma_wait_obj_type_t object_type)
Create a wait object that will be used with sangoma_waitfor_many() API.
Definition: libsangoma.c:1239
int _LIBSNG_CALL sangoma_tdm_set_codec(sng_fd_t fd, wanpipe_api_t *tdm_api, int codec)
Set TDM Codec per chan.
Definition: libsangoma.c:2082
Wanpipe API Command Structure.
int _LIBSNG_CALL sangoma_get_tx_queue_sz(sng_fd_t fd, wanpipe_api_t *tdm_api)
Get Tx Queue Size for this channel.
Definition: libsangoma.c:2922
int _LIBSNG_CALL sangoma_tdm_enable_rbs_events(sng_fd_t fd, wanpipe_api_t *tdm_api, int poll_in_sec)
Enable RBS Events on a device.
Definition: libsangoma.c:2361
int _LIBSNG_CALL sangoma_tdm_set_buffer_multiplier(sng_fd_t fd, wanpipe_api_t *tdm_api, unsigned int multiplier)
Set voice tx/rx buffer multiplier.
Definition: libsangoma.c:2406
int _LIBSNG_CALL sangoma_fe_reg_read(sng_fd_t fd, uint32_t offset, uint8_t *data)
Read front end register.
Definition: libsangoma.c:3134
int _LIBSNG_CALL sangoma_ss7_get_cfg_status(sng_fd_t fd, wanpipe_api_t *tdm_api, wan_api_ss7_cfg_status_t *ss7_cfg_status)
Get current ss7 hw configuration.
Definition: libsangoma.c:3410
int _LIBSNG_CALL sangoma_flush_bufs(sng_fd_t fd, wanpipe_api_t *tdm_api)
Flush all (tx/rx/event) buffers from current channel.
Definition: libsangoma.c:2329

Here is the call graph for this function:

Here is the caller graph for this function:

void handle_span_chan ( int  open_device_counter)

Write data buffer into a file.

Parameters
open_device_counternumber of opened devices
Returns
void

This function will wait on all opened devices. This example will wait for RX and EVENT signals. In case of POLLIN - rx data available In case of POLLPRI - event is available

Definition at line 576 of file sample.c.

References handle_data(), handle_tdm_event(), SANG_STATUS_APIPOLL_TIMEOUT, SANG_STATUS_SUCCESS, sangoma_wait_obj_get_fd(), SDLA_DECODE_SANG_STATUS, and TEST_NUMBER_OF_OBJECTS.

Referenced by main().

577 {
578  int iResult, i;
579  u_int32_t input_flags[TEST_NUMBER_OF_OBJECTS];
580  u_int32_t output_flags[TEST_NUMBER_OF_OBJECTS];
581 
582  printf("\n\nSpan/Chan Handler: RxEnable=%s, TxEnable=%s, TxCnt=%i, TxLen=%i, rx2tx=%s\n",
583  (read_enable? "Yes":"No"), (write_enable?"Yes":"No"),tx_cnt,tx_size, (rx2tx?"Yes":"No"));
584 
585  for (i = 0; i < open_device_counter; i++) {
586  input_flags[i] = poll_events_bitmap;
587  }
588 
589  /* Main Rx/Tx/Event loop */
590  while(!application_termination_flag)
591  {
592  iResult = sangoma_waitfor_many(sangoma_wait_objects,
593  input_flags,
594  output_flags,
595  open_device_counter /* number of wait objects */,
596  5000 /* wait timeout, in milliseconds */);
597  switch(iResult)
598  {
600  /* timeout (not an error) */
601  {
602  sng_fd_t dev_fd = sangoma_wait_obj_get_fd(sangoma_wait_objects[0]);
603  sangoma_print_stats(dev_fd);
604  }
605  printf("Timeout\n");
606  continue;
607 
608  case SANG_STATUS_SUCCESS:
609  for(i = 0; i < open_device_counter; i++){
610 
611  /* a wait object was signaled */
612  if(output_flags[i] & POLLPRI){
613  /* got tdm api event */
614  if(handle_tdm_event(i)){
615  printf("Error in handle_tdm_event()!\n");
616  application_termination_flag=1;
617  break;
618  }
619  }
620 
621  if(output_flags[i] & (POLLIN | POLLOUT)){
622  /* rx data OR a free tx buffer available */
623  if(handle_data(i, output_flags[i])){
624  printf("Error in handle_data()!\n");
625  //application_termination_flag=1;
626  break;
627  }
628  }
629  }/* for() */
630  break;
631 
632  default:
633  /* error */
634  printf("Error: iResult: %s (%d)\n", SDLA_DECODE_SANG_STATUS(iResult), iResult);
635  return;
636  }
637 
638  }/* while() */
639 }
sng_fd_t _LIBSNG_CALL sangoma_wait_obj_get_fd(sangoma_wait_obj_t *sng_wait_obj)
Get fd device file descriptor which was the &#39;fd&#39; parameter for sangoma_wait_obj_create(), not useful for generic objects.
Definition: libsangoma.c:1382
int sng_fd_t
Windows/Unix file handle abstraction.
int handle_data(uint32_t dev_index, int flags_out)
Read data buffer from the device and transmit it back down.
Definition: sample.c:391
#define SDLA_DECODE_SANG_STATUS(status)
Print decode of Sangoma Return Codes.
int handle_tdm_event(uint32_t dev_index)
Read Event buffer from the device.
Definition: sample.c:513
#define TEST_NUMBER_OF_OBJECTS
Number of wait objects to define in object array.
Definition: sample.c:50

Here is the call graph for this function:

Here is the caller graph for this function:

int handle_tdm_event ( uint32_t  dev_index)

Read Event buffer from the device.

Parameters
dev_indexdevice index number associated with device file descriptor
Returns
0 - Ok otherwise Error

An EVENT has occoured. Execute a system call to read the EVENT on a device.

Definition at line 513 of file sample.c.

References wanpipe_api_cmd::event, sangoma_read_event(), sangoma_wait_obj_get_fd(), and wanpipe_api::wp_cmd.

Referenced by handle_span_chan().

514 {
515  wanpipe_api_t tdm_api;
516  sng_fd_t dev_fd = sangoma_wait_obj_get_fd(sangoma_wait_objects[dev_index]);
517 
518 #if 0
519  printf("%s(): dev_index: %d, dev_fd: 0x%p\n", __FUNCTION__, dev_index, dev_fd);
520 #endif
521 
522  memset(&tdm_api, 0x00, sizeof(tdm_api));
523 
524  if(sangoma_read_event(dev_fd, &tdm_api)){
525  return 1;
526  }
527 
528  decode_api_event(&tdm_api.wp_cmd.event);
529  return 0;
530 }
int _LIBSNG_CALL sangoma_read_event(sng_fd_t fd, wanpipe_api_t *tdm_api)
Read API Events.
Definition: libsangoma.c:2426
sng_fd_t _LIBSNG_CALL sangoma_wait_obj_get_fd(sangoma_wait_obj_t *sng_wait_obj)
Get fd device file descriptor which was the &#39;fd&#39; parameter for sangoma_wait_obj_create(), not useful for generic objects.
Definition: libsangoma.c:1382
int sng_fd_t
Windows/Unix file handle abstraction.
wanpipe_api_cmd_t wp_cmd
wp_api_event_t event
Wanpipe API Command Structure.

Here is the call graph for this function:

Here is the caller graph for this function:

int handle_data ( uint32_t  dev_index,
int  flags_out 
)

Read data buffer from the device and transmit it back down.

Parameters
dev_indexdevice index number associated with device file descriptor
Returns
0 - Ok otherwise Error

Read data buffer from a device.

Definition at line 391 of file sample.c.

References wp_api_hdr::data_length, MAX_NO_DATA_BYTES_IN_FRAME, and write_data().

Referenced by handle_span_chan().

392 {
393  wp_api_hdr_t rxhdr;
394  int err=0;
395 
396  memset(&rxhdr, 0, sizeof(rxhdr));
397 
398 #if 0
399  printf("%s(): span: %d, chan: %d\n", __FUNCTION__,
400  sangoma_wait_objects[dev_index].span, sangoma_wait_objects[dev_index].chan);
401 #endif
402 
403  if(flags_out & POLLIN){
404  if(read_data(dev_index, &rxhdr, rxdata, MAX_NO_DATA_BYTES_IN_FRAME) == 0){
405  if(rx2tx){
406  /* Send back received data (create a "software loopback"), just a test. */
407  return write_data(dev_index, &rxhdr, rxdata,rxhdr.data_length);
408  }
409  }
410  }
411 
412  if( (flags_out & POLLOUT) && write_enable ){
413 
414  wp_api_hdr_t txhdr;
415  static unsigned char tx_test_byte = 2;
416 
417  memset(&txhdr, 0, sizeof(txhdr));
418  txhdr.data_length = (unsigned short)tx_size;/* use '-txsize' command line option to change 'tx_size' */
419 
420  /* set data which will be transmitted */
421  memset(txdata, tx_test_byte, txhdr.data_length);
422 
423  err = write_data(dev_index, &txhdr, txdata, tx_size);
424  if (err== 0) {
425  //tx_test_byte++;
426  }
427  }
428  return err;
429 }
Wanpipe API Header Structure.
int write_data(uint32_t dev_index, wp_api_hdr_t *tx_hdr, void *tx_buffer, int tx_len)
Transmit a data buffer to a device.
Definition: sample.c:325
u_int16_t data_length
#define MAX_NO_DATA_BYTES_IN_FRAME
Maximum tx/rx data size.

Here is the call graph for this function:

Here is the caller graph for this function:

int write_data ( uint32_t  dev_index,
wp_api_hdr_t tx_hdr,
void *  tx_data,
int  tx_len 
)

Transmit a data buffer to a device.

Parameters
dev_indexdevice index number associated with device file descriptor
tx_hdrpointer to a wp_api_hdr_t
tx_datapointer to a data buffer
tx_lentx data buffer len
Returns
0 - Ok otherwise Error

Definition at line 325 of file sample.c.

References sangoma_msleep, sangoma_wait_obj_get_context(), sangoma_wait_obj_get_fd(), and sangoma_writemsg().

Referenced by handle_data().

326 {
327  sng_fd_t dev_fd = sangoma_wait_obj_get_fd(sangoma_wait_objects[dev_index]);
328  sangoma_chan_t *chan = sangoma_wait_obj_get_context(sangoma_wait_objects[dev_index]);
329  int err;
330  static int Tx_count = 0;
331 
332  if (hdlc_repeat) {
333  if (verbose){
334  printf("Repeating Frame\n");
335  }
336  tx_hdr->wp_api_tx_hdr_hdlc_rpt_len=4;
337  memset(tx_hdr->wp_api_tx_hdr_hdlc_rpt_data,Tx_count,4);
338  }
339 
340  /* write a message */
341  err = sangoma_writemsg(
342  dev_fd,
343  tx_hdr, /* header buffer */
344  sizeof(wp_api_hdr_t), /* header size */
345  tx_buffer, /* data buffer */
346  tx_len, /* DATA size */
347  0);
348 
349  if (err <= 0){
350  printf("Span: %d, Chan: %d: Failed to send %s!\n",
351  chan->spanno,
352  chan->channo, strerror(errno));
353  return -1;
354  }
355 
356  Tx_count++;
357  if (verbose){
358  printf("Packet sent: counter: %i, len: %i, errors %i\n", Tx_count, err, tx_hdr->wp_api_tx_hdr_errors);
359  }else{
360  if(Tx_count && (!(Tx_count % 1000))){
361  printf("Packet sent: counter: %i, len: %i\n", Tx_count, err);
362  }
363  }
364 
365  if (tx_delay) {
366  sangoma_msleep(tx_delay);
367  }
368 
369 #if 1
370  if(tx_cnt && Tx_count >= tx_cnt){
371  write_enable=0;
372  printf("Disabling POLLOUT...\n");
373  /* No need for POLLOUT, turn it off!! If not turned off, and we
374  * have nothing for transmission, sangoma_socket_waitfor() will return
375  * immediately, creating a busy loop. */
376  //sangoma_wait_objects[dev_index].flags_in &= (~POLLOUT);
377  return 0;
378  }
379 #endif
380  return 0;
381 }
sng_fd_t _LIBSNG_CALL sangoma_wait_obj_get_fd(sangoma_wait_obj_t *sng_wait_obj)
Get fd device file descriptor which was the &#39;fd&#39; parameter for sangoma_wait_obj_create(), not useful for generic objects.
Definition: libsangoma.c:1382
int _LIBSNG_CALL sangoma_writemsg(sng_fd_t fd, void *hdrbuf, int hdrlen, void *databuf, unsigned short datalen, int flag)
Write Data to device.
Definition: libsangoma.c:1913
int sng_fd_t
Windows/Unix file handle abstraction.
#define sangoma_msleep(x)
milisecond sleep function
Definition: libsangoma.h:251
PVOID _LIBSNG_CALL sangoma_wait_obj_get_context(sangoma_wait_obj_t *sng_wait_obj)
Retrieve the user context (if any) that was set via sangoma_wait_obj_set_context. ...
Definition: libsangoma.c:1406
Wanpipe API Header Structure.

Here is the call graph for this function:

Here is the caller graph for this function:

int write_data_to_file ( unsigned char *  data,
unsigned int  data_length 
)

Write data buffer into a file.

Parameters
datadata buffer
data_lengthlength of a data buffer
Returns
data_length = ok otherwise error

Definition at line 649 of file sample.c.

650 {
651  if(pRxFile == NULL){
652  return 1;
653  }
654 
655  return fwrite(data, 1, data_length, pRxFile);
656 }
void cleanup ( void  )

Protperly shutdown single device.

Parameters
dev_nodevice index number
Returns
void

Definition at line 697 of file sample.c.

References sangoma_close(), sangoma_tdm_disable_dtmf_events(), sangoma_tdm_disable_rbs_events(), sangoma_tdm_disable_rm_dtmf_events(), sangoma_tdm_disable_rxhook_events(), sangoma_wait_obj_delete(), sangoma_wait_obj_get_context(), sangoma_wait_obj_get_fd(), and TEST_NUMBER_OF_OBJECTS.

Referenced by main(), and TerminateHandler().

698 {
699  int dev_no;
700  sng_fd_t fd;
701  sangoma_chan_t *chan;
702  sangoma_wait_obj_t *sng_wait_object;
703  wanpipe_api_t tdm_api;
704 
705  /* do the cleanup before exiting: */
706  for(dev_no = 0; dev_no < TEST_NUMBER_OF_OBJECTS; dev_no++){
707 
708  sng_wait_object = sangoma_wait_objects[dev_no];
709  if(!sng_wait_object){
710  continue;
711  }
712  chan = sangoma_wait_obj_get_context(sng_wait_object);
713  printf("%s(): span: %d, chan: %d ...\n", __FUNCTION__,
714  chan->channo,
715  chan->spanno);
716 
717  fd = sangoma_wait_obj_get_fd(sng_wait_object);
718  memset(&tdm_api, 0x00, sizeof(tdm_api));
719 
720  if(dtmf_enable_octasic == 1){
721  /* Disable dtmf detection on Octasic chip */
722  sangoma_tdm_disable_dtmf_events(fd, &tdm_api);
723  }
724 
725  if(dtmf_enable_remora == 1){
726  /* Disable dtmf detection on Sangoma's Remora SLIC chip */
728  }
729 
730  if(remora_hook == 1){
731  sangoma_tdm_disable_rxhook_events(fd, &tdm_api);
732  }
733 
734  if(rbs_events == 1){
735  sangoma_tdm_disable_rbs_events(fd, &tdm_api);
736  }
737 
738  sangoma_wait_obj_delete(&sng_wait_object);
739 
740  sangoma_close(&fd);
741 
742  }
743 }
int _LIBSNG_CALL sangoma_tdm_disable_rxhook_events(sng_fd_t fd, wanpipe_api_t *tdm_api)
Disable RX HOOK Events (Analog Only)
Definition: libsangoma.c:2686
sng_fd_t _LIBSNG_CALL sangoma_wait_obj_get_fd(sangoma_wait_obj_t *sng_wait_obj)
Get fd device file descriptor which was the &#39;fd&#39; parameter for sangoma_wait_obj_create(), not useful for generic objects.
Definition: libsangoma.c:1382
int _LIBSNG_CALL sangoma_tdm_disable_rm_dtmf_events(sng_fd_t fd, wanpipe_api_t *tdm_api)
Disable DTMF Detection on Analog/Remora SLIC Chip.
Definition: libsangoma.c:2666
int sng_fd_t
Windows/Unix file handle abstraction.
int _LIBSNG_CALL sangoma_tdm_disable_rbs_events(sng_fd_t fd, wanpipe_api_t *tdm_api)
Disable RBS Events for a device.
Definition: libsangoma.c:2370
#define TEST_NUMBER_OF_OBJECTS
Number of wait objects to define in object array.
Definition: sample.c:50
Wanpipe API Command Structure.
sangoma_status_t _LIBSNG_CALL sangoma_wait_obj_delete(sangoma_wait_obj_t **sangoma_wait_object)
De-allocate all resources in a wait object.
Definition: libsangoma.c:1320
void _LIBSNG_CALL sangoma_close(sng_fd_t *fd)
Close device file descriptor.
Definition: libsangoma.c:1818
PVOID _LIBSNG_CALL sangoma_wait_obj_get_context(sangoma_wait_obj_t *sng_wait_obj)
Retrieve the user context (if any) that was set via sangoma_wait_obj_set_context. ...
Definition: libsangoma.c:1406
int _LIBSNG_CALL sangoma_tdm_disable_dtmf_events(sng_fd_t fd, wanpipe_api_t *tdm_api)
Disable DTMF Detection on Octasic chip (if hw supports it)
Definition: libsangoma.c:2646

Here is the call graph for this function:

Here is the caller graph for this function:

void TerminateHandler ( int  sig)

Signal handler for graceful shutdown.

Parameters
sigsignal

Definition at line 679 of file sample.c.

References cleanup().

Referenced by main().

680 {
681  printf("\nProcess terminated by user request.\n");
682  application_termination_flag = 1;
683  /* do the cleanup before exiting: */
684  cleanup();
685  return;
686 }
void cleanup(void)
Protperly shutdown single device.
Definition: sample.c:697

Here is the call graph for this function:

Here is the caller graph for this function: