pn_get_deviceid.cpp 5.75 KB
Newer Older
1
/*
2 3
 * ProviewR   Open Source Process Control.
 * Copyright (C) 2005-2018 SSAB EMEA AB.
Claes Sjofors's avatar
Claes Sjofors committed
4
 *
5
 * This file is part of ProviewR.
Claes Sjofors's avatar
Claes Sjofors committed
6
 *
7 8 9
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 2 of
Claes Sjofors's avatar
Claes Sjofors committed
10 11
 * the License, or (at your option) any later version.
 *
12 13 14
 * This program is distributed in the hope that it will be useful
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claes Sjofors's avatar
Claes Sjofors committed
15 16
 * GNU General Public License for more details.
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with ProviewR. If not, see <http://www.gnu.org/licenses/>
Claes Sjofors's avatar
Claes Sjofors committed
19
 *
20 21
 * Linking ProviewR statically or dynamically with other modules is
 * making a combined work based on ProviewR. Thus, the terms and
22
 * conditions of the GNU General Public License cover the whole
Claes Sjofors's avatar
Claes Sjofors committed
23 24 25
 * combination.
 *
 * In addition, as a special exception, the copyright holders of
26 27 28
 * ProviewR give you permission to, from the build function in the
 * ProviewR Configurator, combine ProviewR with modules generated by the
 * ProviewR PLC Editor to a PLC program, regardless of the license
Claes Sjofors's avatar
Claes Sjofors committed
29
 * terms of these modules. You may copy and distribute the resulting
30 31
 * combined work under the terms of your choice, provided that every
 * copy of the combined work is accompanied by a complete copy of
32
 * the source code of ProviewR (the version used to produce the
33
 * combined work), being distributed under the terms of the GNU
Claes Sjofors's avatar
Claes Sjofors committed
34
 * General Public License plus this exception.
Claes Sjofors's avatar
Claes Sjofors committed
35 36 37
 **/

//
38 39 40 41
// Search for DeviceID and VendorID in gsdml files and extract ProductFamly and
// TextInfo.
// The content should be written in $pwr_exe/profinet_deviceid.dat which is used
// by the
Claes Sjofors's avatar
Claes Sjofors committed
42 43 44 45 46
// Proview Viewer to show info for a device.
//

#include <stdlib.h>

47
#include <fstream>
Claes Sjofors's avatar
Claes Sjofors committed
48

49
#include "co_dcli.h"
50
#include "co_string.h"
Claes Sjofors's avatar
Claes Sjofors committed
51

52
void parse_file(char* filename);
Claes Sjofors's avatar
Claes Sjofors committed
53

54
int main(int argc, char* argv[])
Claes Sjofors's avatar
Claes Sjofors committed
55 56
{
  pwr_tFileName file_spec, found_file;
57

Claes Sjofors's avatar
Claes Sjofors committed
58 59
  pwr_tStatus sts;

60
  if (argc < 2 || streq(argv[1], "-h")) {
61 62
    printf("\nUsage: pn_get_deviceid \"file-pattern\" > "
           "profinet_deviceid.dat\n\n");
Claes Sjofors's avatar
Claes Sjofors committed
63 64 65
    exit(0);
  }

66
  dcli_translate_filename(file_spec, argv[1]);
Claes Sjofors's avatar
Claes Sjofors committed
67

68 69
  sts = dcli_search_file(file_spec, found_file, DCLI_DIR_SEARCH_INIT);
  while (ODD(sts)) {
Claes Sjofors's avatar
Claes Sjofors committed
70
    printf("# Processing file: %s\n", found_file);
71
    parse_file(found_file);
Claes Sjofors's avatar
Claes Sjofors committed
72

73 74 75
    sts = dcli_search_file(file_spec, found_file, DCLI_DIR_SEARCH_NEXT);
  }
  dcli_search_file(file_spec, found_file, DCLI_DIR_SEARCH_END);
Claes Sjofors's avatar
Claes Sjofors committed
76 77
}

78
void parse_file(char* filename)
Claes Sjofors's avatar
Claes Sjofors committed
79
{
80
  std::ifstream fp;
Claes Sjofors's avatar
Claes Sjofors committed
81 82
  pwr_tFileName fname;
  char line[1024];
83 84
  unsigned int deviceid = 0;
  unsigned int vendorid = 0;
Claes Sjofors's avatar
Claes Sjofors committed
85 86 87 88
  char infotextid[500] = "";
  char infotext[500] = "";
  char family[500] = "";

89
  dcli_translate_filename(fname, filename);
Claes Sjofors's avatar
Claes Sjofors committed
90

91 92 93
  fp.open(fname);
  if (!fp) {
    printf("# Unable to open file\n");
Claes Sjofors's avatar
Claes Sjofors committed
94 95 96 97 98 99 100 101 102 103 104
    exit(0);
  }

  int in_deviceid = 0;
  int in_devicefunction = 0;
  int in_text = 0;
  int deviceid_found = 0;
  int family_found = 0;
  int vendorid_found = 0;
  int infotextid_found = 0;
  int infotext_found = 0;
105 106
  while (fp.getline(line, sizeof(line))) {
    char* s;
Claes Sjofors's avatar
Claes Sjofors committed
107

108 109 110
    if (!in_deviceid) {
      if ((s = strstr(line, "<DeviceIdentity")))
        in_deviceid = 1;
Claes Sjofors's avatar
Claes Sjofors committed
111 112
    }

113 114 115 116 117 118 119 120 121 122 123
    if (in_deviceid) {
      if (!deviceid_found) {
        if ((s = strstr(line, "DeviceID"))) {
          for (s += 9; *s; s++) {
            if (*s == '\"') {
              sscanf(s + 3, "%x", &deviceid);
              deviceid_found = 1;
              break;
            }
          }
        }
Claes Sjofors's avatar
Claes Sjofors committed
124
      }
125 126 127 128 129 130 131 132 133 134
      if (!vendorid_found) {
        if ((s = strstr(line, "VendorID"))) {
          for (s += 9; *s; s++) {
            if (*s == '\"') {
              sscanf(s + 3, "%x", &vendorid);
              vendorid_found = 1;
              break;
            }
          }
        }
Claes Sjofors's avatar
Claes Sjofors committed
135
      }
136 137 138 139 140 141 142 143 144 145 146 147
      if (!infotextid_found) {
        if ((s = strstr(line, "<InfoText"))) {
          for (s += 16; *s; s++) {
            if (*s == '\"') {
              strncpy(infotextid, s + 1, sizeof(infotextid));
              if ((s = strchr(infotextid, '\"')))
                *s = 0;
              infotextid_found = 1;
              break;
            }
          }
        }
Claes Sjofors's avatar
Claes Sjofors committed
148
      }
149 150 151 152 153 154 155 156 157 158 159 160
      if (strstr(line, "</DeviceIdentity>")) {
        in_deviceid = 0;

        printf("%u %u\n", vendorid, deviceid);
        if (!deviceid_found)
          printf("# DeviceID not found\n");
        if (!vendorid_found)
          printf("# VendorID not found\n");
        if (!infotextid_found)
          printf("# TextId not found\n");
        if (!(deviceid_found && vendorid_found && infotextid_found))
          return;
Claes Sjofors's avatar
Claes Sjofors committed
161 162 163
      }
    }

164 165 166
    if (!in_devicefunction) {
      if ((s = strstr(line, "<DeviceFunction")))
        in_devicefunction = 1;
Claes Sjofors's avatar
Claes Sjofors committed
167 168
    }

169 170 171 172 173 174 175 176 177 178 179 180 181 182
    if (in_devicefunction) {
      if (!family_found) {
        if ((s = strstr(line, "ProductFamily"))) {
          for (s += 14; *s; s++) {
            if (*s == '\"') {
              strncpy(family, s + 1, sizeof(family));
              if ((s = strchr(family, '\"')))
                *s = 0;
              family_found = 1;
              in_devicefunction = 0;
              break;
            }
          }
        }
Claes Sjofors's avatar
Claes Sjofors committed
183 184
      }
    }
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
    if (infotextid_found && !infotext_found) {
      if (strstr(line, "<Text") && strstr(line, infotextid))
        in_text = 1;

      if (in_text) {
        if ((s = strstr(line, "Value"))) {
          for (s += 6; *s; s++) {
            if (*s == '\"') {
              strncpy(infotext, s + 1, sizeof(infotext));
              if ((s = strchr(infotext, '\"')))
                *s = 0;
              infotext_found = 1;
              in_text = 0;
              break;
            }
          }
        }
Claes Sjofors's avatar
Claes Sjofors committed
202 203 204
      }
    }
  }
205
  printf("%s\n%s\n", family, infotext);
Claes Sjofors's avatar
Claes Sjofors committed
206 207

  fp.close();
208
}