-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,3 +87,5 @@ ENV/ | |
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
files.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,35 @@ | ||
# candy-board-cli | ||
CANDY LINE boards service CLI | ||
# CANDY Board service CLI | ||
|
||
[![GitHub release](https://img.shields.io/github/release/CANDY-LINE/candy-board-cli.svg)](https://github.com/CANDY-LINE/candy-board-cli/releases/latest) | ||
[![License BSD3](https://img.shields.io/github/license/CANDY-LINE/candy-board-cli.svg)](http://opensource.org/licenses/BSD-3-Clause) | ||
|
||
A CLI tool to communicate with a CANDY Board service running on systemd | ||
|
||
## pip Installation | ||
|
||
``` | ||
$ pip install candy-board-cli | ||
``` | ||
|
||
## pip Uninstallation | ||
|
||
``` | ||
$ pip candy-board-cli | ||
``` | ||
|
||
## Local Installation test | ||
|
||
``` | ||
$ ./setup.py install --record files.txt | ||
``` | ||
|
||
## Local Uninstallation test | ||
|
||
``` | ||
$ cat files.txt | xargs rm -rf | ||
``` | ||
|
||
# Revision history | ||
|
||
* 0.0.1 | ||
- Initial beta release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
import argparse | ||
import json | ||
import socket | ||
import struct | ||
import os | ||
import sys | ||
import pkg_resources | ||
|
||
WARN = "\033[93m" # yellow | ||
NOTICE = "\033[94m" # green | ||
ERROR = "\033[91m" # red | ||
END = "\033[0m" | ||
|
||
cli_version = pkg_resources.require("candy-board-cli")[0].version | ||
|
||
# candy apn ls ... list all apns | ||
# candy apn set <name> <user> <password> ... add a given apn | ||
# candy network show ... show phone network state and signal strength (RSSI in dBm) | ||
# candy sim show ... show SIM status and info (MSISDN, IMSI) | ||
# candy modem show ... show modem info (Model, Manufacturer, Revision, IMEI) | ||
# candy service version ... return the board service software version | ||
# candy version ... return this CLI version | ||
|
||
if "SOCK_PATH" in os.environ: | ||
SOCK_PATH = os.environ["SOCK_PATH"] | ||
else: | ||
SOCK_PATH = "/var/run/candy-board-service.sock" | ||
|
||
def err(msg): | ||
print(ERROR + msg + END) | ||
|
||
def warn(msg): | ||
print(WARN + msg + END) | ||
|
||
def notice(msg): | ||
print(NOTICE + msg + END) | ||
|
||
def main(args): | ||
if args.category == "version": | ||
print("CANDY Board Service CLI version: %s" % cli_version) | ||
if not os.path.exists(SOCK_PATH): | ||
notice("[NOTICE] CANDY Board Service isn't running") | ||
return 0 | ||
|
||
if not os.path.exists(SOCK_PATH): | ||
err("[ERROR] CANDY Board Service isn't running") | ||
return 1 | ||
|
||
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) | ||
sock.connect(SOCK_PATH) | ||
code = 0 | ||
|
||
if args.category in ("apn", "network", "sim", "modem", "service"): | ||
try: | ||
# request | ||
packer = struct.Struct("I") | ||
cmd_json = json.dumps(vars(args)) | ||
size = len(cmd_json) | ||
packed_header = packer.pack(size) | ||
sock.sendall(packed_header) | ||
packer = struct.Struct("%is" % size) | ||
packed_json = packer.pack(cmd_json) | ||
sock.sendall(packed_json) | ||
|
||
# response | ||
packer = struct.Struct("I") | ||
packed_result = sock.recv(packer.size) | ||
result = packer.unpack(packed_result) | ||
|
||
# result | ||
if result != 0: | ||
packer = struct.Struct("%is" % result) | ||
packed_message = sock.recv(packer.size) | ||
message_json = packer.unpack(packed_message) | ||
message = json.loads(message_json[0]) | ||
if message["status"] != "OK": | ||
code = 2 | ||
if message["result"]: | ||
print(json.dumps(message["result"], indent=2)) | ||
|
||
finally: | ||
sock.close() | ||
return code | ||
else: | ||
raise ValueError("Unsupported category") | ||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser(description="CANDY Board Service CLI") | ||
categories = parser.add_subparsers(title="categories", dest="category") | ||
|
||
version_commands = categories.add_parser("version", help = "CLI Version") | ||
|
||
parser_apn = categories.add_parser("apn", help = "Manipulate APN settings") | ||
apn_commands = parser_apn.add_subparsers(title="APN actions", dest="action") | ||
|
||
parser_apn_ls = apn_commands.add_parser ("ls", help = "List all APNs") | ||
parser_apn_set = apn_commands.add_parser ("set", help = "Set a new APN") | ||
parser_apn_set.add_argument ("-n", "--name", type = str, required = True, help = "APN") | ||
parser_apn_set.add_argument ("-u", "--user-id", type = str, required = True, help = "User ID") | ||
parser_apn_set.add_argument ("-p", "--password", type = str, required = True, help = "User Password") | ||
|
||
parser_network = categories.add_parser("network", help = "Manage Phone Network") | ||
network_commands = parser_network.add_subparsers(title="Phone Network actions", dest="action") | ||
parser_network_show = network_commands.add_parser ("show", help = "Show Phone network state and Signal strength") | ||
|
||
parser_sim = categories.add_parser("sim", help = "Manage SIM") | ||
sim_commands = parser_sim.add_subparsers(title="SIM actions", dest="action") | ||
parser_sim_show = sim_commands.add_parser ("show", help = "Show SIM state and SIM information") | ||
|
||
parser_modem = categories.add_parser("modem", help = "Manage LTE/3G module modem") | ||
modem_commands = parser_modem.add_subparsers(title="Modem actions", dest="action") | ||
parser_modem_show = modem_commands.add_parser ("show", help = "Show Module information") | ||
|
||
parser_service = categories.add_parser("service", help = "Manage CANDY Board Service") | ||
service_commands = parser_service.add_subparsers(title="CANDY Board Service actions", dest="action") | ||
parser_service_version = service_commands.add_parser ("version", help = "Show CANDY Board Service software version") | ||
|
||
args = parser.parse_args() | ||
sys.exit(main(args)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
|
||
import os | ||
import sys | ||
from setuptools import setup, find_packages | ||
|
||
version = "0.0.1" | ||
|
||
if sys.argv[-1] == 'publish': | ||
os.system('python setup.py sdist upload') | ||
os.system('python setup.py bdist_wheel upload') | ||
sys.exit() | ||
|
||
setup( | ||
name='candy-board-cli', | ||
version=version, | ||
author='Daisuke Baba', | ||
author_email='[email protected]', | ||
url='http://github.com/CANDY-LINE/candy-board-cli', | ||
download_url='https://github.com/CANDY-LINE/candy-board-cli/tarball/{0}'.format(version), | ||
description='CANDY Board Service CLI', | ||
long_description=open('README.md').read() + '\n\n' + open('LICENSE').read(), | ||
license='BSD3', | ||
scripts=['bin/candy'], | ||
classifiers=[ | ||
'Programming Language :: Python', | ||
'Development Status :: 3 - Alpha', | ||
'Natural Language :: English', | ||
'Environment :: Console', | ||
'Intended Audience :: System Administrators', | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: BSD License', | ||
'Operating System :: POSIX :: Linux', | ||
'Topic :: System :: Hardware', | ||
], | ||
keywords=( | ||
'CANDY EGG', 'CANDY LINE' | ||
), | ||
) |