NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
sha_partial_create_main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022-2023 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 #include <doca_argp.h>
30 #include <doca_error.h>
31 #include <doca_dev.h>
32 #include <doca_sha.h>
33 #include <doca_log.h>
34 
35 #include <utils.h>
36 
37 DOCA_LOG_REGISTER(SHA_PARTIAL_CREATE::MAIN);
38 
39 #define MAX_USER_DATA_LEN 1024 /* max user data length */
40 #define MAX_DATA_LEN (MAX_USER_DATA_LEN + 1) /* max data length */
41 
42 /* Sample's Logic */
43 doca_error_t sha_partial_create(const char *data);
44 
45 /*
46  * ARGP Callback - Handle user data parameter
47  *
48  * @param [in]: Input parameter
49  * @config [in/out]: Program configuration context
50  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
51  */
52 static doca_error_t data_callback(void *param, void *config)
53 {
54  char *data = (char *)config;
55  char *input_data = (char *)param;
56  int len;
57 
58  len = strnlen(input_data, MAX_DATA_LEN);
59  if (len == MAX_DATA_LEN) {
60  DOCA_LOG_ERR("Invalid data length, should be less than %d", MAX_USER_DATA_LEN);
62  }
63  strcpy(data, input_data);
64  return DOCA_SUCCESS;
65 }
66 
67 /*
68  * Register the command line parameters for the sample.
69  *
70  * @return: DOCA_SUCCESS on success and DOCA_ERROR otherwise
71  */
73 {
75  struct doca_argp_param *data_param;
76 
77  result = doca_argp_param_create(&data_param);
78  if (result != DOCA_SUCCESS) {
79  DOCA_LOG_ERR("Failed to create ARGP param: %s", doca_error_get_descr(result));
80  return result;
81  }
82  doca_argp_param_set_short_name(data_param, "d");
83  doca_argp_param_set_long_name(data_param, "data");
84  doca_argp_param_set_description(data_param, "user data");
87  result = doca_argp_register_param(data_param);
88  if (result != DOCA_SUCCESS) {
89  DOCA_LOG_ERR("Failed to register program param: %s", doca_error_get_descr(result));
90  return result;
91  }
92  return DOCA_SUCCESS;
93 }
94 
95 /*
96  * Sample main function
97  *
98  * @argc [in]: command line arguments size
99  * @argv [in]: array of command line arguments
100  * @return: EXIT_SUCCESS on success and EXIT_FAILURE otherwise
101  */
102 int main(int argc, char **argv)
103 {
105  struct doca_log_backend *sdk_log;
106  int exit_status = EXIT_FAILURE;
107  char data[MAX_DATA_LEN];
108 
109  strcpy(data, "11111111222222223333333344444444555555556666666677777777888888889999999900000000");
110 
111  /* Register a logger backend */
113  if (result != DOCA_SUCCESS)
114  goto sample_exit;
115 
116  /* Register a logger backend for internal SDK errors and warnings */
117  result = doca_log_backend_create_with_file_sdk(stderr, &sdk_log);
118  if (result != DOCA_SUCCESS)
119  goto sample_exit;
121  if (result != DOCA_SUCCESS)
122  goto sample_exit;
123 
124  DOCA_LOG_INFO("Starting the sample");
125 
126  result = doca_argp_init(NULL, &data);
127  if (result != DOCA_SUCCESS) {
128  DOCA_LOG_ERR("Failed to init ARGP resources: %s", doca_error_get_descr(result));
129  goto sample_exit;
130  }
131 
133  if (result != DOCA_SUCCESS) {
134  DOCA_LOG_ERR("Failed to register ARGP params: %s", doca_error_get_descr(result));
135  goto argp_cleanup;
136  }
137 
138  result = doca_argp_start(argc, argv);
139  if (result != DOCA_SUCCESS) {
140  DOCA_LOG_ERR("Failed to parse sample input: %s", doca_error_get_descr(result));
141  goto argp_cleanup;
142  }
143 
144  result = sha_partial_create(data);
145  if (result != DOCA_SUCCESS) {
146  DOCA_LOG_ERR("sha_partial_create() encountered an error: %s", doca_error_get_descr(result));
147  goto argp_cleanup;
148  }
149 
150  exit_status = EXIT_SUCCESS;
151 
152 argp_cleanup:
154 sample_exit:
155  if (exit_status == EXIT_SUCCESS)
156  DOCA_LOG_INFO("Sample finished successfully");
157  else
158  DOCA_LOG_INFO("Sample finished with errors");
159  return exit_status;
160 }
#define NULL
Definition: __stddef_null.h:26
int32_t result
uint64_t len
DOCA_EXPERIMENTAL void doca_argp_param_set_description(struct doca_argp_param *param, const char *description)
Set the description of the program param, used during program usage.
DOCA_EXPERIMENTAL void doca_argp_param_set_long_name(struct doca_argp_param *param, const char *name)
Set the long name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_start(int argc, char **argv)
Parse incoming arguments (cmd line/json).
DOCA_EXPERIMENTAL doca_error_t doca_argp_init(const char *program_name, void *program_config)
Initialize the parser interface.
DOCA_EXPERIMENTAL void doca_argp_param_set_callback(struct doca_argp_param *param, doca_argp_param_cb_t callback)
Set the callback function of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_param_create(struct doca_argp_param **param)
Create new program param.
DOCA_EXPERIMENTAL void doca_argp_param_set_type(struct doca_argp_param *param, enum doca_argp_type type)
Set the type of the param arguments.
DOCA_EXPERIMENTAL void doca_argp_param_set_short_name(struct doca_argp_param *param, const char *name)
Set the short name of the program param.
DOCA_EXPERIMENTAL doca_error_t doca_argp_destroy(void)
ARG Parser destroy.
DOCA_EXPERIMENTAL doca_error_t doca_argp_register_param(struct doca_argp_param *input_param)
Register a program flag.
@ DOCA_ARGP_TYPE_STRING
Definition: doca_argp.h:56
enum doca_error doca_error_t
DOCA API return codes.
DOCA_STABLE const char * doca_error_get_descr(doca_error_t error)
Returns the description string of an error code.
@ DOCA_ERROR_INVALID_VALUE
Definition: doca_error.h:44
@ DOCA_SUCCESS
Definition: doca_error.h:38
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_standard(void)
Create default, non configurable backend for application messages.
#define DOCA_LOG_ERR(format,...)
Generates an ERROR application log message.
Definition: doca_log.h:466
#define DOCA_LOG_INFO(format,...)
Generates an INFO application log message.
Definition: doca_log.h:486
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_create_with_file_sdk(FILE *fptr, struct doca_log_backend **backend)
Create a logging backend with a FILE* stream for SDK messages.
DOCA_EXPERIMENTAL doca_error_t doca_log_backend_set_sdk_level(struct doca_log_backend *backend, uint32_t level)
Set the log level limit for SDK logging backends.
@ DOCA_LOG_LEVEL_WARNING
Definition: doca_log.h:47
#define MAX_DATA_LEN
#define MAX_USER_DATA_LEN
static doca_error_t data_callback(void *param, void *config)
int main(int argc, char **argv)
static doca_error_t register_sha_params(void)
DOCA_LOG_REGISTER(SHA_PARTIAL_CREATE::MAIN)
doca_error_t sha_partial_create(const char *data)