blob: d28d889f881d651f7796c57abdb2a71e50e1f626 [file] [log] [blame]
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2000-2017 Ericsson Telecom AB
//
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
///////////////////////////////////////////////////////////////////////////////
// File: EPTF_LwM2M_Object_Functions.ttcn
// Description:
// Rev: R1A
// Prodnr: CNL 113 859
// Updated: 2017-09-01
// Contact: http://ttcn.ericsson.se
///////////////////////////////////////////////////////////////////////////////
module EPTF_LwM2M_Object_Functions {
import from LightweightM2M_Types all;
import from EPTF_LwM2M_Object_Definitions all;
import from EPTF_CLL_HashMapInt2Int_Functions all;
import from EPTF_CLL_HashMapStr2Int_Functions all;
function f_LwM2M_ObjectSpecificationDB_init(inout LwM2M_ObjectSpecificationDB p_db, in integer p_id)
{
p_db := c_LwM2M_ObjectSpecificationDB_init;
p_db.id := p_id;
p_db.hashRef := f_EPTF_int2int_HashMap_New("EPTF_LwM2M_ObjectSpecificationDB_"&int2str(p_id)&"_Hash");
}
function f_LwM2M_ObjectSpecificationDB_cleanUp(inout LwM2M_ObjectSpecificationDB p_db)
{
f_EPTF_int2int_HashMap_Delete("EPTF_LwM2M_ObjectSpecificationDB_"&int2str(p_db.id)&"_Hash");
p_db := c_LwM2M_ObjectSpecificationDB_init;
}
function f_LwM2M_ObjectSpecificationDB_add(inout LwM2M_ObjectSpecificationDB p_db, LwM2M_ObjectSpecification p_spec)
return integer
{
var integer vl_idx := -1;
if (not f_EPTF_int2int_HashMap_Find(p_db.hashRef, p_spec.id, vl_idx))
{
vl_idx := sizeof(p_db.specs);
p_db.specs[vl_idx] := p_spec;
f_EPTF_int2int_HashMap_Insert(p_db.hashRef, p_spec.id, vl_idx);
}
return vl_idx;
}
function f_LwM2M_ObjectSpecificationDB_lookUp(in LwM2M_ObjectSpecificationDB p_db, in integer p_objId)
return integer
{
var integer vl_idx := -1;
if (f_EPTF_int2int_HashMap_Find(p_db.hashRef, p_objId, vl_idx)) {
return vl_idx;
}
return -1;
}
function f_LwM2M_ObjectSpecificationDB_fillInOmaSpecs(inout LwM2M_ObjectSpecificationDB p_db)
{
f_LwM2M_ObjectSpecificationDB_add(p_db, c_LwM2M_Security_spec);
f_LwM2M_ObjectSpecificationDB_add(p_db, c_LwM2M_Server_spec);
f_LwM2M_ObjectSpecificationDB_add(p_db, c_LwM2M_Device_spec);
f_LwM2M_ObjectSpecificationDB_add(p_db, c_LwM2M_Location_spec);
f_LwM2M_ObjectSpecificationDB_add(p_db, c_LwM2M_IPSO_Temperature_spec);
}
function f_LwM2M_ObjectDB_init(inout LwM2M_ObjectDB p_db, in integer p_id)
{
p_db := c_LwM2M_ObjectDB_init;
p_db.id := p_id;
p_db.objHashRef := f_EPTF_int2int_HashMap_New("EPTF_LwM2M_ObjectDB_"&int2str(p_db.id)&"_objHash");
p_db.resHashRef := f_EPTF_str2int_HashMap_New("EPTF_LwM2M_ObjectDB_"&int2str(p_db.id)&"_resHash");
}
function f_LwM2M_ObjectDB_cleanUp(inout LwM2M_ObjectDB p_db)
{
f_EPTF_int2int_HashMap_Delete("EPTF_LwM2M_ObjectDB_"&int2str(p_db.id)&"_objHash");
f_EPTF_str2int_HashMap_Delete("EPTF_LwM2M_ObjectDB_"&int2str(p_db.id)&"_resHash");
p_db := c_LwM2M_ObjectDB_init;
}
function f_LwM2M_ObjectDB_createObject(inout LwM2M_ObjectDB p_db, in LwM2M_ObjectSpecification p_spec)
return integer
{
var integer vl_idx := -1;
if (not f_EPTF_int2int_HashMap_Find(p_db.objHashRef, p_spec.id, vl_idx))
{
vl_idx := sizeof(p_db.objs);
f_EPTF_int2int_HashMap_Insert(p_db.objHashRef, p_spec.id, vl_idx);
p_db.objs[vl_idx] := c_LwM2M_Object_init;
p_db.objs[vl_idx].id := p_spec.id;
}
return vl_idx;
}
function f_LwM2M_ObjectDB_getObject(in LwM2M_ObjectDB p_db, in integer p_id, inout LwM2M_Object p_obj)
return boolean
{
var integer vl_idx := f_LwM2M_ObjectDB_getObjectIdx(p_db, p_id);
if (vl_idx >= 0)
{
p_obj := p_db.objs[vl_idx];
return true;
}
return false;
}
function f_LwM2M_ObjectDB_getObjectIdx(in LwM2M_ObjectDB p_db, in integer p_id)
return integer
{
var integer vl_idx := -1;
if (f_EPTF_int2int_HashMap_Find(p_db.objHashRef, p_id, vl_idx))
{
return vl_idx;
}
return -1;
}
function f_LwM2M_ObjectDB_createObjectInstance(inout LwM2M_ObjectDB p_db, in LwM2M_ObjectSpecificationDB p_spec_db, in integer p_id, in boolean p_createResources := true)
return integer
{
var integer vl_objIdx := -1
var integer vl_objInstIdx := -1;
var integer vl_objSpecIdx := -1;
vl_objIdx := f_LwM2M_ObjectDB_getObjectIdx(p_db, p_id);
if (vl_objIdx >= 0)
{
vl_objSpecIdx := f_LwM2M_ObjectSpecificationDB_lookUp(p_spec_db, p_id);
if (vl_objSpecIdx >= 0)
{
vl_objInstIdx := sizeof(p_db.objs[vl_objIdx].instances);
p_db.objs[vl_objIdx].instances[vl_objInstIdx] := c_LwM2M_ObjectInstance_init;
p_db.objs[vl_objIdx].instances[vl_objInstIdx].id := vl_objInstIdx;
p_db.objs[vl_objIdx].instances[vl_objInstIdx].objId := p_spec_db.specs[vl_objSpecIdx].id;
// Creating resources according to spec
if (p_createResources) {
for (var integer i:=0; i<sizeof(p_spec_db.specs[vl_objSpecIdx].resourcedefs); i:=i+1)
{
var LwM2M_Resource v_res := f_LwM2M_createResource(p_db.objs[vl_objIdx].instances[vl_objInstIdx], p_spec_db.specs[vl_objSpecIdx].resourcedefs[i]);
f_LwM2M_ObjectDB_addResource(p_db, v_res);
}
}
}
}
return vl_objInstIdx;
}
function f_LwM2M_ObjectDB_addResource(inout LwM2M_ObjectDB p_db, in LwM2M_Resource p_resource)
return boolean
{
var integer vl_resIdx;
if (not f_EPTF_str2int_HashMap_Find(p_db.resHashRef, f_LwM2M_resourceHash_forResource(p_resource), vl_resIdx))
{
var integer vl_objIdx := f_LwM2M_ObjectDB_getObjectIdx(p_db, p_resource.objId);
if (vl_objIdx >= 0)
{
// Adding to resource array
vl_resIdx := sizeof(p_db.resources);
p_db.resources[vl_resIdx] := p_resource;
// Linking it under the object instance
var integer vl_resIdxInObjInst := sizeof(p_db.objs[vl_objIdx].instances[p_resource.objInstId].resources);
p_db.objs[vl_objIdx].instances[p_resource.objInstId].resources[vl_resIdxInObjInst] := vl_resIdx;
// Adding it to the hash
f_EPTF_str2int_HashMap_Insert(p_db.resHashRef, f_LwM2M_resourceHash_forResource(p_resource), vl_resIdx);
return true;
}
}
return false;
}
function f_LwM2M_ObjectDB_getObjectInstance(in LwM2M_ObjectDB p_db,
in integer p_objId, in integer p_objInstId, inout LwM2M_ObjectInstance p_objInst)
return boolean
{
var integer vl_objIdx := f_LwM2M_ObjectDB_getObjectIdx(p_db, p_objId);
if (vl_objIdx >= 0) {
if (sizeof(p_db.objs[vl_objIdx].instances) > p_objInstId and p_objInstId >= 0) {
p_objInst := p_db.objs[vl_objIdx].instances[p_objInstId];
return true;
}
}
return false;
}
function f_LwM2M_ObjectDB_getResource(in LwM2M_ObjectDB p_db,
in integer p_objId, in integer p_objInstId, in integer p_resourceId, inout LwM2M_Resource p_resource)
return boolean
{
var integer vl_resIdx;
if (f_EPTF_str2int_HashMap_Find(p_db.resHashRef, f_LwM2M_resourceHash_forIds(p_objId, p_objInstId, p_resourceId), vl_resIdx))
{
p_resource := p_db.resources[vl_resIdx];
return true;
}
return false;
}
function f_LwM2M_ObjectDB_setResource(inout LwM2M_ObjectDB p_db,
in integer p_objId, in integer p_objInstId, in integer p_resourceId, in LwM2M_Resource p_resource)
return boolean
{
var integer vl_resIdx;
if (f_EPTF_str2int_HashMap_Find(p_db.resHashRef, f_LwM2M_resourceHash_forIds(p_objId, p_objInstId, p_resourceId), vl_resIdx))
{
p_db.resources[vl_resIdx] := p_resource;
return true;
}
return false;
}
function f_LwM2M_ObjectDB_getResourceValue(in LwM2M_ObjectDB p_db,
in integer p_objId, in integer p_objInstId, in integer p_resourceId, inout LwM2M_ResourceValue p_value)
return boolean
{
var integer vl_resIdx;
if (f_EPTF_str2int_HashMap_Find(p_db.resHashRef, f_LwM2M_resourceHash_forIds(p_objId, p_objInstId, p_resourceId), vl_resIdx))
{
p_value := p_db.resources[vl_resIdx].val;
return true;
}
return false;
}
function f_LwM2M_ObjectDB_setResourceValue(inout LwM2M_ObjectDB p_db,
in integer p_objId, in integer p_objInstId, in integer p_resourceId, in LwM2M_ResourceValue p_value)
return boolean
{
var integer vl_resIdx;
if (f_EPTF_str2int_HashMap_Find(p_db.resHashRef, f_LwM2M_resourceHash_forIds(p_objId, p_objInstId, p_resourceId), vl_resIdx))
{
p_db.resources[vl_resIdx].val := p_value;
return true;
}
return false;
}
function f_LwM2M_ObjectDB_getObjectPaths(in LwM2M_ObjectDB p_db, inout ObjectPath_List p_paths)
{
if (p_db.id != -1) {
for (var integer i:=0; i<sizeof(p_db.objs); i:=i+1) {
// No instances for object
if (sizeof(p_db.objs[i].instances)==0) {
p_paths[sizeof(p_paths)] := {
objectId := p_db.objs[i].id,
objectInstanceId := omit,
resourceId := omit
}
}
// There are object instances
else {
for (var integer j:=0; j<sizeof(p_db.objs[i].instances); j:=j+1) {
p_paths[sizeof(p_paths)] := {
objectId := p_db.objs[i].id,
objectInstanceId := p_db.objs[i].instances[j].id,
resourceId := omit
}
}
}
}
}
}
function f_LwM2M_resourceHash_forResource(in LwM2M_Resource p_res)
return charstring
{
return int2str(p_res.objId) & "/" & int2str(p_res.objInstId) & "/" & int2str(p_res.id);
}
function f_LwM2M_resourceHash_forIds(in integer p_objId, in integer p_objInstId, in integer p_resId)
return charstring
{
return int2str(p_objId) & "/" & int2str(p_objInstId) & "/" & int2str(p_resId);
}
function f_LwM2M_createResource(inout LwM2M_ObjectInstance p_oi, in LwM2M_ResourceSpecification p_spec)
return LwM2M_Resource
{
var LwM2M_Resource v_ret := c_LwM2M_Resource_init;
v_ret.id := p_spec.id;
v_ret.objId := p_oi.objId;
v_ret.objInstId := p_oi.id;
if (p_spec.type_ == FLOAT) {
v_ret.val.floatValue := 0.0;
}
else if (p_spec.type_ == INTEGER) {
v_ret.val.intValue := 0;
}
else if (p_spec.type_ == STRING) {
v_ret.val.strValue := "";
}
else if (p_spec.type_ == OPAQUE) {
v_ret.val.opaqueValue := ''O;
}
else if (p_spec.type_ == BOOLEAN) {
v_ret.val.boolValue := false;
}
return v_ret;
}
function f_LwM2M_Resource_setNextDataSample(inout LwM2M_Resource p_res, inout LwM2M_DataSamples_DB p_db)
{
if (ispresent(p_res.dataSample)) {
// Init samplesPointer
if (p_res.dataSample.samplesPointer == -1) {
p_res.dataSample.samplesPointer := f_LwM2M_DataSamples_DB_lookUp(p_db, p_res.dataSample.samplesName);
}
// We found the dataSamples
if (p_res.dataSample.samplesPointer >=0 and p_res.dataSample.samplesPointer < sizeof(p_db.data)) {
// We have values
if (sizeof(p_db.data[p_res.dataSample.samplesPointer].values)>0) {
// Check value pointer boundaries
if (p_res.dataSample.valuePointer < 0 or
p_res.dataSample.valuePointer >= sizeof(p_db.data[p_res.dataSample.samplesPointer].values))
{
// Outside of boundaries, set it to zero
p_res.dataSample.valuePointer := 0;
}
// Set the value
p_res.val := p_db.data[p_res.dataSample.samplesPointer].values[p_res.dataSample.valuePointer];
// Step value pointer
p_res.dataSample.valuePointer := p_res.dataSample.valuePointer+1;
}
}
}
}
function f_LwM2M_DataSamples_DB_init(inout LwM2M_DataSamples_DB p_db, in charstring p_name)
{
p_db.name := p_name;
p_db.data := {};
p_db.hashRef := f_EPTF_str2int_HashMap_New(p_name);
}
function f_LwM2M_DataSamples_DB_add(inout LwM2M_DataSamples_DB p_db, in LwM2M_DataSamples p_samples)
return integer
{
var integer p_idx := f_LwM2M_DataSamples_DB_lookUp(p_db, p_samples.name);
if (p_idx == -1)
{
p_idx := sizeof(p_db.data);
f_EPTF_str2int_HashMap_Insert(p_db.hashRef, p_samples.name, p_idx);
p_db.data[p_idx] := p_samples;
}
return p_idx;
}
function f_LwM2M_DataSamples_DB_lookUp(inout LwM2M_DataSamples_DB p_db, in charstring p_sampleName)
return integer
{
var integer vl_idx := -1;
f_EPTF_str2int_HashMap_Find(p_db.hashRef, p_sampleName, vl_idx);
return vl_idx;
}
function f_LwM2M_DataSamples_DB_get(inout LwM2M_DataSamples_DB p_db, in integer p_idx, inout LwM2M_DataSamples p_samples)
{
if (p_idx < sizeof(p_db.data))
{
p_samples := p_db.data[p_idx];
}
}
function f_LwM2M_DataSamples_DB_cleanUp(inout LwM2M_DataSamples_DB p_db)
{
p_db.data := {};
f_EPTF_str2int_HashMap_Delete(p_db.name);
}
}