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