Translation update done using Pootle.
[gammu.git] / tests / sms-at-encode.c
blobda9131fcfe79a720b0df5151a92cc44525430484
1 /* Test for encoding SMS using AT driver */
3 #include <gammu.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include "../libgammu/protocol/protocol.h" /* Needed for GSM_Protocol_Message */
8 #include "../libgammu/gsmstate.h" /* Needed for state machine internals */
9 #include "../libgammu/gsmphones.h" /* Phone data */
11 #include "common.h"
13 extern GSM_Error ATGEN_MakeSMSFrame(GSM_StateMachine * s, GSM_SMSMessage * message, unsigned char *hexreq, int *current, int *length2);
15 #define BUFFER_SIZE 16384
17 int main(int argc, char **argv)
19 GSM_Debug_Info *debug_info;
20 GSM_Phone_ATGENData *Priv;
21 GSM_Phone_Data *Data;
22 unsigned char dumpbuffer[BUFFER_SIZE];
23 FILE *f;
24 size_t len;
25 GSM_StateMachine *s;
26 GSM_Error error;
27 int current, current2;
28 unsigned char hexreq[1000];
29 GSM_SMS_Backup Backup;
30 gboolean generate = FALSE;
32 /* Enable debugging */
33 debug_info = GSM_GetGlobalDebug();
34 GSM_SetDebugFileDescriptor(stderr, FALSE, debug_info);
35 GSM_SetDebugLevel("textall", debug_info);
37 /* Check parameters */
38 if (argc != 3 && argc != 4) {
39 printf("Not enough parameters!\nUsage: sms-at-encode message.backup message.dump\n");
40 return 1;
43 /* Check for generating option */
44 if (argc == 4 && strcmp(argv[3], "generate") == 0) {
45 generate = TRUE;
48 /* Read message */
49 error = GSM_ReadSMSBackupFile(argv[1], &Backup);
50 gammu_test_result(error, "GSM_ReadSMSBackupFile");
52 if (!generate) {
53 /* Open file */
54 f = fopen(argv[2], "r");
55 if (f == NULL) {
56 printf("Could not open %s\n", argv[2]);
57 return 1;
60 /* Read data */
61 len = fread(dumpbuffer, 1, sizeof(dumpbuffer) - 1, f);
62 if (!feof(f)) {
63 printf("Could not read whole file %s\n", argv[2]);
64 fclose(f);
65 return 1;
68 /* Zero terminate data */
69 dumpbuffer[len] = 0;
71 /* Close file */
72 fclose(f);
75 /* Allocates state machine */
76 s = GSM_AllocStateMachine();
77 if (s == NULL) {
78 printf("Could not allocate state machine!\n");
79 return 1;
81 debug_info = GSM_GetDebug(s);
82 GSM_SetDebugGlobal(TRUE, debug_info);
83 GSM_SetDebugFileDescriptor(stderr, FALSE, debug_info);
84 GSM_SetDebugLevel("textall", debug_info);
86 /* Initialize AT engine */
87 Data = &s->Phone.Data;
88 Data->ModelInfo = GetModelData(NULL, NULL, "unknown", NULL);
89 Priv = &s->Phone.Data.Priv.ATGEN;
90 Priv->ReplyState = AT_Reply_OK;
91 Priv->SMSMode = SMS_AT_PDU;
92 Priv->PhoneSMSMemory = AT_AVAILABLE;
93 Priv->SIMSMSMemory = AT_AVAILABLE;
95 /* Format SMS frame */
96 error = ATGEN_MakeSMSFrame(s, Backup.SMS[0], hexreq, &current, &current2);
97 gammu_test_result(error, "ATGEN_MakeSMSFrame");
99 /* We don't need this anymore */
100 GSM_FreeSMSBackup(&Backup);
102 /* Display message */
103 if (generate) {
104 /* Open file */
105 f = fopen(argv[2], "w");
106 if (f == NULL) {
107 printf("Could not open %s\n", argv[2]);
108 return 1;
111 /* Read data */
112 len = fwrite(hexreq, 1, strlen(hexreq), f);
113 if (len != strlen(hexreq)) {
114 printf("Could not save %s\n", argv[2]);
115 return 1;
118 /* Close file */
119 fclose(f);
120 } else {
121 if (strcmp(hexreq, dumpbuffer) != 0) {
122 printf("Encoded does not match with template!\n");
123 printf("Encoded: %s\n", hexreq);
124 printf("Template: %s\n", dumpbuffer);
125 return 1;
129 /* Free state machine */
130 GSM_FreeStateMachine(s);
132 return 0;
135 /* Editor configuration
136 * vim: noexpandtab sw=8 ts=8 sts=8 tw=72: