1 /*
2 * Copyright (c) 2022 FTP Technologies
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*
8 * Source material for uCIFI LPWAN object (3412):
9 * https://raw.githubusercontent.com/OpenMobileAlliance/lwm2m-registry/prod/3412.xml
10 */
11
12 #define LOG_MODULE_NAME net_ucifi_lpwan
13 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
14
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
17
18 #include <stdio.h>
19 #include <string.h>
20
21 #include <zephyr/init.h>
22
23 #include "lwm2m_object.h"
24 #include "lwm2m_engine.h"
25 #include "ucifi_lpwan.h"
26
27 #define LPWAN_VERSION_MAJOR 1
28 #define LPWAN_VERSION_MINOR 0
29
30 /* clang-format off */
31 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_UCIFI_LPWAN_INSTANCE_COUNT
32 #define IPV4_ADDRESS_MAX CONFIG_LWM2M_UCIFI_LPWAN_IPV4_ADDRESS_MAX
33 #define IPV6_ADDRESS_MAX CONFIG_LWM2M_UCIFI_LPWAN_IPV6_ADDRESS_MAX
34 #define NETWORK_ADDRESS_MAX CONFIG_LWM2M_UCIFI_LPWAN_NETWORK_ADDRESS_MAX
35 #define SECONDARY_NETWORK_ADDRESS_MAX CONFIG_LWM2M_UCIFI_LPWAN_SECONDARY_NETWORK_ADDRESS_MAX
36 #define PEER_ADDRESS_MAX CONFIG_LWM2M_UCIFI_LPWAN_PEER_ADDRESS_MAX
37 #define MULTICAST_GRP_ADDRESS_MAX CONFIG_LWM2M_UCIFI_LPWAN_MULTICAST_GRP_ADDRESS_MAX
38 #define MULTICAST_GRP_KEY_MAX CONFIG_LWM2M_UCIFI_LPWAN_MULTICAST_GRP_KEY_MAX
39 /* clang-format on */
40
41 /*
42 * Calculate resource instances as follows:
43 * start with UCIFI_LPWAN_MAX_RID
44 * subtract EXEC resources (0)
45 * subtract MULTI resources because their counts include 0 resource (7)
46 * add UCIFI_LPWAN_IPV4_ADDRESS_RID resource instances
47 * add UCIFI_LPWAN_IPV6_ADDRESS_RID resource instances
48 * add UCIFI_LPWAN_NETWORK_ADDRESS_RID resource instances
49 * add UCIFI_LPWAN_SECONDARY_ADDRESS_RID resource instances
50 * add UCIFI_LPWAN_PEER_ADDRESS_RID resource instances
51 * add UCIFI_LPWAN_MULTICAST_GRP_ADDRESS_RID resource instances
52 * add UCIFI_LPWAN_MULTICAST_GRP_KEY_RID resource instances
53 */
54 #define NUMBER_EXEC_RESOURCES 0
55 #define NUMBER_MULTI_RESOURCES 7
56 #define RESOURCE_INSTANCE_COUNT \
57 (UCIFI_LPWAN_MAX_RID - NUMBER_EXEC_RESOURCES - NUMBER_MULTI_RESOURCES + IPV4_ADDRESS_MAX + \
58 IPV6_ADDRESS_MAX + NETWORK_ADDRESS_MAX + SECONDARY_NETWORK_ADDRESS_MAX + \
59 PEER_ADDRESS_MAX + MULTICAST_GRP_ADDRESS_MAX + MULTICAST_GRP_KEY_MAX)
60
61 static struct lwm2m_engine_obj lpwan;
62 static struct lwm2m_engine_obj_field fields[] = {
63 OBJ_FIELD_DATA(UCIFI_LPWAN_NETWORK_TYPE_RID, R_OPT, STRING),
64 OBJ_FIELD_DATA(UCIFI_LPWAN_IPV4_ADDRESS_RID, RW_OPT, STRING),
65 OBJ_FIELD_DATA(UCIFI_LPWAN_IPV6_ADDRESS_RID, RW_OPT, STRING),
66 OBJ_FIELD_DATA(UCIFI_LPWAN_NETWORK_ADDRESS_RID, RW_OPT, STRING),
67 OBJ_FIELD_DATA(UCIFI_LPWAN_SECONDARY_ADDRESS_RID, RW_OPT, STRING),
68 OBJ_FIELD_DATA(UCIFI_LPWAN_MAC_ADDRESS_RID, RW, STRING),
69 OBJ_FIELD_DATA(UCIFI_LPWAN_PEER_ADDRESS_RID, R_OPT, STRING),
70 OBJ_FIELD_DATA(UCIFI_LPWAN_MULTICAST_GRP_ADDRESS_RID, RW_OPT, STRING),
71 OBJ_FIELD_DATA(UCIFI_LPWAN_MULTICAST_GRP_KEY_RID, RW_OPT, STRING),
72 OBJ_FIELD_DATA(UCIFI_LPWAN_DATA_RATE_RID, RW_OPT, INT),
73 OBJ_FIELD_DATA(UCIFI_LPWAN_TRANSMIT_POWER_RID, R_OPT, FLOAT),
74 OBJ_FIELD_DATA(UCIFI_LPWAN_FREQUENCY_RID, RW_OPT, FLOAT),
75 OBJ_FIELD_DATA(UCIFI_LPWAN_SESSION_TIME_RID, RW_OPT, TIME),
76 OBJ_FIELD_DATA(UCIFI_LPWAN_SESSION_DURATION_RID, R_OPT, TIME),
77 OBJ_FIELD_DATA(UCIFI_LPWAN_MESH_NODE_RID, RW_OPT, BOOL),
78 OBJ_FIELD_DATA(UCIFI_LPWAN_MAX_REPEAT_TIME_RID, RW_OPT, INT),
79 OBJ_FIELD_DATA(UCIFI_LPWAN_NUMBER_REPEATS_RID, R_OPT, INT),
80 OBJ_FIELD_DATA(UCIFI_LPWAN_SIGNAL_NOISE_RATIO_RID, R_OPT, FLOAT),
81 OBJ_FIELD_DATA(UCIFI_LPWAN_COMM_FAILURE_RID, R_OPT, BOOL),
82 OBJ_FIELD_DATA(UCIFI_LPWAN_RSSI_RID, R_OPT, FLOAT),
83 OBJ_FIELD_DATA(UCIFI_LPWAN_IMSI_RID, R_OPT, STRING),
84 OBJ_FIELD_DATA(UCIFI_LPWAN_IMEI_RID, R_OPT, STRING),
85 OBJ_FIELD_DATA(UCIFI_LPWAN_COMM_OPERATOR_RID, R_OPT, STRING),
86 OBJ_FIELD_DATA(UCIFI_LPWAN_IC_CARD_IDENTIFIER_RID, R_OPT, STRING),
87 };
88
89 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
90 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][UCIFI_LPWAN_MAX_RID];
91 static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
92
93 /* resource state variables */
94 static char mac[MAX_INSTANCE_COUNT][MAC_ADDRESS_SIZE];
95
lpwan_create(uint16_t obj_inst_id)96 static struct lwm2m_engine_obj_inst *lpwan_create(uint16_t obj_inst_id)
97 {
98 int index, i = 0, j = 0;
99
100 /* Check that there is no other instance with this ID */
101 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
102 if ((inst[index].obj != NULL) && (inst[index].obj_inst_id == obj_inst_id)) {
103 LOG_ERR("Can not create instance - already existing: %u", obj_inst_id);
104 return NULL;
105 }
106 }
107
108 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
109 if (inst[index].obj == NULL) {
110 break;
111 }
112 }
113
114 if (index >= MAX_INSTANCE_COUNT) {
115 LOG_ERR("Can not create instance - no more room: %u", obj_inst_id);
116 return NULL;
117 }
118
119 /* Reset to uninitialised values */
120 (void)memset(res[index], 0, sizeof(res[index]));
121 mac[index][0] = '\0';
122
123 init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
124
125 /* initialize instance resource data */
126 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_NETWORK_TYPE_RID, res[index], i, res_inst[index], j);
127 INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_IPV4_ADDRESS_RID, res[index], i, res_inst[index], j,
128 IPV4_ADDRESS_MAX, false);
129 INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_IPV6_ADDRESS_RID, res[index], i, res_inst[index], j,
130 IPV6_ADDRESS_MAX, false);
131 INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_NETWORK_ADDRESS_RID, res[index], i, res_inst[index],
132 j, NETWORK_ADDRESS_MAX, false);
133 INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_SECONDARY_ADDRESS_RID, res[index], i,
134 res_inst[index], j, SECONDARY_NETWORK_ADDRESS_MAX, false);
135 INIT_OBJ_RES_DATA(UCIFI_LPWAN_MAC_ADDRESS_RID, res[index], i, res_inst[index], j,
136 mac[index], MAC_ADDRESS_SIZE);
137 INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_PEER_ADDRESS_RID, res[index], i, res_inst[index], j,
138 PEER_ADDRESS_MAX, false);
139 INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_MULTICAST_GRP_ADDRESS_RID, res[index], i,
140 res_inst[index], j, MULTICAST_GRP_ADDRESS_MAX, false);
141 INIT_OBJ_RES_MULTI_OPTDATA(UCIFI_LPWAN_MULTICAST_GRP_KEY_RID, res[index], i,
142 res_inst[index], j, MULTICAST_GRP_KEY_MAX, false);
143 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_DATA_RATE_RID, res[index], i, res_inst[index], j);
144 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_TRANSMIT_POWER_RID, res[index], i, res_inst[index], j);
145 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_FREQUENCY_RID, res[index], i, res_inst[index], j);
146 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_SESSION_TIME_RID, res[index], i, res_inst[index], j);
147 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_SESSION_DURATION_RID, res[index], i, res_inst[index], j);
148 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_MESH_NODE_RID, res[index], i, res_inst[index], j);
149 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_MAX_REPEAT_TIME_RID, res[index], i, res_inst[index], j);
150 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_NUMBER_REPEATS_RID, res[index], i, res_inst[index], j);
151 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_SIGNAL_NOISE_RATIO_RID, res[index], i, res_inst[index], j);
152 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_COMM_FAILURE_RID, res[index], i, res_inst[index], j);
153 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_RSSI_RID, res[index], i, res_inst[index], j);
154 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_IMSI_RID, res[index], i, res_inst[index], j);
155 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_IMEI_RID, res[index], i, res_inst[index], j);
156 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_COMM_OPERATOR_RID, res[index], i, res_inst[index], j);
157 INIT_OBJ_RES_OPTDATA(UCIFI_LPWAN_IC_CARD_IDENTIFIER_RID, res[index], i, res_inst[index], j);
158
159 inst[index].resources = res[index];
160 inst[index].resource_count = i;
161
162 LOG_DBG("Create access control instance: %d", obj_inst_id);
163 return &inst[index];
164 }
165
ucifi_lpwan_init(void)166 static int ucifi_lpwan_init(void)
167 {
168 lpwan.obj_id = LWM2M_UCIFI_LPWAN_ID;
169 lpwan.version_major = LPWAN_VERSION_MAJOR;
170 lpwan.version_minor = LPWAN_VERSION_MINOR;
171 lpwan.is_core = false;
172 lpwan.fields = fields;
173 lpwan.field_count = ARRAY_SIZE(fields);
174 lpwan.max_instance_count = MAX_INSTANCE_COUNT;
175 lpwan.create_cb = lpwan_create;
176 lwm2m_register_obj(&lpwan);
177
178 return 0;
179 }
180
181 SYS_INIT(ucifi_lpwan_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
182