Gammu internals  1.38.0
obex.c
Go to the documentation of this file.
1 /* (c) 2003 by Marcin Wiacek */
2 /* www.irda.org OBEX specs 1.3 */
3 
4 #include "../../gsmstate.h"
5 
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 
10 #if defined(GSM_ENABLE_BLUEOBEX) || defined(GSM_ENABLE_IRDAOBEX) || defined(GSM_ENABLE_ATOBEX)
11 
12 #include "../../gsmcomon.h"
13 #include "../../misc/coding/coding.h"
14 #include "obex.h"
15 
16 static GSM_Error OBEX_WriteMessage (GSM_StateMachine *s, unsigned const char *MsgBuffer,
17  int MsgLength, int type)
18 {
19  unsigned char *buffer=NULL;
20  int length=0,sent=0;
21 
22  buffer = (unsigned char *)malloc(MsgLength + 3);
23 
24  OBEXAddBlock(buffer, &length, type, MsgBuffer, MsgLength);
25 
26  GSM_DumpMessageText(s, buffer+3, MsgLength, type);
27  GSM_DumpMessageBinary(s, buffer+3, MsgLength, type);
28 
29  /* Send it out... */
30  sent = s->Device.Functions->WriteDevice(s,buffer,length);
31 
32  free(buffer);
33  buffer=NULL;
34 
35  if (sent!=length) {
36  return ERR_DEVICEWRITEERROR;
37  }
38  return ERR_NONE;
39 }
40 
41 static GSM_Error OBEX_StateMachine(GSM_StateMachine *s, unsigned char rx_char)
42 {
44  GSM_Protocol_OBEXData *d = &s->Protocol.Data.OBEX;
45 
46  switch (d->MsgRXState) {
47  case RX_Sync:
48  d->Msg.Type = rx_char;
50  break;
51  case RX_GetLength1:
52  d->Msg.Length = rx_char * 256;
54  break;
55  case RX_GetLength2:
56  d->Msg.Length = d->Msg.Length + rx_char - 3;
57  d->Msg.Count = 0;
58  if (d->Msg.Count == d->Msg.Length) {
59  s->Phone.Data.RequestMsg = &d->Msg;
60  s->Phone.Data.DispatchError = Phone->DispatchMessage(s);
61  d->MsgRXState = RX_Sync;
62  } else {
63  if (d->Msg.BufferUsed < d->Msg.Length) {
64  d->Msg.BufferUsed = d->Msg.Length;
65  d->Msg.Buffer = (unsigned char *)realloc(d->Msg.Buffer,d->Msg.BufferUsed);
66  }
68  }
69  break;
70  case RX_GetMessage:
71  d->Msg.Buffer[d->Msg.Count] = rx_char;
72  d->Msg.Count++;
73  if (d->Msg.Count == d->Msg.Length) {
74  s->Phone.Data.RequestMsg = &d->Msg;
75  s->Phone.Data.DispatchError = Phone->DispatchMessage(s);
76  d->MsgRXState = RX_Sync;
77  }
78  break;
79  }
80 
81  return ERR_NONE;
82 }
83 
84 static GSM_Error OBEX_Initialise(GSM_StateMachine *s)
85 {
86  GSM_Protocol_OBEXData *d = &s->Protocol.Data.OBEX;
87 
88  d->Msg.BufferUsed = 0;
89  d->Msg.Buffer = NULL;
90  d->Msg.Length = 0;
91 
92  d->MsgRXState = RX_Sync;
93 
94  return ERR_NONE;
95 }
96 
97 static GSM_Error OBEX_Terminate(GSM_StateMachine *s)
98 {
99  free(s->Protocol.Data.OBEX.Msg.Buffer);
100  s->Protocol.Data.OBEX.Msg.Buffer=NULL;
101  return ERR_NONE;
102 }
103 
104 GSM_Protocol_Functions OBEXProtocol = {
105  OBEX_WriteMessage,
106  OBEX_StateMachine,
107  OBEX_Initialise,
108  OBEX_Terminate
109 };
110 
111 void OBEXAddBlock(char *Buffer, int *Pos, unsigned char ID, const char *AddData, int AddLength)
112 {
113  Buffer[(*Pos)++] = ID;
114  Buffer[(*Pos)++] = (AddLength+3)/256;
115  Buffer[(*Pos)++] = (AddLength+3)%256;
116  if (AddData != NULL) {
117  memcpy(Buffer+(*Pos),AddData,AddLength);
118  (*Pos) += AddLength;
119  }
120 }
121 
122 #endif
123 
124 /* How should editor hadle tabs in this file? Add editor commands here.
125  * vim: noexpandtab sw=8 ts=8 sts=8:
126  */
void GSM_DumpMessageBinary(GSM_StateMachine *s, unsigned const char *message, size_t messagesize, int type)
Definition: gsmstate.c:1583
struct GSM_Protocol::@1 Data
void OBEXAddBlock(char *Buffer, int *Pos, unsigned char ID, const char *AddData, int AddLength)
GSM_Error
Definition: gammu-error.h:23
GSM_Error DispatchError
Definition: gsmstate.h:689
int(* WriteDevice)(GSM_StateMachine *s, const void *buf, size_t nbytes)
Definition: gsmstate.h:256
GSM_Phone Phone
Definition: gsmstate.h:1431
GSM_Device_Functions * Functions
Definition: gsmstate.h:335
GSM_Protocol Protocol
Definition: gsmstate.h:1430
GSM_Phone_Data Data
Definition: gsmstate.h:1369
GSM_Phone_Functions * Functions
Definition: gsmstate.h:1373
GSM_Protocol_Message Msg
Definition: obex.h:10
GSM_Error(* DispatchMessage)(GSM_StateMachine *s)
Definition: gsmstate.h:765
unsigned char * Buffer
Definition: protocol.h:22
GSM_Protocol_Message * RequestMsg
Definition: gsmstate.h:676
void GSM_DumpMessageText(GSM_StateMachine *s, unsigned const char *message, size_t messagesize, int type)
Definition: gsmstate.c:1555
GSM_Device Device
Definition: gsmstate.h:1429