Coverage for d7a/system_files/firmware_version.py: 92%

52 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-05-24 08:03 +0200

1# 

2# Copyright (c) 2015-2021 University of Antwerp, Aloxy NV. 

3# 

4# This file is part of pyd7a. 

5# See https://github.com/Sub-IoT/pyd7a for further info. 

6# 

7# Licensed under the Apache License, Version 2.0 (the "License"); 

8# you may not use this file except in compliance with the License. 

9# You may obtain a copy of the License at 

10# 

11# http://www.apache.org/licenses/LICENSE-2.0 

12# 

13# Unless required by applicable law or agreed to in writing, software 

14# distributed under the License is distributed on an "AS IS" BASIS, 

15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18# 

19import struct 

20 

21from d7a.support.schema import Validatable, Types 

22from d7a.system_files.file import File 

23from d7a.system_files.system_file_ids import SystemFileIds 

24 

25 

26class FirmwareVersionFile(File, Validatable): 

27 

28 SCHEMA = [{ 

29 "d7a_protocol_version_major": Types.INTEGER(min=0, max=255), 

30 "d7a_protocol_version_minor": Types.INTEGER(min=0, max=255), 

31 # custom fields, specific to Sub-IoT 

32 "filesystem_version_major": Types.INTEGER(min=0, max=255), 

33 "filesystem_version_minor": Types.INTEGER(min=0, max=255), 

34 "application_name": Types.STRING(maxlength=6), 

35 "git_sha1": Types.STRING(maxlength=7) 

36 }] 

37 

38 def __init__(self, d7a_protocol_version_major=0, d7a_protocol_version_minor=0, filesystem_version_major=0, filesystem_version_minor=0, application_name="", git_sha1=""): 

39 self.d7a_protocol_version_major = d7a_protocol_version_major 

40 self.d7a_protocol_version_minor = d7a_protocol_version_minor 

41 self.filesystem_version_major = filesystem_version_major 

42 self.filesystem_version_minor = filesystem_version_minor 

43 self.application_name = application_name 

44 self.git_sha1 = git_sha1 

45 Validatable.__init__(self) 

46 File.__init__(self, SystemFileIds.FIRMWARE_VERSION.value, 17) 

47 

48 @property 

49 def d7ap_version(self): 

50 return str(self.d7a_protocol_version_major) + '.' + str(self.d7a_protocol_version_minor) 

51 

52 @property 

53 def filesystem_version(self): 

54 return str(self.filesystem_version_major) + '.' + str(self.filesystem_version_minor) 

55 

56 @staticmethod 

57 def parse(s, offset=0, length=17): 

58 version_file = FirmwareVersionFile() 

59 if (offset <= 0) and (length + offset >= 1): 

60 version_file.d7a_protocol_version_major = s.read("uint:8") 

61 if (offset <= 1) and (length + offset >= 2): 

62 version_file.d7a_protocol_version_minor = s.read("uint:8") 

63 if (offset <= 2) and (length + offset >= 3): 

64 version_file.filesystem_version_major = s.read("uint:8") 

65 if (offset <= 3) and (length + offset >= 4): 

66 version_file.filesystem_version_minor = s.read("uint:8") 

67 if (offset <= 4) and (length + offset >= 10): 

68 try: 

69 version_file.application_name = s.read("bytes:6").decode("ascii") 

70 except UnicodeDecodeError: 

71 version_file.application_name = "DecErr" 

72 pass 

73 if (offset <= 10) and (length + offset >= 17): 

74 version_file.git_sha1 = s.read("bytes:7").decode("ascii") 

75 return version_file 

76 

77 def __iter__(self): 

78 yield self.d7a_protocol_version_major 

79 yield self.d7a_protocol_version_minor 

80 

81 yield self.filesystem_version_major 

82 yield self.filesystem_version_minor 

83 

84 for byte in bytearray(self.application_name.encode("ASCII").ljust(6)): 

85 yield byte 

86 

87 for byte in bytearray(self.git_sha1.encode("ASCII").ljust(7)): 

88 yield byte 

89 

90 def __str__(self): 

91 return "d7ap v{}, filesystem v{}, application_name={}, git_sha1={}".format(self.d7ap_version, self.filesystem_version, self.application_name, self.git_sha1)