@@ -73,7 +73,7 @@ static uint32_t pppos_rx_count = 0;
73
73
static uint32_t pppos_tx_count = 0 ;
74
74
75
75
int gsm_uart_baudrate = 115200 ;
76
- bool gsm_debug = true ;
76
+ bool gsm_debug = false ;
77
77
int gsm_uart_num = -1 ;
78
78
at_responses_t gsm_at_responses = { 0 };
79
79
at_command_t gsm_at_command = { 0 };
@@ -89,6 +89,7 @@ static uint8_t ppp_status = ATDEV_STATEFIRSTINIT;
89
89
static uint32_t gsm_ppp_ip = 0 ;
90
90
static uint32_t gsm_ppp_netmask = 0 ;
91
91
static uint32_t gsm_ppp_gw = 0 ;
92
+ static time_t ntp_got_time = 0 ;
92
93
93
94
static char gsm_connect_string [32 ] = "ATDT*99***1#\r\n" ; // "AT+CGDATA=\"PPP\",1\r\n"
94
95
const char * GSM_PPP_TAG = "[GSM_PPPOS]" ;
@@ -225,18 +226,6 @@ extern handle_t mp_rtc_rtc0;
225
226
226
227
static void * ntp_time_cb = NULL ;
227
228
228
- //==========================
229
- int setNTP_cb (void * cb_func )
230
- {
231
- if (mpy_uarts [gsm_uart_num ].uart_mutex == NULL ) return 0 ;
232
- if (xSemaphoreTake (mpy_uarts [gsm_uart_num ].uart_mutex , PPPOSMUTEX_TIMEOUT ) == pdTRUE ) {
233
- ntp_time_cb = cb_func ;
234
- xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
235
- }
236
- return 1 ;
237
- }
238
-
239
-
240
229
//==============================================
241
230
// Function used by SNTP to synchronize the time
242
231
//==============================================
@@ -246,15 +235,7 @@ void set_rtc_time_from_seconds(time_t seconds)
246
235
tm_info = gmtime (& seconds );
247
236
//rtc_set_datetime(mp_rtc_rtc0, tm_info); // set RTC time
248
237
_set_sys_time (tm_info , 0 );
249
- if (gsm_debug ) {
250
- LOGM ("[NTP]" , "Time synchronized from NTP (%ld)" , seconds );
251
- }
252
- int taken = pdFALSE ;
253
- if (mpy_uarts [gsm_uart_num ].uart_mutex ) taken = xSemaphoreTake (mpy_uarts [gsm_uart_num ].uart_mutex , PPPOSMUTEX_TIMEOUT );
254
- if ((taken ) && (ntp_time_cb )) {
255
- mp_sched_schedule ((mp_obj_t )ntp_time_cb , mp_obj_new_int (seconds ));
256
- }
257
- if (taken ) xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
238
+ ntp_got_time = seconds ;
258
239
if (sntp_enabled ()) {
259
240
sntp_stop ();
260
241
}
@@ -515,7 +496,7 @@ static void checkSMS()
515
496
gsm_debug = false;
516
497
sms_timer = mp_hal_ticks_ms () + SMS_check_interval ;
517
498
SMS_indexes indexes ;
518
- int nmsg = checkMessages (SMS_LIST_NEW , 0 , NULL , & indexes , SMS_SORT_NONE );
499
+ int nmsg = checkMessages (SMS_LIST_NEW , 0 , NULL , & indexes , SMS_SORT_ASC );
519
500
if (nmsg > 0 ) {
520
501
if (nmsg > 100 ) nmsg = 100 ;
521
502
// Create a tuple containing SMS indexes
@@ -548,8 +529,8 @@ static int _task_idle()
548
529
}
549
530
550
531
gstat = do_pppos_connect ;
551
- checkSMS ();
552
532
xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
533
+ checkSMS ();
553
534
}
554
535
net_active_interfaces &= ~ACTIVE_INTERFACE_GSM ;
555
536
return gstat ;
@@ -567,6 +548,11 @@ static void _handle_pppos_data(char *data)
567
548
vTaskDelay (5 / portTICK_PERIOD_MS );
568
549
do_check = true;
569
550
}
551
+ if (ntp_got_time > 0 ) {
552
+ if (gsm_debug ) LOGM (GSM_PPP_TAG , "NTP time synchronized (%lu)" , ntp_got_time );
553
+ if (ntp_time_cb ) mp_sched_schedule ((mp_obj_t )ntp_time_cb , mp_obj_new_int (ntp_got_time ));
554
+ ntp_got_time = 0 ;
555
+ }
570
556
571
557
if (do_check ) {
572
558
//if (xSemaphoreTake(mpy_uarts[gsm_uart_num].uart_mutex, PPPOSMUTEX_TIMEOUT) == pdTRUE) {
@@ -1065,7 +1051,7 @@ int gsm_ppposInit(int tx, int rx, int rts, int cts, int bdr, char *user, char *p
1065
1051
strncpy (GSM_APN , apn , PPP_MAX_NAME_LEN );
1066
1052
1067
1053
if (!tcpip_adapter_initialized ) {
1068
- LOGM (GSM_PPP_TAG ,"Network init (from GSM module)" );
1054
+ if ( gsm_debug ) LOGM (GSM_PPP_TAG ,"Network init (from GSM module)" );
1069
1055
network_init ();
1070
1056
tcpip_adapter_initialized = true;
1071
1057
}
@@ -1305,8 +1291,8 @@ static bool is_OnLine()
1305
1291
return ret ;
1306
1292
}
1307
1293
1308
- //---------------------------------------
1309
- time_t sms_time (char * msg_time , int * tz )
1294
+ //-------------------------------------------------
1295
+ static time_t sms_time (char * msg_time , int * tzone )
1310
1296
{
1311
1297
if (strlen (msg_time ) >= 20 ) {
1312
1298
// Convert message time to time structure
@@ -1319,7 +1305,7 @@ time_t sms_time(char * msg_time, int *tz)
1319
1305
tm .tm_year = yy + 100 ;
1320
1306
tm .tm_mon = mn - 1 ;
1321
1307
tm .tm_mday = dd ;
1322
- if (tz ) tz = tz /4 ; // time zone info
1308
+ if (tzone ) * tzone = tz /4 ; // time zone info
1323
1309
return mktime (& tm ); // Linux time
1324
1310
}
1325
1311
return 0 ;
@@ -1439,6 +1425,9 @@ static int getSMSindex(char *msgstart, time_t *msgtime)
1439
1425
//-------------------------------------------------------------------------------------------------
1440
1426
int checkMessages (uint8_t rd_status , int sms_idx , SMS_Msg * msg , SMS_indexes * indexes , uint8_t sort )
1441
1427
{
1428
+ char cmd [32 ] = {0 };
1429
+ char * list_change = "" ;
1430
+ if (msg == NULL ) list_change = ",1" ;
1442
1431
char * rbuffer = pvPortMalloc (1024 );
1443
1432
if (rbuffer == NULL ) {
1444
1433
if (gsm_debug ) {
@@ -1449,36 +1438,61 @@ int checkMessages(uint8_t rd_status, int sms_idx, SMS_Msg *msg, SMS_indexes *ind
1449
1438
memset (rbuffer , 0 , 1024 );
1450
1439
1451
1440
// ** Send command to GSM and get the response
1452
- char * cmd = SMS_LIST_ALL_STR ;
1453
- if (rd_status == SMS_LIST_NEW ) cmd = SMS_LIST_NEW_STR ;
1454
- else if ( rd_status == SMS_LIST_OLD ) cmd = SMS_LIST_OLD_STR ;
1441
+ if ( rd_status == SMS_LIST_NEW ) sprintf ( cmd , "%s%s\r\n" , SMS_LIST_NEW_STR , list_change ) ;
1442
+ else if (rd_status == SMS_LIST_OLD ) sprintf ( cmd , "%s%s\r\n" , SMS_LIST_OLD_STR , list_change ) ;
1443
+ else sprintf ( cmd , "%s%s\r\n" , SMS_LIST_ALL_STR , list_change ) ;
1455
1444
1456
1445
// Read all messages to buffer
1446
+ at_responses_t at_responses1 ;
1447
+ memset (& at_responses1 , 0 , sizeof (at_responses_t ));
1448
+ at_responses1 .nresp = 2 ;
1449
+ at_responses1 .resp [0 ] = AT_OK_Str ;
1450
+ at_responses1 .resp [1 ] = AT_Error_Str ;
1451
+
1457
1452
memset (& gsm_at_responses , 0 , sizeof (at_responses_t ));
1458
- memset (& gsm_at_command , 0 , sizeof (at_command_t ));
1459
1453
gsm_at_responses .nresp = 2 ;
1460
- gsm_at_responses .resp [0 ] = "\r\n\r\nOK" ;
1454
+ gsm_at_responses .resp [0 ] = "\r\n\r\nOK\r\n" ; // wait for end of messages list
1461
1455
gsm_at_responses .resp [1 ] = AT_Error_Str ;
1462
- gsm_at_command .cmd = cmd ;
1463
- gsm_at_command .cmdSize = strlen (cmd );
1464
- gsm_at_command .responses = & gsm_at_responses ;
1465
- gsm_at_command .timeout = 4000 ;
1466
- gsm_at_command .at_uart_num = gsm_uart_num ;
1467
- gsm_at_command .dbg = gsm_debug ;
1468
- gsm_at_command .respbSize = 1024 ;
1469
- gsm_at_command .respbuff = rbuffer ;
1470
- gsm_at_command .flush = true;
1456
+
1457
+ at_commands_t at_commands ;
1458
+ memset (& at_commands , 0 , sizeof (at_commands_t ));
1459
+ at_commands .ncmd = 2 ;
1460
+ at_commands .at_uart_num = gsm_uart_num ;
1461
+ at_commands .dbg = gsm_debug ;
1462
+ at_commands .expect_resp [0 ] = 1 ;
1463
+ at_commands .expect_resp [1 ] = 1 ;
1464
+
1465
+ at_commands .commands [0 ].cmd = "AT+CMGF=1\r\n" ;
1466
+ at_commands .commands [0 ].cmdSize = -1 ;
1467
+ at_commands .commands [0 ].responses = & at_responses1 ;
1468
+ at_commands .commands [0 ].timeout = 100 ;
1469
+ at_commands .commands [0 ].at_uart_num = gsm_uart_num ;
1470
+ at_commands .commands [0 ].dbg = gsm_debug ;
1471
+ at_commands .commands [0 ].flush = true;
1472
+
1473
+ at_commands .commands [1 ].cmd = cmd ;
1474
+ at_commands .commands [1 ].cmdSize = strlen (cmd );
1475
+ at_commands .commands [1 ].responses = & gsm_at_responses ;
1476
+ at_commands .commands [1 ].timeout = 2000 ;
1477
+ at_commands .commands [1 ].at_uart_num = gsm_uart_num ;
1478
+ at_commands .commands [1 ].dbg = gsm_debug ;
1479
+ at_commands .commands [1 ].respbSize = 1024 ;
1480
+ at_commands .commands [1 ].respbuff = rbuffer ;
1481
+ at_commands .commands [1 ].flush = true;
1471
1482
1472
1483
int buflen = 0 ;
1484
+ int res , n_proc = 0 ;
1485
+ int msgidx = 0 ;
1486
+ time_t msgtime = 0 ;
1473
1487
1474
1488
// Execute command
1475
- int res = gsm_at_Cmd ( & gsm_at_command );
1489
+ res = gsm_at_Commands ( & at_commands , & n_proc );
1476
1490
1477
- if (res == 1 ) buflen = strlen (gsm_at_command .respbuff );
1491
+ if (( n_proc == 2 ) && ( res == 1 )) buflen = strlen (at_commands . commands [ 1 ] .respbuff );
1478
1492
1479
1493
if (indexes != NULL ) memset (indexes , 0 , sizeof (SMS_indexes ));
1480
1494
// The buffer may have been expanded
1481
- rbuffer = gsm_at_command .respbuff ;
1495
+ rbuffer = at_commands . commands [ 1 ] .respbuff ;
1482
1496
1483
1497
// Parse the response
1484
1498
int nmsg = 0 ;
@@ -1498,13 +1512,15 @@ int checkMessages(uint8_t rd_status, int sms_idx, SMS_Msg *msg, SMS_indexes *ind
1498
1512
}
1499
1513
}
1500
1514
if ((msgstart ) && (msgend_h ) && (msgend_t )) {
1501
- // We have at least one the whole message in the buffer
1515
+ // We have at least one whole message in the buffer
1502
1516
nmsg ++ ;
1517
+ msgidx = getSMSindex (msgstart + 7 , & msgtime );
1503
1518
if ((indexes != NULL ) && (nmsg < SMS_MAX_MESSAGES )) {
1504
1519
// Only save message index and time
1505
- indexes -> idx [nmsg - 1 ] = getSMSindex (msgstart + 7 , & indexes -> time [nmsg - 1 ]);
1520
+ indexes -> idx [nmsg - 1 ] = msgidx ;
1521
+ indexes -> time [nmsg - 1 ] = msgtime ;
1506
1522
}
1507
- if ((sms_idx == ( nmsg - 1 ) ) && (msg != NULL )) {
1523
+ if ((sms_idx == msgidx ) && (msg != NULL )) {
1508
1524
// Get the message text if requested, save message info and text
1509
1525
getSMS (msgstart + 7 , msg , 1 );
1510
1526
idx_found = 1 ;
@@ -1608,7 +1624,7 @@ int smsSend(char *smsnum, char *msg)
1608
1624
at_commands .commands [1 ].dbg = gsm_debug ;
1609
1625
at_commands .commands [1 ].flush = true;
1610
1626
1611
- res = gsm_at_Commands (& at_commands );
1627
+ res = gsm_at_Commands (& at_commands , NULL );
1612
1628
if (res != 1 ) {
1613
1629
// Try to recover
1614
1630
at_uart_write (gsm_uart_num , (uint8_t * )"\x1B" , 1 );
@@ -1715,25 +1731,43 @@ bool gsm_set_baudrate(int bdr, bool perm)
1715
1731
}
1716
1732
1717
1733
1734
+ //==========================
1735
+ int setNTP_cb (void * cb_func )
1736
+ {
1737
+ int stat = ppposStatus (NULL , NULL , NULL );
1738
+ int taken = pdFALSE ;
1739
+ if ((stat != ATDEV_STATEFIRSTINIT ) && (mpy_uarts [gsm_uart_num ].uart_mutex != NULL ))
1740
+ taken = xSemaphoreTake (mpy_uarts [gsm_uart_num ].uart_mutex , PPPOSMUTEX_TIMEOUT * 5 );
1741
+
1742
+ ntp_time_cb = cb_func ;
1743
+
1744
+ if (taken ) xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
1745
+ return 1 ;
1746
+ }
1747
+
1718
1748
//=============================================
1719
1749
int setSMS_cb (void * cb_func , uint32_t interval )
1720
1750
{
1721
- if (mpy_uarts [gsm_uart_num ].uart_mutex == NULL ) return 0 ;
1722
- if (xSemaphoreTake (mpy_uarts [gsm_uart_num ].uart_mutex , PPPOSMUTEX_TIMEOUT * 5 ) != pdTRUE ) return 0 ;
1751
+ int stat = ppposStatus (NULL , NULL , NULL );
1752
+ int taken = pdFALSE ;
1753
+ if ((stat != ATDEV_STATEFIRSTINIT ) && (mpy_uarts [gsm_uart_num ].uart_mutex != NULL ))
1754
+ taken = xSemaphoreTake (mpy_uarts [gsm_uart_num ].uart_mutex , PPPOSMUTEX_TIMEOUT * 5 );
1723
1755
1724
1756
new_SMS_cb = cb_func ;
1725
1757
SMS_check_interval = interval ;
1726
1758
sms_timer = mp_hal_ticks_ms () + SMS_check_interval ;
1759
+ doCheckSMS = 1 ;
1727
1760
1728
- xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
1761
+ if ( taken ) xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
1729
1762
return 1 ;
1730
1763
}
1731
1764
1732
1765
//=========================
1733
1766
void gsm_setDebug (bool dbg )
1734
1767
{
1768
+ int stat = ppposStatus (NULL , NULL , NULL );
1735
1769
int taken = pdFALSE ;
1736
- if (mpy_uarts [gsm_uart_num ].uart_mutex != NULL ) taken = xSemaphoreTake (mpy_uarts [gsm_uart_num ].uart_mutex , PPPOSMUTEX_TIMEOUT );
1770
+ if (( stat != ATDEV_STATEFIRSTINIT ) && ( mpy_uarts [gsm_uart_num ].uart_mutex != NULL ) ) taken = xSemaphoreTake (mpy_uarts [gsm_uart_num ].uart_mutex , PPPOSMUTEX_TIMEOUT );
1737
1771
gsm_debug = dbg ;
1738
1772
if (taken ) xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
1739
1773
}
@@ -1755,8 +1789,8 @@ int gsm_at_Cmd(at_command_t *command)
1755
1789
return res ;
1756
1790
}
1757
1791
1758
- //==========================================
1759
- int gsm_at_Commands (at_commands_t * commands )
1792
+ //==========================================================
1793
+ int gsm_at_Commands (at_commands_t * commands , int * processed )
1760
1794
{
1761
1795
if (ppposStatus (NULL , NULL , NULL ) != ATDEV_STATEIDLE ) return 0 ;
1762
1796
@@ -1766,8 +1800,7 @@ int gsm_at_Commands(at_commands_t *commands)
1766
1800
}
1767
1801
return 0 ;
1768
1802
}
1769
- int n_proc = 0 ;
1770
- int res = at_Commands (commands , & n_proc );
1803
+ int res = at_Commands (commands , processed );
1771
1804
xSemaphoreGive (mpy_uarts [gsm_uart_num ].uart_mutex );
1772
1805
1773
1806
return res ;
0 commit comments