|
Line 0
Link Here
|
|
|
1 |
/* |
| 2 |
|
| 3 |
$Id: fbus-phonet.c,v 1.40 2005/04/20 20:43:32 pkot Exp $ |
| 4 |
|
| 5 |
G N O K I I |
| 6 |
|
| 7 |
A Linux/Unix toolset and driver for the mobile phones. |
| 8 |
|
| 9 |
This file is part of gnokii. |
| 10 |
|
| 11 |
Gnokii is free software; you can redistribute it and/or modify |
| 12 |
it under the terms of the GNU General Public License as published by |
| 13 |
the Free Software Foundation; either version 2 of the License, or |
| 14 |
(at your option) any later version. |
| 15 |
|
| 16 |
Gnokii is distributed in the hope that it will be useful, |
| 17 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
GNU General Public License for more details. |
| 20 |
|
| 21 |
You should have received a copy of the GNU General Public License |
| 22 |
along with gnokii; if not, write to the Free Software |
| 23 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 |
|
| 25 |
Copyright (C) 1999-2000 Hugh Blemings & Pavel JanÃk ml. |
| 26 |
Copyright (C) 2000-2001 Chris Kemp |
| 27 |
Copyright (C) 2001-2004 Pawel Kot |
| 28 |
Copyright (C) 2001 Manfred Jonsson, Martin Jancar |
| 29 |
Copyright (C) 2002 Ladis Michl |
| 30 |
Copyright (C) 2002-2003 BORBELY Zoltan |
| 31 |
|
| 32 |
This file provides an API for accessing functions via fbus over irda. |
| 33 |
See README for more details on supported mobile phones. |
| 34 |
|
| 35 |
The various routines are called phonet_(whatever). |
| 36 |
|
| 37 |
*/ |
| 38 |
|
| 39 |
|
| 40 |
/* System header files */ |
| 41 |
#include <stdio.h> |
| 42 |
#include <string.h> |
| 43 |
#include <stdlib.h> |
| 44 |
|
| 45 |
/* Various header file */ |
| 46 |
#include "config.h" |
| 47 |
#include "compat.h" |
| 48 |
#include "misc.h" |
| 49 |
#include "gnokii.h" |
| 50 |
#include "device.h" |
| 51 |
#include "links/fbus-phonet.h" |
| 52 |
#include "links/fbus.h" |
| 53 |
#include "gnokii-internal.h" |
| 54 |
|
| 55 |
static void phonet_rx_statemachine(unsigned char rx_byte, struct gn_statemachine *state); |
| 56 |
static gn_error phonet_send_message(unsigned int messagesize, unsigned char messagetype, unsigned char *message, struct gn_statemachine *state); |
| 57 |
|
| 58 |
|
| 59 |
#define FBUSINST(s) (*((phonet_incoming_message **)(&(s)->link.link_instance))) |
| 60 |
|
| 61 |
#define FBUS_PHONET_BLUETOOTH_INITSEQ 0xd0, 0x00, 0x01 |
| 62 |
|
| 63 |
/*--------------------------------------------*/ |
| 64 |
|
| 65 |
static bool phonet_open(struct gn_statemachine *state) |
| 66 |
{ |
| 67 |
int result, i, n, total = 0; |
| 68 |
unsigned char init_sequence[] = { FBUS_PHONET_BLUETOOTH_FRAME_ID, |
| 69 |
FBUS_DEVICE_PHONE, |
| 70 |
FBUS_PHONET_BLUETOOTH_DEVICE_PC, |
| 71 |
FBUS_PHONET_BLUETOOTH_INITSEQ, |
| 72 |
0x04}; |
| 73 |
unsigned char init_resp[7]; |
| 74 |
unsigned char init_pattern[7] = { FBUS_PHONET_BLUETOOTH_FRAME_ID, |
| 75 |
FBUS_PHONET_BLUETOOTH_DEVICE_PC, |
| 76 |
FBUS_DEVICE_PHONE, |
| 77 |
FBUS_PHONET_BLUETOOTH_INITSEQ, |
| 78 |
0x05}; |
| 79 |
|
| 80 |
if (!state) |
| 81 |
return false; |
| 82 |
|
| 83 |
memset(&init_resp, 0, 7); |
| 84 |
|
| 85 |
/* Open device. */ |
| 86 |
result = device_open(state->config.port_device, false, false, false, |
| 87 |
state->config.connection_type, state); |
| 88 |
|
| 89 |
if (!result) { |
| 90 |
perror(_("Couldn't open PHONET device")); |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
if (state->config.connection_type == GN_CT_Bluetooth) { |
| 95 |
device_write(&init_sequence, 7, state); |
| 96 |
while (total < 7) { |
| 97 |
n = device_read(&init_resp + total, 7 - total, state); |
| 98 |
total += n; |
| 99 |
} |
| 100 |
for (i = 0; i < n; i++) { |
| 101 |
if (init_resp[i] != init_pattern[i]) { |
| 102 |
dprintf("Incorrect byte in the answer\n"); |
| 103 |
return false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/* RX_State machine for receive handling. Called once for each character |
| 112 |
received from the phone. */ |
| 113 |
static void phonet_rx_statemachine(unsigned char rx_byte, struct gn_statemachine *state) |
| 114 |
{ |
| 115 |
phonet_incoming_message *i = FBUSINST(state); |
| 116 |
|
| 117 |
/* FIXME: perhaps we should return something here */ |
| 118 |
if (!i) |
| 119 |
return; |
| 120 |
|
| 121 |
switch (i->state) { |
| 122 |
case FBUS_RX_Sync: |
| 123 |
if (rx_byte == FBUS_PHONET_FRAME_ID || |
| 124 |
rx_byte == FBUS_PHONET_BLUETOOTH_FRAME_ID || |
| 125 |
rx_byte == FBUS_PHONET_DKU2_FRAME_ID) { |
| 126 |
i->state = FBUS_RX_GetDestination; |
| 127 |
} |
| 128 |
break; |
| 129 |
|
| 130 |
case FBUS_RX_GetDestination: |
| 131 |
|
| 132 |
i->message_destination = rx_byte; |
| 133 |
i->state = FBUS_RX_GetSource; |
| 134 |
|
| 135 |
if (rx_byte != FBUS_DEVICE_PC && |
| 136 |
rx_byte != FBUS_PHONET_DKU2_DEVICE_PC && |
| 137 |
rx_byte != FBUS_PHONET_BLUETOOTH_DEVICE_PC) { |
| 138 |
i->state = FBUS_RX_Sync; |
| 139 |
dprintf("The fbus stream is out of sync - expected 0x0c, got 0x%02x\n", rx_byte); |
| 140 |
} |
| 141 |
break; |
| 142 |
|
| 143 |
case FBUS_RX_GetSource: |
| 144 |
|
| 145 |
i->message_source = rx_byte; |
| 146 |
i->state = FBUS_RX_GetType; |
| 147 |
|
| 148 |
if (rx_byte != FBUS_DEVICE_PHONE) { |
| 149 |
i->state = FBUS_RX_Sync; |
| 150 |
dprintf("The fbus stream is out of sync - expected 0x00, got 0x%02x\n", rx_byte); |
| 151 |
} |
| 152 |
|
| 153 |
break; |
| 154 |
|
| 155 |
case FBUS_RX_GetType: |
| 156 |
|
| 157 |
i->message_type = rx_byte; |
| 158 |
i->state = FBUS_RX_GetLength1; |
| 159 |
|
| 160 |
break; |
| 161 |
|
| 162 |
case FBUS_RX_GetLength1: |
| 163 |
|
| 164 |
i->message_length = rx_byte << 8; |
| 165 |
i->state = FBUS_RX_GetLength2; |
| 166 |
|
| 167 |
break; |
| 168 |
|
| 169 |
case FBUS_RX_GetLength2: |
| 170 |
|
| 171 |
i->message_length = i->message_length + rx_byte; |
| 172 |
i->state = FBUS_RX_GetMessage; |
| 173 |
i->buffer_count = 0; |
| 174 |
|
| 175 |
break; |
| 176 |
|
| 177 |
case FBUS_RX_GetMessage: |
| 178 |
|
| 179 |
i->message_buffer[i->buffer_count] = rx_byte; |
| 180 |
i->buffer_count++; |
| 181 |
|
| 182 |
if (i->buffer_count > PHONET_FRAME_MAX_LENGTH) { |
| 183 |
dprintf("PHONET: Message buffer overun - resetting\n"); |
| 184 |
i->state = FBUS_RX_Sync; |
| 185 |
break; |
| 186 |
} |
| 187 |
|
| 188 |
/* Is that it? */ |
| 189 |
|
| 190 |
if (i->buffer_count == i->message_length) { |
| 191 |
sm_incoming_function(i->message_type, i->message_buffer, i->message_length, state); |
| 192 |
i->state = FBUS_RX_Sync; |
| 193 |
} |
| 194 |
break; |
| 195 |
default: |
| 196 |
i->state = FBUS_RX_Sync; |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
/* This is the main loop function which must be called regularly */ |
| 203 |
/* timeout can be used to make it 'busy' or not */ |
| 204 |
|
| 205 |
static gn_error phonet_loop(struct timeval *timeout, struct gn_statemachine *state) |
| 206 |
{ |
| 207 |
gn_error error = GN_ERR_INTERNALERROR; |
| 208 |
unsigned char buffer[255]; |
| 209 |
int count, res; |
| 210 |
|
| 211 |
res = device_select(timeout, state); |
| 212 |
|
| 213 |
if (res > 0) { |
| 214 |
res = device_read(buffer, 255, state); |
| 215 |
for (count = 0; count < res; count++) { |
| 216 |
phonet_rx_statemachine(buffer[count], state); |
| 217 |
} |
| 218 |
if (res > 0) { |
| 219 |
error = GN_ERR_NONE; /* This traps errors from device_read */ |
| 220 |
} |
| 221 |
} else if (!res) { |
| 222 |
error = GN_ERR_TIMEOUT; |
| 223 |
} |
| 224 |
|
| 225 |
return error; |
| 226 |
} |
| 227 |
|
| 228 |
/* Main function to send an fbus message */ |
| 229 |
|
| 230 |
static gn_error phonet_send_message(unsigned int messagesize, unsigned char messagetype, unsigned char *message, struct gn_statemachine *state) |
| 231 |
{ |
| 232 |
|
| 233 |
u8 out_buffer[PHONET_TRANSMIT_MAX_LENGTH + 5]; |
| 234 |
int current = 0; |
| 235 |
int total, sent; |
| 236 |
|
| 237 |
if (!state) |
| 238 |
return GN_ERR_FAILED; |
| 239 |
|
| 240 |
/* FIXME - we should check for the message length ... */ |
| 241 |
|
| 242 |
/* Now construct the message header. */ |
| 243 |
|
| 244 |
if (state->config.connection_type == GN_CT_Bluetooth) { |
| 245 |
out_buffer[current++] = FBUS_PHONET_BLUETOOTH_FRAME_ID; |
| 246 |
out_buffer[current++] = FBUS_DEVICE_PHONE; |
| 247 |
out_buffer[current++] = FBUS_PHONET_BLUETOOTH_DEVICE_PC; |
| 248 |
} else if (state->config.connection_type == GN_CT_DKU2) { |
| 249 |
out_buffer[current++] = FBUS_PHONET_DKU2_FRAME_ID; |
| 250 |
out_buffer[current++] = FBUS_DEVICE_PHONE; |
| 251 |
out_buffer[current++] = FBUS_PHONET_DKU2_DEVICE_PC; |
| 252 |
} else { |
| 253 |
out_buffer[current++] = FBUS_PHONET_FRAME_ID; |
| 254 |
out_buffer[current++] = FBUS_DEVICE_PHONE; /* Destination */ |
| 255 |
out_buffer[current++] = FBUS_DEVICE_PC; /* Source */ |
| 256 |
} |
| 257 |
|
| 258 |
out_buffer[current++] = messagetype; /* Type */ |
| 259 |
|
| 260 |
out_buffer[current++] = (messagesize & 0xff00) >> 8; |
| 261 |
out_buffer[current++] = (messagesize & 0x00ff); |
| 262 |
|
| 263 |
/* Copy in data if any. */ |
| 264 |
|
| 265 |
if (messagesize > 0) { |
| 266 |
memcpy(out_buffer + current, message, messagesize); |
| 267 |
current += messagesize; |
| 268 |
} |
| 269 |
|
| 270 |
/* Send it out... */ |
| 271 |
|
| 272 |
total = current; |
| 273 |
current = 0; |
| 274 |
sent = 0; |
| 275 |
|
| 276 |
do { |
| 277 |
sent = device_write(out_buffer + current, total - current, state); |
| 278 |
if (sent < 0) return GN_ERR_FAILED; |
| 279 |
else current += sent; |
| 280 |
} while (current < total); |
| 281 |
|
| 282 |
sm_incoming_acknowledge(state); |
| 283 |
|
| 284 |
return GN_ERR_NONE; |
| 285 |
} |
| 286 |
|
| 287 |
/* Initialise variables and start the link */ |
| 288 |
gn_error phonet_initialise(struct gn_statemachine *state) |
| 289 |
{ |
| 290 |
gn_error error = GN_ERR_FAILED; |
| 291 |
|
| 292 |
if (!state) |
| 293 |
return error; |
| 294 |
|
| 295 |
/* Fill in the link functions */ |
| 296 |
state->link.loop = &phonet_loop; |
| 297 |
state->link.send_message = &phonet_send_message; |
| 298 |
|
| 299 |
if ((FBUSINST(state) = calloc(1, sizeof(phonet_incoming_message))) == NULL) |
| 300 |
return GN_ERR_MEMORYFULL; |
| 301 |
|
| 302 |
switch (state->config.connection_type) { |
| 303 |
case GN_CT_Infrared: |
| 304 |
case GN_CT_Irda: |
| 305 |
case GN_CT_DKU2: |
| 306 |
case GN_CT_Bluetooth: |
| 307 |
if (phonet_open(state) == true) |
| 308 |
error = GN_ERR_NONE; |
| 309 |
break; |
| 310 |
default: |
| 311 |
break; |
| 312 |
} |
| 313 |
if (error != GN_ERR_NONE) { |
| 314 |
free(FBUSINST(state)); |
| 315 |
FBUSINST(state) = NULL; |
| 316 |
return error; |
| 317 |
} |
| 318 |
|
| 319 |
/* Init variables */ |
| 320 |
FBUSINST(state)->state = FBUS_RX_Sync; |
| 321 |
FBUSINST(state)->buffer_count = 0; |
| 322 |
|
| 323 |
return error; |
| 324 |
} |