"> 39
+
40
+
41
+async def main():
42
+    async with websockets.connect(
43
+        "ws://localhost:9000/CP_1", subprotocols=["ocpp2.0"]
44
+    ) as ws:
45
+
46
+        cp = ChargePoint("CP_1", ws)
47
+
48
+        await asyncio.gather(cp.start(), cp.send_boot_notification())
49
+
50
+
51
+if __name__ == "__main__":
52
+    # asyncio.run() is used when running this example with Python >= 3.7v
53
+    asyncio.run(main())

+ 79 - 0
ocpp/examples/v201/central_system.py

@@ -0,0 +1,79 @@
1
+import asyncio
2
+import logging
3
+from datetime import datetime
4
+
5
+try:
6
+    import websockets
7
+except ModuleNotFoundError:
8
+    print("This example relies on the 'websockets' package.")
9
+    print("Please install it by running: ")
10
+    print()
11
+    print(" $ pip install websockets")
12
+    import sys
13
+
14
+    sys.exit(1)
15
+
16
+from ocpp.routing import on
17
+from ocpp.v201 import ChargePoint as cp
18
+from ocpp.v201 import call_result
19
+
20
+logging.basicConfig(level=logging.INFO)
21
+
22
+
23
+class ChargePoint(cp):
24
+    @on("BootNotification")
25
+    def on_boot_notification(self, charging_station, reason, **kwargs):
26
+        return call_result.BootNotificationPayload(
27
+            current_time=datetime.utcnow().isoformat(), interval=10, status="Accepted"
28
+        )
29
+
30
+    @on("Heartbeat")
31
+    def on_heartbeat(self):
32
+        print("Got a Heartbeat!")
33
+        return call_result.HeartbeatPayload(
34
+            current_time=datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") + "Z"
35
+        )
36
+
37
+
38
+async def on_connect(websocket, path):
39
+    """For every new charge point that connects, create a ChargePoint
40
+    instance and start listening for messages.
41
+    """
42
+    try:
43
+        requested_protocols = websocket.request_headers["Sec-WebSocket-Protocol"]
44
+    except KeyError:
45
+        logging.error("Client hasn't requested any Subprotocol. Closing Connection")
46
+        return await websocket.close()
47
+    if websocket.subprotocol:
48
+        logging.info("Protocols Matched: %s", websocket.subprotocol)
49
+    else:
50
+        # In the websockets lib if no subprotocols are supported by the
51
+        # client and the server, it proceeds without a subprotocol,
52
+        # so we have to manually close the connection.
53
+        logging.warning(
54
+            "Protocols Mismatched | Expected Subprotocols: %s,"
55
+            " but client supports %s | Closing connection",
56
+            websocket.available_subprotocols,
57
+            requested_protocols,
58
+        )
59
+        return await websocket.close()
60
+
61
+    charge_point_id = path.strip("/")
62
+    charge_point = ChargePoint(charge_point_id, websocket)
63
+
64
+    await charge_point.start()
65
+
66
+
67
+async def main():
68
+    #  deepcode ignore BindToAllNetworkInterfaces: <Example Purposes>
69
+    server = await websockets.serve(
70
+        on_connect, "0.0.0.0", 9000, subprotocols=["ocpp2.0.1"]
71
+    )
72
+
73
+    logging.info("Server Started listening to new connections...")
74
+    await server.wait_closed()
75
+
76
+
77
+if __name__ == "__main__":
78
+    # asyncio.run() is used when running this example with Python >= 3.7v
79
+    asyncio.run(main())

+ 54 - 0
ocpp/examples/v201/charge_point.py

@@ -0,0 +1,54 @@
1
+import asyncio
2
+import logging
3
+
4
+try:
5
+    import websockets
6
+except ModuleNotFoundError:
7
+    print("This example relies on the 'websockets' package.")
8
+    print("Please install it by running: ")
9
+    print()
10
+    print(" $ pip install websockets")
11
+    import sys
12
+
13
+    sys.exit(1)
14
+
15
+
16
+from ocpp.v201 import ChargePoint as cp
17
+from ocpp.v201 import call
18
+
19
+logging.basicConfig(level=logging.INFO)
20
+
21
+
22
+class ChargePoint(cp):
23
+    async def send_heartbeat(self, interval):
24
+        request = call.HeartbeatPayload()
25
+        while True:
26
+            await self.call(request)
27
+            await asyncio.sleep(interval)
28
+
29
+    async def send_boot_notification(self):
30
+        request = call.BootNotificationPayload(
31
+            charging_station={"model": "Wallbox XYZ", "vendor_name": "anewone"},
32
+            reason="PowerUp",
33
+        )
34
+        response = await self.call(request)
35
+
36
+        if response.status == "Accepted":
37
+            print("Connected to central system.")
38
+            await self.send_heartbeat(response.interval)
39
+
40
+
41
+async def main():
42
+    async with websockets.connect(
43
+        "ws://localhost:9000/CP_1", subprotocols=["ocpp2.0.1"]
44
+    ) as ws:
45
+
46
+        charge_point = ChargePoint("CP_1", ws)
47
+        await asyncio.gather(
48
+            charge_point.start(), charge_point.send_boot_notification()
49
+        )
50
+
51
+
52
+if __name__ == "__main__":
53
+    # asyncio.run() is used when running this example with Python >= 3.7v
54
+    asyncio.run(main())

+ 0 - 0
ocpp/ocpp/__init__.py


+ 368 - 0
ocpp/ocpp/charge_point.py

@@ -0,0 +1,368 @@
1
+import asyncio
2
+import inspect
3
+import logging
4
+import re
5
+import time
6
+import uuid
7
+from dataclasses import asdict
8
+from typing import Dict, List, Union
9
+
10
+from ocpp.exceptions import NotImplementedError, NotSupportedError, OCPPError
11
+from ocpp.messages import Call, MessageType, unpack, validate_payload
12
+from ocpp.routing import create_route_map
13
+
14
+LOGGER = logging.getLogger("ocpp")
15
+
16
+
17
+def camel_to_snake_case(data):
18
+    """
19
+    Convert all keys of all dictionaries inside the given argument from
20
+    camelCase to snake_case.
21
+
22
+    Inspired by: https://stackoverflow.com/a/1176023/1073222
23
+
24
+    """
25
+    if isinstance(data, dict):
26
+        snake_case_dict = {}
27
+        for key, value in data.items():
28
+            s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", key)
29
+            key = re.sub("([a-z0-9])([A-Z])(?=\\S)", r"\1_\2", s1).lower()
30
+
31
+            snake_case_dict[key] = camel_to_snake_case(value)
32
+
33
+        return snake_case_dict
34
+
35
+    if isinstance(data, list):
36
+        snake_case_list = []
37
+        for value in data:
38
+            snake_case_list.append(camel_to_snake_case(value))
39
+
40
+        return snake_case_list
41
+
42
+    return data
43
+
44
+
45
+def snake_to_camel_case(data):
46
+    """
47
+    Convert all keys of all dictionaries inside given argument from
48
+    snake_case to camelCase.
49
+
50
+    Inspired by: https://stackoverflow.com/a/19053800/1073222
51
+    """
52
+    if isinstance(data, dict):
53
+        camel_case_dict = {}
54
+        for key, value in data.items():
55
+            key = key.replace("soc", "SoC")
56
+            key = key.replace("_v2x", "V2X")
57
+            components = key.split("_")
58
+            key = components[0] + "".join(x[:1].upper() + x[1:] for x in components[1:])
59
+            camel_case_dict[key] = snake_to_camel_case(value)
60
+
61
+        return camel_case_dict
62
+
63
+    if isinstance(data, list):
64
+        camel_case_list = []
65
+        for value in data:
66
+            camel_case_list.append(snake_to_camel_case(value))
67
+
68
+        return camel_case_list
69
+
70
+    return data
71
+
72
+
73
+def remove_nones(data: Union[List, Dict]) -> Union[List, Dict]:
74
+    if isinstance(data, dict):
75
+        return {k: remove_nones(v) for k, v in data.items() if v is not None}
76
+
77
+    elif isinstance(data, list):
78
+        return [remove_nones(v) for v in data if v is not None]
79
+
80
+    return data
81
+
82
+
83
+def _raise_key_error(action, version):
84
+    """
85
+    Checks whether a keyerror returned by _handle_call
86
+    is supported by the OCPP version or is simply
87
+    not implemented by the server/client and raises
88
+    the appropriate error.
89
+    """
90
+
91
+    from ocpp.v16.enums import Action as v16_Action
92
+    from ocpp.v201.enums import Action as v201_Action
93
+
94
+    if version == "1.6":
95
+        if hasattr(v16_Action, action):
96
+            raise NotImplementedError(
97
+                details={"cause": f"No handler for {action} registered."}
98
+            )
99
+        else:
100
+            raise NotSupportedError(
101
+                details={"cause": f"{action} not supported by OCPP{version}."}
102
+            )
103
+    elif version in ["2.0", "2.0.1"]:
104
+        if hasattr(v201_Action, action):
105
+            raise NotImplementedError(
106
+                details={"cause": f"No handler for {action} registered."}
107
+            )
108
+        else:
109
+            raise NotSupportedError(
110
+                details={"cause": f"{action} not supported by OCPP{version}."}
111
+            )
112
+
113
+    return
114
+
115
+
116
+class ChargePoint:
117
+    """
118
+    Base Element containing all the necessary OCPP1.6J messages for messages
119
+    initiated and received by the Central System
120
+    """
121
+
122
+    def __init__(self, id, connection, response_timeout=30):
123
+        """
124
+
125
+        Args:
126
+
127
+            charger_id (str): ID of the charger.
128
+            connection: Connection to CP.
129
+            response_timeout (int): When no response on a request is received
130
+                within this interval, a asyncio.TimeoutError is raised.
131
+
132
+        """
133
+        self.id = id
134
+
135
+        # The maximum time in seconds it may take for a CP to respond to a
136
+        # CALL. An asyncio.TimeoutError will be raised if this limit has been
137
+        # exceeded.
138
+        self._response_timeout = response_timeout
139
+
140
+        # A connection to the client. Currently this is an instance of gh
141
+        self._connection = connection
142
+
143
+        # A dictionary that hooks for Actions. So if the CS receives a it will
144
+        # look up the Action into this map and execute the corresponding hooks
145
+        # if exists.
146
+        self.route_map = create_route_map(self)
147
+
148
+        self._call_lock = asyncio.Lock()
149
+
150
+        # A queue used to pass CallResults and CallErrors from
151
+        # the self.serve() task to the self.call() task.
152
+        self._response_queue = asyncio.Queue()
153
+
154
+        # Function used to generate unique ids for CALLs. By default
155
+        # uuid.uuid4() is used, but it can be changed. This is meant primarily
156
+        # for testing purposes to have predictable unique ids.
157
+        self._unique_id_generator = uuid.uuid4
158
+
159
+    async def start(self):
160
+        while True:
161
+            message = await self._connection.recv()
162
+            LOGGER.info("%s: receive message %s", self.id, message)
163
+
164
+            await self.route_message(message)
165
+
166
+    async def route_message(self, raw_msg):
167
+        """
168
+        Route a message received from a CP.
169
+
170
+        If the message is a of type Call the corresponding hooks are executed.
171
+        If the message is of type CallResult or CallError the message is passed
172
+        to the call() function via the response_queue.
173
+        """
174
+        try:
175
+            msg = unpack(raw_msg)
176
+        except OCPPError as e:
177
+            LOGGER.exception(
178
+                "Unable to parse message: '%s', it doesn't seem "
179
+                "to be valid OCPP: %s",
180
+                raw_msg,
181
+                e,
182
+            )
183
+            return
184
+
185
+        if msg.message_type_id == MessageType.Call:
186
+            try:
187
+                await self._handle_call(msg)
188
+            except OCPPError as error:
189
+                LOGGER.exception("Error while handling request '%s'", msg)
190
+                response = msg.create_call_error(error).to_json()
191
+                await self._send(response)
192
+
193
+        elif msg.message_type_id in [MessageType.CallResult, MessageType.CallError]:
194
+            self._response_queue.put_nowait(msg)
195
+
196
+    async def _handle_call(self, msg):
197
+        """
198
+        Execute all hooks installed for based on the Action of the message.
199
+
200
+        First the '_on_action' hook is executed and its response is returned to
201
+        the client. If there is no '_on_action' hook for Action in the message
202
+        a CallError with a NotImplementedError is returned. If the Action is
203
+        not supported by the OCPP version a NotSupportedError is returned.
204
+
205
+        Next the '_after_action' hook is executed.
206
+
207
+        """
208
+        try:
209
+            handlers = self.route_map[msg.action]
210
+        except KeyError:
211
+            _raise_key_error(msg.action, self._ocpp_version)
212
+            return
213
+
214
+        if not handlers.get("_skip_schema_validation", False):
215
+            validate_payload(msg, self._ocpp_version)
216
+        # OCPP uses camelCase for the keys in the payload. It's more pythonic
217
+        # to use snake_case for keyword arguments. Therefore the keys must be
218
+        # 'translated'. Some examples:
219
+        #
220
+        # * chargePointVendor becomes charge_point_vendor
221
+        # * firmwareVersion becomes firmwareVersion
222
+        if msg.payload is None:
223
+            msg.payload = {}
224
+        snake_case_payload = camel_to_snake_case(msg.payload)
225
+
226
+        try:
227
+            handler = handlers["_on_action"]
228
+        except KeyError:
229
+            _raise_key_error(msg.action, self._ocpp_version)
230
+
231
+        try:
232
+        
233
+            response = handler(**snake_case_payload)
234
+            if inspect.isawaitable(response):
235
+                response = await response
236
+        except Exception as e:
237
+            LOGGER.exception("Error while handling request '%s'", msg)
238
+            response = msg.create_call_error(e).to_json()
239
+            await self._send(response)
240
+
241
+            return
242
+
243
+        temp_response_payload = asdict(response)
244
+
245
+        # Remove nones ensures that we strip out optional arguments
246
+        # which were not set and have a default value of None
247
+        response_payload = remove_nones(temp_response_payload)
248
+
249
+        # The response payload must be 'translated' from snake_case to
250
+        # camelCase. So:
251
+        #
252
+        # * charge_point_vendor becomes chargePointVendor
253
+        # * firmware_version becomes firmwareVersion
254
+        camel_case_payload = snake_to_camel_case(response_payload)
255
+
256
+        response = msg.create_call_result(camel_case_payload)
257
+
258
+        if not handlers.get("_skip_schema_validation", False):
259
+            validate_payload(response, self._ocpp_version)
260
+
261
+        await self._send(response.to_json())
262
+
263
+        try:
264
+            handler = handlers["_after_action"]
265
+            # Create task to avoid blocking when making a call inside the
266
+            # after handler
267
+            response = handler(**snake_case_payload)
268
+            if inspect.isawaitable(response):
269
+                asyncio.ensure_future(response)
270
+        except KeyError:
271
+            # '_on_after' hooks are not required. Therefore ignore exception
272
+            # when no '_on_after' hook is installed.
273
+            pass
274
+        return response
275
+
276
+    async def call(self, payload, suppress=True, unique_id=None):
277
+        """
278
+        Send Call message to client and return payload of response.
279
+
280
+        The given payload is transformed into a Call object by looking at the
281
+        type of the payload. A payload of type BootNotificationPayload will
282
+        turn in a Call with Action BootNotification, a HeartbeatPayload will
283
+        result in a Call with Action Heartbeat etc.
284
+
285
+        A timeout is raised when no response has arrived before expiring of
286
+        the configured timeout.
287
+
288
+        When waiting for a response no other Call message can be send. So this
289
+        function will wait before response arrives or response timeout has
290
+        expired. This is in line the OCPP specification
291
+
292
+        Suppress is used to maintain backwards compatibility. When set to True,
293
+        if response is a CallError, then this call will be suppressed. When
294
+        set to False, an exception will be raised for users to handle this
295
+        CallError.
296
+
297
+        """
298
+        camel_case_payload = snake_to_camel_case(asdict(payload))
299
+
300
+        unique_id = (
301
+            unique_id if unique_id is not None else str(self._unique_id_generator())
302
+        )
303
+
304
+        call = Call(
305
+            unique_id=unique_id,
306
+            action=payload.__class__.__name__[:-7],
307
+            payload=remove_nones(camel_case_payload),
308
+        )
309
+
310
+        validate_payload(call, self._ocpp_version)
311
+
312
+        # Use a lock to prevent make sure that only 1 message can be send at a
313
+        # a time.
314
+        async with self._call_lock:
315
+            await self._send(call.to_json())
316
+            try:
317
+                response = await self._get_specific_response(
318
+                    call.unique_id, self._response_timeout
319
+                )
320
+            except asyncio.TimeoutError:
321
+                raise asyncio.TimeoutError(
322
+                    f"Waited {self._response_timeout}s for response on "
323
+                    f"{call.to_json()}."
324
+                )
325
+
326
+        if response.message_type_id == MessageType.CallError:
327
+            LOGGER.warning("Received a CALLError: %s'", response)
328
+            if suppress:
329
+                return
330
+            raise response.to_exception()
331
+        else:
332
+            response.action = call.action
333
+            validate_payload(response, self._ocpp_version)
334
+
335
+        snake_case_payload = camel_to_snake_case(response.payload)
336
+        # Create the correct Payload instance based on the received payload. If
337
+        # this method is called with a call.BootNotificationPayload, then it
338
+        # will create a call_result.BootNotificationPayload. If this method is
339
+        # called with a call.HeartbeatPayload, then it will create a
340
+        # call_result.HeartbeatPayload etc.
341
+        cls = getattr(self._call_result, payload.__class__.__name__)  # noqa
342
+        return cls(**snake_case_payload)
343
+
344
+    async def _get_specific_response(self, unique_id, timeout):
345
+        """
346
+        Return response with given unique ID or raise an asyncio.TimeoutError.
347
+        """
348
+        wait_until = time.time() + timeout
349
+        try:
350
+            # Wait for response of the Call message.
351
+            response = await asyncio.wait_for(self._response_queue.get(), timeout)
352
+        except asyncio.TimeoutError:
353
+            raise
354
+
355
+        if response.unique_id == unique_id:
356
+            return response
357
+
358
+        LOGGER.error("Ignoring response with unknown unique id: %s", response)
359
+        timeout_left = wait_until - time.time()
360
+
361
+        if timeout_left < 0:
362
+            raise asyncio.TimeoutError
363
+
364
+        return await self._get_specific_response(unique_id, timeout_left)
365
+
366
+    async def _send(self, message):
367
+        LOGGER.info("%s: send %s", self.id, message)
368
+        await self._connection.send(message)

+ 163 - 0
ocpp/ocpp/exceptions.py

@@ -0,0 +1,163 @@
1
+class OCPPError(Exception):
2
+    """Base class for all OCPP errors. It shouldn't be raised, only it
3
+    subclasses.
4
+    """
5
+
6
+    default_description = ""
7
+
8
+    def __init__(self, description=None, details=None):
9
+        self.description = description
10
+        if description is None:
11
+            self.description = self.default_description
12
+
13
+        self.details = details
14
+        if self.details is None:
15
+            self.details = {}
16
+
17
+    def __eq__(self, other):
18
+        if other.__class__ is self.__class__:
19
+            return (self.description, self.details) == (
20
+                other.description,
21
+                other.details,
22
+            )
23
+
24
+        return NotImplemented
25
+
26
+    def __repr__(self):
27
+        return (
28
+            f"<{self.__class__.__name__} - description={self.description},"
29
+            f" details={self.details}>"
30
+        )
31
+
32
+    def __str__(self):
33
+        return f"{self.__class__.__name__}: {self.description}," f" {self.details}"
34
+
35
+
36
+class NotImplementedError(OCPPError):
37
+    code = "NotImplemented"
38
+    default_description = (
39
+        "Request Action is recognized but not supported by the receiver"
40
+    )
41
+
42
+
43
+class NotSupportedError(OCPPError):
44
+    code = "NotSupported"
45
+    default_description = "Requested Action is not known by receiver"
46
+
47
+
48
+class InternalError(OCPPError):
49
+    code = "InternalError"
50
+    default_description = (
51
+        "An internal error occurred and the receiver was "
52
+        "able to process the requested Action successfully"
53
+    )
54
+
55
+
56
+class ProtocolError(OCPPError):
57
+    code = "ProtocolError"
58
+    default_description = "Payload for Action is incomplete"
59
+
60
+
61
+class SecurityError(OCPPError):
62
+    code = "SecurityError"
63
+    default_description = (
64
+        "During the processing of Action a security issue "
65
+        "occurred preventing receiver from completing the "
66
+        "Action successfully"
67
+    )
68
+
69
+
70
+class FormatViolationError(OCPPError):
71
+    """
72
+    Not strict OCPP 1.6 - see FormationViolationError
73
+    Valid OCPP 2.0.1
74
+    """
75
+
76
+    code = "FormatViolation"
77
+    default_description = (
78
+        "Payload for Action is syntactically incorrect or " "structure for Action"
79
+    )
80
+
81
+
82
+class FormationViolationError(OCPPError):
83
+    """
84
+    To allow for strict OCPP 1.6 compliance
85
+        5. Known issues that will not be fixed
86
+        5.2. Page 14, par 4.2.3. CallError: incorrect name in enum: FormationViolation
87
+        Incorrect name in enum: FormationViolation
88
+    """
89
+
90
+    code = "FormationViolation"
91
+    default_description = (
92
+        "Payload for Action is syntactically incorrect or structure for Action"
93
+    )
94
+
95
+
96
+class PropertyConstraintViolationError(OCPPError):
97
+    code = "PropertyConstraintViolation"
98
+    default_description = (
99
+        "Payload is syntactically correct but at least "
100
+        "one field contains an invalid value"
101
+    )
102
+
103
+
104
+class OccurenceConstraintViolationError(OCPPError):
105
+    """
106
+    To allow for strict OCPP 1.6 compliance
107
+    ocpp-j-1.6-errata-sheet.pdf
108
+        5. Known issues that will not be fixed
109
+        5.1. Page 14, par 4.2.3: CallError: Typo in enum
110
+        Typo in enum: OccurenceConstraintViolation
111
+    Valid in 2.0.1
112
+    """
113
+
114
+    code = "OccurenceConstraintViolation"
115
+    default_description = (
116
+        "Payload for Action is syntactically correct but "
117
+        "at least one of the fields violates occurence "
118
+        "constraints"
119
+    )
120
+
121
+
122
+class OccurrenceConstraintViolationError(OCPPError):
123
+    """
124
+    Not strict OCPP 1.6 - see OccurenceConstraintViolationError
125
+    Not valid OCPP 2.0.1
126
+    Valid in OCPP 2.1
127
+    """
128
+
129
+    code = "OccurrenceConstraintViolation"
130
+    default_description = (
131
+        "Payload for Action is syntactically correct but "
132
+        "at least one of the fields violates occurence "
133
+        "constraints"
134
+    )
135
+
136
+
137
+class TypeConstraintViolationError(OCPPError):
138
+    code = "TypeConstraintViolation"
139
+    default_description = (
140
+        "Payload for Action is syntactically correct but "
141
+        "at least one of the fields violates data type "
142
+        "constraints (e.g. “somestring”: 12)"
143
+    )
144
+
145
+
146
+class GenericError(OCPPError):
147
+    code = "GenericError"
148
+    default_description = "Any other error not all other OCPP defined errors"
149
+
150
+
151
+class ValidationError(Exception):
152
+    """ValidationError should be raised if validation a message payload fails.
153
+
154
+    Note this isn't an official OCPP error!
155
+    """
156
+
157
+    pass
158
+
159
+
160
+class UnknownCallErrorCodeError(Exception):
161
+    """Raised when a CALLERROR is received with unknown error code."""
162
+
163
+    pass

+ 450 - 0
ocpp/ocpp/messages.py

@@ -0,0 +1,450 @@
1
+""" Module containing classes that model the several OCPP messages types. It
2
+also contain some helper functions for packing and unpacking messages.  """
3
+from __future__ import annotations
4
+
5
+import decimal
6
+import json
7
+import os
8
+from dataclasses import asdict, is_dataclass
9
+from typing import Callable, Dict, Union
10
+
11
+from jsonschema import Draft4Validator
12
+from jsonschema.exceptions import ValidationError as SchemaValidationError
13
+
14
+from ocpp.exceptions import (
15
+    FormatViolationError,
16
+    NotImplementedError,
17
+    OCPPError,
18
+    PropertyConstraintViolationError,
19
+    ProtocolError,
20
+    TypeConstraintViolationError,
21
+    UnknownCallErrorCodeError,
22
+    ValidationError,
23
+)
24
+
25
+_validators: Dict[str, Draft4Validator] = {}
26
+
27
+
28
+class _DecimalEncoder(json.JSONEncoder):
29
+    """Encode values of type `decimal.Decimal` using 1 decimal point.
30
+
31
+    A custom encoder is required because `json.dumps()` cannot encode a value
32
+    of type decimal.Decimal. This raises a TypeError:
33
+
34
+        >>> import decimal
35
+        >>> import json
36
+        >>> >>> json.dumps(decimal.Decimal(3))
37
+        Traceback (most recent call last):
38
+          File "<stdin>", line 1, in <module>
39
+          File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/__init__.py", line 231, in dumps  # noqa
40
+            return _default_encoder.encode(obj)
41
+          File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 199, in encode
42
+            chunks = self.iterencode(o, _one_shot=True)
43
+          File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 257, in iterencode
44
+            return _iterencode(o, 0)
45
+          File "/home/developer/.pyenv/versions/3.7.0/lib/python3.7/json/encoder.py", line 179, in default
46
+            raise TypeError(f'Object of type {o.__class__.__name__} '
47
+        TypeError: Object of type Decimal is not JSON serializable
48
+
49
+    This can be prevented by using a custom encoder.
50
+
51
+    """
52
+
53
+    def default(self, obj):
54
+        if isinstance(obj, decimal.Decimal):
55
+            return float("%.1f" % obj)
56
+        try:
57
+            return json.JSONEncoder.default(self, obj)
58
+        except TypeError as e:
59
+            try:
60
+                return obj.to_json()
61
+            except AttributeError:
62
+                raise e
63
+
64
+
65
+class MessageType:
66
+    """Number identifying the different types of OCPP messages."""
67
+
68
+    #: Call identifies a request.
69
+    Call = 2
70
+
71
+    #: CallResult identifies a successful response.
72
+    CallResult = 3
73
+
74
+    #: CallError identifies an erroneous response.
75
+    CallError = 4
76
+
77
+
78
+def unpack(msg):
79
+    """
80
+    Unpacks a message into either a Call, CallError or CallResult.
81
+    """
82
+    try:
83
+        msg = json.loads(msg)
84
+    except json.JSONDecodeError:
85
+        raise FormatViolationError(
86
+            details={"cause": "Message is not valid JSON", "ocpp_message": msg}
87
+        )
88
+
89
+    if not isinstance(msg, list):
90
+        raise ProtocolError(
91
+            details={
92
+                "cause": (
93
+                    "OCPP message hasn't the correct format. It "
94
+                    f"should be a list, but got '{type(msg)}' "
95
+                    "instead"
96
+                )
97
+            }
98
+        )
99
+
100
+    for cls in [Call, CallResult, CallError]:
101
+        try:
102
+            if msg[0] == cls.message_type_id:
103
+                return cls(*msg[1:])
104
+        except IndexError:
105
+            raise ProtocolError(
106
+                details={"cause": "Message does not contain MessageTypeId"}
107
+            )
108
+        except TypeError:
109
+            raise ProtocolError(details={"cause": "Message is missing elements."})
110
+
111
+    raise PropertyConstraintViolationError(
112
+        details={"cause": f"MessageTypeId '{msg[0]}' isn't valid"}
113
+    )
114
+
115
+
116
+def pack(msg):
117
+    """
118
+    Returns the JSON representation of a Call, CallError or CallResult.
119
+
120
+    It just calls the 'to_json()' method of the message. But it is here mainly
121
+    to complement the 'unpack' function of this module.
122
+    """
123
+    return msg.to_json()
124
+
125
+
126
+def get_validator(
127
+    message_type_id: int, action: str, ocpp_version: str, parse_float: Callable = float
128
+) -> Draft4Validator:
129
+    """
130
+    Read schema from disk and return as `Draft4Validator`. Instances will be
131
+    cached for performance reasons.
132
+
133
+    The `parse_float` argument can be used to set the conversion method that
134
+    is used to parse floats. It must be a callable taking 1 argument. By
135
+    default it is `float()`, but certain schema's require `decimal.Decimal()`.
136
+    """
137
+    if ocpp_version not in ["1.6", "2.0", "2.0.1"]:
138
+        raise ValueError
139
+
140
+    schemas_dir = "v" + ocpp_version.replace(".", "")
141
+
142
+    schema_name = action
143
+    if message_type_id == MessageType.CallResult:
144
+        schema_name += "Response"
145
+    elif message_type_id == MessageType.Call:
146
+        if ocpp_version in ["2.0", "2.0.1"]:
147
+            schema_name += "Request"
148
+
149
+    if ocpp_version == "2.0":
150
+        schema_name += "_v1p0"
151
+
152
+    cache_key = schema_name + "_" + ocpp_version
153
+    if cache_key in _validators:
154
+        return _validators[cache_key]
155
+
156
+    dir, _ = os.path.split(os.path.realpath(__file__))
157
+    relative_path = f"{schemas_dir}/schemas/{schema_name}.json"
158
+    path = os.path.join(dir, relative_path)
159
+
160
+    # The JSON schemas for OCPP 2.0 start with a byte order mark (BOM)
161
+    # character. If no encoding is given, reading the schema would fail with:
162
+    #
163
+    #     Unexpected UTF-8 BOM (decode using utf-8-sig):
164
+    with open(path, "r", encoding="utf-8-sig") as f:
165
+        data = f.read()
166
+        validator = Draft4Validator(json.loads(data, parse_float=parse_float))
167
+        _validators[cache_key] = validator
168
+
169
+    return _validators[cache_key]
170
+
171
+
172
+def validate_payload(message: Union[Call, CallResult], ocpp_version: str) -> None:
173
+    """Validate the payload of the message using JSON schemas."""
174
+    if type(message) not in [Call, CallResult]:
175
+        raise ValidationError(
176
+            "Payload can't be validated because message "
177
+            f"type. It's '{type(message)}', but it should "
178
+            "be either 'Call'  or 'CallResult'."
179
+        )
180
+
181
+    try:
182
+        # 3 OCPP 1.6 schedules have fields of type floats. The JSON schema
183
+        # defines a certain precision for these fields of 1 decimal. A value of
184
+        # 21.4 is valid, whereas a value if 4.11 is not.
185
+        #
186
+        # The problem is that Python's internal representation of 21.4 might
187
+        # have more than 1 decimal. It might be 21.399999999999995. This would
188
+        # make the validation fail, although the payload is correct. This is a
189
+        # known issue with jsonschemas, see:
190
+        # https://github.com/Julian/jsonschema/issues/247
191
+        #
192
+        # This issue can be fixed by using a different parser for floats than
193
+        # the default one that is used.
194
+        #
195
+        # Both the schema and the payload must be parsed using the different
196
+        # parser for floats.
197
+        if ocpp_version == "1.6" and (
198
+            (
199
+                type(message) == Call
200
+                and message.action in ["SetChargingProfile", "RemoteStartTransaction"]
201
+            )  # noqa
202
+            or (
203
+                type(message) == CallResult and message.action == "GetCompositeSchedule"
204
+            )
205
+        ):
206
+            validator = get_validator(
207
+                message.message_type_id,
208
+                message.action,
209
+                ocpp_version,
210
+                parse_float=decimal.Decimal,
211
+            )
212
+
213
+            message.payload = json.loads(
214
+                json.dumps(message.payload), parse_float=decimal.Decimal
215
+            )
216
+        else:
217
+            validator = get_validator(
218
+                message.message_type_id, message.action, ocpp_version
219
+            )
220
+    except (OSError, json.JSONDecodeError):
221
+        raise NotImplementedError(
222
+            details={"cause": f"Failed to validate action: {message.action}"}
223
+        )
224
+
225
+    try:
226
+        validator.validate(message.payload)
227
+    except SchemaValidationError as e:
228
+        if e.validator == "type":
229
+            raise TypeConstraintViolationError(
230
+                details={"cause": e.message, "ocpp_message": message}
231
+            )
232
+        elif e.validator == "additionalProperties":
233
+            raise FormatViolationError(
234
+                details={"cause": e.message, "ocpp_message": message}
235
+            )
236
+        elif e.validator == "required":
237
+            raise ProtocolError(details={"cause": e.message})
238
+
239
+        elif e.validator == "maxLength":
240
+            raise TypeConstraintViolationError(
241
+                details={"cause": e.message, "ocpp_message": message}
242
+            ) from e
243
+        else:
244
+            raise FormatViolationError(
245
+                details={
246
+                    "cause": f"Payload '{message.payload}' for action "
247
+                    f"'{message.action}' is not valid: {e}",
248
+                    "ocpp_message": message,
249
+                }
250
+            )
251
+
252
+
253
+class Call:
254
+    """A Call is a type of message that initiate a request/response sequence.
255
+    Both central systems and charge points can send this message.
256
+
257
+    From the specification:
258
+
259
+        A Call always consists of 4 elements: The standard elements
260
+        MessageTypeId and UniqueId, a specific Action that is required on the
261
+        other side and a payload, the arguments to the Action. The syntax of a
262
+        call looks like this:
263
+
264
+            [<MessageTypeId>, "<UniqueId>", "<Action>", {<Payload>}]
265
+
266
+        ...
267
+
268
+        For example, a BootNotification request could look like this:
269
+
270
+            [2,
271
+             "19223201",
272
+             "BootNotification",
273
+             {
274
+              "chargePointVendor": "VendorX",
275
+              "chargePointModel": "SingleSocketCharger"
276
+             }
277
+            ]
278
+    """
279
+
280
+    message_type_id = 2
281
+
282
+    def __init__(self, unique_id, action, payload):
283
+        self.unique_id = unique_id
284
+        self.action = action
285
+        self.payload = payload
286
+
287
+        if is_dataclass(payload):
288
+            self.payload = asdict(payload)
289
+
290
+    def to_json(self):
291
+        """Return a valid JSON representation of the instance."""
292
+        return json.dumps(
293
+            [
294
+                self.message_type_id,
295
+                self.unique_id,
296
+                self.action,
297
+                self.payload,
298
+            ],
299
+            # By default json.dumps() adds a white space after every separator.
300
+            # By setting the separator manually that can be avoided.
301
+            separators=(",", ":"),
302
+            cls=_DecimalEncoder,
303
+        )
304
+
305
+    def create_call_result(self, payload):
306
+        call_result = CallResult(self.unique_id, payload)
307
+        call_result.action = self.action
308
+        return call_result
309
+
310
+    def create_call_error(self, exception):
311
+        error_code = "InternalError"
312
+        error_description = "An unexpected error occurred."
313
+        error_details = {}
314
+
315
+        if isinstance(exception, OCPPError):
316
+            error_code = exception.code
317
+            error_description = exception.description
318
+            error_details = exception.details
319
+
320
+        return CallError(
321
+            self.unique_id,
322
+            error_code,
323
+            error_description,
324
+            error_details,
325
+        )
326
+
327
+    def __repr__(self):
328
+        return (
329
+            f"<Call - unique_id={self.unique_id}, action={self.action}, "
330
+            f"payload={self.payload}>"
331
+        )
332
+
333
+
334
+class CallResult:
335
+    """
336
+    A CallResult is a message indicating that a Call has been handled
337
+    successfully.
338
+
339
+    From the specification:
340
+
341
+        A CallResult always consists of 3 elements: The standard elements
342
+        MessageTypeId, UniqueId and a payload, containing the response to the
343
+        Action in the original Call. The syntax of a call looks like this:
344
+
345
+            [<MessageTypeId>, "<UniqueId>", {<Payload>}]
346
+
347
+        ...
348
+
349
+        For example, a BootNotification response could look like this:
350
+
351
+            [3,
352
+             "19223201",
353
+             {
354
+              "status":"Accepted",
355
+              "currentTime":"2013-02-01T20:53:32.486Z",
356
+              "heartbeatInterval":300
357
+             }
358
+            ]
359
+
360
+    """
361
+
362
+    message_type_id = 3
363
+
364
+    def __init__(self, unique_id, payload, action=None):
365
+        self.unique_id = unique_id
366
+        self.payload = payload
367
+
368
+        # Strictly speaking no action is required in a CallResult. But in order
369
+        # to validate the message it is needed.
370
+        self.action = action
371
+
372
+    def to_json(self):
373
+        return json.dumps(
374
+            [
375
+                self.message_type_id,
376
+                self.unique_id,
377
+                self.payload,
378
+            ],
379
+            # By default json.dumps() adds a white space after every separator.
380
+            # By setting the separator manually that can be avoided.
381
+            separators=(",", ":"),
382
+            cls=_DecimalEncoder,
383
+        )
384
+
385
+    def __repr__(self):
386
+        return (
387
+            f"<CallResult - unique_id={self.unique_id}, "
388
+            f"action={self.action}, "
389
+            f"payload={self.payload}>"
390
+        )
391
+
392
+
393
+class CallError:
394
+    """
395
+    A CallError is a response to a Call that indicates an error.
396
+
397
+    From the specification:
398
+
399
+        CallError always consists of 5 elements: The standard elements
400
+        MessageTypeId and UniqueId, an errorCode string, an errorDescription
401
+        string and an errorDetails object.
402
+
403
+        The syntax of a call looks like this:
404
+
405
+            [<MessageTypeId>, "<UniqueId>", "<errorCode>", "<errorDescription>", {<errorDetails>}] # noqa
406
+    """
407
+
408
+    message_type_id = 4
409
+
410
+    def __init__(self, unique_id, error_code, error_description, error_details=None):
411
+        self.unique_id = unique_id
412
+        self.error_code = error_code
413
+        self.error_description = error_description
414
+        self.error_details = error_details
415
+
416
+    def to_json(self):
417
+        return json.dumps(
418
+            [
419
+                self.message_type_id,
420
+                self.unique_id,
421
+                self.error_code,
422
+                self.error_description,
423
+                self.error_details,
424
+            ],
425
+            # By default json.dumps() adds a white space after every separator.
426
+            # By setting the separator manually that can be avoided.
427
+            separators=(",", ":"),
428
+            cls=_DecimalEncoder,
429
+        )
430
+
431
+    def to_exception(self):
432
+        """Return the exception that corresponds to the CallError."""
433
+        for error in OCPPError.__subclasses__():
434
+            if error.code == self.error_code:
435
+                return error(
436
+                    description=self.error_description, details=self.error_details
437
+                )
438
+
439
+        raise UnknownCallErrorCodeError(
440
+            f"Error code '{self.error_code}' is not defined by the"
441
+            " OCPP specification"
442
+        )
443
+
444
+    def __repr__(self):
445
+        return (
446
+            f"<CallError - unique_id={self.unique_id}, "
447
+            f"error_code={self.error_code}, "
448
+            f"error_description={self.error_description}, "
449
+            f"error_details={self.error_details}>"
450
+        )

+ 139 - 0
ocpp/ocpp/routing.py

@@ -0,0 +1,139 @@
1
+import functools
2
+
3
+routables = []
4
+
5
+
6
+def on(action, *, skip_schema_validation=False):
7
+    """
8
+    Function decorator to mark function as handler for specific action. The
9
+    wrapped function may be async or sync.
10
+
11
+    The handler function will receive keyword arguments derived from the
12
+    payload of the specific action. It's recommended you use `**kwargs` in your
13
+    definition to ignore any extra arguments that may be added in the future.
14
+
15
+    The handler function should return a relevant payload to be returned to the
16
+    Charge Point.
17
+
18
+    It can be used like so:
19
+
20
+    ```
21
+    class MyChargePoint(cp):
22
+        @on(Action.BootNotification):
23
+        async def on_boot_notification(
24
+            self,
25
+            charge_point_model,
26
+            charge_point_vendor,
27
+            **kwargs,
28
+        ):
29
+            print(f'{charge_point_model} from {charge_point_vendor} booted.')
30
+
31
+            now = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S') + "Z"
32
+            return call_result.BootNotificationPayload(
33
+                current_time=now,
34
+                interval=30,
35
+                status="Accepted",
36
+            )
37
+    ```
38
+
39
+    The decorator takes an optional argument `skip_schema_validation` which
40
+    defaults to False. Setting this argument to `True` will disable schema
41
+    validation of the request and the response of the specific route.
42
+
43
+    """
44
+
45
+    def decorator(func):
46
+        @functools.wraps(func)
47
+        def inner(*args, **kwargs):
48
+            return func(*args, **kwargs)
49
+
50
+        inner._on_action = action
51
+        inner._skip_schema_validation = skip_schema_validation
52
+        if func.__name__ not in routables:
53
+            routables.append(func.__name__)
54
+        return inner
55
+
56
+    return decorator
57
+
58
+
59
+def after(action):
60
+    """Function decorator to mark function as hook to post-request hook.
61
+
62
+    This hook's arguments are the data that is in the payload for the specific
63
+    action.
64
+
65
+    It can be used like so:
66
+
67
+        @after(Action.BootNotification):
68
+        def after_boot_notification():
69
+            pass
70
+
71
+    """
72
+
73
+    def decorator(func):
74
+        @functools.wraps(func)
75
+        def inner(*args, **kwargs):
76
+            return func(*args, **kwargs)
77
+
78
+        inner._after_action = action
79
+        if func.__name__ not in routables:
80
+            routables.append(func.__name__)
81
+        return inner
82
+
83
+    return decorator
84
+
85
+
86
+def create_route_map(obj):
87
+    """
88
+    Iterates of all attributes of the class looking for attributes which
89
+    have been decorated by the @on() decorator It returns a dictionary where
90
+    the action name are the keys and the decorated functions are the values.
91
+
92
+    To illustrate this with an example, consider the following function:
93
+
94
+        class ChargePoint:
95
+
96
+            @on(Action.BootNotification)
97
+            def on_boot_notification(self, *args, **kwargs):
98
+                pass
99
+
100
+            @after(Action.BootNotification)
101
+            def after_boot_notification(self, *args, **kwargs):
102
+                pass
103
+
104
+
105
+    In this case this returns:
106
+
107
+        {
108
+            Action.BootNotification: {
109
+                '_on_action': <reference to 'on_boot_notification'>,
110
+                '_after_action': <reference to 'after_boot_notification'>,
111
+                '_skip_schema_validation': False,
112
+            },
113
+        }
114
+
115
+    """
116
+    routes = {}
117
+    for attr_name in routables:
118
+        for option in ["_on_action", "_after_action"]:
119
+            try:
120
+                attr = getattr(obj, attr_name)
121
+                action = getattr(attr, option)
122
+
123
+                if action not in routes:
124
+                    routes[action] = {}
125
+
126
+                # Routes decorated with the `@on()` decorator can be configured
127
+                # to skip validation of the input and output. For more info see
128
+                # the docstring of `on()`.
129
+                if option == "_on_action":
130
+                    routes[action]["_skip_schema_validation"] = getattr(
131
+                        attr, "_skip_schema_validation", False
132
+                    )
133
+
134
+                routes[action][option] = attr
135
+
136
+            except AttributeError:
137
+                continue
138
+
139
+    return routes

+ 8 - 0
ocpp/ocpp/v16/__init__.py

@@ -0,0 +1,8 @@
1
+from ocpp.charge_point import ChargePoint as cp
2
+from ocpp.v16 import call, call_result
3
+
4
+
5
+class ChargePoint(cp):
6
+    _call = call
7
+    _call_result = call_result
8
+    _ocpp_version = "1.6"

+ 303 - 0
ocpp/ocpp/v16/call.py

@@ -0,0 +1,303 @@
1
+from dataclasses import dataclass, field
2
+from typing import Dict, List, Optional
3
+
4
+from ocpp.v16.enums import (
5
+    AvailabilityType,
6
+    CertificateUse,
7
+    ChargePointErrorCode,
8
+    ChargePointStatus,
9
+    ChargingProfilePurposeType,
10
+    ChargingRateUnitType,
11
+    DiagnosticsStatus,
12
+    FirmwareStatus,
13
+    Log,
14
+    MessageTrigger,
15
+    Reason,
16
+    ResetType,
17
+    UpdateType,
18
+    UploadLogStatus,
19
+)
20
+
21
+# Most types of CALL messages can originate from only 1 source, either
22
+# from a Charge Point or Central System, but not from both.
23
+#
24
+# Take for example the CALL for an ChangeConfiguration action. This type of
25
+# CALL can only be send from a Central System to Charging Station, not
26
+# the other way around.
27
+#
28
+# For some types of CALL messages the opposite is true; for example for the
29
+# CALL message for a BootNotification action. This can only come from a Charge
30
+# Point and send to a Central System.
31
+#
32
+# The only CALL that can originate from both a Central System and a
33
+# Charge Point is the CALL message for a DataTransfer.
34
+
35
+# The now following section of classes are for CALL messages that flow
36
+# from Central System to Charge Point.
37
+
38
+
39
+@dataclass
40
+class CancelReservationPayload:
41
+    reservation_id: int
42
+
43
+
44
+@dataclass
45
+class CertificateSignedPayload:
46
+    certificate_chain: str
47
+
48
+
49
+@dataclass
50
+class ChangeAvailabilityPayload:
51
+    connector_id: int
52
+    type: AvailabilityType
53
+
54
+
55
+@dataclass
56
+class ChangeConfigurationPayload:
57
+    key: str
58
+    value: str
59
+
60
+
61
+@dataclass
62
+class ClearCachePayload:
63
+    pass
64
+
65
+
66
+@dataclass
67
+class ClearChargingProfilePayload:
68
+    id: Optional[int] = None
69
+    connector_id: Optional[int] = None
70
+    charging_profile_purpose: Optional[ChargingProfilePurposeType] = None
71
+    stack_level: Optional[int] = None
72
+
73
+
74
+@dataclass
75
+class DeleteCertificatePayload:
76
+    certificate_hash_data: Dict
77
+
78
+
79
+@dataclass
80
+class ExtendedTriggerMessagePayload:
81
+    requested_message: MessageTrigger
82
+    connector_id: Optional[int] = None
83
+
84
+
85
+@dataclass
86
+class GetCompositeSchedulePayload:
87
+    connector_id: int
88
+    duration: int
89
+    charging_rate_unit: Optional[ChargingRateUnitType] = None
90
+
91
+
92
+@dataclass
93
+class GetConfigurationPayload:
94
+    key: Optional[List] = None
95
+
96
+
97
+@dataclass
98
+class GetDiagnosticsPayload:
99
+    location: str
100
+    retries: Optional[int] = None
101
+    retry_interval: Optional[int] = None
102
+    start_time: Optional[str] = None
103
+    stop_time: Optional[str] = None
104
+
105
+
106
+@dataclass
107
+class GetInstalledCertificateIdsPayload:
108
+    certificate_type: CertificateUse
109
+
110
+
111
+@dataclass
112
+class GetLocalListVersionPayload:
113
+    pass
114
+
115
+
116
+@dataclass
117
+class GetLogPayload:
118
+    log: Dict
119
+    log_type: Log
120
+    request_id: int
121
+    retries: Optional[int] = None
122
+    retry_interval: Optional[int] = None
123
+
124
+
125
+@dataclass
126
+class InstallCertificatePayload:
127
+    certificate_type: CertificateUse
128
+    certificate: str
129
+
130
+
131
+@dataclass
132
+class RemoteStartTransactionPayload:
133
+    id_tag: str
134
+    connector_id: Optional[int] = None
135
+    charging_profile: Optional[Dict] = None
136
+
137
+
138
+@dataclass
139
+class RemoteStopTransactionPayload:
140
+    transaction_id: int
141
+
142
+
143
+@dataclass
144
+class ReserveNowPayload:
145
+    connector_id: int
146
+    expiry_date: str
147
+    id_tag: str
148
+    reservation_id: int
149
+    parent_id_tag: Optional[str] = None
150
+
151
+
152
+@dataclass
153
+class ResetPayload:
154
+    type: ResetType
155
+
156
+
157
+@dataclass
158
+class SendLocalListPayload:
159
+    list_version: int
160
+    update_type: UpdateType
161
+    local_authorization_list: List = field(default_factory=list)
162
+
163
+
164
+@dataclass
165
+class SetChargingProfilePayload:
166
+    connector_id: int
167
+    cs_charging_profiles: Dict
168
+
169
+
170
+@dataclass
171
+class SignedUpdateFirmwarePayload:
172
+    request_id: int
173
+    firmware: Dict
174
+    retries: Optional[int] = None
175
+    retry_interval: Optional[int] = None
176
+
177
+
178
+@dataclass
179
+class TriggerMessagePayload:
180
+    requested_message: MessageTrigger
181
+    connector_id: Optional[int] = None
182
+
183
+
184
+@dataclass
185
+class UnlockConnectorPayload:
186
+    connector_id: int
187
+
188
+
189
+@dataclass
190
+class UpdateFirmwarePayload:
191
+    location: str
192
+    retrieve_date: str
193
+    retries: Optional[int] = None
194
+    retry_interval: Optional[int] = None
195
+
196
+
197
+# The CALL messages that flow from Charge Point to Central System are listed
198
+# in the bottom part of this module.
199
+
200
+
201
+@dataclass
202
+class AuthorizePayload:
203
+    id_tag: str
204
+
205
+
206
+@dataclass
207
+class BootNotificationPayload:
208
+    charge_point_model: str
209
+    charge_point_vendor: str
210
+    charge_box_serial_number: Optional[str] = None
211
+    charge_point_serial_number: Optional[str] = None
212
+    firmware_version: Optional[str] = None
213
+    iccid: Optional[str] = None
214
+    imsi: Optional[str] = None
215
+    meter_serial_number: Optional[str] = None
216
+    meter_type: Optional[str] = None
217
+
218
+
219
+@dataclass
220
+class DiagnosticsStatusNotificationPayload:
221
+    status: DiagnosticsStatus
222
+
223
+
224
+@dataclass
225
+class FirmwareStatusNotificationPayload:
226
+    status: FirmwareStatus
227
+
228
+
229
+@dataclass
230
+class HeartbeatPayload:
231
+    pass
232
+
233
+
234
+@dataclass
235
+class LogStatusNotificationPayload:
236
+    status: UploadLogStatus
237
+    request_id: int
238
+
239
+
240
+@dataclass
241
+class MeterValuesPayload:
242
+    connector_id: int
243
+    meter_value: List = field(default_factory=list)
244
+    transaction_id: Optional[int] = None
245
+
246
+
247
+@dataclass
248
+class SecurityEventNotificationPayload:
249
+    type: str
250
+    timestamp: str
251
+    tech_info: Optional[str]
252
+
253
+
254
+@dataclass
255
+class SignCertificatePayload:
256
+    csr: str
257
+
258
+
259
+@dataclass
260
+class SignedFirmwareStatusNotificationPayload:
261
+    status: FirmwareStatus
262
+    request_id: int
263
+
264
+
265
+@dataclass
266
+class StartTransactionPayload:
267
+    connector_id: int
268
+    id_tag: str
269
+    meter_start: int
270
+    timestamp: str
271
+    reservation_id: Optional[int] = None
272
+
273
+
274
+@dataclass
275
+class StopTransactionPayload:
276
+    meter_stop: int
277
+    timestamp: str
278
+    transaction_id: int
279
+    reason: Optional[Reason] = None
280
+    id_tag: Optional[str] = None
281
+    transaction_data: Optional[List] = None
282
+
283
+
284
+@dataclass
285
+class StatusNotificationPayload:
286
+    connector_id: int
287
+    error_code: ChargePointErrorCode
288
+    status: ChargePointStatus
289
+    timestamp: Optional[str] = None
290
+    info: Optional[str] = None
291
+    vendor_id: Optional[str] = None
292
+    vendor_error_code: Optional[str] = None
293
+
294
+
295
+# The DataTransfer CALL can be send both from Central System as well as from a
296
+# Charge Point.
297
+
298
+
299
+@dataclass
300
+class DataTransferPayload:
301
+    vendor_id: str
302
+    message_id: Optional[str] = None
303
+    data: Optional[str] = None

+ 258 - 0
ocpp/ocpp/v16/call_result.py

@@ -0,0 +1,258 @@
1
+from dataclasses import dataclass
2
+from typing import Dict, List, Optional
3
+
4
+from ocpp.v16.datatypes import IdTagInfo
5
+from ocpp.v16.enums import (
6
+    AvailabilityStatus,
7
+    CancelReservationStatus,
8
+    CertificateSignedStatus,
9
+    CertificateStatus,
10
+    ChargingProfileStatus,
11
+    ClearCacheStatus,
12
+    ClearChargingProfileStatus,
13
+    ConfigurationStatus,
14
+    DataTransferStatus,
15
+    DeleteCertificateStatus,
16
+    GenericStatus,
17
+    GetCompositeScheduleStatus,
18
+    GetInstalledCertificateStatus,
19
+    LogStatus,
20
+    RegistrationStatus,
21
+    RemoteStartStopStatus,
22
+    ReservationStatus,
23
+    ResetStatus,
24
+    TriggerMessageStatus,
25
+    UnlockStatus,
26
+    UpdateFirmwareStatus,
27
+    UpdateStatus,
28
+)
29
+
30
+# Most types of CALLRESULT messages can originate from only 1 source, either
31
+# from a Charge Point or Central System, but not from both.
32
+#
33
+# Take for example the CALLRESULT for an Authorize action. This type of
34
+# CALLRESULT can only be send from a Central System to Charging Station, not
35
+# the other way around.
36
+#
37
+# For some types of CALLRESULT messages the opposite is true; for example for
38
+# the CALLRESULT message for a Reset action. This can only come from a Charge
39
+# Point to a Central System.
40
+#
41
+# The only CALLRESULT that can originate from both a Central System and a
42
+# Charge Point is the CALLRESULT message for a DataTransfer.
43
+
44
+# The now following section of classes are for CALLRESULT messages that flow
45
+# from Central System to Charge Point.
46
+
47
+
48
+@dataclass
49
+class AuthorizePayload:
50
+    id_tag_info: IdTagInfo
51
+
52
+
53
+@dataclass
54
+class BootNotificationPayload:
55
+    current_time: str
56
+    interval: int
57
+    status: RegistrationStatus
58
+
59
+
60
+@dataclass
61
+class DiagnosticsStatusNotificationPayload:
62
+    pass
63
+
64
+
65
+@dataclass
66
+class FirmwareStatusNotificationPayload:
67
+    pass
68
+
69
+
70
+@dataclass
71
+class HeartbeatPayload:
72
+    current_time: str
73
+
74
+
75
+@dataclass
76
+class LogStatusNotificationPayload:
77
+    pass
78
+
79
+
80
+@dataclass
81
+class SecurityEventNotificationPayload:
82
+    pass
83
+
84
+
85
+@dataclass
86
+class SignCertificatePayload:
87
+    status: GenericStatus
88
+
89
+
90
+@dataclass
91
+class MeterValuesPayload:
92
+    pass
93
+
94
+
95
+@dataclass
96
+class StartTransactionPayload:
97
+    transaction_id: int
98
+    id_tag_info: IdTagInfo
99
+
100
+
101
+@dataclass
102
+class StatusNotificationPayload:
103
+    pass
104
+
105
+
106
+@dataclass
107
+class StopTransactionPayload:
108
+    id_tag_info: Optional[IdTagInfo] = None
109
+
110
+
111
+# The CALLRESULT messages that flow from Charge Point to Central System are
112
+# listed in the bottom part of this module.
113
+
114
+
115
+@dataclass
116
+class CancelReservationPayload:
117
+    status: CancelReservationStatus
118
+
119
+
120
+@dataclass
121
+class CertificateSignedPayload:
122
+    status: CertificateSignedStatus
123
+
124
+
125
+@dataclass
126
+class ChangeAvailabilityPayload:
127
+    status: AvailabilityStatus
128
+
129
+
130
+@dataclass
131
+class ChangeConfigurationPayload:
132
+    status: ConfigurationStatus
133
+
134
+
135
+@dataclass
136
+class ClearCachePayload:
137
+    status: ClearCacheStatus
138
+
139
+
140
+@dataclass
141
+class ClearChargingProfilePayload:
142
+    status: ClearChargingProfileStatus
143
+
144
+
145
+@dataclass
146
+class DeleteCertificatePayload:
147
+    status: DeleteCertificateStatus
148
+
149
+
150
+@dataclass
151
+class ExtendedTriggerMessagePayload:
152
+    status: TriggerMessageStatus
153
+
154
+
155
+@dataclass
156
+class GetInstalledCertificateIdsPayload:
157
+    status: GetInstalledCertificateStatus
158
+    certificate_hash_data: Optional[List] = None
159
+
160
+
161
+@dataclass
162
+class GetCompositeSchedulePayload:
163
+    status: GetCompositeScheduleStatus
164
+    connector_id: Optional[int] = None
165
+    schedule_start: Optional[str] = None
166
+    charging_schedule: Optional[Dict] = None
167
+
168
+
169
+@dataclass
170
+class GetConfigurationPayload:
171
+    configuration_key: Optional[List] = None
172
+    unknown_key: Optional[List] = None
173
+
174
+
175
+@dataclass
176
+class GetDiagnosticsPayload:
177
+    file_name: Optional[str] = None
178
+
179
+
180
+@dataclass
181
+class GetLocalListVersionPayload:
182
+    list_version: int
183
+
184
+
185
+@dataclass
186
+class GetLogPayload:
187
+    status: LogStatus
188
+    filename: Optional[str] = None
189
+
190
+
191
+@dataclass
192
+class InstallCertificatePayload:
193
+    status: CertificateStatus
194
+
195
+
196
+@dataclass
197
+class RemoteStartTransactionPayload:
198
+    status: RemoteStartStopStatus
199
+
200
+
201
+@dataclass
202
+class RemoteStopTransactionPayload:
203
+    status: RemoteStartStopStatus
204
+
205
+
206
+@dataclass
207
+class ReserveNowPayload:
208
+    status: ReservationStatus
209
+
210
+
211
+@dataclass
212
+class ResetPayload:
213
+    status: ResetStatus
214
+
215
+
216
+@dataclass
217
+class SendLocalListPayload:
218
+    status: UpdateStatus
219
+
220
+
221
+@dataclass
222
+class SetChargingProfilePayload:
223
+    status: ChargingProfileStatus
224
+
225
+
226
+@dataclass
227
+class SignedFirmwareStatusNotificationPayload:
228
+    pass
229
+
230
+
231
+@dataclass
232
+class SignedUpdateFirmwarePayload:
233
+    status: UpdateFirmwareStatus
234
+
235
+
236
+@dataclass
237
+class TriggerMessagePayload:
238
+    status: TriggerMessageStatus
239
+
240
+
241
+@dataclass
242
+class UnlockConnectorPayload:
243
+    status: UnlockStatus
244
+
245
+
246
+@dataclass
247
+class UpdateFirmwarePayload:
248
+    pass
249
+
250
+
251
+# The DataTransfer CALLRESULT can be send both from Central System as well as
252
+# from a Charge Point.
253
+
254
+
255
+@dataclass
256
+class DataTransferPayload:
257
+    status: DataTransferStatus
258
+    data: Optional[str] = None

+ 166 - 0
ocpp/ocpp/v16/datatypes.py

@@ -0,0 +1,166 @@
1
+from dataclasses import dataclass
2
+from typing import List, Optional
3
+
4
+from ocpp.v16.enums import (
5
+    AuthorizationStatus,
6
+    ChargingProfileKindType,
7
+    ChargingProfilePurposeType,
8
+    ChargingRateUnitType,
9
+    CiStringType,
10
+    HashAlgorithm,
11
+    Location,
12
+    Measurand,
13
+    Phase,
14
+    ReadingContext,
15
+    RecurrencyKind,
16
+    UnitOfMeasure,
17
+    ValueFormat,
18
+)
19
+
20
+
21
+@dataclass
22
+class IdTagInfo:
23
+    """
24
+    Contains status information about an identifier. It is returned in
25
+    Authorize, Start Transaction and Stop Transaction responses.
26
+
27
+    If expiryDate is not given, the status has no end date.
28
+    """
29
+
30
+    status: AuthorizationStatus
31
+    parent_id_tag: Optional[str] = None
32
+    expiry_date: Optional[str] = None
33
+
34
+
35
+@dataclass
36
+class AuthorizationData:
37
+    """
38
+    Elements that constitute an entry of a Local Authorization List update.
39
+    """
40
+
41
+    id_tag: str
42
+    id_tag_info: Optional[IdTagInfo] = None
43
+
44
+
45
+@dataclass
46
+class ChargingSchedulePeriod:
47
+    start_period: int
48
+    limit: float
49
+    number_phases: Optional[int] = None
50
+
51
+
52
+@dataclass
53
+class ChargingSchedule:
54
+    charging_rate_unit: ChargingRateUnitType
55
+    charging_schedule_period: List[ChargingSchedulePeriod]
56
+    duration: Optional[int] = None
57
+    start_schedule: Optional[str] = None
58
+    min_charging_rate: Optional[float] = None
59
+
60
+
61
+@dataclass
62
+class ChargingProfile:
63
+    """
64
+    A ChargingProfile consists of a ChargingSchedule, describing the
65
+    amount of power or current that can be delivered per time interval.
66
+    """
67
+
68
+    charging_profile_id: int
69
+    stack_level: int
70
+    charging_profile_purpose: ChargingProfilePurposeType
71
+    charging_profile_kind: ChargingProfileKindType
72
+    charging_schedule: ChargingSchedule
73
+    transaction_id: Optional[int] = None
74
+    recurrency_kind: Optional[RecurrencyKind] = None
75
+    valid_from: Optional[str] = None
76
+    valid_to: Optional[str] = None
77
+
78
+
79
+@dataclass
80
+class KeyValue:
81
+    """
82
+    Contains information about a specific configuration key.
83
+    It is returned in GetConfiguration.conf.
84
+    """
85
+
86
+    key: str
87
+    readonly: bool
88
+    value: Optional[str] = None
89
+
90
+    def __post_init__(self):
91
+        if len(self.key) > CiStringType.ci_string_50:
92
+            msg = "Field key is longer than 50 characters"
93
+            raise ValueError(msg)
94
+
95
+        if self.value and len(self.value) > CiStringType.ci_string_500:
96
+            msg = "Field key is longer than 500 characters"
97
+            raise ValueError(msg)
98
+
99
+
100
+@dataclass
101
+class SampledValue:
102
+    """
103
+    Single sampled value in MeterValues. Each value can be accompanied by
104
+    optional fields.
105
+    """
106
+
107
+    value: str
108
+    context: ReadingContext
109
+    format: Optional[ValueFormat] = None
110
+    measurand: Optional[Measurand] = None
111
+    phase: Optional[Phase] = None
112
+    location: Optional[Location] = None
113
+    unit: Optional[UnitOfMeasure] = None
114
+
115
+
116
+@dataclass
117
+class MeterValue:
118
+    """
119
+    Collection of one or more sampled values in MeterValues.req.
120
+    All sampled values in a MeterValue are sampled at the same point in time.
121
+    """
122
+
123
+    timestamp: str
124
+    sampled_value: List[SampledValue]
125
+
126
+
127
+# Security Extension
128
+
129
+
130
+@dataclass
131
+class CertificateHashData:
132
+    """
133
+    CertificateHashDataType is used by:
134
+    DeleteCertificate.req, GetInstalledCertificateIds.conf
135
+    """
136
+
137
+    hash_algorithm: HashAlgorithm
138
+    issuer_name_hash: str
139
+    issuer_key_hash: str
140
+    serial_number: str
141
+
142
+
143
+@dataclass
144
+class Firmware:
145
+    """
146
+    Represents a copy of the firmware that can be loaded/updated on the Charge Point.
147
+    FirmwareType is used by: SignedUpdateFirmware.req
148
+    """
149
+
150
+    location: str
151
+    retrieve_date_time: str
152
+    signing_certificate: str
153
+    install_date_time: Optional[str] = None
154
+    signature: Optional[str] = None
155
+
156
+
157
+@dataclass
158
+class LogParameters:
159
+    """
160
+    Class for detailed information the retrieval of logging entries.
161
+    LogParametersType is used by: GetLog.req
162
+    """
163
+
164
+    remote_location: str
165
+    oldest_timestamp: Optional[str] = None
166
+    latest_timestamp: Optional[str] = None

+ 852 - 0
ocpp/ocpp/v16/enums.py

@@ -0,0 +1,852 @@
1
+from enum import Enum
2
+
3
+
4
+class Action(str, Enum):
5
+    """An Action is a required part of a Call message."""
6
+
7
+    Authorize = "Authorize"
8
+    BootNotification = "BootNotification"
9
+    CancelReservation = "CancelReservation"
10
+    CertificateSigned = "CertificateSigned"
11
+    ChangeAvailability = "ChangeAvailability"
12
+    ChangeConfiguration = "ChangeConfiguration"
13
+    ClearCache = "ClearCache"
14
+    ClearChargingProfile = "ClearChargingProfile"
15
+    DataTransfer = "DataTransfer"
16
+    DeleteCertificate = "DeleteCertificate"
17
+    DiagnosticsStatusNotification = "DiagnosticsStatusNotification"
18
+    ExtendedTriggerMessage = "ExtendedTriggerMessage"
19
+    FirmwareStatusNotification = "FirmwareStatusNotification"
20
+    GetCompositeSchedule = "GetCompositeSchedule"
21
+    GetConfiguration = "GetConfiguration"
22
+    GetDiagnostics = "GetDiagnostics"
23
+    GetInstalledCertificateIds = "GetInstalledCertificateIds"
24
+    GetLocalListVersion = "GetLocalListVersion"
25
+    GetLog = "GetLog"
26
+    Heartbeat = "Heartbeat"
27
+    InstallCertificate = "InstallCertificate"
28
+    LogStatusNotification = "LogStatusNotification"
29
+    MeterValues = "MeterValues"
30
+    RemoteStartTransaction = "RemoteStartTransaction"
31
+    RemoteStopTransaction = "RemoteStopTransaction"
32
+    ReserveNow = "ReserveNow"
33
+    Reset = "Reset"
34
+    SecurityEventNotification = "SecurityEventNotification"
35
+    SendLocalList = "SendLocalList"
36
+    SetChargingProfile = "SetChargingProfile"
37
+    SignCertificate = "SignCertificate"
38
+    SignedFirmwareStatusNotification = "SignedFirmwareStatusNotification"
39
+    SignedUpdateFirmware = "SignedUpdateFirmware"
40
+    StartTransaction = "StartTransaction"
41
+    StatusNotification = "StatusNotification"
42
+    StopTransaction = "StopTransaction"
43
+    TriggerMessage = "TriggerMessage"
44
+    UnlockConnector = "UnlockConnector"
45
+    UpdateFirmware = "UpdateFirmware"
46
+
47
+
48
+class AuthorizationStatus(str, Enum):
49
+    """
50
+    Elements that constitute an entry of a Local Authorization List update.
51
+    """
52
+
53
+    accepted = "Accepted"
54
+    blocked = "Blocked"
55
+    expired = "Expired"
56
+    invalid = "Invalid"
57
+    concurrent_tx = "ConcurrentTx"
58
+
59
+
60
+class AvailabilityStatus(str, Enum):
61
+    """
62
+    Status returned in response to ChangeAvailability.req.
63
+    """
64
+
65
+    accepted = "Accepted"
66
+    rejected = "Rejected"
67
+    scheduled = "Scheduled"
68
+
69
+
70
+class AvailabilityType(str, Enum):
71
+    """
72
+    Requested availability change in ChangeAvailability.req.
73
+    """
74
+
75
+    inoperative = "Inoperative"
76
+    operative = "Operative"
77
+
78
+
79
+class CancelReservationStatus(str, Enum):
80
+    """
81
+    Status in CancelReservation.conf.
82
+    """
83
+
84
+    accepted = "Accepted"
85
+    rejected = "Rejected"
86
+
87
+
88
+class CertificateSignedStatus(str, Enum):
89
+    """
90
+    CertificateSignedStatusEnumType is used by: CertificateSigned.conf
91
+    """
92
+
93
+    accepted = "Accepted"
94
+    rejected = "Rejected"
95
+
96
+
97
+class CertificateStatus(str, Enum):
98
+    """
99
+    CertificateStatusEnumType is used by: InstallCertificate.conf
100
+    """
101
+
102
+    accepted = "Accepted"
103
+    rejected = "Rejected"
104
+    failed = "Failed"
105
+
106
+
107
+class CertificateUse(str, Enum):
108
+    """
109
+    CertificateUseEnumType is used by: GetInstalledCertificateIds.req,
110
+    InstallCertificate.req
111
+    """
112
+
113
+    central_system_root_certificate = "CentralSystemRootCertificate"
114
+    manufacturer_root_certificate = "ManufacturerRootCertificate"
115
+
116
+
117
+class ChargePointErrorCode(str, Enum):
118
+    """
119
+    Charge Point status reported in StatusNotification.req.
120
+    """
121
+
122
+    connector_lock_failure = "ConnectorLockFailure"
123
+    ev_communication_error = "EVCommunicationError"
124
+    ground_failure = "GroundFailure"
125
+    high_temperature = "HighTemperature"
126
+    internal_error = "InternalError"
127
+    local_list_conflict = "LocalListConflict"
128
+    no_error = "NoError"
129
+    other_error = "OtherError"
130
+    over_current_failure = "OverCurrentFailure"
131
+    over_voltage = "OverVoltage"
132
+    power_meter_failure = "PowerMeterFailure"
133
+    power_switch_failure = "PowerSwitchFailure"
134
+    reader_failure = "ReaderFailure"
135
+    reset_failure = "ResetFailure"
136
+    under_voltage = "UnderVoltage"
137
+    weak_signal = "WeakSignal"
138
+
139
+    # Soon to be deprecated enums
140
+    connectorLockFailure = "ConnectorLockFailure"
141
+    evCommunicationError = "EVCommunicationError"
142
+    groundFailure = "GroundFailure"
143
+    highTemperature = "HighTemperature"
144
+    internalError = "InternalError"
145
+    localListConflict = "LocalListConflict"
146
+    noError = "NoError"
147
+    otherError = "OtherError"
148
+    overCurrentFailure = "OverCurrentFailure"
149
+    overVoltage = "OverVoltage"
150
+    powerMeterFailure = "PowerMeterFailure"
151
+    powerSwitchFailure = "PowerSwitchFailure"
152
+    readerFailure = "ReaderFailure"
153
+    resetFailure = "ResetFailure"
154
+    underVoltage = "UnderVoltage"
155
+    weakSignal = "WeakSignal"
156
+
157
+
158
+class ChargePointStatus(str, Enum):
159
+    """
160
+    Status reported in StatusNotification.req. A status can be reported for
161
+    the Charge Point main controller (connectorId = 0) or for a specific
162
+    connector. Status for the Charge Point main controller is a subset of the
163
+    enumeration: Available, Unavailable or Faulted.
164
+
165
+    States considered Operative are: Available, Preparing, Charging,
166
+    SuspendedEVSE, SuspendedEV, Finishing, Reserved.
167
+    States considered Inoperative are: Unavailable, Faulted.
168
+    """
169
+
170
+    available = "Available"
171
+    preparing = "Preparing"
172
+    charging = "Charging"
173
+    suspended_evse = "SuspendedEVSE"
174
+    suspended_ev = "SuspendedEV"
175
+    finishing = "Finishing"
176
+    reserved = "Reserved"
177
+    unavailable = "Unavailable"
178
+    faulted = "Faulted"
179
+
180
+    # Soon to be deprecated enums
181
+    suspendedevse = "SuspendedEVSE"
182
+    suspendedev = "SuspendedEV"
183
+
184
+
185
+class ChargingProfileKindType(str, Enum):
186
+    """
187
+    "Absolute": Schedule periods are relative to a fixed point in time defined
188
+                in the schedule.
189
+    "Recurring": Schedule restarts periodically at the first schedule period.
190
+    "Relative": Schedule periods are relative to a situation- specific start
191
+                point(such as the start of a session)
192
+    """
193
+
194
+    absolute = "Absolute"
195
+    recurring = "Recurring"
196
+    relative = "Relative"
197
+
198
+
199
+class ChargingProfilePurposeType(str, Enum):
200
+    """
201
+    In load balancing scenarios, the Charge Point has one or more local
202
+    charging profiles that limit the power or current to be shared by all
203
+    connectors of the Charge Point. The Central System SHALL configure such
204
+    a profile with ChargingProfilePurpose set to “ChargePointMaxProfile”.
205
+    ChargePointMaxProfile can only be set at Charge Point ConnectorId 0.
206
+
207
+    Default schedules for new transactions MAY be used to impose charging
208
+    policies. An example could be a policy that prevents charging during
209
+    the day. For schedules of this purpose, ChargingProfilePurpose SHALL
210
+    be set to TxDefaultProfile. If TxDefaultProfile is set to ConnectorId 0,
211
+    the TxDefaultProfile is applicable to all Connectors. If ConnectorId is
212
+    set >0, it only applies to that specific connector. In the event a
213
+    TxDefaultProfile for connector 0 is installed, and the Central
214
+    System sends a new profile with ConnectorId >0, the TxDefaultProfile
215
+    SHALL be replaced only for that specific connector.
216
+
217
+    If a transaction-specific profile with purpose TxProfile is present,
218
+    it SHALL overrule the default charging profile with purpose
219
+    TxDefaultProfile for the duration of the current transaction only.
220
+    After the transaction is stopped, the profile SHOULD be deleted.
221
+    If there is no transaction active on the connector specified in a
222
+    charging profile of type TxProfile, then the Charge Point SHALL
223
+    discard it and return an error status in SetChargingProfile.conf.
224
+    TxProfile SHALL only be set at Charge Point ConnectorId >0.
225
+
226
+    It is not possible to set a ChargingProfile with purpose set to
227
+    TxProfile without presence of an active transaction, or in advance of
228
+    a transaction.
229
+
230
+    In order to ensure that the updated charging profile applies only to the
231
+    current transaction, the chargingProfilePurpose of the ChargingProfile
232
+    MUST be set to TxProfile.
233
+    """
234
+
235
+    charge_point_max_profile = "ChargePointMaxProfile"
236
+    tx_default_profile = "TxDefaultProfile"
237
+    tx_profile = "TxProfile"
238
+
239
+    # Soon to be deprecated enums
240
+    chargepointmaxprofile = "ChargePointMaxProfile"
241
+    txdefaultprofile = "TxDefaultProfile"
242
+    txprofile = "TxProfile"
243
+
244
+
245
+class ChargingProfileStatus(str, Enum):
246
+    """
247
+    Status returned in response to SetChargingProfile.req.
248
+    """
249
+
250
+    accepted = "Accepted"
251
+    rejected = "Rejected"
252
+    not_supported = "NotSupported"
253
+    # Soon to be deprecated enums
254
+    notSupported = "NotSupported"
255
+
256
+
257
+class ChargingRateUnitType(str, Enum):
258
+    """
259
+    Unit in which a charging schedule is defined, as used in:
260
+    GetCompositeSchedule.req and ChargingSchedule
261
+    """
262
+
263
+    watts = "W"
264
+    amps = "A"
265
+
266
+
267
+class CiStringType(int):
268
+    """
269
+    Generic used case insensitive string of X characters
270
+    """
271
+
272
+    ci_string_20 = 20
273
+    ci_string_25 = 25
274
+    ci_string_50 = 50
275
+    ci_string_255 = 255
276
+    ci_string_500 = 500
277
+
278
+
279
+class ClearCacheStatus(str, Enum):
280
+    """
281
+    Status returned in response to ClearCache.req.
282
+    """
283
+
284
+    accepted = "Accepted"
285
+    rejected = "Rejected"
286
+
287
+
288
+class ClearChargingProfileStatus(str, Enum):
289
+    """
290
+    Status returned in response to ClearChargingProfile.req.
291
+    """
292
+
293
+    accepted = "Accepted"
294
+    unknown = "Unknown"
295
+
296
+
297
+class ConfigurationStatus(str, Enum):
298
+    """
299
+    Status in ChangeConfiguration.conf.
300
+    """
301
+
302
+    accepted = "Accepted"
303
+    rejected = "Rejected"
304
+    reboot_required = "RebootRequired"
305
+    not_supported = "NotSupported"
306
+
307
+    # Soon to be deprecated enums
308
+    rebootRequired = "RebootRequired"
309
+    notSupported = "NotSupported"
310
+
311
+
312
+class ConfigurationKey(str, Enum):
313
+    """
314
+    Configuration Key Names.
315
+    """
316
+
317
+    # 9.1 Core Profile
318
+    allow_offline_tx_for_unknown_id = "AllowOfflineTxForUnknownId"
319
+    authorization_cache_enabled = "AuthorizationCacheEnabled"
320
+    authorize_remote_tx_requests = "AuthorizeRemoteTxRequests"
321
+    blink_repeat = "BlinkRepeat"
322
+    clock_aligned_data_interval = "ClockAlignedDataInterval"
323
+    connection_time_out = "ConnectionTimeOut"
324
+    connector_phase_rotation = "ConnectorPhaseRotation"
325
+    connector_phase_rotation_max_length = "ConnectorPhaseRotationMaxLength"
326
+    get_configuration_max_keys = "GetConfigurationMaxKeys"
327
+    heartbeat_interval = "HeartbeatInterval"
328
+    light_intensity = "LightIntensity"
329
+    local_authorize_offline = "LocalAuthorizeOffline"
330
+    local_pre_authorize = "LocalPreAuthorize"
331
+    max_energy_on_invalid_id = "MaxEnergyOnInvalidId"
332
+    meter_values_aligned_data = "MeterValuesAlignedData"
333
+    meter_values_aligned_data_max_length = "MeterValuesAlignedDataMaxLength"
334
+    meter_values_sampled_data = "MeterValuesSampledData"
335
+    meter_values_sampled_data_max_length = "MeterValuesSampledDataMaxLength"
336
+    meter_value_sample_interval = "MeterValueSampleInterval"
337
+    minimum_status_duration = "MinimumStatusDuration"
338
+    number_of_connectors = "NumberOfConnectors"
339
+    reset_retries = "ResetRetries"
340
+    stop_transaction_on_ev_side_disconnect = "StopTransactionOnEVSideDisconnect"
341
+    stop_transaction_on_invalid_id = "StopTransactionOnInvalidId"
342
+    stop_txn_aligned_data = "StopTxnAlignedData"
343
+    stop_txn_aligned_data_max_length = "StopTxnAlignedDataMaxLength"
344
+    stop_txn_sampled_data = "StopTxnSampledData"
345
+    stop_txn_sampled_data_max_length = "StopTxnSampledDataMaxLength"
346
+    supported_feature_profiles = "SupportedFeatureProfiles"
347
+    supported_feature_profiles_max_length = "SupportedFeatureProfilesMaxLength"
348
+    transaction_message_attempts = "TransactionMessageAttempts"
349
+    transaction_message_retry_interval = "TransactionMessageRetryInterval"
350
+    unlock_connector_on_ev_side_disconnect = "UnlockConnectorOnEVSideDisconnect"
351
+    web_socket_ping_interval = "WebSocketPingInterval"
352
+
353
+    # 9.2 Local Auth List Management Profile
354
+    local_auth_list_enabled = "LocalAuthListEnabled"
355
+    local_auth_list_max_length = "LocalAuthListMaxLength"
356
+    send_local_list_max_length = "SendLocalListMaxLength"
357
+
358
+    # 9.3 Reservation Profile
359
+    reserve_connector_zero_supported = "ReserveConnectorZeroSupported"
360
+
361
+    # 9.4 Smart Charging Profile
362
+    charge_profile_max_stack_level = "ChargeProfileMaxStackLevel"
363
+    charging_schedule_allowed_charging_rate_unit = (
364
+        "ChargingScheduleAllowedChargingRateUnit"
365
+    )
366
+    charging_schedule_max_periods = "ChargingScheduleMaxPeriods"
367
+    connector_switch_3to1_phase_supported = "ConnectorSwitch3to1PhaseSupported"
368
+    max_charging_profiles_installed = "MaxChargingProfilesInstalled"
369
+
370
+    # OCPP 1.6 ISO 15118 v10 added configuration keys
371
+    central_contract_validation_allowed = "CentralContractValidationAllowed"
372
+    certificate_signed_max_chain_size = "CertificateSignedMaxChainSize"
373
+    cert_signing_wait_minimum = "CertSigningWaitMinimum"
374
+    cert_signing_repeat_times = "CertSigningRepeatTimes"
375
+    certificate_store_max_length = "CertificateStoreMaxLength"
376
+    contract_validation_offline = "ContractValidationOffline"
377
+    iso_15118_pnc_enabled = "ISO15118PnCEnabled"
378
+
379
+    # OCPP security whitepaper added configuration keys
380
+    additional_root_certificate_check = "AdditionalRootCertificateCheck"
381
+    authorization_key = "AuthorizationKey"
382
+    cpo_name = "CpoName"
383
+    security_profile = "SecurityProfile"
384
+
385
+
386
+class DataTransferStatus(str, Enum):
387
+    """
388
+    Status in DataTransfer.conf.
389
+    """
390
+
391
+    accepted = "Accepted"
392
+    rejected = "Rejected"
393
+    unknown_message_id = "UnknownMessageId"
394
+    unknown_vendor_id = "UnknownVendorId"
395
+
396
+    # Soon to be deprecated enums
397
+    unknownMessageId = "UnknownMessageId"
398
+    unknownVendorId = "UnknownVendorId"
399
+
400
+
401
+class DeleteCertificateStatus(str, Enum):
402
+    """
403
+    DeleteCertificateStatusEnumType is used by: DeleteCertificate.conf
404
+    """
405
+
406
+    accepted = "Accepted"
407
+    failed = "Failed"
408
+    not_found = "NotFound"
409
+
410
+
411
+class DiagnosticsStatus(str, Enum):
412
+    """
413
+    Status in DiagnosticsStatusNotification.req.
414
+    """
415
+
416
+    idle = "Idle"
417
+    uploaded = "Uploaded"
418
+    upload_failed = "UploadFailed"
419
+    uploading = "Uploading"
420
+
421
+    # Soon to be deprecated enums
422
+    uploadFailed = "UploadFailed"
423
+
424
+
425
+class FirmwareStatus(str, Enum):
426
+    """
427
+    Status of a firmware download as reported in FirmwareStatusNotification.req
428
+    """
429
+
430
+    # Common for:
431
+    # FirmwareStatusNotification.req and SignedFirmwareStatusNotification.req
432
+    downloaded = "Downloaded"
433
+    download_failed = "DownloadFailed"
434
+    downloading = "Downloading"
435
+    idle = "Idle"
436
+    installation_failed = "InstallationFailed"
437
+    installing = "Installing"
438
+    installed = "Installed"
439
+
440
+    # Only for SignedFirmwareStatusNotification.reg
441
+    download_scheduled = "DownloadScheduled"
442
+    download_paused = "DownloadPaused"
443
+    install_rebooting = "InstallRebooting"
444
+    install_scheduled = "InstallScheduled"
445
+    install_verification_failed = "InstallVerificationFailed"
446
+    invalid_signature = "InvalidSignature"
447
+    signature_verified = "SignatureVerified"
448
+
449
+    # Soon to be deprecated enums
450
+    downloadFailed = "DownloadFailed"
451
+    installationFailed = "InstallationFailed"
452
+
453
+
454
+class GenericStatus(str, Enum):
455
+    """
456
+    Generic message response status
457
+    """
458
+
459
+    accepted = "Accepted"
460
+    rejected = "Rejected"
461
+
462
+
463
+class GetCompositeScheduleStatus(str, Enum):
464
+    """
465
+    Status returned in response to GetCompositeSchedule.req
466
+    """
467
+
468
+    accepted = "Accepted"
469
+    rejected = "Rejected"
470
+
471
+
472
+class GetInstalledCertificateStatus(str, Enum):
473
+    """
474
+    GetInstalledCertificateStatusEnumType is used by:
475
+    GetInstalledCertificateIds.conf
476
+    """
477
+
478
+    accepted = "Accepted"
479
+    not_found = "NotFound"
480
+
481
+
482
+class HashAlgorithm(str, Enum):
483
+    """
484
+    HashAlgorithmEnumType is used by: CertificateHashDataType
485
+    """
486
+
487
+    sha256 = "SHA256"
488
+    sha384 = "SHA384"
489
+    sha512 = "SHA512"
490
+
491
+
492
+class Location(str, Enum):
493
+    """
494
+    Allowable values of the optional "location" field of a value element in
495
+    SampledValue.
496
+    """
497
+
498
+    inlet = "Inlet"
499
+    outlet = "Outlet"
500
+    body = "Body"
501
+    cable = "Cable"
502
+    ev = "EV"
503
+
504
+
505
+class Log(str, Enum):
506
+    """
507
+    LogEnumType is used by GetLog.req
508
+    """
509
+
510
+    diagnostics_log = "DiagnosticsLog"
511
+    security_log = "SecurityLog"
512
+
513
+
514
+class LogStatus(str, Enum):
515
+    """
516
+    LogStatusEnumType is used by: GetLog.conf
517
+    """
518
+
519
+    accepted = "Accepted"
520
+    rejected = "Rejected"
521
+    accepted_canceled = "AcceptedCanceled"
522
+
523
+
524
+class Measurand(str, Enum):
525
+    """
526
+    Allowable values of the optional "measurand" field of a Value element, as
527
+    used in MeterValues.req and StopTransaction.req messages. Default value of
528
+    "measurand" is always "Energy.Active.Import.Register"
529
+    """
530
+
531
+    current_export = "Current.Export"
532
+    current_import = "Current.Import"
533
+    current_offered = "Current.Offered"
534
+    energy_active_export_register = "Energy.Active.Export.Register"
535
+    energy_active_import_register = "Energy.Active.Import.Register"
536
+    energy_reactive_export_register = "Energy.Reactive.Export.Register"
537
+    energy_reactive_import_register = "Energy.Reactive.Import.Register"
538
+    energy_active_export_interval = "Energy.Active.Export.Interval"
539
+    energy_active_import_interval = "Energy.Active.Import.Interval"
540
+    energy_reactive_export_interval = "Energy.Reactive.Export.Interval"
541
+    energy_reactive_import_interval = "Energy.Reactive.Import.Interval"
542
+    frequency = "Frequency"
543
+    power_active_export = "Power.Active.Export"
544
+    power_active_import = "Power.Active.Import"
545
+    power_factor = "Power.Factor"
546
+    power_offered = "Power.Offered"
547
+    power_reactive_export = "Power.Reactive.Export"
548
+    power_reactive_import = "Power.Reactive.Import"
549
+    rpm = "RPM"
550
+    soc = "SoC"
551
+    temperature = "Temperature"
552
+    voltage = "Voltage"
553
+
554
+    # Soon to be deprecated enums
555
+    currentExport = "Current.Export"
556
+    currentImport = "Current.Import"
557
+    currentOffered = "Current.Offered"
558
+    energyActiveExportRegister = "Energy.Active.Export.Register"
559
+    energyActiveImportRegister = "Energy.Active.Import.Register"
560
+    energyReactiveExportRegister = "Energy.Reactive.Export.Register"
561
+    energyReactiveImportRegister = "Energy.Reactive.Import.Register"
562
+    energyActiveExportInterval = "Energy.Active.Export.Interval"
563
+    energyActiveImportInterval = "Energy.Active.Import.Interval"
564
+    energyReactiveExportInterval = "Energy.Reactive.Export.Interval"
565
+    energyReactiveImportInterval = "Energy.Reactive.Import.Interval"
566
+    powerActiveExport = "Power.Active.Export"
567
+    powerActiveImport = "Power.Active.Import"
568
+    powerFactor = "Power.Factor"
569
+    powerOffered = "Power.Offered"
570
+    powerReactiveExport = "Power.Reactive.Export"
571
+    powerReactiveImport = "Power.Reactive.Import"
572
+
573
+
574
+class MessageTrigger(str, Enum):
575
+    """
576
+    Type of request to be triggered in a TriggerMessage.req
577
+    """
578
+
579
+    # Common for TriggerMessage.req and ExtendedTriggerMessage.req
580
+    boot_notification = "BootNotification"
581
+    firmware_status_notification = "FirmwareStatusNotification"
582
+    heartbeat = "Heartbeat"
583
+    meter_values = "MeterValues"
584
+    status_notification = "StatusNotification"
585
+
586
+    # Only for TriggerMessage.req
587
+    diagnostics_status_notification = "DiagnosticsStatusNotification"
588
+
589
+    # Only for ExtendedTriggerMessage.req
590
+    log_status_notification = "LogStatusNotification"
591
+    sign_charge_point_certificate = "SignChargePointCertificate"
592
+
593
+    # Soon to be deprecated enums
594
+    bootNotification = "BootNotification"
595
+    diagnosticsStatusNotification = "DiagnosticsStatusNotification"
596
+    firmwareStatusNotification = "FirmwareStatusNotification"
597
+    meterValues = "MeterValues"
598
+    statusNotification = "StatusNotification"
599
+
600
+
601
+class Phase(str, Enum):
602
+    """
603
+    Phase as used in SampledValue. Phase specifies how a measured value is to
604
+    be interpreted. Please note that not all values of Phase are applicable to
605
+    all Measurands.
606
+    """
607
+
608
+    l1 = "L1"
609
+    l2 = "L2"
610
+    l3 = "L3"
611
+    n = "N"
612
+    l1_n = "L1-N"
613
+    l2_n = "L2-N"
614
+    l3_n = "L3-N"
615
+    l1_l2 = "L1-L2"
616
+    l2_l3 = "L2-L3"
617
+    l3_l1 = "L3-L1"
618
+
619
+    # Soon to be deprecated enums
620
+    l1n = "L1-N"
621
+    l2n = "L2-N"
622
+    l3n = "L3-N"
623
+    l1l2 = "L1-L2"
624
+    l2l3 = "L2-L3"
625
+    l3l1 = "L3-L1"
626
+
627
+
628
+class ReadingContext(str, Enum):
629
+    """
630
+    Values of the context field of a value in SampledValue.
631
+    """
632
+
633
+    interruption_begin = "Interruption.Begin"
634
+    interruption_end = "Interruption.End"
635
+    other = "Other"
636
+    sample_clock = "Sample.Clock"
637
+    sample_periodic = "Sample.Periodic"
638
+    transaction_begin = "Transaction.Begin"
639
+    transaction_end = "Transaction.End"
640
+    trigger = "Trigger"
641
+
642
+    # Soon to be deprecated enums
643
+    interruptionBegin = "Interruption.Begin"
644
+    interruptionEnd = "Interruption.End"
645
+    sampleClock = "Sample.Clock"
646
+    samplePeriodic = "Sample.Periodic"
647
+    transactionBegin = "Transaction.Begin"
648
+    transactionEnd = "Transaction.End"
649
+
650
+
651
+class Reason(str, Enum):
652
+    """
653
+    Reason for stopping a transaction in StopTransaction.req.
654
+    """
655
+
656
+    emergency_stop = "EmergencyStop"
657
+    ev_disconnected = "EVDisconnected"
658
+    hard_reset = "HardReset"
659
+    local = "Local"
660
+    other = "Other"
661
+    power_loss = "PowerLoss"
662
+    reboot = "Reboot"
663
+    remote = "Remote"
664
+    soft_reset = "SoftReset"
665
+    unlock_command = "UnlockCommand"
666
+    de_authorized = "DeAuthorized"
667
+
668
+    # Soon to be deprecated enums
669
+    emergencyStop = "EmergencyStop"
670
+    evDisconnected = "EVDisconnected"
671
+    hardReset = "HardReset"
672
+    powerLoss = "PowerLoss"
673
+    softReset = "SoftReset"
674
+    unlockCommand = "UnlockCommand"
675
+    deAuthorized = "DeAuthorized"
676
+
677
+
678
+class RecurrencyKind(str, Enum):
679
+    """
680
+    "Daily": The schedule restarts at the beginning of the next day.
681
+    "Weekly": The schedule restarts at the beginning of the next week
682
+              (defined as Monday morning)
683
+    """
684
+
685
+    daily = "Daily"
686
+    weekly = "Weekly"
687
+
688
+
689
+class RegistrationStatus(str, Enum):
690
+    """
691
+    Result of registration in response to BootNotification.req.
692
+    """
693
+
694
+    accepted = "Accepted"
695
+    pending = "Pending"
696
+    rejected = "Rejected"
697
+
698
+
699
+class RemoteStartStopStatus(str, Enum):
700
+    """
701
+    The result of a RemoteStartTransaction.req or RemoteStopTransaction.req
702
+    request.
703
+    """
704
+
705
+    accepted = "Accepted"
706
+    rejected = "Rejected"
707
+
708
+
709
+class ReservationStatus(str, Enum):
710
+    """
711
+    Status in ReserveNow.conf.
712
+    """
713
+
714
+    accepted = "Accepted"
715
+    faulted = "Faulted"
716
+    occupied = "Occupied"
717
+    rejected = "Rejected"
718
+    unavailable = "Unavailable"
719
+
720
+
721
+class ResetStatus(str, Enum):
722
+    """
723
+    Result of Reset.req
724
+    """
725
+
726
+    accepted = "Accepted"
727
+    rejected = "Rejected"
728
+
729
+
730
+class ResetType(str, Enum):
731
+    """
732
+    Type of reset requested by Reset.req
733
+    """
734
+
735
+    hard = "Hard"
736
+    soft = "Soft"
737
+
738
+
739
+class TriggerMessageStatus(str, Enum):
740
+    """
741
+    Status in TriggerMessage.conf.
742
+    """
743
+
744
+    accepted = "Accepted"
745
+    rejected = "Rejected"
746
+    not_implemented = "NotImplemented"
747
+
748
+    # Soon to be deprecated enums
749
+    notImplemented = "NotImplemented"
750
+
751
+
752
+class UnitOfMeasure(str, Enum):
753
+    """
754
+    Allowable values of the optional "unit" field of a Value element, as used
755
+    in MeterValues.req and StopTransaction.req messages. Default value of
756
+    "unit" is always "Wh".
757
+    """
758
+
759
+    wh = "Wh"
760
+    kwh = "kWh"
761
+    varh = "varh"
762
+    kvarh = "kvarh"
763
+    w = "W"
764
+    kw = "kW"
765
+    va = "VA"
766
+    kva = "kVA"
767
+    var = "var"
768
+    kvar = "kvar"
769
+    a = "A"
770
+    v = "V"
771
+    celsius = "Celsius"
772
+    fahrenheit = "Fahrenheit"
773
+    k = "K"
774
+    percent = "Percent"
775
+    hertz = "Hertz"
776
+
777
+
778
+class UnlockStatus(str, Enum):
779
+    """
780
+    Status in response to UnlockConnector.req.
781
+    """
782
+
783
+    unlocked = "Unlocked"
784
+    unlock_failed = "UnlockFailed"
785
+    not_supported = "NotSupported"
786
+
787
+    # Soon to be deprecated enums
788
+    unlockFailed = "UnlockFailed"
789
+    notSupported = "NotSupported"
790
+
791
+
792
+class UpdateFirmwareStatus(str, Enum):
793
+    """
794
+    UpdateFirmwareStatusEnumType is used by: SignedUpdateFirmware.conf
795
+    """
796
+
797
+    accepted = "Accepted"
798
+    rejected = "Rejected"
799
+    accepted_canceled = "AcceptedCanceled"
800
+    invalid_certificate = "InvalidCertificate"
801
+    revoked_certificate = "RevokedCertificate"
802
+
803
+
804
+class UploadLogStatus(str, Enum):
805
+    """
806
+    UploadLogStatusEnumType is used by: LogStatusNotification.req
807
+    """
808
+
809
+    bad_message = "BadMessage"
810
+    idle = "Idle"
811
+    not_supported_operation = "NotSupportedOperation"
812
+    permission_denied = "PermissionDenied"
813
+    uploaded = "Uploaded"
814
+    upload_failure = "UploadFailure"
815
+    uploading = "Uploading"
816
+
817
+
818
+class UpdateStatus(str, Enum):
819
+    """
820
+    Type of update for a SendLocalList.req.
821
+    """
822
+
823
+    accepted = "Accepted"
824
+    failed = "Failed"
825
+    not_supported = "NotSupported"
826
+    version_mismatch = "VersionMismatch"
827
+
828
+    # Soon to be deprecated enums
829
+    notSupported = "NotSupported"
830
+    versionMismatch = "VersionMismatch"
831
+
832
+
833
+class UpdateType(str, Enum):
834
+    """
835
+    Type of update for a SendLocalList.req.
836
+    """
837
+
838
+    differential = "Differential"
839
+    full = "Full"
840
+
841
+
842
+class ValueFormat(str, Enum):
843
+    """
844
+    Format that specifies how the value element in SampledValue is to be
845
+    interpreted.
846
+    """
847
+
848
+    raw = "Raw"
849
+    signed_data = "SignedData"
850
+
851
+    # Soon to be deprecated enums
852
+    signedData = "SignedData"

+ 15 - 0
ocpp/ocpp/v16/schemas/Authorize.json

@@ -0,0 +1,15 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "AuthorizeRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "idTag": {
7
+            "type": "string",
8
+            "maxLength": 20
9
+        }
10
+    },
11
+    "additionalProperties": false,
12
+    "required": [
13
+        "idTag"
14
+    ]
15
+}

+ 39 - 0
ocpp/ocpp/v16/schemas/AuthorizeResponse.json

@@ -0,0 +1,39 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "AuthorizeResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "idTagInfo": {
7
+            "type": "object",
8
+            "properties": {
9
+                "expiryDate": {
10
+                    "type": "string",
11
+                    "format": "date-time"
12
+                },
13
+                "parentIdTag": {
14
+                    "type": "string",
15
+                    "maxLength": 20
16
+                },
17
+                "status": {
18
+                    "type": "string",
19
+                    "additionalProperties": false,
20
+                    "enum": [
21
+                        "Accepted",
22
+                        "Blocked",
23
+                        "Expired",
24
+                        "Invalid",
25
+                        "ConcurrentTx"
26
+                    ]
27
+                }
28
+            },
29
+            "additionalProperties": false,
30
+            "required": [
31
+                "status"
32
+            ]
33
+        }
34
+    },
35
+    "additionalProperties": false,
36
+    "required": [
37
+        "idTagInfo"
38
+    ]
39
+}

+ 48 - 0
ocpp/ocpp/v16/schemas/BootNotification.json

@@ -0,0 +1,48 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "BootNotificationRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "chargePointVendor": {
7
+            "type": "string",
8
+            "maxLength": 20
9
+        },
10
+        "chargePointModel": {
11
+            "type": "string",
12
+            "maxLength": 20
13
+        },
14
+        "chargePointSerialNumber": {
15
+            "type": "string",
16
+            "maxLength": 25
17
+        },
18
+        "chargeBoxSerialNumber": {
19
+            "type": "string",
20
+            "maxLength": 25
21
+        },
22
+        "firmwareVersion": {
23
+            "type": "string",
24
+            "maxLength": 50
25
+        },
26
+        "iccid": {
27
+            "type": "string",
28
+            "maxLength": 20
29
+        },
30
+        "imsi": {
31
+            "type": "string",
32
+            "maxLength": 20
33
+        },
34
+        "meterType": {
35
+            "type": "string",
36
+            "maxLength": 25
37
+        },
38
+        "meterSerialNumber": {
39
+            "type": "string",
40
+            "maxLength": 25
41
+        }
42
+    },
43
+    "additionalProperties": false,
44
+    "required": [
45
+        "chargePointVendor",
46
+        "chargePointModel"
47
+    ]
48
+}

+ 29 - 0
ocpp/ocpp/v16/schemas/BootNotificationResponse.json

@@ -0,0 +1,29 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "BootNotificationResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Pending",
12
+                "Rejected"
13
+            ]
14
+        },
15
+        "currentTime": {
16
+            "type": "string",
17
+            "format": "date-time"
18
+        },
19
+        "interval": {
20
+            "type": "integer"
21
+        }
22
+    },
23
+    "additionalProperties": false,
24
+    "required": [
25
+        "status",
26
+        "currentTime",
27
+        "interval"
28
+    ]
29
+}

+ 14 - 0
ocpp/ocpp/v16/schemas/CancelReservation.json

@@ -0,0 +1,14 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "CancelReservationRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "reservationId": {
7
+            "type": "integer"
8
+        }
9
+    },
10
+    "additionalProperties": false,
11
+    "required": [
12
+        "reservationId"
13
+    ]
14
+}

+ 19 - 0
ocpp/ocpp/v16/schemas/CancelReservationResponse.json

@@ -0,0 +1,19 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "CancelReservationResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected"
12
+            ]
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "status"
18
+    ]
19
+}

+ 15 - 0
ocpp/ocpp/v16/schemas/CertificateSigned.json

@@ -0,0 +1,15 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:CertificateSigned.req",
4
+    "type": "object",
5
+    "properties": {
6
+        "certificateChain": {
7
+            "type": "string",
8
+            "maxLength": 10000
9
+        }
10
+    },
11
+    "additionalProperties": false,
12
+    "required": [
13
+        "certificateChain"
14
+    ]
15
+}

+ 24 - 0
ocpp/ocpp/v16/schemas/CertificateSignedResponse.json

@@ -0,0 +1,24 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:CertificateSigned.conf",
4
+  "definitions": {
5
+    "CertificateSignedStatusEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "Accepted",
10
+        "Rejected"
11
+      ]
12
+    }
13
+  },
14
+  "type": "object",
15
+  "additionalProperties": false,
16
+  "properties": {
17
+    "status": {
18
+      "$ref": "#/definitions/CertificateSignedStatusEnumType"
19
+    }
20
+  },
21
+  "required": [
22
+    "status"
23
+  ]
24
+}

+ 23 - 0
ocpp/ocpp/v16/schemas/ChangeAvailability.json

@@ -0,0 +1,23 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ChangeAvailabilityRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "connectorId": {
7
+            "type": "integer"
8
+        },
9
+        "type": {
10
+            "type": "string",
11
+            "additionalProperties": false,
12
+            "enum": [
13
+                "Inoperative",
14
+                "Operative"
15
+            ]
16
+        }
17
+    },
18
+    "additionalProperties": false,
19
+    "required": [
20
+        "connectorId",
21
+        "type"
22
+    ]
23
+}

+ 20 - 0
ocpp/ocpp/v16/schemas/ChangeAvailabilityResponse.json

@@ -0,0 +1,20 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ChangeAvailabilityResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected",
12
+                "Scheduled"
13
+            ]
14
+        }
15
+    },
16
+    "additionalProperties": false,
17
+    "required": [
18
+        "status"
19
+    ]
20
+}

+ 20 - 0
ocpp/ocpp/v16/schemas/ChangeConfiguration.json

@@ -0,0 +1,20 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ChangeConfigurationRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "key": {
7
+            "type": "string",
8
+            "maxLength": 50
9
+        },
10
+        "value": {
11
+            "type": "string",
12
+            "maxLength": 500
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "key",
18
+        "value"
19
+    ]
20
+}

+ 21 - 0
ocpp/ocpp/v16/schemas/ChangeConfigurationResponse.json

@@ -0,0 +1,21 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ChangeConfigurationResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected",
12
+                "RebootRequired",
13
+                "NotSupported"
14
+            ]
15
+        }
16
+    },
17
+    "additionalProperties": false,
18
+    "required": [
19
+        "status"
20
+    ]
21
+}

+ 7 - 0
ocpp/ocpp/v16/schemas/ClearCache.json

@@ -0,0 +1,7 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ClearCacheRequest",
4
+    "type": "object",
5
+    "properties": {},
6
+    "additionalProperties": false
7
+}

+ 19 - 0
ocpp/ocpp/v16/schemas/ClearCacheResponse.json

@@ -0,0 +1,19 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ClearCacheResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected"
12
+            ]
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "status"
18
+    ]
19
+}

+ 26 - 0
ocpp/ocpp/v16/schemas/ClearChargingProfile.json

@@ -0,0 +1,26 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ClearChargingProfileRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "id": {
7
+            "type": "integer"
8
+        },
9
+        "connectorId": {
10
+            "type": "integer"
11
+        },
12
+        "chargingProfilePurpose": {
13
+            "type": "string",
14
+            "additionalProperties": false,
15
+            "enum": [
16
+                "ChargePointMaxProfile",
17
+                "TxDefaultProfile",
18
+                "TxProfile"
19
+            ]
20
+        },
21
+        "stackLevel": {
22
+            "type": "integer"
23
+        }
24
+    },
25
+    "additionalProperties": false
26
+}

+ 19 - 0
ocpp/ocpp/v16/schemas/ClearChargingProfileResponse.json

@@ -0,0 +1,19 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ClearChargingProfileResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Unknown"
12
+            ]
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "status"
18
+    ]
19
+}

+ 22 - 0
ocpp/ocpp/v16/schemas/DataTransfer.json

@@ -0,0 +1,22 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "DataTransferRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "vendorId": {
7
+            "type": "string",
8
+            "maxLength": 255
9
+        },
10
+        "messageId": {
11
+            "type": "string",
12
+            "maxLength": 50
13
+        },
14
+        "data": {
15
+            "type": "string"
16
+        }
17
+    },
18
+    "additionalProperties": false,
19
+    "required": [
20
+        "vendorId"
21
+    ]
22
+}

+ 24 - 0
ocpp/ocpp/v16/schemas/DataTransferResponse.json

@@ -0,0 +1,24 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "DataTransferResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected",
12
+                "UnknownMessageId",
13
+                "UnknownVendorId"
14
+            ]
15
+        },
16
+        "data": {
17
+            "type": "string"
18
+        }
19
+    },
20
+    "additionalProperties": false,
21
+    "required": [
22
+        "status"
23
+    ]
24
+}

+ 52 - 0
ocpp/ocpp/v16/schemas/DeleteCertificate.json

@@ -0,0 +1,52 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:DeleteCertificate.req",
4
+  "definitions": {
5
+    "HashAlgorithmEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "SHA256",
10
+        "SHA384",
11
+        "SHA512"
12
+      ]
13
+    },
14
+    "CertificateHashDataType": {
15
+      "type": "object",
16
+      "additionalProperties": false,
17
+      "properties": {
18
+        "hashAlgorithm": {
19
+          "$ref": "#/definitions/HashAlgorithmEnumType"
20
+        },
21
+        "issuerNameHash": {
22
+          "type": "string",
23
+          "maxLength": 128
24
+        },
25
+        "issuerKeyHash": {
26
+          "type": "string",
27
+          "maxLength": 128
28
+        },
29
+        "serialNumber": {
30
+          "type": "string",
31
+          "maxLength": 40
32
+        }
33
+      },
34
+      "required": [
35
+        "hashAlgorithm",
36
+        "issuerNameHash",
37
+        "issuerKeyHash",
38
+        "serialNumber"
39
+      ]
40
+    }
41
+  },
42
+  "type": "object",
43
+  "additionalProperties": false,
44
+  "properties": {
45
+    "certificateHashData": {
46
+      "$ref": "#/definitions/CertificateHashDataType"
47
+    }
48
+  },
49
+  "required": [
50
+    "certificateHashData"
51
+  ]
52
+}

+ 25 - 0
ocpp/ocpp/v16/schemas/DeleteCertificateResponse.json

@@ -0,0 +1,25 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:DeleteCertificate.conf",
4
+  "definitions": {
5
+    "DeleteCertificateStatusEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "Accepted",
10
+        "Failed",
11
+        "NotFound"
12
+      ]
13
+    }
14
+  },
15
+  "type": "object",
16
+  "additionalProperties": false,
17
+  "properties": {
18
+    "status": {
19
+      "$ref": "#/definitions/DeleteCertificateStatusEnumType"
20
+    }
21
+  },
22
+  "required": [
23
+    "status"
24
+  ]
25
+}

+ 21 - 0
ocpp/ocpp/v16/schemas/DiagnosticsStatusNotification.json

@@ -0,0 +1,21 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "DiagnosticsStatusNotificationRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Idle",
11
+                "Uploaded",
12
+                "UploadFailed",
13
+                "Uploading"
14
+            ]
15
+        }
16
+    },
17
+    "additionalProperties": false,
18
+    "required": [
19
+        "status"
20
+    ]
21
+}

+ 7 - 0
ocpp/ocpp/v16/schemas/DiagnosticsStatusNotificationResponse.json

@@ -0,0 +1,7 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "DiagnosticsStatusNotificationResponse",
4
+    "type": "object",
5
+    "properties": {},
6
+    "additionalProperties": false
7
+}

+ 32 - 0
ocpp/ocpp/v16/schemas/ExtendedTriggerMessage.json

@@ -0,0 +1,32 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:ExtendedTriggerMessage.req",
4
+  "definitions": {
5
+    "MessageTriggerEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "BootNotification",
10
+        "LogStatusNotification",
11
+        "FirmwareStatusNotification",
12
+        "Heartbeat",
13
+        "MeterValues",
14
+        "SignChargePointCertificate",        
15
+        "StatusNotification"        
16
+      ]
17
+    }	  
18
+  },
19
+  "type": "object",
20
+  "additionalProperties": false,
21
+  "properties": {    
22
+    "requestedMessage": {
23
+      "$ref": "#/definitions/MessageTriggerEnumType"
24
+    },
25
+	"connectorId": {
26
+          "type": "integer"
27
+    }  
28
+  },
29
+  "required": [
30
+    "requestedMessage"
31
+  ]
32
+}

+ 25 - 0
ocpp/ocpp/v16/schemas/ExtendedTriggerMessageResponse.json

@@ -0,0 +1,25 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:ExtendedTriggerMessage.conf",
4
+  "definitions": {
5
+    "TriggerMessageStatusEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "Accepted",
10
+        "Rejected",
11
+        "NotImplemented"
12
+      ]
13
+    }
14
+  },
15
+  "type": "object",
16
+  "additionalProperties": false,
17
+  "properties": {
18
+    "status": {
19
+      "$ref": "#/definitions/TriggerMessageStatusEnumType"
20
+    }
21
+  },
22
+  "required": [
23
+    "status"
24
+  ]
25
+}

+ 24 - 0
ocpp/ocpp/v16/schemas/FirmwareStatusNotification.json

@@ -0,0 +1,24 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "FirmwareStatusNotificationRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Downloaded",
11
+                "DownloadFailed",
12
+                "Downloading",
13
+                "Idle",
14
+                "InstallationFailed",
15
+                "Installing",
16
+                "Installed"
17
+            ]
18
+        }
19
+    },
20
+    "additionalProperties": false,
21
+    "required": [
22
+        "status"
23
+    ]
24
+}

+ 7 - 0
ocpp/ocpp/v16/schemas/FirmwareStatusNotificationResponse.json

@@ -0,0 +1,7 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "FirmwareStatusNotificationResponse",
4
+    "type": "object",
5
+    "properties": {},
6
+    "additionalProperties": false
7
+}

+ 26 - 0
ocpp/ocpp/v16/schemas/GetCompositeSchedule.json

@@ -0,0 +1,26 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetCompositeScheduleRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "connectorId": {
7
+            "type": "integer"
8
+        },
9
+    "duration": {
10
+        "type": "integer"
11
+    },
12
+    "chargingRateUnit": {
13
+        "type": "string",
14
+        "additionalProperties": false,
15
+        "enum": [
16
+            "A",
17
+            "W"
18
+            ]
19
+        }
20
+    },
21
+    "additionalProperties": false,
22
+    "required": [
23
+        "connectorId",
24
+        "duration"
25
+    ]
26
+}

+ 78 - 0
ocpp/ocpp/v16/schemas/GetCompositeScheduleResponse.json

@@ -0,0 +1,78 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetCompositeScheduleResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected"
12
+            ]
13
+        },
14
+    "connectorId": {
15
+        "type": "integer"
16
+        },
17
+    "scheduleStart": {
18
+        "type": "string",
19
+        "format": "date-time"
20
+    },
21
+    "chargingSchedule": {
22
+        "type": "object",
23
+        "properties": {
24
+            "duration": {
25
+                "type": "integer"
26
+            },
27
+            "startSchedule": {
28
+                "type": "string",
29
+                "format": "date-time"
30
+            },
31
+            "chargingRateUnit": {
32
+                "type": "string",
33
+                "additionalProperties": false,
34
+                "enum": [
35
+                    "A",
36
+                    "W"
37
+                    ]
38
+            },
39
+            "chargingSchedulePeriod": {
40
+                "type": "array",
41
+                "items": {
42
+                    "type": "object",
43
+                    "properties": {
44
+                        "startPeriod": {
45
+                            "type": "integer"
46
+                        },
47
+                        "limit": {
48
+                            "type": "number",
49
+                            "multipleOf" : 0.1
50
+                        },
51
+                        "numberPhases": {
52
+                            "type": "integer"
53
+                        }
54
+                    },
55
+                    "additionalProperties": false,
56
+                    "required": [
57
+                        "startPeriod",
58
+                        "limit"
59
+                        ]
60
+                }
61
+            },
62
+            "minChargingRate": {
63
+                "type": "number",
64
+                "multipleOf" : 0.1
65
+            }
66
+        },
67
+        "additionalProperties": false,
68
+        "required": [
69
+            "chargingRateUnit",
70
+            "chargingSchedulePeriod"
71
+        ]
72
+        }
73
+    },
74
+    "additionalProperties": false,
75
+    "required": [
76
+        "status"
77
+    ]
78
+}

+ 15 - 0
ocpp/ocpp/v16/schemas/GetConfiguration.json

@@ -0,0 +1,15 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetConfigurationRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "key": {
7
+            "type": "array",
8
+            "items": {
9
+                "type": "string",
10
+                "maxLength": 50
11
+            }
12
+        }
13
+    },
14
+    "additionalProperties": false
15
+}

+ 39 - 0
ocpp/ocpp/v16/schemas/GetConfigurationResponse.json

@@ -0,0 +1,39 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetConfigurationResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "configurationKey": {
7
+            "type": "array",
8
+            "items": {
9
+                "type": "object",
10
+                "properties": {
11
+                    "key": {
12
+                        "type": "string",
13
+                        "maxLength": 50
14
+                    },
15
+                    "readonly": {
16
+                        "type": "boolean"
17
+                    },
18
+                    "value": {
19
+                        "type": "string",
20
+                        "maxLength": 500
21
+                    }
22
+                },
23
+                "additionalProperties": false,
24
+                "required": [
25
+                    "key",
26
+                    "readonly"
27
+                ]
28
+            }
29
+        },
30
+        "unknownKey": {
31
+            "type": "array",
32
+            "items": {
33
+                "type": "string",
34
+                "maxLength": 50
35
+            }
36
+        }
37
+    },
38
+    "additionalProperties": false
39
+}

+ 29 - 0
ocpp/ocpp/v16/schemas/GetDiagnostics.json

@@ -0,0 +1,29 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetDiagnosticsRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "location": {
7
+            "type": "string",
8
+            "format": "uri"
9
+        },
10
+        "retries": {
11
+            "type": "integer"
12
+        },
13
+        "retryInterval": {
14
+            "type": "integer"
15
+        },
16
+        "startTime": {
17
+            "type": "string",
18
+            "format": "date-time"
19
+        },
20
+        "stopTime": {
21
+            "type": "string",
22
+            "format": "date-time"
23
+        }
24
+    },
25
+    "additionalProperties": false,
26
+    "required": [
27
+        "location"
28
+    ]
29
+}

+ 12 - 0
ocpp/ocpp/v16/schemas/GetDiagnosticsResponse.json

@@ -0,0 +1,12 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetDiagnosticsResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "fileName": {
7
+            "type": "string",
8
+            "maxLength": 255
9
+        }
10
+    },
11
+    "additionalProperties": false
12
+}

+ 24 - 0
ocpp/ocpp/v16/schemas/GetInstalledCertificateIds.json

@@ -0,0 +1,24 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:GetInstalledCertificateIds.req",
4
+  "definitions": {
5
+    "CertificateUseEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "CentralSystemRootCertificate",
10
+        "ManufacturerRootCertificate"
11
+      ]
12
+    }
13
+  },
14
+  "type": "object",
15
+  "additionalProperties": false,
16
+  "properties": {
17
+    "certificateType": {
18
+      "$ref": "#/definitions/CertificateUseEnumType"
19
+    }
20
+  },
21
+  "required": [
22
+    "certificateType"
23
+  ]
24
+}

+ 69 - 0
ocpp/ocpp/v16/schemas/GetInstalledCertificateIdsResponse.json

@@ -0,0 +1,69 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:GetInstalledCertificateIds.conf",
4
+  "definitions": {
5
+    "GetInstalledCertificateStatusEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "Accepted",
10
+        "NotFound"
11
+      ]
12
+    },
13
+    "HashAlgorithmEnumType": {
14
+      "type": "string",
15
+      "additionalProperties": false,
16
+      "enum": [
17
+        "SHA256",
18
+        "SHA384",
19
+        "SHA512"
20
+      ]
21
+    },
22
+    "CertificateHashDataType": {
23
+      "javaType": "CertificateHashData",
24
+      "type": "object",
25
+      "additionalProperties": false,
26
+      "properties": {
27
+        "hashAlgorithm": {
28
+          "$ref": "#/definitions/HashAlgorithmEnumType"
29
+        },
30
+        "issuerNameHash": {
31
+          "type": "string",
32
+          "maxLength": 128
33
+        },
34
+        "issuerKeyHash": {
35
+          "type": "string",
36
+          "maxLength": 128
37
+        },
38
+        "serialNumber": {
39
+          "type": "string",
40
+          "maxLength": 40
41
+        }
42
+      },
43
+      "required": [
44
+        "hashAlgorithm",
45
+        "issuerNameHash",
46
+        "issuerKeyHash",
47
+        "serialNumber"
48
+      ]
49
+    }
50
+  },
51
+  "type": "object",
52
+  "additionalProperties": false,
53
+  "properties": {
54
+    "certificateHashData": {
55
+      "type": "array",
56
+      "additionalItems": false,
57
+      "items": {
58
+        "$ref": "#/definitions/CertificateHashDataType"
59
+      },
60
+      "minItems": 1
61
+    },
62
+    "status": {
63
+      "$ref": "#/definitions/GetInstalledCertificateStatusEnumType"
64
+    }
65
+  },
66
+  "required": [
67
+    "status"
68
+  ]
69
+}

+ 7 - 0
ocpp/ocpp/v16/schemas/GetLocalListVersion.json

@@ -0,0 +1,7 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetLocalListVersionRequest",
4
+    "type": "object",
5
+    "properties": {},
6
+    "additionalProperties": false
7
+}

+ 14 - 0
ocpp/ocpp/v16/schemas/GetLocalListVersionResponse.json

@@ -0,0 +1,14 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "GetLocalListVersionResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "listVersion": {
7
+            "type": "integer"
8
+        }
9
+    },
10
+    "additionalProperties": false,
11
+    "required": [
12
+        "listVersion"
13
+    ]
14
+}

+ 59 - 0
ocpp/ocpp/v16/schemas/GetLog.json

@@ -0,0 +1,59 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:GetLog.req",
4
+  "definitions": {
5
+    "LogEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "DiagnosticsLog",
10
+        "SecurityLog"
11
+      ]
12
+    },
13
+    "LogParametersType": {
14
+      "type": "object",
15
+      "additionalProperties": false,
16
+      "properties": {
17
+        "remoteLocation": {
18
+          "type": "string",
19
+          "maxLength": 512
20
+        },
21
+        "oldestTimestamp": {
22
+          "type": "string",
23
+          "format": "date-time"
24
+        },
25
+        "latestTimestamp": {
26
+          "type": "string",
27
+          "format": "date-time"
28
+        }
29
+      },
30
+      "required": [
31
+        "remoteLocation"
32
+      ]
33
+    }
34
+  },
35
+  "type": "object",
36
+  "additionalProperties": false,
37
+  "properties": {
38
+    "log": {
39
+      "$ref": "#/definitions/LogParametersType"
40
+    },
41
+    "logType": {
42
+      "$ref": "#/definitions/LogEnumType"
43
+    },
44
+    "requestId": {
45
+      "type": "integer"
46
+    },
47
+    "retries": {
48
+      "type": "integer"
49
+    },
50
+    "retryInterval": {
51
+      "type": "integer"
52
+    }
53
+  },
54
+  "required": [
55
+    "logType",
56
+    "requestId",
57
+    "log"
58
+  ]
59
+}

+ 29 - 0
ocpp/ocpp/v16/schemas/GetLogResponse.json

@@ -0,0 +1,29 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:GetLog.conf",
4
+  "definitions": {
5
+    "LogStatusEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "Accepted",
10
+        "Rejected",
11
+        "AcceptedCanceled"
12
+      ]
13
+    }
14
+  },
15
+  "type": "object",
16
+  "additionalProperties": false,
17
+  "properties": {
18
+    "status": {
19
+      "$ref": "#/definitions/LogStatusEnumType"
20
+    },
21
+    "filename": {
22
+      "type": "string",
23
+      "maxLength": 255
24
+    }
25
+  },
26
+  "required": [
27
+    "status"
28
+  ]
29
+}

+ 7 - 0
ocpp/ocpp/v16/schemas/Heartbeat.json

@@ -0,0 +1,7 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "HeartbeatRequest",
4
+    "type": "object",
5
+    "properties": {},
6
+    "additionalProperties": false
7
+}

+ 15 - 0
ocpp/ocpp/v16/schemas/HeartbeatResponse.json

@@ -0,0 +1,15 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "HeartbeatResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "currentTime": {
7
+            "type": "string",
8
+            "format": "date-time"
9
+        }
10
+    },
11
+    "additionalProperties": false,
12
+    "required": [
13
+        "currentTime"
14
+    ]
15
+}

+ 29 - 0
ocpp/ocpp/v16/schemas/InstallCertificate.json

@@ -0,0 +1,29 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:InstallCertificate.req",
4
+  "definitions": {
5
+    "CertificateUseEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "CentralSystemRootCertificate",
10
+        "ManufacturerRootCertificate"
11
+      ]
12
+    }
13
+  },
14
+  "type": "object",
15
+  "additionalProperties": false,
16
+  "properties": {
17
+    "certificateType": {
18
+      "$ref": "#/definitions/CertificateUseEnumType"
19
+    },
20
+    "certificate": {
21
+      "type": "string",
22
+      "maxLength": 5500
23
+    }
24
+  },
25
+  "required": [
26
+    "certificateType",
27
+    "certificate"
28
+  ]
29
+}

+ 25 - 0
ocpp/ocpp/v16/schemas/InstallCertificateResponse.json

@@ -0,0 +1,25 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:InstallCertificate.conf",
4
+  "definitions": {
5
+    "InstallCertificateStatusEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "Accepted",
10
+        "Failed",
11
+        "Rejected"
12
+      ]
13
+    }
14
+  },
15
+  "type": "object",
16
+  "additionalProperties": false,
17
+  "properties": {
18
+    "status": {
19
+      "$ref": "#/definitions/InstallCertificateStatusEnumType"
20
+    }
21
+  },
22
+  "required": [
23
+    "status"
24
+  ]
25
+}

+ 32 - 0
ocpp/ocpp/v16/schemas/LogStatusNotification.json

@@ -0,0 +1,32 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:LogStatusNotification.req",
4
+  "definitions": {
5
+    "UploadLogStatusEnumType": {
6
+      "type": "string",
7
+      "additionalProperties": false,
8
+      "enum": [
9
+        "BadMessage",
10
+        "Idle",
11
+        "NotSupportedOperation",
12
+        "PermissionDenied",
13
+        "Uploaded",
14
+        "UploadFailure",
15
+        "Uploading"
16
+      ]
17
+    }
18
+  },
19
+  "type": "object",
20
+  "additionalProperties": false,
21
+  "properties": {
22
+    "status": {
23
+      "$ref": "#/definitions/UploadLogStatusEnumType"
24
+    },
25
+    "requestId": {
26
+      "type": "integer"
27
+    }
28
+  },
29
+  "required": [
30
+    "status"
31
+  ]
32
+}

+ 6 - 0
ocpp/ocpp/v16/schemas/LogStatusNotificationResponse.json

@@ -0,0 +1,6 @@
1
+{
2
+  "$schema": "http://json-schema.org/draft-06/schema#",
3
+  "$id": "urn:OCPP:Cp:1.6:2020:3:LogStatusNotification.conf",
4
+  "type": "object",
5
+  "additionalProperties": false
6
+}

+ 153 - 0
ocpp/ocpp/v16/schemas/MeterValues.json

@@ -0,0 +1,153 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "MeterValuesRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "connectorId": {
7
+            "type": "integer"
8
+        },
9
+        "transactionId": {
10
+            "type": "integer"
11
+        },
12
+        "meterValue": {
13
+            "type": "array",
14
+            "minItems": 1,
15
+            "items": {
16
+                "type": "object",
17
+                "properties": {
18
+                    "timestamp": {
19
+                        "type": "string",
20
+                        "format": "date-time"
21
+                    },
22
+                    "sampledValue": {
23
+                        "type": "array",
24
+                        "minItems": 1,
25
+                        "items": {
26
+                            "type": "object",
27
+                            "properties": {
28
+                                "value": {
29
+                                    "type": "string"
30
+                                },
31
+                                "context": {
32
+                                    "type": "string",
33
+                                    "additionalProperties": false,
34
+                                    "enum": [
35
+                                        "Interruption.Begin",
36
+                                        "Interruption.End",
37
+                                        "Sample.Clock",
38
+                                        "Sample.Periodic",
39
+                                        "Transaction.Begin",
40
+                                        "Transaction.End",
41
+                                        "Trigger",
42
+                                        "Other"
43
+                                    ]
44
+                                },
45
+                                "format": {
46
+                                    "type": "string",
47
+                                    "additionalProperties": false,
48
+                                    "enum": [
49
+                                        "Raw",
50
+                                        "SignedData"
51
+                                    ]
52
+                                },
53
+                                "measurand": {
54
+                                    "type": "string",
55
+                                    "additionalProperties": false,
56
+                                    "enum": [
57
+                                        "Energy.Active.Export.Register",
58
+                                        "Energy.Active.Import.Register",
59
+                                        "Energy.Reactive.Export.Register",
60
+                                        "Energy.Reactive.Import.Register",
61
+                                        "Energy.Active.Export.Interval",
62
+                                        "Energy.Active.Import.Interval",
63
+                                        "Energy.Reactive.Export.Interval",
64
+                                        "Energy.Reactive.Import.Interval",
65
+                                        "Power.Active.Export",
66
+                                        "Power.Active.Import",
67
+                                        "Power.Offered",
68
+                                        "Power.Reactive.Export",
69
+                                        "Power.Reactive.Import",
70
+                                        "Power.Factor",
71
+                                        "Current.Import",
72
+                                        "Current.Export",
73
+                                        "Current.Offered",
74
+                                        "Voltage",
75
+                                        "Frequency",
76
+                                        "Temperature",
77
+                                        "SoC",
78
+                                        "RPM"
79
+                                    ]
80
+                                },
81
+                                "phase": {
82
+                                    "type": "string",
83
+                                    "additionalProperties": false,
84
+                                    "enum": [
85
+                                        "L1",
86
+                                        "L2",
87
+                                        "L3",
88
+                                        "N",
89
+                                        "L1-N",
90
+                                        "L2-N",
91
+                                        "L3-N",
92
+                                        "L1-L2",
93
+                                        "L2-L3",
94
+                                        "L3-L1"
95
+                                    ]
96
+                                },
97
+                                "location": {
98
+                                    "type": "string",
99
+                                    "additionalProperties": false,
100
+                                    "enum": [
101
+                                        "Cable",
102
+                                        "EV",
103
+                                        "Inlet",
104
+                                        "Outlet",
105
+                                        "Body"
106
+                                    ]
107
+                                },
108
+                                "unit": {
109
+                                    "type": "string",
110
+                                    "additionalProperties": false,
111
+                                    "enum": [
112
+                                        "Wh",
113
+                                        "kWh",
114
+                                        "varh",
115
+                                        "kvarh",
116
+                                        "W",
117
+                                        "kW",
118
+                                        "VA",
119
+                                        "kVA",
120
+                                        "var",
121
+                                        "kvar",
122
+                                        "A",
123
+                                        "V",
124
+                                        "K",
125
+                                        "Celcius",
126
+                                        "Celsius",
127
+                                        "Fahrenheit",
128
+                                        "Percent",
129
+                                        "Hertz"
130
+                                    ]
131
+                                }
132
+                            },
133
+                            "additionalProperties": false,
134
+                            "required": [
135
+                                "value"
136
+                            ]
137
+                        }
138
+                    }
139
+                },
140
+                "additionalProperties": false,
141
+                "required": [
142
+                    "timestamp",
143
+                    "sampledValue"
144
+                ]       
145
+            }
146
+        }
147
+    },
148
+    "additionalProperties": false,
149
+    "required": [
150
+        "connectorId",
151
+        "meterValue"
152
+    ]
153
+}

+ 7 - 0
ocpp/ocpp/v16/schemas/MeterValuesResponse.json

@@ -0,0 +1,7 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "MeterValuesResponse",
4
+    "type": "object",
5
+    "properties": {},
6
+    "additionalProperties": false
7
+}

+ 126 - 0
ocpp/ocpp/v16/schemas/RemoteStartTransaction.json

@@ -0,0 +1,126 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "RemoteStartTransactionRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "connectorId": {
7
+            "type": "integer"
8
+        },
9
+        "idTag": {
10
+            "type": "string",
11
+            "maxLength": 20
12
+        },
13
+        "chargingProfile": {
14
+            "type": "object",
15
+            "properties": {
16
+                "chargingProfileId": {
17
+                    "type": "integer"
18
+                },
19
+                "transactionId": {
20
+                    "type": "integer"
21
+                },
22
+                "stackLevel": {
23
+                    "type": "integer"
24
+                },
25
+                "chargingProfilePurpose": {
26
+                    "type": "string",
27
+                    "additionalProperties": false,
28
+                    "enum": [
29
+                        "ChargePointMaxProfile",
30
+                        "TxDefaultProfile",
31
+                        "TxProfile"
32
+                    ]
33
+                },
34
+                "chargingProfileKind": {
35
+                    "type": "string",
36
+                    "additionalProperties": false,
37
+                    "enum": [
38
+                        "Absolute",
39
+                        "Recurring",
40
+                        "Relative"
41
+                    ]
42
+                },
43
+                "recurrencyKind": {
44
+                    "type": "string",
45
+                    "additionalProperties": false,
46
+                    "enum": [
47
+                        "Daily",
48
+                        "Weekly"
49
+                    ]
50
+                },
51
+                "validFrom": {
52
+                    "type": "string",
53
+                    "format": "date-time"
54
+                },
55
+                "validTo": {
56
+                    "type": "string",
57
+                    "format": "date-time"
58
+                },
59
+                "chargingSchedule": {
60
+                    "type": "object",
61
+                    "properties": {
62
+                        "duration": {
63
+                            "type": "integer"
64
+                        },
65
+                        "startSchedule": {
66
+                            "type": "string",
67
+                            "format": "date-time"
68
+                        },
69
+                        "chargingRateUnit": {
70
+                            "type": "string",
71
+                            "additionalProperties": false,
72
+                            "enum": [
73
+                                "A",
74
+                                "W"
75
+                            ]
76
+                        },
77
+                        "chargingSchedulePeriod": {
78
+                            "type": "array",
79
+                            "items": {
80
+                                "type": "object",
81
+                                "properties": {
82
+                                    "startPeriod": {
83
+                                        "type": "integer"
84
+                                    },
85
+                                    "limit": {
86
+                                        "type": "number",
87
+                                        "multipleOf" : 0.1
88
+                                    },
89
+                                    "numberPhases": {
90
+                                        "type": "integer"
91
+                                    }
92
+                                },
93
+                                "additionalProperties": false,
94
+                                "required": [
95
+                                    "startPeriod",
96
+                                    "limit"
97
+                                ]
98
+                            }
99
+                        },
100
+                        "minChargingRate": {
101
+                            "type": "number",
102
+                            "multipleOf" : 0.1
103
+                        }
104
+                    },
105
+                    "additionalProperties": false,
106
+                    "required": [
107
+                        "chargingRateUnit",
108
+                        "chargingSchedulePeriod"
109
+                    ]
110
+                }
111
+            },
112
+            "additionalProperties": false,
113
+            "required": [
114
+                "chargingProfileId",
115
+                "stackLevel",
116
+                "chargingProfilePurpose",
117
+                "chargingProfileKind",
118
+                "chargingSchedule"
119
+            ]
120
+        }
121
+    },
122
+    "additionalProperties": false,
123
+    "required": [
124
+        "idTag"
125
+    ]
126
+}

+ 19 - 0
ocpp/ocpp/v16/schemas/RemoteStartTransactionResponse.json

@@ -0,0 +1,19 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "RemoteStartTransactionResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected"
12
+            ]
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "status"
18
+    ]
19
+}

+ 14 - 0
ocpp/ocpp/v16/schemas/RemoteStopTransaction.json

@@ -0,0 +1,14 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "RemoteStopTransactionRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "transactionId": {
7
+            "type": "integer"
8
+        }
9
+    },
10
+    "additionalProperties": false,
11
+    "required": [
12
+        "transactionId"
13
+    ]
14
+}

+ 19 - 0
ocpp/ocpp/v16/schemas/RemoteStopTransactionResponse.json

@@ -0,0 +1,19 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "RemoteStopTransactionResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected"
12
+            ]
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "status"
18
+    ]
19
+}

+ 32 - 0
ocpp/ocpp/v16/schemas/ReserveNow.json

@@ -0,0 +1,32 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ReserveNowRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "connectorId": {
7
+            "type": "integer"
8
+        },
9
+        "expiryDate": {
10
+            "type": "string",
11
+            "format": "date-time"
12
+        },
13
+        "idTag": {
14
+            "type": "string",
15
+            "maxLength": 20
16
+        },
17
+        "parentIdTag": {
18
+            "type": "string",
19
+            "maxLength": 20
20
+        },
21
+        "reservationId": {
22
+            "type": "integer"
23
+        }
24
+    },
25
+    "additionalProperties": false,
26
+    "required": [
27
+        "connectorId",
28
+        "expiryDate",
29
+        "idTag",
30
+        "reservationId"
31
+    ]
32
+}

+ 22 - 0
ocpp/ocpp/v16/schemas/ReserveNowResponse.json

@@ -0,0 +1,22 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ReserveNowResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Faulted",
12
+                "Occupied",
13
+                "Rejected",
14
+                "Unavailable"
15
+            ]
16
+        }
17
+    },
18
+    "additionalProperties": false,
19
+    "required": [
20
+        "status"
21
+    ]
22
+}

+ 19 - 0
ocpp/ocpp/v16/schemas/Reset.json

@@ -0,0 +1,19 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ResetRequest",
4
+    "type": "object",
5
+    "properties": {
6
+        "type": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Hard",
11
+                "Soft"
12
+            ]
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "type"
18
+    ]
19
+}

+ 19 - 0
ocpp/ocpp/v16/schemas/ResetResponse.json

@@ -0,0 +1,19 @@
1
+{
2
+    "$schema": "http://json-schema.org/draft-04/schema#",
3
+    "title": "ResetResponse",
4
+    "type": "object",
5
+    "properties": {
6
+        "status": {
7
+            "type": "string",
8
+            "additionalProperties": false,
9
+            "enum": [
10
+                "Accepted",
11
+                "Rejected"
12
+            ]
13
+        }
14
+    },
15
+    "additionalProperties": false,
16
+    "required": [
17
+        "status"
18
+    ]
19
+}

+ 0 - 0
ocpp/ocpp/v16/schemas/SecurityEventNotification.json


Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio

tum/whitesports - Gogs: Simplico Git Service

Нет описания

module.audio.ogg.php 42KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.audio.ogg.php //
  11. // module for analyzing Ogg Vorbis, OggFLAC and Speex files //
  12. // dependencies: module.audio.flac.php //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.flac.php', __FILE__, true);
  19. class getid3_ogg extends getid3_handler
  20. {
  21. /**
  22. * @link http://xiph.org/vorbis/doc/Vorbis_I_spec.html
  23. *
  24. * @return bool
  25. */
  26. public function Analyze() {
  27. $info = &$this->getid3->info;
  28. $info['fileformat'] = 'ogg';
  29. // Warn about illegal tags - only vorbiscomments are allowed
  30. if (isset($info['id3v2'])) {
  31. $this->warning('Illegal ID3v2 tag present.');
  32. }
  33. if (isset($info['id3v1'])) {
  34. $this->warning('Illegal ID3v1 tag present.');
  35. }
  36. if (isset($info['ape'])) {
  37. $this->warning('Illegal APE tag present.');
  38. }
  39. // Page 1 - Stream Header
  40. $this->fseek($info['avdataoffset']);
  41. $oggpageinfo = $this->ParseOggPageHeader();
  42. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  43. if ($this->ftell() >= $this->getid3->fread_buffer_size()) {
  44. $this->error('Could not find start of Ogg page in the first '.$this->getid3->fread_buffer_size().' bytes (this might not be an Ogg-Vorbis file?)');
  45. unset($info['fileformat']);
  46. unset($info['ogg']);
  47. return false;
  48. }
  49. $filedata = $this->fread($oggpageinfo['page_length']);
  50. $filedataoffset = 0;
  51. if (substr($filedata, 0, 4) == 'fLaC') {
  52. $info['audio']['dataformat'] = 'flac';
  53. $info['audio']['bitrate_mode'] = 'vbr';
  54. $info['audio']['lossless'] = true;
  55. } elseif (substr($filedata, 1, 6) == 'vorbis') {
  56. $this->ParseVorbisPageHeader($filedata, $filedataoffset, $oggpageinfo);
  57. } elseif (substr($filedata, 0, 8) == 'OpusHead') {
  58. if ($this->ParseOpusPageHeader($filedata, $filedataoffset, $oggpageinfo) === false) {
  59. return false;
  60. }
  61. } elseif (substr($filedata, 0, 8) == 'Speex ') {
  62. // http://www.speex.org/manual/node10.html
  63. $info['audio']['dataformat'] = 'speex';
  64. $info['mime_type'] = 'audio/speex';
  65. $info['audio']['bitrate_mode'] = 'abr';
  66. $info['audio']['lossless'] = false;
  67. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_string'] = substr($filedata, $filedataoffset, 8); // hard-coded to 'Speex '
  68. $filedataoffset += 8;
  69. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version'] = substr($filedata, $filedataoffset, 20);
  70. $filedataoffset += 20;
  71. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version_id'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  72. $filedataoffset += 4;
  73. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['header_size'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  74. $filedataoffset += 4;
  75. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['rate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  76. $filedataoffset += 4;
  77. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  78. $filedataoffset += 4;
  79. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode_bitstream_version'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  80. $filedataoffset += 4;
  81. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['nb_channels'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  82. $filedataoffset += 4;
  83. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['bitrate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  84. $filedataoffset += 4;
  85. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['framesize'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  86. $filedataoffset += 4;
  87. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['vbr'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  88. $filedataoffset += 4;
  89. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['frames_per_packet'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  90. $filedataoffset += 4;
  91. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['extra_headers'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  92. $filedataoffset += 4;
  93. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['reserved1'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  94. $filedataoffset += 4;
  95. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['reserved2'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  96. $filedataoffset += 4;
  97. $info['speex']['speex_version'] = trim($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['speex_version']);
  98. $info['speex']['sample_rate'] = $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['rate'];
  99. $info['speex']['channels'] = $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['nb_channels'];
  100. $info['speex']['vbr'] = (bool) $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['vbr'];
  101. $info['speex']['band_type'] = $this->SpeexBandModeLookup($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['mode']);
  102. $info['audio']['sample_rate'] = $info['speex']['sample_rate'];
  103. $info['audio']['channels'] = $info['speex']['channels'];
  104. if ($info['speex']['vbr']) {
  105. $info['audio']['bitrate_mode'] = 'vbr';
  106. }
  107. } elseif (substr($filedata, 0, 7) == "\x80".'theora') {
  108. // http://www.theora.org/doc/Theora.pdf (section 6.2)
  109. $info['ogg']['pageheader']['theora']['theora_magic'] = substr($filedata, $filedataoffset, 7); // hard-coded to "\x80.'theora'
  110. $filedataoffset += 7;
  111. $info['ogg']['pageheader']['theora']['version_major'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1));
  112. $filedataoffset += 1;
  113. $info['ogg']['pageheader']['theora']['version_minor'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1));
  114. $filedataoffset += 1;
  115. $info['ogg']['pageheader']['theora']['version_revision'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1));
  116. $filedataoffset += 1;
  117. $info['ogg']['pageheader']['theora']['frame_width_macroblocks'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 2));
  118. $filedataoffset += 2;
  119. $info['ogg']['pageheader']['theora']['frame_height_macroblocks'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 2));
  120. $filedataoffset += 2;
  121. $info['ogg']['pageheader']['theora']['resolution_x'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3));
  122. $filedataoffset += 3;
  123. $info['ogg']['pageheader']['theora']['resolution_y'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3));
  124. $filedataoffset += 3;
  125. $info['ogg']['pageheader']['theora']['picture_offset_x'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1));
  126. $filedataoffset += 1;
  127. $info['ogg']['pageheader']['theora']['picture_offset_y'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1));
  128. $filedataoffset += 1;
  129. $info['ogg']['pageheader']['theora']['frame_rate_numerator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 4));
  130. $filedataoffset += 4;
  131. $info['ogg']['pageheader']['theora']['frame_rate_denominator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 4));
  132. $filedataoffset += 4;
  133. $info['ogg']['pageheader']['theora']['pixel_aspect_numerator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3));
  134. $filedataoffset += 3;
  135. $info['ogg']['pageheader']['theora']['pixel_aspect_denominator'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3));
  136. $filedataoffset += 3;
  137. $info['ogg']['pageheader']['theora']['color_space_id'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 1));
  138. $filedataoffset += 1;
  139. $info['ogg']['pageheader']['theora']['nominal_bitrate'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 3));
  140. $filedataoffset += 3;
  141. $info['ogg']['pageheader']['theora']['flags'] = getid3_lib::BigEndian2Int(substr($filedata, $filedataoffset, 2));
  142. $filedataoffset += 2;
  143. $info['ogg']['pageheader']['theora']['quality'] = ($info['ogg']['pageheader']['theora']['flags'] & 0xFC00) >> 10;
  144. $info['ogg']['pageheader']['theora']['kfg_shift'] = ($info['ogg']['pageheader']['theora']['flags'] & 0x03E0) >> 5;
  145. $info['ogg']['pageheader']['theora']['pixel_format_id'] = ($info['ogg']['pageheader']['theora']['flags'] & 0x0018) >> 3;
  146. $info['ogg']['pageheader']['theora']['reserved'] = ($info['ogg']['pageheader']['theora']['flags'] & 0x0007) >> 0; // should be 0
  147. $info['ogg']['pageheader']['theora']['color_space'] = self::TheoraColorSpace($info['ogg']['pageheader']['theora']['color_space_id']);
  148. $info['ogg']['pageheader']['theora']['pixel_format'] = self::TheoraPixelFormat($info['ogg']['pageheader']['theora']['pixel_format_id']);
  149. $info['video']['dataformat'] = 'theora';
  150. $info['mime_type'] = 'video/ogg';
  151. //$info['audio']['bitrate_mode'] = 'abr';
  152. //$info['audio']['lossless'] = false;
  153. $info['video']['resolution_x'] = $info['ogg']['pageheader']['theora']['resolution_x'];
  154. $info['video']['resolution_y'] = $info['ogg']['pageheader']['theora']['resolution_y'];
  155. if ($info['ogg']['pageheader']['theora']['frame_rate_denominator'] > 0) {
  156. $info['video']['frame_rate'] = (float) $info['ogg']['pageheader']['theora']['frame_rate_numerator'] / $info['ogg']['pageheader']['theora']['frame_rate_denominator'];
  157. }
  158. if ($info['ogg']['pageheader']['theora']['pixel_aspect_denominator'] > 0) {
  159. $info['video']['pixel_aspect_ratio'] = (float) $info['ogg']['pageheader']['theora']['pixel_aspect_numerator'] / $info['ogg']['pageheader']['theora']['pixel_aspect_denominator'];
  160. }
  161. $this->warning('Ogg Theora (v3) not fully supported in this version of getID3 ['.$this->getid3->version().'] -- bitrate, playtime and all audio data are currently unavailable');
  162. } elseif (substr($filedata, 0, 8) == "fishead\x00") {
  163. // Ogg Skeleton version 3.0 Format Specification
  164. // http://xiph.org/ogg/doc/skeleton.html
  165. $filedataoffset += 8;
  166. $info['ogg']['skeleton']['fishead']['raw']['version_major'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2));
  167. $filedataoffset += 2;
  168. $info['ogg']['skeleton']['fishead']['raw']['version_minor'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2));
  169. $filedataoffset += 2;
  170. $info['ogg']['skeleton']['fishead']['raw']['presentationtime_numerator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  171. $filedataoffset += 8;
  172. $info['ogg']['skeleton']['fishead']['raw']['presentationtime_denominator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  173. $filedataoffset += 8;
  174. $info['ogg']['skeleton']['fishead']['raw']['basetime_numerator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  175. $filedataoffset += 8;
  176. $info['ogg']['skeleton']['fishead']['raw']['basetime_denominator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  177. $filedataoffset += 8;
  178. $info['ogg']['skeleton']['fishead']['raw']['utc'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 20));
  179. $filedataoffset += 20;
  180. $info['ogg']['skeleton']['fishead']['version'] = $info['ogg']['skeleton']['fishead']['raw']['version_major'].'.'.$info['ogg']['skeleton']['fishead']['raw']['version_minor'];
  181. $info['ogg']['skeleton']['fishead']['presentationtime'] = $info['ogg']['skeleton']['fishead']['raw']['presentationtime_numerator'] / $info['ogg']['skeleton']['fishead']['raw']['presentationtime_denominator'];
  182. $info['ogg']['skeleton']['fishead']['basetime'] = $info['ogg']['skeleton']['fishead']['raw']['basetime_numerator'] / $info['ogg']['skeleton']['fishead']['raw']['basetime_denominator'];
  183. $info['ogg']['skeleton']['fishead']['utc'] = $info['ogg']['skeleton']['fishead']['raw']['utc'];
  184. $counter = 0;
  185. do {
  186. $oggpageinfo = $this->ParseOggPageHeader();
  187. $info['ogg']['pageheader'][$oggpageinfo['page_seqno'].'.'.$counter++] = $oggpageinfo;
  188. $filedata = $this->fread($oggpageinfo['page_length']);
  189. $this->fseek($oggpageinfo['page_end_offset']);
  190. if (substr($filedata, 0, 8) == "fisbone\x00") {
  191. $filedataoffset = 8;
  192. $info['ogg']['skeleton']['fisbone']['raw']['message_header_offset'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  193. $filedataoffset += 4;
  194. $info['ogg']['skeleton']['fisbone']['raw']['serial_number'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  195. $filedataoffset += 4;
  196. $info['ogg']['skeleton']['fisbone']['raw']['number_header_packets'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  197. $filedataoffset += 4;
  198. $info['ogg']['skeleton']['fisbone']['raw']['granulerate_numerator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  199. $filedataoffset += 8;
  200. $info['ogg']['skeleton']['fisbone']['raw']['granulerate_denominator'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  201. $filedataoffset += 8;
  202. $info['ogg']['skeleton']['fisbone']['raw']['basegranule'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  203. $filedataoffset += 8;
  204. $info['ogg']['skeleton']['fisbone']['raw']['preroll'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  205. $filedataoffset += 4;
  206. $info['ogg']['skeleton']['fisbone']['raw']['granuleshift'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  207. $filedataoffset += 1;
  208. $info['ogg']['skeleton']['fisbone']['raw']['padding'] = substr($filedata, $filedataoffset, 3);
  209. $filedataoffset += 3;
  210. } elseif (substr($filedata, 1, 6) == 'theora') {
  211. $info['video']['dataformat'] = 'theora1';
  212. $this->error('Ogg Theora (v1) not correctly handled in this version of getID3 ['.$this->getid3->version().']');
  213. //break;
  214. } elseif (substr($filedata, 1, 6) == 'vorbis') {
  215. $this->ParseVorbisPageHeader($filedata, $filedataoffset, $oggpageinfo);
  216. } else {
  217. $this->error('unexpected');
  218. //break;
  219. }
  220. //} while ($oggpageinfo['page_seqno'] == 0);
  221. } while (($oggpageinfo['page_seqno'] == 0) && (substr($filedata, 0, 8) != "fisbone\x00"));
  222. $this->fseek($oggpageinfo['page_start_offset']);
  223. $this->error('Ogg Skeleton not correctly handled in this version of getID3 ['.$this->getid3->version().']');
  224. //return false;
  225. } elseif (substr($filedata, 0, 5) == "\x7F".'FLAC') {
  226. // https://xiph.org/flac/ogg_mapping.html
  227. $info['audio']['dataformat'] = 'flac';
  228. $info['audio']['bitrate_mode'] = 'vbr';
  229. $info['audio']['lossless'] = true;
  230. $info['ogg']['flac']['header']['version_major'] = ord(substr($filedata, 5, 1));
  231. $info['ogg']['flac']['header']['version_minor'] = ord(substr($filedata, 6, 1));
  232. $info['ogg']['flac']['header']['header_packets'] = getid3_lib::BigEndian2Int(substr($filedata, 7, 2)) + 1; // "A two-byte, big-endian binary number signifying the number of header (non-audio) packets, not including this one. This number may be zero (0x0000) to signify 'unknown' but be aware that some decoders may not be able to handle such streams."
  233. $info['ogg']['flac']['header']['magic'] = substr($filedata, 9, 4);
  234. if ($info['ogg']['flac']['header']['magic'] != 'fLaC') {
  235. $this->error('Ogg-FLAC expecting "fLaC", found "'.$info['ogg']['flac']['header']['magic'].'" ('.trim(getid3_lib::PrintHexBytes($info['ogg']['flac']['header']['magic'])).')');
  236. return false;
  237. }
  238. $info['ogg']['flac']['header']['STREAMINFO_bytes'] = getid3_lib::BigEndian2Int(substr($filedata, 13, 4));
  239. $info['flac']['STREAMINFO'] = getid3_flac::parseSTREAMINFOdata(substr($filedata, 17, 34));
  240. if (!empty($info['flac']['STREAMINFO']['sample_rate'])) {
  241. $info['audio']['bitrate_mode'] = 'vbr';
  242. $info['audio']['sample_rate'] = $info['flac']['STREAMINFO']['sample_rate'];
  243. $info['audio']['channels'] = $info['flac']['STREAMINFO']['channels'];
  244. $info['audio']['bits_per_sample'] = $info['flac']['STREAMINFO']['bits_per_sample'];
  245. $info['playtime_seconds'] = $info['flac']['STREAMINFO']['samples_stream'] / $info['flac']['STREAMINFO']['sample_rate'];
  246. }
  247. } else {
  248. $this->error('Expecting one of "vorbis", "Speex", "OpusHead", "vorbis", "fishhead", "theora", "fLaC" identifier strings, found "'.substr($filedata, 0, 8).'"');
  249. unset($info['ogg']);
  250. unset($info['mime_type']);
  251. return false;
  252. }
  253. // Page 2 - Comment Header
  254. $oggpageinfo = $this->ParseOggPageHeader();
  255. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  256. switch ($info['audio']['dataformat']) {
  257. case 'vorbis':
  258. $filedata = $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']);
  259. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['packet_type'] = getid3_lib::LittleEndian2Int(substr($filedata, 0, 1));
  260. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, 1, 6); // hard-coded to 'vorbis'
  261. $this->ParseVorbisComments();
  262. break;
  263. case 'flac':
  264. $flac = new getid3_flac($this->getid3);
  265. if (!$flac->parseMETAdata()) {
  266. $this->error('Failed to parse FLAC headers');
  267. return false;
  268. }
  269. unset($flac);
  270. break;
  271. case 'speex':
  272. $this->fseek($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length'], SEEK_CUR);
  273. $this->ParseVorbisComments();
  274. break;
  275. case 'opus':
  276. $filedata = $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']);
  277. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, 0, 8); // hard-coded to 'OpusTags'
  278. if(substr($filedata, 0, 8) != 'OpusTags') {
  279. $this->error('Expected "OpusTags" as header but got "'.substr($filedata, 0, 8).'"');
  280. return false;
  281. }
  282. $this->ParseVorbisComments();
  283. break;
  284. }
  285. // Last Page - Number of Samples
  286. if (!getid3_lib::intValueSupported($info['avdataend'])) {
  287. $this->warning('Unable to parse Ogg end chunk file (PHP does not support file operations beyond '.round(PHP_INT_MAX / 1073741824).'GB)');
  288. } else {
  289. $this->fseek(max($info['avdataend'] - $this->getid3->fread_buffer_size(), 0));
  290. $LastChunkOfOgg = strrev($this->fread($this->getid3->fread_buffer_size()));
  291. if ($LastOggSpostion = strpos($LastChunkOfOgg, 'SggO')) {
  292. $this->fseek($info['avdataend'] - ($LastOggSpostion + strlen('SggO')));
  293. $info['avdataend'] = $this->ftell();
  294. $info['ogg']['pageheader']['eos'] = $this->ParseOggPageHeader();
  295. $info['ogg']['samples'] = $info['ogg']['pageheader']['eos']['pcm_abs_position'];
  296. if ($info['ogg']['samples'] == 0) {
  297. $this->error('Corrupt Ogg file: eos.number of samples == zero');
  298. return false;
  299. }
  300. if (!empty($info['audio']['sample_rate'])) {
  301. $info['ogg']['bitrate_average'] = (($info['avdataend'] - $info['avdataoffset']) * 8) / ($info['ogg']['samples'] / $info['audio']['sample_rate']);
  302. }
  303. }
  304. }
  305. if (!empty($info['ogg']['bitrate_average'])) {
  306. $info['audio']['bitrate'] = $info['ogg']['bitrate_average'];
  307. } elseif (!empty($info['ogg']['bitrate_nominal'])) {
  308. $info['audio']['bitrate'] = $info['ogg']['bitrate_nominal'];
  309. } elseif (!empty($info['ogg']['bitrate_min']) && !empty($info['ogg']['bitrate_max'])) {
  310. $info['audio']['bitrate'] = ($info['ogg']['bitrate_min'] + $info['ogg']['bitrate_max']) / 2;
  311. }
  312. if (isset($info['audio']['bitrate']) && !isset($info['playtime_seconds'])) {
  313. if ($info['audio']['bitrate'] == 0) {
  314. $this->error('Corrupt Ogg file: bitrate_audio == zero');
  315. return false;
  316. }
  317. $info['playtime_seconds'] = (float) ((($info['avdataend'] - $info['avdataoffset']) * 8) / $info['audio']['bitrate']);
  318. }
  319. if (isset($info['ogg']['vendor'])) {
  320. $info['audio']['encoder'] = preg_replace('/^Encoded with /', '', $info['ogg']['vendor']);
  321. // Vorbis only
  322. if ($info['audio']['dataformat'] == 'vorbis') {
  323. // Vorbis 1.0 starts with Xiph.Org
  324. if (preg_match('/^Xiph.Org/', $info['audio']['encoder'])) {
  325. if ($info['audio']['bitrate_mode'] == 'abr') {
  326. // Set -b 128 on abr files
  327. $info['audio']['encoder_options'] = '-b '.round($info['ogg']['bitrate_nominal'] / 1000);
  328. } elseif (($info['audio']['bitrate_mode'] == 'vbr') && ($info['audio']['channels'] == 2) && ($info['audio']['sample_rate'] >= 44100) && ($info['audio']['sample_rate'] <= 48000)) {
  329. // Set -q N on vbr files
  330. $info['audio']['encoder_options'] = '-q '.$this->get_quality_from_nominal_bitrate($info['ogg']['bitrate_nominal']);
  331. }
  332. }
  333. if (empty($info['audio']['encoder_options']) && !empty($info['ogg']['bitrate_nominal'])) {
  334. $info['audio']['encoder_options'] = 'Nominal bitrate: '.intval(round($info['ogg']['bitrate_nominal'] / 1000)).'kbps';
  335. }
  336. }
  337. }
  338. return true;
  339. }
  340. /**
  341. * @param string $filedata
  342. * @param int $filedataoffset
  343. * @param array $oggpageinfo
  344. *
  345. * @return bool
  346. */
  347. public function ParseVorbisPageHeader(&$filedata, &$filedataoffset, &$oggpageinfo) {
  348. $info = &$this->getid3->info;
  349. $info['audio']['dataformat'] = 'vorbis';
  350. $info['audio']['lossless'] = false;
  351. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['packet_type'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  352. $filedataoffset += 1;
  353. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['stream_type'] = substr($filedata, $filedataoffset, 6); // hard-coded to 'vorbis'
  354. $filedataoffset += 6;
  355. $info['ogg']['bitstreamversion'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  356. $filedataoffset += 4;
  357. $info['ogg']['numberofchannels'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  358. $filedataoffset += 1;
  359. $info['audio']['channels'] = $info['ogg']['numberofchannels'];
  360. $info['ogg']['samplerate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  361. $filedataoffset += 4;
  362. if ($info['ogg']['samplerate'] == 0) {
  363. $this->error('Corrupt Ogg file: sample rate == zero');
  364. return false;
  365. }
  366. $info['audio']['sample_rate'] = $info['ogg']['samplerate'];
  367. $info['ogg']['samples'] = 0; // filled in later
  368. $info['ogg']['bitrate_average'] = 0; // filled in later
  369. $info['ogg']['bitrate_max'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  370. $filedataoffset += 4;
  371. $info['ogg']['bitrate_nominal'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  372. $filedataoffset += 4;
  373. $info['ogg']['bitrate_min'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  374. $filedataoffset += 4;
  375. $info['ogg']['blocksize_small'] = pow(2, getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)) & 0x0F);
  376. $info['ogg']['blocksize_large'] = pow(2, (getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)) & 0xF0) >> 4);
  377. $info['ogg']['stop_bit'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1)); // must be 1, marks end of packet
  378. $info['audio']['bitrate_mode'] = 'vbr'; // overridden if actually abr
  379. if ($info['ogg']['bitrate_max'] == 0xFFFFFFFF) {
  380. unset($info['ogg']['bitrate_max']);
  381. $info['audio']['bitrate_mode'] = 'abr';
  382. }
  383. if ($info['ogg']['bitrate_nominal'] == 0xFFFFFFFF) {
  384. unset($info['ogg']['bitrate_nominal']);
  385. }
  386. if ($info['ogg']['bitrate_min'] == 0xFFFFFFFF) {
  387. unset($info['ogg']['bitrate_min']);
  388. $info['audio']['bitrate_mode'] = 'abr';
  389. }
  390. return true;
  391. }
  392. /**
  393. * @link http://tools.ietf.org/html/draft-ietf-codec-oggopus-03
  394. *
  395. * @param string $filedata
  396. * @param int $filedataoffset
  397. * @param array $oggpageinfo
  398. *
  399. * @return bool
  400. */
  401. public function ParseOpusPageHeader(&$filedata, &$filedataoffset, &$oggpageinfo) {
  402. $info = &$this->getid3->info;
  403. $info['audio']['dataformat'] = 'opus';
  404. $info['mime_type'] = 'audio/ogg; codecs=opus';
  405. /** @todo find a usable way to detect abr (vbr that is padded to be abr) */
  406. $info['audio']['bitrate_mode'] = 'vbr';
  407. $info['audio']['lossless'] = false;
  408. $info['ogg']['pageheader']['opus']['opus_magic'] = substr($filedata, $filedataoffset, 8); // hard-coded to 'OpusHead'
  409. $filedataoffset += 8;
  410. $info['ogg']['pageheader']['opus']['version'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  411. $filedataoffset += 1;
  412. if ($info['ogg']['pageheader']['opus']['version'] < 1 || $info['ogg']['pageheader']['opus']['version'] > 15) {
  413. $this->error('Unknown opus version number (only accepting 1-15)');
  414. return false;
  415. }
  416. $info['ogg']['pageheader']['opus']['out_channel_count'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  417. $filedataoffset += 1;
  418. if ($info['ogg']['pageheader']['opus']['out_channel_count'] == 0) {
  419. $this->error('Invalid channel count in opus header (must not be zero)');
  420. return false;
  421. }
  422. $info['ogg']['pageheader']['opus']['pre_skip'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2));
  423. $filedataoffset += 2;
  424. $info['ogg']['pageheader']['opus']['input_sample_rate'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  425. $filedataoffset += 4;
  426. //$info['ogg']['pageheader']['opus']['output_gain'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 2));
  427. //$filedataoffset += 2;
  428. //$info['ogg']['pageheader']['opus']['channel_mapping_family'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  429. //$filedataoffset += 1;
  430. $info['opus']['opus_version'] = $info['ogg']['pageheader']['opus']['version'];
  431. $info['opus']['sample_rate_input'] = $info['ogg']['pageheader']['opus']['input_sample_rate'];
  432. $info['opus']['out_channel_count'] = $info['ogg']['pageheader']['opus']['out_channel_count'];
  433. $info['audio']['channels'] = $info['opus']['out_channel_count'];
  434. $info['audio']['sample_rate_input'] = $info['opus']['sample_rate_input'];
  435. $info['audio']['sample_rate'] = 48000; // "All Opus audio is coded at 48 kHz, and should also be decoded at 48 kHz for playback (unless the target hardware does not support this sampling rate). However, this field may be used to resample the audio back to the original sampling rate, for example, when saving the output to a file." -- https://mf4.xiph.org/jenkins/view/opus/job/opusfile-unix/ws/doc/html/structOpusHead.html
  436. return true;
  437. }
  438. /**
  439. * @return array|false
  440. */
  441. public function ParseOggPageHeader() {
  442. // http://xiph.org/ogg/vorbis/doc/framing.html
  443. $oggheader = array();
  444. $oggheader['page_start_offset'] = $this->ftell(); // where we started from in the file
  445. $filedata = $this->fread($this->getid3->fread_buffer_size());
  446. $filedataoffset = 0;
  447. while ((substr($filedata, $filedataoffset++, 4) != 'OggS')) {
  448. if (($this->ftell() - $oggheader['page_start_offset']) >= $this->getid3->fread_buffer_size()) {
  449. // should be found before here
  450. return false;
  451. }
  452. if ((($filedataoffset + 28) > strlen($filedata)) || (strlen($filedata) < 28)) {
  453. if ($this->feof() || (($filedata .= $this->fread($this->getid3->fread_buffer_size())) === '')) {
  454. // get some more data, unless eof, in which case fail
  455. return false;
  456. }
  457. }
  458. }
  459. $filedataoffset += strlen('OggS') - 1; // page, delimited by 'OggS'
  460. $oggheader['stream_structver'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  461. $filedataoffset += 1;
  462. $oggheader['flags_raw'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  463. $filedataoffset += 1;
  464. $oggheader['flags']['fresh'] = (bool) ($oggheader['flags_raw'] & 0x01); // fresh packet
  465. $oggheader['flags']['bos'] = (bool) ($oggheader['flags_raw'] & 0x02); // first page of logical bitstream (bos)
  466. $oggheader['flags']['eos'] = (bool) ($oggheader['flags_raw'] & 0x04); // last page of logical bitstream (eos)
  467. $oggheader['pcm_abs_position'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 8));
  468. $filedataoffset += 8;
  469. $oggheader['stream_serialno'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  470. $filedataoffset += 4;
  471. $oggheader['page_seqno'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  472. $filedataoffset += 4;
  473. $oggheader['page_checksum'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 4));
  474. $filedataoffset += 4;
  475. $oggheader['page_segments'] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  476. $filedataoffset += 1;
  477. $oggheader['page_length'] = 0;
  478. for ($i = 0; $i < $oggheader['page_segments']; $i++) {
  479. $oggheader['segment_table'][$i] = getid3_lib::LittleEndian2Int(substr($filedata, $filedataoffset, 1));
  480. $filedataoffset += 1;
  481. $oggheader['page_length'] += $oggheader['segment_table'][$i];
  482. }
  483. $oggheader['header_end_offset'] = $oggheader['page_start_offset'] + $filedataoffset;
  484. $oggheader['page_end_offset'] = $oggheader['header_end_offset'] + $oggheader['page_length'];
  485. $this->fseek($oggheader['header_end_offset']);
  486. return $oggheader;
  487. }
  488. /**
  489. * @link http://xiph.org/vorbis/doc/Vorbis_I_spec.html#x1-810005
  490. *
  491. * @return bool
  492. */
  493. public function ParseVorbisComments() {
  494. $info = &$this->getid3->info;
  495. $OriginalOffset = $this->ftell();
  496. $commentdata = null;
  497. $commentdataoffset = 0;
  498. $VorbisCommentPage = 1;
  499. $CommentStartOffset = 0;
  500. switch ($info['audio']['dataformat']) {
  501. case 'vorbis':
  502. case 'speex':
  503. case 'opus':
  504. $CommentStartOffset = $info['ogg']['pageheader'][$VorbisCommentPage]['page_start_offset']; // Second Ogg page, after header block
  505. $this->fseek($CommentStartOffset);
  506. $commentdataoffset = 27 + $info['ogg']['pageheader'][$VorbisCommentPage]['page_segments'];
  507. $commentdata = $this->fread(self::OggPageSegmentLength($info['ogg']['pageheader'][$VorbisCommentPage], 1) + $commentdataoffset);
  508. if ($info['audio']['dataformat'] == 'vorbis') {
  509. $commentdataoffset += (strlen('vorbis') + 1);
  510. }
  511. else if ($info['audio']['dataformat'] == 'opus') {
  512. $commentdataoffset += strlen('OpusTags');
  513. }
  514. break;
  515. case 'flac':
  516. $CommentStartOffset = $info['flac']['VORBIS_COMMENT']['raw']['offset'] + 4;
  517. $this->fseek($CommentStartOffset);
  518. $commentdata = $this->fread($info['flac']['VORBIS_COMMENT']['raw']['block_length']);
  519. break;
  520. default:
  521. return false;
  522. }
  523. $VendorSize = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4));
  524. $commentdataoffset += 4;
  525. $info['ogg']['vendor'] = substr($commentdata, $commentdataoffset, $VendorSize);
  526. $commentdataoffset += $VendorSize;
  527. $CommentsCount = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4));
  528. $commentdataoffset += 4;
  529. $info['avdataoffset'] = $CommentStartOffset + $commentdataoffset;
  530. $basicfields = array('TITLE', 'ARTIST', 'ALBUM', 'TRACKNUMBER', 'GENRE', 'DATE', 'DESCRIPTION', 'COMMENT');
  531. $ThisFileInfo_ogg_comments_raw = &$info['ogg']['comments_raw'];
  532. for ($i = 0; $i < $CommentsCount; $i++) {
  533. if ($i >= 10000) {
  534. // https://github.com/owncloud/music/issues/212#issuecomment-43082336
  535. $this->warning('Unexpectedly large number ('.$CommentsCount.') of Ogg comments - breaking after reading '.$i.' comments');
  536. break;
  537. }
  538. $ThisFileInfo_ogg_comments_raw[$i]['dataoffset'] = $CommentStartOffset + $commentdataoffset;
  539. if ($this->ftell() < ($ThisFileInfo_ogg_comments_raw[$i]['dataoffset'] + 4)) {
  540. if ($oggpageinfo = $this->ParseOggPageHeader()) {
  541. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  542. $VorbisCommentPage++;
  543. // First, save what we haven't read yet
  544. $AsYetUnusedData = substr($commentdata, $commentdataoffset);
  545. // Then take that data off the end
  546. $commentdata = substr($commentdata, 0, $commentdataoffset);
  547. // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct
  548. $commentdata .= str_repeat("\x00", 27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  549. $commentdataoffset += (27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  550. // Finally, stick the unused data back on the end
  551. $commentdata .= $AsYetUnusedData;
  552. //$commentdata .= $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']);
  553. $commentdata .= $this->fread($this->OggPageSegmentLength($info['ogg']['pageheader'][$VorbisCommentPage], 1));
  554. }
  555. }
  556. $ThisFileInfo_ogg_comments_raw[$i]['size'] = getid3_lib::LittleEndian2Int(substr($commentdata, $commentdataoffset, 4));
  557. // replace avdataoffset with position just after the last vorbiscomment
  558. $info['avdataoffset'] = $ThisFileInfo_ogg_comments_raw[$i]['dataoffset'] + $ThisFileInfo_ogg_comments_raw[$i]['size'] + 4;
  559. $commentdataoffset += 4;
  560. while ((strlen($commentdata) - $commentdataoffset) < $ThisFileInfo_ogg_comments_raw[$i]['size']) {
  561. if (($ThisFileInfo_ogg_comments_raw[$i]['size'] > $info['avdataend']) || ($ThisFileInfo_ogg_comments_raw[$i]['size'] < 0)) {
  562. $this->warning('Invalid Ogg comment size (comment #'.$i.', claims to be '.number_format($ThisFileInfo_ogg_comments_raw[$i]['size']).' bytes) - aborting reading comments');
  563. break 2;
  564. }
  565. $VorbisCommentPage++;
  566. if ($oggpageinfo = $this->ParseOggPageHeader()) {
  567. $info['ogg']['pageheader'][$oggpageinfo['page_seqno']] = $oggpageinfo;
  568. // First, save what we haven't read yet
  569. $AsYetUnusedData = substr($commentdata, $commentdataoffset);
  570. // Then take that data off the end
  571. $commentdata = substr($commentdata, 0, $commentdataoffset);
  572. // Add [headerlength] bytes of dummy data for the Ogg Page Header, just to keep absolute offsets correct
  573. $commentdata .= str_repeat("\x00", 27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  574. $commentdataoffset += (27 + $info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_segments']);
  575. // Finally, stick the unused data back on the end
  576. $commentdata .= $AsYetUnusedData;
  577. //$commentdata .= $this->fread($info['ogg']['pageheader'][$oggpageinfo['page_seqno']]['page_length']);
  578. if (!isset($info['ogg']['pageheader'][$VorbisCommentPage])) {
  579. $this->warning('undefined Vorbis Comment page "'.$VorbisCommentPage.'" at offset '.$this->ftell());
  580. break;
  581. }
  582. $readlength = self::OggPageSegmentLength($info['ogg']['pageheader'][$VorbisCommentPage], 1);
  583. if ($readlength <= 0) {
  584. $this->warning('invalid length Vorbis Comment page "'.$VorbisCommentPage.'" at offset '.$this->ftell());
  585. break;
  586. }
  587. $commentdata .= $this->fread($readlength);
  588. //$filebaseoffset += $oggpageinfo['header_end_offset'] - $oggpageinfo['page_start_offset'];
  589. } else {
  590. $this->warning('failed to ParseOggPageHeader() at offset '.$this->ftell());
  591. break;
  592. }
  593. }
  594. $ThisFileInfo_ogg_comments_raw[$i]['offset'] = $commentdataoffset;
  595. $commentstring = substr($commentdata, $commentdataoffset, $ThisFileInfo_ogg_comments_raw[$i]['size']);
  596. $commentdataoffset += $ThisFileInfo_ogg_comments_raw[$i]['size'];
  597. if (!$commentstring) {
  598. // no comment?
  599. $this->warning('Blank Ogg comment ['.$i.']');
  600. } elseif (strstr($commentstring, '=')) {
  601. $commentexploded = explode('=', $commentstring, 2);
  602. $ThisFileInfo_ogg_comments_raw[$i]['key'] = strtoupper($commentexploded[0]);
  603. $ThisFileInfo_ogg_comments_raw[$i]['value'] = (isset($commentexploded[1]) ? $commentexploded[1] : '');
  604. if ($ThisFileInfo_ogg_comments_raw[$i]['key'] == 'METADATA_BLOCK_PICTURE') {
  605. // http://wiki.xiph.org/VorbisComment#METADATA_BLOCK_PICTURE
  606. // The unencoded format is that of the FLAC picture block. The fields are stored in big endian order as in FLAC, picture data is stored according to the relevant standard.
  607. // http://flac.sourceforge.net/format.html#metadata_block_picture
  608. $flac = new getid3_flac($this->getid3);
  609. $flac->setStringMode(base64_decode($ThisFileInfo_ogg_comments_raw[$i]['value']));
  610. $flac->parsePICTURE();
  611. $info['ogg']['comments']['picture'][] = $flac->getid3->info['flac']['PICTURE'][0];
  612. unset($flac);
  613. } elseif ($ThisFileInfo_ogg_comments_raw[$i]['key'] == 'COVERART') {
  614. $data = base64_decode($ThisFileInfo_ogg_comments_raw[$i]['value']);
  615. $this->notice('Found deprecated COVERART tag, it should be replaced in honor of METADATA_BLOCK_PICTURE structure');
  616. /** @todo use 'coverartmime' where available */
  617. $imageinfo = getid3_lib::GetDataImageSize($data);
  618. if ($imageinfo === false || !isset($imageinfo['mime'])) {
  619. $this->warning('COVERART vorbiscomment tag contains invalid image');
  620. continue;
  621. }
  622. $ogg = new self($this->getid3);
  623. $ogg->setStringMode($data);
  624. $info['ogg']['comments']['picture'][] = array(
  625. 'image_mime' => $imageinfo['mime'],
  626. 'datalength' => strlen($data),
  627. 'picturetype' => 'cover art',
  628. 'image_height' => $imageinfo['height'],
  629. 'image_width' => $imageinfo['width'],
  630. 'data' => $ogg->saveAttachment('coverart', 0, strlen($data), $imageinfo['mime']),
  631. );
  632. unset($ogg);
  633. } else {
  634. $info['ogg']['comments'][strtolower($ThisFileInfo_ogg_comments_raw[$i]['key'])][] = $ThisFileInfo_ogg_comments_raw[$i]['value'];
  635. }
  636. } else {
  637. $this->warning('[known problem with CDex >= v1.40, < v1.50b7] Invalid Ogg comment name/value pair ['.$i.']: '.$commentstring);
  638. }
  639. unset($ThisFileInfo_ogg_comments_raw[$i]);
  640. }
  641. unset($ThisFileInfo_ogg_comments_raw);
  642. // Replay Gain Adjustment
  643. // http://privatewww.essex.ac.uk/~djmrob/replaygain/
  644. if (isset($info['ogg']['comments']) && is_array($info['ogg']['comments'])) {
  645. foreach ($info['ogg']['comments'] as $index => $commentvalue) {
  646. switch ($index) {
  647. case 'rg_audiophile':
  648. case 'replaygain_album_gain':
  649. $info['replay_gain']['album']['adjustment'] = (double) $commentvalue[0];
  650. unset($info['ogg']['comments'][$index]);
  651. break;
  652. case 'rg_radio':
  653. case 'replaygain_track_gain':
  654. $info['replay_gain']['track']['adjustment'] = (double) $commentvalue[0];
  655. unset($info['ogg']['comments'][$index]);
  656. break;
  657. case 'replaygain_album_peak':
  658. $info['replay_gain']['album']['peak'] = (double) $commentvalue[0];
  659. unset($info['ogg']['comments'][$index]);
  660. break;
  661. case 'rg_peak':
  662. case 'replaygain_track_peak':
  663. $info['replay_gain']['track']['peak'] = (double) $commentvalue[0];
  664. unset($info['ogg']['comments'][$index]);
  665. break;
  666. case 'replaygain_reference_loudness':
  667. $info['replay_gain']['reference_volume'] = (double) $commentvalue[0];
  668. unset($info['ogg']['comments'][$index]);
  669. break;
  670. default:
  671. // do nothing
  672. break;
  673. }
  674. }
  675. }
  676. $this->fseek($OriginalOffset);
  677. return true;
  678. }
  679. /**
  680. * @param int $mode
  681. *
  682. * @return string|null
  683. */
  684. public static function SpeexBandModeLookup($mode) {
  685. static $SpeexBandModeLookup = array();
  686. if (empty($SpeexBandModeLookup)) {
  687. $SpeexBandModeLookup[0] = 'narrow';
  688. $SpeexBandModeLookup[1] = 'wide';
  689. $SpeexBandModeLookup[2] = 'ultra-wide';
  690. }
  691. return (isset($SpeexBandModeLookup[$mode]) ? $SpeexBandModeLookup[$mode] : null);
  692. }
  693. /**
  694. * @param array $OggInfoArray
  695. * @param int $SegmentNumber
  696. *
  697. * @return int
  698. */
  699. public static function OggPageSegmentLength($OggInfoArray, $SegmentNumber=1) {
  700. $segmentlength = 0;
  701. for ($i = 0; $i < $SegmentNumber; $i++) {
  702. $segmentlength = 0;
  703. foreach ($OggInfoArray['segment_table'] as $key => $value) {
  704. $segmentlength += $value;
  705. if ($value < 255) {
  706. break;
  707. }
  708. }
  709. }
  710. return $segmentlength;
  711. }
  712. /**
  713. * @param int $nominal_bitrate
  714. *
  715. * @return float
  716. */
  717. public static function get_quality_from_nominal_bitrate($nominal_bitrate) {
  718. // decrease precision
  719. $nominal_bitrate = $nominal_bitrate / 1000;
  720. if ($nominal_bitrate < 128) {
  721. // q-1 to q4
  722. $qval = ($nominal_bitrate - 64) / 16;
  723. } elseif ($nominal_bitrate < 256) {
  724. // q4 to q8
  725. $qval = $nominal_bitrate / 32;
  726. } elseif ($nominal_bitrate < 320) {
  727. // q8 to q9
  728. $qval = ($nominal_bitrate + 256) / 64;
  729. } else {
  730. // q9 to q10
  731. $qval = ($nominal_bitrate + 1300) / 180;
  732. }
  733. //return $qval; // 5.031324
  734. //return intval($qval); // 5
  735. return round($qval, 1); // 5 or 4.9
  736. }
  737. /**
  738. * @param int $colorspace_id
  739. *
  740. * @return string|null
  741. */
  742. public static function TheoraColorSpace($colorspace_id) {
  743. // http://www.theora.org/doc/Theora.pdf (table 6.3)
  744. static $TheoraColorSpaceLookup = array();
  745. if (empty($TheoraColorSpaceLookup)) {
  746. $TheoraColorSpaceLookup[0] = 'Undefined';
  747. $TheoraColorSpaceLookup[1] = 'Rec. 470M';
  748. $TheoraColorSpaceLookup[2] = 'Rec. 470BG';
  749. $TheoraColorSpaceLookup[3] = 'Reserved';
  750. }
  751. return (isset($TheoraColorSpaceLookup[$colorspace_id]) ? $TheoraColorSpaceLookup[$colorspace_id] : null);
  752. }
  753. /**
  754. * @param int $pixelformat_id
  755. *
  756. * @return string|null
  757. */
  758. public static function TheoraPixelFormat($pixelformat_id) {
  759. // http://www.theora.org/doc/Theora.pdf (table 6.4)
  760. static $TheoraPixelFormatLookup = array();
  761. if (empty($TheoraPixelFormatLookup)) {
  762. $TheoraPixelFormatLookup[0] = '4:2:0';
  763. $TheoraPixelFormatLookup[1] = 'Reserved';
  764. $TheoraPixelFormatLookup[2] = '4:2:2';
  765. $TheoraPixelFormatLookup[3] = '4:4:4';
  766. }
  767. return (isset($TheoraPixelFormatLookup[$pixelformat_id]) ? $TheoraPixelFormatLookup[$pixelformat_id] : null);
  768. }
  769. }