PageRenderTime 57ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/hardware_ril/mock-ril/src/python/tcs.py

https://code.google.com/
Python | 247 lines | 206 code | 8 blank | 33 comment | 3 complexity | 21cbcdbab2c47307a32da2ef02c84dd6 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, LGPL-2.1, CC0-1.0
  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2010 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License")
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Test server
  18. A detailed description of tms.
  19. """
  20. __author__ = 'wink@google.com (Wink Saville)'
  21. import socket
  22. import struct
  23. import sys
  24. import time
  25. import ctrl_pb2
  26. import ril_pb2
  27. import msgheader_pb2
  28. def recvall(s, count):
  29. """Receive all of the data otherwise return none.
  30. Args:
  31. s: socket
  32. count: number of bytes
  33. Returns:
  34. data received
  35. None if no data is received
  36. """
  37. all_data = []
  38. while (count > 0):
  39. data = s.recv(count)
  40. if (len(data) == 0):
  41. return None
  42. count -= len(data)
  43. all_data.append(data)
  44. result = ''.join(all_data);
  45. return result
  46. def sendall(s, data):
  47. """Send all of the data
  48. Args:
  49. s: socket
  50. count: number of bytes
  51. Returns:
  52. Nothing
  53. """
  54. s.sendall(data)
  55. class MsgHeader:
  56. """A fixed length message header; cmd, token, status and length of protobuf."""
  57. def __init__(self):
  58. self.cmd = 0
  59. self.token = 0
  60. self.status = 0
  61. self.length_protobuf = 0
  62. def sendHeader(self, s):
  63. """Send the header to the socket
  64. Args:
  65. s: socket
  66. Returns
  67. nothing
  68. """
  69. mh = msgheader_pb2.MsgHeader()
  70. mh.cmd = self.cmd
  71. mh.token = self.token
  72. mh.status = self.status
  73. mh.length_data = self.length_protobuf
  74. mhser = mh.SerializeToString()
  75. len_msg_header_raw = struct.pack('<i', len(mhser))
  76. sendall(s, len_msg_header_raw)
  77. sendall(s, mhser)
  78. def recvHeader(self, s):
  79. """Receive the header from the socket"""
  80. len_msg_header_raw = recvall(s, 4)
  81. len_msg_hdr = struct.unpack('<i', len_msg_header_raw)
  82. mh = msgheader_pb2.MsgHeader()
  83. mh_raw = recvall(s, len_msg_hdr[0])
  84. mh.ParseFromString(mh_raw)
  85. self.cmd = mh.cmd
  86. self.token = mh.token
  87. self.status = mh.status
  88. self.length_protobuf = mh.length_data;
  89. class Msg:
  90. """A message consists of a fixed length MsgHeader followed by a protobuf.
  91. This class sends and receives messages, when sending the status
  92. will be zero and when receiving the protobuf field will be an
  93. empty string if there was no protobuf.
  94. """
  95. def __init__(self):
  96. """Initialize the protobuf to None and header to an empty"""
  97. self.protobuf = None
  98. self.header = MsgHeader()
  99. # Keep a local copy of header for convenience
  100. self.cmd = 0
  101. self.token = 0
  102. self.status = 0
  103. def sendMsg(self, s, cmd, token, protobuf=''):
  104. """Send a message to a socket
  105. Args:
  106. s: socket
  107. cmd: command to send
  108. token: token to send, will be returned unmodified
  109. protobuf: optional protobuf to send
  110. Returns
  111. nothing
  112. """
  113. self.cmd = cmd
  114. self.token = token
  115. self.status = 0
  116. self.protobuf = protobuf
  117. self.header.cmd = self.cmd
  118. self.header.token = self.token
  119. self.header.status = self.status
  120. if (len(protobuf) > 0):
  121. self.header.length_protobuf = len(protobuf)
  122. else:
  123. self.header.length_protobuf = 0
  124. self.protobuf = ''
  125. self.header.sendHeader(s)
  126. if (self.header.length_protobuf > 0):
  127. sendall(s, self.protobuf)
  128. def recvMsg(self, s):
  129. """Receive a message from a socket
  130. Args:
  131. s: socket
  132. Returns:
  133. nothing
  134. """
  135. self.header.recvHeader(s)
  136. self.cmd = self.header.cmd
  137. self.token = self.header.token
  138. self.status = self.header.status
  139. if (self.header.length_protobuf > 0):
  140. self.protobuf = recvall(s, self.header.length_protobuf)
  141. else:
  142. self.protobuf = ''
  143. def main(argv):
  144. """Create a socket and connect.
  145. Before using you'll need to forward the port
  146. used by mock_ril, 54312 to a port on the PC.
  147. The following worked for me:
  148. adb forward tcp:11111 tcp:54312.
  149. Then you can execute this test using:
  150. tms.py 127.0.0.1 11111
  151. """
  152. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  153. host = sys.argv[1] # server address
  154. print "host=%s" % host
  155. port = int(sys.argv[2]) # server port
  156. print "port=%d" % port
  157. s.connect((host, port))
  158. # Create an object which is serializable to a protobuf
  159. rrs = ctrl_pb2.CtrlRspRadioState()
  160. rrs.state = ril_pb2.RADIOSTATE_UNAVAILABLE
  161. print "rrs.state=%d" % (rrs.state)
  162. # Serialize
  163. rrs_ser = rrs.SerializeToString()
  164. print "len(rrs_ser)=%d" % (len(rrs_ser))
  165. # Deserialize
  166. rrs_new = ctrl_pb2.CtrlRspRadioState()
  167. rrs_new.ParseFromString(rrs_ser)
  168. print "rrs_new.state=%d" % (rrs_new.state)
  169. # Do an echo test
  170. req = Msg()
  171. req.sendMsg(s, 0, 1234567890123, rrs_ser)
  172. resp = Msg()
  173. resp.recvMsg(s)
  174. response = ctrl_pb2.CtrlRspRadioState()
  175. response.ParseFromString(resp.protobuf)
  176. print "cmd=%d" % (resp.cmd)
  177. print "token=%d" % (resp.token)
  178. print "status=%d" % (resp.status)
  179. print "len(protobuf)=%d" % (len(resp.protobuf))
  180. print "response.state=%d" % (response.state)
  181. if ((resp.cmd == 0) & (resp.token == 1234567890123) &
  182. (resp.status == 0) & (response.state == 1)):
  183. print "SUCCESS: echo ok"
  184. else:
  185. print "ERROR: expecting cmd=0 token=1234567890123 status=0 state=1"
  186. # Test CTRL_GET_RADIO_STATE
  187. req.sendMsg(s, ctrl_pb2.CTRL_CMD_GET_RADIO_STATE, 4)
  188. resp = Msg()
  189. resp.recvMsg(s)
  190. print "cmd=%d" % (resp.cmd)
  191. print "token=%d" % (resp.token)
  192. print "status=%d" % (resp.status)
  193. print "length_protobuf=%d" % (len(resp.protobuf))
  194. if (resp.cmd == ctrl_pb2.CTRL_CMD_GET_RADIO_STATE):
  195. response = ctrl_pb2.CtrlRspRadioState()
  196. response.ParseFromString(resp.protobuf)
  197. print "SUCCESS: response.state=%d" % (response.state)
  198. else:
  199. print "ERROR: expecting resp.cmd == ctrl_pb2.CTRL_CMD_GET_RADIO_STATE"
  200. # Close socket
  201. print "closing socket"
  202. s.close()
  203. if __name__ == '__main__':
  204. main(sys.argv)