summaryrefslogtreecommitdiff
path: root/tests/sms-at-encode.c
blob: da9131fcfe79a720b0df5151a92cc44525430484 (plain)
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
/* Test for encoding SMS using AT driver */

#include <gammu.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../libgammu/protocol/protocol.h"	/* Needed for GSM_Protocol_Message */
#include "../libgammu/gsmstate.h"	/* Needed for state machine internals */
#include "../libgammu/gsmphones.h"	/* Phone data */

#include "common.h"

extern GSM_Error ATGEN_MakeSMSFrame(GSM_StateMachine * s, GSM_SMSMessage * message, unsigned char *hexreq, int *current, int *length2);

#define BUFFER_SIZE 16384

int main(int argc, char **argv)
{
	GSM_Debug_Info *debug_info;
	GSM_Phone_ATGENData *Priv;
	GSM_Phone_Data *Data;
	unsigned char dumpbuffer[BUFFER_SIZE];
	FILE *f;
	size_t len;
	GSM_StateMachine *s;
	GSM_Error error;
	int current, current2;
	unsigned char hexreq[1000];
	GSM_SMS_Backup Backup;
	gboolean generate = FALSE;

	/* Enable debugging */
	debug_info = GSM_GetGlobalDebug();
	GSM_SetDebugFileDescriptor(stderr, FALSE, debug_info);
	GSM_SetDebugLevel("textall", debug_info);

	/* Check parameters */
	if (argc != 3 && argc != 4) {
		printf("Not enough parameters!\nUsage: sms-at-encode message.backup message.dump\n");
		return 1;
	}

	/* Check for generating option */
	if (argc == 4 && strcmp(argv[3], "generate") == 0) {
		generate = TRUE;
	}

	/* Read message */
	error = GSM_ReadSMSBackupFile(argv[1], &Backup);
	gammu_test_result(error, "GSM_ReadSMSBackupFile");

	if (!generate) {
		/* Open file */
		f = fopen(argv[2], "r");
		if (f == NULL) {
			printf("Could not open %s\n", argv[2]);
			return 1;
		}

		/* Read data */
		len = fread(dumpbuffer, 1, sizeof(dumpbuffer) - 1, f);
		if (!feof(f)) {
			printf("Could not read whole file %s\n", argv[2]);
			fclose(f);
			return 1;
		}

		/* Zero terminate data */
		dumpbuffer[len] = 0;

		/* Close file */
		fclose(f);
	}

	/* Allocates state machine */
	s = GSM_AllocStateMachine();
	if (s == NULL) {
		printf("Could not allocate state machine!\n");
		return 1;
	}
	debug_info = GSM_GetDebug(s);
	GSM_SetDebugGlobal(TRUE, debug_info);
	GSM_SetDebugFileDescriptor(stderr, FALSE, debug_info);
	GSM_SetDebugLevel("textall", debug_info);

	/* Initialize AT engine */
	Data = &s->Phone.Data;
	Data->ModelInfo = GetModelData(NULL, NULL, "unknown", NULL);
	Priv = &s->Phone.Data.Priv.ATGEN;
	Priv->ReplyState = AT_Reply_OK;
	Priv->SMSMode = SMS_AT_PDU;
	Priv->PhoneSMSMemory = AT_AVAILABLE;
	Priv->SIMSMSMemory = AT_AVAILABLE;

	/* Format SMS frame */
	error = ATGEN_MakeSMSFrame(s, Backup.SMS[0], hexreq, &current, &current2);
	gammu_test_result(error, "ATGEN_MakeSMSFrame");

	/* We don't need this anymore */
	GSM_FreeSMSBackup(&Backup);

	/* Display message */
	if (generate) {
		/* Open file */
		f = fopen(argv[2], "w");
		if (f == NULL) {
			printf("Could not open %s\n", argv[2]);
			return 1;
		}

		/* Read data */
		len = fwrite(hexreq, 1, strlen(hexreq), f);
		if (len != strlen(hexreq)) {
			printf("Could not save %s\n", argv[2]);
			return 1;
		}

		/* Close file */
		fclose(f);
	} else {
		if (strcmp(hexreq, dumpbuffer) != 0) {
			printf("Encoded does not match with template!\n");
			printf("Encoded:  %s\n", hexreq);
			printf("Template: %s\n", dumpbuffer);
			return 1;
		}
	}

	/* Free state machine */
	GSM_FreeStateMachine(s);

	return 0;
}

/* Editor configuration
 * vim: noexpandtab sw=8 ts=8 sts=8 tw=72:
 */