Coverage for d7a/sp/qos.py: 97%

35 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# 

19 

20# author: Christophe VG <contact@christophe.vg> 

21# class implementation of qos parameters 

22from enum import Enum 

23 

24from d7a.support.schema import Validatable, Types 

25 

26class ResponseMode(Enum): 

27 RESP_MODE_NO = 0 

28 RESP_MODE_ALL = 1 

29 RESP_MODE_ANY = 2 

30 RESP_MODE_NO_RPT = 4 

31 RESP_MODE_ON_ERROR = 5 

32 RESP_MODE_PREFERRED = 6 

33 

34class RetryMode(Enum): 

35 RETRY_MODE_NO = 0 

36 

37class QoS(Validatable): 

38 SCHEMA = [{ 

39 "stop_on_err": Types.BOOLEAN(), 

40 "record" : Types.BOOLEAN(), 

41 "resp_mod" : Types.ENUM(ResponseMode), 

42 "retry_mod": Types.ENUM(RetryMode) 

43 }] 

44 

45 def __init__(self, resp_mod=ResponseMode.RESP_MODE_NO, retry_mod=RetryMode.RETRY_MODE_NO, stop_on_err=False, record=False): 

46 self.resp_mod = resp_mod 

47 self.retry_mod = retry_mod 

48 self.stop_on_err = stop_on_err 

49 self.record = record 

50 super(QoS, self).__init__() 

51 

52 def __iter__(self): 

53 byte = 0 

54 if self.stop_on_err: byte |= 1 << 7 

55 if self.record: byte |= 1 << 6 

56 byte |= self.retry_mod.value << 3 

57 byte += self.resp_mod.value 

58 yield byte 

59 

60 @staticmethod 

61 def parse(s): 

62 stop_on_error = s.read("bool") 

63 record = s.read("bool") 

64 retry_mode = RetryMode(int(s.read("uint:3"))) 

65 resp_mode = ResponseMode(int(s.read("uint:3"))) 

66 return QoS(stop_on_err=stop_on_error, record=record, resp_mod=resp_mode, retry_mod=retry_mode) 

67 

68 def __str__(self): 

69 return str(self.as_dict())