Added processing of second caller id.
[lcr.git] / port.cpp
index d9f07c3..3867464 100644 (file)
--- a/port.cpp
+++ b/port.cpp
@@ -45,14 +45,6 @@ Functions:
    
 */
 
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <errno.h>
 #include "main.h"
 
 #define SHORT_MIN -32768
@@ -60,7 +52,7 @@ Functions:
 
 class Port *port_first = NULL;
 
-unsigned long port_serial = 1; /* must be 1, because 0== no port */
+unsigned int port_serial = 1; /* must be 1, because 0== no port */
 
 
 /* free epointlist relation
@@ -94,7 +86,7 @@ void Port::free_epointlist(struct epoint_list *epointlist)
 }
 
 
-void Port::free_epointid(unsigned long epoint_id)
+void Port::free_epointid(unsigned int epoint_id)
 {
        struct epoint_list *temp, **tempp;
 
@@ -125,7 +117,7 @@ void Port::free_epointid(unsigned long epoint_id)
 
 /* create new epointlist relation
  */
-struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
+struct epoint_list *Port::epointlist_new(unsigned int epoint_id)
 {
        struct epoint_list *epointlist, **epointlistpointer;
 
@@ -154,7 +146,7 @@ struct epoint_list *Port::epointlist_new(unsigned long epoint_id)
 /*
  * port constructor
  */
-Port::Port(int type, char *portname, struct port_settings *settings)
+Port::Port(int type, const char *portname, struct port_settings *settings)
 {
        class Port *temp, **tempp;
 
@@ -215,10 +207,10 @@ Port::Port(int type, char *portname, struct port_settings *settings)
 Port::~Port(void)
 {
        class Port *temp, **tempp;
-       struct message *message;
+       struct lcr_msg *message;
 
        if (p_record)
-               close_record(0);
+               close_record(0, 0);
 
        classuse--;
 
@@ -275,7 +267,7 @@ void Port::new_state(int state)
 /*
  * find the port with port_id
  */ 
-class Port *find_port_id(unsigned long port_id)
+class Port *find_port_id(unsigned int port_id)
 {
        class Port *port = port_first;
 
@@ -303,7 +295,7 @@ void Port::set_echotest(int echotest)
 /*
  * set the file in the tone directory with the given name
  */
-void Port::set_tone(char *dir, char *name)
+void Port::set_tone(const char *dir, const char *name)
 {
        int fh;
        char filename[128];
@@ -311,6 +303,17 @@ void Port::set_tone(char *dir, char *name)
        if (name == NULL)
                name = "";
 
+#if 0
+       /* if tones is jingle, store next tone */
+       if ((p_tone_fh >= 0 || p_tone_fetched)
+        && (!strcmp(p_tone_name, "left") || !strcmp(p_tone_name, "joined")))
+       {
+               SCPY(p_tone_dir, dir);
+               SCPY(p_tone_name, name);
+               return;
+       }
+#endif
+
        /* no counter, no eof, normal speed */
        p_tone_counter = 0;
        p_tone_eof = 0;
@@ -358,13 +361,13 @@ void Port::set_tone(char *dir, char *name)
                p_tone_fetched = NULL;
                return;
        }
-       SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
+       SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
        if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
        {
                close(fh);
                return;
        }
-       SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
+       SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
        if ((fh=open_tone(filename, &p_tone_codec, 0, 0)) >= 0)
        {
                close(fh);
@@ -392,7 +395,7 @@ void Port::set_tone(char *dir, char *name)
  * set the file in the tone directory for vbox playback
  * also set the play_eof-flag
  */
-void Port::set_vbox_tone(char *dir, char *name)
+void Port::set_vbox_tone(const char *dir, const char *name)
 {
        char filename[256];
 
@@ -420,7 +423,7 @@ void Port::set_vbox_tone(char *dir, char *name)
                        PDEBUG(DEBUG_PORT, "PORT(%s) opening fetched tone: %s\n", p_name, p_tone_name);
                        return;
                }
-               SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
+               SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
                if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) >= 0)
                {
                        fhuse++;
@@ -445,10 +448,9 @@ void Port::set_vbox_tone(char *dir, char *name)
  * also set the eof-flag
  * also set the counter-flag
  */
-void Port::set_vbox_play(char *name, int offset)
+void Port::set_vbox_play(const char *name, int offset)
 {
-       signed long size;
-       struct message *message;
+       struct lcr_msg *message;
 
        /* use ser_box_tone() */
        set_vbox_tone("", name);
@@ -466,7 +468,7 @@ void Port::set_vbox_play(char *name, int offset)
                {
                        message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
                        message->param.counter.current = offset;
-                       message->param.counter.max = size;
+                       message->param.counter.max = p_tone_size;
                        message_put(message);
                }
        }
@@ -488,7 +490,7 @@ void Port::set_vbox_speed(int speed)
  */
 int Port::read_audio(unsigned char *buffer, int length)
 {
-       int l,len;
+       int l = 0,len;
        int nodata=0; /* to detect 0-length files and avoid endless reopen */
        char filename[128];
        int tone_left_before; /* temp variable to determine the change in p_tone_left */
@@ -512,7 +514,7 @@ int Port::read_audio(unsigned char *buffer, int length)
                        /* if file does not exist */
                        if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
                        {
-                               SPRINT(filename, "%s/%s/%s", INSTALL_DATA, p_tone_dir, p_tone_name);
+                               SPRINT(filename, "%s/%s/%s", SHARE_DATA, p_tone_dir, p_tone_name);
                                /* if file does not exist */
                                if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
                                {
@@ -562,7 +564,7 @@ read_more:
                if (((p_tone_size-p_tone_left)/8000) != (p_tone_size-tone_left_before)/8000)
                {
 //printf("\nsize=%d left=%d\n\n",p_tone_size,p_tone_left);
-                       struct message *message;
+                       struct lcr_msg *message;
                        message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_COUNTER);
                        message->param.counter.current = (p_tone_size-p_tone_left)/8000;
                        message->param.counter.max = -1;
@@ -597,7 +599,7 @@ read_more:
 try_loop:
        if (p_tone_eof && ACTIVE_EPOINT(p_epointlist))
        {
-               struct message *message;
+               struct lcr_msg *message;
                message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_TONE_EOF);
                message_put(message);
        }
@@ -608,7 +610,7 @@ try_loop:
                SPRINT(filename, "%s_loop", p_tone_name);
                if (!(p_tone_fetched=open_tone_fetched(p_tone_dir, filename, &p_tone_codec, &p_tone_size, &p_tone_left)))
                {
-                       SPRINT(filename, "%s/%s/%s_loop", INSTALL_DATA, p_tone_dir, p_tone_name);
+                       SPRINT(filename, "%s/%s/%s_loop", SHARE_DATA, p_tone_dir, p_tone_name);
                        /* if file does not exist */
                        if ((p_tone_fh=open_tone(filename, &p_tone_codec, &p_tone_size, &p_tone_left)) < 0)
                        {
@@ -651,8 +653,8 @@ int Port::handler(void)
  * this is called by the message_epoint inherited by child classes
  * therefor a return=1 means: stop, no more processing
  */
-//extern struct message *dddebug;
-int Port::message_epoint(unsigned long epoint_id, int message_id, union parameter *param)
+//extern struct lcr_msg *dddebug;
+int Port::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
 {
        /* check if we got audio data from one remote port */
        switch(message_id)
@@ -687,8 +689,8 @@ int Port::message_epoint(unsigned long epoint_id, int message_id, union paramete
 struct fmt {
        unsigned short  stereo; /* 1 = mono, 2 = stereo */
        unsigned short  channels; /* number of channels */
-       unsigned long   sample_rate; /* sample rate */
-       unsigned long   data_rate; /* data rate */
+       unsigned int    sample_rate; /* sample rate */
+       unsigned int    data_rate; /* data rate */
        unsigned short  bytes_sample; /* bytes per sample (all channels) */
        unsigned short  bits_sample; /* bits per sample (one channel) */
 };
@@ -699,7 +701,7 @@ struct fmt {
  * written before close, because we do not know the size yet)
  * type=1 record annoucement,  type=0 record audio stream, type=2 record vbox
  */
-int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, char *vbox_email, int vbox_email_file)
+int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ignore, const char *vbox_email, int vbox_email_file)
 {
        /* RIFFxxxxWAVEfmt xxxx(fmt-size)dataxxxx... */
        char dummyheader[8+4+8+sizeof(fmt)+8];
@@ -722,9 +724,9 @@ int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ig
        }
 
        if (vbox != 0)
-               SPRINT(filename, "%s/%s/%s/vbox", INSTALL_DATA, options.extensions_dir, p_record_extension);
+               SPRINT(filename, "%s/%s/vbox", EXTENSION_DATA, p_record_extension);
        else
-               SPRINT(filename, "%s/%s/%s/recordings", INSTALL_DATA, options.extensions_dir, p_record_extension);
+               SPRINT(filename, "%s/%s/recordings", EXTENSION_DATA, p_record_extension);
        if (mkdir(filename, 0755) < 0)
        {
                if (errno != EEXIST)
@@ -788,11 +790,10 @@ int Port::open_record(int type, int vbox, int skip, char *extension, int anon_ig
 /*
  * close the recoding file, put header in front and rename
  */
-void Port::close_record(int beep)
+void Port::close_record(int beep, int mute)
 {
-       static signed long beep_mono[] = {-10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000, -10000, 10000};
-       static unsigned char beep_8bit[] = {48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208, 48, 208};
-       unsigned long size, wsize;
+       static signed short beep_mono[256];
+       unsigned int size = 0, wsize = 0;
        struct fmt fmt;
        char filename[512], indexname[512];
        FILE *fp;
@@ -800,17 +801,17 @@ void Port::close_record(int beep)
        char number[256], callerid[256];
        char *p;
        struct caller_info callerinfo;
-       char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
+       const char *valid_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890_.-!$%&/()=+*;~";
 
        if (!p_record)
                return;
        PDEBUG(DEBUG_PORT, "data still in record buffer: %d (dir %d)\n", (p_record_buffer_writep - p_record_buffer_readp) & RECORD_BUFFER_MASK, p_record_buffer_dir);
 
        memcpy(&callerinfo, &p_callerinfo, sizeof(struct caller_info));
-       apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
+//     apply_callerid_restriction(p_record_anon_ignore, callerinfo.id, &callerinfo.ntype, &callerinfo.present, &callerinfo.screen, callerinfo.extension, callerinfo.name);
 
        SCPY(number, p_dialinginfo.id);
-       SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype));
+       SCPY(callerid, numberrize_callerinfo(callerinfo.id, callerinfo.ntype, options.national, options.international));
        if (callerid[0] == '\0')
        {
                if (callerinfo.present == INFO_PRESENT_RESTRICTED)
@@ -849,41 +850,31 @@ void Port::close_record(int beep)
                i++;
        }
 
+       /* mute */
+       if (mute && p_record_type==CODEC_MONO)
+       {
+               i = p_record_length;
+               if (i > mute)
+                       i = mute;       
+               fseek(p_record, -(i<<1), SEEK_END);
+               p_record_length -= (i<<1);
+       }
        /* add beep to the end of recording */
-       if (beep)
-       switch(p_record_type)
+       if (beep && p_record_type==CODEC_MONO)
        {
-               case CODEC_MONO:
                i = 0;
-               while(i < beep)
+               while(i < 256)
                {
-                       fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
-                       i += sizeof(beep_mono);
-                       p_record_length += sizeof(beep_mono);
-               }
-               break;
-               case CODEC_8BIT:
-               i = 0;
-               while(i < beep)
-               {
-                       fwrite(beep_8bit, sizeof(beep_8bit), 1, p_record);
-                       i += sizeof(beep_8bit);
-                       p_record_length += sizeof(beep_8bit);
+                       beep_mono[i] = (signed short)(sin((double)i / 5.688888888889 * 2.0 * 3.1415927) * 2000.0);
+                       i++;
                }
-               break;
-#if 0
-               case CODEC_LAW:
                i = 0;
                while(i < beep)
                {
-                       fwrite(beep_law, sizeof(beep_law), 1, p_record);
-                       i += sizeof(beep_law);
-                       p_record_length += sizeof(beep_law);
+                       fwrite(beep_mono, sizeof(beep_mono), 1, p_record);
+                       i += sizeof(beep_mono);
+                       p_record_length += sizeof(beep_mono);
                }
-               break;
-#endif
-               default:
-               PERROR("codec %d not supported for beep adding\n", p_record_type);
        }
 
        /* complete header */
@@ -912,7 +903,7 @@ void Port::close_record(int beep)
                fprintf(p_record, "WAVE");
 
                /* fmt */
-               fprintf(p_record, "fmt %c%c%c%c", sizeof(fmt), 0, 0, 0);
+               fprintf(p_record, "fmt %c%c%c%c", (unsigned int)sizeof(fmt), 0, 0, 0);
                switch(p_record_type)
                {
                        case CODEC_MONO:
@@ -977,7 +968,7 @@ void Port::close_record(int beep)
 
        if (p_record_vbox == 2)
        {
-               SPRINT(indexname, "%s/%s/%s/vbox/index", INSTALL_DATA, options.extensions_dir, p_record_extension);
+               SPRINT(indexname, "%s/%s/vbox/index", EXTENSION_DATA, p_record_extension);
                if ((fp = fopen(indexname,"a")))
                {
                        fduse++;
@@ -1023,7 +1014,7 @@ void Port::record(unsigned char *data, int length, int dir_fromup)
        unsigned char write_buffer[1024], *d;
        signed short *s;
        int free, i, ii;
-       signed long sample;
+       signed int sample;
 
        /* no recording */
        if (!p_record || !length)