summaryrefslogtreecommitdiff
path: root/recipes-connectivity/bluez/bluez5/rfcomm/rfcomm.py
blob: e8f2554260acf715959bdc77113a6d6d220d5f4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#!/usr/bin/env python2
import thread
import os
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GObject, GLib
import sys
import time
import threading
import socket
import logging
import logging.handlers
import syslog
import grp
import stat
import atexit
import re
import mmap
import subprocess
import threading
import struct
import fcntl
import termios
import signal
import time
import bluetooth

# Do we need stty onlcr????

#SerialPortProfile = '00001101-0000-1000-8000-00805f9b34fb'

global lg
global opts
global RFCOMMDIR
RFCOMMDIR = '/run/rfcomm'
SLAVEDIR='/dev/pts'
BLUEZLIB='/var/lib/bluetooth'

global TTY_GID  # Group-ID number of the TTY group
global doterm  # If true, this python program is a terminal console.
global needpseudot # Login option and pseudoterminal option
needpseudot = False
global terminatenow
terminatenow = False

myscript = os.path.basename(__file__)
class dopidfile(object):
    pidPath = "/"

    def writePidFile(self):
        global pidPath
        pid = str(os.getpid())
        pidPath = '/run/rfcomm' + '.pid'
        f = open(pidPath, 'w')
        f.write(pid)
        f.close()

    def rmPidFile(self):
        try:
            os.remove(pidPath)
        except OSError:
            pass


# Log formating class
class flog:
    # priority strings to be used
    # with the __init__ function
    priorities = {
        'debug':    logging.DEBUG,
        'info':     logging.INFO,
        'warn':     logging.WARNING,
        'warning':  logging.WARNING,
        'error':    logging.ERROR,
        'critical': logging.CRITICAL,
        }
    
    def __init__(self,myscript,facility,priority):
        """
        Initialize for logging

        :param myscript:    The name of the python program script
        :param facility:    The syslog facility, such as daemon or user
        :param priority:    The minimum priority to be printed to the log
        :returns: Nothing
        :raises TBD:        logging class errors.
        """
        name_len = str(len(myscript))
        self.myscript = myscript
        self.log = logging.getLogger(myscript)
        self.handler = logging.handlers.SysLogHandler(address=('/dev/log'),facility=facility)
        self.default_fmt = ' %(levelname)-9s %(name)-' + name_len + 's %(message)s'
        self.verbose_fmt1 = ' %(levelname)-9s %(name)-' + name_len + 's %(threadName)-14s '
        self.verbose_fmt2 = ' %(message)s'
        formatter = logging.Formatter(self.default_fmt)
        self.handler.setFormatter(formatter)
        self.log.setLevel(self.priorities[priority]) # Minimum infolevel to log
        self.log.addHandler(self.handler)
        self.handler.createLock()

    def __default(self,func,*args):
        self.handler.acquire()
        formatter = logging.Formatter(self.default_fmt)
        self.handler.setFormatter(formatter)
        func(*args)
        self.handler.release()

    def setThreshold(self,threshold):
        """
        Change the syslog priority threshold
        
        :param priority:    Character string corresponding to the threshold
        """
        self.handler.acquire()
        self.log.setLevel(self.priorities[threshold]) # Minimum infolevel to log
        self.handler.release()

    def critical(self,*args):
        """
        Prints a variable argument list at critical priority
        
        :returns: logging result
        """
        self.__default(self.log.critical,*args)

    def error(self,*args):
        """
        Prints a variable argument list at error priority
            
        :returns: logging result
        """
        self.__default(self.log.error,*args)

    def warning(self,*args):
        """
        Prints a variable argument list at warning priority
        
        :returns: logging result
        """
        self.__default(self.log.warning,*args)

    #  Python has no notice level!

    def info(self,*args):
        """
        Prints a variable argument list at info priority
        
        :returns: logging result
        """
        self.__default(self.log.info,*args)

    def debug(self,*args):
        """
        Prints a variable argument list at debug priority

        Printing debug includes function name and line
        number.
        
        :returns: logging result
        """
        caller_frame = sys._getframe().f_back
        callerfunc = caller_frame.f_code.co_name + '@' + str(caller_frame.f_lineno);
        callerfunc = callerfunc.ljust(16)
        self.handler.acquire()
        log = logging.getLogger(self.myscript)
        formatter = logging.Formatter(self.verbose_fmt1+callerfunc+self.verbose_fmt2)
        self.handler.setFormatter(formatter)
        log.debug(*args)
        self.handler.release()

# End of log handler


    
# Thread to create login process, with
# stdin, stdout, stderr matching the file descriptor
# This is because NewConnection cannot create threads or
# use a mutex.  Workarounds are pipes, IPC semamphores,
# IPC messaging
class logins(object):
    slavefd = -1

    # Thread to wait on our children
    def IgnoreWait(self,pid):
        lg.debug("IgnoreWait: Waiting on process pid: %d" % (pid.pid))
        pid.wait()
        lg.debug("login terminated: %d" % (pid.pid))
    
    def StartLogin(self,rpipe,mainloop):
        datafd=''
        masters = []
        lg.debug("StartLogin enter: rpipe fd=%d" % (rpipe))
        while 1:
            try:
                datafd=os.read(rpipe,8)
            except Exception as e:
                lg.error('os.read error: %s' % (e))
                lg.debug('Done with StartLogin, calling quit')
                for fd in masters:
                    os.close(fd)
                os.kill(os.getpid(), signal.SIGINT)
                thread.exit()
            # Single integer.
            (slavefd,masterfd) = struct.unpack("ii",bytearray(datafd))
            if masterfd > 0:
                masters.append(masterfd)
            lg.debug("StartLogin: slavefd %d" % (slavefd))
            if slavefd < 0:
                lg.debug("Told to exit, so exiting StartLogin thread")
                os.close(rpipe)
                # We try to close all the masters, as it gets
                # things wound down in a hurry.
                for fd in masters:
                    lg.debug('StartLogin: Try to close fd %d' % (fd))
                    try:
                        os.close(fd)
                    except Exception as e:
                        lg.debug('StartLogin (ignore error): OK: Did not close fd: %d %s' % (fd,e))
                        sys.exc_clear()
                os.kill(os.getpid(), signal.SIGINT)
                thread.exit()
            # Start login with fd, and close it.
            Env = {'TERM': 'dumb'}
            self.slavefd = slavefd
            lg.debug('Popen slavefd: %d' % (slavefd))
            try:
                # Mar 16 14:23:35 mtcdt daemon.err  ERROR     rfcomm.py Popen login: global name 's' is not defined
                pid = subprocess.Popen(['/bin/login','--'],env=Env,preexec_fn = lambda: ( os.setsid(),fcntl.ioctl(0, termios.TIOCSCTTY, 0)  ),stdin=slavefd,stdout=slavefd,stderr=slavefd,close_fds=True,cwd='/')
                lg.debug('Start IgnoreWait thread')
                try:
                    IgnoreWaitThread = threading.Thread(target=self.IgnoreWait,args=[pid])
                except Exception as e:
                    lg.error('IgnoreWaitThread: threading.Thread: %s' % (e))
                try:
                    IgnoreWaitThread.start()
                except Exception as e:
                    lg.error('IgnoreWaitThread: start: %s' % (e))
            except Exception as e:
                lg.error('Popen login: %s' % (e))
            os.close(slavefd)

class Profile(dbus.service.Object):
    fd = -1
    readThread = None
    path = None
    io_id = -1
    io_id2 = -1
    hup_id = -1
    hup_id2 = -1
    io_pty_master = -1
    io_pty_slave = -1
    slavePath = None
    linkPath = None
    w = -1
    # True False pseudonyms for making code readable (or not!)
    exiting = True
    notexiting = False
    
    @dbus.service.method('org.bluez.Profile1',
                         in_signature='',
                         out_signature='')
    def Release(self):
        lg.info('Release/quit')
        mainloop.quit()

    @dbus.service.method("org.bluez.Profile1",
        in_signature="", out_signature="")
    def Cancel(self):
        lg.info("Cancel")
        
    def removeLink(self,state):
        lg.debug('removeLink: state: %r' % (state))
        path = self.linkPath
        lg.debug('removeLink: path %s' % (path))
        if state == self.exiting:
            lg.debug('Clearing out linkPath')
            self.linkPath = None  # Burn bridges, do it once.
        if path and os.path.lexists(path):
            try:
                os.remove(path)
            except Exception as e:
                lg.error("os.remove(self.linkPath): Tried to remove %s" % (path))
                lg.error('%s' % (e))

        

    # New Connection is called when a new Bluetooth
    # is established
    @dbus.service.method('org.bluez.Profile1',
                         in_signature='oha{sv}',
                         out_signature='')
    def NewConnection(self, path, fd, properties):
        dbus.mainloop.glib.threads_init()
        self.fd = fd.take()  # Extract File Descriptor from dbus UnixFD class.
        self.path = path
        
        # Bluetooth address portion of the path
        address = os.path.basename(self.path)

	numaddr = address[address.find("_")+1:]
	# Replace _ with :
	Name = bluetooth.lookup_name(numaddr.replace("_",":"))
        
        print('NewConnection(%s, %s, %s:%d)' % (path,Name,type(fd).__name__,self.fd))
        lg.info('NewConnection(%s, %s, %s:%d)' % (path,Name,type(fd).__name__,self.fd))
        atexit.register(self.RequestDisconnection,self.path)
        lg.debug('Past atexit.register')
        
        # Get a pseudoterminal to provide an I/O driver for
        # a program that needs a TTY.
        if needpseudot:
            (self.io_pty_master,self.io_pty_slave) = os.openpty()
            slavestat = os.fstat(self.io_pty_slave)
            self.minor = os.minor(slavestat.st_rdev)
            lg.debug('pseudoterminal major and minor: (%d,%d)' % (os.major(slavestat.st_rdev),self.minor))
            if not os.path.isdir(RFCOMMDIR):
                lg.debug('Before mkdir: RFCOMMDIR %s' % (RFCOMMDIR))
                os.mkdir(RFCOMMDIR,0755)
                            
                    
            lg.debug('Address %s' % (address))
            self.linkPath = RFCOMMDIR + '/' + address + '_' + Name + '_pts' + str(self.minor)
	    self.slavePath = SLAVEDIR + '/' + str(self.minor)
            lg.debug('termPath %s' % (self.linkPath))
            self.removeLink(self.notexiting)
            # linkPath was removed
            
            lg.debug('os.symlink(%s,%s)' % (self.slavePath,self.linkPath))
            old = os.umask(002)
            lg.debug('past umask')
            try:
                os.symlink(self.slavePath,self.linkPath)
                try:
                    atexit.register(self.removeLink,self.exiting);
                except Exception as e:
                    lg.error('Register atexit: %s' % (e))
            except Exception as e:
                lg.error('symlink failed: %s' % (e))
                return False
            lg.debug('Before umask')
            os.umask(old)
            lg.debug('After umask')
            # os.chown(self.myPath,0,TTY_GID)

        # Completed pseudoterminal case to create device and node

        # +  For loopback, we only monitor the RFCOMM line (self.fd).
        # +  For interactive (not pseudoterminal or loopback option,
        #    we monitor the stdin (0) and the RFCOMM line for input
        # +  For pseudoterminal, we monitor input on the RFCOMM line,
        #    and the slave pseudoterminal.
        # Note that io_add_watch causes a poll to be done by the
        # python GI library.  The callback functions (io_term and io_cb)
        # will be called when there is an event on the file descriptor
        # being polled.
        sys.stdout.flush()
        if not opts.loopback:
            if needpseudot:
                local_fd = self.io_pty_master
                lg.debug('NewConnection: master_fd: %d slave_fd: %d' % (local_fd,self.io_pty_slave))  #success to here.
            else:
                # stdin
                local_fd = 0
            lg.debug('Ready to do io_add_watch on local_fd: %d' % (local_fd))
            try:
                self.io_id2 = GObject.io_add_watch(local_fd,
                                     GObject.PRIORITY_DEFAULT,
                                     GObject.IO_IN | GObject.IO_PRI | GObject.IO_HUP | GObject.IO_ERR,
                                     self.io_term)
            except Exception as e:
                lg.error('io_addwatch failed for local_fd %d: IO_IN, IO_PRI %s' % (local_fd,e))
             
        if opts.login:
            # Writing the slave file descriptor causes the login process to start.
            lg.debug('opts.login is true slave fd: %d pipe: %d' % (self.io_pty_slave,self.w))
            try:
                os.write(self.w,struct.pack('ii',self.io_pty_slave,self.io_pty_master))
            except Exception as e:
                lg.error('os.write of slave fd:%d master fd: %d failed: %s' % (self.io_pty_slave,self.io_pty_slave,e))
                os.close(self.io_pty_slave)
                self.io_pty_slave = -1
                return False
            self.io_pty_slave = -1

        if doterm:
            lg.debug('Profile: Write the prompt')
            os.write(1,'TTY> ')

        lg.debug('NewConnection: doterm: %s,  io_add_watch is next' % doterm)
            
        self.io_id = GObject.io_add_watch(self.fd,
                                     GObject.PRIORITY_DEFAULT,
                                     GObject.IO_IN | GObject.IO_PRI | GObject.IO_HUP | GObject.IO_ERR,
                                     self.io_cb)
        lg.debug('io_id(remote input) = %d io_id2(local input) = %d' % (self.io_id,self.io_id2))


    # I/O read from Bluetooth remote to local application.
    def io_cb(self, fd, conditions):
        if terminatenow:
            self.RequestDisconnection(self.path)

        if (conditions & GObject.IO_HUP or conditions & GObject.IO_ERR):
            lg.debug('Found HUP on fd: %d, so terminate' % (fd))
            self.RequestDisconnection(self.path)
            return False
        # Read from remote
        data = None
        try:
            data = os.read(fd, 1024)
        except:
            return True
        lg.debug('io_cb: past read: doterm: %s' % doterm)
        if opts.loopback:
            toutput = fd # same as input
        else:
            toutput = self.io_pty_master
    
        if opts.loopback or needpseudot or opts.login:
            if data:
                start = 0
                remain = len(data)
                result = 1
                lg.debug('remain is %d entering the loop' % (remain))
                while remain > 0 and result > 0:
                    try:
                        result = os.write(toutput,data)
                    except Exception as e:
                        lg.debug('os.write failed: %s' %  (e))
                        return True
                    lg.debug('os.write returned %d for %s remain: %d' % (result,data,remain))
                    if remain != result and remain > 0:
                        remain -= result
                        lg.debug('remain is now %d result is %d' % (remain,result))
                        data = data[-remain:]
                        lg.debug('remain to print %s' % (data))
                    else:
                        remain = 0
            lg.debug('returning true to end this routing')
            return True
        
        if data and len(data) > 0:
            final = data[-1]
            if data[-1] == '\n':
                date = data[:-1]
            if doterm:
                print('\n'+data.decode('ascii'))
                os.write(1,'TTY> ')
                
        return True

    # I/O written to bluetooth from local slave (application write to remote)
    def io_term(self, fd0, conditions):
        if terminatenow:
            self.RequestDisconnection(self.path)
        if (conditions & GObject.IO_HUP or conditions & GObject.IO_ERR):
            lg.debug('Found HUP on fd0: %d, so terminate' % (fd0))
            self.RequestDisconnection(self.path)
            return False
        # Read from local (not used for loopback)
        data = None
        data = os.read(fd0, 1024)
        lg.debug('io_term: fd0: %d len(data): %d' % (fd0,len(data)))
        if not data:
            # No Data == EOF
            self.RequestDisconnection(self.path)
            return True
        #for character in data:
        #    print character, character.encode('hex')
        try:
            os.write(self.fd,data)
        except Exception as e:
            print '%s' % (e)
            lg.error('%s' % (e))
            self.RequestDisconnection(self.path)
            return True
        if doterm:
            os.write(fd0,'TTY> ')
        return True
  
    @dbus.service.method('org.bluez.Profile1',
                         in_signature='o',
                         out_signature='')
    def RequestDisconnection(self, path):
        print('RequestDisconnection(%s)' % (path))
        lg.info('RequestDisconnection(%s)' % (path))
        if self.fd != -1:
            lg.debug('closing fd: %s' % (self.fd))
            s = socket.fromfd(self.fd,socket.AF_INET,socket.SOCK_STREAM)
            result = s.shutdown(socket.SHUT_RDWR)
            lg.debug('After shutdown fd: %s %s %s' % (self.fd,' result:',result))
            result = os.close(self.fd)
            lg.debug('After closing fd: %s %s %s' % (self.fd,' result:',result))
            self.fd = -1
        if self.io_id != -1:
            lg.debug('remove id: %s' % (self.io_id))
            rmv = GObject.source_remove(self.io_id)
        self.io_id = -1
        if self.io_id2 != -1:
            lg.debug('closing id2: %s' % (self.io_id2))
            rmv = GObject.source_remove(self.io_id2)
            lg.debug('removed id2: %s %s %s' % (self.io_id2,'result: ',rmv))
            self.io_id2 = -1
        if self.hup_id != -1:
            lg.debug('closing hup_id: %s' % (self.hup_id))
            rmv = GObject.source_remove(self.hup_id)
            lg.debug('removed id2: %s %s %s' % (self.hup_id,'result: ',rmv))
            self.hup_id = -1
        if self.hup_id2 != -1:
            lg.debug('closing hup_id2: %s' % (self.hup_id2))
            rmv = GObject.source_remove(self.hup_id2)
            lg.debug('removed id2: %s %s %s' % (self.hup_id2,'result: ',rmv))
            self.hup_id2 = -1
        if needpseudot:
            if self.io_pty_slave != -1:
                os.close(self.io_pty_slave)
                self.io_pty_slave = -1
            if self.io_pty_master != -1:
                try:
                    savefd = self.io_pty_master
                    self.io_pty_master = -1
                    os.close(savefd)
                except Exception as e:
                    lg.error("close(io_pty_master): Tried to close %d" % (savefd))
                    lg.error('%s' % (e))

                self.removeLink(self.exiting)

def terminationHandler(mainloop):
    lg.debug('SIGTERM: terminationHandler was called')
    mainloop.quit()

        
if __name__ == '__main__':
    import argparse

    doterm = False
    TTY_GID = grp.getgrnam('tty').gr_gid
    # Set up logging initially info and above	
    lg = flog(myscript,'daemon','info')

    parser = argparse.ArgumentParser(
		description='BlueZ RFCOMM server.')

    parser.add_argument('-u', '--uuid',
            metavar='uuid_or_shortcut', default='spp',
            help='Service UUID to use. Can be either full UUID'
                    ' or one of the shortcuts: gn, panu, nap. Default: %(default)s.')
    parser.add_argument('--pseudoterminal', action='store_true',
            help='Create a pseudoterminal and put slave in /run/rfcomm'
                    ' Suitable for background operation.')
    parser.add_argument('--loopback', action='store_true',
            help='Echo data for testing (exclusive with pseudoterminal)')
    parser.add_argument('--debug',
            action='store_true', help='Verbose operation mode.')
    parser.add_argument('--login',
            action='store_true', help='Use RFCOMM to log into this device.')
    opts = parser.parse_args()

    if opts.debug:
        lg.setThreshold('debug')

    if opts.pseudoterminal and opts.loopback:
        msg = 'Cannot have both pseudoterminal and loopback option'
        print msg
        lg.error(msg)
        exit(1)
    if not opts.pseudoterminal and not opts.loopback and not opts.login:
        doterm = True
        print "main: doterm is %s" % (str(doterm))

    if opts.pseudoterminal or opts.login:
        needpseudot = True
    
  

    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    
    bus = dbus.SystemBus()
    
    manager = dbus.Interface(bus.get_object('org.bluez',
                                            '/org/bluez'),
                             'org.bluez.ProfileManager1')

    mainloop = GObject.MainLoop()

    GLib.unix_signal_add(GLib.PRIORITY_HIGH,signal.SIGTERM,terminationHandler,mainloop)
    mypidfile = dopidfile()
    mypidfile.writePidFile()

    if opts.login:
        # Need to create thead to exec logins.
        rpipe,wpipe = os.pipe()
        loginProcess = logins()
        Profile.w = wpipe
        lg.debug('Call threading next')
        try:
            StartLoginThread = threading.Thread(target=loginProcess.StartLogin,args=[rpipe,mainloop])
        except Exception as e:
            lg.error('threading.Thread: StartLogin %s' % (e))
        try:
            StartLoginThread.start()
        except Exception as e:
            lg.error('StartLogin.start: %s' % (e))

    
    profile_path = '/foo/bar/profile'
    
    SPP_opts = {
        'AutoConnect': True,
        'Role': 'server',
        'Name': 'SerialPort'
    }

    print('Starting Serial Port Profile...')
    lg.info('Starting Serial Port Profile...')

    profile = Profile(bus, profile_path)

    try:
        manager.RegisterProfile(profile_path, opts.uuid, SPP_opts)
    except dbus.exceptions.DBusException as inst:
        print 'dbus exception:',inst._dbus_error_name
        lg.error('dbus exception: %s',inst._dbus_error_name)
        if inst._dbus_error_name == 'org.freedesktop.DBus.Error.AccessDenied':
	    print 'Try running as root'
        exit(1)
    
    lg.debug('Completed Register Profile...')
    dbus.mainloop.glib.threads_init()
    lg.debug('Completed threads init... Now mainloop.run')
    try:
        mainloop.run()
    except KeyboardInterrupt:
        pass
    except Exception as e:
        lg.error('mainloop exception: %s' % (e))
        print '\nSerial Port Profile: ERROR Goodbye'
        lg.error('Serial Port Profile: ERROR Goodbye')
        data = struct.pack('i',-1)
        os.write(wpipe,data)
        mainloop.quit()

    lg.info('Serial Port Profile: Goodbye')
    if opts.login:
        data = struct.pack('ii',-1,-1)
        os.write(wpipe,data)
    mypidfile.rmPidFile()
    mainloop.quit()