|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +#include <ot_rpc_ids.h> |
| 8 | +#include <ot_rpc_common.h> |
| 9 | + |
| 10 | +#include <nrf_rpc_cbor.h> |
| 11 | + |
| 12 | +#include <openthread/error.h> |
| 13 | +#include <openthread/dataset.h> |
| 14 | +#include <openthread/instance.h> |
| 15 | +#include <openthread/link.h> |
| 16 | +#include <openthread/thread.h> |
| 17 | +#include <zephyr/logging/log.h> |
| 18 | + |
| 19 | +NRF_RPC_GROUP_DECLARE(ot_group); |
| 20 | +LOG_MODULE_DECLARE(ot_rpc, LOG_LEVEL_DBG); |
| 21 | + |
| 22 | +static char version[256]; |
| 23 | +static otExtAddress mac = {.m8 = {0xF4, 0xCE, 0x36, 0x24, 0x95, 0x6A, 0xD1, 0xD0}}; |
| 24 | +static otExtendedPanId ext_pan_id; |
| 25 | +static char thread_network_name[OT_NETWORK_NAME_MAX_SIZE + 1]; |
| 26 | +static otMeshLocalPrefix mesh_prefix; |
| 27 | + |
| 28 | +static void get_uint_t(int id, void *result, size_t result_size) |
| 29 | +{ |
| 30 | + const size_t cbor_buffer_size = 0; |
| 31 | + struct nrf_rpc_cbor_ctx ctx; |
| 32 | + |
| 33 | + NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size); |
| 34 | + |
| 35 | + nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, id, &ctx); |
| 36 | + |
| 37 | + if (!zcbor_uint_decode(ctx.zs, result, result_size)) { |
| 38 | + nrf_rpc_cbor_decoding_done(&ot_group, &ctx); |
| 39 | + ot_rpc_report_decoding_error(id); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + nrf_rpc_cbor_decoding_done(&ot_group, &ctx); |
| 44 | +} |
| 45 | + |
| 46 | +static int get_string(int id, char *buffer, size_t buffer_size) |
| 47 | +{ |
| 48 | + const size_t cbor_buffer_size = 0; |
| 49 | + size_t bytes_copied = 0; |
| 50 | + struct zcbor_string zst; |
| 51 | + struct nrf_rpc_cbor_ctx ctx; |
| 52 | + |
| 53 | + NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size); |
| 54 | + |
| 55 | + nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, id, &ctx); |
| 56 | + |
| 57 | + if (!zcbor_bstr_decode(ctx.zs, &zst)) { |
| 58 | + nrf_rpc_cbor_decoding_done(&ot_group, &ctx); |
| 59 | + ot_rpc_report_decoding_error(id); |
| 60 | + goto exit; |
| 61 | + } |
| 62 | + |
| 63 | + if (buffer_size >= zst.len) { |
| 64 | + bytes_copied = zst.len; |
| 65 | + } else { |
| 66 | + bytes_copied = 0; |
| 67 | + nrf_rpc_cbor_decoding_done(&ot_group, &ctx); |
| 68 | + ot_rpc_report_decoding_error(id); |
| 69 | + goto exit; |
| 70 | + } |
| 71 | + |
| 72 | + memcpy(buffer, zst.value, bytes_copied); |
| 73 | + |
| 74 | + nrf_rpc_cbor_decoding_done(&ot_group, &ctx); |
| 75 | + |
| 76 | +exit: |
| 77 | + return bytes_copied; |
| 78 | +} |
| 79 | + |
| 80 | +const char *otGetVersionString(void) |
| 81 | +{ |
| 82 | + get_string(OT_RPC_CMD_GET_VERSION_STRING, version, sizeof(version)); |
| 83 | + |
| 84 | + return version; |
| 85 | +} |
| 86 | + |
| 87 | +otError otDatasetGetActive(otInstance *aInstance, otOperationalDataset *aDataset) |
| 88 | +{ |
| 89 | + return 0; |
| 90 | +} |
| 91 | + |
| 92 | +uint8_t otLinkGetChannel(otInstance *aInstance) |
| 93 | +{ |
| 94 | + uint8_t ret; |
| 95 | + |
| 96 | + get_uint_t(OT_RPC_CMD_LINK_GET_CHANNEL, &ret, sizeof(ret)); |
| 97 | + |
| 98 | + return ret; |
| 99 | +} |
| 100 | + |
| 101 | +const otMacCounters *otLinkGetCounters(otInstance *aInstance) |
| 102 | +{ |
| 103 | + return 0; |
| 104 | +} |
| 105 | + |
| 106 | +const otExtAddress *otLinkGetExtendedAddress(otInstance *aInstance) |
| 107 | +{ |
| 108 | + int ret_size = get_string(OT_RPC_CMD_LINK_GET_EXTENDED_ADDRESS, mac.m8, sizeof(mac.m8)); |
| 109 | + |
| 110 | + if (ret_size != sizeof(mac.m8)) { |
| 111 | + LOG_ERR("Received mac addr size is too short"); |
| 112 | + ot_rpc_report_decoding_error(OT_RPC_CMD_LINK_GET_EXTENDED_ADDRESS); |
| 113 | + } |
| 114 | + |
| 115 | + return &mac; |
| 116 | +} |
| 117 | + |
| 118 | +otPanId otLinkGetPanId(otInstance *aInstance) |
| 119 | +{ |
| 120 | + otPanId ret; |
| 121 | + |
| 122 | + get_uint_t(OT_RPC_CMD_LINK_GET_PAN_ID, &ret, sizeof(ret)); |
| 123 | + |
| 124 | + return ret; |
| 125 | +} |
| 126 | + |
| 127 | +uint8_t otNetDataGetStableVersion(otInstance *aInstance) |
| 128 | +{ |
| 129 | + uint8_t ret; |
| 130 | + |
| 131 | + get_uint_t(OT_RPC_CMD_NET_DATA_GET_STABLE_VERSION, &ret, sizeof(ret)); |
| 132 | + |
| 133 | + return ret; |
| 134 | +} |
| 135 | + |
| 136 | +uint8_t otNetDataGetVersion(otInstance *aInstance) |
| 137 | +{ |
| 138 | + uint8_t ret; |
| 139 | + |
| 140 | + get_uint_t(OT_RPC_CMD_NET_DATA_GET_VERSION, &ret, sizeof(ret)); |
| 141 | + |
| 142 | + return ret; |
| 143 | +} |
| 144 | + |
| 145 | +const otExtendedPanId *otThreadGetExtendedPanId(otInstance *aInstance) |
| 146 | +{ |
| 147 | + get_string(OT_RPC_CMD_THREAD_GET_EXTENDED_PANID, ext_pan_id.m8, sizeof(ext_pan_id.m8)); |
| 148 | + |
| 149 | + return &ext_pan_id; |
| 150 | +} |
| 151 | + |
| 152 | +uint8_t otThreadGetLeaderRouterId(otInstance *aInstance) |
| 153 | +{ |
| 154 | + uint8_t ret; |
| 155 | + |
| 156 | + get_uint_t(OT_RPC_CMD_THREAD_GET_LEADER_ROUTER_ID, &ret, sizeof(ret)); |
| 157 | + |
| 158 | + return ret; |
| 159 | +} |
| 160 | + |
| 161 | +uint8_t otThreadGetLeaderWeight(otInstance *aInstance) |
| 162 | +{ |
| 163 | + uint8_t ret; |
| 164 | + |
| 165 | + get_uint_t(OT_RPC_CMD_THREAD_GET_LEADER_WEIGHT, &ret, sizeof(ret)); |
| 166 | + |
| 167 | + return ret; |
| 168 | +} |
| 169 | + |
| 170 | +const otMeshLocalPrefix *otThreadGetMeshLocalPrefix(otInstance *aInstance) |
| 171 | +{ |
| 172 | + get_string(OT_RPC_CMD_THREAD_GET_MESH_LOCAL_PREFIX, mesh_prefix.m8, sizeof(mesh_prefix)); |
| 173 | + |
| 174 | + return &mesh_prefix; |
| 175 | +} |
| 176 | + |
| 177 | +const otMleCounters *otThreadGetMleCounters(otInstance *aInstance) |
| 178 | +{ |
| 179 | + return 0; |
| 180 | +} |
| 181 | + |
| 182 | +const char *otThreadGetNetworkName(otInstance *aInstance) |
| 183 | +{ |
| 184 | + get_string(OT_RPC_CMD_THREAD_GET_NETWORK_NAME, thread_network_name, |
| 185 | + sizeof(thread_network_name)); |
| 186 | + |
| 187 | + return thread_network_name; |
| 188 | +} |
| 189 | + |
| 190 | +otError otThreadGetParentInfo(otInstance *aInstance, otRouterInfo *aParentInfo) |
| 191 | +{ |
| 192 | + return 0; |
| 193 | +} |
| 194 | + |
| 195 | +uint32_t otThreadGetPartitionId(otInstance *aInstance) |
| 196 | +{ |
| 197 | + uint32_t ret; |
| 198 | + |
| 199 | + get_uint_t(OT_RPC_CMD_THREAD_GET_PARTITION_ID, &ret, sizeof(ret)); |
| 200 | + |
| 201 | + return ret; |
| 202 | +} |
0 commit comments