|
| 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_types.h> |
| 9 | +#include <ot_rpc_common.h> |
| 10 | + |
| 11 | +#include <nrf_rpc/nrf_rpc_serialize.h> |
| 12 | +#include <nrf_rpc/nrf_rpc_cbkproxy.h> |
| 13 | + |
| 14 | +#include <zephyr/net/openthread.h> |
| 15 | + |
| 16 | +#include <openthread/link.h> |
| 17 | + |
| 18 | +#define ACTIVE_SCAN_RESULTS_LENGTH(result) \ |
| 19 | + sizeof((result)->mExtAddress.m8) + 1 + \ |
| 20 | + sizeof((result)->mNetworkName.m8) + 1 + \ |
| 21 | + sizeof((result)->mExtendedPanId.m8) + 1 + \ |
| 22 | + sizeof((result)->mSteeringData.m8) + 1 + \ |
| 23 | + sizeof((result)->mPanId) + 1 + \ |
| 24 | + sizeof((result)->mJoinerUdpPort) + 1 + \ |
| 25 | + sizeof((result)->mChannel) + 1 + \ |
| 26 | + sizeof((result)->mRssi) + 1 + \ |
| 27 | + sizeof((result)->mLqi) + 1 + \ |
| 28 | + sizeof(uint8_t) + 1 + 3 /* mIsNative, mDiscover, mIsJoinable as booleans */ |
| 29 | + |
| 30 | +static void ot_thread_discover_cb(otActiveScanResult *result, void *context, uint32_t callback_slot) |
| 31 | +{ |
| 32 | + struct nrf_rpc_cbor_ctx ctx; |
| 33 | + size_t cbor_buffer_size = result ? ACTIVE_SCAN_RESULTS_LENGTH(result) : 1; |
| 34 | + |
| 35 | + cbor_buffer_size += 10; /* for context and callback slot */ |
| 36 | + NRF_RPC_CBOR_ALLOC(&ot_group, ctx, cbor_buffer_size); |
| 37 | + |
| 38 | + if (result) { |
| 39 | + nrf_rpc_encode_buffer(&ctx, result->mExtAddress.m8, OT_EXT_ADDRESS_SIZE); |
| 40 | + nrf_rpc_encode_str(&ctx, result->mNetworkName.m8, strlen(result->mNetworkName.m8)); |
| 41 | + nrf_rpc_encode_buffer(&ctx, result->mExtendedPanId.m8, OT_EXT_PAN_ID_SIZE); |
| 42 | + nrf_rpc_encode_buffer(&ctx, result->mSteeringData.m8, OT_STEERING_DATA_MAX_LENGTH); |
| 43 | + nrf_rpc_encode_uint(&ctx, result->mPanId); |
| 44 | + nrf_rpc_encode_uint(&ctx, result->mJoinerUdpPort); |
| 45 | + nrf_rpc_encode_uint(&ctx, result->mChannel); |
| 46 | + nrf_rpc_encode_int(&ctx, result->mRssi); |
| 47 | + nrf_rpc_encode_uint(&ctx, result->mLqi); |
| 48 | + nrf_rpc_encode_uint(&ctx, result->mVersion); |
| 49 | + nrf_rpc_encode_bool(&ctx, result->mIsNative); |
| 50 | + nrf_rpc_encode_bool(&ctx, result->mDiscover); |
| 51 | + nrf_rpc_encode_bool(&ctx, result->mIsJoinable); |
| 52 | + } else { |
| 53 | + nrf_rpc_encode_null(&ctx); |
| 54 | + } |
| 55 | + |
| 56 | + nrf_rpc_encode_uint(&ctx, (uintptr_t)context); |
| 57 | + nrf_rpc_encode_uint(&ctx, callback_slot); |
| 58 | + |
| 59 | + nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_THREAD_DISCOVER_CB, &ctx, ot_rpc_decode_void, |
| 60 | + NULL); |
| 61 | +} |
| 62 | + |
| 63 | +NRF_RPC_CBKPROXY_HANDLER(ot_thread_discover_cb_encoder, ot_thread_discover_cb, |
| 64 | + (otActiveScanResult *result, void *context), (result, context)); |
| 65 | + |
| 66 | +static void ot_rpc_thread_discover_rpc_handler(const struct nrf_rpc_group *group, |
| 67 | + struct nrf_rpc_cbor_ctx *ctx, void *handler_data) |
| 68 | +{ |
| 69 | + uint32_t scan_channels; |
| 70 | + uint16_t pan_id; |
| 71 | + bool joiner; |
| 72 | + bool enable_eui64_filtering; |
| 73 | + otHandleActiveScanResult cb; |
| 74 | + void *cb_ctx; |
| 75 | + otError error; |
| 76 | + |
| 77 | + scan_channels = nrf_rpc_decode_uint(ctx); |
| 78 | + pan_id = nrf_rpc_decode_uint(ctx); |
| 79 | + joiner = nrf_rpc_decode_bool(ctx); |
| 80 | + enable_eui64_filtering = nrf_rpc_decode_bool(ctx); |
| 81 | + cb = (otHandleActiveScanResult)nrf_rpc_decode_callbackd(ctx, ot_thread_discover_cb_encoder); |
| 82 | + cb_ctx = (void *)nrf_rpc_decode_uint(ctx); |
| 83 | + |
| 84 | + if (!nrf_rpc_decoding_done_and_check(group, ctx)) { |
| 85 | + ot_rpc_report_decoding_error(OT_RPC_CMD_THREAD_DISCOVER); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + openthread_api_mutex_lock(openthread_get_default_context()); |
| 90 | + error = otThreadDiscover(openthread_get_default_instance(), scan_channels, pan_id, joiner, |
| 91 | + enable_eui64_filtering, cb, cb_ctx); |
| 92 | + openthread_api_mutex_unlock(openthread_get_default_context()); |
| 93 | + |
| 94 | + nrf_rpc_rsp_send_uint(group, error); |
| 95 | +} |
| 96 | + |
| 97 | +NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_thread_discover, OT_RPC_CMD_THREAD_DISCOVER, |
| 98 | + ot_rpc_thread_discover_rpc_handler, NULL); |
| 99 | + |
| 100 | +static void ot_rpc_dataset_is_commissioned_rpc_handler(const struct nrf_rpc_group *group, |
| 101 | + struct nrf_rpc_cbor_ctx *ctx, |
| 102 | + void *handler_data) |
| 103 | +{ |
| 104 | + bool result; |
| 105 | + |
| 106 | + if (!nrf_rpc_decoding_done_and_check(group, ctx)) { |
| 107 | + ot_rpc_report_decoding_error(OT_RPC_CMD_DATASET_IS_COMMISSIONED); |
| 108 | + return; |
| 109 | + } |
| 110 | + |
| 111 | + openthread_api_mutex_lock(openthread_get_default_context()); |
| 112 | + result = otDatasetIsCommissioned(openthread_get_default_instance()); |
| 113 | + openthread_api_mutex_unlock(openthread_get_default_context()); |
| 114 | + |
| 115 | + nrf_rpc_rsp_send_bool(group, result); |
| 116 | +} |
| 117 | + |
| 118 | +NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_dataset_is_commissioned, |
| 119 | + OT_RPC_CMD_DATASET_IS_COMMISSIONED, |
| 120 | + ot_rpc_dataset_is_commissioned_rpc_handler, NULL); |
| 121 | + |
| 122 | +static void ot_rpc_data_set_active_tlvs_rpc_handler(const struct nrf_rpc_group *group, |
| 123 | + struct nrf_rpc_cbor_ctx *ctx, |
| 124 | + void *handler_data) |
| 125 | +{ |
| 126 | + otOperationalDatasetTlvs dataset; |
| 127 | + void *data_ptr = &dataset; |
| 128 | + otError error; |
| 129 | + |
| 130 | + ot_rpc_decode_dataset_tlvs(group, ctx, &data_ptr); |
| 131 | + |
| 132 | + if (!nrf_rpc_decoding_done_and_check(group, ctx)) { |
| 133 | + ot_rpc_report_decoding_error(OT_RPC_CMD_DATA_SET_ACTIVE_TLVS); |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + if (data_ptr) { |
| 138 | + openthread_api_mutex_lock(openthread_get_default_context()); |
| 139 | + error = otDatasetSetActiveTlvs(openthread_get_default_instance(), &dataset); |
| 140 | + openthread_api_mutex_unlock(openthread_get_default_context()); |
| 141 | + } else { |
| 142 | + error = OT_ERROR_INVALID_ARGS; |
| 143 | + } |
| 144 | + |
| 145 | + nrf_rpc_rsp_send_uint(group, error); |
| 146 | +} |
| 147 | + |
| 148 | +NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_data_set_active_tlvs, OT_RPC_CMD_DATA_SET_ACTIVE_TLVS, |
| 149 | + ot_rpc_data_set_active_tlvs_rpc_handler, NULL); |
| 150 | + |
| 151 | +static void ot_rpc_rsp_send_dataset(otOperationalDatasetTlvs *dataset) |
| 152 | +{ |
| 153 | + struct nrf_rpc_cbor_ctx ctx; |
| 154 | + |
| 155 | + NRF_RPC_CBOR_ALLOC(&ot_group, ctx, dataset ? dataset->mLength + 1 : 1); |
| 156 | + nrf_rpc_encode_buffer(&ctx, dataset ? dataset->mTlvs : NULL, |
| 157 | + dataset ? dataset->mLength : 0); |
| 158 | + nrf_rpc_cbor_rsp_no_err(&ot_group, &ctx); |
| 159 | +} |
| 160 | + |
| 161 | +static void ot_rpc_data_get_active_tlvs_rpc_handler(const struct nrf_rpc_group *group, |
| 162 | + struct nrf_rpc_cbor_ctx *ctx, |
| 163 | + void *handler_data) |
| 164 | +{ |
| 165 | + otOperationalDatasetTlvs dataset; |
| 166 | + otError error; |
| 167 | + |
| 168 | + if (!nrf_rpc_decoding_done_and_check(group, ctx)) { |
| 169 | + ot_rpc_report_decoding_error(OT_RPC_CMD_DATA_GET_ACTIVE_TLVS); |
| 170 | + return; |
| 171 | + } |
| 172 | + |
| 173 | + openthread_api_mutex_lock(openthread_get_default_context()); |
| 174 | + error = otDatasetGetActiveTlvs(openthread_get_default_instance(), &dataset); |
| 175 | + openthread_api_mutex_unlock(openthread_get_default_context()); |
| 176 | + |
| 177 | + ot_rpc_rsp_send_dataset(error == OT_ERROR_NONE ? &dataset : NULL); |
| 178 | +} |
| 179 | + |
| 180 | +NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_data_get_active_tlvs, OT_RPC_CMD_DATA_GET_ACTIVE_TLVS, |
| 181 | + ot_rpc_data_get_active_tlvs_rpc_handler, NULL); |
0 commit comments