summaryrefslogtreecommitdiff
path: root/recipes-connectivity/bluez/bluez5/rfcomm/rfcomm.py
blob: 294268b16a16fbf804b8d13ae79db521ec8de635 (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
#!/usr/bin/env python2
import thread
import os
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GObject
import sys
import time
import threading
import socket
import logging
import logging.handlers
import syslog
import grp
import stat

global lg
global opts
global RFCOMMDIR
RFCOMMDIR = '/run/rfcomm'
global TTY_GID  # Group-ID number of the TTY group
global doterm
doterm = True

# cgitb is in python-misc and requires python-pkgutil and python-pydoc
# It is very usefull for analyzing exceptions.
# import cgitb

# who am i
myscript = os.path.basename(__file__)

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

# 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


class Profile(dbus.service.Object):
    fd = -1
    readThread = None
    path = None
    io_id = -1
    io_id2 = -1
    io_pty_master = -1
    io_pty_slave = -1
    myPath = None

    @dbus.service.method('org.bluez.Profile1',
                         in_signature='',
                         out_signature='')
    def Release(self):
        print('Release')
        log.info('Release')
        mainloop.quit()

    @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
        print('NewConnection(%s, %s:%d)' % (path,type(fd).__name__,self.fd))
        lg.info('NewConnection(%s, %d)' % (path, self.fd))

        if opts.pseudoterminal:
            (self.io_pty_master,self.io_pty_slave) = os.openpty()
            print('Acquired pseudoterminal master and slave fd: (%d %d)' % (self.io_pty_master,self.io_pty_slave))
            slavestat = os.fstat(self.io_pty_slave)
            lg.debug('pseudoterminal major and minor: (%d,%d)' % (os.major(slavestat.st_rdev),os.minor(slavestat.st_rdev)))
            if not os.path.isdir(RFCOMMDIR):
                lg.debug('Before mkdir: RFCOMMDIR %s' % (RFCOMMDIR))
                os.mkdir(RFCOMMDIR,0755)
            # Complete directory does not exist test

            address = os.path.basename(path)
            lg.debug('Address %s' % (address))
            self.myPath = RFCOMMDIR + '/' + address
            lg.debug('termPath %s' % (self.myPath))
            if os.path.lexists(self.myPath):
                lg.debug('termPath %s already exists so remove it' % (self.myPath))
                os.remove(self.myPath)
            print('os.mknod(%s,0%o,0%o)' % (self.myPath,0660,slavestat.st_rdev))
            old = os.umask(002)
            try:
                os.mknod(self.myPath,0660 | stat.S_IFCHR,slavestat.st_rdev)
            except Exception as e:
                print '%s' % (e)
                lg.error('%s' % (e))
                return False
            os.umask(old)
            os.chown(self.myPath,0,TTY_GID)
        # Completed pseudoterminal case to create device and node

        # Following code shows noting (why?)
        for key in properties.keys():
            if key == 'Version' or key == 'Features':
                print('  %s = 0x%04x' % (key, properties[key]))
                log.debug('  %s = 0x%04x' % (key, properties[key]))
            else:
                print('  %s = %s' % (key, properties[key]))
                log.info('  %s = %s' % (key, properties[key]))

         
        # +  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.
        sys.stdout.flush()
        if not opts.loopback:
            if opts.pseudoterminal:
                local_fd = self.io_pty_master
            else:
                local_fd = 0
            self.io_id2 = GObject.io_add_watch(local_fd,
                                     GObject.PRIORITY_DEFAULT,
                                     GObject.IO_IN | GObject.IO_PRI,
                                     self.io_term)
        if doterm:
            os.write(1,'TTY> ')
        elif opts.pseudoterminal:
            os.write(io_pty_master,'TTY> ')
            
        self.io_id = GObject.io_add_watch(self.fd,
                                     GObject.PRIORITY_DEFAULT,
                                     GObject.IO_IN | GObject.IO_PRI,
                                     self.io_cb)
        lg.debug('io_id(remote input) = %d io_id2(local input) = %d' % (self.io_id,self.io_id2))

    def io_cb(self, fd, conditions):
        # Read from remote
        data = None
        try:
            data = os.read(fd, 1024)
        except:
            return True
        if opts.loopback:
            if data:
                start = 0
                remain = len(data)
                while remain:
                    try:
                        result = os.write(fd,len(data))
                    except:
                        lg.debut
                        return True
                    if remain != result:
                        remain -= result
                        data = data[-result:]
            return True
        
        if data and len(data) > 0:
            final = data[-1]
            if data[-1] == '\n':
                date = data[:-1]
            if opts.pseudoterminal:
                # Write to master
                os.write(io_pty_master,'\r\n'+data.decode('ascii')+'\r\nTTY>')
            else:
                print('\n'+data.decode('ascii'))
                os.write(1,'TTY> ')
        return True

    def io_term(self, fd0, conditions):
        # Read from local (not used for loopback)
        data = None
        data = os.read(fd0, 1024)
        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 opts.pseudoterminal:
            os.write(fd0,'TTY> ')
        else:
            os.write(1,'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)
            # lg.debug('removed id: %s %s %s',self.io_id,'result: ',rmv)
        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 opts.pseudoterminal:
            if self.io_pty_slave != -1:
                os.close(self.io_pty_slave)
            if self.io_pty_master != -1:
                os.close(self.io_pty_master)
                if self.myPath and os.path.lexists(self.myPath):
                    os.remove(self.myPath)
                    self.myPath = None
        
if __name__ == '__main__':
    import argparse

    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.')
    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:
        doterm = 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()

    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)
    
    dbus.mainloop.glib.threads_init()
    try:
        mainloop.run()
    except KeyboardInterrupt:
        pass
    finally:
        print '\nSerial Port Profile: Goodbye'
        lg.info('Serial Port Profile: Goodbye')
        exit