12
12
import shutil
13
13
import subprocess
14
14
import warnings
15
+ from pathlib import Path
15
16
from typing import Dict , Type
16
17
from socket import gethostname , getfqdn
17
18
import attr
@@ -557,6 +558,8 @@ def _get_params(self):
557
558
558
559
@attr .s (eq = False )
559
560
class GPIOGenericExport (ResourceExport ):
561
+ _gpio_sysfs_path_prefix = '/sys/class/gpio'
562
+
560
563
"""ResourceExport for GPIO lines accessed directly from userspace"""
561
564
562
565
def __attrs_post_init__ (self ):
@@ -566,6 +569,9 @@ def __attrs_post_init__(self):
566
569
from ..resource import base
567
570
local_cls = getattr (base , local_cls_name )
568
571
self .local = local_cls (target = None , name = None , ** self .local_params )
572
+ self .export_path = Path (GpioGenericExport ._gpio_sysfs_path_prefix ,
573
+ f'gpio{ self .local .index } ' )
574
+ self .system_exported = False
569
575
570
576
def _get_params (self ):
571
577
"""Helper function to return parameters"""
@@ -574,6 +580,34 @@ def _get_params(self):
574
580
'index' : self .local .index ,
575
581
}
576
582
583
+ def _get_start_params (self ):
584
+ return {
585
+ 'index' : self .local .index ,
586
+ }
587
+
588
+ def _start (self , start_params ):
589
+ """Start a GPIO export to userspace"""
590
+ index = start_params ['index' ]
591
+
592
+ if self .export_path .exists ():
593
+ self .system_exported = True
594
+ return
595
+
596
+ export_sysfs_path = os .path .join (GpioGenericExport ._gpio_sysfs_path_prefix , 'export' )
597
+ with open (export_sysfs_path , mode = 'wb' ) as export :
598
+ export .write (str (index ).encode ('utf-8' ))
599
+
600
+ def _stop (self , start_params ):
601
+ """Disable a GPIO export to userspace"""
602
+ index = start_params ['index' ]
603
+
604
+ if self .system_exported :
605
+ return
606
+
607
+ export_sysfs_path = os .path .join (GpioGenericExport ._gpio_sysfs_path_prefix , 'unexport' )
608
+ with open (export_sysfs_path , mode = 'wb' ) as unexport :
609
+ unexport .write (str (index ).encode ('utf-8' ))
610
+
577
611
exports ["SysfsGPIO" ] = GPIOGenericExport
578
612
579
613
0 commit comments