⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fbus.c

📁 NOKIA手机开发包
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  $Id: fbus.c,v 1.56 2003/10/28 00:03:40 bozo Exp $  G N O K I I  A Linux/Unix toolset and driver for Nokia mobile phones.  This file is part of gnokii.  Gnokii 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.  Gnokii 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 gnokii; if not, write to the Free Software  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA  Copyright (C) 2000 Hugh Blemings & Pavel Jan韐 ml.  Copyright (C) 2000 Chris Kemp  This file provides an API for accessing functions via fbus.  See README for more details on supported mobile phones.  The various routines are called FBUS_(whatever).*//* System header files */#include <stdio.h>#include <string.h>#include <stdlib.h>/* Various header file */#include "config.h"#include "compat.h"#include "misc.h"#include "gnokii.h"#include "device.h"#include "links/fbus.h"#include "gnokii-internal.h"static void fbus_rx_statemachine(unsigned char rx_byte, struct gn_statemachine *state);static gn_error fbus_send_message(unsigned int messagesize, unsigned char messagetype, unsigned char *message, struct gn_statemachine *state);static int fbus_tx_send_ack(u8 message_type, u8 message_seq, struct gn_statemachine *state);/* FIXME - win32 stuff! */#define FBUSINST(s) ((fbus_link *)((s)->link.link_instance))#define	IR_MODE(s) ((s)->config.connection_type == GN_CT_Infrared || (s)->config.connection_type == GN_CT_Tekram)/*--------------------------------------------*/static bool fbus_serial_open(bool dlr3, struct gn_statemachine *state){	int type;	if (state->config.connection_type == GN_CT_TCP)		type = GN_CT_TCP;	else		type = GN_CT_Serial;	if (dlr3) dlr3 = 1;	/* Open device. */	if (!device_open(state->config.port_device, false, false, false, type, state)) {		perror(_("Couldn't open FBUS device"));		return false;	}	device_changespeed(115200, state);	/* clearing the RTS bit and setting the DTR bit */	device_setdtrrts((1-dlr3), 0, state);	return true;}static bool at2fbus_serial_open(struct gn_statemachine *state, gn_connection_type type){	unsigned char init_char = 0x55;	unsigned char end_init_char = 0xc1;	int count, res;	unsigned char buffer[255];	/* Open device. */	if (!device_open(state->config.port_device, false, false, false, type, state)) {		perror(_("Couldn't open FBUS device"));		return false;	} 	device_setdtrrts(0, 0, state);	sleep(1);	device_setdtrrts(1, 1, state);	device_changespeed(19200, state);	sleep(1);	device_write("AT\r", 3, state);	sleep(1);	res = device_read(buffer, 255, state);	device_write("AT&F\r", 5, state);	usleep(100000);	res = device_read(buffer, 255, state);	device_write("AT*NOKIAFBUS\r", 13, state);	usleep(100000);	res = device_read(buffer, 255, state); 	device_changespeed(115200, state);	if (type != GN_CT_Bluetooth && type != GN_CT_TCP) { 		for (count = 0; count < 32; count++) {			device_write(&init_char, 1, state);		}		device_write(&end_init_char, 1, state);		usleep(1000000);	} 	return true;}static bool fbus_ir_open(struct gn_statemachine *state){	struct timeval timeout;	unsigned char init_char = 0x55;	unsigned char end_init_char = 0xc1;	unsigned char connect_seq[] = { FBUS_FRAME_HEADER, 0x0d, 0x00, 0x00, 0x02 };	unsigned int count, retry;	if (!device_open(state->config.port_device, false, false, false, state->config.connection_type, state)) {		perror(_("Couldn't open FBUS device"));		return false;	}	/* clearing the RTS bit and setting the DTR bit */	device_setdtrrts(1, 0, state);	for (retry = 0; retry < 5; retry++) {		dprintf("IR init, retry=%d\n", retry);		device_changespeed(9600, state);		for (count = 0; count < 32; count++) {			device_write(&init_char, 1, state);		}		device_write(&end_init_char, 1, state);		usleep(100000);		device_changespeed(115200, state);		fbus_send_message(7, 0x02, connect_seq, state);		/* Wait for 1 sec. */		timeout.tv_sec	= 1;		timeout.tv_usec	= 0;		if (device_select(&timeout, state)) {			dprintf("IR init succeeded\n");			return true;		}	}	return false;}/* RX_State machine for receive handling.  Called once for each character   received from the phone. */static void fbus_rx_statemachine(unsigned char rx_byte, struct gn_statemachine *state){	struct timeval time_diff;	fbus_incoming_frame *i = &FBUSINST(state)->i;	int frm_num, seq_num;	fbus_incoming_message *m;	unsigned char *message_buffer;	/* XOR the byte with the current checksum */	i->checksum[i->buffer_count & 1] ^= rx_byte;	switch (i->state) {		/* Messages from the phone start with an 0x1e (cable) or 0x1c (IR).		   We use this to "synchronise" with the incoming data stream. However,		   if we see something else, we assume we have lost sync and we require		   a gap of at least 5ms before we start looking again. This is because		   the data part of the frame could contain a byte which looks like the		   sync byte */	case FBUS_RX_Discarding:		gettimeofday(&i->time_now, NULL);		timersub(&i->time_now, &i->time_last, &time_diff);		if (time_diff.tv_sec == 0 && time_diff.tv_usec < 5000) {			i->time_last = i->time_now;	/* no gap seen, continue discarding */			break;		}		/* else fall through to... */	case FBUS_RX_Sync:		if (IR_MODE(state)) {			if (rx_byte == FBUS_IR_FRAME_ID) {				/* Initialize checksums. */				i->checksum[0] = FBUS_IR_FRAME_ID;				i->checksum[1] = 0;				i->state = FBUS_RX_GetDestination;			} else {				/* Lost frame sync */				i->state = FBUS_RX_Discarding;				gettimeofday(&i->time_last, NULL);			}		} else {	/* !IR_MODE */			if (rx_byte == FBUS_FRAME_ID) {				/* Initialize checksums. */				i->checksum[0] = FBUS_FRAME_ID;				i->checksum[1] = 0;				i->state = FBUS_RX_GetDestination;			} else {				/* Lost frame sync */				i->state = FBUS_RX_Discarding;				gettimeofday(&i->time_last, NULL);			}		}		break;	case FBUS_RX_GetDestination:		i->message_destination = rx_byte;		i->state = FBUS_RX_GetSource;		/* When there is a checksum error and things get out of sync we have to manage to resync */		/* If doing a data call at the time, finding a 0x1e etc is really quite likely in the data stream */		/* Then all sorts of horrible things happen because the packet length etc is wrong... */		/* Therefore we test here for a destination of 0x0c and return to the top if it is not */		if (rx_byte != 0x0c) {			i->state = FBUS_RX_Sync;			dprintf("The fbus stream is out of sync - expected 0x0c, got %2x\n", rx_byte);		}		break;	case FBUS_RX_GetSource:		i->message_source = rx_byte;		i->state = FBUS_RX_GetType;		/* Source should be 0x00 */		if (rx_byte != 0x00) {			i->state = FBUS_RX_Sync;			dprintf("The fbus stream is out of sync - expected 0x00, got %2x\n",rx_byte);		}		break;	case FBUS_RX_GetType:		i->message_type = rx_byte;		i->state = FBUS_RX_GetLength1;		break;	case FBUS_RX_GetLength1:		i->frame_length = rx_byte << 8;		i->state = FBUS_RX_GetLength2;		break;	case FBUS_RX_GetLength2:		i->frame_length = i->frame_length + rx_byte;		i->state = FBUS_RX_GetMessage;		i->buffer_count = 0;		break;	case FBUS_RX_GetMessage:		if (i->buffer_count >= FBUS_FRAME_MAX_LENGTH) {			dprintf("FBUS: Message buffer overun - resetting\n");			i->state = FBUS_RX_Sync;			break;		}		i->message_buffer[i->buffer_count] = rx_byte;		i->buffer_count++;		/* If this is the last byte, it's the checksum. */		if (i->buffer_count == i->frame_length + (i->frame_length % 2) + 2) {			i->state = FBUS_RX_Sync;			/* Is the checksum correct? */			if (i->checksum[0] == i->checksum[1]) {				/* Deal with exceptions to the rules - acks and rlp.. */				if (i->message_type == 0x7f) {					dprintf("[Received Ack of type %02x, seq: %2x]\n",						i->message_buffer[0], (unsigned char) i->message_buffer[1]);					sm_incoming_acknowledge(state);				} else if (i->message_type == 0xf1) {					sm_incoming_function(i->message_type, i->message_buffer,							     i->frame_length - 2, state);				} else {	/* Normal message type */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -