message.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # --------------------------------------------------------------------------
  2. # Copyright (c) 2020-2022 Arm Limited (or its affiliates). All rights reserved.
  3. #
  4. # SPDX-License-Identifier: Apache-2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the License); you may
  7. # not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an AS IS BASIS, WITHOUT
  14. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. # --------------------------------------------------------------------------
  18. import sys
  19. # VSI KIND
  20. VSIOUTPUT = 0
  21. VSIINPUT = 1
  22. # MESSAGE IDs
  23. CLIENTREADBUF=1
  24. CLIENTWRITEBUF=2
  25. CLIENTSTOP=3
  26. # PACKETSIZE : default number of bytes read on a socket
  27. PACKETSIZE = 1024
  28. # Conersion between size expressed in bytes or in Q15
  29. INTSIZE = 2
  30. # Error raised when trying to read / write to sockets
  31. class ErrorTooMuchDataReceived(Exception):
  32. pass
  33. class CantReceiveData(Exception):
  34. pass
  35. def clientID(inputMode,theID):
  36. return([(theID << 1) | inputMode])
  37. # Receive a given number of bytes
  38. # Socket is read by block of PACKETSIZE
  39. def receiveBytes(conn,nb):
  40. data = b""
  41. while nb > 0:
  42. if nb < PACKETSIZE:
  43. newData = conn.recv(nb)
  44. if not newData: raise CantReceiveData
  45. else:
  46. newData= conn.recv(PACKETSIZE)
  47. if not newData: raise CantReceiveData
  48. nb = nb - len(newData)
  49. if nb < 0:
  50. raise ErrorTooMuchDataReceived
  51. data += newData
  52. return(data)
  53. # Send bytes
  54. def sendBytes(conn,data):
  55. conn.sendall(data)
  56. # Convert a list of Q15 to a bytestream
  57. def list_to_bytes(l):
  58. return(b"".join([x.to_bytes(INTSIZE,byteorder=sys.byteorder,signed=True) for x in l]))
  59. # Convert a bytestream to a list of Q15
  60. def bytes_to_list(l):
  61. res=[]
  62. i = 0
  63. while(i<len(l)):
  64. res.append(int.from_bytes(l[i:i+INTSIZE],byteorder=sys.byteorder,signed=True))
  65. i = i+INTSIZE
  66. return(res)
  67. # Send a list of Q15
  68. def sendIntList(conn,l):
  69. data = list_to_bytes(l)
  70. sendBytes(conn,data)
  71. # Receive a list of Q15
  72. def getIntList(conn,length):
  73. data = receiveBytes(conn,INTSIZE*length)
  74. return(bytes_to_list(data))
  75. # Low level bytes management
  76. # Return the message ID and the number of bytes expected in the message
  77. def getMsgAndNbOfBytes(data):
  78. msgID = int(data[0])
  79. length= int.from_bytes(data[1:5],byteorder=sys.byteorder,signed=False)
  80. return(msgID,length)
  81. # A client is requesting data from the server. It is the input of VHT
  82. # Client -> Server
  83. def getBufferMsg(conn,nbBytes):
  84. # Ask buffer from server
  85. a=(CLIENTREADBUF).to_bytes(1,byteorder=sys.byteorder)
  86. b=(nbBytes).to_bytes(4,byteorder=sys.byteorder)
  87. msg=a+b
  88. sendBytes(conn,msg)
  89. # Receive buffer from server
  90. data = receiveBytes(conn,nbBytes)
  91. return(data)
  92. # Stop the server when the end of the SDF scheduling has been reached.
  93. # It is to make it easier to end the demo.
  94. # Only the VHT client has to be killed.
  95. # Client -> Server
  96. def stopMsg(conn):
  97. # Send a stop message to server
  98. a=(CLIENTSTOP).to_bytes(1,byteorder=sys.byteorder)
  99. b=(0).to_bytes(4,byteorder=sys.byteorder)
  100. msg=a+b
  101. sendBytes(conn,msg)
  102. # Data in bytes
  103. # A client is sending that some bytes be sent to the server
  104. # It is the output of VHT
  105. # Client -> Server
  106. def writeBufferMsg(conn,theBytes):
  107. # Tell server a buffer is coming
  108. a=(CLIENTWRITEBUF).to_bytes(1,byteorder=sys.byteorder)
  109. nbBytes = len(theBytes)
  110. b=(nbBytes).to_bytes(4,byteorder=sys.byteorder)
  111. msg = a+b+theBytes
  112. # Send message and buffer to server
  113. sendBytes(conn,msg)