1 // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 #include "freertos/FreeRTOS.h"
16 #include "freertos/task.h"
17 #include "freertos/queue.h"
18 #include "freertos/xtensa_api.h"
19 #include "freertos/FreeRTOSConfig.h"
21 #include "esp_ble_mesh_networking_api.h"
22 #include "ble_mesh_adapter.h"
24 SemaphoreHandle_t ble_mesh_test_perf_send_sema;
25 SemaphoreHandle_t ble_mesh_test_perf_sema;
28 struct arg_str *action_type;
29 struct arg_int *playload_byte;
30 struct arg_int *test_num;
31 struct arg_int *opcode;
32 struct arg_int *unicast_address;
34 struct arg_int *app_idx;
35 struct arg_int *net_idx;
36 struct arg_int *dev_role;
38 } ble_mesh_test_perf_client_model_t;
39 ble_mesh_test_perf_client_model_t test_perf_client_model;
42 struct arg_str *action_type;
43 struct arg_int *test_size;
44 struct arg_int *node_num;
47 } ble_mesh_test_perf_client_model_statistics_t;
48 ble_mesh_test_perf_client_model_statistics_t test_perf_client_model_statistics;
50 void ble_mesh_performance_client_model_command();
52 void ble_mesh_register_mesh_test_performance_client()
54 ble_mesh_performance_client_model_command();
57 void ble_mesh_test_performance_client_model_throughput(void *params)
61 esp_ble_mesh_msg_ctx_t ctx;
62 ble_mesh_test_perf_throughput_data *profile_context = (ble_mesh_test_perf_throughput_data *)params;
64 ESP_LOGD(TAG, "enter %s\n", __func__);
66 ctx.net_idx = profile_context->net_idx;
67 ctx.app_idx = profile_context->app_idx;
68 ctx.addr = profile_context->address;
69 ctx.send_ttl = profile_context->ttl;
70 ctx.model = profile_context->model;
72 test_perf_statistics.test_length = profile_context->length;
75 data = malloc(profile_context->length);
77 ESP_LOGE(TAG, " %s, %d, malloc fail\n", __func__, __LINE__);
80 ble_mesh_test_perf_send_sema = xSemaphoreCreateMutex();
81 xSemaphoreTake(ble_mesh_test_perf_send_sema, SEND_MESSAGE_TIMEOUT);
83 for (i = 1; i <= profile_context->test_num; i++) {
84 ble_mesh_create_send_data((char *)data, profile_context->length, i, profile_context->opcode);
85 start_time = esp_timer_get_time();
86 esp_ble_mesh_client_model_send_msg(profile_context->model, &ctx, profile_context->opcode,
87 profile_context->length, data, 8000, profile_context->need_ack, profile_context->device_role);
88 ble_mesh_test_performance_client_model_accumulate_statistics(profile_context->length);
89 xSemaphoreTake(ble_mesh_test_perf_send_sema, SEND_MESSAGE_TIMEOUT);
92 ESP_LOGI(TAG, "VendorModel:SendPackage,Finish");
95 ESP_LOGD(TAG, "exit %s\n", __func__);
98 int ble_mesh_test_performance_client_model(int argc, char **argv)
100 esp_ble_mesh_model_t *model;
101 esp_err_t result = ESP_OK;
102 ble_mesh_test_perf_throughput_data *profile_data = NULL;
104 ESP_LOGD(TAG, "enter %s\n", __func__);
105 int nerrors = arg_parse(argc, argv, (void **) &test_perf_client_model);
107 arg_print_errors(stderr, test_perf_client_model.end, argv[0]);
111 model = ble_mesh_get_model(ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI);
113 if (strcmp(test_perf_client_model.action_type->sval[0], "init") == 0) {
114 ble_mesh_test_perf_sema = xSemaphoreCreateMutex();
115 result = esp_ble_mesh_client_model_init(model);
116 if (result == ESP_OK) {
117 ESP_LOGI(TAG, "VendorClientModel:Init,OK");
119 } else if (strcmp(test_perf_client_model.action_type->sval[0], "start") == 0) {
120 profile_data = malloc(sizeof(ble_mesh_test_perf_throughput_data));
121 profile_data->model = model;
122 if (profile_data == NULL) {
123 ESP_LOGE(TAG, " %s, %d malloc fail\n", __func__, __LINE__);
127 arg_int_to_value(test_perf_client_model.playload_byte, profile_data->length, "length");
128 arg_int_to_value(test_perf_client_model.opcode, profile_data->opcode, "opcode");
129 arg_int_to_value(test_perf_client_model.unicast_address, profile_data->address, "publish address");
130 arg_int_to_value(test_perf_client_model.ttl, profile_data->ttl, "model ttl");
131 arg_int_to_value(test_perf_client_model.app_idx, profile_data->app_idx, "appkey index");
132 arg_int_to_value(test_perf_client_model.net_idx, profile_data->net_idx, "network key index");
133 arg_int_to_value(test_perf_client_model.dev_role, profile_data->device_role, "device role");
134 arg_int_to_value(test_perf_client_model.test_num, profile_data->test_num, "test number");
136 if (profile_data->opcode == ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET) {
137 profile_data->need_ack = true;
139 profile_data->need_ack = false;
142 xTaskCreate(ble_mesh_test_performance_client_model_throughput, "MESHTHROUGHPUTSEND", 4048, profile_data, 1, NULL);
145 ESP_LOGD(TAG, "exit %s\n", __func__);
149 int ble_mesh_test_performance_client_model_performance(int argc, char **argv)
153 ESP_LOGD(TAG, "enter %s\n", __func__);
154 int nerrors = arg_parse(argc, argv, (void **) &test_perf_client_model_statistics);
156 arg_print_errors(stderr, test_perf_client_model_statistics.end, argv[0]);
160 if (strcmp(test_perf_client_model_statistics.action_type->sval[0], "init") == 0) {
161 result = ble_mesh_test_performance_client_model_init(test_perf_client_model_statistics.node_num->ival[0],
162 test_perf_client_model_statistics.test_size->ival[0], test_perf_client_model_statistics.ttl->ival[0]);
164 ESP_LOGI(TAG, "VendorPerfTest:InitStatistics,OK\n");
166 } else if (strcmp(test_perf_client_model_statistics.action_type->sval[0], "get") == 0) {
167 ble_mesh_test_performance_client_model_get();
168 } else if (strcmp(test_perf_client_model_statistics.action_type->sval[0], "destroy") == 0) {
169 ble_mesh_test_performance_client_model_destroy();
170 ESP_LOGI(TAG, "VendorPerfTest:DestroyStatistics,OK\n");
171 } else if (strcmp(test_perf_client_model_statistics.action_type->sval[0], "percent") == 0) {
172 ble_mesh_test_performance_client_model_get_received_percent();
173 ESP_LOGI(TAG, "VendorPerfTest:GetPercent,OK\n");
176 ESP_LOGD(TAG, "exit %s\n", __func__);
180 void ble_mesh_performance_client_model_command()
182 test_perf_client_model.action_type = arg_str1("z", NULL, "<action>", "action type");
183 test_perf_client_model.playload_byte = arg_int0("p", NULL, "<byte>", "playload byte");
184 test_perf_client_model.test_num = arg_int0("n", NULL, "<number>", "test number");
185 // set test num default to 1000
186 test_perf_client_model.test_num->ival[0] = 1000;
187 test_perf_client_model.opcode = arg_int0("o", NULL, "<opcode>", "opcode");
188 test_perf_client_model.unicast_address = arg_int0("u", NULL, "<address>", "unicast address");
189 test_perf_client_model.ttl = arg_int0("t", NULL, "<ttl>", "ttl");
190 test_perf_client_model.app_idx = arg_int0("a", NULL, "<appkey>", "appkey index");
191 test_perf_client_model.net_idx = arg_int0("i", NULL, "<network key>", "network key index");
192 test_perf_client_model.dev_role = arg_int0("d", NULL, "<role>", "device role");
193 test_perf_client_model.dev_role->ival[0] = ROLE_PROVISIONER;
194 test_perf_client_model.end = arg_end(1);
196 const esp_console_cmd_t test_perf_client_model_cmd = {
197 .command = "bmtpcvm",
198 .help = "ble mesh test performance client vendor model",
200 .func = &ble_mesh_test_performance_client_model,
201 .argtable = &test_perf_client_model,
203 ESP_ERROR_CHECK(esp_console_cmd_register(&test_perf_client_model_cmd));
205 test_perf_client_model_statistics.action_type = arg_str1("z", NULL, "<action>", "action type");
206 test_perf_client_model_statistics.test_size = arg_int0("s", NULL, "<test size>", "test size");
207 test_perf_client_model_statistics.node_num = arg_int0("n", NULL, "<node number>", "node number");
208 test_perf_client_model_statistics.ttl = arg_int0("l", NULL, "<test number>", "ttl");
209 test_perf_client_model_statistics.end = arg_end(1);
211 const esp_console_cmd_t test_perf_client_model_performance_cmd = {
212 .command = "bmcperf",
213 .help = "ble mesh client: test performance",
215 .func = &ble_mesh_test_performance_client_model_performance,
216 .argtable = &test_perf_client_model_statistics,
218 ESP_ERROR_CHECK(esp_console_cmd_register(&test_perf_client_model_performance_cmd));