summaryrefslogtreecommitdiff
path: root/libgammu/protocol/s60/s60.c
blob: 3b85664036150bc1c420bdbe09ad91330b3408fc (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
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
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Copyright (c) 2011 Michal Cihar <michal@cihar.com>
 */


#include "../../gsmstate.h"

#if defined(GSM_ENABLE_S60)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "../../gsmcomon.h"
#include "s60.h"
#include "s60-ids.h"

static GSM_Error S60_WriteMessage (GSM_StateMachine *s, unsigned const char *MsgBuffer,
				    int MsgLength, int MsgType)
{
	unsigned char	*buffer=NULL;
	int pos, sent, length, buflen, bufpos;
	GSM_Error error;

	/* No type */
	if (MsgType == 0) {
		return ERR_NONE;
	}

	/* Allocate buffer for composing message */
	buflen = MIN(MAX_LENGTH, MsgLength) + 10;
	buffer = (unsigned char *)malloc(buflen);
	if (buffer == NULL) {
		return ERR_MOREMEMORY;
	}

	/* Send message parts */
	for (pos = 0; MsgLength - pos > MAX_LENGTH; pos += MAX_LENGTH) {
		error = S60_WriteMessage(s, MsgBuffer + pos, MAX_LENGTH, NUM_PARTIAL_MESSAGE);
		if (error != ERR_NONE) {
			free(buffer);
			return ERR_DEVICEWRITEERROR;
		}
	}

	/* Send final message */
	buffer[0] = MsgType;
	length = MsgLength - pos;

	bufpos = snprintf(buffer, buflen, "%d%c", MsgType, NUM_END_HEADER);
	memcpy(buffer + bufpos, MsgBuffer + pos, length);
	buffer[bufpos + length] = '\n';
	length += bufpos + 1;

	/* Debugging */
	GSM_DumpMessageLevel3(s, buffer, length, MsgType);
	GSM_DumpMessageLevel2(s, buffer, length, MsgType);

	sent = s->Device.Functions->WriteDevice(s, buffer, length);
	free(buffer);
	if (sent != length) {
		return ERR_DEVICEWRITEERROR;
	}

	return ERR_NONE;
}

static GSM_Error S60_StateMachine(GSM_StateMachine *s, unsigned char rxchar)
{
	GSM_Protocol_S60Data *d = &s->Protocol.Data.S60;

	/* Did we complete part of packet? */
	switch (d->State) {
		case S60_Header:
			if (rxchar == NUM_END_HEADER) {
				d->Msg.Type = atoi(d->idbuffer);
				d->State = S60_Data;
				d->idpos = 0;
			} else {
				d->idbuffer[d->idpos++] = rxchar;
				d->idbuffer[d->idpos] = 0;
			}
			break;
		case S60_Data:
			if (rxchar == NUM_END_TEXT) {
				d->State = S60_Header;
				/* Should we wait for other parts? */
				if (d->Msg.Type == NUM_PARTIAL_MESSAGE) {
					return ERR_NONE;
				}

				/* We've got data to process */
				s->Phone.Data.RequestMsg = &d->Msg;
				s->Phone.Data.DispatchError = s->Phone.Functions->DispatchMessage(s);

				/* Reset message length */
				d->Msg.Length = 0;
			} else {
				/* Allocate buffer */
				if (d->Msg.BufferUsed < d->Msg.Length + 2) {
					d->Msg.BufferUsed = d->Msg.Length + 2;
					d->Msg.Buffer = (unsigned char *)realloc(d->Msg.Buffer, d->Msg.BufferUsed);
					if (d->Msg.Buffer == NULL) {
						return ERR_MOREMEMORY;
					}
				}

				/* Store received byte */
				d->Msg.Buffer[d->Msg.Length++] = rxchar;
				d->Msg.Buffer[d->Msg.Length] = 0;
			}
			break;
	}

	return ERR_NONE;
}

static GSM_Error S60_Initialise(GSM_StateMachine *s)
{
	GSM_Protocol_S60Data *d = &s->Protocol.Data.S60;

	d->Msg.BufferUsed	= 0;
	d->Msg.Buffer 		= NULL;
	d->Msg.Length		= 0;
	d->State = S60_Header;
	d->idpos = 0;

	return ERR_NONE;
}

static GSM_Error S60_Terminate(GSM_StateMachine *s)
{
	free(s->Protocol.Data.S60.Msg.Buffer);
	s->Protocol.Data.S60.Msg.Buffer = NULL;

	return ERR_NONE;
}

GSM_Protocol_Functions S60Protocol = {
	S60_WriteMessage,
	S60_StateMachine,
	S60_Initialise,
	S60_Terminate
};

#endif


/* How should editor hadle tabs in this file? Add editor commands here.
 * vim: noexpandtab sw=8 ts=8 sts=8:
 */