1 /* $Id$ */
2 /* Copyright (c) 2014-2017 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Phone */
4 /* This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <time.h>
22 #include "Phone/modem.h"
23 #include "../src/modems/hayes/channel.c"
24 #include "../src/modems/hayes/command.c"
25 #include "../src/modems/hayes/common.c"
26 #include "../src/modems/hayes/pdu.c"
27 #include "../src/modems/hayes/quirks.c"
28 #include "../src/modems/hayes.c"
29 
30 #ifndef PROGNAME
31 # define PROGNAME "pdu"
32 #endif
33 
34 
35 /* pdu */
36 /* prototypes */
37 static int _pdu(void);
38 
39 static int _pdu_decode(char const * pdu, char const * number,
40 		char const * datetime, ModemMessageEncoding encoding,
41 		char const * message);
42 static int _pdu_encode(void);
43 
44 
45 /* functions */
46 /* pdu */
_pdu(void)47 static int _pdu(void)
48 {
49 	int ret = 0;
50 
51 	ret |= (_pdu_decode("07916407058099F9040B916407752743"
52 				"F60000990121017580001554747A0E4A"
53 				"CF416110945805B5CBF379F85C06",
54 				"+46705772346", "12/10/1999 12:57:08",
55 				MODEM_MESSAGE_ENCODING_UTF8,
56 				"This is a PDU message") != 0) ? 1 : 0;
57 	ret |= (_pdu_encode() != 0) ? 2 : 0;
58 	return ret;
59 }
60 
61 
62 /* pdu_decode */
_pdu_decode(char const * pdu,char const * number,char const * datetime,ModemMessageEncoding encoding,char const * message)63 static int _pdu_decode(char const * pdu, char const * number,
64 		char const * datetime, ModemMessageEncoding encoding,
65 		char const * message)
66 {
67 	int ret = 0;
68 	time_t timestamp;
69 	char buf[32];
70 	ModemMessageEncoding e;
71 	char * p;
72 	struct tm t;
73 	size_t len;
74 
75 	if((p = _cmgr_pdu_parse(pdu, &timestamp, buf, &e, &len)) == NULL)
76 	{
77 		fputs(PROGNAME ": Unable to decode PDU\n", stderr);
78 		return -1;
79 	}
80 	/* check the number */
81 	if(strcmp(buf, number) != 0)
82 	{
83 		fputs(PROGNAME ": Did not match the number\n", stderr);
84 		ret = 1;
85 	}
86 	/* check the timestamp */
87 	localtime_r(&timestamp, &t);
88 	strftime(buf, sizeof(buf), "%d/%m/%Y %H:%M:%S", &t);
89 	if(strcmp(buf, datetime) != 0)
90 	{
91 		fprintf(stderr, "%s: %s: %s\n", PROGNAME, buf,
92 				"Did not match the date");
93 		ret = 2;
94 	}
95 	/* check the encoding */
96 	if(e != encoding)
97 	{
98 		fputs(PROGNAME ": Did not match the encoding\n", stderr);
99 		ret = 3;
100 	}
101 	/* check the message */
102 	if(strcmp(p, message) != 0)
103 	{
104 		fputs(PROGNAME ": Did not match the message\n", stderr);
105 		ret = 4;
106 	}
107 	free(p);
108 	return ret;
109 }
110 
111 
112 /* pdu_encode */
_pdu_encode(void)113 static int _pdu_encode(void)
114 {
115 	int ret;
116 	char * pdu;
117 	char const number[] = "1234";
118 	ModemMessageEncoding encoding = MODEM_MESSAGE_ENCODING_ASCII;
119 	char const string[] = "This is just a test.";
120 
121 	if((pdu = hayespdu_encode(number, encoding, sizeof(string) - 1, string,
122 					0)) == NULL)
123 		return -1;
124 	ret = _pdu_decode(pdu, number, NULL, encoding, string);
125 	free(pdu);
126 	return ret;
127 }
128 
129 
130 /* main */
main(void)131 int main(void)
132 {
133 	return (_pdu() << 1);
134 }
135