libxcoder  5.2.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 
43 #ifdef _WIN32
44 #include "ni_getopt.h"
45 #define DEV_NAME_PREFIX "\\\\.\\Scsi"
46 #elif __linux__
47 #define DEV_NAME_PREFIX "/dev/nvme"
48 #elif __APPLE__
49 #define DEV_NAME_PREFIX "/dev/disk"
50 #endif
51 
52 /*!******************************************************************************
53  * \brief get the NVMe device's block device name (e.g. /dev/nvmeXnY)
54  *
55  * \param in whole device name passed in
56  * dev_name full block device name
57  *
58  * \return 0 if value device name is found, -1 otherwise
59  ******************************************************************************/
60 static int get_dev_name(const char *in, char *dev_name)
61 {
62  if (!in || !dev_name)
63  {
65  "Error: one or more of the given arguments is NULL.\n");
66  return -1;
67  }
68 
69  // for linux blk name (/dev/nvmeXnY)
70  // for apple blk name (/dev/diskX)
71  // for android blk name (/dev/nvmeXnY or /dev/block/nvmeXnY)
72  // for windows blk name (\\\\.\\PHYSICALDRIVEX)
73  strcpy(dev_name, in);
74 
75  return 0;
76 
77 }
78 
79 static void display_help(void)
80 {
81  printf("Usage: ni_rsrc_update [OPTION]\n"
82  "Update NetInt xcoder resource (encoders and decoders) status.\n"
83  "\n"
84  " -a device_file Create a resource entry for a newly active "
85  "transcoder card on host\n"
86  " -d device_file Delete the resource entry for a transcoder "
87  "card removed from host\n"
88  " -D Delete ALL the resource entries for transcoder "
89  "card on this host\n"
90  " -r Init transcoder card resource regardless "
91  "firmware release version\n"
92  " Default is to only init cards matching current "
93  "release version\n"
94  " -l Set loglevel of libxcoder API.\n"
95  " [none, fatal, error, info, debug, trace]\n"
96  " Default: info\n"
97  " -h Display this help and exit\n"
98  " -v Print version info and exit\n");
99 }
100 
101 int main(int argc, char *argv[])
102 {
103  int opt, rc = 0;
104  char char_dev_name[64];
105  int should_match_rev = 1;
106  int add_dev = 0; // default is to add(not delete) a resource
107  int del_dev = 0;
108  int delete_all = 0; // delete ALL the resources on the host
109  ni_log_level_t log_level = NI_LOG_INFO;
110 
111  if (argc == 1) {
112  display_help();
113  return 0;
114  }
115 
116  // arg handling
117  while ((opt = getopt(argc, argv, "hvrDa:d:l:")) != -1)
118  {
119  switch (opt)
120  {
121  case 'd':
122  rc = get_dev_name(optarg, char_dev_name);
123  if (rc)
124  {
125  fprintf(stderr, "ERROR: get_dev_name() returned %d\n", rc);
126  return EXIT_FAILURE;
127  }
128  del_dev = 1;
129  break;
130  case 'a':
131 #ifdef __linux__
132  rc = get_dev_name(optarg, char_dev_name);
133  if (rc)
134  {
135  fprintf(stderr, "ERROR: get_dev_name() returned %d\n", rc);
136  return EXIT_FAILURE;
137  }
138  add_dev = 1;
139 #endif
140  break;
141  case 'D':
142  delete_all = 1;
143  break;
144  case 'r':
145  should_match_rev = 0;
146  break;
147  case 'l':
148  log_level = arg_to_ni_log_level(optarg);
149  if (log_level != NI_LOG_INVALID)
150  {
151  ni_log_set_level(log_level);
152  } else {
153  fprintf(stderr, "FATAL: invalid log level selected: %s\n",
154  optarg);
155  exit(1);
156  }
157  break;
158  case 'v':
159  printf("Release ver: %s\n"
160  "API ver: %s\n"
161  "Date: %s\n"
162  "ID: %s\n",
165  return 0;
166  case 'h':
167  default:
168  display_help();
169  return 0;
170  }
171  }
172 
173  // check option
174  if (add_dev && (del_dev || delete_all))
175  {
176  fprintf(stderr, "Error: can not add and delete device at the same time\n\n");
177  display_help();
178  return 1;
179  }
180  if (!should_match_rev && !add_dev)
181  {
182  fprintf(stderr, "Error: -r option must be used with -a option\n\n");
183  display_help();
184  return 1;
185  }
186  if (add_dev)
187  {
188  rc = ni_rsrc_add_device(char_dev_name, should_match_rev);
189  if (rc)
190  printf("%s not added as transcoder.\n", char_dev_name);
191  else
192  printf("Added transcoder %s successfully.\n", char_dev_name);
193  return rc;
194  }
195  else if (delete_all)
196  {
198  if (rc)
199  printf("Error removing all transcoder resources.\n");
200  else
201  printf("Removing all transcoder resources successfully.\n");
202  return rc;
203  }
204  else if (del_dev)
205  {
206  rc = ni_rsrc_remove_device(char_dev_name);
207  if (rc)
208  printf("%s not removed as transcoder.\n", char_dev_name);
209  else
210  printf("Removed transcoder %s successfully.\n", char_dev_name);
211  return rc;
212  }
213  else
214  {
215  fprintf(stderr, "Error: ni_rsrc_update option must be used with -a or -b or -D option\n\n");
216  display_help();
217  return 1;
218  }
219 }
ni_log_level_t
ni_log_level_t
Definition: ni_log.h:55
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:2203
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:95
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:2523
ni_log.h
Logging definitions.
NI_LOG_INFO
@ NI_LOG_INFO
Definition: ni_log.h:61
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:60
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:57
optarg
char * optarg
Definition: ni_getopt.c:33
main
int main(int argc, char *argv[])
Definition: ni_rsrc_update.c:101
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:112
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:2407
ni_getopt.h
Implementation of getopt() and getopt_long() for Windows environment.