gnokii-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

gnokii/common/devices bluetooth.c, NONE, 1.1 osxbluetooth.c, NONE, 1.1 M


From: BORBELY Zoltan <address@hidden>
Subject: gnokii/common/devices bluetooth.c, NONE, 1.1 osxbluetooth.c, NONE, 1.1 Makefile, 1.10, 1.11 unixbluetooth.c, 1.9, 1.10
Date: Thu, 20 Nov 2003 21:10:36 +0000

Update of /cvsroot/gnokii/gnokii/common/devices
In directory subversions:/tmp/cvs-serv11607/common/devices

Modified Files:
        Makefile unixbluetooth.c 
Added Files:
        bluetooth.c osxbluetooth.c 
Log Message:
Bluetooth support on Mac OSX


--- NEW FILE: osxbluetooth.c ---
/*

  $Id: osxbluetooth.c,v 1.1 2003/11/20 21:10:34 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) 1999, 2000 Hugh Blemings & Pavel Janík ml.
  Copyright (C) 2003       Siegfried Schloissnig

*/

#include "devices/osxbluetooth.h"

/* ----- bluetooth io thread ----- */

static void *thread_main(void *pArg)
{
        threadContext* pContext = (threadContext *)pArg;
        IOBluetoothDeviceRef device = 
IOBluetoothDeviceCreateWithAddress(&(pContext->deviceAddress));
        IOBluetoothRFCOMMChannelRef rfcommChannel;

        if(IOBluetoothDeviceOpenRFCOMMChannel(device, pContext->nChannel,
                                      &rfcommChannel) != kIOReturnSuccess) {
                rfcommChannel = 0;
        } else {
                /* register an incoming data listener */
                if 
(IOBluetoothRFCOMMChannelRegisterIncomingDataListener(rfcommChannel,
                         thread_rfcommDataListener, pArg) != kIOReturnSuccess) {
                    rfcommChannel = 0;
                }
        }

        pContext->rfcommChannel = rfcommChannel;

        pthread_mutex_unlock(&(pContext->mutexWait));

        /* start the runloop */
        CFRunLoopRun();
}

static void thread_rfcommDataListener(IOBluetoothRFCOMMChannelRef rfcommChannel,
                               void* data, UInt16 length, void* refCon)
{
        threadContext *pContext = (threadContext *)refCon;
        void *pBuffer = malloc(length);
        dataBlock *pDataBlock = (dataBlock *)malloc(sizeof(dataBlock));

        memcpy(pBuffer, data, length);

        pDataBlock->pData = pBuffer;
        pDataBlock->nSize = length;

        pthread_mutex_lock(&(pContext->mutexWait));
        CFArrayAppendValue(pContext->arrDataReceived, pDataBlock);
        pthread_mutex_unlock(&(pContext->mutexWait));
}

/* ---- bluetooth io thread ---- */

int bluetooth_open(const char* addr, uint8_t channel, struct gn_statemachine* 
state)
{
        /* create the thread context and start the thread */
        CFStringRef strDevice;
        pthread_t threadID;
        threadContext *pContext = (threadContext 
*)malloc(sizeof(threadContext));

        strDevice = CFStringCreateWithCString(kCFAllocatorDefault, addr, 
kCFStringEncodingMacRomanLatin1);
        IOBluetoothNSStringToDeviceAddress(strDevice, &pContext->deviceAddress);
        CFRelease(strDevice);

        pContext->arrDataReceived = CFArrayCreateMutable(kCFAllocatorDefault, 
0, NULL);
        pContext->rfcommChannel = 0;
        pContext->nChannel = channel;

        pthread_mutex_init(&(pContext->mutexWait), NULL);
        pthread_mutex_lock(&(pContext->mutexWait));     /* lock */

        pthread_create(&(pContext->threadID), NULL, thread_main, pContext);

        /* wait until main finishes its initialization */
        pthread_mutex_lock(&(pContext->mutexWait));
        /* unblock the mutex */
        pthread_mutex_unlock(&(pContext->mutexWait));

        if (pContext->rfcommChannel == 0)
                return -1;
        else
                /* return the thread context as the file descriptor */
                return (int)pContext;
}

int bluetooth_close(int fd, struct gn_statemachine *state)
{
        threadContext * pContext = (threadContext *)fd;
        IOBluetoothDeviceRef device;

        sleep(2);

        if (fd != -1 && pContext->rfcommChannel > 0) {
                /* de-register the callback */
                
IOBluetoothRFCOMMChannelRegisterIncomingDataListener(pContext->rfcommChannel, 
NULL, NULL);

                /* close channel and device connection */
                IOBluetoothRFCOMMChannelCloseChannel(pContext->rfcommChannel);
                device = 
IOBluetoothRFCOMMChannelGetDevice(pContext->rfcommChannel);
                IOBluetoothDeviceCloseConnection(device);
                /* IOBluetoothObjectRelease(pContext->rfcommChannel); */
                IOBluetoothObjectRelease(device);
        }

        return 1;
}

int bluetooth_write(int fd, const __ptr_t bytes, int size, struct 
gn_statemachine *state)
{
        threadContext *pContext = (threadContext *)fd;

        if (IOBluetoothRFCOMMChannelWrite(pContext->rfcommChannel, bytes, size, 
TRUE) != kIOReturnSuccess)
                return -1;

        return size;
}

int bluetooth_read(int fd, __ptr_t bytes, int size, struct gn_statemachine 
*state)
{
        threadContext *pContext = (threadContext *)fd;
        int nOffset = 0;
        int nBytes = 0;
        dataBlock* pDataBlock;

        /* no data received so far */
        if (CFArrayGetCount(pContext->arrDataReceived) == 0)
                return 0;

        while (CFArrayGetCount(pContext->arrDataReceived) != 0) {
                pthread_mutex_lock(&(pContext->mutexWait));
                pDataBlock = 
(dataBlock*)CFArrayGetValueAtIndex(pContext->arrDataReceived, 0);
                pthread_mutex_unlock(&(pContext->mutexWait));

                if (pDataBlock->nSize == size) {
                        /* copy data and remove block */
                        memcpy(bytes + nOffset, pDataBlock->pData, size);

                        pthread_mutex_lock(&(pContext->mutexWait));
                        CFArrayRemoveValueAtIndex(pContext->arrDataReceived, 0);
                        pthread_mutex_unlock(&(pContext->mutexWait));

                        free(pDataBlock->pData);
                        free(pDataBlock);

                        return nBytes + size;
                } else if (pDataBlock->nSize > size) {
                        /* copy data and update block contents */
                        memcpy(bytes + nOffset, pDataBlock->pData, size);
                        memmove(pDataBlock->pData, pDataBlock->pData + size, 
pDataBlock->nSize - size);
                        pDataBlock->nSize -= size;
                        return nBytes + size;
                } else { /* pDataBlock->nSize < size */
                        /* copy data and remove block */
                        memcpy(bytes + nOffset, pDataBlock->pData, 
pDataBlock->nSize);

                        size -= pDataBlock->nSize;
                        nOffset += pDataBlock->nSize;
                        nBytes += pDataBlock->nSize;

                        pthread_mutex_lock(&(pContext->mutexWait));
                        CFArrayRemoveValueAtIndex(pContext->arrDataReceived, 0);
                        pthread_mutex_unlock(&(pContext->mutexWait));

                        free(pDataBlock->pData);
                        free(pDataBlock);
                }
        }

        return nBytes;
}

int bluetooth_select(int fd, struct timeval *timeout, struct gn_statemachine 
*state)
{
        threadContext *pContext = (threadContext *)fd;
        int nRetVal = 0;

        usleep(timeout->tv_usec);

        pthread_mutex_lock(&(pContext->mutexWait));

        if (CFArrayGetCount(pContext->arrDataReceived) > 0)
                nRetVal = 1;

        pthread_mutex_unlock(&(pContext->mutexWait));

        return nRetVal;
}

--- NEW FILE: bluetooth.c ---
/*

  $Id: bluetooth.c,v 1.1 2003/11/20 21:10:34 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) 1999, 2000 Hugh Blemings & Pavel Janík ml.
  Copyright (C) 2002       Marcel Holtmann <address@hidden>

*/

#include "config.h"
#include "compat.h"
#include "misc.h"
#include "gnokii.h"

#ifdef HAVE_BLUETOOTH

#include "devices/unixbluetooth.h"

static char *phone[] = {
        "Nokia 3650",
        "Nokia 6210",
        "Nokia 6310",
        "Nokia 6310i",
        "Nokia 7650",
        "Nokia 8910"
};

#ifdef HAVE_BLUETOOTH_MACOSX
#  include "osxbluetooth.c"
#else
#  include "unixbluetooth.c"
#endif

#else /* HAVE_BLUETOOTH */

int bluetooth_open(const char *addr, uint8_t channel, struct gn_statemachine 
*state) { return -1; }
int bluetooth_close(int fd, struct gn_statemachine *state) { return -1; }
int bluetooth_write(int fd, const __ptr_t bytes, int size, struct 
gn_statemachine *state) { return -1; }
int bluetooth_read(int fd, __ptr_t bytes, int size, struct gn_statemachine 
*state) { return -1; }
int bluetooth_select(int fd, struct timeval *timeout, struct gn_statemachine 
*state) { return -1; }

#endif /* HAVE_BLUETOOTH */

Index: Makefile
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/devices/Makefile,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** Makefile    2 Jan 2003 10:55:26 -0000       1.10
--- Makefile    20 Nov 2003 21:10:34 -0000      1.11
***************
*** 20,24 ****
  OBJS =        tekram.o \
        unixirda.o \
!       unixbluetooth.o
  
  ifdef WIN32
--- 20,24 ----
  OBJS =        tekram.o \
        unixirda.o \
!       bluetooth.o
  
  ifdef WIN32

Index: unixbluetooth.c
===================================================================
RCS file: /cvsroot/gnokii/gnokii/common/devices/unixbluetooth.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** unixbluetooth.c     28 Apr 2003 12:57:48 -0000      1.9
--- unixbluetooth.c     20 Nov 2003 21:10:34 -0000      1.10
***************
*** 28,54 ****
  */
  
! #include "config.h"
! #include "compat.h"
! #include "misc.h"
! #include "gnokii.h"
! 
! #ifdef HAVE_BLUETOOTH
! 
! #include "devices/unixbluetooth.h"
  
! static char *phone[] = {
!       "Nokia 3650",
!       "Nokia 6210",
!       "Nokia 6310",
!       "Nokia 6310i",
!       "Nokia 7650",
!       "Nokia 8910"
! };
  
! int bluetooth_open(bdaddr_t *bdaddr, uint8_t channel, struct gn_statemachine 
*state)
  {
        struct sockaddr_rc laddr, raddr;
        int fd;
  
        if ((fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
                perror("Can't create socket");
--- 28,51 ----
  */
  
! #include <stdlib.h>
! #include <unistd.h>
! #include <stdio.h>
! #include <fcntl.h>
! #include <errno.h>
! #include <string.h>
! #include <sys/time.h>
! #include <sys/socket.h>
  
! #include <bluetooth/bluetooth.h>
! #include <bluetooth/rfcomm.h>
  
! int bluetooth_open(const char *addr, uint8_t channel, struct gn_statemachine 
*state)
  {
+       bdaddr_t bdaddr;
        struct sockaddr_rc laddr, raddr;
        int fd;
  
+       str2ba((char *)addr, &bdaddr);
+ 
        if ((fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
                perror("Can't create socket");
***************
*** 67,71 ****
        memset(&raddr, 0, sizeof(raddr));
        raddr.rc_family = AF_BLUETOOTH;
!       bacpy(&raddr.rc_bdaddr, bdaddr);
        raddr.rc_channel = channel;
        if (connect(fd, (struct sockaddr *)&raddr, sizeof(raddr)) < 0) {
--- 64,68 ----
        memset(&raddr, 0, sizeof(raddr));
        raddr.rc_family = AF_BLUETOOTH;
!       bacpy(&raddr.rc_bdaddr, &bdaddr);
        raddr.rc_channel = channel;
        if (connect(fd, (struct sockaddr *)&raddr, sizeof(raddr)) < 0) {
***************
*** 103,114 ****
        return select(fd + 1, &readfds, NULL, NULL, timeout);
  }
- 
- #else /* HAVE_BLUETOOTH */
- 
- int bluetooth_open(void *bdaddr, uint8_t channel, struct gn_statemachine 
*state) { return -1; }
- int bluetooth_close(int fd, struct gn_statemachine *state) { return -1; }
- int bluetooth_write(int fd, const __ptr_t bytes, int size, struct 
gn_statemachine *state) { return -1; }
- int bluetooth_read(int fd, __ptr_t bytes, int size, struct gn_statemachine 
*state) { return -1; }
- int bluetooth_select(int fd, struct timeval *timeout, struct gn_statemachine 
*state) { return -1; }
- 
- #endif /* HAVE_BLUETOOTH */
--- 100,101 ----





reply via email to

[Prev in Thread] Current Thread [Next in Thread]