Skip to content

Commit

Permalink
add session handling to ruby wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
cjhdev committed Mar 13, 2021
1 parent d45fcd3 commit e9db06e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
24 changes: 24 additions & 0 deletions wrappers/ruby/ext/ldl/ext_ldl/ext_mac.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ static VALUE get_join_nonce(VALUE self);
static VALUE get_next_dev_nonce(VALUE self);
static VALUE get_dev_addr(VALUE self);
static VALUE get_net_id(VALUE self);
static VALUE get_session(VALUE self);

static uint32_t system_ticks(void *app);
static uint32_t system_rand(void *app);
Expand Down Expand Up @@ -120,6 +121,7 @@ void ext_mac_init(void)
rb_define_method(cExtMAC, "join_nonce", get_join_nonce, 0);
rb_define_method(cExtMAC, "net_id", get_net_id, 0);
rb_define_method(cExtMAC, "dev_addr", get_dev_addr, 0);
rb_define_method(cExtMAC, "session", get_session, 0);

rb_define_method(cExtMAC, "entropy", entropy, 0);
rb_define_method(cExtMAC, "otaa", otaa, 0);
Expand Down Expand Up @@ -198,6 +200,7 @@ static VALUE initialize(int argc, VALUE *argv, VALUE self)
VALUE logger;
VALUE clock;
VALUE join_nonce;
VALUE session;

Data_Get_Struct(self, struct ldl_mac, mac);

Expand All @@ -209,6 +212,7 @@ static VALUE initialize(int argc, VALUE *argv, VALUE self)
join_eui = rb_hash_aref(options, ID2SYM(rb_intern("join_eui")));
dev_eui = rb_hash_aref(options, ID2SYM(rb_intern("dev_eui")));
region = rb_hash_aref(options, ID2SYM(rb_intern("region")));
session = rb_hash_aref(options, ID2SYM(rb_intern("session")));

otaa_dither = rb_hash_aref(options, ID2SYM(rb_intern("otaa_dither")));

Expand Down Expand Up @@ -267,6 +271,7 @@ static VALUE initialize(int argc, VALUE *argv, VALUE self)
rb_iv_set(self, "@name", name);
rb_iv_set(self, "@log_header", name);
rb_iv_set(self, "@region", region);
rb_iv_set(self, "@session", session);

arg.ticks = system_ticks;
arg.rand = system_rand;
Expand All @@ -280,6 +285,18 @@ static VALUE initialize(int argc, VALUE *argv, VALUE self)
arg.sm = (struct ldl_sm *)sm;
arg.sm_interface = &ext_sm_interface;

if(session != Qnil){

if(RSTRING_LEN(session) == sizeof(struct ldl_mac_session)){

arg.session = (const struct ldl_mac_session *)RSTRING_PTR(session);
}
else{

LDL_ERROR("session wrong size")
}
}

arg.joinEUI = RSTRING_PTR(join_eui);
arg.devEUI = RSTRING_PTR(dev_eui);
arg.devNonce = (uint32_t)NUM2UINT(dev_nonce);
Expand Down Expand Up @@ -469,6 +486,7 @@ static VALUE forget(VALUE self)

rb_iv_set(self, "@net_id", Qnil);
rb_iv_set(self, "@dev_addr", Qnil);
rb_iv_set(self, "@session", Qnil);

return self;
}
Expand Down Expand Up @@ -631,6 +649,7 @@ static void response(void *receiver, enum ldl_mac_response_type type, const unio
break;
case LDL_MAC_SESSION_UPDATED:
event = ID2SYM(rb_intern("session_updated"));
rb_iv_set(self, "@session", rb_str_new((const char *)arg->session_updated.session, sizeof(*arg->session_updated.session)));
break;
case LDL_MAC_DEVICE_TIME:
event = ID2SYM(rb_intern("device_time"));
Expand Down Expand Up @@ -816,3 +835,8 @@ static VALUE get_net_id(VALUE self)
{
return rb_iv_get(self, "@net_id");
}

static VALUE get_session(VALUE self)
{
return rb_iv_get(self, "@session");
}
6 changes: 5 additions & 1 deletion wrappers/ruby/lib/ldl/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class Device
:dev_addr,
:net_id,
:join_nonce,
:next_dev_nonce
:next_dev_nonce,
:session

def_delegators :@sm,
:nwk_key,
Expand All @@ -60,6 +61,9 @@ def initialize(broker, clock, **opts)
opts[:nwk_key] ||= SecureRandom.bytes(16)
opts[:app_key] ||= SecureRandom.bytes(16)

@clock = clock
@broker = broker

@radio = Radio.new(broker, clock, **opts)
@sm = SM.new(broker, clock, **opts)
@mac = MAC.new(broker, clock, sm, radio, **opts)
Expand Down
3 changes: 2 additions & 1 deletion wrappers/ruby/lib/ldl/mac.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def running?
:next_dev_nonce,
:join_nonce,
:net_id,
:dev_addr
:dev_addr,
:session

def initialize(broker, clock, sm, radio, **opts)

Expand Down

0 comments on commit e9db06e

Please sign in to comment.