Modified: freeswitch/branches/gmaruzz/mod_gsmopen/gsmopen_protocol.cpp (16208 => 16209)
--- freeswitch/branches/gmaruzz/mod_gsmopen/gsmopen_protocol.cpp 2010-01-08 01:05:05 UTC (rev 16208)
+++ freeswitch/branches/gmaruzz/mod_gsmopen/gsmopen_protocol.cpp 2010-01-08 03:40:46 UTC (rev 16209)
@@ -1,5 +1,6 @@
#include "gsmopen.h"
//#include <iostream.h>
+
#ifndef NO_GSMLIB
#include <gsmlib/gsm_sms.h>
#ifdef WIN32
@@ -15,7 +16,6 @@
using namespace gsmlib;
#endif// NO_GSMLIB
-
#ifdef ASTERISK
#define gsmopen_sleep usleep
#define gsmopen_strncpy strncpy
@@ -36,8 +36,58 @@
int option_debug = 100;
+#ifdef WIN32
+#define GSMLIBGIO
+#else //WIN32
+#undef GSMLIBGIO
+#endif //WIN32
-#undef GSMLIBGIO
+#ifdef WIN32
+/***************/
+// from http://www.openasthra.com/c-tidbits/gettimeofday-function-for-windows/
+
+#include <time.h>
+
+#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS)
+#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64
+#else /* */
+#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
+#endif /* */
+struct sk_timezone {
+ int tz_minuteswest; /* minutes W of Greenwich */
+ int tz_dsttime; /* type of dst correction */
+};
+int gettimeofday(struct timeval *tv, struct sk_timezone *tz)
+{
+ FILETIME ft;
+ unsigned __int64 tmpres = 0;
+ static int tzflag;
+ if (NULL != tv) {
+ GetSystemTimeAsFileTime(&ft);
+ tmpres |= ft.dwHighDateTime;
+ tmpres <<= 32;
+ tmpres |= ft.dwLowDateTime;
+
+ /*converting file time to unix epoch */
+ tmpres /= 10; /*convert into microseconds */
+ tmpres -= DELTA_EPOCH_IN_MICROSECS;
+ tv->tv_sec = (long) (tmpres / 1000000UL);
+ tv->tv_usec = (long) (tmpres % 1000000UL);
+ }
+ if (NULL != tz) {
+ if (!tzflag) {
+ _tzset();
+ tzflag++;
+ }
+ tz->tz_minuteswest = _timezone / 60;
+ tz->tz_dsttime = _daylight;
+ }
+ return 0;
+}
+
+/***************/
+#endif /* WIN32 */
+
#ifndef GSMLIBGIO
int gsmopen_serial_init(private_t * tech_pvt, speed_t controldevice_speed)
{
@@ -121,13 +171,17 @@
return (fd);
}
#else //GSMLIBGIO
+#ifdef WIN32
+int gsmopen_serial_init(private_t * tech_pvt, int controldevice_speed)
+#else
int gsmopen_serial_init(private_t * tech_pvt, speed_t controldevice_speed)
+#endif //WIN32
{
int i;
string ciapa;
#ifdef WIN32
- Ref < Port > port = new Win32SerialPort((string) tech_pvt->controldevice_name, 38400);
+ Ref <Port> port = new Win32SerialPort((string) tech_pvt->controldevice_name, 38400);
#else
//Ref<Port> port = new UnixSerialPort((string)argv[1], B38400);
Ref < Port > port = new UnixSerialPort((string) tech_pvt->controldevice_name, B115200);
@@ -135,7 +189,7 @@
MeTa m(port);
//cout << "Creating GsmAt object" << endl;
- Ref < GsmAt > gsmat = new GsmAt(m);
+ Ref <GsmAt> gsmat = new GsmAt(m);
//cout << "Using GsmAt object" << endl;
//cout << gsmat->chat("AT", "OK", false, false) << endl;
@@ -144,11 +198,10 @@
for (i = 0; i < 4; i++) {
ciapa = gsmat->getLine();
//cout << "PRESO: |||" << ciapa << "|||" << endl;
- ERRORA("PRESO %d |||%s|||\n", GSMOPEN_P_LOG, i, ciapa.c_str());
- //usleep(5000);
+ NOTICA("PRESO %d |||%s|||\n", GSMOPEN_P_LOG, i, ciapa.c_str());
+ //gsmopen_sleep(5000);
}
-
return (-1);
}
@@ -236,7 +289,7 @@
/* initial_pause? */
if (tech_pvt->at_initial_pause) {
DEBUGA_GSMOPEN("sleeping for %d usec\n", GSMOPEN_P_LOG, tech_pvt->at_initial_pause);
- usleep(tech_pvt->at_initial_pause);
+ gsmopen_sleep(tech_pvt->at_initial_pause);
}
/* go until first empty preinit string, or last preinit string */
@@ -293,7 +346,7 @@
/* after_preinit_pause? */
if (tech_pvt->at_after_preinit_pause) {
DEBUGA_GSMOPEN("sleeping for %d usec\n", GSMOPEN_P_LOG, tech_pvt->at_after_preinit_pause);
- usleep(tech_pvt->at_after_preinit_pause);
+ gsmopen_sleep(tech_pvt->at_after_preinit_pause);
}
/* phone, brother, art you alive? */
@@ -307,7 +360,7 @@
if (res) {
DEBUGA_GSMOPEN("AT+mode=0 does not get OK from the phone. If it is NOT Motorola," " no problem.\n", GSMOPEN_P_LOG);
}
- usleep(50000);
+ gsmopen_sleep(50000);
/* for motorola end */
/* reset AT configuration to phone default */
@@ -532,7 +585,7 @@
int gsmopen_serial_sync_AT(private_t * tech_pvt)
{
- usleep(10000); /* 10msec */
+ gsmopen_sleep(10000); /* 10msec */
time(&tech_pvt->gsmopen_serial_synced_timestamp);
return 0;
}
@@ -1021,7 +1074,7 @@
} else {
DEBUGA_GSMOPEN("|%s| +CMTI: Incoming SMS in position: %d!\n", GSMOPEN_P_LOG, tech_pvt->line_array.result[i], pos);
tech_pvt->unread_sms_msg_id = pos;
- usleep(1000);
+ gsmopen_sleep(1000);
if (tech_pvt->unread_sms_msg_id) {
char at_command[256];
@@ -1754,7 +1807,7 @@
if (res != 1) {
DEBUGA_GSMOPEN("Error sending (%.1s): %d (%s)\n", GSMOPEN_P_LOG, &data[i], res, strerror(errno));
- usleep(100000);
+ gsmopen_sleep(100000);
for (count = 0; count < 10; count++) {
res = write(tech_pvt->controldevfd, &data[i], 1);
if (res == 1) {
@@ -1762,7 +1815,7 @@
break;
} else
DEBUGA_GSMOPEN("Error RE-sending (%.1s): %d %d (%s)\n", GSMOPEN_P_LOG, &data[i], count, res, strerror(errno));
- usleep(100000);
+ gsmopen_sleep(100000);
}
if (res != 1) {
@@ -1772,14 +1825,14 @@
}
if (option_debug > 1)
DEBUGA_GSMOPEN("sent data... (%.1s)\n", GSMOPEN_P_LOG, &data[i]);
- usleep(1000); /* release the cpu */
+ gsmopen_sleep(1000); /* release the cpu */
}
res = write(tech_pvt->controldevfd, "\r", 1);
if (res != 1) {
DEBUGA_GSMOPEN("Error sending (carriage return): %d (%s)\n", GSMOPEN_P_LOG, res, strerror(errno));
- usleep(100000);
+ gsmopen_sleep(100000);
for (count = 0; count < 10; count++) {
res = write(tech_pvt->controldevfd, "\r", 1);
@@ -1788,7 +1841,7 @@
break;
} else
DEBUGA_GSMOPEN("Error RE-sending (carriage return): %d %d (%s)\n", GSMOPEN_P_LOG, count, res, strerror(errno));
- usleep(100000);
+ gsmopen_sleep(100000);
}
if (res != 1) {
@@ -1798,7 +1851,7 @@
}
if (option_debug > 1)
DEBUGA_GSMOPEN("sent (carriage return)\n", GSMOPEN_P_LOG);
- usleep(1000); /* release the cpu */
+ gsmopen_sleep(1000); /* release the cpu */
return howmany;
}
@@ -1817,14 +1870,14 @@
if (res != 1) {
DEBUGA_GSMOPEN("Error sending (%.1s): %d (%s)\n", GSMOPEN_P_LOG, &data[i], res, strerror(errno));
- usleep(100000);
+ gsmopen_sleep(100000);
for (count = 0; count < 10; count++) {
res = write(tech_pvt->controldevfd, &data[i], 1);
if (res == 1)
break;
else
DEBUGA_GSMOPEN("Error RE-sending (%.1s): %d %d (%s)\n", GSMOPEN_P_LOG, &data[i], count, res, strerror(errno));
- usleep(100000);
+ gsmopen_sleep(100000);
}
if (res != 1) {
@@ -1834,10 +1887,10 @@
}
if (option_debug > 1)
DEBUGA_GSMOPEN("sent data... (%.1s)\n", GSMOPEN_P_LOG, &data[i]);
- usleep(1000); /* release the cpu */
+ gsmopen_sleep(1000); /* release the cpu */
}
- usleep(1000); /* release the cpu */
+ gsmopen_sleep(1000); /* release the cpu */
return howmany;
}
@@ -2075,6 +2128,7 @@
int ucs2_to_utf8(private_t * tech_pvt, char *ucs2_in, char *utf8_out, size_t outbytesleft)
{
char converted[16000];
+#ifndef WIN32
iconv_t iconv_format;
int iconv_res;
char *outbuf;
@@ -2123,12 +2177,14 @@
GSMOPEN_P_LOG, iconv_res, inbuf, (int) inbytesleft, outbuf, (int) outbytesleft, converted, utf8_out);
iconv_close(iconv_format);
+#endif //WIN32
return 0;
}
int iso_8859_1_to_utf8(private_t * tech_pvt, char *iso_8859_1_in, char *utf8_out, size_t outbytesleft)
{
char converted[16000];
+#ifndef WIN32
iconv_t iconv_format;
int iconv_res;
char *outbuf;
@@ -2169,6 +2225,7 @@
iconv_close(iconv_format);
+#endif //WIN32
return 0;
}
@@ -2176,6 +2233,7 @@
int utf_to_ucs2(private_t * tech_pvt, char *utf_in, size_t inbytesleft, char *ucs2_out, size_t outbytesleft)
{
/* cicopet */
+#ifndef WIN32
iconv_t iconv_format;
int iconv_res;
char *outbuf;
@@ -2219,6 +2277,7 @@
strncat(ucs2_out, stringa2, ((outbytesleft - strlen(ucs2_out)) - 1)); //add the received line to the buffer
DEBUGA_GSMOPEN("stringa=%s, stringa2=%s, ucs2_out=%s\n", GSMOPEN_P_LOG, stringa, stringa2, ucs2_out);
}
+#endif //WIN32
return 0;
}
@@ -2246,7 +2305,7 @@
tech_pvt->phone_callflow = CALLFLOW_CALL_ACTIVE;
while (tech_pvt->interface_state == GSMOPEN_STATE_RING) {
- usleep(10000); //10msec
+ gsmopen_sleep(10000); //10msec
}
if (tech_pvt->interface_state != GSMOPEN_STATE_UP) {
ERRORA("call answering failed\n", GSMOPEN_P_LOG);
@@ -2292,7 +2351,7 @@
new_inbound_channel(tech_pvt);
- usleep(10000);
+ gsmopen_sleep(10000);
session = switch_core_session_locate(tech_pvt->session_uuid_str);
if (session) {
@@ -2349,7 +2408,7 @@
}
while (tech_pvt->interface_state != GSMOPEN_STATE_DOWN) {
- usleep(10000); //10msec
+ gsmopen_sleep(10000); //10msec
}
if (tech_pvt->interface_state != GSMOPEN_STATE_DOWN) {
ERRORA("call hangup failed\n", GSMOPEN_P_LOG);
@@ -3039,14 +3098,14 @@
}
DEBUGA_GSMOPEN("prepared!\n", GSMOPEN_P_LOG);
}
- usleep(1000);
+ gsmopen_sleep(1000);
error = snd_pcm_start(tech_pvt->alsac);
if (error) {
ERRORA("snd_pcm_start failed, %s\n", GSMOPEN_P_LOG, snd_strerror(error));
return r;
}
DEBUGA_GSMOPEN("started!\n", GSMOPEN_P_LOG);
- usleep(1000);
+ gsmopen_sleep(1000);
}
buf = __buf + AST_FRIENDLY_OFFSET / 2;
@@ -3079,7 +3138,7 @@
} else if (r == -EAGAIN) {
DEBUGA_GSMOPEN("ALSA read -EAGAIN, the soundcard is not ready to be read by gsmopen\n", GSMOPEN_P_LOG);
while (r == -EAGAIN) {
- usleep(1000);
+ gsmopen_sleep(1000);
if (tech_pvt->alsa_capture_is_mono) {
r = snd_pcm_readi(tech_pvt->alsac, buf + readpos, left);
@@ -3248,7 +3307,7 @@
}
}
uscita:
- usleep(1000);
+ gsmopen_sleep(1000);
if (tech_pvt->at_cmgw[0]) {
@@ -3268,7 +3327,7 @@
tech_pvt->at_cmgw[0] = '\0';
}
- //usleep(500000); //.5 secs
+ //gsmopen_sleep(500000); //.5 secs
UNLOCKA(tech_pvt->controldev_lock);
POPPA_UNLOCKA(&tech_pvt->controldev_lock);
}
@@ -3313,7 +3372,11 @@
WARNINGA("boost %s too large, using %d\n", GSMOPEN_P_LOG, s, BOOST_MAX);
*boost = BOOST_MAX;
}
+#ifdef WIN32
+ *boost = exp(log ((double)10) * *boost / 20) * BOOST_SCALE;
+#else
*boost = exp(log(10) * *boost / 20) * BOOST_SCALE;
+#endif //WIN32
if (option_debug > 1)
DEBUGA_GSMOPEN("setting boost %s to %f\n", GSMOPEN_P_LOG, s, *boost);
}
@@ -3362,14 +3425,14 @@
if (res) {
ERRORA("AT was not acknowledged, continuing but maybe there is a problem\n", GSMOPEN_P_LOG);
}
- usleep(1000);
+ gsmopen_sleep(1000);
if (strlen(p->at_query_battchg)) {
res = gsmopen_serial_write_AT_expect(p, p->at_query_battchg, p->at_query_battchg_expect);
if (res) {
WARNINGA("%s does not get %s from the phone. Continuing.\n", GSMOPEN_P_LOG, p->at_query_battchg, p->at_query_battchg_expect);
}
- usleep(1000);
+ gsmopen_sleep(1000);
}
if (strlen(p->at_query_signal)) {
@@ -3377,7 +3440,7 @@
if (res) {
WARNINGA("%s does not get %s from the phone. Continuing.\n", GSMOPEN_P_LOG, p->at_query_signal, p->at_query_signal_expect);
}
- usleep(1000);
+ gsmopen_sleep(1000);
}
//FIXME all the following commands in config!
@@ -3388,7 +3451,7 @@
("%s does not get %s from the phone. If your phone is not Motorola, please contact the gsmopen developers. Else, if your phone IS a Motorola, probably a long msg was incoming and ther first part was read and then deleted. The second part is now orphan. If you got this warning repeatedly, and you cannot correctly receive SMSs from this interface, please manually clean all messages (and the residual parts of them) from the cellphone/SIM. Continuing.\n",
GSMOPEN_P_LOG, "AT+MMGL=\"HEADER ONLY\"", "OK");
} else {
- usleep(1000);
+ gsmopen_sleep(1000);
if (p->unread_sms_msg_id) {
char at_command[256];