Newbie
Newbie

Reputation: 9

Convert code from 'hard-coding' to 'soft-coding'

I am configuring the DCP module for PROFINET devices and using Lua for that. My hard-coded piece of code looks like:

function dcp:setname()
    local pkt = CreateFromPath("ethernet/profinet/dcp/dcp_block_nameofstation")
    pkt.src.from_string(tc.Mac)
    pkt.dst.from_string("00:a0:45:38:20:ec")
    pkt.get_layer("profinet").frameid.from_string("0xfefd")
    local d = pkt.get_layer("dcp")
    local xid = tostring(rand()) 
    d.service_id.from_string("4")
    d.xid.from_string(xid)

    d.service_type.from_string("0")

    d.get_layer("dcp_block_nameofstation").Option.from_string("2")
    d.get_layer("dcp_block_nameofstation").SubOption.from_string("2")

    d.get_layer("dcp_block_nameofstation").BlockInfo .from_string("1")
    d.get_layer("dcp_block_nameofstation").NameOfStation.from_string("test-device")
end

I need to pass the 'test-device' as an argument and it is a string. How do I do that?

Upvotes: 0

Views: 324

Answers (1)

Henrik Ilgen
Henrik Ilgen

Reputation: 1967

Add an argument to the function definition:

function dcp:setname(nameOfStation)

And use it:

d.get_layer("dcp_block_nameofstation").NameOfStation.from_string(nameOfStation)

Then, wenn calling the function, pass a value:

myDcp:setname("test-device")

Upvotes: 1

Related Questions