Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python2 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | |
| 4 | # GR-GSM based transceiver |
| 5 | # CTRL interface implementation |
| 6 | # |
| 7 | # (C) 2016-2017 by Vadim Yanitskiy <axilirator@gmail.com> |
| 8 | # |
| 9 | # All Rights Reserved |
| 10 | # |
| 11 | # This program is free software; you can redistribute it and/or modify |
| 12 | # it under the terms of the GNU General Public License as published by |
| 13 | # the Free Software Foundation; either version 2 of the License, or |
| 14 | # (at your option) any later version. |
| 15 | # |
| 16 | # This program is distributed in the hope that it will be useful, |
| 17 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | # GNU General Public License for more details. |
| 20 | # |
| 21 | # You should have received a copy of the GNU General Public License along |
| 22 | # with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | |
Vadim Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 25 | from grgsm.trx import udp_link |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 26 | |
Vadim Yanitskiy | 873e44e | 2017-10-17 06:52:01 +0700 | [diff] [blame] | 27 | class ctrl_if(udp_link): |
Piotr Krysik | 902f4eb | 2017-09-19 08:04:33 +0200 | [diff] [blame] | 28 | def handle_rx(self, data): |
| 29 | if self.verify_req(data): |
| 30 | request = self.prepare_req(data) |
| 31 | rc = self.parse_cmd(request) |
| 32 | |
| 33 | if type(rc) is tuple: |
| 34 | self.send_response(request, rc[0], rc[1]) |
| 35 | else: |
| 36 | self.send_response(request, rc) |
| 37 | else: |
| 38 | print("[!] Wrong data on CTRL interface") |
| 39 | |
| 40 | def verify_req(self, data): |
| 41 | # Verify command signature |
| 42 | return data.startswith("CMD") |
| 43 | |
| 44 | def prepare_req(self, data): |
| 45 | # Strip signature, paddings and \0 |
| 46 | request = data[4:].strip().strip("\0") |
| 47 | # Split into a command and arguments |
| 48 | request = request.split(" ") |
| 49 | # Now we have something like ["TXTUNE", "941600"] |
| 50 | return request |
| 51 | |
| 52 | def verify_cmd(self, request, cmd, argc): |
| 53 | # Check if requested command matches |
| 54 | if request[0] != cmd: |
| 55 | return False |
| 56 | |
| 57 | # And has enough arguments |
| 58 | if len(request) - 1 != argc: |
| 59 | return False |
| 60 | |
| 61 | # Check if all arguments are numeric |
| 62 | for v in request[1:]: |
| 63 | if not v.isdigit(): |
| 64 | return False |
| 65 | |
| 66 | return True |
| 67 | |
| 68 | def send_response(self, request, response_code, params = None): |
| 69 | # Include status code, for example ["TXTUNE", "0", "941600"] |
| 70 | request.insert(1, str(response_code)) |
| 71 | |
| 72 | # Optionally append command specific parameters |
| 73 | if params is not None: |
| 74 | request += params |
| 75 | |
| 76 | # Add the response signature, and join back to string |
| 77 | response = "RSP " + " ".join(request) + "\0" |
| 78 | # Now we have something like "RSP TXTUNE 0 941600" |
| 79 | self.send(response) |
| 80 | |
| 81 | def parse_cmd(self, request): |
| 82 | raise NotImplementedError |