1#!/usr/bin/env python
2#
3# Copyright (c) 2017, The OpenThread Authors.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are met:
8# 1. Redistributions of source code must retain the above copyright
9#    notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11#    notice, this list of conditions and the following disclaimer in the
12#    documentation and/or other materials provided with the distribution.
13# 3. Neither the name of the copyright holder nor the
14#    names of its contributors may be used to endorse or promote products
15#    derived from this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27# POSSIBILITY OF SUCH DAMAGE.
28#
29
30import sys
31import abc
32import logging
33import serial
34import time
35
36ABC = abc.ABC if sys.version_info >= (3, 4) else abc.ABCMeta('ABC', (), {})
37logger = logging.getLogger(__name__)
38
39
40class RfShieldController(ABC):
41
42    @abc.abstractmethod
43    def shield(self):
44        pass
45
46    @abc.abstractmethod
47    def unshield(self):
48        pass
49
50    @abc.abstractmethod
51    def __enter__(self):
52        pass
53
54    @abc.abstractmethod
55    def __exit__(self, *args, **kwargs):
56        pass
57
58
59class RfSwitchController(RfShieldController):
60
61    def __init__(self, channel, port):
62        self._channel = channel
63        self._port = port
64        self._conn = None
65
66    def shield(self):
67        self._display_string('CLOSE {}'.format(self._channel))
68        self._write('CLOSE (@{})'.format(self._channel))
69
70    def unshield(self):
71        self._display_string('OPEN {}'.format(self._channel))
72        self._write('OPEN (@{})'.format(self._channel))
73
74    def _write(self, data):
75        return self._conn.write('{}\r\n'.format(data))
76
77    def _display_string(self, string):
78        self._write('DIAGNOSTIC:DISPLAY "{}"'.format(string))
79
80    def __enter__(self):
81        self._conn = serial.Serial(self._port, 9600)
82        if not self._conn.isOpen():
83            self._conn.open()
84        self._write('')
85        time.sleep(1)
86        return self
87
88    def __exit__(self, *args, **kwargs):
89        if self._conn:
90            self._conn.close()
91            self._conn = None
92
93
94CONTROLLERS = {'RF_SWITCH': RfSwitchController}
95
96
97def get_rf_shield_controller(shield_type, params):
98    if shield_type in CONTROLLERS:
99        return CONTROLLERS[shield_type](**params)
100    logger.exception('Unknown RF shield controller type: {}'.format(shield_type))
101