summaryrefslogtreecommitdiffabout
path: root/gammu/emb/common/protocol/nokia/phonet.c
Unidiff
Diffstat (limited to 'gammu/emb/common/protocol/nokia/phonet.c') (more/less context) (ignore whitespace changes)
-rw-r--r--gammu/emb/common/protocol/nokia/phonet.c217
1 files changed, 217 insertions, 0 deletions
diff --git a/gammu/emb/common/protocol/nokia/phonet.c b/gammu/emb/common/protocol/nokia/phonet.c
new file mode 100644
index 0000000..bc5717d
--- a/dev/null
+++ b/gammu/emb/common/protocol/nokia/phonet.c
@@ -0,0 +1,217 @@
1/* (c) 2002-2003 by Marcin Wiacek */
2/* based on some work from Gnokii */
3
4#include "../../gsmstate.h"
5
6#if defined(GSM_ENABLE_IRDA) || defined(GSM_ENABLE_PHONETBLUE) || defined(GSM_ENABLE_BLUEPHONET)
7
8#include <stdio.h>
9#include <string.h>
10
11#include "../../gsmcomon.h"
12#include "phonet.h"
13
14 static GSM_Error PHONET_WriteMessage (GSM_StateMachine *s,
15 unsigned char *MsgBuffer,
16 int MsgLength,
17 unsigned char MsgType)
18{
19 unsigned char *buffer2;
20 int sent;
21
22 GSM_DumpMessageLevel3(s, MsgBuffer, MsgLength, MsgType);
23
24 buffer2 = (unsigned char *)malloc(MsgLength + 6);
25
26 buffer2[0] = PHONET_FRAME_ID,
27 buffer2[1] = PHONET_DEVICE_PHONE; //destination
28 buffer2[2] = PHONET_DEVICE_PC; //source
29
30 if (s->ConnectionType==GCT_PHONETBLUE || s->ConnectionType==GCT_BLUEPHONET) {
31 buffer2[0] = PHONET_BLUE_FRAME_ID;
32 buffer2[1] = PHONET_DEVICE_PHONE;//destination
33 buffer2[2] = PHONET_BLUE_DEVICE_PC;//source
34 }
35
36 buffer2[3] = MsgType;
37 buffer2[4] = MsgLength / 256;
38 buffer2[5] = MsgLength % 256;
39
40 memcpy(buffer2 + 6, MsgBuffer, MsgLength);
41
42 GSM_DumpMessageLevel2(s, buffer2+6, MsgLength, MsgType);
43
44 /* Sending to phone */
45 sent = s->Device.Functions->WriteDevice(s,buffer2,MsgLength+6);
46
47 free(buffer2);
48
49 if (sent!=MsgLength+6) return ERR_DEVICEWRITEERROR;
50 return ERR_NONE;
51}
52
53static GSM_Error PHONET_StateMachine(GSM_StateMachine *s, unsigned char rx_char)
54{
55 GSM_Protocol_PHONETData *d = &s->Protocol.Data.PHONET;
56 bool correct = false;
57
58 if (d->MsgRXState==RX_GetMessage) {
59 d->Msg.Buffer[d->Msg.Count] = rx_char;
60 d->Msg.Count++;
61
62 /* This is not last byte in frame */
63 if (d->Msg.Count != d->Msg.Length) return ERR_NONE;
64
65 s->Phone.Data.RequestMsg= &d->Msg;
66 s->Phone.Data.DispatchError= s->Phone.Functions->DispatchMessage(s);
67
68 free(d->Msg.Buffer);
69 d->Msg.Length = 0;
70 d->Msg.Buffer = NULL;
71
72 d->MsgRXState = RX_Sync;
73 return ERR_NONE;
74 }
75 if (d->MsgRXState==RX_GetLength2) {
76 d->Msg.Length = d->Msg.Length + rx_char;
77 d->Msg.Buffer = (unsigned char *)malloc(d->Msg.Length);
78
79 d->MsgRXState = RX_GetMessage;
80 return ERR_NONE;
81 }
82 if (d->MsgRXState==RX_GetLength1) {
83 d->Msg.Length = rx_char * 256;
84
85 d->MsgRXState = RX_GetLength2;
86 return ERR_NONE;
87 }
88 if (d->MsgRXState==RX_GetType) {
89 d->Msg.Type = rx_char;
90
91 d->MsgRXState = RX_GetLength1;
92 return ERR_NONE;
93 }
94 if (d->MsgRXState==RX_GetSource) {
95 if (rx_char != PHONET_DEVICE_PHONE) {
96 if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || s->di.dl==DL_TEXTERROR ||
97 s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE || s->di.dl==DL_TEXTERRORDATE) {
98 smprintf(s,"[ERROR: incorrect char - %02x, not %02x]\n", rx_char, PHONET_DEVICE_PHONE);
99 }
100 d->MsgRXState = RX_Sync;
101 return ERR_NONE;
102 }
103 d->Msg.Source = rx_char;
104
105 d->MsgRXState = RX_GetType;
106 return ERR_NONE;
107 }
108 if (d->MsgRXState==RX_GetDestination) {
109 switch (s->ConnectionType) {
110 case GCT_IRDAPHONET:
111 if (rx_char == PHONET_DEVICE_PC) correct = true;
112 break;
113 case GCT_PHONETBLUE:
114 case GCT_BLUEPHONET:
115 if (rx_char == PHONET_BLUE_DEVICE_PC) correct = true;
116 break;
117 default:
118 break;
119 }
120 if (!correct) {
121 if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || s->di.dl==DL_TEXTERROR ||
122 s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE || s->di.dl==DL_TEXTERRORDATE) {
123 smprintf(s,"[ERROR: incorrect char - %02x, not %02x]\n", rx_char, PHONET_DEVICE_PC);
124 }
125 d->MsgRXState = RX_Sync;
126 return ERR_NONE;
127 }
128 d->Msg.Destination = rx_char;
129
130 d->MsgRXState = RX_GetSource;
131 return ERR_NONE;
132 }
133 if (d->MsgRXState==RX_Sync) {
134 switch (s->ConnectionType) {
135 case GCT_IRDAPHONET:
136 if (rx_char == PHONET_FRAME_ID) correct = true;
137 break;
138 case GCT_PHONETBLUE:
139 case GCT_BLUEPHONET:
140 if (rx_char == PHONET_BLUE_FRAME_ID) correct = true;
141 break;
142 default:
143 break;
144 }
145 if (!correct) {
146 if (s->di.dl==DL_TEXT || s->di.dl==DL_TEXTALL || s->di.dl==DL_TEXTERROR ||
147 s->di.dl==DL_TEXTDATE || s->di.dl==DL_TEXTALLDATE || s->di.dl==DL_TEXTERRORDATE) {
148 smprintf(s,"[ERROR: incorrect char - %02x, not %02x]\n", rx_char, PHONET_FRAME_ID);
149 }
150 return ERR_NONE;
151 }
152 d->Msg.Count = 0;
153
154 d->MsgRXState = RX_GetDestination;
155 return ERR_NONE;
156 }
157 return ERR_NONE;
158}
159
160static GSM_Error PHONET_Initialise(GSM_StateMachine *s)
161{
162 int total = 0, i, n;
163 GSM_Protocol_PHONETData *d = &s->Protocol.Data.PHONET;
164 unsigned char req[50];
165
166 d->Msg.Length= 0;
167 d->Msg.Buffer= NULL;
168 d->MsgRXState= RX_Sync;
169
170 if (s->ConnectionType == GCT_PHONETBLUE || s->ConnectionType == GCT_BLUEPHONET) {
171 /* Send frame in PHONET style */
172 req[0] = PHONET_BLUE_FRAME_ID; req[1] = PHONET_DEVICE_PHONE;
173 req[2] = PHONET_BLUE_DEVICE_PC; req[3] = 0xD0;
174 req[4] = 0x00; req[5] = 0x01;
175 req[6] = 0x04;
176 if (s->Device.Functions->WriteDevice(s,req,7) != 7) return ERR_DEVICEWRITEERROR;
177
178 while (total < 7) {
179 n = s->Device.Functions->ReadDevice(s, req + total, 50 - total);
180 total += n;
181 }
182
183 /* Answer frame in PHONET style */
184 req[10] = PHONET_BLUE_FRAME_ID; req[11] = PHONET_BLUE_DEVICE_PC;
185 req[12] = PHONET_DEVICE_PHONE;req[13] = 0xD0;
186 req[14] = 0x00; req[15] = 0x01;
187 req[16] = 0x05;
188
189 for (i = 0; i < 7; i++) {
190 if (req[i] != req[10+i]) {
191 smprintf(s,"Incorrect byte in the answer\n");
192 return ERR_UNKNOWN;
193 }
194 }
195 }
196
197 return ERR_NONE;
198}
199
200static GSM_Error PHONET_Terminate(GSM_StateMachine *s)
201{
202 free(s->Protocol.Data.PHONET.Msg.Buffer);
203 return ERR_NONE;
204}
205
206GSM_Protocol_Functions PHONETProtocol = {
207 PHONET_WriteMessage,
208 PHONET_StateMachine,
209 PHONET_Initialise,
210 PHONET_Terminate
211};
212
213#endif
214
215/* How should editor hadle tabs in this file? Add editor commands here.
216 * vim: noexpandtab sw=8 ts=8 sts=8:
217 */