pyfieldbridge 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. pyfieldbridge-0.1.0/LICENSE +21 -0
  2. pyfieldbridge-0.1.0/PKG-INFO +359 -0
  3. pyfieldbridge-0.1.0/README.md +325 -0
  4. pyfieldbridge-0.1.0/fieldbridge/__init__.py +93 -0
  5. pyfieldbridge-0.1.0/fieldbridge/adapters/__init__.py +1 -0
  6. pyfieldbridge-0.1.0/fieldbridge/adapters/can.py +30 -0
  7. pyfieldbridge-0.1.0/fieldbridge/adapters/ethercat.py +15 -0
  8. pyfieldbridge-0.1.0/fieldbridge/adapters/ethernet.py +18 -0
  9. pyfieldbridge-0.1.0/fieldbridge/adapters/flexray.py +18 -0
  10. pyfieldbridge-0.1.0/fieldbridge/adapters/index.py +56 -0
  11. pyfieldbridge-0.1.0/fieldbridge/adapters/lin.py +17 -0
  12. pyfieldbridge-0.1.0/fieldbridge/adapters/modbus.py +30 -0
  13. pyfieldbridge-0.1.0/fieldbridge/adapters/profibus_dp.py +4 -0
  14. pyfieldbridge-0.1.0/fieldbridge/adapters/profibus_pa.py +17 -0
  15. pyfieldbridge-0.1.0/fieldbridge/adapters/profinet.py +18 -0
  16. pyfieldbridge-0.1.0/fieldbridge/adapters/rules.py +22 -0
  17. pyfieldbridge-0.1.0/fieldbridge/core/__init__.py +6 -0
  18. pyfieldbridge-0.1.0/fieldbridge/core/adapter.py +79 -0
  19. pyfieldbridge-0.1.0/fieldbridge/core/convert.py +384 -0
  20. pyfieldbridge-0.1.0/fieldbridge/core/errors.py +50 -0
  21. pyfieldbridge-0.1.0/fieldbridge/core/result.py +19 -0
  22. pyfieldbridge-0.1.0/fieldbridge/core/types.py +186 -0
  23. pyfieldbridge-0.1.0/fieldbridge/core/validation.py +100 -0
  24. pyfieldbridge-0.1.0/fieldbridge/py.typed +0 -0
  25. pyfieldbridge-0.1.0/fieldbridge/wire/__init__.py +33 -0
  26. pyfieldbridge-0.1.0/fieldbridge/wire/bytes.py +99 -0
  27. pyfieldbridge-0.1.0/fieldbridge/wire/can.py +262 -0
  28. pyfieldbridge-0.1.0/fieldbridge/wire/ethernet.py +327 -0
  29. pyfieldbridge-0.1.0/fieldbridge/wire/index.py +97 -0
  30. pyfieldbridge-0.1.0/fieldbridge/wire/modbus.py +116 -0
  31. pyfieldbridge-0.1.0/pyfieldbridge/__init__.py +4 -0
  32. pyfieldbridge-0.1.0/pyfieldbridge/py.typed +0 -0
  33. pyfieldbridge-0.1.0/pyfieldbridge/wire.py +1 -0
  34. pyfieldbridge-0.1.0/pyfieldbridge.egg-info/PKG-INFO +359 -0
  35. pyfieldbridge-0.1.0/pyfieldbridge.egg-info/SOURCES.txt +41 -0
  36. pyfieldbridge-0.1.0/pyfieldbridge.egg-info/dependency_links.txt +1 -0
  37. pyfieldbridge-0.1.0/pyfieldbridge.egg-info/requires.txt +7 -0
  38. pyfieldbridge-0.1.0/pyfieldbridge.egg-info/top_level.txt +2 -0
  39. pyfieldbridge-0.1.0/pyproject.toml +53 -0
  40. pyfieldbridge-0.1.0/setup.cfg +4 -0
  41. pyfieldbridge-0.1.0/tests/test_conversion.py +111 -0
  42. pyfieldbridge-0.1.0/tests/test_cross_language_parity.py +218 -0
  43. pyfieldbridge-0.1.0/tests/test_wire.py +86 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mohamed Mahmoud
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,359 @@
1
+ Metadata-Version: 2.4
2
+ Name: pyfieldbridge
3
+ Version: 0.1.0
4
+ Summary: Python conversion for industrial and automotive fieldbus frame envelopes
5
+ Author-email: Mohamed Mahmoud <imohamed.abdelhamed@gmail.com>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/MAbdelhamid-Admin/PyfieldBridge
8
+ Project-URL: Repository, https://github.com/MAbdelhamid-Admin/PyfieldBridge
9
+ Project-URL: Issues, https://github.com/MAbdelhamid-Admin/PyfieldBridge/issues
10
+ Project-URL: Node.js / TypeScript version, https://github.com/MAbdelhamid-Admin/fieldBridge
11
+ Project-URL: Documentation, https://mabdelhamid-admin.github.io/PyfieldBridge/
12
+ Keywords: industrial,fieldbus,protocol,converter,gateway,modbus,profibus,profinet,ethercat,can,can-fd,lin,flexray
13
+ Classifier: Development Status :: 3 - Alpha
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Classifier: Topic :: Software Development :: Libraries
23
+ Classifier: Topic :: System :: Networking
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.10
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Provides-Extra: docs
29
+ Requires-Dist: pdoc>=15; extra == "docs"
30
+ Provides-Extra: dev
31
+ Requires-Dist: pytest>=8; extra == "dev"
32
+ Requires-Dist: pdoc>=15; extra == "dev"
33
+ Dynamic: license-file
34
+
35
+ # PyfieldBridge
36
+
37
+ [![CI](https://github.com/MAbdelhamid-Admin/PyfieldBridge/actions/workflows/ci.yml/badge.svg)](https://github.com/MAbdelhamid-Admin/PyfieldBridge/actions/workflows/ci.yml)
38
+ [![Docs](https://img.shields.io/badge/docs-online-blue.svg)](https://mabdelhamid-admin.github.io/PyfieldBridge/)
39
+ [![Python](https://img.shields.io/badge/python-3.10%2B-blue.svg)](https://www.python.org/)
40
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE)
41
+
42
+ Validated conversion of **frame envelopes** between industrial and automotive
43
+ protocols in Python: Ethernet, PROFINET, PROFIBUS DP/PA, Modbus RTU/TCP,
44
+ CAN / CAN FD, LIN, FlexRay and EtherCAT.
45
+
46
+ - Strict validation against each protocol's limits on both encode and decode
47
+ - Payload bytes are preserved exactly; nothing is silently guessed
48
+ - Device IDs bridge message-addressed buses (CAN, LIN) and device-addressed
49
+ buses (Modbus, PROFIBUS, EtherCAT)
50
+ - Parse and serialize real wire bytes: Modbus TCP/RTU, SocketCAN and candump,
51
+ Ethernet, PROFINET RT, EtherCAT
52
+ - Typed `FieldbridgeError` exceptions with stable codes, plus a non-throwing
53
+ `try_*` API
54
+ - No runtime dependencies; Python 3.10+
55
+
56
+ Using Node.js or TypeScript? See the [`fieldbridge`](https://github.com/MAbdelhamid-Admin/fieldBridge)
57
+ package for the same protocol model on that platform.
58
+
59
+ **Documentation:** https://mabdelhamid-admin.github.io/PyfieldBridge/
60
+
61
+ ## Install
62
+
63
+ From PyPI:
64
+
65
+ ```bash
66
+ python -m pip install pyfieldbridge
67
+ ```
68
+
69
+ From a checkout:
70
+
71
+ ```bash
72
+ python -m pip install -e .
73
+ ```
74
+
75
+ The import package is `fieldbridge`; `pyfieldbridge` is provided as a
76
+ compatibility package name.
77
+
78
+ ## Quick start
79
+
80
+ ```python
81
+ from fieldbridge import convert_message
82
+
83
+ result = convert_message({
84
+ "source": {
85
+ "device_id": 17,
86
+ "message": {"protocol": "can", "identifier": 0x123, "payload": [3, 0, 10]},
87
+ },
88
+ "target": {"protocol": "modbus-rtu"},
89
+ })
90
+
91
+ result.message
92
+ # {'protocol': 'modbus-rtu', 'address': 17, 'device_id': 17, 'payload': [3, 0, 10]}
93
+ result.warnings
94
+ # explains what was inferred, overridden or dropped
95
+ ```
96
+
97
+ Every conversion returns `warnings` describing anything that was inferred,
98
+ overridden or dropped. Log them; they are how you learn what changed.
99
+
100
+ ## Scope and limitations
101
+
102
+ PyfieldBridge converts the **envelope** (addresses, identifiers, payload
103
+ limits, metadata) and preserves payload bytes. It does **not** perform
104
+ semantic, bit-level translation: process images, device profiles, cycle
105
+ timing, safety semantics, endianness and vendor-specific mappings cannot be
106
+ inferred safely from an arbitrary frame. Applications that know those details
107
+ can use a `signals` mapping or supply their own adapter.
108
+
109
+ ## API
110
+
111
+ ### `convert_frame({ source, target_protocol, options? })`
112
+
113
+ Converts a single frame.
114
+
115
+ ```python
116
+ from fieldbridge import convert_frame
117
+
118
+ result = convert_frame({
119
+ "source": {"protocol": "ethernet", "payload": [1], "signals": {"speed": 42}},
120
+ "target_protocol": "ethercat",
121
+ "options": {
122
+ "mapping": {"speed": "velocity"}, # rename/select signals; unlisted ones are dropped
123
+ "metadata": {"cycle": "1ms"}, # metadata for the target frame
124
+ "target_device_id": 3, # target device if the frame has no address
125
+ },
126
+ })
127
+
128
+ result.frame
129
+ result.warnings
130
+ ```
131
+
132
+ ### `convert_message({ source, target, options? })`
133
+
134
+ Converts a message that comes with a device identity and context.
135
+
136
+ ```python
137
+ from fieldbridge import convert_message
138
+
139
+ convert_message({
140
+ "source": {
141
+ "device_id": "gateway-1",
142
+ "context": {"line": "A"},
143
+ "message": {"protocol": "can", "identifier": 0x12, "payload": [1, 2]},
144
+ },
145
+ "target": {
146
+ "protocol": "lin",
147
+ "context": {"schedule": "body-controller-10ms"},
148
+ },
149
+ })
150
+ ```
151
+
152
+ - `source.device_id` identifies the source device.
153
+ - `source.context` holds extra facts about the source, used as defaults for the
154
+ message metadata.
155
+ - `target.device_id` is the device to address on the target bus.
156
+ - `target.context` holds target-specific fields (schedule, slot, profile…). If
157
+ an adapter declares required context keys, they must appear here; a missing
158
+ key fails the conversion instead of being guessed.
159
+
160
+ ### Non-throwing API
161
+
162
+ ```python
163
+ from fieldbridge import try_convert_message
164
+
165
+ result = try_convert_message(request)
166
+ if result.ok:
167
+ send(result.value.message)
168
+ else:
169
+ print(result.error.code, result.error) # e.g. "INVALID_DEVICE_ID"
170
+ ```
171
+
172
+ The `try_*` functions never raise. Errors raised inside a custom adapter are
173
+ wrapped with code `INTERNAL`, and the original exception is kept in `cause`.
174
+
175
+ ### Errors
176
+
177
+ The throwing functions only raise `FieldbridgeError`, which has a stable `code`
178
+ and, where relevant, `protocol` and `field`. Branch on `code`, not on the
179
+ message text.
180
+
181
+ | Code | Meaning |
182
+ | --- | --- |
183
+ | `UNSUPPORTED_PROTOCOL` | Unknown protocol name |
184
+ | `INVALID_FRAME` | Input is not a valid frame/request object |
185
+ | `PROTOCOL_MISMATCH` | Frame given to the adapter of a different protocol |
186
+ | `INVALID_PAYLOAD` | Non-byte values or a protocol length rule violated |
187
+ | `PAYLOAD_TOO_LARGE` | Payload exceeds the protocol maximum |
188
+ | `MISSING_FIELD` | A required field (e.g. CAN identifier, Modbus address) is absent |
189
+ | `INVALID_FIELD` | A field is out of range |
190
+ | `MISSING_CONTEXT` | A required target context key is absent |
191
+ | `INVALID_DEVICE_ID` | A device ID cannot be used as a bus address |
192
+ | `CHECKSUM_MISMATCH` | A wire frame's checksum (Modbus RTU CRC) is wrong |
193
+ | `UNSUPPORTED_FEATURE` | Valid input that PyfieldBridge doesn't support yet (e.g. CAN error frames) |
194
+ | `INTERNAL` | Error raised by a custom adapter (`try_*` API only) |
195
+
196
+ ### Custom adapters
197
+
198
+ The built-in `adapters` registry is frozen. To add or replace adapters, create
199
+ your own converter; nothing global is modified.
200
+
201
+ ```python
202
+ from fieldbridge import create_adapter, create_converter
203
+
204
+ converter = create_converter({
205
+ "adapters": {
206
+ "ethercat": create_adapter({
207
+ "protocol": "ethercat",
208
+ "max_payload": 1486,
209
+ "fields": ["address"],
210
+ "uses_device_address": True,
211
+ "required_context": ["profile"],
212
+ "validate": lambda frame: None, # raise FieldbridgeError on violations
213
+ }),
214
+ },
215
+ })
216
+
217
+ converter.convert_message(request)
218
+ converter.try_convert_frame(request)
219
+ ```
220
+
221
+ ## Wire formats
222
+
223
+ The main API works on parsed frames. The `fieldbridge.wire` module turns raw
224
+ bytes into those frames and back. Parsing checks lengths, checksums, header
225
+ fields, and the protocol rules enforced by the adapters.
226
+
227
+ ```python
228
+ from fieldbridge import convert_frame
229
+ from fieldbridge.wire import parse, serialize
230
+
231
+ # Modbus RTU -> Modbus TCP gateway
232
+ [rtu] = parse("modbus-rtu", rtu_bytes) # CRC is verified
233
+ result = convert_frame({
234
+ "source": rtu,
235
+ "target_protocol": "modbus-tcp",
236
+ "options": {"metadata": {"transactionId": 1}},
237
+ })
238
+ socket.send(serialize(result.frame)) # MBAP header is built for you
239
+ ```
240
+
241
+ `parse(protocol, bytes)` always returns a **list** of frames, because one
242
+ Modbus TCP buffer can hold several ADUs and one EtherCAT frame several
243
+ datagrams. `serialize(frame)` returns `bytes`.
244
+
245
+ | Protocol | Functions | Format | Frame mapping |
246
+ | --- | --- | --- | --- |
247
+ | Modbus TCP | `parse_modbus_tcp`, `parse_modbus_tcp_stream`, `serialize_modbus_tcp` | MBAP header + PDU | `address` = unit id, `payload` = PDU, `metadata.transactionId` |
248
+ | Modbus RTU | `parse_modbus_rtu`, `serialize_modbus_rtu`, `crc16_modbus` | address + PDU + CRC-16 | `address` = slave, `payload` = PDU |
249
+ | CAN / CAN FD | `parse_socket_can`, `serialize_socket_can` | Linux `struct can_frame` (16 B) / `canfd_frame` (72 B) | `identifier`, `metadata.extended / fd / brs / esi / rtr` |
250
+ | CAN / CAN FD | `parse_candump_line`, `format_candump_line` | can-utils `candump` text: log format (`123#DEADBEEF`) and default display format (`can0 123 [4] DE AD BE EF`) | as above, plus `metadata.timestamp / interface` |
251
+ | Ethernet | `parse_ethernet`, `serialize_ethernet` | Ethernet II / 802.3, optional 802.1Q | MACs in `destination_address` / `source_address`, `identifier` = EtherType, `metadata.vlanId / vlanPriority` |
252
+ | PROFINET RT | `parse_profinet`, `serialize_profinet` | EtherType 0x8892 | `identifier` = FrameID, `payload` = IO data + APDU status |
253
+ | EtherCAT | `parse_ethercat`, `serialize_ethercat` | EtherType 0x88A4, one frame per datagram | `metadata.command / index / offset / workingCounter`; `address` for configured (FPxx) commands, `metadata.position` for auto-increment, `metadata.adp` for broadcast, `metadata.logicalAddress` for logical commands |
254
+
255
+ `parse_ethernet_auto(bytes)` detects PROFINET and EtherCAT from the EtherType.
256
+ For a TCP connection, feed each chunk to `parse_modbus_tcp_stream`, which
257
+ returns the complete ADUs plus a `rest` to prepend to the next chunk.
258
+
259
+ Notes:
260
+
261
+ - Ethernet frames are read and written **without the FCS**, which is how most
262
+ capture tools deliver them. Serialized frames are zero-padded to 60 bytes.
263
+ - Modbus RTU parsing expects **one complete frame**. Splitting a raw serial
264
+ stream (by inter-frame silence) is up to your transport layer.
265
+ - LIN, FlexRay and PROFIBUS wire formats depend on the interface hardware and
266
+ are not implemented yet. For those protocols `parse` / `serialize` raise
267
+ `UNSUPPORTED_FEATURE`. `wire_protocols` lists the supported ones.
268
+
269
+ ## Conversion rules
270
+
271
+ ### Device IDs and bus addresses
272
+
273
+ Some buses address *devices* (Modbus slave/unit id, PROFIBUS station,
274
+ EtherCAT station). Others only address *messages* (CAN, LIN, FlexRay
275
+ identifiers). `device_id` connects the two:
276
+
277
+ - **To a device-addressed bus**, the target `address` is the first available
278
+ of:
279
+ 1. the `address` in the message
280
+ 2. `target.device_id` (or `options.target_device_id`)
281
+ 3. the source `device_id`
282
+
283
+ A numeric `device_id`, or a string like `"17"` or `"0x11"`, is used as the
284
+ address. A name such as `"gateway-1"` is rejected with `INVALID_DEVICE_ID`
285
+ instead of being guessed. The result is then checked against the target's
286
+ address range.
287
+ - **From a device-addressed bus**, the source `address` becomes the result's
288
+ `device_id` when none was given.
289
+
290
+ ### Conflicts: the message wins
291
+
292
+ When the message and the surrounding request disagree, **the value in the
293
+ message is used**, and a warning is added:
294
+
295
+ | Conflict | Used |
296
+ | --- | --- |
297
+ | `message.address` vs `target.device_id` | `message.address` |
298
+ | `message.device_id` vs `source.device_id` | `message.device_id` |
299
+ | `message.metadata` vs `source.context` | `message.metadata` |
300
+
301
+ `target.context` and `options.metadata` are instructions for the target, not
302
+ facts about the source, so they set the target frame's metadata. Any message
303
+ value they override is also reported in the warnings.
304
+
305
+ ### Envelope fields
306
+
307
+ Each protocol declares which envelope fields it uses. A field the target
308
+ protocol doesn't use is dropped from the result and listed in the warnings.
309
+ For example, a CAN `identifier` has no meaning on Modbus.
310
+
311
+ | Protocol | Fields used | Payload limit | Other rules |
312
+ | --- | --- | --- | --- |
313
+ | Ethernet | `source_address`, `destination_address` (MAC), `identifier` (EtherType) | 1500 bytes | MAC ≤ 48 bits, EtherType ≤ 0xFFFF |
314
+ | PROFINET | `source_address`, `destination_address` (MAC), `identifier` (FrameID) | 1498 bytes (cyclic IO data 40..1440 + status) | FrameID ≤ 0xFFFF |
315
+ | PROFIBUS DP / PA | `address` (destination station), `source_address` | 244 bytes | station addresses 0..127 |
316
+ | Modbus RTU | `address` | 253-byte PDU | address 0..247 required, valid function code |
317
+ | Modbus TCP | `address` (unit id) | 253-byte PDU | unit id 0..255 required, valid function code |
318
+ | CAN | `identifier` | 8 bytes classic; CAN FD DLC lengths up to 64 | identifier required; `metadata.extended` / `metadata.fd` flags, inferred when unambiguous |
319
+ | LIN | `identifier` | 1..8 bytes | 6-bit identifier required |
320
+ | FlexRay | `identifier` (slot) | 0..254 bytes, even length | slot 1..2047 required |
321
+ | EtherCAT | `address` (station) | 1486 bytes | station address ≤ 0xFFFF |
322
+
323
+ For Modbus, `payload` is the PDU (function code followed by data). The address
324
+ byte, MBAP header and CRC are not part of it.
325
+
326
+ ## Development
327
+
328
+ ```bash
329
+ python -m pip install -e . pytest
330
+ python -m pytest
331
+ ```
332
+
333
+ Build the documentation site locally:
334
+
335
+ ```bash
336
+ python -m pip install -e . "pdoc>=15"
337
+ pdoc -o site fieldbridge fieldbridge.wire pyfieldbridge pyfieldbridge.wire
338
+ ```
339
+
340
+ Project layout:
341
+
342
+ ```
343
+ fieldbridge/
344
+ __init__.py public API
345
+ core/ types, errors, validation, conversion engine, result helpers
346
+ adapters/ one adapter per protocol + shared rule helpers
347
+ wire/ byte-level parse/serialize
348
+ index.py generic parse()/serialize() + re-exports
349
+ bytes.py shared byte helpers
350
+ modbus.py Modbus TCP / RTU
351
+ can.py SocketCAN binary, candump text
352
+ ethernet.py Ethernet, PROFINET RT, EtherCAT
353
+ pyfieldbridge/ compatibility package name
354
+ tests/ conversion, wire and differential tests
355
+ ```
356
+
357
+ ## License
358
+
359
+ [MIT](LICENSE)