summaryrefslogtreecommitdiffstats
path: root/meta-facebook/meta-yosemite/recipes-yosemite/fbutils/files/power_util.py
blob: d1cc60ef4979c6a5aded94168046eb5da284f1fc (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
#!/usr/bin/env python
#
# Copyright 2015-present Facebook. All Rights Reserved.
#
# This program file 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; version 2 of the License.
#
# This program 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 this program in a file named COPYING; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301 USA
#

import os
import re
import time
import logging
from ctypes import *

POR_DIR = '/mnt/data/power/por'
POR_CONFIG = '%s/config' % POR_DIR
POR_LPS = '%s/last_state' % POR_DIR

logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)

class PORConfig():
    on  = "1"     # Default ON
    off = "2"     # Default OFF
    lps = "3"     # Default is to use the Last Power State

# Handler for Bridge IC libraries
bic = CDLL("libbic.so")

class BIC_GPIO(Structure):
    _fields_ = [ ("bic_gpio_data", c_char * 4) ]

# Get 32-bit GPIO data
def get_bic_gpio():
    gpio = BIC_GPIO()
    p_gpio = pointer(gpio)
    bic.bic_get_gpio(p_gpio)
    return gpio

# Get the CPU power status
def get_pwr_cpu():
    gpio = get_bic_gpio()
    pwrgood_cpu = (ord(gpio.bic_gpio_data[0]) & 0x01)
    return pwrgood_cpu

# Initilize the POR configuration files in /mnt/data
def init_por():

    por = PORConfig()

    # For the Power On Reset Config
    if not os.path.isfile(POR_CONFIG):
        try:
            os.makedirs(POR_DIR)
        except OSERROR as err:
            pass

        por_cnfg = open(POR_CONFIG, 'w')
        por_cnfg.write('%s\n' % por.on)
        por_cnfg.close()

    # For the Last Power State info
    if not os.path.isfile(POR_LPS):
        curr_time = int(time.time())
        lps = 'on %s' % str(curr_time)

        f_lps = open(POR_LPS, 'w')
        f_lps.write('%s\n' % lps)
        f_lps.close()

# Get the POR config [ ON | OFF | LPS ]
def get_por_config():

    por = PORConfig()

    if os.path.isfile(POR_CONFIG):
        por_cnfg = open(POR_CONFIG, 'r')
        cnfg = por_cnfg.read(1)

        if cnfg in [por.on, por.off, por.lps]:
            return cnfg
        else:
            return 0
    else:
        return -1

# To check whether the last power state was on or off
def get_por_lps():

    if os.path.isfile(POR_LPS):
        f_lps = open(POR_LPS, 'r')
        lps = f_lps.readline()
        if re.search(r'on', lps):
            return 1
        elif re.search(r'off', lps):
            return 0
    else:
        return -1

# This tells whether to Power ON or not on POR
# 1 - Power ON
# 0 - Do not Power ON
def por_policy():

    por = PORConfig()
    cnfg = get_por_config()
    if cnfg < 1:
        log.error("power_util: Error getting the POR config.")
        exit(-1)

    if (cnfg == por.on):
        # cpu power ON
        log.debug('ON: Powering ON')
        return 1

    elif (cnfg == por.off):
        # cpu power OFF
        log.debug('OFF: Powering OFF')
        return 0

    elif (cnfg == por.lps):
        lps = get_por_lps()
        if lps < 0:
            log.error("power_util: Error getting the POR Last State.")
            exit(-1)

        if lps == 1:
            # cpu power ON
            log.debug('LPS: Powering ON')
            return 1

        elif lps == 0:
            # cpu power OFF
            log.debug('LPS: Powering OFF')
            return 0


if __name__ == "__main__":
    main()
OpenPOWER on IntegriCloud