#define LOG_MODULE_NAME net_lwm2m_obj_supl
#define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL

#include <zephyr/logging/log.h>
#include "minilog.h"
LOG_MODULE_REGISTER(LOG_MODULE_NAME);

#include <string.h>
#include <zephyr/init.h>

#include "lwm2m_object.h"
#include "lwm2m_engine.h"

#define OBJECT_ID 7276
#define OBJECT_VERSION_MAJOR 1
#define OBJECT_VERSION_MINOR 0

/* SUPL object resource IDs */
#define RESOURCE_DATA				0

#define RESOURCES_MAX_ID			1
#define RESOURCE_INSTANCE_COUNT	(RESOURCES_MAX_ID)

#define SUPL_BUFF_SIZE	1024

/* Storage variables to hold SUPL values. */
static char data[SUPL_BUFF_SIZE];

static struct lwm2m_engine_obj object;
static struct lwm2m_engine_obj_field fields[] = {
	OBJ_FIELD_DATA(RESOURCE_DATA, RW, OPAQUE)
};

static struct lwm2m_engine_obj_inst inst;
static struct lwm2m_engine_res res[RESOURCES_MAX_ID];
static struct lwm2m_engine_res_inst res_inst[RESOURCE_INSTANCE_COUNT];

static struct lwm2m_engine_obj_inst *object_create(uint16_t obj_inst_id)
{
	int i = 0, j = 0;

	init_res_instance(res_inst, ARRAY_SIZE(res_inst));

	/* Initialize object instance resource data */
	INIT_OBJ_RES_DATA(RESOURCE_DATA, res, i, res_inst, j,
			  data, sizeof(data));

	inst.resources = res;
	inst.resource_count = i;

	MLOG_DBG("Created a SUPL object: %d", obj_inst_id);
	return &inst;
}
static int object_init(const struct device *dev)
{
	struct lwm2m_engine_obj_inst *obj_inst = NULL;
	int ret = 0;

	object.obj_id = OBJECT_ID;
	object.version_major = OBJECT_VERSION_MAJOR;
	object.version_minor = OBJECT_VERSION_MINOR;
	object.is_core = false;
	object.fields = fields;
	object.field_count = ARRAY_SIZE(fields);
	object.max_instance_count = 1U;
	object.create_cb = object_create;
	lwm2m_register_obj(&object);

	ret = lwm2m_create_obj_inst(OBJECT_ID, 0, &obj_inst);
	if (ret < 0) {
		MLOG_ERR("Create SUPL object error: %d", ret);
		return ret;
	}

	return 0;
}

SYS_INIT(object_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
