Repository navigation
Expand file tree
/
Copy pathmobile_seaplus_driver.c
More file actions
1923 lines (1286 loc) · 50.1 KB
/
Copy pathmobile_seaplus_driver.c
File metadata and controls
1923 lines (1286 loc) · 50.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2018-2026 Olivier Boudeville
*
* This file is part of the Ceylan-Mobile library.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License or
* the GNU General Public License, as they are published by the Free Software
* Foundation, either version 3 of these Licenses, or (at your option)
* any later version.
* You can also redistribute it and/or modify it under the terms of the
* Mozilla Public License, version 1.1 or later.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License and the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License, of the GNU General Public License and of the Mozilla Public License
* along with this library.
* If not, see <http://www.gnu.org/licenses/> and
* <http://www.mozilla.org/MPL/>.
*
* Author: Olivier Boudeville [olivier (dot) boudeville (at) esperide (dot) com]
* Creation date: Sunday, December 16, 2018.
*
*/
/*
* C Seaplus driver in charge of converting, for each function exposed by the
* 'mobile' (Ceylan-Mobile) service API, the Erlang parameters received from the
* port into C variables that can be passed to the service functions (provided
* by libgammu), and to perform the reciprocal operation on their results, so
* that they can be sent back to the Erlang calling side.
*
*/
// Generated by the Seaplus parse transform, based on mobile.erl:
#include "mobile_seaplus_api_mapping.h"
// Generic helpers to facilitate the writing of this C part of the bridge:
#include "seaplus.h"
// To access to the actual C implementation of the Gammu library:
#include "gammu.h"
// For signal:
#include <signal.h>
// For strlen:
#include <string.h>
// For va_start and friends:
#include <stdarg.h>
// For free:
#include <stdlib.h>
// For printf:
#include <stdio.h>
/* Apparently, when the callback set in SetSendSMSStatusCallback/3 is triggered,
no piece of information allows to relate a specific SMS that was sent to that
callback (message reference is set by the carrier in the PDU that is received
back).
So, if sending a series of SMS in a row, we can only suppose that the
callbacks triggered will be in-order (first callback corresponding to first
SMS sent, etc.).
Anyway, even with the dummy device, if performing two sendings in a row, the
callback for the first will be triggered before the second is sent, so no
problematic interleaving is expected to happen (hopefully!).
*/
/* The reference onto a SMS is supposed to be an unsigned char, but the
* signature of the callback tells otherwise:
*
*/
typedef int sms_tpmr ;
// Matches the encoding_table() in mobile_encoding_key:
enum encoding { unicode_uncompressed=1,
unicode_compressed,
gsm_uncompressed,
gsm_compressed,
eight_bit } ;
// Forward declarations:
void start_gammu( GSM_StateMachine * gammu_fsm ) ;
void send_regular_sms( input_buffer read_buff, buffer_index * index,
GSM_StateMachine * gammu_fsm ) ;
void send_multipart_sms( input_buffer read_buf, buffer_index * index,
GSM_StateMachine * gammu_fsm ) ;
void read_all_sms( input_buffer read_buf, buffer_index * index,
GSM_StateMachine * gammu_fsm ) ;
void delete_all_sms( unsigned int * deletion_count, unsigned int * error_count,
input_buffer read_buf, buffer_index * index, GSM_StateMachine * gammu_fsm ) ;
GSM_Coding_Type get_gammu_encoding( enum encoding e ) ;
enum encoding get_mobile_encoding( GSM_Coding_Type e ) ;
void check_gammu_error( GSM_Error error, const char * step_description,
GSM_StateMachine * gammu_fsm ) ;
void raise_gammu_error( GSM_StateMachine * gammu_fsm,
const char * format, ... ) ;
void stop_gammu( GSM_StateMachine * gammu_fsm ) ;
// Defined and used by Seaplus:
extern FILE * log_file ;
/* If wanting to enable global debugging of Gammu in Seaplus logs (warning: very
* verbose):
*
*/
#ifdef mobile_debug_mode_for_gammu
bool enable_gammu_logging = true ;
#else // mobile_debug_mode_for_gammu
bool enable_gammu_logging = false ;
#endif // mobile_debug_mode_for_gammu
/* If wanting to enable FSM-level debugging of Gammu in Seaplus logs:
* (even more verbose)
*
*/
bool enable_gammu_state_machine_logging = false ;
// Typically for a sending:
typedef int status ;
// UNIX signal:
typedef int signal_reported ;
typedef unsigned int sms_count ;
volatile bool shutdown_requested = false ;
// Tells whether the last SMS sending succeeded:
volatile status sms_send_status = ERR_NONE ;
GSM_Debug_Info * debug_info ;
GSM_SMSMessage sms ;
GSM_SMSC device_smsc ;
/* The Seaplus-managed main output (smart) buffer, where returned terms will be
* written:
*
*/
output_buffer output_sm_buf ;
/*
* Due to libgammu's mode of operation, an interrupt-specific output (smart)
* buffer is useful, in order not to interfere with the main Seaplus one that
* could be already in use.
*
* We believe that, here, up to one interrupt can be active at any time (not
* multiple ones).
*
* Note that, at least for this case, the main output buffer could be directly
* used here (as either the answer to the Erlang part is directly sent once the
* command is triggered, or when a corresponding interrupt fires - never in both
* cases).
*
*/
output_buffer interrupt_sm_buf ;
// Buffer to store temporary strings (including the text of very long SMS):
char * string_buffer ;
// Poor's man pseudo-mutex (most probably useless by design):
bool interrupt_in_use = false ;
const size_t main_buffer_size = 10000 ;
// Maximum number of SMS read in one operation:
//const sms_count max_sms_read = 500 ;
/* Static variable, whose address will be sent as user data of the next
* callback:
*
*/
static unsigned int message_count = 0 ;
// To report this error only once:
bool empty_entry_on_deletion_found = false ;
/*
* Callback triggered by Gammu after a request to send an SMS was issued.
*
* Returns a relevant term to the Erlang side.
*
*/
void sms_sending_callback( GSM_StateMachine * gammu_fsm, status send_status,
sms_tpmr ref, void * user_data )
{
LOG_DEBUG( "Entering SMS-sending callback." ) ;
/* Not expected to ever happen, as the (Erlang) caller is blocked, waiting for
* an answer not sent yet.
*
*/
if ( interrupt_in_use )
raise_gammu_error( gammu_fsm, "Unexpected nested interrupt" ) ;
interrupt_in_use = true ;
unsigned int * msg_count_ptr = (unsigned int *) user_data;
// Only active in the course of this callback (cleared at its end):
init_output_buffer( &interrupt_sm_buf ) ;
sms_send_status = ERR_NONE ;
/* In case of success, returning {send_success, SMSRef, TotalSMSCount},
* otherwise returning {send_failure, SMSRef, TotalSMSCount}:
*
*/
write_tuple_header_result( &interrupt_sm_buf, 3 ) ;
if ( send_status == 0 )
{
LOG_DEBUG( "Received a success notification regarding the sending of "
"the SMS whose reference is #%i on device %s.", ref,
GSM_GetConfig( gammu_fsm, -1 )->Device ) ;
write_atom_result( &interrupt_sm_buf, "send_success" ) ;
}
else
{
LOG_DEBUG( "Received a failure notification regarding the sending of "
"the SMS whose reference is #%i on device %s.", ref,
GSM_GetConfig( gammu_fsm, -1 )->Device ) ;
write_atom_result( &interrupt_sm_buf, "send_failure" ) ;
}
write_int_result( &interrupt_sm_buf, ref ) ;
write_unsigned_int_result( &interrupt_sm_buf, *msg_count_ptr ) ;
finalize_command_after_writing( &interrupt_sm_buf ) ;
LOG_DEBUG( "Leaving sending callback." ) ;
interrupt_in_use = false ;
}
// Mobile-specific interrupt signal handler.
void mobile_interrupt( signal_reported sign )
{
log_warning( "Signal #%i caught, shutting down Ceylan-Mobile.", sign ) ;
signal( sign, SIG_IGN ) ;
shutdown_requested = true ;
}
int main( int argc, char *argv[] )
{
/* Notably useful to perform a first check that all library dependencies are
* satisfied, and thus that the port executable can be launched at all:
*
*/
if ( argc != 1 )
{
printf( "This is a Ceylan-Seaplus driver generated for service "
"Ceylan-Mobile. It is not meant to be executed by itself, "
"but to be run by the Erlang-based Seaplus integration logic. "
"Exiting now.\n" ) ;
return EXIT_SUCCESS ;
}
// Provided by the Seaplus library:
byte * current_read_buf ;
input_buffer read_buf = ¤t_read_buf ;
// No driver log before this point:
start_seaplus_driver( read_buf ) ;
log_trace( "<Ceylan-Seaplus driver for service Ceylan-Mobile "
"now running>" ) ;
// Gammu uses strings in the local encoding:
GSM_InitLocales( NULL ) ;
// Gammu is state-machine based:
GSM_StateMachine * gammu_fsm = GSM_AllocStateMachine() ;
if ( gammu_fsm == NULL )
raise_gammu_error( gammu_fsm, "Unable to allocate Gammu state machine." ) ;
// Pre-allocations:
string_buffer = malloc( main_buffer_size * sizeof(char) ) ;
if ( string_buffer == NULL )
raise_error( "Allocation of main string buffer failed." ) ;
// Secondary buffer to store temporary strings:
char * aux_string_buffer = malloc( 250 * sizeof(char) ) ;
if ( aux_string_buffer == NULL )
raise_error( "Allocation of auxiliary string buffer failed." ) ;
GSM_Error gammu_error ;
start_gammu( gammu_fsm ) ;
/* Tells whether a command will directly send back to the Erlang part a term
* (i.e. after having performed at least one write_*_result call).
*
* Here it is not always the case as, for example when sending a SMS, the
* operation will be known to be completed only in an asynchronous manner,
* i.e. only from an interrupt handler - which is thus the only part of the
* program able to send back any answer.
*
* As a result, the initiating command will trigger the sending but not write
* anything back, thus it should not finalize that command (as the interrupt
* handler will take care of that).
*
*/
bool answer_sent ;
/* Reads a full command from (receive) buffer, based on its initial length:
*
* (a single term is expected hence read)
*
*/
while ( read_command( read_buf ) > 0 )
{
LOG_TRACE( "New command received." ) ;
// Relevant default, as most operations will send directly an answer:
answer_sent = true ;
// Current index in the input buffer (for decoding purpose):
buffer_index index = 0 ;
/* Will be set to the corresponding Seaplus-defined function identifier
* (e.g. whose value is FOO_1_ID):
*
*/
fun_id current_fun_id ;
/* Will be set to the number of parameters obtained from Erlang for the
* function whose identifier has been transmitted:
*
*/
arity param_count ;
read_function_information( read_buf, &index, ¤t_fun_id,
¶m_count ) ;
LOG_DEBUG( "Function identifier is %u, arity is %u (new index is %u).",
current_fun_id, param_count, index ) ;
prepare_for_command( &output_sm_buf ) ;
// Now, taking care of the corresponding function call:
switch( current_fun_id )
{
case GET_BACKEND_INFORMATION_0_ID:
/* -spec get_backend_information() ->
* { backend_type(), backend_version() }.
*/
LOG_DEBUG( "Executing get_backend_information/0." ) ;
check_arity_is( 0, param_count, GET_BACKEND_INFORMATION_0_ID ) ;
// Returning for example: {gammu, "1.40.0"}:
write_tuple_header_result( &output_sm_buf, 2 ) ;
write_atom_result( &output_sm_buf, "gammu" ) ;
write_string_result( &output_sm_buf, GetGammuVersion() ) ;
break ;
case GET_DEVICE_NAME_0_ID:
// -spec get_device_name() -> device_name().
LOG_DEBUG( "Executing get_device_name/0." ) ;
check_arity_is( 0, param_count, GET_DEVICE_NAME_0_ID ) ;
// Internal field, not owned:
const char * device_name = GSM_GetConfig( gammu_fsm, -1 )->Device ;
write_binary_string_result( &output_sm_buf, device_name ) ;
break ;
case GET_DEVICE_MANUFACTURER_0_ID:
// -spec get_device_manufacturer() -> manufacturer_name().
LOG_DEBUG( "Executing get_device_manufacturer/0." ) ;
check_arity_is( 0, param_count, GET_DEVICE_MANUFACTURER_0_ID ) ;
// Life-cycle of a global string buffer, not to manage here:
gammu_error = GSM_GetManufacturer( gammu_fsm, string_buffer ) ;
check_gammu_error( gammu_error, "get manufacturer", gammu_fsm ) ;
write_binary_string_result( &output_sm_buf, string_buffer ) ;
break ;
case GET_DEVICE_MODEL_0_ID:
// -spec get_device_model() -> model_name().
LOG_DEBUG( "Executing get_device_model/0." ) ;
check_arity_is( 0, param_count, GET_DEVICE_MODEL_0_ID ) ;
// Life-cycle of a global string buffer, not to manage here:
gammu_error = GSM_GetModel( gammu_fsm, string_buffer ) ;
check_gammu_error( gammu_error, "get model", gammu_fsm ) ;
write_binary_string_result( &output_sm_buf, string_buffer ) ;
break ;
case GET_FIRMWARE_INFORMATION_0_ID:
/* -spec get_firmware_information() ->
* { revision_text(), date_text(), revision_number() }.
*/
LOG_DEBUG( "Executing get_firmware_information/0." ) ;
check_arity_is( 0, param_count, GET_FIRMWARE_INFORMATION_0_ID ) ;
double rev_number ;
// Life-cycle of global string buffers, not to manage here:
gammu_error = GSM_GetFirmware( gammu_fsm, string_buffer,
aux_string_buffer, &rev_number ) ;
check_gammu_error( gammu_error, "get firmware information",
gammu_fsm ) ;
write_tuple_header_result( &output_sm_buf, 3 ) ;
write_binary_string_result( &output_sm_buf, string_buffer ) ;
write_binary_string_result( &output_sm_buf, aux_string_buffer ) ;
write_double_result( &output_sm_buf, rev_number ) ;
break ;
case GET_IMEI_CODE_0_ID:
// -spec get_imei_code() -> imei().
LOG_DEBUG( "Executing get_imei_code/0." ) ;
check_arity_is( 0, param_count, GET_IMEI_CODE_0_ID ) ;
// Life-cycle of a global string buffer, not to manage here:
gammu_error = GSM_GetIMEI( gammu_fsm, string_buffer ) ;
check_gammu_error( gammu_error, "get IMEI code", gammu_fsm ) ;
write_binary_string_result( &output_sm_buf, string_buffer ) ;
break ;
case GET_HARDWARE_INFORMATION_0_ID:
// -spec get_hardware_information() -> hardware_info().
LOG_DEBUG( "Executing get_hardware_information/0." ) ;
check_arity_is( 0, param_count, GET_HARDWARE_INFORMATION_0_ID ) ;
// Life-cycle of a global string buffer, not to manage here:
gammu_error = GSM_GetHardware( gammu_fsm, string_buffer ) ;
if ( gammu_error == ERR_NONE )
{
write_binary_string_result( &output_sm_buf, string_buffer ) ;
}
else
{
/*
* Returning a string instead (convention here is that an exception is
* thrown by mobile.erl should a non-binary result be received):
*
*/
int res = sprintf( string_buffer, "Gammu error: %s",
GSM_ErrorString( gammu_error ) ) ;
if ( res < 0 )
raise_gammu_error( gammu_fsm, "Error reporting failed" ) ;
write_binary_string_result( &output_sm_buf, string_buffer ) ;
}
break ;
case GET_IMSI_CODE_0_ID:
// -spec get_imsi_code() -> imsi_code().
LOG_DEBUG( "Executing get_imsi_code/0." ) ;
check_arity_is( 0, param_count, GET_IMSI_CODE_0_ID ) ;
// Life-cycle of a global string buffer, not to manage here:
gammu_error = GSM_GetSIMIMSI( gammu_fsm, string_buffer ) ;
check_gammu_error( gammu_error, "get IMSI code", gammu_fsm ) ;
write_binary_string_result( &output_sm_buf, string_buffer ) ;
break ;
case GET_SIGNAL_QUALITY_0_ID:
/* -spec get_signal_quality() ->
* { signal_strength(), signal_strength_percent(), error_rate() }.
*/
LOG_DEBUG( "Executing get_signal_quality/0." ) ;
check_arity_is( 0, param_count, GET_SIGNAL_QUALITY_0_ID ) ;
GSM_SignalQuality sq ;
gammu_error = GSM_GetSignalQuality( gammu_fsm, &sq ) ;
check_gammu_error( gammu_error, "get signal quality", gammu_fsm ) ;
write_tuple_header_result( &output_sm_buf, 3 ) ;
write_int_result( &output_sm_buf, sq.SignalStrength ) ;
write_int_result( &output_sm_buf, sq.SignalPercent ) ;
write_int_result( &output_sm_buf, sq.BitErrorRate ) ;
LOG_DEBUG( "get_signal_quality/0 executed." ) ;
break ;
case SEND_REGULAR_SMS_2_ID:
/* No SEND_REGULAR_SMS_2_ID case: the Erlang part is to trigger only the
* SEND_REGULAR_SMS_4_ID version ultimately.
*
*/
raise_error( "Unexpected call to driver-level send_regular_sms/2." ) ;
break ;
case SEND_REGULAR_SMS_3_ID:
/* No SEND_REGULAR_SMS_3_ID case: the Erlang part is to trigger only the
* SEND_REGULAR_SMS_4_ID version ultimately.
*
*/
raise_error( "Unexpected call to driver-level send_regular_sms/3." ) ;
break ;
case SEND_REGULAR_SMS_4_ID:
/* -spec send_regular_sms( message(), recipient_number(), class(),
* encoding() ) -> sms_id().
*
*/
LOG_DEBUG( "Executing send_regular_sms/4." ) ;
check_arity_is( 4, param_count, SEND_REGULAR_SMS_4_ID ) ;
send_regular_sms( read_buf, &index, gammu_fsm ) ;
// Sending operation to be finalized by the interrupt handler:
answer_sent = false ;
break ;
case SEND_MULTIPART_SMS_2_ID:
/* No SEND_MULTIPART_SMS_2_ID case: the Erlang part is to trigger only
* the SEND_MULTIPART_SMS_4_ID version ultimately.
*
*/
raise_error( "Unexpected call to driver-level send_multipart_sms/2." ) ;
break ;
case SEND_MULTIPART_SMS_3_ID:
/* No SEND_MULTIPART_SMS_3_ID case: the Erlang part is to trigger only
* the SEND_MULTIPART_SMS_4_ID version ultimately.
*
*/
raise_error( "Unexpected call to driver-level send_multipart_sms/3." ) ;
break ;
case SEND_MULTIPART_SMS_4_ID:
/* -spec send_multipart_sms( message(), recipient_number(),
* class(), encoding() ) -> sms_id().
*
*/
LOG_DEBUG( "Executing send_multipart_sms/4." ) ;
check_arity_is( 4, param_count, SEND_MULTIPART_SMS_4_ID ) ;
send_multipart_sms( read_buf, &index, gammu_fsm ) ;
// Sending operation to be finalised by the interrupt handler:
answer_sent = false ;
break ;
case SEND_SMS_2_ID:
/* No SEND_SMS_2_ID case: the Erlang part is to select the right version
* among the SEND_*_SMS_4_ID.
*
*/
raise_error( "Unexpected call to driver-level send_sms/2." ) ;
break ;
case SEND_SMS_3_ID:
/* No SEND_SMS_3_ID case: the Erlang part is to select the right version
* among the SEND_*_SMS_4_ID ultimately.
*
*/
raise_error( "Unexpected call to driver-level send_sms/3." ) ;
break ;
case READ_ALL_SMS_1_ID:
/* -spec read_all_sms( boolean() ) -> [mobile:received_sms()].
*
*/
LOG_DEBUG( "Executing read_all_sms/1." ) ;
check_arity_is( 1, param_count, READ_ALL_SMS_1_ID ) ;
read_all_sms( read_buf, &index, gammu_fsm ) ;
LOG_DEBUG( "read_all_sms/1 executed." ) ;
break ;
case DELETE_ALL_SMS_0_ID:
/* -spec delete_all_sms() ->
* { DeletionCount :: count(), ErrorCount :: count() }.
*
*/
LOG_DEBUG( "Executing delete_all_sms/0." ) ;
check_arity_is( 0, param_count, DELETE_ALL_SMS_0_ID ) ;
unsigned int deletion_count, error_count ;
delete_all_sms( &deletion_count, &error_count, read_buf,
&index, gammu_fsm ) ;
LOG_DEBUG( "delete_all_sms/0 executed." ) ;
write_tuple_header_result( &output_sm_buf, 2 ) ;
write_unsigned_int_result( &output_sm_buf, deletion_count ) ;
write_unsigned_int_result( &output_sm_buf, error_count ) ;
break ;
default:
// Hopefully no 'break' has been forgotten above!
raise_gammu_error( gammu_fsm, "Unknown function identifier: %u",
current_fun_id ) ;
}
if ( answer_sent )
finalize_command_after_writing( &output_sm_buf ) ;
}
LOG_DEBUG( "Empty read, no more SMS to read." ) ;
// output_sm_buf internally already freed appropriately.
free( string_buffer ) ;
string_buffer = NULL ;
free( aux_string_buffer ) ;
aux_string_buffer = NULL ;
stop_gammu( gammu_fsm ) ;
stop_seaplus_driver( read_buf ) ;
}
void start_gammu( GSM_StateMachine * gammu_fsm )
{
log_trace( "Starting Gammu." ) ;
// Registering our signal handler:
signal( SIGINT, mobile_interrupt ) ;
signal( SIGTERM, mobile_interrupt) ;
FILE * debug_file = NULL ;
if ( enable_gammu_logging )
{
if ( log_file != NULL )
{
log_debug( "Directing Gammu logs to Seaplus ones." ) ;
debug_file = log_file ;
}
else
{
log_debug( "Gammu logs directed to standard error." ) ;
debug_file = stderr ;
}
// Internal pointer, not to be deallocated afterwards:
debug_info = GSM_GetGlobalDebug() ;
GSM_SetDebugFileDescriptor( debug_file, false, debug_info ) ;
GSM_SetDebugLevel( "textall", debug_info ) ;
}
else
{
log_debug( "No Gammu logs requested." ) ;
}
if ( enable_gammu_state_machine_logging )
{
if ( log_file != NULL )
{
log_debug( "Directing Gammu state machine logs to Seaplus ones." ) ;
debug_file = log_file ;
}
else
{
log_debug( "Gammu state machine logs directed to standard error." ) ;
debug_file = stderr ;
}
debug_info = GSM_GetDebug( gammu_fsm ) ;
GSM_SetDebugGlobal( false, debug_info ) ;
GSM_SetDebugFileDescriptor( debug_file, false, debug_info ) ;
GSM_SetDebugLevel( "textall", debug_info ) ;
}
else
{
log_debug( "No Gammu state machine logs requested." ) ;
}
INI_Section * iniConfig ;
// Autodetect the configuration file (e.g. ~/.gammurc):
GSM_Error error = GSM_FindGammuRC( &iniConfig, NULL ) ;
check_gammu_error( error, "find Gammu RC", gammu_fsm ) ;
// Read it:
int read_section_count = 0 ;
// To be populated from INI content (and deallocated just after):
GSM_Config * config = GSM_GetConfig( gammu_fsm, read_section_count ) ;
check_gammu_error( error, "get configuration", gammu_fsm ) ;
error = GSM_ReadConfig( iniConfig, config, read_section_count ) ;
check_gammu_error( error, "init configuration", gammu_fsm ) ;
INI_Free( iniConfig ) ;
// We care only about the first configuration:
int section_id = 1 ; // perform full initialisation sequence
GSM_SetConfigNum( gammu_fsm, section_id ) ;
check_gammu_error( error, "set configuration number", gammu_fsm ) ;
// Number of replies to wait for:
int reply_count = 1 ; // 3 ;
error = GSM_InitConnection( gammu_fsm, reply_count ) ;
check_gammu_error( error, "init connection", gammu_fsm ) ;
// User data set so that the callback can fetch the SMS counts:
GSM_SetSendSMSStatusCallback( gammu_fsm, sms_sending_callback,
&message_count ) ;
// We need to know the SMSC number:
device_smsc.Location = 1 ;
error = GSM_GetSMSC( gammu_fsm, &device_smsc ) ;
check_gammu_error( error, "get SMSC", gammu_fsm ) ;
// interrupt_sm_buf managed fully and directly in its callback.
/* Note: a check whether phone needs to enter some PIN could be added, as done
* in gammu/smsd/core.c (SMSD_CheckSecurity/1)
*
*/
}
/* Helpers defined to avoid variable name clashes in the function switch, and to
* factor code:
*
*/
/* Sends a regular (single-part) SMS with the specified class and encoding.
*
* See also https://docs.gammu.org/c/examples.html#sending-sms-message
*
*/
void send_regular_sms( input_buffer read_buf, buffer_index * index,
GSM_StateMachine * gammu_fsm )
{
char * message = read_binary_parameter( read_buf, index ) ;
if ( message == NULL )
raise_gammu_error( gammu_fsm,
"SMS message could not be obtained (regular)." ) ;
/* Clean-up the struct; performs for SMS sendings more than 'memset(&sms, 0,
sizeof(sms);':
*/
GSM_SetDefaultSMSData( &sms ) ;
EncodeUnicode( sms.Text, message, strlen( message ) ) ;
// Message recipient:
char * recipient_number = read_binary_parameter( read_buf, index ) ;
if ( recipient_number == NULL )
raise_gammu_error( gammu_fsm,
"SMS recipient mobile number could not be obtained." ) ;
EncodeUnicode( sms.Number, recipient_number, strlen( recipient_number ) ) ;
// We want to submit message ("SMS for sending or in Outbox"):
sms.PDU = SMS_Submit ;
// No User Data Header, just a plain message:
sms.UDH.Type = UDH_NoUDH ;
sms.Class = read_int_parameter( read_buf, index ) ;
sms.Coding = get_gammu_encoding(
read_int_parameter( read_buf, index ) ) ;
// Sets the SMSC number in message:
CopyUnicodeString( sms.SMSC.Number, device_smsc.Number ) ;
// Resets it first, some phones might give instant response:
sms_send_status = ERR_TIMEOUT ;
/* Set for the upcoming sms_sending_callback call (a single SMS part for
* a regular SMS):
*
*/
message_count = 1 ;
// Finally:
GSM_Error gammu_error = GSM_SendSMS( gammu_fsm, &sms ) ;
check_gammu_error( gammu_error, "send SMS", gammu_fsm ) ;
/* We do not have yet anything to return, but the callback will. */
//write_as_XXX( buffer, ... ) ;
/* However, using real devices (not the dummy one), we see that the callback
* is never triggered unless we poll explicitly from a network reply.
*
* Loops as long as the status is ERR_TIMEOUT:
*
*/
while ( ( ! shutdown_requested ) && ( sms_send_status == ERR_TIMEOUT ) )
{
LOG_DEBUG( "Reading device..." ) ;