]> granicus.if.org Git - esp-idf/blob
e46efc8ecacf15f74146f4899fd7b3e044def98d
[esp-idf] /
1 // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
2 //
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
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
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"
20
21 #include "esp_ble_mesh_networking_api.h"
22 #include "ble_mesh_adapter.h"
23
24 SemaphoreHandle_t ble_mesh_test_perf_send_sema;
25 SemaphoreHandle_t ble_mesh_test_perf_sema;
26
27 typedef struct {
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;
33     struct arg_int *ttl;
34     struct arg_int *app_idx;
35     struct arg_int *net_idx;
36     struct arg_int *dev_role;
37     struct arg_end *end;
38 } ble_mesh_test_perf_client_model_t;
39 ble_mesh_test_perf_client_model_t test_perf_client_model;
40
41 typedef struct {
42     struct arg_str *action_type;
43     struct arg_int *test_size;
44     struct arg_int *node_num;
45     struct arg_int *ttl;
46     struct arg_end *end;
47 } ble_mesh_test_perf_client_model_statistics_t;
48 ble_mesh_test_perf_client_model_statistics_t test_perf_client_model_statistics;
49
50 void ble_mesh_performance_client_model_command();
51
52 void ble_mesh_register_mesh_test_performance_client()
53 {
54     ble_mesh_performance_client_model_command();
55 }
56
57 void ble_mesh_test_performance_client_model_throughput(void *params)
58 {
59     uint16_t i;
60     uint8_t *data = NULL;
61     esp_ble_mesh_msg_ctx_t ctx;
62     ble_mesh_test_perf_throughput_data *profile_context = (ble_mesh_test_perf_throughput_data *)params;
63
64     ESP_LOGD(TAG, "enter %s\n", __func__);
65
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;
71     ctx.send_rel = 0;
72     test_perf_statistics.test_length = profile_context->length;
73
74     // create send data
75     data = malloc(profile_context->length);
76     if (data == NULL) {
77         ESP_LOGE(TAG, " %s, %d, malloc fail\n", __func__, __LINE__);
78     }
79
80     ble_mesh_test_perf_send_sema = xSemaphoreCreateMutex();
81     xSemaphoreTake(ble_mesh_test_perf_send_sema, SEND_MESSAGE_TIMEOUT);
82
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);
90     }
91
92     ESP_LOGI(TAG, "VendorModel:SendPackage,Finish");
93     free(params);
94     vTaskDelete(NULL);
95     ESP_LOGD(TAG, "exit %s\n", __func__);
96 }
97
98 int ble_mesh_test_performance_client_model(int argc, char **argv)
99 {
100     esp_ble_mesh_model_t *model;
101     esp_err_t result = ESP_OK;
102     ble_mesh_test_perf_throughput_data *profile_data = NULL;
103
104     ESP_LOGD(TAG, "enter %s\n", __func__);
105     int nerrors = arg_parse(argc, argv, (void **) &test_perf_client_model);
106     if (nerrors != 0) {
107         arg_print_errors(stderr, test_perf_client_model.end, argv[0]);
108         return 1;
109     }
110
111     model = ble_mesh_get_model(ESP_BLE_MESH_VND_MODEL_ID_TEST_PERF_CLI);
112
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");
118         }
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__);
124             return 1;
125         }
126
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");
135
136         if (profile_data->opcode == ESP_BLE_MESH_VND_MODEL_OP_TEST_PERF_SET) {
137             profile_data->need_ack = true;
138         } else {
139             profile_data->need_ack = false;
140         }
141
142         xTaskCreate(ble_mesh_test_performance_client_model_throughput, "MESHTHROUGHPUTSEND", 4048, profile_data, 1, NULL);
143     }
144
145     ESP_LOGD(TAG, "exit %s\n", __func__);
146     return result;
147 }
148
149 int ble_mesh_test_performance_client_model_performance(int argc, char **argv)
150 {
151     uint8_t result = 0;
152
153     ESP_LOGD(TAG, "enter %s\n", __func__);
154     int nerrors = arg_parse(argc, argv, (void **) &test_perf_client_model_statistics);
155     if (nerrors != 0) {
156         arg_print_errors(stderr, test_perf_client_model_statistics.end, argv[0]);
157         return 1;
158     }
159
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]);
163         if (result == 0) {
164             ESP_LOGI(TAG, "VendorPerfTest:InitStatistics,OK\n");
165         }
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");
174     }
175
176     ESP_LOGD(TAG, "exit %s\n", __func__);
177     return 0;
178 }
179
180 void ble_mesh_performance_client_model_command()
181 {
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);
195
196     const esp_console_cmd_t test_perf_client_model_cmd = {
197         .command = "bmtpcvm",
198         .help = "ble mesh test performance client vendor model",
199         .hint = NULL,
200         .func = &ble_mesh_test_performance_client_model,
201         .argtable = &test_perf_client_model,
202     };
203     ESP_ERROR_CHECK(esp_console_cmd_register(&test_perf_client_model_cmd));
204
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);
210
211     const esp_console_cmd_t test_perf_client_model_performance_cmd = {
212         .command = "bmcperf",
213         .help = "ble mesh client: test performance",
214         .hint = NULL,
215         .func = &ble_mesh_test_performance_client_model_performance,
216         .argtable = &test_perf_client_model_statistics,
217     };
218     ESP_ERROR_CHECK(esp_console_cmd_register(&test_perf_client_model_performance_cmd));
219 }