libxcoder  5.4.0
ni_rsrc_update.c
Go to the documentation of this file.
1 /*******************************************************************************
2  *
3  * Copyright (C) 2022 NETINT Technologies
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy
6  * of this software and associated documentation files (the "Software"), to deal
7  * in the Software without restriction, including without limitation the rights
8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  * copies of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18  * SOFTWARE.
19  *
20  ******************************************************************************/
21 
22 /*!*****************************************************************************
23  * \file ni_rsrc_update.c
24  *
25  * \brief Application for managing registration/deregistration of individual
26  * NETINT video processing devices on system
27  ******************************************************************************/
28 
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include "ni_log.h"
33 
34 #if __linux__ || __APPLE__
35 #include <unistd.h>
36 #include <sys/types.h>
37 #endif
38 
39 #include "ni_defs.h"
40 #include "ni_rsrc_api.h"
41 #include "ni_rsrc_priv.h"
42 #include "ni_util.h"
43 
44 #ifdef _WIN32
45 #include "ni_getopt.h"
46 #define DEV_NAME_PREFIX "\\\\.\\Scsi"
47 #elif __linux__
48 #define DEV_NAME_PREFIX "/dev/nvme"
49 #elif __APPLE__
50 #define DEV_NAME_PREFIX "/dev/disk"
51 #endif
52 
53 #define CHAR_DEV_NAME_LEN 64
54 
55 /*!******************************************************************************
56  * \brief get the NVMe device's block device name (e.g. /dev/nvmeXnY)
57  *
58  * \param in whole device name passed in
59  * dev_name full block device name
60  *
61  * \return 0 if value device name is found, -1 otherwise
62  ******************************************************************************/
63 static int get_dev_name(const char *in, char *dev_name)
64 {
65  if (!in || !dev_name)
66  {
68  "Error: one or more of the given arguments is NULL.\n");
69  return -1;
70  }
71 
72  // for linux blk name (/dev/nvmeXnY)
73  // for apple blk name (/dev/diskX)
74  // for android blk name (/dev/nvmeXnY or /dev/block/nvmeXnY)
75  // for windows blk name (\\\\.\\PHYSICALDRIVEX)
76  ni_strcpy(dev_name, CHAR_DEV_NAME_LEN, in);
77 
78  return 0;
79 
80 }
81 
82 static void display_help(void)
83 {
84  printf("Usage: ni_rsrc_update [OPTION]\n"
85  "Update NetInt xcoder resource (encoders and decoders) status.\n"
86  "\n"
87  " -a device_file Create a resource entry for a newly active "
88  "transcoder card on host\n"
89  " -d device_file Delete the resource entry for a transcoder "
90  "card removed from host\n"
91  " -D Delete ALL the resource entries for transcoder "
92  "card on this host\n"
93  " -r Init transcoder card resource regardless "
94  "firmware release version\n"
95  " Default is to only init cards matching current "
96  "release version\n"
97  " -l Set loglevel of libxcoder API.\n"
98  " [none, fatal, error, info, debug, trace]\n"
99  " Default: info\n"
100  " -h Display this help and exit\n"
101  " -v Print version info and exit\n");
102 }
103 
104 int main(int argc, char *argv[])
105 {
106  int opt, rc = 0;
107  char char_dev_name[CHAR_DEV_NAME_LEN] = {0};
108  int should_match_rev = 1;
109  int add_dev = 0; // default is to add(not delete) a resource
110  int del_dev = 0;
111  int delete_all = 0; // delete ALL the resources on the host
112  ni_log_level_t log_level = NI_LOG_INFO;
113 
114  if (argc == 1) {
115  display_help();
116  return 0;
117  }
118 
119  // arg handling
120  while ((opt = getopt(argc, argv, "hvrDa:d:l:")) != -1)
121  {
122  switch (opt)
123  {
124  case 'd':
125  rc = get_dev_name(optarg, char_dev_name);
126  if (rc)
127  {
128  fprintf(stderr, "ERROR: get_dev_name() returned %d\n", rc);
129  return EXIT_FAILURE;
130  }
131  del_dev = 1;
132  break;
133  case 'a':
134 #ifdef __linux__
135  rc = get_dev_name(optarg, char_dev_name);
136  if (rc)
137  {
138  fprintf(stderr, "ERROR: get_dev_name() returned %d\n", rc);
139  return EXIT_FAILURE;
140  }
141  add_dev = 1;
142 #endif
143  break;
144  case 'D':
145  delete_all = 1;
146  break;
147  case 'r':
148  should_match_rev = 0;
149  break;
150  case 'l':
151  log_level = arg_to_ni_log_level(optarg);
152  if (log_level != NI_LOG_INVALID)
153  {
154  ni_log_set_level(log_level);
155  } else {
156  fprintf(stderr, "FATAL: invalid log level selected: %s\n",
157  optarg);
158  exit(1);
159  }
160  break;
161  case 'v':
162  printf("Release ver: %s\n"
163  "API ver: %s\n"
164  "Date: %s\n"
165  "ID: %s\n",
168  return 0;
169  case 'h':
170  default:
171  display_help();
172  return 0;
173  }
174  }
175 
176  // check option
177 #ifdef __linux__
178  if (add_dev && (del_dev || delete_all))
179  {
180  fprintf(stderr, "Error: can not add and delete device at the same time\n\n");
181  display_help();
182  return 1;
183  }
184 #endif
185  if (!should_match_rev && !add_dev)
186  {
187  fprintf(stderr, "Error: -r option must be used with -a option\n\n");
188  display_help();
189  return 1;
190  }
191  if (add_dev)
192  {
193  rc = ni_rsrc_add_device(char_dev_name, should_match_rev);
194  if (rc)
195  printf("%s not added as transcoder.\n", char_dev_name);
196  else
197  printf("Added transcoder %s successfully.\n", char_dev_name);
198  return rc;
199  }
200  else if (delete_all)
201  {
203  if (rc)
204  printf("Error removing all transcoder resources.\n");
205  else
206  printf("Removing all transcoder resources successfully.\n");
207  return rc;
208  }
209  else if (del_dev)
210  {
211  rc = ni_rsrc_remove_device(char_dev_name);
212  if (rc)
213  printf("%s not removed as transcoder.\n", char_dev_name);
214  else
215  printf("Removed transcoder %s successfully.\n", char_dev_name);
216  return rc;
217  }
218  else
219  {
220  fprintf(stderr, "Error: ni_rsrc_update option must be used with -a or -b or -D option\n\n");
221  display_help();
222  return 1;
223  }
224 }
ni_log_level_t
ni_log_level_t
Definition: ni_log.h:57
ni_rsrc_remove_device
int ni_rsrc_remove_device(const char *dev)
Remove an NetInt h/w device from resource pool on the host.
Definition: ni_rsrc_api.cpp:2237
CHAR_DEV_NAME_LEN
#define CHAR_DEV_NAME_LEN
Definition: ni_rsrc_update.c:53
ni_strcpy
ni_retcode_t ni_strcpy(char *dest, size_t dmax, const char *src)
Definition: ni_util.c:449
NI_SW_RELEASE_ID
#define NI_SW_RELEASE_ID
Definition: ni_release_info.h:29
ni_log_set_level
void ni_log_set_level(ni_log_level_t level)
Set ni_log_level.
Definition: ni_log.c:202
ni_rsrc_api.h
Public definitions for managing NETINT video processing devices.
NI_XCODER_REVISION
#define NI_XCODER_REVISION
Definition: ni_defs.h:98
ni_rsrc_add_device
int ni_rsrc_add_device(const char *dev, int should_match_rev)
Add an NetInt h/w device into resource pool on the host.
Definition: ni_rsrc_api.cpp:2570
ni_log.h
Logging definitions.
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:63
arg_to_ni_log_level
ni_log_level_t arg_to_ni_log_level(const char *arg_str)
Convert terminal arg string to ni_log_level_t.
Definition: ni_log.c:262
NI_LOG_ERROR
@ NI_LOG_ERROR
Definition: ni_log.h:62
ni_defs.h
Common NETINT definitions used by all modules.
NI_SW_RELEASE_TIME
#define NI_SW_RELEASE_TIME
Definition: ni_release_info.h:28
ni_log
void ni_log(ni_log_level_t level, const char *fmt,...)
print log message using ni_log_callback
Definition: ni_log.c:183
NI_LOG_INVALID
@ NI_LOG_INVALID
Definition: ni_log.h:59
optarg
char * optarg
Definition: ni_getopt.c:33
main
int main(int argc, char *argv[])
Definition: ni_rsrc_update.c:104
ni_rsrc_priv.h
Private definitions used by ni_rsrc_api.cpp for management of NETINT video processing devices.
getopt
int getopt(int argc, char *argv[], const char *optstring)
Definition: ni_getopt.c:38
LIBXCODER_API_VERSION
#define LIBXCODER_API_VERSION
Definition: ni_defs.h:115
ni_util.h
Utility definitions.
ni_rsrc_remove_all_devices
int ni_rsrc_remove_all_devices(void)
Remove all NetInt h/w devices from resource pool on the host.
Definition: ni_rsrc_api.cpp:2452
ni_getopt.h
Implementation of getopt() and getopt_long() for Windows environment.