Skip to content

Commit

Permalink
Refactor client service instance and add test for receiving events
Browse files Browse the repository at this point in the history
  • Loading branch information
chrizog committed Apr 25, 2024
1 parent 89e06bf commit 46bf66f
Show file tree
Hide file tree
Showing 15 changed files with 429 additions and 220 deletions.
48 changes: 0 additions & 48 deletions article.md

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import ipaddress
import logging

from someipy import ServiceBuilder, EventGroup, TransportLayerProtocol, SomeIpMessage
from someipy.service_discovery import construct_service_discovery
from someipy.client_service_instance import construct_client_service_instance
from someipy.logging import set_someipy_log_level
Expand All @@ -11,12 +12,15 @@
SD_PORT = 30490
INTERFACE_IP = "127.0.0.1"

SAMPLE_EVENTGROUP_ID = 20
SAMPLE_SERVICE_ID = 0x1234
SAMPLE_INSTANCE_ID = 0x5678
SAMPLE_EVENTGROUP_ID = 0x0321
SAMPLE_EVENT_ID = 0x0123


def temperature_callback(payload: bytes) -> None:
print(f"Received {len(payload)} bytes.")
temperature_msg = TemparatureMsg().deserialize(payload)
def temperature_callback(someip_message: SomeIpMessage) -> None:
print(f"Received {len(someip_message.payload)} bytes.")
temperature_msg = TemparatureMsg().deserialize(someip_message.payload)
print(temperature_msg)


Expand All @@ -37,14 +41,24 @@ async def main():
# and port to which the events are sent to and the client will listen to
# 3. The ServiceDiscoveryProtocol object has to be passed as well, so the ClientServiceInstance can offer his service to
# other ECUs
temperature_eventgroup = EventGroup(
id=SAMPLE_EVENTGROUP_ID, event_ids=[SAMPLE_EVENT_ID]
)
temperature_service = (
ServiceBuilder()
.with_service_id(SAMPLE_SERVICE_ID)
.with_major_version(1)
.with_eventgroup(temperature_eventgroup)
.build()
)

service_instance_temperature = await construct_client_service_instance(
service_id=1,
instance_id=1000,
major_version=1,
minor_version=0,
service=temperature_service,
instance_id=SAMPLE_INSTANCE_ID,
endpoint=(ipaddress.IPv4Address(INTERFACE_IP), 3002),
ttl=5,
sd_sender=service_discovery,
protocol=TransportLayerProtocol.UDP
)

# It's possible to optionally register a callback function which will be called when an event from the
Expand Down
6 changes: 2 additions & 4 deletions example_apps/send_events_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
import ipaddress
import logging

from someipy import TransportLayerProtocol
from someipy.service import ServiceBuilder, EventGroup
from someipy import TransportLayerProtocol, ServiceBuilder, EventGroup, construct_server_service_instance
from someipy.service_discovery import construct_service_discovery
from someipy.server_service_instance import construct_server_service_instance
from someipy.logging import set_someipy_log_level
from someipy.serialization import Uint8, Uint64, Float32
from temperature_msg import TemparatureMsg
Expand Down Expand Up @@ -83,7 +81,7 @@ async def main():
try:
# Either cyclically send events in an endless loop..
while True:
await asyncio.sleep(5)
await asyncio.sleep(1)
tmp_msg.timestamp = Uint64(tmp_msg.timestamp.value + 1)
payload = tmp_msg.serialize()
service_instance_temperature.send_event(
Expand Down
3 changes: 2 additions & 1 deletion src/someipy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from ._internal.someip_sd_header import TransportLayerProtocol
from .server_service_instance import ServerServiceInstance, construct_server_service_instance
from .service import Service, ServiceBuilder, EventGroup
from .service import Service, ServiceBuilder, EventGroup
from ._internal.someip_message import SomeIpMessage
Loading

0 comments on commit 46bf66f

Please sign in to comment.