Version of easy-connect with the u-blox cellular platforms C027 and C030 added.

Dependents:   HelloMQTT

Committer:
group-ublox
Date:
Thu Aug 10 14:33:05 2017 +0000
Revision:
0:19aa55d66228
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
group-ublox 0:19aa55d66228 1 """
group-ublox 0:19aa55d66228 2 mbed SDK
group-ublox 0:19aa55d66228 3 Copyright (c) 2011-2013 ARM Limited
group-ublox 0:19aa55d66228 4
group-ublox 0:19aa55d66228 5 Licensed under the Apache License, Version 2.0 (the "License");
group-ublox 0:19aa55d66228 6 you may not use this file except in compliance with the License.
group-ublox 0:19aa55d66228 7 You may obtain a copy of the License at
group-ublox 0:19aa55d66228 8
group-ublox 0:19aa55d66228 9 http://www.apache.org/licenses/LICENSE-2.0
group-ublox 0:19aa55d66228 10
group-ublox 0:19aa55d66228 11 Unless required by applicable law or agreed to in writing, software
group-ublox 0:19aa55d66228 12 distributed under the License is distributed on an "AS IS" BASIS,
group-ublox 0:19aa55d66228 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
group-ublox 0:19aa55d66228 14 See the License for the specific language governing permissions and
group-ublox 0:19aa55d66228 15 limitations under the License.
group-ublox 0:19aa55d66228 16 """
group-ublox 0:19aa55d66228 17
group-ublox 0:19aa55d66228 18 import sys
group-ublox 0:19aa55d66228 19 import socket
group-ublox 0:19aa55d66228 20 from sys import stdout
group-ublox 0:19aa55d66228 21 from threading import Thread
group-ublox 0:19aa55d66228 22 from SocketServer import BaseRequestHandler, UDPServer
group-ublox 0:19aa55d66228 23 from mbed_host_tests import BaseHostTest, event_callback
group-ublox 0:19aa55d66228 24
group-ublox 0:19aa55d66228 25
group-ublox 0:19aa55d66228 26 class UDPEchoClientHandler(BaseRequestHandler):
group-ublox 0:19aa55d66228 27 def handle(self):
group-ublox 0:19aa55d66228 28 """ UDP packet handler. Echoes data back to sender's address.
group-ublox 0:19aa55d66228 29 """
group-ublox 0:19aa55d66228 30 data, sock = self.request
group-ublox 0:19aa55d66228 31 sock.sendto(data, self.client_address)
group-ublox 0:19aa55d66228 32
group-ublox 0:19aa55d66228 33
group-ublox 0:19aa55d66228 34 class UDPEchoClientTest(BaseHostTest):
group-ublox 0:19aa55d66228 35
group-ublox 0:19aa55d66228 36 def __init__(self):
group-ublox 0:19aa55d66228 37 """
group-ublox 0:19aa55d66228 38 Initialise test parameters.
group-ublox 0:19aa55d66228 39
group-ublox 0:19aa55d66228 40 :return:
group-ublox 0:19aa55d66228 41 """
group-ublox 0:19aa55d66228 42 BaseHostTest.__init__(self)
group-ublox 0:19aa55d66228 43 self.SERVER_IP = None # Will be determined after knowing the target IP
group-ublox 0:19aa55d66228 44 self.SERVER_PORT = 0 # Let TCPServer choose an arbitrary port
group-ublox 0:19aa55d66228 45 self.server = None
group-ublox 0:19aa55d66228 46 self.server_thread = None
group-ublox 0:19aa55d66228 47 self.target_ip = None
group-ublox 0:19aa55d66228 48
group-ublox 0:19aa55d66228 49 @staticmethod
group-ublox 0:19aa55d66228 50 def find_interface_to_target_addr(target_ip):
group-ublox 0:19aa55d66228 51 """
group-ublox 0:19aa55d66228 52 Finds IP address of the interface through which it is connected to the target.
group-ublox 0:19aa55d66228 53
group-ublox 0:19aa55d66228 54 :return:
group-ublox 0:19aa55d66228 55 """
group-ublox 0:19aa55d66228 56 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
group-ublox 0:19aa55d66228 57 try:
group-ublox 0:19aa55d66228 58 s.connect((target_ip, 0)) # Target IP, any port
group-ublox 0:19aa55d66228 59 except socket.error:
group-ublox 0:19aa55d66228 60 s.connect((target_ip, 8000)) # Target IP, 'random' port
group-ublox 0:19aa55d66228 61 ip = s.getsockname()[0]
group-ublox 0:19aa55d66228 62 s.close()
group-ublox 0:19aa55d66228 63 return ip
group-ublox 0:19aa55d66228 64
group-ublox 0:19aa55d66228 65 def setup_udp_server(self):
group-ublox 0:19aa55d66228 66 """
group-ublox 0:19aa55d66228 67 sets up a UDP server for target to connect and send test data.
group-ublox 0:19aa55d66228 68
group-ublox 0:19aa55d66228 69 :return:
group-ublox 0:19aa55d66228 70 """
group-ublox 0:19aa55d66228 71 # !NOTE: There should mechanism to assert in the host test
group-ublox 0:19aa55d66228 72 if self.SERVER_IP is None:
group-ublox 0:19aa55d66228 73 self.log("setup_udp_server() called before determining server IP!")
group-ublox 0:19aa55d66228 74 self.notify_complete(False)
group-ublox 0:19aa55d66228 75
group-ublox 0:19aa55d66228 76 # Returning none will suppress host test from printing success code
group-ublox 0:19aa55d66228 77 self.server = UDPServer((self.SERVER_IP, self.SERVER_PORT), UDPEchoClientHandler)
group-ublox 0:19aa55d66228 78 ip, port = self.server.server_address
group-ublox 0:19aa55d66228 79 self.SERVER_PORT = port
group-ublox 0:19aa55d66228 80 self.server.allow_reuse_address = True
group-ublox 0:19aa55d66228 81 self.log("HOST: Listening for UDP packets: " + self.SERVER_IP + ":" + str(self.SERVER_PORT))
group-ublox 0:19aa55d66228 82 self.server_thread = Thread(target=UDPEchoClientTest.server_thread_func, args=(self,))
group-ublox 0:19aa55d66228 83 self.server_thread.start()
group-ublox 0:19aa55d66228 84
group-ublox 0:19aa55d66228 85 @staticmethod
group-ublox 0:19aa55d66228 86 def server_thread_func(this):
group-ublox 0:19aa55d66228 87 """
group-ublox 0:19aa55d66228 88 Thread function to run TCP server forever.
group-ublox 0:19aa55d66228 89
group-ublox 0:19aa55d66228 90 :param this:
group-ublox 0:19aa55d66228 91 :return:
group-ublox 0:19aa55d66228 92 """
group-ublox 0:19aa55d66228 93 this.server.serve_forever()
group-ublox 0:19aa55d66228 94
group-ublox 0:19aa55d66228 95 @event_callback("target_ip")
group-ublox 0:19aa55d66228 96 def _callback_target_ip(self, key, value, timestamp):
group-ublox 0:19aa55d66228 97 """
group-ublox 0:19aa55d66228 98 Callback to handle reception of target's IP address.
group-ublox 0:19aa55d66228 99
group-ublox 0:19aa55d66228 100 :param key:
group-ublox 0:19aa55d66228 101 :param value:
group-ublox 0:19aa55d66228 102 :param timestamp:
group-ublox 0:19aa55d66228 103 :return:
group-ublox 0:19aa55d66228 104 """
group-ublox 0:19aa55d66228 105 self.target_ip = value
group-ublox 0:19aa55d66228 106 self.SERVER_IP = self.find_interface_to_target_addr(self.target_ip)
group-ublox 0:19aa55d66228 107 self.setup_udp_server()
group-ublox 0:19aa55d66228 108
group-ublox 0:19aa55d66228 109 @event_callback("host_ip")
group-ublox 0:19aa55d66228 110 def _callback_host_ip(self, key, value, timestamp):
group-ublox 0:19aa55d66228 111 """
group-ublox 0:19aa55d66228 112 Callback for request for host IP Addr
group-ublox 0:19aa55d66228 113
group-ublox 0:19aa55d66228 114 """
group-ublox 0:19aa55d66228 115 self.send_kv("host_ip", self.SERVER_IP)
group-ublox 0:19aa55d66228 116
group-ublox 0:19aa55d66228 117 @event_callback("host_port")
group-ublox 0:19aa55d66228 118 def _callback_host_port(self, key, value, timestamp):
group-ublox 0:19aa55d66228 119 """
group-ublox 0:19aa55d66228 120 Callback for request for host port
group-ublox 0:19aa55d66228 121 """
group-ublox 0:19aa55d66228 122 self.send_kv("host_port", self.SERVER_PORT)
group-ublox 0:19aa55d66228 123
group-ublox 0:19aa55d66228 124 def teardown(self):
group-ublox 0:19aa55d66228 125 if self.server:
group-ublox 0:19aa55d66228 126 self.server.shutdown()
group-ublox 0:19aa55d66228 127 self.server_thread.join()