1 /*
2  * Copyright (c) 2019 Foundries.io
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #define LOG_MODULE_NAME net_lwm2m_obj_location
8 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
9 
10 #include <logging/log.h>
11 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
12 
13 #include <stdint.h>
14 #include <init.h>
15 
16 #include "lwm2m_object.h"
17 #include "lwm2m_engine.h"
18 
19 #define LOCATION_VERSION_MAJOR 1
20 #define LOCATION_VERSION_MINOR 0
21 
22 /* resource IDs */
23 #define LOCATION_LATITUDE_ID			0
24 #define LOCATION_LONGITUDE_ID			1
25 #define LOCATION_ALTITUDE_ID			2
26 #define LOCATION_RADIUS_ID			3
27 #define LOCATION_VELOCITY_ID			4
28 #define LOCATION_TIMESTAMP_ID			5
29 #define LOCATION_SPEED_ID			6
30 
31 #define LOCATION_MAX_ID				7
32 
33 /*
34  * Calculate resource instances as follows:
35  * start with LOCATION_MAX_ID
36  */
37 #define RESOURCE_INSTANCE_COUNT	(LOCATION_MAX_ID)
38 
39 /* resource state */
40 static float32_value_t latitude;
41 static float32_value_t longitude;
42 static float32_value_t altitude;
43 static float32_value_t radius;
44 static float32_value_t speed;
45 static int32_t timestamp;
46 
47 static struct lwm2m_engine_obj location;
48 static struct lwm2m_engine_obj_field fields[] = {
49 	OBJ_FIELD_DATA(LOCATION_LATITUDE_ID, R, FLOAT),
50 	OBJ_FIELD_DATA(LOCATION_LONGITUDE_ID, R, FLOAT),
51 	OBJ_FIELD_DATA(LOCATION_ALTITUDE_ID, R_OPT, FLOAT),
52 	OBJ_FIELD_DATA(LOCATION_RADIUS_ID, R_OPT, FLOAT),
53 	OBJ_FIELD_DATA(LOCATION_VELOCITY_ID, R_OPT, OPAQUE),
54 	OBJ_FIELD_DATA(LOCATION_TIMESTAMP_ID, R, TIME),
55 	OBJ_FIELD_DATA(LOCATION_SPEED_ID, R_OPT, FLOAT),
56 };
57 
58 static struct lwm2m_engine_obj_inst inst;
59 static struct lwm2m_engine_res res[LOCATION_MAX_ID];
60 static struct lwm2m_engine_res_inst res_inst[RESOURCE_INSTANCE_COUNT];
61 
location_create(uint16_t obj_inst_id)62 static struct lwm2m_engine_obj_inst *location_create(uint16_t obj_inst_id)
63 {
64 	int i = 0, j = 0;
65 
66 	if (inst.resource_count) {
67 		LOG_ERR("Only 1 instance of Location object can exist.");
68 		return NULL;
69 	}
70 
71 	init_res_instance(res_inst, ARRAY_SIZE(res_inst));
72 
73 	/* initialize instance resource data */
74 	INIT_OBJ_RES_DATA(LOCATION_LATITUDE_ID, res, i, res_inst, j,
75 			  &latitude, sizeof(latitude));
76 	INIT_OBJ_RES_DATA(LOCATION_LONGITUDE_ID, res, i, res_inst, j,
77 			  &longitude, sizeof(longitude));
78 	INIT_OBJ_RES_DATA(LOCATION_ALTITUDE_ID, res, i, res_inst, j,
79 			  &altitude, sizeof(altitude));
80 	INIT_OBJ_RES_DATA(LOCATION_RADIUS_ID, res, i, res_inst, j,
81 			  &radius, sizeof(radius));
82 	INIT_OBJ_RES_OPTDATA(LOCATION_VELOCITY_ID, res, i, res_inst, j);
83 	INIT_OBJ_RES_DATA(LOCATION_TIMESTAMP_ID, res, i, res_inst, j,
84 			  &timestamp, sizeof(timestamp));
85 	INIT_OBJ_RES_DATA(LOCATION_SPEED_ID, res, i, res_inst, j,
86 			  &speed, sizeof(speed));
87 
88 	inst.resources = res;
89 	inst.resource_count = i;
90 
91 	LOG_DBG("Create Location instance: %d", obj_inst_id);
92 
93 	return &inst;
94 }
95 
ipso_location_init(const struct device * dev)96 static int ipso_location_init(const struct device *dev)
97 {
98 	int ret;
99 	struct lwm2m_engine_obj_inst *obj_inst = NULL;
100 
101 	location.obj_id = LWM2M_OBJECT_LOCATION_ID;
102 	location.version_major = LOCATION_VERSION_MAJOR;
103 	location.version_minor = LOCATION_VERSION_MINOR;
104 	location.is_core = true;
105 	location.fields = fields;
106 	location.field_count = ARRAY_SIZE(fields);
107 	location.max_instance_count = 1U;
108 	location.create_cb = location_create;
109 	lwm2m_register_obj(&location);
110 
111 	/* auto create the only instance */
112 	ret = lwm2m_create_obj_inst(LWM2M_OBJECT_LOCATION_ID, 0, &obj_inst);
113 	if (ret < 0) {
114 		LOG_DBG("Create LWM2M instance 0 error: %d", ret);
115 	}
116 
117 	return ret;
118 }
119 
120 SYS_INIT(ipso_location_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
121